Department of Computer Sciences
Graphics Spring 2013 (Lecture 7)
Viewing Systems II: Orientations and Quaternions
z
P
PP
PP
PP
PP
PP
q
P
The University of Texas at Austin
Department of Computer Sciences
Graphics Spring 2013 (Lecture 7)
z
w
PP
PP
PP
PP
PP
q
P
z = w
PP
PP
PP
PP
PP
z
q
P
P
PP
PP
PP
PP
PP
q
x
u
x
x
(c) Rotate about z so that
(a) Rotate about x so that (b) Rotate about y so that
u and v coincide with x
w lies in xz -plane.
w coincides with z .
and y .
Rotating a frame to coincide with the standard frame
The University of Texas at Austin
Department of Computer Sciences
Graphics Spring 2013 (Lecture 7)
z
z
PPP
PP
PP
PP
q
Smooth Interpolation of Frames
It is possible to perform any change of orientation about an arbitrary axis with three rotations,
one about each of the coordinate axes, by a triple of three angles, (x, y , z ). These define
a general rotation matrix, by composing the three basic rotations:
R(x, y , z ) = Rz (z )Ry (y )Rx(x ).
These three angles are called the Euler angles for the rotation. Thus, we can parameterize
The University of Texas at Austin
Department of Computer Sciences
Graphics Spring 2013 (Lecture 7)
any rotation in 3-space as triple of numbers, each in the range [0, 2].
With ca = cos(a) and sa = sin(a),
cy cz
sxsy cz cxsz
R(x, y , z ) =
c x sy c z + sx sz
0
c y sz
sx sy sz + c x c z
c x sy sz sx c z
0
sy
sx c y
cx cy
0
0
0
0
1
= Rz (z )Ry (y )Rx (x),
where Rx(x), Ry (y ) and Rz (z ) are the standard rotation matrices.
Given a point P represented as a homogeneous row vector, the rotation of P is given by P =
P R(x, y , z ). Animation between two rotations involves interpolating independently the
three angles x, y and z .
The University of Texas at Austin
Department of Computer Sciences
Graphics Spring 2013 (Lecture 7)
The standard rotation matrices are given by
1 0
0
0 cx sx
Rx(x) =
0 sx
cx
0 0
0
cy
0 sy
0
1 0
Ry (y ) =
sy 0 cy
0
0 0
cz sz 0
sz
cz
0
Rz (z ) =
0
0
1
0
0
0
The University of Texas at Austin
0
0
0
1
0
0
0
1
0
0
0
1
Department of Computer Sciences
Graphics Spring 2013 (Lecture 7)
Specifying Orientation using Quaternions are easier than Euler angles
glMatrixMode (GL_MODELVIEW);
glRotatef(45, 1, 0, 0);
glRotatef(45, 0, 1, 0);
glRotatef(45, 0, 0, 1);
glutAirPlane(1);
The University of Texas at Austin
Quaternion = -0.46, -0.21, -0.41, 0.75
Rotation Matrix =
0.55 0.82 0.06 0.00
-0.43 0.22 0.87 0.00
0.71 -0.51 0.48 0.00
0.00 0.00 0.00 1.00
6
Department of Computer Sciences
Graphics Spring 2013 (Lecture 7)
Quaternions:
i = j = k = 1 ij = k, jk = i, ki = j.
Combining these, it follows that ji = k, kj = i and ik = j . A quaternion is defined
to be a generalized complex number of the form
q = q0 + q1i + q2j + q3k.
We will see that quaternions bear a striking resemblance to our notation for angular
displacement. In particular, we can rewrite the quaternion notation in terms of a scalar and
vector as
q = (s, ~
u) = s + uxi + uy j + uz k.
Furthermore define the product of quaternions to be
q1q2 = (s1 s2 (~
u1 ~
u2 ),
s1 ~
u2 + s2~
u1 + ~
u1 ~
u2 ).
Define the conjugate of a quaternion q = (s, ~
u) to be q = (s, ~
u). Define the magnitude
The University of Texas at Austin
Department of Computer Sciences
Graphics Spring 2013 (Lecture 7)
of a quaternion to be the square root of this product:
|q|2 = q q = s2 + |~
u|2.
A unit quaternion is one of unit magnitude, |q| = 1. A pure quaternion is one with a 0
scalar component
p = (0, ~
v ).
Any quaternion of nonzero magnitude has a multiplicative inverse, which is
1
q.
2
|q|
Quaternion and Rotation:
Define the rotation operator
Rq (p) = qpq .
Rq (p) = (0, (s2 (~
u~
u))~
v + 2~
u(~
u~
v ) + 2s(~
u~
v )).
Unit quaternions can be shown to be isomorphic to orientations and given by
q = (cos , (sin )~
u),
The University of Texas at Austin
where |~
u| = 1.
8
Department of Computer Sciences
Graphics Spring 2013 (Lecture 7)
This is equivalent to a rotation by an angle 2 around the axis ~
u.
Thus, in summary, we encode points in 3-space as pure quaternions
p = (0, ~
v ),
and we encode a rotation by angle about a unit vector u as a unit quaternion
q = (cos(/2), sin(/2)~
u),
then the image of the point under this rotation is given by the vector part of the result of
the quaternion rotation operator Rq (p) = qpq 1.
The University of Texas at Austin
Department of Computer Sciences
Graphics Spring 2013 (Lecture 7)
y
x
Rotation example.
The University of Texas at Austin
10
Department of Computer Sciences
Graphics Spring 2013 (Lecture 7)
Composing Rotations:
Given two unit quaternions q and q , a rotation by q followed by a rotation by q is equivalent
to a single rotation by the product q = q q . That is,
Rq Rq = Rq
where q = q q.
This follows from the associativity of quaternion multiplication, and the fact that (qq )1 =
1
q 1q , as shown below.
Rq (Rq (p)) = q (qpq
)q
= (q q)p(q 1q 1)
= (q q)p(qq )1
= q pq
= Rq (p).
The University of Texas at Austin
11
Department of Computer Sciences
Graphics Spring 2013 (Lecture 7)
Matrices and Quaternions:
Given a unit quaternion
q = (cos(/2), sin(/2)~
u) = (w, (x, y, z))
what is the corresponding affine transformation (expressed as a rotation matrix). By simply
expanding the definition of Rq (p), it is not hard to show that the following (homogeneous)
matrix is equivalent
1 2y 2 2z 2
2xy 2wz
2xz + 2wy
0
2
2
2xy + 2wz
1
2x
2z
2yz 2wx
0
2
2
2xz 2wy
2yz + 2wx
1 2x 2y
0
0
0
0
1
To convert from an orthogonal rotation matrix to a unit quaternion, we observe that if
M = [mi,j ] is the affine transformation in homogeneous form,
2
trace(M ) = 4 4(x + y + z ) = 4w .
The University of Texas at Austin
12
Department of Computer Sciences
Graphics Spring 2013 (Lecture 7)
Once we have w, we can find the order quantities by cancelling symmetric terms:
m32 m23
,
x=
4w
m13 m31
y=
,
4w
m21 m12
z=
4w
The University of Texas at Austin
13
Department of Computer Sciences
Graphics Spring 2013 (Lecture 7)
Pitch
Rotation Axis = 1, 0, 0, 0
Rotation Angle = /4
Quaternion Vector = 0.382683 0.000000 0.000000 0.923880
The University of Texas at Austin
14
Department of Computer Sciences
Graphics Spring 2013 (Lecture 7)
Yaw
Rotation Axis = 0, 1, 0, 0
Rotation Angle = /4
Quaternion Vector = 0.000000 0.382683 0.000000 0.923880
The University of Texas at Austin
15
Department of Computer Sciences
Graphics Spring 2013 (Lecture 7)
Roll
Rotation Axis = 0, 0, 1, 0
Rotation Angle = /4
Quaternion Vector = 0.000000 0.000000 0.382683 0.923880
The University of Texas at Austin
16
Department of Computer Sciences
Graphics Spring 2013 (Lecture 7)
Additonal Examples
The following web-page contains a good quaternion intro and C++ source codes:
http://www.lboro.ac.uk/departments/ma/gallery/quat/intro.html
The University of Texas at Austin
17
Department of Computer Sciences
Graphics Spring 2013 (Lecture 7)
Reading Assignment and News
Before the next class please review Chapter 3 and its practice exercises, of the recommended
text.
(Recommended Text: Interactive Computer Graphics, by Edward Angel, Dave Shreiner, 6th
edition, Addison-Wesley)
Please track Blackboard for the most recent Announcements and Project postings related to
this course.
(http://www.cs.utexas.edu/users/bajaj/graphics2012/cs354/)
The University of Texas at Austin
18