CS 184 home page
A note on these notes:
These notes on quaternions were created as a resource for
students taking
CS184 at UC Berkeley.
I am not doing any research related to quaternions and I do not claim to have
discovered anything original about them.
If your questions about quaternions are not answered here, please see the
references
at the end of this page for more information.
|
CS184: Using Quaternions to Represent Rotation
If you would like a hard copy of these notes, a gzipped postscript version can
be found in the file quatnotes.ps.
The LaTeX source can
be found in the file quatnotes.tex.
If you are interested in better understanding quaternions,
look at the
proof that quaternions can be used to compute rotations
Quaternions
Complex Numbers (a brief review)
As you know, the system of complex numbers is defined in terms of
i, a square root of -1.
Although i is not a real number, we can write any complex
number in terms of real numbers, using a real and a complex part.
We can also define a conjugate and a magnitude (or absolute value) for these numbers.
Using the properties above, we can describe multiplication for complex
numbers
Quaternions
Quaternions are an extension of complex numbers.
Instead of just i, we have three different numbers that are all
square roots of -1 labelled i, j, and k.
When you multiply two of these numbers together, they behave similarly
to cross products of the unit basis vectors.
The conjugate and magnitude of a quaternion are found in much the same
way as complex conjugate and magnitude.
Unit quaternions |
|
Quaternions are associative |
|
Quaternions are not commutative |
|
The inverse of a quaternion refers to the multiplicative
inverse (or 1/q) and can be computed by q-1=q'/(q*q')
If a quaternion q has length 1,
we say that q is a unit quaternion.
The inverse of a unit quaternion is its conjugate, q-1=q'
We can represent a quaternion in several ways,
- as a linear combination of 1, i, j, and k,
- as a vector of the four coefficients in this linear combination,
- or as a scalar for the coefficient of 1
and a vector for the coefficients of the imaginary terms.
We can write the product of two quaternions in terms of the (s,v) representation
using standard vector products in the following way:
Representing Rotations with Quaternions
We will compute a rotation
about the unit vector, u by an angle theta.
The quaternion that computes this rotation is
We will represent a point p in space by the quaternion P=(0,p)
We compute the desired rotation of that point by this formula:
You may want to confirm that q is a unit quaternion,
since that will allow us to use the fact that
the inverse of q is q' if q is a unit quaternion.
Concatenating Rotations
Suppose we want to perform two rotations on an object.
This may come up in a manipulation interface where each movement of
the mouse adds another rotation to the current object pose.
This is very easy and numerically stable with a quaternion
representation.
Suppose q1 and q2 are unit quaternions representing two
rotations. We want to perform q1 first and then q2.
To do this, we apply q2 to the result of q1, regroup
the product using associativity, and find that the composite rotation
is represented by the quaternion q2*q1.
Therefore, the only time we need to compute the matrix is when we want
to transform the object. For other operations we need only look at
the quaternions. A matrix product requires many more operations than
a quaternion product so we can save a lot of time and preserve more
numerical accuracy with quaternions than with matrices.
For the mathematically inclined
(ref: Topics in Algebra by I.N.Herstein pp120-126)
The complex numbers are a field.
This means that:
- Complex numbers have an addition operation and a multiplication operation.
- Both operations are commutative and associative, i.e.
z1+z2 = z2+z1 and
(z1+z2)+z3
= z1+(z2+z3)
z1z2 = z2z1 and
(z1z2)z3
= z1(z2z3)
- The addition operation has an identity, 0, and an inverse, z+(-z)=0.
- The multiplication operation has an identity, 1, and an
inverse for all numbers but z=0, z(z-1)=1,
or z-1=1/z.
The inverse of a complex number is z-1=z'/(zz').
- The operations are distributive
(z1+z2)z3
= z1z3+z2z3
and
z3(z1+z2)
= z3z1+z3z2
Essentially what this says is that complex numbers act just like
numbers as we know them. We can add, multiply, subtract, and divide
them and we can use any of our standard algebraic manipulations.
Other examples of fields are the real numbers and the rational numbers.
The quaternions have all of the same properties except that
quaternion multiplication is not commutative.
In general,
q1*q2 != q2*q1.
We call this structure a division ring.
This means that we can do any kind of arithmetic with quaternions as
long as we are careful to note the order of multiplication.
The inverse of a quaternion refers to the multiplicative
inverse (or 1/q) and can be computed by q-1=q'/(q*q')
The set of all 3x3 matrices (or all 4x4 matrices) with
standard matrix addition and multiplication is a ring but not
a division ring, because not all matrices have inverses.
The set of all 3D vectors with vector addition and
cross-product is also a ring, but not a division ring, since the
set of vectors does not even have a multiplicative identity.
(Can you think of a vector v such that u x v = u for all u?)
A Matrix Representation for Quaternion Multiplication
(ref: Graphics Gems II, pp351-354 - warning: the final matrix is transposed!)
We can use the rules above to compute the product of two quaternions.
If you examine each term in this product, you can see that
each term depends linearly on the coefficients for q1.
Also each term depends linearly on the coefficients for q2.
So, we can write the product of two quaternions in terms of a matrix
multiplication.
When the matrix Lrow(q1) multiplies a row vector q2, the
result is a row vector representation for q1 * q2.
When the matrix Rrow(q2) multiplies a row vector q1, the
result is also a row vector representation for q1 * q2.
Computing Rotation Matrices from Quaternions
Now we have all the tools we need to use quaternions to generate a
rotation matrix for the given rotation.
We have a matrix form for left-multiplication by q
and a matrix form for right-multiplication by q'.
The resulting rotation matrix is the product of these two matrices.
q * P * q'
= q * ( P * q')
= q * (P Rrow(q'))
= (P Rrow(q')) Lrow(q)
= P (Rrow(q') Lrow(q))
= P Qrow(q)
So using this matrix, we could compute Protated another way:
Euler Angles
In many fields Euler angles are used to represent rotations.
Any rotation can be broken down into a series of three rotations about
the major axes.
We can simulate any arbitrary rotation with one rotation about the
x-axis, one about the y-axis, and then one about the z-axis.
For example, consider an airplane pointing along the x-axis with
the z-axis pointing up.
We can represent any pose by
- the "roll" about the x-axis along the plane,
- the "pitch" about the y-axis which extends along the wings of
the plane,
- and the "yaw" or "heading" about the z-axis
as a vector (roll,pitch,yaw).
This representation is useful and intuitive in some cases,
such as this one, but Euler angles have some drawbacks.
- There is no universal standard for Euler rotations.
Different fields use different sequences of Euler angles, for
example some physicists use z-y-z as opposed to the
x-y-z system described above.
- Since any rotation can be represented by either a set of
Euler angles or a matrix, we should be able to convert between
them. However, computing the required angles is expensive and
can introduce errors.
- Interpolation between two poses represented in this system does
not follow the great arc between the rotations, but involves
wild swings about the canonical axes.
- Graphics Gems II, pp. 351-354, pp. 377-380
- Computer Graphics, Hearn & Baker, pp. 419-420 & pp. 617-618
- "Animating Rotation with Quaternion Curves", Ken Shoemake,
Computer Graphics V.19 N.3, 1985
Last modified: Wed Feb 2 11:01:38 2000
Laura Downs (laura@cs.berkeley.edu)