Quaternions and spatial rotation
From Wikipedia, the free encyclopedia
Unit quaternions provide a convenient mathematical notation for representing orientations and rotations of objects in three dimensions. Compared to Euler angles they are simpler to compose and avoid the problem of gimbal lock. Compared to rotation matrices they are more numerically stable and may be more efficient. Quaternions have found their way into applications in computer graphics, robotics, navigation, molecular dynamics and orbital mechanics of satellites.[1]
[edit] Quaternion rotation operations
A very strict explanation of the properties used in this section is given by Altmann.[2]
[edit] The hypersphere of rotations
[edit] Visualizing the space of rotations
Unit quaternions represent the mathematical space of rotations in three dimensions in a very straightforward way. The correspondence between rotations and quaternions can be understood by first visualizing the space of rotations itself.
Any rotation in three dimensions is a rotation by some angle about some axis. When the angle is zero the axis does not matter, so rotation by zero degrees is a single point in the space of rotations (the identity rotation). For a tiny but nonzero angle, the set of possible rotations is like a small sphere surrounding the identity rotation, where each point on the sphere represents an axis pointing in a particular direction (compare the celestial sphere). Rotations through increasingly large angles are increasingly far from the identity rotation, and we can think of them as concentric spheres of increasing radius. Thus, near the identity rotation, the abstract space of rotations looks similar to ordinary three-dimensional space (which can also be seen as a central point surrounded by concentric spheres of every radius). However, as the rotation angle increases past 180°, rotations about different axes stop diverging and become more similar to each other, becoming identical (and equal to the identity rotation) when the angle reaches 360°.
We can see similar behavior on the surface of a sphere. If we start at the north pole and draw straight lines (that is, lines of longitude) in many directions, they will diverge but eventually converge again at the south pole. Concentric circles of increasing radius drawn around the north pole (lines of latitude) will eventually collapse to a point at the south pole once the radius reaches the distance between the poles. If we think of different directions from the pole (that is, different longitudes) as different rotation axes, and different distances from the pole (that is, different latitudes) as different rotation angles, we have an analogy to the space of rotations. Since the sphere's surface is two dimensional while the axes of rotations use three dimensions, we must actually model the space of rotations as a hypersphere, that is a sphere in 4 dimensions. We can think of the ordinary sphere as a slice through the full hypersphere (just as a circle is a slice through a sphere). We can take the slice to represent, for example, just the rotations about axes in the xy plane (see illustration in front). Note that the angle of rotation is twice the latitude difference from the north pole: points on the equator represent rotations of 180°, not 90°, and the south pole represents a rotation of 360°, not 180°.
The north pole and the south pole represent the same rotation, and in fact this is true of any two antipodal points: if one is a rotation by α around the axis directed by vector
, the other is a rotation by
around the axis directed by vector
. In fact, then, the space of rotations is not the hypersphere itself but the hypersphere with antipodal points identified. But for many purposes we can think of rotations as points on the 4 dimensions sphere, even though they are twofold redundant (a so-called double cover).
[edit] Parameterizing the space of rotations
We can parameterize the surface of a sphere with two coordinates, such as latitude and longitude. But latitude and longitude are ill-behaved (degenerate) at the north and south poles, though the poles are not intrinsically different from any other points on the sphere. At the poles (latitudes +90° and -90°), the longitude becomes meaningless.
It can be shown that no two-parameter coordinate system can avoid such degeneracy (the hairy ball theorem). We can avoid such problems by embedding the sphere in three-dimensional space and parameterizing it with three Cartesian coordinates (here w,x,y), placing the north pole at (w,x,y) = (1,0,0), the south pole at (w,x,y) = (−1,0,0), and the equator at w = 0, x2 + y2 = 1. Points on the sphere satisfy the constraint w2 + x2 + y2 = 1, so we still have just two degrees of freedom though there are three coordinates. A point (w,x,y) on the sphere represents a rotation around the axis directed by the vector (x,y,0) by an angle
.
In the same way the hyperspherical space of 3D rotations can be parameterized by three angles (Euler angles), but any such parameterization is degenerate at some points on the hypersphere, leading to the problem of gimbal lock. We can avoid this by using four Euclidean coordinates w,x,y,z, with w2 + x2 + y2 + z2 = 1. The point (w,x,y,z) represents a rotation around the axis directed by the vector (x,y,z) by an angle 
[edit] From the rotations to the quaternions
[edit] Quaternions briefly
The complex numbers can be defined by introducing an abstract symbol i which satisfies the usual rules of algebra and additionally the rule i2 = −1. This is sufficient to reproduce all of the rules of complex number arithmetic: for example,
.
In the same way the quaternions can be defined by introducing abstract symbols i, j, k which satisfy the rules i2 = j2 = k2 = ijk = −1 and the usual algebraic rules except the commutative law of multiplication (a familiar example of such a noncommutative multiplication is matrix multiplication). From this all of the rules of quaternion arithmetic follow: for example, one can show that
.
The imaginary part
of a quaternion behaves like a vector
in three dimension vector space, and the real part a behaves like a scalar in
. When quaternions are used in geometry, it is more convenient to define them as a scalar plus a vector:
.
Those who have studied vectors at school might find strange to add a number to a vector, as they are objects of very different natures, or to multiply two vectors together, as this operation is usually undefined. However, if one remembers that it is a mere notation for the real and imaginary parts of a quaternion, it becomes more legitimate.
We can express quaternion multiplication in the modern language of vector cross and dot products (which were actually inspired by the quaternions in the first place). In place of the rules i2 = j2 = k2 = ijk = −1 we have the vector multiplication rule
, where:
is vector multiplication,
is vector cross product (a vector),
is vector scalar product (a number).
Vector multiplication is noncommutative (because of the cross product), while scalar-scalar and scalar-vector multiplications commute. From these rules it follows immediately that
.
The (left and right) multiplicative inverse of a nonzero quaternion is
, as can be verified by direct calculation.
[edit] Describing rotations with quaternions
Let (w,x,y,z) be the coordinates of a rotation as previously described. Define the quaternion
where
is a unit vector. Let also
be an ordinary vector of the 3 dimensional space, considered as a quaternion with a real coordinate equal to zero. Then it can be shown (see next section) that the quaternion product
yields the vector
rotated by an angle α around the
axis. The rotation is clockwise if our line of sight points in the direction pointed by
. This operation is known as conjugation by q.
It follows that quaternion multiplication is composition of rotations, for if p and q are quaternions representing rotations, then rotation (conjugation) by pq is
,
which is the same as rotating (conjugating) by q and then by p.
The quaternion inverse of a rotation is the opposite rotation, since
. The square of a quaternion rotation is a rotation by twice the angle around the same axis. More generally qn is a rotation by n times the angle around the same axis as q. This can be extended to arbitrary real n, allowing for smooth interpolation between spatial orientations; see Slerp.
[edit] Proof of the quaternion rotation identity
Let
be a unit vector (the rotation axis) and let
. Our goal is to show that
yields the vector
rotated by an angle α around the axis
. Expanding out, we have
where
and
are the components of
perpendicular and parallel to
respectively. This is the formula of a rotation by α around the
axis.
[edit] Example
[edit] The conjugation operation
Consider the rotation f around the axis
, with a rotation angle of 120°, or 2π⁄3 radians.
The length of
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
If f is the rotation function,
It can be proved that the inverse of a unit quaternion is obtained simply by changing the sign of its imaginary components. As a consequence,
and
This can be simplified, using the ordinary rules for quaternion arithmetic, to
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).
[edit] Quaternion arithmetic in practice
Let's show how we reached the previous result. Let's develop the expression of f (in two stages), and apply the rules
It gives us:
which is the expected result. As we can see, such computations are relatively long and tedious if done manually; however, in a computer program, this amounts to calling the quaternion multiplication routine twice.
[edit] Explaining quaternions' properties with rotations
[edit] Non-commutativity
The multiplication of quaternions is non-commutative. Since the multiplication of unit quaternions corresponds to the composition of three dimensional rotations, this property can be made intuitive 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 and memorize the result. Then restore the original orientation, so that the book title is again readable, and apply those rotations in opposite order. Compare the outcome to the earlier result. This shows that, in general, the composition of two different rotations around two distinct spatial axes will not commute.
[edit] Are quaternions handed?
Note that quaternions, like the rotations or other linear transforms, are not "handed" (as in left-handed vs right-handed). Handedness of a coordinate system comes from the interpretation of the numbers in physical space. No matter what the handedness convention, rotating the X vector 90 degrees around the Z vector will yield the Y vector — the mathematics and numbers are the same.
[edit] Quaternions and other representations of rotations
[edit] Qualitative description of the advantages of quaternions
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 may not be orthogonal anymore and 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° 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.
[edit] Conversion to and from the matrix representation
[edit] From a quaternion to an orthogonal matrix
The orthogonal matrix corresponding to a rotation by the unit quaternion z = a + bi + cj + dk (with |z| = 1) is given by
[edit] From an orthogonal matrix to a quaternion
Finding the quaternion (q0 + qxi + qyj + qzk) that corresponds to a rotation matrix Qij can be numerically unstable if the trace (sum of the diagonal elements) of the rotation matrix is zero or very small. A robust method is to choose the diagonal element with the largest absolute value (Quu). The value of
will certainly be a real number, where uvw is an even permutation of xyz (i.e. xyz, yzx or zxy). The quaternion may now be written as:
Beware the vector convention: There are two conventions for rotation matrices: one assumes row vectors on the left; the other assumes column vectors on the right; the two conventions generate matrices that are the transpose of each other. The above matrix assumes column vectors on the right. In general, a matrix for vertex transpose is ambiguous unless the vector convention is also mentioned. Historically, the column-on-the-right convention comes from mathematics and classical mechanics, whereas row-vector-on-the-left comes from computer graphics, where typesetting row vectors was easier back in the early days.
(Compare the equivalent general formula for a 3 × 3 rotation matrix in terms of the axis and the angle.)
[edit] Fitting quaternions
The above section described how to recover a quaternion q from a 3 × 3 rotation matrix Q. Suppose, however, that we have some matrix Q that is not a pure rotation — due to round-off errors, for example — and we wish to find the quaternion q that most accurately represents Q. In that case we construct a symmetric 4×4 matrix
and find the eigenvector (x,y,z,w) corresponding to the largest eigenvalue. (That value will be 1 if and only if Q is a pure rotation.) The quaternion so obtained will correspond to the rotation closest to the original matrix Q [3]
[edit] 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.
[edit] Results
| Method | Storage |
|---|---|
| Rotation matrix | 9 |
| Quaternion | 4 |
| Angle/axis | 4* |
* 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 mathematical operations.
| Method | # multiplies | # add/subtracts | total operations |
|---|---|---|---|
| Rotation matrices | 27 | 18 | 45 |
| Quaternions | 16 | 12 | 28 |
| Method | # multiplies | # add/subtracts | # sin/cos | total operations |
|---|---|---|---|---|
| Rotation matrix | 9 | 6 | 0 | 15 |
| Quaternions | 21 | 18 | 0 | 39 |
| Angle/axis | 23 | 16 | 2 | 41 |
[edit] Used methods
There are three basic approaches to rotating a vector
:
- Compute the matrix product of a 3x3 rotation matrix R and the original 3x1 column matrix representing
. 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
with coordinates 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 a2 + b2 + c2 + d2 = 1, rewrite the top-left entry as a2 + b2 + c2 + d2 − 2c2 − 2d2 = 1 − 2c2 − 2d2; 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
Given a unit axis (v1, v2, v3) and a θ angle, the angle/axis matrix can be computed with:
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
[edit] 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
, and pretending that it is a quaternion, we can rotate the vector
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
, 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.



[edit] See also
- Slerp — spherical linear interpolation
- Charts on SO(3)
- Rotation representation
- Euler angles
- Axis angle
- Gimbal lock
- Dual quaternion
- Conversion between quaternions and Euler angles
- Rotation group
- Coordinate rotations
- Clifford algebras
- Spinor group
- Covering map
- 3-sphere
- SO(4)
[edit] References
- ^ Quaternions and rotation Sequences: a Primer with Applications to Orbits, Aerospace, and Virtual Reality. Kuipers, Jack B., Princeton University Press copyright 1999.
- ^ Rotations, Quaternions, and Double Groups. Altmann, Simon L., Dover Publications, 1986.
- ^ Bar-Itzhack, Itzhack Y. (Nov.–Dec. 2000), "New method for extracting the quaternion from a rotation matrix", AIAA Journal of Guidance, Control and Dynamics 23 (6): 1085–1087 (Engineering Note), doi:, ISSN 0731-5090
[edit] External links and resources
- Shoemake, Ken. Quaternion tutorial[dead link]
- potential replacement for deadlink — Shoemake, Ken. Quaternions
- Hart, Francis, Kauffman. Quaternion demo
- Dam, Koch, Lillholm. Quaternions, Interpolation and Animation
- Byung-Uk Lee, Unit Quaternion Representation of Rotation
- Ibanez, Luis, Quaternion Tutorial I
- Ibanez, Luis, Quaternion Tutorial II
- Leandra Vicci, Quaternions and Rotations in 3-Space: The Algebra and its Geometric Interpretation




















