79 explicit Matrix(
const float (&_m)[16])
82 for (
int i = 0; i < 16; ++i)
91 explicit Matrix(
const float (&_m)[4][4])
94 for (
int c = 0; c < 4; ++c)
96 for (
int r = 0; r < 4; ++r)
109 void Set(
int row,
int col,
float value)
111 m[(col-1)*4+(row-1)] = value;
120 float Get(
int row,
int col)
122 return m[(col-1)*4+(row-1)];
128 for (
int i = 0; i < 16; ++i)
145 return reinterpret_cast<float*
>(
this);
164 for (
int i = 0; i < 4; ++i)
179 assert(r >= 0 && r <= 3);
180 assert(c >= 0 && c <= 3);
193 result = + m[5 ] * (m[10] * m[15] - m[14] * m[11])
194 - m[9 ] * (m[6 ] * m[15] - m[14] * m[7 ])
195 + m[13] * (m[6 ] * m[11] - m[10] * m[7 ]);
203 result = - m[1 ] * (m[10] * m[15] - m[14] * m[11])
204 + m[9 ] * (m[2 ] * m[15] - m[14] * m[3 ])
205 - m[13] * (m[2 ] * m[11] - m[10] * m[3 ]);
213 result = + m[1 ] * (m[6 ] * m[15] - m[14] * m[7 ])
214 - m[5 ] * (m[2 ] * m[15] - m[14] * m[3 ])
215 + m[13] * (m[2 ] * m[7 ] - m[6 ] * m[3 ]);
223 result = - m[1 ] * (m[6 ] * m[11] - m[10] * m[7 ])
224 + m[5 ] * (m[2 ] * m[11] - m[10] * m[3 ])
225 - m[9 ] * (m[2 ] * m[7 ] - m[6 ] * m[3 ]);
233 result = - m[4 ] * (m[10] * m[15] - m[14] * m[11])
234 + m[8 ] * (m[6 ] * m[15] - m[14] * m[7 ])
235 - m[12] * (m[6 ] * m[11] - m[10] * m[7 ]);
243 result = + m[0 ] * (m[10] * m[15] - m[14] * m[11])
244 - m[8 ] * (m[2 ] * m[15] - m[14] * m[3 ])
245 + m[12] * (m[2 ] * m[11] - m[10] * m[3 ]);
253 result = - m[0 ] * (m[6 ] * m[15] - m[14] * m[7 ])
254 + m[4 ] * (m[2 ] * m[15] - m[14] * m[3 ])
255 - m[12] * (m[2 ] * m[7 ] - m[6 ] * m[3 ]);
263 result = + m[0 ] * (m[6 ] * m[11] - m[10] * m[7 ])
264 - m[4 ] * (m[2 ] * m[11] - m[10] * m[3 ])
265 + m[8 ] * (m[2 ] * m[7 ] - m[6 ] * m[3 ]);
273 result = + m[4 ] * (m[9 ] * m[15] - m[13] * m[11])
274 - m[8 ] * (m[5 ] * m[15] - m[13] * m[7 ])
275 + m[12] * (m[5 ] * m[11] - m[9 ] * m[7 ]);
283 result = - m[0 ] * (m[9 ] * m[15] - m[13] * m[11])
284 + m[8 ] * (m[1 ] * m[15] - m[13] * m[3 ])
285 - m[12] * (m[1 ] * m[11] - m[9 ] * m[3 ]);
293 result = + m[0 ] * (m[5 ] * m[15] - m[13] * m[7 ])
294 - m[4 ] * (m[1 ] * m[15] - m[13] * m[3 ])
295 + m[12] * (m[1 ] * m[7 ] - m[5 ] * m[3 ]);
303 result = - m[0 ] * (m[5 ] * m[11] - m[9 ] * m[7 ])
304 + m[4 ] * (m[1 ] * m[11] - m[9 ] * m[3 ])
305 - m[8 ] * (m[1 ] * m[7 ] - m[5 ] * m[3 ]);
313 result = - m[4 ] * (m[9 ] * m[14] - m[13] * m[10])
314 + m[8 ] * (m[5 ] * m[14] - m[13] * m[6 ])
315 - m[12] * (m[5 ] * m[10] - m[9 ] * m[6 ]);
323 result = + m[0 ] * (m[9 ] * m[14] - m[13] * m[10])
324 - m[8 ] * (m[1 ] * m[14] - m[13] * m[2 ])
325 + m[12] * (m[1 ] * m[10] - m[9 ] * m[2 ]);
333 result = - m[0 ] * (m[5 ] * m[14] - m[13] * m[6 ])
334 + m[4 ] * (m[1 ] * m[14] - m[13] * m[2 ])
335 - m[12] * (m[1 ] * m[6 ] - m[5 ] * m[2 ]);
343 result = + m[0 ] * (m[5 ] * m[10] - m[9 ] * m[6 ])
344 - m[4 ] * (m[1 ] * m[10] - m[9 ] * m[2 ])
345 + m[8 ] * (m[1 ] * m[6 ] - m[5 ] * m[2 ]);
365 float result[16] = { 0.0f };
367 for (
int r = 0; r < 4; ++r)
369 for (
int c = 0; c < 4; ++c)
372 result[4*r+c] = (1.0f / d) *
Cofactor(r, c);
386 float result[16] = { 0.0f };
388 for (
int c = 0; c < 4; ++c)
390 for (
int r = 0; r < 4; ++r)
392 result[4*c+r] = 0.0f;
393 for (
int i = 0; i < 4; ++i)
395 result[4*c+r] += m[4*i+r] * right.
m[4*c+i];
408 for (
int i = 0; i < 16; ++i)
410 if (!
IsEqual(m1.
m[i], m2.
m[i], tolerance))
449 float x = v.
x * m.
m[0 ] + v.
y * m.
m[4 ] + v.
z * m.
m[8 ] + m.
m[12];
450 float y = v.
x * m.
m[1 ] + v.
y * m.
m[5 ] + v.
z * m.
m[9 ] + m.
m[13];
451 float z = v.
x * m.
m[2 ] + v.
y * m.
m[6 ] + v.
z * m.
m[10] + m.
m[14];
456 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:133
void Transpose()
Transposes the matrix.
Definition: matrix.h:149
const float TOLERANCE
Tolerance level – minimum accepted float value.
Definition: const.h:37
bool IsZero(float a, float tolerance=Math::TOLERANCE)
Compares a to zero within tolerance.
Definition: func.h:47
void LoadZero()
Loads the zero matrix.
Definition: matrix.h:126
bool MatricesEqual(const Matrix &m1, const Matrix &m2, float tolerance=TOLERANCE)
Checks if two matrices are equal within given tolerance.
Definition: matrix.h:405
void Set(int row, int col, float value)
Sets value in given row and col.
Definition: matrix.h:109
Matrix(const float(&_m)[16])
Creates the matrix from 1D array.
Definition: matrix.h:79
4x4 matrix
Definition: matrix.h:65
float x
X - 1st coord.
Definition: vector.h:56
Math::Matrix MultiplyMatrices(const Math::Matrix &left, const Math::Matrix &right)
Convenience function for multiplying a matrix.
Definition: matrix.h:429
Matrix()
Creates the indentity matrix.
Definition: matrix.h:71
float Get(int row, int col)
Returns the value in given row and col.
Definition: matrix.h:120
bool IsEqual(float a, float b, float tolerance=Math::TOLERANCE)
Compares a and b within tolerance.
Definition: func.h:41
Math::Vector MatrixVectorMultiply(const Math::Matrix &m, const Math::Vector &v, bool wDivide=false)
Calculates the result of multiplying m * v.
Definition: matrix.h:447
float Cofactor(int r, int c) const
Calculates the cofactor of the matrix.
Definition: matrix.h:177
Namespace for (new) math code.
Definition: device.h:39
void Swap(int &a, int &b)
Swaps two integers.
Definition: func.h:114
float z
Z - 3rd coord.
Definition: vector.h:60
Constants used in math functions.
Matrix Inverse() const
Calculates the inverse matrix.
Definition: matrix.h:360
Vector struct and related functions.
float * Array()
Returns the struct cast to float* array; use with care!
Definition: matrix.h:143
3D (3x1) vector
Definition: vector.h:53
float Det() const
Calculates the determinant of the matrix.
Definition: matrix.h:161
Matrix(const float(&_m)[4][4])
Creates the matrix from 2D array.
Definition: matrix.h:91
Matrix Multiply(const Matrix &right) const
Calculates the multiplication of this matrix * given matrix.
Definition: matrix.h:384
float y
Y - 2nd coord.
Definition: vector.h:58
float m[16]
Matrix values in column-major order.
Definition: matrix.h:68