Mat3.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #ifndef Mat3_H
  2. #define Mat3_H
  3. #include "Vec2.h"
  4. namespace Framework
  5. {
  6. template< typename T >
  7. class Mat3
  8. {
  9. public:
  10. // [ Zeile ][ Spalte ] ( [ y ][ x ] )
  11. T elements[ 3 ][ 3 ];
  12. // nicht constant
  13. Mat3 &operator=( const Mat3 &r )
  14. {
  15. memcpy( elements, r.elements, sizeof( elements ) );
  16. return *this;
  17. }
  18. Mat3 &operator*=( const T r )
  19. {
  20. for( T &e : elements )
  21. r *= r;
  22. return *this;
  23. }
  24. Mat3 &operator*=( const Mat3 &r )
  25. {
  26. return *this = *this * r;
  27. }
  28. // constant
  29. Mat3 operator*( const T r ) const
  30. {
  31. Mat3 result = *this;
  32. return result *= r;
  33. }
  34. Mat3 operator*( const Mat3 &r ) const
  35. {
  36. Mat3 result;
  37. for( int j = 0; j < 3; j++ )
  38. {
  39. for( int k = 0; k < 3; k++ )
  40. {
  41. T sum = 0;
  42. for( int i = 0; i < 3; i++ )
  43. sum += elements[ j ][ i ] * r.elements[ i ][ k ];
  44. result.elements[ j ][ k ] = sum;
  45. }
  46. }
  47. return result;
  48. }
  49. Vec2< T > operator*( const Vec2< T > r ) const
  50. {
  51. Vec2< T > result;
  52. result.x = elements[ 0 ][ 0 ] * r.x + elements[ 0 ][ 1 ] * r.y + elements[ 0 ][ 2 ];
  53. result.y = elements[ 1 ][ 0 ] * r.x + elements[ 1 ][ 1 ] * r.y + elements[ 1 ][ 2 ];
  54. return result;
  55. }
  56. // static
  57. static Mat3 rotation( T radian )
  58. {
  59. const T cosTheta = (T)cos( radian );
  60. const T sinTheta = (T)sin( radian );
  61. Mat3 r = { cosTheta, -sinTheta, 0, sinTheta, cosTheta, 0, 0, 0, 1 };
  62. return r;
  63. }
  64. static Mat3 scaling( T faktor )
  65. {
  66. Mat3 s = { faktor, 0, 0, 0, faktor, 0, 0, 0, 1 };
  67. return s;
  68. }
  69. static Mat3 translation( const Vec2< T > offset )
  70. {
  71. Mat3 t = { 1, 0, offset.x, 0, 1, offset.y, 0, 0, 1 };
  72. return t;
  73. }
  74. static Mat3 identity()
  75. {
  76. Mat3 i = { 1, 0, 0, 0, 1, 0, 0, 0, 1 };
  77. return i;
  78. }
  79. };
  80. }
  81. #endif