2
0

b3Quaternion.h 28 KB

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