Pywayne VIO SO3
Overview
Complete SO(3) rotation matrix toolkit for 3D rotations with Lie group/ Lie algebra operations, rotation representation conversions, skew-symmetric matrix operations, and rotation averaging.
Quick Start
from pywayne.vio.SO3 import SO3_skew, SO3_Exp, SO3_Log, SO3_to_quat
import numpy as np
# Skew-symmetric matrix
vec = np.array([1, 2, 3])
skew = SO3_skew(vec) # Returns 3x3 skew-symmetric matrix
# Log/Exp mapping
R = np.eye(3)
rotvec = SO3_Log(R) # Rotation vector (Lie algebra)
R_recon = SO3_Exp(rotvec) # Back to rotation matrix
# Quaternion conversion
quat = SO3_to_quat(R) # Returns [w, x, y, z]
Core Functions
Basic Operations
check_SO3(R)
Check if matrix is a valid SO(3) rotation matrix.
- Validates shape (3, 3)
- Checks R.T @ R = I (orthogonality)
SO3_mul(R1, R2)
Multiply two rotation matrices: R1 @ R2.
SO3_diff(R1, R2, from_1_to_2=True)
Compute relative rotation between two matrices.
from_1_to_2=True: Returns R1.T @ R2
from_1_to_2=False: Returns R2.T @ R1
SO3_inv(R)
Compute inverse of rotation matrix (transpose).
- Supports single (3, 3) or batch (N, 3, 3) inputs
Skew-Symmetric Matrices
SO3_skew(vec)
Convert 3D vector to skew-symmetric matrix.
vec = [x, y, z] -> [[ 0, -z, y],
[ z, 0, -x],
[-y, x, 0]]
- Supports single vector (3,) or batch (N, 3)
SO3_unskew(skew)
Extract vector from skew-symmetric matrix.
- Single matrix (3, 3) -> vector (3,)
- Batch (N, 3, 3) -> vectors (N, 3)
Rotation Representation Conversions
Quaternion
SO3_from_quat(q) - Quaternion [w, x, y, z] to rotation matrix
SO3_to_quat(R) - Rotation matrix to quaternion [w, x, y, z]
- Uses Hamilton convention (wxyz)
Axis-Angle
SO3_from_axis_angle(axis, angle) - Axis-angle to rotation matrix
SO3_to_axis_angle(R) - Returns (axis, angle) tuple
Euler Angles
SO3_from_euler(euler_angles, axes='zyx', intrinsic=True) - Euler to matrix
SO3_to_euler(R, axes='zyx', intrinsic=True) - Matrix to Euler
- Supports all rotation sequences
Lie Group/ Lie Algebra Mapping
SO3_Log(R)
SO(3) to so(3) log map, returns rotation vector (3D).
- Input: (3, 3) or (N, 3, 3)
- Output: (3,) or (N, 3)
SO3_log(R)
SO(3) to so(3) log map, returns skew-symmetric matrix (3x3).
- Equivalent to
SO3_skew(SO3_Log(R))
SO3_Exp(rotvec)
so(3) to SO(3) exp map from rotation vector.
- Handles zero vectors gracefully
- Input: (3,) or (N, 3)
- Output: (3, 3) or (N, 3, 3)
SO3_exp(omega_hat)
so(3) to SO(3) exp map from skew-symmetric matrix.
- Equivalent to
SO3_Exp(SO3_unskew(omega_hat))
Averaging
SO3_mean(R)
Compute mean rotation matrix from multiple rotations.
- Uses scipy Rotation.mean()
- Input: (N, 3, 3)
- Output: (3, 3)
Data Formats
Single vs Batch
- Single matrix: shape (3, 3)
- Batch: shape (N, 3, 3)
Most functions handle both automatically.
SO(3) Matrix Properties
R @ R.T = I (orthogonal)
det(R) = 1 (special)
Lie Algebra Vector
Rotation vector where direction is axis, magnitude is angle.
Dependencies
Required packages:
numpy - Array operations
qmt - Quaternion utilities
scipy - Rotation averaging
Install with:
pip install numpy qmt scipy
Example Usage
# Create rotation from axis-angle
axis = np.array([0, 0, 1]) # Z-axis
angle = np.pi / 4 # 45 degrees
R = SO3_from_axis_angle(axis, angle)
# Verify it's valid
print(check_SO3(R)) # True
# Get Lie algebra representation
rotvec = SO3_Log(R)
print(f"Rotation vector: {rotvec}")
# Convert back
R_recon = SO3_Exp(rotvec)
print(f"Reconstruction error: {np.linalg.norm(R - R_recon):.2e}")
# Batch averaging
R_batch = np.array([R, SO3_inv(R), SO3_mul(R, R)])
R_mean = SO3_mean(R_batch)
1---
2name: pywayne-vio-so3
3description: SO(3) rotation matrix utilities including Lie group/ Lie algebra operations, rotation representation conversions, skew-symmetric matrix operations, and rotation averaging. Use when working with 3D rotations, robot kinematics, computer vision, SLAM, or any task requiring SO(3) matrix validation and manipulation, quaternion/ axis-angle/ Euler angle conversions, Lie algebra Log/Exp mapping, skew-symmetric matrix operations, or rotation matrix averaging
4metadata:
5 source: https://skillmd.com/skills/modbender/pywayne-vio-so3
6---
7
8# Pywayne VIO SO3
9
10## Overview
11
12Complete SO(3) rotation matrix toolkit for 3D rotations with Lie group/ Lie algebra operations, rotation representation conversions, skew-symmetric matrix operations, and rotation averaging.
13
14## Quick Start
15
16```python
17from pywayne.vio.SO3 import SO3_skew, SO3_Exp, SO3_Log, SO3_to_quat
18import numpy as np
19
20# Skew-symmetric matrix
21vec = np.array([1, 2, 3])
22skew = SO3_skew(vec) # Returns 3x3 skew-symmetric matrix
23
24# Log/Exp mapping
25R = np.eye(3)
26rotvec = SO3_Log(R) # Rotation vector (Lie algebra)
27R_recon = SO3_Exp(rotvec) # Back to rotation matrix
28
29# Quaternion conversion
30quat = SO3_to_quat(R) # Returns [w, x, y, z]
31```
32
33## Core Functions
34
35### Basic Operations
36
37#### check_SO3(R)
38Check if matrix is a valid SO(3) rotation matrix.
39- Validates shape (3, 3)
40- Checks R.T @ R = I (orthogonality)
41
42#### SO3_mul(R1, R2)
43Multiply two rotation matrices: `R1 @ R2`.
44
45#### SO3_diff(R1, R2, from_1_to_2=True)
46Compute relative rotation between two matrices.
47- `from_1_to_2=True`: Returns `R1.T @ R2`
48- `from_1_to_2=False`: Returns `R2.T @ R1`
49
50#### SO3_inv(R)
51Compute inverse of rotation matrix (transpose).
52- Supports single (3, 3) or batch (N, 3, 3) inputs
53
54### Skew-Symmetric Matrices
55
56#### SO3_skew(vec)
57Convert 3D vector to skew-symmetric matrix.
58```
59vec = [x, y, z] -> [[ 0, -z, y],
60 [ z, 0, -x],
61 [-y, x, 0]]
62```
63- Supports single vector (3,) or batch (N, 3)
64
65#### SO3_unskew(skew)
66Extract vector from skew-symmetric matrix.
67- Single matrix (3, 3) -> vector (3,)
68- Batch (N, 3, 3) -> vectors (N, 3)
69
70### Rotation Representation Conversions
71
72#### Quaternion
73- `SO3_from_quat(q)` - Quaternion [w, x, y, z] to rotation matrix
74- `SO3_to_quat(R)` - Rotation matrix to quaternion [w, x, y, z]
75- Uses Hamilton convention (wxyz)
76
77#### Axis-Angle
78- `SO3_from_axis_angle(axis, angle)` - Axis-angle to rotation matrix
79- `SO3_to_axis_angle(R)` - Returns (axis, angle) tuple
80
81#### Euler Angles
82- `SO3_from_euler(euler_angles, axes='zyx', intrinsic=True)` - Euler to matrix
83- `SO3_to_euler(R, axes='zyx', intrinsic=True)` - Matrix to Euler
84- Supports all rotation sequences
85
86### Lie Group/ Lie Algebra Mapping
87
88#### SO3_Log(R)
89SO(3) to so(3) log map, returns rotation vector (3D).
90- Input: (3, 3) or (N, 3, 3)
91- Output: (3,) or (N, 3)
92
93#### SO3_log(R)
94SO(3) to so(3) log map, returns skew-symmetric matrix (3x3).
95- Equivalent to `SO3_skew(SO3_Log(R))`
96
97#### SO3_Exp(rotvec)
98so(3) to SO(3) exp map from rotation vector.
99- Handles zero vectors gracefully
100- Input: (3,) or (N, 3)
101- Output: (3, 3) or (N, 3, 3)
102
103#### SO3_exp(omega_hat)
104so(3) to SO(3) exp map from skew-symmetric matrix.
105- Equivalent to `SO3_Exp(SO3_unskew(omega_hat))`
106
107### Averaging
108
109#### SO3_mean(R)
110Compute mean rotation matrix from multiple rotations.
111- Uses scipy Rotation.mean()
112- Input: (N, 3, 3)
113- Output: (3, 3)
114
115## Data Formats
116
117### Single vs Batch
118- Single matrix: shape (3, 3)
119- Batch: shape (N, 3, 3)
120
121Most functions handle both automatically.
122
123### SO(3) Matrix Properties
124```
125R @ R.T = I (orthogonal)
126det(R) = 1 (special)
127```
128
129### Lie Algebra Vector
130Rotation vector where direction is axis, magnitude is angle.
131
132## Dependencies
133
134Required packages:
135- `numpy` - Array operations
136- `qmt` - Quaternion utilities
137- `scipy` - Rotation averaging
138
139Install with:
140```bash
141pip install numpy qmt scipy
142```
143
144## Example Usage
145
146```python
147# Create rotation from axis-angle
148axis = np.array([0, 0, 1]) # Z-axis
149angle = np.pi / 4 # 45 degrees
150R = SO3_from_axis_angle(axis, angle)
151
152# Verify it's valid
153print(check_SO3(R)) # True
154
155# Get Lie algebra representation
156rotvec = SO3_Log(R)
157print(f"Rotation vector: {rotvec}")
158
159# Convert back
160R_recon = SO3_Exp(rotvec)
161print(f"Reconstruction error: {np.linalg.norm(R - R_recon):.2e}")
162
163# Batch averaging
164R_batch = np.array([R, SO3_inv(R), SO3_mul(R, R)])
165R_mean = SO3_mean(R_batch)
166```