Quaternion.h 16 KB

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