Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages
math3d.h
Go to the documentation of this file.00001 /* 00002 Copyright (C) 1998-2005 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 00029 #include "csextern.h" 00030 00031 #include "csgeom/box.h" 00032 #include "csgeom/frustum.h" 00033 #include "csgeom/plane3.h" 00034 #include "csgeom/segment.h" 00035 #include "csgeom/vector3.h" 00036 #include "csutil/ref.h" 00037 #include "csutil/scf_implementation.h" 00038 00039 #include "iutil/dbghelp.h" 00040 #include "iutil/string.h" 00041 00042 class csPlane2; 00043 class csPoly3D; 00044 00049 class CS_CRYSTALSPACE_EXPORT csMath3 00050 { 00051 public: 00064 static int WhichSide3D (const csVector3& p, 00065 const csVector3& v1, const csVector3& v2) 00066 { 00067 // float s = p * (v1%v2); (original expression: expanded to the below:) 00068 float s = p.x*(v1.y*v2.z-v1.z*v2.y) + p.y*(v1.z*v2.x-v1.x*v2.z) + 00069 p.z*(v1.x*v2.y-v1.y*v2.x); 00070 if (s < 0) return 1; 00071 else if (s > 0) return -1; 00072 else return 0; 00073 } 00074 00080 static bool Visible (const csVector3& p, const csVector3& t1, 00081 const csVector3& t2, const csVector3& t3); 00082 00088 static bool Visible (const csVector3& p, const csPlane3& pl) 00089 { return pl.Classify (p) <= 0; } 00090 00100 static void Between (const csVector3& v1, const csVector3& v2, csVector3& v, 00101 float pct, float wid); 00102 00109 static void SetMinMax (const csVector3& v, 00110 csVector3& min, csVector3& max) 00111 { 00112 if (v.x > max.x) max.x = v.x; else if (v.x < min.x ) min.x = v.x; 00113 if (v.y > max.y) max.y = v.y; else if (v.y < min.y ) min.y = v.y; 00114 if (v.z > max.z) max.z = v.z; else if (v.z < min.z ) min.z = v.z; 00115 } 00116 00122 inline static float DoubleArea3 (const csVector3 &a, const csVector3 &b, 00123 const csVector3 &c) 00124 { 00125 csVector3 v1 = b - a; 00126 csVector3 v2 = c - a; 00127 return (v1 % v2).Norm (); 00128 } 00129 00133 inline static float Direction3 (const csVector3 &a, const csVector3 &b, 00134 const csVector3 &c) 00135 { 00136 csVector3 v1 = b - a; 00137 csVector3 v2 = c - a; 00138 return ((v1.y * v2.z + v1.z * v2.x + v1.x * v2.y) - 00139 (v1.y * v2.x + v1.x * v2.z + v1.z * v2.y)); 00140 } 00141 00147 inline static void CalcNormal (csVector3& norm, const csVector3& v1, 00148 const csVector3& v2, const csVector3& v3) 00149 { 00150 norm = (v1-v2)%(v1-v3); 00151 } 00152 00158 static void CalcNormal (csVector3& norm, 00159 const csVector3& v, const csVector3& u) 00160 { norm = u%v; /* NOT v%u - vertexes are defined clockwise */ } 00161 00168 static void CalcPlane (const csVector3& v1, const csVector3& v2, 00169 const csVector3& v3, csVector3& normal, float& D) 00170 { 00171 CalcNormal (normal, v1, v2, v3); 00172 D = - (normal * v1); 00173 } 00174 00181 static bool PlanesEqual (const csPlane3& p1, const csPlane3& p2) 00182 { 00183 return ( ( p1.norm - p2.norm) < (float).001 ) && 00184 ( ABS (p1.DD-p2.DD) < (float).001 ); 00185 } 00186 00192 static bool PlanesClose (const csPlane3& p1, const csPlane3& p2); 00193 00201 static int OuterPlanes (const csBox3& box1, const csBox3& box2, 00202 csPlane3* planes); 00203 00211 static int FindObserverSides (const csBox3& box1, const csBox3& box2, 00212 int* sides); 00213 00219 static void SpherePosition (float angle_xz, float angle_vert, 00220 csVector3& pos); 00221 }; 00222 00227 class CS_CRYSTALSPACE_EXPORT csSquaredDist 00228 { 00229 public: 00231 static float PointPoint (const csVector3& p1, const csVector3& p2) 00232 { 00233 csVector3 d = (p1-p2); 00234 return d*d; 00235 } 00236 00238 static float PointLine (const csVector3& p, 00239 const csVector3& l1, const csVector3& l2); 00240 00242 static float PointPlane (const csVector3& p, const csPlane3& plane) 00243 { float r = plane.Classify (p); return r * r; } 00244 00251 static float PointPoly (const csVector3& p, csVector3 *V, int n, 00252 const csPlane3& plane, float sqdist = -1); 00253 }; 00254 00260 class CS_CRYSTALSPACE_EXPORT csIntersect3 00261 { 00262 private: 00263 static bool BoxPlaneInternal (const csVector3& normal, 00264 const csVector3& vert, const csVector3& boxhalfsize); 00265 00266 public: 00273 static bool PlanePolygon (const csPlane3& plane, csPoly3D* poly, 00274 csSegment3& segment); 00275 00285 static int SegmentFrustum (csPlane3* planes, int num_planes, 00286 csSegment3& seg); 00287 00293 static bool SegmentTriangle (const csSegment3& seg, 00294 const csVector3& tr1, 00295 const csVector3& tr2, const csVector3& tr3, 00296 csVector3& isect); 00297 00304 static bool SegmentPolygon (const csSegment3& seg, const csPoly3D& poly, 00305 const csPlane3& poly_plane, csVector3& isect); 00306 00315 static bool SegmentPlanes ( 00316 const csVector3& u, const csVector3& v, 00317 const csPlane3* planes, int length, 00318 csVector3& isect, float& dist); 00319 00324 static bool SegmentPlane ( 00325 const csVector3& u, const csVector3& v, 00326 const csVector3& normal, const csVector3& a, // plane 00327 csVector3& isect, float& dist); // intersection point 00328 00350 static bool SegmentPlane ( 00351 const csVector3& u, const csVector3& v, 00352 const csPlane3& p, // plane Ax+By+Cz+D=0 00353 csVector3& isect, // intersection point 00354 float& dist); // distance from u to isect 00355 00361 static bool ThreePlanes (const csPlane3& p1, const csPlane3& p2, 00362 const csPlane3& p3, csVector3& isect); 00363 00370 static bool PlaneXPlane (const csPlane3& p1, float x2, csPlane2& isect); 00371 00378 static bool PlaneYPlane (const csPlane3& p1, float y2, csPlane2& isect); 00379 00386 static bool PlaneZPlane (const csPlane3& p1, float z2, csPlane2& isect); 00387 00394 static bool PlaneAxisPlane (const csPlane3& p1, int nr, float pos, 00395 csPlane2& isect) 00396 { 00397 switch (nr) 00398 { 00399 case 0: return PlaneXPlane (p1, pos, isect); 00400 case 1: return PlaneYPlane (p1, pos, isect); 00401 case 2: return PlaneZPlane (p1, pos, isect); 00402 } 00403 return false; 00404 } 00405 00412 static float SegmentZ0Plane ( 00413 const csVector3& v1, const csVector3& v2, 00414 csVector3& isect); // intersection point 00415 00422 static float SegmentZ0Plane ( 00423 const csSegment3& uv, 00424 csVector3& isect) // intersection point 00425 { 00426 return SegmentZ0Plane (uv.Start (), uv.End (), isect); 00427 } 00428 00435 static float SegmentXPlane ( 00436 const csVector3& u, const csVector3& v, 00437 float xval, 00438 csVector3& isect); // intersection point 00439 00446 static float SegmentXPlane ( 00447 const csSegment3& uv, 00448 float xval, 00449 csVector3& isect) // intersection point 00450 { 00451 return SegmentXPlane (uv.Start (), uv.End (), xval, isect); 00452 } 00453 00460 static float SegmentYPlane ( 00461 const csVector3& u, const csVector3& v, 00462 float yval, // plane y = yval 00463 csVector3& isect); // intersection point 00464 00471 static float SegmentYPlane ( 00472 const csSegment3& uv, 00473 float yval, // plane y = yval 00474 csVector3& isect) // intersection point 00475 { 00476 return SegmentYPlane (uv.Start (), uv.End (), yval, isect); 00477 } 00478 00485 static float SegmentZPlane ( 00486 const csVector3& u, const csVector3& v, 00487 float zval, // plane z = zval 00488 csVector3& isect); // intersection point 00489 00496 static float SegmentZPlane ( 00497 const csSegment3& uv, 00498 float zval, // plane z = zval 00499 csVector3& isect) // intersection point 00500 { 00501 return SegmentZPlane (uv.Start (), uv.End (), zval, isect); 00502 } 00503 00510 static float SegmentAxisPlane (const csVector3& u, const csVector3& v, 00511 int nr, float pos, csVector3& isect) 00512 { 00513 switch (nr) 00514 { 00515 case 0: return SegmentXPlane (u, v, pos, isect); 00516 case 1: return SegmentYPlane (u, v, pos, isect); 00517 case 2: return SegmentZPlane (u, v, pos, isect); 00518 } 00519 return 0.0; 00520 } 00521 00526 static float SegmentXFrustum ( 00527 const csVector3& u, const csVector3& v, float A, csVector3& isect); 00528 00533 static float SegmentXFrustum ( 00534 const csSegment3& uv, float A, csVector3& isect) 00535 { 00536 return SegmentXFrustum (uv.Start (), uv.End (), A, isect); 00537 } 00538 00543 static float SegmentYFrustum ( 00544 const csVector3& u, const csVector3& v, float B, csVector3& isect); 00545 00550 static float SegmentYFrustum ( 00551 const csSegment3& uv, float B, csVector3& isect) 00552 { 00553 return SegmentYFrustum (uv.Start (), uv.End (), B, isect); 00554 } 00555 00565 static int BoxSegment (const csBox3& box, const csSegment3& segment, 00566 csVector3& isect, float* pr = 0); 00567 00577 static bool BoxFrustum (const csBox3& box, csPlane3* frustum, 00578 uint32 inClipMask, uint32& outClipMask); 00579 00584 static bool BoxSphere (const csBox3& box, const csVector3& center, 00585 float sqradius); 00586 00590 static bool BoxPlane (const csBox3& box, const csPlane3& plane); 00591 00596 static bool BoxPlane (const csBox3& box, const csVector3& normal, 00597 const csVector3& vert); 00598 00602 static bool BoxTriangle (const csBox3& box, 00603 const csVector3& tri0, const csVector3& tri1, const csVector3& tri2); 00604 00608 static bool BoxBox (const csBox3& box1, const csBox3& box2) 00609 { 00610 return box1.TestIntersect (box2); 00611 } 00612 00616 static csPtr<csFrustum> FrustumFrustum (const csFrustum& f1, 00617 const csFrustum& f2) 00618 { 00619 return f1.Intersect (f2); 00620 } 00621 00625 static csPtr<csFrustum> FrustumFrustum (const csFrustum& f1, 00626 csVector3* poly, int num) 00627 { 00628 return f1.Intersect (poly, num); 00629 } 00630 00637 static bool TriangleTriangle (const csVector3 tri1[3], 00638 const csVector3 tri2[3]); 00639 00649 static bool TriangleTriangle (const csVector3 tri1[3], 00650 const csVector3 tri2[3], 00651 csSegment3& isectline, bool& coplanar); 00652 }; 00653 00658 class CS_CRYSTALSPACE_EXPORT csGeomDebugHelper : 00659 public scfImplementation1<csGeomDebugHelper, iDebugHelper> 00660 { 00661 public: 00662 csGeomDebugHelper (); 00663 virtual ~csGeomDebugHelper (); 00664 00665 virtual int GetSupportedTests () const 00666 { 00667 return CS_DBGHELP_UNITTEST; 00668 } 00669 virtual csPtr<iString> UnitTest (); 00670 virtual csPtr<iString> StateTest () 00671 { 00672 return 0; 00673 } 00674 virtual csTicks Benchmark (int /*num_iterations*/) 00675 { 00676 return 0; 00677 } 00678 virtual csPtr<iString> Dump () 00679 { 00680 return 0; 00681 } 00682 virtual void Dump (iGraphics3D* /*g3d*/) 00683 { 00684 } 00685 virtual bool DebugCommand (const char*) 00686 { 00687 return false; 00688 } 00689 }; 00690 00693 #endif // __CS_MATH3D_H__ 00694
Generated for Crystal Space by doxygen 1.4.4