CrystalSpace

Public API Reference

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

csgeom/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 
00023 #ifndef __CS_CSSYSDEFS_H__
00024 #error "cssysdef.h must be included in EVERY source file!"
00025 #endif
00026 
00033 #include "csgeom/vector3.h"
00034 #include "csgeom/plane3.h"
00035 #include "csgeom/plane2.h"
00036 #include "csgeom/segment.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 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 Area3 (const csVector3 &a, const csVector3 &b,
00137                              const csVector3 &c)
00138   {
00139     csVector3 v1 = b - a;
00140     csVector3 v2 = c - a;
00141     return ((v1.y * v2.z + v1.z * v2.x + v1.x * v2.y) -
00142             (v1.y * v2.x + v1.x * v2.z + v1.z * v2.y));
00143   }
00144 
00150   inline static void CalcNormal (csVector3& norm,     const csVector3& v1,
00151                                  const csVector3& v2, const csVector3& v3)
00152   {
00153     norm = (v1-v2)%(v1-v3);
00154   }
00155 
00161   static void CalcNormal (csVector3& norm,
00162                           const csVector3& v, const csVector3& u)
00163   { norm = u%v; /* NOT v%u - vertexes are defined clockwise */ }
00164 
00171   static void CalcPlane (const csVector3& v1, const csVector3& v2,
00172          const csVector3& v3, csVector3& normal, float& D)
00173   {
00174     CalcNormal (normal, v1, v2, v3);
00175     D = - (normal * v1);
00176   }
00177 
00184   static bool PlanesEqual (const csPlane3& p1, const csPlane3& p2)
00185   {
00186     return ( ( p1.norm - p2.norm) < (float).001 ) &&
00187              (  ABS (p1.DD-p2.DD) < (float).001 );
00188   }
00189 
00195   static bool PlanesClose (const csPlane3& p1, const csPlane3& p2);
00196 
00204   static int OuterPlanes (const csBox3& box1, const csBox3& box2,
00205     csPlane3* planes);
00206 
00214   static int FindObserverSides (const csBox3& box1, const csBox3& box2,
00215         int* sides);
00216 
00222   static void SpherePosition (float angle_xz, float angle_vert,
00223         csVector3& pos);
00224 };
00225 
00230 class csSquaredDist
00231 {
00232 public:
00234   static float PointPoint (const csVector3& p1, const csVector3& p2)
00235   { return fSqr (p1.x - p2.x) + fSqr (p1.y - p2.y) + fSqr (p1.z - p2.z); }
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 csIntersect3
00261 {
00262 public:
00269   static bool IntersectPolygon (const csPlane3& plane, csPoly3D* poly,
00270         csSegment3& segment);
00271 
00281   static int IntersectSegment (csPlane3* planes, int num_planes,
00282         csSegment3& seg);
00283 
00289   static bool IntersectTriangle (const csVector3& tr1,
00290         const csVector3& tr2, const csVector3& tr3,
00291         const csSegment3& seg, csVector3& isect);
00292 
00297   static bool Plane (
00298     const csVector3& u, const csVector3& v,
00299     const csVector3& normal, const csVector3& a, // plane
00300     csVector3& isect, float& dist);              // intersection point
00301 
00310   static bool Plane (
00311     const csVector3& u, const csVector3& v,
00312     const csPlane3& p,                     // plane Ax+By+Cz+D=0
00313     csVector3& isect,                     // intersection point
00314     float& dist);                       // distance from u to isect
00315 
00321   static bool Planes (const csPlane3& p1, const csPlane3& p2,
00322         const csPlane3& p3, csVector3& isect);
00323 
00330   static bool PlaneXPlane (const csPlane3& p1, float x2, csPlane2& isect);
00331 
00338   static bool PlaneYPlane (const csPlane3& p1, float y2, csPlane2& isect);
00339 
00346   static bool PlaneZPlane (const csPlane3& p1, float z2, csPlane2& isect);
00347 
00354   static bool PlaneAxisPlane (const csPlane3& p1, int nr, float pos,
00355         csPlane2& isect)
00356   {
00357     switch (nr)
00358     {
00359       case 0: return PlaneXPlane (p1, pos, isect);
00360       case 1: return PlaneYPlane (p1, pos, isect);
00361       case 2: return PlaneZPlane (p1, pos, isect);
00362     }
00363     return false;
00364   }
00365 
00372   static float Z0Plane (
00373     const csVector3& v1, const csVector3& v2,
00374     csVector3& isect);                    // intersection point
00375 
00382   static float Z0Plane (
00383     const csSegment3& uv,
00384     csVector3& isect)                    // intersection point
00385   {
00386     return Z0Plane (uv.Start (), uv.End (), isect);
00387   }
00388 
00395   static float ZPlane (float zval,      // plane z = zval
00396     const csVector3& u, const csVector3& v,
00397     csVector3& isect);                    // intersection point
00398 
00405   static float ZPlane (float zval,      // plane z = zval
00406     const csSegment3& uv,
00407     csVector3& isect)                     // intersection point
00408   {
00409     return ZPlane (zval, uv.Start (), uv.End (), isect);
00410   }
00411 
00416   static float XFrustum (
00417     float A, const csVector3& u, const csVector3& v, csVector3& isect);
00418 
00423   static float XFrustum (
00424     float A, const csSegment3& uv, csVector3& isect)
00425   {
00426     return XFrustum (A, uv.Start (), uv.End (), isect);
00427   }
00428 
00433   static float YFrustum (
00434     float B, const csVector3& u, const csVector3& v, csVector3& isect);
00435 
00440   static float YFrustum (
00441     float B, const csSegment3& uv, csVector3& isect)
00442   {
00443     return YFrustum (B, uv.Start (), uv.End (), isect);
00444   }
00445 
00455   static int BoxSegment (const csBox3& box, const csSegment3& segment,
00456         csVector3& isect, float* pr = 0);
00457 
00467   static bool BoxFrustum (const csBox3& box, csPlane3* frustum,
00468         uint32 inClipMask, uint32& outClipMask);
00469 
00474   static bool BoxSphere (const csBox3& box, const csVector3& center,
00475                   float sqradius);
00476 };
00477 
00482 class csGeomDebugHelper : public iDebugHelper
00483 {
00484 public:
00485   csGeomDebugHelper ();
00486   virtual ~csGeomDebugHelper ();
00487 
00488   SCF_DECLARE_IBASE;
00489   virtual int GetSupportedTests () const
00490   {
00491     return CS_DBGHELP_UNITTEST;
00492   }
00493   virtual csPtr<iString> UnitTest ();
00494   virtual csPtr<iString> StateTest ()
00495   {
00496     return 0;
00497   }
00498   virtual csTicks Benchmark (int /*num_iterations*/)
00499   {
00500     return 0;
00501   }
00502   virtual csPtr<iString> Dump ()
00503   {
00504     return 0;
00505   }
00506   virtual void Dump (iGraphics3D* /*g3d*/)
00507   {
00508   }
00509   virtual bool DebugCommand (const char*)
00510   {
00511     return false;
00512   }
00513 };
00514 
00517 #endif // __CS_MATH3D_H__
00518 

Generated for Crystal Space by doxygen 1.2.18