Coordinate Systems and Transformations: Topics
Coordinate Systems and Transformations: Topics
1
v =
1
v
1
+
2
v
2
+
3
v
3
2
Suppose we want to (linearly) change the basis vectors v
1
, v
2
, v
3
to u
1
, u
2
,
u
3
. We express the new basis vectors as combinations of the old ones,
u
1
= a
11
v
1
+ a
12
v
2
+ a
13
v
3
,
u
2
= a
21
v
1
+ a
22
v
2
+ a
23
v
3
,
u
3
= a
31
v
1
+ a
32
v
2
+ a
33
v
3
,
and thus obtain a 3 3 change of basis matrix
M =
a
11
a
12
a
13
a
21
a
22
a
23
a
31
a
32
a
33
.
If the two representations of a given vector v are
v = a
T
v
1
v
2
v
3
, and v = b
T
u
1
u
2
u
3
,
where a = (
1
2
3
)
T
and b = (
1
2
3
)
T
, then
a
T
v
1
v
2
v
3
= v = b
T
u
1
u
2
u
3
= b
T
M
v
1
v
2
v
3
,
which implies that
a = M
T
b and b = (M
T
)
1
a.
3
This 3D coordinate system is not, however, rich enough for use in computer
graphics. Though the matrix M could be used to rotate and scale vectors,
it cannot deal with points, and we want to be able to translate points
(and objects).
In fact an arbitary ane transformation can be achieved by multiplication
by a 3 3 matrix and shift by a vector. However, in computer graphics we
prefer to use frames to achieve the same thing.
A frame is a richer coordinate system in which we have a reference point
P
0
in addition to three linearly independent basis vectors v
1
, v
2
, v
3
, and we
represent vectors v and points P, dierently, as
v =
1
v
1
+
2
v
2
+
3
v
3
,
P = P
0
+
1
v
1
+
2
v
2
+
3
v
3
.
We can use vector and matrix notation and re-express the vector v and
point P as
v = (
1
2
3
0 )
v
1
v
2
v
3
P
0
, and P = (
1
2
3
1 )
v
1
v
2
v
3
P
0
.
The coecients
1
,
2
,
3
, 0 and
1
,
2
,
3
, 1 are the homogeneous coor-
dinates of v and P respectively.
4
Change of frames
Suppose we want to change from the frame (v
1
, v
2
, v
3
, P
0
) to a new frame
(u
1
, u
2
, u
3
, Q
0
). We express the new basis vectors and reference point in
terms of the old ones,
u
1
= a
11
v
1
+ a
12
v
2
+ a
13
v
3
,
u
2
= a
21
v
1
+ a
22
v
2
+ a
23
v
3
,
u
3
= a
31
v
1
+ a
32
v
2
+ a
33
v
3
,
Q
0
= a
41
v
1
+ a
42
v
2
+ a
43
v
3
+ P
0
,
and thus obtain a 4 4 matrix
M =
a
11
a
12
a
13
0
a
21
a
22
a
23
0
a
31
a
32
a
33
0
a
41
a
42
a
43
1
.
Similar to 3D vector coordinates, we suppose now that a and b are the
homogeneous representations of the same point or vector with respect to
the two frames. Then
a
T
v
1
v
2
v
3
P
0
= b
T
u
1
u
2
u
3
Q
0
= b
T
M
v
1
v
2
v
3
P
0
,
which implies that
a = M
T
b and b = (M
T
)
1
a.
5
Ane transformations
The transposed matrix
M
T
=
a
11
a
21
a
31
a
41
a
12
a
22
a
32
a
42
a
13
a
23
a
33
a
43
0 0 0 1
,
simply represents an arbitrary ane transformation, having 12 degrees
of freedom. These degrees of freedom can be viewed as the nine elements
of a 3 3 matrix plus the three components of a vector shift.
The most important ane transformations are rotations, scalings,
and translations, and in fact all ane transformations can be expressed
as combinaitons of these three.
Ane transformations preserve line segments. If a line segment
P() = (1 )P
0
+ P
1
is expressed in homogeneous coordinates as
p() = (1 )p
0
+ p
1
,
with respect to some frame, then an ane transformation matrix M sends
the line segment P into the new one,
Mp() = (1 )Mp
0
+ Mp
1
.
Similarly, ane transformations map triangles to triangles and tetrahedra
to tetrahedra. Thus many objects in OpenGL can be transformed by trans-
forming their vertices only.
6
Rotation, translation, scaling, and shear
Translation is an operation that displaces points by a xed distance in a
given direction. If the displacement vector is d then the point P will be
moved to
P
= P + d.
We can write this equation in homeogeneous coordinates as
p
= p +d,
where
p =
x
y
z
1
, p
, d =
z
0
.
so that
x
= x +
x
, y
= y +
y
, z
= z +
z
.
So the transformation matrix T which gives p
= Tp is clearly
T = T(
x
,
y
,
z
) =
1 0 0
x
0 1 0
y
0 0 1
z
0 0 0 1
,
called the translation matrix. One can check that the inverse is
T
1
(
x
,
y
,
z
) = T(
x
,
y
,
z
).
7
Rotation depends on an axis of rotation and the angle turned through.
Consider rst rotation in the plane, about the origin. If a point (x, y) with
coordinates
x = cos , y = sin,
is rotated through an angle , then the new position is (x
, y
), where
x
= cos( + ), y
= sin( + ).
Expanding these latter expressions, we nd
x
= xcos y sin,
y
= xsin + y cos ,
or
x
cos sin
sin cos
x
y
.
x
y
x
y
8
Thus the three rotation matrices corresponding to rotation about the z, x,
and y axes in lR
3
are:
R
z
= R
z
() =
cos sin 0 0
sin cos 0 0
0 0 1 0
0 0 0 1
,
R
x
= R
x
() =
1 0 0 0
0 cos sin 0
0 sin cos 0
0 0 0 1
,
R
y
= R
y
() =
cos 0 sin 0
0 1 0 0
sin 0 cos 0
0 0 0 1
.
All three give positive rotations for positive with respect to the right
hand rule for the axes x, y, z. If R = R() denotes any of these matrices,
its inverse is clearly
R
1
() = R() = R
T
().
Translations and rotations are examples of solid-body transforma-
tions: transformations which do not alter the size or shape of an object.
9
Scaling can be applied in any of the three axes independently. If we send
a point (x, y, z) to the new point
x
=
x
x, y
=
y
y, z
=
z
z,
then the corresponding matrix becomes
S = S(
x
,
y
,
z
) =
x
0 0 0
0
y
0 0
0 0
z
0
0 0 0 1
,
with inverse
S
1
(
x
,
y
,
z
) = S(1/
x
, 1/
y
, 1/
z
).
If the scaling factors
x
,
y
,
z
are equal then the scaling is uniform:
objects retain their shape but alter their size. Otherwise the scaling is
non-uniform and the object is deformed.
10
Shears can be constructed from translations, rotations, and scalings, but
are sometimes of independent interest.
x
y
z
x
y
z
A shear in the x direction is dened by
x
= x + (cot )y, y
= y, z
= z,
for some scalar a. The corresponding shearing matrix is therefore
H
x
() =
1 cot 0 0
0 1 0 0
0 0 1 0
0 0 0 1
,
with inverse
H
1
x
() = H
x
().
11
Rotation about an arbitrary axis
How do we nd the matrix which rotates an object about an arbitrary point
p
0
and around a direction u = p
2
p
1
, through an angle ?
x
y
z
p
0
p
1
p
2
u
The answer is to concatenate some of the matrices we have already
developed. We will assume that u has length 1. The rst step is to use
translation to reduce the problem to that of rotation about the origin:
M = T(p
0
) RT(p
0
).
To nd the rotation matrix R for rotation around the vector u, we rst
align u with the z axis using two rotations
x
and
y
. Then we can apply
a rotation of around the z-axis and afterwards undo the alignments, thus
R = R
x
(
x
)R
y
(
y
)R
z
()R
y
(
y
)R
x
(
x
).
12
It remains to calculate
x
and
y
from u = (
x
,
y
,
z
). The rst rotation
R
x
(
x
) will rotate the vector u around the x axis until it lies in the y = 0
plane. Using simple trigonometry, we nd that
cos
x
=
z
/d, sin
x
=
y
/d,
where d =
2
y
+
2
z
, so, without needing
x
explicitly, we nd
R
x
(
x
) =
1 0 0 0
0
z
/d
y
/d 0
0
y
/d
z
/d 0
0 0 0 1
.
In the second alignment we nd
cos
y
= d, sin
y
=
x
,
and so
R
y
(
y
) =
d 0
x
0
0 1 0 0
x
0 d 0
0 0 0 1
.
13
Example in OpenGL
The following OpenGL sequence sets the model-view matrix to represent a
45-degree rotation about the line through the origin and the point (1, 2, 3)
with a xed point of (4, 5, 6):
void myinit(void)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(4.0, 5.0, 6.0);
glRotatef(45.0, 1.0, 2.0, 3.0);
glTranslatef(-4.0, -5.0, -6.0);
}
OpenGL concatenates the three matrices into the single matrix
C = T(4, 5, 6) R(45, 1, 2, 3) T(4, 5, 6).
Each vertex p specied after this code will be multiplied by C to yield q,
q = Cp.
14
Spinning the Cube
The following program rotates a cube, using the three buttons of the mouse.
There are three callbacks:
glutDisplayFunc(display);
glutIdleFunc(spincube);
glutMouseFunc(mouse);
The display callback sets the model-view matrix with three angles deter-
mined by the mouse callback, and then draws the cube (see Section 4.4 of
the book).
void display()
{
glClear(GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(theta[0], 1.0, 0.0, 0.0);
glRotatef(theta[1], 0.0, 1.0, 0.0);
glRotatef(theta[2], 0.0, 0.0, 1.0);
colorcube();
glutSwapBuffers();
}
15
The mouse callback selects the axis of rotation.
void mouse(int btn, int state, int x, int y)
{
if(btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0;
if(btn == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1;
if(btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2;
}
The idle callback increments the angle of the chosen axis by 2 degrees.
void spincube()
{
theta[axis] += 2.0;
if(theta[axis] >= 360.0) theta[axis] -= 360.0;
glutPostRedisplay();
}
16
A little bit about quarternions
Quarternions oer an alternative way of describing rotations, and have been
used in hardware implementations.
In the complex plane, rotation through an angle can be expressed as
multiplication by the complex number
e
i
= cos + i sin.
This rotates a given point re
i
in the complex plane to the new point
re
i
e
i
= re
i(+)
.
Analogously, quarternions can be used to elegantly describe rotations in
three dimensions. A quarternion consists of a scalar and a vector,
a = (q
0
, q
1
, q
2
, q
3
) = (q
0
, q).
We can write the vector part q as
q = q
1
i + q
2
j + q
3
k,
where i, j, k play a similar role to that of the unit vectors in lR
3
, and obey
the properties
i
2
= j
2
= k
2
= 1,
and
ij = k = ji, jk = i = kj, ki = j = ik.
17
These properties imply that the sum and multiple of two quarternions a =
(q
0
, q) and b = (p
0
, p) are
a + b = (q
0
+ p
0
, q +p),
and
ab = (q
0
p
0
q p, q
0
p + p
0
q +q p).
The magnitude and inverse of a are
|a| =
q
2
0
+q q, a
1
=
1
|a|
(q
0
, q).
Rotation. Suppose now that v is a unit vector in lR
3
, and let p be an
arbitrary point. Denote by p
= (0, p
),
we claim that
p
= r
1
pr.
18
Lets check that this is correct! Following the rule for multiplication, a little
computation shows that p
), and that
p
= cos
2
2
p + sin
2
2
(v p)v + 2 sin
2
cos
2
(v p) + sin
2
2
v (v p).
But using the identity
p = (v p)v v (v p), (1)
and the multiple-angle formulas for cos and sin, this simplies to
p