[1963] | 1 | /* |
---|
| 2 | Copyright (c) 2003-2006 Gino van den Bergen / Erwin Coumans http://continuousphysics.com/Bullet/ |
---|
| 3 | |
---|
| 4 | This software is provided 'as-is', without any express or implied warranty. |
---|
| 5 | In no event will the authors be held liable for any damages arising from the use of this software. |
---|
| 6 | Permission is granted to anyone to use this software for any purpose, |
---|
| 7 | including commercial applications, and to alter it and redistribute it freely, |
---|
| 8 | subject to the following restrictions: |
---|
| 9 | |
---|
| 10 | 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. |
---|
| 11 | 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. |
---|
| 12 | 3. This notice may not be removed or altered from any source distribution. |
---|
| 13 | */ |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | |
---|
[8393] | 17 | #ifndef BT_VECTOR3_H |
---|
| 18 | #define BT_VECTOR3_H |
---|
[1963] | 19 | |
---|
| 20 | |
---|
[2882] | 21 | #include "btScalar.h" |
---|
| 22 | #include "btMinMax.h" |
---|
[8351] | 23 | |
---|
| 24 | #ifdef BT_USE_DOUBLE_PRECISION |
---|
| 25 | #define btVector3Data btVector3DoubleData |
---|
| 26 | #define btVector3DataName "btVector3DoubleData" |
---|
| 27 | #else |
---|
| 28 | #define btVector3Data btVector3FloatData |
---|
| 29 | #define btVector3DataName "btVector3FloatData" |
---|
| 30 | #endif //BT_USE_DOUBLE_PRECISION |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | |
---|
[2430] | 35 | /**@brief btVector3 can be used to represent 3D points and vectors. |
---|
| 36 | * It has an un-used w component to suit 16-byte alignment when btVector3 is stored in containers. This extra component can be used by derived classes (Quaternion?) or by user |
---|
| 37 | * Ideally, this class should be replaced by a platform optimized SIMD version that keeps the data in registers |
---|
| 38 | */ |
---|
[2882] | 39 | ATTRIBUTE_ALIGNED16(class) btVector3 |
---|
| 40 | { |
---|
[1963] | 41 | public: |
---|
[2882] | 42 | |
---|
| 43 | #if defined (__SPU__) && defined (__CELLOS_LV2__) |
---|
| 44 | btScalar m_floats[4]; |
---|
| 45 | public: |
---|
[8351] | 46 | SIMD_FORCE_INLINE const vec_float4& get128() const |
---|
[2882] | 47 | { |
---|
[8351] | 48 | return *((const vec_float4*)&m_floats[0]); |
---|
[2882] | 49 | } |
---|
| 50 | public: |
---|
| 51 | #else //__CELLOS_LV2__ __SPU__ |
---|
[8351] | 52 | #ifdef BT_USE_SSE // _WIN32 |
---|
[2882] | 53 | union { |
---|
| 54 | __m128 mVec128; |
---|
| 55 | btScalar m_floats[4]; |
---|
| 56 | }; |
---|
| 57 | SIMD_FORCE_INLINE __m128 get128() const |
---|
| 58 | { |
---|
| 59 | return mVec128; |
---|
| 60 | } |
---|
| 61 | SIMD_FORCE_INLINE void set128(__m128 v128) |
---|
| 62 | { |
---|
| 63 | mVec128 = v128; |
---|
| 64 | } |
---|
| 65 | #else |
---|
| 66 | btScalar m_floats[4]; |
---|
| 67 | #endif |
---|
| 68 | #endif //__CELLOS_LV2__ __SPU__ |
---|
| 69 | |
---|
| 70 | public: |
---|
| 71 | |
---|
[2430] | 72 | /**@brief No initialization constructor */ |
---|
[1963] | 73 | SIMD_FORCE_INLINE btVector3() {} |
---|
| 74 | |
---|
[2882] | 75 | |
---|
[1963] | 76 | |
---|
[2430] | 77 | /**@brief Constructor from scalars |
---|
| 78 | * @param x X value |
---|
| 79 | * @param y Y value |
---|
| 80 | * @param z Z value |
---|
| 81 | */ |
---|
[2882] | 82 | SIMD_FORCE_INLINE btVector3(const btScalar& x, const btScalar& y, const btScalar& z) |
---|
[1963] | 83 | { |
---|
[2882] | 84 | m_floats[0] = x; |
---|
| 85 | m_floats[1] = y; |
---|
| 86 | m_floats[2] = z; |
---|
| 87 | m_floats[3] = btScalar(0.); |
---|
[1963] | 88 | } |
---|
| 89 | |
---|
| 90 | |
---|
[2430] | 91 | /**@brief Add a vector to this one |
---|
| 92 | * @param The vector to add to this one */ |
---|
[1963] | 93 | SIMD_FORCE_INLINE btVector3& operator+=(const btVector3& v) |
---|
| 94 | { |
---|
| 95 | |
---|
[2882] | 96 | m_floats[0] += v.m_floats[0]; m_floats[1] += v.m_floats[1];m_floats[2] += v.m_floats[2]; |
---|
[1963] | 97 | return *this; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | |
---|
[2430] | 101 | /**@brief Subtract a vector from this one |
---|
| 102 | * @param The vector to subtract */ |
---|
[1963] | 103 | SIMD_FORCE_INLINE btVector3& operator-=(const btVector3& v) |
---|
| 104 | { |
---|
[2882] | 105 | m_floats[0] -= v.m_floats[0]; m_floats[1] -= v.m_floats[1];m_floats[2] -= v.m_floats[2]; |
---|
[1963] | 106 | return *this; |
---|
| 107 | } |
---|
[2430] | 108 | /**@brief Scale the vector |
---|
| 109 | * @param s Scale factor */ |
---|
[1963] | 110 | SIMD_FORCE_INLINE btVector3& operator*=(const btScalar& s) |
---|
| 111 | { |
---|
[2882] | 112 | m_floats[0] *= s; m_floats[1] *= s;m_floats[2] *= s; |
---|
[1963] | 113 | return *this; |
---|
| 114 | } |
---|
| 115 | |
---|
[2430] | 116 | /**@brief Inversely scale the vector |
---|
| 117 | * @param s Scale factor to divide by */ |
---|
[1963] | 118 | SIMD_FORCE_INLINE btVector3& operator/=(const btScalar& s) |
---|
| 119 | { |
---|
| 120 | btFullAssert(s != btScalar(0.0)); |
---|
| 121 | return *this *= btScalar(1.0) / s; |
---|
| 122 | } |
---|
| 123 | |
---|
[2430] | 124 | /**@brief Return the dot product |
---|
| 125 | * @param v The other vector in the dot product */ |
---|
[1963] | 126 | SIMD_FORCE_INLINE btScalar dot(const btVector3& v) const |
---|
| 127 | { |
---|
[2882] | 128 | return m_floats[0] * v.m_floats[0] + m_floats[1] * v.m_floats[1] +m_floats[2] * v.m_floats[2]; |
---|
[1963] | 129 | } |
---|
| 130 | |
---|
[2430] | 131 | /**@brief Return the length of the vector squared */ |
---|
[1963] | 132 | SIMD_FORCE_INLINE btScalar length2() const |
---|
| 133 | { |
---|
| 134 | return dot(*this); |
---|
| 135 | } |
---|
| 136 | |
---|
[2430] | 137 | /**@brief Return the length of the vector */ |
---|
[1963] | 138 | SIMD_FORCE_INLINE btScalar length() const |
---|
| 139 | { |
---|
| 140 | return btSqrt(length2()); |
---|
| 141 | } |
---|
| 142 | |
---|
[2430] | 143 | /**@brief Return the distance squared between the ends of this and another vector |
---|
| 144 | * This is symantically treating the vector like a point */ |
---|
[1963] | 145 | SIMD_FORCE_INLINE btScalar distance2(const btVector3& v) const; |
---|
| 146 | |
---|
[2430] | 147 | /**@brief Return the distance between the ends of this and another vector |
---|
| 148 | * This is symantically treating the vector like a point */ |
---|
[1963] | 149 | SIMD_FORCE_INLINE btScalar distance(const btVector3& v) const; |
---|
| 150 | |
---|
[8351] | 151 | SIMD_FORCE_INLINE btVector3& safeNormalize() |
---|
| 152 | { |
---|
| 153 | btVector3 absVec = this->absolute(); |
---|
| 154 | int maxIndex = absVec.maxAxis(); |
---|
| 155 | if (absVec[maxIndex]>0) |
---|
| 156 | { |
---|
| 157 | *this /= absVec[maxIndex]; |
---|
| 158 | return *this /= length(); |
---|
| 159 | } |
---|
| 160 | setValue(1,0,0); |
---|
| 161 | return *this; |
---|
| 162 | } |
---|
| 163 | |
---|
[2430] | 164 | /**@brief Normalize this vector |
---|
| 165 | * x^2 + y^2 + z^2 = 1 */ |
---|
[1963] | 166 | SIMD_FORCE_INLINE btVector3& normalize() |
---|
| 167 | { |
---|
| 168 | return *this /= length(); |
---|
| 169 | } |
---|
| 170 | |
---|
[2430] | 171 | /**@brief Return a normalized version of this vector */ |
---|
[1963] | 172 | SIMD_FORCE_INLINE btVector3 normalized() const; |
---|
| 173 | |
---|
[8351] | 174 | /**@brief Return a rotated version of this vector |
---|
[2430] | 175 | * @param wAxis The axis to rotate about |
---|
| 176 | * @param angle The angle to rotate by */ |
---|
[8351] | 177 | SIMD_FORCE_INLINE btVector3 rotate( const btVector3& wAxis, const btScalar angle ) const; |
---|
[1963] | 178 | |
---|
[2430] | 179 | /**@brief Return the angle between this and another vector |
---|
| 180 | * @param v The other vector */ |
---|
[1963] | 181 | SIMD_FORCE_INLINE btScalar angle(const btVector3& v) const |
---|
| 182 | { |
---|
| 183 | btScalar s = btSqrt(length2() * v.length2()); |
---|
| 184 | btFullAssert(s != btScalar(0.0)); |
---|
| 185 | return btAcos(dot(v) / s); |
---|
| 186 | } |
---|
[2430] | 187 | /**@brief Return a vector will the absolute values of each element */ |
---|
[1963] | 188 | SIMD_FORCE_INLINE btVector3 absolute() const |
---|
| 189 | { |
---|
| 190 | return btVector3( |
---|
[2430] | 191 | btFabs(m_floats[0]), |
---|
| 192 | btFabs(m_floats[1]), |
---|
| 193 | btFabs(m_floats[2])); |
---|
[1963] | 194 | } |
---|
[2430] | 195 | /**@brief Return the cross product between this and another vector |
---|
| 196 | * @param v The other vector */ |
---|
[1963] | 197 | SIMD_FORCE_INLINE btVector3 cross(const btVector3& v) const |
---|
| 198 | { |
---|
| 199 | return btVector3( |
---|
[2882] | 200 | m_floats[1] * v.m_floats[2] -m_floats[2] * v.m_floats[1], |
---|
| 201 | m_floats[2] * v.m_floats[0] - m_floats[0] * v.m_floats[2], |
---|
| 202 | m_floats[0] * v.m_floats[1] - m_floats[1] * v.m_floats[0]); |
---|
[1963] | 203 | } |
---|
| 204 | |
---|
| 205 | SIMD_FORCE_INLINE btScalar triple(const btVector3& v1, const btVector3& v2) const |
---|
| 206 | { |
---|
[2882] | 207 | return m_floats[0] * (v1.m_floats[1] * v2.m_floats[2] - v1.m_floats[2] * v2.m_floats[1]) + |
---|
| 208 | m_floats[1] * (v1.m_floats[2] * v2.m_floats[0] - v1.m_floats[0] * v2.m_floats[2]) + |
---|
| 209 | m_floats[2] * (v1.m_floats[0] * v2.m_floats[1] - v1.m_floats[1] * v2.m_floats[0]); |
---|
[1963] | 210 | } |
---|
| 211 | |
---|
[2430] | 212 | /**@brief Return the axis with the smallest value |
---|
| 213 | * Note return values are 0,1,2 for x, y, or z */ |
---|
[1963] | 214 | SIMD_FORCE_INLINE int minAxis() const |
---|
| 215 | { |
---|
[2882] | 216 | return m_floats[0] < m_floats[1] ? (m_floats[0] <m_floats[2] ? 0 : 2) : (m_floats[1] <m_floats[2] ? 1 : 2); |
---|
[1963] | 217 | } |
---|
| 218 | |
---|
[2430] | 219 | /**@brief Return the axis with the largest value |
---|
| 220 | * Note return values are 0,1,2 for x, y, or z */ |
---|
[1963] | 221 | SIMD_FORCE_INLINE int maxAxis() const |
---|
| 222 | { |
---|
[2882] | 223 | return m_floats[0] < m_floats[1] ? (m_floats[1] <m_floats[2] ? 2 : 1) : (m_floats[0] <m_floats[2] ? 2 : 0); |
---|
[1963] | 224 | } |
---|
| 225 | |
---|
| 226 | SIMD_FORCE_INLINE int furthestAxis() const |
---|
| 227 | { |
---|
| 228 | return absolute().minAxis(); |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | SIMD_FORCE_INLINE int closestAxis() const |
---|
| 232 | { |
---|
| 233 | return absolute().maxAxis(); |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | SIMD_FORCE_INLINE void setInterpolate3(const btVector3& v0, const btVector3& v1, btScalar rt) |
---|
| 237 | { |
---|
| 238 | btScalar s = btScalar(1.0) - rt; |
---|
[2882] | 239 | m_floats[0] = s * v0.m_floats[0] + rt * v1.m_floats[0]; |
---|
| 240 | m_floats[1] = s * v0.m_floats[1] + rt * v1.m_floats[1]; |
---|
| 241 | m_floats[2] = s * v0.m_floats[2] + rt * v1.m_floats[2]; |
---|
[1963] | 242 | //don't do the unused w component |
---|
| 243 | // m_co[3] = s * v0[3] + rt * v1[3]; |
---|
| 244 | } |
---|
| 245 | |
---|
[2430] | 246 | /**@brief Return the linear interpolation between this and another vector |
---|
| 247 | * @param v The other vector |
---|
| 248 | * @param t The ration of this to v (t = 0 => return this, t=1 => return other) */ |
---|
[1963] | 249 | SIMD_FORCE_INLINE btVector3 lerp(const btVector3& v, const btScalar& t) const |
---|
| 250 | { |
---|
[2882] | 251 | return btVector3(m_floats[0] + (v.m_floats[0] - m_floats[0]) * t, |
---|
| 252 | m_floats[1] + (v.m_floats[1] - m_floats[1]) * t, |
---|
| 253 | m_floats[2] + (v.m_floats[2] -m_floats[2]) * t); |
---|
[1963] | 254 | } |
---|
| 255 | |
---|
[2430] | 256 | /**@brief Elementwise multiply this vector by the other |
---|
| 257 | * @param v The other vector */ |
---|
[1963] | 258 | SIMD_FORCE_INLINE btVector3& operator*=(const btVector3& v) |
---|
| 259 | { |
---|
[2882] | 260 | m_floats[0] *= v.m_floats[0]; m_floats[1] *= v.m_floats[1];m_floats[2] *= v.m_floats[2]; |
---|
[1963] | 261 | return *this; |
---|
| 262 | } |
---|
| 263 | |
---|
[2882] | 264 | /**@brief Return the x value */ |
---|
| 265 | SIMD_FORCE_INLINE const btScalar& getX() const { return m_floats[0]; } |
---|
| 266 | /**@brief Return the y value */ |
---|
| 267 | SIMD_FORCE_INLINE const btScalar& getY() const { return m_floats[1]; } |
---|
| 268 | /**@brief Return the z value */ |
---|
| 269 | SIMD_FORCE_INLINE const btScalar& getZ() const { return m_floats[2]; } |
---|
| 270 | /**@brief Set the x value */ |
---|
| 271 | SIMD_FORCE_INLINE void setX(btScalar x) { m_floats[0] = x;}; |
---|
| 272 | /**@brief Set the y value */ |
---|
| 273 | SIMD_FORCE_INLINE void setY(btScalar y) { m_floats[1] = y;}; |
---|
| 274 | /**@brief Set the z value */ |
---|
| 275 | SIMD_FORCE_INLINE void setZ(btScalar z) {m_floats[2] = z;}; |
---|
| 276 | /**@brief Set the w value */ |
---|
| 277 | SIMD_FORCE_INLINE void setW(btScalar w) { m_floats[3] = w;}; |
---|
| 278 | /**@brief Return the x value */ |
---|
| 279 | SIMD_FORCE_INLINE const btScalar& x() const { return m_floats[0]; } |
---|
| 280 | /**@brief Return the y value */ |
---|
| 281 | SIMD_FORCE_INLINE const btScalar& y() const { return m_floats[1]; } |
---|
| 282 | /**@brief Return the z value */ |
---|
| 283 | SIMD_FORCE_INLINE const btScalar& z() const { return m_floats[2]; } |
---|
| 284 | /**@brief Return the w value */ |
---|
| 285 | SIMD_FORCE_INLINE const btScalar& w() const { return m_floats[3]; } |
---|
[1963] | 286 | |
---|
[2882] | 287 | //SIMD_FORCE_INLINE btScalar& operator[](int i) { return (&m_floats[0])[i]; } |
---|
| 288 | //SIMD_FORCE_INLINE const btScalar& operator[](int i) const { return (&m_floats[0])[i]; } |
---|
| 289 | ///operator btScalar*() replaces operator[], using implicit conversion. We added operator != and operator == to avoid pointer comparisons. |
---|
| 290 | SIMD_FORCE_INLINE operator btScalar *() { return &m_floats[0]; } |
---|
| 291 | SIMD_FORCE_INLINE operator const btScalar *() const { return &m_floats[0]; } |
---|
| 292 | |
---|
| 293 | SIMD_FORCE_INLINE bool operator==(const btVector3& other) const |
---|
| 294 | { |
---|
| 295 | 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])); |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | SIMD_FORCE_INLINE bool operator!=(const btVector3& other) const |
---|
| 299 | { |
---|
| 300 | return !(*this == other); |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | /**@brief Set each element to the max of the current values and the values of another btVector3 |
---|
| 304 | * @param other The other btVector3 to compare with |
---|
| 305 | */ |
---|
| 306 | SIMD_FORCE_INLINE void setMax(const btVector3& other) |
---|
| 307 | { |
---|
| 308 | btSetMax(m_floats[0], other.m_floats[0]); |
---|
| 309 | btSetMax(m_floats[1], other.m_floats[1]); |
---|
| 310 | btSetMax(m_floats[2], other.m_floats[2]); |
---|
| 311 | btSetMax(m_floats[3], other.w()); |
---|
| 312 | } |
---|
| 313 | /**@brief Set each element to the min of the current values and the values of another btVector3 |
---|
| 314 | * @param other The other btVector3 to compare with |
---|
| 315 | */ |
---|
| 316 | SIMD_FORCE_INLINE void setMin(const btVector3& other) |
---|
| 317 | { |
---|
| 318 | btSetMin(m_floats[0], other.m_floats[0]); |
---|
| 319 | btSetMin(m_floats[1], other.m_floats[1]); |
---|
| 320 | btSetMin(m_floats[2], other.m_floats[2]); |
---|
| 321 | btSetMin(m_floats[3], other.w()); |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | SIMD_FORCE_INLINE void setValue(const btScalar& x, const btScalar& y, const btScalar& z) |
---|
| 325 | { |
---|
| 326 | m_floats[0]=x; |
---|
| 327 | m_floats[1]=y; |
---|
| 328 | m_floats[2]=z; |
---|
[8351] | 329 | m_floats[3] = btScalar(0.); |
---|
[2882] | 330 | } |
---|
| 331 | |
---|
| 332 | void getSkewSymmetricMatrix(btVector3* v0,btVector3* v1,btVector3* v2) const |
---|
| 333 | { |
---|
| 334 | v0->setValue(0. ,-z() ,y()); |
---|
| 335 | v1->setValue(z() ,0. ,-x()); |
---|
| 336 | v2->setValue(-y() ,x() ,0.); |
---|
| 337 | } |
---|
| 338 | |
---|
[8351] | 339 | void setZero() |
---|
| 340 | { |
---|
| 341 | setValue(btScalar(0.),btScalar(0.),btScalar(0.)); |
---|
| 342 | } |
---|
| 343 | |
---|
| 344 | SIMD_FORCE_INLINE bool isZero() const |
---|
| 345 | { |
---|
| 346 | return m_floats[0] == btScalar(0) && m_floats[1] == btScalar(0) && m_floats[2] == btScalar(0); |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | SIMD_FORCE_INLINE bool fuzzyZero() const |
---|
| 350 | { |
---|
| 351 | return length2() < SIMD_EPSILON; |
---|
| 352 | } |
---|
| 353 | |
---|
| 354 | SIMD_FORCE_INLINE void serialize(struct btVector3Data& dataOut) const; |
---|
| 355 | |
---|
| 356 | SIMD_FORCE_INLINE void deSerialize(const struct btVector3Data& dataIn); |
---|
| 357 | |
---|
| 358 | SIMD_FORCE_INLINE void serializeFloat(struct btVector3FloatData& dataOut) const; |
---|
| 359 | |
---|
| 360 | SIMD_FORCE_INLINE void deSerializeFloat(const struct btVector3FloatData& dataIn); |
---|
| 361 | |
---|
| 362 | SIMD_FORCE_INLINE void serializeDouble(struct btVector3DoubleData& dataOut) const; |
---|
| 363 | |
---|
| 364 | SIMD_FORCE_INLINE void deSerializeDouble(const struct btVector3DoubleData& dataIn); |
---|
| 365 | |
---|
[1963] | 366 | }; |
---|
| 367 | |
---|
[2430] | 368 | /**@brief Return the sum of two vectors (Point symantics)*/ |
---|
[1963] | 369 | SIMD_FORCE_INLINE btVector3 |
---|
| 370 | operator+(const btVector3& v1, const btVector3& v2) |
---|
| 371 | { |
---|
[2882] | 372 | 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]); |
---|
[1963] | 373 | } |
---|
| 374 | |
---|
[2430] | 375 | /**@brief Return the elementwise product of two vectors */ |
---|
[1963] | 376 | SIMD_FORCE_INLINE btVector3 |
---|
| 377 | operator*(const btVector3& v1, const btVector3& v2) |
---|
| 378 | { |
---|
[2882] | 379 | 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]); |
---|
[1963] | 380 | } |
---|
| 381 | |
---|
[2430] | 382 | /**@brief Return the difference between two vectors */ |
---|
[1963] | 383 | SIMD_FORCE_INLINE btVector3 |
---|
| 384 | operator-(const btVector3& v1, const btVector3& v2) |
---|
| 385 | { |
---|
[2882] | 386 | 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]); |
---|
[1963] | 387 | } |
---|
[2430] | 388 | /**@brief Return the negative of the vector */ |
---|
[1963] | 389 | SIMD_FORCE_INLINE btVector3 |
---|
| 390 | operator-(const btVector3& v) |
---|
| 391 | { |
---|
[2882] | 392 | return btVector3(-v.m_floats[0], -v.m_floats[1], -v.m_floats[2]); |
---|
[1963] | 393 | } |
---|
| 394 | |
---|
[2430] | 395 | /**@brief Return the vector scaled by s */ |
---|
[1963] | 396 | SIMD_FORCE_INLINE btVector3 |
---|
| 397 | operator*(const btVector3& v, const btScalar& s) |
---|
| 398 | { |
---|
[2882] | 399 | return btVector3(v.m_floats[0] * s, v.m_floats[1] * s, v.m_floats[2] * s); |
---|
[1963] | 400 | } |
---|
| 401 | |
---|
[2430] | 402 | /**@brief Return the vector scaled by s */ |
---|
[1963] | 403 | SIMD_FORCE_INLINE btVector3 |
---|
| 404 | operator*(const btScalar& s, const btVector3& v) |
---|
| 405 | { |
---|
| 406 | return v * s; |
---|
| 407 | } |
---|
| 408 | |
---|
[2430] | 409 | /**@brief Return the vector inversely scaled by s */ |
---|
[1963] | 410 | SIMD_FORCE_INLINE btVector3 |
---|
| 411 | operator/(const btVector3& v, const btScalar& s) |
---|
| 412 | { |
---|
| 413 | btFullAssert(s != btScalar(0.0)); |
---|
| 414 | return v * (btScalar(1.0) / s); |
---|
| 415 | } |
---|
| 416 | |
---|
[2430] | 417 | /**@brief Return the vector inversely scaled by s */ |
---|
[1963] | 418 | SIMD_FORCE_INLINE btVector3 |
---|
| 419 | operator/(const btVector3& v1, const btVector3& v2) |
---|
| 420 | { |
---|
[2882] | 421 | 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]); |
---|
[1963] | 422 | } |
---|
| 423 | |
---|
[2430] | 424 | /**@brief Return the dot product between two vectors */ |
---|
[1963] | 425 | SIMD_FORCE_INLINE btScalar |
---|
[8351] | 426 | btDot(const btVector3& v1, const btVector3& v2) |
---|
[1963] | 427 | { |
---|
| 428 | return v1.dot(v2); |
---|
| 429 | } |
---|
| 430 | |
---|
| 431 | |
---|
[2430] | 432 | /**@brief Return the distance squared between two vectors */ |
---|
[1963] | 433 | SIMD_FORCE_INLINE btScalar |
---|
[8351] | 434 | btDistance2(const btVector3& v1, const btVector3& v2) |
---|
[1963] | 435 | { |
---|
| 436 | return v1.distance2(v2); |
---|
| 437 | } |
---|
| 438 | |
---|
| 439 | |
---|
[2430] | 440 | /**@brief Return the distance between two vectors */ |
---|
[1963] | 441 | SIMD_FORCE_INLINE btScalar |
---|
[8351] | 442 | btDistance(const btVector3& v1, const btVector3& v2) |
---|
[1963] | 443 | { |
---|
| 444 | return v1.distance(v2); |
---|
| 445 | } |
---|
| 446 | |
---|
[2430] | 447 | /**@brief Return the angle between two vectors */ |
---|
[1963] | 448 | SIMD_FORCE_INLINE btScalar |
---|
[8351] | 449 | btAngle(const btVector3& v1, const btVector3& v2) |
---|
[1963] | 450 | { |
---|
| 451 | return v1.angle(v2); |
---|
| 452 | } |
---|
| 453 | |
---|
[2430] | 454 | /**@brief Return the cross product of two vectors */ |
---|
[1963] | 455 | SIMD_FORCE_INLINE btVector3 |
---|
[8351] | 456 | btCross(const btVector3& v1, const btVector3& v2) |
---|
[1963] | 457 | { |
---|
| 458 | return v1.cross(v2); |
---|
| 459 | } |
---|
| 460 | |
---|
| 461 | SIMD_FORCE_INLINE btScalar |
---|
[8351] | 462 | btTriple(const btVector3& v1, const btVector3& v2, const btVector3& v3) |
---|
[1963] | 463 | { |
---|
| 464 | return v1.triple(v2, v3); |
---|
| 465 | } |
---|
| 466 | |
---|
[2430] | 467 | /**@brief Return the linear interpolation between two vectors |
---|
| 468 | * @param v1 One vector |
---|
| 469 | * @param v2 The other vector |
---|
| 470 | * @param t The ration of this to v (t = 0 => return v1, t=1 => return v2) */ |
---|
[1963] | 471 | SIMD_FORCE_INLINE btVector3 |
---|
| 472 | lerp(const btVector3& v1, const btVector3& v2, const btScalar& t) |
---|
| 473 | { |
---|
| 474 | return v1.lerp(v2, t); |
---|
| 475 | } |
---|
| 476 | |
---|
| 477 | |
---|
[2882] | 478 | |
---|
[1963] | 479 | SIMD_FORCE_INLINE btScalar btVector3::distance2(const btVector3& v) const |
---|
| 480 | { |
---|
| 481 | return (v - *this).length2(); |
---|
| 482 | } |
---|
| 483 | |
---|
| 484 | SIMD_FORCE_INLINE btScalar btVector3::distance(const btVector3& v) const |
---|
| 485 | { |
---|
| 486 | return (v - *this).length(); |
---|
| 487 | } |
---|
| 488 | |
---|
| 489 | SIMD_FORCE_INLINE btVector3 btVector3::normalized() const |
---|
| 490 | { |
---|
| 491 | return *this / length(); |
---|
| 492 | } |
---|
| 493 | |
---|
[8351] | 494 | SIMD_FORCE_INLINE btVector3 btVector3::rotate( const btVector3& wAxis, const btScalar angle ) const |
---|
[1963] | 495 | { |
---|
| 496 | // wAxis must be a unit lenght vector |
---|
| 497 | |
---|
| 498 | btVector3 o = wAxis * wAxis.dot( *this ); |
---|
| 499 | btVector3 x = *this - o; |
---|
| 500 | btVector3 y; |
---|
| 501 | |
---|
| 502 | y = wAxis.cross( *this ); |
---|
| 503 | |
---|
| 504 | return ( o + x * btCos( angle ) + y * btSin( angle ) ); |
---|
| 505 | } |
---|
| 506 | |
---|
| 507 | class btVector4 : public btVector3 |
---|
| 508 | { |
---|
| 509 | public: |
---|
| 510 | |
---|
| 511 | SIMD_FORCE_INLINE btVector4() {} |
---|
| 512 | |
---|
| 513 | |
---|
| 514 | SIMD_FORCE_INLINE btVector4(const btScalar& x, const btScalar& y, const btScalar& z,const btScalar& w) |
---|
| 515 | : btVector3(x,y,z) |
---|
| 516 | { |
---|
[2430] | 517 | m_floats[3] = w; |
---|
[1963] | 518 | } |
---|
| 519 | |
---|
| 520 | |
---|
| 521 | SIMD_FORCE_INLINE btVector4 absolute4() const |
---|
| 522 | { |
---|
| 523 | return btVector4( |
---|
[2430] | 524 | btFabs(m_floats[0]), |
---|
| 525 | btFabs(m_floats[1]), |
---|
| 526 | btFabs(m_floats[2]), |
---|
| 527 | btFabs(m_floats[3])); |
---|
[1963] | 528 | } |
---|
| 529 | |
---|
| 530 | |
---|
| 531 | |
---|
[2430] | 532 | btScalar getW() const { return m_floats[3];} |
---|
[1963] | 533 | |
---|
| 534 | |
---|
| 535 | SIMD_FORCE_INLINE int maxAxis4() const |
---|
| 536 | { |
---|
| 537 | int maxIndex = -1; |
---|
[8351] | 538 | btScalar maxVal = btScalar(-BT_LARGE_FLOAT); |
---|
[2430] | 539 | if (m_floats[0] > maxVal) |
---|
[1963] | 540 | { |
---|
| 541 | maxIndex = 0; |
---|
[2430] | 542 | maxVal = m_floats[0]; |
---|
[1963] | 543 | } |
---|
[2430] | 544 | if (m_floats[1] > maxVal) |
---|
[1963] | 545 | { |
---|
| 546 | maxIndex = 1; |
---|
[2430] | 547 | maxVal = m_floats[1]; |
---|
[1963] | 548 | } |
---|
[2430] | 549 | if (m_floats[2] > maxVal) |
---|
[1963] | 550 | { |
---|
| 551 | maxIndex = 2; |
---|
[2882] | 552 | maxVal =m_floats[2]; |
---|
[1963] | 553 | } |
---|
[2430] | 554 | if (m_floats[3] > maxVal) |
---|
[1963] | 555 | { |
---|
| 556 | maxIndex = 3; |
---|
[2430] | 557 | maxVal = m_floats[3]; |
---|
[1963] | 558 | } |
---|
| 559 | |
---|
| 560 | |
---|
| 561 | |
---|
| 562 | |
---|
| 563 | return maxIndex; |
---|
| 564 | |
---|
| 565 | } |
---|
| 566 | |
---|
| 567 | |
---|
| 568 | SIMD_FORCE_INLINE int minAxis4() const |
---|
| 569 | { |
---|
| 570 | int minIndex = -1; |
---|
[8351] | 571 | btScalar minVal = btScalar(BT_LARGE_FLOAT); |
---|
[2430] | 572 | if (m_floats[0] < minVal) |
---|
[1963] | 573 | { |
---|
| 574 | minIndex = 0; |
---|
[2430] | 575 | minVal = m_floats[0]; |
---|
[1963] | 576 | } |
---|
[2430] | 577 | if (m_floats[1] < minVal) |
---|
[1963] | 578 | { |
---|
| 579 | minIndex = 1; |
---|
[2430] | 580 | minVal = m_floats[1]; |
---|
[1963] | 581 | } |
---|
[2430] | 582 | if (m_floats[2] < minVal) |
---|
[1963] | 583 | { |
---|
| 584 | minIndex = 2; |
---|
[2882] | 585 | minVal =m_floats[2]; |
---|
[1963] | 586 | } |
---|
[2430] | 587 | if (m_floats[3] < minVal) |
---|
[1963] | 588 | { |
---|
| 589 | minIndex = 3; |
---|
[2430] | 590 | minVal = m_floats[3]; |
---|
[1963] | 591 | } |
---|
| 592 | |
---|
| 593 | return minIndex; |
---|
| 594 | |
---|
| 595 | } |
---|
| 596 | |
---|
| 597 | |
---|
| 598 | SIMD_FORCE_INLINE int closestAxis4() const |
---|
| 599 | { |
---|
| 600 | return absolute4().maxAxis4(); |
---|
| 601 | } |
---|
| 602 | |
---|
[2882] | 603 | |
---|
| 604 | |
---|
| 605 | |
---|
| 606 | /**@brief Set x,y,z and zero w |
---|
| 607 | * @param x Value of x |
---|
| 608 | * @param y Value of y |
---|
| 609 | * @param z Value of z |
---|
| 610 | */ |
---|
| 611 | |
---|
| 612 | |
---|
| 613 | /* void getValue(btScalar *m) const |
---|
| 614 | { |
---|
| 615 | m[0] = m_floats[0]; |
---|
| 616 | m[1] = m_floats[1]; |
---|
| 617 | m[2] =m_floats[2]; |
---|
| 618 | } |
---|
| 619 | */ |
---|
| 620 | /**@brief Set the values |
---|
| 621 | * @param x Value of x |
---|
| 622 | * @param y Value of y |
---|
| 623 | * @param z Value of z |
---|
| 624 | * @param w Value of w |
---|
| 625 | */ |
---|
| 626 | SIMD_FORCE_INLINE void setValue(const btScalar& x, const btScalar& y, const btScalar& z,const btScalar& w) |
---|
| 627 | { |
---|
| 628 | m_floats[0]=x; |
---|
| 629 | m_floats[1]=y; |
---|
| 630 | m_floats[2]=z; |
---|
| 631 | m_floats[3]=w; |
---|
| 632 | } |
---|
| 633 | |
---|
| 634 | |
---|
[1963] | 635 | }; |
---|
| 636 | |
---|
| 637 | |
---|
| 638 | ///btSwapVector3Endian swaps vector endianness, useful for network and cross-platform serialization |
---|
| 639 | SIMD_FORCE_INLINE void btSwapScalarEndian(const btScalar& sourceVal, btScalar& destVal) |
---|
| 640 | { |
---|
| 641 | #ifdef BT_USE_DOUBLE_PRECISION |
---|
| 642 | unsigned char* dest = (unsigned char*) &destVal; |
---|
| 643 | unsigned char* src = (unsigned char*) &sourceVal; |
---|
| 644 | dest[0] = src[7]; |
---|
| 645 | dest[1] = src[6]; |
---|
| 646 | dest[2] = src[5]; |
---|
| 647 | dest[3] = src[4]; |
---|
| 648 | dest[4] = src[3]; |
---|
| 649 | dest[5] = src[2]; |
---|
| 650 | dest[6] = src[1]; |
---|
| 651 | dest[7] = src[0]; |
---|
| 652 | #else |
---|
| 653 | unsigned char* dest = (unsigned char*) &destVal; |
---|
| 654 | unsigned char* src = (unsigned char*) &sourceVal; |
---|
| 655 | dest[0] = src[3]; |
---|
| 656 | dest[1] = src[2]; |
---|
| 657 | dest[2] = src[1]; |
---|
| 658 | dest[3] = src[0]; |
---|
| 659 | #endif //BT_USE_DOUBLE_PRECISION |
---|
| 660 | } |
---|
| 661 | ///btSwapVector3Endian swaps vector endianness, useful for network and cross-platform serialization |
---|
| 662 | SIMD_FORCE_INLINE void btSwapVector3Endian(const btVector3& sourceVec, btVector3& destVec) |
---|
| 663 | { |
---|
| 664 | for (int i=0;i<4;i++) |
---|
| 665 | { |
---|
| 666 | btSwapScalarEndian(sourceVec[i],destVec[i]); |
---|
| 667 | } |
---|
| 668 | |
---|
| 669 | } |
---|
| 670 | |
---|
| 671 | ///btUnSwapVector3Endian swaps vector endianness, useful for network and cross-platform serialization |
---|
| 672 | SIMD_FORCE_INLINE void btUnSwapVector3Endian(btVector3& vector) |
---|
| 673 | { |
---|
| 674 | |
---|
| 675 | btVector3 swappedVec; |
---|
| 676 | for (int i=0;i<4;i++) |
---|
| 677 | { |
---|
| 678 | btSwapScalarEndian(vector[i],swappedVec[i]); |
---|
| 679 | } |
---|
| 680 | vector = swappedVec; |
---|
| 681 | } |
---|
| 682 | |
---|
[8351] | 683 | template <class T> |
---|
| 684 | SIMD_FORCE_INLINE void btPlaneSpace1 (const T& n, T& p, T& q) |
---|
| 685 | { |
---|
| 686 | if (btFabs(n[2]) > SIMDSQRT12) { |
---|
| 687 | // choose p in y-z plane |
---|
| 688 | btScalar a = n[1]*n[1] + n[2]*n[2]; |
---|
| 689 | btScalar k = btRecipSqrt (a); |
---|
| 690 | p[0] = 0; |
---|
| 691 | p[1] = -n[2]*k; |
---|
| 692 | p[2] = n[1]*k; |
---|
| 693 | // set q = n x p |
---|
| 694 | q[0] = a*k; |
---|
| 695 | q[1] = -n[0]*p[2]; |
---|
| 696 | q[2] = n[0]*p[1]; |
---|
| 697 | } |
---|
| 698 | else { |
---|
| 699 | // choose p in x-y plane |
---|
| 700 | btScalar a = n[0]*n[0] + n[1]*n[1]; |
---|
| 701 | btScalar k = btRecipSqrt (a); |
---|
| 702 | p[0] = -n[1]*k; |
---|
| 703 | p[1] = n[0]*k; |
---|
| 704 | p[2] = 0; |
---|
| 705 | // set q = n x p |
---|
| 706 | q[0] = -n[2]*p[1]; |
---|
| 707 | q[1] = n[2]*p[0]; |
---|
| 708 | q[2] = a*k; |
---|
| 709 | } |
---|
| 710 | } |
---|
| 711 | |
---|
| 712 | |
---|
| 713 | struct btVector3FloatData |
---|
| 714 | { |
---|
| 715 | float m_floats[4]; |
---|
| 716 | }; |
---|
| 717 | |
---|
| 718 | struct btVector3DoubleData |
---|
| 719 | { |
---|
| 720 | double m_floats[4]; |
---|
| 721 | |
---|
| 722 | }; |
---|
| 723 | |
---|
| 724 | SIMD_FORCE_INLINE void btVector3::serializeFloat(struct btVector3FloatData& dataOut) const |
---|
| 725 | { |
---|
| 726 | ///could also do a memcpy, check if it is worth it |
---|
| 727 | for (int i=0;i<4;i++) |
---|
| 728 | dataOut.m_floats[i] = float(m_floats[i]); |
---|
| 729 | } |
---|
| 730 | |
---|
| 731 | SIMD_FORCE_INLINE void btVector3::deSerializeFloat(const struct btVector3FloatData& dataIn) |
---|
| 732 | { |
---|
| 733 | for (int i=0;i<4;i++) |
---|
| 734 | m_floats[i] = btScalar(dataIn.m_floats[i]); |
---|
| 735 | } |
---|
| 736 | |
---|
| 737 | |
---|
| 738 | SIMD_FORCE_INLINE void btVector3::serializeDouble(struct btVector3DoubleData& dataOut) const |
---|
| 739 | { |
---|
| 740 | ///could also do a memcpy, check if it is worth it |
---|
| 741 | for (int i=0;i<4;i++) |
---|
| 742 | dataOut.m_floats[i] = double(m_floats[i]); |
---|
| 743 | } |
---|
| 744 | |
---|
| 745 | SIMD_FORCE_INLINE void btVector3::deSerializeDouble(const struct btVector3DoubleData& dataIn) |
---|
| 746 | { |
---|
| 747 | for (int i=0;i<4;i++) |
---|
| 748 | m_floats[i] = btScalar(dataIn.m_floats[i]); |
---|
| 749 | } |
---|
| 750 | |
---|
| 751 | |
---|
| 752 | SIMD_FORCE_INLINE void btVector3::serialize(struct btVector3Data& dataOut) const |
---|
| 753 | { |
---|
| 754 | ///could also do a memcpy, check if it is worth it |
---|
| 755 | for (int i=0;i<4;i++) |
---|
| 756 | dataOut.m_floats[i] = m_floats[i]; |
---|
| 757 | } |
---|
| 758 | |
---|
| 759 | SIMD_FORCE_INLINE void btVector3::deSerialize(const struct btVector3Data& dataIn) |
---|
| 760 | { |
---|
| 761 | for (int i=0;i<4;i++) |
---|
| 762 | m_floats[i] = dataIn.m_floats[i]; |
---|
| 763 | } |
---|
| 764 | |
---|
| 765 | |
---|
[8393] | 766 | #endif //BT_VECTOR3_H |
---|