79 inline explicit Matrix(
const float (&_m)[16])
81 for (
int i = 0; i < 16; ++i)
90 inline explicit Matrix(
const float (&_m)[4][4])
92 for (
int c = 0; c < 4; ++c)
94 for (
int r = 0; r < 4; ++r)
107 inline void Set(
int row,
int col,
float value)
109 m[(col-1)*4+(row-1)] = value;
118 inline float Get(
int row,
int col)
120 return m[(col-1)*4+(row-1)];
126 for (
int i = 0; i < 16; ++i)
143 return reinterpret_cast<float*
>(
this);
162 for (
int i = 0; i < 4; ++i)
177 assert(r >= 0 && r <= 3);
178 assert(c >= 0 && c <= 3);
191 result = + m[5 ] * (m[10] * m[15] - m[14] * m[11])
192 - m[9 ] * (m[6 ] * m[15] - m[14] * m[7 ])
193 + m[13] * (m[6 ] * m[11] - m[10] * m[7 ]);
201 result = - m[1 ] * (m[10] * m[15] - m[14] * m[11])
202 + m[9 ] * (m[2 ] * m[15] - m[14] * m[3 ])
203 - m[13] * (m[2 ] * m[11] - m[10] * m[3 ]);
211 result = + m[1 ] * (m[6 ] * m[15] - m[14] * m[7 ])
212 - m[5 ] * (m[2 ] * m[15] - m[14] * m[3 ])
213 + m[13] * (m[2 ] * m[7 ] - m[6 ] * m[3 ]);
221 result = - m[1 ] * (m[6 ] * m[11] - m[10] * m[7 ])
222 + m[5 ] * (m[2 ] * m[11] - m[10] * m[3 ])
223 - m[9 ] * (m[2 ] * m[7 ] - m[6 ] * m[3 ]);
231 result = - m[4 ] * (m[10] * m[15] - m[14] * m[11])
232 + m[8 ] * (m[6 ] * m[15] - m[14] * m[7 ])
233 - m[12] * (m[6 ] * m[11] - m[10] * m[7 ]);
241 result = + m[0 ] * (m[10] * m[15] - m[14] * m[11])
242 - m[8 ] * (m[2 ] * m[15] - m[14] * m[3 ])
243 + m[12] * (m[2 ] * m[11] - m[10] * m[3 ]);
251 result = - m[0 ] * (m[6 ] * m[15] - m[14] * m[7 ])
252 + m[4 ] * (m[2 ] * m[15] - m[14] * m[3 ])
253 - m[12] * (m[2 ] * m[7 ] - m[6 ] * m[3 ]);
261 result = + m[0 ] * (m[6 ] * m[11] - m[10] * m[7 ])
262 - m[4 ] * (m[2 ] * m[11] - m[10] * m[3 ])
263 + m[8 ] * (m[2 ] * m[7 ] - m[6 ] * m[3 ]);
271 result = + m[4 ] * (m[9 ] * m[15] - m[13] * m[11])
272 - m[8 ] * (m[5 ] * m[15] - m[13] * m[7 ])
273 + m[12] * (m[5 ] * m[11] - m[9 ] * m[7 ]);
281 result = - m[0 ] * (m[9 ] * m[15] - m[13] * m[11])
282 + m[8 ] * (m[1 ] * m[15] - m[13] * m[3 ])
283 - m[12] * (m[1 ] * m[11] - m[9 ] * m[3 ]);
291 result = + m[0 ] * (m[5 ] * m[15] - m[13] * m[7 ])
292 - m[4 ] * (m[1 ] * m[15] - m[13] * m[3 ])
293 + m[12] * (m[1 ] * m[7 ] - m[5 ] * m[3 ]);
301 result = - m[0 ] * (m[5 ] * m[11] - m[9 ] * m[7 ])
302 + m[4 ] * (m[1 ] * m[11] - m[9 ] * m[3 ])
303 - m[8 ] * (m[1 ] * m[7 ] - m[5 ] * m[3 ]);
311 result = - m[4 ] * (m[9 ] * m[14] - m[13] * m[10])
312 + m[8 ] * (m[5 ] * m[14] - m[13] * m[6 ])
313 - m[12] * (m[5 ] * m[10] - m[9 ] * m[6 ]);
321 result = + m[0 ] * (m[9 ] * m[14] - m[13] * m[10])
322 - m[8 ] * (m[1 ] * m[14] - m[13] * m[2 ])
323 + m[12] * (m[1 ] * m[10] - m[9 ] * m[2 ]);
331 result = - m[0 ] * (m[5 ] * m[14] - m[13] * m[6 ])
332 + m[4 ] * (m[1 ] * m[14] - m[13] * m[2 ])
333 - m[12] * (m[1 ] * m[6 ] - m[5 ] * m[2 ]);
341 result = + m[0 ] * (m[5 ] * m[10] - m[9 ] * m[6 ])
342 - m[4 ] * (m[1 ] * m[10] - m[9 ] * m[2 ])
343 + m[8 ] * (m[1 ] * m[6 ] - m[5 ] * m[2 ]);
363 float result[16] = { 0.0f };
365 for (
int r = 0; r < 4; ++r)
367 for (
int c = 0; c < 4; ++c)
370 result[4*r+c] = (1.0f / d) *
Cofactor(r, c);
384 float result[16] = { 0.0f };
386 for (
int c = 0; c < 4; ++c)
388 for (
int r = 0; r < 4; ++r)
390 result[4*c+r] = 0.0f;
391 for (
int i = 0; i < 4; ++i)
393 result[4*c+r] += m[4*i+r] * right.
m[4*c+i];
406 for (
int i = 0; i < 16; ++i)
408 if (!
IsEqual(m1.
m[i], m2.
m[i], tolerance))
447 float x = v.
x * m.
m[0 ] + v.
y * m.
m[4 ] + v.
z * m.
m[8 ] + m.
m[12];
448 float y = v.
x * m.
m[1 ] + v.
y * m.
m[5 ] + v.
z * m.
m[9 ] + m.
m[13];
449 float z = v.
x * m.
m[2 ] + v.
y * m.
m[6 ] + v.
z * m.
m[10] + m.
m[14];
454 float w = v.
x * m.
m[3 ] + v.
y * m.
m[7 ] + v.
z * m.
m[11] + m.
m[15];
void LoadIdentity()
Loads the identity matrix.
Definition: matrix.h:131
void Transpose()
Transposes the matrix.
Definition: matrix.h:147
const float TOLERANCE
Tolerance level – minimum accepted float value.
Definition: const.h:36
bool IsZero(float a, float tolerance=Math::TOLERANCE)
Compares a to zero within tolerance.
Definition: func.h:46
void LoadZero()
Loads the zero matrix.
Definition: matrix.h:124
bool MatricesEqual(const Matrix &m1, const Matrix &m2, float tolerance=TOLERANCE)
Checks if two matrices are equal within given tolerance.
Definition: matrix.h:403
void Set(int row, int col, float value)
Sets value in given row and col.
Definition: matrix.h:107
Matrix(const float(&_m)[16])
Creates the matrix from 1D array.
Definition: matrix.h:79
4x4 matrix
Definition: matrix.h:66
float x
X - 1st coord.
Definition: vector.h:55
Math::Matrix MultiplyMatrices(const Math::Matrix &left, const Math::Matrix &right)
Convenience function for multiplying a matrix.
Definition: matrix.h:427
Matrix()
Creates the indentity matrix.
Definition: matrix.h:72
float Get(int row, int col)
Returns the value in given row and col.
Definition: matrix.h:118
Math::Matrix Transpose(const Math::Matrix &m)
Convenience function for getting transposed matrix.
Definition: matrix.h:416
bool IsEqual(float a, float b, float tolerance=Math::TOLERANCE)
Compares a and b within tolerance.
Definition: func.h:40
Math::Vector MatrixVectorMultiply(const Math::Matrix &m, const Math::Vector &v, bool wDivide=false)
Calculates the result of multiplying m * v.
Definition: matrix.h:445
float Cofactor(int r, int c) const
Calculates the cofactor of the matrix.
Definition: matrix.h:175
Namespace for (new) math code.
Definition: const.h:32
void Swap(int &a, int &b)
Swaps two integers.
Definition: func.h:104
float z
Z - 3rd coord.
Definition: vector.h:59
Constants used in math functions.
Matrix Inverse() const
Calculates the inverse matrix.
Definition: matrix.h:358
Vector struct and related functions.
float * Array()
Returns the struct cast to float* array; use with care!
Definition: matrix.h:141
3D (3x1) vector
Definition: vector.h:52
float Det() const
Calculates the determinant of the matrix.
Definition: matrix.h:159
Matrix(const float(&_m)[4][4])
Creates the matrix from 2D array.
Definition: matrix.h:90
Matrix Multiply(const Matrix &right) const
Calculates the multiplication of this matrix * given matrix.
Definition: matrix.h:382
float y
Y - 2nd coord.
Definition: vector.h:57
float m[16]
Matrix values in column-major order.
Definition: matrix.h:69