matrix3x3.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2016, 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 matrix3x3.h
  35. * @brief Definition of a 3x3 matrix, including operators when compiling in C++
  36. */
  37. #pragma once
  38. #ifndef AI_MATRIX3X3_H_INC
  39. #define AI_MATRIX3X3_H_INC
  40. #include "defs.h"
  41. #ifdef __cplusplus
  42. template <typename T> class aiMatrix4x4t;
  43. template <typename T> class aiVector2t;
  44. // ---------------------------------------------------------------------------
  45. /** @brief Represents a row-major 3x3 matrix
  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 aiMatrix3x3t
  55. {
  56. public:
  57. aiMatrix3x3t () :
  58. a1(static_cast<TReal>(1.0f)), a2(), a3(),
  59. b1(), b2(static_cast<TReal>(1.0f)), b3(),
  60. c1(), c2(), c3(static_cast<TReal>(1.0f)) {}
  61. aiMatrix3x3t ( TReal _a1, TReal _a2, TReal _a3,
  62. TReal _b1, TReal _b2, TReal _b3,
  63. TReal _c1, TReal _c2, TReal _c3) :
  64. a1(_a1), a2(_a2), a3(_a3),
  65. b1(_b1), b2(_b2), b3(_b3),
  66. c1(_c1), c2(_c2), c3(_c3)
  67. {}
  68. public:
  69. // matrix multiplication.
  70. aiMatrix3x3t& operator *= (const aiMatrix3x3t& m);
  71. aiMatrix3x3t operator * (const aiMatrix3x3t& m) const;
  72. // array access operators
  73. TReal* operator[] (unsigned int p_iIndex);
  74. const TReal* operator[] (unsigned int p_iIndex) const;
  75. // comparison operators
  76. bool operator== (const aiMatrix4x4t<TReal>& m) const;
  77. bool operator!= (const aiMatrix4x4t<TReal>& m) const;
  78. bool Equal(const aiMatrix4x4t<TReal>& m, TReal epsilon = 1e-6) const;
  79. template <typename TOther>
  80. operator aiMatrix3x3t<TOther> () const;
  81. public:
  82. // -------------------------------------------------------------------
  83. /** @brief Construction from a 4x4 matrix. The remaining parts
  84. * of the matrix are ignored.
  85. */
  86. explicit aiMatrix3x3t( const aiMatrix4x4t<TReal>& pMatrix);
  87. // -------------------------------------------------------------------
  88. /** @brief Transpose the matrix
  89. */
  90. aiMatrix3x3t& Transpose();
  91. // -------------------------------------------------------------------
  92. /** @brief Invert the matrix.
  93. * If the matrix is not invertible all elements are set to qnan.
  94. * Beware, use (f != f) to check whether a TReal f is qnan.
  95. */
  96. aiMatrix3x3t& Inverse();
  97. TReal Determinant() const;
  98. public:
  99. // -------------------------------------------------------------------
  100. /** @brief Returns a rotation matrix for a rotation around z
  101. * @param a Rotation angle, in radians
  102. * @param out Receives the output matrix
  103. * @return Reference to the output matrix
  104. */
  105. static aiMatrix3x3t& RotationZ(TReal a, aiMatrix3x3t& out);
  106. // -------------------------------------------------------------------
  107. /** @brief Returns a rotation matrix for a rotation around
  108. * an arbitrary axis.
  109. *
  110. * @param a Rotation angle, in radians
  111. * @param axis Axis to rotate around
  112. * @param out To be filled
  113. */
  114. static aiMatrix3x3t& Rotation( TReal a,
  115. const aiVector3t<TReal>& axis, aiMatrix3x3t& out);
  116. // -------------------------------------------------------------------
  117. /** @brief Returns a translation matrix
  118. * @param v Translation vector
  119. * @param out Receives the output matrix
  120. * @return Reference to the output matrix
  121. */
  122. static aiMatrix3x3t& Translation( const aiVector2t<TReal>& v, aiMatrix3x3t& out);
  123. // -------------------------------------------------------------------
  124. /** @brief A function for creating a rotation matrix that rotates a
  125. * vector called "from" into another vector called "to".
  126. * Input : from[3], to[3] which both must be *normalized* non-zero vectors
  127. * Output: mtx[3][3] -- a 3x3 matrix in column-major form
  128. * Authors: Tomas Möller, John Hughes
  129. * "Efficiently Building a Matrix to Rotate One Vector to Another"
  130. * Journal of Graphics Tools, 4(4):1-4, 1999
  131. */
  132. static aiMatrix3x3t& FromToMatrix(const aiVector3t<TReal>& from,
  133. const aiVector3t<TReal>& to, aiMatrix3x3t& out);
  134. public:
  135. TReal a1, a2, a3;
  136. TReal b1, b2, b3;
  137. TReal c1, c2, c3;
  138. };
  139. typedef aiMatrix3x3t<ai_real> aiMatrix3x3;
  140. #else
  141. struct aiMatrix3x3 {
  142. ai_real a1, a2, a3;
  143. ai_real b1, b2, b3;
  144. ai_real c1, c2, c3;
  145. };
  146. #endif // __cplusplus
  147. #endif // AI_MATRIX3X3_H_INC