matrix4.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #pragma once
  2. #include "vector4.h"
  3. // 4x4 matrix with elements of type T
  4. template <typename T>
  5. class Matrix4 {
  6. public:
  7. // Vectors comprising the rows of the matrix
  8. Vector4<T> a, b, c,d;
  9. // trivial ctor
  10. // note that the Vector3 ctor will zero the vector elements
  11. constexpr Matrix4<T>() {}
  12. // setting ctor
  13. constexpr Matrix4<T>(const Vector4<T> &a0, const Vector4<T> &b0, const Vector4<T> &c0, const Vector4<T> &d0)
  14. : a(a0)
  15. , b(b0)
  16. , c(c0)
  17. , d(d0){}
  18. // setting ctor
  19. // M00, M01, M02, M03
  20. // M10, M11, M12, M13
  21. // M20, M21, M22, M23
  22. // M30, M31, M32, M33
  23. constexpr Matrix4<T>(const T M00, const T M01, const T M02,const T M03,
  24. const T M10, const T M11, const T M12,const T M13,
  25. const T M20, const T M21, const T M22,const T M23,
  26. const T M30, const T M31, const T M32,const T M33)
  27. : a(M00, M01, M02, M03)
  28. , b(M10, M11, M12, M13)
  29. , c(M20, M21, M22, M23)
  30. , d(M30, M31, M32, M33)
  31. {}
  32. // function call operator
  33. void operator () (const Vector4<T> &a0, const Vector4<T> &b0, const Vector4<T> &c0, const Vector4<T> &d0)
  34. {
  35. a = a0; b = b0; c = c0; d = d0;
  36. }
  37. // test for equality
  38. bool operator == (const Matrix4<T> &m)
  39. {
  40. return (a==m.a && b==m.b && c==m.c && d==m.d);
  41. }
  42. // test for inequality
  43. bool operator != (const Matrix4<T> &m)
  44. {
  45. return (a!=m.a || b!=m.b || c!=m.c|| d!=m.d);
  46. }
  47. // negation
  48. Matrix4<T> operator - (void) const
  49. {
  50. return Matrix4<T>(-a,-b,-c,-d);
  51. }
  52. // addition
  53. Matrix4<T> operator + (const Matrix4<T> &m) const
  54. {
  55. return Matrix4<T>(a+m.a, b+m.b, c+m.c, d+m.d);
  56. }
  57. Matrix4<T> &operator += (const Matrix4<T> &m)
  58. {
  59. return *this = *this + m;
  60. }
  61. // subtraction
  62. Matrix4<T> operator - (const Matrix4<T> &m) const
  63. {
  64. return Matrix3<T>(a-m.a, b-m.b, c-m.c,d-m.d);
  65. }
  66. Matrix4<T> &operator -= (const Matrix4<T> &m)
  67. {
  68. return *this = *this - m;
  69. }
  70. // uniform scaling
  71. Matrix4<T> operator * (const T num) const
  72. {
  73. return Matrix4<T>(a*num, b*num, c*num, d*num);
  74. }
  75. Matrix4<T> &operator *= (const T num)
  76. {
  77. return *this = *this * num;
  78. }
  79. Matrix4<T> operator / (const T num) const
  80. {
  81. return Matrix4<T>(a/num, b/num, c/num, d/num);
  82. }
  83. Matrix4<T> &operator /= (const T num)
  84. {
  85. return *this = *this / num;
  86. }
  87. Vector4<T> operator *(const Vector4<T> &v) const;
  88. /**
  89. * Calculate the determinant of this matrix.
  90. *
  91. * @return The value of the determinant.
  92. */
  93. T det() const;
  94. /**
  95. * Calculate the inverse of this matrix.
  96. *
  97. * @param inv[in] Where to store the result.
  98. *
  99. * @return If this matrix is invertible, then true is returned. Otherwise,
  100. * \p inv is unmodified and false is returned.
  101. */
  102. bool inverse(Matrix4<T>& inv) const;
  103. // check if any elements are NAN
  104. bool is_nan(void) WARN_IF_UNUSED
  105. {
  106. return a.is_nan() || b.is_nan() || c.is_nan();
  107. }
  108. void zero(void);
  109. void normalize(void);
  110. };
  111. typedef Matrix4<int16_t> Matrix4i;
  112. typedef Matrix4<uint16_t> Matrix4ui;
  113. typedef Matrix4<int32_t> Matrix4l;
  114. typedef Matrix4<uint32_t> Matrix4ul;
  115. typedef Matrix4<float> Matrix4f;