27#ifndef EASY3D_CORE_MAT_H
28#define EASY3D_CORE_MAT_H
33#include <easy3d/core/vec.h>
34#include <easy3d/core/constant.h>
35#include <easy3d/util/logging.h>
46 template <
typename T>
class Mat2;
47 template <
typename T>
class Mat3;
48 template <
typename T>
class Mat4;
62 template <
size_t N,
size_t M,
typename T>
90 template <
size_t rN,
size_t rM>
97 explicit Mat(
const T *m);
156 operator const T*()
const;
350 template <
size_t N,
size_t M,
typename T>
362 template <
typename T>
372 template <
typename T>
382 template <
typename T>
394 template <
typename T>
404 template <
typename T>
414 template <
typename T>
424 template <
typename T>
434 template <
typename T>
446 template <
size_t N,
typename T>
458 template <
size_t N,
typename T>
469 template <
size_t N,
size_t M,
typename T>
481 template <
size_t N,
typename T>
494 template <
size_t M,
size_t N,
typename T>
509 template<
size_t N,
size_t M,
typename T>
510 bool gauss_jordan_elimination(
const Mat<N, N, T> &a,
const Mat<N, M, T> &b,
Mat<N, N, T> *ainv,
Mat<N, M, T> *x);
527 template<
size_t N,
typename T>
589 template <
size_t N,
typename T>
604 template<
size_t N,
typename FT>
615 template <
size_t N,
typename FT>
628 template<
size_t N,
size_t M,
typename T>
640 template <
size_t N,
size_t M,
typename T>
652 template <
size_t N,
size_t M,
typename T>
663 template <
size_t N,
size_t M,
typename T>
665 for (
size_t i = 0; i < N; ++i) {
666 for (
size_t j = 0; j < M; ++j)
668 (*this)(i, j) = T(s);
670 (*
this)(i, j) = T(0);
675 template <
size_t N,
size_t M,
typename T>
676 template <
size_t rN,
size_t rM>
681 for (
size_t i = 0; i < N; ++i)
682 for (
size_t j = 0; j < M; ++j)
683 (*
this)(i, j) = rhs(i, j);
687 template <
size_t N,
size_t M,
typename T>
689 assert(m !=
nullptr);
691 for (
size_t i = 0; i < N * M; ++i)
696 template <
size_t N,
size_t M,
typename T>
699 for (
size_t i = 0; i < N; ++i) {
700 for (
size_t j = 0; j < M; ++j)
711 template <
size_t N,
size_t M,
typename T>
716 for (
size_t i = 0; i < M; ++i)
717 result[i] = (*
this)(r, i);
722 template <
size_t N,
size_t M,
typename T>
727 for (
size_t i = 0; i < N; ++i)
728 result[i] = (*
this)(i, c);
733 template <
size_t N,
size_t M,
typename T>
739 for (
size_t i = 0; i < M; ++i)
740 (*
this)(r, i) = v[i];
744 template <
size_t N,
size_t M,
typename T>
750 for (
size_t i = 0; i < N; ++i)
751 (*
this)(i, c) = v[i];
755 template <
size_t N,
size_t M,
typename T>
760 for (
size_t i = 0; i < M; ++i) {
761 T tmp = (*this)(a, i);
762 (*this)(a, i) = (*
this)(b, i);
768 template <
size_t N,
size_t M,
typename T>
771 for (
size_t i = 0; i < size; ++i)
776 template <
size_t N,
size_t M,
typename T>
778 for (
size_t i = 0; i < N; ++i) {
779 for (
size_t j = 0; j < M; ++j)
781 (*this)(i, j) = T(s);
783 (*
this)(i, j) = T(0);
788 template <
size_t N,
size_t M,
typename T>
793 for (
size_t i = 0; i < N; ++i) {
794 T tmp = (*this)(i, a);
795 (*this)(i, a) = (*
this)(i, b);
801 template <
size_t N,
size_t M,
typename T>
807 template <
size_t N,
size_t M,
typename T>
813 template <
size_t N,
size_t M,
typename T>
818 #ifdef MATRIX_ROW_MAJOR
826 template <
size_t N,
size_t M,
typename T>
831 #ifdef MATRIX_ROW_MAJOR
839 template <
size_t N,
size_t M,
typename T>
842 for (
size_t i = 0; i < N * M; ++i)
848 template <
size_t N,
size_t M,
typename T>
850 return !(*
this == rhs);
855 template <
size_t N,
size_t M,
typename T>
859 for (
size_t i = 0; i < N; ++i) {
860 for (
size_t j = 0; j < rM; ++j) {
862 for (
size_t k = 0; k < M; ++k) {
863 result(i, j) += (*this)(i, k) * rhs(k, j);
871 template <
size_t N,
size_t M,
typename T>
874 for (
size_t i = 0; i < N * M; ++i)
875 result[i] = m_[i] + rhs[i];
880 template <
size_t N,
size_t M,
typename T>
883 for (
size_t i = 0; i < N * M; ++i)
884 result[i] = m_[i] - rhs[i];
889 template <
size_t N,
size_t M,
typename T>
892 for (
size_t i = 0; i < N * M; ++i)
901 template <
size_t N,
size_t M,
typename T>
904 for (
size_t i = 0; i < N; ++i) {
906 for (
size_t j = 0; j < M; ++j) {
907 result[i] += (*this)(i, j) * rhs[j];
917 template <
size_t N,
size_t M,
typename T>
920 for (
size_t i = 0; i < N * M; ++i)
921 result[i] = m_[i] * rhs;
926 template <
size_t N,
size_t M,
typename T>
929 for (
size_t i = 0; i < N * M; ++i)
930 result[i] = m_[i] / rhs;
937 template <
size_t N,
size_t M,
typename T>
945 template <
size_t N,
size_t M,
typename T>
947 for (
size_t i = 0; i < N * M; ++i)
953 template <
size_t N,
size_t M,
typename T>
955 for (
size_t i = 0; i < N * M; ++i)
964 template <
size_t N,
size_t M,
typename T>
966 for (
size_t i = 0; i < N * M; ++i)
972 template <
size_t N,
size_t M,
typename T>
974 for (
size_t i = 0; i < N * M; ++i)
980 template <
size_t N,
size_t M,
typename T>
982 for (
size_t i = 0; i < N * M; ++i)
988 template <
size_t N,
size_t M,
typename T>
990 for (
size_t i = 0; i < N * M; ++i)
999 template <
size_t N,
size_t M,
typename T>
1002 for (
size_t i = 0; i < N * M; ++i)
1003 result[i] = lhs * rhs[i];
1010 template <
typename T>
1013 for (
size_t i = 0; i < 2; ++i) {
1014 for (
size_t j = 0; j < 2; ++j) {
1015 result(i, j) = T(0);
1016 for (
size_t k = 0; k < 2; ++k) {
1017 result(i, j) += lhs(i, k) * rhs(k, j);
1024 template <
typename T>
1027 for (
size_t i = 0; i < 3; ++i) {
1028 for (
size_t j = 0; j < 3; ++j) {
1029 result(i, j) = T(0);
1030 for (
size_t k = 0; k < 3; ++k) {
1031 result(i, j) += lhs(i, k) * rhs(k, j);
1038 template <
typename T>
1041 for (
size_t i = 0; i < 4; ++i) {
1042 for (
size_t j = 0; j < 4; ++j) {
1043 result(i, j) = T(0);
1044 for (
size_t k = 0; k < 4; ++k) {
1045 result(i, j) += lhs(i, k) * rhs(k, j);
1055 template <
typename T>
1059 result /= result[3];
1064 template <
typename T>
1068 result /= result[2];
1074 template <
typename T>
1077 for (
size_t i = 0; i < 2; ++i) {
1079 for (
size_t j = 0; j < 2; ++j) {
1080 result[i] += lhs(i, j) * rhs[j];
1088 template <
typename T>
1091 for (
size_t i = 0; i < 3; ++i) {
1093 for (
size_t j = 0; j < 3; ++j) {
1094 result[i] += lhs(i, j) * rhs[j];
1102 template <
typename T>
1105 for (
size_t i = 0; i < 4; ++i) {
1107 for (
size_t j = 0; j < 4; ++j) {
1108 result[i] += lhs(i, j) * rhs[j];
1119 template <
size_t N,
size_t M,
typename T>
1122 for (
size_t i = 0; i < N; ++i) {
1123 for (
size_t j = 0; j < M; ++j) {
1124 result(j, i) = m(i, j);
1132 template <
size_t D,
typename T>
1135 for (
size_t i = 1; i < D; ++i)
1142 template <
size_t N,
typename T>
1149 for (
size_t i = 0; i < N; ++i)
1150 result *= tmp(i, i);
1155 template <
typename T>
1157 return m(0, 0) * m(1, 1) - m(0, 1) * m(1, 0);
1161 template <
typename T>
1164 m(0, 0) * (m(1, 1) * m(2, 2) - m(2, 1) * m(1, 2)) +
1165 m(0, 1) * (m(2, 0) * m(1, 2) - m(1, 0) * m(2, 2)) +
1166 m(0, 2) * (m(1, 0) * m(2, 1) - m(2, 0) * m(1, 1));
1170 template <
typename T>
1173 m(0, 3) * m(1, 2) * m(2, 1) * m(3, 0) - m(0, 2) * m(1, 3) * m(2, 1) * m(3, 0) -
1174 m(0, 3) * m(1, 1) * m(2, 2) * m(3, 0) + m(0, 1) * m(1, 3) * m(2, 2) * m(3, 0) +
1175 m(0, 2) * m(1, 1) * m(2, 3) * m(3, 0) - m(0, 1) * m(1, 2) * m(2, 3) * m(3, 0) -
1176 m(0, 3) * m(1, 2) * m(2, 0) * m(3, 1) + m(0, 2) * m(1, 3) * m(2, 0) * m(3, 1) +
1177 m(0, 3) * m(1, 0) * m(2, 2) * m(3, 1) - m(0, 0) * m(1, 3) * m(2, 2) * m(3, 1) -
1178 m(0, 2) * m(1, 0) * m(2, 3) * m(3, 1) + m(0, 0) * m(1, 2) * m(2, 3) * m(3, 1) +
1179 m(0, 3) * m(1, 1) * m(2, 0) * m(3, 2) - m(0, 1) * m(1, 3) * m(2, 0) * m(3, 2) -
1180 m(0, 3) * m(1, 0) * m(2, 1) * m(3, 2) + m(0, 0) * m(1, 3) * m(2, 1) * m(3, 2) +
1181 m(0, 1) * m(1, 0) * m(2, 3) * m(3, 2) - m(0, 0) * m(1, 1) * m(2, 3) * m(3, 2) -
1182 m(0, 2) * m(1, 1) * m(2, 0) * m(3, 3) + m(0, 1) * m(1, 2) * m(2, 0) * m(3, 3) +
1183 m(0, 2) * m(1, 0) * m(2, 1) * m(3, 3) - m(0, 0) * m(1, 2) * m(2, 1) * m(3, 3) -
1184 m(0, 1) * m(1, 0) * m(2, 2) * m(3, 3) + m(0, 0) * m(1, 1) * m(2, 2) * m(3, 3);
1189 template <
size_t N,
typename T>
1193 size_t indxc[N], indxr[N], ipiv[N];
1197 for (
size_t i = 0; i < N; ++i)
1200 for (
size_t i = 0; i < N; ++i) {
1203 for (
size_t j = 0; j < N; ++j) {
1205 for (
size_t k = 0; k < N; ++k) {
1207 T element = std::abs(result(j, k));
1208 if (element >
max) {
1226 if (std::abs(result(maxc, maxc)) <
epsilon<T>()) {
1227 LOG(ERROR) <<
"input matrix is singular";
1232 T rpivot = T(1) / result(maxc, maxc);
1233 result(maxc, maxc) = T(1);
1234 result.
set_row(maxc, result.
row(maxc) * rpivot);
1237 for (
size_t j = 0; j < N; ++j) {
1239 T dum = result(j, maxc);
1240 result(j, maxc) = T(0);
1241 for (
size_t k = 0; k < N; ++k)
1242 result(j, k) -= result(maxc, k) * dum;
1251 if (indxr[i] != indxc[i])
1259 template <
typename T>
1262 result(0, 0) = m(1, 1);
1263 result(0, 1) = -m(0, 1);
1264 result(1, 0) = -m(1, 0);
1265 result(1, 1) = m(0, 0);
1275 template <
typename T>
1278 result(0, 0) = (m(1, 1) * m(2, 2) - m(2, 1) * m(1, 2));
1279 result(0, 1) = -(m(0, 1) * m(2, 2) - m(0, 2) * m(2, 1));
1280 result(0, 2) = (m(0, 1) * m(1, 2) - m(0, 2) * m(1, 1));
1281 result(1, 0) = -(m(1, 0) * m(2, 2) - m(1, 2) * m(2, 0));
1282 result(1, 1) = (m(0, 0) * m(2, 2) - m(0, 2) * m(2, 0));
1283 result(1, 2) = -(m(0, 0) * m(1, 2) - m(1, 0) * m(0, 2));
1284 result(2, 0) = (m(1, 0) * m(2, 1) - m(2, 0) * m(1, 1));
1285 result(2, 1) = -(m(0, 0) * m(2, 1) - m(2, 0) * m(0, 1));
1286 result(2, 2) = (m(0, 0) * m(1, 1) - m(1, 0) * m(0, 1));
1296 template <
typename T>
1299 result(0, 0) = m(1, 2) * m(2, 3) * m(3, 1) - m(1, 3) * m(2, 2) * m(3, 1) + m(1, 3) * m(2, 1) * m(3, 2) - m(1, 1) * m(2, 3) * m(3, 2) - m(1, 2) * m(2, 1) * m(3, 3) + m(1, 1) * m(2, 2) * m(3, 3);
1300 result(0, 1) = m(0, 3) * m(2, 2) * m(3, 1) - m(0, 2) * m(2, 3) * m(3, 1) - m(0, 3) * m(2, 1) * m(3, 2) + m(0, 1) * m(2, 3) * m(3, 2) + m(0, 2) * m(2, 1) * m(3, 3) - m(0, 1) * m(2, 2) * m(3, 3);
1301 result(0, 2) = m(0, 2) * m(1, 3) * m(3, 1) - m(0, 3) * m(1, 2) * m(3, 1) + m(0, 3) * m(1, 1) * m(3, 2) - m(0, 1) * m(1, 3) * m(3, 2) - m(0, 2) * m(1, 1) * m(3, 3) + m(0, 1) * m(1, 2) * m(3, 3);
1302 result(0, 3) = m(0, 3) * m(1, 2) * m(2, 1) - m(0, 2) * m(1, 3) * m(2, 1) - m(0, 3) * m(1, 1) * m(2, 2) + m(0, 1) * m(1, 3) * m(2, 2) + m(0, 2) * m(1, 1) * m(2, 3) - m(0, 1) * m(1, 2) * m(2, 3);
1303 result(1, 0) = m(1, 3) * m(2, 2) * m(3, 0) - m(1, 2) * m(2, 3) * m(3, 0) - m(1, 3) * m(2, 0) * m(3, 2) + m(1, 0) * m(2, 3) * m(3, 2) + m(1, 2) * m(2, 0) * m(3, 3) - m(1, 0) * m(2, 2) * m(3, 3);
1304 result(1, 1) = m(0, 2) * m(2, 3) * m(3, 0) - m(0, 3) * m(2, 2) * m(3, 0) + m(0, 3) * m(2, 0) * m(3, 2) - m(0, 0) * m(2, 3) * m(3, 2) - m(0, 2) * m(2, 0) * m(3, 3) + m(0, 0) * m(2, 2) * m(3, 3);
1305 result(1, 2) = m(0, 3) * m(1, 2) * m(3, 0) - m(0, 2) * m(1, 3) * m(3, 0) - m(0, 3) * m(1, 0) * m(3, 2) + m(0, 0) * m(1, 3) * m(3, 2) + m(0, 2) * m(1, 0) * m(3, 3) - m(0, 0) * m(1, 2) * m(3, 3);
1306 result(1, 3) = m(0, 2) * m(1, 3) * m(2, 0) - m(0, 3) * m(1, 2) * m(2, 0) + m(0, 3) * m(1, 0) * m(2, 2) - m(0, 0) * m(1, 3) * m(2, 2) - m(0, 2) * m(1, 0) * m(2, 3) + m(0, 0) * m(1, 2) * m(2, 3);
1307 result(2, 0) = m(1, 1) * m(2, 3) * m(3, 0) - m(1, 3) * m(2, 1) * m(3, 0) + m(1, 3) * m(2, 0) * m(3, 1) - m(1, 0) * m(2, 3) * m(3, 1) - m(1, 1) * m(2, 0) * m(3, 3) + m(1, 0) * m(2, 1) * m(3, 3);
1308 result(2, 1) = m(0, 3) * m(2, 1) * m(3, 0) - m(0, 1) * m(2, 3) * m(3, 0) - m(0, 3) * m(2, 0) * m(3, 1) + m(0, 0) * m(2, 3) * m(3, 1) + m(0, 1) * m(2, 0) * m(3, 3) - m(0, 0) * m(2, 1) * m(3, 3);
1309 result(2, 2) = m(0, 1) * m(1, 3) * m(3, 0) - m(0, 3) * m(1, 1) * m(3, 0) + m(0, 3) * m(1, 0) * m(3, 1) - m(0, 0) * m(1, 3) * m(3, 1) - m(0, 1) * m(1, 0) * m(3, 3) + m(0, 0) * m(1, 1) * m(3, 3);
1310 result(2, 3) = m(0, 3) * m(1, 1) * m(2, 0) - m(0, 1) * m(1, 3) * m(2, 0) - m(0, 3) * m(1, 0) * m(2, 1) + m(0, 0) * m(1, 3) * m(2, 1) + m(0, 1) * m(1, 0) * m(2, 3) - m(0, 0) * m(1, 1) * m(2, 3);
1311 result(3, 0) = m(1, 2) * m(2, 1) * m(3, 0) - m(1, 1) * m(2, 2) * m(3, 0) - m(1, 2) * m(2, 0) * m(3, 1) + m(1, 0) * m(2, 2) * m(3, 1) + m(1, 1) * m(2, 0) * m(3, 2) - m(1, 0) * m(2, 1) * m(3, 2);
1312 result(3, 1) = m(0, 1) * m(2, 2) * m(3, 0) - m(0, 2) * m(2, 1) * m(3, 0) + m(0, 2) * m(2, 0) * m(3, 1) - m(0, 0) * m(2, 2) * m(3, 1) - m(0, 1) * m(2, 0) * m(3, 2) + m(0, 0) * m(2, 1) * m(3, 2);
1313 result(3, 2) = m(0, 2) * m(1, 1) * m(3, 0) - m(0, 1) * m(1, 2) * m(3, 0) - m(0, 2) * m(1, 0) * m(3, 1) + m(0, 0) * m(1, 2) * m(3, 1) + m(0, 1) * m(1, 0) * m(3, 2) - m(0, 0) * m(1, 1) * m(3, 2);
1314 result(3, 3) = m(0, 1) * m(1, 2) * m(2, 0) - m(0, 2) * m(1, 1) * m(2, 0) + m(0, 2) * m(1, 0) * m(2, 1) - m(0, 0) * m(1, 2) * m(2, 1) - m(0, 1) * m(1, 0) * m(2, 2) + m(0, 0) * m(1, 1) * m(2, 2);
1324 template <
size_t M,
size_t N,
typename T>
1333 template <
size_t N,
size_t M,
typename T>
1340 size_t indxc[N], indxr[N], ipiv[N];
1347 for (
size_t i = 0; i < N; ++i)
1350 for (
size_t i = 0; i < N; ++i) {
1353 for (
size_t j = 0; j < N; ++j) {
1355 for (
size_t k = 0; k < N; ++k) {
1357 T element = std::abs(amat(j, k));
1358 if (element >
max) {
1378 if (std::abs(amat(maxc, maxc)) <
epsilon<T>())
1382 T rpivot = T(1) / amat(maxc, maxc);
1383 amat(maxc, maxc) = T(1);
1388 for (
size_t j = 0; j < N; ++j) {
1390 T dum = amat(j, maxc);
1391 amat(j, maxc) = T(0);
1392 for (
size_t k = 0; k < N; ++k)
1393 amat(j, k) -= amat(maxc, k) * dum;
1394 for (
size_t k = 0; k < M; ++k)
1395 bmat(j, k) -= bmat(maxc, k) * dum;
1404 if (indxr[i] != indxc[i])
1413 template <
size_t N,
typename T>
1425 for (
size_t i = 0; i < N; ++i) {
1428 for (
size_t j = 0; j < N; ++j) {
1429 T element = std::abs(amat(i, j));
1438 scalev[i] = T(1) /
max;
1441 for (
size_t j = 0; j < N; ++j) {
1442 for (
size_t i = 0; i < j; ++i) {
1444 for (
size_t k = 0; k < i; ++k)
1445 sum -= amat(i, k) * amat(k, j);
1451 for (
size_t i = j; i < N; ++i) {
1453 for (
size_t k = 0; k < j; ++k)
1454 sum -= amat(i, k) * amat(k, j);
1457 T dum = scalev[i] * std::abs(
sum);
1466 scalev[imax] = scalev[j];
1469 (*rowp)[j] =
static_cast<T
>(imax);
1477 T dum = T(1) / amat(j, j);
1478 for (
size_t i = j + 1; i < N; ++i)
1489 template <
size_t N,
typename T>
1501 for (
size_t i = 0; i < N; ++i) {
1502 auto ip =
static_cast<size_t>(rowp[i]);
1506 result[ip] = result[i];
1508 for (
size_t j = ii - 1; j < i; ++j)
1509 sum -= alu(i, j) * result[j];
1521 for (
size_t j = i + 1; j < N; ++j)
1522 sum -= alu(i, j) * result[j];
1523 result[i] =
sum / alu(i, i);
1530 template<
size_t N,
typename FT>
1533 for (
size_t j = 0; j < N; ++j) {
1535 for (
size_t k = 0; k < j; ++k) {
1537 for (
size_t i = 0; i < k; ++i)
1538 s += L(k, i) * L(j, i);
1540 L(j, k) = s = (A(j, k) - s) / L(k, k);
1542 spd = spd && (A(k, j) == A(j, k));
1546 spd = spd && (d > 0);
1548 L(j, j) = std::sqrt(d > 0 ? d : 0);
1549 for (
size_t k = j + 1; k < N; ++k)
1557 template <
size_t N,
typename FT>
1561 for (
size_t k = 0; k < N; ++k) {
1562 for (
size_t i = 0; i < k; ++i)
1563 x[k] -= x[i] * L(k, i);
1568 for (
int k = N - 1; k >= 0; --k) {
1569 for (
size_t i = k + 1; i < N; ++i)
1570 x[k] -= x[i] * L(i, k);
1575 template<
size_t N,
size_t M,
typename T>
1579 for (
size_t j = 0; j < M; ++j) {
1580 for (
size_t k = 0; k < N; ++k) {
1581 for (
size_t i = 0; i < k; ++i)
1582 X(k, j) -= X(i, j) * L(k, i);
1589 for (
size_t j = 0; j < M; ++j) {
1590 for (
int k = N - 1; k >= 0; --k) {
1591 for (
size_t i = k + 1; i < N; ++i)
1592 X(k, j) -= X(i, j) * L(i, k);
1602 template <
size_t N,
size_t M,
typename T>
inline
1604 output << std::fixed << std::setprecision(8);
1605 const char sep =
' ';
1606 for (
int i = 0; i < N; i++) {
1607 for (
int j = 0; j < M; j++) {
1608 output << sep << std::setw(7) << m(i, j);
1610 output << std::endl;
1612 output << std::resetiosflags(std::ios_base::fixed | std::ios_base::floatfield);
1618 template <
size_t N,
size_t M,
typename T>
1620 for (
int i = 0; i < N; i++) {
1621 for (
int j = 0; j < M; j++) {
1632 template<
size_t N,
typename FT>
1639 template<
size_t N,
typename FT>
1647 template <
size_t N,
size_t M,
typename T>
1649 for (
int i = 0; i < N; i++) {
1650 for (
int j = 0; j < M; j++) {
1651 if (std::isnan(m(i, j)) || std::isinf(m(i, j)))
1671 template <
typename T>
1742 template <
typename T>
1744 for (
size_t i = 0; i < 2; ++i) {
1745 for (
size_t j = 0; j < 2; ++j)
1747 (*this)(i, j) = T(s);
1749 (*
this)(i, j) = T(0);
1754 template <
typename T>
1756 for (
size_t i = 0; i < 4; ++i)
1757 (*this).m_[i] = rhs[i];
1761 template <
typename T>
1763 (*this)(0, 0) = rhs(0, 0); (*this)(0, 1) = rhs(0, 1);
1764 (*this)(1, 0) = rhs(1, 0); (*this)(1, 1) = rhs(1, 1);
1768 template <
typename T>
1773 (*this)(0, 0) = s00; (*this)(0, 1) = s01;
1774 (*this)(1, 0) = s10; (*this)(1, 1) = s11;
1778 template <
typename T>
1780 assert(m !=
nullptr);
1782 #ifdef MATRIX_ROW_MAJOR
1783 (*this)(0, 0) = m[0]; (*this)(0, 1) = m[1];
1784 (*this)(1, 0) = m[2]; (*this)(1, 1) = m[3];
1786 (*this)(0, 0) = m[0]; (*this)(0, 1) = m[2];
1787 (*this)(1, 0) = m[1]; (*this)(1, 1) = m[3];
1792 template <
typename T>
1794 #ifdef MATRIX_ROW_MAJOR
1795 (*this).set_row(0, x);
1796 (*this).set_row(1, y);
1798 (*this).set_col(0, x);
1799 (*this).set_col(1, y);
1804 template <
typename T>
1807 std::cos(angle), -std::sin(angle),
1808 std::sin(angle), std::cos(angle)
1813 template <
typename T>
1822 template <
typename T>
1836 template <
typename T>
class Quat;
1843 template <
typename T>
1874 T s00, T s01, T s02,
1875 T s10, T s11, T s12,
1966 template <
typename T>
1968 for (
size_t i = 0; i < 3; ++i) {
1969 for (
size_t j = 0; j < 3; ++j)
1971 (*this)(i, j) = T(s);
1973 (*
this)(i, j) = T(0);
1978 template <
typename T>
1980 for (
size_t i = 0; i < 9; ++i)
1981 (*this).m_[i] = rhs[i];
1985 template <
typename T>
1987 (*this)(0, 0) = rhs(0, 0); (*this)(0, 1) = rhs(0, 1); (*this)(0, 2) = rhs(0, 2);
1988 (*this)(1, 0) = rhs(1, 0); (*this)(1, 1) = rhs(1, 1); (*this)(1, 2) = rhs(1, 2);
1989 (*this)(2, 0) = rhs(2, 0); (*this)(2, 1) = rhs(2, 1); (*this)(2, 2) = rhs(2, 2);
1993 template <
typename T>
1995 T s00, T s01, T s02,
1996 T s10, T s11, T s12,
1999 (*this)(0, 0) = s00; (*this)(0, 1) = s01; (*this)(0, 2) = s02;
2000 (*this)(1, 0) = s10; (*this)(1, 1) = s11; (*this)(1, 2) = s12;
2001 (*this)(2, 0) = s20; (*this)(2, 1) = s21; (*this)(2, 2) = s22;
2005 template <
typename T>
2007 assert(m !=
nullptr);
2009 #ifdef MATRIX_ROW_MAJOR
2010 (*this)(0, 0) = m[0]; (*this)(0, 1) = m[1]; (*this)(0, 2) = m[2];
2011 (*this)(1, 0) = m[3]; (*this)(1, 1) = m[4]; (*this)(1, 2) = m[5];
2012 (*this)(2, 0) = m[6]; (*this)(2, 1) = m[7]; (*this)(2, 2) = m[8];
2014 (*this)(0, 0) = m[0]; (*this)(0, 1) = m[3]; (*this)(0, 2) = m[6];
2015 (*this)(1, 0) = m[1]; (*this)(1, 1) = m[4]; (*this)(1, 2) = m[7];
2016 (*this)(2, 0) = m[2]; (*this)(2, 1) = m[5]; (*this)(2, 2) = m[8];
2021 template <
typename T>
2023 #ifdef MATRIX_ROW_MAJOR
2024 (*this).set_row(0, x);
2025 (*this).set_row(1, y);
2026 (*this).set_row(2, z);
2028 (*this).set_col(0, x);
2029 (*this).set_col(1, y);
2030 (*this).set_col(2, z);
2035 template <
typename T>
2037 (*this)(0, 0) = rhs(0, 1); (*this)(0, 0) = rhs(0, 1); (*this)(0, 2) = T(0);
2038 (*this)(1, 0) = rhs(1, 1); (*this)(1, 0) = rhs(1, 1); (*this)(1, 2) = T(0);
2039 (*this)(2, 0) = T(0); (*this)(2, 0) = T(0); (*this)(2, 2) = T(1);
2043 template <
typename T>
2051 (*this)(0, 0) = 1.0 - 2.0 * (y*y + z*z); (*this)(0, 1) = 2.0 * (x*y - w*z); (*this)(0, 2) = 2.0 * (x*z + w*y);
2052 (*this)(1, 0) = 2.0 * (x*y + w*z); (*this)(1, 1) = 1.0 - 2.0 * (x*x + z*z); (*this)(1, 2) = 2.0 * (y*z - w*x);
2053 (*this)(2, 0) = 2.0 * (x*z - w*y); (*this)(2, 1) = 2.0 * (y*z + w*x); (*this)(2, 2) = 1.0 - 2.0 * (x*x + y*y);
2057 template <
typename T>
2060 for (
size_t i = 0; i < 2; i++) {
2061 for (
size_t j = 0; j < 2; j++) {
2062 mat(i, j) = (*this)(i, j);
2069 template <
typename T>
2079 template <
typename T>
2089 template <
typename T>
2095 T(0), -axis[2], axis[1],
2096 axis[2], T(0), -axis[0],
2097 -axis[1], axis[0], T(0)
2104 const T c = std::cos(angle);
2105 const T rc = T(1) - c;
2106 const T s = std::sin(angle);
2112 template <
typename T>
2114 const T len = axis_angle.
length();
2115 return rotation(axis_angle/len, len);
2119 template <
typename T>
2128 m(0, 0) = 1.0 - 2.0 * (y*y + z*z); m(0, 1) = 2.0 * (x*y - w*z); m(0, 2) = 2.0 * (x*z + w*y);
2129 m(1, 0) = 2.0 * (x*y + w*z); m(1, 1) = 1.0 - 2.0 * (x*x + z*z); m(1, 2) = 2.0 * (y*z - w*x);
2130 m(2, 0) = 2.0 * (x*z - w*y); m(2, 1) = 2.0 * (y*z + w*x); m(2, 2) = 1.0 - 2.0 * (x*x + y*y);
2135 template <
typename T>
2141 rx(0, 0) = T(1); rx(0, 1) = T(0); rx(0, 2) = T(0);
2142 rx(1, 0) = T(0); rx(1, 1) = std::cos(x); rx(1, 2) = -std::sin(x);
2143 rx(2, 0) = T(0); rx(2, 1) = std::sin(x); rx(2, 2) = std::cos(x);
2146 ry(0, 0) = std::cos(y); ry(0, 1) = T(0); ry(0, 2) = std::sin(y);
2147 ry(1, 0) = T(0); ry(1, 1) = T(1); ry(1, 2) = T(0);
2148 ry(2, 0) = -std::sin(y); ry(2, 1) = T(0); ry(2, 2) = std::cos(y);
2151 rz(0, 0) = std::cos(z); rz(0, 1) = -std::sin(z); rz(0, 2) = T(0);
2152 rz(1, 0) = std::sin(z); rz(1, 1) = std::cos(z); rz(1, 2) = T(0);
2153 rz(2, 0) = T(0); rz(2, 1) = T(0); rz(2, 2) = T(1);
2156 case 123:
return rz * ry * rx;
2157 case 132:
return ry * rz * rx;
2158 case 213:
return rz * rx * ry;
2159 case 231:
return rx * rz * ry;
2160 case 312:
return ry * rx * rz;
2161 case 321:
return rx * ry * rz;
2163 LOG(ERROR) <<
"invalid rotation order";
2164 return rx * rz * ry;
2179 template <
typename T>
2204 T s00, T s01, T s02, T s03,
2205 T s10, T s11, T s12, T s13,
2206 T s20, T s21, T s22, T s23,
2207 T s30, T s31, T s32, T s33
2321 template <
typename T>
2323 for (
size_t i = 0; i < 4; ++i) {
2324 for (
size_t j = 0; j < 4; ++j)
2326 (*this)(i, j) = T(s);
2328 (*
this)(i, j) = T(0);
2333 template <
typename T>
2335 for (
size_t i = 0; i < 16; ++i)
2336 (*this).m_[i] = rhs[i];
2340 template <
typename T>
2342 T s00, T s01, T s02, T s03,
2343 T s10, T s11, T s12, T s13,
2344 T s20, T s21, T s22, T s23,
2345 T s30, T s31, T s32, T s33
2347 (*this)(0, 0) = s00; (*this)(0, 1) = s01; (*this)(0, 2) = s02; (*this)(0, 3) = s03;
2348 (*this)(1, 0) = s10; (*this)(1, 1) = s11; (*this)(1, 2) = s12; (*this)(1, 3) = s13;
2349 (*this)(2, 0) = s20; (*this)(2, 1) = s21; (*this)(2, 2) = s22; (*this)(2, 3) = s23;
2350 (*this)(3, 0) = s30; (*this)(3, 1) = s31; (*this)(3, 2) = s32; (*this)(3, 3) = s33;
2354 template <
typename T>
2356 assert(m !=
nullptr);
2358 #ifdef MATRIX_ROW_MAJOR
2359 (*this)(0, 0) = m[0]; (*this)(0, 1) = m[1]; (*this)(0, 2) = m[2]; (*this)(0, 3) = m[3];
2360 (*this)(1, 0) = m[4]; (*this)(1, 1) = m[5]; (*this)(1, 2) = m[6]; (*this)(1, 3) = m[7];
2361 (*this)(2, 0) = m[8]; (*this)(2, 1) = m[9]; (*this)(2, 2) = m[10]; (*this)(2, 3) = m[11];
2362 (*this)(3, 0) = m[12]; (*this)(3, 1) = m[13]; (*this)(3, 2) = m[14]; (*this)(3, 3) = m[15];
2364 (*this)(0, 0) = m[0]; (*this)(0, 1) = m[4]; (*this)(0, 2) = m[8]; (*this)(0, 3) = m[12];
2365 (*this)(1, 0) = m[1]; (*this)(1, 1) = m[5]; (*this)(1, 2) = m[9]; (*this)(1, 3) = m[13];
2366 (*this)(2, 0) = m[2]; (*this)(2, 1) = m[6]; (*this)(2, 2) = m[10]; (*this)(2, 3) = m[14];
2367 (*this)(3, 0) = m[3]; (*this)(3, 1) = m[7]; (*this)(3, 2) = m[11]; (*this)(3, 3) = m[15];
2372 template <
typename T>
2380 #ifdef MATRIX_ROW_MAJOR
2381 (*this).set_row(0, x);
2382 (*this).set_row(1, y);
2383 (*this).set_row(2, z);
2384 (*this).set_row(3, w);
2386 (*this).set_col(0, x);
2387 (*this).set_col(1, y);
2388 (*this).set_col(2, z);
2389 (*this).set_col(3, w);
2394 template <
typename T>
2396 (*this)(0, 0) = rhs(0, 0); (*this)(0, 1) = rhs(0, 1); (*this)(0, 2) = rhs(0, 2); (*this)(0, 3) = T(0);
2397 (*this)(1, 0) = rhs(1, 0); (*this)(1, 1) = rhs(1, 1); (*this)(1, 2) = rhs(1, 2); (*this)(1, 3) = T(0);
2398 (*this)(2, 0) = rhs(2, 0); (*this)(2, 1) = rhs(2, 1); (*this)(2, 2) = rhs(2, 2); (*this)(2, 3) = T(0);
2399 (*this)(3, 0) = T(0); (*this)(3, 1) = T(0); (*this)(3, 2) = T(0); (*this)(3, 3) = T(1);
2403 template <
typename T>
2406 for (
size_t i = 0; i < 3; i++) {
2407 for (
size_t j = 0; j < 3; j++) {
2408 mat(i, j) = (*this)(i, j);
2415 template <
typename T>
2423 r(0, 0) = r(0, 0) * s[0]; r(0, 1) = r(0, 1) * s[1]; r(0, 2) = r(0, 2) * s[2];
2424 r(1, 0) = r(1, 0) * s[0]; r(1, 1) = r(1, 1) * s[1]; r(1, 2) = r(1, 2) * s[2];
2425 r(2, 0) = r(2, 0) * s[0]; r(2, 1) = r(2, 1) * s[1]; r(2, 2) = r(2, 2) * s[2];
2428 (*this)(0, 0) = r(0, 0); (*this)(0, 1) = r(0, 1); (*this)(0, 2) = r(0, 2); (*this)(0, 3) = t[0];
2429 (*this)(1, 0) = r(1, 0); (*this)(1, 1) = r(1, 1); (*this)(1, 2) = r(1, 2); (*this)(1, 3) = t[1];
2430 (*this)(2, 0) = r(2, 0); (*this)(2, 1) = r(2, 1); (*this)(2, 2) = r(2, 2); (*this)(2, 3) = t[2];
2431 (*this)(3, 0) = T(0); (*this)(3, 1) = T(0); (*this)(3, 2) = T(0); (*this)(3, 3) = T(1);
2435 template <
typename T>
2438 s, T(0), T(0), T(0),
2439 T(0), s, T(0), T(0),
2440 T(0), T(0), s, T(0),
2441 T(0), T(0), T(0), T(1)
2446 template <
typename T>
2449 x, T(0), T(0), T(0),
2450 T(0), y, T(0), T(0),
2451 T(0), T(0), z, T(0),
2457 template <
typename T>
2460 s.x, T(0), T(0), T(0),
2461 T(0), s.y, T(0), T(0),
2462 T(0), T(0), s.z, T(0),
2463 T(0), T(0), T(0), s.w
2469 template <
typename T>
2477 template <
typename T>
2479 const T len = axis_angle.
length();
2484 template <
typename T>
2493 m(0, 0) = 1.0 - 2.0 * (y*y + z*z); m(0, 1) = 2.0 * (x*y - w*z); m(0, 2) = 2.0 * (x*z + w*y); m(0, 3) = 0.0;
2494 m(1, 0) = 2.0 * (x*y + w*z); m(1, 1) = 1.0 - 2.0 * (x*x + z*z); m(1, 2) = 2.0 * (y*z - w*x); m(1, 3) = 0.0;
2495 m(2, 0) = 2.0 * (x*z - w*y); m(2, 1) = 2.0 * (y*z + w*x); m(2, 2) = 1.0 - 2.0 * (x*x + y*y); m(2, 3) = 0.0;
2496 m(3, 0) = 0.0; m(3, 1) = 0.0; m(3, 2) = 0.0; m(3, 3) = 1.0;
2501 template <
typename T>
2507 rx(0, 0) = T(1); rx(0, 1) = T(0); rx(0, 2) = T(0); rx(0, 3) = T(0);
2508 rx(1, 0) = T(0); rx(1, 1) = std::cos(x); rx(1, 2) = -std::sin(x); rx(1, 3) = T(0);
2509 rx(2, 0) = T(0); rx(2, 1) = std::sin(x); rx(2, 2) = std::cos(x); rx(2, 3) = T(0);
2510 rx(3, 0) = T(0); rx(3, 1) = T(0); rx(3, 2) = T(0); rx(3, 3) = T(1);
2513 ry(0, 0) = std::cos(y); ry(0, 1) = T(0); ry(0, 2) = std::sin(y); ry(0, 3) = T(0);
2514 ry(1, 0) = T(0); ry(1, 1) = T(1); ry(1, 2) = T(0); ry(1, 3) = T(0);
2515 ry(2, 0) = -std::sin(y); ry(2, 1) = T(0); ry(2, 2) = std::cos(y); ry(2, 3) = T(0);
2516 ry(3, 0) = T(0); ry(3, 1) = T(0); ry(3, 2) = T(0); ry(3, 3) = T(1);
2519 rz(0, 0) = std::cos(z); rz(0, 1) = -std::sin(z); rz(0, 2) = T(0); rz(0, 3) = T(0);
2520 rz(1, 0) = std::sin(z); rz(1, 1) = std::cos(z); rz(1, 2) = T(0); rz(1, 3) = T(0);
2521 rz(2, 0) = T(0); rz(2, 1) = T(0); rz(2, 2) = T(1); rz(2, 3) = T(0);
2522 rz(3, 0) = T(0); rz(3, 1) = T(0); rz(3, 2) = T(0); rz(3, 3) = T(1);
2525 case 123:
return rz * ry * rx;
2526 case 132:
return ry * rz * rx;
2527 case 213:
return rz * rx * ry;
2528 case 231:
return rx * rz * ry;
2529 case 312:
return ry * rx * rz;
2530 case 321:
return rx * ry * rz;
2532 LOG(ERROR) <<
"invalid rotation order";
2533 return rx * rz * ry;
2538 template <
typename T>
2541 T(1), T(0), T(0), t[0],
2542 T(0), T(1), T(0), t[1],
2543 T(0), T(0), T(1), t[2],
2544 T(0), T(0), T(0), T(1)
2549 template <
typename T>
2552 T(1), T(0), T(0), x,
2553 T(0), T(1), T(0), y,
2554 T(0), T(0), T(1), z,
2555 T(0), T(0), T(0), T(1)
2 by 2 matrix. Extends Mat with 2D-specific functionality and constructors.
Definition mat.h:1673
static Mat2< T > scale(T s)
Static constructor return a 2D uniform scale matrix.
Definition mat.h:1814
static Mat2< T > rotation(T angle)
Static constructor return a 2D rotation matrix.
Definition mat.h:1805
Mat2(T s)
Constructor that initializes diagonal elements to a scalar value and others zeros.
Definition mat.h:1743
Mat2(const T *m)
Initialize elements from an array of type T.
Definition mat.h:1779
static Mat2< T > scale(T x, T y)
Static constructor return a 2D non-uniform scale matrix.
Definition mat.h:1823
Mat2()=default
Default constructor.
Mat2(const Mat< 3, 3, T > &rhs)
Copies the top-left corner of rhs. This provides compatibility with generic operations implemented by...
Definition mat.h:1762
Mat2(T s00, T s01, T s10, T s11)
Initialize elements from individual scalars. The digits following s in the parameter names indicate t...
Definition mat.h:1769
Mat2(const Vec< 2, T > &x, const Vec< 2, T > &y)
Initialize elements from two vectors. If MATRIX_ROW_MAJOR is defined, x and y specify rows of the mat...
Definition mat.h:1793
Mat2(const Mat< 2, 2, T > &rhs)
Copy constructor. This provides compatibility with generic operations implemented by Mat.
Definition mat.h:1755
3 by 3 matrix. Extends Mat with 3D-specific functionality and constructors.
Definition mat.h:1844
static Mat3< T > rotation(T x, T y, T z, int order=312)
Static constructor returning a 3D rotation matrix defined by Euler angles. The three rotations are ap...
Definition mat.h:2136
Mat3(T s)
Constructor that initializes diagonal elements to a scalar value and others zeros.
Definition mat.h:1967
Mat3()=default
Default constructor.
static Mat3< T > rotation(const Vec< 3, T > &axis_angle)
Static constructor returning a 3D rotation matrix defined by the axis–angle representation....
Definition mat.h:2113
static Mat3< T > rotation(const Vec< 3, T > &axis, T angle)
Static constructor returning a 3D rotation matrix defined by its axis and angle.
Definition mat.h:2090
Mat3(const Quat< T > &q)
Definition mat.h:2044
Mat2< T > sub() const
the upper-left 2x2 sub-matrix.
Definition mat.h:2058
Mat3(const Mat< 4, 4, T > &rhs)
Copies the top-left corner of rhs. This provides compatibility with generic operations implemented by...
Definition mat.h:1986
Mat3(const Mat< 3, 3, T > &rhs)
Copy constructor. This provides compatibility with generic operations implemented by Mat.
Definition mat.h:1979
Mat3(const T *m)
Definition mat.h:2006
static Mat3< T > scale(T x, T y, T z)
Static constructor returning a 3D non-uniform scale matrix.
Definition mat.h:2080
Mat3(T s00, T s01, T s02, T s10, T s11, T s12, T s20, T s21, T s22)
Initialize elements from individual scalars. The digits following s in the parameter names indicate t...
Definition mat.h:1994
static Mat3< T > rotation(const Quat< T > &q)
Static constructor return a 3D rotation matrix defined by a quaternion.
Definition mat.h:2120
Mat3(const Mat< 2, 2, T > &rhs)
Definition mat.h:2036
Mat3(const Vec< 3, T > &x, const Vec< 3, T > &y, const Vec< 3, T > &z)
Initialize elements from three vectors. If MATRIX_ROW_MAJOR is defined, x, y and z specify rows of th...
Definition mat.h:2022
static Mat3< T > scale(T s)
Static constructor returning a 3D uniform scale matrix.
Definition mat.h:2070
4 by 4 matrix. Extends Mat with 4D-specific functionality and constructors.
Definition mat.h:2180
Mat4(const T *m)
Initialize elements from an array of type T.
Definition mat.h:2355
Mat4()=default
Default constructor.
static Mat4< T > scale(const Vec< 4, T > &s)
Static constructor returning a 4D non-uniform scale matrix,.
Definition mat.h:2458
Mat4(T s)
Constructor that initializes diagonal elements to a scalar value and others zeros.
Definition mat.h:2322
static Mat4< T > rotation(const Quat< T > &q)
Static constructor returning a 3D rotation matrix defined by a quaternion.
Definition mat.h:2485
static Mat4< T > scale(T s)
Static constructor returning a 4D uniform scale matrix.
Definition mat.h:2436
static Mat4< T > rotation(const Vec< 3, T > &axis, T angle)
Static constructor returning a 3D rotation matrix defined by its axis and angle.
Definition mat.h:2470
Mat4(const Mat< 4, 4, T > &rhs)
Copy constructor. This provides compatibility with generic operations implemented by Mat.
Definition mat.h:2334
static Mat4< T > translation(T x, T y, T z)
Static constructor return a 3D translation matrix (as a 4D affine transformation).
Definition mat.h:2550
Mat4(const Mat< 3, 3, T > &rhs)
Initialize from a 3D matrix. rhs forms the upper-left 3x3 sub-matrix, other elements are initialized ...
Definition mat.h:2395
static Mat4< T > scale(T x, T y, T z, T w)
Static constructor returning a 4D non-uniform scale matrix,.
Definition mat.h:2447
static Mat4< T > translation(const Vec< 3, T > &t)
Static constructor return a 3D translation matrix (as a 4D affine transformation).
Definition mat.h:2539
Mat4(const Vec< 4, T > &x, const Vec< 4, T > &y, const Vec< 4, T > &z, const Vec< 4, T > &w)
Initialize elements from four vectors. If MATRIX_ROW_MAJOR is defined, x, y, z and w specify rows of ...
Definition mat.h:2373
static Mat4< T > rotation(const Vec< 3, T > &axis_angle)
Static constructor returning a 3D rotation matrix defined by the axis–angle representation....
Definition mat.h:2478
Mat4(const Vec< 3, T > &s, const Quat< T > &r, const Vec< 3, T > &t)
Initialize from scale/rotation/translation. The order of the transformations is scale,...
Definition mat.h:2416
Mat3< T > sub() const
Return the upper-left 3x3 sub-matrix.
Definition mat.h:2404
static Mat4< T > rotation(T x, T y, T z, int order=312)
Static constructor returning a 3D rotation matrix defined by Euler angles. The three rotations are ap...
Definition mat.h:2502
Mat4(T s00, T s01, T s02, T s03, T s10, T s11, T s12, T s13, T s20, T s21, T s22, T s23, T s30, T s31, T s32, T s33)
Initialize elements from individual scalars. The digits following s in the parameter names indicate t...
Definition mat.h:2341
Base class for matrix types.
Definition mat.h:64
Vec< N, T > col(size_t col) const
Returns the specified column as a vector.
Definition mat.h:723
Mat< N, M, T > & operator*=(T rhs)
Multiplies the matrix by a scalar and assigns the result to the matrix.
Definition mat.h:965
void swap_cols(size_t a, size_t b)
Swaps the specified columns.
Definition mat.h:789
void set_row(size_t row, const Vec< vN, T > &v)
Sets the specified row from a vector.
Definition mat.h:735
Vec< M, T > row(size_t row) const
Returns the specified row as a vector.
Definition mat.h:712
static Mat< N, M, T > identity()
Returns an identity matrix.
Definition mat.h:697
Mat()=default
Default constructor.
Mat(const Mat< rN, rM, T > &rhs)
Copy constructor for matrices with different dimensions (rN >= N, rM >= M).
Definition mat.h:677
Mat(T s)
Constructs a matrix with diagonal elements set to s and other elements set to zero.
Definition mat.h:664
Mat< N, M, T > & operator-=(const Mat< N, M, T > &rhs)
Subtracts another matrix from the matrix and assigns the result to the matrix.
Definition mat.h:954
Mat< N, M, T > & operator*=(const Mat< N, M, T > &rhs)
Multiplies the matrix by another matrix and assigns the result to the matrix.
Definition mat.h:938
Mat(const T *m)
Constructs a matrix from an array of elements.
Definition mat.h:688
void set_col(size_t col, const Vec< vN, T > &v)
Sets the specified column from a vector.
Definition mat.h:746
Mat< N, M, T > & operator-=(T rhs)
Subtracts a scalar from the matrix and assigns the result to the matrix.
Definition mat.h:989
Mat< N, M, T > operator*(T rhs) const
Multiplies the matrix by a scalar.
Definition mat.h:918
T & operator()(size_t row, size_t col)
Returns a reference to the element at the specified row and column.
Definition mat.h:827
void swap_rows(size_t a, size_t b)
Swaps the specified rows.
Definition mat.h:756
void load_zero()
Sets all elements to zero.
Definition mat.h:769
void load_identity(T s=T(1))
Sets the diagonal elements to s and other elements to zero.
Definition mat.h:777
Mat< N, M, T > operator-(const Mat< N, M, T > &rhs) const
Subtracts another matrix from the matrix. Component-wise matrix-matrix subtraction.
Definition mat.h:881
static size_t num_columns()
Returns the number of columns in the matrix.
Definition mat.h:118
bool operator!=(const Mat< N, M, T > &rhs) const
Checks if the matrix is not equal to another matrix.
Definition mat.h:849
Mat< N, M, T > & operator/=(T rhs)
Divides the matrix by a scalar and assigns the result to the matrix.
Definition mat.h:973
Mat< N, M, T > & operator+=(const Mat< N, M, T > &rhs)
Adds another matrix to the matrix and assigns the result to the matrix.
Definition mat.h:946
static size_t num_rows()
Returns the number of rows in the matrix.
Definition mat.h:112
const T & operator()(size_t row, size_t col) const
Returns a constant reference to the element at the specified row and column.
Definition mat.h:814
Vec< N, T > operator*(const Vec< M, T > &rhs) const
Multiplies the matrix by a vector.
Definition mat.h:902
Mat< N, M, T > operator/(T rhs) const
Divides the matrix by a scalar.
Definition mat.h:927
Mat< N, M, T > & operator+=(T rhs)
Adds a scalar to the matrix and assigns the result to the matrix.
Definition mat.h:981
Mat< N, rM, T > operator*(const Mat< M, rM, T > &rhs) const
Multiplies the matrix by another matrix.
Definition mat.h:857
bool operator==(const Mat< N, M, T > &rhs) const
Checks if the matrix is equal to another matrix.
Definition mat.h:840
Mat< N, M, T > operator-() const
Negates the matrix. Component-wise matrix negation.
Definition mat.h:890
Mat< N, M, T > operator+(const Mat< N, M, T > &rhs) const
Adds the matrix to another matrix. Component-wise matrix-matrix addition.
Definition mat.h:872
The Quaternion class represents 3D rotations and orientations.
Definition quat.h:107
FT length() const
Return the length of the quaternion.
Definition quat.h:285
Base class for vector types. It provides generic functionality for N dimensional vectors.
Definition vec.h:30
T length() const
Returns the length of this vector.
Definition vec.h:115
T * data()
Returns the memory address of the vector.
Definition vec.h:82
Definition collider.cpp:182
T trace(const Mat< N, N, T > &m)
Returns the trace (sum of elements on the main diagonal) of an N x N (square) matrix.
Mat< M, N, T > transpose(const Mat< N, M, T > &m)
Transposes a matrix.
Definition mat.h:1120
FT min()
Function returning minimum representable value for a given type.
T determinant(const Mat< N, N, T > &m)
Computes the determinant of a square matrix.
Definition mat.h:1143
FT epsilon()
Function returning the epsilon value for a given type.
bool cholesky_decompose(const Mat< N, N, FT > &A, Mat< N, N, FT > &L)
Perform Cholesky decomposition of a symmetric, positive-definite matrix.
Definition mat.h:1531
bool epsilon_equal(FT const &x, FT const &y, FT const &eps)
Tests if two values are Epsilon equal.
Definition constant.h:134
std::ostream & operator<<(std::ostream &os, Graph::Vertex v)
Output stream support for Graph::Vertex.
Definition graph.h:1300
void lu_back_substitution(const Mat< N, N, T > &alu, const Vec< N, T > &rowp, const Vec< N, T > &b, Vec< N, T > *x)
Solve a set of linear equations using outputs from lu_decomposition() as inputs.
Definition mat.h:1490
FT max()
Function returning maximum representable value for a given type.
T det(const Vec< 2, T > &v1, const Vec< 2, T > &v2)
Compute the determinant of the 2x2 matrix formed by the two 2D vectors as the two rows.
Definition vec.h:481
bool lu_decomposition(const Mat< N, N, T > &a, Mat< N, N, T > *alu, Vec< N, T > *rowp, T *d)
Perform LU decomposition of a square matrix.
Definition mat.h:1414
Mat< N, N, T > inverse(const Mat< N, N, T > &m)
Returns the inverse of an N x N (square) matrix.
Definition mat.h:1190
std::istream & operator>>(std::istream &is, GenericLine< DIM, FT > &line)
Input stream support for GenericLine.
Definition line.h:183
Mat< N, 1, FT > to_matrix(const Vec< N, FT > &v)
Convert a N-dimensional vector into a N by 1 matrix.
Definition mat.h:1633
bool has_nan(const GenericBox< DIM, FT > &box)
Check if the representation of a box has NaN.
Definition box.h:373
std::vector< FT > sum(const Matrix< FT > &)
Definition matrix.h:1485
bool gauss_jordan_elimination(const Mat< N, N, T > &a, const Mat< N, M, T > &b, Mat< N, N, T > *ainv, Mat< N, M, T > *x)
Perform Gauss-Jordan elimination to solve a set of linear equations and additionally compute the inve...
Definition mat.h:1334
void cholesky_solve(const Mat< N, N, FT > &L, const Vec< N, FT > &b, Vec< N, FT > &x)
Solve a set of linear equations using outputs from cholesky_decompose() as inputs.
Definition mat.h:1558
Mat< N, M, T > operator*(T lhs, const Mat< N, M, T > &rhs)
Multiplies a scalar by a matrix.
Definition mat.h:1000
Mat< N, M, T > tensor(const Vec< M, T > &u, const Vec< N, T > &v)
Returns the tensor product (outer product) of vectors u and v, where u is treated as a column vector ...
Definition mat.h:1325