BsQuaternion.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "Prerequisites/BsPrerequisitesUtil.h"
  5. #include "Math/BsMath.h"
  6. #include "Math/BsVector3.h"
  7. namespace bs
  8. {
  9. /** @addtogroup Math
  10. * @{
  11. */
  12. /** Represents a quaternion used for 3D rotations. */
  13. class BS_UTILITY_EXPORT Quaternion
  14. {
  15. private:
  16. struct EulerAngleOrderData
  17. {
  18. int a, b, c;
  19. };
  20. public:
  21. Quaternion()
  22. { }
  23. Quaternion(BS_ZERO zero)
  24. :x(0.0f), y(0.0f), z(0.0f), w(0.0f)
  25. { }
  26. Quaternion(BS_IDENTITY identity)
  27. :x(0.0f), y(0.0f), z(0.0f), w(1.0f)
  28. { }
  29. Quaternion(float w, float x, float y, float z)
  30. :x(x), y(y), z(z), w(w)
  31. { }
  32. /** Construct a quaternion from a rotation matrix. */
  33. explicit Quaternion(const Matrix3& rot)
  34. {
  35. fromRotationMatrix(rot);
  36. }
  37. /** Construct a quaternion from an angle/axis. */
  38. explicit Quaternion(const Vector3& axis, const Radian& angle)
  39. {
  40. fromAxisAngle(axis, angle);
  41. }
  42. /** Construct a quaternion from 3 orthonormal local axes. */
  43. explicit Quaternion(const Vector3& xaxis, const Vector3& yaxis, const Vector3& zaxis)
  44. {
  45. fromAxes(xaxis, yaxis, zaxis);
  46. }
  47. /**
  48. * Construct a quaternion from euler angles, YXZ ordering.
  49. *
  50. * @see Quaternion::fromEulerAngles
  51. */
  52. explicit Quaternion(const Radian& xAngle, const Radian& yAngle, const Radian& zAngle)
  53. {
  54. fromEulerAngles(xAngle, yAngle, zAngle);
  55. }
  56. /**
  57. * Construct a quaternion from euler angles, custom ordering.
  58. *
  59. * @see Quaternion::fromEulerAngles
  60. */
  61. explicit Quaternion(const Radian& xAngle, const Radian& yAngle, const Radian& zAngle, EulerAngleOrder order)
  62. {
  63. fromEulerAngles(xAngle, yAngle, zAngle, order);
  64. }
  65. /** Exchange the contents of this quaternion with another. */
  66. void swap(Quaternion& other)
  67. {
  68. std::swap(w, other.w);
  69. std::swap(x, other.x);
  70. std::swap(y, other.y);
  71. std::swap(z, other.z);
  72. }
  73. float operator[] (const size_t i) const
  74. {
  75. assert(i < 4);
  76. return *(&x+i);
  77. }
  78. float& operator[] (const size_t i)
  79. {
  80. assert(i < 4);
  81. return *(&x+i);
  82. }
  83. /**
  84. * Initializes the quaternion from a 3x3 rotation matrix.
  85. *
  86. * @note It's up to the caller to ensure the matrix is orthonormal.
  87. */
  88. void fromRotationMatrix(const Matrix3& mat);
  89. /**
  90. * Initializes the quaternion from an angle axis pair. Quaternion will represent a rotation of "angle" radians
  91. * around "axis".
  92. */
  93. void fromAxisAngle(const Vector3& axis, const Radian& angle);
  94. /**
  95. * Initializes the quaternion from orthonormal set of axes. Quaternion will represent a rotation from base axes
  96. * to the specified set of axes.
  97. *
  98. * @note It's up to the caller to ensure the axes are orthonormal.
  99. */
  100. void fromAxes(const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis);
  101. /**
  102. * Creates a quaternion from the provided Pitch/Yaw/Roll angles.
  103. *
  104. * @param[in] xAngle Rotation about x axis. (AKA Pitch)
  105. * @param[in] yAngle Rotation about y axis. (AKA Yaw)
  106. * @param[in] zAngle Rotation about z axis. (AKA Roll)
  107. *
  108. * @note
  109. * Since different values will be produced depending in which order are the rotations applied, this method assumes
  110. * they are applied in YXZ order. If you need a specific order, use the overloaded fromEulerAngles() method instead.
  111. */
  112. void fromEulerAngles(const Radian& xAngle, const Radian& yAngle, const Radian& zAngle);
  113. /**
  114. * Creates a quaternion from the provided Pitch/Yaw/Roll angles.
  115. *
  116. * @param[in] xAngle Rotation about x axis. (AKA Pitch)
  117. * @param[in] yAngle Rotation about y axis. (AKA Yaw)
  118. * @param[in] zAngle Rotation about z axis. (AKA Roll)
  119. * @param[in] order The order in which rotations will be extracted. Different values can be retrieved depending
  120. * on the order.
  121. */
  122. void fromEulerAngles(const Radian& xAngle, const Radian& yAngle, const Radian& zAngle, EulerAngleOrder order);
  123. /**
  124. * Converts a quaternion to a rotation matrix.
  125. */
  126. void toRotationMatrix(Matrix3& mat) const;
  127. /**
  128. * Converts a quaternion to an angle axis pair.
  129. *
  130. * @param[out] axis The axis around the which rotation takes place.
  131. * @param[out] angle The angle in radians determining amount of rotation around the axis.
  132. */
  133. void toAxisAngle(Vector3& axis, Radian& angle) const;
  134. /**
  135. * Converts a quaternion to an orthonormal set of axes.
  136. *
  137. * @param[out] xAxis The X axis.
  138. * @param[out] yAxis The Y axis.
  139. * @param[out] zAxis The Z axis.
  140. */
  141. void toAxes(Vector3& xAxis, Vector3& yAxis, Vector3& zAxis) const;
  142. /**
  143. * Extracts Pitch/Yaw/Roll rotations from this quaternion.
  144. *
  145. * @param[out] xAngle Rotation about x axis. (AKA Pitch)
  146. * @param[out] yAngle Rotation about y axis. (AKA Yaw)
  147. * @param[out] zAngle Rotation about z axis. (AKA Roll)
  148. *
  149. * @return True if unique solution was found, false otherwise.
  150. */
  151. bool toEulerAngles(Radian& xAngle, Radian& yAngle, Radian& zAngle) const;
  152. /** Gets the positive x-axis of the coordinate system transformed by this quaternion. */
  153. Vector3 xAxis() const;
  154. /** Gets the positive y-axis of the coordinate system transformed by this quaternion. */
  155. Vector3 yAxis() const;
  156. /** Gets the positive z-axis of the coordinate system transformed by this quaternion. */
  157. Vector3 zAxis() const;
  158. Quaternion& operator= (const Quaternion& rhs)
  159. {
  160. w = rhs.w;
  161. x = rhs.x;
  162. y = rhs.y;
  163. z = rhs.z;
  164. return *this;
  165. }
  166. Quaternion operator+ (const Quaternion& rhs) const
  167. {
  168. return Quaternion(w + rhs.w, x + rhs.x, y + rhs.y, z + rhs.z);
  169. }
  170. Quaternion operator- (const Quaternion& rhs) const
  171. {
  172. return Quaternion(w - rhs.w, x - rhs.x, y - rhs.y, z - rhs.z);
  173. }
  174. Quaternion operator* (const Quaternion& rhs) const
  175. {
  176. return Quaternion
  177. (
  178. w * rhs.w - x * rhs.x - y * rhs.y - z * rhs.z,
  179. w * rhs.x + x * rhs.w + y * rhs.z - z * rhs.y,
  180. w * rhs.y + y * rhs.w + z * rhs.x - x * rhs.z,
  181. w * rhs.z + z * rhs.w + x * rhs.y - y * rhs.x
  182. );
  183. }
  184. Quaternion operator* (float rhs) const
  185. {
  186. return Quaternion(rhs * w, rhs * x, rhs * y, rhs * z);
  187. }
  188. Quaternion operator- () const
  189. {
  190. return Quaternion(-w, -x, -y, -z);
  191. }
  192. bool operator== (const Quaternion& rhs) const
  193. {
  194. return (rhs.x == x) && (rhs.y == y) && (rhs.z == z) && (rhs.w == w);
  195. }
  196. bool operator!= (const Quaternion& rhs) const
  197. {
  198. return !operator==(rhs);
  199. }
  200. Quaternion& operator+= (const Quaternion& rhs)
  201. {
  202. w += rhs.w;
  203. x += rhs.x;
  204. y += rhs.y;
  205. z += rhs.z;
  206. return *this;
  207. }
  208. Quaternion& operator-= (const Quaternion& rhs)
  209. {
  210. w -= rhs.w;
  211. x -= rhs.x;
  212. y -= rhs.y;
  213. z -= rhs.z;
  214. return *this;
  215. }
  216. Quaternion& operator*= (const Quaternion& rhs)
  217. {
  218. float newW = w * rhs.w - x * rhs.x - y * rhs.y - z * rhs.z;
  219. float newX = w * rhs.x + x * rhs.w + y * rhs.z - z * rhs.y;
  220. float newY = w * rhs.y + y * rhs.w + z * rhs.x - x * rhs.z;
  221. float newZ = w * rhs.z + z * rhs.w + x * rhs.y - y * rhs.x;
  222. w = newW;
  223. x = newX;
  224. y = newY;
  225. z = newZ;
  226. return *this;
  227. }
  228. friend Quaternion operator* (float lhs, const Quaternion& rhs)
  229. {
  230. return Quaternion(lhs * rhs.w, lhs * rhs.x, lhs * rhs.y, lhs * rhs.z);
  231. }
  232. /** Calculates the dot product of this quaternion and another. */
  233. float dot(const Quaternion& other) const
  234. {
  235. return w * other.w + x * other.x + y * other.y + z * other.z;
  236. }
  237. /** Normalizes this quaternion, and returns the previous length. */
  238. float normalize()
  239. {
  240. float len = w*w + x*x + y*y + z*z;
  241. float factor = 1.0f / Math::sqrt(len);
  242. *this = *this * factor;
  243. return len;
  244. }
  245. /**
  246. * Gets the inverse.
  247. *
  248. * @note Quaternion must be non-zero.
  249. */
  250. Quaternion inverse() const;
  251. /** Rotates the provided vector. */
  252. Vector3 rotate(const Vector3& vec) const;
  253. /**
  254. * Orients the quaternion so its negative z axis points to the provided direction.
  255. *
  256. * @param[in] forwardDir Direction to orient towards.
  257. */
  258. void lookRotation(const Vector3& forwardDir);
  259. /**
  260. * Orients the quaternion so its negative z axis points to the provided direction.
  261. *
  262. * @param[in] forwardDir Direction to orient towards.
  263. * @param[in] upDir Constrains y axis orientation to a plane this vector lies on. This rule might be broken
  264. * if forward and up direction are nearly parallel.
  265. */
  266. void lookRotation(const Vector3& forwardDir, const Vector3& upDir);
  267. /** Query if any of the components of the quaternion are not a number. */
  268. bool isNaN() const
  269. {
  270. return Math::isNaN(x) || Math::isNaN(y) || Math::isNaN(z) || Math::isNaN(w);
  271. }
  272. /** Calculates the dot product between two quaternions. */
  273. static float dot(const Quaternion& lhs, const Quaternion& rhs)
  274. {
  275. return lhs.w * rhs.w + lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
  276. }
  277. /** Normalizes the provided quaternion. */
  278. static Quaternion normalize(const Quaternion& q)
  279. {
  280. float len = dot(q, q);
  281. float factor = 1.0f / Math::sqrt(len);
  282. return q * factor;
  283. }
  284. /**
  285. * Performs spherical interpolation between two quaternions. Spherical interpolation neatly interpolates between
  286. * two rotations without modifying the size of the vector it is applied to (unlike linear interpolation).
  287. */
  288. static Quaternion slerp(float t, const Quaternion& p, const Quaternion& q, bool shortestPath = true);
  289. /**
  290. * Linearly interpolates between the two quaternions using @p t. t should be in [0, 1] range, where t = 0
  291. * corresponds to the left vector, while t = 1 corresponds to the right vector.
  292. */
  293. static Quaternion lerp(float t, const Quaternion& a, const Quaternion& b)
  294. {
  295. float d = dot(a, b);
  296. float flip = d >= 0.0f ? 1.0f : -1.0f;
  297. Quaternion output = flip * (1.0f - t) * a + t * b;
  298. return normalize(output);
  299. }
  300. /** Gets the shortest arc quaternion to rotate this vector to the destination vector. */
  301. static Quaternion getRotationFromTo(const Vector3& from, const Vector3& dest, const Vector3& fallbackAxis = Vector3::ZERO);
  302. static const float EPSILON;
  303. static const Quaternion ZERO;
  304. static const Quaternion IDENTITY;
  305. float x, y, z, w; // Note: Order is relevant, don't break it
  306. private:
  307. static const EulerAngleOrderData EA_LOOKUP[6];
  308. };
  309. /** @} */
  310. /** @cond SPECIALIZATIONS */
  311. BS_ALLOW_MEMCPY_SERIALIZATION(Quaternion);
  312. /** @endcond */
  313. }