CrystalSpace

Public API Reference

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 public:
00274   static bool PlanePolygon (const csPlane3& plane, csPoly3D* poly,
00275         csSegment3& segment);
00276 
00286   static int SegmentFrustum (csPlane3* planes, int num_planes,
00287         csSegment3& seg);
00288 
00295   static bool SegmentTriangle (const csSegment3& seg,
00296         const csVector3& tr1,
00297         const csVector3& tr2, const csVector3& tr3,
00298         csVector3& isect);
00299 
00307   static bool SegmentPolygon (const csSegment3& seg, const csPoly3D& poly,
00308         const csPlane3& poly_plane, csVector3& isect);
00309 
00318   static bool SegmentPlanes (
00319      const csVector3& u, const csVector3& v,
00320      const csPlane3* planes, int length,
00321      csVector3& isect, float& dist);
00322 
00328   static bool SegmentPlane (
00329     const csVector3& u, const csVector3& v,
00330     const csVector3& normal, const csVector3& a, // plane
00331     csVector3& isect, float& dist);              // intersection point
00332 
00355   static bool SegmentPlane (
00356     const csVector3& u, const csVector3& v,
00357     const csPlane3& p,                     // plane Ax+By+Cz+D=0
00358     csVector3& isect,                     // intersection point
00359     float& dist);                       // distance from u to isect
00360 
00367   static bool ThreePlanes (const csPlane3& p1, const csPlane3& p2,
00368         const csPlane3& p3, csVector3& isect);
00369 
00376   static bool PlaneXPlane (const csPlane3& p1, float x2, csPlane2& isect);
00377 
00384   static bool PlaneYPlane (const csPlane3& p1, float y2, csPlane2& isect);
00385 
00392   static bool PlaneZPlane (const csPlane3& p1, float z2, csPlane2& isect);
00393 
00400   static bool PlaneAxisPlane (const csPlane3& p1, int nr, float pos,
00401         csPlane2& isect)
00402   {
00403     switch (nr)
00404     {
00405       case 0: return PlaneXPlane (p1, pos, isect);
00406       case 1: return PlaneYPlane (p1, pos, isect);
00407       case 2: return PlaneZPlane (p1, pos, isect);
00408     }
00409     return false;
00410   }
00411 
00418   static float SegmentZ0Plane (
00419     const csVector3& v1, const csVector3& v2,
00420     csVector3& isect);                    // intersection point
00421 
00428   static float SegmentZ0Plane (
00429     const csSegment3& uv,
00430     csVector3& isect)                    // intersection point
00431   {
00432     return SegmentZ0Plane (uv.Start (), uv.End (), isect);
00433   }
00434 
00441   static float SegmentXPlane (
00442     const csVector3& u, const csVector3& v,
00443     float xval,
00444     csVector3& isect);                    // intersection point
00445 
00452   static float SegmentXPlane (
00453     const csSegment3& uv,
00454     float xval,
00455     csVector3& isect)                     // intersection point
00456   {
00457     return SegmentXPlane (uv.Start (), uv.End (), xval, isect);
00458   }
00459 
00466   static float SegmentYPlane (
00467     const csVector3& u, const csVector3& v,
00468     float yval,      // plane y = yval
00469     csVector3& isect);                    // intersection point
00470 
00477   static float SegmentYPlane (
00478     const csSegment3& uv,
00479     float yval,      // plane y = yval
00480     csVector3& isect)                     // intersection point
00481   {
00482     return SegmentYPlane (uv.Start (), uv.End (), yval, isect);
00483   }
00484 
00491   static float SegmentZPlane (
00492     const csVector3& u, const csVector3& v,
00493     float zval,      // plane z = zval
00494     csVector3& isect);                    // intersection point
00495 
00502   static float SegmentZPlane (
00503     const csSegment3& uv,
00504     float zval,      // plane z = zval
00505     csVector3& isect)                     // intersection point
00506   {
00507     return SegmentZPlane (uv.Start (), uv.End (), zval, isect);
00508   }
00509 
00516   static float SegmentAxisPlane (const csVector3& u, const csVector3& v,
00517         int nr, float pos, csVector3& isect)
00518   {
00519     switch (nr)
00520     {
00521       case 0: return SegmentXPlane (u, v, pos, isect);
00522       case 1: return SegmentYPlane (u, v, pos, isect);
00523       case 2: return SegmentZPlane (u, v, pos, isect);
00524     }
00525     return 0.0;
00526   }
00527 
00532   static float SegmentXFrustum (
00533     const csVector3& u, const csVector3& v, float A, csVector3& isect);
00534 
00539   static float SegmentXFrustum (
00540     const csSegment3& uv, float A, csVector3& isect)
00541   {
00542     return SegmentXFrustum (uv.Start (), uv.End (), A, isect);
00543   }
00544 
00549   static float SegmentYFrustum (
00550     const csVector3& u, const csVector3& v, float B, csVector3& isect);
00551 
00556   static float SegmentYFrustum (
00557     const csSegment3& uv, float B, csVector3& isect)
00558   {
00559     return SegmentYFrustum (uv.Start (), uv.End (), B, isect);
00560   }
00561 
00576   static int BoxSegment (const csBox3& box, const csSegment3& segment,
00577         csVector3& isect, float* pr = 0);
00578 
00588   static bool BoxFrustum (const csBox3& box, csPlane3* frustum,
00589         uint32 inClipMask, uint32& outClipMask);
00590 
00594   static bool BoxFrustum (const csBox3& box, const csFrustum* frustum);
00595 
00600   static bool BoxSphere (const csBox3& box, const csVector3& center,
00601                   float sqradius);
00602 
00606   static bool BoxPlane (const csBox3& box, const csPlane3& plane);
00607 
00612   static bool BoxPlane (const csBox3& box, const csVector3& normal,
00613         const csVector3& vert);
00614 
00618   static bool BoxTriangle (const csBox3& box,
00619         const csVector3& tri0, const csVector3& tri1, const csVector3& tri2);
00620 
00624   static bool BoxBox (const csBox3& box1, const csBox3& box2)
00625   {
00626     return box1.TestIntersect (box2);
00627   }
00628 
00632   static csPtr<csFrustum> FrustumFrustum (const csFrustum& f1,
00633         const csFrustum& f2)
00634   {
00635     return f1.Intersect (f2);
00636   }
00637 
00641   static csPtr<csFrustum> FrustumFrustum (const csFrustum& f1,
00642         csVector3* poly, int num)
00643   {
00644     return f1.Intersect (poly, num);
00645   }
00646 
00653   static bool TriangleTriangle (const csVector3 tri1[3],
00654                          const csVector3 tri2[3]);
00655 
00665   static bool TriangleTriangle (const csVector3 tri1[3],
00666                          const csVector3 tri2[3],
00667                          csSegment3& isectline, bool& coplanar);
00668 };
00669 
00674 class CS_CRYSTALSPACE_EXPORT csGeomDebugHelper :
00675   public scfImplementation1<csGeomDebugHelper, iDebugHelper>
00676 {
00677 public:
00678   csGeomDebugHelper ();
00679   virtual ~csGeomDebugHelper ();
00680 
00681   virtual int GetSupportedTests () const
00682   {
00683     return CS_DBGHELP_UNITTEST;
00684   }
00685   virtual csPtr<iString> UnitTest ();
00686   virtual csPtr<iString> StateTest ()
00687   {
00688     return 0;
00689   }
00690   virtual csTicks Benchmark (int /*num_iterations*/)
00691   {
00692     return 0;
00693   }
00694   virtual csPtr<iString> Dump ()
00695   {
00696     return 0;
00697   }
00698   virtual void Dump (iGraphics3D* /*g3d*/)
00699   {
00700   }
00701   virtual bool DebugCommand (const char*)
00702   {
00703     return false;
00704   }
00705 };
00706 
00709 #endif // __CS_MATH3D_H__
00710 

Generated for Crystal Space 1.0.2 by doxygen 1.4.7