csgeom/math3d.h
Go to the documentation of this file.00001 /* 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 00033 #include "csextern.h" 00034 00035 #include "csgeom/vector3.h" 00036 #include "csgeom/plane3.h" 00037 #include "csgeom/plane2.h" 00038 #include "csgeom/segment.h" 00039 #include "iutil/dbghelp.h" 00040 00041 class csDVector3; 00042 class csPoly3D; 00043 class csBox3; 00044 00045 inline float fSqr (float f) 00046 { 00047 return f * f; 00048 } 00049 00054 class CS_CSGEOM_EXPORT csMath3 00055 { 00056 public: 00069 static int WhichSide3D (const csVector3& p, 00070 const csVector3& v1, const csVector3& v2) 00071 { 00072 // float s = p * (v1%v2); (original expression: expanded to the below:) 00073 float s = p.x*(v1.y*v2.z-v1.z*v2.y) + p.y*(v1.z*v2.x-v1.x*v2.z) + 00074 p.z*(v1.x*v2.y-v1.y*v2.x); 00075 if (s < 0) return 1; 00076 else if (s > 0) return -1; 00077 else return 0; 00078 } 00079 00085 static bool Visible (const csVector3& p, const csVector3& t1, 00086 const csVector3& t2, const csVector3& t3); 00087 00093 static bool Visible (const csVector3& p, const csPlane3& pl) 00094 { return pl.Classify (p) <= 0; } 00095 00103 static bool FindIntersection(const csVector3 tri1[3], 00104 const csVector3 tri2[3], 00105 csVector3 line[2]); 00106 00116 static void Between (const csVector3& v1, const csVector3& v2, csVector3& v, 00117 float pct, float wid); 00118 00125 static void SetMinMax (const csVector3& v, 00126 csVector3& min, csVector3& max) 00127 { 00128 if (v.x > max.x) max.x = v.x; else if (v.x < min.x ) min.x = v.x; 00129 if (v.y > max.y) max.y = v.y; else if (v.y < min.y ) min.y = v.y; 00130 if (v.z > max.z) max.z = v.z; else if (v.z < min.z ) min.z = v.z; 00131 } 00132 00138 inline static float DoubleArea3 (const csVector3 &a, const csVector3 &b, 00139 const csVector3 &c) 00140 { 00141 csVector3 v1 = b - a; 00142 csVector3 v2 = c - a; 00143 return (v1 % v2).Norm (); 00144 } 00145 00149 inline static float Direction3 (const csVector3 &a, const csVector3 &b, 00150 const csVector3 &c) 00151 { 00152 csVector3 v1 = b - a; 00153 csVector3 v2 = c - a; 00154 return ((v1.y * v2.z + v1.z * v2.x + v1.x * v2.y) - 00155 (v1.y * v2.x + v1.x * v2.z + v1.z * v2.y)); 00156 } 00157 00163 inline static void CalcNormal (csVector3& norm, const csVector3& v1, 00164 const csVector3& v2, const csVector3& v3) 00165 { 00166 norm = (v1-v2)%(v1-v3); 00167 } 00168 00174 static void CalcNormal (csVector3& norm, 00175 const csVector3& v, const csVector3& u) 00176 { norm = u%v; /* NOT v%u - vertexes are defined clockwise */ } 00177 00184 static void CalcPlane (const csVector3& v1, const csVector3& v2, 00185 const csVector3& v3, csVector3& normal, float& D) 00186 { 00187 CalcNormal (normal, v1, v2, v3); 00188 D = - (normal * v1); 00189 } 00190 00197 static bool PlanesEqual (const csPlane3& p1, const csPlane3& p2) 00198 { 00199 return ( ( p1.norm - p2.norm) < (float).001 ) && 00200 ( ABS (p1.DD-p2.DD) < (float).001 ); 00201 } 00202 00208 static bool PlanesClose (const csPlane3& p1, const csPlane3& p2); 00209 00217 static int OuterPlanes (const csBox3& box1, const csBox3& box2, 00218 csPlane3* planes); 00219 00227 static int FindObserverSides (const csBox3& box1, const csBox3& box2, 00228 int* sides); 00229 00235 static void SpherePosition (float angle_xz, float angle_vert, 00236 csVector3& pos); 00237 }; 00238 00243 class CS_CSGEOM_EXPORT csSquaredDist 00244 { 00245 public: 00247 static float PointPoint (const csVector3& p1, const csVector3& p2) 00248 { return fSqr (p1.x - p2.x) + fSqr (p1.y - p2.y) + fSqr (p1.z - p2.z); } 00249 00251 static float PointLine (const csVector3& p, 00252 const csVector3& l1, const csVector3& l2); 00253 00255 static float PointPlane (const csVector3& p, const csPlane3& plane) 00256 { float r = plane.Classify (p); return r * r; } 00257 00264 static float PointPoly (const csVector3& p, csVector3 *V, int n, 00265 const csPlane3& plane, float sqdist = -1); 00266 }; 00267 00273 class CS_CSGEOM_EXPORT csIntersect3 00274 { 00275 public: 00282 static bool IntersectPolygon (const csPlane3& plane, csPoly3D* poly, 00283 csSegment3& segment); 00284 00294 static int IntersectSegment (csPlane3* planes, int num_planes, 00295 csSegment3& seg); 00296 00302 static bool IntersectTriangle (const csVector3& tr1, 00303 const csVector3& tr2, const csVector3& tr3, 00304 const csSegment3& seg, csVector3& isect); 00305 00312 static bool Planes( 00313 const csVector3& u, const csVector3& v, 00314 const csPlane3* planes, int length, 00315 csVector3& isect, float& dist); 00316 00321 static bool Plane ( 00322 const csVector3& u, const csVector3& v, 00323 const csVector3& normal, const csVector3& a, // plane 00324 csVector3& isect, float& dist); // intersection point 00325 00347 static bool Plane ( 00348 const csVector3& u, const csVector3& v, 00349 const csPlane3& p, // plane Ax+By+Cz+D=0 00350 csVector3& isect, // intersection point 00351 float& dist); // distance from u to isect 00352 00358 static bool Planes (const csPlane3& p1, const csPlane3& p2, 00359 const csPlane3& p3, csVector3& isect); 00360 00367 static bool PlaneXPlane (const csPlane3& p1, float x2, csPlane2& isect); 00368 00375 static bool PlaneYPlane (const csPlane3& p1, float y2, csPlane2& isect); 00376 00383 static bool PlaneZPlane (const csPlane3& p1, float z2, csPlane2& isect); 00384 00391 static bool PlaneAxisPlane (const csPlane3& p1, int nr, float pos, 00392 csPlane2& isect) 00393 { 00394 switch (nr) 00395 { 00396 case 0: return PlaneXPlane (p1, pos, isect); 00397 case 1: return PlaneYPlane (p1, pos, isect); 00398 case 2: return PlaneZPlane (p1, pos, isect); 00399 } 00400 return false; 00401 } 00402 00409 static float Z0Plane ( 00410 const csVector3& v1, const csVector3& v2, 00411 csVector3& isect); // intersection point 00412 00419 static float Z0Plane ( 00420 const csSegment3& uv, 00421 csVector3& isect) // intersection point 00422 { 00423 return Z0Plane (uv.Start (), uv.End (), isect); 00424 } 00425 00432 static float ZPlane (float zval, // plane z = zval 00433 const csVector3& u, const csVector3& v, 00434 csVector3& isect); // intersection point 00435 00442 static float ZPlane (float zval, // plane z = zval 00443 const csSegment3& uv, 00444 csVector3& isect) // intersection point 00445 { 00446 return ZPlane (zval, uv.Start (), uv.End (), isect); 00447 } 00448 00453 static float XFrustum ( 00454 float A, const csVector3& u, const csVector3& v, csVector3& isect); 00455 00460 static float XFrustum ( 00461 float A, const csSegment3& uv, csVector3& isect) 00462 { 00463 return XFrustum (A, uv.Start (), uv.End (), isect); 00464 } 00465 00470 static float YFrustum ( 00471 float B, const csVector3& u, const csVector3& v, csVector3& isect); 00472 00477 static float YFrustum ( 00478 float B, const csSegment3& uv, csVector3& isect) 00479 { 00480 return YFrustum (B, uv.Start (), uv.End (), isect); 00481 } 00482 00492 static int BoxSegment (const csBox3& box, const csSegment3& segment, 00493 csVector3& isect, float* pr = 0); 00494 00504 static bool BoxFrustum (const csBox3& box, csPlane3* frustum, 00505 uint32 inClipMask, uint32& outClipMask); 00506 00511 static bool BoxSphere (const csBox3& box, const csVector3& center, 00512 float sqradius); 00513 }; 00514 00519 class CS_CSGEOM_EXPORT csGeomDebugHelper : public iDebugHelper 00520 { 00521 public: 00522 csGeomDebugHelper (); 00523 virtual ~csGeomDebugHelper (); 00524 00525 SCF_DECLARE_IBASE; 00526 virtual int GetSupportedTests () const 00527 { 00528 return CS_DBGHELP_UNITTEST; 00529 } 00530 virtual csPtr<iString> UnitTest (); 00531 virtual csPtr<iString> StateTest () 00532 { 00533 return 0; 00534 } 00535 virtual csTicks Benchmark (int /*num_iterations*/) 00536 { 00537 return 0; 00538 } 00539 virtual csPtr<iString> Dump () 00540 { 00541 return 0; 00542 } 00543 virtual void Dump (iGraphics3D* /*g3d*/) 00544 { 00545 } 00546 virtual bool DebugCommand (const char*) 00547 { 00548 return false; 00549 } 00550 }; 00551 00554 #endif // __CS_MATH3D_H__ 00555
Generated for Crystal Space by doxygen 1.2.18