csgeom/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 00030 #include "csextern.h" 00031 00032 #include "csgeom/box.h" 00033 #include "csgeom/frustum.h" 00034 #include "csgeom/plane3.h" 00035 #include "csgeom/segment.h" 00036 #include "csgeom/vector3.h" 00037 #include "csutil/ref.h" 00038 #include "csutil/scf_implementation.h" 00039 00040 #include "iutil/dbghelp.h" 00041 00042 struct iString; 00043 class csPlane2; 00044 class csPoly3D; 00045 00050 class CS_CRYSTALSPACE_EXPORT csMath3 00051 { 00052 public: 00065 static int WhichSide3D (const csVector3& p, 00066 const csVector3& v1, const csVector3& v2) 00067 { 00068 // float s = p * (v1%v2); (original expression: expanded to the below:) 00069 float s = p.x*(v1.y*v2.z-v1.z*v2.y) + p.y*(v1.z*v2.x-v1.x*v2.z) + 00070 p.z*(v1.x*v2.y-v1.y*v2.x); 00071 if (s < 0) return 1; 00072 else if (s > 0) return -1; 00073 else return 0; 00074 } 00075 00081 static bool Visible (const csVector3& p, const csVector3& t1, 00082 const csVector3& t2, const csVector3& t3); 00083 00089 static bool Visible (const csVector3& p, const csPlane3& pl) 00090 { return pl.Classify (p) <= 0; } 00091 00101 static void Between (const csVector3& v1, const csVector3& v2, csVector3& v, 00102 float pct, float wid); 00103 00110 static void SetMinMax (const csVector3& v, 00111 csVector3& min, csVector3& max) 00112 { 00113 if (v.x > max.x) max.x = v.x; else if (v.x < min.x ) min.x = v.x; 00114 if (v.y > max.y) max.y = v.y; else if (v.y < min.y ) min.y = v.y; 00115 if (v.z > max.z) max.z = v.z; else if (v.z < min.z ) min.z = v.z; 00116 } 00117 00123 inline static float DoubleArea3 (const csVector3 &a, const csVector3 &b, 00124 const csVector3 &c) 00125 { 00126 csVector3 v1 = b - a; 00127 csVector3 v2 = c - a; 00128 return (v1 % v2).Norm (); 00129 } 00130 00134 inline static float Direction3 (const csVector3 &a, const csVector3 &b, 00135 const csVector3 &c) 00136 { 00137 csVector3 v1 = b - a; 00138 csVector3 v2 = c - a; 00139 return ((v1.y * v2.z + v1.z * v2.x + v1.x * v2.y) - 00140 (v1.y * v2.x + v1.x * v2.z + v1.z * v2.y)); 00141 } 00142 00148 inline static void CalcNormal (csVector3& norm, const csVector3& v1, 00149 const csVector3& v2, const csVector3& v3) 00150 { 00151 norm = (v1-v2)%(v1-v3); 00152 } 00153 00159 static void CalcNormal (csVector3& norm, 00160 const csVector3& v, const csVector3& u) 00161 { norm = u%v; /* NOT v%u - vertexes are defined clockwise */ } 00162 00169 static void CalcPlane (const csVector3& v1, const csVector3& v2, 00170 const csVector3& v3, csVector3& normal, float& D) 00171 { 00172 CalcNormal (normal, v1, v2, v3); 00173 D = - (normal * v1); 00174 } 00175 00182 static bool PlanesEqual (const csPlane3& p1, const csPlane3& p2) 00183 { 00184 return ( ( p1.norm - p2.norm) < (float).001 ) && 00185 ( ABS (p1.DD-p2.DD) < (float).001 ); 00186 } 00187 00193 static bool PlanesClose (const csPlane3& p1, const csPlane3& p2); 00194 00202 static int OuterPlanes (const csBox3& box1, const csBox3& box2, 00203 csPlane3* planes); 00204 00212 static int FindObserverSides (const csBox3& box1, const csBox3& box2, 00213 int* sides); 00214 00220 static void SpherePosition (float angle_xz, float angle_vert, 00221 csVector3& pos); 00222 }; 00223 00228 class CS_CRYSTALSPACE_EXPORT csSquaredDist 00229 { 00230 public: 00232 static float PointPoint (const csVector3& p1, const csVector3& p2) 00233 { 00234 csVector3 d = (p1-p2); 00235 return d*d; 00236 } 00237 00239 static float PointLine (const csVector3& p, 00240 const csVector3& l1, const csVector3& l2); 00241 00243 static float PointPlane (const csVector3& p, const csPlane3& plane) 00244 { float r = plane.Classify (p); return r * r; } 00245 00252 static float PointPoly (const csVector3& p, csVector3 *V, int n, 00253 const csPlane3& plane, float sqdist = -1); 00254 }; 00255 00261 class CS_CRYSTALSPACE_EXPORT csIntersect3 00262 { 00263 private: 00264 static bool BoxPlaneInternal (const csVector3& normal, 00265 const csVector3& vert, const csVector3& boxhalfsize); 00266 00267 static bool TestInTriangle (const csSegment3 &seg, 00268 const csPlane3& plane, 00269 const csVector3 &tr1, const csVector3 &tr2, const csVector3 &tr3, 00270 const csVector3 &isect); 00271 00272 public: 00279 static bool PlanePolygon (const csPlane3& plane, csPoly3D* poly, 00280 csSegment3& segment); 00281 00291 static int SegmentFrustum (csPlane3* planes, int num_planes, 00292 csSegment3& seg); 00293 00299 static bool SegmentTriangle (const csSegment3& seg, 00300 const csVector3& tr1, 00301 const csVector3& tr2, const csVector3& tr3, 00302 csVector3& isect); 00303 00309 static bool SegmentTriangleBF (const csSegment3& seg, 00310 const csVector3& tr1, 00311 const csVector3& tr2, const csVector3& tr3, 00312 csVector3& isect); 00313 00321 static bool SegmentPolygon (const csSegment3& seg, const csPoly3D& poly, 00322 const csPlane3& poly_plane, csVector3& isect); 00323 00332 static bool SegmentPlanes ( 00333 const csVector3& u, const csVector3& v, 00334 const csPlane3* planes, int length, 00335 csVector3& isect, float& dist); 00336 00342 static bool SegmentPlane ( 00343 const csVector3& u, const csVector3& v, 00344 const csVector3& normal, const csVector3& a, // plane 00345 csVector3& isect, float& dist); // intersection point 00346 00352 static bool SegmentPlane ( 00353 const csPlane3& plane, 00354 csSegment3& segment); 00355 00378 static bool SegmentPlane ( 00379 const csVector3& u, const csVector3& v, 00380 const csPlane3& p, // plane Ax+By+Cz+D=0 00381 csVector3& isect, // intersection point 00382 float& dist); // distance from u to isect 00383 00390 static bool ThreePlanes (const csPlane3& p1, const csPlane3& p2, 00391 const csPlane3& p3, csVector3& isect); 00392 00399 static bool PlaneXPlane (const csPlane3& p1, float x2, csPlane2& isect); 00400 00407 static bool PlaneYPlane (const csPlane3& p1, float y2, csPlane2& isect); 00408 00415 static bool PlaneZPlane (const csPlane3& p1, float z2, csPlane2& isect); 00416 00423 static bool PlaneAxisPlane (const csPlane3& p1, int nr, float pos, 00424 csPlane2& isect) 00425 { 00426 switch (nr) 00427 { 00428 case 0: return PlaneXPlane (p1, pos, isect); 00429 case 1: return PlaneYPlane (p1, pos, isect); 00430 case 2: return PlaneZPlane (p1, pos, isect); 00431 } 00432 return false; 00433 } 00434 00441 static float SegmentZ0Plane ( 00442 const csVector3& v1, const csVector3& v2, 00443 csVector3& isect); // intersection point 00444 00451 static float SegmentZ0Plane ( 00452 const csSegment3& uv, 00453 csVector3& isect) // intersection point 00454 { 00455 return SegmentZ0Plane (uv.Start (), uv.End (), isect); 00456 } 00457 00464 static float SegmentXPlane ( 00465 const csVector3& u, const csVector3& v, 00466 float xval, 00467 csVector3& isect); // intersection point 00468 00475 static float SegmentXPlane ( 00476 const csSegment3& uv, 00477 float xval, 00478 csVector3& isect) // intersection point 00479 { 00480 return SegmentXPlane (uv.Start (), uv.End (), xval, isect); 00481 } 00482 00489 static float SegmentYPlane ( 00490 const csVector3& u, const csVector3& v, 00491 float yval, // plane y = yval 00492 csVector3& isect); // intersection point 00493 00500 static float SegmentYPlane ( 00501 const csSegment3& uv, 00502 float yval, // plane y = yval 00503 csVector3& isect) // intersection point 00504 { 00505 return SegmentYPlane (uv.Start (), uv.End (), yval, isect); 00506 } 00507 00514 static float SegmentZPlane ( 00515 const csVector3& u, const csVector3& v, 00516 float zval, // plane z = zval 00517 csVector3& isect); // intersection point 00518 00525 static float SegmentZPlane ( 00526 const csSegment3& uv, 00527 float zval, // plane z = zval 00528 csVector3& isect) // intersection point 00529 { 00530 return SegmentZPlane (uv.Start (), uv.End (), zval, isect); 00531 } 00532 00539 static float SegmentAxisPlane (const csVector3& u, const csVector3& v, 00540 int nr, float pos, csVector3& isect) 00541 { 00542 switch (nr) 00543 { 00544 case 0: return SegmentXPlane (u, v, pos, isect); 00545 case 1: return SegmentYPlane (u, v, pos, isect); 00546 case 2: return SegmentZPlane (u, v, pos, isect); 00547 } 00548 return 0.0; 00549 } 00550 00555 static float SegmentXFrustum ( 00556 const csVector3& u, const csVector3& v, float A, csVector3& isect); 00557 00562 static float SegmentXFrustum ( 00563 const csSegment3& uv, float A, csVector3& isect) 00564 { 00565 return SegmentXFrustum (uv.Start (), uv.End (), A, isect); 00566 } 00567 00572 static float SegmentYFrustum ( 00573 const csVector3& u, const csVector3& v, float B, csVector3& isect); 00574 00579 static float SegmentYFrustum ( 00580 const csSegment3& uv, float B, csVector3& isect) 00581 { 00582 return SegmentYFrustum (uv.Start (), uv.End (), B, isect); 00583 } 00584 00603 static int BoxSegment (const csBox3& box, const csSegment3& segment, 00604 csVector3& isect, float* pr = 0, bool use_ray = false); 00605 00614 static bool ClipSegmentBox (csSegment3& segment, const csBox3& box, 00615 bool use_ray = false); 00616 00626 static bool BoxFrustum (const csBox3& box, const csPlane3* frustum, 00627 uint32 inClipMask, uint32& outClipMask); 00628 00632 static bool BoxFrustum (const csBox3& box, const csFrustum* frustum); 00633 00638 static bool BoxSphere (const csBox3& box, const csVector3& center, 00639 float sqradius); 00640 00644 static bool BoxPlane (const csBox3& box, const csPlane3& plane); 00645 00650 static bool BoxPlane (const csBox3& box, const csVector3& normal, 00651 const csVector3& vert); 00652 00656 static bool BoxTriangle (const csBox3& box, 00657 const csVector3& tri0, const csVector3& tri1, const csVector3& tri2); 00658 00662 static bool BoxBox (const csBox3& box1, const csBox3& box2) 00663 { 00664 return box1.TestIntersect (box2); 00665 } 00666 00670 static csPtr<csFrustum> FrustumFrustum (const csFrustum& f1, 00671 const csFrustum& f2) 00672 { 00673 return f1.Intersect (f2); 00674 } 00675 00679 static csPtr<csFrustum> FrustumFrustum (const csFrustum& f1, 00680 csVector3* poly, int num) 00681 { 00682 return f1.Intersect (poly, num); 00683 } 00684 00691 static bool TriangleTriangle (const csVector3 tri1[3], 00692 const csVector3 tri2[3]); 00693 00703 static bool TriangleTriangle (const csVector3 tri1[3], 00704 const csVector3 tri2[3], 00705 csSegment3& isectline, bool& coplanar); 00706 }; 00707 00708 00711 #endif // __CS_MATH3D_H__ 00712
Generated for Crystal Space 2.1 by doxygen 1.6.1
