torchref.base.alignment.rotation module

Rotation utility functions.

Functions for coordinate rotations and rotation matrix conversions including axis-angle, quaternion, and Euler representations.

torchref.base.alignment.rotation.rotate_coords_torch(coords, phi, rho)[source]

Rotate coordinates using phi and rho angles (PyTorch version).

Parameters:
  • coords (torch.Tensor) – Coordinates of shape (N, 3) to rotate.

  • phi (float) – Rotation angle phi in degrees.

  • rho (float) – Rotation angle rho in degrees.

Returns:

Rotated coordinates of shape (N, 3).

Return type:

torch.Tensor

torchref.base.alignment.rotation.rotate_coords_numpy(coords, phi, rho)[source]

Rotate 3D coordinates by phi and rho angles (NumPy version).

Applies a rotation transformation to a set of 3D coordinates using two rotation angles (phi and rho) in degrees.

Parameters:
  • coords (numpy.ndarray) – Array of 3D coordinates with shape (N, 3).

  • phi (float) – First rotation angle in degrees.

  • rho (float) – Second rotation angle in degrees.

Returns:

Rotated coordinates with the same shape as input (N, 3).

Return type:

numpy.ndarray

torchref.base.alignment.rotation.axis_angle_to_rotation_matrix(axis_angle)[source]

Convert axis-angle representation to 3x3 rotation matrix.

Uses Rodrigues’ formula. Supports batched input and gradients.

Parameters:

axis_angle (torch.Tensor) – Axis-angle representation with shape (3,) or (N, 3). Direction is rotation axis, magnitude is angle in radians.

Returns:

Rotation matrix with shape (3, 3) or (N, 3, 3).

Return type:

torch.Tensor

torchref.base.alignment.rotation.rotation_matrix_to_axis_angle(R)[source]

Convert 3x3 rotation matrix to axis-angle representation.

Parameters:

R (torch.Tensor) – Rotation matrix with shape (3, 3) or (N, 3, 3).

Returns:

Axis-angle representation with shape (3,) or (N, 3).

Return type:

torch.Tensor

torchref.base.alignment.rotation.quaternion_to_rotation_matrix(q)[source]

Convert quaternion to rotation matrix.

Parameters:

q (torch.Tensor) – Quaternion with shape (4,) or (N, 4). Format: [w, x, y, z].

Returns:

Rotation matrix with shape (3, 3) or (N, 3, 3).

Return type:

torch.Tensor

torchref.base.alignment.rotation.random_rotation_uniform(n=1, device=device(type='cpu'), dtype=torch.float32)[source]

Generate uniform random rotations over SO(3).

Uses Shoemake’s quaternion-based uniform sampling.

Parameters:
  • n (int, optional) – Number of rotations to generate. Default is 1.

  • device (str, optional) – Device for output tensor. Defaults to the configured device.current.

  • dtype (torch.dtype, optional) – Data type for output tensor. Default is dtypes.float.

Returns:

Rotation matrices with shape (n, 3, 3) or (3, 3) if n=1.

Return type:

torch.Tensor

torchref.base.alignment.rotation.rotation_matrix_euler_zyz(angles)[source]

Create rotation matrix from ZYZ Euler angles (differentiable PyTorch version).

R = Rz(alpha) @ Ry(beta) @ Rz(gamma)

Parameters:

angles (torch.Tensor) – Tensor of three rotation angles (alpha, beta, gamma) in radians. Or shape (B, 3) for batched input. The function will return (B, 3, 3) in that case.

Returns:

3x3 rotation matrix.

Return type:

torch.Tensor