matrix4x4.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2021, 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. #ifdef __cplusplus
  46. template<typename TReal> class aiMatrix3x3t;
  47. template<typename TReal> class aiQuaterniont;
  48. // ---------------------------------------------------------------------------
  49. /** @brief Represents a row-major 4x4 matrix, use this for homogeneous
  50. * coordinates.
  51. *
  52. * There's much confusion about matrix layouts (column vs. row order).
  53. * This is *always* a row-major matrix. Not even with the
  54. * #aiProcess_ConvertToLeftHanded flag, which absolutely does not affect
  55. * matrix order - it just affects the handedness of the coordinate system
  56. * defined thereby.
  57. */
  58. template<typename TReal>
  59. class aiMatrix4x4t {
  60. public:
  61. /** set to identity */
  62. aiMatrix4x4t() AI_NO_EXCEPT;
  63. /** construction from single values */
  64. aiMatrix4x4t ( TReal _a1, TReal _a2, TReal _a3, TReal _a4,
  65. TReal _b1, TReal _b2, TReal _b3, TReal _b4,
  66. TReal _c1, TReal _c2, TReal _c3, TReal _c4,
  67. TReal _d1, TReal _d2, TReal _d3, TReal _d4);
  68. /** construction from 3x3 matrix, remaining elements are set to identity */
  69. explicit aiMatrix4x4t( const aiMatrix3x3t<TReal>& m);
  70. /** construction from position, rotation and scaling components
  71. * @param scaling The scaling for the x,y,z axes
  72. * @param rotation The rotation as a hamilton quaternion
  73. * @param position The position for the x,y,z axes
  74. */
  75. aiMatrix4x4t(const aiVector3t<TReal>& scaling, const aiQuaterniont<TReal>& rotation,
  76. const aiVector3t<TReal>& position);
  77. // array access operators
  78. /** @fn TReal* operator[] (unsigned int p_iIndex)
  79. * @param [in] p_iIndex - index of the row.
  80. * @return pointer to pointed row.
  81. */
  82. TReal* operator[] (unsigned int p_iIndex);
  83. /** @fn const TReal* operator[] (unsigned int p_iIndex) const
  84. * @overload TReal* operator[] (unsigned int p_iIndex)
  85. */
  86. const TReal* operator[] (unsigned int p_iIndex) const;
  87. // comparison operators
  88. bool operator== (const aiMatrix4x4t& m) const;
  89. bool operator!= (const aiMatrix4x4t& m) const;
  90. bool Equal(const aiMatrix4x4t& m, TReal epsilon = 1e-6) const;
  91. // matrix multiplication.
  92. aiMatrix4x4t& operator *= (const aiMatrix4x4t& m);
  93. aiMatrix4x4t operator * (const aiMatrix4x4t& m) const;
  94. aiMatrix4x4t operator * (const TReal& aFloat) const;
  95. aiMatrix4x4t operator + (const aiMatrix4x4t& aMatrix) const;
  96. template <typename TOther>
  97. operator aiMatrix4x4t<TOther> () const;
  98. // -------------------------------------------------------------------
  99. /** @brief Transpose the matrix */
  100. aiMatrix4x4t& Transpose();
  101. // -------------------------------------------------------------------
  102. /** @brief Invert the matrix.
  103. * If the matrix is not invertible all elements are set to qnan.
  104. * Beware, use (f != f) to check whether a TReal f is qnan.
  105. */
  106. aiMatrix4x4t& Inverse();
  107. TReal Determinant() const;
  108. // -------------------------------------------------------------------
  109. /** @brief Returns true of the matrix is the identity matrix.
  110. * The check is performed against a not so small epsilon.
  111. */
  112. inline bool IsIdentity() const;
  113. // -------------------------------------------------------------------
  114. /** @brief Decompose a trafo matrix into its original components
  115. * @param scaling Receives the output scaling for the x,y,z axes
  116. * @param rotation Receives the output rotation as a hamilton
  117. * quaternion
  118. * @param position Receives the output position for the x,y,z axes
  119. */
  120. void Decompose (aiVector3t<TReal>& scaling, aiQuaterniont<TReal>& rotation,
  121. aiVector3t<TReal>& position) const;
  122. // -------------------------------------------------------------------
  123. /** @fn void Decompose(aiVector3t<TReal>& pScaling, aiVector3t<TReal>& pRotation, aiVector3t<TReal>& pPosition) const
  124. * @brief Decompose a trafo matrix into its original components.
  125. * Thx to good FAQ at http://www.gamedev.ru/code/articles/faq_matrix_quat
  126. * @param [out] pScaling - Receives the output scaling for the x,y,z axes.
  127. * @param [out] pRotation - Receives the output rotation as a Euler angles.
  128. * @param [out] pPosition - Receives the output position for the x,y,z axes.
  129. */
  130. void Decompose(aiVector3t<TReal>& pScaling, aiVector3t<TReal>& pRotation, aiVector3t<TReal>& pPosition) const;
  131. // -------------------------------------------------------------------
  132. /** @fn void Decompose(aiVector3t<TReal>& pScaling, aiVector3t<TReal>& pRotationAxis, TReal& pRotationAngle, aiVector3t<TReal>& pPosition) const
  133. * @brief Decompose a trafo matrix into its original components
  134. * Thx to good FAQ at http://www.gamedev.ru/code/articles/faq_matrix_quat
  135. * @param [out] pScaling - Receives the output scaling for the x,y,z axes.
  136. * @param [out] pRotationAxis - Receives the output rotation axis.
  137. * @param [out] pRotationAngle - Receives the output rotation angle for @ref pRotationAxis.
  138. * @param [out] pPosition - Receives the output position for the x,y,z axes.
  139. */
  140. void Decompose(aiVector3t<TReal>& pScaling, aiVector3t<TReal>& pRotationAxis, TReal& pRotationAngle, aiVector3t<TReal>& pPosition) const;
  141. // -------------------------------------------------------------------
  142. /** @brief Decompose a trafo matrix with no scaling into its
  143. * original components
  144. * @param rotation Receives the output rotation as a hamilton
  145. * quaternion
  146. * @param position Receives the output position for the x,y,z axes
  147. */
  148. void DecomposeNoScaling (aiQuaterniont<TReal>& rotation,
  149. aiVector3t<TReal>& position) const;
  150. // -------------------------------------------------------------------
  151. /** @brief Creates a trafo matrix from a set of euler angles
  152. * @param x Rotation angle for the x-axis, in radians
  153. * @param y Rotation angle for the y-axis, in radians
  154. * @param z Rotation angle for the z-axis, in radians
  155. */
  156. aiMatrix4x4t& FromEulerAnglesXYZ(TReal x, TReal y, TReal z);
  157. aiMatrix4x4t& FromEulerAnglesXYZ(const aiVector3t<TReal>& blubb);
  158. // -------------------------------------------------------------------
  159. /** @brief Returns a rotation matrix for a rotation around the x axis
  160. * @param a Rotation angle, in radians
  161. * @param out Receives the output matrix
  162. * @return Reference to the output matrix
  163. */
  164. static aiMatrix4x4t& RotationX(TReal a, aiMatrix4x4t& out);
  165. // -------------------------------------------------------------------
  166. /** @brief Returns a rotation matrix for a rotation around the y axis
  167. * @param a Rotation angle, in radians
  168. * @param out Receives the output matrix
  169. * @return Reference to the output matrix
  170. */
  171. static aiMatrix4x4t& RotationY(TReal a, aiMatrix4x4t& out);
  172. // -------------------------------------------------------------------
  173. /** @brief Returns a rotation matrix for a rotation around the z axis
  174. * @param a Rotation angle, in radians
  175. * @param out Receives the output matrix
  176. * @return Reference to the output matrix
  177. */
  178. static aiMatrix4x4t& RotationZ(TReal a, aiMatrix4x4t& out);
  179. // -------------------------------------------------------------------
  180. /** Returns a rotation matrix for a rotation around an arbitrary axis.
  181. * @param a Rotation angle, in radians
  182. * @param axis Rotation axis, should be a normalized vector.
  183. * @param out Receives the output matrix
  184. * @return Reference to the output matrix
  185. */
  186. static aiMatrix4x4t& Rotation(TReal a, const aiVector3t<TReal>& axis,
  187. aiMatrix4x4t& out);
  188. // -------------------------------------------------------------------
  189. /** @brief Returns a translation matrix
  190. * @param v Translation vector
  191. * @param out Receives the output matrix
  192. * @return Reference to the output matrix
  193. */
  194. static aiMatrix4x4t& Translation( const aiVector3t<TReal>& v,
  195. aiMatrix4x4t& out);
  196. // -------------------------------------------------------------------
  197. /** @brief Returns a scaling matrix
  198. * @param v Scaling vector
  199. * @param out Receives the output matrix
  200. * @return Reference to the output matrix
  201. */
  202. static aiMatrix4x4t& Scaling( const aiVector3t<TReal>& v, aiMatrix4x4t& out);
  203. // -------------------------------------------------------------------
  204. /** @brief A function for creating a rotation matrix that rotates a
  205. * vector called "from" into another vector called "to".
  206. * Input : from[3], to[3] which both must be *normalized* non-zero vectors
  207. * Output: mtx[3][3] -- a 3x3 matrix in column-major form
  208. * Authors: Tomas Mueller, John Hughes
  209. * "Efficiently Building a Matrix to Rotate One Vector to Another"
  210. * Journal of Graphics Tools, 4(4):1-4, 1999
  211. */
  212. static aiMatrix4x4t& FromToMatrix(const aiVector3t<TReal>& from,
  213. const aiVector3t<TReal>& to, aiMatrix4x4t& out);
  214. TReal a1, a2, a3, a4;
  215. TReal b1, b2, b3, b4;
  216. TReal c1, c2, c3, c4;
  217. TReal d1, d2, d3, d4;
  218. };
  219. typedef aiMatrix4x4t<ai_real> aiMatrix4x4;
  220. #else
  221. struct aiMatrix4x4 {
  222. ai_real a1, a2, a3, a4;
  223. ai_real b1, b2, b3, b4;
  224. ai_real c1, c2, c3, c4;
  225. ai_real d1, d2, d3, d4;
  226. };
  227. #endif // __cplusplus
  228. #endif // AI_MATRIX4X4_H_INC