BsQuaternion.h 11 KB

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