[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 | |
---|
| 17 | #ifndef SIMD__QUATERNION_H_ |
---|
| 18 | #define SIMD__QUATERNION_H_ |
---|
| 19 | |
---|
[2430] | 20 | |
---|
[1963] | 21 | #include "btVector3.h" |
---|
| 22 | |
---|
[2430] | 23 | /**@brief The btQuaternion implements quaternion to perform linear algebra rotations in combination with btMatrix3x3, btVector3 and btTransform. */ |
---|
[1963] | 24 | class btQuaternion : public btQuadWord { |
---|
| 25 | public: |
---|
[2430] | 26 | /**@brief No initialization constructor */ |
---|
[1963] | 27 | btQuaternion() {} |
---|
| 28 | |
---|
| 29 | // template <typename btScalar> |
---|
| 30 | // explicit Quaternion(const btScalar *v) : Tuple4<btScalar>(v) {} |
---|
[2430] | 31 | /**@brief Constructor from scalars */ |
---|
[1963] | 32 | btQuaternion(const btScalar& x, const btScalar& y, const btScalar& z, const btScalar& w) |
---|
| 33 | : btQuadWord(x, y, z, w) |
---|
| 34 | {} |
---|
[2430] | 35 | /**@brief Axis angle Constructor |
---|
| 36 | * @param axis The axis which the rotation is around |
---|
| 37 | * @param angle The magnitude of the rotation around the angle (Radians) */ |
---|
[1963] | 38 | btQuaternion(const btVector3& axis, const btScalar& angle) |
---|
| 39 | { |
---|
| 40 | setRotation(axis, angle); |
---|
| 41 | } |
---|
[2430] | 42 | /**@brief Constructor from Euler angles |
---|
| 43 | * @param yaw Angle around Y unless BT_EULER_DEFAULT_ZYX defined then Z |
---|
| 44 | * @param pitch Angle around X unless BT_EULER_DEFAULT_ZYX defined then Y |
---|
| 45 | * @param roll Angle around Z unless BT_EULER_DEFAULT_ZYX defined then X */ |
---|
[1963] | 46 | btQuaternion(const btScalar& yaw, const btScalar& pitch, const btScalar& roll) |
---|
| 47 | { |
---|
[2430] | 48 | #ifndef BT_EULER_DEFAULT_ZYX |
---|
[1963] | 49 | setEuler(yaw, pitch, roll); |
---|
[2430] | 50 | #else |
---|
| 51 | setEulerZYX(yaw, pitch, roll); |
---|
| 52 | #endif |
---|
[1963] | 53 | } |
---|
[2430] | 54 | /**@brief Set the rotation using axis angle notation |
---|
| 55 | * @param axis The axis around which to rotate |
---|
| 56 | * @param angle The magnitude of the rotation in Radians */ |
---|
[1963] | 57 | void setRotation(const btVector3& axis, const btScalar& angle) |
---|
| 58 | { |
---|
| 59 | btScalar d = axis.length(); |
---|
| 60 | assert(d != btScalar(0.0)); |
---|
| 61 | btScalar s = btSin(angle * btScalar(0.5)) / d; |
---|
| 62 | setValue(axis.x() * s, axis.y() * s, axis.z() * s, |
---|
| 63 | btCos(angle * btScalar(0.5))); |
---|
| 64 | } |
---|
[2430] | 65 | /**@brief Set the quaternion using Euler angles |
---|
| 66 | * @param yaw Angle around Y |
---|
| 67 | * @param pitch Angle around X |
---|
| 68 | * @param roll Angle around Z */ |
---|
[1963] | 69 | void setEuler(const btScalar& yaw, const btScalar& pitch, const btScalar& roll) |
---|
| 70 | { |
---|
| 71 | btScalar halfYaw = btScalar(yaw) * btScalar(0.5); |
---|
| 72 | btScalar halfPitch = btScalar(pitch) * btScalar(0.5); |
---|
| 73 | btScalar halfRoll = btScalar(roll) * btScalar(0.5); |
---|
| 74 | btScalar cosYaw = btCos(halfYaw); |
---|
| 75 | btScalar sinYaw = btSin(halfYaw); |
---|
| 76 | btScalar cosPitch = btCos(halfPitch); |
---|
| 77 | btScalar sinPitch = btSin(halfPitch); |
---|
| 78 | btScalar cosRoll = btCos(halfRoll); |
---|
| 79 | btScalar sinRoll = btSin(halfRoll); |
---|
| 80 | setValue(cosRoll * sinPitch * cosYaw + sinRoll * cosPitch * sinYaw, |
---|
| 81 | cosRoll * cosPitch * sinYaw - sinRoll * sinPitch * cosYaw, |
---|
| 82 | sinRoll * cosPitch * cosYaw - cosRoll * sinPitch * sinYaw, |
---|
| 83 | cosRoll * cosPitch * cosYaw + sinRoll * sinPitch * sinYaw); |
---|
| 84 | } |
---|
[2430] | 85 | /**@brief Set the quaternion using euler angles |
---|
| 86 | * @param yaw Angle around Z |
---|
| 87 | * @param pitch Angle around Y |
---|
| 88 | * @param roll Angle around X */ |
---|
| 89 | void setEulerZYX(const btScalar& yaw, const btScalar& pitch, const btScalar& roll) |
---|
[1963] | 90 | { |
---|
[2430] | 91 | btScalar halfYaw = btScalar(yaw) * btScalar(0.5); |
---|
| 92 | btScalar halfPitch = btScalar(pitch) * btScalar(0.5); |
---|
| 93 | btScalar halfRoll = btScalar(roll) * btScalar(0.5); |
---|
| 94 | btScalar cosYaw = btCos(halfYaw); |
---|
| 95 | btScalar sinYaw = btSin(halfYaw); |
---|
| 96 | btScalar cosPitch = btCos(halfPitch); |
---|
| 97 | btScalar sinPitch = btSin(halfPitch); |
---|
| 98 | btScalar cosRoll = btCos(halfRoll); |
---|
| 99 | btScalar sinRoll = btSin(halfRoll); |
---|
| 100 | setValue(sinRoll * cosPitch * cosYaw - cosRoll * sinPitch * sinYaw, //x |
---|
| 101 | cosRoll * sinPitch * cosYaw + sinRoll * cosPitch * sinYaw, //y |
---|
| 102 | cosRoll * cosPitch * sinYaw - sinRoll * sinPitch * cosYaw, //z |
---|
| 103 | cosRoll * cosPitch * cosYaw + sinRoll * sinPitch * sinYaw); //formerly yzx |
---|
| 104 | } |
---|
| 105 | /**@brief Add two quaternions |
---|
| 106 | * @param q The quaternion to add to this one */ |
---|
| 107 | SIMD_FORCE_INLINE btQuaternion& operator+=(const btQuaternion& q) |
---|
| 108 | { |
---|
| 109 | m_floats[0] += q.x(); m_floats[1] += q.y(); m_floats[2] += q.z(); m_floats[3] += q.m_floats[3]; |
---|
[1963] | 110 | return *this; |
---|
| 111 | } |
---|
| 112 | |
---|
[2430] | 113 | /**@brief Subtract out a quaternion |
---|
| 114 | * @param q The quaternion to subtract from this one */ |
---|
[1963] | 115 | btQuaternion& operator-=(const btQuaternion& q) |
---|
| 116 | { |
---|
[2430] | 117 | m_floats[0] -= q.x(); m_floats[1] -= q.y(); m_floats[2] -= q.z(); m_floats[3] -= q.m_floats[3]; |
---|
[1963] | 118 | return *this; |
---|
| 119 | } |
---|
| 120 | |
---|
[2430] | 121 | /**@brief Scale this quaternion |
---|
| 122 | * @param s The scalar to scale by */ |
---|
[1963] | 123 | btQuaternion& operator*=(const btScalar& s) |
---|
| 124 | { |
---|
[2430] | 125 | m_floats[0] *= s; m_floats[1] *= s; m_floats[2] *= s; m_floats[3] *= s; |
---|
[1963] | 126 | return *this; |
---|
| 127 | } |
---|
| 128 | |
---|
[2430] | 129 | /**@brief Multiply this quaternion by q on the right |
---|
| 130 | * @param q The other quaternion |
---|
| 131 | * Equivilant to this = this * q */ |
---|
[1963] | 132 | btQuaternion& operator*=(const btQuaternion& q) |
---|
| 133 | { |
---|
[2430] | 134 | setValue(m_floats[3] * q.x() + m_floats[0] * q.m_floats[3] + m_floats[1] * q.z() - m_floats[2] * q.y(), |
---|
| 135 | m_floats[3] * q.y() + m_floats[1] * q.m_floats[3] + m_floats[2] * q.x() - m_floats[0] * q.z(), |
---|
| 136 | m_floats[3] * q.z() + m_floats[2] * q.m_floats[3] + m_floats[0] * q.y() - m_floats[1] * q.x(), |
---|
| 137 | m_floats[3] * q.m_floats[3] - m_floats[0] * q.x() - m_floats[1] * q.y() - m_floats[2] * q.z()); |
---|
[1963] | 138 | return *this; |
---|
| 139 | } |
---|
[2430] | 140 | /**@brief Return the dot product between this quaternion and another |
---|
| 141 | * @param q The other quaternion */ |
---|
[1963] | 142 | btScalar dot(const btQuaternion& q) const |
---|
| 143 | { |
---|
[2430] | 144 | return m_floats[0] * q.x() + m_floats[1] * q.y() + m_floats[2] * q.z() + m_floats[3] * q.m_floats[3]; |
---|
[1963] | 145 | } |
---|
| 146 | |
---|
[2430] | 147 | /**@brief Return the length squared of the quaternion */ |
---|
[1963] | 148 | btScalar length2() const |
---|
| 149 | { |
---|
| 150 | return dot(*this); |
---|
| 151 | } |
---|
| 152 | |
---|
[2430] | 153 | /**@brief Return the length of the quaternion */ |
---|
[1963] | 154 | btScalar length() const |
---|
| 155 | { |
---|
| 156 | return btSqrt(length2()); |
---|
| 157 | } |
---|
| 158 | |
---|
[2430] | 159 | /**@brief Normalize the quaternion |
---|
| 160 | * Such that x^2 + y^2 + z^2 +w^2 = 1 */ |
---|
[1963] | 161 | btQuaternion& normalize() |
---|
| 162 | { |
---|
| 163 | return *this /= length(); |
---|
| 164 | } |
---|
| 165 | |
---|
[2430] | 166 | /**@brief Return a scaled version of this quaternion |
---|
| 167 | * @param s The scale factor */ |
---|
[1963] | 168 | SIMD_FORCE_INLINE btQuaternion |
---|
| 169 | operator*(const btScalar& s) const |
---|
| 170 | { |
---|
[2430] | 171 | return btQuaternion(x() * s, y() * s, z() * s, m_floats[3] * s); |
---|
[1963] | 172 | } |
---|
| 173 | |
---|
| 174 | |
---|
[2430] | 175 | /**@brief Return an inversely scaled versionof this quaternion |
---|
| 176 | * @param s The inverse scale factor */ |
---|
[1963] | 177 | btQuaternion operator/(const btScalar& s) const |
---|
| 178 | { |
---|
| 179 | assert(s != btScalar(0.0)); |
---|
| 180 | return *this * (btScalar(1.0) / s); |
---|
| 181 | } |
---|
| 182 | |
---|
[2430] | 183 | /**@brief Inversely scale this quaternion |
---|
| 184 | * @param s The scale factor */ |
---|
[1963] | 185 | btQuaternion& operator/=(const btScalar& s) |
---|
| 186 | { |
---|
| 187 | assert(s != btScalar(0.0)); |
---|
| 188 | return *this *= btScalar(1.0) / s; |
---|
| 189 | } |
---|
| 190 | |
---|
[2430] | 191 | /**@brief Return a normalized version of this quaternion */ |
---|
[1963] | 192 | btQuaternion normalized() const |
---|
| 193 | { |
---|
| 194 | return *this / length(); |
---|
| 195 | } |
---|
[2430] | 196 | /**@brief Return the angle between this quaternion and the other |
---|
| 197 | * @param q The other quaternion */ |
---|
[1963] | 198 | btScalar angle(const btQuaternion& q) const |
---|
| 199 | { |
---|
| 200 | btScalar s = btSqrt(length2() * q.length2()); |
---|
| 201 | assert(s != btScalar(0.0)); |
---|
| 202 | return btAcos(dot(q) / s); |
---|
| 203 | } |
---|
[2430] | 204 | /**@brief Return the angle of rotation represented by this quaternion */ |
---|
[1963] | 205 | btScalar getAngle() const |
---|
| 206 | { |
---|
[2430] | 207 | btScalar s = btScalar(2.) * btAcos(m_floats[3]); |
---|
[1963] | 208 | return s; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | |
---|
[2430] | 212 | /**@brief Return the inverse of this quaternion */ |
---|
[1963] | 213 | btQuaternion inverse() const |
---|
| 214 | { |
---|
[2430] | 215 | return btQuaternion(-m_floats[0], -m_floats[1], -m_floats[2], m_floats[3]); |
---|
[1963] | 216 | } |
---|
| 217 | |
---|
[2430] | 218 | /**@brief Return the sum of this quaternion and the other |
---|
| 219 | * @param q2 The other quaternion */ |
---|
[1963] | 220 | SIMD_FORCE_INLINE btQuaternion |
---|
| 221 | operator+(const btQuaternion& q2) const |
---|
| 222 | { |
---|
| 223 | const btQuaternion& q1 = *this; |
---|
[2430] | 224 | return btQuaternion(q1.x() + q2.x(), q1.y() + q2.y(), q1.z() + q2.z(), q1.m_floats[3] + q2.m_floats[3]); |
---|
[1963] | 225 | } |
---|
| 226 | |
---|
[2430] | 227 | /**@brief Return the difference between this quaternion and the other |
---|
| 228 | * @param q2 The other quaternion */ |
---|
[1963] | 229 | SIMD_FORCE_INLINE btQuaternion |
---|
| 230 | operator-(const btQuaternion& q2) const |
---|
| 231 | { |
---|
| 232 | const btQuaternion& q1 = *this; |
---|
[2430] | 233 | return btQuaternion(q1.x() - q2.x(), q1.y() - q2.y(), q1.z() - q2.z(), q1.m_floats[3] - q2.m_floats[3]); |
---|
[1963] | 234 | } |
---|
| 235 | |
---|
[2430] | 236 | /**@brief Return the negative of this quaternion |
---|
| 237 | * This simply negates each element */ |
---|
[1963] | 238 | SIMD_FORCE_INLINE btQuaternion operator-() const |
---|
| 239 | { |
---|
| 240 | const btQuaternion& q2 = *this; |
---|
[2430] | 241 | return btQuaternion( - q2.x(), - q2.y(), - q2.z(), - q2.m_floats[3]); |
---|
[1963] | 242 | } |
---|
[2430] | 243 | /**@todo document this and it's use */ |
---|
[1963] | 244 | SIMD_FORCE_INLINE btQuaternion farthest( const btQuaternion& qd) const |
---|
| 245 | { |
---|
| 246 | btQuaternion diff,sum; |
---|
| 247 | diff = *this - qd; |
---|
| 248 | sum = *this + qd; |
---|
| 249 | if( diff.dot(diff) > sum.dot(sum) ) |
---|
| 250 | return qd; |
---|
| 251 | return (-qd); |
---|
| 252 | } |
---|
| 253 | |
---|
[2430] | 254 | /**@brief Return the quaternion which is the result of Spherical Linear Interpolation between this and the other quaternion |
---|
| 255 | * @param q The other quaternion to interpolate with |
---|
| 256 | * @param t The ratio between this and q to interpolate. If t = 0 the result is this, if t=1 the result is q. |
---|
| 257 | * Slerp interpolates assuming constant velocity. */ |
---|
[1963] | 258 | btQuaternion slerp(const btQuaternion& q, const btScalar& t) const |
---|
| 259 | { |
---|
| 260 | btScalar theta = angle(q); |
---|
| 261 | if (theta != btScalar(0.0)) |
---|
| 262 | { |
---|
| 263 | btScalar d = btScalar(1.0) / btSin(theta); |
---|
| 264 | btScalar s0 = btSin((btScalar(1.0) - t) * theta); |
---|
| 265 | btScalar s1 = btSin(t * theta); |
---|
[2430] | 266 | return btQuaternion((m_floats[0] * s0 + q.x() * s1) * d, |
---|
| 267 | (m_floats[1] * s0 + q.y() * s1) * d, |
---|
| 268 | (m_floats[2] * s0 + q.z() * s1) * d, |
---|
| 269 | (m_floats[3] * s0 + q.m_floats[3] * s1) * d); |
---|
[1963] | 270 | } |
---|
| 271 | else |
---|
| 272 | { |
---|
| 273 | return *this; |
---|
| 274 | } |
---|
| 275 | } |
---|
| 276 | |
---|
[2430] | 277 | SIMD_FORCE_INLINE const btScalar& getW() const { return m_floats[3]; } |
---|
[1963] | 278 | |
---|
| 279 | |
---|
| 280 | }; |
---|
| 281 | |
---|
| 282 | |
---|
[2430] | 283 | /**@brief Return the negative of a quaternion */ |
---|
[1963] | 284 | SIMD_FORCE_INLINE btQuaternion |
---|
| 285 | operator-(const btQuaternion& q) |
---|
| 286 | { |
---|
| 287 | return btQuaternion(-q.x(), -q.y(), -q.z(), -q.w()); |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | |
---|
| 291 | |
---|
[2430] | 292 | /**@brief Return the product of two quaternions */ |
---|
[1963] | 293 | SIMD_FORCE_INLINE btQuaternion |
---|
| 294 | operator*(const btQuaternion& q1, const btQuaternion& q2) { |
---|
| 295 | return btQuaternion(q1.w() * q2.x() + q1.x() * q2.w() + q1.y() * q2.z() - q1.z() * q2.y(), |
---|
| 296 | q1.w() * q2.y() + q1.y() * q2.w() + q1.z() * q2.x() - q1.x() * q2.z(), |
---|
| 297 | q1.w() * q2.z() + q1.z() * q2.w() + q1.x() * q2.y() - q1.y() * q2.x(), |
---|
| 298 | q1.w() * q2.w() - q1.x() * q2.x() - q1.y() * q2.y() - q1.z() * q2.z()); |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | SIMD_FORCE_INLINE btQuaternion |
---|
| 302 | operator*(const btQuaternion& q, const btVector3& w) |
---|
| 303 | { |
---|
| 304 | return btQuaternion( q.w() * w.x() + q.y() * w.z() - q.z() * w.y(), |
---|
| 305 | q.w() * w.y() + q.z() * w.x() - q.x() * w.z(), |
---|
| 306 | q.w() * w.z() + q.x() * w.y() - q.y() * w.x(), |
---|
| 307 | -q.x() * w.x() - q.y() * w.y() - q.z() * w.z()); |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | SIMD_FORCE_INLINE btQuaternion |
---|
| 311 | operator*(const btVector3& w, const btQuaternion& q) |
---|
| 312 | { |
---|
| 313 | return btQuaternion( w.x() * q.w() + w.y() * q.z() - w.z() * q.y(), |
---|
| 314 | w.y() * q.w() + w.z() * q.x() - w.x() * q.z(), |
---|
| 315 | w.z() * q.w() + w.x() * q.y() - w.y() * q.x(), |
---|
| 316 | -w.x() * q.x() - w.y() * q.y() - w.z() * q.z()); |
---|
| 317 | } |
---|
| 318 | |
---|
[2430] | 319 | /**@brief Calculate the dot product between two quaternions */ |
---|
[1963] | 320 | SIMD_FORCE_INLINE btScalar |
---|
| 321 | dot(const btQuaternion& q1, const btQuaternion& q2) |
---|
| 322 | { |
---|
| 323 | return q1.dot(q2); |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | |
---|
[2430] | 327 | /**@brief Return the length of a quaternion */ |
---|
[1963] | 328 | SIMD_FORCE_INLINE btScalar |
---|
| 329 | length(const btQuaternion& q) |
---|
| 330 | { |
---|
| 331 | return q.length(); |
---|
| 332 | } |
---|
| 333 | |
---|
[2430] | 334 | /**@brief Return the angle between two quaternions*/ |
---|
[1963] | 335 | SIMD_FORCE_INLINE btScalar |
---|
| 336 | angle(const btQuaternion& q1, const btQuaternion& q2) |
---|
| 337 | { |
---|
| 338 | return q1.angle(q2); |
---|
| 339 | } |
---|
| 340 | |
---|
[2430] | 341 | /**@brief Return the inverse of a quaternion*/ |
---|
[1963] | 342 | SIMD_FORCE_INLINE btQuaternion |
---|
| 343 | inverse(const btQuaternion& q) |
---|
| 344 | { |
---|
| 345 | return q.inverse(); |
---|
| 346 | } |
---|
| 347 | |
---|
[2430] | 348 | /**@brief Return the result of spherical linear interpolation betwen two quaternions |
---|
| 349 | * @param q1 The first quaternion |
---|
| 350 | * @param q2 The second quaternion |
---|
| 351 | * @param t The ration between q1 and q2. t = 0 return q1, t=1 returns q2 |
---|
| 352 | * Slerp assumes constant velocity between positions. */ |
---|
[1963] | 353 | SIMD_FORCE_INLINE btQuaternion |
---|
| 354 | slerp(const btQuaternion& q1, const btQuaternion& q2, const btScalar& t) |
---|
| 355 | { |
---|
| 356 | return q1.slerp(q2, t); |
---|
| 357 | } |
---|
| 358 | |
---|
| 359 | SIMD_FORCE_INLINE btVector3 |
---|
| 360 | quatRotate(const btQuaternion& rotation, const btVector3& v) |
---|
| 361 | { |
---|
| 362 | btQuaternion q = rotation * v; |
---|
| 363 | q *= rotation.inverse(); |
---|
| 364 | return btVector3(q.getX(),q.getY(),q.getZ()); |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | SIMD_FORCE_INLINE btQuaternion |
---|
| 368 | shortestArcQuat(const btVector3& v0, const btVector3& v1) // Game Programming Gems 2.10. make sure v0,v1 are normalized |
---|
| 369 | { |
---|
| 370 | btVector3 c = v0.cross(v1); |
---|
| 371 | btScalar d = v0.dot(v1); |
---|
| 372 | |
---|
| 373 | if (d < -1.0 + SIMD_EPSILON) |
---|
| 374 | return btQuaternion(0.0f,1.0f,0.0f,0.0f); // just pick any vector |
---|
| 375 | |
---|
| 376 | btScalar s = btSqrt((1.0f + d) * 2.0f); |
---|
| 377 | btScalar rs = 1.0f / s; |
---|
| 378 | |
---|
| 379 | return btQuaternion(c.getX()*rs,c.getY()*rs,c.getZ()*rs,s * 0.5f); |
---|
| 380 | } |
---|
| 381 | |
---|
| 382 | SIMD_FORCE_INLINE btQuaternion |
---|
| 383 | shortestArcQuatNormalize2(btVector3& v0,btVector3& v1) |
---|
| 384 | { |
---|
| 385 | v0.normalize(); |
---|
| 386 | v1.normalize(); |
---|
| 387 | return shortestArcQuat(v0,v1); |
---|
| 388 | } |
---|
| 389 | |
---|
| 390 | #endif |
---|
| 391 | |
---|
| 392 | |
---|
| 393 | |
---|
| 394 | |
---|