00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef BT_VECTOR3_H
00018 #define BT_VECTOR3_H
00019
00020
00021 #include "btScalar.h"
00022 #include "btMinMax.h"
00023
00024 #ifdef BT_USE_DOUBLE_PRECISION
00025 #define btVector3Data btVector3DoubleData
00026 #define btVector3DataName "btVector3DoubleData"
00027 #else
00028 #define btVector3Data btVector3FloatData
00029 #define btVector3DataName "btVector3FloatData"
00030 #endif //BT_USE_DOUBLE_PRECISION
00031
00032
00033
00034
00039 ATTRIBUTE_ALIGNED16(class) btVector3
00040 {
00041 public:
00042
00043 #if defined (__SPU__) && defined (__CELLOS_LV2__)
00044 btScalar m_floats[4];
00045 public:
00046 SIMD_FORCE_INLINE const vec_float4& get128() const
00047 {
00048 return *((const vec_float4*)&m_floats[0]);
00049 }
00050 public:
00051 #else //__CELLOS_LV2__ __SPU__
00052 #ifdef BT_USE_SSE // _WIN32
00053 union {
00054 __m128 mVec128;
00055 btScalar m_floats[4];
00056 };
00057 SIMD_FORCE_INLINE __m128 get128() const
00058 {
00059 return mVec128;
00060 }
00061 SIMD_FORCE_INLINE void set128(__m128 v128)
00062 {
00063 mVec128 = v128;
00064 }
00065 #else
00066 btScalar m_floats[4];
00067 #endif
00068 #endif //__CELLOS_LV2__ __SPU__
00069
00070 public:
00071
00073 SIMD_FORCE_INLINE btVector3() {}
00074
00075
00076
00082 SIMD_FORCE_INLINE btVector3(const btScalar& x, const btScalar& y, const btScalar& z)
00083 {
00084 m_floats[0] = x;
00085 m_floats[1] = y;
00086 m_floats[2] = z;
00087 m_floats[3] = btScalar(0.);
00088 }
00089
00090
00093 SIMD_FORCE_INLINE btVector3& operator+=(const btVector3& v)
00094 {
00095
00096 m_floats[0] += v.m_floats[0]; m_floats[1] += v.m_floats[1];m_floats[2] += v.m_floats[2];
00097 return *this;
00098 }
00099
00100
00103 SIMD_FORCE_INLINE btVector3& operator-=(const btVector3& v)
00104 {
00105 m_floats[0] -= v.m_floats[0]; m_floats[1] -= v.m_floats[1];m_floats[2] -= v.m_floats[2];
00106 return *this;
00107 }
00110 SIMD_FORCE_INLINE btVector3& operator*=(const btScalar& s)
00111 {
00112 m_floats[0] *= s; m_floats[1] *= s;m_floats[2] *= s;
00113 return *this;
00114 }
00115
00118 SIMD_FORCE_INLINE btVector3& operator/=(const btScalar& s)
00119 {
00120 btFullAssert(s != btScalar(0.0));
00121 return *this *= btScalar(1.0) / s;
00122 }
00123
00126 SIMD_FORCE_INLINE btScalar dot(const btVector3& v) const
00127 {
00128 return m_floats[0] * v.m_floats[0] + m_floats[1] * v.m_floats[1] +m_floats[2] * v.m_floats[2];
00129 }
00130
00132 SIMD_FORCE_INLINE btScalar length2() const
00133 {
00134 return dot(*this);
00135 }
00136
00138 SIMD_FORCE_INLINE btScalar length() const
00139 {
00140 return btSqrt(length2());
00141 }
00142
00145 SIMD_FORCE_INLINE btScalar distance2(const btVector3& v) const;
00146
00149 SIMD_FORCE_INLINE btScalar distance(const btVector3& v) const;
00150
00151 SIMD_FORCE_INLINE btVector3& safeNormalize()
00152 {
00153 btVector3 absVec = this->absolute();
00154 int maxIndex = absVec.maxAxis();
00155 if (absVec[maxIndex]>0)
00156 {
00157 *this /= absVec[maxIndex];
00158 return *this /= length();
00159 }
00160 setValue(1,0,0);
00161 return *this;
00162 }
00163
00166 SIMD_FORCE_INLINE btVector3& normalize()
00167 {
00168 return *this /= length();
00169 }
00170
00172 SIMD_FORCE_INLINE btVector3 normalized() const;
00173
00177 SIMD_FORCE_INLINE btVector3 rotate( const btVector3& wAxis, const btScalar angle ) const;
00178
00181 SIMD_FORCE_INLINE btScalar angle(const btVector3& v) const
00182 {
00183 btScalar s = btSqrt(length2() * v.length2());
00184 btFullAssert(s != btScalar(0.0));
00185 return btAcos(dot(v) / s);
00186 }
00188 SIMD_FORCE_INLINE btVector3 absolute() const
00189 {
00190 return btVector3(
00191 btFabs(m_floats[0]),
00192 btFabs(m_floats[1]),
00193 btFabs(m_floats[2]));
00194 }
00197 SIMD_FORCE_INLINE btVector3 cross(const btVector3& v) const
00198 {
00199 return btVector3(
00200 m_floats[1] * v.m_floats[2] -m_floats[2] * v.m_floats[1],
00201 m_floats[2] * v.m_floats[0] - m_floats[0] * v.m_floats[2],
00202 m_floats[0] * v.m_floats[1] - m_floats[1] * v.m_floats[0]);
00203 }
00204
00205 SIMD_FORCE_INLINE btScalar triple(const btVector3& v1, const btVector3& v2) const
00206 {
00207 return m_floats[0] * (v1.m_floats[1] * v2.m_floats[2] - v1.m_floats[2] * v2.m_floats[1]) +
00208 m_floats[1] * (v1.m_floats[2] * v2.m_floats[0] - v1.m_floats[0] * v2.m_floats[2]) +
00209 m_floats[2] * (v1.m_floats[0] * v2.m_floats[1] - v1.m_floats[1] * v2.m_floats[0]);
00210 }
00211
00214 SIMD_FORCE_INLINE int minAxis() const
00215 {
00216 return m_floats[0] < m_floats[1] ? (m_floats[0] <m_floats[2] ? 0 : 2) : (m_floats[1] <m_floats[2] ? 1 : 2);
00217 }
00218
00221 SIMD_FORCE_INLINE int maxAxis() const
00222 {
00223 return m_floats[0] < m_floats[1] ? (m_floats[1] <m_floats[2] ? 2 : 1) : (m_floats[0] <m_floats[2] ? 2 : 0);
00224 }
00225
00226 SIMD_FORCE_INLINE int furthestAxis() const
00227 {
00228 return absolute().minAxis();
00229 }
00230
00231 SIMD_FORCE_INLINE int closestAxis() const
00232 {
00233 return absolute().maxAxis();
00234 }
00235
00236 SIMD_FORCE_INLINE void setInterpolate3(const btVector3& v0, const btVector3& v1, btScalar rt)
00237 {
00238 btScalar s = btScalar(1.0) - rt;
00239 m_floats[0] = s * v0.m_floats[0] + rt * v1.m_floats[0];
00240 m_floats[1] = s * v0.m_floats[1] + rt * v1.m_floats[1];
00241 m_floats[2] = s * v0.m_floats[2] + rt * v1.m_floats[2];
00242
00243
00244 }
00245
00249 SIMD_FORCE_INLINE btVector3 lerp(const btVector3& v, const btScalar& t) const
00250 {
00251 return btVector3(m_floats[0] + (v.m_floats[0] - m_floats[0]) * t,
00252 m_floats[1] + (v.m_floats[1] - m_floats[1]) * t,
00253 m_floats[2] + (v.m_floats[2] -m_floats[2]) * t);
00254 }
00255
00258 SIMD_FORCE_INLINE btVector3& operator*=(const btVector3& v)
00259 {
00260 m_floats[0] *= v.m_floats[0]; m_floats[1] *= v.m_floats[1];m_floats[2] *= v.m_floats[2];
00261 return *this;
00262 }
00263
00265 SIMD_FORCE_INLINE const btScalar& getX() const { return m_floats[0]; }
00267 SIMD_FORCE_INLINE const btScalar& getY() const { return m_floats[1]; }
00269 SIMD_FORCE_INLINE const btScalar& getZ() const { return m_floats[2]; }
00271 SIMD_FORCE_INLINE void setX(btScalar x) { m_floats[0] = x;};
00273 SIMD_FORCE_INLINE void setY(btScalar y) { m_floats[1] = y;};
00275 SIMD_FORCE_INLINE void setZ(btScalar z) {m_floats[2] = z;};
00277 SIMD_FORCE_INLINE void setW(btScalar w) { m_floats[3] = w;};
00279 SIMD_FORCE_INLINE const btScalar& x() const { return m_floats[0]; }
00281 SIMD_FORCE_INLINE const btScalar& y() const { return m_floats[1]; }
00283 SIMD_FORCE_INLINE const btScalar& z() const { return m_floats[2]; }
00285 SIMD_FORCE_INLINE const btScalar& w() const { return m_floats[3]; }
00286
00287
00288
00290 SIMD_FORCE_INLINE operator btScalar *() { return &m_floats[0]; }
00291 SIMD_FORCE_INLINE operator const btScalar *() const { return &m_floats[0]; }
00292
00293 SIMD_FORCE_INLINE bool operator==(const btVector3& other) const
00294 {
00295 return ((m_floats[3]==other.m_floats[3]) && (m_floats[2]==other.m_floats[2]) && (m_floats[1]==other.m_floats[1]) && (m_floats[0]==other.m_floats[0]));
00296 }
00297
00298 SIMD_FORCE_INLINE bool operator!=(const btVector3& other) const
00299 {
00300 return !(*this == other);
00301 }
00302
00306 SIMD_FORCE_INLINE void setMax(const btVector3& other)
00307 {
00308 btSetMax(m_floats[0], other.m_floats[0]);
00309 btSetMax(m_floats[1], other.m_floats[1]);
00310 btSetMax(m_floats[2], other.m_floats[2]);
00311 btSetMax(m_floats[3], other.w());
00312 }
00316 SIMD_FORCE_INLINE void setMin(const btVector3& other)
00317 {
00318 btSetMin(m_floats[0], other.m_floats[0]);
00319 btSetMin(m_floats[1], other.m_floats[1]);
00320 btSetMin(m_floats[2], other.m_floats[2]);
00321 btSetMin(m_floats[3], other.w());
00322 }
00323
00324 SIMD_FORCE_INLINE void setValue(const btScalar& x, const btScalar& y, const btScalar& z)
00325 {
00326 m_floats[0]=x;
00327 m_floats[1]=y;
00328 m_floats[2]=z;
00329 m_floats[3] = btScalar(0.);
00330 }
00331
00332 void getSkewSymmetricMatrix(btVector3* v0,btVector3* v1,btVector3* v2) const
00333 {
00334 v0->setValue(0. ,-z() ,y());
00335 v1->setValue(z() ,0. ,-x());
00336 v2->setValue(-y() ,x() ,0.);
00337 }
00338
00339 void setZero()
00340 {
00341 setValue(btScalar(0.),btScalar(0.),btScalar(0.));
00342 }
00343
00344 SIMD_FORCE_INLINE bool isZero() const
00345 {
00346 return m_floats[0] == btScalar(0) && m_floats[1] == btScalar(0) && m_floats[2] == btScalar(0);
00347 }
00348
00349 SIMD_FORCE_INLINE bool fuzzyZero() const
00350 {
00351 return length2() < SIMD_EPSILON;
00352 }
00353
00354 SIMD_FORCE_INLINE void serialize(struct btVector3Data& dataOut) const;
00355
00356 SIMD_FORCE_INLINE void deSerialize(const struct btVector3Data& dataIn);
00357
00358 SIMD_FORCE_INLINE void serializeFloat(struct btVector3FloatData& dataOut) const;
00359
00360 SIMD_FORCE_INLINE void deSerializeFloat(const struct btVector3FloatData& dataIn);
00361
00362 SIMD_FORCE_INLINE void serializeDouble(struct btVector3DoubleData& dataOut) const;
00363
00364 SIMD_FORCE_INLINE void deSerializeDouble(const struct btVector3DoubleData& dataIn);
00365
00366 };
00367
00369 SIMD_FORCE_INLINE btVector3
00370 operator+(const btVector3& v1, const btVector3& v2)
00371 {
00372 return btVector3(v1.m_floats[0] + v2.m_floats[0], v1.m_floats[1] + v2.m_floats[1], v1.m_floats[2] + v2.m_floats[2]);
00373 }
00374
00376 SIMD_FORCE_INLINE btVector3
00377 operator*(const btVector3& v1, const btVector3& v2)
00378 {
00379 return btVector3(v1.m_floats[0] * v2.m_floats[0], v1.m_floats[1] * v2.m_floats[1], v1.m_floats[2] * v2.m_floats[2]);
00380 }
00381
00383 SIMD_FORCE_INLINE btVector3
00384 operator-(const btVector3& v1, const btVector3& v2)
00385 {
00386 return btVector3(v1.m_floats[0] - v2.m_floats[0], v1.m_floats[1] - v2.m_floats[1], v1.m_floats[2] - v2.m_floats[2]);
00387 }
00389 SIMD_FORCE_INLINE btVector3
00390 operator-(const btVector3& v)
00391 {
00392 return btVector3(-v.m_floats[0], -v.m_floats[1], -v.m_floats[2]);
00393 }
00394
00396 SIMD_FORCE_INLINE btVector3
00397 operator*(const btVector3& v, const btScalar& s)
00398 {
00399 return btVector3(v.m_floats[0] * s, v.m_floats[1] * s, v.m_floats[2] * s);
00400 }
00401
00403 SIMD_FORCE_INLINE btVector3
00404 operator*(const btScalar& s, const btVector3& v)
00405 {
00406 return v * s;
00407 }
00408
00410 SIMD_FORCE_INLINE btVector3
00411 operator/(const btVector3& v, const btScalar& s)
00412 {
00413 btFullAssert(s != btScalar(0.0));
00414 return v * (btScalar(1.0) / s);
00415 }
00416
00418 SIMD_FORCE_INLINE btVector3
00419 operator/(const btVector3& v1, const btVector3& v2)
00420 {
00421 return btVector3(v1.m_floats[0] / v2.m_floats[0],v1.m_floats[1] / v2.m_floats[1],v1.m_floats[2] / v2.m_floats[2]);
00422 }
00423
00425 SIMD_FORCE_INLINE btScalar
00426 btDot(const btVector3& v1, const btVector3& v2)
00427 {
00428 return v1.dot(v2);
00429 }
00430
00431
00433 SIMD_FORCE_INLINE btScalar
00434 btDistance2(const btVector3& v1, const btVector3& v2)
00435 {
00436 return v1.distance2(v2);
00437 }
00438
00439
00441 SIMD_FORCE_INLINE btScalar
00442 btDistance(const btVector3& v1, const btVector3& v2)
00443 {
00444 return v1.distance(v2);
00445 }
00446
00448 SIMD_FORCE_INLINE btScalar
00449 btAngle(const btVector3& v1, const btVector3& v2)
00450 {
00451 return v1.angle(v2);
00452 }
00453
00455 SIMD_FORCE_INLINE btVector3
00456 btCross(const btVector3& v1, const btVector3& v2)
00457 {
00458 return v1.cross(v2);
00459 }
00460
00461 SIMD_FORCE_INLINE btScalar
00462 btTriple(const btVector3& v1, const btVector3& v2, const btVector3& v3)
00463 {
00464 return v1.triple(v2, v3);
00465 }
00466
00471 SIMD_FORCE_INLINE btVector3
00472 lerp(const btVector3& v1, const btVector3& v2, const btScalar& t)
00473 {
00474 return v1.lerp(v2, t);
00475 }
00476
00477
00478
00479 SIMD_FORCE_INLINE btScalar btVector3::distance2(const btVector3& v) const
00480 {
00481 return (v - *this).length2();
00482 }
00483
00484 SIMD_FORCE_INLINE btScalar btVector3::distance(const btVector3& v) const
00485 {
00486 return (v - *this).length();
00487 }
00488
00489 SIMD_FORCE_INLINE btVector3 btVector3::normalized() const
00490 {
00491 return *this / length();
00492 }
00493
00494 SIMD_FORCE_INLINE btVector3 btVector3::rotate( const btVector3& wAxis, const btScalar angle ) const
00495 {
00496
00497
00498 btVector3 o = wAxis * wAxis.dot( *this );
00499 btVector3 x = *this - o;
00500 btVector3 y;
00501
00502 y = wAxis.cross( *this );
00503
00504 return ( o + x * btCos( angle ) + y * btSin( angle ) );
00505 }
00506
00507 class btVector4 : public btVector3
00508 {
00509 public:
00510
00511 SIMD_FORCE_INLINE btVector4() {}
00512
00513
00514 SIMD_FORCE_INLINE btVector4(const btScalar& x, const btScalar& y, const btScalar& z,const btScalar& w)
00515 : btVector3(x,y,z)
00516 {
00517 m_floats[3] = w;
00518 }
00519
00520
00521 SIMD_FORCE_INLINE btVector4 absolute4() const
00522 {
00523 return btVector4(
00524 btFabs(m_floats[0]),
00525 btFabs(m_floats[1]),
00526 btFabs(m_floats[2]),
00527 btFabs(m_floats[3]));
00528 }
00529
00530
00531
00532 btScalar getW() const { return m_floats[3];}
00533
00534
00535 SIMD_FORCE_INLINE int maxAxis4() const
00536 {
00537 int maxIndex = -1;
00538 btScalar maxVal = btScalar(-BT_LARGE_FLOAT);
00539 if (m_floats[0] > maxVal)
00540 {
00541 maxIndex = 0;
00542 maxVal = m_floats[0];
00543 }
00544 if (m_floats[1] > maxVal)
00545 {
00546 maxIndex = 1;
00547 maxVal = m_floats[1];
00548 }
00549 if (m_floats[2] > maxVal)
00550 {
00551 maxIndex = 2;
00552 maxVal =m_floats[2];
00553 }
00554 if (m_floats[3] > maxVal)
00555 {
00556 maxIndex = 3;
00557 maxVal = m_floats[3];
00558 }
00559
00560
00561
00562
00563 return maxIndex;
00564
00565 }
00566
00567
00568 SIMD_FORCE_INLINE int minAxis4() const
00569 {
00570 int minIndex = -1;
00571 btScalar minVal = btScalar(BT_LARGE_FLOAT);
00572 if (m_floats[0] < minVal)
00573 {
00574 minIndex = 0;
00575 minVal = m_floats[0];
00576 }
00577 if (m_floats[1] < minVal)
00578 {
00579 minIndex = 1;
00580 minVal = m_floats[1];
00581 }
00582 if (m_floats[2] < minVal)
00583 {
00584 minIndex = 2;
00585 minVal =m_floats[2];
00586 }
00587 if (m_floats[3] < minVal)
00588 {
00589 minIndex = 3;
00590 minVal = m_floats[3];
00591 }
00592
00593 return minIndex;
00594
00595 }
00596
00597
00598 SIMD_FORCE_INLINE int closestAxis4() const
00599 {
00600 return absolute4().maxAxis4();
00601 }
00602
00603
00604
00605
00613
00614
00615
00616
00617
00618
00619
00626 SIMD_FORCE_INLINE void setValue(const btScalar& x, const btScalar& y, const btScalar& z,const btScalar& w)
00627 {
00628 m_floats[0]=x;
00629 m_floats[1]=y;
00630 m_floats[2]=z;
00631 m_floats[3]=w;
00632 }
00633
00634
00635 };
00636
00637
00639 SIMD_FORCE_INLINE void btSwapScalarEndian(const btScalar& sourceVal, btScalar& destVal)
00640 {
00641 #ifdef BT_USE_DOUBLE_PRECISION
00642 unsigned char* dest = (unsigned char*) &destVal;
00643 unsigned char* src = (unsigned char*) &sourceVal;
00644 dest[0] = src[7];
00645 dest[1] = src[6];
00646 dest[2] = src[5];
00647 dest[3] = src[4];
00648 dest[4] = src[3];
00649 dest[5] = src[2];
00650 dest[6] = src[1];
00651 dest[7] = src[0];
00652 #else
00653 unsigned char* dest = (unsigned char*) &destVal;
00654 unsigned char* src = (unsigned char*) &sourceVal;
00655 dest[0] = src[3];
00656 dest[1] = src[2];
00657 dest[2] = src[1];
00658 dest[3] = src[0];
00659 #endif //BT_USE_DOUBLE_PRECISION
00660 }
00662 SIMD_FORCE_INLINE void btSwapVector3Endian(const btVector3& sourceVec, btVector3& destVec)
00663 {
00664 for (int i=0;i<4;i++)
00665 {
00666 btSwapScalarEndian(sourceVec[i],destVec[i]);
00667 }
00668
00669 }
00670
00672 SIMD_FORCE_INLINE void btUnSwapVector3Endian(btVector3& vector)
00673 {
00674
00675 btVector3 swappedVec;
00676 for (int i=0;i<4;i++)
00677 {
00678 btSwapScalarEndian(vector[i],swappedVec[i]);
00679 }
00680 vector = swappedVec;
00681 }
00682
00683 template <class T>
00684 SIMD_FORCE_INLINE void btPlaneSpace1 (const T& n, T& p, T& q)
00685 {
00686 if (btFabs(n[2]) > SIMDSQRT12) {
00687
00688 btScalar a = n[1]*n[1] + n[2]*n[2];
00689 btScalar k = btRecipSqrt (a);
00690 p[0] = 0;
00691 p[1] = -n[2]*k;
00692 p[2] = n[1]*k;
00693
00694 q[0] = a*k;
00695 q[1] = -n[0]*p[2];
00696 q[2] = n[0]*p[1];
00697 }
00698 else {
00699
00700 btScalar a = n[0]*n[0] + n[1]*n[1];
00701 btScalar k = btRecipSqrt (a);
00702 p[0] = -n[1]*k;
00703 p[1] = n[0]*k;
00704 p[2] = 0;
00705
00706 q[0] = -n[2]*p[1];
00707 q[1] = n[2]*p[0];
00708 q[2] = a*k;
00709 }
00710 }
00711
00712
00713 struct btVector3FloatData
00714 {
00715 float m_floats[4];
00716 };
00717
00718 struct btVector3DoubleData
00719 {
00720 double m_floats[4];
00721
00722 };
00723
00724 SIMD_FORCE_INLINE void btVector3::serializeFloat(struct btVector3FloatData& dataOut) const
00725 {
00727 for (int i=0;i<4;i++)
00728 dataOut.m_floats[i] = float(m_floats[i]);
00729 }
00730
00731 SIMD_FORCE_INLINE void btVector3::deSerializeFloat(const struct btVector3FloatData& dataIn)
00732 {
00733 for (int i=0;i<4;i++)
00734 m_floats[i] = btScalar(dataIn.m_floats[i]);
00735 }
00736
00737
00738 SIMD_FORCE_INLINE void btVector3::serializeDouble(struct btVector3DoubleData& dataOut) const
00739 {
00741 for (int i=0;i<4;i++)
00742 dataOut.m_floats[i] = double(m_floats[i]);
00743 }
00744
00745 SIMD_FORCE_INLINE void btVector3::deSerializeDouble(const struct btVector3DoubleData& dataIn)
00746 {
00747 for (int i=0;i<4;i++)
00748 m_floats[i] = btScalar(dataIn.m_floats[i]);
00749 }
00750
00751
00752 SIMD_FORCE_INLINE void btVector3::serialize(struct btVector3Data& dataOut) const
00753 {
00755 for (int i=0;i<4;i++)
00756 dataOut.m_floats[i] = m_floats[i];
00757 }
00758
00759 SIMD_FORCE_INLINE void btVector3::deSerialize(const struct btVector3Data& dataIn)
00760 {
00761 for (int i=0;i<4;i++)
00762 m_floats[i] = dataIn.m_floats[i];
00763 }
00764
00765
00766 #endif //BT_VECTOR3_H