Skip to content

Commit c7138c9

Browse files
authored
Direct matrix composition (#92)
* direct matrix composition * use python syntax
1 parent 4058474 commit c7138c9

File tree

1 file changed

+40
-11
lines changed

1 file changed

+40
-11
lines changed

pylinalg/matrix.py

+40-11
Original file line numberDiff line numberDiff line change
@@ -306,17 +306,46 @@ def mat_compose(translation, rotation, scaling, /, *, out=None, dtype=None):
306306
ndarray, [4, 4]
307307
Transformation matrix
308308
"""
309-
from .quaternion import mat_from_quat
310-
311-
return mat_combine(
312-
[
313-
mat_from_translation(translation),
314-
mat_from_quat(rotation),
315-
mat_from_scale(scaling),
316-
],
317-
out=out,
318-
dtype=dtype,
319-
)
309+
if out is None:
310+
out = np.empty((4, 4), dtype=dtype)
311+
312+
x, y, z, w = rotation
313+
x2 = x + x
314+
y2 = y + y
315+
z2 = z + z
316+
xx = x * x2
317+
xy = x * y2
318+
xz = x * z2
319+
yy = y * y2
320+
yz = y * z2
321+
zz = z * z2
322+
wx = w * x2
323+
wy = w * y2
324+
wz = w * z2
325+
326+
sx, sy, sz = scaling
327+
328+
out.flat[0] = (1 - (yy + zz)) * sx
329+
out.flat[4] = (xy + wz) * sx
330+
out.flat[8] = (xz - wy) * sx
331+
out.flat[12] = 0
332+
333+
out.flat[1] = (xy - wz) * sy
334+
out.flat[5] = (1 - (xx + zz)) * sy
335+
out.flat[9] = (yz + wx) * sy
336+
out.flat[13] = 0
337+
338+
out.flat[2] = (xz + wy) * sz
339+
out.flat[6] = (yz - wx) * sz
340+
out.flat[10] = (1 - (xx + yy)) * sz
341+
out.flat[14] = 0
342+
343+
out.flat[3] = translation[0]
344+
out.flat[7] = translation[1]
345+
out.flat[11] = translation[2]
346+
out.flat[15] = 1
347+
348+
return out
320349

321350

322351
def mat_decompose(matrix, /, *, scaling_signs=None, dtype=None, out=None):

0 commit comments

Comments
 (0)