27 #include "Portability.h" 32 #ifndef DOXYGEN_SHOULD_SKIP_THIS 59 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
61 #ifndef DOXYGEN_SHOULD_SKIP_THIS 62 static_assert(ROWS > 0,
"ROWS must be strictly positive");
63 static_assert(COLS > 0,
"COLS must be strictly positive");
74 static constexpr std::size_t Rows = ROWS;
79 static constexpr std::size_t Cols = COLS;
96 std::fill_n(data, ROWS * COLS, val);
101 std::copy_n(array, ROWS * COLS, data);
105 return grid[row][col];
109 return grid[row][col];
115 constexpr
void zero() noexcept {
116 for (std::size_t i = 0; i < ROWS * COLS; ++i) {
157 static constexpr std::size_t Rows = 2;
162 static constexpr std::size_t Cols = 2;
182 : m11(array[0]), m12(array[1])
183 , m21(array[2]), m22(array[3])
212 const T *grid[Rows][Cols] = {
216 return *grid[row][col];
220 T *grid[Rows][Cols] = {
224 return *grid[row][col];
227 constexpr
void zero() noexcept {
281 static constexpr std::size_t Rows = 3;
286 static constexpr std::size_t Cols = 3;
299 : m11(val), m12(val), m13(val)
300 , m21(val), m22(val), m23(val)
301 , m31(val), m32(val), m33(val)
307 : m11(array[0]), m12(array[1]), m13(array[2])
308 , m21(array[3]), m22(array[4]), m23(array[5])
309 , m31(array[6]), m32(array[7]), m33(array[8])
327 constexpr
Matrix(
T e11,
T e12,
T e13,
T e21,
T e22,
T e23,
T e31,
T e32,
T e33) noexcept
328 : m11(e11), m12(e12), m13(e13)
329 , m21(e21), m22(e22), m23(e23)
330 , m31(e31), m32(e32), m33(e33)
344 const T *grid[Rows][Cols] = {
345 { &m11, &m12, &m13 },
346 { &m21, &m22, &m23 },
349 return *grid[row][col];
353 T *grid[Rows][Cols] = {
354 { &m11, &m12, &m13 },
355 { &m21, &m22, &m23 },
358 return *grid[row][col];
361 constexpr
void zero() noexcept {
362 m11 = m12 = m13 =
T{};
363 m21 = m22 = m23 =
T{};
364 m31 = m32 = m33 =
T{};
439 static constexpr std::size_t Rows = 4;
444 static constexpr std::size_t Cols = 4;
457 : m11(val), m12(val), m13(val), m14(val)
458 , m21(val), m22(val), m23(val), m24(val)
459 , m31(val), m32(val), m33(val), m34(val)
460 , m41(val), m42(val), m43(val), m44(val)
466 : m11(array[ 0]), m12(array[ 1]), m13(array[ 2]), m14(array[ 3])
467 , m21(array[ 4]), m22(array[ 5]), m23(array[ 6]), m24(array[ 7])
468 , m31(array[ 8]), m32(array[ 9]), m33(array[10]), m34(array[11])
469 , m41(array[12]), m42(array[13]), m43(array[14]), m44(array[15])
494 constexpr
Matrix(
T e11,
T e12,
T e13,
T e14,
T e21,
T e22,
T e23,
T e24,
T e31,
T e32,
T e33,
T e34,
T e41,
T e42,
T e43,
T e44) noexcept
495 : m11(e11), m12(e12), m13(e13), m14(e14)
496 , m21(e21), m22(e22), m23(e23), m24(e24)
497 , m31(e31), m32(e32), m33(e33), m34(e34)
498 , m41(e41), m42(e42), m43(e43), m44(e44)
512 const T *grid[Rows][Cols] = {
513 { &m11, &m12, &m13, &m14 },
514 { &m21, &m22, &m23, &m24 },
515 { &m31, &m32, &m33, &m34 },
516 { &m41, &m42, &m43, &m44 }
518 return *grid[row][col];
522 T *grid[Rows][Cols] = {
523 { &m11, &m12, &m13, &m14 },
524 { &m21, &m22, &m23, &m24 },
525 { &m31, &m32, &m33, &m34 },
526 { &m41, &m42, &m43, &m44 }
528 return *grid[row][col];
531 constexpr
void zero() noexcept {
532 m11 = m12 = m13 = m14 =
T{};
533 m21 = m22 = m23 = m24 =
T{};
534 m31 = m32 = m33 = m34 =
T{};
535 m41 = m42 = m43 = m44 =
T{};
659 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
662 for (std::size_t i = 0; i < ROWS; ++i) {
663 for (std::size_t j = 0; j < COLS; ++j) {
664 if (lhs(i,j) != rhs(i,j)) {
677 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
680 return !(lhs == rhs);
691 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
696 for (std::size_t i = 0; i < ROWS; ++i) {
697 for (std::size_t j = 0; j < COLS; ++j) {
698 out(i,j) = - val(i,j);
713 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
718 for (std::size_t i = 0; i < ROWS; ++i) {
719 for (std::size_t j = 0; j < COLS; ++j) {
720 out(i,j) = lhs(i,j) + rhs(i,j);
731 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
734 for (std::size_t i = 0; i < ROWS; ++i) {
735 for (std::size_t j = 0; j < COLS; ++j) {
736 lhs(i,j) += rhs(i,j);
748 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
753 for (std::size_t i = 0; i < ROWS; ++i) {
754 for (std::size_t j = 0; j < COLS; ++j) {
755 out(i,j) = lhs(i,j) - rhs(i,j);
766 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
769 for (std::size_t i = 0; i < ROWS; ++i) {
770 for (std::size_t j = 0; j < COLS; ++j) {
771 lhs(i,j) -= rhs(i,j);
786 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
791 for (std::size_t i = 0; i < ROWS; ++i) {
792 for (std::size_t j = 0; j < COLS; ++j) {
793 out(i,j) = lhs(i,j) * rhs;
804 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
807 for (std::size_t i = 0; i < ROWS; ++i) {
808 for (std::size_t j = 0; j < COLS; ++j) {
820 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
825 for (std::size_t i = 0; i < ROWS; ++i) {
826 for (std::size_t j = 0; j < COLS; ++j) {
827 out(i,j) = lhs * rhs(i,j);
838 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
843 for (std::size_t i = 0; i < ROWS; ++i) {
844 for (std::size_t j = 0; j < COLS; ++j) {
845 out(i,j) = lhs(i,j) / rhs;
856 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
859 for (std::size_t i = 0; i < ROWS; ++i) {
860 for (std::size_t j = 0; j < COLS; ++j) {
872 template<
typename T, std::
size_t S1, std::
size_t S2>
877 for (std::size_t i = 0; i < S1; ++i) {
880 for (std::size_t j = 0; j < S2; ++j) {
881 val += lhs(i,j) * rhs[j];
894 template<
typename T, std::
size_t S1, std::
size_t S2>
899 for (std::size_t j = 0; j < S2; ++j) {
902 for (std::size_t i = 0; i < S1; ++i) {
903 val += lhs[i] * rhs(i,j);
917 template<
typename T, std::
size_t S1, std::
size_t S2, std::
size_t S3>
922 for (std::size_t i = 0; i < S1; ++i) {
923 for (std::size_t j = 0; j < S3; ++j) {
926 for (std::size_t k = 0; k < S2; ++k) {
927 val += lhs(i,k) * rhs(k,j);
943 template<
typename T, std::
size_t N>
960 template<
typename MatrixType>
963 static_assert(MatrixType::Rows == MatrixType::Cols,
"identity() is only defined for square matrices.");
967 typedef typename MatrixType::value_type
value_type;
969 for (std::size_t i = 0; i < MatrixType::Rows; ++i) {
970 for (std::size_t j = 0; j < MatrixType::Cols; ++j) {
971 out(i,j) = (i == j) ? value_type{1} : value_type{0};
982 template<
typename T, std::
size_t S1, std::
size_t S2>
987 for (std::size_t i = 0; i < S1; ++i) {
988 for (std::size_t j = 0; j < S2; ++j) {
1002 template<
typename T>
1012 T det = mat.
xx * mat.
yy - mat.
yx * mat.
xy;
1021 template<
typename T>
1027 out.
xy = - (mat.
xy * mat.
zz - mat.
zy * mat.
xz);
1029 out.
yx = - (mat.
yx * mat.
zz - mat.
zx * mat.
yz);
1031 out.
yz = - (mat.
xx * mat.
yz - mat.
yx * mat.
xz);
1033 out.
zy = - (mat.
xx * mat.
zy - mat.
zx * mat.
xy);
1036 T det = mat.
xx * out.
xx + mat.
xy * out.
yx + mat.
xz * out.
zx;
1041 #ifndef DOXYGEN_SHOULD_SKIP_THIS 1046 #endif // GAME_MATRIX_H constexpr Matrix< T, ROWS, COLS > operator-(const Matrix< T, ROWS, COLS > &val)
Component-wise unary minus.
Definition: Matrix.h:693
const T * getData() const noexcept
Definition: Matrix.h:503
T m43
Definition: Matrix.h:610
T & operator()(std::size_t row, std::size_t col)
Definition: Matrix.h:521
T m13
Definition: Matrix.h:550
T m34
Definition: Matrix.h:595
T m44
Definition: Matrix.h:615
T m41
Definition: Matrix.h:600
T yx
Definition: Matrix.h:243
T m21
Definition: Matrix.h:244
T * getData() noexcept
Definition: Matrix.h:339
T m24
Definition: Matrix.h:575
T m42
Definition: Matrix.h:605
constexpr void zero() noexcept
Definition: Matrix.h:227
constexpr Matrix< T, ROWS, COLS > operator/(const Matrix< T, ROWS, COLS > &lhs, T rhs)
Right scalar division.
Definition: Matrix.h:840
T xy
Definition: Matrix.h:544
T zz
Definition: Matrix.h:408
constexpr Matrix< T, ROWS, COLS > operator*(T lhs, const Matrix< T, ROWS, COLS > &rhs)
Left scalar multiplication.
Definition: Matrix.h:822
T yy
Definition: Matrix.h:388
T wz
Definition: Matrix.h:609
T xx
Definition: Matrix.h:368
constexpr void zero() noexcept
Zero out the matrix.
Definition: Matrix.h:115
constexpr Matrix< T, S2, S1 > transpose(const Matrix< T, S1, S2 > &mat)
Transposition of a matrix.
Definition: Matrix.h:984
T m13
Definition: Matrix.h:379
constexpr Vector< T, S2 > operator*(const Vector< T, S1 > &lhs, const Matrix< T, S1, S2 > &rhs)
Vector-matrix multiplication.
Definition: Matrix.h:896
constexpr Matrix< T, 2, 2 > invert(const Matrix< T, 2, 2 > &mat)
Inversion of a 2x2 matrix.
Definition: Matrix.h:1004
T operator()(std::size_t row, std::size_t col) const
Definition: Matrix.h:104
T m11
Definition: Matrix.h:540
T xy
Definition: Matrix.h:373
T zy
Definition: Matrix.h:584
constexpr Matrix(T e11, T e12, T e21, T e22) noexcept
Constructor that takes all the elements.
Definition: Matrix.h:196
constexpr Matrix(const T *array)
Definition: Matrix.h:306
constexpr Matrix< T, ROWS, COLS > operator-(const Matrix< T, ROWS, COLS > &lhs, const Matrix< T, ROWS, COLS > &rhs)
Component-wise substraction.
Definition: Matrix.h:750
T yx
Definition: Matrix.h:559
T operator()(std::size_t row, std::size_t col) const
Definition: Matrix.h:211
T * getData() noexcept
Definition: Matrix.h:507
T m23
Definition: Matrix.h:570
constexpr Matrix(ZeroType) noexcept
Definition: Matrix.h:293
T & operator()(std::size_t row, std::size_t col)
Definition: Matrix.h:219
T zx
Definition: Matrix.h:579
constexpr Matrix< T, ROWS, COLS > operator*(const Matrix< T, ROWS, COLS > &lhs, T rhs)
Right scalar multiplication.
Definition: Matrix.h:788
T m31
Definition: Matrix.h:399
constexpr Matrix(ZeroType) noexcept
Definition: Matrix.h:169
constexpr MatrixType identity()
Identity matrix.
Definition: Matrix.h:962
T m32
Definition: Matrix.h:585
T wx
Definition: Matrix.h:599
constexpr Vector< T, S1 > operator*(const Matrix< T, S1, S2 > &lhs, const Vector< T, S2 > &rhs)
Matrix-vector multiplication.
Definition: Matrix.h:874
T xy
Definition: Matrix.h:238
T m21
Definition: Matrix.h:384
T m32
Definition: Matrix.h:404
T m14
Definition: Matrix.h:555
T yy
Definition: Matrix.h:248
T m22
Definition: Matrix.h:565
T yy
Definition: Matrix.h:564
General purpose math matrix.
Definition: Matrix.h:60
constexpr Matrix< T, ROWS, COLS > operator+(const Matrix< T, ROWS, COLS > &lhs, const Matrix< T, ROWS, COLS > &rhs)
Component-wise addition.
Definition: Matrix.h:715
constexpr Matrix< T, ROWS, COLS > & operator*=(Matrix< T, ROWS, COLS > &lhs, T rhs)
Right scalar multiplication and assignment.
Definition: Matrix.h:806
T m12
Definition: Matrix.h:545
Matrix(T val) noexcept
Constructor that fills the matrix with a value.
Definition: Matrix.h:94
T zy
Definition: Matrix.h:403
float value_type
The value type of the elements of the matrix.
Definition: Matrix.h:69
T zw
Definition: Matrix.h:594
constexpr bool operator!=(const Matrix< T, ROWS, COLS > &lhs, const Matrix< T, ROWS, COLS > &rhs)
Inequality operator between two matrices.
Definition: Matrix.h:679
The namespace for gf classes.
Definition: Action.h:34
T yz
Definition: Matrix.h:569
constexpr Matrix(T e11, T e12, T e13, T e21, T e22, T e23, T e31, T e32, T e33) noexcept
Constructor that takes all the elements.
Definition: Matrix.h:327
T xz
Definition: Matrix.h:549
const T * getData() const noexcept
Definition: Matrix.h:335
T value_type
The value type of the elements of the matrix.
Definition: Matrix.h:434
T xz
Definition: Matrix.h:378
T m23
Definition: Matrix.h:394
constexpr void zero() noexcept
Definition: Matrix.h:361
T m31
Definition: Matrix.h:580
constexpr Matrix< T, ROWS, COLS > & operator/=(Matrix< T, ROWS, COLS > &lhs, T rhs)
Right scalar division and assignment.
Definition: Matrix.h:858
T yw
Definition: Matrix.h:574
T xx
Definition: Matrix.h:233
T xx
Definition: Matrix.h:539
T m33
Definition: Matrix.h:590
T wy
Definition: Matrix.h:604
T operator()(std::size_t row, std::size_t col) const
Definition: Matrix.h:511
constexpr bool operator==(const Matrix< T, ROWS, COLS > &lhs, const Matrix< T, ROWS, COLS > &rhs)
Equality operator between two matrices.
Definition: Matrix.h:661
constexpr Matrix(T val) noexcept
Definition: Matrix.h:298
T value_type
The value type of the elements of the matrix.
Definition: Matrix.h:152
T zx
Definition: Matrix.h:398
constexpr Matrix< T, ROWS, COLS > & operator+=(Matrix< T, ROWS, COLS > &lhs, const Matrix< T, ROWS, COLS > &rhs)
Component-wise addition and assignment.
Definition: Matrix.h:733
constexpr Matrix(T e11, T e12, T e13, T e14, T e21, T e22, T e23, T e24, T e31, T e32, T e33, T e34, T e41, T e42, T e43, T e44) noexcept
Constructor that takes all the elements.
Definition: Matrix.h:494
T m12
Definition: Matrix.h:374
T m22
Definition: Matrix.h:389
T yz
Definition: Matrix.h:393
constexpr Matrix< T, N, N > & operator*=(Matrix< T, N, N > &lhs, const Matrix< T, N, N > &rhs)
Matrix-matrix multiplication and assignment.
Definition: Matrix.h:945
constexpr Matrix(ZeroType) noexcept
Definition: Matrix.h:86
T zz
Definition: Matrix.h:589
T m22
Definition: Matrix.h:249
Matrix(const T *array)
Definition: Matrix.h:99
T m21
Definition: Matrix.h:560
T value_type
The value type of the elements of the matrix.
Definition: Matrix.h:276
constexpr Matrix< T, ROWS, COLS > & operator-=(Matrix< T, ROWS, COLS > &lhs, const Matrix< T, ROWS, COLS > &rhs)
Component-wise substraction and assignment.
Definition: Matrix.h:768
T m11
Definition: Matrix.h:234
T m12
Definition: Matrix.h:239
constexpr Matrix(ZeroType) noexcept
Definition: Matrix.h:451
constexpr Matrix< T, S1, S3 > operator*(const Matrix< T, S1, S2 > &lhs, const Matrix< T, S2, S3 > &rhs)
Matrix-matrix multiplication.
Definition: Matrix.h:919
T & operator()(std::size_t row, std::size_t col)
Definition: Matrix.h:108
T m33
Definition: Matrix.h:409
General purpose math vector.
Definition: Vector.h:61
T m11
Definition: Matrix.h:369
T operator()(std::size_t row, std::size_t col) const
Definition: Matrix.h:343
T ww
Definition: Matrix.h:614
constexpr Matrix(const T *array)
Definition: Matrix.h:465
T * getData() noexcept
Definition: Matrix.h:207
T xw
Definition: Matrix.h:554
T & operator()(std::size_t row, std::size_t col)
Definition: Matrix.h:352
A 3x3 matrix.
Definition: Matrix.h:272
A 2x2 matrix.
Definition: Matrix.h:148
constexpr void zero() noexcept
Definition: Matrix.h:531
constexpr Matrix< T, 3, 3 > invert(const Matrix< T, 3, 3 > &mat)
Inversion of a 3x3 matrix.
Definition: Matrix.h:1023
T yx
Definition: Matrix.h:383
constexpr Matrix(const T *array)
Definition: Matrix.h:181
constexpr Matrix(T val) noexcept
Definition: Matrix.h:456
const T * getData() const noexcept
Definition: Matrix.h:203
constexpr Matrix(T val) noexcept
Definition: Matrix.h:174
Semantic type to represent "zero".
Definition: Types.h:69