Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

249 linhas
8.9 KiB

  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. /** construction from position, rotation and scaling components
  67. * @param scaling The scaling for the x,y,z axes
  68. * @param rotation The rotation as a hamilton quaternion
  69. * @param position The position for the x,y,z axes
  70. */
  71. aiMatrix4x4t(const aiVector3t<TReal>& scaling, const aiQuaterniont<TReal>& rotation,
  72. const aiVector3t<TReal>& position);
  73. public:
  74. // array access operators
  75. TReal* operator[] (unsigned int p_iIndex);
  76. const TReal* operator[] (unsigned int p_iIndex) const;
  77. // comparison operators
  78. bool operator== (const aiMatrix4x4t& m) const;
  79. bool operator!= (const aiMatrix4x4t& m) const;
  80. bool Equal(const aiMatrix4x4t& m, TReal epsilon = 1e-6) const;
  81. // matrix multiplication.
  82. aiMatrix4x4t& operator *= (const aiMatrix4x4t& m);
  83. aiMatrix4x4t operator * (const aiMatrix4x4t& m) const;
  84. template <typename TOther>
  85. operator aiMatrix4x4t<TOther> () const;
  86. public:
  87. // -------------------------------------------------------------------
  88. /** @brief Transpose the matrix */
  89. aiMatrix4x4t& Transpose();
  90. // -------------------------------------------------------------------
  91. /** @brief Invert the matrix.
  92. * If the matrix is not invertible all elements are set to qnan.
  93. * Beware, use (f != f) to check whether a TReal f is qnan.
  94. */
  95. aiMatrix4x4t& Inverse();
  96. TReal Determinant() const;
  97. // -------------------------------------------------------------------
  98. /** @brief Returns true of the matrix is the identity matrix.
  99. * The check is performed against a not so small epsilon.
  100. */
  101. inline bool IsIdentity() const;
  102. // -------------------------------------------------------------------
  103. /** @brief Decompose a trafo matrix into its original components
  104. * @param scaling Receives the output scaling for the x,y,z axes
  105. * @param rotation Receives the output rotation as a hamilton
  106. * quaternion
  107. * @param position Receives the output position for the x,y,z axes
  108. */
  109. void Decompose (aiVector3t<TReal>& scaling, aiQuaterniont<TReal>& rotation,
  110. aiVector3t<TReal>& position) const;
  111. // -------------------------------------------------------------------
  112. /** @brief Decompose a trafo matrix with no scaling into its
  113. * original components
  114. * @param rotation Receives the output rotation as a hamilton
  115. * quaternion
  116. * @param position Receives the output position for the x,y,z axes
  117. */
  118. void DecomposeNoScaling (aiQuaterniont<TReal>& rotation,
  119. aiVector3t<TReal>& position) const;
  120. // -------------------------------------------------------------------
  121. /** @brief Creates a trafo matrix from a set of euler angles
  122. * @param x Rotation angle for the x-axis, in radians
  123. * @param y Rotation angle for the y-axis, in radians
  124. * @param z Rotation angle for the z-axis, in radians
  125. */
  126. aiMatrix4x4t& FromEulerAnglesXYZ(TReal x, TReal y, TReal z);
  127. aiMatrix4x4t& FromEulerAnglesXYZ(const aiVector3t<TReal>& blubb);
  128. public:
  129. // -------------------------------------------------------------------
  130. /** @brief Returns a rotation matrix for a rotation around the x axis
  131. * @param a Rotation angle, in radians
  132. * @param out Receives the output matrix
  133. * @return Reference to the output matrix
  134. */
  135. static aiMatrix4x4t& RotationX(TReal a, aiMatrix4x4t& out);
  136. // -------------------------------------------------------------------
  137. /** @brief Returns a rotation matrix for a rotation around the y axis
  138. * @param a Rotation angle, in radians
  139. * @param out Receives the output matrix
  140. * @return Reference to the output matrix
  141. */
  142. static aiMatrix4x4t& RotationY(TReal a, aiMatrix4x4t& out);
  143. // -------------------------------------------------------------------
  144. /** @brief Returns a rotation matrix for a rotation around the z axis
  145. * @param a Rotation angle, in radians
  146. * @param out Receives the output matrix
  147. * @return Reference to the output matrix
  148. */
  149. static aiMatrix4x4t& RotationZ(TReal a, aiMatrix4x4t& out);
  150. // -------------------------------------------------------------------
  151. /** Returns a rotation matrix for a rotation around an arbitrary axis.
  152. * @param a Rotation angle, in radians
  153. * @param axis Rotation axis, should be a normalized vector.
  154. * @param out Receives the output matrix
  155. * @return Reference to the output matrix
  156. */
  157. static aiMatrix4x4t& Rotation(TReal a, const aiVector3t<TReal>& axis,
  158. aiMatrix4x4t& out);
  159. // -------------------------------------------------------------------
  160. /** @brief Returns a translation matrix
  161. * @param v Translation vector
  162. * @param out Receives the output matrix
  163. * @return Reference to the output matrix
  164. */
  165. static aiMatrix4x4t& Translation( const aiVector3t<TReal>& v, aiMatrix4x4t& out);
  166. // -------------------------------------------------------------------
  167. /** @brief Returns a scaling matrix
  168. * @param v Scaling vector
  169. * @param out Receives the output matrix
  170. * @return Reference to the output matrix
  171. */
  172. static aiMatrix4x4t& Scaling( const aiVector3t<TReal>& v, aiMatrix4x4t& out);
  173. // -------------------------------------------------------------------
  174. /** @brief A function for creating a rotation matrix that rotates a
  175. * vector called "from" into another vector called "to".
  176. * Input : from[3], to[3] which both must be *normalized* non-zero vectors
  177. * Output: mtx[3][3] -- a 3x3 matrix in colum-major form
  178. * Authors: Tomas Möller, John Hughes
  179. * "Efficiently Building a Matrix to Rotate One Vector to Another"
  180. * Journal of Graphics Tools, 4(4):1-4, 1999
  181. */
  182. static aiMatrix4x4t& FromToMatrix(const aiVector3t<TReal>& from,
  183. const aiVector3t<TReal>& to, aiMatrix4x4t& out);
  184. public:
  185. TReal a1, a2, a3, a4;
  186. TReal b1, b2, b3, b4;
  187. TReal c1, c2, c3, c4;
  188. TReal d1, d2, d3, d4;
  189. } PACK_STRUCT;
  190. typedef aiMatrix4x4t<float> aiMatrix4x4;
  191. #else
  192. struct aiMatrix4x4 {
  193. float a1, a2, a3, a4;
  194. float b1, b2, b3, b4;
  195. float c1, c2, c3, c4;
  196. float d1, d2, d3, d4;
  197. };
  198. #endif // __cplusplus
  199. #include "./Compiler/poppack1.h"
  200. #endif // AI_MATRIX4X4_H_INC