Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix matrix inverse and add another test for it #24

Merged
merged 1 commit into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/srctools/_math_matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ bool mat3_inverse(const mat3_t* in, mat3_t* out)
}

*out =
{omat[0][3], omat[0][4], omat[0][5],
omat[1][3], omat[1][4], omat[1][5],
omat[2][3], omat[2][4], omat[2][5]};
{mat[0][3], mat[0][4], mat[0][5],
mat[1][3], mat[1][4], mat[1][5],
mat[2][3], mat[2][4], mat[2][5]};

return true;
}
6 changes: 3 additions & 3 deletions src/srctools/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -1933,9 +1933,9 @@ def inverse(self: MatrixT) -> MatrixT:
cls: Type[MatrixT] = type(self)
out = cls.__new__(cls)

out._aa, out._ab, out._ac = omat_r[0][0], omat_r[0][1], omat_r[0][2]
out._ba, out._bb, out._bc = omat_r[1][0], omat_r[1][1], omat_r[1][2]
out._ca, out._cb, out._cc = omat_r[2][0], omat_r[2][1], omat_r[2][2]
out._aa, out._ab, out._ac = mat_r[0][0], mat_r[0][1], mat_r[0][2]
out._ba, out._bb, out._bc = mat_r[1][0], mat_r[1][1], mat_r[1][2]
out._ca, out._cb, out._cc = mat_r[2][0], mat_r[2][1], mat_r[2][2]

return out

Expand Down
14 changes: 14 additions & 0 deletions tests/test_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,20 @@ def test_inverse_known(frozen_thawed_matrix: MatrixClass) -> None:

assert_rot(invert, correct, type=frozen_thawed_matrix)

# Test for matrix with known inverse
mat[0, 0], mat[0, 1], mat[0, 2] = ( 0.0, 0.0, -1.0)
mat[1, 0], mat[1, 1], mat[1, 2] = (-1.0, 0.0, 0.0)
mat[2, 0], mat[2, 1], mat[2, 2] = ( 0.0, 1.0, 0.0)

correct[0, 0], correct[0, 1], correct[0, 2] = ( 0.0, -1.0, 0.0)
correct[1, 0], correct[1, 1], correct[1, 2] = ( 0.0, 0.0, 1.0)
correct[2, 0], correct[2, 1], correct[2, 2] = (-1.0, 0.0, 0.0)

to_invert = frozen_thawed_matrix(mat)
invert = to_invert.inverse()

assert_rot(invert, correct, type=frozen_thawed_matrix)


def test_inverse_fail(frozen_thawed_matrix: MatrixClass) -> None:
"""Test the matrix inverse() method for known failure. """
Expand Down