Overview
Autonomous driving systems must handle:- 3D rotations: Vehicle orientation in 3D space (SO(3))
- 2D rotations: Planar motion on roads
- Yaw extraction: Converting 3D orientation to heading angle
- Coordinate transforms: Moving between reference frames
- Angle wrapping: Handling angular discontinuities at ±π
Rotation Representations
SO(3) to Yaw Conversion
Extract the yaw (heading) angle from a 3D rotation matrix:rotation.py:25-38:
so3_to_yaw_np() at rotation.py:41-53.
2D Rotation Matrices
Create 2D rotation matrices from angles:rotation.py:109-125:
rotation_matrix() at rotation.py:85-106.
Converting Between 2D and 3D
3D → 2D Projection
Project a 3D rotation to the XY plane:rotation.py:177-194.
2D → 3D Embedding
Embed a 2D rotation into 3D space (flat ground plane assumption):rotation.py:197-213.
Coordinate Transformations
2D Coordinate Transform
Apply rotation and translation to 2D points:rotation.py:128-153. The transformation applies rotation first, then translation:
Angular Operations
Angle Wrapping
Normalize angles to the range[-π, π):
-
Basic wrapping (
angle_wrapatrotation.py:71-82): -
Robust wrapping (
round_2pi_torchatrotation.py:237-246):
atan2(sin, cos) handles edge cases better but is slightly slower.
Robust Arctan2
Stable arctan2 that avoids NaN when both inputs are zero:rotation.py:216-222, this adds a small epsilon when c is near zero to prevent division issues.
Euler Angles
Convert Euler angles to SO(3) rotation matrices (NumPy only):scipy.spatial.transform.Rotation. See rotation.py:56-68.
Advanced: Gram-Schmidt Orthogonalization
Stably orthonormalize two 3D vectors:rotation.py:156-174, this:
- Normalizes the first vector
- Projects the second vector onto the orthogonal complement
- Normalizes the second vector
- Computes the third vector as the cross product
Usage Examples
Example 1: Transform Trajectory to Vehicle Frame
Example 2: Heading Smoothing
Example 3: Batch Coordinate Transforms
Best Practices
- Use robust angle operations: Prefer
round_2pi_torch()over manual modulo for numerical stability - Avoid gimbal lock: When possible, work with rotation matrices rather than Euler angles
- Unwrap angles: For trajectory smoothing, unwrap angles to avoid ±π discontinuities
- Batch operations: Vectorize coordinate transforms to leverage GPU parallelism
- Numerical stability: Use
stable_gramschmidt()when reconstructing rotation matrices - Type consistency: Match NumPy/PyTorch usage with your data pipeline
Common Pitfalls
-
Forgetting to wrap angles: Always wrap angles after arithmetic operations
-
Incorrect transform order: Rotation must happen before translation
-
Mixing 2D and 3D: Ensure dimensional consistency
Performance Notes
- NumPy functions: Optimized for CPU, good for preprocessing
- PyTorch functions: GPU-accelerated, use for training/inference
- Batched operations: Always prefer batched operations over loops
rotation_matrix_torch(angles)whereangles.shape = (B,)→(B, 2, 2)- Much faster than looping over batch dimension
API Reference
Rotation Conversions
| Function | Input | Output | Description |
|---|---|---|---|
so3_to_yaw_torch | (…, 3, 3) | (…,) | Extract yaw from 3D rotation |
so3_to_yaw_np | (…, 3, 3) | (…,) | NumPy version |
euler_2_so3 | (N, 3) | (N, 3, 3) | Euler angles → rotation matrices |
rot_2d_to_3d | (…, 2, 2) | (…, 3, 3) | Embed 2D rotation in 3D |
rot_3d_to_2d | (…, 3, 3) | (…, 2, 2) | Project 3D rotation to 2D |
Rotation Matrices
| Function | Input | Output | Description |
|---|---|---|---|
rotation_matrix_torch | (…,) | (…, 2, 2) | Create 2D rotation matrices |
rotation_matrix | scalar or (…,) | (2, 2) or (…, 2, 2) | NumPy version |
stable_gramschmidt | (…, 3, 2) | (…, 3, 3) | Orthonormalize vectors |
Coordinate Transforms
| Function | Input | Output | Description |
|---|---|---|---|
transform_coords_2d_np | (…, 2) + offset/angle | (…, 2) | Rotate and translate points |
Angular Operations
| Function | Input | Output | Description |
|---|---|---|---|
angle_wrap | (…,) | (…,) | Wrap to [-π, π) |
round_2pi_torch | (…,) | (…,) | Robust angle wrapping |
round_2pi | (…,) | (…,) | NumPy version |
ratan2 | sin, cos | angle | Robust arctan2 |
geometry/rotation.py.