Render Anmations in Python
This part describes the methods/classes we apply to render animations using Python’s PyRender interface.
- render.python.utils._angle_from_tan(axis: str, other_axis: str, data: torch.Tensor, horizontal: bool, tait_bryan: bool) torch.Tensor[source]
Extract the first or third Euler angle from the two members of the matrix which are positive constant times its sine and cosine.
- Parameters
axis (str) – Axis label “X” or “Y or “Z” for the angle we are finding.
other_axis (str) – Axis label “X” or “Y or “Z” for the middle axis in the convention.
data (torch.Tensor) – Rotation matrices as tensor of shape (…, 3, 3).
horizontal (bool) – Whether we are looking for the angle for the third axis, which means the relevant entries are in the same row of the rotation matrix. If not, they are in the same column.
tait_bryan (bool) – Whether the first and third axes in the convention differ.
- Returns
Euler Angles in radians for each matrix in data as a tensor of shape (…).
- Return type
torch.Tensor
- render.python.utils._axis_angle_rotation(axis: str, angle: torch.Tensor) torch.Tensor[source]
Return the rotation matrices for one of the rotations about an axis of which Euler angles describe, for each value of the angle given.
- Parameters
axis (str) – Axis label “X” or “Y or “Z”.
angle (torch.Tensor) – any shape tensor of Euler angles in radians
- Returns
Rotation matrices as tensor of shape (…, 3, 3).
- Return type
torch.Tensor
- render.python.utils._copysign(a: torch.Tensor, b: torch.Tensor) torch.Tensor[source]
Return a tensor where each element has the absolute value taken from the, corresponding element of a, with sign taken from the corresponding element of b. This is like the standard copysign floating-point operation, but is not careful about negative 0 and NaN.
- Parameters
a (torch.Tensor) – source tensor.
b (torch.Tensor) – tensor whose signs will be used, of the same shape as a.
- Returns
Tensor of the same shape as a with the signs of b.
- Return type
torch.Tensor
- render.python.utils._index_from_letter(letter: str) int[source]
return the index of axis from string
- render.python.utils._sqrt_positive_part(x: torch.Tensor) torch.Tensor[source]
Returns torch.sqrt(torch.max(0, x)) but with a zero subgradient where x is 0.
- Parameters
x (torch.Tensor) – input tensor
- render.python.utils.axis_angle_to_matrix(axis_angle: torch.Tensor) torch.Tensor[source]
Convert rotations given as axis/angle to rotation matrices.
- Parameters
axis_angle (torch.Tensor) – Rotations given as a vector in axis angle form, as a tensor of shape (…, 3), where the magnitude is the angle turned anticlockwise in radians around the vector’s direction.
- Returns
Rotation matrices as tensor of shape (…, 3, 3).
- Return type
torch.Tensor
- render.python.utils.axis_angle_to_quaternion(axis_angle: torch.Tensor) torch.Tensor[source]
Convert rotations given as axis/angle to quaternions.
- Parameters
axis_angle (torch.Tensor) – Rotations given as a vector in axis angle form, as a tensor of shape (…, 3), where the magnitude is the angle turned anticlockwise in radians around the vector’s direction.
- Returns
quaternions with real part first, as tensor of shape (…, 4).
- Return type
torch.Tensor
- render.python.utils.euler_angles_to_matrix(euler_angles: torch.Tensor, convention: str) torch.Tensor[source]
Convert rotations given as Euler angles in radians to rotation matrices.
- Parameters
euler_angles (torch.Tensor) – Euler angles in radians as tensor of shape (…, 3).
convention (str) – Convention string of three uppercase letters from {“X”, “Y”, and “Z”}.
- Returns
Rotation matrices as tensor of shape (…, 3, 3).
- Return type
torch.Tensor
- render.python.utils.matrix_to_axis_angle(matrix: torch.Tensor) torch.Tensor[source]
Convert rotations given as rotation matrices to axis/angle.
- Parameters
matrix (torch.Tensor) – Rotation matrices as tensor of shape (…, 3, 3).
- Returns
Rotations given as a vector in axis angle form, as a tensor of shape (…, 3), where the magnitude is the angle turned anticlockwise in radians around the vector’s direction.
- Return type
torch.Tensor
- render.python.utils.matrix_to_euler_angles(matrix: torch.Tensor, convention: str) torch.Tensor[source]
Convert rotations given as rotation matrices to Euler angles in radians.
- Parameters
matrix (torch.Tensor) – Rotation matrices as tensor of shape (…, 3, 3).
convention (str) – Convention string of three uppercase letters.
- Returns
Euler angles in radians as tensor of shape (…, 3).
- Return type
torch.Tensor
- render.python.utils.matrix_to_quaternion(matrix: torch.Tensor) torch.Tensor[source]
Convert rotations given as rotation matrices to quaternions.
- Parameters
matrix (torch.Tensor) – Rotation matrices as tensor of shape (…, 3, 3).
- Returns
quaternions with real part first, as tensor of shape (…, 4).
- Return type
torch.Tensor
- render.python.utils.matrix_to_rotation_6d(matrix: torch.Tensor) torch.Tensor[source]
Converts rotation matrices to 6D rotation representation by Zhou et al. [1] by dropping the last row. Note that 6D representation is not unique.
- Parameters
matrix (torch.Tensor) – batch of rotation matrices of size (x, 3, 3)
- Returns
6D rotation representation, of size (x, 6)
- Return type
torch.Tensor
[1] Zhou, Y., Barnes, C., Lu, J., Yang, J., & Li, H. On the Continuity of Rotation Representations in Neural Networks. IEEE Conference on Computer Vision and Pattern Recognition, 2019. Retrieved from http://arxiv.org/abs/1812.07035
- render.python.utils.quaternion_apply(quaternion: torch.Tensor, point: torch.Tensor) torch.Tensor[source]
Apply the rotation given by a quaternion to a 3D point. Usual torch rules for broadcasting apply.
- Parameters
quaternion (torch.Tensor) – Tensor of quaternions, real part first, of shape (…, 4).
point (torch.Tensor) – Tensor of 3D points of shape (…, 3).
- Returns
Tensor of rotated points of shape (…, 3).
- Return type
torch.Tensor
- render.python.utils.quaternion_invert(quaternion: torch.Tensor) torch.Tensor[source]
Given a quaternion representing rotation, get the quaternion representing its inverse.
- Param
quaternion: Quaternions as tensor of shape (…, 4), with real part first, which must be versors (unit quaternions).
- Returns
The inverse, a tensor of quaternions of shape (…, 4).
- Return type
torch.Tensor
- render.python.utils.quaternion_multiply(a: torch.Tensor, b: torch.Tensor) torch.Tensor[source]
Multiply two quaternions representing rotations, returning the quaternion representing their composition, i.e. the versor with nonnegative real part. Usual torch rules for broadcasting apply.
- Parameters
a (torch.Tensor) – Quaternions as tensor of shape (…, 4), real part first.
b (torch.Tensor) – Quaternions as tensor of shape (…, 4), real part first.
- Returns
The product of a and b, a tensor of quaternions shape (…, 4).
- Return type
torch.Tensor
- render.python.utils.quaternion_raw_multiply(a: torch.Tensor, b: torch.Tensor) torch.Tensor[source]
Multiply two quaternions. Usual torch rules for broadcasting apply.
- Parameters
a (torch.Tensor) – Quaternions as tensor of shape (…, 4), real part first.
b (torch.Tensor) – Quaternions as tensor of shape (…, 4), real part first.
- Returns
The product of a and b, a tensor of quaternions shape (…, 4).
- Return type
torch.Tensor
- render.python.utils.quaternion_to_axis_angle(quaternions: torch.Tensor) torch.Tensor[source]
Convert rotations given as quaternions to axis/angle.
- Parameters
quaternions (torch.Tensor) – quaternions with real part first, as tensor of shape (…, 4).
- Returns
Rotations given as a vector in axis angle form, as a tensor of shape (…, 3), where the magnitude is the angle turned anticlockwise in radians around the vector’s direction.
- Return type
torch.Tensor
- render.python.utils.quaternion_to_matrix(quaternions: torch.Tensor) torch.Tensor[source]
Convert rotations given as quaternions to rotation matrices.
- Parameters
quaternions (torch.Tensor) – quaternions as tensor of shape (…, 4), with real part first
- Returns
Rotation matrices as tensor of shape (…, 3, 3).
- Return type
torch.Tensor
- render.python.utils.random_quaternions(n: int, dtype: Optional[torch.dtype] = None, device=None, requires_grad=False) torch.Tensor[source]
Generate random quaternions representing rotations, i.e. versors with nonnegative real part.
- Parameters
n (int) – Number of quaternions in a batch to return.
dtype (Optional[torch.dtype]) – Type to return.
device (str) – Desired device of returned tensor. Default: uses the current device for the default tensor type.
requires_grad (bool) – Whether the resulting tensor should have the gradient flag set.
- Returns
Quaternions as tensor of shape (N, 4).
- Return type
torch.Tensor
- render.python.utils.random_rotation(dtype: Optional[torch.dtype] = None, device=None, requires_grad=False) torch.Tensor[source]
Generate a single random 3x3 rotation matrix.
- Parameters
- Returns
Rotation matrix as tensor of shape (3, 3).
- Return type
torch.Tensor
- render.python.utils.random_rotations(n: int, dtype: Optional[torch.dtype] = None, device=None, requires_grad=False) torch.Tensor[source]
Generate random rotations as 3x3 rotation matrices.
- Parameters
n (int) – Number of rotation matrices in a batch to return.
dtype (Optional[torch.dtype]) – Type to return.
device (str) – Device of returned tensor. Default: if None, uses the current device for the default tensor type.
requires_grad (bool) – Whether the resulting tensor should have the gradient flag set.
- Returns
Rotation matrices as tensor of shape (n, 3, 3).
- Return type
torch.Tensor
- render.python.utils.rotation_6d_to_matrix(d6: torch.Tensor) torch.Tensor[source]
Converts 6D rotation representation by Zhou et al. [1] to rotation matrix using Gram–Schmidt orthogonalisation per Section B of [1].
- Parameters
d6 (torch.Tensor) – 6D rotation representation, of size (x, 6)
- Returns
batch of rotation matrices of size (x, 3, 3)
- Return type
torch.Tensor
[1] Zhou, Y., Barnes, C., Lu, J., Yang, J., & Li, H. On the Continuity of Rotation Representations in Neural Networks. IEEE Conference on Computer Vision and Pattern Recognition, 2019. Retrieved from http://arxiv.org/abs/1812.07035
- render.python.utils.standardize_quaternion(quaternions: torch.Tensor) torch.Tensor[source]
Convert a unit quaternion to a standard form: one in which the real part is non negative.
- Parameters
quaternions (torch.Tensor) – Quaternions with real part first, as tensor of shape (…, 4).
- Returns
Standardized quaternions as tensor of shape (…, 4).
- Return type
torch.Tensor