quaternion.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #pragma once
  5. #include "irrTypes.h"
  6. #include "irrMath.h"
  7. #include "matrix4.h"
  8. #include "vector3d.h"
  9. // NOTE: You *only* need this when updating an application from Irrlicht before 1.8 to Irrlicht 1.8 or later.
  10. // Between Irrlicht 1.7 and Irrlicht 1.8 the quaternion-matrix conversions changed.
  11. // Before the fix they had mixed left- and right-handed rotations.
  12. // To test if your code was affected by the change enable IRR_TEST_BROKEN_QUATERNION_USE and try to compile your application.
  13. // This defines removes those functions so you get compile errors anywhere you use them in your code.
  14. // For every line with a compile-errors you have to change the corresponding lines like that:
  15. // - When you pass the matrix to the quaternion constructor then replace the matrix by the transposed matrix.
  16. // - For uses of getMatrix() you have to use quaternion::getMatrix_transposed instead.
  17. // #define IRR_TEST_BROKEN_QUATERNION_USE
  18. namespace irr
  19. {
  20. namespace core
  21. {
  22. //! Quaternion class for representing rotations.
  23. /** It provides cheap combinations and avoids gimbal locks.
  24. Also useful for interpolations. */
  25. class quaternion
  26. {
  27. public:
  28. //! Default Constructor
  29. constexpr quaternion() :
  30. X(0.0f), Y(0.0f), Z(0.0f), W(1.0f) {}
  31. //! Constructor
  32. constexpr quaternion(f32 x, f32 y, f32 z, f32 w) :
  33. X(x), Y(y), Z(z), W(w) {}
  34. //! Constructor which converts Euler angles (radians) to a quaternion
  35. quaternion(f32 x, f32 y, f32 z);
  36. //! Constructor which converts Euler angles (radians) to a quaternion
  37. quaternion(const vector3df &vec);
  38. #ifndef IRR_TEST_BROKEN_QUATERNION_USE
  39. //! Constructor which converts a matrix to a quaternion
  40. quaternion(const matrix4 &mat);
  41. #endif
  42. //! Equality operator
  43. constexpr bool operator==(const quaternion &other) const
  44. {
  45. return ((X == other.X) &&
  46. (Y == other.Y) &&
  47. (Z == other.Z) &&
  48. (W == other.W));
  49. }
  50. //! inequality operator
  51. constexpr bool operator!=(const quaternion &other) const
  52. {
  53. return !(*this == other);
  54. }
  55. #ifndef IRR_TEST_BROKEN_QUATERNION_USE
  56. //! Matrix assignment operator
  57. inline quaternion &operator=(const matrix4 &other);
  58. #endif
  59. //! Add operator
  60. quaternion operator+(const quaternion &other) const;
  61. //! Multiplication operator
  62. //! Be careful, unfortunately the operator order here is opposite of that in CMatrix4::operator*
  63. quaternion operator*(const quaternion &other) const;
  64. //! Multiplication operator with scalar
  65. quaternion operator*(f32 s) const;
  66. //! Multiplication operator with scalar
  67. quaternion &operator*=(f32 s);
  68. //! Multiplication operator
  69. vector3df operator*(const vector3df &v) const;
  70. //! Multiplication operator
  71. quaternion &operator*=(const quaternion &other);
  72. //! Calculates the dot product
  73. inline f32 dotProduct(const quaternion &other) const;
  74. //! Sets new quaternion
  75. inline quaternion &set(f32 x, f32 y, f32 z, f32 w);
  76. //! Sets new quaternion based on Euler angles (radians)
  77. inline quaternion &set(f32 x, f32 y, f32 z);
  78. //! Sets new quaternion based on Euler angles (radians)
  79. inline quaternion &set(const core::vector3df &vec);
  80. //! Sets new quaternion from other quaternion
  81. inline quaternion &set(const core::quaternion &quat);
  82. //! returns if this quaternion equals the other one, taking floating point rounding errors into account
  83. inline bool equals(const quaternion &other,
  84. const f32 tolerance = ROUNDING_ERROR_f32) const;
  85. //! Normalizes the quaternion
  86. inline quaternion &normalize();
  87. #ifndef IRR_TEST_BROKEN_QUATERNION_USE
  88. //! Creates a matrix from this quaternion
  89. matrix4 getMatrix() const;
  90. #endif
  91. //! Faster method to create a rotation matrix, you should normalize the quaternion before!
  92. void getMatrixFast(matrix4 &dest) const;
  93. //! Creates a matrix from this quaternion
  94. void getMatrix(matrix4 &dest, const core::vector3df &translation = core::vector3df()) const;
  95. /*!
  96. Creates a matrix from this quaternion
  97. Rotate about a center point
  98. shortcut for
  99. core::quaternion q;
  100. q.rotationFromTo ( vin[i].Normal, forward );
  101. q.getMatrixCenter ( lookat, center, newPos );
  102. core::matrix4 m2;
  103. m2.setInverseTranslation ( center );
  104. lookat *= m2;
  105. core::matrix4 m3;
  106. m2.setTranslation ( newPos );
  107. lookat *= m3;
  108. */
  109. void getMatrixCenter(matrix4 &dest, const core::vector3df &center, const core::vector3df &translation) const;
  110. //! Creates a matrix from this quaternion
  111. inline void getMatrix_transposed(matrix4 &dest) const;
  112. //! Inverts this quaternion
  113. quaternion &makeInverse();
  114. //! Set this quaternion to the linear interpolation between two quaternions
  115. /** NOTE: lerp result is *not* a normalized quaternion. In most cases
  116. you will want to use lerpN instead as most other quaternion functions expect
  117. to work with a normalized quaternion.
  118. \param q1 First quaternion to be interpolated.
  119. \param q2 Second quaternion to be interpolated.
  120. \param time Progress of interpolation. For time=0 the result is
  121. q1, for time=1 the result is q2. Otherwise interpolation
  122. between q1 and q2. Result is not normalized.
  123. */
  124. quaternion &lerp(quaternion q1, quaternion q2, f32 time);
  125. //! Set this quaternion to the linear interpolation between two quaternions and normalize the result
  126. /**
  127. \param q1 First quaternion to be interpolated.
  128. \param q2 Second quaternion to be interpolated.
  129. \param time Progress of interpolation. For time=0 the result is
  130. q1, for time=1 the result is q2. Otherwise interpolation
  131. between q1 and q2. Result is normalized.
  132. */
  133. quaternion &lerpN(quaternion q1, quaternion q2, f32 time);
  134. //! Set this quaternion to the result of the spherical interpolation between two quaternions
  135. /** \param q1 First quaternion to be interpolated.
  136. \param q2 Second quaternion to be interpolated.
  137. \param time Progress of interpolation. For time=0 the result is
  138. q1, for time=1 the result is q2. Otherwise interpolation
  139. between q1 and q2.
  140. \param threshold To avoid inaccuracies at the end (time=1) the
  141. interpolation switches to linear interpolation at some point.
  142. This value defines how much of the remaining interpolation will
  143. be calculated with lerp. Everything from 1-threshold up will be
  144. linear interpolation.
  145. */
  146. quaternion &slerp(quaternion q1, quaternion q2,
  147. f32 time, f32 threshold = .05f);
  148. //! Set this quaternion to represent a rotation from angle and axis.
  149. /** Axis must be unit length.
  150. The quaternion representing the rotation is
  151. q = cos(A/2)+sin(A/2)*(x*i+y*j+z*k).
  152. \param angle Rotation Angle in radians.
  153. \param axis Rotation axis. */
  154. quaternion &fromAngleAxis(f32 angle, const vector3df &axis);
  155. //! Fills an angle (radians) around an axis (unit vector)
  156. void toAngleAxis(f32 &angle, core::vector3df &axis) const;
  157. //! Output this quaternion to an Euler angle (radians)
  158. void toEuler(vector3df &euler) const;
  159. //! Set quaternion to identity
  160. quaternion &makeIdentity();
  161. //! Set quaternion to represent a rotation from one vector to another.
  162. quaternion &rotationFromTo(const vector3df &from, const vector3df &to);
  163. //! Quaternion elements.
  164. f32 X; // vectorial (imaginary) part
  165. f32 Y;
  166. f32 Z;
  167. f32 W; // real part
  168. };
  169. // Constructor which converts Euler angles to a quaternion
  170. inline quaternion::quaternion(f32 x, f32 y, f32 z)
  171. {
  172. set(x, y, z);
  173. }
  174. // Constructor which converts Euler angles to a quaternion
  175. inline quaternion::quaternion(const vector3df &vec)
  176. {
  177. set(vec.X, vec.Y, vec.Z);
  178. }
  179. #ifndef IRR_TEST_BROKEN_QUATERNION_USE
  180. // Constructor which converts a matrix to a quaternion
  181. inline quaternion::quaternion(const matrix4 &mat)
  182. {
  183. (*this) = mat;
  184. }
  185. #endif
  186. #ifndef IRR_TEST_BROKEN_QUATERNION_USE
  187. // matrix assignment operator
  188. inline quaternion &quaternion::operator=(const matrix4 &m)
  189. {
  190. const f32 diag = m[0] + m[5] + m[10] + 1;
  191. if (diag > 0.0f) {
  192. const f32 scale = sqrtf(diag) * 2.0f; // get scale from diagonal
  193. // TODO: speed this up
  194. X = (m[6] - m[9]) / scale;
  195. Y = (m[8] - m[2]) / scale;
  196. Z = (m[1] - m[4]) / scale;
  197. W = 0.25f * scale;
  198. } else {
  199. if (m[0] > m[5] && m[0] > m[10]) {
  200. // 1st element of diag is greatest value
  201. // find scale according to 1st element, and double it
  202. const f32 scale = sqrtf(1.0f + m[0] - m[5] - m[10]) * 2.0f;
  203. // TODO: speed this up
  204. X = 0.25f * scale;
  205. Y = (m[4] + m[1]) / scale;
  206. Z = (m[2] + m[8]) / scale;
  207. W = (m[6] - m[9]) / scale;
  208. } else if (m[5] > m[10]) {
  209. // 2nd element of diag is greatest value
  210. // find scale according to 2nd element, and double it
  211. const f32 scale = sqrtf(1.0f + m[5] - m[0] - m[10]) * 2.0f;
  212. // TODO: speed this up
  213. X = (m[4] + m[1]) / scale;
  214. Y = 0.25f * scale;
  215. Z = (m[9] + m[6]) / scale;
  216. W = (m[8] - m[2]) / scale;
  217. } else {
  218. // 3rd element of diag is greatest value
  219. // find scale according to 3rd element, and double it
  220. const f32 scale = sqrtf(1.0f + m[10] - m[0] - m[5]) * 2.0f;
  221. // TODO: speed this up
  222. X = (m[8] + m[2]) / scale;
  223. Y = (m[9] + m[6]) / scale;
  224. Z = 0.25f * scale;
  225. W = (m[1] - m[4]) / scale;
  226. }
  227. }
  228. return normalize();
  229. }
  230. #endif
  231. // multiplication operator
  232. inline quaternion quaternion::operator*(const quaternion &other) const
  233. {
  234. quaternion tmp;
  235. tmp.W = (other.W * W) - (other.X * X) - (other.Y * Y) - (other.Z * Z);
  236. tmp.X = (other.W * X) + (other.X * W) + (other.Y * Z) - (other.Z * Y);
  237. tmp.Y = (other.W * Y) + (other.Y * W) + (other.Z * X) - (other.X * Z);
  238. tmp.Z = (other.W * Z) + (other.Z * W) + (other.X * Y) - (other.Y * X);
  239. return tmp;
  240. }
  241. // multiplication operator
  242. inline quaternion quaternion::operator*(f32 s) const
  243. {
  244. return quaternion(s * X, s * Y, s * Z, s * W);
  245. }
  246. // multiplication operator
  247. inline quaternion &quaternion::operator*=(f32 s)
  248. {
  249. X *= s;
  250. Y *= s;
  251. Z *= s;
  252. W *= s;
  253. return *this;
  254. }
  255. // multiplication operator
  256. inline quaternion &quaternion::operator*=(const quaternion &other)
  257. {
  258. return (*this = other * (*this));
  259. }
  260. // add operator
  261. inline quaternion quaternion::operator+(const quaternion &b) const
  262. {
  263. return quaternion(X + b.X, Y + b.Y, Z + b.Z, W + b.W);
  264. }
  265. #ifndef IRR_TEST_BROKEN_QUATERNION_USE
  266. // Creates a matrix from this quaternion
  267. inline matrix4 quaternion::getMatrix() const
  268. {
  269. core::matrix4 m;
  270. getMatrix(m);
  271. return m;
  272. }
  273. #endif
  274. //! Faster method to create a rotation matrix, you should normalize the quaternion before!
  275. inline void quaternion::getMatrixFast(matrix4 &dest) const
  276. {
  277. // TODO:
  278. // gpu quaternion skinning => fast Bones transform chain O_O YEAH!
  279. // http://www.mrelusive.com/publications/papers/SIMD-From-Quaternion-to-Matrix-and-Back.pdf
  280. dest[0] = 1.0f - 2.0f * Y * Y - 2.0f * Z * Z;
  281. dest[1] = 2.0f * X * Y + 2.0f * Z * W;
  282. dest[2] = 2.0f * X * Z - 2.0f * Y * W;
  283. dest[3] = 0.0f;
  284. dest[4] = 2.0f * X * Y - 2.0f * Z * W;
  285. dest[5] = 1.0f - 2.0f * X * X - 2.0f * Z * Z;
  286. dest[6] = 2.0f * Z * Y + 2.0f * X * W;
  287. dest[7] = 0.0f;
  288. dest[8] = 2.0f * X * Z + 2.0f * Y * W;
  289. dest[9] = 2.0f * Z * Y - 2.0f * X * W;
  290. dest[10] = 1.0f - 2.0f * X * X - 2.0f * Y * Y;
  291. dest[11] = 0.0f;
  292. dest[12] = 0.f;
  293. dest[13] = 0.f;
  294. dest[14] = 0.f;
  295. dest[15] = 1.f;
  296. dest.setDefinitelyIdentityMatrix(false);
  297. }
  298. /*!
  299. Creates a matrix from this quaternion
  300. */
  301. inline void quaternion::getMatrix(matrix4 &dest,
  302. const core::vector3df &center) const
  303. {
  304. // ok creating a copy may be slower, but at least avoid internal
  305. // state chance (also because otherwise we cannot keep this method "const").
  306. quaternion q(*this);
  307. q.normalize();
  308. f32 X = q.X;
  309. f32 Y = q.Y;
  310. f32 Z = q.Z;
  311. f32 W = q.W;
  312. dest[0] = 1.0f - 2.0f * Y * Y - 2.0f * Z * Z;
  313. dest[1] = 2.0f * X * Y + 2.0f * Z * W;
  314. dest[2] = 2.0f * X * Z - 2.0f * Y * W;
  315. dest[3] = 0.0f;
  316. dest[4] = 2.0f * X * Y - 2.0f * Z * W;
  317. dest[5] = 1.0f - 2.0f * X * X - 2.0f * Z * Z;
  318. dest[6] = 2.0f * Z * Y + 2.0f * X * W;
  319. dest[7] = 0.0f;
  320. dest[8] = 2.0f * X * Z + 2.0f * Y * W;
  321. dest[9] = 2.0f * Z * Y - 2.0f * X * W;
  322. dest[10] = 1.0f - 2.0f * X * X - 2.0f * Y * Y;
  323. dest[11] = 0.0f;
  324. dest[12] = center.X;
  325. dest[13] = center.Y;
  326. dest[14] = center.Z;
  327. dest[15] = 1.f;
  328. dest.setDefinitelyIdentityMatrix(false);
  329. }
  330. /*!
  331. Creates a matrix from this quaternion
  332. Rotate about a center point
  333. shortcut for
  334. core::quaternion q;
  335. q.rotationFromTo(vin[i].Normal, forward);
  336. q.getMatrix(lookat, center);
  337. core::matrix4 m2;
  338. m2.setInverseTranslation(center);
  339. lookat *= m2;
  340. */
  341. inline void quaternion::getMatrixCenter(matrix4 &dest,
  342. const core::vector3df &center,
  343. const core::vector3df &translation) const
  344. {
  345. quaternion q(*this);
  346. q.normalize();
  347. f32 X = q.X;
  348. f32 Y = q.Y;
  349. f32 Z = q.Z;
  350. f32 W = q.W;
  351. dest[0] = 1.0f - 2.0f * Y * Y - 2.0f * Z * Z;
  352. dest[1] = 2.0f * X * Y + 2.0f * Z * W;
  353. dest[2] = 2.0f * X * Z - 2.0f * Y * W;
  354. dest[3] = 0.0f;
  355. dest[4] = 2.0f * X * Y - 2.0f * Z * W;
  356. dest[5] = 1.0f - 2.0f * X * X - 2.0f * Z * Z;
  357. dest[6] = 2.0f * Z * Y + 2.0f * X * W;
  358. dest[7] = 0.0f;
  359. dest[8] = 2.0f * X * Z + 2.0f * Y * W;
  360. dest[9] = 2.0f * Z * Y - 2.0f * X * W;
  361. dest[10] = 1.0f - 2.0f * X * X - 2.0f * Y * Y;
  362. dest[11] = 0.0f;
  363. dest.setRotationCenter(center, translation);
  364. }
  365. // Creates a matrix from this quaternion
  366. inline void quaternion::getMatrix_transposed(matrix4 &dest) const
  367. {
  368. quaternion q(*this);
  369. q.normalize();
  370. f32 X = q.X;
  371. f32 Y = q.Y;
  372. f32 Z = q.Z;
  373. f32 W = q.W;
  374. dest[0] = 1.0f - 2.0f * Y * Y - 2.0f * Z * Z;
  375. dest[4] = 2.0f * X * Y + 2.0f * Z * W;
  376. dest[8] = 2.0f * X * Z - 2.0f * Y * W;
  377. dest[12] = 0.0f;
  378. dest[1] = 2.0f * X * Y - 2.0f * Z * W;
  379. dest[5] = 1.0f - 2.0f * X * X - 2.0f * Z * Z;
  380. dest[9] = 2.0f * Z * Y + 2.0f * X * W;
  381. dest[13] = 0.0f;
  382. dest[2] = 2.0f * X * Z + 2.0f * Y * W;
  383. dest[6] = 2.0f * Z * Y - 2.0f * X * W;
  384. dest[10] = 1.0f - 2.0f * X * X - 2.0f * Y * Y;
  385. dest[14] = 0.0f;
  386. dest[3] = 0.f;
  387. dest[7] = 0.f;
  388. dest[11] = 0.f;
  389. dest[15] = 1.f;
  390. dest.setDefinitelyIdentityMatrix(false);
  391. }
  392. // Inverts this quaternion
  393. inline quaternion &quaternion::makeInverse()
  394. {
  395. X = -X;
  396. Y = -Y;
  397. Z = -Z;
  398. return *this;
  399. }
  400. // sets new quaternion
  401. inline quaternion &quaternion::set(f32 x, f32 y, f32 z, f32 w)
  402. {
  403. X = x;
  404. Y = y;
  405. Z = z;
  406. W = w;
  407. return *this;
  408. }
  409. // sets new quaternion based on Euler angles
  410. inline quaternion &quaternion::set(f32 x, f32 y, f32 z)
  411. {
  412. f64 angle;
  413. angle = x * 0.5;
  414. const f64 sr = sin(angle);
  415. const f64 cr = cos(angle);
  416. angle = y * 0.5;
  417. const f64 sp = sin(angle);
  418. const f64 cp = cos(angle);
  419. angle = z * 0.5;
  420. const f64 sy = sin(angle);
  421. const f64 cy = cos(angle);
  422. const f64 cpcy = cp * cy;
  423. const f64 spcy = sp * cy;
  424. const f64 cpsy = cp * sy;
  425. const f64 spsy = sp * sy;
  426. X = (f32)(sr * cpcy - cr * spsy);
  427. Y = (f32)(cr * spcy + sr * cpsy);
  428. Z = (f32)(cr * cpsy - sr * spcy);
  429. W = (f32)(cr * cpcy + sr * spsy);
  430. return normalize();
  431. }
  432. // sets new quaternion based on Euler angles
  433. inline quaternion &quaternion::set(const core::vector3df &vec)
  434. {
  435. return set(vec.X, vec.Y, vec.Z);
  436. }
  437. // sets new quaternion based on other quaternion
  438. inline quaternion &quaternion::set(const core::quaternion &quat)
  439. {
  440. return (*this = quat);
  441. }
  442. //! returns if this quaternion equals the other one, taking floating point rounding errors into account
  443. inline bool quaternion::equals(const quaternion &other, const f32 tolerance) const
  444. {
  445. return core::equals(X, other.X, tolerance) &&
  446. core::equals(Y, other.Y, tolerance) &&
  447. core::equals(Z, other.Z, tolerance) &&
  448. core::equals(W, other.W, tolerance);
  449. }
  450. // normalizes the quaternion
  451. inline quaternion &quaternion::normalize()
  452. {
  453. // removed conditional branch since it may slow down and anyway the condition was
  454. // false even after normalization in some cases.
  455. return (*this *= (f32)reciprocal_squareroot((f64)(X * X + Y * Y + Z * Z + W * W)));
  456. }
  457. // Set this quaternion to the result of the linear interpolation between two quaternions
  458. inline quaternion &quaternion::lerp(quaternion q1, quaternion q2, f32 time)
  459. {
  460. const f32 scale = 1.0f - time;
  461. return (*this = (q1 * scale) + (q2 * time));
  462. }
  463. // Set this quaternion to the result of the linear interpolation between two quaternions and normalize the result
  464. inline quaternion &quaternion::lerpN(quaternion q1, quaternion q2, f32 time)
  465. {
  466. const f32 scale = 1.0f - time;
  467. return (*this = ((q1 * scale) + (q2 * time)).normalize());
  468. }
  469. // set this quaternion to the result of the interpolation between two quaternions
  470. inline quaternion &quaternion::slerp(quaternion q1, quaternion q2, f32 time, f32 threshold)
  471. {
  472. f32 angle = q1.dotProduct(q2);
  473. // make sure we use the short rotation
  474. if (angle < 0.0f) {
  475. q1 *= -1.0f;
  476. angle *= -1.0f;
  477. }
  478. if (angle <= (1 - threshold)) { // spherical interpolation
  479. const f32 theta = acosf(angle);
  480. const f32 invsintheta = reciprocal(sinf(theta));
  481. const f32 scale = sinf(theta * (1.0f - time)) * invsintheta;
  482. const f32 invscale = sinf(theta * time) * invsintheta;
  483. return (*this = (q1 * scale) + (q2 * invscale));
  484. } else // linear interpolation
  485. return lerpN(q1, q2, time);
  486. }
  487. // calculates the dot product
  488. inline f32 quaternion::dotProduct(const quaternion &q2) const
  489. {
  490. return (X * q2.X) + (Y * q2.Y) + (Z * q2.Z) + (W * q2.W);
  491. }
  492. //! axis must be unit length, angle in radians
  493. inline quaternion &quaternion::fromAngleAxis(f32 angle, const vector3df &axis)
  494. {
  495. const f32 fHalfAngle = 0.5f * angle;
  496. const f32 fSin = sinf(fHalfAngle);
  497. W = cosf(fHalfAngle);
  498. X = fSin * axis.X;
  499. Y = fSin * axis.Y;
  500. Z = fSin * axis.Z;
  501. return *this;
  502. }
  503. inline void quaternion::toAngleAxis(f32 &angle, core::vector3df &axis) const
  504. {
  505. const f32 scale = sqrtf(X * X + Y * Y + Z * Z);
  506. if (core::iszero(scale) || W > 1.0f || W < -1.0f) {
  507. angle = 0.0f;
  508. axis.X = 0.0f;
  509. axis.Y = 1.0f;
  510. axis.Z = 0.0f;
  511. } else {
  512. const f32 invscale = reciprocal(scale);
  513. angle = 2.0f * acosf(W);
  514. axis.X = X * invscale;
  515. axis.Y = Y * invscale;
  516. axis.Z = Z * invscale;
  517. }
  518. }
  519. inline void quaternion::toEuler(vector3df &euler) const
  520. {
  521. const f64 sqw = W * W;
  522. const f64 sqx = X * X;
  523. const f64 sqy = Y * Y;
  524. const f64 sqz = Z * Z;
  525. const f64 test = 2.0 * (Y * W - X * Z);
  526. if (core::equals(test, 1.0, 0.000001)) {
  527. // heading = rotation about z-axis
  528. euler.Z = (f32)(-2.0 * atan2(X, W));
  529. // bank = rotation about x-axis
  530. euler.X = 0;
  531. // attitude = rotation about y-axis
  532. euler.Y = (f32)(core::PI64 / 2.0);
  533. } else if (core::equals(test, -1.0, 0.000001)) {
  534. // heading = rotation about z-axis
  535. euler.Z = (f32)(2.0 * atan2(X, W));
  536. // bank = rotation about x-axis
  537. euler.X = 0;
  538. // attitude = rotation about y-axis
  539. euler.Y = (f32)(core::PI64 / -2.0);
  540. } else {
  541. // heading = rotation about z-axis
  542. euler.Z = (f32)atan2(2.0 * (X * Y + Z * W), (sqx - sqy - sqz + sqw));
  543. // bank = rotation about x-axis
  544. euler.X = (f32)atan2(2.0 * (Y * Z + X * W), (-sqx - sqy + sqz + sqw));
  545. // attitude = rotation about y-axis
  546. euler.Y = (f32)asin(clamp(test, -1.0, 1.0));
  547. }
  548. }
  549. inline vector3df quaternion::operator*(const vector3df &v) const
  550. {
  551. // nVidia SDK implementation
  552. vector3df uv, uuv;
  553. const vector3df qvec(X, Y, Z);
  554. uv = qvec.crossProduct(v);
  555. uuv = qvec.crossProduct(uv);
  556. uv *= (2.0f * W);
  557. uuv *= 2.0f;
  558. return v + uv + uuv;
  559. }
  560. // set quaternion to identity
  561. inline core::quaternion &quaternion::makeIdentity()
  562. {
  563. W = 1.f;
  564. X = 0.f;
  565. Y = 0.f;
  566. Z = 0.f;
  567. return *this;
  568. }
  569. inline core::quaternion &quaternion::rotationFromTo(const vector3df &from, const vector3df &to)
  570. {
  571. // Based on Stan Melax's article in Game Programming Gems
  572. // Optimized by Robert Eisele: https://raw.org/proof/quaternion-from-two-vectors
  573. // Copy, since cannot modify local
  574. vector3df v0 = from;
  575. vector3df v1 = to;
  576. v0.normalize();
  577. v1.normalize();
  578. const f32 d = v0.dotProduct(v1);
  579. if (d >= 1.0f) { // If dot == 1, vectors are the same
  580. return makeIdentity();
  581. } else if (d <= -1.0f) { // exactly opposite
  582. core::vector3df axis(1.0f, 0.f, 0.f);
  583. axis = axis.crossProduct(v0);
  584. if (axis.getLength() == 0) {
  585. axis.set(0.f, 1.f, 0.f);
  586. axis = axis.crossProduct(v0);
  587. }
  588. // same as fromAngleAxis(core::PI, axis).normalize();
  589. return set(axis.X, axis.Y, axis.Z, 0).normalize();
  590. }
  591. const vector3df c = v0.crossProduct(v1);
  592. return set(c.X, c.Y, c.Z, 1 + d).normalize();
  593. }
  594. } // end namespace core
  595. } // end namespace irr