b3Quaternion.h 27 KB

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