affine#

voxel.affine.translation_matrix(translation) → AffineMatrix#

Compute a 3D translation matrix from translation vector.

Parameters:

translation (Tensor) – Translation vector.

Returns:

AffineMatrix – Translation affine matrix.

voxel.affine.angles_to_rotation_matrix(rotation, degrees=True, dtype=torch.float32) → AffineMatrix#

Compute a 3D rotation matrix from rotation angles, following the standard right-handed convention.

Parameters:
  • rotation (Tensor) – Rotation angles (x, y, z). If degrees is True, the angles are in degrees, otherwise they are in radians.

  • degrees (bool, optional) – Whether the angles are defined as degrees or, alternatively, as radians.

  • dtype (dtype, optional) – Data type of the generated matrix. Defaults to float32. Use float64 to retain full-precision gradients.

Returns:

AffineMatrix – Rotation affine matrix.

voxel.affine.quaternion_to_rotation_matrix(quaternion, dtype=torch.float32) → AffineMatrix#

Compute a 3D rotation matrix from a quaternion.

The quaternion is expected in scalar-first (w, x, y, z) order and is normalized to unit length before the matrix is built, so any non-unit input still yields a valid rotation.

Parameters:
  • quaternion (Tensor) – Quaternion of shape (4,) in (w, x, y, z) order. It need not be normalized.

  • dtype (dtype, optional) – Data type of the generated matrix. Defaults to float32. Use float64 to retain full-precision gradients.

Returns:

AffineMatrix – Rotation affine matrix.

voxel.affine.compose_affine(translation=None, rotation=None, scale=None, shear=None, degrees=True, device=None, dtype=torch.float32) → AffineMatrix#

Composes an affine matrix from a set of translation, rotation, scale, and shear transform components.

Parameters:
  • translation (Tensor, optional) – Translation vector.

  • rotation (Tensor, optional) – Rotation, given either as euler angles of shape (3,) or as a scalar-first (w, x, y, z) quaternion.

  • scale (Tensor, optional) – Scaling factors.

  • shear (Tensor, optional) – Shearing factors.

  • degrees (bool, optional) – Whether the rotation angles are in degrees.

  • device (device, optional) – Device of the generated matrix.

  • dtype (dtype, optional) – Data type of the generated matrix. Defaults to float32. Use float64 to retain full-precision gradients.

Returns:

AffineMatrix – Composed affine matrix.

voxel.affine.random_affine(max_translation=0, max_rotation=0, max_scaling=0, device=None) → AffineMatrix#

Generate a random affine transformation matrix.

Parameters:
  • max_translation (float, optional) – Maximum translation in each direction.

  • max_rotation (float, optional) – Maximum rotation in each direction.

  • max_scaling (float, optional) – Maximum fractional scaling in each direction.

  • device (device, optional) – Device of the generated affine matrix.

Returns:

AffineMatrix – Random affine matrix.

voxel.affine.least_squares_alignment(source, target, weights=None, regularization=1e-6) → AffineMatrix#

Compute an affine least squares alignment between two 3D point sets.

Parameters:
  • source (Tensor or Mesh) – Source point set.

  • target (Tensor or Mesh) – Target point set.

  • weights (Tensor, optional) – Weights for each point in the source set.

  • regularization (float, optional) – Regularization scale. Default is 1e-6.

Returns:

AffineMatrix – Affine alignment matrix.