File size: 6,672 Bytes
1ea89dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import torch
from torch.nn import functional as F


def quaternion_to_R(quaternions):
    """
    Convert rotations given as quaternions to rotation matrices.

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

    Returns:
        Rotation matrices as tensor of shape (..., 3, 3).
    """
    r, i, j, k = torch.unbind(quaternions, -1)
    two_s = 2.0 / (quaternions * quaternions).sum(-1)

    o = torch.stack(
        (
            1 - two_s * (j * j + k * k),
            two_s * (i * j - k * r),
            two_s * (i * k + j * r),
            two_s * (i * j + k * r),
            1 - two_s * (i * i + k * k),
            two_s * (j * k - i * r),
            two_s * (i * k - j * r),
            two_s * (j * k + i * r),
            1 - two_s * (i * i + j * j),
        ),
        -1,
    )
    return o.reshape(quaternions.shape[:-1] + (3, 3))


def standardize_quaternion(quaternions: torch.Tensor) -> torch.Tensor:
    """
    Convert a unit quaternion to a standard form: one in which the real
    part is non negative.

    Args:
        quaternions: Quaternions with real part first,
            as tensor of shape (..., 4).

    Returns:
        Standardized quaternions as tensor of shape (..., 4).
    """
    return torch.where(quaternions[..., 0:1] < 0, -quaternions, quaternions)


def _sqrt_positive_part(x: torch.Tensor) -> torch.Tensor:
    """
    Returns torch.sqrt(torch.max(0, x))
    but with a zero subgradient where x is 0.
    """
    ret = torch.zeros_like(x)
    positive_mask = x > 0
    ret[positive_mask] = torch.sqrt(x[positive_mask])
    return ret


def R_to_quaternion(matrix: torch.Tensor) -> torch.Tensor:
    """
    Convert rotations given as rotation matrices to quaternions.

    Args:
        matrix: Rotation matrices as tensor of shape (..., 3, 3).

    Returns:
        quaternions with real part first, as tensor of shape (..., 4).
    """
    if matrix.size(-1) != 3 or matrix.size(-2) != 3:
        raise ValueError(f"Invalid rotation matrix shape {matrix.shape}.")

    batch_dim = matrix.shape[:-2]
    m00, m01, m02, m10, m11, m12, m20, m21, m22 = torch.unbind(
        matrix.reshape(batch_dim + (9,)), dim=-1
    )

    q_abs = _sqrt_positive_part(
        torch.stack(
            [
                1.0 + m00 + m11 + m22,
                1.0 + m00 - m11 - m22,
                1.0 - m00 + m11 - m22,
                1.0 - m00 - m11 + m22,
            ],
            dim=-1,
        )
    )
    # we produce the desired quaternion multiplied by each of r, i, j, k
    quat_by_rijk = torch.stack(
        [
            torch.stack([q_abs[..., 0] ** 2, m21 - m12, m02 - m20, m10 - m01], dim=-1),
            torch.stack([m21 - m12, q_abs[..., 1] ** 2, m10 + m01, m02 + m20], dim=-1),
            torch.stack([m02 - m20, m10 + m01, q_abs[..., 2] ** 2, m12 + m21], dim=-1),
            torch.stack([m10 - m01, m20 + m02, m21 + m12, q_abs[..., 3] ** 2], dim=-1),
        ],
        dim=-2,
    )

    # We floor here at 0.1 but the exact level is not important; if q_abs is small,
    # the candidate won't be picked.
    flr = torch.tensor(0.1).to(dtype=q_abs.dtype, device=q_abs.device)
    quat_candidates = quat_by_rijk / (2.0 * q_abs[..., None].max(flr))

    # if not for numerical problems, quat_candidates[i] should be same (up to a sign),
    # forall i; we pick the best-conditioned one (with the largest denominator)
    out = quat_candidates[
        F.one_hot(q_abs.argmax(dim=-1), num_classes=4) > 0.5, :
    ].reshape(batch_dim + (4,))
    out = standardize_quaternion(out)
    return out


def Rt_to_pose(R, t):
    assert R.shape[-2:] == (3, 3), "The last two dimensions of R must be 3x3"
    assert t.shape[-2:] == (3, 1), "The last dimension of t must be 3"

    # Create the pose matrix
    pose = torch.cat([R, t], dim=-1)
    pose = F.pad(pose, (0, 0, 0, 1), value=0)
    pose[..., 3, 3] = 1

    return pose


def pose_to_Rt(pose):
    assert pose.shape[-2:] == (4, 4), "The last two dimensions of pose must be 4x4"

    # Extract the rotation matrix and translation vector
    R = pose[..., :3, :3]
    t = pose[..., :3, 3:]

    return R, t


def relative_pose(pose1, pose2):
    # Compute world_to_cam for pose1
    pose1_inv = invert_pose(pose1)

    # Relative pose as cam_to_world_2 -> world_to_cam_1 => cam2_to_cam1
    relative_pose = pose1_inv @ pose2

    return relative_pose


@torch.autocast(device_type="cuda", dtype=torch.float32)
def invert_pose(pose):
    R, t = pose_to_Rt(pose)
    R_inv = R.transpose(-2, -1)
    t_inv = -torch.matmul(R_inv, t)
    pose_inv = Rt_to_pose(R_inv, t_inv)
    return pose_inv


def apply_pose_transformation(point_cloud, pose):
    reshape = point_cloud.ndim > 3
    shapes = point_cloud.shape
    # Extract rotation and translation from pose
    R, t = pose_to_Rt(pose)

    # Apply the pose transformation
    if reshape:
        point_cloud = point_cloud.reshape(shapes[0], -1, shapes[-1])
    transformed_points = torch.matmul(point_cloud, R.transpose(-2, -1)) + t.transpose(
        -2, -1
    )
    if reshape:
        transformed_points = transformed_points.reshape(shapes)
    return transformed_points


def euler2mat(roll, pitch, yaw) -> torch.Tensor:
    """
    Convert Euler angles (roll, pitch, yaw) to a 3x3 rotation matrix.

    Args:
        euler_angles (torch.Tensor): Tensor of shape (N, 3) representing roll, pitch, yaw in radians.
            - roll: rotation around z-axis
            - pitch: rotation around x-axis
            - yaw: rotation around y-axis
    Returns:
        torch.Tensor: Tensor of shape (N, 3, 3) representing the rotation matrices.
    """

    cos_r, sin_r = torch.cos(roll), torch.sin(roll)  # Roll
    cos_p, sin_p = torch.cos(pitch), torch.sin(pitch)  # Pitch
    cos_y, sin_y = torch.cos(yaw), torch.sin(yaw)  # Yaw

    # Rotation matrices
    R_z = torch.zeros((roll.shape[0], 3, 3), device=roll.device)
    R_y = torch.zeros_like(R_z)
    R_x = torch.zeros_like(R_z)

    # Z-axis (roll)
    R_z[:, 0, 0], R_z[:, 0, 1], R_z[:, 1, 0], R_z[:, 1, 1], R_z[:, 2, 2] = (
        cos_y,
        -sin_y,
        sin_y,
        cos_y,
        1.0,
    )

    # Y-axis (yaw)
    R_y[:, 0, 0], R_y[:, 0, 2], R_y[:, 2, 0], R_y[:, 2, 2], R_y[:, 1, 1] = (
        cos_p,
        sin_p,
        -sin_p,
        cos_p,
        1.0,
    )

    # X-axis (pitch)
    R_x[:, 1, 1], R_x[:, 1, 2], R_x[:, 2, 1], R_x[:, 2, 2], R_x[:, 0, 0] = (
        cos_r,
        -sin_r,
        sin_r,
        cos_r,
        1.0,
    )

    # Combine rotations: R = R_z * R_y * R_x
    rotation_matrix = torch.matmul(torch.matmul(R_z, R_y), R_x)
    return rotation_matrix