File size: 9,854 Bytes
04afe87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
"""Rotation conversion utilities for MEI representation.

All functions support arbitrary batch dimensions (..., ).
Coordinate convention: Y-up, right-handed (X-right, Y-up, Z-forward).

Core rotation functions ported from HY-Motion (hymotion/utils/geometry.py),
torch -> numpy.
"""

import numpy as np


# ============================================================
# Helpers (from HY-Motion)
# ============================================================

def _sqrt_positive_part(x: np.ndarray) -> np.ndarray:
    """Returns np.sqrt(np.maximum(0, x))."""
    ret = np.zeros_like(x)
    positive_mask = x > 0
    ret[positive_mask] = np.sqrt(x[positive_mask])
    return ret


def standardize_quaternion(quaternions: np.ndarray) -> np.ndarray:
    """
    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 array of shape (..., 4).

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


# ============================================================
# Axis-angle <-> Quaternion <-> Rotation matrix
# ============================================================

def axis_angle_to_quaternion(axis_angle: np.ndarray) -> np.ndarray:
    """Convert rotations given as axis/angle to quaternions.

    Args:
        axis_angle: Rotations given as a vector in axis angle form,
            as an array 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 array of shape (..., 4).
    """
    angles = np.linalg.norm(axis_angle, axis=-1, keepdims=True)
    half_angles = angles * 0.5
    # sin(angle/2) / angle, exact; limit -> 0.5 as angle -> 0
    nonzero = angles != 0
    safe_angles = np.where(nonzero, angles, np.ones_like(angles))
    sin_half_angles_over_angles = np.where(
        nonzero, np.sin(half_angles) / safe_angles, 0.5
    )
    quaternions = np.concatenate(
        [np.cos(half_angles), axis_angle * sin_half_angles_over_angles], axis=-1
    )
    return quaternions


def quaternion_to_matrix(quaternions: np.ndarray) -> np.ndarray:
    """Convert rotations given as quaternions to rotation matrices.

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

    Returns:
        Rotation matrices as array of shape (..., 3, 3).
    """
    r, i, j, k = (
        quaternions[..., 0],
        quaternions[..., 1],
        quaternions[..., 2],
        quaternions[..., 3],
    )
    two_s = 2.0 / (quaternions * quaternions).sum(-1)

    o = np.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),
        ),
        axis=-1,
    )
    return o.reshape(quaternions.shape[:-1] + (3, 3))


def axis_angle_to_matrix(axis_angle: np.ndarray) -> np.ndarray:
    """Convert rotations given as axis/angle to rotation matrices.

    Args:
        axis_angle: Rotations given as a vector in axis angle form,
            as an array of shape (..., 3), where the magnitude is
            the angle turned anticlockwise in radians around the
            vector's direction.

    Returns:
        Rotation matrices as array of shape (..., 3, 3).
    """
    return quaternion_to_matrix(axis_angle_to_quaternion(axis_angle))


def matrix_to_quaternion(matrix: np.ndarray) -> np.ndarray:
    """Convert rotations given as rotation matrices to quaternions.

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

    Returns:
        quaternions with real part first, as array of shape (..., 4).
    """
    if matrix.shape[-1] != 3 or matrix.shape[-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 = np.split(
        matrix.reshape(batch_dim + (9,)), 9, axis=-1
    )
    m00 = m00[..., 0]
    m01 = m01[..., 0]
    m02 = m02[..., 0]
    m10 = m10[..., 0]
    m11 = m11[..., 0]
    m12 = m12[..., 0]
    m20 = m20[..., 0]
    m21 = m21[..., 0]
    m22 = m22[..., 0]

    q_abs = _sqrt_positive_part(
        np.stack(
            [
                1.0 + m00 + m11 + m22,
                1.0 + m00 - m11 - m22,
                1.0 - m00 + m11 - m22,
                1.0 - m00 - m11 + m22,
            ],
            axis=-1,
        )
    )

    # we produce the desired quaternion multiplied by each of r, i, j, k
    quat_by_rijk = np.stack(
        [
            np.stack(
                [q_abs[..., 0] ** 2, m21 - m12, m02 - m20, m10 - m01], axis=-1
            ),
            np.stack(
                [m21 - m12, q_abs[..., 1] ** 2, m10 + m01, m02 + m20], axis=-1
            ),
            np.stack(
                [m02 - m20, m10 + m01, q_abs[..., 2] ** 2, m12 + m21], axis=-1
            ),
            np.stack(
                [m10 - m01, m20 + m02, m21 + m12, q_abs[..., 3] ** 2], axis=-1
            ),
        ],
        axis=-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 = 0.1
    quat_candidates = quat_by_rijk / (2.0 * np.maximum(q_abs[..., None], 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)
    best = q_abs.argmax(axis=-1)  # (*batch_dim,)
    # Advanced indexing to select the best candidate per element
    flat_candidates = quat_candidates.reshape(-1, 4, 4)
    flat_best = best.reshape(-1)
    out = flat_candidates[np.arange(flat_candidates.shape[0]), flat_best, :]
    out = out.reshape(batch_dim + (4,))
    return standardize_quaternion(out)


def quaternion_to_axis_angle(quaternions: np.ndarray) -> np.ndarray:
    """Convert rotations given as quaternions to axis/angle.

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

    Returns:
        Rotations given as a vector in axis angle form, as an array
            of shape (..., 3), where the magnitude is the angle
            turned anticlockwise in radians around the vector's
            direction.
    """
    norms = np.linalg.norm(quaternions[..., 1:], axis=-1, keepdims=True)
    half_angles = np.arctan2(norms, quaternions[..., :1])
    angles = 2 * half_angles
    # sin(half_angle) / angle, exact; limit -> 0.5 as angle -> 0
    nonzero = angles != 0
    safe_angles = np.where(nonzero, angles, np.ones_like(angles))
    sin_half_angles_over_angles = np.where(
        nonzero, np.sin(half_angles) / safe_angles, 0.5
    )
    return quaternions[..., 1:] / sin_half_angles_over_angles


def matrix_to_axis_angle(matrix: np.ndarray) -> np.ndarray:
    """Convert rotations given as rotation matrices to axis/angle.

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

    Returns:
        Rotations given as a vector in axis angle form, as an array
            of shape (..., 3), where the magnitude is the angle
            turned anticlockwise in radians around the vector's
            direction.
    """
    return quaternion_to_axis_angle(matrix_to_quaternion(matrix))


# ============================================================
# 6D continuous rotation representation (Zhou et al., CVPR 2019)
# ============================================================

def rotation_6d_to_matrix(rot6d: np.ndarray) -> np.ndarray:
    """Convert 6D rotation representation to 3x3 rotation matrix.

    Based on Zhou et al., "On the Continuity of Rotation Representations
    in Neural Networks", CVPR 2019.

    Args:
        rot6d: array of shape (*, 6) of 6d rotation representations.

    Returns:
        rotation matrices of size (*, 3, 3).
    """
    x = rot6d.reshape(*rot6d.shape[:-1], 3, 2)
    a1 = x[..., 0]
    a2 = x[..., 1]
    b1 = a1 / np.maximum(np.linalg.norm(a1, axis=-1, keepdims=True), 1e-12)
    b2 = a2 - np.sum(b1 * a2, axis=-1, keepdims=True) * b1
    b2 = b2 / np.maximum(np.linalg.norm(b2, axis=-1, keepdims=True), 1e-12)
    b3 = np.cross(b1, b2, axis=-1)
    return np.stack((b1, b2, b3), axis=-1)


def matrix_to_rotation_6d(matrix: np.ndarray) -> np.ndarray:
    """Convert 3x3 rotation matrix to 6D rotation representation.

    Args:
        matrix: rotation matrices of shape (*, 3, 3).

    Returns:
        6D rotation representation of shape (*, 6).
    """
    v1 = matrix[..., 0:1]
    v2 = matrix[..., 1:2]
    rot6d = np.concatenate([v1, v2], axis=-1).reshape(*matrix.shape[:-2], 6)
    return rot6d


# ============================================================
# Yaw (Y-axis) rotation helpers (MEI-specific)
# ============================================================

def yaw_rotation_matrix(angle: np.ndarray) -> np.ndarray:
    """Create rotation matrices for yaw (Y-axis rotation).

    R_y(theta) maps local Z-forward to the heading direction in world XZ plane.

    Args:
        angle: (...) yaw angles in radians.

    Returns:
        R: (..., 3, 3) rotation matrices.
    """
    c = np.cos(angle)
    s = np.sin(angle)
    z = np.zeros_like(angle)
    o = np.ones_like(angle)

    return np.stack([
        c,  z, s,
        z,  o, z,
        -s, z, c,
    ], axis=-1).reshape(*angle.shape, 3, 3)


def wrap_angle(angle: np.ndarray) -> np.ndarray:
    """Wrap angle to [-pi, pi]."""
    return (angle + np.pi) % (2 * np.pi) - np.pi