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);
123 using value_type = T;
128 static constexpr std::size_t
Rows = 2;
133 static constexpr std::size_t
Cols = 2;
149 : data{xx, xy, yx, yy}
189 using value_type = T;
194 static constexpr std::size_t
Rows = 3;
199 static constexpr std::size_t
Cols = 3;
219 constexpr Matrix(T xx, T xy, T xz, T yx, T yy, T yz, T zx, T zy, T zz)
220 : data{xx, xy, xz, yx, yy, yz, zx, zy, zz}
259 using value_type = T;
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}
338 extern template struct Matrix<
float, 2, 2>;
339 extern template struct Matrix<
float, 3, 3>;
340 extern template struct Matrix<
float, 4, 4>;
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>
386 Matrix<T, ROWS, COLS> out;
388 for (std::size_t i = 0; i < ROWS; ++i) {
389 for (std::size_t j = 0; j < COLS; ++j) {
390 out.grid[i][j] = - val.grid[i][j];
405 template<
typename T, std::size_t ROWS, std::size_t COLS>
408 Matrix<T, ROWS, COLS> out;
410 for (std::size_t i = 0; i < ROWS; ++i) {
411 for (std::size_t j = 0; j < COLS; ++j) {
412 out.grid[i][j] = lhs.grid[i][j] + rhs.grid[i][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) {
428 lhs.grid[i][j] += rhs.grid[i][j];
440 template<
typename T, std::size_t ROWS, std::size_t COLS>
443 Matrix<T, ROWS, COLS> out;
445 for (std::size_t i = 0; i < ROWS; ++i) {
446 for (std::size_t j = 0; j < COLS; ++j) {
447 out.grid[i][j] = lhs.grid[i][j] - rhs.grid[i][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) {
463 lhs.grid[i][j] -= rhs.grid[i][j];
478 template<
typename T, std::size_t ROWS, std::size_t COLS>
481 Matrix<T, ROWS, COLS> out;
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>
515 Matrix<T, ROWS, COLS> out;
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>
533 Matrix<T, ROWS, COLS> out;
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>
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) {
681 out.grid[j][i] = mat.grid[i][j];
704 T det = mat.xx * mat.yy - mat.yx * mat.xy;
718 out.xx = mat.yy * mat.zz - mat.zy * mat.yz;
719 out.xy = - (mat.xy * mat.zz - mat.zy * mat.xz);
720 out.xz = mat.xy * mat.yz - mat.yy * mat.xz;
721 out.yx = - (mat.yx * mat.zz - mat.zx * mat.yz);
722 out.yy = mat.xx * mat.zz - mat.zx * mat.xz;
723 out.yz = - (mat.xx * mat.yz - mat.yx * mat.xz);
724 out.zx = mat.yx * mat.zy - mat.zx * mat.yy;
725 out.zy = - (mat.xx * mat.zy - mat.zx * mat.xy);
726 out.zz = mat.xx * mat.yy - mat.yx * mat.xy;
728 T det = mat.xx * out.xx + mat.xy * out.yx + mat.xz * out.zx;
733 #ifndef DOXYGEN_SHOULD_SKIP_THIS
static constexpr std::size_t Rows
The number of rows in the matrix.
Definition: Matrix.h:128
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
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
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
friend class RenderTarget
Definition: Shader.h:387
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
static constexpr std::size_t Rows
The number of rows in the matrix.
Definition: Matrix.h:264
Matrix()=default
Default constructor.
General purpose math matrix.
Definition: Matrix.h:59
static constexpr std::size_t Cols
The number of columns in the matrix.
Definition: Matrix.h:269
Matrix()=default
Default constructor.
static constexpr std::size_t Cols
The number of columns in the matrix.
Definition: Matrix.h:78
Matrix< T, ROWS, COLS > & operator*=(Matrix< T, ROWS, COLS > &lhs, T rhs)
Right scalar multiplication and assignment.
Definition: Matrix.h:498
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
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
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
static constexpr std::size_t Cols
The number of columns in the matrix.
Definition: Matrix.h:133
Matrix()=default
Default constructor.
Matrix< T, ROWS, COLS > operator-(const Matrix< T, ROWS, COLS > &val)
Component-wise unary minus.
Definition: Matrix.h:385
static constexpr std::size_t Rows
The number of rows in the matrix.
Definition: Matrix.h:73
General purpose math vector.
Definition: Vector.h:60
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
static constexpr std::size_t Cols
The number of columns in the matrix.
Definition: Matrix.h:199
static constexpr std::size_t Rows
The number of rows in the matrix.
Definition: Matrix.h:194