Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

glvector.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_VECTOR_INCLUDED
00027 #define GRINLIZ_VECTOR_INCLUDED
00028 
00029 #include <math.h>
00030 #include "gldebug.h"
00031 #include "glmath.h"
00032 #include "glutil.h"
00033 
00034 
00035 namespace grinliz {
00036 
00037 template< class T >
00038 struct Vector2
00039 {
00040         T x;
00041         T y;
00042 
00043         T X( int i ) const              {       GLASSERT( InRange( i, 0, 1 ));
00044                                                                 return *( &x + i ); }
00045         T* pX( int i )                  {       GLASSERT( InRange( i, 0, 1 ));
00046                                                                 return &x + i; }
00047 
00048         void Add( const Vector2<T>& vec ) {
00049                 x += vec.x;
00050                 y += vec.y;
00051         }
00052         void Subtract( const Vector2<T>& vec ) {
00053                 x -= vec.x;
00054                 y -= vec.y;
00055         }
00056 
00057         friend void Subtract( const Vector2<T>& head, const Vector2<T>& tail, Vector2<T>* vec ) {
00058                 vec->x = head.x - tail.x;
00059                 vec->y = head.y - tail.y;
00060         }
00061         
00062         void operator+=( const Vector2<T>& vec )                { x += vec.x; y += vec.y; }
00063         bool operator==( const Vector2<T>& rhs ) const  { return x == rhs.x && y == rhs.y; }
00064 
00065         void Set( T x, T y ) {
00066                 this->x = x;
00067                 this->y = y;
00068         }
00069         void Zero() {
00070                 x = (T)0;
00071                 y = (T)0;
00072         }
00073 
00074         T Length() const { return grinliz::Length( x, y ); };
00075 
00076         void Normalize()        
00077         { 
00078                 T lenInv = static_cast<T>(1) / Length( x, y );
00079                 x *= lenInv; y *= lenInv;
00080                 #ifdef DEBUG
00081                 float len = x*x + y*y;
00082                 GLASSERT( len > .9999f && len < 1.0001f );
00083                 #endif
00084         }
00085 
00086         void RotatePos90()
00087         {
00088                 T a = x;
00089                 T b = y;
00090                 x = -b;
00091                 y = a;
00092         }
00093         void RotateNeg90()
00094         {
00095                 T a = x;
00096                 T b = y;
00097                 x = b;
00098                 y = -a;
00099         }
00100 };
00101 
00102 typedef Vector2< int > Vector2I;
00103 typedef Vector2< float > Vector2F;
00104 
00105 
00106 template< class T >
00107 struct Line2
00108 {
00109         Vector2<T>      n;      // normal
00110         T                       d;              // offset
00111 
00112         T EvaluateX( T y )              { return ( -d - y*n.y ) / n.x; }
00113         T EvaluateY( T x )              { return ( -d - x*n.x ) / n.y; }
00114 };
00115 
00116 typedef Line2< float > Line2F;
00117 
00118 
00119 
00120 template< class T >
00121 struct Vector3
00122 {
00123         T x;
00124         T y;
00125         T z;
00126 
00127         T X( int i ) const              {       GLASSERT( InRange( i, 0, 2 ));
00128                                                                 return *( &x + i ); }
00129         T* pX( int i )                  {       GLASSERT( InRange( i, 0, 2 ));
00130                                                                 return &x + i; }
00131 
00132         void Add( const Vector3<T>& vec ) {
00133                 x += vec.x;
00134                 y += vec.y;
00135                 z += vec.z;
00136         }
00137         void Subtract( const Vector3<T>& vec ) {
00138                 x -= vec.x;
00139                 y -= vec.y;
00140                 z -= vec.z;
00141         }
00142 
00143         friend void Subtract( const Vector3<T>& head, const Vector3<T>& tail, Vector3<T>* vec ) {
00144                 vec->x = head.x - tail.x;
00145                 vec->y = head.y - tail.y;
00146                 vec->z = head.z - tail.z;
00147         }
00148         
00149         friend Vector3<T> operator-( const Vector3<T>& head, const Vector3<T>& tail ) {
00150                 Vector3<T> vec;
00151                 vec.x = head.x - tail.x;
00152                 vec.y = head.y - tail.y;
00153                 vec.z = head.z - tail.z;
00154                 return vec;
00155         }
00156 
00157         friend Vector3<T> operator+( const Vector3<T>& head, const Vector3<T>& tail ) {
00158                 Vector3<T> vec;
00159                 vec.x = head.x + tail.x;
00160                 vec.y = head.y + tail.y;
00161                 vec.z = head.z + tail.z;
00162                 return vec;
00163         }
00164         
00165         friend Vector3<T> operator*( const Vector3<T>& head, float scalar ) {
00166                 Vector3<T> vec = head;
00167                 vec.x *= scalar;
00168                 vec.y *= scalar;
00169                 vec.z *= scalar;
00170                 return vec;
00171         }
00172 
00173         void operator+=( const Vector3<T>& vec )                { x += vec.x; y += vec.y; z += vec.z; }
00174         bool operator==( const Vector3<T>& rhs ) const  { return x == rhs.x && y == rhs.y && z == rhs.z; }
00175 
00176         void Set( T x, T y, T z ) {
00177                 this->x = x;
00178                 this->y = y;
00179                 this->z = z;
00180         }
00181 
00182         void Zero() {
00183                 x = (T)0;
00184                 y = (T)0;
00185                 z = (T)0;
00186         }
00187 
00188         void Normalize()        
00189         { 
00190                 GLASSERT( grinliz::Length( x, y, z ) > 0.001f );
00191                 T lenInv = static_cast<T>(1) / grinliz::Length( x, y, z );
00192                 x *= lenInv; 
00193                 y *= lenInv;
00194                 z *= lenInv;
00195                 #ifdef DEBUG
00196                 float len = x*x + y*y + z*z;
00197                 GLASSERT( len > .9999f && len < 1.0001f );
00198                 #endif
00199         }
00200 
00201         T Length() const { return grinliz::Length( x, y, z ); };
00202 };
00203 
00204 typedef Vector3< int > Vector3I;
00205 typedef Vector3< float > Vector3F;
00206 
00207 
00208 struct Vector4F
00209 {
00210         float x, y, z, w;
00211 
00212         void operator+=( const Vector4F& rhs )  { x += rhs.x; y += rhs.y; z += rhs.z; w += rhs.w; }
00213 };
00214 
00215 };      // namespace grinliz
00216 
00217 
00218 #endif

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