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