00001 #include <math.h>
00002 #include <stdio.h>
00003 #include "vuVector.h"
00004 #include "vuMatrix.h"
00005
00006
00007 vuVector::vuVector()
00008 {
00009 val[0] = val[1] = val[2] = 0.0f;
00010 val[3] = 1.0f;
00011 }
00012
00013
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
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
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
00041 vuVector::vuVector(float v)
00042 {
00043 val[0] = val[1] = val[2] = v;
00044 val[3] = 1.0f;
00045 }
00046
00047
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
00057 vuVector::~vuVector()
00058 {
00059 }
00060
00061
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
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
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
00093 float& vuVector::operator[](dword index)
00094 {
00095 return val[index];
00096 }
00097
00098
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
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
00124 float vuVector::norm2(void) const
00125 {
00126 return val[0]*val[0]+val[1]*val[1]+val[2]*val[2];
00127 }
00128
00129
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
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
00168
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
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
00188
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
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
00208
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
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
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
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
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
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
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
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
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
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
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
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
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
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
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379 vuVector& vuVector::operator*=(const vuMatrix& m)
00380 {
00381 vuVector r;
00382 dword i, j;
00383 r[3] = 0.0f;
00384 for(i=0;i<4;++i)
00385 for(j=0;j<4;++j)
00386
00387
00388
00389
00390
00391
00392 r.val[i] += m.val[(j<<2)+i]*val[j];
00393 return (*this=r);
00394 }
00395
00396
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
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
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
00541
00542
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 }