3D transformations for Python with comprehensive rotation representations, coordinate conversions, and visualization tools
—
Batch operations on rotations in three dimensions (SO(3)) providing orders of magnitude faster processing compared to individual conversions. All functions operate on n-dimensional arrays where the last dimension (vectors) or last two dimensions (matrices) contain individual rotations.
Utilities for batch vector normalization, angle calculations, and cross product matrices.
def norm_vectors(V):
"""
Normalize vectors.
Parameters:
- V: array-like, shape (..., 3) - vectors to normalize
Returns:
- V_normalized: array, shape (..., 3) - normalized vectors
"""
def angles_between_vectors(A, B):
"""
Compute angles between corresponding vectors.
Parameters:
- A: array-like, shape (..., 3) - first vectors
- B: array-like, shape (..., 3) - second vectors
Returns:
- angles: array, shape (...,) - angles in radians
"""
def cross_product_matrices(V):
"""
Generate cross product matrices from vectors.
Parameters:
- V: array-like, shape (..., 3) - vectors
Returns:
- matrices: array, shape (..., 3, 3) - cross product matrices
"""Batch operations for axis-angle representations and conversions.
def norm_axis_angles(A):
"""
Normalize axis-angle representations.
Parameters:
- A: array-like, shape (..., 4) - axis-angle vectors [x, y, z, angle]
Returns:
- A_normalized: array, shape (..., 4) - normalized axis-angles
"""
def matrices_from_compact_axis_angles(A):
"""
Compute rotation matrices from compact axis-angle vectors.
Parameters:
- A: array-like, shape (..., 3) - compact axis-angles [x*angle, y*angle, z*angle]
Returns:
- R: array, shape (..., 3, 3) - rotation matrices
"""
def axis_angles_from_matrices(R):
"""
Compute axis-angle representations from rotation matrices.
Parameters:
- R: array-like, shape (..., 3, 3) - rotation matrices
Returns:
- A: array, shape (..., 4) - axis-angle vectors [x, y, z, angle]
"""Batch conversion from Euler angles to rotation matrices with support for both intrinsic and extrinsic conventions.
def active_matrices_from_intrinsic_euler_angles(e, i, j, k):
"""
Compute rotation matrices from intrinsic Euler angles.
Parameters:
- e: array-like, shape (..., 3) - Euler angles in radians
- i: int - first rotation axis (0=x, 1=y, 2=z)
- j: int - second rotation axis
- k: int - third rotation axis
Returns:
- R: array, shape (..., 3, 3) - rotation matrices
"""
def active_matrices_from_extrinsic_euler_angles(e, i, j, k):
"""
Compute rotation matrices from extrinsic Euler angles.
Parameters:
- e: array-like, shape (..., 3) - Euler angles in radians
- i: int - first rotation axis (0=x, 1=y, 2=z)
- j: int - second rotation axis
- k: int - third rotation axis
Returns:
- R: array, shape (..., 3, 3) - rotation matrices
"""Batch operations for quaternion representations including concatenation, conjugation, and conversion functions.
def matrices_from_quaternions(Q):
"""
Compute rotation matrices from quaternions.
Parameters:
- Q: array-like, shape (..., 4) - quaternions [w, x, y, z] or [x, y, z, w]
Returns:
- R: array, shape (..., 3, 3) - rotation matrices
"""
def quaternions_from_matrices(R):
"""
Compute quaternions from rotation matrices.
Parameters:
- R: array-like, shape (..., 3, 3) - rotation matrices
Returns:
- Q: array, shape (..., 4) - quaternions
"""
def batch_concatenate_quaternions(Q1, Q2):
"""
Concatenate quaternions (Q1 * Q2).
Parameters:
- Q1: array-like, shape (..., 4) - first quaternions
- Q2: array-like, shape (..., 4) - second quaternions
Returns:
- Q: array, shape (..., 4) - concatenated quaternions
"""
def batch_q_conj(Q):
"""
Compute quaternion conjugates.
Parameters:
- Q: array-like, shape (..., 4) - quaternions
Returns:
- Q_conj: array, shape (..., 4) - conjugated quaternions
"""
def quaternion_slerp_batch(start, end, t):
"""
Spherical linear interpolation between quaternions.
Parameters:
- start: array-like, shape (..., 4) - start quaternions
- end: array-like, shape (..., 4) - end quaternions
- t: array-like, shape (...,) - interpolation parameters [0, 1]
Returns:
- Q: array, shape (..., 4) - interpolated quaternions
"""
def axis_angles_from_quaternions(Q):
"""
Compute axis-angle representations from quaternions.
Parameters:
- Q: array-like, shape (..., 4) - quaternions
Returns:
- A: array, shape (..., 4) - axis-angle vectors [x, y, z, angle]
"""
def smooth_quaternion_trajectory(Q):
"""
Smooth quaternion trajectory by ensuring shortest path.
Parameters:
- Q: array-like, shape (n_steps, 4) - quaternion trajectory
Returns:
- Q_smooth: array, shape (n_steps, 4) - smoothed trajectory
"""
def batch_quaternion_wxyz_from_xyzw(Q):
"""
Convert quaternions from [x, y, z, w] to [w, x, y, z] format.
Parameters:
- Q: array-like, shape (..., 4) - quaternions in [x, y, z, w] format
Returns:
- Q_wxyz: array, shape (..., 4) - quaternions in [w, x, y, z] format
"""
def batch_quaternion_xyzw_from_wxyz(Q):
"""
Convert quaternions from [w, x, y, z] to [x, y, z, w] format.
Parameters:
- Q: array-like, shape (..., 4) - quaternions in [w, x, y, z] format
Returns:
- Q_xyzw: array, shape (..., 4) - quaternions in [x, y, z, w] format
"""Batch generation of rotation matrices from simple angles.
def active_matrices_from_angles(angles, axes):
"""
Compute rotation matrices from angles around specified axes.
Parameters:
- angles: array-like, shape (...,) - rotation angles in radians
- axes: int - rotation axis (0=x, 1=y, 2=z)
Returns:
- R: array, shape (..., 3, 3) - rotation matrices
"""import numpy as np
from pytransform3d.batch_rotations import (
matrices_from_quaternions,
batch_concatenate_quaternions,
quaternion_slerp_batch
)
# Process multiple quaternions at once
quaternions = np.array([
[1, 0, 0, 0], # identity
[0.707, 0, 0, 0.707], # 90° around z
[0.5, 0.5, 0.5, 0.5] # complex rotation
])
# Convert all to rotation matrices
rotation_matrices = matrices_from_quaternions(quaternions)
print(rotation_matrices.shape) # (3, 3, 3)
# Interpolate between quaternions
start_q = np.array([[1, 0, 0, 0]])
end_q = np.array([[0.707, 0, 0, 0.707]])
t_values = np.linspace(0, 1, 10)
interpolated = quaternion_slerp_batch(
np.broadcast_to(start_q, (10, 4)),
np.broadcast_to(end_q, (10, 4)),
t_values
)import numpy as np
from pytransform3d.batch_rotations import active_matrices_from_intrinsic_euler_angles
# Multiple sets of XYZ Euler angles
euler_angles = np.array([
[0.1, 0.2, 0.3],
[0.5, -0.1, 0.8],
[-0.3, 0.7, -0.2]
])
# Convert all to rotation matrices (XYZ intrinsic)
matrices = active_matrices_from_intrinsic_euler_angles(euler_angles, 0, 1, 2)
print(matrices.shape) # (3, 3, 3)Install with Tessl CLI
npx tessl i tessl/pypi-pytransform3d