Rotation Quaternions, and How to Use Them

D. Rose - May, 2015


Abstract

This paper provides a basic introduction to the use of quaternions in 3D rotation applications. We give a simple definition of quaternions, and show how to convert back and forth between quaternions, axis-angle representations, Euler angles, and rotation matrices. We also show how to rotate objects forward and back using quaternions, and how to concatenate several rotation operations into a single quaternion.


Introduction

Strictly speaking, a quaternion is represented by four elements:

q = q0 + iq1 + jq2 + kq3
(1)

where q0, q1, q2 and q3 are real numbers, and i, j and k are mutually orthogonal imaginary unit vectors. The q0 term is referred to as the "real" component, and the remaining three terms are the "imaginary" components. In practice (and for the remainder of this paper), the imaginary notation is implied, and only the four coefficients are used to specify a quaternion, as in equation 2:

q = (q0, q1, q2, q3)
(2)

Quaternions are a complicated subject. However, in this paper we will restrict ourselves to a subset of quaternions called rotation quaternions. Rotation quaternions are a mechanism for representing rotations in three dimensions, and can be used as an alternative to rotation matrices in 3D graphics and other applications. Using them requires no understanding of complex numbers.

Rotation quaternions are closely related to the axis-angle representation of rotation. We will therefore start with an explanation of the axis-angle representation, and then show how to convert to a quaternion.

Axis-Angle Representation of 3D Rotations

According to Euler's rotation theorem, any 3D rotation (or sequence of rotations) can be specified using two parameters: a unit vector that defines an axis of rotation; and an angle θ describing the magnitude of the rotation about that axis. This is illustrated in Figure 1:

Axis-Angle illustration

Figure 1: Any 3D rotation can be specified by an axis of rotation and a rotation angle around that axis

An axis-angle rotation can therefore be represented by four numbers as in equation 3:

(θ, x̂, ŷ, ẑ)
(3)

where:

(x̂, ŷ, ẑ) is a unit vector that defines the axis of rotation

θ is the amount of rotation around (x̂, ŷ, ẑ)

Convert Axis-Angle to Quaternion

A rotation quaternion is similar to the axis-angle representation. If we know the axis-angle components (θ, x̂, ŷ, ẑ), we can convert to a rotation quaternion q as follows:

q = (q0, q1, q2, q3)
(4a)

where:

Equation
(4b)

Equation
(4c)

Equation
(4d)

Equation
(4e)

From these equations we can see that the real term of the quaternion (q0) is completely determined by the rotation angle, and the remaining three imaginary terms (q1, q2 and q3) are just the three rotation axis vectors scaled by a common factor. One consequence of this representation is that the magnitude of a rotation quaternion (that is, the sum of the squares of all four components) is always equal to one.

Since the axis-angle and quaternion representations contain exactly the same information, it is reasonable to ask why we would bother with the less-intuitive quaternions at all? The answer is that to do anything useful with an axis-angle quantity—such as rotate a set of points that make up some 3D object—we have to perform these trigonometric operations anyway. Performing them ahead of time means that most quaternion operations can be accomplished using only multiplication/division and addition/subtraction, thus saving valuable computer cycles.

Convert Quaternion to Axis-Angle

Given the quaternion q = (q0, q1, q2, q3), we can convert back to an axis-angle representation as follows. First, we extract the rotation angle from q0:

θ = 2cos-1(q0)
(5)

If θ is not zero, we can then find the rotation axis unit vector as follows:

Equation
(6)

There is one special case in which equation (6) will fail. A quaternion with the value q = (1,0,0,0) is known as the identity quaternion, and will produce no rotation. In this case, equation (5) will produce a rotation angle (θ) of zero, which is what we expect. However, since the axis of rotation is undefined when there is no rotation, equation (6) will generate a divide-by-zero error. Any software implementation should therefore test whether q0 equals 1.0, and if it does should set θ = 0, and (x̂, ŷ, ẑ) = (1, 0, 0).

It is worth noting that there are several ways to convert from a quaternion to axis-angle, so don't be too concerned if equations 5 and 6 don't match other sources.

Convert Quaternion to Rotation Matrix

Given the rotation quaternion q = (q0, q1, q2, q3), the corresponding rotation matrix is:

Equation

(7a)

Or equivalently:

Equation

(7b)

Both methods work for all valid unit rotation quaternions, including the identity quaternion.

Convert Rotation Matrix to Quaternion

Given the rotation matrix R:

Equation

(8)

We can find the equivalent quaternion using two steps.

Step 1: Find the magnitude of each quaternion component. This leaves the sign of each component undefined:

Equation

(9a)

Equation

(9b)

Equation

(9c)

Equation

(9d)

Step 2: To resolve the signs, find the largest of q0, q1, q2, q3 and assume its sign is positive. Then compute the remaining components as shown in the table below. Taking the largest magnitude avoids division by small numbers, which would reduce numerical accuracy.

table1

The reason the sign is ambiguous is that any given rotation has two possible quaternion representations. If one is known, the other can be found by taking the negative of all four terms. This has the effect of reversing both the rotation angle and the axis of rotation. So for all rotation quaternions, (q0, q1, q2, q3) and (−q0, −q1, −q2, −q3) produce identical rotations. To convert from a rotation matrix to a quaternion, we must arbitrarily pick one of the two possible answers as described in steps 1 and 2.

Convert Euler Angles to Quaternion

Euler angles are a complicated subject, primarily because there are dozens of mutually exclusive ways to define them. Different authors are likely to use different conventions, often without clearly stating the underlying assumptions, which makes it difficult to combine equations and code from more than one source.

In this paper we will use the following definition of Euler angles.

This is a common convention, and most people find it the easiest to visualize. For a more thorough discussion of Euler angles, see this paper.

Given the above definition, we can convert from Euler angles to Quaternion as follows:

Equation

(10a)

Equation

(10b)

Equation

(10c)

Equation

(10d)

where:

u = roll angle

v = pitch angle

w = yaw angle

c() = cosine function

s() = sine function

Equations 10a-d work for all values of Euler angle, including the condition of gimbal lock, where the pitch angle equals +90° or −90°.

Convert Quaternion to Euler Angles

Equations 11a through 11c show how to convert from quaternions to Euler angles:

Equation

(11a)

Equation
(11b)

Equation

(11c)

Gimbal Lock

Equations 11a through 11c are the general solution for extracting Euler angles from a quaternion. But in the special case where the pitch angle is +90° or −90°, the arguments for 11a and 11c will all be zero, for which the atan2() function is undefined.

This is the dreaded "gimbal lock." It occurs because, at a pitch angle of +90° and −90°, the yaw and roll axes of rotation are aligned with each other in the world coordinate system, and therefore produce the same effect. This means there is no unique solution: any orientation can be described using an infinite number of yaw and roll angle combinations.

To handle the gimbal lock condition, we must first use equation 11b to determine whether the pitch angle is +π/2 or −π/2 radians. Then we set either roll or yaw to zero and solve for the other as follows:

table

Note that while Euler angles are susceptible to gimbal lock, quaternions and rotation matrices are not.

Rotating a Point using Quaternions

This section defines quaternion multiplication and inversion, and shows how they are used to perform a rotation.

Quaternion Multiplication

The product of two quaternions:

t = rs

(t0, t1, t2, t3) = (r0, r1, r2, r3) ✕ (s0, s1, s2, s3)

Is defined as:

t0 = (r0s0r1s1r2s2r3s3)
(12a)

t1 = (r0s1 + r1s0r2s3 + r3s2)
(12b)

t2 = (r0s2 + r1s3 + r2s0r3s1)
(12c)

t3 = (r0s3r1s2 + r2s1 + r3s0)
(12d)

Quaternion multiplication is associative, but (except for some special cases) is not commutative. Therefore if a, b and c are quaternions, then:

(ab)c = a(bc)

abba

Quaternion Inversion

The inverse of a quaternion is obtained by negating the imaginary components:

q-1 = ( q0, −q1, −q2, −q3 )
(13)

Quaternion Rotation

We can rotate a point (x, y, z) by the quaternion q using the following three steps:

Step 1: Convert the point to be rotated into a quaternion by assigning the point's coordinates as the quaternion's imaginary components, and setting the quaternion's real component to zero. If (x, y, z) is the point to be rotated, then it is converted to a quaternion as follows:

p = (p0, p1, p2, p3) = ( 0, x, y, z )
(14)

Step 2: Perform the rotation. Quaternion rotation requires two multiplications.

For active rotation:    p' = q−1 pq
(15a)

For passive rotation:    p' = qpq−1
(15b)

where:

p contains the point to be rotated (see step 1)

q is a rotation quaternion

q−1 is the inverse of q

p' contains the coordinates of the rotated point

Active rotation is when the point is rotated with respect to the coordinate system, and passive rotation is when the coordinate system is rotated with respect to the point. The two rotations are opposite from each other.

Note that since quaternion multiplication is associative, it doesn't matter if we perform the multiplications in the order (qp)q−1 or q(pq−1).

Step 3: Extract the rotated coordinates from p':

p' = (0, x', y', z' )
(16)

The rotated quaternion p' will have four elements as does any quaternion. However, the real element will always equal zero. The 3D coordinates of the rotated point (x', y', z') are therefore just the imaginary components of p'.

Properties of Quaternions

The following are some useful properties of quaternions. Up until now, this paper has discussed only rotation quaternions. However, rotation quaternions are only a subset of all possible quaternions, just as rotation matrices are a subset of all possible 3x3 matrices. The following properties apply to all quaternions unless otherwise specified.
  1. The length (magnitude) of a quaternion is equation
  2. A quaternion is a "unit" quaternion if |q| = 1.
  3. All rotation quaternions must be unit quaternions.
  4. The quaternion q = (1, 0, 0, 0) is the identity quaternion. It represents no rotation. If q is an arbitrary quaternion and i is the identity quaternion, then qi = iq = q.
  5. The conjugate of a quaternion is q* = ( q0, −q1, −q2, −q3 )
  6. The inverse of a quaternion is equation. The product of a quaternion and its inverse is the identity quaternion: qq−1 = q−1q = (1, 0, 0, 0). Note that for this special case, quaternion multiplication is commutative.
  7. For rotation quaternions, the inverse equals the conjugate. So for rotation quaternions, q−1 = q* = ( q0, −q1, −q2, −q3 ).
  8. Inverting or conjugating a rotation quaternion has the effect of reversing the axis of rotation, which modifies it to rotate in the opposite direction from the original. That is, if a point is rotated to a new position using q, then rotating it again using q−1 or q* will return it to its original location.
  9. Any given rotation has two possible quaternion representations. If one is known, the other can be found by taking the negative of all four terms. This has the effect of reversing both the rotation angle and the axis of rotation. So if q is a rotation quaternion, then q and −q will produce the same rotation.
  10. A rotation of qa followed by a rotation of qb can be combined into the single rotation qc = qbqa. This can be extended to an arbitrary number of rotations. Note that the order matters (because quaternion multiplication is not commutative).
  11. Quaternion multiplication is associative: (ab)c = a(bc)
  12. Quaternion multiplication is not commutative: abba

Related Pages

 


Comments and error reports may be sent to the following address. We may post comments of general interest. Be sure to identify the page you are commenting on.

Email address as an image to prevent spamming.