BsQuaternion.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #pragma once
  2. #include "BsPrerequisitesUtil.h"
  3. #include "BsMath.h"
  4. #include "BsVector3.h"
  5. namespace BansheeEngine
  6. {
  7. /**
  8. * @brief Represents a quaternion used for 3D rotations.
  9. */
  10. class BS_UTILITY_EXPORT Quaternion
  11. {
  12. private:
  13. struct EulerAngleOrderData
  14. {
  15. int a, b, c;
  16. };
  17. public:
  18. Quaternion(float w = 1.0f, float x = 0.0f, float y = 0.0f, float z = 0.0f)
  19. :w(w), z(z), y(y), x(x)
  20. { }
  21. /**
  22. * @brief Construct a quaternion from a rotation matrix.
  23. */
  24. explicit Quaternion(const Matrix3& rot)
  25. {
  26. fromRotationMatrix(rot);
  27. }
  28. /**
  29. * @brief Construct a quaternion from an angle/axis.
  30. */
  31. explicit Quaternion(const Vector3& axis, const Radian& angle)
  32. {
  33. fromAxisAngle(axis, angle);
  34. }
  35. /**
  36. * @brief Construct a quaternion from 3 orthonormal local axes.
  37. */
  38. explicit Quaternion(const Vector3& xaxis, const Vector3& yaxis, const Vector3& zaxis)
  39. {
  40. fromAxes(xaxis, yaxis, zaxis);
  41. }
  42. /**
  43. * @brief Construct a quaternion from euler angles, XYZ ordering.
  44. *
  45. * @see Quaternion::fromEulerAngles
  46. */
  47. explicit Quaternion(const Radian& xAngle, const Radian& yAngle, const Radian& zAngle)
  48. {
  49. fromEulerAngles(xAngle, yAngle, zAngle);
  50. }
  51. /**
  52. * @brief Construct a quaternion from euler angles, custom ordering.
  53. *
  54. * @see Quaternion::fromEulerAngles
  55. */
  56. explicit Quaternion(const Radian& xAngle, const Radian& yAngle, const Radian& zAngle, EulerAngleOrder order)
  57. {
  58. fromEulerAngles(xAngle, yAngle, zAngle, order);
  59. }
  60. /**
  61. * @brief Exchange the contents of this quaternion with another.
  62. */
  63. void swap(Quaternion& other)
  64. {
  65. std::swap(w, other.w);
  66. std::swap(x, other.x);
  67. std::swap(y, other.y);
  68. std::swap(z, other.z);
  69. }
  70. float operator[] (const size_t i) const
  71. {
  72. assert(i < 4);
  73. return *(&w+i);
  74. }
  75. float& operator[] (const size_t i)
  76. {
  77. assert(i < 4);
  78. return *(&w+i);
  79. }
  80. /**
  81. * @brief Initializes the quaternion from a 3x3 rotation matrix.
  82. *
  83. * @note It's up to the caller to ensure the matrix is orthonormal.
  84. */
  85. void fromRotationMatrix(const Matrix3& mat);
  86. /**
  87. * @brief Initializes the quaternion from an angle axis pair. Quaternion
  88. * will represent a rotation of "angle" radians around "axis".
  89. */
  90. void fromAxisAngle(const Vector3& axis, const Radian& angle);
  91. /**
  92. * @brief Initializes the quaternion from orthonormal set of axes. Quaternion
  93. * will represent a rotation from base axes to the specified set of axes.
  94. *
  95. * @note It's up to the caller to ensure the axes are orthonormal.
  96. */
  97. void fromAxes(const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis);
  98. /**
  99. * @brief Creates a quaternion from the provided Pitch/Yaw/Roll angles.
  100. *
  101. * @param xAngle Rotation about x axis. (AKA Pitch)
  102. * @param yAngle Rotation about y axis. (AKA Yaw)
  103. * @param zAngle Rotation about z axis. (AKA Roll)
  104. *
  105. * @note Since different values will be produced depending in which order are the rotations applied, this method assumes
  106. * they are applied in XYZ order. If you need a specific order, use the overloaded "fromEulerAngles" method instead.
  107. */
  108. void fromEulerAngles(const Radian& xAngle, const Radian& yAngle, const Radian& zAngle);
  109. /**
  110. * @brief Creates a quaternion from the provided Pitch/Yaw/Roll angles.
  111. *
  112. * @param xAngle Rotation about x axis. (AKA Pitch)
  113. * @param yAngle Rotation about y axis. (AKA Yaw)
  114. * @param zAngle Rotation about z axis. (AKA Roll)
  115. * @param order The order in which rotations will be extracted.
  116. * Different values can be retrieved depending on the order.
  117. */
  118. void fromEulerAngles(const Radian& xAngle, const Radian& yAngle, const Radian& zAngle, EulerAngleOrder order);
  119. /**
  120. * @brief Converts a quaternion to a rotation matrix.
  121. */
  122. void toRotationMatrix(Matrix3& mat) const;
  123. /**
  124. * @brief Converts a quaternion to an angle axis pair.
  125. *
  126. * @param [out] axis The axis around the which rotation takes place.
  127. * @param [out] angle The angle in radians determining amount of rotation around the axis.
  128. */
  129. void toAxisAngle(Vector3& axis, Radian& angle) const;
  130. /**
  131. * @brief Converts a quaternion to an orthonormal set of axes.
  132. *
  133. * @param [out] xAxis The X axis.
  134. * @param [out] yAxis The Y axis.
  135. * @param [out] zAxis The Z axis.
  136. */
  137. void toAxes(Vector3& xAxis, Vector3& yAxis, Vector3& zAxis) const;
  138. /**
  139. * @brief Extracts Pitch/Yaw/Roll rotations from this quaternion.
  140. *
  141. * @param [out] xAngle Rotation about x axis. (AKA Pitch)
  142. * @param [out] yAngle Rotation about y axis. (AKA Yaw)
  143. * @param [out] zAngle Rotation about z axis. (AKA Roll)
  144. *
  145. * @return True if unique solution was found, false otherwise.
  146. *
  147. * @note Since different values will be returned depending in which order are the rotations applied, this method assumes
  148. * they are applied in XYZ order. If you need a specific order, use the overloaded "toEulerAngles" method instead.
  149. */
  150. bool toEulerAngles(Radian& xAngle, Radian& yAngle, Radian& zAngle) const;
  151. /**
  152. * @brief Extracts Pitch/Yaw/Roll rotations from this quaternion.
  153. *
  154. * @param [out] xAngle Rotation about x axis. (AKA Pitch)
  155. * @param [out] yAngle Rotation about y axis. (AKA Yaw)
  156. * @param [out] zAngle Rotation about z axis. (AKA Roll)
  157. * @param order The order in which rotations will be extracted.
  158. * Different values can be retrieved depending on the order.
  159. *
  160. * @return True if unique solution was found, false otherwise.
  161. */
  162. bool toEulerAngles(Radian& xAngle, Radian& yAngle, Radian& zAngle, EulerAngleOrder order) const;
  163. /**
  164. * @brief Get the local x-axis.
  165. */
  166. Vector3 xAxis() const;
  167. /**
  168. * @brief Get the local y-axis.
  169. */
  170. Vector3 yAxis() const;
  171. /**
  172. * @brief Get the local z-axis.
  173. */
  174. Vector3 zAxis() const;
  175. Quaternion& operator= (const Quaternion& rhs)
  176. {
  177. w = rhs.w;
  178. x = rhs.x;
  179. y = rhs.y;
  180. z = rhs.z;
  181. return *this;
  182. }
  183. Quaternion operator+ (const Quaternion& rhs) const;
  184. Quaternion operator- (const Quaternion& rhs) const;
  185. Quaternion operator* (const Quaternion& rhs) const;
  186. Quaternion operator* (float rhs) const;
  187. Quaternion operator- () const;
  188. bool operator== (const Quaternion& rhs) const
  189. {
  190. return (rhs.x == x) && (rhs.y == y) && (rhs.z == z) && (rhs.w == w);
  191. }
  192. bool operator!= (const Quaternion& rhs) const
  193. {
  194. return !operator==(rhs);
  195. }
  196. friend Quaternion operator* (float lhs, const Quaternion& rhs);
  197. /**
  198. * @brief Calculates the dot product of this quaternion and another.
  199. */
  200. float dot(const Quaternion& other) const;
  201. /**
  202. * @brief Normalizes this quaternion, and returns the previous length.
  203. */
  204. float normalize();
  205. /**
  206. * @brief Gets the inverse.
  207. *
  208. * @note Quaternion must be non-zero.
  209. */
  210. Quaternion inverse() const;
  211. /**
  212. * @brief Rotates the provided vector.
  213. */
  214. Vector3 rotate(const Vector3& vec) const;
  215. /**
  216. * @brief Query if any of the components of the quaternion are NaN.
  217. */
  218. bool isNaN() const
  219. {
  220. return Math::isNaN(x) || Math::isNaN(y) || Math::isNaN(z) || Math::isNaN(w);
  221. }
  222. /**
  223. * @brief Spherical linear interpolation
  224. */
  225. static Quaternion slerp(float t, const Quaternion& p,
  226. const Quaternion& q, bool shortestPath = false);
  227. /**
  228. * @brief Gets the shortest arc quaternion to rotate this vector to the destination
  229. * vector.
  230. */
  231. static Quaternion getRotationFromTo(const Vector3& from, const Vector3& dest, const Vector3& fallbackAxis = Vector3::ZERO);
  232. static const float EPSILON;
  233. static const Quaternion ZERO;
  234. static const Quaternion IDENTITY;
  235. float x, y, z, w;
  236. private:
  237. static const EulerAngleOrderData EA_LOOKUP[6];
  238. };
  239. }