BsMatrix3.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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/BsVector3.h"
  6. namespace bs
  7. {
  8. /** @addtogroup Math
  9. * @{
  10. */
  11. /**
  12. * A 3x3 matrix. Can be used for non-homogenous transformations of three dimensional vectors and points. In row major
  13. * format.
  14. */
  15. class BS_UTILITY_EXPORT Matrix3
  16. {
  17. private:
  18. struct EulerAngleOrderData
  19. {
  20. int a, b, c;
  21. float sign;
  22. };
  23. public:
  24. Matrix3() {}
  25. Matrix3(BS_ZERO zero)
  26. :Matrix3(Matrix3::ZERO)
  27. { }
  28. Matrix3(BS_IDENTITY identity)
  29. :Matrix3(Matrix3::IDENTITY)
  30. { }
  31. Matrix3(const Matrix3& mat)
  32. {
  33. memcpy(m, mat.m, 9*sizeof(float));
  34. }
  35. Matrix3(float m00, float m01, float m02,
  36. float m10, float m11, float m12,
  37. float m20, float m21, float m22)
  38. {
  39. m[0][0] = m00;
  40. m[0][1] = m01;
  41. m[0][2] = m02;
  42. m[1][0] = m10;
  43. m[1][1] = m11;
  44. m[1][2] = m12;
  45. m[2][0] = m20;
  46. m[2][1] = m21;
  47. m[2][2] = m22;
  48. }
  49. /** Construct a matrix from a quaternion. */
  50. explicit Matrix3(const Quaternion& rotation)
  51. {
  52. fromQuaternion(rotation);
  53. }
  54. /** Construct a matrix that performs rotation and scale. */
  55. explicit Matrix3(const Quaternion& rotation, const Vector3& scale)
  56. {
  57. fromQuaternion(rotation);
  58. for (int row = 0; row < 3; row++)
  59. {
  60. for (int col = 0; col < 3; col++)
  61. m[row][col] = scale[row]*m[row][col];
  62. }
  63. }
  64. /** Construct a matrix from an angle/axis pair. */
  65. explicit Matrix3(const Vector3& axis, const Radian& angle)
  66. {
  67. fromAxisAngle(axis, angle);
  68. }
  69. /** Construct a matrix from 3 orthonormal local axes. */
  70. explicit Matrix3(const Vector3& xaxis, const Vector3& yaxis, const Vector3& zaxis)
  71. {
  72. fromAxes(xaxis, yaxis, zaxis);
  73. }
  74. /**
  75. * Construct a matrix from euler angles, YXZ ordering.
  76. *
  77. * @see Matrix3::fromEulerAngles
  78. */
  79. explicit Matrix3(const Radian& xAngle, const Radian& yAngle, const Radian& zAngle)
  80. {
  81. fromEulerAngles(xAngle, yAngle, zAngle);
  82. }
  83. /**
  84. * Construct a matrix from euler angles, custom ordering.
  85. *
  86. * @see Matrix3::fromEulerAngles
  87. */
  88. explicit Matrix3(const Radian& xAngle, const Radian& yAngle, const Radian& zAngle, EulerAngleOrder order)
  89. {
  90. fromEulerAngles(xAngle, yAngle, zAngle, order);
  91. }
  92. /** Swaps the contents of this matrix with another. */
  93. void swap(Matrix3& other)
  94. {
  95. std::swap(m[0][0], other.m[0][0]);
  96. std::swap(m[0][1], other.m[0][1]);
  97. std::swap(m[0][2], other.m[0][2]);
  98. std::swap(m[1][0], other.m[1][0]);
  99. std::swap(m[1][1], other.m[1][1]);
  100. std::swap(m[1][2], other.m[1][2]);
  101. std::swap(m[2][0], other.m[2][0]);
  102. std::swap(m[2][1], other.m[2][1]);
  103. std::swap(m[2][2], other.m[2][2]);
  104. }
  105. /** Returns a row of the matrix. */
  106. float* operator[] (UINT32 row) const
  107. {
  108. assert(row < 3);
  109. return (float*)m[row];
  110. }
  111. Vector3 getColumn(UINT32 col) const;
  112. void setColumn(UINT32 col, const Vector3& vec);
  113. Matrix3& operator= (const Matrix3& rhs)
  114. {
  115. memcpy(m, rhs.m, 9*sizeof(float));
  116. return *this;
  117. }
  118. bool operator== (const Matrix3& rhs) const;
  119. bool operator!= (const Matrix3& rhs) const;
  120. Matrix3 operator+ (const Matrix3& rhs) const;
  121. Matrix3 operator- (const Matrix3& rhs) const;
  122. Matrix3 operator* (const Matrix3& rhs) const;
  123. Matrix3 operator- () const;
  124. Matrix3 operator* (float rhs) const;
  125. friend Matrix3 operator* (float lhs, const Matrix3& rhs);
  126. /** Transforms the given vector by this matrix and returns the newly transformed vector. */
  127. Vector3 transform(const Vector3& vec) const;
  128. /** Returns a transpose of the matrix (switched columns and rows). */
  129. Matrix3 transpose () const;
  130. /**
  131. * Calculates an inverse of the matrix if it exists.
  132. *
  133. * @param[out] mat Resulting matrix inverse.
  134. * @param[in] fTolerance (optional) Tolerance to use when checking if determinant is zero (or near zero in this case).
  135. * Zero determinant means inverse doesn't exist.
  136. * @return True if inverse exists, false otherwise.
  137. */
  138. bool inverse(Matrix3& mat, float fTolerance = 1e-06f) const;
  139. /**
  140. * Calculates an inverse of the matrix if it exists.
  141. *
  142. * @param[in] fTolerance (optional) Tolerance to use when checking if determinant is zero (or near zero in this case).
  143. * Zero determinant means inverse doesn't exist.
  144. *
  145. * @return Resulting matrix inverse if it exists, otherwise a zero matrix.
  146. */
  147. Matrix3 inverse(float fTolerance = 1e-06f) const;
  148. /** Calculates the matrix determinant. */
  149. float determinant() const;
  150. /**
  151. * Decompose a Matrix3 to rotation and scale.
  152. *
  153. * @note
  154. * Matrix must consist only of rotation and uniform scale transformations, otherwise accurate results are not
  155. * guaranteed. Applying non-uniform scale guarantees rotation portion will not be accurate.
  156. */
  157. void decomposition(Quaternion& rotation, Vector3& scale) const;
  158. /**
  159. * Decomposes the matrix into various useful values.
  160. *
  161. * @param[out] matL Unitary matrix. Columns form orthonormal bases. If your matrix is affine and
  162. * doesn't use non-uniform scaling this matrix will be a conjugate transpose of the rotation part of the matrix.
  163. * @param[out] matS Singular values of the matrix. If your matrix is affine these will be scaling factors of the matrix.
  164. * @param[out] matR Unitary matrix. Columns form orthonormal bases. If your matrix is affine and
  165. * doesn't use non-uniform scaling this matrix will be the rotation part of the matrix.
  166. */
  167. void singularValueDecomposition(Matrix3& matL, Vector3& matS, Matrix3& matR) const;
  168. /**
  169. * Decomposes the matrix into a set of values.
  170. *
  171. * @param[out] matQ Columns form orthonormal bases. If your matrix is affine and
  172. * doesn't use non-uniform scaling this matrix will be the rotation part of the matrix.
  173. * @param[out] vecD If the matrix is affine these will be scaling factors of the matrix.
  174. * @param[out] vecU If the matrix is affine these will be shear factors of the matrix.
  175. */
  176. void QDUDecomposition(Matrix3& matQ, Vector3& vecD, Vector3& vecU) const;
  177. /** Gram-Schmidt orthonormalization (applied to columns of rotation matrix) */
  178. void orthonormalize();
  179. /**
  180. * Converts an orthonormal matrix to axis angle representation.
  181. *
  182. * @note Matrix must be orthonormal.
  183. */
  184. void toAxisAngle(Vector3& axis, Radian& angle) const;
  185. /** Creates a rotation matrix from an axis angle representation. */
  186. void fromAxisAngle(const Vector3& axis, const Radian& angle);
  187. /**
  188. * Converts an orthonormal matrix to quaternion representation.
  189. *
  190. * @note Matrix must be orthonormal.
  191. */
  192. void toQuaternion(Quaternion& quat) const;
  193. /** Creates a rotation matrix from a quaternion representation. */
  194. void fromQuaternion(const Quaternion& quat);
  195. /** Creates a matrix from a three axes. */
  196. void fromAxes(const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis);
  197. /**
  198. * Converts an orthonormal matrix to euler angle (pitch/yaw/roll) representation.
  199. *
  200. * @param[in,out] xAngle Rotation about x axis. (AKA Pitch)
  201. * @param[in,out] yAngle Rotation about y axis. (AKA Yaw)
  202. * @param[in,out] zAngle Rotation about z axis. (AKA Roll)
  203. * @return True if unique solution was found, false otherwise.
  204. *
  205. * @note Matrix must be orthonormal.
  206. */
  207. bool toEulerAngles(Radian& xAngle, Radian& yAngle, Radian& zAngle) const;
  208. /**
  209. * Creates a rotation matrix from the provided Pitch/Yaw/Roll angles.
  210. *
  211. * @param[in] xAngle Rotation about x axis. (AKA Pitch)
  212. * @param[in] yAngle Rotation about y axis. (AKA Yaw)
  213. * @param[in] zAngle Rotation about z axis. (AKA Roll)
  214. *
  215. * @note Matrix must be orthonormal.
  216. * Since different values will be produced depending in which order are the rotations applied, this method assumes
  217. * they are applied in YXZ order. If you need a specific order, use the overloaded "fromEulerAngles" method instead.
  218. */
  219. void fromEulerAngles(const Radian& xAngle, const Radian& yAngle, const Radian& zAngle);
  220. /**
  221. * Creates a rotation matrix from the provided Pitch/Yaw/Roll angles.
  222. *
  223. * @param[in] xAngle Rotation about x axis. (AKA Pitch)
  224. * @param[in] yAngle Rotation about y axis. (AKA Yaw)
  225. * @param[in] zAngle Rotation about z axis. (AKA Roll)
  226. * @param[in] order The order in which rotations will be applied.
  227. * Different rotations can be created depending on the order.
  228. *
  229. * @note Matrix must be orthonormal.
  230. */
  231. void fromEulerAngles(const Radian& xAngle, const Radian& yAngle, const Radian& zAngle, EulerAngleOrder order);
  232. /**
  233. * Eigensolver, matrix must be symmetric.
  234. *
  235. * @note
  236. * Eigenvectors are vectors which when transformed by the matrix, only change in magnitude, but not in direction.
  237. * Eigenvalue is that magnitude. In other words you will get the same result whether you multiply the vector by the
  238. * matrix or by its eigenvalue.
  239. */
  240. void eigenSolveSymmetric(float eigenValues[3], Vector3 eigenVectors[3]) const;
  241. static const float EPSILON;
  242. static const Matrix3 ZERO;
  243. static const Matrix3 IDENTITY;
  244. protected:
  245. friend class Matrix4;
  246. // Support for eigensolver
  247. void tridiagonal (float diag[3], float subDiag[3]);
  248. bool QLAlgorithm (float diag[3], float subDiag[3]);
  249. // Support for singular value decomposition
  250. static const float SVD_EPSILON;
  251. static const unsigned int SVD_MAX_ITERS;
  252. static void bidiagonalize (Matrix3& matA, Matrix3& matL, Matrix3& matR);
  253. static void golubKahanStep (Matrix3& matA, Matrix3& matL, Matrix3& matR);
  254. // Euler angle conversions
  255. static const EulerAngleOrderData EA_LOOKUP[6];
  256. float m[3][3];
  257. };
  258. /** @} */
  259. /** @cond SPECIALIZATIONS */
  260. BS_ALLOW_MEMCPY_SERIALIZATION(Matrix3);
  261. /** @endcond */
  262. }