v3math.cpp

Go to the documentation of this file.
00001 
00032 #include "linden_common.h"
00033 
00034 #include "v3math.h"
00035 
00036 //#include "vmath.h"
00037 #include "v4math.h"
00038 #include "m4math.h"
00039 #include "m3math.h"
00040 #include "llquaternion.h"
00041 #include "llquantize.h"
00042 #include "v3dmath.h"
00043 
00044 // LLVector3
00045 // WARNING: Don't use these for global const definitions!
00046 // For example: 
00047 //              const LLQuaternion(0.5f * F_PI, LLVector3::zero);
00048 // at the top of a *.cpp file might not give you what you think.
00049 const LLVector3 LLVector3::zero(0,0,0);
00050 const LLVector3 LLVector3::x_axis(1.f, 0, 0);
00051 const LLVector3 LLVector3::y_axis(0, 1.f, 0);
00052 const LLVector3 LLVector3::z_axis(0, 0, 1.f);
00053 const LLVector3 LLVector3::x_axis_neg(-1.f, 0, 0);
00054 const LLVector3 LLVector3::y_axis_neg(0, -1.f, 0);
00055 const LLVector3 LLVector3::z_axis_neg(0, 0, -1.f);
00056 const LLVector3 LLVector3::all_one(1.f,1.f,1.f);
00057 
00058 
00059 // Clamps each values to range (min,max).
00060 // Returns TRUE if data changed.
00061 BOOL LLVector3::clamp(F32 min, F32 max)
00062 {
00063         BOOL ret = FALSE;
00064 
00065         if (mV[0] < min) { mV[0] = min; ret = TRUE; }
00066         if (mV[1] < min) { mV[1] = min; ret = TRUE; }
00067         if (mV[2] < min) { mV[2] = min; ret = TRUE; }
00068 
00069         if (mV[0] > max) { mV[0] = max; ret = TRUE; }
00070         if (mV[1] > max) { mV[1] = max; ret = TRUE; }
00071         if (mV[2] > max) { mV[2] = max; ret = TRUE; }
00072 
00073         return ret;
00074 }
00075 
00076 // Sets all values to absolute value of their original values
00077 // Returns TRUE if data changed
00078 BOOL LLVector3::abs()
00079 {
00080         BOOL ret = FALSE;
00081 
00082         if (mV[0] < 0.f) { mV[0] = -mV[0]; ret = TRUE; }
00083         if (mV[1] < 0.f) { mV[1] = -mV[1]; ret = TRUE; }
00084         if (mV[2] < 0.f) { mV[2] = -mV[2]; ret = TRUE; }
00085 
00086         return ret;
00087 }
00088 
00089 // Quatizations
00090 void    LLVector3::quantize16(F32 lowerxy, F32 upperxy, F32 lowerz, F32 upperz)
00091 {
00092         F32 x = mV[VX];
00093         F32 y = mV[VY];
00094         F32 z = mV[VZ];
00095 
00096         x = U16_to_F32(F32_to_U16(x, lowerxy, upperxy), lowerxy, upperxy);
00097         y = U16_to_F32(F32_to_U16(y, lowerxy, upperxy), lowerxy, upperxy);
00098         z = U16_to_F32(F32_to_U16(z, lowerz,  upperz),  lowerz,  upperz);
00099 
00100         mV[VX] = x;
00101         mV[VY] = y;
00102         mV[VZ] = z;
00103 }
00104 
00105 void    LLVector3::quantize8(F32 lowerxy, F32 upperxy, F32 lowerz, F32 upperz)
00106 {
00107         mV[VX] = U8_to_F32(F32_to_U8(mV[VX], lowerxy, upperxy), lowerxy, upperxy);;
00108         mV[VY] = U8_to_F32(F32_to_U8(mV[VY], lowerxy, upperxy), lowerxy, upperxy);
00109         mV[VZ] = U8_to_F32(F32_to_U8(mV[VZ], lowerz, upperz), lowerz, upperz);
00110 }
00111 
00112 
00113 void    LLVector3::snap(S32 sig_digits)
00114 {
00115         mV[VX] = snap_to_sig_figs(mV[VX], sig_digits);
00116         mV[VY] = snap_to_sig_figs(mV[VY], sig_digits);
00117         mV[VZ] = snap_to_sig_figs(mV[VZ], sig_digits);
00118 }
00119 
00120 
00121 std::ostream& operator<<(std::ostream& s, const LLVector3 &a) 
00122 {
00123         s << "{ " << a.mV[VX] << ", " << a.mV[VY] << ", " << a.mV[VZ] << " }";
00124         return s;
00125 }
00126 
00127 
00128 const LLVector3&        LLVector3::rotVec(const LLMatrix3 &mat)
00129 {
00130         *this = *this * mat;
00131         return *this;
00132 }
00133 
00134 const LLVector3&        LLVector3::rotVec(const LLQuaternion &q)
00135 {
00136         *this = *this * q;
00137         return *this;
00138 }
00139 
00140 const LLVector3&        LLVector3::rotVec(F32 angle, const LLVector3 &vec)
00141 {
00142         if ( !vec.isExactlyZero() && angle )
00143         {
00144                 *this = *this * LLQuaternion(angle, vec);
00145         }
00146         return *this;
00147 }
00148 
00149 const LLVector3&        LLVector3::rotVec(F32 angle, F32 x, F32 y, F32 z)
00150 {
00151         LLVector3 vec(x, y, z);
00152         if ( !vec.isExactlyZero() && angle )
00153         {
00154                 *this = *this * LLQuaternion(angle, vec);
00155         }
00156         return *this;
00157 }
00158 
00159 const LLVector3&        LLVector3::scaleVec(const LLVector3& vec)
00160 {
00161         mV[VX] *= vec.mV[VX];
00162         mV[VY] *= vec.mV[VY];
00163         mV[VZ] *= vec.mV[VZ];
00164 
00165         return *this;
00166 }
00167 
00168 LLVector3                       LLVector3::scaledVec(const LLVector3& vec) const
00169 {
00170         LLVector3 ret = LLVector3(*this);
00171         ret.scaleVec(vec);
00172         return ret;
00173 }
00174 
00175 const LLVector3&        LLVector3::setVec(const LLVector3d &vec)
00176 {
00177         mV[0] = (F32)vec.mdV[0];
00178         mV[1] = (F32)vec.mdV[1];
00179         mV[2] = (F32)vec.mdV[2];
00180         return (*this);
00181 }
00182 
00183 const LLVector3&        LLVector3::setVec(const LLVector4 &vec)
00184 {
00185         mV[0] = vec.mV[0];
00186         mV[1] = vec.mV[1];
00187         mV[2] = vec.mV[2];
00188         return (*this);
00189 }
00190 
00191 LLVector3::LLVector3(const LLVector3d &vec)
00192 {
00193         mV[VX] = (F32)vec.mdV[VX];
00194         mV[VY] = (F32)vec.mdV[VY];
00195         mV[VZ] = (F32)vec.mdV[VZ];
00196 }
00197 
00198 LLVector3::LLVector3(const LLVector4 &vec)
00199 {
00200         mV[VX] = (F32)vec.mV[VX];
00201         mV[VY] = (F32)vec.mV[VY];
00202         mV[VZ] = (F32)vec.mV[VZ];
00203 }
00204 
00205 LLVector3::LLVector3(const LLSD& sd)
00206 {
00207         setValue(sd);
00208 }
00209 
00210 LLSD LLVector3::getValue() const
00211 {
00212         LLSD ret;
00213         ret[0] = mV[0];
00214         ret[1] = mV[1];
00215         ret[2] = mV[2];
00216         return ret;
00217 }
00218 
00219 void LLVector3::setValue(const LLSD& sd)
00220 {
00221         mV[0] = (F32) sd[0].asReal();
00222         mV[1] = (F32) sd[1].asReal();
00223         mV[2] = (F32) sd[2].asReal();
00224 }
00225 
00226 const LLVector3& LLVector3::operator=(const LLSD& sd)
00227 {
00228         setValue(sd);
00229         return *this;
00230 }
00231 
00232 const LLVector3& operator*=(LLVector3 &a, const LLQuaternion &rot)
00233 {
00234     const F32 rw = - rot.mQ[VX] * a.mV[VX] - rot.mQ[VY] * a.mV[VY] - rot.mQ[VZ] * a.mV[VZ];
00235     const F32 rx =   rot.mQ[VW] * a.mV[VX] + rot.mQ[VY] * a.mV[VZ] - rot.mQ[VZ] * a.mV[VY];
00236     const F32 ry =   rot.mQ[VW] * a.mV[VY] + rot.mQ[VZ] * a.mV[VX] - rot.mQ[VX] * a.mV[VZ];
00237     const F32 rz =   rot.mQ[VW] * a.mV[VZ] + rot.mQ[VX] * a.mV[VY] - rot.mQ[VY] * a.mV[VX];
00238         
00239     a.mV[VX] = - rw * rot.mQ[VX] +  rx * rot.mQ[VW] - ry * rot.mQ[VZ] + rz * rot.mQ[VY];
00240     a.mV[VY] = - rw * rot.mQ[VY] +  ry * rot.mQ[VW] - rz * rot.mQ[VX] + rx * rot.mQ[VZ];
00241     a.mV[VZ] = - rw * rot.mQ[VZ] +  rz * rot.mQ[VW] - rx * rot.mQ[VY] + ry * rot.mQ[VX];
00242 
00243         return a;
00244 }
00245 
00246 // static 
00247 BOOL LLVector3::parseVector3(const char* buf, LLVector3* value)
00248 {
00249         if( buf == NULL || buf[0] == '\0' || value == NULL)
00250         {
00251                 return FALSE;
00252         }
00253 
00254         LLVector3 v;
00255         S32 count = sscanf( buf, "%f %f %f", v.mV + 0, v.mV + 1, v.mV + 2 );
00256         if( 3 == count )
00257         {
00258                 value->setVec( v );
00259                 return TRUE;
00260         }
00261 
00262         return FALSE;
00263 }

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