27 #include "Portability.h" 31 #ifndef DOXYGEN_SHOULD_SKIP_THIS 58 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
60 #ifndef DOXYGEN_SHOULD_SKIP_THIS 61 static_assert(ROWS > 0,
"ROWS must be strictly positive");
62 static_assert(COLS > 0,
"COLS must be strictly positive");
73 static constexpr std::size_t Rows = ROWS;
78 static constexpr std::size_t Cols = COLS;
89 std::fill_n(data, ROWS * COLS, val);
128 static constexpr std::size_t Rows = 2;
133 static constexpr std::size_t Cols = 2;
149 : data{xx, xy, yx, yy}
194 static constexpr std::size_t Rows = 3;
199 static constexpr std::size_t Cols = 3;
220 : data{xx, xy, xz, yx, yy, yz, zx, zy, zz}
264 static constexpr std::size_t Rows = 4;
269 static constexpr std::size_t Cols = 4;
291 constexpr
Matrix(
T xx,
T xy,
T xz,
T xw,
T yx,
T yy,
T yz,
T yw,
T zx,
T zy,
T zz,
T zw,
T wx,
T wy,
T wz,
T ww)
292 : data{xx, xy, xz, xw, yx, yy, yz, yw, zx, zy, zz, zw, wx, wy, wz, ww}
351 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
354 for (std::size_t i = 0; i < ROWS; ++i) {
355 for (std::size_t j = 0; j < COLS; ++j) {
356 if (lhs.
grid[i][j] != rhs.
grid[i][j]) {
369 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
372 return !(lhs == rhs);
383 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
388 for (std::size_t i = 0; i < ROWS; ++i) {
389 for (std::size_t j = 0; j < COLS; ++j) {
405 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
410 for (std::size_t i = 0; i < ROWS; ++i) {
411 for (std::size_t j = 0; j < COLS; ++j) {
423 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
426 for (std::size_t i = 0; i < ROWS; ++i) {
427 for (std::size_t j = 0; j < COLS; ++j) {
440 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
445 for (std::size_t i = 0; i < ROWS; ++i) {
446 for (std::size_t j = 0; j < COLS; ++j) {
458 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
461 for (std::size_t i = 0; i < ROWS; ++i) {
462 for (std::size_t j = 0; j < COLS; ++j) {
478 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
483 for (std::size_t i = 0; i < ROWS; ++i) {
484 for (std::size_t j = 0; j < COLS; ++j) {
485 out.
grid[i][j] = lhs.
grid[i][j] * rhs;
496 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
499 for (std::size_t i = 0; i < ROWS; ++i) {
500 for (std::size_t j = 0; j < COLS; ++j) {
501 lhs.
grid[i][j] *= rhs;
512 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
517 for (std::size_t i = 0; i < ROWS; ++i) {
518 for (std::size_t j = 0; j < COLS; ++j) {
519 out.
grid[i][j] = lhs * rhs.
grid[i][j];
530 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
535 for (std::size_t i = 0; i < ROWS; ++i) {
536 for (std::size_t j = 0; j < COLS; ++j) {
537 out.
grid[i][j] = lhs.
grid[i][j] / rhs;
548 template<
typename T, std::
size_t ROWS, std::
size_t COLS>
551 for (std::size_t i = 0; i < ROWS; ++i) {
552 for (std::size_t j = 0; j < COLS; ++j) {
553 lhs.
grid[i][j] /= rhs;
564 template<
typename T, std::
size_t S1, std::
size_t S2>
569 for (std::size_t i = 0; i < S1; ++i) {
572 for (std::size_t j = 0; j < S2; ++j) {
573 val += lhs.
grid[i][j] * rhs.
data[j];
586 template<
typename T, std::
size_t S1, std::
size_t S2>
591 for (std::size_t j = 0; j < S2; ++j) {
594 for (std::size_t i = 0; i < S1; ++i) {
595 val += lhs.
data[i] * rhs.
grid[i][j];
609 template<
typename T, std::
size_t S1, std::
size_t S2, std::
size_t S3>
614 for (std::size_t i = 0; i < S1; ++i) {
615 for (std::size_t j = 0; j < S3; ++j) {
618 for (std::size_t k = 0; k < S2; ++k) {
619 val += lhs.
grid[i][k] * rhs.
grid[k][j];
622 out.
grid[i][j] = val;
635 template<
typename T, std::
size_t N>
652 template<
typename MatrixType>
655 static_assert(MatrixType::Rows == MatrixType::Cols,
"identity() is only defined for square matrices.");
659 typedef typename MatrixType::value_type
value_type;
661 for (std::size_t i = 0; i < MatrixType::Rows; ++i) {
662 for (std::size_t j = 0; j < MatrixType::Cols; ++j) {
663 out.grid[i][j] = (i == j) ? value_type{1} : value_type{0};
674 template<
typename T, std::
size_t S1, std::
size_t S2>
679 for (std::size_t i = 0; i < S1; ++i) {
680 for (std::size_t j = 0; j < S2; ++j) {
704 T det = mat.
xx * mat.
yy - mat.
yx * mat.
xy;
719 out.
xy = - (mat.
xy * mat.
zz - mat.
zy * mat.
xz);
721 out.
yx = - (mat.
yx * mat.
zz - mat.
zx * mat.
yz);
723 out.
yz = - (mat.
xx * mat.
yz - mat.
yx * mat.
xz);
725 out.
zy = - (mat.
xx * mat.
zy - mat.
zx * mat.
xy);
733 #ifndef DOXYGEN_SHOULD_SKIP_THIS 738 #endif // GAME_MATRIX_H Matrix< T, 3, 3 > invert(const Matrix< T, 3, 3 > &mat)
Inversion of a 3x3 matrix.
Definition: Matrix.h:715
Matrix< T, S2, S1 > transpose(const Matrix< T, S1, S2 > &mat)
Transposition of a matrix.
Definition: Matrix.h:676
bool operator==(const Matrix< T, ROWS, COLS > &lhs, const Matrix< T, ROWS, COLS > &rhs)
Equality operator between two matrices.
Definition: Matrix.h:353
Matrix< T, ROWS, COLS > operator+(const Matrix< T, ROWS, COLS > &lhs, const Matrix< T, ROWS, COLS > &rhs)
Component-wise addition.
Definition: Matrix.h:407
T yx
Definition: Matrix.h:162
Matrix< T, ROWS, COLS > operator*(T lhs, const Matrix< T, ROWS, COLS > &rhs)
Left scalar multiplication.
Definition: Matrix.h:514
Matrix< T, 2, 2 > invert(const Matrix< T, 2, 2 > &mat)
Inversion of a 2x2 matrix.
Definition: Matrix.h:696
Matrix< T, ROWS, COLS > & operator/=(Matrix< T, ROWS, COLS > &lhs, T rhs)
Right scalar division and assignment.
Definition: Matrix.h:550
constexpr Matrix(T xx, T xy, T yx, T yy)
Constructor that takes all the elements.
Definition: Matrix.h:148
bool operator!=(const Matrix< T, ROWS, COLS > &lhs, const Matrix< T, ROWS, COLS > &rhs)
Inequality operator between two matrices.
Definition: Matrix.h:371
T zz
Definition: Matrix.h:234
T yy
Definition: Matrix.h:233
T wz
Definition: Matrix.h:307
T xx
Definition: Matrix.h:232
T grid[ROWS][COLS]
Grid representation.
Definition: Matrix.h:96
MatrixType identity()
Identity matrix.
Definition: Matrix.h:654
Vector< T, S2 > operator*(const Vector< T, S1 > &lhs, const Matrix< T, S1, S2 > &rhs)
Vector-matrix multiplication.
Definition: Matrix.h:588
T xy
Definition: Matrix.h:232
Matrix(T val)
Constructor that fills the matrix with a value.
Definition: Matrix.h:88
Matrix< T, ROWS, COLS > & operator-=(Matrix< T, ROWS, COLS > &lhs, const Matrix< T, ROWS, COLS > &rhs)
Component-wise substraction and assignment.
Definition: Matrix.h:460
Matrix< T, ROWS, COLS > operator-(const Matrix< T, ROWS, COLS > &lhs, const Matrix< T, ROWS, COLS > &rhs)
Component-wise substraction.
Definition: Matrix.h:442
Matrix< T, ROWS, COLS > operator/(const Matrix< T, ROWS, COLS > &lhs, T rhs)
Right scalar division.
Definition: Matrix.h:532
Vector< T, S1 > operator*(const Matrix< T, S1, S2 > &lhs, const Vector< T, S2 > &rhs)
Matrix-vector multiplication.
Definition: Matrix.h:566
Matrix< T, S1, S3 > operator*(const Matrix< T, S1, S2 > &lhs, const Matrix< T, S2, S3 > &rhs)
Matrix-matrix multiplication.
Definition: Matrix.h:611
T data[N]
The internal representation of the vector.
Definition: Vector.h:251
T xy
Definition: Matrix.h:161
T yy
Definition: Matrix.h:162
General purpose math matrix.
Definition: Matrix.h:59
T zy
Definition: Matrix.h:234
float value_type
The value type of the elements of the matrix.
Definition: Matrix.h:68
The namespace for gf classes.
Definition: Action.h:34
T yz
Definition: Matrix.h:305
T xz
Definition: Matrix.h:304
T value_type
The value type of the elements of the matrix.
Definition: Matrix.h:259
T xz
Definition: Matrix.h:232
Matrix< T, ROWS, COLS > & operator*=(Matrix< T, ROWS, COLS > &lhs, T rhs)
Right scalar multiplication and assignment.
Definition: Matrix.h:498
T xx
Definition: Matrix.h:161
constexpr Matrix(T xx, T xy, T xz, T yx, T yy, T yz, T zx, T zy, T zz)
Constructor that takes all the elements.
Definition: Matrix.h:219
T value_type
The value type of the elements of the matrix.
Definition: Matrix.h:123
T zx
Definition: Matrix.h:234
Matrix< T, N, N > & operator*=(Matrix< T, N, N > &lhs, const Matrix< T, N, N > &rhs)
Matrix-matrix multiplication and assignment.
Definition: Matrix.h:637
T yz
Definition: Matrix.h:233
T zz
Definition: Matrix.h:306
Matrix< T, ROWS, COLS > & operator+=(Matrix< T, ROWS, COLS > &lhs, const Matrix< T, ROWS, COLS > &rhs)
Component-wise addition and assignment.
Definition: Matrix.h:425
T value_type
The value type of the elements of the matrix.
Definition: Matrix.h:189
Matrix< T, ROWS, COLS > operator-(const Matrix< T, ROWS, COLS > &val)
Component-wise unary minus.
Definition: Matrix.h:385
General purpose math vector.
Definition: Vector.h:60
A 3x3 matrix.
Definition: Matrix.h:185
A 2x2 matrix.
Definition: Matrix.h:119
constexpr Matrix(T xx, T xy, T xz, T xw, T yx, T yy, T yz, T yw, T zx, T zy, T zz, T zw, T wx, T wy, T wz, T ww)
Constructor that takes all the elements.
Definition: Matrix.h:291
Matrix< T, ROWS, COLS > operator*(const Matrix< T, ROWS, COLS > &lhs, T rhs)
Right scalar multiplication.
Definition: Matrix.h:480
T yx
Definition: Matrix.h:233