matrix4x4.h 8.4 KB

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