AI_TVector3.h

Go to the documentation of this file.
00001 #ifndef AI__VECTOR3_H
00002 #define AI__VECTOR3_H
00003 //------------------------------------------------------------------------------
00012 #include "AI_Math.h"
00013 #include <float.h>
00014 
00015 class AI_TVector3
00016 {
00017 public:
00019     AI_TVector3();
00021     AI_TVector3(const float _x, const float _y, const float _z);
00023     AI_TVector3(const AI_TVector3& vec);
00025     void set(const float _x, const float _y, const float _z);
00027     void set(const AI_TVector3& vec);
00029     float len() const;
00031     float lensquared() const;
00033     void norm();
00035     void operator +=(const AI_TVector3& v0);
00037     void operator -=(const AI_TVector3& v0);
00039     void operator *=(float s);
00041     bool operator >(const AI_TVector3& rhs);
00043     bool operator <(const AI_TVector3& rhs);
00045     bool isequal(const AI_TVector3& v, float tol) const;
00047     int compare(const AI_TVector3& v, float tol) const;
00049     void rotate(const AI_TVector3& axis, float angle);
00051     void lerp(const AI_TVector3& v0, float lerpVal);
00053     void lerp(const AI_TVector3& v0, const AI_TVector3& v1, float lerpVal);
00055     AI_TVector3 findortho() const;
00057     void saturate();
00059     float dot(const AI_TVector3& v0) const;
00061     static float distance(const AI_TVector3& v0, const AI_TVector3& v1);
00062 
00063     float x, y, z;
00064 };
00065 
00066 //------------------------------------------------------------------------------
00069 inline
00070 AI_TVector3::AI_TVector3() :
00071     x(0.0f),
00072     y(0.0f),
00073     z(0.0f)
00074 {
00075     // empty
00076 }
00077 
00078 //------------------------------------------------------------------------------
00081 inline
00082 AI_TVector3::AI_TVector3(const float _x, const float _y, const float _z) :
00083     x(_x),
00084     y(_y),
00085     z(_z)
00086 {
00087     // empty
00088 }
00089 
00090 //------------------------------------------------------------------------------
00093 inline
00094 AI_TVector3::AI_TVector3(const AI_TVector3& vec) :
00095     x(vec.x),
00096     y(vec.y),
00097     z(vec.z)
00098 {
00099     // empty
00100 }
00101 
00102 //------------------------------------------------------------------------------
00105 inline
00106 void
00107 AI_TVector3::set(const float _x, const float _y, const float _z)
00108 {
00109     x = _x;
00110     y = _y;
00111     z = _z;
00112 }
00113 
00114 //------------------------------------------------------------------------------
00117 inline
00118 void
00119 AI_TVector3::set(const AI_TVector3& vec)
00120 {
00121     x = vec.x;
00122     y = vec.y;
00123     z = vec.z;
00124 }
00125 
00126 //------------------------------------------------------------------------------
00129 inline
00130 float
00131 AI_TVector3::len() const
00132 {
00133     return (float) ai_sqrt(x * x + y * y + z * z);
00134 }
00135 
00136 //------------------------------------------------------------------------------
00139 inline
00140 float 
00141 AI_TVector3::lensquared() const
00142 {
00143     return x * x + y * y + z * z;
00144 }
00145 
00146 //------------------------------------------------------------------------------
00149 inline
00150 void
00151 AI_TVector3::norm()
00152 {
00153     float l = len();
00154     if (l > AI_TINY) 
00155     {
00156         x /= l;
00157         y /= l;
00158         z /= l;
00159     }
00160 }
00161 
00162 //------------------------------------------------------------------------------
00165 inline
00166 void
00167 AI_TVector3::operator +=(const AI_TVector3& v0)
00168 {
00169     x += v0.x;
00170     y += v0.y;
00171     z += v0.z;
00172 }
00173 
00174 //------------------------------------------------------------------------------
00177 inline
00178 void
00179 AI_TVector3::operator -=(const AI_TVector3& v0)
00180 {
00181     x -= v0.x;
00182     y -= v0.y;
00183     z -= v0.z;
00184 }
00185 
00186 //------------------------------------------------------------------------------
00189 inline
00190 void
00191 AI_TVector3::operator *=(float s)
00192 {
00193     x *= s;
00194     y *= s;
00195     z *= s;
00196 }
00197 
00198 //------------------------------------------------------------------------------
00201 inline
00202 bool
00203 AI_TVector3::isequal(const AI_TVector3& v, float tol) const
00204 {
00205     if (fabs(v.x - x) > tol)      return false;
00206     else if (fabs(v.y - y) > tol) return false;
00207     else if (fabs(v.z - z) > tol) return false;
00208     return true;
00209 }
00210 
00211 //------------------------------------------------------------------------------
00214 inline
00215 int
00216 AI_TVector3::compare(const AI_TVector3& v, float tol) const
00217 {
00218     if (fabs(v.x - x) > tol)      return (v.x > x) ? +1 : -1; 
00219     else if (fabs(v.y - y) > tol) return (v.y > y) ? +1 : -1;
00220     else if (fabs(v.z - z) > tol) return (v.z > z) ? +1 : -1;
00221     else                          return 0;
00222 }
00223 
00224 //------------------------------------------------------------------------------
00227 inline
00228 void
00229 AI_TVector3::rotate(const AI_TVector3& axis, float angle)
00230 {
00231     // rotates this one around given vector. We do
00232     // rotation with matrices, but these aren't defined yet!
00233     float rotM[9];
00234     float sa, ca;
00235 
00236     sa = (float) sin(angle);
00237     ca = (float) cos(angle);
00238 
00239     // build a rotation matrix
00240     rotM[0] = ca + (1 - ca) * axis.x * axis.x;
00241     rotM[1] = (1 - ca) * axis.x * axis.y - sa * axis.z;
00242     rotM[2] = (1 - ca) * axis.z * axis.x + sa * axis.y;
00243     rotM[3] = (1 - ca) * axis.x * axis.y + sa * axis.z;
00244     rotM[4] = ca + (1 - ca) * axis.y * axis.y;
00245     rotM[5] = (1 - ca) * axis.y * axis.z - sa * axis.x;
00246     rotM[6] = (1 - ca) * axis.z * axis.x - sa * axis.y;
00247     rotM[7] = (1 - ca) * axis.y * axis.z + sa * axis.x;
00248     rotM[8] = ca + (1 - ca) * axis.z * axis.z;
00249 
00250     // "handmade" multiplication
00251     AI_TVector3 help(rotM[0] * this->x + rotM[1] * this->y + rotM[2] * this->z,
00252                   rotM[3] * this->x + rotM[4] * this->y + rotM[5] * this->z,
00253                   rotM[6] * this->x + rotM[7] * this->y + rotM[8] * this->z);
00254     *this = help;
00255 }
00256 
00257 //------------------------------------------------------------------------------
00260 static 
00261 inline 
00262 AI_TVector3 operator +(const AI_TVector3& v0, const AI_TVector3& v1) 
00263 {
00264     return AI_TVector3(v0.x + v1.x, v0.y + v1.y, v0.z + v1.z);
00265 }
00266 
00267 //------------------------------------------------------------------------------
00270 static 
00271 inline 
00272 AI_TVector3 operator -(const AI_TVector3& v0, const AI_TVector3& v1) 
00273 {
00274     return AI_TVector3(v0.x - v1.x, v0.y - v1.y, v0.z - v1.z);
00275 }
00276 
00277 //------------------------------------------------------------------------------
00280 static 
00281 inline 
00282 AI_TVector3 operator *(const AI_TVector3& v0, const float s) 
00283 {
00284     return AI_TVector3(v0.x * s, v0.y * s, v0.z * s);
00285 }
00286 
00287 //------------------------------------------------------------------------------
00290 static 
00291 inline 
00292 AI_TVector3 operator -(const AI_TVector3& v) 
00293 {
00294     return AI_TVector3(-v.x, -v.y, -v.z);
00295 }
00296 
00297 //------------------------------------------------------------------------------
00300 static 
00301 inline 
00302 AI_TVector3 operator /(const AI_TVector3& v0, const float s)
00303 {
00304     float one_over_s = 1.0f/s;
00305     return AI_TVector3(v0.x*one_over_s, v0.y*one_over_s, v0.z*one_over_s);
00306 }
00307 
00308 //------------------------------------------------------------------------------
00312 static
00313 inline
00314 float operator %(const AI_TVector3& v0, const AI_TVector3& v1)
00315 {
00316     return v0.x * v1.x + v0.y * v1.y + v0.z * v1.z;
00317 }
00318 
00319 //------------------------------------------------------------------------------
00323 static 
00324 inline 
00325 AI_TVector3 operator *(const AI_TVector3& v0, const AI_TVector3& v1) 
00326 {
00327     return AI_TVector3(v0.y * v1.z - v0.z * v1.y,
00328                     v0.z * v1.x - v0.x * v1.z,
00329                     v0.x * v1.y - v0.y * v1.x);
00330 }
00331 
00332 //------------------------------------------------------------------------------
00335 inline
00336 void
00337 AI_TVector3::lerp(const AI_TVector3& v0, float lerpVal)
00338 {
00339     x = v0.x + ((x - v0.x) * lerpVal);
00340     y = v0.y + ((y - v0.y) * lerpVal);
00341     z = v0.z + ((z - v0.z) * lerpVal);
00342 }
00343 
00344 //------------------------------------------------------------------------------
00347 inline
00348 void
00349 AI_TVector3::lerp(const AI_TVector3& v0, const AI_TVector3& v1, float lerpVal)
00350 {
00351     x = v0.x + ((v1.x - v0.x) * lerpVal);
00352     y = v0.y + ((v1.y - v0.y) * lerpVal);
00353     z = v0.z + ((v1.z - v0.z) * lerpVal);
00354 }
00355 
00356 //------------------------------------------------------------------------------
00359 inline
00360 void
00361 AI_TVector3::saturate()
00362 {
00363     x = ai_saturate(x);
00364     y = ai_saturate(y);
00365     z = ai_saturate(z);
00366 }
00367 
00368 //------------------------------------------------------------------------------
00373 inline
00374 AI_TVector3
00375 AI_TVector3::findortho() const
00376 {
00377     if (0.0 != x)
00378     {
00379         return AI_TVector3((-y - z) / x, 1.0, 1.0);
00380     } else
00381     if (0.0 != y)
00382     {
00383         return AI_TVector3(1.0, (-x - z) / y, 1.0);
00384     } else
00385     if (0.0 != z)
00386     {
00387         return AI_TVector3(1.0, 1.0, (-x - y) / z);
00388     } else
00389     {
00390         return AI_TVector3(0.0, 0.0, 0.0);
00391     }
00392 }
00393 
00394 //------------------------------------------------------------------------------
00398 inline
00399 float
00400 AI_TVector3::dot(const AI_TVector3& v0) const
00401 {
00402     return ( x * v0.x + y * v0.y + z * v0.z );
00403 }
00404 
00405 //------------------------------------------------------------------------------
00408 inline
00409 bool
00410 AI_TVector3::operator >(const AI_TVector3& rhs)
00411 {
00412     if ((this->x > rhs.x) || (this->y > rhs.y) || (this->z > rhs.z))
00413     {
00414         return true;
00415     }
00416     else
00417     {
00418         return false;
00419     }
00420 }
00421 
00422 //------------------------------------------------------------------------------
00425 inline
00426 bool
00427 AI_TVector3::operator <(const AI_TVector3& rhs)
00428 {
00429     if ((this->x < rhs.x) || (this->y < rhs.y) || (this->z < rhs.z))
00430     {
00431         return true;
00432     }
00433     else
00434     {
00435         return false;
00436     }
00437 }
00438 
00439 //------------------------------------------------------------------------------
00442 inline
00443 float
00444 AI_TVector3::distance(const AI_TVector3& v0, const AI_TVector3& v1)
00445 {
00446     AI_TVector3 v(v1 - v0);
00447     return (float) ai_sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
00448 }
00449 
00450 //------------------------------------------------------------------------------
00451 #endif