Quaternion.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Math/Matrix3.h"
  5. #ifdef URHO3D_SSE
  6. #include <emmintrin.h>
  7. #endif
  8. namespace Urho3D
  9. {
  10. /// Rotation represented as a four-dimensional normalized vector.
  11. class URHO3D_API Quaternion
  12. {
  13. public:
  14. #ifdef _MSC_VER
  15. #pragma warning(push)
  16. #pragma warning(disable:26495)
  17. #endif
  18. /// Construct an identity quaternion.
  19. Quaternion() noexcept
  20. #ifndef URHO3D_SSE
  21. :w_(1.0f),
  22. x_(0.0f),
  23. y_(0.0f),
  24. z_(0.0f)
  25. #endif
  26. {
  27. #ifdef URHO3D_SSE
  28. _mm_storeu_ps(&w_, _mm_set_ps(0.f, 0.f, 0.f, 1.f));
  29. #endif
  30. }
  31. /// Copy-construct from another quaternion.
  32. Quaternion(const Quaternion& quat) noexcept
  33. #if defined(URHO3D_SSE) && (!defined(_MSC_VER) || _MSC_VER >= 1700) /* Visual Studio 2012 and newer. VS2010 has a bug with these, see https://github.com/urho3d/Urho3D/issues/1044 */
  34. {
  35. _mm_storeu_ps(&w_, _mm_loadu_ps(&quat.w_));
  36. }
  37. #else
  38. :w_(quat.w_),
  39. x_(quat.x_),
  40. y_(quat.y_),
  41. z_(quat.z_)
  42. {
  43. }
  44. #endif
  45. /// Construct from values.
  46. Quaternion(float w, float x, float y, float z) noexcept
  47. #ifndef URHO3D_SSE
  48. :w_(w),
  49. x_(x),
  50. y_(y),
  51. z_(z)
  52. #endif
  53. {
  54. #ifdef URHO3D_SSE
  55. _mm_storeu_ps(&w_, _mm_set_ps(z, y, x, w));
  56. #endif
  57. }
  58. /// Construct from a float array.
  59. explicit Quaternion(const float* data) noexcept
  60. #ifndef URHO3D_SSE
  61. :w_(data[0]),
  62. x_(data[1]),
  63. y_(data[2]),
  64. z_(data[3])
  65. #endif
  66. {
  67. #ifdef URHO3D_SSE
  68. _mm_storeu_ps(&w_, _mm_loadu_ps(data));
  69. #endif
  70. }
  71. #ifdef URHO3D_SSE
  72. explicit Quaternion(__m128 wxyz) noexcept
  73. {
  74. _mm_storeu_ps(&w_, wxyz);
  75. }
  76. #endif
  77. #ifdef _MSC_VER
  78. #pragma warning(pop)
  79. #endif
  80. /// Construct from an angle (in degrees) and axis.
  81. Quaternion(float angle, const Vector3& axis) noexcept
  82. {
  83. FromAngleAxis(angle, axis);
  84. }
  85. /// Construct from an angle (in degrees, for Urho2D).
  86. explicit Quaternion(float angle) noexcept
  87. {
  88. FromAngleAxis(angle, Vector3::FORWARD);
  89. }
  90. /// Construct from Euler angles (in degrees). Equivalent to Y*X*Z.
  91. Quaternion(float x, float y, float z) noexcept
  92. {
  93. FromEulerAngles(x, y, z);
  94. }
  95. /// Construct from Euler angles (in degrees).
  96. explicit Quaternion(const Vector3& angles) noexcept
  97. {
  98. FromEulerAngles(angles.x_, angles.y_, angles.z_);
  99. }
  100. /// Construct from the rotation difference between two direction vectors.
  101. Quaternion(const Vector3& start, const Vector3& end) noexcept
  102. {
  103. FromRotationTo(start, end);
  104. }
  105. /// Construct from orthonormal axes.
  106. Quaternion(const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis) noexcept
  107. {
  108. FromAxes(xAxis, yAxis, zAxis);
  109. }
  110. /// Construct from a rotation matrix.
  111. explicit Quaternion(const Matrix3& matrix) noexcept
  112. {
  113. FromRotationMatrix(matrix);
  114. }
  115. /// Assign from another quaternion.
  116. Quaternion& operator =(const Quaternion& rhs) noexcept
  117. {
  118. #if defined(URHO3D_SSE) && (!defined(_MSC_VER) || _MSC_VER >= 1700) /* Visual Studio 2012 and newer. VS2010 has a bug with these, see https://github.com/urho3d/Urho3D/issues/1044 */
  119. _mm_storeu_ps(&w_, _mm_loadu_ps(&rhs.w_));
  120. #else
  121. w_ = rhs.w_;
  122. x_ = rhs.x_;
  123. y_ = rhs.y_;
  124. z_ = rhs.z_;
  125. #endif
  126. return *this;
  127. }
  128. /// Add-assign a quaternion.
  129. Quaternion& operator +=(const Quaternion& rhs)
  130. {
  131. #ifdef URHO3D_SSE
  132. _mm_storeu_ps(&w_, _mm_add_ps(_mm_loadu_ps(&w_), _mm_loadu_ps(&rhs.w_)));
  133. #else
  134. w_ += rhs.w_;
  135. x_ += rhs.x_;
  136. y_ += rhs.y_;
  137. z_ += rhs.z_;
  138. #endif
  139. return *this;
  140. }
  141. /// Multiply-assign a scalar.
  142. Quaternion& operator *=(float rhs)
  143. {
  144. #ifdef URHO3D_SSE
  145. _mm_storeu_ps(&w_, _mm_mul_ps(_mm_loadu_ps(&w_), _mm_set1_ps(rhs)));
  146. #else
  147. w_ *= rhs;
  148. x_ *= rhs;
  149. y_ *= rhs;
  150. z_ *= rhs;
  151. #endif
  152. return *this;
  153. }
  154. /// Test for equality with another quaternion without epsilon.
  155. bool operator ==(const Quaternion& rhs) const
  156. {
  157. #ifdef URHO3D_SSE
  158. __m128 c = _mm_cmpeq_ps(_mm_loadu_ps(&w_), _mm_loadu_ps(&rhs.w_));
  159. c = _mm_and_ps(c, _mm_movehl_ps(c, c));
  160. c = _mm_and_ps(c, _mm_shuffle_ps(c, c, _MM_SHUFFLE(1, 1, 1, 1)));
  161. return _mm_cvtsi128_si32(_mm_castps_si128(c)) == -1;
  162. #else
  163. return w_ == rhs.w_ && x_ == rhs.x_ && y_ == rhs.y_ && z_ == rhs.z_;
  164. #endif
  165. }
  166. /// Test for inequality with another quaternion without epsilon.
  167. bool operator !=(const Quaternion& rhs) const { return !(*this == rhs); }
  168. /// Multiply with a scalar.
  169. Quaternion operator *(float rhs) const
  170. {
  171. #ifdef URHO3D_SSE
  172. return Quaternion(_mm_mul_ps(_mm_loadu_ps(&w_), _mm_set1_ps(rhs)));
  173. #else
  174. return Quaternion(w_ * rhs, x_ * rhs, y_ * rhs, z_ * rhs);
  175. #endif
  176. }
  177. /// Return negation.
  178. Quaternion operator -() const
  179. {
  180. #ifdef URHO3D_SSE
  181. return Quaternion(_mm_xor_ps(_mm_loadu_ps(&w_), _mm_castsi128_ps(_mm_set1_epi32((int)0x80000000UL))));
  182. #else
  183. return Quaternion(-w_, -x_, -y_, -z_);
  184. #endif
  185. }
  186. /// Add a quaternion.
  187. Quaternion operator +(const Quaternion& rhs) const
  188. {
  189. #ifdef URHO3D_SSE
  190. return Quaternion(_mm_add_ps(_mm_loadu_ps(&w_), _mm_loadu_ps(&rhs.w_)));
  191. #else
  192. return Quaternion(w_ + rhs.w_, x_ + rhs.x_, y_ + rhs.y_, z_ + rhs.z_);
  193. #endif
  194. }
  195. /// Subtract a quaternion.
  196. Quaternion operator -(const Quaternion& rhs) const
  197. {
  198. #ifdef URHO3D_SSE
  199. return Quaternion(_mm_sub_ps(_mm_loadu_ps(&w_), _mm_loadu_ps(&rhs.w_)));
  200. #else
  201. return Quaternion(w_ - rhs.w_, x_ - rhs.x_, y_ - rhs.y_, z_ - rhs.z_);
  202. #endif
  203. }
  204. /// Multiply a quaternion.
  205. Quaternion operator *(const Quaternion& rhs) const
  206. {
  207. #ifdef URHO3D_SSE
  208. __m128 q1 = _mm_loadu_ps(&w_);
  209. __m128 q2 = _mm_loadu_ps(&rhs.w_);
  210. q2 = _mm_shuffle_ps(q2, q2, _MM_SHUFFLE(0, 3, 2, 1));
  211. const __m128 signy = _mm_castsi128_ps(_mm_set_epi32((int)0x80000000UL, (int)0x80000000UL, 0, 0));
  212. const __m128 signx = _mm_shuffle_ps(signy, signy, _MM_SHUFFLE(2, 0, 2, 0));
  213. const __m128 signz = _mm_shuffle_ps(signy, signy, _MM_SHUFFLE(3, 0, 0, 3));
  214. __m128 out = _mm_mul_ps(_mm_shuffle_ps(q1, q1, _MM_SHUFFLE(1, 1, 1, 1)), _mm_shuffle_ps(q2, q2, _MM_SHUFFLE(0, 1, 2, 3)));
  215. out = _mm_add_ps(_mm_mul_ps(_mm_xor_ps(signy, _mm_shuffle_ps(q1, q1, _MM_SHUFFLE(2, 2, 2, 2))), _mm_shuffle_ps(q2, q2, _MM_SHUFFLE(1, 0, 3, 2))), _mm_xor_ps(signx, out));
  216. out = _mm_add_ps(_mm_mul_ps(_mm_xor_ps(signz, _mm_shuffle_ps(q1, q1, _MM_SHUFFLE(3, 3, 3, 3))), _mm_shuffle_ps(q2, q2, _MM_SHUFFLE(2, 3, 0, 1))), out);
  217. out = _mm_add_ps(_mm_mul_ps(_mm_shuffle_ps(q1, q1, _MM_SHUFFLE(0, 0, 0, 0)), q2), out);
  218. return Quaternion(_mm_shuffle_ps(out, out, _MM_SHUFFLE(2, 1, 0, 3)));
  219. #else
  220. return Quaternion(
  221. w_ * rhs.w_ - x_ * rhs.x_ - y_ * rhs.y_ - z_ * rhs.z_,
  222. w_ * rhs.x_ + x_ * rhs.w_ + y_ * rhs.z_ - z_ * rhs.y_,
  223. w_ * rhs.y_ + y_ * rhs.w_ + z_ * rhs.x_ - x_ * rhs.z_,
  224. w_ * rhs.z_ + z_ * rhs.w_ + x_ * rhs.y_ - y_ * rhs.x_
  225. );
  226. #endif
  227. }
  228. /// Multiply a Vector3.
  229. Vector3 operator *(const Vector3& rhs) const
  230. {
  231. #ifdef URHO3D_SSE
  232. __m128 q = _mm_loadu_ps(&w_);
  233. q = _mm_shuffle_ps(q, q, _MM_SHUFFLE(0, 3, 2, 1));
  234. __m128 v = _mm_set_ps(0.f, rhs.z_, rhs.y_, rhs.x_);
  235. const __m128 W = _mm_shuffle_ps(q, q, _MM_SHUFFLE(3, 3, 3, 3));
  236. const __m128 a_yzx = _mm_shuffle_ps(q, q, _MM_SHUFFLE(3, 0, 2, 1));
  237. __m128 x = _mm_mul_ps(q, _mm_shuffle_ps(v, v, _MM_SHUFFLE(3, 0, 2, 1)));
  238. __m128 qxv = _mm_sub_ps(x, _mm_mul_ps(a_yzx, v));
  239. __m128 Wv = _mm_mul_ps(W, v);
  240. __m128 s = _mm_add_ps(qxv, _mm_shuffle_ps(Wv, Wv, _MM_SHUFFLE(3, 1, 0, 2)));
  241. __m128 qs = _mm_mul_ps(q, s);
  242. __m128 y = _mm_shuffle_ps(qs, qs, _MM_SHUFFLE(3, 1, 0, 2));
  243. s = _mm_sub_ps(_mm_mul_ps(a_yzx, s), y);
  244. s = _mm_add_ps(s, s);
  245. s = _mm_add_ps(s, v);
  246. return Vector3(
  247. _mm_cvtss_f32(s),
  248. _mm_cvtss_f32(_mm_shuffle_ps(s, s, _MM_SHUFFLE(1, 1, 1, 1))),
  249. _mm_cvtss_f32(_mm_movehl_ps(s, s)));
  250. #else
  251. Vector3 qVec(x_, y_, z_);
  252. Vector3 cross1(qVec.CrossProduct(rhs));
  253. Vector3 cross2(qVec.CrossProduct(cross1));
  254. return rhs + 2.0f * (cross1 * w_ + cross2);
  255. #endif
  256. }
  257. /// Define from an angle (in degrees) and axis.
  258. void FromAngleAxis(float angle, const Vector3& axis);
  259. /// Define from Euler angles (in degrees). Equivalent to Y*X*Z.
  260. void FromEulerAngles(float x, float y, float z);
  261. /// Define from the rotation difference between two direction vectors.
  262. void FromRotationTo(const Vector3& start, const Vector3& end);
  263. /// Define from orthonormal axes.
  264. void FromAxes(const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis);
  265. /// Define from a rotation matrix.
  266. void FromRotationMatrix(const Matrix3& matrix);
  267. /// Define from a direction to look in and an up direction. Return true if successful, or false if would result in a NaN, in which case the current value remains.
  268. bool FromLookRotation(const Vector3& direction, const Vector3& up = Vector3::UP);
  269. /// Normalize to unit length.
  270. void Normalize()
  271. {
  272. #ifdef URHO3D_SSE
  273. __m128 q = _mm_loadu_ps(&w_);
  274. __m128 n = _mm_mul_ps(q, q);
  275. n = _mm_add_ps(n, _mm_shuffle_ps(n, n, _MM_SHUFFLE(2, 3, 0, 1)));
  276. n = _mm_add_ps(n, _mm_shuffle_ps(n, n, _MM_SHUFFLE(0, 1, 2, 3)));
  277. __m128 e = _mm_rsqrt_ps(n);
  278. __m128 e3 = _mm_mul_ps(_mm_mul_ps(e, e), e);
  279. __m128 half = _mm_set1_ps(0.5f);
  280. n = _mm_add_ps(e, _mm_mul_ps(half, _mm_sub_ps(e, _mm_mul_ps(n, e3))));
  281. _mm_storeu_ps(&w_, _mm_mul_ps(q, n));
  282. #else
  283. float lenSquared = LengthSquared();
  284. if (!Urho3D::Equals(lenSquared, 1.0f) && lenSquared > 0.0f)
  285. {
  286. float invLen = 1.0f / sqrtf(lenSquared);
  287. w_ *= invLen;
  288. x_ *= invLen;
  289. y_ *= invLen;
  290. z_ *= invLen;
  291. }
  292. #endif
  293. }
  294. /// Return normalized to unit length.
  295. Quaternion Normalized() const
  296. {
  297. #ifdef URHO3D_SSE
  298. __m128 q = _mm_loadu_ps(&w_);
  299. __m128 n = _mm_mul_ps(q, q);
  300. n = _mm_add_ps(n, _mm_shuffle_ps(n, n, _MM_SHUFFLE(2, 3, 0, 1)));
  301. n = _mm_add_ps(n, _mm_shuffle_ps(n, n, _MM_SHUFFLE(0, 1, 2, 3)));
  302. __m128 e = _mm_rsqrt_ps(n);
  303. __m128 e3 = _mm_mul_ps(_mm_mul_ps(e, e), e);
  304. __m128 half = _mm_set1_ps(0.5f);
  305. n = _mm_add_ps(e, _mm_mul_ps(half, _mm_sub_ps(e, _mm_mul_ps(n, e3))));
  306. return Quaternion(_mm_mul_ps(q, n));
  307. #else
  308. float lenSquared = LengthSquared();
  309. if (!Urho3D::Equals(lenSquared, 1.0f) && lenSquared > 0.0f)
  310. {
  311. float invLen = 1.0f / sqrtf(lenSquared);
  312. return *this * invLen;
  313. }
  314. else
  315. return *this;
  316. #endif
  317. }
  318. /// Return inverse.
  319. Quaternion Inverse() const
  320. {
  321. #ifdef URHO3D_SSE
  322. __m128 q = _mm_loadu_ps(&w_);
  323. __m128 n = _mm_mul_ps(q, q);
  324. n = _mm_add_ps(n, _mm_shuffle_ps(n, n, _MM_SHUFFLE(2, 3, 0, 1)));
  325. n = _mm_add_ps(n, _mm_shuffle_ps(n, n, _MM_SHUFFLE(0, 1, 2, 3)));
  326. return Quaternion(_mm_div_ps(_mm_xor_ps(q, _mm_castsi128_ps(_mm_set_epi32((int)0x80000000UL, (int)0x80000000UL, (int)0x80000000UL, 0))), n));
  327. #else
  328. float lenSquared = LengthSquared();
  329. if (lenSquared == 1.0f)
  330. return Conjugate();
  331. else if (lenSquared >= M_EPSILON)
  332. return Conjugate() * (1.0f / lenSquared);
  333. else
  334. return IDENTITY;
  335. #endif
  336. }
  337. /// Return squared length.
  338. float LengthSquared() const
  339. {
  340. #ifdef URHO3D_SSE
  341. __m128 q = _mm_loadu_ps(&w_);
  342. __m128 n = _mm_mul_ps(q, q);
  343. n = _mm_add_ps(n, _mm_shuffle_ps(n, n, _MM_SHUFFLE(2, 3, 0, 1)));
  344. n = _mm_add_ps(n, _mm_shuffle_ps(n, n, _MM_SHUFFLE(0, 1, 2, 3)));
  345. return _mm_cvtss_f32(n);
  346. #else
  347. return w_ * w_ + x_ * x_ + y_ * y_ + z_ * z_;
  348. #endif
  349. }
  350. /// Calculate dot product.
  351. float DotProduct(const Quaternion& rhs) const
  352. {
  353. #ifdef URHO3D_SSE
  354. __m128 q1 = _mm_loadu_ps(&w_);
  355. __m128 q2 = _mm_loadu_ps(&rhs.w_);
  356. __m128 n = _mm_mul_ps(q1, q2);
  357. n = _mm_add_ps(n, _mm_shuffle_ps(n, n, _MM_SHUFFLE(2, 3, 0, 1)));
  358. n = _mm_add_ps(n, _mm_shuffle_ps(n, n, _MM_SHUFFLE(0, 1, 2, 3)));
  359. return _mm_cvtss_f32(n);
  360. #else
  361. return w_ * rhs.w_ + x_ * rhs.x_ + y_ * rhs.y_ + z_ * rhs.z_;
  362. #endif
  363. }
  364. /// Test for equality with another quaternion with epsilon.
  365. bool Equals(const Quaternion& rhs) const
  366. {
  367. return Urho3D::Equals(w_, rhs.w_) && Urho3D::Equals(x_, rhs.x_) && Urho3D::Equals(y_, rhs.y_) && Urho3D::Equals(z_, rhs.z_);
  368. }
  369. /// Return whether any element is NaN.
  370. bool IsNaN() const { return Urho3D::IsNaN(w_) || Urho3D::IsNaN(x_) || Urho3D::IsNaN(y_) || Urho3D::IsNaN(z_); }
  371. /// Return whether any element is Inf.
  372. bool IsInf() const { return Urho3D::IsInf(w_) || Urho3D::IsInf(x_) || Urho3D::IsInf(y_) || Urho3D::IsInf(z_); }
  373. /// Return conjugate.
  374. Quaternion Conjugate() const
  375. {
  376. #ifdef URHO3D_SSE
  377. __m128 q = _mm_loadu_ps(&w_);
  378. return Quaternion(_mm_xor_ps(q, _mm_castsi128_ps(_mm_set_epi32((int)0x80000000UL, (int)0x80000000UL, (int)0x80000000UL, 0))));
  379. #else
  380. return Quaternion(w_, -x_, -y_, -z_);
  381. #endif
  382. }
  383. /// Return Euler angles in degrees.
  384. /// @property
  385. Vector3 EulerAngles() const;
  386. /// Return yaw angle in degrees.
  387. /// @property{get_yaw}
  388. float YawAngle() const;
  389. /// Return pitch angle in degrees.
  390. /// @property{get_pitch}
  391. float PitchAngle() const;
  392. /// Return roll angle in degrees.
  393. /// @property{get_roll}
  394. float RollAngle() const;
  395. /// Return rotation axis.
  396. /// @property
  397. Vector3 Axis() const;
  398. /// Return rotation angle.
  399. /// @property
  400. float Angle() const;
  401. /// Return the rotation matrix that corresponds to this quaternion.
  402. /// @property
  403. Matrix3 RotationMatrix() const;
  404. /// Spherical interpolation with another quaternion.
  405. Quaternion Slerp(const Quaternion& rhs, float t) const;
  406. /// Normalized linear interpolation with another quaternion.
  407. Quaternion Nlerp(const Quaternion& rhs, float t, bool shortestPath = false) const;
  408. /// Return float data.
  409. const float* Data() const { return &w_; }
  410. /// Return as string.
  411. String ToString() const;
  412. /// W coordinate.
  413. float w_;
  414. /// X coordinate.
  415. float x_;
  416. /// Y coordinate.
  417. float y_;
  418. /// Z coordinate.
  419. float z_;
  420. /// Identity quaternion.
  421. static const Quaternion IDENTITY;
  422. };
  423. }