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

vuVector.cpp

Go to the documentation of this file.
00001 #include <math.h>
00002 #include <stdio.h>
00003 #include "vuVector.h"
00004 #include "vuMatrix.h"
00005 
00006 // Default constructor - set initial values
00007 vuVector::vuVector()
00008 {
00009     val[0] = val[1] = val[2] = 0.0f;
00010     val[3] = 1.0f;
00011 }
00012 
00013 // Copy constructor
00014 vuVector::vuVector(const vuVector& v)
00015 {
00016     val[0] = v.val[0];
00017     val[1] = v.val[1];
00018     val[2] = v.val[2];
00019     val[3] = v.val[3];
00020 }
00021 
00022 // Constructor - set to <v1,v2,v3,1.0>
00023 vuVector::vuVector(float v1, float v2, float v3)
00024 {
00025     val[0] = v1;
00026     val[1] = v2;
00027     val[2] = v3;
00028     val[3] = 1.0f;
00029 }
00030 
00031 // Constructor - set to <v1,v2,v3,v4>
00032 vuVector::vuVector(float v1, float v2, float v3, float v4)
00033 {
00034     val[0] = v1;
00035     val[1] = v2;
00036     val[2] = v3;
00037     val[3] = v4;
00038 }
00039 
00040 // Constructor - set all values to v
00041 vuVector::vuVector(float v)
00042 {
00043     val[0] = val[1] = val[2] = v;
00044     val[3] = 1.0f;
00045 }
00046 
00047 // Constroctor - get values from an array
00048 vuVector::vuVector(const float* v)
00049 {
00050     val[0] = v[0];
00051     val[1] = v[1];
00052     val[2] = v[2];
00053     val[3] = 1.0f;
00054 }
00055 
00056 // Destructor
00057 vuVector::~vuVector()
00058 {
00059 }
00060 
00061 // Assignment operator
00062 vuVector& vuVector::operator=(const vuVector& v)
00063 {
00064     if (this != &v)
00065     {
00066         val[0] = v.val[0];
00067         val[1] = v.val[1];
00068         val[2] = v.val[2];
00069         val[3] = v.val[3];
00070     }
00071     return *this;
00072 }
00073 
00074 // Assignment operator - set all values to v
00075 vuVector& vuVector::operator=(float v)
00076 {
00077     val[0] = val[1] = val[2] = v;
00078     val[3] = 1.0f;
00079     return *this;
00080 }
00081 
00082 // Assignment operator - get values from an array
00083 vuVector& vuVector::operator=(const float* v)
00084 {
00085     val[0] = v[0];
00086     val[1] = v[1];
00087     val[2] = v[2];
00088     val[3] = 1.0f;
00089     return *this;
00090 }
00091 
00092 // Access operator - return the value at the given index
00093 float& vuVector::operator[](dword index)
00094 {
00095     return val[index];
00096 }
00097 
00098 // Const Access operator - return the value at the given index
00099 const float& vuVector::operator[](dword index) const
00100 {
00101     return val[index];
00102 }
00103 
00104 
00111 float* vuVector::getData(void)
00112 { return val; }
00113 
00114 float const* vuVector::getData(void) const
00115 { return val; }
00116 
00117 // Compute the norm of the vector  - |v|
00118 float vuVector::norm(void) const
00119 {
00120     return (float)sqrt((double)(val[0]*val[0]+val[1]*val[1]+val[2]*val[2]));
00121 }
00122 
00123 // Compute the square norm of the vector
00124 float vuVector::norm2(void) const
00125 {
00126     return val[0]*val[0]+val[1]*val[1]+val[2]*val[2];
00127 }
00128 
00129 // Normalize the vector - set its length to 1
00130 vuVector& vuVector::makeUnit(void)
00131 {
00132     float d = (float)sqrt((double)(val[0]*val[0]+val[1]*val[1]+val[2]*val[2]));
00133     if (d)
00134     {
00135         val[0] /= d;
00136         val[1] /= d;
00137         val[2] /= d;
00138     }
00139     else
00140     {
00141         val[0] = val[1] = val[2] = 0.0f;
00142     }
00143     return *this;
00144 }
00145 
00146 vuVector& vuVector::normalize(void)
00147 {
00148     float d;
00149     d = (val[3])?(1.0f/val[3]):0.0f;
00150     val[0] *= d;
00151     val[1] *= d;
00152     val[2] *= d;
00153     val[3] = 1.0f;
00154     return *this;
00155 }
00156 
00157 // Invert the vector - divide all entries into 1
00158 vuVector vuVector::inv(void) const
00159 {
00160     vuVector r;
00161     r[0] = (val[0])?(1.0f/val[0]):0.0f;
00162     r[1] = (val[1])?(1.0f/val[1]):0.0f;
00163     r[2] = (val[2])?(1.0f/val[2]):0.0f;
00164     return r;
00165 }
00166 
00167 // Invert and assign - divide all entries into 1
00168 // for current vector
00169 vuVector& vuVector::invEq(void)
00170 {
00171     val[0] = (val[0])?(1.0f/val[0]):0.0f;
00172     val[1] = (val[1])?(1.0f/val[1]):0.0f;
00173     val[2] = (val[2])?(1.0f/val[2]):0.0f;
00174     return *this;
00175 }
00176 
00177 // Entry-wise multiplication with another vector
00178 vuVector vuVector::mul(const vuVector& rhs) const
00179 {
00180     vuVector r;
00181     r[0] = val[0] * rhs.val[0];
00182     r[1] = val[1] * rhs.val[1];
00183     r[2] = val[2] * rhs.val[2];
00184     return r;
00185 }
00186 
00187 // Entry-wise multiplication with another vector, then
00188 // assign result to the current vector.
00189 vuVector& vuVector::mulEq(const vuVector& rhs)
00190 {
00191     val[0] *= rhs.val[0];
00192     val[1] *= rhs.val[1];
00193     val[2] *= rhs.val[2];
00194     return *this;
00195 }
00196 
00197 // Entry-wise division with another vector
00198 vuVector vuVector::div(const vuVector& rhs) const
00199 {
00200     vuVector r;
00201     r[0] = (rhs.val[0])?(val[0]/rhs.val[0]):0.0f;
00202     r[1] = (rhs.val[1])?(val[1]/rhs.val[1]):0.0f;
00203     r[2] = (rhs.val[2])?(val[2]/rhs.val[2]):0.0f;
00204     return r;
00205 }
00206 
00207 // Entry-wise division with another vector, then
00208 // assign result to the current vector.
00209 vuVector& vuVector::divEq(const vuVector& rhs)
00210 {
00211     val[0] = (rhs.val[0])?(val[0]/rhs.val[0]):val[0]=0.0f;
00212     val[1] = (rhs.val[1])?(val[1]/rhs.val[1]):val[1]=0.0f;
00213     val[2] = (rhs.val[2])?(val[2]/rhs.val[2]):val[2]=0.0f;
00214     return *this;
00215 }
00216 
00217 // Dot product friend function
00218 float dot(const vuVector& v1,const vuVector& v2)
00219 {
00220     return v1.val[0]*v2.val[0]+v1.val[1]*v2.val[1]+v1.val[2]*v2.val[2];
00221 }
00222 
00223 // Cross product friend function
00224 vuVector cross(const vuVector& v1,const vuVector& v2)
00225 {
00226     vuVector v;
00227     v.val[0] = (v1.val[1]*v2.val[2] - v1.val[2]*v2.val[1]);
00228     v.val[1] = (v1.val[2]*v2.val[0] - v1.val[0]*v2.val[2]);
00229     v.val[2] = (v1.val[0]*v2.val[1] - v1.val[1]*v2.val[0]);
00230     v.val[3] = 1.0f;
00231     return v;
00232 }
00233 
00234 // Dot product member function
00235 float vuVector::dot(const vuVector& v) const
00236 {
00237     return val[0]*v.val[0]+val[1]*v.val[1]+val[2]*v.val[2];
00238 }
00239 
00240 // Cross product member function
00241 vuVector vuVector::cross(const vuVector& v) const
00242 {
00243     vuVector r;
00244     r.val[0] = (val[1]*v.val[2] - val[2]*v.val[1]);
00245     r.val[1] = (val[2]*v.val[0] - val[0]*v.val[2]);
00246     r.val[2] = (val[0]*v.val[1] - val[1]*v.val[0]);
00247     r.val[3] = 1.0f;
00248 
00249     return r;
00250 }
00251 
00252 // Addition operator
00253 vuVector vuVector::operator+(const vuVector& v) const
00254 {
00255     vuVector r;
00256     r.val[0] = val[0] + v.val[0];
00257     r.val[1] = val[1] + v.val[1];
00258     r.val[2] = val[2] + v.val[2];
00259     r.val[3] = 1.0f;
00260     return r;
00261 }
00262 
00263 // Subtraction operator
00264 vuVector vuVector::operator-(const vuVector& v) const
00265 {
00266     vuVector r;
00267     r.val[0] = val[0] - v.val[0];
00268     r.val[1] = val[1] - v.val[1];
00269     r.val[2] = val[2] - v.val[2];
00270     r.val[3] = 1.0f;
00271     return r;
00272 }
00273 
00274 // Multiplication operator - vector*matrix
00275 vuVector vuVector::operator*(const vuMatrix& m) const
00276 {
00277     vuVector r;
00278     dword i, j;
00279     r[3] = 0.0f;
00280     for(i=0;i<4;++i)
00281         for(j=0;j<4;++j)
00282             r.val[i] += m.val[(i<<2)+j]*val[j];
00283     return r;
00284 }
00285 
00286 // Multiplication operator - vector*vector -> matrix
00287 vuMatrix vuVector::operator*(const vuVector& v) const
00288 {
00289     vuMatrix r;
00290     dword i, j;
00291     for(i=0;i<4;++i)
00292         for(j=0;j<4;++j)
00293             r.val[(j<<2)+i] = val[i]*v.val[j];
00294     return r;
00295 }
00296 
00297 // Multiplication operator - vector*scalar
00298 vuVector vuVector::operator*(float s) const
00299 {
00300     vuVector r;
00301     r.val[0] = val[0] * s;
00302     r.val[1] = val[1] * s;
00303     r.val[2] = val[2] * s;
00304     r.val[3] = 1.0f;
00305     return r;
00306 }
00307 
00308 // Division operator - vector/scalar
00309 vuVector vuVector::operator/(float s) const
00310 {
00311     vuVector r;
00312     r.val[0] = val[0] / s;
00313     r.val[1] = val[1] / s;
00314     r.val[2] = val[2] / s;
00315     r.val[3] = 1.0f;
00316     return r;
00317 }
00318 
00319 // Multiplication operator - scalar*vector
00320 const vuVector operator*(float s,const vuVector& v)
00321 {
00322     vuVector r;
00323     r.val[0] = v.val[0] * s;
00324     r.val[1] = v.val[1] * s;
00325     r.val[2] = v.val[2] * s;
00326     r.val[3] = 1.0f;
00327     return r;
00328 }
00329 
00330 // Add then assign operator
00331 vuVector& vuVector::operator+=(const vuVector& v)
00332 {
00333     val[0] += v.val[0];
00334     val[1] += v.val[1];
00335     val[2] += v.val[2];
00336     val[3] = 1.0f;
00337     return *this;
00338 }
00339 
00340 // Subtract then assign operator
00341 vuVector& vuVector::operator-=(const vuVector& v)
00342 {
00343     val[0] -= v.val[0];
00344     val[1] -= v.val[1];
00345     val[2] -= v.val[2];
00346     val[3] = 1.0f;
00347     return *this;
00348 }
00349 
00350 /*
00351 // Multiply then assign operator - vector*matrix
00352 vuVector& vuVector::operator*=(const vuMatrix& m)
00353 {
00354     vuVector r;
00355     dword i, j;
00356     r[3] = 0.0f;
00357     for(i=0;i<4;++i)
00358         for(j=0;j<4;++j)
00359             r.val[i] += m.val[(i<<2)+j]*val[j];
00360     return (*this=r);
00361 }
00362 */
00363 
00364 //WARNING: you should NOT use this function if you can,
00365 //becuase it is unclear weather it does vector*matrix
00366 //or matrix*vector when someone is simply reading through
00367 //your code.
00368 
00369 //Multiply then assign operator - matrix*vector
00370 //Old code (see above) does vector*matrix, which
00371 //is not the same. Since openGL uses column major
00372 //ordering and postmultiplies (matrix is left
00373 //of vector in multiplication), the above function
00374 //is not easily useable. Since I (Tai) seem to be the only one
00375 //using this function, if you change it back, please
00376 //make sure you modify CellProjector so that its
00377 //matrix multiplications work properly. Feel free to email me
00378 //at tmeng@sfu.ca if you have any questions.
00379 vuVector& vuVector::operator*=(const vuMatrix& m)
00380 {
00381     vuVector r;
00382     dword i, j;
00383     r[3] = 0.0f; //r starts out being a vector of all 0's.
00384     for(i=0;i<4;++i)
00385         for(j=0;j<4;++j)
00386                 //j<<2 is left shift; same as j * 4
00387           //column major ordering
00388           //1   5   9   13
00389           //2   6   10  14
00390           //3... etc
00391                 //it used to be m.val[(i<<2 + j)] which was wrong
00392                 r.val[i] += m.val[(j<<2)+i]*val[j];
00393     return (*this=r);
00394 }
00395 
00396 // Multiply then assign operator - vector*scalar
00397 vuVector& vuVector::operator*=(float s)
00398 {
00399     val[0] *= s;
00400     val[1] *= s;
00401     val[2] *= s;
00402     val[3] = 1.0f;
00403     return *this;
00404 }
00405 
00406 // Equality operator
00407 bool vuVector::operator==(const vuVector& v) const
00408 {
00409     return ((val[0]==v.val[0])&&(val[1]==v.val[1])&&(val[2]==v.val[2]));
00410 }
00411 
00412 // Inequality operator
00413 bool vuVector::operator!=(const vuVector& v) const
00414 {
00415     return !(operator==(v));
00416 }
00417 
00418 int vuVector::getDominantAxis() const
00419 {
00420   float a0 = fabs(val[0]);
00421   float a1 = fabs(val[1]);
00422   float a2 = fabs(val[2]);
00423   if(a0<a1) {
00424     if(a1<a2) return 2;
00425     else return 1;
00426   } else 
00427     if(a0<a2) return 2;
00428     else return 0;
00429 }
00430 
00431 void vuVector::print()
00432 {
00433   printf("( %4.4f,  %4.4f,  %4.4f,  %4.4f )\n",
00434           val[0], val[1], val[2], val[3]);
00435 }
00436 
00437 int strip_white_space (char* temp)
00438 
00439 {
00440         int position = 0;
00441 
00442         while ((temp [position] == ' ') || (temp [position] == '\t'))
00443                 position++;
00444 
00445         return position;
00446 }
00447 
00448 int get_next_comma (char* temp)
00449 
00450 {
00451         int position = 0;
00452 
00453         while ((temp [position] != ',') && (temp [position] != '\0'))
00454                 position++;
00455 
00456         return position;
00457 }
00458 
00459 int get_next_return (char* temp)
00460 
00461 {
00462         int position = 0;
00463 
00464         while ((temp [position] != '\n') && (temp [position] != '\0'))
00465                 position++;
00466 
00467         return position;
00468 }
00469 
00470 int get_next_blank (char* temp)
00471 
00472 {
00473         int position = 0;
00474 
00475         while ((temp [position] != ' ') && (temp [position] != '\t') && (temp [position] != '\0'))
00476                 position++;
00477 
00478         return position;
00479 }
00480 
00481 int clear_blanks (char* temp)
00482 
00483 {
00484         int position = 0;
00485 
00486         while (((temp [position] == ' ') || (temp [position] == '\t')) && (temp [position] != '\0'))
00487                 position++;
00488 
00489         return position;
00490 }
00491 
00492 int vuVector::load (char* load_from)
00493 
00494 {
00495         int position = 0;
00496 
00497         for (int i = 0; i < 3; i++)
00498 
00499         {
00500                 position += strip_white_space (&(load_from [position]));
00501 
00502                 val [i] = atof (&(load_from [position]));
00503 
00504                 position += get_next_comma (&(load_from [position]));
00505                 position++;
00506         }
00507 
00508         position += strip_white_space (&(load_from [position]));
00509         val [3] = atof (&(load_from [position]));
00510 
00511         position += get_next_blank (&(load_from [position]));
00512 
00513         return position;
00514 }
00515 
00516 void vuVector::save (char* save_to)
00517 
00518 {
00519         int position = 0;
00520         save_to [0] = '\0';
00521 
00522         char temp [64];
00523 
00524         for (int i = 0; i < 4; i++)
00525 
00526         {
00527                 gcvt (val [i], 14, temp);
00528 
00529                 strcat (save_to, temp);
00530 
00531                 position += strlen (temp);
00532 
00533                 if (i != 3)
00534                         strcat (save_to, ", ");
00535         }
00536 }
00537 
00538 ostream& operator<<(ostream& out,const vuVector& v)
00539 {
00540     //this one is from Chris, but it works better without comma,
00541     //hope this doesn't mess up things...
00542     //out << v[0] <<", "<< v[1] <<", "<< v[2] <<", "<< v[3];
00543     out<<v.val[0]<<" "<<v.val[1]<<" "<<v.val[2]<<" "<<v.val[3]<<endl;
00544     return out;
00545 }
00546 
00547 istream& operator>>(istream& in, vuVector& v)
00548 {
00549         in>>v.val[0];
00550         in>>v.val[1];
00551         in>>v.val[2];
00552         in>>v.val[3];
00553         return in;
00554 }
00555 
00556 vuString vuVector::getString()
00557 {
00558   vuString str;
00559   float tmp[3];
00560   char  buf[64];
00561 
00562   if (val[3] == 0)
00563     tmp[0] = tmp[1] = tmp[2] = 0.0f;
00564   else {
00565     tmp[0] = val[0] / val[3];
00566     tmp[1] = val[1] / val[3];
00567     tmp[2] = val[2] / val[3];
00568   }
00569 
00570   sprintf(buf,"[%4.4f,  %4.4f,  %4.4f]", tmp[0], tmp[1], tmp[2]);
00571 
00572   str = buf;
00573   return str;
00574 }

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