Main Page | Class List | File List

vector3f.h

00001 //-----------------------------------------------------------------------------
00002 //           Name: vector3f.h
00003 //         Author: Kevin Harris
00004 //  Last Modified: 04/25/02
00005 //    Description: OpenGL compatible utility class for a 3D vector of floats
00006 //                 NOTE: This class has been left unoptimized for readability.
00007 //-----------------------------------------------------------------------------
00008 
00009 #ifndef _VECTOR3F_H_
00010 #define _VECTOR3F_H_
00011 
00012 #include <cmath>
00013 
00014 class vector3f
00015 {
00016 public:
00017 
00018     float x;
00019     float y;
00020     float z;
00021 
00022     vector3f(void)
00023     {
00024         x = 0.0f;
00025         y = 0.0f;
00026         z = 0.0f;
00027     }
00028 
00029     vector3f(float x_, float y_, float z_);
00030     void set(float x_, float y_, float z_);
00031     float length(void);
00032     void normalize(void);
00033 
00034     // Static utility methods
00035     static float distance(const vector3f &v1, const vector3f &v2);
00036     static float dotProduct(const vector3f &v1,  const vector3f &v2 );
00037     static vector3f crossProduct(const vector3f &v1, const vector3f &v2);
00038 
00039     // Operators...
00040     vector3f operator + (const vector3f &other);
00041     vector3f operator - (const vector3f &other);
00042     vector3f operator * (const vector3f &other);
00043         vector3f operator / (const vector3f &other);
00044 
00045     vector3f operator * (const float scalar);
00046     friend vector3f operator * (const float scalar, const vector3f &other);
00047     
00048         vector3f& operator = (const vector3f &other);
00049     vector3f& operator += (const vector3f &other);
00050     vector3f& operator -= (const vector3f &other);
00051 
00052         vector3f operator + (void) const;
00053         vector3f operator - (void) const;
00054 };
00055 
00056 vector3f::vector3f( float x_, float y_, float z_ )
00057 {
00058     x = x_;
00059     y = y_;
00060     z = z_;
00061 }
00062 
00063 void vector3f::set( float x_, float y_, float z_ )
00064 {
00065     x = x_;
00066     y = y_;
00067     z = z_;
00068 }
00069 
00070 float vector3f::length( void )
00071 {
00072     return( (float)sqrt( x * x + y * y + z * z ) );
00073 }
00074 
00075 void vector3f::normalize( void )
00076 {
00077         float fLength = length();
00078 
00079         x = x / fLength;
00080         y = y / fLength;
00081         z = z / fLength;
00082 }
00083 
00084 // Static utility methods...
00085 
00086 static float distance( const vector3f &v1,  const vector3f &v2  )
00087 {
00088         float dx = v1.x - v2.x;
00089         float dy = v1.y - v2.y;
00090         float dz = v1.z - v2.z;
00091 
00092         return (float)sqrt( dx * dx + dy * dy + dz * dz );
00093 }
00094 
00095 static float dotProduct( const vector3f &v1,  const vector3f &v2 )
00096 {
00097         return( v1.x * v2.x + v1.y * v2.y + v1.z * v2.z  );
00098 }
00099 
00100 static vector3f crossProduct( const vector3f &v1,  const vector3f &v2 )
00101 {
00102         vector3f vCrossProduct;
00103 
00104         vCrossProduct.x =  v1.y * v2.z - v1.z * v2.y;
00105         vCrossProduct.y = -v1.x * v2.z + v1.z * v2.x;
00106         vCrossProduct.z =  v1.x * v2.y - v1.y * v2.x;
00107 
00108         return vCrossProduct;
00109 }
00110 
00111 // Operators...
00112 
00113 vector3f vector3f::operator + ( const vector3f &other )
00114 {
00115     vector3f vResult(0.0f, 0.0f, 0.0f);
00116 
00117     vResult.x = x + other.x;
00118     vResult.y = y + other.y;
00119     vResult.z = z + other.z;
00120 
00121     return vResult;
00122 }
00123 
00124 vector3f vector3f::operator + ( void ) const
00125 {
00126     return *this;
00127 }
00128 
00129 vector3f vector3f::operator - ( const vector3f &other )
00130 {
00131     vector3f vResult(0.0f, 0.0f, 0.0f);
00132 
00133     vResult.x = x - other.x;
00134     vResult.y = y - other.y;
00135     vResult.z = z - other.z;
00136 
00137     return vResult;
00138 }
00139 
00140 vector3f vector3f::operator - ( void ) const
00141 {
00142     vector3f vResult(-x, -y, -z);
00143 
00144     return vResult;
00145 }
00146 
00147 vector3f vector3f::operator * ( const vector3f &other )
00148 {
00149     vector3f vResult(0.0f, 0.0f, 0.0f);
00150 
00151     vResult.x = x * other.x;
00152     vResult.y = y * other.y;
00153     vResult.z = z * other.z;
00154 
00155     return vResult;
00156 }
00157 
00158 vector3f vector3f::operator * ( const float scalar )
00159 {
00160     vector3f vResult(0.0f, 0.0f, 0.0f);
00161 
00162     vResult.x = x * scalar;
00163     vResult.y = y * scalar;
00164     vResult.z = z * scalar;
00165 
00166     return vResult;
00167 }
00168 
00169 vector3f operator * ( const float scalar, const vector3f &other )
00170 {
00171     vector3f vResult(0.0f, 0.0f, 0.0f);
00172 
00173     vResult.x = other.x * scalar;
00174     vResult.y = other.y * scalar;
00175     vResult.z = other.z * scalar;
00176 
00177     return vResult;
00178 }
00179 
00180 /*
00181 D3DXINLINE D3DXVECTOR3 operator * ( FLOAT f, CONST struct D3DXVECTOR3& v )
00182 {
00183     return D3DXVECTOR3(f * v.x, f * v.y, f * v.z);
00184 }
00185 */
00186 
00187 vector3f vector3f::operator / ( const vector3f &other )
00188 {
00189     vector3f vResult(0.0f, 0.0f, 0.0f);
00190 
00191     vResult.x = x / other.x;
00192     vResult.y = y / other.y;
00193     vResult.z = z / other.z;
00194 
00195     return vResult;
00196 }
00197 
00198 vector3f& vector3f::operator = ( const vector3f &other )
00199 {
00200         x = other.x;
00201         y = other.y;
00202         z = other.z;
00203 
00204     return *this;
00205 }
00206 
00207 vector3f& vector3f::operator += ( const vector3f &other )
00208 {
00209     x += other.x;
00210     y += other.y;
00211     z += other.z;
00212 
00213     return *this;
00214 }
00215 
00216 vector3f& vector3f::operator -= ( const vector3f &other )
00217 {
00218     x -= other.x;
00219     y -= other.y;
00220     z -= other.z;
00221 
00222     return *this;
00223 }
00224 
00225 #endif /* _VECTOR3F_H_ */

Generated on Mon Jan 26 22:13:13 2004 for fluid3 by doxygen 1.3.5