- Main Page
- Classes
- Files
- Directories
- Related Pages

# glgeometry.h

    00001 /*
    00002 Copyright (c) 2000-2003 Lee Thomason (www.grinninglizard.com)
    00003 Grinning Lizard Utilities.
    00004
    00005 This software is provided 'as-is', without any express or implied
    00006 warranty. In no event will the authors be held liable for any
    00007 damages arising from the use of this software.
    00008
    00009 Permission is granted to anyone to use this software for any
    00010 purpose, including commercial applications, and to alter it and
    00011 redistribute it freely, subject to the following restrictions:
    00012
    00013 1. The origin of this software must not be misrepresented; you must
    00014 not claim that you wrote the original software. If you use this
    00015 software in a product, an acknowledgment in the product documentation
    00016 would be appreciated but is not required.
    00017
    00018 2. Altered source versions must be plainly marked as such, and
    00019 must not be misrepresented as being the original software.
    00020
    00021 3. This notice may not be removed or altered from any source
    00022 distribution.
    00023 */
    00024
    00025
    00026 #ifndef GRINLIZ_GEOMETRY_INCLUDED
    00027 #define GRINLIZ_GEOMETRY_INCLUDED
    00028
    00029 #include "glvector.h"
    00030 #include "glrectangle.h"
    00031 #include "glmatrix.h"
    00032 #include "glmemorypool.h"
    00033
    00034 #include <vector>
    00035
    00036 namespace grinliz {
    00037
    00039 template< class T>
    00040 inline void CrossProduct( const Vector2<T>& u, const Vector2<T>& v, Vector3<T>* w )
    00041 {
    00042         w->x = 0.0f;
    00043         w->y = 0.0f;
    00044         w->z = u.x * v.y - u.y * v.x;
    00045 }
    00046
    00047
    00049 template< class T>
    00050 inline T DotProduct( const Vector2<T>& v0, const Vector2<T>& v1 )
    00051 {
    00052         return v0.x*v1.x + v0.y*v1.y;
    00053 }
    00054
    00055
    00057 template< class T>
    00058 inline void CrossProduct( const Vector3<T>& u, const Vector3<T>& v, Vector3<T>* w )
    00059 {
    00060         GLASSERT( &u != w && &v != w );
    00061
    00062         w->x = u.y * v.z - u.z * v.y;
    00063         w->y = u.z * v.x - u.x * v.z;
    00064         w->z = u.x * v.y - u.y * v.x;
    00065 }
    00066
    00067
    00069 template< class T>
    00070 inline Vector3<T> CrossProduct( const Vector3<T>& u, const Vector3<T>& v )
    00071 {
    00072         Vector3<T> w;
    00073
    00074         w.x = u.y * v.z - u.z * v.y;
    00075         w.y = u.z * v.x - u.x * v.z;
    00076         w.z = u.x * v.y - u.y * v.x;
    00077         return w;
    00078 }
    00079
    00080
    00082 template< class T>
    00083 inline T DotProduct( const Vector3<T>& v0, const Vector3<T>& v1 )
    00084 {
    00085         return v0.x*v1.x + v0.y*v1.y + v0.z*v1.z;
    00086 }
    00087
    00088
    00089 struct BoundingSphere
    00090 {
    00091         Vector3F        point;
    00092         float           rad;
    00093
    00094 };
    00095
    00096
    00099 struct Ray
    00100 {
    00101         Vector3F        origin;
    00102         Vector3F        direction;      // should always have a length of 1.0f
    00103         float           length;
    00104 };
    00105
    00110 struct Plane
    00111 {
    00113         static void CreatePlane( const Vector3F* vertices, Plane* plane );
    00114
    00115         Vector3F        n;
    00116         float           d;
    00117
    00118         float Z( float x, float y ) const { return -( d + (n.x * x) + (n.y * y) ) / ( n.z ); }
    00119
    00120         void Normalize()
    00121         {
    00122                 float div = 1.0f / sqrtf( ( n.x * n.x ) + ( n.y * n.y ) + ( n.z * n.z ) );
    00123                 n.x *= div;
    00124                 n.y *= div;
    00125                 n.z *= div;
    00126                 d   *= div;
    00127         }
    00128
    00129 //      void Negative( Plane* p ) const
    00130 //      {
    00131 //              p->n.x = -n.x;
    00132 //              p->n.y = -n.y;
    00133 //              p->n.z = -n.z;
    00134 //              p->d = -d;
    00135 //      }
    00136
    00138         void ProjectToPlane( const Vector3F& vector, Vector3F* projected ) const;
    00139 };
    00140
    00141 float CalcSphereTexture( float x, float y, bool roundHigh );
    00142
    00143 enum TessellateSphereMode
    00144 {
    00145         TESS_SPHERE_NORMAL,
    00146         TESS_SPHERE_TOPHALF,
    00147         TESS_SPHERE_MIRROR,
    00148 };
    00149
    00163 void TessellateSphere( int iterations, float radius, bool innerNormals,
    00164                                           std::vector< Vector3F >* _vertex,
    00165                                           std::vector< U32 >* _index,
    00166                                           std::vector< Vector3F >* _normal = 0,
    00167                                           std::vector< Vector2F >* _texture = 0,
    00168                                           TessellateSphereMode mode = TESS_SPHERE_NORMAL );
    00169
    00170
    00173 struct LineNode
    00174 {
    00175         grinliz::Vector2F       point;          // the point on the line
    00176         LineNode*       prev;
    00177         LineNode*       next;
    00178         float           value;                          // optional interpolation info
    00179
    00180         LineNode( const grinliz::Vector2F& p ) : point( p ), prev( 0 ), next( 0 ), value( 0.0f ) {}
    00181         LineNode( float x, float y ) : prev( 0 ), next( 0 ), value( 0.0f ) { point.Set( x, y ); }
    00182         LineNode( float x, float y, float val ) : prev( 0 ), next( 0 ), value( val ) { point.Set( x, y ); }
    00183         ~LineNode() {}
    00184
    00185         void* operator new( size_t size ) {
    00186                 GLASSERT( sizeof( LineNode ) == size );
    00187                 return pool.Alloc();
    00188         }
    00189
    00190         void operator delete( void* mem ) {     pool.Free( mem ); }
    00191
    00192         static MemoryPool pool;
    00193 };
    00194
    00195
    00201 class LineLoop
    00202 {
    00203 public:
    00205         LineLoop()      : first( 0 )    {}
    00206         ~LineLoop()     { Clear(); }
    00207
    00209         void Clear();
    00211         void Delete( LineNode* del );
    00222         void AddAtEnd( LineNode* node );
    00224         void AddAfter( LineNode* me, LineNode* add );
    00226         LineNode* First()       { return first; }
    00228         const LineNode* First() const   { return first; }
    00233         void SortToTop();
    00235         void Bounds( grinliz::Rectangle2F* bounds );
    00236
    00241         void Render( float* surface, int width, int height, bool fill=false );
    00242
    00243         #ifdef DEBUG
    00244         void Dump();
    00245         #endif
    00246
    00247 private:
    00248         LineNode* first;
    00249 };
    00250
    00251
    00257 template< int X0, int X1, int XC >
    00258 struct Quad3F
    00259 {
    00260         enum { NUM_VERTEX = 4 };
    00261         Vector3F vertex[NUM_VERTEX];
    00262
    00263         Quad3F( float x0, float y0, float x1, float y1, float c )
    00264         {
    00265                 vertex[0].X(X0) = x0;   vertex[0].X(X1) = y0;   vertex[0].X(XC) = c;
    00266                 vertex[1].X(X0) = x1;   vertex[1].X(X1) = y0;   vertex[1].X(XC) = c;
    00267                 vertex[2].X(X0) = x1;   vertex[2].X(X1) = y1;   vertex[2].X(XC) = c;
    00268                 vertex[3].X(X0) = x0;   vertex[3].X(X1) = y1;   vertex[3].X(XC) = c;
    00269         }
    00270 };
    00271
    00272 typedef Quad3F< 0, 1, 2 > Quad3Fz;
    00273
    00274
    00280 template< int X0, int X1 >
    00281 struct Quad2F
    00282 {
    00283         enum { NUM_VERTEX = 4 };
    00284         Vector2F vertex[NUM_VERTEX];
    00285
    00286         Quad2F( float x0, float y0, float x1, float y1 )
    00287         {
    00288                 vertex[0].X(X0) = x0;   vertex[0].X(X1) = y0;
    00289                 vertex[1].X(X0) = x1;   vertex[1].X(X1) = y0;
    00290                 vertex[2].X(X0) = x1;   vertex[2].X(X1) = y1;
    00291                 vertex[3].X(X0) = x0;   vertex[3].X(X1) = y1;
    00292         }
    00293 };
    00294
    00295 typedef Quad2F< 0, 1 > Quad2Fz;
    00296
    00297
    00298
    00303 class Quaternion
    00304 {
    00305 public:
    00306         Quaternion() : x( 0.0f ), y( 0.0f ), z( 0.0f ), w( 1.0f )       {}
    00307
    00308         void Normalize();
    00309
    00310         const void ToAxisAngle( Vector3F* axis, float* angleOut );
    00311         const void ToMatrix( Matrix4* matrix );
    00312         void FromRotationMatrix( const Matrix4& matrix );
    00313         void FromAxisAngle( const Vector3F& axis, float angle );
    00314
    00315         static void SLERP( const Quaternion& start, const Quaternion& end, float t, Quaternion* result );
    00316         static void Multiply( const Quaternion& a, const Quaternion& b, Quaternion* result );
    00317
    00318         float x, y, z, w;
    00319 };
    00320
    00321
    00322 enum
    00323 {
    00324         REJECT = 0,             // No intersection.
    00325         INTERSECT,              // Intersected
    00326         POSITIVE,               // All comparison positive
    00327         NEGATIVE,               // All Comparison negative
    00328         INSIDE,                 // An intersection, expected to be outside of the object, is originating inside
    00329 };
    00330
    00331 enum
    00332 {
    00333         // Reordering would be bad. x=0, y=1, z=2
    00334         YZ_PLANE,
    00335         XZ_PLANE,
    00336         XY_PLANE
    00337 };
    00338
    00339
    00361 int ComparePlaneAABB( const Plane&, const Rectangle3F& aabb );
    00362
    00366 int ComparePlanePoint( const Plane&, const Vector3F& p );
    00367
    00371 inline int IntersectPlaneAABB( const Plane& p, const Rectangle3F& aabb )
    00372 {
    00373         return ( ComparePlaneAABB( p, aabb ) == INTERSECT ) ? INTERSECT : REJECT;
    00374 }
    00375
    00376
    00381 int IntersectRayTri(    const Vector3F& point, const Vector3F& dir,
    00382                                                 const Vector3F& vert0, const Vector3F& vert1, const Vector3F& vert2,
    00383                                                 Vector3F* intersect );
    00384
    00389 int IntersectRayAAPlane(        const Vector3F& point, const Vector3F& dir,
    00390                                                         int planeType, float planeLoc,
    00391                                                         Vector3F* intersect,
    00392                                                         float* t );
    00393
    00398 int IntersectRayAABB(   const Vector3F& origin, const Vector3F& dir,
    00399                                                 const Rectangle3F& aabb,
    00400                                                 Vector3F* intersect,
    00401                                                 float* t );
    00402
    00408 int IntersectionRayAABB(        const Ray& ray,
    00409                                                         const Rectangle3F& aabb,
    00410                                                         Rectangle3F* result );
    00411
    00415 int IntersectRayZPlane( const Vector3F& origin, const Vector3F& dir,
    00416                                                 float z,
    00417                                                 Vector3F* intersect );
    00418
    00423 int IntersectLinePlane( const Vector3F& a0, const Vector3F& a1,
    00424                                                 const Plane& plane,
    00425                                                 Vector3F* out,
    00426                                                 float* t );
    00427
    00433 int IntersectLineLine(  const Vector3F& a0, const Vector3F& a1,
    00434                                                 const Vector3F& b0, const Vector3F& b1,
    00435                                                 Vector3F* out0,
    00436                                                 Vector3F* out1 );
    00437
    00443 int IntersectLineLine(  const Vector2F& a0, const Vector2F& a1,
    00444                                                 const Vector2F& b0, const Vector2F& b1,
    00445                                                 Vector2F* out,
    00446                                                 float* t0,
    00447                                                 float* t1 );
    00448
    00449
    00453 int PointInPolygon( const Vector2F& p, const Vector2F* vertex, int numVertex );
    00454
    00458 float PointBetweenPoints( const Vector3F& p0, const Vector3F& p1, const Vector3F& pi );
    00459
    00464 int Intersect3Planes( const Plane& p0, const Plane& p1, const Plane& p2, Vector3F* intersection );
    00465
    00466 };      // namespace grinliz
    00467 #endif
    00468