Merge pull request #12529 from iwubcode/math_determinant

Common: add determinant functions to Matrix33 and Matrix44
This commit is contained in:
Admiral H. Curtiss 2024-01-26 19:13:38 +01:00 committed by GitHub
commit b0491c2ce8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 5 deletions

View File

@ -250,11 +250,7 @@ Matrix33 Matrix33::Inverted() const
{
const auto m = [this](int x, int y) { return data[y + x * 3]; };
const auto det = m(0, 0) * (m(1, 1) * m(2, 2) - m(2, 1) * m(1, 2)) -
m(0, 1) * (m(1, 0) * m(2, 2) - m(1, 2) * m(2, 0)) +
m(0, 2) * (m(1, 0) * m(2, 1) - m(1, 1) * m(2, 0));
const auto invdet = 1 / det;
const auto invdet = 1 / Determinant();
Matrix33 result;
@ -273,6 +269,15 @@ Matrix33 Matrix33::Inverted() const
return result;
}
float Matrix33::Determinant() const
{
const auto m = [this](int x, int y) { return data[y + x * 3]; };
return m(0, 0) * (m(1, 1) * m(2, 2) - m(2, 1) * m(1, 2)) -
m(0, 1) * (m(1, 0) * m(2, 2) - m(1, 2) * m(2, 0)) +
m(0, 2) * (m(1, 0) * m(2, 1) - m(1, 1) * m(2, 0));
}
Matrix44 Matrix44::Identity()
{
Matrix44 mtx = {};
@ -360,4 +365,17 @@ void Matrix44::Multiply(const Matrix44& a, const Vec4& vec, Vec4* result)
result->data = MatrixMultiply<4, 4, 1>(a.data, vec.data);
}
float Matrix44::Determinant() const
{
const auto& m = data;
return m[12] * m[9] * m[6] * m[3] - m[8] * m[13] * m[6] * m[3] - m[12] * m[5] * m[10] * m[3] +
m[4] * m[13] * m[10] * m[3] + m[8] * m[5] * m[14] * m[3] - m[4] * m[9] * m[14] * m[3] -
m[12] * m[9] * m[2] * m[7] + m[8] * m[13] * m[2] * m[7] + m[12] * m[1] * m[10] * m[7] -
m[0] * m[13] * m[10] * m[7] - m[8] * m[1] * m[14] * m[7] + m[0] * m[9] * m[14] * m[7] +
m[12] * m[5] * m[2] * m[11] - m[4] * m[13] * m[2] * m[11] - m[12] * m[1] * m[6] * m[11] +
m[0] * m[13] * m[6] * m[11] + m[4] * m[1] * m[14] * m[11] - m[0] * m[5] * m[14] * m[11] -
m[8] * m[5] * m[2] * m[15] + m[4] * m[9] * m[2] * m[15] + m[8] * m[1] * m[6] * m[15] -
m[0] * m[9] * m[6] * m[15] - m[4] * m[1] * m[10] * m[15] + m[0] * m[5] * m[10] * m[15];
}
} // namespace Common

View File

@ -392,6 +392,7 @@ public:
static void Multiply(const Matrix33& a, const Vec3& vec, Vec3* result);
Matrix33 Inverted() const;
float Determinant() const;
Matrix33& operator*=(const Matrix33& rhs)
{
@ -432,6 +433,8 @@ public:
// For when a vec4 isn't needed a multiplication function that takes a Vec3 and w:
Vec3 Transform(const Vec3& point, float w) const;
float Determinant() const;
Matrix44& operator*=(const Matrix44& rhs)
{
Multiply(*this, rhs, this);