MCPcopy Create free account
hub / github.com/MotrixLab/MotionDiffuse / quaternion_to_matrix

Function quaternion_to_matrix

text2motion/utils/quaternion.py:274–300  ·  view source on GitHub ↗

Convert rotations given as quaternions to rotation matrices. Args: quaternions: quaternions with real part first, as tensor of shape (..., 4). Returns: Rotation matrices as tensor of shape (..., 3, 3).

(quaternions)

Source from the content-addressed store, hash-verified

272
273
274def quaternion_to_matrix(quaternions):
275 """
276 Convert rotations given as quaternions to rotation matrices.
277 Args:
278 quaternions: quaternions with real part first,
279 as tensor of shape (..., 4).
280 Returns:
281 Rotation matrices as tensor of shape (..., 3, 3).
282 """
283 r, i, j, k = torch.unbind(quaternions, -1)
284 two_s = 2.0 / (quaternions * quaternions).sum(-1)
285
286 o = torch.stack(
287 (
288 1 - two_s * (j * j + k * k),
289 two_s * (i * j - k * r),
290 two_s * (i * k + j * r),
291 two_s * (i * j + k * r),
292 1 - two_s * (i * i + k * k),
293 two_s * (j * k - i * r),
294 two_s * (i * k - j * r),
295 two_s * (j * k + i * r),
296 1 - two_s * (i * i + j * j),
297 ),
298 -1,
299 )
300 return o.reshape(quaternions.shape[:-1] + (3, 3))
301
302
303def quaternion_to_matrix_np(quaternions):

Callers 2

quaternion_to_matrix_npFunction · 0.85
quaternion_to_cont6dFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected