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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
|
import math
import torch
# matrix
def rand_matrix(*shape, requires_grad: bool = False, dtype=None, device=None):
r"""random rotation matrix
Parameters
----------
*shape : int
Returns
-------
`torch.Tensor`
tensor of shape :math:`(\mathrm{shape}, 3, 3)`
"""
R = angles_to_matrix(*rand_angles(*shape, dtype=dtype, device=device))
return R.detach().requires_grad_(requires_grad)
# angles
def identity_angles(*shape, requires_grad: bool = False, dtype=None, device=None):
r"""angles of the identity rotation
Parameters
----------
*shape : int
Returns
-------
alpha : `torch.Tensor`
tensor of shape :math:`(\mathrm{shape})`
beta : `torch.Tensor`
tensor of shape :math:`(\mathrm{shape})`
gamma : `torch.Tensor`
tensor of shape :math:`(\mathrm{shape})`
"""
abc = torch.zeros(3, *shape, dtype=dtype, device=device)
return abc[0].requires_grad_(requires_grad), abc[1].requires_grad_(requires_grad), abc[2].requires_grad_(requires_grad)
def rand_angles(*shape, requires_grad: bool = False, dtype=None, device=None):
r"""random rotation angles
Parameters
----------
*shape : int
Returns
-------
alpha : `torch.Tensor`
tensor of shape :math:`(\mathrm{shape})`
beta : `torch.Tensor`
tensor of shape :math:`(\mathrm{shape})`
gamma : `torch.Tensor`
tensor of shape :math:`(\mathrm{shape})`
"""
alpha, gamma = 2 * math.pi * torch.rand(2, *shape, dtype=dtype, device=device)
beta = torch.rand(shape, dtype=dtype, device=device).mul(2).sub(1).acos()
alpha = alpha.detach().requires_grad_(requires_grad)
beta = beta.detach().requires_grad_(requires_grad)
gamma = gamma.detach().requires_grad_(requires_grad)
return alpha, beta, gamma
def compose_angles(a1, b1, c1, a2, b2, c2):
r"""compose angles
Computes :math:`(a, b, c)` such that :math:`R(a, b, c) = R(a_1, b_1, c_1) \circ R(a_2, b_2, c_2)`
Parameters
----------
a1 : `torch.Tensor`
tensor of shape :math:`(...)`, (applied second)
b1 : `torch.Tensor`
tensor of shape :math:`(...)`, (applied second)
c1 : `torch.Tensor`
tensor of shape :math:`(...)`, (applied second)
a2 : `torch.Tensor`
tensor of shape :math:`(...)`, (applied first)
b2 : `torch.Tensor`
tensor of shape :math:`(...)`, (applied first)
c2 : `torch.Tensor`
tensor of shape :math:`(...)`, (applied first)
Returns
-------
alpha : `torch.Tensor`
tensor of shape :math:`(...)`
beta : `torch.Tensor`
tensor of shape :math:`(...)`
gamma : `torch.Tensor`
tensor of shape :math:`(...)`
"""
a1, b1, c1, a2, b2, c2 = torch.broadcast_tensors(a1, b1, c1, a2, b2, c2)
return matrix_to_angles(angles_to_matrix(a1, b1, c1) @ angles_to_matrix(a2, b2, c2))
def inverse_angles(a, b, c):
r"""angles of the inverse rotation
Parameters
----------
a : `torch.Tensor`
tensor of shape :math:`(...)`
b : `torch.Tensor`
tensor of shape :math:`(...)`
c : `torch.Tensor`
tensor of shape :math:`(...)`
Returns
-------
alpha : `torch.Tensor`
tensor of shape :math:`(...)`
beta : `torch.Tensor`
tensor of shape :math:`(...)`
gamma : `torch.Tensor`
tensor of shape :math:`(...)`
"""
return -c, -b, -a
# quaternions
def identity_quaternion(*shape, requires_grad: bool = False, dtype=None, device=None):
r"""quaternion of identity rotation
Parameters
----------
*shape : int
Returns
-------
`torch.Tensor`
tensor of shape :math:`(\mathrm{shape}, 4)`
"""
q = torch.zeros(*shape, 4, dtype=dtype, device=device)
q[..., 0] = 1 # or -1...
q = q.detach().requires_grad_(requires_grad)
return q
def rand_quaternion(*shape, requires_grad: bool = False, dtype=None, device=None):
r"""generate random quaternion
Parameters
----------
*shape : int
Returns
-------
`torch.Tensor`
tensor of shape :math:`(\mathrm{shape}, 4)`
"""
q = angles_to_quaternion(*rand_angles(*shape, dtype=dtype, device=device))
q = q.detach().requires_grad_(requires_grad)
return q
def compose_quaternion(q1, q2) -> torch.Tensor:
r"""compose two quaternions: :math:`q_1 \circ q_2`
Parameters
----------
q1 : `torch.Tensor`
tensor of shape :math:`(..., 4)`, (applied second)
q2 : `torch.Tensor`
tensor of shape :math:`(..., 4)`, (applied first)
Returns
-------
`torch.Tensor`
tensor of shape :math:`(..., 4)`
"""
q1, q2 = torch.broadcast_tensors(q1, q2)
return torch.stack(
[
q1[..., 0] * q2[..., 0] - q1[..., 1] * q2[..., 1] - q1[..., 2] * q2[..., 2] - q1[..., 3] * q2[..., 3],
q1[..., 1] * q2[..., 0] + q1[..., 0] * q2[..., 1] + q1[..., 2] * q2[..., 3] - q1[..., 3] * q2[..., 2],
q1[..., 0] * q2[..., 2] - q1[..., 1] * q2[..., 3] + q1[..., 2] * q2[..., 0] + q1[..., 3] * q2[..., 1],
q1[..., 0] * q2[..., 3] + q1[..., 1] * q2[..., 2] - q1[..., 2] * q2[..., 1] + q1[..., 3] * q2[..., 0],
],
dim=-1,
)
def inverse_quaternion(q):
r"""inverse of a quaternion
Works only for unit quaternions.
Parameters
----------
q : `torch.Tensor`
tensor of shape :math:`(..., 4)`
Returns
-------
`torch.Tensor`
tensor of shape :math:`(..., 4)`
"""
q = q.clone()
q[..., 1:].neg_()
return q
# axis-angle
def rand_axis_angle(*shape, requires_grad: bool = False, dtype=None, device=None):
r"""generate random rotation as axis-angle
Parameters
----------
*shape : int
Returns
-------
axis : `torch.Tensor`
tensor of shape :math:`(\mathrm{shape}, 3)`
angle : `torch.Tensor`
tensor of shape :math:`(\mathrm{shape})`
"""
axis, angle = angles_to_axis_angle(*rand_angles(*shape, dtype=dtype, device=device))
axis = axis.detach().requires_grad_(requires_grad)
angle = angle.detach().requires_grad_(requires_grad)
return axis, angle
def compose_axis_angle(axis1, angle1, axis2, angle2):
r"""compose :math:`(\vec x_1, \alpha_1)` with :math:`(\vec x_2, \alpha_2)`
Parameters
----------
axis1 : `torch.Tensor`
tensor of shape :math:`(..., 3)`, (applied second)
angle1 : `torch.Tensor`
tensor of shape :math:`(...)`, (applied second)
axis2 : `torch.Tensor`
tensor of shape :math:`(..., 3)`, (applied first)
angle2 : `torch.Tensor`
tensor of shape :math:`(...)`, (applied first)
Returns
-------
axis : `torch.Tensor`
tensor of shape :math:`(..., 3)`
angle : `torch.Tensor`
tensor of shape :math:`(...)`
"""
return quaternion_to_axis_angle(
compose_quaternion(axis_angle_to_quaternion(axis1, angle1), axis_angle_to_quaternion(axis2, angle2))
)
# conversions
def matrix_x(angle: torch.Tensor) -> torch.Tensor:
r"""matrix of rotation around X axis
Parameters
----------
angle : `torch.Tensor`
tensor of any shape :math:`(...)`
Returns
-------
`torch.Tensor`
matrices of shape :math:`(..., 3, 3)`
"""
c = angle.cos()
s = angle.sin()
o = torch.ones_like(angle)
z = torch.zeros_like(angle)
return torch.stack(
[
torch.stack([o, z, z], dim=-1),
torch.stack([z, c, -s], dim=-1),
torch.stack([z, s, c], dim=-1),
],
dim=-2,
)
def matrix_y(angle: torch.Tensor) -> torch.Tensor:
r"""matrix of rotation around Y axis
Parameters
----------
angle : `torch.Tensor`
tensor of any shape :math:`(...)`
Returns
-------
`torch.Tensor`
matrices of shape :math:`(..., 3, 3)`
"""
c = angle.cos()
s = angle.sin()
o = torch.ones_like(angle)
z = torch.zeros_like(angle)
return torch.stack(
[
torch.stack([c, z, s], dim=-1),
torch.stack([z, o, z], dim=-1),
torch.stack([-s, z, c], dim=-1),
],
dim=-2,
)
def matrix_z(angle: torch.Tensor) -> torch.Tensor:
r"""matrix of rotation around Z axis
Parameters
----------
angle : `torch.Tensor`
tensor of any shape :math:`(...)`
Returns
-------
`torch.Tensor`
matrices of shape :math:`(..., 3, 3)`
"""
c = angle.cos()
s = angle.sin()
o = torch.ones_like(angle)
z = torch.zeros_like(angle)
return torch.stack(
[torch.stack([c, -s, z], dim=-1), torch.stack([s, c, z], dim=-1), torch.stack([z, z, o], dim=-1)], dim=-2
)
def angles_to_matrix(alpha, beta, gamma) -> torch.Tensor:
r"""conversion from angles to matrix
Parameters
----------
alpha : `torch.Tensor`
tensor of shape :math:`(...)`
beta : `torch.Tensor`
tensor of shape :math:`(...)`
gamma : `torch.Tensor`
tensor of shape :math:`(...)`
Returns
-------
`torch.Tensor`
matrices of shape :math:`(..., 3, 3)`
"""
alpha, beta, gamma = torch.broadcast_tensors(alpha, beta, gamma)
return matrix_y(alpha) @ matrix_x(beta) @ matrix_y(gamma)
def matrix_to_angles(R):
r"""conversion from matrix to angles
Parameters
----------
R : `torch.Tensor`
matrices of shape :math:`(..., 3, 3)`
Returns
-------
alpha : `torch.Tensor`
tensor of shape :math:`(...)`
beta : `torch.Tensor`
tensor of shape :math:`(...)`
gamma : `torch.Tensor`
tensor of shape :math:`(...)`
"""
assert torch.allclose(torch.det(R), R.new_tensor(1))
x = R @ R.new_tensor([0.0, 1.0, 0.0])
a, b = xyz_to_angles(x)
R = angles_to_matrix(a, b, torch.zeros_like(a)).transpose(-1, -2) @ R
c = torch.atan2(R[..., 0, 2], R[..., 0, 0])
return a, b, c
def angles_to_quaternion(alpha, beta, gamma) -> torch.Tensor:
r"""conversion from angles to quaternion
Parameters
----------
alpha : `torch.Tensor`
tensor of shape :math:`(...)`
beta : `torch.Tensor`
tensor of shape :math:`(...)`
gamma : `torch.Tensor`
tensor of shape :math:`(...)`
Returns
-------
`torch.Tensor`
matrices of shape :math:`(..., 4)`
"""
alpha, beta, gamma = torch.broadcast_tensors(alpha, beta, gamma)
qa = axis_angle_to_quaternion(alpha.new_tensor([0.0, 1.0, 0.0]), alpha)
qb = axis_angle_to_quaternion(beta.new_tensor([1.0, 0.0, 0.0]), beta)
qc = axis_angle_to_quaternion(gamma.new_tensor([0.0, 1.0, 0.0]), gamma)
return compose_quaternion(qa, compose_quaternion(qb, qc))
def matrix_to_quaternion(R) -> torch.Tensor:
r"""conversion from matrix :math:`R` to quaternion :math:`q`
Parameters
----------
R : `torch.Tensor`
tensor of shape :math:`(..., 3, 3)`
Returns
-------
`torch.Tensor`
tensor of shape :math:`(..., 4)`
"""
return axis_angle_to_quaternion(*matrix_to_axis_angle(R))
def axis_angle_to_quaternion(xyz, angle) -> torch.Tensor:
r"""convertion from axis-angle to quaternion
Parameters
----------
xyz : `torch.Tensor`
tensor of shape :math:`(..., 3)`
angle : `torch.Tensor`
tensor of shape :math:`(...)`
Returns
-------
`torch.Tensor`
tensor of shape :math:`(..., 4)`
"""
xyz, angle = torch.broadcast_tensors(xyz, angle[..., None])
xyz = torch.nn.functional.normalize(xyz, dim=-1)
c = torch.cos(angle[..., :1] / 2)
s = torch.sin(angle / 2)
return torch.cat([c, xyz * s], dim=-1)
def quaternion_to_axis_angle(q):
r"""convertion from quaternion to axis-angle
Parameters
----------
q : `torch.Tensor`
tensor of shape :math:`(..., 4)`
Returns
-------
axis : `torch.Tensor`
tensor of shape :math:`(..., 3)`
angle : `torch.Tensor`
tensor of shape :math:`(...)`
"""
angle = 2 * torch.acos(q[..., 0].clamp(-1, 1))
axis = torch.nn.functional.normalize(q[..., 1:], dim=-1)
return axis, angle
def matrix_to_axis_angle(R):
r"""conversion from matrix to axis-angle
Parameters
----------
R : `torch.Tensor`
tensor of shape :math:`(..., 3, 3)`
Returns
-------
axis : `torch.Tensor`
tensor of shape :math:`(..., 3)`
angle : `torch.Tensor`
tensor of shape :math:`(...)`
"""
assert torch.allclose(torch.det(R), R.new_tensor(1))
tr = R[..., 0, 0] + R[..., 1, 1] + R[..., 2, 2]
angle = torch.acos(tr.sub(1).div(2).clamp(-1, 1))
axis = torch.stack(
[
R[..., 2, 1] - R[..., 1, 2],
R[..., 0, 2] - R[..., 2, 0],
R[..., 1, 0] - R[..., 0, 1],
],
dim=-1,
)
axis = torch.nn.functional.normalize(axis, dim=-1)
return axis, angle
def angles_to_axis_angle(alpha, beta, gamma):
r"""conversion from angles to axis-angle
Parameters
----------
alpha : `torch.Tensor`
tensor of shape :math:`(...)`
beta : `torch.Tensor`
tensor of shape :math:`(...)`
gamma : `torch.Tensor`
tensor of shape :math:`(...)`
Returns
-------
axis : `torch.Tensor`
tensor of shape :math:`(..., 3)`
angle : `torch.Tensor`
tensor of shape :math:`(...)`
"""
return matrix_to_axis_angle(angles_to_matrix(alpha, beta, gamma))
def axis_angle_to_matrix(axis, angle) -> torch.Tensor:
r"""conversion from axis-angle to matrix
Parameters
----------
axis : `torch.Tensor`
tensor of shape :math:`(..., 3)`
angle : `torch.Tensor`
tensor of shape :math:`(...)`
Returns
-------
`torch.Tensor`
tensor of shape :math:`(..., 3, 3)`
"""
axis, angle = torch.broadcast_tensors(axis, angle[..., None])
alpha, beta = xyz_to_angles(axis)
R = angles_to_matrix(alpha, beta, torch.zeros_like(beta))
Ry = matrix_y(angle[..., 0])
return R @ Ry @ R.transpose(-2, -1)
def quaternion_to_matrix(q) -> torch.Tensor:
r"""convertion from quaternion to matrix
Parameters
----------
q : `torch.Tensor`
tensor of shape :math:`(..., 4)`
Returns
-------
`torch.Tensor`
tensor of shape :math:`(..., 3, 3)`
"""
return axis_angle_to_matrix(*quaternion_to_axis_angle(q))
def quaternion_to_angles(q):
r"""convertion from quaternion to angles
Parameters
----------
q : `torch.Tensor`
tensor of shape :math:`(..., 4)`
Returns
-------
alpha : `torch.Tensor`
tensor of shape :math:`(...)`
beta : `torch.Tensor`
tensor of shape :math:`(...)`
gamma : `torch.Tensor`
tensor of shape :math:`(...)`
"""
return matrix_to_angles(quaternion_to_matrix(q))
def axis_angle_to_angles(axis, angle):
r"""convertion from axis-angle to angles
Parameters
----------
axis : `torch.Tensor`
tensor of shape :math:`(..., 3)`
angle : `torch.Tensor`
tensor of shape :math:`(...)`
Returns
-------
alpha : `torch.Tensor`
tensor of shape :math:`(...)`
beta : `torch.Tensor`
tensor of shape :math:`(...)`
gamma : `torch.Tensor`
tensor of shape :math:`(...)`
"""
return matrix_to_angles(axis_angle_to_matrix(axis, angle))
# point on the sphere
def angles_to_xyz(alpha, beta) -> torch.Tensor:
r"""convert :math:`(\alpha, \beta)` into a point :math:`(x, y, z)` on the sphere
Parameters
----------
alpha : `torch.Tensor`
tensor of shape :math:`(...)`
beta : `torch.Tensor`
tensor of shape :math:`(...)`
Returns
-------
`torch.Tensor`
tensor of shape :math:`(..., 3)`
Examples
--------
>>> angles_to_xyz(torch.tensor(1.7), torch.tensor(0.0)).abs()
tensor([0., 1., 0.])
"""
alpha, beta = torch.broadcast_tensors(alpha, beta)
x = torch.sin(beta) * torch.sin(alpha)
y = torch.cos(beta)
z = torch.sin(beta) * torch.cos(alpha)
return torch.stack([x, y, z], dim=-1)
def xyz_to_angles(xyz):
r"""convert a point :math:`\vec r = (x, y, z)` on the sphere into angles :math:`(\alpha, \beta)`
.. math::
\vec r = R(\alpha, \beta, 0) \vec e_z
Parameters
----------
xyz : `torch.Tensor`
tensor of shape :math:`(..., 3)`
Returns
-------
alpha : `torch.Tensor`
tensor of shape :math:`(...)`
beta : `torch.Tensor`
tensor of shape :math:`(...)`
"""
xyz = torch.nn.functional.normalize(xyz, p=2.0, dim=-1) # forward 0's instead of nan for zero-radius
xyz = xyz.clamp(-1, 1)
beta = torch.acos(xyz[..., 1])
alpha = torch.atan2(xyz[..., 0], xyz[..., 2])
return alpha, beta
|