Back to index

supertuxkart  0.5+dfsg1
btVector3.h
Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2003-2006 Gino van den Bergen / Erwin Coumans  http://continuousphysics.com/Bullet/
00003 
00004 This software is provided 'as-is', without any express or implied warranty.
00005 In no event will the authors be held liable for any damages arising from the use of this software.
00006 Permission is granted to anyone to use this software for any purpose, 
00007 including commercial applications, and to alter it and redistribute it freely, 
00008 subject to the following restrictions:
00009 
00010 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
00011 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00012 3. This notice may not be removed or altered from any source distribution.
00013 */
00014 
00015 
00016 
00017 #ifndef SIMD__VECTOR3_H
00018 #define SIMD__VECTOR3_H
00019 
00020 #include "btQuadWord.h"
00021 
00025 class  btVector3 : public btQuadWord {
00026 
00027 public:
00028        SIMD_FORCE_INLINE btVector3() {}
00029 
00030        SIMD_FORCE_INLINE btVector3(const btQuadWordStorage& q) 
00031               : btQuadWord(q)
00032        {
00033        }
00034        
00035 
00036        SIMD_FORCE_INLINE btVector3(const btScalar& x, const btScalar& y, const btScalar& z) 
00037               :btQuadWord(x,y,z,btScalar(0.))
00038        {
00039        }
00040 
00041 //     SIMD_FORCE_INLINE btVector3(const btScalar& x, const btScalar& y, const btScalar& z,const btScalar& w) 
00042 //            : btQuadWord(x,y,z,w)
00043 //     {
00044 //     }
00045 
00046        
00047 
00048        SIMD_FORCE_INLINE btVector3& operator+=(const btVector3& v)
00049        {
00050               m_x += v.x(); m_y += v.y(); m_z += v.z();
00051               return *this;
00052        }
00053 
00054 
00055 
00056        SIMD_FORCE_INLINE btVector3& operator-=(const btVector3& v) 
00057        {
00058               m_x -= v.x(); m_y -= v.y(); m_z -= v.z();
00059               return *this;
00060        }
00061 
00062        SIMD_FORCE_INLINE btVector3& operator*=(const btScalar& s)
00063        {
00064               m_x *= s; m_y *= s; m_z *= s;
00065               return *this;
00066        }
00067 
00068        SIMD_FORCE_INLINE btVector3& operator/=(const btScalar& s) 
00069        {
00070               btFullAssert(s != btScalar(0.0));
00071               return *this *= btScalar(1.0) / s;
00072        }
00073 
00074        SIMD_FORCE_INLINE btScalar dot(const btVector3& v) const
00075        {
00076               return m_x * v.x() + m_y * v.y() + m_z * v.z();
00077        }
00078 
00079        SIMD_FORCE_INLINE btScalar length2() const
00080        {
00081               return dot(*this);
00082        }
00083 
00084        SIMD_FORCE_INLINE btScalar length() const
00085        {
00086               return btSqrt(length2());
00087        }
00088 
00089        SIMD_FORCE_INLINE btScalar distance2(const btVector3& v) const;
00090 
00091        SIMD_FORCE_INLINE btScalar distance(const btVector3& v) const;
00092 
00093        SIMD_FORCE_INLINE btVector3& normalize() 
00094        {
00095               return *this /= length();
00096        }
00097 
00098        SIMD_FORCE_INLINE btVector3 normalized() const;
00099 
00100        SIMD_FORCE_INLINE btVector3 rotate( const btVector3& wAxis, const btScalar angle );
00101 
00102        SIMD_FORCE_INLINE btScalar angle(const btVector3& v) const 
00103        {
00104               btScalar s = btSqrt(length2() * v.length2());
00105               btFullAssert(s != btScalar(0.0));
00106               return btAcos(dot(v) / s);
00107        }
00108 
00109        SIMD_FORCE_INLINE btVector3 absolute() const 
00110        {
00111               return btVector3(
00112                      btFabs(m_x), 
00113                      btFabs(m_y), 
00114                      btFabs(m_z));
00115        }
00116 
00117        SIMD_FORCE_INLINE btVector3 cross(const btVector3& v) const
00118        {
00119               return btVector3(
00120                      m_y * v.z() - m_z * v.y(),
00121                      m_z * v.x() - m_x * v.z(),
00122                      m_x * v.y() - m_y * v.x());
00123        }
00124 
00125        SIMD_FORCE_INLINE btScalar triple(const btVector3& v1, const btVector3& v2) const
00126        {
00127               return m_x * (v1.y() * v2.z() - v1.z() * v2.y()) + 
00128                      m_y * (v1.z() * v2.x() - v1.x() * v2.z()) + 
00129                      m_z * (v1.x() * v2.y() - v1.y() * v2.x());
00130        }
00131 
00132        SIMD_FORCE_INLINE int minAxis() const
00133        {
00134               return m_x < m_y ? (m_x < m_z ? 0 : 2) : (m_y < m_z ? 1 : 2);
00135        }
00136 
00137        SIMD_FORCE_INLINE int maxAxis() const 
00138        {
00139               return m_x < m_y ? (m_y < m_z ? 2 : 1) : (m_x < m_z ? 2 : 0);
00140        }
00141 
00142        SIMD_FORCE_INLINE int furthestAxis() const
00143        {
00144               return absolute().minAxis();
00145        }
00146 
00147        SIMD_FORCE_INLINE int closestAxis() const 
00148        {
00149               return absolute().maxAxis();
00150        }
00151 
00152        SIMD_FORCE_INLINE void setInterpolate3(const btVector3& v0, const btVector3& v1, btScalar rt)
00153        {
00154               btScalar s = btScalar(1.0) - rt;
00155               m_x = s * v0.x() + rt * v1.x();
00156               m_y = s * v0.y() + rt * v1.y();
00157               m_z = s * v0.z() + rt * v1.z();
00158               //don't do the unused w component
00159               //            m_co[3] = s * v0[3] + rt * v1[3];
00160        }
00161 
00162        SIMD_FORCE_INLINE btVector3 lerp(const btVector3& v, const btScalar& t) const 
00163        {
00164               return btVector3(m_x + (v.x() - m_x) * t,
00165                      m_y + (v.y() - m_y) * t,
00166                      m_z + (v.z() - m_z) * t);
00167        }
00168 
00169 
00170        SIMD_FORCE_INLINE btVector3& operator*=(const btVector3& v)
00171        {
00172               m_x *= v.x(); m_y *= v.y(); m_z *= v.z();
00173               return *this;
00174        }
00175 
00176        
00177 
00178 };
00179 
00180 SIMD_FORCE_INLINE btVector3 
00181 operator+(const btVector3& v1, const btVector3& v2) 
00182 {
00183        return btVector3(v1.x() + v2.x(), v1.y() + v2.y(), v1.z() + v2.z());
00184 }
00185 
00186 SIMD_FORCE_INLINE btVector3 
00187 operator*(const btVector3& v1, const btVector3& v2) 
00188 {
00189        return btVector3(v1.x() * v2.x(), v1.y() * v2.y(), v1.z() * v2.z());
00190 }
00191 
00192 SIMD_FORCE_INLINE btVector3 
00193 operator-(const btVector3& v1, const btVector3& v2)
00194 {
00195        return btVector3(v1.x() - v2.x(), v1.y() - v2.y(), v1.z() - v2.z());
00196 }
00197 
00198 SIMD_FORCE_INLINE btVector3 
00199 operator-(const btVector3& v)
00200 {
00201        return btVector3(-v.x(), -v.y(), -v.z());
00202 }
00203 
00204 SIMD_FORCE_INLINE btVector3 
00205 operator*(const btVector3& v, const btScalar& s)
00206 {
00207        return btVector3(v.x() * s, v.y() * s, v.z() * s);
00208 }
00209 
00210 SIMD_FORCE_INLINE btVector3 
00211 operator*(const btScalar& s, const btVector3& v)
00212 { 
00213        return v * s; 
00214 }
00215 
00216 SIMD_FORCE_INLINE btVector3
00217 operator/(const btVector3& v, const btScalar& s)
00218 {
00219        btFullAssert(s != btScalar(0.0));
00220        return v * (btScalar(1.0) / s);
00221 }
00222 
00223 SIMD_FORCE_INLINE btVector3
00224 operator/(const btVector3& v1, const btVector3& v2)
00225 {
00226        return btVector3(v1.x() / v2.x(),v1.y() / v2.y(),v1.z() / v2.z());
00227 }
00228 
00229 SIMD_FORCE_INLINE btScalar 
00230 dot(const btVector3& v1, const btVector3& v2) 
00231 { 
00232        return v1.dot(v2); 
00233 }
00234 
00235 
00236 
00237 SIMD_FORCE_INLINE btScalar
00238 distance2(const btVector3& v1, const btVector3& v2) 
00239 { 
00240        return v1.distance2(v2); 
00241 }
00242 
00243 
00244 SIMD_FORCE_INLINE btScalar
00245 distance(const btVector3& v1, const btVector3& v2) 
00246 { 
00247        return v1.distance(v2); 
00248 }
00249 
00250 SIMD_FORCE_INLINE btScalar
00251 angle(const btVector3& v1, const btVector3& v2) 
00252 { 
00253        return v1.angle(v2); 
00254 }
00255 
00256 SIMD_FORCE_INLINE btVector3 
00257 cross(const btVector3& v1, const btVector3& v2) 
00258 { 
00259        return v1.cross(v2); 
00260 }
00261 
00262 SIMD_FORCE_INLINE btScalar
00263 triple(const btVector3& v1, const btVector3& v2, const btVector3& v3)
00264 {
00265        return v1.triple(v2, v3);
00266 }
00267 
00268 SIMD_FORCE_INLINE btVector3 
00269 lerp(const btVector3& v1, const btVector3& v2, const btScalar& t)
00270 {
00271        return v1.lerp(v2, t);
00272 }
00273 
00274 
00275 SIMD_FORCE_INLINE bool operator==(const btVector3& p1, const btVector3& p2) 
00276 {
00277        return p1.x() == p2.x() && p1.y() == p2.y() && p1.z() == p2.z();
00278 }
00279 
00280 SIMD_FORCE_INLINE btScalar btVector3::distance2(const btVector3& v) const
00281 {
00282        return (v - *this).length2();
00283 }
00284 
00285 SIMD_FORCE_INLINE btScalar btVector3::distance(const btVector3& v) const
00286 {
00287        return (v - *this).length();
00288 }
00289 
00290 SIMD_FORCE_INLINE btVector3 btVector3::normalized() const
00291 {
00292        return *this / length();
00293 } 
00294 
00295 SIMD_FORCE_INLINE btVector3 btVector3::rotate( const btVector3& wAxis, const btScalar angle )
00296 {
00297        // wAxis must be a unit lenght vector
00298 
00299        btVector3 o = wAxis * wAxis.dot( *this );
00300        btVector3 x = *this - o;
00301        btVector3 y;
00302 
00303        y = wAxis.cross( *this );
00304 
00305        return ( o + x * btCos( angle ) + y * btSin( angle ) );
00306 }
00307 
00308 class btVector4 : public btVector3
00309 {
00310 public:
00311 
00312        SIMD_FORCE_INLINE btVector4() {}
00313 
00314 
00315        SIMD_FORCE_INLINE btVector4(const btScalar& x, const btScalar& y, const btScalar& z,const btScalar& w) 
00316               : btVector3(x,y,z)
00317        {
00318               m_unusedW = w;
00319        }
00320 
00321 
00322        SIMD_FORCE_INLINE btVector4 absolute4() const 
00323        {
00324               return btVector4(
00325                      btFabs(m_x), 
00326                      btFabs(m_y), 
00327                      btFabs(m_z),
00328                      btFabs(m_unusedW));
00329        }
00330 
00331 
00332 
00333        btScalar      getW() const { return m_unusedW;}
00334 
00335 
00336               SIMD_FORCE_INLINE int maxAxis4() const
00337        {
00338               int maxIndex = -1;
00339               btScalar maxVal = btScalar(-1e30);
00340               if (m_x > maxVal)
00341               {
00342                      maxIndex = 0;
00343                      maxVal = m_x;
00344               }
00345               if (m_y > maxVal)
00346               {
00347                      maxIndex = 1;
00348                      maxVal = m_y;
00349               }
00350               if (m_z > maxVal)
00351               {
00352                      maxIndex = 2;
00353                      maxVal = m_z;
00354               }
00355               if (m_unusedW > maxVal)
00356               {
00357                      maxIndex = 3;
00358                      maxVal = m_unusedW;
00359               }
00360               
00361               
00362               
00363 
00364               return maxIndex;
00365 
00366        }
00367 
00368 
00369        SIMD_FORCE_INLINE int minAxis4() const
00370        {
00371               int minIndex = -1;
00372               btScalar minVal = btScalar(1e30);
00373               if (m_x < minVal)
00374               {
00375                      minIndex = 0;
00376                      minVal = m_x;
00377               }
00378               if (m_y < minVal)
00379               {
00380                      minIndex = 1;
00381                      minVal = m_y;
00382               }
00383               if (m_z < minVal)
00384               {
00385                      minIndex = 2;
00386                      minVal = m_z;
00387               }
00388               if (m_unusedW < minVal)
00389               {
00390                      minIndex = 3;
00391                      minVal = m_unusedW;
00392               }
00393               
00394               return minIndex;
00395 
00396        }
00397 
00398 
00399        SIMD_FORCE_INLINE int closestAxis4() const 
00400        {
00401               return absolute4().maxAxis4();
00402        }
00403 
00404 };
00405 
00406 
00408 SIMD_FORCE_INLINE void      btSwapScalarEndian(const btScalar& sourceVal, btScalar& destVal)
00409 {
00410        #ifdef BT_USE_DOUBLE_PRECISION
00411        unsigned char* dest = (unsigned char*) &destVal;
00412        unsigned char* src  = (unsigned char*) &sourceVal;
00413        dest[0] = src[7];
00414     dest[1] = src[6];
00415     dest[2] = src[5];
00416     dest[3] = src[4];
00417     dest[4] = src[3];
00418     dest[5] = src[2];
00419     dest[6] = src[1];
00420     dest[7] = src[0];
00421 #else
00422        unsigned char* dest = (unsigned char*) &destVal;
00423        unsigned char* src  = (unsigned char*) &sourceVal;
00424        dest[0] = src[3];
00425     dest[1] = src[2];
00426     dest[2] = src[1];
00427     dest[3] = src[0];
00428 #endif //BT_USE_DOUBLE_PRECISION
00429 }
00431 SIMD_FORCE_INLINE void      btSwapVector3Endian(const btVector3& sourceVec, btVector3& destVec)
00432 {
00433        for (int i=0;i<4;i++)
00434        {
00435               btSwapScalarEndian(sourceVec[i],destVec[i]);
00436        }
00437 
00438 }
00439 
00441 SIMD_FORCE_INLINE void      btUnSwapVector3Endian(btVector3& vector)
00442 {
00443 
00444        btVector3     swappedVec;
00445        for (int i=0;i<4;i++)
00446        {
00447               btSwapScalarEndian(vector[i],swappedVec[i]);
00448        }
00449        vector = swappedVec;
00450 }
00451 
00452 #endif //SIMD__VECTOR3_H