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

Vector.h

Go to the documentation of this file.
00001 #ifndef _VECTOR_H_
00002 #define _VECTOR_H_
00003 
00004 #include "vuSimpleTypes.h"
00005 #include <math.h>
00006 #include "Matrix.h"
00007 
00008 //
00009 // The Vector class is a 3D homogeneous vector
00010 // that works in conjunction with the Matrix
00011 // class (a 4x4 matrix).  The elements of the
00012 // Vector are all single precision floating-
00013 // point numbers.
00014 //
00015 class Vector
00016 {
00017 friend class Matrix;
00018 public:
00019     //
00020     // Constructors and destructor.
00021     //
00022     inline Vector();
00023     inline Vector(const Vector& v);
00024     inline Vector(float v1, float v2, float v3);
00025     inline Vector(float v1, float v2, float v3, float v4);
00026     inline Vector(float v);
00027     inline Vector(float* v);
00028     inline ~Vector();
00029 
00030     //
00031     // Perform the 1-norm and 2-norm on the vector.
00032     // (The 2-norm is the same as the length).
00033     //
00034     inline float Norm(void);
00035     inline float Norm2(void);
00036 
00037     inline Vector& MakeUnit(void);
00038     inline Vector& Normalize(void);
00039 
00040     //
00041     // Pair-wise entry operations.
00042     //
00043     inline Vector Inv(void);
00044     inline Vector& InvEq(void);
00045     inline Vector Mul(Vector& rhs);
00046     inline Vector& MulEq(Vector& rhs);
00047     inline Vector Div(Vector& rhs);
00048     inline Vector& DivEq(Vector& rhs);
00049 
00050     //
00051     // Assignment and accessor operators.
00052     //
00053     inline Vector& operator=(const Vector& v);
00054     inline Vector& operator=(const float v);
00055     inline Vector& operator=(const float* v);
00056     inline float& operator[](dword index);
00057 
00058     //
00059     // Get the data pointer.
00060     //
00061     inline float* GetData(void);
00062 
00063     //
00064     // Dot and cross products.
00065     //
00066     inline friend float Dot(Vector& v1, Vector& v2);
00067     inline friend Vector Cross(Vector& v1, Vector& v2);
00068     inline float Dot(Vector& v);
00069     inline void Cross(Vector& v, Vector& r);
00070 
00071     //
00072     // Operators for vector-vector and vector-matrix
00073     // operations.
00074     //
00075     inline Vector operator+(Vector& v);
00076     inline Vector operator-(Vector& v);
00077     inline Vector operator*(Matrix& m);
00078     inline Matrix operator*(Vector& v);
00079     inline Vector operator*(float s);
00080     inline Vector operator/(float s);
00081     inline friend Vector operator*(float s, Vector& v);
00082     inline Vector& operator+=(Vector& v);
00083     inline Vector& operator-=(Vector& v);
00084     inline Vector& operator*=(Matrix& m);
00085     inline Vector& operator*=(float s);
00086     inline Vector& operator/=(float s);
00087 
00088     //
00089     // Equality and inequality operators.
00090     //
00091     inline bool operator==(const Vector& v) const;
00092     inline bool operator!=(const Vector& v) const;
00093 
00094 private:
00095     float val[4];
00096 };
00097 
00098 // Default constructor - set initial values
00099 inline Vector::Vector()
00100 {
00101     val[0] = val[1] = val[2] = 0.0f;
00102     val[3] = 1.0f;
00103 }
00104 
00105 // Copy constructor
00106 inline Vector::Vector(const Vector& v)
00107 {
00108     val[0] = v.val[0];
00109     val[1] = v.val[1];
00110     val[2] = v.val[2];
00111     val[3] = v.val[3];
00112 }
00113 
00114 // Constructor - set to <v1,v2,v3,1.0>
00115 inline Vector::Vector(float v1, float v2, float v3)
00116 {
00117     val[0] = v1;
00118     val[1] = v2;
00119     val[2] = v3;
00120     val[3] = 1.0f;
00121 }
00122 
00123 // Constructor - set to <v1,v2,v3,v4>
00124 inline Vector::Vector(float v1, float v2, float v3, float v4)
00125 {
00126     val[0] = v1;
00127     val[1] = v2;
00128     val[2] = v3;
00129     val[3] = v4;
00130 }
00131 
00132 // Constructor - set all values to v
00133 inline Vector::Vector(float v)
00134 {
00135     val[0] = val[1] = val[2] = v;
00136     val[3] = 1.0f;
00137 }
00138 
00139 // Constroctor - get values from an array
00140 inline Vector::Vector(float* v)
00141 {
00142     val[0] = v[0];
00143     val[1] = v[1];
00144     val[2] = v[2];
00145     val[3] = 1.0f;
00146 }
00147 
00148 // Destructor
00149 inline Vector::~Vector()
00150 {
00151 }
00152 
00153 // Compute the norm of the vector  - |v|
00154 inline float Vector::Norm(void)
00155 {
00156     return (float)sqrt((double)(val[0]*val[0]+val[1]*val[1]+val[2]*val[2]));
00157 }
00158 
00159 // Compute the square norm of the vector
00160 inline float Vector::Norm2(void)
00161 {
00162     return val[0]*val[0]+val[1]*val[1]+val[2]*val[2];
00163 }
00164 
00165 // Normalize the vector - set its length to 1
00166 inline Vector& Vector::MakeUnit(void)
00167 {
00168     float d = (float)sqrt((double)(val[0]*val[0]+val[1]*val[1]+val[2]*val[2]));
00169     if (d)
00170     {
00171         val[0] /= d;
00172         val[1] /= d;
00173         val[2] /= d;
00174     }
00175     else
00176     {
00177         val[0] = val[1] = val[2] = 0.0f;
00178     }
00179     return *this;
00180 }
00181 
00182 inline Vector& Vector::Normalize(void)
00183 {
00184     float d;
00185     d = (val[3])?(1.0f/val[3]):0.0f;
00186     val[0] *= d;
00187     val[1] *= d;
00188     val[2] *= d;
00189     val[3] = 1.0f;
00190     return *this;
00191 }
00192 
00193 // Invert the vector - divide all entries into 1
00194 inline Vector Vector::Inv(void)
00195 {
00196     Vector r;
00197     r[0] = (val[0])?(1.0f/val[0]):0.0f;
00198     r[1] = (val[1])?(1.0f/val[1]):0.0f;
00199     r[2] = (val[2])?(1.0f/val[2]):0.0f;
00200     return r;
00201 }
00202 
00203 // Invert and assign - divide all entries into 1
00204 // for current vector
00205 inline Vector& Vector::InvEq(void)
00206 {
00207     val[0] = (val[0])?(1.0f/val[0]):0.0f;
00208     val[1] = (val[1])?(1.0f/val[1]):0.0f;
00209     val[2] = (val[2])?(1.0f/val[2]):0.0f;
00210     return *this;
00211 }
00212 
00213 // Entry-wise multiplication with another vector
00214 inline Vector Vector::Mul(Vector& rhs)
00215 {
00216     Vector r;
00217     r[0] = val[0] * rhs.val[0];
00218     r[1] = val[1] * rhs.val[1];
00219     r[2] = val[2] * rhs.val[2];
00220     return r;
00221 }
00222 
00223 // Entry-wise multiplication with another vector, then
00224 // assign result to the current vector.
00225 inline Vector& Vector::MulEq(Vector& rhs)
00226 {
00227     val[0] *= rhs.val[0];
00228     val[1] *= rhs.val[1];
00229     val[2] *= rhs.val[2];
00230     return *this;
00231 }
00232 
00233 // Entry-wise division with another vector
00234 inline Vector Vector::Div(Vector& rhs)
00235 {
00236     Vector r;
00237     r[0] = (rhs.val[0])?(val[0]/rhs.val[0]):0.0f;
00238     r[1] = (rhs.val[1])?(val[1]/rhs.val[1]):0.0f;
00239     r[2] = (rhs.val[2])?(val[2]/rhs.val[2]):0.0f;
00240     return r;
00241 }
00242 
00243 // Entry-wise division with another vector, then
00244 // assign result to the current vector.
00245 inline Vector& Vector::DivEq(Vector& rhs)
00246 {
00247     val[0] = (rhs.val[0])?(val[0]/rhs.val[0]):val[0]=0.0f;
00248     val[1] = (rhs.val[1])?(val[1]/rhs.val[1]):val[1]=0.0f;
00249     val[2] = (rhs.val[2])?(val[2]/rhs.val[2]):val[2]=0.0f;
00250     return *this;
00251 }
00252 
00253 // Assignment operator
00254 inline Vector& Vector::operator=(const Vector& v)
00255 {
00256     if (this != &v)
00257     {
00258         val[0] = v.val[0];
00259         val[1] = v.val[1];
00260         val[2] = v.val[2];
00261         val[3] = v.val[3];
00262     }
00263     return *this;
00264 }
00265 
00266 // Assignment operator - set all values to v
00267 inline Vector& Vector::operator=(const float v)
00268 {
00269     val[0] = val[1] = val[2] = v;
00270     val[3] = 1.0f;
00271     return *this;
00272 }
00273 
00274 // Assignment operator - get values from an array
00275 inline Vector& Vector::operator=(const float* v)
00276 {
00277     val[0] = v[0];
00278     val[1] = v[1];
00279     val[2] = v[2];
00280     val[3] = 1.0f;
00281     return *this;
00282 }
00283 
00284 // Access operator - return the value at the given index
00285 inline float& Vector::operator[](dword index)
00286 {
00287     return val[index];
00288 }
00289 
00290 // Return the internal data pointer.
00291 inline float* Vector::GetData(void)
00292 {
00293     return val;
00294 }
00295 
00296 // Dot product friend function
00297 inline float Dot(Vector& v1, Vector& v2)
00298 {
00299     return v1.val[0]*v2.val[0]+v1.val[1]*v2.val[1]+v1.val[2]*v2.val[2];
00300 }
00301 
00302 // Cross product friend function
00303 inline Vector Cross(Vector& v1, Vector& v2)
00304 {
00305     Vector v;
00306     v.val[0] = (v1.val[1]*v2.val[2] - v1.val[2]*v2.val[1]);
00307         v.val[1] = (v1.val[2]*v2.val[0] - v1.val[0]*v2.val[2]);
00308     v.val[2] = (v1.val[0]*v2.val[1] - v1.val[1]*v2.val[0]);
00309     v.val[3] = 1.0f;
00310     return v;
00311 }
00312 
00313 // Dot product member function
00314 inline float Vector::Dot(Vector& v)
00315 {
00316     return val[0]*v.val[0]+val[1]*v.val[1]+val[2]*v.val[2];
00317 }
00318 
00319 // Cross product member function
00320 inline void Vector::Cross(Vector& v, Vector& r)
00321 {
00322         r.val[0] = (val[1]*v.val[2] - val[2]*v.val[1]);
00323     r.val[1] = (val[2]*v.val[0] - val[0]*v.val[2]);
00324     r.val[2] = (val[0]*v.val[1] - val[1]*v.val[0]);
00325     r.val[3] = 1.0f;
00326 }
00327 
00328 // Addition operator
00329 inline Vector Vector::operator+(Vector& v)
00330 {
00331     Vector r;
00332     r.val[0] = val[0] + v.val[0];
00333     r.val[1] = val[1] + v.val[1];
00334     r.val[2] = val[2] + v.val[2];
00335     r.val[3] = 1.0f;
00336     return r;
00337 }
00338 
00339 // Subtraction operator
00340 inline Vector Vector::operator-(Vector& v)
00341 {
00342     Vector r;
00343     r.val[0] = val[0] - v.val[0];
00344     r.val[1] = val[1] - v.val[1];
00345     r.val[2] = val[2] - v.val[2];
00346     r.val[3] = 1.0f;
00347     return r;
00348 }
00349 
00350 // Multiplication operator - vector*matrix
00351 inline Vector Vector::operator*(Matrix& m)
00352 {
00353     Vector r;
00354     dword i, j;
00355     r[3] = 0.0f;
00356     for(i=0;i<4;++i)
00357         for(j=0;j<4;++j)
00358             r.val[i] += m.val[(i<<2)+j]*val[j];
00359     return r;
00360 }
00361 
00362 // Multiplication operator - vector*vector -> matrix
00363 inline Matrix Vector::operator*(Vector& v)
00364 {
00365     Matrix r;
00366     dword i, j;
00367     for(i=0;i<4;++i)
00368         for(j=0;j<4;++j)
00369             r.val[(j<<2)+i] = val[i]*v.val[j];
00370     return r;
00371 }
00372 
00373 // Multiplication operator - vector*scalar
00374 inline Vector Vector::operator*(float s)
00375 {
00376     Vector r;
00377     r.val[0] = val[0] * s;
00378     r.val[1] = val[1] * s;
00379     r.val[2] = val[2] * s;
00380     r.val[3] = 1.0f;
00381     return r;
00382 }
00383 
00384 // Division operator - vector/scalar
00385 inline Vector Vector::operator/(float s)
00386 {
00387     Vector r;
00388     r.val[0] = val[0] / s;
00389     r.val[1] = val[1] / s;
00390     r.val[2] = val[2] / s;
00391     r.val[3] = 1.0f;
00392     return r;
00393 }
00394 
00395 // Multiplication operator - scalar*vector
00396 inline Vector operator*(float s, Vector& v)
00397 {
00398     Vector r;
00399     r.val[0] = v.val[0] * s;
00400     r.val[1] = v.val[1] * s;
00401     r.val[2] = v.val[2] * s;
00402     r.val[3] = 1.0f;
00403     return r;
00404 }
00405 
00406 // Add then assign operator
00407 inline Vector& Vector::operator+=(Vector& v)
00408 {
00409     val[0] += v.val[0];
00410     val[1] += v.val[1];
00411     val[2] += v.val[2];
00412     val[3] = 1.0f;
00413     return *this;
00414 }
00415 
00416 // Subtract then assign operator
00417 inline Vector& Vector::operator-=(Vector& v)
00418 {
00419     val[0] -= v.val[0];
00420     val[1] -= v.val[1];
00421     val[2] -= v.val[2];
00422     val[3] = 1.0f;
00423     return *this;
00424 }
00425 
00426 // Multiply then assign operator - vector*matrix
00427 inline Vector& Vector::operator*=(Matrix& m)
00428 {
00429     Vector r;
00430     dword i, j;
00431     r[3] = 0.0f;
00432     for(i=0;i<4;++i)
00433         for(j=0;j<4;++j)
00434             r.val[i] += m.val[(i<<2)+j]*val[j];
00435     return (*this=r);
00436 }
00437 
00438 // Multiply then assign operator - vector*scalar
00439 inline Vector& Vector::operator*=(float s)
00440 {
00441     val[0] *= s;
00442     val[1] *= s;
00443     val[2] *= s;
00444     val[3] = 1.0f;
00445     return *this;
00446 }
00447 
00448 // Divide then assign operator - vector/scalar
00449 inline Vector& Vector::operator/=(float s)
00450 {
00451     val[0] /= s;
00452     val[1] /= s;
00453     val[2] /= s;
00454     val[3] = 1.0f;
00455     return *this;
00456 }
00457 
00458 // Equality operator
00459 inline bool Vector::operator==(const Vector& v) const
00460 {
00461     return ((val[0]==v.val[0])&&(val[1]==v.val[1])&&(val[2]==v.val[2]));
00462 }
00463 
00464 // Inequality operator
00465 inline bool Vector::operator!=(const Vector& v) const
00466 {
00467     return !(operator==(v));
00468 }
00469 
00470 #endif

Generated on Wed Dec 15 21:20:32 2004 for vuVolume by  doxygen 1.3.9.1