25 #ifndef __I6ENGINE_MATH_I6EMATRIX_H__
26 #define __I6ENGINE_MATH_I6EMATRIX_H__
35 namespace serialization {
59 i6eMatrix(uint32_t m, uint32_t n) : _width(n), _height(m), _data(m, std::vector<T>(n, T())) {
71 explicit i6eMatrix(
const i6eVector & vector) : _width(1), _height(3), _data(3, std::vector<T>(1, 0.0)) {
72 _data[0][0] = vector.
getX();
73 _data[1][0] = vector.
getY();
74 _data[2][0] = vector.
getZ();
94 for (
unsigned int i = 0; i < _height; ++i) {
95 for (
unsigned int j = 0; j < _width; ++j) {
120 for (uint32_t i = 0; i < _height; ++i) {
121 for (uint32_t j = 0; j < _width; ++j) {
122 _data[i][j] /= value;
145 ISIXE_THROW_API(
"i6eMatrix",
"wrong dimension for matrix-matrix-multiplication");
150 for (uint32_t i = 0; i < _height; ++i) {
151 for (uint32_t j = 0; j < other.
getWidth(); ++j) {
152 for (uint32_t k = 0; k < _width; ++k) {
153 result(i, j, result(i, j) + _data[i][k] * other(k, j));
179 ISIXE_THROW_API(
"i6eMatrix",
"wrong dimension for matrix-matrix-addition");
182 for (uint32_t i = 0; i < _height; ++i) {
183 for (uint32_t j = 0; j < _width; ++j) {
184 _data[i][j] += other(i, j);
207 ISIXE_THROW_API(
"i6eMatrix",
"wrong dimension for matrix-matrix-subtraction");
210 for (uint32_t i = 0; i < _height; ++i) {
211 for (uint32_t j = 0; j < _width; ++j) {
212 _data[i][j] -= other(i, j);
225 for (uint32_t i = 0; i < _height; ++i) {
226 for (uint32_t j = 0; j < _width; ++j) {
227 result(i, j, -_data[i][j]);
256 for (uint32_t i = 0; i < _height; ++i) {
257 for (uint32_t j = 0; j < _width; ++j) {
258 if (std::fabs(_data[i][j] - other._data[i][j]) > 0.0000001f) {
271 return !(*
this == other);
278 if (m >= _height || n >= _width) {
279 ISIXE_THROW_API(
"i6eMatrix",
"field (" << m <<
", " << n <<
") not part of the matrix");
289 for (uint32_t i = 0; i < _height; ++i) {
290 for (uint32_t j = 0; j < _width; ++j) {
300 for (uint32_t i = 0; i < _height; ++i) {
301 for (uint32_t j = 0; j < _width; ++j) {
329 if (m >= _height || n >= _width) {
330 ISIXE_THROW_API(
"i6eMatrix",
"field (" << m <<
", " << n <<
") not part of the matrix");
342 if (_height != _width) {
347 return _data[0][0] * _data[1][1] - _data[1][0] * _data[0][1];
355 for (uint32_t i = 0; i <
getHeight(); ++i) {
368 for (uint32_t i = 1; i < amount; ++i) {
381 for (uint32_t i = 0; i < other.
getHeight(); ++i) {
382 for (uint32_t j = 0; j < other.
getWidth(); ++j) {
383 result(j, i, other(i, j));
400 assert(std::fabs(det) > 1e-15);
402 return adjoint(other) * (1.0f / det);
415 for (uint32_t i = 0; i < other.
getHeight(); ++i) {
416 for (uint32_t j = 0; j < other.
getWidth(); ++j) {
420 uint32_t columnC = 0;
422 for (uint32_t x = 0; x < other.
getHeight(); ++x) {
426 for (uint32_t y = 0; y < other.
getWidth(); ++y) {
431 m(rowC, columnC, other(x, y));
439 T k = T(std::pow(-1.0f, i + j) * m.calculateDeterminant());
452 ISIXE_THROW_API(
"i6eMatrix",
"only nxn matrices can be solved using LU decomposition");
462 forwardSubstitution(l, b, y);
463 backwardSubstitution(u, y, x);
471 std::vector<std::vector<T>> _data;
482 for (uint32_t i = 0; i < l.
getWidth(); ++i) {
486 for (uint32_t i = 0; i <
getHeight(); ++i) {
487 for (uint32_t j = i + 1; j <
getHeight(); ++j) {
488 l(j, i, u(j, i) / u(i, i));
490 for (uint32_t k = i; k <
getWidth(); ++k) {
491 u(j, k, u(j, k) - l(j, i) * u(i, k));
501 assert(l.getWidth() == l.getHeight());
502 assert(l.getHeight() == b.getHeight() && b.getWidth() == 1);
506 for (uint32_t i = 0; i < l.getWidth(); ++i) {
509 for (uint32_t j = 0; j < i; ++j) {
510 tmp -= y(j, 0) * l(i, j);
521 assert(u.getWidth() == u.getHeight());
522 assert(y.getHeight() == u.getHeight() && y.getWidth() == 1);
526 for (uint32_t i = u.getHeight(); i > 0; --i) {
529 for (uint32_t j = u.getHeight() - 1; j > i - 1; --j) {
530 tmp -= x(j, 0) * u(i - 1, j);
533 x(i - 1, 0, tmp / u(i - 1, i - 1));
544 template<
class Archive>
545 void serialize(Archive & ar,
const unsigned int version) {
556 result *= 1.0f / value;
563 *
this *= 1.0f / value;
void setZero()
sets all values of the matrix to zero
i6eMatrix(const i6eMatrix &other)
copy constructor
i6eMatrix operator-(const i6eMatrix &other)
operator for substraction of matrix
i6eMatrix operator+(const i6eMatrix &other)
operator for addition with matrix
void setIdentity()
sets matrix to be an identity matrix
i6eMatrix & operator-=(const i6eMatrix &other)
operator for substraction of matrix
bool operator==(const i6eMatrix &other)
returns true, if both matrixes are equal
uint32_t getHeight() const
returns height of the matrix
static i6eMatrix transpose(const i6eMatrix &other)
calculates the transposed matrix
i6eMatrix operator*=(const i6eMatrix &other)
multiplication with matrix
#define ISIXE_THROW_API(module, message)
i6eMatrix operator-()
flips values of the matrix
i6eMatrix & operator/=(const T &value)
division of matrix with a scalar
Implements 3-dimensional vectors.
i6eMatrix(const i6eVector &vector)
constructor taking an i6eVector
static i6eMatrix pow(const i6eMatrix &other, uint32_t amount)
calculates a given amount of multiplications for the given matrix
void setEntry(uint32_t m, uint32_t n, T value)
sets the given value to the given position
double calculateDeterminant() const
returns determinant of the matrix
T getEntry(uint32_t m, uint32_t n) const
returns entry at given position
i6eMatrix & operator+=(const i6eMatrix &other)
operator for addition with matrix
uint32_t getWidth() const
returns width of the matrix
i6eMatrix()
default constructor, creates an emtpy matrix
bool operator!=(const i6eMatrix &other) const
return true, if both matrixes aren't equal
void operator()(uint32_t m, uint32_t n, const T &value)
sets the given value to the given position
static i6eMatrix adjoint(const i6eMatrix &other)
calculates the adjoint of the given matrix
void solveSystem(const i6eMatrix &b, i6eMatrix &x)
solves the linear system mx = b using LU-decomposition, where m is the input square matrix and x is t...
i6eMatrix operator*(const T &value)
multiplication of matrix with a scalar
double getX() const
getters for the values of the Vector
i6eMatrix operator*(const i6eMatrix &other)
multiplication with matrix
friend class boost::serialization::access
i6eMatrix(uint32_t m, uint32_t n)
constructor taking dimension of the matrix setting all values to zero
static i6eMatrix invert(const i6eMatrix &other)
calculates the inverted of the given matrix
i6eMatrix operator/(const T &value)
division of matrix with scalar
i6eMatrix & operator*=(const T &value)
multiplication of matrix with a scalar
T operator()(uint32_t m, uint32_t n) const
returs the value at the given position