|
| 1 | +# AUTOGENERATED! DO NOT EDIT! File to edit: ../notebooks/api/06_pypose.ipynb. |
| 2 | + |
| 3 | +# %% auto 0 |
| 4 | +__all__ = ['convert'] |
| 5 | + |
| 6 | +# %% ../notebooks/api/06_pypose.ipynb 3 |
| 7 | +import torch |
| 8 | +import pypose as pp |
| 9 | + |
| 10 | +# %% ../notebooks/api/06_pypose.ipynb 4 |
| 11 | +def convert(*args, parameterization, convention=None, **kwargs) -> pp.SE3: |
| 12 | + if parameterization == "euler_angles" and convention is None: |
| 13 | + raise ValueError( |
| 14 | + "convention for Euler angles must be specified as a 3 letter combination of [X, Y, Z]" |
| 15 | + ) |
| 16 | + |
| 17 | + if parameterization == "axis_angle": |
| 18 | + rotation, translation = args |
| 19 | + quaternion = pp.so3(rotation).Exp().tensor() |
| 20 | + return convert(quaternion, translation, parameterization="quaternion") |
| 21 | + elif parameterization == "euler_angles": |
| 22 | + rotation, translation = args |
| 23 | + rotmat = euler_angles_to_matrix(rotation, convention) |
| 24 | + matrix = torch.concat([rotmat, translation.unsqueeze(-1)], axis=-1) |
| 25 | + return convert(matrix, parameterization="matrix", check=False) |
| 26 | + elif parameterization == "matrix": |
| 27 | + return pp.from_matrix(*args, ltype=pp.SE3_type, **kwargs) |
| 28 | + elif parameterization == "quaternion": |
| 29 | + rotation, translation = args |
| 30 | + return pp.SE3(torch.concat([translation, rotation], axis=-1)) |
| 31 | + elif parameterization == "quaternion_adjugate": |
| 32 | + rotation, translation = args |
| 33 | + quaternion = quaternion_adjugate_to_quaternion(rotation) |
| 34 | + return convert(quaternion, translation, parameterization="quaternion") |
| 35 | + elif parameterization == "rotation_6d": |
| 36 | + rotation, translation = args |
| 37 | + rotmat = rotation_6d_to_matrix(rotation) |
| 38 | + matrix = torch.concat([rotmat, translation.unsqueeze(-1)], axis=-1) |
| 39 | + return convert(matrix, parameterization="matrix", check=False) |
| 40 | + elif parameterization in ["rotation_10d"]: |
| 41 | + rotation, translation = args |
| 42 | + quaternion = rotation_10d_to_quaternion(rotation) |
| 43 | + return convert(quaternion, translation, parameterization="quaternion") |
| 44 | + elif parameterization == "se3": |
| 45 | + rotation, translation = args |
| 46 | + return pp.se3(torch.concat([translation, rotation], axis=-1)).Exp() |
| 47 | + elif parameterization == "SE3": |
| 48 | + return args[0] |
| 49 | + else: |
| 50 | + raise ValueError |
| 51 | + |
| 52 | +# %% ../notebooks/api/06_pypose.ipynb 5 |
| 53 | +def rotation_6d_to_matrix(d6: torch.Tensor) -> torch.Tensor: |
| 54 | + """Source: http://arxiv.org/abs/1812.07035""" |
| 55 | + a1, a2 = d6[..., :3], d6[..., 3:] |
| 56 | + b1 = F.normalize(a1, dim=-1) |
| 57 | + b2 = a2 - (b1 * a2).sum(-1, keepdim=True) * b1 |
| 58 | + b2 = F.normalize(b2, dim=-1) |
| 59 | + b3 = torch.cross(b1, b2, dim=-1) |
| 60 | + return torch.stack((b1, b2, b3), dim=-2) |
| 61 | + |
| 62 | + |
| 63 | +def rotation_10d_to_quaternion(rotation: torch.Tensor) -> torch.Tensor: |
| 64 | + """ |
| 65 | + Convert a 10-vector into a symmetric matrix, whose eigenvector corresponding |
| 66 | + to the eigenvalue of minimum modulus is the resulting quaternion. |
| 67 | +
|
| 68 | + Source: https://arxiv.org/abs/2006.01031 |
| 69 | + """ |
| 70 | + A = _10vec_to_4x4symmetric(rotation) # A is a symmetric data matrix |
| 71 | + return torch.linalg.eigh(A).eigenvectors[..., 0] |
| 72 | + |
| 73 | + |
| 74 | +def quaternion_adjugate_to_quaternion(rotation: torch.Tensor) -> torch.Tensor: |
| 75 | + """ |
| 76 | + Convert a 10-vector in the quaternion adjugate, a symmetric matrix whose |
| 77 | + eigenvector corresponding to the eigenvalue of maximum modulus is the |
| 78 | + (unnormalized) quaternion. Uses a fast method to solve for the eigenvector |
| 79 | + without explicity computing the eigendecomposition. |
| 80 | +
|
| 81 | + Source: https://arxiv.org/abs/2205.09116 |
| 82 | + """ |
| 83 | + A = _10vec_to_4x4symmetric(rotation) # A is the quaternion adjugate |
| 84 | + norms = A.norm(dim=1).amax(dim=1, keepdim=True) |
| 85 | + max_eigenvectors = torch.argmax(A.norm(dim=1), dim=1) |
| 86 | + return A[range(len(A)), max_eigenvectors] / norms |
| 87 | + |
| 88 | + |
| 89 | +def _10vec_to_4x4symmetric(vec): |
| 90 | + """Convert a 10-vector to a symmetric 4x4 matrix.""" |
| 91 | + b = len(vec) |
| 92 | + A = torch.zeros(b, 4, 4, device=vec.device) |
| 93 | + idx, jdx = torch.triu_indices(4, 4) |
| 94 | + A[..., idx, jdx] = vec |
| 95 | + A[..., jdx, idx] = vec |
| 96 | + return A |
| 97 | + |
| 98 | +# %% ../notebooks/api/06_pypose.ipynb 6 |
| 99 | +def euler_angles_to_matrix(euler_angles: torch.Tensor, convention: str) -> torch.Tensor: |
| 100 | + if euler_angles.dim() == 0 or euler_angles.shape[-1] != 3: |
| 101 | + raise ValueError("Invalid input euler angles.") |
| 102 | + if len(convention) != 3: |
| 103 | + raise ValueError("Convention must have 3 letters.") |
| 104 | + if convention[1] in (convention[0], convention[2]): |
| 105 | + raise ValueError(f"Invalid convention {convention}.") |
| 106 | + for letter in convention: |
| 107 | + if letter not in ("X", "Y", "Z"): |
| 108 | + raise ValueError(f"Invalid letter {letter} in convention string.") |
| 109 | + matrices = [ |
| 110 | + _axis_angle_rotation(c, e) |
| 111 | + for c, e in zip(convention, torch.unbind(euler_angles, -1)) |
| 112 | + ] |
| 113 | + return torch.matmul(torch.matmul(matrices[0], matrices[1]), matrices[2]) |
| 114 | + |
| 115 | + |
| 116 | +def _axis_angle_rotation(axis: str, angle: torch.Tensor) -> torch.Tensor: |
| 117 | + cos = torch.cos(angle) |
| 118 | + sin = torch.sin(angle) |
| 119 | + one = torch.ones_like(angle) |
| 120 | + zero = torch.zeros_like(angle) |
| 121 | + |
| 122 | + if axis == "X": |
| 123 | + R_flat = (one, zero, zero, zero, cos, -sin, zero, sin, cos) |
| 124 | + elif axis == "Y": |
| 125 | + R_flat = (cos, zero, sin, zero, one, zero, -sin, zero, cos) |
| 126 | + elif axis == "Z": |
| 127 | + R_flat = (cos, -sin, zero, sin, cos, zero, zero, zero, one) |
| 128 | + else: |
| 129 | + raise ValueError("letter must be either X, Y or Z.") |
| 130 | + |
| 131 | + return torch.stack(R_flat, -1).reshape(angle.shape + (3, 3)) |
0 commit comments