vector.h

Go to the documentation of this file.
00001 //=============================================================================
00016 #ifndef __VECTOR_H_INCLUDED__
00017 #define __VECTOR_H_INCLUDED__
00018 
00019 #include <math.h>
00020 
00021 
00022 //=============================================================================
00023 //  4D vector
00024 //=============================================================================
00025 
00030 class Vector4D
00031 {
00032 public:
00035     Vector4D( void ) : x(0.0f), y(0.0f), z(0.0f), w(0.0f)
00036     {
00037     }
00038 
00041     Vector4D( float X, float Y, float Z, float W ) : x(X), y(Y), z(Z), w(W)
00042     {
00043     }
00044 
00047     Vector4D( const float* xyzw ) : x(xyzw[0]), y(xyzw[1]), z(xyzw[2]), w(xyzw[3])
00048     {
00049     }
00050 
00052     inline int operator==(const Vector4D& v) const { return (x==v.x && y==v.y && z==v.z && w==v.w); }
00053 
00055     inline int operator!=(const Vector4D& v) const { return !(*this == v); }
00056 
00058     inline Vector4D operator-(void)                 const { return Vector4D(-x,-y,-z,-w); }
00059 
00061     inline Vector4D operator+(const Vector4D& v)    const { return Vector4D(x+v.x,y+v.y,z+v.z,w+v.w); }
00062 
00066     inline Vector4D operator-(const Vector4D& v)    const { return Vector4D(x-v.x,y-v.y,z-v.z,w-v.w); }
00067 
00072     inline Vector4D operator*(const float f)        const { return Vector4D(x*f,y*f,z*f,w*f); }
00073 
00075     inline float operator*(const Vector4D & v) const { return x*v.x + y*v.y + z*v.z + w*v.w; }
00076 
00080     inline const float* toFloatPointer( void ) const { return &x; }
00081 
00083     inline Vector4D modulate( const Vector4D & v ) const { return Vector4D( x*v.x, y*v.y, z*v.z, w*v.w ); }
00084 
00086     inline float lengthSq( void ) const { return x*x + y*y + z*z + w*w; }
00087 
00089     inline void colorNormalize( void )
00090     {
00091         if( x < 0.0f ) x = 0.0f;
00092         if( x > 1.0f ) x = 1.0f;
00093         if( y < 0.0f ) y = 0.0f;
00094         if( y > 1.0f ) y = 1.0f;
00095         if( z < 0.0f ) z = 0.0f;
00096         if( z > 1.0f ) z = 1.0f;
00097         if( w < 0.0f ) w = 0.0f;
00098         if( w > 1.0f ) w = 1.0f;
00099     }
00100 
00101     float x; 
00102     float y; 
00103     float z; 
00104     float w; 
00105 };
00106 
00107 
00108 //=============================================================================
00109 //  3D Vector
00110 //=============================================================================
00111 
00112 
00116 class Vector3D
00117 {
00118 public:
00119 
00121     Vector3D( float X=0.0f, float Y=0.0f, float Z=0.0f ) : x(X), y(Y), z(Z)
00122     {
00123     }
00124 
00126     Vector3D( const Vector4D & v ) : x(v.x), y(v.y), z(v.z)
00127     {
00128     }
00129 
00131     inline Vector3D operator+(const Vector3D &v) const { return Vector3D(x+v.x,y+v.y,z+v.z); }
00132 
00136     inline Vector3D operator-(const Vector3D &v) const { return Vector3D(x-v.x,y-v.y,z-v.z); }
00137 
00142     inline Vector3D operator*(const float f) const { return Vector3D(x*f,y*f,z*f); }
00143 
00147     inline const float* toFloatPointer( void ) const { return &x; }
00148 
00150     inline float lengthSq( void ) const
00151     {
00152         return x*x + y*y + z*z;
00153     }
00154 
00158     inline Vector3D normalize( void ) const
00159     {
00160         float len = lengthSq();
00161         if( len > 0.0f )
00162         {
00163             return (*this) * ( 1.0f / sqrtf( len ) );
00164         }
00165         return Vector3D( 0,0,0 );
00166     }
00167 
00168 
00172     inline float dotProduct( const Vector3D & v ) const
00173     {
00174         return x*v.x + y*v.y + z*v.z;
00175     }
00176 
00177 
00182     inline Vector3D crossProduct( const Vector3D & v ) const
00183     {
00184         return Vector3D(
00185             y * v.z - v.y * z,
00186             z * v.x - v.z * x,
00187             x * v.y - v.x * y
00188             );
00189     }
00190 
00191 
00194     inline Vector3D absolute( void ) const
00195     {
00196         return Vector3D( (float)fabs( x ),
00197                          (float)fabs( y ),
00198                          (float)fabs( z ) );
00199     }
00200 
00201 
00204     inline float absoluteCoordMaximum( void ) const
00205     {
00206         float ax = (float)fabs( x );
00207         float ay = (float)fabs( y );
00208         float az = (float)fabs( z );
00209 
00210         if( ax > ay )
00211         {
00212             return ( ax > az ) ? ax : az;
00213         }
00214 
00215         // now: ax <= ay
00216         return ( ay > az ) ? ay : az;
00217     }
00218 
00219     float x; 
00220     float y; 
00221     float z; 
00222 };
00223 
00224 
00225 //=============================================================================
00226 //  2D Vector
00227 //=============================================================================
00228 
00233 class Vector2D
00234 {
00235 public:
00236 
00238     Vector2D( float X=0.0f, float Y=0.0f ) : x(X), y(Y)
00239     {
00240     }
00241 
00243     inline Vector2D operator+(const Vector2D &v) const { return Vector2D(x+v.x,y+v.y); }
00244 
00248     inline Vector2D operator-(const Vector2D &v) const { return Vector2D(x-v.x,y-v.y); }
00249 
00254     inline Vector2D operator*(const float f) const { return Vector2D(x*f,y*f); }
00255 
00260     inline const float* toFloatPointer( void ) const { return &x; }
00261 
00262     float x; 
00263     float y; 
00264 };
00265 
00266 
00267 //=============================================================================
00268 //  4x4 matrix
00269 //=============================================================================
00270 
00274 class Matrix4x4
00275 {
00276 public:
00279     Matrix4x4( void )
00280     {
00281         for( int i = 0 ; i < 16 ; i++ )
00282             m[i] = 0.0f;
00283         m[0] = m[5] = m[10] = m[15] = 1.0f;
00284     }
00285 
00286 
00289     inline Vector4D operator*( const Vector4D & v ) const
00290     {
00291         // m contains a column major order matrix!
00292         return Vector4D(
00293             m[0] * v.x + m[4] * v.y + m[ 8] * v.z + m[12] * v.w,
00294             m[1] * v.x + m[5] * v.y + m[ 9] * v.z + m[13] * v.w,
00295             m[2] * v.x + m[6] * v.y + m[10] * v.z + m[14] * v.w,
00296             m[3] * v.x + m[7] * v.y + m[11] * v.z + m[15] * v.w );
00297     }
00298 
00299 
00303     inline void setTranslationVector( const Vector3D & t )
00304     {
00305         m[12] = t.x;
00306         m[13] = t.y;
00307         m[14] = t.z;
00308     }
00309 
00314     float* toFloatPointer( void ) { return m; }
00315 
00320     const float* toConstFloatPointer( void ) const { return m; }
00321 
00322 private:
00323     float m[ 16 ]; // this is the 4x4 column major order matrix.
00324 };
00325 
00326 
00327 //=============================================================================
00328 //  type name shortcuts
00329 //=============================================================================
00330 
00331 typedef Vector4D  vec4_t; 
00332 typedef Vector3D  vec3_t; 
00333 typedef Vector2D  vec2_t; 
00334 typedef Matrix4x4 mat4_t; 
00335 
00336 
00337 #endif  // __VECTOR_H_INCLUDED__

Generated on Sun Mar 2 17:12:31 2008 for Shader Maker by  doxygen 1.5.4