matrix4.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * matrix3.cpp
  3. * Copyright (C) Andrew Tridgell 2012
  4. *
  5. * This file is free software: you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This file is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #pragma GCC optimize("O2")
  19. #include "AP_Math.h"
  20. // multiplication by a vector
  21. template <typename T>
  22. Vector4<T> Matrix4<T>::operator *(const Vector4<T> &v) const
  23. {
  24. return Vector4<T>(a.M0 * v.M0 + a.M1 * v.M1 + a.M2 * v.M2 + a.M3 * v.M3,
  25. b.M0 * v.M0 + b.M1 * v.M1 + b.M2 * v.M2 + b.M3 * v.M3,
  26. c.M0 * v.M0 + c.M1 * v.M1 + c.M2 * v.M2 + c.M3 * v.M3,
  27. d.M0 * v.M0 + d.M1 * v.M1 + d.M2 * v.M2 + d.M3 * v.M3);
  28. }
  29. template <typename T>
  30. T Matrix4<T>::det() const
  31. {
  32. Matrix3<T> m11(b.M1, b.M2, b.M3,
  33. c.M1, c.M2, c.M3,
  34. d.M1, d.M2, d.M3);
  35. Matrix3<T> m12(b.M0, b.M2, b.M3,
  36. c.M0, c.M2, c.M3,
  37. d.M0, d.M2, d.M3);
  38. Matrix3<T> m13(b.M0, b.M1, b.M3,
  39. c.M0, c.M1, c.M3,
  40. d.M0, d.M1, d.M3);
  41. Matrix3<T> m14(b.M0, b.M1, b.M2,
  42. c.M0, c.M1, c.M2,
  43. d.M0, d.M1, d.M2);
  44. T Matrix_det = a.M0*(m11.det())
  45. -a.M1*(m12.det())
  46. +a.M2*(m13.det())
  47. -a.M3*(m14.det());
  48. return Matrix_det;
  49. }
  50. template <typename T>
  51. bool Matrix4<T>::inverse(Matrix4<T>& inv) const
  52. {
  53. const T d_val = det();
  54. if (is_zero(d_val)) {
  55. return false;
  56. }
  57. //1 lie
  58. Matrix3<T> m11(b.M1, b.M2, b.M3,
  59. c.M1, c.M2, c.M3,
  60. d.M1, d.M2, d.M3);
  61. inv.a.M0 = m11.det()/d_val;
  62. Matrix3<T> m12(b.M0, b.M2, b.M3,
  63. c.M0, c.M2, c.M3,
  64. d.M0, d.M2, d.M3);
  65. inv.b.M0 = -m12.det()/d_val;
  66. Matrix3<T> m13(b.M0, b.M1, b.M3,
  67. c.M0, c.M1, c.M3,
  68. d.M0, d.M1, d.M3);
  69. inv.c.M0 = m13.det()/d_val;
  70. Matrix3<T> m14(b.M0, b.M1, b.M2,
  71. c.M0, c.M1, c.M2,
  72. d.M0, d.M1, d.M2);
  73. inv.d.M0 = -m14.det()/d_val;
  74. //2 lie----------------
  75. Matrix3<T> m21(a.M1, a.M2, a.M3,
  76. c.M1, c.M2, c.M3,
  77. d.M1, d.M2, d.M3);
  78. inv.a.M1 = -m21.det()/d_val;
  79. Matrix3<T> m22(a.M0, a.M2, a.M3,
  80. c.M0, c.M2, c.M3,
  81. d.M0, d.M2, d.M3);
  82. inv.b.M1 = m22.det()/d_val;
  83. Matrix3<T> m23(a.M0, a.M1, a.M3,
  84. c.M0, c.M1, c.M3,
  85. d.M0, d.M1, d.M3);
  86. inv.c.M1 = -m23.det()/d_val;
  87. Matrix3<T> m24(a.M0, a.M1, a.M2,
  88. c.M0, c.M1, c.M2,
  89. d.M0, d.M1, d.M2);
  90. inv.d.M1 = m24.det()/d_val;
  91. //3 lie---------------------
  92. Matrix3<T> m31(a.M1, a.M2, a.M3,
  93. b.M1, b.M2, b.M3,
  94. d.M1, d.M2, d.M3);
  95. inv.a.M2 = m31.det()/d_val;
  96. Matrix3<T> m32(a.M0, a.M2, a.M3,
  97. b.M0, b.M2, b.M3,
  98. d.M0, d.M2, d.M3);
  99. inv.b.M2 = -m32.det()/d_val;
  100. Matrix3<T> m33(a.M0, a.M1, a.M3,
  101. b.M0, b.M1, b.M3,
  102. d.M0, d.M1, d.M3);
  103. inv.c.M2 = m33.det()/d_val;
  104. Matrix3<T> m34(a.M0, a.M1, a.M2,
  105. b.M0, b.M1, b.M2,
  106. d.M0, d.M1, d.M2);
  107. inv.d.M2 = -m34.det()/d_val;
  108. //4 lie---------------------
  109. Matrix3<T> m41(a.M1, a.M2, a.M3,
  110. b.M1, b.M2, b.M3,
  111. c.M1, c.M2, c.M3);
  112. inv.a.M3 = -m41.det()/d_val;
  113. Matrix3<T> m42(a.M0, a.M2, a.M3,
  114. b.M0, b.M2, b.M3,
  115. c.M0, c.M2, c.M3);
  116. inv.b.M3 = m42.det()/d_val;
  117. Matrix3<T> m43(a.M0, a.M1, a.M3,
  118. b.M0, b.M1, b.M3,
  119. c.M0, c.M1, c.M3);
  120. inv.c.M3 = -m43.det()/d_val;
  121. Matrix3<T> m44(a.M0, a.M1, a.M2,
  122. b.M0, b.M1, b.M2,
  123. c.M0, c.M1, c.M2);
  124. inv.d.M3 = m44.det()/d_val;
  125. return true;
  126. }
  127. template <typename T>
  128. void Matrix4<T>::zero(void)
  129. {
  130. a.M0 = a.M1 = a.M2 = a.M3 = 0;
  131. b.M0 = b.M1 = b.M2 = b.M3 = 0;
  132. c.M0 = c.M1 = c.M2 = c.M3 = 0;
  133. d.M0 = d.M1 = d.M2 = d.M3 = 0;
  134. }
  135. // only define for float
  136. template void Matrix4<float>::zero(void);
  137. template Vector4<float> Matrix4<float>::operator *(const Vector4<float> &v) const;
  138. template float Matrix4<float>::det() const;
  139. template bool Matrix4<float>::inverse(Matrix4<float>& inv) const;