From 9e64974cb0ed09c9ddf84b191658be2311fd4308 Mon Sep 17 00:00:00 2001 From: Korijn van Golen Date: Fri, 27 Dec 2024 09:46:32 +0100 Subject: [PATCH 1/3] add unit test to compare to old implementation --- tests/test_matrix.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/test_matrix.py b/tests/test_matrix.py index bfcaa73..d77e7b7 100644 --- a/tests/test_matrix.py +++ b/tests/test_matrix.py @@ -268,6 +268,26 @@ def test_mat_compose_validation(): la.mat_decompose(matrix, scaling_signs=signs) +def naive_mat_compose(translation, rotation, scaling, /, *, out=None, dtype=None): + return la.mat_combine( + [ + la.mat_from_translation(translation), + la.mat_from_quat(rotation), + la.mat_from_scale(scaling), + ], + out=out, + dtype=dtype, + ) + + +def test_mat_compose_naive(): + """Compare the direct composition with the naive composition.""" + npt.assert_almost_equal( + la.mat_compose([1, 2, 3], [np.pi, np.pi / 4, 0, 1], [1, -2, 9]), + naive_mat_compose([1, 2, 3], [np.pi, np.pi / 4, 0, 1], [1, -2, 9]), + ) + + def test_mat_perspective(): a = la.mat_perspective(-1, 1, -1, 1, 1, 100) npt.assert_array_almost_equal( From 5aad0996ed3576a0f836e6fc06c50732b2c69044 Mon Sep 17 00:00:00 2001 From: Korijn van Golen Date: Fri, 27 Dec 2024 10:01:11 +0100 Subject: [PATCH 2/3] add support & test for scalar scaling argument to mat_compose --- pylinalg/matrix.py | 39 ++++++++++++++++++++------------------- tests/test_matrix.py | 15 ++++++++++++++- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/pylinalg/matrix.py b/pylinalg/matrix.py index 111969c..6ca22e3 100644 --- a/pylinalg/matrix.py +++ b/pylinalg/matrix.py @@ -323,27 +323,28 @@ def mat_compose(translation, rotation, scaling, /, *, out=None, dtype=None): wy = w * y2 wz = w * z2 + scaling = np.asarray(scaling) + if scaling.ndim == 0: + scaling = np.array([scaling, scaling, scaling]) + elif scaling.size == 1: + scaling = np.array([scaling[0], scaling[0], scaling[0]]) sx, sy, sz = scaling - out.flat[0] = (1 - (yy + zz)) * sx - out.flat[4] = (xy + wz) * sx - out.flat[8] = (xz - wy) * sx - out.flat[12] = 0 - - out.flat[1] = (xy - wz) * sy - out.flat[5] = (1 - (xx + zz)) * sy - out.flat[9] = (yz + wx) * sy - out.flat[13] = 0 - - out.flat[2] = (xz + wy) * sz - out.flat[6] = (yz - wx) * sz - out.flat[10] = (1 - (xx + yy)) * sz - out.flat[14] = 0 - - out.flat[3] = translation[0] - out.flat[7] = translation[1] - out.flat[11] = translation[2] - out.flat[15] = 1 + out[0, 0] = (1 - (yy + zz)) * sx + out[1, 0] = (xy + wz) * sx + out[2, 0] = (xz - wy) * sx + out[3, 0:3] = 0 + + out[0, 1] = (xy - wz) * sy + out[1, 1] = (1 - (xx + zz)) * sy + out[2, 1] = (yz + wx) * sy + + out[0, 2] = (xz + wy) * sz + out[1, 2] = (yz - wx) * sz + out[2, 2] = (1 - (xx + yy)) * sz + + out[0:3, 3] = translation + out[3, 3] = 1 return out diff --git a/tests/test_matrix.py b/tests/test_matrix.py index d77e7b7..b45d590 100644 --- a/tests/test_matrix.py +++ b/tests/test_matrix.py @@ -282,12 +282,25 @@ def naive_mat_compose(translation, rotation, scaling, /, *, out=None, dtype=None def test_mat_compose_naive(): """Compare the direct composition with the naive composition.""" - npt.assert_almost_equal( + npt.assert_equal( la.mat_compose([1, 2, 3], [np.pi, np.pi / 4, 0, 1], [1, -2, 9]), naive_mat_compose([1, 2, 3], [np.pi, np.pi / 4, 0, 1], [1, -2, 9]), ) +def test_mat_compose_scalar_scaling(): + """Check that a scaler scaling argument is supported in mat_compose.""" + npt.assert_equal( + la.mat_compose([1, 2, 3], [np.pi, np.pi / 4, 0, 1], [1.25, 1.25, 1.25]), + la.mat_compose([1, 2, 3], [np.pi, np.pi / 4, 0, 1], 1.25), + ) + + npt.assert_equal( + la.mat_compose([1, 2, 3], [np.pi, np.pi / 4, 0, 1], [1.25, 1.25, 1.25]), + la.mat_compose([1, 2, 3], [np.pi, np.pi / 4, 0, 1], [1.25]), + ) + + def test_mat_perspective(): a = la.mat_perspective(-1, 1, -1, 1, 1, 100) npt.assert_array_almost_equal( From 4703ae4e2a70ea8455a93533a3d3c49f7c79e081 Mon Sep 17 00:00:00 2001 From: Korijn van Golen Date: Fri, 27 Dec 2024 10:05:06 +0100 Subject: [PATCH 3/3] simplify --- pylinalg/matrix.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pylinalg/matrix.py b/pylinalg/matrix.py index 6ca22e3..70a887b 100644 --- a/pylinalg/matrix.py +++ b/pylinalg/matrix.py @@ -324,10 +324,8 @@ def mat_compose(translation, rotation, scaling, /, *, out=None, dtype=None): wz = w * z2 scaling = np.asarray(scaling) - if scaling.ndim == 0: - scaling = np.array([scaling, scaling, scaling]) - elif scaling.size == 1: - scaling = np.array([scaling[0], scaling[0], scaling[0]]) + if scaling.size == 1: + scaling = np.broadcast_to(scaling, (3,)) sx, sy, sz = scaling out[0, 0] = (1 - (yy + zz)) * sx