BsQuaternion.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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, YXZ 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 YXZ 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. bool toEulerAngles(Radian& xAngle, Radian& yAngle, Radian& zAngle) const;
  148. /**
  149. * @brief Gets the positive x-axis of the coordinate system transformed by this quaternion.
  150. */
  151. Vector3 xAxis() const;
  152. /**
  153. * @brief Gets the positive y-axis of the coordinate system transformed by this quaternion.
  154. */
  155. Vector3 yAxis() const;
  156. /**
  157. * @brief Gets the positive z-axis of the coordinate system transformed by this quaternion.
  158. */
  159. Vector3 zAxis() const;
  160. Quaternion& operator= (const Quaternion& rhs)
  161. {
  162. w = rhs.w;
  163. x = rhs.x;
  164. y = rhs.y;
  165. z = rhs.z;
  166. return *this;
  167. }
  168. Quaternion operator+ (const Quaternion& rhs) const;
  169. Quaternion operator- (const Quaternion& rhs) const;
  170. Quaternion operator* (const Quaternion& rhs) const;
  171. Quaternion operator* (float rhs) const;
  172. Quaternion operator- () const;
  173. bool operator== (const Quaternion& rhs) const
  174. {
  175. return (rhs.x == x) && (rhs.y == y) && (rhs.z == z) && (rhs.w == w);
  176. }
  177. bool operator!= (const Quaternion& rhs) const
  178. {
  179. return !operator==(rhs);
  180. }
  181. friend Quaternion operator* (float lhs, const Quaternion& rhs);
  182. /**
  183. * @brief Calculates the dot product of this quaternion and another.
  184. */
  185. float dot(const Quaternion& other) const;
  186. /**
  187. * @brief Normalizes this quaternion, and returns the previous length.
  188. */
  189. float normalize();
  190. /**
  191. * @brief Gets the inverse.
  192. *
  193. * @note Quaternion must be non-zero.
  194. */
  195. Quaternion inverse() const;
  196. /**
  197. * @brief Rotates the provided vector.
  198. */
  199. Vector3 rotate(const Vector3& vec) const;
  200. /**
  201. * @brief Query if any of the components of the quaternion are NaN.
  202. */
  203. bool isNaN() const
  204. {
  205. return Math::isNaN(x) || Math::isNaN(y) || Math::isNaN(z) || Math::isNaN(w);
  206. }
  207. /**
  208. * @brief Performs spherical interpolation between two quaternions. Spherical interpolation
  209. * neatly interpolates between two rotations without modifying the size of the vector
  210. * it is applied to (unlike linear interpolation).
  211. */
  212. static Quaternion slerp(float t, const Quaternion& p,
  213. const Quaternion& q, bool shortestPath = false);
  214. /**
  215. * @brief Gets the shortest arc quaternion to rotate this vector to the destination
  216. * vector.
  217. */
  218. static Quaternion getRotationFromTo(const Vector3& from, const Vector3& dest, const Vector3& fallbackAxis = Vector3::ZERO);
  219. static const float EPSILON;
  220. static const Quaternion ZERO;
  221. static const Quaternion IDENTITY;
  222. float x, y, z, w;
  223. private:
  224. static const EulerAngleOrderData EA_LOOKUP[6];
  225. };
  226. BS_ALLOW_MEMCPY_SERIALIZATION(Quaternion);
  227. }