CrystalSpace

Public API Reference

Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

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 
00029 #include "csextern.h"
00030 
00031 #include "csgeom/vector3.h"
00032 #include "csgeom/plane3.h"
00033 #include "csgeom/plane2.h"
00034 #include "csgeom/segment.h"
00035 #include "csgeom/box.h"
00036 #include "csgeom/frustum.h"
00037 #include "iutil/dbghelp.h"
00038 
00039 class csDVector3;
00040 class csPoly3D;
00041 class csBox3;
00042 
00043 inline float fSqr (float f)
00044 {
00045   return f * f;
00046 }
00047 
00052 class CS_CSGEOM_EXPORT csMath3
00053 {
00054 public:
00067   static int WhichSide3D (const csVector3& p,
00068                           const csVector3& v1, const csVector3& v2)
00069   {
00070 //    float s = p * (v1%v2);  (original expression: expanded to the below:)
00071     float s = p.x*(v1.y*v2.z-v1.z*v2.y) + p.y*(v1.z*v2.x-v1.x*v2.z) +
00072               p.z*(v1.x*v2.y-v1.y*v2.x);
00073     if (s < 0) return 1;
00074     else if (s > 0) return -1;
00075     else return 0;
00076   }
00077 
00083   static bool Visible (const csVector3& p, const csVector3& t1,
00084                        const csVector3& t2, const csVector3& t3);
00085 
00091   static bool Visible (const csVector3& p, const csPlane3& pl)
00092   { return pl.Classify (p) <= 0; }
00093 
00101   static bool FindIntersection (const csVector3  tri1[3],
00102                                 const csVector3  tri2[3],
00103                                 csVector3        line[2]);
00104 
00114   static void Between (const csVector3& v1, const csVector3& v2, csVector3& v,
00115                        float pct, float wid);
00116 
00123   static void SetMinMax (const csVector3& v,
00124                          csVector3& min, csVector3& max)
00125   {
00126     if (v.x > max.x) max.x = v.x; else if (v.x < min.x ) min.x = v.x;
00127     if (v.y > max.y) max.y = v.y; else if (v.y < min.y ) min.y = v.y;
00128     if (v.z > max.z) max.z = v.z; else if (v.z < min.z ) min.z = v.z;
00129   }
00130 
00136   inline static float DoubleArea3 (const csVector3 &a, const csVector3 &b,
00137                              const csVector3 &c)
00138   {
00139     csVector3 v1 = b - a;
00140     csVector3 v2 = c - a;
00141     return (v1 % v2).Norm ();
00142   }
00143 
00147   inline static float Direction3 (const csVector3 &a, const csVector3 &b,
00148                              const csVector3 &c)
00149   {
00150     csVector3 v1 = b - a;
00151     csVector3 v2 = c - a;
00152     return ((v1.y * v2.z + v1.z * v2.x + v1.x * v2.y) -
00153             (v1.y * v2.x + v1.x * v2.z + v1.z * v2.y));
00154   }
00155 
00161   inline static void CalcNormal (csVector3& norm,     const csVector3& v1,
00162                                  const csVector3& v2, const csVector3& v3)
00163   {
00164     norm = (v1-v2)%(v1-v3);
00165   }
00166 
00172   static void CalcNormal (csVector3& norm,
00173                           const csVector3& v, const csVector3& u)
00174   { norm = u%v; /* NOT v%u - vertexes are defined clockwise */ }
00175 
00182   static void CalcPlane (const csVector3& v1, const csVector3& v2,
00183          const csVector3& v3, csVector3& normal, float& D)
00184   {
00185     CalcNormal (normal, v1, v2, v3);
00186     D = - (normal * v1);
00187   }
00188 
00195   static bool PlanesEqual (const csPlane3& p1, const csPlane3& p2)
00196   {
00197     return ( ( p1.norm - p2.norm) < (float).001 ) &&
00198              (  ABS (p1.DD-p2.DD) < (float).001 );
00199   }
00200 
00206   static bool PlanesClose (const csPlane3& p1, const csPlane3& p2);
00207 
00215   static int OuterPlanes (const csBox3& box1, const csBox3& box2,
00216     csPlane3* planes);
00217 
00225   static int FindObserverSides (const csBox3& box1, const csBox3& box2,
00226         int* sides);
00227 
00233   static void SpherePosition (float angle_xz, float angle_vert,
00234         csVector3& pos);
00235 };
00236 
00241 class CS_CSGEOM_EXPORT csSquaredDist
00242 {
00243 public:
00245   static float PointPoint (const csVector3& p1, const csVector3& p2)
00246   { return fSqr (p1.x - p2.x) + fSqr (p1.y - p2.y) + fSqr (p1.z - p2.z); }
00247 
00249   static float PointLine (const csVector3& p,
00250                           const csVector3& l1, const csVector3& l2);
00251 
00253   static float PointPlane (const csVector3& p, const csPlane3& plane)
00254   { float r = plane.Classify (p);  return r * r; }
00255 
00262   static float PointPoly (const csVector3& p, csVector3 *V, int n,
00263                           const csPlane3& plane, float sqdist = -1);
00264 };
00265 
00271 class CS_CSGEOM_EXPORT csIntersect3
00272 {
00273 private:
00274   static bool BoxPlaneInternal (const csVector3& normal,
00275         const csVector3& vert, const csVector3& boxhalfsize);
00276 
00277 public:
00284   static bool PlanePolygon (const csPlane3& plane, csPoly3D* poly,
00285         csSegment3& segment);
00286 
00296   static int SegmentFrustum (csPlane3* planes, int num_planes,
00297         csSegment3& seg);
00298 
00304   static bool SegmentTriangle (const csSegment3& seg,
00305         const csVector3& tr1,
00306         const csVector3& tr2, const csVector3& tr3,
00307         csVector3& isect);
00308 
00315   static bool SegmentPolygon (const csSegment3& seg, const csPoly3D& poly,
00316         const csPlane3& poly_plane, csVector3& isect);
00317 
00326   static bool SegmentPlanes (
00327      const csVector3& u, const csVector3& v,
00328      const csPlane3* planes, int length,
00329      csVector3& isect, float& dist);
00330 
00335   static bool SegmentPlane (
00336     const csVector3& u, const csVector3& v,
00337     const csVector3& normal, const csVector3& a, // plane
00338     csVector3& isect, float& dist);              // intersection point
00339 
00361   static bool SegmentPlane (
00362     const csVector3& u, const csVector3& v,
00363     const csPlane3& p,                     // plane Ax+By+Cz+D=0
00364     csVector3& isect,                     // intersection point
00365     float& dist);                       // distance from u to isect
00366 
00372   static bool ThreePlanes (const csPlane3& p1, const csPlane3& p2,
00373         const csPlane3& p3, csVector3& isect);
00374 
00381   static bool PlaneXPlane (const csPlane3& p1, float x2, csPlane2& isect);
00382 
00389   static bool PlaneYPlane (const csPlane3& p1, float y2, csPlane2& isect);
00390 
00397   static bool PlaneZPlane (const csPlane3& p1, float z2, csPlane2& isect);
00398 
00405   static bool PlaneAxisPlane (const csPlane3& p1, int nr, float pos,
00406         csPlane2& isect)
00407   {
00408     switch (nr)
00409     {
00410       case 0: return PlaneXPlane (p1, pos, isect);
00411       case 1: return PlaneYPlane (p1, pos, isect);
00412       case 2: return PlaneZPlane (p1, pos, isect);
00413     }
00414     return false;
00415   }
00416 
00423   static float SegmentZ0Plane (
00424     const csVector3& v1, const csVector3& v2,
00425     csVector3& isect);                    // intersection point
00426 
00433   static float SegmentZ0Plane (
00434     const csSegment3& uv,
00435     csVector3& isect)                    // intersection point
00436   {
00437     return SegmentZ0Plane (uv.Start (), uv.End (), isect);
00438   }
00439 
00446   static float SegmentXPlane (
00447     const csVector3& u, const csVector3& v,
00448     float xval,
00449     csVector3& isect);                    // intersection point
00450 
00457   static float SegmentXPlane (
00458     const csSegment3& uv,
00459     float xval,
00460     csVector3& isect)                     // intersection point
00461   {
00462     return SegmentXPlane (uv.Start (), uv.End (), xval, isect);
00463   }
00464 
00471   static float SegmentYPlane (
00472     const csVector3& u, const csVector3& v,
00473     float yval,      // plane y = yval
00474     csVector3& isect);                    // intersection point
00475 
00482   static float SegmentYPlane (
00483     const csSegment3& uv,
00484     float yval,      // plane y = yval
00485     csVector3& isect)                     // intersection point
00486   {
00487     return SegmentYPlane (uv.Start (), uv.End (), yval, isect);
00488   }
00489 
00496   static float SegmentZPlane (
00497     const csVector3& u, const csVector3& v,
00498     float zval,      // plane z = zval
00499     csVector3& isect);                    // intersection point
00500 
00507   static float SegmentZPlane (
00508     const csSegment3& uv,
00509     float zval,      // plane z = zval
00510     csVector3& isect)                     // intersection point
00511   {
00512     return SegmentZPlane (uv.Start (), uv.End (), zval, isect);
00513   }
00514 
00521   static float SegmentAxisPlane (const csVector3& u, const csVector3& v,
00522         int nr, float pos, csVector3& isect)
00523   {
00524     switch (nr)
00525     {
00526       case 0: return SegmentXPlane (u, v, pos, isect);
00527       case 1: return SegmentYPlane (u, v, pos, isect);
00528       case 2: return SegmentZPlane (u, v, pos, isect);
00529     }
00530     return 0.0;
00531   }
00532 
00537   static float SegmentXFrustum (
00538     const csVector3& u, const csVector3& v, float A, csVector3& isect);
00539 
00544   static float SegmentXFrustum (
00545     const csSegment3& uv, float A, csVector3& isect)
00546   {
00547     return SegmentXFrustum (uv.Start (), uv.End (), A, isect);
00548   }
00549 
00554   static float SegmentYFrustum (
00555     const csVector3& u, const csVector3& v, float B, csVector3& isect);
00556 
00561   static float SegmentYFrustum (
00562     const csSegment3& uv, float B, csVector3& isect)
00563   {
00564     return SegmentYFrustum (uv.Start (), uv.End (), B, isect);
00565   }
00566 
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 
00595   static bool BoxSphere (const csBox3& box, const csVector3& center,
00596                   float sqradius);
00597 
00601   static bool BoxPlane (const csBox3& box, const csPlane3& plane);
00602 
00607   static bool BoxPlane (const csBox3& box, const csVector3& normal,
00608         const csVector3& vert);
00609 
00613   static bool BoxTriangle (const csBox3& box,
00614         const csVector3& tri0, const csVector3& tri1, const csVector3& tri2);
00615 
00619   static bool BoxBox (const csBox3& box1, const csBox3& box2)
00620   {
00621     return box1.TestIntersect (box2);
00622   }
00623 
00627   static csPtr<csFrustum> FrustumFrustum (const csFrustum& f1,
00628         const csFrustum& f2)
00629   {
00630     return f1.Intersect (f2);
00631   }
00632 
00636   static csPtr<csFrustum> FrustumFrustum (const csFrustum& f1,
00637         csVector3* poly, int num)
00638   {
00639     return f1.Intersect (poly, num);
00640   }
00641 };
00642 
00647 class CS_CSGEOM_EXPORT csGeomDebugHelper : public iDebugHelper
00648 {
00649 public:
00650   csGeomDebugHelper ();
00651   virtual ~csGeomDebugHelper ();
00652 
00653   SCF_DECLARE_IBASE;
00654   virtual int GetSupportedTests () const
00655   {
00656     return CS_DBGHELP_UNITTEST;
00657   }
00658   virtual csPtr<iString> UnitTest ();
00659   virtual csPtr<iString> StateTest ()
00660   {
00661     return 0;
00662   }
00663   virtual csTicks Benchmark (int /*num_iterations*/)
00664   {
00665     return 0;
00666   }
00667   virtual csPtr<iString> Dump ()
00668   {
00669     return 0;
00670   }
00671   virtual void Dump (iGraphics3D* /*g3d*/)
00672   {
00673   }
00674   virtual bool DebugCommand (const char*)
00675   {
00676     return false;
00677   }
00678 };
00679 
00682 #endif // __CS_MATH3D_H__
00683 

Generated for Crystal Space by doxygen 1.3.9.1