rlightning.humanoid.utils.common

rlightning.humanoid.utils.common.Device

The transformation matrices returned from the functions in this file assume the points on which the transformation will be applied are column vectors. i.e. the R matrix is structured as

R = [

[Rxx, Rxy, Rxz], [Ryx, Ryy, Ryz], [Rzx, Rzy, Rzz],

] # (3, 3)

This matrix can be applied to column vectors by post multiplication by the points e.g.

points = [[0], [1], [2]] # (3 x 1) xyz coordinates of a point transformed_points = R * points

To apply the same matrix to points which are row vectors, the R matrix can be transposed and pre multiplied by the points:

e.g.

points = [[0, 1, 2]] # (1 x 3) xyz coordinates of a point transformed_points = points * R.transpose(1, 0)

alias of str | torch.device

rlightning.humanoid.utils.common.axis_angle_to_matrix(axis_angle: MockModule('torch.Tensor'))[source]

Convert rotations given as axis/angle to rotation matrices.

Parameters:

axis_angle – 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).

rlightning.humanoid.utils.common.axis_angle_to_quaternion(axis_angle: MockModule('torch.Tensor'))[source]

Convert rotations given as axis/angle to quaternions.

Parameters:

axis_angle – 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).

rlightning.humanoid.utils.common.euler_angles_to_matrix(euler_angles: MockModule('torch.Tensor'), convention: str)[source]

Convert rotations given as Euler angles in radians to rotation matrices.

Parameters:
  • euler_angles – Euler angles in radians as tensor of shape (…, 3).

  • convention – Convention string of three uppercase letters from {“X”, “Y”, and “Z”}.

Returns:

Rotation matrices as tensor of shape (…, 3, 3).

rlightning.humanoid.utils.common.insert_tensors_at_dim(tensor, dim, indices, values)[source]

在tensor的指定维度上的若干个位置插入若干个张量

Parameters:
  • tensor – 原始tensor

  • dim – 指定的维度

  • indices – 要插入的位置索引列表/tensor(插入后的位置)

  • values – 要插入的张量,形状应该与tensor在除了指定维度外的其他维度匹配

Returns:

插入后的新tensor

rlightning.humanoid.utils.common.matrix_to_axis_angle(matrix: MockModule('torch.Tensor'))[source]

Convert rotations given as rotation matrices to axis/angle.

Parameters:

matrix – 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.

rlightning.humanoid.utils.common.matrix_to_euler_angles(matrix: MockModule('torch.Tensor'), convention: str)[source]

Convert rotations given as rotation matrices to Euler angles in radians.

Parameters:
  • matrix – Rotation matrices as tensor of shape (…, 3, 3).

  • convention – Convention string of three uppercase letters.

Returns:

Euler angles in radians as tensor of shape (…, 3).

rlightning.humanoid.utils.common.matrix_to_quaternion(matrix: MockModule('torch.Tensor'))[source]

w x y z Convert rotations given as rotation matrices to quaternions.

Parameters:

matrix – Rotation matrices as tensor of shape (…, 3, 3).

Returns:

quaternions with real part first, as tensor of shape (…, 4).

rlightning.humanoid.utils.common.matrix_to_rotation_6d(matrix: MockModule('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. :param matrix: batch of rotation matrices of size (*, 3, 3)

Returns:

6D rotation representation, of size (*, 6)

[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

rlightning.humanoid.utils.common.quaternion_apply(quaternion: MockModule('torch.Tensor'), point: MockModule('torch.Tensor'))[source]

Apply the rotation given by a quaternion to a 3D point. Usual torch rules for broadcasting apply.

Parameters:
  • quaternion – Tensor of quaternions, real part first, of shape (…, 4).

  • point – Tensor of 3D points of shape (…, 3).

Returns:

Tensor of rotated points of shape (…, 3).

rlightning.humanoid.utils.common.quaternion_invert(quaternion: MockModule('torch.Tensor'))[source]

Given a quaternion representing rotation, get the quaternion representing its inverse.

Parameters:

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).

rlightning.humanoid.utils.common.quaternion_multiply(a: MockModule('torch.Tensor'), b: MockModule('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 – Quaternions as tensor of shape (…, 4), real part first.

  • b – Quaternions as tensor of shape (…, 4), real part first.

Returns:

The product of a and b, a tensor of quaternions of shape (…, 4).

rlightning.humanoid.utils.common.quaternion_raw_multiply(a: MockModule('torch.Tensor'), b: MockModule('torch.Tensor'))[source]

Multiply two quaternions. Usual torch rules for broadcasting apply.

Parameters:
  • a – Quaternions as tensor of shape (…, 4), real part first.

  • b – Quaternions as tensor of shape (…, 4), real part first.

Returns:

The product of a and b, a tensor of quaternions shape (…, 4).

rlightning.humanoid.utils.common.quaternion_to_axis_angle(quaternions: MockModule('torch.Tensor'))[source]

Convert rotations given as quaternions to axis/angle.

Parameters:

quaternions – 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.

rlightning.humanoid.utils.common.quaternion_to_matrix(quaternions: MockModule('torch.Tensor'))[source]

Convert rotations given as quaternions to rotation matrices.

Parameters:

quaternions – quaternions with real part first, as tensor of shape (…, 4).

Returns:

Rotation matrices as tensor of shape (…, 3, 3).

rlightning.humanoid.utils.common.random_quaternions(n: int, dtype: MockModule('torch.dtype') | None = None, device: str | MockModule('torch.device') | None = None)[source]

Generate random quaternions representing rotations, i.e. versors with nonnegative real part.

Parameters:
  • n – Number of quaternions in a batch to return.

  • dtype – Type to return.

  • device – Desired device of returned tensor. Default: uses the current device for the default tensor type.

Returns:

Quaternions as tensor of shape (N, 4).

rlightning.humanoid.utils.common.random_rotation(dtype: MockModule('torch.dtype') | None = None, device: str | MockModule('torch.device') | None = None)[source]

Generate a single random 3x3 rotation matrix.

Parameters:
  • dtype – Type to return

  • device – Device of returned tensor. Default: if None, uses the current device for the default tensor type

Returns:

Rotation matrix as tensor of shape (3, 3).

rlightning.humanoid.utils.common.random_rotations(n: int, dtype: MockModule('torch.dtype') | None = None, device: str | MockModule('torch.device') | None = None)[source]

Generate random rotations as 3x3 rotation matrices.

Parameters:
  • n – Number of rotation matrices in a batch to return.

  • dtype – Type to return.

  • device – Device of returned tensor. Default: if None, uses the current device for the default tensor type.

Returns:

Rotation matrices as tensor of shape (n, 3, 3).

rlightning.humanoid.utils.common.rotation_6d_to_matrix(d6: MockModule('torch.Tensor'))[source]

Converts 6D rotation representation by Zhou et al. [1] to rotation matrix using Gram–Schmidt orthogonalization per Section B of [1]. :param d6: 6D rotation representation, of size (*, 6)

Returns:

batch of rotation matrices of size (*, 3, 3)

[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

rlightning.humanoid.utils.common.standardize_quaternion(quaternions: MockModule('torch.Tensor'))[source]

Convert a unit quaternion to a standard form: one in which the real part is non negative.

Parameters:

quaternions – Quaternions with real part first, as tensor of shape (…, 4).

Returns:

Standardized quaternions as tensor of shape (…, 4).

rlightning.humanoid.utils.common.wxyz_to_xyzw(quat)[source]
rlightning.humanoid.utils.common.xyzw_to_wxyz(quat)[source]