matrix4x4.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2024, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file matrix4x4.h
  35. * @brief 4x4 matrix structure, including operators when compiling in C++
  36. */
  37. #pragma once
  38. #ifndef AI_MATRIX4X4_H_INC
  39. #define AI_MATRIX4X4_H_INC
  40. #ifdef __GNUC__
  41. # pragma GCC system_header
  42. #endif
  43. #include <assimp/vector3.h>
  44. #include <assimp/defs.h>
  45. #include <assimp/config.h>
  46. #ifdef __cplusplus
  47. template<typename TReal> class aiMatrix3x3t;
  48. template<typename TReal> class aiQuaterniont;
  49. // ---------------------------------------------------------------------------
  50. /** @brief Represents a row-major 4x4 matrix, use this for homogeneous
  51. * coordinates.
  52. *
  53. * There's much confusion about matrix layouts (column vs. row order).
  54. * This is *always* a row-major matrix. Not even with the
  55. * #aiProcess_ConvertToLeftHanded flag, which absolutely does not affect
  56. * matrix order - it just affects the handedness of the coordinate system
  57. * defined thereby.
  58. */
  59. template<typename TReal>
  60. class aiMatrix4x4t {
  61. public:
  62. /**
  63. * @brief Set to identity
  64. * */
  65. aiMatrix4x4t() AI_NO_EXCEPT;
  66. /**
  67. * @brief Construction from single values
  68. * */
  69. aiMatrix4x4t ( TReal _a1, TReal _a2, TReal _a3, TReal _a4,
  70. TReal _b1, TReal _b2, TReal _b3, TReal _b4,
  71. TReal _c1, TReal _c2, TReal _c3, TReal _c4,
  72. TReal _d1, TReal _d2, TReal _d3, TReal _d4);
  73. /** construction from 3x3 matrix, remaining elements are set to identity */
  74. explicit aiMatrix4x4t( const aiMatrix3x3t<TReal>& m);
  75. /** construction from position, rotation and scaling components
  76. * @param scaling The scaling for the x,y,z axes
  77. * @param rotation The rotation as a hamilton quaternion
  78. * @param position The position for the x,y,z axes
  79. */
  80. aiMatrix4x4t(const aiVector3t<TReal>& scaling, const aiQuaterniont<TReal>& rotation,
  81. const aiVector3t<TReal>& position);
  82. // array access operators
  83. /** @fn TReal* operator[] (unsigned int p_iIndex)
  84. * @param [in] p_iIndex - index of the row.
  85. * @return pointer to pointed row.
  86. */
  87. TReal* operator[] (unsigned int p_iIndex);
  88. /** @fn const TReal* operator[] (unsigned int p_iIndex) const
  89. * @overload TReal* operator[] (unsigned int p_iIndex)
  90. */
  91. const TReal* operator[] (unsigned int p_iIndex) const;
  92. // comparison operators
  93. bool operator== (const aiMatrix4x4t& m) const;
  94. bool operator!= (const aiMatrix4x4t& m) const;
  95. bool Equal(const aiMatrix4x4t &m, TReal epsilon = ai_epsilon) const;
  96. // matrix multiplication.
  97. aiMatrix4x4t& operator *= (const aiMatrix4x4t& m);
  98. aiMatrix4x4t operator * (const aiMatrix4x4t& m) const;
  99. aiMatrix4x4t operator * (const TReal& aFloat) const;
  100. aiMatrix4x4t operator + (const aiMatrix4x4t& aMatrix) const;
  101. template <typename TOther>
  102. operator aiMatrix4x4t<TOther> () const;
  103. // -------------------------------------------------------------------
  104. /** @brief Transpose the matrix */
  105. aiMatrix4x4t& Transpose();
  106. // -------------------------------------------------------------------
  107. /** @brief Invert the matrix.
  108. * If the matrix is not invertible all elements are set to qnan.
  109. * Beware, use (f != f) to check whether a TReal f is qnan.
  110. */
  111. aiMatrix4x4t& Inverse();
  112. // -------------------------------------------------------------------
  113. /**
  114. * @brief Inverts the matrix if it is invertible.
  115. */
  116. TReal Determinant() const;
  117. // -------------------------------------------------------------------
  118. /** @brief Returns true of the matrix is the identity matrix.
  119. * @param epsilon Value of epsilon. Default value is 10e-3 for backward
  120. * compatibility with legacy code.
  121. * @return Returns true of the matrix is the identity matrix.
  122. * The check is performed against a not so small epsilon.
  123. */
  124. inline bool IsIdentity(const TReal
  125. epsilon = AI_CONFIG_CHECK_IDENTITY_MATRIX_EPSILON_DEFAULT) const;
  126. // -------------------------------------------------------------------
  127. /** @brief Decompose a trafo matrix into its original components
  128. * @param scaling Receives the output scaling for the x,y,z axes
  129. * @param rotation Receives the output rotation as a hamilton quaternion
  130. * @param position Receives the output position for the x,y,z axes
  131. */
  132. void Decompose (aiVector3t<TReal>& scaling, aiQuaterniont<TReal>& rotation,
  133. aiVector3t<TReal>& position) const;
  134. // -------------------------------------------------------------------
  135. /**
  136. * @brief Decompose a trafo matrix into its original components.
  137. * Thx to good FAQ at http://www.gamedev.ru/code/articles/faq_matrix_quat
  138. * @param [out] pScaling - Receives the output scaling for the x,y,z axes.
  139. * @param [out] pRotation - Receives the output rotation as a Euler angles.
  140. * @param [out] pPosition - Receives the output position for the x,y,z axes.
  141. */
  142. void Decompose(aiVector3t<TReal>& pScaling, aiVector3t<TReal>& pRotation, aiVector3t<TReal>& pPosition) const;
  143. // -------------------------------------------------------------------
  144. /**
  145. * @brief Decompose a trafo matrix into its original components
  146. * Thx to good FAQ at http://www.gamedev.ru/code/articles/faq_matrix_quat
  147. * @param [out] pScaling - Receives the output scaling for the x,y,z axes.
  148. * @param [out] pRotationAxis - Receives the output rotation axis.
  149. * @param [out] pRotationAngle - Receives the output rotation angle for @ref pRotationAxis.
  150. * @param [out] pPosition - Receives the output position for the x,y,z axes.
  151. */
  152. void Decompose(aiVector3t<TReal>& pScaling, aiVector3t<TReal>& pRotationAxis, TReal& pRotationAngle, aiVector3t<TReal>& pPosition) const;
  153. // -------------------------------------------------------------------
  154. /** @brief Decompose a trafo matrix with no scaling into its
  155. * original components
  156. * @param rotation Receives the output rotation as a hamilton quaternion
  157. * @param position Receives the output position for the x,y,z axes
  158. */
  159. void DecomposeNoScaling (aiQuaterniont<TReal>& rotation,
  160. aiVector3t<TReal>& position) const;
  161. // -------------------------------------------------------------------
  162. /** @brief Creates a trafo matrix from a set of euler angles
  163. * @param x Rotation angle for the x-axis, in radians
  164. * @param y Rotation angle for the y-axis, in radians
  165. * @param z Rotation angle for the z-axis, in radians
  166. */
  167. aiMatrix4x4t& FromEulerAnglesXYZ(TReal x, TReal y, TReal z);
  168. aiMatrix4x4t& FromEulerAnglesXYZ(const aiVector3t<TReal>& blubb);
  169. // -------------------------------------------------------------------
  170. /** @brief Returns a rotation matrix for a rotation around the x axis
  171. * @param a Rotation angle, in radians
  172. * @param out Receives the output matrix
  173. * @return Reference to the output matrix
  174. */
  175. static aiMatrix4x4t& RotationX(TReal a, aiMatrix4x4t& out);
  176. // -------------------------------------------------------------------
  177. /** @brief Returns a rotation matrix for a rotation around the y axis
  178. * @param a Rotation angle, in radians
  179. * @param out Receives the output matrix
  180. * @return Reference to the output matrix
  181. */
  182. static aiMatrix4x4t& RotationY(TReal a, aiMatrix4x4t& out);
  183. // -------------------------------------------------------------------
  184. /** @brief Returns a rotation matrix for a rotation around the z axis
  185. * @param a Rotation angle, in radians
  186. * @param out Receives the output matrix
  187. * @return Reference to the output matrix
  188. */
  189. static aiMatrix4x4t& RotationZ(TReal a, aiMatrix4x4t& out);
  190. // -------------------------------------------------------------------
  191. /** Returns a rotation matrix for a rotation around an arbitrary axis.
  192. * @param a Rotation angle, in radians
  193. * @param axis Rotation axis, should be a normalized vector.
  194. * @param out Receives the output matrix
  195. * @return Reference to the output matrix
  196. */
  197. static aiMatrix4x4t& Rotation(TReal a, const aiVector3t<TReal>& axis,
  198. aiMatrix4x4t& out);
  199. // -------------------------------------------------------------------
  200. /** @brief Returns a translation matrix
  201. * @param v Translation vector
  202. * @param out Receives the output matrix
  203. * @return Reference to the output matrix
  204. */
  205. static aiMatrix4x4t& Translation( const aiVector3t<TReal>& v,
  206. aiMatrix4x4t& out);
  207. // -------------------------------------------------------------------
  208. /** @brief Returns a scaling matrix
  209. * @param v Scaling vector
  210. * @param out Receives the output matrix
  211. * @return Reference to the output matrix
  212. */
  213. static aiMatrix4x4t& Scaling( const aiVector3t<TReal>& v, aiMatrix4x4t& out);
  214. // -------------------------------------------------------------------
  215. /** @brief A function for creating a rotation matrix that rotates a
  216. * vector called "from" into another vector called "to".
  217. * Input : from[3], to[3] which both must be *normalized* non-zero vectors
  218. * Output: mtx[3][3] -- a 3x3 matrix in column-major form
  219. * Authors: Tomas Mueller, John Hughes
  220. * "Efficiently Building a Matrix to Rotate One Vector to Another"
  221. * Journal of Graphics Tools, 4(4):1-4, 1999
  222. */
  223. static aiMatrix4x4t& FromToMatrix(const aiVector3t<TReal>& from,
  224. const aiVector3t<TReal>& to, aiMatrix4x4t& out);
  225. TReal a1, a2, a3, a4;
  226. TReal b1, b2, b3, b4;
  227. TReal c1, c2, c3, c4;
  228. TReal d1, d2, d3, d4;
  229. };
  230. typedef aiMatrix4x4t<ai_real> aiMatrix4x4;
  231. #else
  232. struct aiMatrix4x4 {
  233. ai_real a1, a2, a3, a4;
  234. ai_real b1, b2, b3, b4;
  235. ai_real c1, c2, c3, c4;
  236. ai_real d1, d2, d3, d4;
  237. };
  238. #endif // __cplusplus
  239. #endif // AI_MATRIX4X4_H_INC