You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
6.5 KiB

5 years ago
  1. ///////////////////////////////////////////////////////////////////////////////////
  2. /// OpenGL Mathematics (glm.g-truc.net)
  3. ///
  4. /// Copyright (c) 2005 - 2013 G-Truc Creation (www.g-truc.net)
  5. /// Permission is hereby granted, free of charge, to any person obtaining a copy
  6. /// of this software and associated documentation files (the "Software"), to deal
  7. /// in the Software without restriction, including without limitation the rights
  8. /// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. /// copies of the Software, and to permit persons to whom the Software is
  10. /// furnished to do so, subject to the following conditions:
  11. ///
  12. /// The above copyright notice and this permission notice shall be included in
  13. /// all copies or substantial portions of the Software.
  14. ///
  15. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. /// THE SOFTWARE.
  22. ///
  23. /// @ref gtc_matrix_inverse
  24. /// @file glm/gtc/matrix_inverse.inl
  25. /// @date 2005-12-21 / 2011-06-15
  26. /// @author Christophe Riccio
  27. ///////////////////////////////////////////////////////////////////////////////////
  28. namespace glm
  29. {
  30. template <typename T>
  31. GLM_FUNC_QUALIFIER detail::tmat3x3<T> affineInverse
  32. (
  33. detail::tmat3x3<T> const & m
  34. )
  35. {
  36. detail::tmat3x3<T> Result(m);
  37. Result[2] = detail::tvec3<T>(0, 0, 1);
  38. Result = transpose(Result);
  39. detail::tvec3<T> Translation = Result * detail::tvec3<T>(-detail::tvec2<T>(m[2]), m[2][2]);
  40. Result[2] = Translation;
  41. return Result;
  42. }
  43. template <typename T>
  44. GLM_FUNC_QUALIFIER detail::tmat4x4<T> affineInverse
  45. (
  46. detail::tmat4x4<T> const & m
  47. )
  48. {
  49. detail::tmat4x4<T> Result(m);
  50. Result[3] = detail::tvec4<T>(0, 0, 0, 1);
  51. Result = transpose(Result);
  52. detail::tvec4<T> Translation = Result * detail::tvec4<T>(-detail::tvec3<T>(m[3]), m[3][3]);
  53. Result[3] = Translation;
  54. return Result;
  55. }
  56. template <typename valType>
  57. GLM_FUNC_QUALIFIER detail::tmat2x2<valType> inverseTranspose
  58. (
  59. detail::tmat2x2<valType> const & m
  60. )
  61. {
  62. valType Determinant = m[0][0] * m[1][1] - m[1][0] * m[0][1];
  63. detail::tmat2x2<valType> Inverse(
  64. + m[1][1] / Determinant,
  65. - m[0][1] / Determinant,
  66. - m[1][0] / Determinant,
  67. + m[0][0] / Determinant);
  68. return Inverse;
  69. }
  70. template <typename valType>
  71. GLM_FUNC_QUALIFIER detail::tmat3x3<valType> inverseTranspose
  72. (
  73. detail::tmat3x3<valType> const & m
  74. )
  75. {
  76. valType Determinant =
  77. + m[0][0] * (m[1][1] * m[2][2] - m[1][2] * m[2][1])
  78. - m[0][1] * (m[1][0] * m[2][2] - m[1][2] * m[2][0])
  79. + m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0]);
  80. detail::tmat3x3<valType> Inverse;
  81. Inverse[0][0] = + (m[1][1] * m[2][2] - m[2][1] * m[1][2]);
  82. Inverse[0][1] = - (m[1][0] * m[2][2] - m[2][0] * m[1][2]);
  83. Inverse[0][2] = + (m[1][0] * m[2][1] - m[2][0] * m[1][1]);
  84. Inverse[1][0] = - (m[0][1] * m[2][2] - m[2][1] * m[0][2]);
  85. Inverse[1][1] = + (m[0][0] * m[2][2] - m[2][0] * m[0][2]);
  86. Inverse[1][2] = - (m[0][0] * m[2][1] - m[2][0] * m[0][1]);
  87. Inverse[2][0] = + (m[0][1] * m[1][2] - m[1][1] * m[0][2]);
  88. Inverse[2][1] = - (m[0][0] * m[1][2] - m[1][0] * m[0][2]);
  89. Inverse[2][2] = + (m[0][0] * m[1][1] - m[1][0] * m[0][1]);
  90. Inverse /= Determinant;
  91. return Inverse;
  92. }
  93. template <typename valType>
  94. GLM_FUNC_QUALIFIER detail::tmat4x4<valType> inverseTranspose
  95. (
  96. detail::tmat4x4<valType> const & m
  97. )
  98. {
  99. valType SubFactor00 = m[2][2] * m[3][3] - m[3][2] * m[2][3];
  100. valType SubFactor01 = m[2][1] * m[3][3] - m[3][1] * m[2][3];
  101. valType SubFactor02 = m[2][1] * m[3][2] - m[3][1] * m[2][2];
  102. valType SubFactor03 = m[2][0] * m[3][3] - m[3][0] * m[2][3];
  103. valType SubFactor04 = m[2][0] * m[3][2] - m[3][0] * m[2][2];
  104. valType SubFactor05 = m[2][0] * m[3][1] - m[3][0] * m[2][1];
  105. valType SubFactor06 = m[1][2] * m[3][3] - m[3][2] * m[1][3];
  106. valType SubFactor07 = m[1][1] * m[3][3] - m[3][1] * m[1][3];
  107. valType SubFactor08 = m[1][1] * m[3][2] - m[3][1] * m[1][2];
  108. valType SubFactor09 = m[1][0] * m[3][3] - m[3][0] * m[1][3];
  109. valType SubFactor10 = m[1][0] * m[3][2] - m[3][0] * m[1][2];
  110. valType SubFactor11 = m[1][1] * m[3][3] - m[3][1] * m[1][3];
  111. valType SubFactor12 = m[1][0] * m[3][1] - m[3][0] * m[1][1];
  112. valType SubFactor13 = m[1][2] * m[2][3] - m[2][2] * m[1][3];
  113. valType SubFactor14 = m[1][1] * m[2][3] - m[2][1] * m[1][3];
  114. valType SubFactor15 = m[1][1] * m[2][2] - m[2][1] * m[1][2];
  115. valType SubFactor16 = m[1][0] * m[2][3] - m[2][0] * m[1][3];
  116. valType SubFactor17 = m[1][0] * m[2][2] - m[2][0] * m[1][2];
  117. valType SubFactor18 = m[1][0] * m[2][1] - m[2][0] * m[1][1];
  118. detail::tmat4x4<valType> Inverse;
  119. Inverse[0][0] = + (m[1][1] * SubFactor00 - m[1][2] * SubFactor01 + m[1][3] * SubFactor02);
  120. Inverse[0][1] = - (m[1][0] * SubFactor00 - m[1][2] * SubFactor03 + m[1][3] * SubFactor04);
  121. Inverse[0][2] = + (m[1][0] * SubFactor01 - m[1][1] * SubFactor03 + m[1][3] * SubFactor05);
  122. Inverse[0][3] = - (m[1][0] * SubFactor02 - m[1][1] * SubFactor04 + m[1][2] * SubFactor05);
  123. Inverse[1][0] = - (m[0][1] * SubFactor00 - m[0][2] * SubFactor01 + m[0][3] * SubFactor02);
  124. Inverse[1][1] = + (m[0][0] * SubFactor00 - m[0][2] * SubFactor03 + m[0][3] * SubFactor04);
  125. Inverse[1][2] = - (m[0][0] * SubFactor01 - m[0][1] * SubFactor03 + m[0][3] * SubFactor05);
  126. Inverse[1][3] = + (m[0][0] * SubFactor02 - m[0][1] * SubFactor04 + m[0][2] * SubFactor05);
  127. Inverse[2][0] = + (m[0][1] * SubFactor06 - m[0][2] * SubFactor07 + m[0][3] * SubFactor08);
  128. Inverse[2][1] = - (m[0][0] * SubFactor06 - m[0][2] * SubFactor09 + m[0][3] * SubFactor10);
  129. Inverse[2][2] = + (m[0][0] * SubFactor11 - m[0][1] * SubFactor09 + m[0][3] * SubFactor12);
  130. Inverse[2][3] = - (m[0][0] * SubFactor08 - m[0][1] * SubFactor10 + m[0][2] * SubFactor12);
  131. Inverse[3][0] = - (m[0][1] * SubFactor13 - m[0][2] * SubFactor14 + m[0][3] * SubFactor15);
  132. Inverse[3][1] = + (m[0][0] * SubFactor13 - m[0][2] * SubFactor16 + m[0][3] * SubFactor17);
  133. Inverse[3][2] = - (m[0][0] * SubFactor14 - m[0][1] * SubFactor16 + m[0][3] * SubFactor18);
  134. Inverse[3][3] = + (m[0][0] * SubFactor15 - m[0][1] * SubFactor17 + m[0][2] * SubFactor18);
  135. valType Determinant =
  136. + m[0][0] * Inverse[0][0]
  137. + m[0][1] * Inverse[0][1]
  138. + m[0][2] * Inverse[0][2]
  139. + m[0][3] * Inverse[0][3];
  140. Inverse /= Determinant;
  141. return Inverse;
  142. }
  143. }//namespace glm