pymomentum.quaternion

pymomentum.quaternion.blend(quaternions: Tensor, weights_in: Tensor | None = None) Tensor

Blend multiple quaternions together using the method described in https://stackoverflow.com/questions/12374087/average-of-multiple-quaternions and http://www.acsu.buffalo.edu/~johnc/ave_quat07.pdf.

Parameters:
  • quaternions – A tensor of shape (…, k, 4) representing the quaternions to blend.

  • weights_in – An optional tensor of shape (…, k) representing the weights for each quaternion. If not provided, all quaternions will be weighted equally.

Returns:

A tensor of shape (…, 4) representing the blended quaternion.

pymomentum.quaternion.check(q: Tensor) None

Check if a tensor represents a quaternion.

Parameters:

q – A tensor representing a quaternion.

pymomentum.quaternion.check_and_normalize_weights(quaternions: Tensor, weights_in: Tensor | None = None) Tensor

Check and normalize the weights for blending quaternions.

Parameters:
  • quaternions – A tensor of shape (…, k, 4) representing the quaternions to blend.

  • weights_in – An optional tensor of shape (…, k) representing the weights for each quaternion. If not provided, all quaternions will be weighted equally.

Returns:

A tensor of shape (…, k) representing the normalized weights.

pymomentum.quaternion.conjugate(q: Tensor) Tensor

Conjugate a quaternion.

Parameters:

q – A quaternion ((x, y, z), w)).

Returns:

The conjugate.

pymomentum.quaternion.euler_xyz_to_quaternion(euler_xyz: Tensor) Tensor

Convert Euler XYZ angles to a quaternion.

Parameters:

euler_xyz – A tensor of shape (…, 3) representing the Euler XYZ angles.

Returns:

A tensor of shape (…, 4) representing the quaternion in ((x, y, z), w) format.

pymomentum.quaternion.from_axis_angle(axis_angle: Tensor) Tensor

Convert an axis-angle tensor to a quaternion.

Parameters:

axis_angle – A tensor of shape (…, 3) representing the axis-angle.

Returns:

A tensor of shape (…, 4) representing the quaternion in ((x, y, z), w) format.

pymomentum.quaternion.from_rotation_matrix(matrices: Tensor) Tensor

Convert a rotation matrix to a quaternion.

Parameters:

matrices – A tensor of shape (…, 3, 3) representing the rotation matrices.

Returns:

A tensor of shape (…, 4) representing the quaternions in ((x, y, z), w) format.

pymomentum.quaternion.from_two_vectors(v1: Tensor, v2: Tensor) Tensor

Construct a quaternion that rotates one vector into another.

Parameters:
  • v1 – The initial vector.

  • v2 – The target vector.

Returns:

A quaternion representing the rotation from v1 to v2.

pymomentum.quaternion.identity(size: Sequence[int] | None = None, device: device | None = None, dtype: dtype = torch.float32) Tensor

Create a quaternion identity tensor.

Parameters:
  • sizes – A tuple of integers representing the size of the quaternion tensor.

  • device – The device on which to create the tensor.

Returns:

A quaternion identity tensor with the specified sizes and device.

pymomentum.quaternion.inverse(q: Tensor) Tensor

Compute the inverse of a quaternion.

Parameters:

q – A quaternion ((x, y, z), w)).

Returns:

The inverse.

pymomentum.quaternion.multiply(q1: Tensor, q2: Tensor) Tensor

Multiply two quaternions together.

Parameters:
  • q1 – A quaternion ((x, y, z), w)).

  • q2 – A quaternion ((x, y, z), w)).

Returns:

The product q1*q2.

pymomentum.quaternion.normalize(q: Tensor) Tensor

Normalize a quaternion.

Parameters:

q – A quaternion ((x, y, z), w)).

Returns:

The normalized quaternion.

pymomentum.quaternion.quaternion_to_xyz_euler(q: Tensor) Tensor

Convert quaternions to XYZ Euler rotations.

Parameters:

quat – (nBatch x k x 4) tensor with the quaternions in ((x, y, z), w) format.

Returns:

A (nBatch x k x 3) tensor containing (x, y, z) Euler angles.

pymomentum.quaternion.rotate_vector(q: Tensor, v: Tensor) Tensor

Rotate a vector by a quaternion.

Parameters:
  • q – (nBatch x k x 4) tensor with the quaternions in ((x, y, z), w) format.

  • v – (nBatch x k x 3) vector.

Returns:

(nBatch x k x 3) rotated vectors.

pymomentum.quaternion.split(q: Tensor) tuple[Tensor, Tensor]

Split a quaternion into its scalar and vector parts.

Parameters:

q – A tensor representing a quaternion.

Returns:

The scalar and vector parts of the quaternion.

pymomentum.quaternion.to_rotation_matrix(q: Tensor) Tensor

Convert quaternions to 3x3 rotation matrices.

Parameters:

q – (nBatch x k x 4) tensor with the quaternions in ((x, y, z), w) format.

Returns:

(nBatch x k x 3 x 3) tensor with 3x3 rotation matrices.