Skip to content

Commit

Permalink
add support & test for scalar scaling argument to mat_compose
Browse files Browse the repository at this point in the history
  • Loading branch information
Korijn committed Dec 27, 2024
1 parent 9e64974 commit 5aad099
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
39 changes: 20 additions & 19 deletions pylinalg/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 14 additions & 1 deletion tests/test_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 5aad099

Please sign in to comment.