kups.core.utils.quaternion
¶
Quaternion representation for 3D rotations.
This module provides a quaternion class for representing and manipulating 3D rotations efficiently. Quaternions avoid gimbal lock and provide smooth interpolation compared to Euler angles.
Quaternion
¶
Bases: Sliceable
Unit quaternion representing a 3D rotation.
Quaternions are represented as (w, x, y, z) where w is the scalar (real)
part and (x, y, z) is the vector (imaginary) part. For a rotation by angle
θ around axis u:
Attributes:
| Name | Type | Description |
|---|---|---|
components |
Array
|
Array of shape |
Example
# Create identity quaternion (no rotation)
q = Quaternion.identity()
# Generate random rotation
key = jax.random.PRNGKey(0)
q_random = Quaternion.random(key, shape=(10,))
# Rotate a point
point = jnp.array([1.0, 0.0, 0.0])
rotated = point @ q
# Compose rotations
q_combined = q1 * q2
# Invert rotation
q_inv = q.inv()
Source code in src/kups/core/utils/quaternion.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | |
__mul__(other)
¶
Compose two rotations via quaternion multiplication.
Quaternion multiplication is non-commutative. The result q1 * q2
applies rotation q2 first, then q1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
Another quaternion to compose with. |
required |
Returns:
| Type | Description |
|---|---|
Quaternion
|
Composed quaternion representing the combined rotation. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Example
Source code in src/kups/core/utils/quaternion.py
__pow__(other)
¶
Raise quaternion to a power for scaled rotations.
For a quaternion representing rotation by angle theta, raising it to
power p produces a rotation by angle p * theta around the same axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
Scalar or array exponent. |
required |
Returns:
| Type | Description |
|---|---|
Quaternion
|
Quaternion representing the scaled rotation. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Source code in src/kups/core/utils/quaternion.py
__rmatmul__(other)
¶
Rotate a 3D vector or batch of vectors by this quaternion.
Implements the operation point @ quaternion to apply the rotation
represented by the quaternion to one or more 3D points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
Array of shape |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Rotated point(s) with the same shape as input. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If the last dimension of |
Example
Source code in src/kups/core/utils/quaternion.py
as_matrix()
¶
Convert quaternion to a 3×3 rotation matrix.
Returns:
| Type | Description |
|---|---|
Array
|
Rotation matrix of shape |
Source code in src/kups/core/utils/quaternion.py
identity()
classmethod
¶
Create an identity quaternion representing no rotation.
Returns:
| Type | Description |
|---|---|
Quaternion
|
Identity quaternion |
inv()
¶
Compute the inverse (conjugate) quaternion.
For unit quaternions, the inverse equals the conjugate, which negates the vector part while keeping the scalar part unchanged.
Returns:
| Type | Description |
|---|---|
Quaternion
|
Inverse quaternion that reverses this rotation. |
Source code in src/kups/core/utils/quaternion.py
random(key, shape=())
classmethod
¶
Generate uniformly distributed random rotation quaternions.
Uses the method from Shoemake (1992) to sample uniformly from the rotation group SO(3).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Array
|
JAX PRNG key for random number generation. |
required |
shape
|
tuple[int, ...]
|
Shape of the batch dimensions. Default is |
()
|
Returns:
| Type | Description |
|---|---|
Quaternion
|
Random unit quaternion(s) with shape |
Reference
K. Shoemake, "Uniform random rotations", Graphics Gems III, 1992.