Main Page   Modules   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages   Examples  

math/vector3.cpp

Go to the documentation of this file.
00001 #include "vector3.h"
00002 
00007 #include "matrix4.h"
00008 
00009 #include <glt/gl.h>
00010 #include <glt/glu.h>
00011 #include <glt/viewport.h>
00012 
00013 #include <misc/string.h>
00014 #include <cassert>
00015 #include <cmath>
00016 
00017 #include <iostream>
00018 #include <algorithm>
00019 using namespace std;
00020 
00021 const Vector VectorX(1.0,0.0,0.0);
00022 const Vector VectorY(0.0,1.0,0.0);
00023 const Vector VectorZ(0.0,0.0,1.0);
00024 const Vector Vector0(0.0,0.0,0.0);
00025 const Vector Vector1(1.0,1.0,1.0);
00026 
00027 Vector::Vector()
00028 {
00029     _vector[0] = _vector[1] = _vector[2] = 0.0;
00030 }
00031 
00032 Vector::Vector(const Vector &v)
00033 {
00034     _vector[0] = v._vector[0];
00035     _vector[1] = v._vector[1];
00036     _vector[2] = v._vector[2];
00037 }
00038 
00039 Vector::Vector(const real x, const real y, const real z)
00040 {
00041     _vector[0] = x;
00042     _vector[1] = y;
00043     _vector[2] = z;
00044 }
00045 
00046 Vector::Vector(const float *v)
00047 {
00048     _vector[0] = v[0];
00049     _vector[1] = v[1];
00050     _vector[2] = v[2];
00051 }
00052 
00053 Vector::Vector(const double *v)
00054 {
00055     _vector[0] = v[0];
00056     _vector[1] = v[1];
00057     _vector[2] = v[2];
00058 }
00059 
00060 Vector::Vector(const string &str)
00061 {
00062     #ifndef NDEBUG
00063     const int n = 
00064     #endif
00065         atoc(str,atof,"+-eE.0123456789",_vector+0,_vector+3);
00066         
00067     assert(n==3);
00068 }
00069 
00070 const real &
00071 Vector::operator[](const int i) const
00072 {
00073     assert(i>=0 && i<3);
00074     return _vector[i];
00075 }
00076 
00077 real &
00078 Vector::operator[](const int i)
00079 {
00080     assert(i>=0 && i<3);
00081     return _vector[i];
00082 }
00083 
00084 Vector::operator real *()
00085 {
00086     return (real *) _vector;
00087 }
00088 
00089       real &Vector::x()       { return _vector[0]; }
00090 const real &Vector::x() const { return _vector[0]; }
00091       real &Vector::y()       { return _vector[1]; }
00092 const real &Vector::y() const { return _vector[1]; }
00093       real &Vector::z()       { return _vector[2]; }
00094 const real &Vector::z() const { return _vector[2]; }
00095 
00096 bool
00097 Vector::operator==(const Vector &v) const
00098 {
00099     return 
00100         _vector[0] == v[0] &&
00101         _vector[1] == v[1] &&
00102         _vector[2] == v[2];
00103 }
00104 
00105 bool 
00106 Vector::operator!=(const Vector &v) const
00107 {
00108     return 
00109         _vector[0] != v[0] ||
00110         _vector[1] != v[1] ||
00111         _vector[2] != v[2];
00112 }
00113 
00114 bool
00115 Vector::operator==(const real &a) const
00116 {
00117     return 
00118         _vector[0] == a &&
00119         _vector[1] == a &&
00120         _vector[2] == a;
00121 }
00122 
00123 bool 
00124 Vector::operator< (const Vector &v) const
00125 {
00126     if (x()!=v.x()) return x()<v.x();
00127     if (y()!=v.y()) return y()<v.y();
00128     return z()<v.z();
00129 }
00130 
00131 bool 
00132 Vector::operator> (const Vector &v) const
00133 {
00134     if (x()!=v.x()) return x()>v.x();
00135     if (y()!=v.y()) return y()>v.y();
00136     return z()>v.z();
00137 }
00138 
00139 ostream &
00140 Vector::writePov(ostream &os) const
00141 {
00142     os << "< ";
00143     os << x() << ',';
00144     os << y() << ',';
00145     os << z() << " >";
00146 
00147     return os;
00148 }
00149 
00150 Vector
00151 Vector::operator-() const
00152 {
00153     return Vector(-_vector[0], -_vector[1], -_vector[2]);
00154 }
00155 
00156 void 
00157 Vector::scale(const real &x)
00158 {
00159     _vector[0] *= x;
00160     _vector[1] *= x;
00161     _vector[2] *= x;
00162 }
00163 
00164 void 
00165 Vector::scale(const Vector &x)
00166 {
00167     _vector[0] *= x._vector[0];
00168     _vector[1] *= x._vector[1];
00169     _vector[2] *= x._vector[2];
00170 }
00171 
00172 void
00173 Vector::normalize()
00174 {
00175     const real mag = sqrt(norm());
00176 
00177     if (mag == 0.0)
00178         return;
00179 
00180     const real magInv = 1.0/mag;
00181 
00182     _vector[0] *= magInv;
00183     _vector[1] *= magInv;
00184     _vector[2] *= magInv;
00185 }
00186 
00187 void 
00188 Vector::abs()
00189 {
00190     _vector[0] = fabs(_vector[0]);
00191     _vector[1] = fabs(_vector[1]);
00192     _vector[2] = fabs(_vector[2]);
00193 }
00194 
00195 int
00196 Vector::dominant() const
00197 {
00198     const real x = fabs(_vector[0]);
00199     const real y = fabs(_vector[1]);
00200     const real z = fabs(_vector[2]);
00201 
00202     if (x>y && x>z)
00203         return 0;
00204     else
00205         if (y>z)
00206             return 1;
00207         else
00208             return 2;
00209 }
00210 
00211 real
00212 Vector::norm() const
00213 {
00214     return ((*this)*(*this));
00215 }
00216 
00217 real
00218 Vector::length() const
00219 {
00220     return sqrt((*this)*(*this));
00221 }
00222 
00223 bool
00224 Vector::project(const Matrix &model,const Matrix &proj,const GltViewport &view)
00225 {
00226     // TODO - No need to copy from x,y,z if real is GLdouble
00227     
00228     GLdouble x,y,z;
00229     GLint ret = gluProject(_vector[0],_vector[1],_vector[2],model,proj,view,&x,&y,&z);
00230 
00231     _vector[0] = x;
00232     _vector[1] = y;
00233     _vector[2] = z;
00234 
00235     return ret==GL_TRUE;
00236 }
00237 
00238 Vector &
00239 Vector::operator=(const Vector &x)
00240 {
00241     _vector[0] = x[0];
00242     _vector[1] = x[1];
00243     _vector[2] = x[2];
00244 
00245     return *this;
00246 }
00247 
00248 Vector &
00249 Vector::operator=(const float *x)
00250 {
00251     _vector[0] = x[0];
00252     _vector[1] = x[1];
00253     _vector[2] = x[2];
00254 
00255     return *this;
00256 }
00257 
00258 Vector &
00259 Vector::operator=(const double *x)
00260 {
00261     _vector[0] = x[0];
00262     _vector[1] = x[1];
00263     _vector[2] = x[2];
00264 
00265     return *this;
00266 }
00267 
00268 real
00269 Vector::operator*(const Vector &x) const
00270 {
00271     return _vector[0]*x[0] + _vector[1]*x[1] + _vector[2]*x[2];
00272 }
00273 
00274 Vector & 
00275 Vector::operator+=(const Vector &v)
00276 {
00277     _vector[0] += v[0];
00278     _vector[1] += v[1];
00279     _vector[2] += v[2];
00280 
00281     return *this;
00282 }
00283 
00284 Vector & 
00285 Vector::operator-=(const Vector &x)
00286 {
00287     _vector[0] -= x[0];
00288     _vector[1] -= x[1];
00289     _vector[2] -= x[2];
00290 
00291     return *this;
00292 }
00293 
00294 Vector &
00295 Vector::operator*=(const real &x)
00296 {
00297     _vector[0] *= x;
00298     _vector[1] *= x;
00299     _vector[2] *= x;
00300 
00301     return *this;
00302 }
00303 
00304 Vector &
00305 Vector::operator*=(const Matrix &m)
00306 {
00307     return operator=(m*(*this));
00308 }
00309 
00310 real
00311 Vector::dist(const Vector &x) const
00312 {
00313     return ((*this)-x).norm();
00314 }
00315 
00316 Vector &
00317 Vector::vmin(const Vector &v)
00318 {
00319     _vector[0] = MIN(_vector[0],v[0]);
00320     _vector[1] = MIN(_vector[1],v[1]);
00321     _vector[2] = MIN(_vector[2],v[2]);
00322     return *this;
00323 }
00324 
00325 Vector &
00326 Vector::vmax(const Vector &v)
00327 {
00328     _vector[0] = MAX(_vector[0],v[0]);
00329     _vector[1] = MAX(_vector[1],v[1]);
00330     _vector[2] = MAX(_vector[2],v[2]);
00331     return *this;
00332 }
00333 
00335 //
00336 // OpenGL
00337 
00338 #include <glt/gl.h>
00339 
00340 void
00341 Vector::glVertex() const
00342 {
00343     #ifdef GLT_FAST_FLOAT
00344     glVertex3fv(_vector);
00345     #else
00346     glVertex3dv(_vector);
00347     #endif
00348 }
00349 
00350 void
00351 Vector::glNormal() const
00352 {
00353     #ifdef GLT_FAST_FLOAT
00354     glNormal3fv(_vector);
00355     #else
00356     glNormal3dv(_vector);
00357     #endif
00358 }
00359 
00360 void
00361 Vector::glColor() const
00362 {
00363     #ifdef GLT_FAST_FLOAT
00364     glColor3fv(_vector);
00365     #else
00366     glColor3dv(_vector);
00367     #endif
00368 }
00369 
00370 void
00371 Vector::glTexCoord() const
00372 {
00373     #ifdef GLT_FAST_FLOAT
00374     glTexCoord3fv(_vector);
00375     #else
00376     glTexCoord3dv(_vector);
00377     #endif
00378 }
00379 
00381 
00387 ostream &
00388 operator<<(ostream &os, const Vector &x)
00389 {
00390     os << x[0] << '\t';
00391     os << x[1] << '\t';
00392     os << x[2];
00393 
00394     return os;
00395 }
00396 
00402 istream &
00403 operator>>(istream &is, Vector &x)
00404 {
00405     is >> x[0];
00406     is >> x[1];
00407     is >> x[2];
00408 
00409     return is;
00410 }
00411 
00417 Vector
00418 operator*(const real x, const Vector &v)
00419 {
00420     return v*x;
00421 }
00422 
00428 Vector
00429 operator*(const Vector &v, const real x)
00430 {
00431     return Vector( x*v._vector[0], x*v._vector[1], x*v._vector[2]);
00432 }
00433 
00439 Vector
00440 operator/(const Vector &v, const real x)
00441 {
00442     assert(x != 0.0);
00443     const real inv = 1.0/x;
00444     return v*inv;
00445 }
00446 
00452 Vector
00453 operator+(const Vector &v1, const Vector &v2)
00454 {
00455     return Vector(v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2]);
00456 }
00457 
00463 Vector
00464 operator-(const Vector &v1, const Vector &v2)
00465 {
00466     return Vector(v1[0] - v2[0], v1[1] - v2[1], v1[2] - v2[2]);
00467 }
00468 
00474 Vector 
00475 xProduct(const Vector &v1, const Vector &v2)
00476 {
00477     return Vector(v1[1]*v2[2] - v1[2]*v2[1],
00478                   v1[2]*v2[0] - v1[0]*v2[2],
00479                   v1[0]*v2[1] - v1[1]*v2[0]);
00480 }
00481 
00487 Vector 
00488 planeNormal(const Vector &v1, const Vector &v2,const Vector &v3)
00489 {
00490     return xProduct(v2-v1,v3-v1);
00491 }
00492 
00498 Vector 
00499 polar(const real lat,const real longitude)
00500 {
00501     return 
00502         Vector(
00503             cos(lat*M_PI_DEG) * cos(longitude*M_PI_DEG),
00504             sin(lat*M_PI_DEG),
00505             cos(lat*M_PI_DEG) * sin(longitude*M_PI_DEG)
00506         );
00507 }
00508 
00514 void orthogonalSystem(Vector &a,Vector &b,Vector &c)
00515 {
00516     a.normalize();
00517 
00518     if (fabs(a.z())>0.8)
00519     {
00520         b = xProduct(a,VectorY);
00521         c = xProduct(a,b);
00522     }
00523     else
00524     {
00525         b = xProduct(a,VectorZ);
00526         c = xProduct(a,b);
00527     }
00528     
00529     b.normalize();
00530     c.normalize();
00531 }
00532 
00533 
00534 

Generated on Tue Nov 5 11:11:04 2002 for GLT by doxygen1.2.18