A fully featured, pythonic library for quaternion representation, manipulation, 3D animation and geometry.
—
Methods for creating Quaternion objects from various representations including components, axis-angle pairs, rotation matrices, and other formats.
Create quaternions using flexible constructor that accepts multiple input formats.
def __init__(self, *args, **kwargs):
"""
Initialize a new Quaternion object.
Supports multiple initialization methods:
- No arguments: Identity quaternion (1, 0, 0, 0)
- Single scalar: Real quaternion (scalar, 0, 0, 0)
- Four scalars: Direct component specification (w, x, y, z)
- Single sequence: Four-element array [w, x, y, z]
- Another Quaternion: Copy constructor
Keyword arguments:
- scalar/vector: Separate real and imaginary parts
- real/imaginary: Alternative names for scalar/vector
- axis/angle or axis/radians or axis/degrees: Axis-angle representation
- array: Four-element sequence [w, x, y, z]
- matrix: 3x3 or 4x4 rotation/transformation matrix
"""Usage Examples:
# Identity quaternion
q = Quaternion() # (1, 0, 0, 0)
# From components
q = Quaternion(1, 0, 0, 0) # (w, x, y, z)
q = Quaternion([1, 0, 0, 0]) # From array
# From scalar and vector parts
q = Quaternion(scalar=1, vector=[0, 0, 0])
q = Quaternion(real=1, imaginary=[0, 0, 0])
# From axis and angle
q = Quaternion(axis=[0, 1, 0], angle=1.57) # Radians
q = Quaternion(axis=[0, 1, 0], radians=1.57)
q = Quaternion(axis=[0, 1, 0], degrees=90)
# From rotation matrix
import numpy as np
matrix = np.eye(3)
q = Quaternion(matrix=matrix)
# Copy constructor
q1 = Quaternion(1, 0, 0, 0)
q2 = Quaternion(q1)Generate uniformly distributed random unit quaternions.
@classmethod
def random(cls):
"""
Generate a random unit quaternion.
Uniformly distributed across the rotation space using method from:
http://planning.cs.uiuc.edu/node198.html
Returns:
Quaternion: Random unit quaternion
"""Usage Example:
# Generate random rotation
random_q = Quaternion.random()
print(f"Random quaternion: {random_q}")
print(f"Is unit: {random_q.is_unit()}")Advanced construction methods primarily for internal use.
@classmethod
def _from_matrix(cls, matrix, rtol=1e-05, atol=1e-08):
"""
Create quaternion from 3x3 rotation or 4x4 transformation matrix.
Parameters:
matrix: numpy array, 3x3 or 4x4 matrix
rtol: relative tolerance for orthogonality check
atol: absolute tolerance for orthogonality check
Returns:
Quaternion: Quaternion representing the same rotation
Raises:
ValueError: If matrix is not orthogonal or determinant != 1
TypeError: If matrix is not numpy array
"""
@classmethod
def _from_axis_angle(cls, axis, angle):
"""
Create quaternion from axis and angle representation.
Parameters:
axis: array-like, 3-vector rotation axis (will be normalized)
angle: float, rotation angle in radians
Returns:
Quaternion: Quaternion representing the rotation
Raises:
ZeroDivisionError: If axis has zero length
"""Internal validation method for ensuring proper input format.
def _validate_number_sequence(self, seq, n):
"""
Validate a sequence to be of a certain length and ensure it's a numpy array of floats.
Parameters:
seq: sequence to validate
n: expected length
Returns:
numpy.ndarray: Validated array of floats
Raises:
ValueError: Invalid length or non-numeric value
"""Convert between degrees and radians.
@staticmethod
def to_degrees(angle_rad):
"""
Convert angle from radians to degrees.
Parameters:
angle_rad: float or None, angle in radians
Returns:
float or None: angle in degrees
"""
@staticmethod
def to_radians(angle_deg):
"""
Convert angle from degrees to radians.
Parameters:
angle_deg: float or None, angle in degrees
Returns:
float or None: angle in radians
"""Usage Example:
# Angle conversion
degrees = Quaternion.to_degrees(3.14159) # ~180.0
radians = Quaternion.to_radians(90) # ~1.5708Install with Tessl CLI
npx tessl i tessl/pypi-pyquaternion