Quaternions and spatial rotation
Quaternions provide a convenient mathematical notation for representing orientations and rotations of objects. Due to certain compactness, efficiency, and stability advantages over matrices, quaternions have found their way into applications in computer graphics, robotics, global navigation, and orbital mechanics of satellites.[1]
Quaternion rotation operations
Goal
The goal is to find a formula that expresses rotation in 3D space using quaternion multiplication. Ideally, this formula should be an analog to the complex multiplication used to represent 2D rotations:
- ,
where
is used for rotation by an angle α.
Derivation
The desired 3D rotation formula cannot be simple quaternion multiplication, because rotating a vector (represented as a quaternion with zero real part) should yield a vector, but multiplying a vector with an arbitrary quaternion may result in a non-vector (with non-zero real part).
However, it turns out that we can cancel the real part if we multiply by a quaternion from one side and by the inverse of that quaternion from the other side. Let z = a + u be a non-zero quaternion, and consider the function
where z −1 is the multiplicative inverse of z, and v is a vector in quaternion form. The function f is known as conjugation by z. Note that the real part of f(v) is zero, because in general zw and wz have the same real part for any quaternions z and w, and so
(note that this proof applies the associativity of quaternion multiplication). Furthermore, f is R-linear and we have f(v) = v if and only if v and the imaginary part u of z are collinear (because f(v) = v means v z = z v). Hence f is a rotation whose axis of rotation passes through the origin and is given by the vector u.
Note that conjugation with z is the equivalent to conjugation with rz for any real number r. We can thus restrict our attention to the quaternions of absolute value 1, the so-called unit quaternions. Note that even then z and −z represent the same rotation. (The absolute value |z| of the quaternion z = a + v is defined as the square root of a2 + ||v||2, which makes it multiplicative: |zw| = |z| |w|.) Inverting unit quaternions is especially easy: If |z| = 1, then z−1 = z* (the conjugate z* of the quaternion z = a + v is defined as z* = a − v) and this makes our rotation formula even easier.
It turns out that the angle of rotation is also easy to read off if we are dealing with a unit quaternion z = a + v: we have
- .
Summary
To summarize, a counterclockwise rotation through an angle about an axis v can be represented via conjugation by the unit quaternion z
where is the normalized vector
- .
The composition of two rotations corresponds to quaternion multiplication: if the rotation f is represented by conjugation with the quaternion z and the rotation g is represented by conjugation with w, then the composition f ∘ g is represented by conjugation with zw.
If one wishes to rotate about an axis that does not pass through the origin, then one first translates the vectors into the origin, conjugates, and translates back.
The angle between two quaternions should not be confused with the angle of rotation involved in the rotation between the orientations corresponding to these quaternions: the former is half of the latter (or 180° minus half the latter). The angle between the axes of two rotations is again different.
For example the quaternion for the identity is ±1 and for a 180° rotation about the z-axis is ±k. The angle between the two quaternions is 90°. The angle between the axes of the two rotations is in this case undefined.
Example
Consider the rotation f around the axis u = i + j + k, with a rotation angle of 120°, or 2π⁄3 radians.
The length of u is √3, the half angle is π⁄3 (60°) with cosine ½, (cos 60° = 0.5) and sine √3⁄2, (sin 60° = 0.866...). We are therefore dealing with a conjugation by the unit quaternion
- .
Concretely,
- f(ai + bj + ck) = z (ai + bj + ck) z∗ .
Note that z∗ = 1/z, as z has unit modulus; here z∗ = (1−i−j−k)/2. This can be simplified, using the ordinary rules for quaternion arithmetic, to
- f(ai + bj + ck) = ci + aj + bk,
as expected: the rotation corresponds to keeping a cube held fixed at one point, and rotating it 120° about the long diagonal through the fixed point (observe how the three axes are permuted cyclically).
Quaternion non-commutativity and rotations
The multiplication of quaternions is non-commutative. Since this operation corresponds to a three dimensional rotation, this property can be easily demonstrated by showing that three dimensional rotations are not commutative in general. A simple exercise of applying two rotations to an asymmetrical object (e.g., a book) can explain it. First, rotate a book 90 degrees clockwise around the z axis. Next rotate it 180 degrees clockwise around the x axis. Then restore the original orientation, so that the book title is again readable, and apply those rotations in opposite order. This shows that, in general, the composition of two different rotations around two distinct spatial axes will not commute.
Quaternions versus other representations of rotations
The representation of a rotation as a quaternion (4 numbers) is more compact than the representation as an orthogonal matrix (9 numbers). Furthermore, for a given axis and angle, one can easily construct the corresponding quaternion, and conversely, for a given quaternion one can easily read off the axis and the angle. Both of these are much harder with matrices or Euler angles.
In computer games and other applications, one is often interested in “smooth rotations,” meaning that the scene should slowly rotate and not in a single step. This can be accomplished by choosing a curve such as the spherical linear interpolation in the quaternions, with one endpoint being the identity transformation 1 (or some other initial rotation) and the other being the intended final rotation. This is more problematic with other representations of rotations.
When composing several rotations on a computer, rounding errors necessarily accumulate. A quaternion that’s slightly off still represents a rotation after being normalised—a matrix that’s slightly off need not be orthogonal anymore and therefore is harder to convert back to a proper orthogonal matrix.
Quaternions also avoid a phenomenon called gimbal lock which can result when, for example in pitch/yaw/roll rotational systems, the pitch is rotated 90 degrees up or down, so that yaw and roll then correspond to the same motion, and a degree of freedom of rotation is lost. In a gimbal-based aerospace inertial navigation system, for instance, this could have disastrous results if the aircraft is in a steep dive or ascent.
The orthogonal matrix corresponding to a rotation by the unit quaternion z = a + bi + cj + dk (with |z| = 1) is given by
(Compare the equivalent general formula for a 3 × 3 rotation matrix in terms of the axis and the angle.)
(Matrix is valid for opposite sense rotation (e.g., right-handed rotation on a left-handed coordinate system). For same-sense rotation, transpose the matrix.)
See also: Charts on SO(3), Euler angles, axis angle
Performance comparisons with other rotation methods
This section discusses the performance implications of using quaternions versus other methods (axis/angle or rotation matrices) to perform rotations in 3D. A brief summary:
Method | Storage | # multiplies | # add/subtracts | total operations |
---|---|---|---|---|
Rotation matrix | 9 | 27 | 18 | 45 |
Quaternions | 4 | 16 | 12 | 28 |
Method | Storage | # multiplies | # add/subtracts | # sin/cos | total operations |
---|---|---|---|---|---|
Rotation matrix | 9 | 9 | 6 | 0 | 15 |
Quaternions | 4 | 21 | 18 | 0 | 39 |
Angle/axis | 4* | 23 | 16 | 2 | 41 |
* Note: angle-axis can be stored as 3 elements by multiplying the unit rotation axis by the rotation angle; however, before using it the unit axis and angle must be recovered by normalizing, costing additional math operations.
There are three basic approaches to rotating a vector v:
- Compute the matrix-vector product of a 3x3 rotation matrix R and the original 3x1 vector v, giving . This requires 3*(3 multiplications + 2 additions) = 9 multiplications and 6 additions, the most efficient method for rotating a vector.
- Use the quaternion rotation formula derived above of . Computing this result is equivalent to transforming the quaternion to a rotation matrix R using the formula above then multiplying with a vector. Performing some common subexpression elimination yields an algorithm that costs 21 multiplies and 18 adds. As a second approach, the quaternion could first be converted to its equivalent angle/axis representation then the angle/axis representation used to rotate the vector. However, this is both less efficient and less numerically stable when the quaternion nears the no rotation point.
- Use the angle-axis formula to convert an angle/axis to a rotation matrix R then multiplying with a vector. Converting the angle/axis to R using common subexpression elimination costs 14 multiplies, 2 function calls (sin, cos), and 10 add/subtracts; from item 1, rotating using R adds an additional 9 multiplications and 6 additions for a total of 23 multiplies, 16 add/subtracts, and 2 function calls (sin, cos).
Pseudo-code for rotating using a quaternion: given a quaternion z = a + bi + cj + dk (with |z| = 1) and a vector v with elements v1, v2, and v3, the following code performs a rotation. Note the use of temporary variables txx. Also note one optimization of the diagonal entries of the R matrix: since , rewrite the top-left entry as ; the other two diagonal entries can be similarly rewritten.
t2 = a*b t3 = a*c t4 = a*d t5 = -b*b t6 = b*c t7 = b*d t8 = -c*c t9 = c*d t10 = -d*d v1new = 2*( (t8 + t10)*v1 + (t6 - t4)*v2 + (t3 + t7)*v3 ) + v1 v2new = 2*( (t4 + t6)*v1 + (t5 + t10)*v2 + (t9 - t2)*v3 ) + v2 v3new = 2*( (t7 - t3)*v1 + (t2 + t9)*v2 + (t5 + t8)*v3 ) + v3
Pseudo-code for creating an angle/axis matrix where the unit axis is (v1, v2, v3) and the angle is θ:
t1 = cos(theta) t2 = 1 - t1 t3 = v1*v1 t6 = t2*v1 t7 = t6*v2 t8 = sin(theta) t9 = t8*v3 t11 = t6*v3 t12 = t8*v2 t15 = v2*v2 t19 = t2*v2*v3 t20 = t8*v1 t24 = v3*v3 R[1, 1] = t1 + t2*t3 R[1, 2] = t7 - t9 R[1, 3] = t11 + t12 R[2, 1] = t7 + t9 R[2, 2] = t1 + t2*t15 R[2, 3] = t19 - t20 R[3, 1] = t11 - t12 R[3, 2] = t19 + t20 R[3, 3] = t1 + t2*t24
Pairs of unit quaternions as rotations in 4D space
A pair of unit quaternions zl and zr can represent any rotation in 4D space. Given a four dimensional vector v, and pretending that it is a quaternion, we can rotate the vector v like this:
It is straightforward to check that for each matrix M MT = I, that is, that each matrix (and hence both matrices together) represents a rotation. Note that since (zl v) zr = zl (v zr), the two matrices must commute. Therefore, there are two commuting subgroups of the set of four dimensional rotations. Arbitrary four dimensional rotations have 6 degrees of freedom, each matrix represents 3 of those 6 degrees of freedom.
Since an infinitesimal four-dimensional rotation can be represented by a pair of quaternions (as follows), all (non-infinitesimal) four-dimensional rotations can also be represented.
See also
- Slerp — spherical linear interpolation
- conversion between quaternions and Euler angles
- rotation group
- coordinate rotations
- Clifford algebras
- spinor group
- covering map
- 3-sphere
References
- ^ Quaternions and rotation Sequences: a Primer with Applications to Orbits, Aerospace, and Virtual Reality. Kuipers, Jack B., Princeton University Press copyright 1999.