Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

437 строки
14 KiB

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