1 #ifndef _VN_MATH_MAT_H_
7 #define _VN_MATH_MAT_H_
19 template <
size_t m,
size_t n = m,
typename T =
float>
44 std::fill_n(e, m * n, val);
76 for (
size_t i = 0; i < m; i++)
93 return const_cast<T&
>(
static_cast<const mat&
>(*this)(row, col));
103 assert(row < m && col < n);
106 return e[col + row * m];
124 for (
size_t i = 0; i < m*n; i++)
134 template<
size_t r,
size_t s,
typename S>
150 for (
size_t row_index = 0; row_index < r; row_index++)
152 for (
size_t col_index = 0; col_index < n; col_index++)
155 cell = row_index + (col_index * r);
157 for (
size_t cell_index = 0; cell_index < m; cell_index++)
160 row = (col_index * m) + cell_index;
162 col = (cell_index * r) + row_index;
163 return_mat.e[cell] += this->e[row] * rhs.e[col];
178 for (
size_t i = 0; i < m*n; i++)
191 for (
size_t i = 0; i < m*n; i++)
204 for (
size_t i = 0; i < m*n; i++)
231 for (
size_t i = 0; i < m * n; i++)
245 for (
size_t i = 0; i < m*n; i++)
246 nm.e[i] = e[i] * scalar;
259 for (
size_t i = 0; i < m*n; i++)
260 nm.e[i] = e[i] / scalar;
273 for (
size_t i = 0; i < m*n; i++)
274 nm.e[i] = e[i] + toAdd.e[i];
287 for (
size_t i = 0; i < m*n; i++)
288 nm.e[i] = e[i] - toSub.e[i];
300 for (
size_t row = 0; row < m; row++)
302 for (
size_t col = 0; col < n; col++)
304 nm.e[row * n + col] = e[col * m + row];
315 #if defined(_MSC_VER)
316 #pragma warning(push)
319 #pragma warning(disable:4201)
323 template <
typename T>
376 e00(e00v), e10(e10v),
387 e00(col0.x), e10(col1.x),
388 e01(col0.y), e11(col1.y)
432 return const_cast<T&
>(
static_cast<const mat&
>(*this)(row, col));
442 assert(row < 2 && col < 2);
444 return e[col * 2 + row];
462 #if defined(_MSC_VER)
463 #pragma warning(push)
466 #pragma warning(disable:4244)
469 for (
size_t i = 0; i < 2 * 2; i++)
472 #if defined (_MSC_VER)
493 for (
size_t row_index = 0; row_index < 2; row_index++)
495 for (
size_t col_index = 0; col_index < 2; col_index++)
497 cell = row_index + (col_index * 2);
499 for (
size_t cell_index = 0; cell_index < 2; cell_index++)
502 row = row_index + (cell_index * 2);
504 col = cell_index + (col_index * 2);
506 return_mat.
e[cell] += this->e[row] * rhs.e[col];
523 #if defined(_MSC_VER)
524 #pragma warning(push)
527 #pragma warning(disable:4244)
530 for (
size_t i = 0; i < 2 * 2; i++)
533 #if defined (_MSC_VER)
547 for (
size_t i = 0; i < 2 * 2; i++)
560 for (
size_t i = 0; i < 2 * 2; i++)
594 for (
size_t i = 0; i < 2 * 2; i++)
608 for (
size_t i = 0; i < 2 * 2; i++)
609 nm.e[i] = e[i] * scalar;
622 for (
size_t i = 0; i < 2 * 2; i++)
623 nm.e[i] = e[i] / scalar;
636 for (
size_t i = 0; i < 2 * 2; i++)
637 nm.e[i] = e[i] + toAdd.e[i];
650 for (
size_t i = 0; i < 2 * 2; i++)
651 nm.e[i] = e[i] - toSub.e[i];
663 for (
size_t row = 0; row < 2; row++)
665 for (
size_t col = 0; col < 2; col++)
667 nm.
e[row * 2 + col] = e[col * 2 + row];
677 template <
typename T>
732 e00(val), e10(val), e20(val),
733 e01(val), e11(val), e21(val),
734 e02(val), e12(val), e22(val)
749 mat(T e00v, T e01v, T e02v,
750 T e10v, T e11v, T e12v,
751 T e20v, T e21v, T e22v) :
752 e00(e00v), e10(e10v), e20(e20v),
753 e01(e01v), e11(e11v), e21(e21v),
754 e02(e02v), e12(e12v), e22(e22v)
764 e00(col0.x), e10(col0.y), e20(col0.z),
765 e01(col1.x), e11(col1.y), e21(col1.z),
766 e02(col2.x), e12(col2.y), e22(col2.z)
813 return const_cast<T&
>(
static_cast<const mat&
>(*this)(row, col));
823 assert(row < 3 && col < 3);
825 return e[col * 3 + row];
843 #if defined(_MSC_VER)
844 #pragma warning(push)
847 #pragma warning(disable:4244)
850 for (
size_t i = 0; i < 3 * 3; i++)
853 #if defined (_MSC_VER)
874 for(
size_t row_index = 0; row_index < 3; row_index++)
876 for(
size_t col_index = 0; col_index < 3; col_index++)
878 cell = row_index + (col_index * 3);
880 for (
size_t cell_index = 0; cell_index < 3; cell_index++)
883 row = row_index + (cell_index * 3);
885 col = cell_index + (col_index * 3);
887 return_mat.
e[cell] += this->e[row] * rhs.e[col];
906 #if defined(_MSC_VER)
907 #pragma warning(push)
910 #pragma warning(disable:4244)
913 for (
size_t i = 0; i < 3 * 3; i++)
916 #if defined (_MSC_VER)
930 for (
size_t i = 0; i < 3 * 3; i++)
943 for (
size_t i = 0; i < 3 * 3; i++)
962 size_t dimCols()
const {
return 3; }
965 void copy(
const mat<3, 3, S>& rhs)
977 for (
size_t i = 0; i < 3 * 3; i++)
991 for (
size_t i = 0; i < 3 * 3; i++)
992 nm.e[i] = e[i] * scalar;
1005 for (
size_t i = 0; i < 3 * 3; i++)
1006 nm.e[i] = e[i] / scalar;
1019 for (
size_t i = 0; i < 3 * 3; i++)
1020 nm.e[i] = e[i] + toAdd.e[i];
1033 for (
size_t i = 0; i < 3 * 3; i++)
1034 nm.e[i] = e[i] - toSub.e[i];
1046 for (
size_t row = 0; row < 3; row++)
1048 for (
size_t col = 0; col < 3; col++)
1050 nm.
e[row * 3 + col] = e[col * 3 + row];
1059 template <
typename T>
1135 e00(val), e01(val), e02(val), e03(val),
1136 e10(val), e11(val), e12(val), e13(val),
1137 e20(val), e21(val), e22(val), e23(val),
1138 e30(val), e31(val), e32(val), e33(val)
1160 mat(T e00v, T e01v, T e02v, T e03v,
1161 T e10v, T e11v, T e12v, T e13v,
1162 T e20v, T e21v, T e22v, T e23v,
1163 T e30v, T e31v, T e32v, T e33v) :
1164 e00(e00v), e01(e01v), e02(e02v), e03(e03v),
1165 e10(e10v), e11(e11v), e12(e12v), e13(e13v),
1166 e20(e20v), e21(e21v), e22(e22v), e23(e23v),
1167 e30(e30v), e31(e31v), e32(e32v), e33(e33v)
1177 e00(col0.x), e10(col1.x), e20(col2.x), e30(col3.x),
1178 e01(col0.y), e11(col1.y), e21(col2.y), e31(col3.y),
1179 e02(col0.z), e12(col1.z), e22(col2.z), e32(col3.z),
1180 e03(col0.w), e13(col1.w), e23(col2.w), e33(col3.w)
1227 return const_cast<T&
>(
static_cast<const mat&
>(*this)(row, col));
1237 assert(row < 4 && col < 4);
1239 return e[col * 4 + row];
1254 template<
typename S>
1257 #if defined(_MSC_VER)
1258 #pragma warning(push)
1261 #pragma warning(disable:4244)
1264 for (
size_t i = 0; i < 4 * 4; i++)
1267 #if defined (_MSC_VER)
1268 #pragma warning(pop)
1278 template<
typename S>
1288 for (
size_t row_index = 0; row_index < 4; row_index++)
1290 for (
size_t col_index = 0; col_index < 4; col_index++)
1292 cell = row_index + (col_index * 4);
1294 for (
size_t cell_index = 0; cell_index < 4; cell_index++)
1297 row = row_index + (cell_index * 4);
1299 col = cell_index + (col_index * 4);
1301 return_mat.
e[cell] += this->e[row] * rhs.e[col];
1315 template<
typename S>
1318 #if defined(_MSC_VER)
1319 #pragma warning(push)
1322 #pragma warning(disable:4244)
1325 for (
size_t i = 0; i < 4 * 4; i++)
1328 #if defined (_MSC_VER)
1329 #pragma warning(pop)
1339 template<
typename S>
1342 for (
size_t i = 0; i < 4*4; i++)
1352 template<
typename S>
1355 for (
size_t i = 0; i < 4*4; i++)
1375 template<
typename S>
1388 for (
size_t i = 0; i < 4 * 4; i++)
1402 for (
size_t i = 0; i < 4*4; i++)
1403 nm.e[i] = e[i] * scalar;
1418 for (
size_t i = 0; i < 4*4; i++)
1419 nm.e[i] = e[i] / scalar;
1432 for (
size_t i = 0; i < 4*4; i++)
1433 nm.e[i] = e[i] + toAdd.e[i];
1446 for (
size_t i = 0; i < 4*4; i++)
1447 nm.e[i] = e[i] - toSub.e[i];
1459 for (
size_t row = 0; row < 4; row++)
1461 for (
size_t col = 0; col < 4; col++)
1463 nm.
e[row * 4 + col] = e[col * 4 + row];
1471 #if defined (_MSC_VER)
1472 #pragma warning(pop)
1482 template <
size_t m,
size_t n,
typename T,
typename S>
1483 mat<m, n, T> operator*(mat<m, n, T> lhs,
const S& rhs)
1495 template <
size_t m,
size_t n,
typename T,
typename S>
1496 mat<m, n, T> operator*(
const S& lhs, mat<m, n, T> rhs)
1508 template <
size_t m,
size_t n,
typename T,
size_t r,
size_t s,
typename S>
1509 mat<m, n, T> operator*(mat<m, n, T>&lhs,
const mat<r, s, S>& rhs)
1511 mat<m, n, T> tmp = lhs;
1522 template <
size_t m,
size_t n,
typename T,
typename S>
1523 mat<m, n, T> operator/(mat<m, n, T> lhs,
const S& rhs)
1535 template <
size_t m,
size_t n,
typename T,
typename S>
1536 mat<m, n, T> operator+(mat<m, n, T> lhs,
const mat<m, n, S>& rhs)
1548 template <
size_t m,
size_t n,
typename T,
typename S>
1549 mat<m, n, T> operator-(mat<m, n, T> lhs,
const mat<m, n, S>& rhs)
1559 typedef mat<2> mat2;
1562 typedef mat<3> mat3;
1565 typedef mat<4> mat4;
1568 typedef mat<2> mat22;
1571 typedef mat<3> mat33;
1574 typedef mat<4> mat44;
1577 typedef mat<2, 2, float> mat2f;
1580 typedef mat<3, 3, float> mat3f;
1583 typedef mat<4, 4, float> mat4f;
1586 typedef mat<2, 2, double> mat2d;
1589 typedef mat<3, 3, double> mat3d;
1592 typedef mat<4, 4, double> mat4d;
1595 typedef mat<2, 2, long double> mat2ld;
1598 typedef mat<3, 3, long double> mat3ld;
1601 typedef mat<4, 4, long double> mat4ld;
1604 typedef mat<2, 2, float> mat22f;
1607 typedef mat<3, 3, float> mat33f;
1610 typedef mat<4, 3, float> mat44f;
1613 typedef mat<2, 2, double> mat22d;
1616 typedef mat<3, 3, double> mat33d;
1619 typedef mat<4, 4, double> mat44d;
1622 typedef mat<2, 2, long double> mat22ld;
1625 typedef mat<3, 3, long double> mat33ld;
1628 typedef mat<4, 4, long double> mat44ld;
1637 template <
size_t mDim,
size_t nDim,
typename T> std::string str(mat<mDim, nDim, T> m)
1639 std::stringstream ss;
1642 for (
size_t row_index = 0; row_index < m.dimRow(); row_index++)
1646 for (
size_t col_index = 0; col_index < m.dimCol(); col_index++)
1648 ss << m(row_index, col_index);
1650 if (col_index + 1 < m.dimCol())
1668 template <
size_t mDim,
size_t nDim,
typename T> std::ostream& operator<<(std::ostream& out, mat<mDim, nDim, T> m)
mat()
Creates a new matrix with uninitialized elements.
Definition: matrix.h:357
T e12
Element 1,2.
Definition: matrix.h:1099
static mat one()
Matrix with all of its elements set to 1.
Definition: matrix.h:406
mat & operator*=(const mat< 4, 4, S > &rhs)
Multiplies the matrix by another matrix.
Definition: matrix.h:1279
T & operator()(size_t row, size_t col)
Indexing into the matrix's elements.
Definition: matrix.h:91
const T & operator()(size_t row, size_t col) const
Indexing into the matrix's elements.
Definition: matrix.h:1235
static mat< 4, 4, T > identity()
Identity matrix with its diagonal elements set to 1.
Definition: matrix.h:1208
mat(vec< 3, T > col0, vec< 3, T > col1, vec< 3, T > col2)
Constructs a matrix from 3 column vectors. Vectors are stored in column order.
Definition: matrix.h:763
T e02
Element 0,2.
Definition: matrix.h:1096
mat add(const mat &toAdd) const
Adds a matrix to this matrix.
Definition: matrix.h:1428
mat(T e00v, T e01v, T e10v, T e11v)
Creates a new matrix with its components initialized to the provided values.
Definition: matrix.h:374
const T & operator()(size_t row, size_t col) const
Indexing into the matrix's elements.
Definition: matrix.h:440
T e33
Element 3,3.
Definition: matrix.h:1117
T & operator()(size_t row, size_t col)
Indexing into the matrix's elements.
Definition: matrix.h:811
mat & operator-=(const mat< m, n, S > &rhs)
Subtracts a matrix from this matrix.
Definition: matrix.h:202
Vector with 4 component specialization.
Definition: vector.h:869
mat neg() const
Negates the matrix.
Definition: matrix.h:973
mat & operator+=(const mat< m, n, S > &rhs)
Adds a matrix to this matrix.
Definition: matrix.h:189
mat add(const mat &toAdd) const
Adds a matrix to this matrix.
Definition: matrix.h:632
T e21
Element 2,1.
Definition: matrix.h:1090
T e12
Element 1,2.
Definition: matrix.h:711
mat div(const double &scalar) const
Divides the matrix by a scalar.
Definition: matrix.h:1414
mat div(const double &scalar) const
Divides the matrix by a scalar.
Definition: matrix.h:1001
mat & operator/=(const S &rhs)
Divides the matrix by a scalar.
Definition: matrix.h:521
3x3 matrix specialization.
Definition: matrix.h:678
T e21
Element 2,1.
Definition: matrix.h:705
T e02
Element 0,2.
Definition: matrix.h:708
T e01
Element 0,1.
Definition: matrix.h:699
mat & operator*=(const mat< 3, 3, S > &rhs)
Multiplies the matrix by another matrix.
Definition: matrix.h:865
T e01
Element 0,1.
Definition: matrix.h:1084
mat mult(const double &scalar) const
Multiplies the matrix by a scalar.
Definition: matrix.h:1398
mat operator-() const
Negates the matrix.
Definition: matrix.h:450
mat & operator*=(const S &rhs)
Multiplies the matrix by a scalar.
Definition: matrix.h:841
mat< 2, 2, T > transpose() const
Transposes the matrix.
Definition: matrix.h:659
2x2 matrix specialization.
Definition: matrix.h:324
mat add(const mat &toAdd) const
Adds a matrix to this matrix.
Definition: matrix.h:1015
size_t dimRow() const
The matrix's row dimension.
Definition: matrix.h:956
mat & operator+=(const mat< 3, 3, S > &rhs)
Adds a matrix to this matrix.
Definition: matrix.h:928
T & operator()(size_t row, size_t col)
Indexing into the matrix's elements.
Definition: matrix.h:430
mat & operator*=(const S &rhs)
Multiplies the matrix by a scalar.
Definition: matrix.h:460
size_t dimCol() const
The matrix's column dimension.
Definition: matrix.h:961
mat< 3, 3, T > transpose() const
Transposes the matrix.
Definition: matrix.h:1042
T e01
Element 0,1.
Definition: matrix.h:342
static mat zero()
Matrix with all of its elements set to 0.
Definition: matrix.h:778
mat(vec< 2, T > col0, vec< 2, T > col1)
Constructs a matrix from 4 column vectors.
Definition: matrix.h:386
size_t dimCol() const
The matrix's column dimension.
Definition: matrix.h:222
static mat one()
Matrix with all of its elements set to 1.
Definition: matrix.h:1200
T e11
Element 1,1.
Definition: matrix.h:1087
T e10
Element 1,0.
Definition: matrix.h:693
mat< n, m, T > transpose() const
Transposes the matrix.
Definition: matrix.h:296
mat(T e00v, T e01v, T e02v, T e10v, T e11v, T e12v, T e20v, T e21v, T e22v)
Creates a new matrix with its components intialized to the provided values.
Definition: matrix.h:749
T e20
Element 2,0.
Definition: matrix.h:696
mat & operator-=(const mat< 4, 4, S > &rhs)
Subtracts a matrix from this matrix.
Definition: matrix.h:1353
T e[4 *4]
The matrix's elements.
Definition: matrix.h:1121
mat & operator*=(const S &rhs)
Multiplies the matrix by a scalar.
Definition: matrix.h:122
Template for a matrix.
Definition: matrix.h:20
T e22
Element 2,2.
Definition: matrix.h:714
size_t dimCol() const
The matrix's column dimension.
Definition: matrix.h:579
mat sub(const mat &toSub) const
Subtracts a matrix from this matrix.
Definition: matrix.h:283
mat mult(const double &scalar) const
Multiplies the matrix by a scalar.
Definition: matrix.h:604
Vector with 3 component specialization.
Definition: vector.h:551
T e10
Element 1,0.
Definition: matrix.h:1075
mat(T val)
Creates a new matrix with its elements initialized to val.
Definition: matrix.h:362
mat & operator/=(const S &rhs)
Divides the matrix by a scalar.
Definition: matrix.h:904
T e00
Element 0,0.
Definition: matrix.h:690
mat()
Creates a new matrix with uninitialized elements.
Definition: matrix.h:726
size_t dimRow() const
The matrix's row dimension.
Definition: matrix.h:573
T e30
Element 3,0.
Definition: matrix.h:1081
size_t dimRow() const
The matrix's row dimension.
Definition: matrix.h:1368
mat add(const mat &toAdd) const
Adds a matrix to this matrix.
Definition: matrix.h:269
mat sub(const mat &toSub) const
Subtracts a matrix from this matrix.
Definition: matrix.h:646
const T & operator()(size_t row, size_t col) const
Indexing into the matrix's elements.
Definition: matrix.h:821
mat & operator+=(const mat< 2, 2, S > &rhs)
Adds a matrix to this matrix.
Definition: matrix.h:545
static mat one()
Matrix with all of its elements set to 1.
Definition: matrix.h:786
mat & operator*=(const mat< 2, 2, S > &rhs)
Multiplies the matrix by another matrix.
Definition: matrix.h:484
mat operator-() const
Negates the matrix.
Definition: matrix.h:112
T e20
Element 2,0.
Definition: matrix.h:1078
mat div(const double &scalar) const
Divides the matrix by a scalar.
Definition: matrix.h:618
mat & operator*=(const S &rhs)
Multiplies the matrix by a scalar.
Definition: matrix.h:1255
size_t dimCol() const
The matrix's column dimension.
Definition: matrix.h:1373
mat neg() const
Negates the matrix.
Definition: matrix.h:590
mat div(const double &scalar) const
Divides the matrix by a scalar.
Definition: matrix.h:255
T e22
Element 2,2.
Definition: matrix.h:1102
T e[3 *3]
The matrix's elements.
Definition: matrix.h:718
T e13
Element 1,3.
Definition: matrix.h:1111
T e00
Element 0,0.
Definition: matrix.h:336
mat & operator+=(const mat< 4, 4, S > &rhs)
Adds a matrix to this matrix.
Definition: matrix.h:1340
T e31
Element 3,1.
Definition: matrix.h:1093
static mat one()
Matrix with all of its elements set to 1.
Definition: matrix.h:62
mat< 4, 4, T > transpose() const
Transposes the matrix.
Definition: matrix.h:1455
T e00
Element 0,0.
Definition: matrix.h:1072
mat sub(const mat &toSub) const
Subtracts a matrix from this matrix.
Definition: matrix.h:1029
T e11
Element 1,1.
Definition: matrix.h:702
mat()
Creates a new matrix with uninitialized elements.
Definition: matrix.h:37
mat(vec< 4, T > col0, vec< 4, T > col1, vec< 4, T > col2, vec< 4, T > col3)
Constructs a matrix from 4 column vectors.
Definition: matrix.h:1176
mat operator-() const
Negates the matrix.
Definition: matrix.h:831
mat & operator-=(const mat< 3, 3, S > &rhs)
Subtracts a matrix from this matrix.
Definition: matrix.h:941
mat(T val)
Creates a new matrix with ints elements initialized to val.
Definition: matrix.h:42
mat & operator*(const mat< r, s, S > &rhs)
Multiplies the matrix by another matrix.
Definition: matrix.h:135
T e10
Element 1,0.
Definition: matrix.h:339
static mat zero()
Matrix with all of its elements set to 0.
Definition: matrix.h:398
mat neg() const
Negates the matrix.
Definition: matrix.h:227
mat & operator-=(const mat< 2, 2, S > &rhs)
Subtracts a matrix from this matrix.
Definition: matrix.h:558
T e[2 *2]
The matrix's elements.
Definition: matrix.h:349
T & operator()(size_t row, size_t col)
Indexing into the matrix's elements.
Definition: matrix.h:1225
const T & operator()(size_t row, size_t col) const
Indexing into the matrix's elements.
Definition: matrix.h:101
mat(T val)
Creates a new matrix with its elements initialized to val.
Definition: matrix.h:731
static mat< m, m, T > identity()
Identity matrix with its diagonal elements set to 1.
Definition: matrix.h:70
Vector with 2 component specialization.
Definition: vector.h:279
T e23
Element 2,3.
Definition: matrix.h:1114
4x4 matrix specialization.
Definition: matrix.h:1060
mat()
Creates a new matrix with uninitialized elements.
Definition: matrix.h:1129
mat(T e00v, T e01v, T e02v, T e03v, T e10v, T e11v, T e12v, T e13v, T e20v, T e21v, T e22v, T e23v, T e30v, T e31v, T e32v, T e33v)
Creates a new matrix with its components intialized to the provided values.
Definition: matrix.h:1160
size_t dimRow() const
The matrix's row dimension.
Definition: matrix.h:217
T e32
Element 3,2.
Definition: matrix.h:1105
mat(T val)
Creates a new matrix with its elements initialized to val.
Definition: matrix.h:1134
mat mult(const double &scalar) const
Multiplies the matrix by a scalar.
Definition: matrix.h:987
mat & operator/=(const S &rhs)
Divides the matrix by a scalar.
Definition: matrix.h:1316
mat mult(const double &scalar) const
Multiplies the matrix by a scalar.
Definition: matrix.h:241
mat neg() const
Negates the matrix.
Definition: matrix.h:1384
static mat zero()
Matrix with all of its elements set to 0.
Definition: matrix.h:1192
static mat zero()
Matrix with all of its elements set to 0.
Definition: matrix.h:54
static mat< 3, 3, T > identity()
Identity matrix with its diagonal elements set to 1.
Definition: matrix.h:794
T e03
Element 0,3.
Definition: matrix.h:1108
static mat< 2, 2, T > identity()
Identity matrix with its diagonal elements set to 1.
Definition: matrix.h:414
mat sub(const mat &toSub) const
Subtracts a matrix from this matrix.
Definition: matrix.h:1442
mat operator-() const
Negates the matrix.
Definition: matrix.h:1245
mat & operator/=(const T &rhs)
Divides the matrix by a scalar.
Definition: matrix.h:176
T e11
Element 1,1.
Definition: matrix.h:345