# Conversions¶

rad2deg(tensor)[source]

Function that converts angles from radians to degrees.

See RadToDeg for details.

Parameters: tensor (Tensor) – Tensor of arbitrary shape. Tensor with same shape as input. Tensor

Example

>>> input = tgm.pi * torch.rand(1, 3, 3)
>>> output = tgm.rad2deg(input)

deg2rad(tensor)[source]

Function that converts angles from degrees to radians.

See DegToRad for details.

Parameters: tensor (Tensor) – Tensor of arbitrary shape. Tensor with same shape as input. Tensor

Examples:

>>> input = 360. * torch.rand(1, 3, 3)
>>> output = tgm.deg2rad(input)

convert_points_from_homogeneous(points)[source]

Function that converts points from homogeneous to Euclidean space.

See ConvertPointsFromHomogeneous for details.

Examples:

>>> input = torch.rand(2, 4, 3)  # BxNx3
>>> output = tgm.convert_points_from_homogeneous(input)  # BxNx2

convert_points_to_homogeneous(points)[source]

Function that converts points from Euclidean to homogeneous space.

See ConvertPointsToHomogeneous for details.

Examples:

>>> input = torch.rand(2, 4, 3)  # BxNx3
>>> output = tgm.convert_points_to_homogeneous(input)  # BxNx4

angle_axis_to_rotation_matrix(angle_axis)[source]

Convert 3d vector of axis-angle rotation to 4x4 rotation matrix

Parameters: angle_axis (Tensor) – tensor of 3d vector of axis-angle rotations. tensor of 4x4 rotation matrices. Tensor
Shape:
• Input: $$(N, 3)$$
• Output: $$(N, 4, 4)$$

Example

>>> input = torch.rand(1, 3)  # Nx3
>>> output = tgm.angle_axis_to_rotation_matrix(input)  # Nx4x4

rotation_matrix_to_angle_axis(rotation_matrix)[source]

Convert 3x4 rotation matrix to Rodrigues vector

Parameters: rotation_matrix (Tensor) – rotation matrix. Rodrigues vector transformation. Tensor
Shape:
• Input: $$(N, 3, 4)$$
• Output: $$(N, 3)$$

Example

>>> input = torch.rand(2, 3, 4)  # Nx4x4
>>> output = tgm.rotation_matrix_to_angle_axis(input)  # Nx3

rotation_matrix_to_quaternion(rotation_matrix, eps=1e-06)[source]

Convert 3x4 rotation matrix to 4d quaternion vector

This algorithm is based on algorithm described in https://github.com/KieranWynn/pyquaternion/blob/master/pyquaternion/quaternion.py#L201

Parameters: rotation_matrix (Tensor) – the rotation matrix to convert. the rotation in quaternion Tensor
Shape:
• Input: $$(N, 3, 4)$$
• Output: $$(N, 4)$$

Example

>>> input = torch.rand(4, 3, 4)  # Nx3x4
>>> output = tgm.rotation_matrix_to_quaternion(input)  # Nx4

quaternion_to_angle_axis(quaternion: torch.Tensor) → torch.Tensor[source]

Convert quaternion vector to angle axis of rotation.

Adapted from ceres C++ library: ceres-solver/include/ceres/rotation.h

Parameters: quaternion (torch.Tensor) – tensor with quaternions. tensor with angle axis of rotation. torch.Tensor
Shape:
• Input: $$(*, 4)$$ where * means, any number of dimensions
• Output: $$(*, 3)$$

Example

>>> quaternion = torch.rand(2, 4)  # Nx4
>>> angle_axis = tgm.quaternion_to_angle_axis(quaternion)  # Nx3

angle_axis_to_quaternion(angle_axis: torch.Tensor) → torch.Tensor[source]

Convert an angle axis to a quaternion.

Adapted from ceres C++ library: ceres-solver/include/ceres/rotation.h

Parameters: angle_axis (torch.Tensor) – tensor with angle axis. tensor with quaternion. torch.Tensor
Shape:
• Input: $$(*, 3)$$ where * means, any number of dimensions
• Output: $$(*, 4)$$

Example

>>> angle_axis = torch.rand(2, 4)  # Nx4
>>> quaternion = tgm.angle_axis_to_quaternion(angle_axis)  # Nx3

rtvec_to_pose(rtvec)[source]

Convert axis-angle rotation and translation vector to 4x4 pose matrix

Parameters: rtvec (Tensor) – Rodrigues vector transformations transformation matrices Tensor
Shape:
• Input: $$(N, 6)$$
• Output: $$(N, 4, 4)$$

Example

>>> input = torch.rand(3, 6)  # Nx6
>>> output = tgm.rtvec_to_pose(input)  # Nx4x4

class RadToDeg[source]

Creates an object that converts angles from radians to degrees.

Parameters: tensor (Tensor) – Tensor of arbitrary shape. Tensor with same shape as input. Tensor

Examples:

>>> input = tgm.pi * torch.rand(1, 3, 3)
>>> output = tgm.RadToDeg()(input)

class DegToRad[source]

Function that converts angles from degrees to radians.

Parameters: tensor (Tensor) – Tensor of arbitrary shape. Tensor with same shape as input. Tensor

Examples:

>>> input = 360. * torch.rand(1, 3, 3)
>>> output = tgm.DegToRad()(input)

class ConvertPointsFromHomogeneous[source]

Creates a transformation that converts points from homogeneous to Euclidean space.

Parameters: points (Tensor) – tensor of N-dimensional points. tensor of N-1-dimensional points. Tensor
Shape:
• Input: $$(B, D, N)$$ or $$(D, N)$$
• Output: $$(B, D, N + 1)$$ or $$(D, N + 1)$$

Examples:

>>> input = torch.rand(2, 4, 3)  # BxNx3
>>> transform = tgm.ConvertPointsFromHomogeneous()
>>> output = transform(input)  # BxNx2

class ConvertPointsToHomogeneous[source]

Creates a transformation to convert points from Euclidean to homogeneous space.

Parameters: points (Tensor) – tensor of N-dimensional points. tensor of N+1-dimensional points. Tensor
Shape:
• Input: $$(B, D, N)$$ or $$(D, N)$$
• Output: $$(B, D, N + 1)$$ or $$(D, N + 1)$$

Examples:

>>> input = torch.rand(2, 4, 3)  # BxNx3
>>> transform = tgm.ConvertPointsToHomogeneous()
>>> output = transform(input)  # BxNx4