Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | 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 
00033 namespace grinliz {
00034 
00035 template< class T>
00036 inline T DotProduct( const Vector2<T>& v0, const Vector2<T>& v1 )
00037 {
00038         return v0.x*v1.x + v0.y*v1.y;
00039 }
00040 
00041 template< class T>
00042 inline void CrossProduct( const Vector3<T>& u, const Vector3<T>& v, Vector3<T>* w )
00043 {
00044         GLASSERT( &u != w && &v != w );
00045 
00046         w->x = u.y * v.z - u.z * v.y;
00047         w->y = u.z * v.x - u.x * v.z;
00048         w->z = u.x * v.y - u.y * v.x;
00049 }
00050 
00051 template< class T>
00052 inline T DotProduct( const Vector3<T>& v0, const Vector3<T>& v1 )
00053 {
00054         return v0.x*v1.x + v0.y*v1.y + v0.z*v1.z;
00055 }
00056 
00057 
00058 struct BoundingSphere
00059 {
00060         Vector3F        point;
00061         float           rad;
00062 
00063 };
00064 
00065 
00071 struct Plane
00072 {
00074         static void CreatePlane( const Vector3F* vertices, Plane* plane );
00075 
00076         Vector3F        n;      // normal
00077         float           d;      // offset
00078 
00079         float Z( float x, float y )     { return -( d + (n.x * x) + (n.y * y) ) / ( n.z ); }
00080 
00081         void Normalize()
00082         {
00083                 float div = 1.0f / sqrtf( ( n.x * n.x ) + ( n.y * n.y ) + ( n.z * n.z ) );
00084                 n.x *= div;
00085                 n.y *= div;
00086                 n.z *= div;
00087                 d   *= div;
00088         }
00089 };
00090 
00091 
00096 class Quaternion
00097 {
00098   public:
00099     Quaternion()        { x = y = z = 0.0f; w = 1.0f; }
00100 
00101         void Normalize();
00102 
00103         const void ToAxisAngle( Vector3F* axis, float* angleOut );
00104         const void ToMatrix( Matrix4* matrix );
00105         void FromRotationMatrix( const Matrix4& matrix );
00106         void FromAxisAngle( const Vector3F& axis, float angle );
00107 
00108         static void SLERP( const Quaternion& start, const Quaternion& end, float t, Quaternion* result );
00109         static void Multiply( const Quaternion& a, const Quaternion& b, Quaternion* result );
00110 
00111         float x, y, z, w;
00112 };
00113 
00114 
00115 enum
00116 {
00117         REJECT = 0,             // No intersection.
00118         INTERSECT,              // Intersected
00119         POSITIVE,               // All comparison positive
00120         NEGATIVE,               // All Comparison negative
00121         INSIDE,                 // An intersection, expected to be outside of the object, is originating inside
00122 };
00123 
00124 enum 
00125 {
00126         // Reordering would be bad. x=0, y=1, z=2
00127         YZ_PLANE,
00128         XZ_PLANE,
00129         XY_PLANE
00130 };
00131 
00132 
00154 int ComparePlaneAABB( const Plane&, const Rectangle3F& aabb );
00155 
00156 // Seemed interesting...but doesn't actually work to cache the plane.
00157 //int CachePlaneAABB( const Plane& );
00158 //int ComparePlaneAABB( const Plane&, const Rect3& aabb, int cache );
00159 
00163 inline int IntersectPlaneAABB( const Plane& p, const Rectangle3F& aabb )
00164 {
00165         return ( ComparePlaneAABB( p, aabb ) == INTERSECT ) ? INTERSECT : REJECT; 
00166 }
00167 
00168 
00172 int IntersectRayTri(    const Vector3F& point, const Vector3F& dir,
00173                                                 const Vector3F& vert0, const Vector3F& vert1, const Vector3F& vert2,
00174                                                 Vector3F* intersect );
00175 
00180 int IntersectRayAAPlane(        const Vector3F& point, const Vector3F& dir,
00181                                                         int planeType, float planeLoc,
00182                                                         Vector3F* intersect,
00183                                                         float* t );
00184                                                  
00189 int IntersectRayAABB(   const Vector3F& origin, const Vector3F& dir,
00190                                                 const Rectangle3F& aabb,
00191                                                 Vector3F* intersect,
00192                                                 float* t );
00193 
00194 
00198 int IntersectRayZPlane( const Vector3F& origin, const Vector3F& dir,
00199                                                 float z,
00200                                                 Vector3F* intersect );
00201 
00202 };      // namespace grinliz
00203 #endif
00204 

Generated on Sun Sep 25 16:25:48 2005 for Kyra by  doxygen 1.4.3