btQuaternion.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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. #ifdef BT_USE_DOUBLE_PRECISION
  17. #define btQuaternionData btQuaternionDoubleData
  18. #define btQuaternionDataName "btQuaternionDoubleData"
  19. #else
  20. #define btQuaternionData btQuaternionFloatData
  21. #define btQuaternionDataName "btQuaternionFloatData"
  22. #endif //BT_USE_DOUBLE_PRECISION
  23. #ifdef BT_USE_SSE
  24. //const __m128 ATTRIBUTE_ALIGNED16(vOnes) = {1.0f, 1.0f, 1.0f, 1.0f};
  25. #define vOnes (_mm_set_ps(1.0f, 1.0f, 1.0f, 1.0f))
  26. #endif
  27. #if defined(BT_USE_SSE)
  28. #define vQInv (_mm_set_ps(+0.0f, -0.0f, -0.0f, -0.0f))
  29. #define vPPPM (_mm_set_ps(-0.0f, +0.0f, +0.0f, +0.0f))
  30. #elif defined(BT_USE_NEON)
  31. const btSimdFloat4 ATTRIBUTE_ALIGNED16(vQInv) = {-0.0f, -0.0f, -0.0f, +0.0f};
  32. const btSimdFloat4 ATTRIBUTE_ALIGNED16(vPPPM) = {+0.0f, +0.0f, +0.0f, -0.0f};
  33. #endif
  34. /**@brief The btQuaternion implements quaternion to perform linear algebra rotations in combination with btMatrix3x3, btVector3 and btTransform. */
  35. class btQuaternion : public btQuadWord {
  36. public:
  37. /**@brief No initialization constructor */
  38. btQuaternion() {}
  39. #if (defined(BT_USE_SSE_IN_API) && defined(BT_USE_SSE))|| defined(BT_USE_NEON)
  40. // Set Vector
  41. SIMD_FORCE_INLINE btQuaternion(const btSimdFloat4 vec)
  42. {
  43. mVec128 = vec;
  44. }
  45. // Copy constructor
  46. SIMD_FORCE_INLINE btQuaternion(const btQuaternion& rhs)
  47. {
  48. mVec128 = rhs.mVec128;
  49. }
  50. // Assignment Operator
  51. SIMD_FORCE_INLINE btQuaternion&
  52. operator=(const btQuaternion& v)
  53. {
  54. mVec128 = v.mVec128;
  55. return *this;
  56. }
  57. #endif
  58. // template <typename btScalar>
  59. // explicit Quaternion(const btScalar *v) : Tuple4<btScalar>(v) {}
  60. /**@brief Constructor from scalars */
  61. btQuaternion(const btScalar& _x, const btScalar& _y, const btScalar& _z, const btScalar& _w)
  62. : btQuadWord(_x, _y, _z, _w)
  63. {}
  64. /**@brief Axis angle Constructor
  65. * @param axis The axis which the rotation is around
  66. * @param angle The magnitude of the rotation around the angle (Radians) */
  67. btQuaternion(const btVector3& _axis, const btScalar& _angle)
  68. {
  69. setRotation(_axis, _angle);
  70. }
  71. /**@brief Constructor from Euler angles
  72. * @param yaw Angle around Y unless BT_EULER_DEFAULT_ZYX defined then Z
  73. * @param pitch Angle around X unless BT_EULER_DEFAULT_ZYX defined then Y
  74. * @param roll Angle around Z unless BT_EULER_DEFAULT_ZYX defined then X */
  75. btQuaternion(const btScalar& yaw, const btScalar& pitch, const btScalar& roll)
  76. {
  77. #ifndef BT_EULER_DEFAULT_ZYX
  78. setEuler(yaw, pitch, roll);
  79. #else
  80. setEulerZYX(yaw, pitch, roll);
  81. #endif
  82. }
  83. /**@brief Set the rotation using axis angle notation
  84. * @param axis The axis around which to rotate
  85. * @param angle The magnitude of the rotation in Radians */
  86. void setRotation(const btVector3& axis, const btScalar& _angle)
  87. {
  88. btScalar d = axis.length();
  89. btAssert(d != btScalar(0.0));
  90. btScalar s = btSin(_angle * btScalar(0.5)) / d;
  91. setValue(axis.x() * s, axis.y() * s, axis.z() * s,
  92. btCos(_angle * btScalar(0.5)));
  93. }
  94. /**@brief Set the quaternion using Euler angles
  95. * @param yaw Angle around Y
  96. * @param pitch Angle around X
  97. * @param roll Angle around Z */
  98. void setEuler(const btScalar& yaw, const btScalar& pitch, const btScalar& roll)
  99. {
  100. btScalar halfYaw = btScalar(yaw) * btScalar(0.5);
  101. btScalar halfPitch = btScalar(pitch) * btScalar(0.5);
  102. btScalar halfRoll = btScalar(roll) * btScalar(0.5);
  103. btScalar cosYaw = btCos(halfYaw);
  104. btScalar sinYaw = btSin(halfYaw);
  105. btScalar cosPitch = btCos(halfPitch);
  106. btScalar sinPitch = btSin(halfPitch);
  107. btScalar cosRoll = btCos(halfRoll);
  108. btScalar sinRoll = btSin(halfRoll);
  109. setValue(cosRoll * sinPitch * cosYaw + sinRoll * cosPitch * sinYaw,
  110. cosRoll * cosPitch * sinYaw - sinRoll * sinPitch * cosYaw,
  111. sinRoll * cosPitch * cosYaw - cosRoll * sinPitch * sinYaw,
  112. cosRoll * cosPitch * cosYaw + sinRoll * sinPitch * sinYaw);
  113. }
  114. /**@brief Set the quaternion using euler angles
  115. * @param yaw Angle around Z
  116. * @param pitch Angle around Y
  117. * @param roll Angle around X */
  118. void setEulerZYX(const btScalar& yawZ, const btScalar& pitchY, const btScalar& rollX)
  119. {
  120. btScalar halfYaw = btScalar(yawZ) * btScalar(0.5);
  121. btScalar halfPitch = btScalar(pitchY) * btScalar(0.5);
  122. btScalar halfRoll = btScalar(rollX) * btScalar(0.5);
  123. btScalar cosYaw = btCos(halfYaw);
  124. btScalar sinYaw = btSin(halfYaw);
  125. btScalar cosPitch = btCos(halfPitch);
  126. btScalar sinPitch = btSin(halfPitch);
  127. btScalar cosRoll = btCos(halfRoll);
  128. btScalar sinRoll = btSin(halfRoll);
  129. setValue(sinRoll * cosPitch * cosYaw - cosRoll * sinPitch * sinYaw, //x
  130. cosRoll * sinPitch * cosYaw + sinRoll * cosPitch * sinYaw, //y
  131. cosRoll * cosPitch * sinYaw - sinRoll * sinPitch * cosYaw, //z
  132. cosRoll * cosPitch * cosYaw + sinRoll * sinPitch * sinYaw); //formerly yzx
  133. }
  134. /**@brief Get the euler angles from this quaternion
  135. * @param yaw Angle around Z
  136. * @param pitch Angle around Y
  137. * @param roll Angle around X */
  138. void getEulerZYX(btScalar& yawZ, btScalar& pitchY, btScalar& rollX) const
  139. {
  140. btScalar squ;
  141. btScalar sqx;
  142. btScalar sqy;
  143. btScalar sqz;
  144. btScalar sarg;
  145. sqx = m_floats[0] * m_floats[0];
  146. sqy = m_floats[1] * m_floats[1];
  147. sqz = m_floats[2] * m_floats[2];
  148. squ = m_floats[3] * m_floats[3];
  149. rollX = btAtan2(2 * (m_floats[1] * m_floats[2] + m_floats[3] * m_floats[0]), squ - sqx - sqy + sqz);
  150. sarg = btScalar(-2.) * (m_floats[0] * m_floats[2] - m_floats[3] * m_floats[1]);
  151. pitchY = sarg <= btScalar(-1.0) ? btScalar(-0.5) * SIMD_PI: (sarg >= btScalar(1.0) ? btScalar(0.5) * SIMD_PI : btAsin(sarg));
  152. yawZ = btAtan2(2 * (m_floats[0] * m_floats[1] + m_floats[3] * m_floats[2]), squ + sqx - sqy - sqz);
  153. }
  154. /**@brief Add two quaternions
  155. * @param q The quaternion to add to this one */
  156. SIMD_FORCE_INLINE btQuaternion& operator+=(const btQuaternion& q)
  157. {
  158. #if defined (BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  159. mVec128 = _mm_add_ps(mVec128, q.mVec128);
  160. #elif defined(BT_USE_NEON)
  161. mVec128 = vaddq_f32(mVec128, q.mVec128);
  162. #else
  163. m_floats[0] += q.x();
  164. m_floats[1] += q.y();
  165. m_floats[2] += q.z();
  166. m_floats[3] += q.m_floats[3];
  167. #endif
  168. return *this;
  169. }
  170. /**@brief Subtract out a quaternion
  171. * @param q The quaternion to subtract from this one */
  172. btQuaternion& operator-=(const btQuaternion& q)
  173. {
  174. #if defined (BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  175. mVec128 = _mm_sub_ps(mVec128, q.mVec128);
  176. #elif defined(BT_USE_NEON)
  177. mVec128 = vsubq_f32(mVec128, q.mVec128);
  178. #else
  179. m_floats[0] -= q.x();
  180. m_floats[1] -= q.y();
  181. m_floats[2] -= q.z();
  182. m_floats[3] -= q.m_floats[3];
  183. #endif
  184. return *this;
  185. }
  186. /**@brief Scale this quaternion
  187. * @param s The scalar to scale by */
  188. btQuaternion& operator*=(const btScalar& s)
  189. {
  190. #if defined (BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  191. __m128 vs = _mm_load_ss(&s); // (S 0 0 0)
  192. vs = bt_pshufd_ps(vs, 0); // (S S S S)
  193. mVec128 = _mm_mul_ps(mVec128, vs);
  194. #elif defined(BT_USE_NEON)
  195. mVec128 = vmulq_n_f32(mVec128, s);
  196. #else
  197. m_floats[0] *= s;
  198. m_floats[1] *= s;
  199. m_floats[2] *= s;
  200. m_floats[3] *= s;
  201. #endif
  202. return *this;
  203. }
  204. /**@brief Multiply this quaternion by q on the right
  205. * @param q The other quaternion
  206. * Equivilant to this = this * q */
  207. btQuaternion& operator*=(const btQuaternion& q)
  208. {
  209. #if defined (BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  210. __m128 vQ2 = q.get128();
  211. __m128 A1 = bt_pshufd_ps(mVec128, BT_SHUFFLE(0,1,2,0));
  212. __m128 B1 = bt_pshufd_ps(vQ2, BT_SHUFFLE(3,3,3,0));
  213. A1 = A1 * B1;
  214. __m128 A2 = bt_pshufd_ps(mVec128, BT_SHUFFLE(1,2,0,1));
  215. __m128 B2 = bt_pshufd_ps(vQ2, BT_SHUFFLE(2,0,1,1));
  216. A2 = A2 * B2;
  217. B1 = bt_pshufd_ps(mVec128, BT_SHUFFLE(2,0,1,2));
  218. B2 = bt_pshufd_ps(vQ2, BT_SHUFFLE(1,2,0,2));
  219. B1 = B1 * B2; // A3 *= B3
  220. mVec128 = bt_splat_ps(mVec128, 3); // A0
  221. mVec128 = mVec128 * vQ2; // A0 * B0
  222. A1 = A1 + A2; // AB12
  223. mVec128 = mVec128 - B1; // AB03 = AB0 - AB3
  224. A1 = _mm_xor_ps(A1, vPPPM); // change sign of the last element
  225. mVec128 = mVec128+ A1; // AB03 + AB12
  226. #elif defined(BT_USE_NEON)
  227. float32x4_t vQ1 = mVec128;
  228. float32x4_t vQ2 = q.get128();
  229. float32x4_t A0, A1, B1, A2, B2, A3, B3;
  230. float32x2_t vQ1zx, vQ2wx, vQ1yz, vQ2zx, vQ2yz, vQ2xz;
  231. {
  232. float32x2x2_t tmp;
  233. tmp = vtrn_f32( vget_high_f32(vQ1), vget_low_f32(vQ1) ); // {z x}, {w y}
  234. vQ1zx = tmp.val[0];
  235. tmp = vtrn_f32( vget_high_f32(vQ2), vget_low_f32(vQ2) ); // {z x}, {w y}
  236. vQ2zx = tmp.val[0];
  237. }
  238. vQ2wx = vext_f32(vget_high_f32(vQ2), vget_low_f32(vQ2), 1);
  239. vQ1yz = vext_f32(vget_low_f32(vQ1), vget_high_f32(vQ1), 1);
  240. vQ2yz = vext_f32(vget_low_f32(vQ2), vget_high_f32(vQ2), 1);
  241. vQ2xz = vext_f32(vQ2zx, vQ2zx, 1);
  242. A1 = vcombine_f32(vget_low_f32(vQ1), vQ1zx); // X Y z x
  243. B1 = vcombine_f32(vdup_lane_f32(vget_high_f32(vQ2), 1), vQ2wx); // W W W X
  244. A2 = vcombine_f32(vQ1yz, vget_low_f32(vQ1));
  245. B2 = vcombine_f32(vQ2zx, vdup_lane_f32(vget_low_f32(vQ2), 1));
  246. A3 = vcombine_f32(vQ1zx, vQ1yz); // Z X Y Z
  247. B3 = vcombine_f32(vQ2yz, vQ2xz); // Y Z x z
  248. A1 = vmulq_f32(A1, B1);
  249. A2 = vmulq_f32(A2, B2);
  250. A3 = vmulq_f32(A3, B3); // A3 *= B3
  251. A0 = vmulq_lane_f32(vQ2, vget_high_f32(vQ1), 1); // A0 * B0
  252. A1 = vaddq_f32(A1, A2); // AB12 = AB1 + AB2
  253. A0 = vsubq_f32(A0, A3); // AB03 = AB0 - AB3
  254. // change the sign of the last element
  255. A1 = (btSimdFloat4)veorq_s32((int32x4_t)A1, (int32x4_t)vPPPM);
  256. A0 = vaddq_f32(A0, A1); // AB03 + AB12
  257. mVec128 = A0;
  258. #else
  259. setValue(
  260. m_floats[3] * q.x() + m_floats[0] * q.m_floats[3] + m_floats[1] * q.z() - m_floats[2] * q.y(),
  261. m_floats[3] * q.y() + m_floats[1] * q.m_floats[3] + m_floats[2] * q.x() - m_floats[0] * q.z(),
  262. m_floats[3] * q.z() + m_floats[2] * q.m_floats[3] + m_floats[0] * q.y() - m_floats[1] * q.x(),
  263. m_floats[3] * q.m_floats[3] - m_floats[0] * q.x() - m_floats[1] * q.y() - m_floats[2] * q.z());
  264. #endif
  265. return *this;
  266. }
  267. /**@brief Return the dot product between this quaternion and another
  268. * @param q The other quaternion */
  269. btScalar dot(const btQuaternion& q) const
  270. {
  271. #if defined BT_USE_SIMD_VECTOR3 && defined (BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  272. __m128 vd;
  273. vd = _mm_mul_ps(mVec128, q.mVec128);
  274. __m128 t = _mm_movehl_ps(vd, vd);
  275. vd = _mm_add_ps(vd, t);
  276. t = _mm_shuffle_ps(vd, vd, 0x55);
  277. vd = _mm_add_ss(vd, t);
  278. return _mm_cvtss_f32(vd);
  279. #elif defined(BT_USE_NEON)
  280. float32x4_t vd = vmulq_f32(mVec128, q.mVec128);
  281. float32x2_t x = vpadd_f32(vget_low_f32(vd), vget_high_f32(vd));
  282. x = vpadd_f32(x, x);
  283. return vget_lane_f32(x, 0);
  284. #else
  285. return m_floats[0] * q.x() +
  286. m_floats[1] * q.y() +
  287. m_floats[2] * q.z() +
  288. m_floats[3] * q.m_floats[3];
  289. #endif
  290. }
  291. /**@brief Return the length squared of the quaternion */
  292. btScalar length2() const
  293. {
  294. return dot(*this);
  295. }
  296. /**@brief Return the length of the quaternion */
  297. btScalar length() const
  298. {
  299. return btSqrt(length2());
  300. }
  301. btQuaternion& safeNormalize()
  302. {
  303. btScalar l2 = length2();
  304. if (l2>SIMD_EPSILON)
  305. {
  306. normalize();
  307. }
  308. return *this;
  309. }
  310. /**@brief Normalize the quaternion
  311. * Such that x^2 + y^2 + z^2 +w^2 = 1 */
  312. btQuaternion& normalize()
  313. {
  314. #if defined (BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  315. __m128 vd;
  316. vd = _mm_mul_ps(mVec128, mVec128);
  317. __m128 t = _mm_movehl_ps(vd, vd);
  318. vd = _mm_add_ps(vd, t);
  319. t = _mm_shuffle_ps(vd, vd, 0x55);
  320. vd = _mm_add_ss(vd, t);
  321. vd = _mm_sqrt_ss(vd);
  322. vd = _mm_div_ss(vOnes, vd);
  323. vd = bt_pshufd_ps(vd, 0); // splat
  324. mVec128 = _mm_mul_ps(mVec128, vd);
  325. return *this;
  326. #else
  327. return *this /= length();
  328. #endif
  329. }
  330. /**@brief Return a scaled version of this quaternion
  331. * @param s The scale factor */
  332. SIMD_FORCE_INLINE btQuaternion
  333. operator*(const btScalar& s) const
  334. {
  335. #if defined (BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  336. __m128 vs = _mm_load_ss(&s); // (S 0 0 0)
  337. vs = bt_pshufd_ps(vs, 0x00); // (S S S S)
  338. return btQuaternion(_mm_mul_ps(mVec128, vs));
  339. #elif defined(BT_USE_NEON)
  340. return btQuaternion(vmulq_n_f32(mVec128, s));
  341. #else
  342. return btQuaternion(x() * s, y() * s, z() * s, m_floats[3] * s);
  343. #endif
  344. }
  345. /**@brief Return an inversely scaled versionof this quaternion
  346. * @param s The inverse scale factor */
  347. btQuaternion operator/(const btScalar& s) const
  348. {
  349. btAssert(s != btScalar(0.0));
  350. return *this * (btScalar(1.0) / s);
  351. }
  352. /**@brief Inversely scale this quaternion
  353. * @param s The scale factor */
  354. btQuaternion& operator/=(const btScalar& s)
  355. {
  356. btAssert(s != btScalar(0.0));
  357. return *this *= btScalar(1.0) / s;
  358. }
  359. /**@brief Return a normalized version of this quaternion */
  360. btQuaternion normalized() const
  361. {
  362. return *this / length();
  363. }
  364. /**@brief Return the ***half*** angle between this quaternion and the other
  365. * @param q The other quaternion */
  366. btScalar angle(const btQuaternion& q) const
  367. {
  368. btScalar s = btSqrt(length2() * q.length2());
  369. btAssert(s != btScalar(0.0));
  370. return btAcos(dot(q) / s);
  371. }
  372. /**@brief Return the angle between this quaternion and the other along the shortest path
  373. * @param q The other quaternion */
  374. btScalar angleShortestPath(const btQuaternion& q) const
  375. {
  376. btScalar s = btSqrt(length2() * q.length2());
  377. btAssert(s != btScalar(0.0));
  378. if (dot(q) < 0) // Take care of long angle case see http://en.wikipedia.org/wiki/Slerp
  379. return btAcos(dot(-q) / s) * btScalar(2.0);
  380. else
  381. return btAcos(dot(q) / s) * btScalar(2.0);
  382. }
  383. /**@brief Return the angle [0, 2Pi] of rotation represented by this quaternion */
  384. btScalar getAngle() const
  385. {
  386. btScalar s = btScalar(2.) * btAcos(m_floats[3]);
  387. return s;
  388. }
  389. /**@brief Return the angle [0, Pi] of rotation represented by this quaternion along the shortest path */
  390. btScalar getAngleShortestPath() const
  391. {
  392. btScalar s;
  393. if (m_floats[3] >= 0)
  394. s = btScalar(2.) * btAcos(m_floats[3]);
  395. else
  396. s = btScalar(2.) * btAcos(-m_floats[3]);
  397. return s;
  398. }
  399. /**@brief Return the axis of the rotation represented by this quaternion */
  400. btVector3 getAxis() const
  401. {
  402. btScalar s_squared = 1.f-m_floats[3]*m_floats[3];
  403. if (s_squared < btScalar(10.) * SIMD_EPSILON) //Check for divide by zero
  404. return btVector3(1.0, 0.0, 0.0); // Arbitrary
  405. btScalar s = 1.f/btSqrt(s_squared);
  406. return btVector3(m_floats[0] * s, m_floats[1] * s, m_floats[2] * s);
  407. }
  408. /**@brief Return the inverse of this quaternion */
  409. btQuaternion inverse() const
  410. {
  411. #if defined (BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  412. return btQuaternion(_mm_xor_ps(mVec128, vQInv));
  413. #elif defined(BT_USE_NEON)
  414. return btQuaternion((btSimdFloat4)veorq_s32((int32x4_t)mVec128, (int32x4_t)vQInv));
  415. #else
  416. return btQuaternion(-m_floats[0], -m_floats[1], -m_floats[2], m_floats[3]);
  417. #endif
  418. }
  419. /**@brief Return the sum of this quaternion and the other
  420. * @param q2 The other quaternion */
  421. SIMD_FORCE_INLINE btQuaternion
  422. operator+(const btQuaternion& q2) const
  423. {
  424. #if defined (BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  425. return btQuaternion(_mm_add_ps(mVec128, q2.mVec128));
  426. #elif defined(BT_USE_NEON)
  427. return btQuaternion(vaddq_f32(mVec128, q2.mVec128));
  428. #else
  429. const btQuaternion& q1 = *this;
  430. return btQuaternion(q1.x() + q2.x(), q1.y() + q2.y(), q1.z() + q2.z(), q1.m_floats[3] + q2.m_floats[3]);
  431. #endif
  432. }
  433. /**@brief Return the difference between this quaternion and the other
  434. * @param q2 The other quaternion */
  435. SIMD_FORCE_INLINE btQuaternion
  436. operator-(const btQuaternion& q2) const
  437. {
  438. #if defined (BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  439. return btQuaternion(_mm_sub_ps(mVec128, q2.mVec128));
  440. #elif defined(BT_USE_NEON)
  441. return btQuaternion(vsubq_f32(mVec128, q2.mVec128));
  442. #else
  443. const btQuaternion& q1 = *this;
  444. return btQuaternion(q1.x() - q2.x(), q1.y() - q2.y(), q1.z() - q2.z(), q1.m_floats[3] - q2.m_floats[3]);
  445. #endif
  446. }
  447. /**@brief Return the negative of this quaternion
  448. * This simply negates each element */
  449. SIMD_FORCE_INLINE btQuaternion operator-() const
  450. {
  451. #if defined (BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  452. return btQuaternion(_mm_xor_ps(mVec128, btvMzeroMask));
  453. #elif defined(BT_USE_NEON)
  454. return btQuaternion((btSimdFloat4)veorq_s32((int32x4_t)mVec128, (int32x4_t)btvMzeroMask) );
  455. #else
  456. const btQuaternion& q2 = *this;
  457. return btQuaternion( - q2.x(), - q2.y(), - q2.z(), - q2.m_floats[3]);
  458. #endif
  459. }
  460. /**@todo document this and it's use */
  461. SIMD_FORCE_INLINE btQuaternion farthest( const btQuaternion& qd) const
  462. {
  463. btQuaternion diff,sum;
  464. diff = *this - qd;
  465. sum = *this + qd;
  466. if( diff.dot(diff) > sum.dot(sum) )
  467. return qd;
  468. return (-qd);
  469. }
  470. /**@todo document this and it's use */
  471. SIMD_FORCE_INLINE btQuaternion nearest( const btQuaternion& qd) const
  472. {
  473. btQuaternion diff,sum;
  474. diff = *this - qd;
  475. sum = *this + qd;
  476. if( diff.dot(diff) < sum.dot(sum) )
  477. return qd;
  478. return (-qd);
  479. }
  480. /**@brief Return the quaternion which is the result of Spherical Linear Interpolation between this and the other quaternion
  481. * @param q The other quaternion to interpolate with
  482. * @param t The ratio between this and q to interpolate. If t = 0 the result is this, if t=1 the result is q.
  483. * Slerp interpolates assuming constant velocity. */
  484. btQuaternion slerp(const btQuaternion& q, const btScalar& t) const
  485. {
  486. const btScalar magnitude = btSqrt(length2() * q.length2());
  487. btAssert(magnitude > btScalar(0));
  488. const btScalar product = dot(q) / magnitude;
  489. const btScalar absproduct = btFabs(product);
  490. if(absproduct < btScalar(1.0 - SIMD_EPSILON))
  491. {
  492. // Take care of long angle case see http://en.wikipedia.org/wiki/Slerp
  493. const btScalar theta = btAcos(absproduct);
  494. const btScalar d = btSin(theta);
  495. btAssert(d > btScalar(0));
  496. const btScalar sign = (product < 0) ? btScalar(-1) : btScalar(1);
  497. const btScalar s0 = btSin((btScalar(1.0) - t) * theta) / d;
  498. const btScalar s1 = btSin(sign * t * theta) / d;
  499. return btQuaternion(
  500. (m_floats[0] * s0 + q.x() * s1),
  501. (m_floats[1] * s0 + q.y() * s1),
  502. (m_floats[2] * s0 + q.z() * s1),
  503. (m_floats[3] * s0 + q.w() * s1));
  504. }
  505. else
  506. {
  507. return *this;
  508. }
  509. }
  510. static const btQuaternion& getIdentity()
  511. {
  512. static const btQuaternion identityQuat(btScalar(0.),btScalar(0.),btScalar(0.),btScalar(1.));
  513. return identityQuat;
  514. }
  515. SIMD_FORCE_INLINE const btScalar& getW() const { return m_floats[3]; }
  516. SIMD_FORCE_INLINE void serialize(struct btQuaternionData& dataOut) const;
  517. SIMD_FORCE_INLINE void deSerialize(const struct btQuaternionData& dataIn);
  518. SIMD_FORCE_INLINE void serializeFloat(struct btQuaternionFloatData& dataOut) const;
  519. SIMD_FORCE_INLINE void deSerializeFloat(const struct btQuaternionFloatData& dataIn);
  520. SIMD_FORCE_INLINE void serializeDouble(struct btQuaternionDoubleData& dataOut) const;
  521. SIMD_FORCE_INLINE void deSerializeDouble(const struct btQuaternionDoubleData& dataIn);
  522. };
  523. /**@brief Return the product of two quaternions */
  524. SIMD_FORCE_INLINE btQuaternion
  525. operator*(const btQuaternion& q1, const btQuaternion& q2)
  526. {
  527. #if defined (BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  528. __m128 vQ1 = q1.get128();
  529. __m128 vQ2 = q2.get128();
  530. __m128 A0, A1, B1, A2, B2;
  531. A1 = bt_pshufd_ps(vQ1, BT_SHUFFLE(0,1,2,0)); // X Y z x // vtrn
  532. B1 = bt_pshufd_ps(vQ2, BT_SHUFFLE(3,3,3,0)); // W W W X // vdup vext
  533. A1 = A1 * B1;
  534. A2 = bt_pshufd_ps(vQ1, BT_SHUFFLE(1,2,0,1)); // Y Z X Y // vext
  535. B2 = bt_pshufd_ps(vQ2, BT_SHUFFLE(2,0,1,1)); // z x Y Y // vtrn vdup
  536. A2 = A2 * B2;
  537. B1 = bt_pshufd_ps(vQ1, BT_SHUFFLE(2,0,1,2)); // z x Y Z // vtrn vext
  538. B2 = bt_pshufd_ps(vQ2, BT_SHUFFLE(1,2,0,2)); // Y Z x z // vext vtrn
  539. B1 = B1 * B2; // A3 *= B3
  540. A0 = bt_splat_ps(vQ1, 3); // A0
  541. A0 = A0 * vQ2; // A0 * B0
  542. A1 = A1 + A2; // AB12
  543. A0 = A0 - B1; // AB03 = AB0 - AB3
  544. A1 = _mm_xor_ps(A1, vPPPM); // change sign of the last element
  545. A0 = A0 + A1; // AB03 + AB12
  546. return btQuaternion(A0);
  547. #elif defined(BT_USE_NEON)
  548. float32x4_t vQ1 = q1.get128();
  549. float32x4_t vQ2 = q2.get128();
  550. float32x4_t A0, A1, B1, A2, B2, A3, B3;
  551. float32x2_t vQ1zx, vQ2wx, vQ1yz, vQ2zx, vQ2yz, vQ2xz;
  552. {
  553. float32x2x2_t tmp;
  554. tmp = vtrn_f32( vget_high_f32(vQ1), vget_low_f32(vQ1) ); // {z x}, {w y}
  555. vQ1zx = tmp.val[0];
  556. tmp = vtrn_f32( vget_high_f32(vQ2), vget_low_f32(vQ2) ); // {z x}, {w y}
  557. vQ2zx = tmp.val[0];
  558. }
  559. vQ2wx = vext_f32(vget_high_f32(vQ2), vget_low_f32(vQ2), 1);
  560. vQ1yz = vext_f32(vget_low_f32(vQ1), vget_high_f32(vQ1), 1);
  561. vQ2yz = vext_f32(vget_low_f32(vQ2), vget_high_f32(vQ2), 1);
  562. vQ2xz = vext_f32(vQ2zx, vQ2zx, 1);
  563. A1 = vcombine_f32(vget_low_f32(vQ1), vQ1zx); // X Y z x
  564. B1 = vcombine_f32(vdup_lane_f32(vget_high_f32(vQ2), 1), vQ2wx); // W W W X
  565. A2 = vcombine_f32(vQ1yz, vget_low_f32(vQ1));
  566. B2 = vcombine_f32(vQ2zx, vdup_lane_f32(vget_low_f32(vQ2), 1));
  567. A3 = vcombine_f32(vQ1zx, vQ1yz); // Z X Y Z
  568. B3 = vcombine_f32(vQ2yz, vQ2xz); // Y Z x z
  569. A1 = vmulq_f32(A1, B1);
  570. A2 = vmulq_f32(A2, B2);
  571. A3 = vmulq_f32(A3, B3); // A3 *= B3
  572. A0 = vmulq_lane_f32(vQ2, vget_high_f32(vQ1), 1); // A0 * B0
  573. A1 = vaddq_f32(A1, A2); // AB12 = AB1 + AB2
  574. A0 = vsubq_f32(A0, A3); // AB03 = AB0 - AB3
  575. // change the sign of the last element
  576. A1 = (btSimdFloat4)veorq_s32((int32x4_t)A1, (int32x4_t)vPPPM);
  577. A0 = vaddq_f32(A0, A1); // AB03 + AB12
  578. return btQuaternion(A0);
  579. #else
  580. return btQuaternion(
  581. q1.w() * q2.x() + q1.x() * q2.w() + q1.y() * q2.z() - q1.z() * q2.y(),
  582. q1.w() * q2.y() + q1.y() * q2.w() + q1.z() * q2.x() - q1.x() * q2.z(),
  583. q1.w() * q2.z() + q1.z() * q2.w() + q1.x() * q2.y() - q1.y() * q2.x(),
  584. q1.w() * q2.w() - q1.x() * q2.x() - q1.y() * q2.y() - q1.z() * q2.z());
  585. #endif
  586. }
  587. SIMD_FORCE_INLINE btQuaternion
  588. operator*(const btQuaternion& q, const btVector3& w)
  589. {
  590. #if defined (BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  591. __m128 vQ1 = q.get128();
  592. __m128 vQ2 = w.get128();
  593. __m128 A1, B1, A2, B2, A3, B3;
  594. A1 = bt_pshufd_ps(vQ1, BT_SHUFFLE(3,3,3,0));
  595. B1 = bt_pshufd_ps(vQ2, BT_SHUFFLE(0,1,2,0));
  596. A1 = A1 * B1;
  597. A2 = bt_pshufd_ps(vQ1, BT_SHUFFLE(1,2,0,1));
  598. B2 = bt_pshufd_ps(vQ2, BT_SHUFFLE(2,0,1,1));
  599. A2 = A2 * B2;
  600. A3 = bt_pshufd_ps(vQ1, BT_SHUFFLE(2,0,1,2));
  601. B3 = bt_pshufd_ps(vQ2, BT_SHUFFLE(1,2,0,2));
  602. A3 = A3 * B3; // A3 *= B3
  603. A1 = A1 + A2; // AB12
  604. A1 = _mm_xor_ps(A1, vPPPM); // change sign of the last element
  605. A1 = A1 - A3; // AB123 = AB12 - AB3
  606. return btQuaternion(A1);
  607. #elif defined(BT_USE_NEON)
  608. float32x4_t vQ1 = q.get128();
  609. float32x4_t vQ2 = w.get128();
  610. float32x4_t A1, B1, A2, B2, A3, B3;
  611. float32x2_t vQ1wx, vQ2zx, vQ1yz, vQ2yz, vQ1zx, vQ2xz;
  612. vQ1wx = vext_f32(vget_high_f32(vQ1), vget_low_f32(vQ1), 1);
  613. {
  614. float32x2x2_t tmp;
  615. tmp = vtrn_f32( vget_high_f32(vQ2), vget_low_f32(vQ2) ); // {z x}, {w y}
  616. vQ2zx = tmp.val[0];
  617. tmp = vtrn_f32( vget_high_f32(vQ1), vget_low_f32(vQ1) ); // {z x}, {w y}
  618. vQ1zx = tmp.val[0];
  619. }
  620. vQ1yz = vext_f32(vget_low_f32(vQ1), vget_high_f32(vQ1), 1);
  621. vQ2yz = vext_f32(vget_low_f32(vQ2), vget_high_f32(vQ2), 1);
  622. vQ2xz = vext_f32(vQ2zx, vQ2zx, 1);
  623. A1 = vcombine_f32(vdup_lane_f32(vget_high_f32(vQ1), 1), vQ1wx); // W W W X
  624. B1 = vcombine_f32(vget_low_f32(vQ2), vQ2zx); // X Y z x
  625. A2 = vcombine_f32(vQ1yz, vget_low_f32(vQ1));
  626. B2 = vcombine_f32(vQ2zx, vdup_lane_f32(vget_low_f32(vQ2), 1));
  627. A3 = vcombine_f32(vQ1zx, vQ1yz); // Z X Y Z
  628. B3 = vcombine_f32(vQ2yz, vQ2xz); // Y Z x z
  629. A1 = vmulq_f32(A1, B1);
  630. A2 = vmulq_f32(A2, B2);
  631. A3 = vmulq_f32(A3, B3); // A3 *= B3
  632. A1 = vaddq_f32(A1, A2); // AB12 = AB1 + AB2
  633. // change the sign of the last element
  634. A1 = (btSimdFloat4)veorq_s32((int32x4_t)A1, (int32x4_t)vPPPM);
  635. A1 = vsubq_f32(A1, A3); // AB123 = AB12 - AB3
  636. return btQuaternion(A1);
  637. #else
  638. return btQuaternion(
  639. q.w() * w.x() + q.y() * w.z() - q.z() * w.y(),
  640. q.w() * w.y() + q.z() * w.x() - q.x() * w.z(),
  641. q.w() * w.z() + q.x() * w.y() - q.y() * w.x(),
  642. -q.x() * w.x() - q.y() * w.y() - q.z() * w.z());
  643. #endif
  644. }
  645. SIMD_FORCE_INLINE btQuaternion
  646. operator*(const btVector3& w, const btQuaternion& q)
  647. {
  648. #if defined (BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  649. __m128 vQ1 = w.get128();
  650. __m128 vQ2 = q.get128();
  651. __m128 A1, B1, A2, B2, A3, B3;
  652. A1 = bt_pshufd_ps(vQ1, BT_SHUFFLE(0,1,2,0)); // X Y z x
  653. B1 = bt_pshufd_ps(vQ2, BT_SHUFFLE(3,3,3,0)); // W W W X
  654. A1 = A1 * B1;
  655. A2 = bt_pshufd_ps(vQ1, BT_SHUFFLE(1,2,0,1));
  656. B2 = bt_pshufd_ps(vQ2, BT_SHUFFLE(2,0,1,1));
  657. A2 = A2 *B2;
  658. A3 = bt_pshufd_ps(vQ1, BT_SHUFFLE(2,0,1,2));
  659. B3 = bt_pshufd_ps(vQ2, BT_SHUFFLE(1,2,0,2));
  660. A3 = A3 * B3; // A3 *= B3
  661. A1 = A1 + A2; // AB12
  662. A1 = _mm_xor_ps(A1, vPPPM); // change sign of the last element
  663. A1 = A1 - A3; // AB123 = AB12 - AB3
  664. return btQuaternion(A1);
  665. #elif defined(BT_USE_NEON)
  666. float32x4_t vQ1 = w.get128();
  667. float32x4_t vQ2 = q.get128();
  668. float32x4_t A1, B1, A2, B2, A3, B3;
  669. float32x2_t vQ1zx, vQ2wx, vQ1yz, vQ2zx, vQ2yz, vQ2xz;
  670. {
  671. float32x2x2_t tmp;
  672. tmp = vtrn_f32( vget_high_f32(vQ1), vget_low_f32(vQ1) ); // {z x}, {w y}
  673. vQ1zx = tmp.val[0];
  674. tmp = vtrn_f32( vget_high_f32(vQ2), vget_low_f32(vQ2) ); // {z x}, {w y}
  675. vQ2zx = tmp.val[0];
  676. }
  677. vQ2wx = vext_f32(vget_high_f32(vQ2), vget_low_f32(vQ2), 1);
  678. vQ1yz = vext_f32(vget_low_f32(vQ1), vget_high_f32(vQ1), 1);
  679. vQ2yz = vext_f32(vget_low_f32(vQ2), vget_high_f32(vQ2), 1);
  680. vQ2xz = vext_f32(vQ2zx, vQ2zx, 1);
  681. A1 = vcombine_f32(vget_low_f32(vQ1), vQ1zx); // X Y z x
  682. B1 = vcombine_f32(vdup_lane_f32(vget_high_f32(vQ2), 1), vQ2wx); // W W W X
  683. A2 = vcombine_f32(vQ1yz, vget_low_f32(vQ1));
  684. B2 = vcombine_f32(vQ2zx, vdup_lane_f32(vget_low_f32(vQ2), 1));
  685. A3 = vcombine_f32(vQ1zx, vQ1yz); // Z X Y Z
  686. B3 = vcombine_f32(vQ2yz, vQ2xz); // Y Z x z
  687. A1 = vmulq_f32(A1, B1);
  688. A2 = vmulq_f32(A2, B2);
  689. A3 = vmulq_f32(A3, B3); // A3 *= B3
  690. A1 = vaddq_f32(A1, A2); // AB12 = AB1 + AB2
  691. // change the sign of the last element
  692. A1 = (btSimdFloat4)veorq_s32((int32x4_t)A1, (int32x4_t)vPPPM);
  693. A1 = vsubq_f32(A1, A3); // AB123 = AB12 - AB3
  694. return btQuaternion(A1);
  695. #else
  696. return btQuaternion(
  697. +w.x() * q.w() + w.y() * q.z() - w.z() * q.y(),
  698. +w.y() * q.w() + w.z() * q.x() - w.x() * q.z(),
  699. +w.z() * q.w() + w.x() * q.y() - w.y() * q.x(),
  700. -w.x() * q.x() - w.y() * q.y() - w.z() * q.z());
  701. #endif
  702. }
  703. /**@brief Calculate the dot product between two quaternions */
  704. SIMD_FORCE_INLINE btScalar
  705. dot(const btQuaternion& q1, const btQuaternion& q2)
  706. {
  707. return q1.dot(q2);
  708. }
  709. /**@brief Return the length of a quaternion */
  710. SIMD_FORCE_INLINE btScalar
  711. length(const btQuaternion& q)
  712. {
  713. return q.length();
  714. }
  715. /**@brief Return the angle between two quaternions*/
  716. SIMD_FORCE_INLINE btScalar
  717. btAngle(const btQuaternion& q1, const btQuaternion& q2)
  718. {
  719. return q1.angle(q2);
  720. }
  721. /**@brief Return the inverse of a quaternion*/
  722. SIMD_FORCE_INLINE btQuaternion
  723. inverse(const btQuaternion& q)
  724. {
  725. return q.inverse();
  726. }
  727. /**@brief Return the result of spherical linear interpolation betwen two quaternions
  728. * @param q1 The first quaternion
  729. * @param q2 The second quaternion
  730. * @param t The ration between q1 and q2. t = 0 return q1, t=1 returns q2
  731. * Slerp assumes constant velocity between positions. */
  732. SIMD_FORCE_INLINE btQuaternion
  733. slerp(const btQuaternion& q1, const btQuaternion& q2, const btScalar& t)
  734. {
  735. return q1.slerp(q2, t);
  736. }
  737. SIMD_FORCE_INLINE btVector3
  738. quatRotate(const btQuaternion& rotation, const btVector3& v)
  739. {
  740. btQuaternion q = rotation * v;
  741. q *= rotation.inverse();
  742. #if defined BT_USE_SIMD_VECTOR3 && defined (BT_USE_SSE_IN_API) && defined (BT_USE_SSE)
  743. return btVector3(_mm_and_ps(q.get128(), btvFFF0fMask));
  744. #elif defined(BT_USE_NEON)
  745. return btVector3((float32x4_t)vandq_s32((int32x4_t)q.get128(), btvFFF0Mask));
  746. #else
  747. return btVector3(q.getX(),q.getY(),q.getZ());
  748. #endif
  749. }
  750. SIMD_FORCE_INLINE btQuaternion
  751. shortestArcQuat(const btVector3& v0, const btVector3& v1) // Game Programming Gems 2.10. make sure v0,v1 are normalized
  752. {
  753. btVector3 c = v0.cross(v1);
  754. btScalar d = v0.dot(v1);
  755. if (d < -1.0 + SIMD_EPSILON)
  756. {
  757. btVector3 n,unused;
  758. btPlaneSpace1(v0,n,unused);
  759. return btQuaternion(n.x(),n.y(),n.z(),0.0f); // just pick any vector that is orthogonal to v0
  760. }
  761. btScalar s = btSqrt((1.0f + d) * 2.0f);
  762. btScalar rs = 1.0f / s;
  763. return btQuaternion(c.getX()*rs,c.getY()*rs,c.getZ()*rs,s * 0.5f);
  764. }
  765. SIMD_FORCE_INLINE btQuaternion
  766. shortestArcQuatNormalize2(btVector3& v0,btVector3& v1)
  767. {
  768. v0.normalize();
  769. v1.normalize();
  770. return shortestArcQuat(v0,v1);
  771. }
  772. struct btQuaternionFloatData
  773. {
  774. float m_floats[4];
  775. };
  776. struct btQuaternionDoubleData
  777. {
  778. double m_floats[4];
  779. };
  780. SIMD_FORCE_INLINE void btQuaternion::serializeFloat(struct btQuaternionFloatData& dataOut) const
  781. {
  782. ///could also do a memcpy, check if it is worth it
  783. for (int i=0;i<4;i++)
  784. dataOut.m_floats[i] = float(m_floats[i]);
  785. }
  786. SIMD_FORCE_INLINE void btQuaternion::deSerializeFloat(const struct btQuaternionFloatData& dataIn)
  787. {
  788. for (int i=0;i<4;i++)
  789. m_floats[i] = btScalar(dataIn.m_floats[i]);
  790. }
  791. SIMD_FORCE_INLINE void btQuaternion::serializeDouble(struct btQuaternionDoubleData& dataOut) const
  792. {
  793. ///could also do a memcpy, check if it is worth it
  794. for (int i=0;i<4;i++)
  795. dataOut.m_floats[i] = double(m_floats[i]);
  796. }
  797. SIMD_FORCE_INLINE void btQuaternion::deSerializeDouble(const struct btQuaternionDoubleData& dataIn)
  798. {
  799. for (int i=0;i<4;i++)
  800. m_floats[i] = btScalar(dataIn.m_floats[i]);
  801. }
  802. SIMD_FORCE_INLINE void btQuaternion::serialize(struct btQuaternionData& dataOut) const
  803. {
  804. ///could also do a memcpy, check if it is worth it
  805. for (int i=0;i<4;i++)
  806. dataOut.m_floats[i] = m_floats[i];
  807. }
  808. SIMD_FORCE_INLINE void btQuaternion::deSerialize(const struct btQuaternionData& dataIn)
  809. {
  810. for (int i=0;i<4;i++)
  811. m_floats[i] = dataIn.m_floats[i];
  812. }
  813. #endif //BT_SIMD__QUATERNION_H_