v3dmath.h

Go to the documentation of this file.
00001 
00032 #ifndef LL_V3DMATH_H
00033 #define LL_V3DMATH_H
00034 
00035 #include "llerror.h"
00036 #include "v3math.h"
00037 
00038 class LLVector3d
00039 {
00040         public:
00041                 F64 mdV[3];
00042 
00043                 const static LLVector3d zero;
00044                 const static LLVector3d x_axis;
00045                 const static LLVector3d y_axis;
00046                 const static LLVector3d z_axis;
00047                 const static LLVector3d x_axis_neg;
00048                 const static LLVector3d y_axis_neg;
00049                 const static LLVector3d z_axis_neg;
00050 
00051                 inline LLVector3d();                                                    // Initializes LLVector3d to (0, 0, 0)
00052                 inline LLVector3d(const F64 x, const F64 y, const F64 z);                       // Initializes LLVector3d to (x. y, z)
00053                 inline explicit LLVector3d(const F64 *vec);                             // Initializes LLVector3d to (vec[0]. vec[1], vec[2])
00054                 inline explicit LLVector3d(const LLVector3 &vec);
00055                 LLVector3d(const LLSD& sd)
00056                 {
00057                         setValue(sd);
00058                 }
00059 
00060                 void setValue(const LLSD& sd)
00061                 {
00062                         mdV[0] = sd[0].asReal();
00063                         mdV[1] = sd[1].asReal();
00064                         mdV[2] = sd[2].asReal();
00065                 }
00066 
00067                 const LLVector3d& operator=(const LLSD& sd)
00068                 {
00069                         setValue(sd);
00070                         return *this;
00071                 }
00072 
00073                 LLSD getValue() const
00074                 {
00075                         LLSD ret;
00076                         ret[0] = mdV[0];
00077                         ret[1] = mdV[1];
00078                         ret[2] = mdV[2];
00079                         return ret;
00080                 }
00081 
00082                 inline BOOL isFinite() const;                                                                   // checks to see if all values of LLVector3d are finite
00083                 BOOL            clamp(const F64 min, const F64 max);            // Clamps all values to (min,max), returns TRUE if data changed
00084                 BOOL            abs();                                          // sets all values to absolute value of original value (first octant), returns TRUE if changed
00085 
00086                 inline const LLVector3d&        clearVec();                                             // Clears LLVector3d to (0, 0, 0, 1)
00087                 inline const LLVector3d&        zeroVec();                                              // Zero LLVector3d to (0, 0, 0, 0)
00088                 inline const LLVector3d&        setVec(const F64 x, const F64 y, const F64 z);  // Sets LLVector3d to (x, y, z, 1)
00089                 inline const LLVector3d&        setVec(const LLVector3d &vec);  // Sets LLVector3d to vec
00090                 inline const LLVector3d&        setVec(const F64 *vec);                 // Sets LLVector3d to vec
00091                 inline const LLVector3d&        setVec(const LLVector3 &vec);
00092 
00093                 F64             magVec() const;                         // Returns magnitude of LLVector3d
00094                 F64             magVecSquared() const;          // Returns magnitude squared of LLVector3d
00095                 inline F64              normVec();                                      // Normalizes and returns the magnitude of LLVector3d
00096 
00097                 const LLVector3d&       rotVec(const F64 angle, const LLVector3d &vec); // Rotates about vec by angle radians
00098                 const LLVector3d&       rotVec(const F64 angle, const F64 x, const F64 y, const F64 z);         // Rotates about x,y,z by angle radians
00099                 const LLVector3d&       rotVec(const LLMatrix3 &mat);                           // Rotates by LLMatrix4 mat
00100                 const LLVector3d&       rotVec(const LLQuaternion &q);                          // Rotates by LLQuaternion q
00101 
00102                 BOOL isNull() const;                    // Returns TRUE if vector has a _very_small_ length
00103                 BOOL isExactlyZero() const              { return !mdV[VX] && !mdV[VY] && !mdV[VZ]; }
00104 
00105                 const LLVector3d&       operator=(const LLVector4 &a);
00106 
00107                 F64 operator[](int idx) const { return mdV[idx]; }
00108                 F64 &operator[](int idx) { return mdV[idx]; }
00109 
00110                 friend LLVector3d operator+(const LLVector3d &a, const LLVector3d &b);  // Return vector a + b
00111                 friend LLVector3d operator-(const LLVector3d &a, const LLVector3d &b);  // Return vector a minus b
00112                 friend F64 operator*(const LLVector3d &a, const LLVector3d &b);         // Return a dot b
00113                 friend LLVector3d operator%(const LLVector3d &a, const LLVector3d &b);  // Return a cross b
00114                 friend LLVector3d operator*(const LLVector3d &a, const F64 k);                          // Return a times scaler k
00115                 friend LLVector3d operator/(const LLVector3d &a, const F64 k);                          // Return a divided by scaler k
00116                 friend LLVector3d operator*(const F64 k, const LLVector3d &a);                          // Return a times scaler k
00117                 friend bool operator==(const LLVector3d &a, const LLVector3d &b);               // Return a == b
00118                 friend bool operator!=(const LLVector3d &a, const LLVector3d &b);               // Return a != b
00119 
00120                 friend const LLVector3d& operator+=(LLVector3d &a, const LLVector3d &b);        // Return vector a + b
00121                 friend const LLVector3d& operator-=(LLVector3d &a, const LLVector3d &b);        // Return vector a minus b
00122                 friend const LLVector3d& operator%=(LLVector3d &a, const LLVector3d &b);        // Return a cross b
00123                 friend const LLVector3d& operator*=(LLVector3d &a, const F64 k);                                // Return a times scaler k
00124                 friend const LLVector3d& operator/=(LLVector3d &a, const F64 k);                                // Return a divided by scaler k
00125 
00126                 friend LLVector3d operator-(const LLVector3d &a);                                       // Return vector -a
00127 
00128                 friend std::ostream&     operator<<(std::ostream& s, const LLVector3d &a);              // Stream a
00129 
00130                 static BOOL parseVector3d(const char* buf, LLVector3d* value);
00131 
00132 };
00133 
00134 typedef LLVector3d LLGlobalVec;
00135 
00136 const LLVector3d &LLVector3d::setVec(const LLVector3 &vec)
00137 {
00138         mdV[0] = vec.mV[0];
00139         mdV[1] = vec.mV[1];
00140         mdV[2] = vec.mV[2];
00141         return *this;
00142 }
00143 
00144 
00145 inline LLVector3d::LLVector3d(void)
00146 {
00147         mdV[0] = 0.f;
00148         mdV[1] = 0.f;
00149         mdV[2] = 0.f;
00150 }
00151 
00152 inline LLVector3d::LLVector3d(const F64 x, const F64 y, const F64 z)
00153 {
00154         mdV[VX] = x;
00155         mdV[VY] = y;
00156         mdV[VZ] = z;
00157 }
00158 
00159 inline LLVector3d::LLVector3d(const F64 *vec)
00160 {
00161         mdV[VX] = vec[VX];
00162         mdV[VY] = vec[VY];
00163         mdV[VZ] = vec[VZ];
00164 }
00165 
00166 inline LLVector3d::LLVector3d(const LLVector3 &vec)
00167 {
00168         mdV[VX] = vec.mV[VX];
00169         mdV[VY] = vec.mV[VY];
00170         mdV[VZ] = vec.mV[VZ];
00171 }
00172 
00173 /*
00174 inline LLVector3d::LLVector3d(const LLVector3d &copy)
00175 {
00176         mdV[VX] = copy.mdV[VX];
00177         mdV[VY] = copy.mdV[VY];
00178         mdV[VZ] = copy.mdV[VZ];
00179 }
00180 */
00181 
00182 // Destructors
00183 
00184 // checker
00185 inline BOOL LLVector3d::isFinite() const
00186 {
00187         return (llfinite(mdV[VX]) && llfinite(mdV[VY]) && llfinite(mdV[VZ]));
00188 }
00189 
00190 
00191 // Clear and Assignment Functions
00192 
00193 inline const LLVector3d&        LLVector3d::clearVec(void)
00194 {
00195         mdV[0] = 0.f;
00196         mdV[1] = 0.f;
00197         mdV[2]= 0.f;
00198         return (*this);
00199 }
00200 
00201 inline const LLVector3d&        LLVector3d::zeroVec(void)
00202 {
00203         mdV[0] = 0.f;
00204         mdV[1] = 0.f;
00205         mdV[2] = 0.f;
00206         return (*this);
00207 }
00208 
00209 inline const LLVector3d&        LLVector3d::setVec(const F64 x, const F64 y, const F64 z)
00210 {
00211         mdV[VX] = x;
00212         mdV[VY] = y;
00213         mdV[VZ] = z;
00214         return (*this);
00215 }
00216 
00217 inline const LLVector3d&        LLVector3d::setVec(const LLVector3d &vec)
00218 {
00219         mdV[0] = vec.mdV[0];
00220         mdV[1] = vec.mdV[1];
00221         mdV[2] = vec.mdV[2];
00222         return (*this);
00223 }
00224 
00225 inline const LLVector3d&        LLVector3d::setVec(const F64 *vec)
00226 {
00227         mdV[0] = vec[0];
00228         mdV[1] = vec[1];
00229         mdV[2] = vec[2];
00230         return (*this);
00231 }
00232 
00233 inline F64 LLVector3d::normVec(void)
00234 {
00235         F64 mag = fsqrtf(mdV[0]*mdV[0] + mdV[1]*mdV[1] + mdV[2]*mdV[2]);
00236         F64 oomag;
00237 
00238         if (mag > FP_MAG_THRESHOLD)
00239         {
00240                 oomag = 1.f/mag;
00241                 mdV[0] *= oomag;
00242                 mdV[1] *= oomag;
00243                 mdV[2] *= oomag;
00244         }
00245         else
00246         {
00247                 mdV[0] = 0.f;
00248                 mdV[1] = 0.f;
00249                 mdV[2] = 0.f;
00250                 mag = 0;
00251         }
00252         return (mag);
00253 }
00254 
00255 // LLVector3d Magnitude and Normalization Functions
00256 
00257 inline F64      LLVector3d::magVec(void) const
00258 {
00259         return fsqrtf(mdV[0]*mdV[0] + mdV[1]*mdV[1] + mdV[2]*mdV[2]);
00260 }
00261 
00262 inline F64      LLVector3d::magVecSquared(void) const
00263 {
00264         return mdV[0]*mdV[0] + mdV[1]*mdV[1] + mdV[2]*mdV[2];
00265 }
00266 
00267 inline LLVector3d operator+(const LLVector3d &a, const LLVector3d &b)
00268 {
00269         LLVector3d c(a);
00270         return c += b;
00271 }
00272 
00273 inline LLVector3d operator-(const LLVector3d &a, const LLVector3d &b)
00274 {
00275         LLVector3d c(a);
00276         return c -= b;
00277 }
00278 
00279 inline F64  operator*(const LLVector3d &a, const LLVector3d &b)
00280 {
00281         return (a.mdV[0]*b.mdV[0] + a.mdV[1]*b.mdV[1] + a.mdV[2]*b.mdV[2]);
00282 }
00283 
00284 inline LLVector3d operator%(const LLVector3d &a, const LLVector3d &b)
00285 {
00286         return LLVector3d( a.mdV[1]*b.mdV[2] - b.mdV[1]*a.mdV[2], a.mdV[2]*b.mdV[0] - b.mdV[2]*a.mdV[0], a.mdV[0]*b.mdV[1] - b.mdV[0]*a.mdV[1] );
00287 }
00288 
00289 inline LLVector3d operator/(const LLVector3d &a, const F64 k)
00290 {
00291         F64 t = 1.f / k;
00292         return LLVector3d( a.mdV[0] * t, a.mdV[1] * t, a.mdV[2] * t );
00293 }
00294 
00295 inline LLVector3d operator*(const LLVector3d &a, const F64 k)
00296 {
00297         return LLVector3d( a.mdV[0] * k, a.mdV[1] * k, a.mdV[2] * k );
00298 }
00299 
00300 inline LLVector3d operator*(F64 k, const LLVector3d &a)
00301 {
00302         return LLVector3d( a.mdV[0] * k, a.mdV[1] * k, a.mdV[2] * k );
00303 }
00304 
00305 inline bool operator==(const LLVector3d &a, const LLVector3d &b)
00306 {
00307         return (  (a.mdV[0] == b.mdV[0])
00308                         &&(a.mdV[1] == b.mdV[1])
00309                         &&(a.mdV[2] == b.mdV[2]));
00310 }
00311 
00312 inline bool operator!=(const LLVector3d &a, const LLVector3d &b)
00313 {
00314         return (  (a.mdV[0] != b.mdV[0])
00315                         ||(a.mdV[1] != b.mdV[1])
00316                         ||(a.mdV[2] != b.mdV[2]));
00317 }
00318 
00319 inline const LLVector3d& operator+=(LLVector3d &a, const LLVector3d &b)
00320 {
00321         a.mdV[0] += b.mdV[0];
00322         a.mdV[1] += b.mdV[1];
00323         a.mdV[2] += b.mdV[2];
00324         return a;
00325 }
00326 
00327 inline const LLVector3d& operator-=(LLVector3d &a, const LLVector3d &b)
00328 {
00329         a.mdV[0] -= b.mdV[0];
00330         a.mdV[1] -= b.mdV[1];
00331         a.mdV[2] -= b.mdV[2];
00332         return a;
00333 }
00334 
00335 inline const LLVector3d& operator%=(LLVector3d &a, const LLVector3d &b)
00336 {
00337         LLVector3d ret( a.mdV[1]*b.mdV[2] - b.mdV[1]*a.mdV[2], a.mdV[2]*b.mdV[0] - b.mdV[2]*a.mdV[0], a.mdV[0]*b.mdV[1] - b.mdV[0]*a.mdV[1]);
00338         a = ret;
00339         return a;
00340 }
00341 
00342 inline const LLVector3d& operator*=(LLVector3d &a, const F64 k)
00343 {
00344         a.mdV[0] *= k;
00345         a.mdV[1] *= k;
00346         a.mdV[2] *= k;
00347         return a;
00348 }
00349 
00350 inline const LLVector3d& operator/=(LLVector3d &a, const F64 k)
00351 {
00352         F64 t = 1.f / k;
00353         a.mdV[0] *= t;
00354         a.mdV[1] *= t;
00355         a.mdV[2] *= t;
00356         return a;
00357 }
00358 
00359 inline LLVector3d operator-(const LLVector3d &a)
00360 {
00361         return LLVector3d( -a.mdV[0], -a.mdV[1], -a.mdV[2] );
00362 }
00363 
00364 inline F64      dist_vec(const LLVector3d &a, const LLVector3d &b)
00365 {
00366         F64 x = a.mdV[0] - b.mdV[0];
00367         F64 y = a.mdV[1] - b.mdV[1];
00368         F64 z = a.mdV[2] - b.mdV[2];
00369         return fsqrtf( x*x + y*y + z*z );
00370 }
00371 
00372 inline F64      dist_vec_squared(const LLVector3d &a, const LLVector3d &b)
00373 {
00374         F64 x = a.mdV[0] - b.mdV[0];
00375         F64 y = a.mdV[1] - b.mdV[1];
00376         F64 z = a.mdV[2] - b.mdV[2];
00377         return x*x + y*y + z*z;
00378 }
00379 
00380 inline F64      dist_vec_squared2D(const LLVector3d &a, const LLVector3d &b)
00381 {
00382         F64 x = a.mdV[0] - b.mdV[0];
00383         F64 y = a.mdV[1] - b.mdV[1];
00384         return x*x + y*y;
00385 }
00386 
00387 inline LLVector3d lerp(const LLVector3d &a, const LLVector3d &b, const F64 u)
00388 {
00389         return LLVector3d(
00390                 a.mdV[VX] + (b.mdV[VX] - a.mdV[VX]) * u,
00391                 a.mdV[VY] + (b.mdV[VY] - a.mdV[VY]) * u,
00392                 a.mdV[VZ] + (b.mdV[VZ] - a.mdV[VZ]) * u);
00393 }
00394 
00395 
00396 inline BOOL     LLVector3d::isNull() const
00397 {
00398         if ( F_APPROXIMATELY_ZERO > mdV[VX]*mdV[VX] + mdV[VY]*mdV[VY] + mdV[VZ]*mdV[VZ] )
00399         {
00400                 return TRUE;
00401         }
00402         return FALSE;
00403 }
00404 
00405 
00406 inline F64 angle_between(const LLVector3d& a, const LLVector3d& b)
00407 {
00408         LLVector3d an = a;
00409         LLVector3d bn = b;
00410         an.normVec();
00411         bn.normVec();
00412         F64 cosine = an * bn;
00413         F64 angle = (cosine >= 1.0f) ? 0.0f :
00414                                 (cosine <= -1.0f) ? F_PI :
00415                                 acos(cosine);
00416         return angle;
00417 }
00418 
00419 inline BOOL are_parallel(const LLVector3d &a, const LLVector3d &b, const F64 epsilon)
00420 {
00421         LLVector3d an = a;
00422         LLVector3d bn = b;
00423         an.normVec();
00424         bn.normVec();
00425         F64 dot = an * bn;
00426         if ( (1.0f - fabs(dot)) < epsilon)
00427         {
00428                 return TRUE;
00429         }
00430         return FALSE;
00431 
00432 }
00433 
00434 inline LLVector3d projected_vec(const LLVector3d &a, const LLVector3d &b)
00435 {
00436         LLVector3d project_axis = b;
00437         project_axis.normVec();
00438         return project_axis * (a * project_axis);
00439 }
00440 
00441 #endif // LL_V3DMATH_H

Generated on Thu Jul 1 06:10:00 2010 for Second Life Viewer by  doxygen 1.4.7