v2math.h

Go to the documentation of this file.
00001 
00032 #ifndef LL_V2MATH_H
00033 #define LL_V2MATH_H
00034 
00035 #include "llmath.h"
00036 
00037 class LLVector4;
00038 class LLMatrix3;
00039 class LLQuaternion;
00040 
00041 //  Llvector2 = |x y z w|
00042 
00043 static const U32 LENGTHOFVECTOR2 = 2;
00044 
00045 class LLVector2
00046 {
00047         public:
00048                 F32 mV[LENGTHOFVECTOR2];
00049 
00050                 static LLVector2 zero;
00051 
00052                 LLVector2();                                                    // Initializes LLVector2 to (0, 0)
00053                 LLVector2(F32 x, F32 y);                            // Initializes LLVector2 to (x. y)
00054                 LLVector2(const F32 *vec);                              // Initializes LLVector2 to (vec[0]. vec[1])
00055                 
00056                 // Clears LLVector2 to (0, 0).  DEPRECATED - prefer zeroVec.
00057                 void    clearVec();
00058 
00059                 // Zero LLVector2 to (0, 0)
00060                 void    zeroVec();
00061 
00062                 void    setVec(F32 x, F32 y);           // Sets LLVector2 to (x, y)
00063                 void    setVec(const LLVector2 &vec);   // Sets LLVector2 to vec
00064                 void    setVec(const F32 *vec);                 // Sets LLVector2 to vec
00065 
00066                 F32             magVec() const;                         // Returns magnitude of LLVector2
00067                 F32             magVecSquared() const;          // Returns magnitude squared of LLVector2
00068                 F32             normVec();                                      // Normalizes and returns the magnitude of LLVector2
00069 
00070                 BOOL            abs();                                          // sets all values to absolute value of original value (first octant), returns TRUE if changed
00071 
00072                 const LLVector2&        scaleVec(const LLVector2& vec);                         // scales per component by vec
00073 
00074                 BOOL isNull();                  // Returns TRUE if vector has a _very_small_ length
00075                 BOOL isExactlyZero() const              { return !mV[VX] && !mV[VY]; }
00076 
00077                 F32 operator[](int idx) const { return mV[idx]; }
00078                 F32 &operator[](int idx) { return mV[idx]; }
00079         
00080                 friend bool operator<(const LLVector2 &a, const LLVector2 &b);  // For sorting. x is "more significant" than y
00081                 friend LLVector2 operator+(const LLVector2 &a, const LLVector2 &b);     // Return vector a + b
00082                 friend LLVector2 operator-(const LLVector2 &a, const LLVector2 &b);     // Return vector a minus b
00083                 friend F32 operator*(const LLVector2 &a, const LLVector2 &b);           // Return a dot b
00084                 friend LLVector2 operator%(const LLVector2 &a, const LLVector2 &b);     // Return a cross b
00085                 friend LLVector2 operator/(const LLVector2 &a, F32 k);                          // Return a divided by scaler k
00086                 friend LLVector2 operator*(const LLVector2 &a, F32 k);                          // Return a times scaler k
00087                 friend LLVector2 operator*(F32 k, const LLVector2 &a);                          // Return a times scaler k
00088                 friend bool operator==(const LLVector2 &a, const LLVector2 &b);         // Return a == b
00089                 friend bool operator!=(const LLVector2 &a, const LLVector2 &b);         // Return a != b
00090 
00091                 friend const LLVector2& operator+=(LLVector2 &a, const LLVector2 &b);   // Return vector a + b
00092                 friend const LLVector2& operator-=(LLVector2 &a, const LLVector2 &b);   // Return vector a minus b
00093                 friend const LLVector2& operator%=(LLVector2 &a, const LLVector2 &b);   // Return a cross b
00094                 friend const LLVector2& operator*=(LLVector2 &a, F32 k);                                // Return a times scaler k
00095                 friend const LLVector2& operator/=(LLVector2 &a, F32 k);                                // Return a divided by scaler k
00096 
00097                 friend LLVector2 operator-(const LLVector2 &a);                                 // Return vector -a
00098 
00099                 friend std::ostream&     operator<<(std::ostream& s, const LLVector2 &a);               // Stream a
00100 };
00101 
00102 
00103 // Non-member functions 
00104 
00105 F32     angle_between(const LLVector2 &a, const LLVector2 &b);  // Returns angle (radians) between a and b
00106 BOOL are_parallel(const LLVector2 &a, const LLVector2 &b, F32 epsilon=F_APPROXIMATELY_ZERO);    // Returns TRUE if a and b are very close to parallel
00107 F32     dist_vec(const LLVector2 &a, const LLVector2 &b);               // Returns distance between a and b
00108 F32     dist_vec_squared(const LLVector2 &a, const LLVector2 &b);// Returns distance sqaured between a and b
00109 F32     dist_vec_squared2D(const LLVector2 &a, const LLVector2 &b);// Returns distance sqaured between a and b ignoring Z component
00110 LLVector2 lerp(const LLVector2 &a, const LLVector2 &b, F32 u); // Returns a vector that is a linear interpolation between a and b
00111 
00112 // Constructors
00113 
00114 inline LLVector2::LLVector2(void)
00115 {
00116         mV[VX] = 0.f;
00117         mV[VY] = 0.f;
00118 }
00119 
00120 inline LLVector2::LLVector2(F32 x, F32 y)
00121 {
00122         mV[VX] = x;
00123         mV[VY] = y;
00124 }
00125 
00126 inline LLVector2::LLVector2(const F32 *vec)
00127 {
00128         mV[VX] = vec[VX];
00129         mV[VY] = vec[VY];
00130 }
00131 
00132 
00133 // Clear and Assignment Functions
00134 
00135 inline void     LLVector2::clearVec(void)
00136 {
00137         mV[VX] = 0.f;
00138         mV[VY] = 0.f;
00139 }
00140 
00141 inline void     LLVector2::zeroVec(void)
00142 {
00143         mV[VX] = 0.f;
00144         mV[VY] = 0.f;
00145 }
00146 
00147 inline void     LLVector2::setVec(F32 x, F32 y)
00148 {
00149         mV[VX] = x;
00150         mV[VY] = y;
00151 }
00152 
00153 inline void     LLVector2::setVec(const LLVector2 &vec)
00154 {
00155         mV[VX] = vec.mV[VX];
00156         mV[VY] = vec.mV[VY];
00157 }
00158 
00159 inline void     LLVector2::setVec(const F32 *vec)
00160 {
00161         mV[VX] = vec[VX];
00162         mV[VY] = vec[VY];
00163 }
00164 
00165 // LLVector2 Magnitude and Normalization Functions
00166 
00167 inline F32              LLVector2::magVec(void) const
00168 {
00169         return fsqrtf(mV[0]*mV[0] + mV[1]*mV[1]);
00170 }
00171 
00172 inline F32              LLVector2::magVecSquared(void) const
00173 {
00174         return mV[0]*mV[0] + mV[1]*mV[1];
00175 }
00176 
00177 inline F32              LLVector2::normVec(void)
00178 {
00179         F32 mag = fsqrtf(mV[0]*mV[0] + mV[1]*mV[1]);
00180         F32 oomag;
00181 
00182         if (mag > FP_MAG_THRESHOLD)
00183         {
00184                 oomag = 1.f/mag;
00185                 mV[0] *= oomag;
00186                 mV[1] *= oomag;
00187         }
00188         else
00189         {
00190                 mV[0] = 0.f;
00191                 mV[1] = 0.f;
00192                 mag = 0;
00193         }
00194         return (mag);
00195 }
00196 
00197 inline const LLVector2& LLVector2::scaleVec(const LLVector2& vec)
00198 {
00199         mV[VX] *= vec.mV[VX];
00200         mV[VY] *= vec.mV[VY];
00201 
00202         return *this;
00203 }
00204 
00205 inline BOOL     LLVector2::isNull()
00206 {
00207         if ( F_APPROXIMATELY_ZERO > mV[VX]*mV[VX] + mV[VY]*mV[VY] )
00208         {
00209                 return TRUE;
00210         }
00211         return FALSE;
00212 }
00213 
00214 
00215 // LLVector2 Operators
00216 
00217 // For sorting. By convention, x is "more significant" than y.
00218 inline bool operator<(const LLVector2 &a, const LLVector2 &b)   
00219 {
00220         if( a.mV[VX] == b.mV[VX] )
00221         {
00222                 return a.mV[VY] < b.mV[VY];
00223         }
00224         else
00225         {
00226                 return a.mV[VX] < b.mV[VX];
00227         }
00228 }
00229 
00230 
00231 inline LLVector2 operator+(const LLVector2 &a, const LLVector2 &b)
00232 {
00233         LLVector2 c(a);
00234         return c += b;
00235 }
00236 
00237 inline LLVector2 operator-(const LLVector2 &a, const LLVector2 &b)
00238 {
00239         LLVector2 c(a);
00240         return c -= b;
00241 }
00242 
00243 inline F32  operator*(const LLVector2 &a, const LLVector2 &b)
00244 {
00245         return (a.mV[0]*b.mV[0] + a.mV[1]*b.mV[1]);
00246 }
00247 
00248 inline LLVector2 operator%(const LLVector2 &a, const LLVector2 &b)
00249 {
00250         return LLVector2(a.mV[0]*b.mV[1] - b.mV[0]*a.mV[1], a.mV[1]*b.mV[0] - b.mV[1]*a.mV[0]);
00251 }
00252 
00253 inline LLVector2 operator/(const LLVector2 &a, F32 k)
00254 {
00255         F32 t = 1.f / k;
00256         return LLVector2( a.mV[0] * t, a.mV[1] * t );
00257 }
00258 
00259 inline LLVector2 operator*(const LLVector2 &a, F32 k)
00260 {
00261         return LLVector2( a.mV[0] * k, a.mV[1] * k );
00262 }
00263 
00264 inline LLVector2 operator*(F32 k, const LLVector2 &a)
00265 {
00266         return LLVector2( a.mV[0] * k, a.mV[1] * k );
00267 }
00268 
00269 inline bool operator==(const LLVector2 &a, const LLVector2 &b)
00270 {
00271         return (  (a.mV[0] == b.mV[0])
00272                         &&(a.mV[1] == b.mV[1]));
00273 }
00274 
00275 inline bool operator!=(const LLVector2 &a, const LLVector2 &b)
00276 {
00277         return (  (a.mV[0] != b.mV[0])
00278                         ||(a.mV[1] != b.mV[1]));
00279 }
00280 
00281 inline const LLVector2& operator+=(LLVector2 &a, const LLVector2 &b)
00282 {
00283         a.mV[0] += b.mV[0];
00284         a.mV[1] += b.mV[1];
00285         return a;
00286 }
00287 
00288 inline const LLVector2& operator-=(LLVector2 &a, const LLVector2 &b)
00289 {
00290         a.mV[0] -= b.mV[0];
00291         a.mV[1] -= b.mV[1];
00292         return a;
00293 }
00294 
00295 inline const LLVector2& operator%=(LLVector2 &a, const LLVector2 &b)
00296 {
00297         LLVector2 ret(a.mV[0]*b.mV[1] - b.mV[0]*a.mV[1], a.mV[1]*b.mV[0] - b.mV[1]*a.mV[0]);
00298         a = ret;
00299         return a;
00300 }
00301 
00302 inline const LLVector2& operator*=(LLVector2 &a, F32 k)
00303 {
00304         a.mV[0] *= k;
00305         a.mV[1] *= k;
00306         return a;
00307 }
00308 
00309 inline const LLVector2& operator/=(LLVector2 &a, F32 k)
00310 {
00311         F32 t = 1.f / k;
00312         a.mV[0] *= t;
00313         a.mV[1] *= t;
00314         return a;
00315 }
00316 
00317 inline LLVector2 operator-(const LLVector2 &a)
00318 {
00319         return LLVector2( -a.mV[0], -a.mV[1] );
00320 }
00321 
00322 inline void update_min_max(LLVector2& min, LLVector2& max, const LLVector2& pos)
00323 {
00324         for (U32 i = 0; i < 2; i++)
00325         {
00326                 if (min.mV[i] > pos.mV[i])
00327                 {
00328                         min.mV[i] = pos.mV[i];
00329                 }
00330                 if (max.mV[i] < pos.mV[i])
00331                 {
00332                         max.mV[i] = pos.mV[i];
00333                 }
00334         }
00335 }
00336 
00337 inline std::ostream& operator<<(std::ostream& s, const LLVector2 &a) 
00338 {
00339         s << "{ " << a.mV[VX] << ", " << a.mV[VY] << " }";
00340         return s;
00341 }
00342 
00343 #endif

Generated on Thu Jul 1 06:09:59 2010 for Second Life Viewer by  doxygen 1.4.7