|
3 | 3 | # This source code is licensed under the MIT license found in the |
4 | 4 | # LICENSE file in the root directory of this source tree. |
5 | 5 |
|
| 6 | +""" |
| 7 | +Quaternion Utilities |
| 8 | +=================== |
| 9 | +
|
| 10 | +This module provides comprehensive utilities for working with quaternions in PyMomentum. |
| 11 | +
|
| 12 | +Quaternions are a mathematical representation of rotations in 3D space that offer several |
| 13 | +advantages over other rotation representations like Euler angles or rotation matrices: |
| 14 | +
|
| 15 | +- **No gimbal lock**: Unlike Euler angles, quaternions don't suffer from singularities |
| 16 | +- **Compact representation**: Only 4 components vs 9 for rotation matrices |
| 17 | +- **Efficient composition**: Quaternion multiplication is faster than matrix multiplication |
| 18 | +- **Smooth interpolation**: SLERP provides natural rotation interpolation |
| 19 | +
|
| 20 | +Quaternion Format |
| 21 | +----------------- |
| 22 | +This module uses the (x, y, z, w) format where: |
| 23 | +
|
| 24 | +- **(x, y, z)**: Vector part representing the rotation axis scaled by sin(θ/2) |
| 25 | +- **w**: Scalar part representing cos(θ/2), where θ is the rotation angle |
| 26 | +
|
| 27 | +The identity quaternion is (0, 0, 0, 1), representing no rotation. |
| 28 | +
|
| 29 | +Core Operations |
| 30 | +--------------- |
| 31 | +The module provides functions for: |
| 32 | +
|
| 33 | +- **Basic operations**: :func:`multiply`, :func:`conjugate`, :func:`inverse`, :func:`normalize` |
| 34 | +- **Conversions**: :func:`from_axis_angle`, :func:`euler_xyz_to_quaternion`, |
| 35 | + :func:`from_rotation_matrix`, :func:`to_rotation_matrix` |
| 36 | +- **Vector operations**: :func:`rotate_vector`, :func:`from_two_vectors` |
| 37 | +- **Interpolation**: :func:`slerp`, :func:`blend` |
| 38 | +- **Utilities**: :func:`check`, :func:`split`, :func:`identity` |
| 39 | +
|
| 40 | +Example: |
| 41 | + Basic quaternion operations:: |
| 42 | +
|
| 43 | + import torch |
| 44 | + from pymomentum import quaternion |
| 45 | +
|
| 46 | + # Create identity quaternion |
| 47 | + q_identity = quaternion.identity() |
| 48 | +
|
| 49 | + # Create quaternion from axis-angle |
| 50 | + axis_angle = torch.tensor([0.0, 0.0, 1.57]) # 90° rotation around Z |
| 51 | + q_rot = quaternion.from_axis_angle(axis_angle) |
| 52 | +
|
| 53 | + # Rotate a vector |
| 54 | + vector = torch.tensor([1.0, 0.0, 0.0]) |
| 55 | + rotated = quaternion.rotate_vector(q_rot, vector) |
| 56 | +
|
| 57 | + # Interpolate between quaternions |
| 58 | + q_interp = quaternion.slerp(q_identity, q_rot, 0.5) |
| 59 | +
|
| 60 | +Note: |
| 61 | + All functions expect quaternions as PyTorch tensors with the last dimension |
| 62 | + having size 4, following the (x, y, z, w) format. Most functions support |
| 63 | + batched operations for efficient processing of multiple quaternions. |
| 64 | +""" |
| 65 | + |
6 | 66 | from typing import Sequence |
7 | 67 |
|
8 | 68 | import torch |
|
0 commit comments