Mat3.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #ifndef Mat3_H
  2. #define Mat3_H
  3. #include "Vec2.h"
  4. namespace Framework
  5. {
  6. template<typename T>
  7. //! A 3x3 Matrix
  8. class Mat3
  9. {
  10. public:
  11. T elements[3][3]; //! The elements of the matrix
  12. //! Copies the elements of another matrix into this one
  13. //! \param r The other matrix
  14. Mat3& operator=(const Mat3& r)
  15. {
  16. memcpy(elements, r.elements, sizeof(elements));
  17. return *this;
  18. }
  19. //! Multiplies all values with a scalar and stores the result
  20. //! in this matrix \param r The scalar
  21. Mat3& operator*=(const T r)
  22. {
  23. for (T& e : elements)
  24. e *= r;
  25. return *this;
  26. }
  27. //! Multiplies the matrix with another and stores the result
  28. //! in this matrix \param r The other matrix
  29. Mat3& operator*=(const Mat3& r)
  30. {
  31. return *this = *this * r;
  32. }
  33. //! Multiplies all values with a scalar without modifying this matrix
  34. //! \param r The scalar
  35. Mat3 operator*(const T r) const
  36. {
  37. Mat3 result = *this;
  38. return result *= r;
  39. }
  40. //! Multiplies the matrix with another without modifying this matrix
  41. //! \param r The other matrix
  42. Mat3 operator*(const Mat3& r) const
  43. {
  44. Mat3 result;
  45. for (int j = 0; j < 3; j++)
  46. {
  47. for (int k = 0; k < 3; k++)
  48. {
  49. T sum = 0;
  50. for (int i = 0; i < 3; i++)
  51. sum += elements[j][i] * r.elements[i][k];
  52. result.elements[j][k] = sum;
  53. }
  54. }
  55. return result;
  56. }
  57. //! Multiplies the matrix with a vector without modifying this matrix
  58. //! \param r The vector
  59. Vec2<T> operator*(const Vec2<T> r) const
  60. {
  61. Vec2<T> result;
  62. result.x
  63. = elements[0][0] * r.x + elements[0][1] * r.y + elements[0][2];
  64. result.y
  65. = elements[1][0] * r.x + elements[1][1] * r.y + elements[1][2];
  66. return result;
  67. }
  68. //! Creates a matrix that rotates a vector when multiplied with it
  69. //! \param radian The angle in radians by which the vector should
  70. //! be rotated
  71. static Mat3 rotation(T radian)
  72. {
  73. const T cosTheta = (T)lowPrecisionCos(radian);
  74. const T sinTheta = (T)lowPrecisionSin(radian);
  75. Mat3 r = {cosTheta, -sinTheta, 0, sinTheta, cosTheta, 0, 0, 0, 1};
  76. return r;
  77. }
  78. //! Creates a matrix that scales a vector when multiplied with it
  79. //! \param faktor The factor by which to scale
  80. static Mat3 scaling(T faktor)
  81. {
  82. Mat3 s = {faktor, 0, 0, 0, faktor, 0, 0, 0, 1};
  83. return s;
  84. }
  85. //! Creates a matrix that translates a vector when multiplied with it
  86. //! \param offset The x and y values to add to the vector
  87. static Mat3 translation(const Vec2<T> offset)
  88. {
  89. Mat3 t = {1, 0, offset.x, 0, 1, offset.y, 0, 0, 1};
  90. return t;
  91. }
  92. //! Creates a matrix that does not change a vector when multiplied with
  93. //! it
  94. static Mat3 identity()
  95. {
  96. Mat3 i = {1, 0, 0, 0, 1, 0, 0, 0, 1};
  97. return i;
  98. }
  99. };
  100. } // namespace Framework
  101. #endif