Skip to content

Commit

Permalink
Merge branch 'main' of github.com:pygfx/pylinalg into revamp-build
Browse files Browse the repository at this point in the history
  • Loading branch information
Korijn committed Dec 25, 2024
2 parents 172120b + 5ed8d25 commit 1aa9021
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
6 changes: 5 additions & 1 deletion pylinalg/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,11 @@ def mat_decompose(matrix, /, *, scaling_signs=None, dtype=None, out=None):
scaling *= scaling_signs

rotation = out[1] if out is not None else None
rotation_matrix = matrix[:-1, :-1] * (1 / scaling)[None, :]

rotation_matrix = matrix[:-1, :-1].copy().astype(float)
mask = scaling != 0
rotation_matrix[:, mask] /= scaling[mask][None, :]
rotation_matrix[:, ~mask] = 0.0
rotation = quat_from_mat(rotation_matrix, out=rotation, dtype=dtype)

return translation, rotation, scaling
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import pylinalg as la


def pytest_report_header(config, start_path, startdir):
def pytest_report_header(config):
# report the CPU model to allow detecting platform-specific problems
if platform.system() == "Windows":
name = (
Expand Down
16 changes: 16 additions & 0 deletions tests/test_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,22 @@ def test_mat_decompose():
npt.assert_array_almost_equal(rotation, [0, 0, np.sqrt(2) / 2, np.sqrt(2) / 2])


def test_mat_decompose_scaling_0():
"""Test that the matrices are decomposed correctly when scaling is 0."""

scaling = [0, 0, 2]
rotation = [0, 0, np.sqrt(2) / 2, np.sqrt(2) / 2]
translation = [2, 2, 2]

matrix = la.mat_compose(translation, rotation, scaling)
translation_, rotation_, scaling_ = la.mat_decompose(matrix)

npt.assert_array_almost_equal(translation_, translation)
npt.assert_array_almost_equal(scaling_, scaling)
# rotation is not uniquely defined when scaling is 0, but it should not be NaN
assert not np.isnan(rotation_).any()


@pytest.mark.parametrize(
"signs",
[
Expand Down

0 comments on commit 1aa9021

Please sign in to comment.