matrix_inverse.inl 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /// @ref gtc_matrix_inverse
  2. namespace glm
  3. {
  4. template<typename T, qualifier Q>
  5. GLM_FUNC_QUALIFIER mat<3, 3, T, Q> affineInverse(mat<3, 3, T, Q> const& m)
  6. {
  7. mat<2, 2, T, Q> const Inv(inverse(mat<2, 2, T, Q>(m)));
  8. return mat<3, 3, T, Q>(
  9. vec<3, T, Q>(Inv[0], static_cast<T>(0)),
  10. vec<3, T, Q>(Inv[1], static_cast<T>(0)),
  11. vec<3, T, Q>(-Inv * vec<2, T, Q>(m[2]), static_cast<T>(1)));
  12. }
  13. template<typename T, qualifier Q>
  14. GLM_FUNC_QUALIFIER mat<4, 4, T, Q> affineInverse(mat<4, 4, T, Q> const& m)
  15. {
  16. mat<3, 3, T, Q> const Inv(inverse(mat<3, 3, T, Q>(m)));
  17. return mat<4, 4, T, Q>(
  18. vec<4, T, Q>(Inv[0], static_cast<T>(0)),
  19. vec<4, T, Q>(Inv[1], static_cast<T>(0)),
  20. vec<4, T, Q>(Inv[2], static_cast<T>(0)),
  21. vec<4, T, Q>(-Inv * vec<3, T, Q>(m[3]), static_cast<T>(1)));
  22. }
  23. template<typename T, qualifier Q>
  24. GLM_FUNC_QUALIFIER mat<2, 2, T, Q> inverseTranspose(mat<2, 2, T, Q> const& m)
  25. {
  26. T Determinant = m[0][0] * m[1][1] - m[1][0] * m[0][1];
  27. mat<2, 2, T, Q> Inverse(
  28. + m[1][1] / Determinant,
  29. - m[0][1] / Determinant,
  30. - m[1][0] / Determinant,
  31. + m[0][0] / Determinant);
  32. return Inverse;
  33. }
  34. template<typename T, qualifier Q>
  35. GLM_FUNC_QUALIFIER mat<3, 3, T, Q> inverseTranspose(mat<3, 3, T, Q> const& m)
  36. {
  37. T Determinant =
  38. + m[0][0] * (m[1][1] * m[2][2] - m[1][2] * m[2][1])
  39. - m[0][1] * (m[1][0] * m[2][2] - m[1][2] * m[2][0])
  40. + m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0]);
  41. mat<3, 3, T, Q> Inverse;
  42. Inverse[0][0] = + (m[1][1] * m[2][2] - m[2][1] * m[1][2]);
  43. Inverse[0][1] = - (m[1][0] * m[2][2] - m[2][0] * m[1][2]);
  44. Inverse[0][2] = + (m[1][0] * m[2][1] - m[2][0] * m[1][1]);
  45. Inverse[1][0] = - (m[0][1] * m[2][2] - m[2][1] * m[0][2]);
  46. Inverse[1][1] = + (m[0][0] * m[2][2] - m[2][0] * m[0][2]);
  47. Inverse[1][2] = - (m[0][0] * m[2][1] - m[2][0] * m[0][1]);
  48. Inverse[2][0] = + (m[0][1] * m[1][2] - m[1][1] * m[0][2]);
  49. Inverse[2][1] = - (m[0][0] * m[1][2] - m[1][0] * m[0][2]);
  50. Inverse[2][2] = + (m[0][0] * m[1][1] - m[1][0] * m[0][1]);
  51. Inverse /= Determinant;
  52. return Inverse;
  53. }
  54. template<typename T, qualifier Q>
  55. GLM_FUNC_QUALIFIER mat<4, 4, T, Q> inverseTranspose(mat<4, 4, T, Q> const& m)
  56. {
  57. T SubFactor00 = m[2][2] * m[3][3] - m[3][2] * m[2][3];
  58. T SubFactor01 = m[2][1] * m[3][3] - m[3][1] * m[2][3];
  59. T SubFactor02 = m[2][1] * m[3][2] - m[3][1] * m[2][2];
  60. T SubFactor03 = m[2][0] * m[3][3] - m[3][0] * m[2][3];
  61. T SubFactor04 = m[2][0] * m[3][2] - m[3][0] * m[2][2];
  62. T SubFactor05 = m[2][0] * m[3][1] - m[3][0] * m[2][1];
  63. T SubFactor06 = m[1][2] * m[3][3] - m[3][2] * m[1][3];
  64. T SubFactor07 = m[1][1] * m[3][3] - m[3][1] * m[1][3];
  65. T SubFactor08 = m[1][1] * m[3][2] - m[3][1] * m[1][2];
  66. T SubFactor09 = m[1][0] * m[3][3] - m[3][0] * m[1][3];
  67. T SubFactor10 = m[1][0] * m[3][2] - m[3][0] * m[1][2];
  68. T SubFactor11 = m[1][0] * m[3][1] - m[3][0] * m[1][1];
  69. T SubFactor12 = m[1][2] * m[2][3] - m[2][2] * m[1][3];
  70. T SubFactor13 = m[1][1] * m[2][3] - m[2][1] * m[1][3];
  71. T SubFactor14 = m[1][1] * m[2][2] - m[2][1] * m[1][2];
  72. T SubFactor15 = m[1][0] * m[2][3] - m[2][0] * m[1][3];
  73. T SubFactor16 = m[1][0] * m[2][2] - m[2][0] * m[1][2];
  74. T SubFactor17 = m[1][0] * m[2][1] - m[2][0] * m[1][1];
  75. mat<4, 4, T, Q> Inverse;
  76. Inverse[0][0] = + (m[1][1] * SubFactor00 - m[1][2] * SubFactor01 + m[1][3] * SubFactor02);
  77. Inverse[0][1] = - (m[1][0] * SubFactor00 - m[1][2] * SubFactor03 + m[1][3] * SubFactor04);
  78. Inverse[0][2] = + (m[1][0] * SubFactor01 - m[1][1] * SubFactor03 + m[1][3] * SubFactor05);
  79. Inverse[0][3] = - (m[1][0] * SubFactor02 - m[1][1] * SubFactor04 + m[1][2] * SubFactor05);
  80. Inverse[1][0] = - (m[0][1] * SubFactor00 - m[0][2] * SubFactor01 + m[0][3] * SubFactor02);
  81. Inverse[1][1] = + (m[0][0] * SubFactor00 - m[0][2] * SubFactor03 + m[0][3] * SubFactor04);
  82. Inverse[1][2] = - (m[0][0] * SubFactor01 - m[0][1] * SubFactor03 + m[0][3] * SubFactor05);
  83. Inverse[1][3] = + (m[0][0] * SubFactor02 - m[0][1] * SubFactor04 + m[0][2] * SubFactor05);
  84. Inverse[2][0] = + (m[0][1] * SubFactor06 - m[0][2] * SubFactor07 + m[0][3] * SubFactor08);
  85. Inverse[2][1] = - (m[0][0] * SubFactor06 - m[0][2] * SubFactor09 + m[0][3] * SubFactor10);
  86. Inverse[2][2] = + (m[0][0] * SubFactor07 - m[0][1] * SubFactor09 + m[0][3] * SubFactor11);
  87. Inverse[2][3] = - (m[0][0] * SubFactor08 - m[0][1] * SubFactor10 + m[0][2] * SubFactor11);
  88. Inverse[3][0] = - (m[0][1] * SubFactor12 - m[0][2] * SubFactor13 + m[0][3] * SubFactor14);
  89. Inverse[3][1] = + (m[0][0] * SubFactor12 - m[0][2] * SubFactor15 + m[0][3] * SubFactor16);
  90. Inverse[3][2] = - (m[0][0] * SubFactor13 - m[0][1] * SubFactor15 + m[0][3] * SubFactor17);
  91. Inverse[3][3] = + (m[0][0] * SubFactor14 - m[0][1] * SubFactor16 + m[0][2] * SubFactor17);
  92. T Determinant =
  93. + m[0][0] * Inverse[0][0]
  94. + m[0][1] * Inverse[0][1]
  95. + m[0][2] * Inverse[0][2]
  96. + m[0][3] * Inverse[0][3];
  97. Inverse /= Determinant;
  98. return Inverse;
  99. }
  100. }//namespace glm