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).
(quaternions)
| 272 | |
| 273 | |
| 274 | def quaternion_to_matrix(quaternions): |
| 275 | """ |
| 276 | Convert rotations given as quaternions to rotation matrices. |
| 277 | Args: |
| 278 | quaternions: quaternions with real part first, |
| 279 | as tensor of shape (..., 4). |
| 280 | Returns: |
| 281 | Rotation matrices as tensor of shape (..., 3, 3). |
| 282 | """ |
| 283 | r, i, j, k = torch.unbind(quaternions, -1) |
| 284 | two_s = 2.0 / (quaternions * quaternions).sum(-1) |
| 285 | |
| 286 | o = torch.stack( |
| 287 | ( |
| 288 | 1 - two_s * (j * j + k * k), |
| 289 | two_s * (i * j - k * r), |
| 290 | two_s * (i * k + j * r), |
| 291 | two_s * (i * j + k * r), |
| 292 | 1 - two_s * (i * i + k * k), |
| 293 | two_s * (j * k - i * r), |
| 294 | two_s * (i * k - j * r), |
| 295 | two_s * (j * k + i * r), |
| 296 | 1 - two_s * (i * i + j * j), |
| 297 | ), |
| 298 | -1, |
| 299 | ) |
| 300 | return o.reshape(quaternions.shape[:-1] + (3, 3)) |
| 301 | |
| 302 | |
| 303 | def quaternion_to_matrix_np(quaternions): |
no outgoing calls
no test coverage detected