diff --git a/libopenage/util/matrix.h b/libopenage/util/matrix.h index 6809899e4e..3f723c11ea 100644 --- a/libopenage/util/matrix.h +++ b/libopenage/util/matrix.h @@ -1,4 +1,4 @@ -// Copyright 2015-2018 the openage authors. See copying.md for legal info. +// Copyright 2015-2023 the openage authors. See copying.md for legal info. #pragma once @@ -16,7 +16,7 @@ namespace openage::util { * Matrix class with arithmetic. M rows, N columns. * T = underlying single value type (float, double, ...) */ -template +template class Matrix : public std::array, M> { public: static_assert(M > 0 and N > 0, "0-dimensional matrix not allowed"); @@ -47,8 +47,8 @@ class Matrix : public std::array, M> { /** * Constructor from Vector */ - template ::type> + template ::type> Matrix(const Vector &vec) { for (size_t i = 0; i < M; i++) { (*this)[i][0] = vec[i]; @@ -58,21 +58,25 @@ class Matrix : public std::array, M> { /** * Constructor with N*M values */ - template - Matrix(Ts ... args) { - static_assert(sizeof...(args) == N*M, "not all values supplied"); - - std::array temp{{static_cast(args)...}}; - for (size_t i = 0; i < N*M; i++) { - (*this)[i / (N*M)][i % (N*M)] = temp[i]; + template + Matrix(Ts... args) { + static_assert(sizeof...(args) == N * M, "not all values supplied"); + + std::array temp{{static_cast(args)...}}; + size_t index = 0; + for (size_t row = 0; row < M; row++) { + for (size_t col = 0; col < N; col++) { + (*this)[row][col] = temp[index]; + index += 1; + } } } /** * Constructs the identity matrix for the current size. */ - template ::type> + template ::type> static this_type identity() { this_type res; @@ -86,7 +90,7 @@ class Matrix : public std::array, M> { /** * Test if both matrices contain the same values within epsilon. */ - bool equals(const this_type &other, float eps=default_eps) const { + bool equals(const this_type &other, float eps = default_eps) const { for (size_t i = 0; i < N; i++) { for (size_t j = 0; j < M; j++) { if (std::abs((*this)[i][j] - other[i][j]) >= eps) { @@ -101,7 +105,7 @@ class Matrix : public std::array, M> { * Matrix multiplication */ template - Matrix operator *(const Matrix &other) const { + Matrix operator*(const Matrix &other) const { Matrix res; for (size_t i = 0; i < M; i++) { for (size_t j = 0; j < P; j++) { @@ -117,14 +121,14 @@ class Matrix : public std::array, M> { /** * Matrix-Vector multiplication */ - Matrix operator *(const Vector &vec) const { + Matrix operator*(const Vector &vec) const { return (*this) * static_cast>(vec); } /** * Matrix addition */ - this_type operator +(const this_type &other) const { + this_type operator+(const this_type &other) const { this_type res; for (size_t i = 0; i < M; i++) { for (size_t j = 0; j < N; j++) { @@ -137,7 +141,7 @@ class Matrix : public std::array, M> { /** * Matrix subtraction */ - this_type operator -(const this_type &other) const { + this_type operator-(const this_type &other) const { this_type res; for (size_t i = 0; i < M; i++) { for (size_t j = 0; j < N; j++) { @@ -150,7 +154,7 @@ class Matrix : public std::array, M> { /** * Scalar multiplication with assignment */ - void operator *=(T other) { + void operator*=(T other) { for (size_t i = 0; i < M; i++) { for (size_t j = 0; j < N; j++) { (*this)[i][j] *= other; @@ -161,7 +165,7 @@ class Matrix : public std::array, M> { /** * Scalar multiplication */ - this_type operator *(T other) const { + this_type operator*(T other) const { this_type res(*this); res *= other; return res; @@ -170,7 +174,7 @@ class Matrix : public std::array, M> { /** * Scalar division with assignment */ - void operator /=(T other) { + void operator/=(T other) { for (size_t i = 0; i < M; i++) { for (size_t j = 0; j < N; j++) { (*this)[i][j] /= other; @@ -181,7 +185,7 @@ class Matrix : public std::array, M> { /** * Scalar division */ - this_type operator /(T other) const { + this_type operator/(T other) const { this_type res(*this); res /= other; return res; @@ -191,7 +195,7 @@ class Matrix : public std::array, M> { * Transposition */ Matrix transpose() const { - Matrix res; + Matrix res; for (size_t i = 0; i < M; i++) { for (size_t j = 0; j < N; j++) { res[j][i] = (*this)[i][j]; @@ -203,8 +207,8 @@ class Matrix : public std::array, M> { /** * Conversion to Vector */ - template::type> + template ::type> Vector to_vector() const { Vector res{}; for (size_t i = 0; i < M; i++) { @@ -216,8 +220,8 @@ class Matrix : public std::array, M> { /** * Matrix trace: the sum of all diagonal entries */ - template::type> + template ::type> T trace() const { T res = 0; @@ -231,22 +235,23 @@ class Matrix : public std::array, M> { /** * Print to output stream using '<<' */ - friend std::ostream &operator <<(std::ostream &o, - const this_type &mat) { + friend std::ostream &operator<<(std::ostream &o, + const this_type &mat) { o << "("; - for (size_t j = 0; j < M-1; j++) { + for (size_t j = 0; j < M - 1; j++) { o << "("; - for (size_t i = 0; i < N-1; i++) { + for (size_t i = 0; i < N - 1; i++) { o << mat[j][i] << ",\t"; } - o << mat[j][N-1] << ")"; - o << "," << std::endl << " "; + o << mat[j][N - 1] << ")"; + o << "," << std::endl + << " "; } o << "("; - for (size_t i = 0; i < N-1; i++) { - o << mat[M-1][i] << ",\t"; + for (size_t i = 0; i < N - 1; i++) { + o << mat[M - 1][i] << ",\t"; } - o << mat[M-1][N-1] << "))"; + o << mat[M - 1][N - 1] << "))"; return o; } }; @@ -254,8 +259,8 @@ class Matrix : public std::array, M> { /** * Scalar multiplication with swapped arguments */ -template -Matrix operator *(T a, const Matrix &mat) { +template +Matrix operator*(T a, const Matrix &mat) { return mat * a; } @@ -264,24 +269,24 @@ Matrix operator *(T a, const Matrix &mat) { * because otherwise the above float-multiplication function might not match to * the template deduction. */ -template -Matrix operator *(int64_t a, const Matrix &mat) { +template +Matrix operator*(int64_t a, const Matrix &mat) { return mat * a; } -template +template using Matrix2t = Matrix<2, 2, T>; -template +template using Matrix3t = Matrix<3, 3, T>; -template +template using Matrix4t = Matrix<4, 4, T>; -template +template using Matrixf = Matrix; -template +template using Matrixd = Matrix; using Matrix2f = Matrix<2, 2, float>; @@ -292,4 +297,4 @@ using Matrix2d = Matrix<2, 2, double>; using Matrix3d = Matrix<3, 3, double>; using Matrix4d = Matrix<4, 4, double>; -} // openage::util +} // namespace openage::util