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 
00338   static bool SegmentPlane (
00339     const csPlane3& plane,
00340     csSegment3& segment);
00341 
00364   static bool SegmentPlane (
00365     const csVector3& u, const csVector3& v,
00366     const csPlane3& p,                     // plane Ax+By+Cz+D=0
00367     csVector3& isect,                     // intersection point
00368     float& dist);                       // distance from u to isect
00369 
00376   static bool ThreePlanes (const csPlane3& p1, const csPlane3& p2,
00377         const csPlane3& p3, csVector3& isect);
00378 
00385   static bool PlaneXPlane (const csPlane3& p1, float x2, csPlane2& isect);
00386 
00393   static bool PlaneYPlane (const csPlane3& p1, float y2, csPlane2& isect);
00394 
00401   static bool PlaneZPlane (const csPlane3& p1, float z2, csPlane2& isect);
00402 
00409   static bool PlaneAxisPlane (const csPlane3& p1, int nr, float pos,
00410         csPlane2& isect)
00411   {
00412     switch (nr)
00413     {
00414       case 0: return PlaneXPlane (p1, pos, isect);
00415       case 1: return PlaneYPlane (p1, pos, isect);
00416       case 2: return PlaneZPlane (p1, pos, isect);
00417     }
00418     return false;
00419   }
00420 
00427   static float SegmentZ0Plane (
00428     const csVector3& v1, const csVector3& v2,
00429     csVector3& isect);                    // intersection point
00430 
00437   static float SegmentZ0Plane (
00438     const csSegment3& uv,
00439     csVector3& isect)                    // intersection point
00440   {
00441     return SegmentZ0Plane (uv.Start (), uv.End (), isect);
00442   }
00443 
00450   static float SegmentXPlane (
00451     const csVector3& u, const csVector3& v,
00452     float xval,
00453     csVector3& isect);                    // intersection point
00454 
00461   static float SegmentXPlane (
00462     const csSegment3& uv,
00463     float xval,
00464     csVector3& isect)                     // intersection point
00465   {
00466     return SegmentXPlane (uv.Start (), uv.End (), xval, isect);
00467   }
00468 
00475   static float SegmentYPlane (
00476     const csVector3& u, const csVector3& v,
00477     float yval,      // plane y = yval
00478     csVector3& isect);                    // intersection point
00479 
00486   static float SegmentYPlane (
00487     const csSegment3& uv,
00488     float yval,      // plane y = yval
00489     csVector3& isect)                     // intersection point
00490   {
00491     return SegmentYPlane (uv.Start (), uv.End (), yval, isect);
00492   }
00493 
00500   static float SegmentZPlane (
00501     const csVector3& u, const csVector3& v,
00502     float zval,      // plane z = zval
00503     csVector3& isect);                    // intersection point
00504 
00511   static float SegmentZPlane (
00512     const csSegment3& uv,
00513     float zval,      // plane z = zval
00514     csVector3& isect)                     // intersection point
00515   {
00516     return SegmentZPlane (uv.Start (), uv.End (), zval, isect);
00517   }
00518 
00525   static float SegmentAxisPlane (const csVector3& u, const csVector3& v,
00526         int nr, float pos, csVector3& isect)
00527   {
00528     switch (nr)
00529     {
00530       case 0: return SegmentXPlane (u, v, pos, isect);
00531       case 1: return SegmentYPlane (u, v, pos, isect);
00532       case 2: return SegmentZPlane (u, v, pos, isect);
00533     }
00534     return 0.0;
00535   }
00536 
00541   static float SegmentXFrustum (
00542     const csVector3& u, const csVector3& v, float A, csVector3& isect);
00543 
00548   static float SegmentXFrustum (
00549     const csSegment3& uv, float A, csVector3& isect)
00550   {
00551     return SegmentXFrustum (uv.Start (), uv.End (), A, isect);
00552   }
00553 
00558   static float SegmentYFrustum (
00559     const csVector3& u, const csVector3& v, float B, csVector3& isect);
00560 
00565   static float SegmentYFrustum (
00566     const csSegment3& uv, float B, csVector3& isect)
00567   {
00568     return SegmentYFrustum (uv.Start (), uv.End (), B, isect);
00569   }
00570 
00589   static int BoxSegment (const csBox3& box, const csSegment3& segment,
00590         csVector3& isect, float* pr = 0, bool use_ray = false);
00591 
00600   static bool ClipSegmentBox (csSegment3& segment, const csBox3& box,
00601         bool use_ray = false);
00602 
00612   static bool BoxFrustum (const csBox3& box, const csPlane3* frustum,
00613         uint32 inClipMask, uint32& outClipMask);
00614 
00618   static bool BoxFrustum (const csBox3& box, const csFrustum* frustum);
00619 
00624   static bool BoxSphere (const csBox3& box, const csVector3& center,
00625                   float sqradius);
00626 
00630   static bool BoxPlane (const csBox3& box, const csPlane3& plane);
00631 
00636   static bool BoxPlane (const csBox3& box, const csVector3& normal,
00637         const csVector3& vert);
00638 
00642   static bool BoxTriangle (const csBox3& box,
00643         const csVector3& tri0, const csVector3& tri1, const csVector3& tri2);
00644 
00648   static bool BoxBox (const csBox3& box1, const csBox3& box2)
00649   {
00650     return box1.TestIntersect (box2);
00651   }
00652 
00656   static csPtr<csFrustum> FrustumFrustum (const csFrustum& f1,
00657         const csFrustum& f2)
00658   {
00659     return f1.Intersect (f2);
00660   }
00661 
00665   static csPtr<csFrustum> FrustumFrustum (const csFrustum& f1,
00666         csVector3* poly, int num)
00667   {
00668     return f1.Intersect (poly, num);
00669   }
00670 
00677   static bool TriangleTriangle (const csVector3 tri1[3],
00678                          const csVector3 tri2[3]);
00679 
00689   static bool TriangleTriangle (const csVector3 tri1[3],
00690                          const csVector3 tri2[3],
00691                          csSegment3& isectline, bool& coplanar);
00692 };
00693 
00698 class CS_CRYSTALSPACE_EXPORT csGeomDebugHelper :
00699   public scfImplementation1<csGeomDebugHelper, iDebugHelper>
00700 {
00701 public:
00702   csGeomDebugHelper ();
00703   virtual ~csGeomDebugHelper ();
00704 
00705   virtual int GetSupportedTests () const
00706   {
00707     return CS_DBGHELP_UNITTEST;
00708   }
00709   virtual csPtr<iString> UnitTest ();
00710   virtual csPtr<iString> StateTest ()
00711   {
00712     return 0;
00713   }
00714   virtual csTicks Benchmark (int /*num_iterations*/)
00715   {
00716     return 0;
00717   }
00718   virtual csPtr<iString> Dump ()
00719   {
00720     return 0;
00721   }
00722   virtual void Dump (iGraphics3D* /*g3d*/)
00723   {
00724   }
00725   virtual bool DebugCommand (const char*)
00726   {
00727     return false;
00728   }
00729 };
00730 
00733 #endif // __CS_MATH3D_H__
00734 

Generated for Crystal Space 1.2.1 by doxygen 1.5.3