|
math3d.h00001 /* 00002 Copyright (C) 1998-2001 by Jorrit Tyberghein 00003 Largely rewritten by Ivan Avramovic <ivan@avramovic.com> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public 00016 License along with this library; if not, write to the Free 00017 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00018 */ 00019 00020 #ifndef __CS_MATH3D_H__ 00021 #define __CS_MATH3D_H__ 00022 00023 #ifndef __CS_CSSYSDEFS_H__ 00024 #error "cssysdef.h must be included in EVERY source file!" 00025 #endif 00026 00027 #include "csgeom/vector3.h" 00028 #include "csgeom/plane3.h" 00029 #include "csgeom/plane2.h" 00030 #include "csgeom/segment.h" 00031 00032 class csDVector3; 00033 class csPoly3D; 00034 class csBox3; 00035 00036 inline float fSqr (float f) 00037 { 00038 return f * f; 00039 } 00040 00045 class csMath3 00046 { 00047 public: 00060 static int WhichSide3D (const csVector3& p, 00061 const csVector3& v1, const csVector3& v2) 00062 { 00063 // float s = p * (v1%v2); (original expression: expanded to the below:) 00064 float s = p.x*(v1.y*v2.z-v1.z*v2.y) + p.y*(v1.z*v2.x-v1.x*v2.z) + 00065 p.z*(v1.x*v2.y-v1.y*v2.x); 00066 if (s < 0) return 1; 00067 else if (s > 0) return -1; 00068 else return 0; 00069 } 00070 00076 static bool Visible (const csVector3& p, const csVector3& t1, 00077 const csVector3& t2, const csVector3& t3); 00078 00084 static bool Visible (const csVector3& p, const csPlane3& pl) 00085 { return pl.Classify (p) <= 0; } 00086 00094 static bool FindIntersection(const csVector3 tri1[3], 00095 const csVector3 tri2[3], 00096 csVector3 line[2]); 00097 00107 static void Between (const csVector3& v1, const csVector3& v2, csVector3& v, 00108 float pct, float wid); 00109 00116 static void SetMinMax (const csVector3& v, 00117 csVector3& min, csVector3& max) 00118 { 00119 if (v.x > max.x) max.x = v.x; else if (v.x < min.x ) min.x = v.x; 00120 if (v.y > max.y) max.y = v.y; else if (v.y < min.y ) min.y = v.y; 00121 if (v.z > max.z) max.z = v.z; else if (v.z < min.z ) min.z = v.z; 00122 } 00123 00129 inline static float Area3 (const csVector3 &a, const csVector3 &b, 00130 const csVector3 &c) 00131 { 00132 csVector3 v1 = b - a; 00133 csVector3 v2 = c - a; 00134 return ((v1.y * v2.z + v1.z * v2.x + v1.x * v2.y) - 00135 (v1.y * v2.x + v1.x * v2.z + v1.z * v2.y)); 00136 } 00137 00143 inline static void CalcNormal (csVector3& norm, const csVector3& v1, 00144 const csVector3& v2, const csVector3& v3) 00145 { 00146 norm = (v1-v2)%(v1-v3); 00147 } 00148 00154 static void CalcNormal (csVector3& norm, 00155 const csVector3& v, const csVector3& u) 00156 { norm = u%v; /* NOT v%u - vertexes are defined clockwise */ } 00157 00164 static void CalcPlane (const csVector3& v1, const csVector3& v2, 00165 const csVector3& v3, csVector3& normal, float& D) 00166 { 00167 CalcNormal (normal, v1, v2, v3); 00168 D = - (normal * v1); 00169 } 00170 00177 static bool PlanesEqual (const csPlane3& p1, const csPlane3& p2) 00178 { 00179 return ( ( p1.norm - p2.norm) < (float).001 ) && 00180 ( ABS (p1.DD-p2.DD) < (float).001 ); 00181 } 00182 00188 static bool PlanesClose (const csPlane3& p1, const csPlane3& p2); 00189 00197 static int OuterPlanes (const csBox3& box1, const csBox3& box2, 00198 csPlane3* planes); 00199 00207 static int FindObserverSides (const csBox3& box1, const csBox3& box2, 00208 int* sides); 00209 00215 static void SpherePosition (float angle_xz, float angle_vert, 00216 csVector3& pos); 00217 }; 00218 00223 class csSquaredDist 00224 { 00225 public: 00227 static float PointPoint (const csVector3& p1, const csVector3& p2) 00228 { return fSqr (p1.x - p2.x) + fSqr (p1.y - p2.y) + fSqr (p1.z - p2.z); } 00229 00231 static float PointLine (const csVector3& p, 00232 const csVector3& l1, const csVector3& l2); 00233 00235 static float PointPlane (const csVector3& p, const csPlane3& plane) 00236 { float r = plane.Classify (p); return r * r; } 00237 00244 static float PointPoly (const csVector3& p, csVector3 *V, int n, 00245 const csPlane3& plane, float sqdist = -1); 00246 }; 00247 00253 class csIntersect3 00254 { 00255 public: 00262 static bool IntersectPolygon (const csPlane3& plane, csPoly3D* poly, 00263 csSegment3& segment); 00264 00274 static int IntersectSegment (csPlane3* planes, int num_planes, 00275 csSegment3& seg); 00276 00282 static bool IntersectTriangle (const csVector3& tr1, 00283 const csVector3& tr2, const csVector3& tr3, 00284 const csSegment3& seg, csVector3& isect); 00285 00290 static bool Plane ( 00291 const csVector3& u, const csVector3& v, 00292 const csVector3& normal, const csVector3& a, // plane 00293 csVector3& isect, float& dist); // intersection point 00294 00303 static bool Plane ( 00304 const csVector3& u, const csVector3& v, 00305 const csPlane3& p, // plane Ax+By+Cz+D=0 00306 csVector3& isect, // intersection point 00307 float& dist); // distance from u to isect 00308 00314 static bool Planes (const csPlane3& p1, const csPlane3& p2, 00315 const csPlane3& p3, csVector3& isect); 00316 00323 static bool PlaneXPlane (const csPlane3& p1, float x2, csPlane2& isect); 00324 00331 static bool PlaneYPlane (const csPlane3& p1, float y2, csPlane2& isect); 00332 00339 static bool PlaneZPlane (const csPlane3& p1, float z2, csPlane2& isect); 00340 00347 static bool PlaneAxisPlane (const csPlane3& p1, int nr, float pos, 00348 csPlane2& isect) 00349 { 00350 switch (nr) 00351 { 00352 case 0: return PlaneXPlane (p1, pos, isect); 00353 case 1: return PlaneYPlane (p1, pos, isect); 00354 case 2: return PlaneZPlane (p1, pos, isect); 00355 } 00356 return false; 00357 } 00358 00365 static float Z0Plane ( 00366 const csVector3& v1, const csVector3& v2, 00367 csVector3& isect); // intersection point 00368 00375 static float Z0Plane ( 00376 const csSegment3& uv, 00377 csVector3& isect) // intersection point 00378 { 00379 return Z0Plane (uv.Start (), uv.End (), isect); 00380 } 00381 00388 static float ZPlane (float zval, // plane z = zval 00389 const csVector3& u, const csVector3& v, 00390 csVector3& isect); // intersection point 00391 00398 static float ZPlane (float zval, // plane z = zval 00399 const csSegment3& uv, 00400 csVector3& isect) // intersection point 00401 { 00402 return ZPlane (zval, uv.Start (), uv.End (), isect); 00403 } 00404 00409 static float XFrustum ( 00410 float A, const csVector3& u, const csVector3& v, csVector3& isect); 00411 00416 static float XFrustum ( 00417 float A, const csSegment3& uv, csVector3& isect) 00418 { 00419 return XFrustum (A, uv.Start (), uv.End (), isect); 00420 } 00421 00426 static float YFrustum ( 00427 float B, const csVector3& u, const csVector3& v, csVector3& isect); 00428 00433 static float YFrustum ( 00434 float B, const csSegment3& uv, csVector3& isect) 00435 { 00436 return YFrustum (B, uv.Start (), uv.End (), isect); 00437 } 00438 00447 static int BoxSegment (const csBox3& box, const csSegment3& segment, 00448 csVector3& isect, float* pr = NULL); 00449 }; 00450 00451 #endif // __CS_MATH3D_H__ Generated for Crystal Space by doxygen 1.2.5 written by Dimitri van Heesch, ©1997-2000 |