Matrix Transformations
Reference:
•
“3D Math Primer for Graphics and Game Development, 2
nd
ed.”
•
Chapter 5
•
Chapter 6.x
•
http://www.teamten.com/lawrence/graphics/homogeneous/
Transform Matrices (4.2 + 5.1)
•
Take this vector:
•
Recall, if we think of this as a point, it means:
–
Start at the origin.
–
Move 0.5 units in the opposite direction of the
world
x

axis.
–
Move 3 units in the direction of the
world
y

axis
–
Move 9 units in the direction of the
world
z

axis.
•
Said mathematically:
9
3
5
.
0
v
1
0
0
*
9
0
1
0
*
3
0
0
1
*
5
.
0
v
9
3
5
.
0
*
1
0
0
0
1
0
0
0
1
Expressing this as a matrix*vector expression:
(assuming +x = [1,0,0], +y = [0,1,0], +z = [0,0,1])
Transform Matrices, cont.
•
Some notation:
–
Let
𝑤𝑜 𝑙
,
𝑤𝑜 𝑙
, and
𝑤𝑜 𝑙
be the
basis vectors
of a matrix M.
–
M is a
transformation matrix
(because
we’re using it to “transform” a vector)
•
Although technically in this case, the matrix
doesn’t actually change the original vector.
–
The first
b.v
., , represents the world +x
direction.
–
The second
b.v
., , represents the
world +y direction
.
–
The third
b.v
., , represents the world +z
direction.
v
M
v
v
M
*
9
3
5
.
0
1
0
0
0
1
0
0
0
1
0
0
1
0
1
0
1
0
0
Transform Matrices, cont.
•
Big deal, right?
–
Yes…but…what if we express that point relative to a “non

standard” coordinate system?
–
In other words, use a “non

standard” set of basis vectors.
•
Example:
–
Let’s assume +y and +z are [0,1,0] and [0,0,1], respectively
(as before)
–
But…let’s make +x be [

1,0,0]
–
Now the matrix is:
1
0
0
0
1
0
0
0
1
MysteryMat
Transform Matrices, cont.
•
Let’s watch what happens as we “transform” a few
points with this matrix…
1
0
0
0
1
0
0
0
1
MysteryMat
2
1
5
1
p
3
4
1
2
p
0
6
3
3
p
2
1
5
*
1
'
1
p
MysteryMat
p
3
4
1
*
2
'
2
p
MysteryMat
p
0
6
3
*
3
'
3
p
MysteryMat
p
p3
P3’
p2
P2’
p1
P1’
MysteryMat
does a x

axis
“Mirror”.
Transform Matrices, cont.
•
Let’s try to apply this idea of basis vectors and
local coordinate systems to a new matrix (4.2
in the book)
•
What does this matrix do?
–
Method1: Try to plot a few points…OK, but not the
best solution (
we might not pick points that show the transform
in action. Also, it takes a lot of time!
)
–
Method2: Look at the local coordinate system
defined by M…(see the next slide)
1
0
0
0
2
1
0
1
2
M
Transform Matrices, cont.
1
0
0
0
2
1
0
1
2
M
0
1
2
0
b
0
2
1
1
b
1
0
0
2
b
worldX
worldY
b0 =
localX
b1 =
localY
The
localX
and
localY
axes are rotated (~27 degrees)
and
scaled
from their
worldX
and
worldY
counter

parts.
localZ
=
worldZ
, so
this transformation leaves the z

values alone.
Imagine “deforming”
worldX
into b0,
worldY
into b1,
worldZ
into b2.
This is the same “deformation” the matrix will apply to all points
that we transform using this matrix.
Transform Matrices, cont.
1
0
0
0
2
1
0
1
2
M
worldX
worldY
b0 =
localX
b1 =
localY
worldX
worldY
Let’s say these are defined in
world space.
0
6
.
0
0
,
0
1
25
.
0
,
0
6
.
0
5
.
0
,
0
0
5
.
0
,
0
0
0
4
3
2
1
0
p
p
p
p
p
0
2
.
1
6
.
0
'
,
0
25
.
2
5
.
0
'
,
0
7
.
1
4
.
0
'
,
0
5
.
0
1
'
,
0
0
0
'
4
3
2
1
0
p
p
p
p
p
When we multiply (transform) them by M (just as we did on the last slide), these points are
transformed
in the same way the world axes are transformed into the basis vectors of M
.
Rotation Transform (5.1)
•
Now the problem of defining new transforms
just boils down to defining the (transformed)
basis vectors.
•
Let’s apply this to the problem of creating a
matrix which rotates
θ
degrees around the z
axis.
Rotation Transform (5.1), cont.
•
Put yourself in the direction of +z (looking at
the origin):
worldX
worldY
θ
θ
Now, let’s define the basis vectors…
Sin(
θ
)
Cos(
θ
)
0
)
sin(
)
cos(
localX

Sin(
θ
)
Cos(
θ
)
0
)
cos(
)
sin(
localY
1
0
0
localZ
Rotation Transformation (5.1), cont.
•
So, if we collect the 3 local vectors (basis
vectors into a matrix), we have…
•
We can similarly come up with matrices for x

axis and y

axis rotations…
1
0
0
0
)
cos(
)
sin(
0
)
sin(
)
cos(
Z
Rot
)
cos(
)
sin(
0
)
sin(
)
cos(
0
0
0
1
X
Rot
)
cos(
0
)
sin(
0
1
0
)
sin(
0
)
cos(
Y
Rot
Note: These matrices are the transpose of the matrices in the book (since the
author uses a Left

handed system)
Rotations
•
I'll show you a simpler (
imo
) version of 5.1.3
(rotation about an arbitrary axis) with matrix
concatenations.
•
Similarly for 5.2.2 (scaling about an arbitrary
axis)
Scale Transform (5.2)
•
The scale transform matrix is even simpler:
worldX
worldY
worldZ
Let’s say we want to scale:
2x in the x

direction
1x in the y

direction
½ in the z

direction
localX
localY
localZ
5
.
0
0
0
0
1
0
0
0
2
S
sz
sy
sx
S
0
0
0
0
0
0
In general:
sx
in the x

direction
sy
in the y

direction
sz
in the z

direction
If
sx
=
sy
=
sz
, the operation
is called a
universal scale.
Mirror (Reflection) and Shear (5.4 +
5.5)
•
[Do it on the board]
Translation (6.4)
•
Let’s say we want to develop a matrix for
translation (let’s say by amounts
tx
,
ty
, and
tz
).
–
Seems simple, right?
–
Wrong!
•
Try it
Translation (6.4)
•
Try it another way:
•
Try to come up with values for a

i
such that
all
points
transformed by the matrix are moved
by +4(x), +5(y), +6(z):
•
You can’t do it!
i
h
g
f
e
d
c
b
a
T
3
2
1
*
3
7
5
T
7
9
2
*
13
14
2
T
and
Translation
•
Up
to this point we've been looking at
linear
transformations
(you can do these with a 3x3 (for
3d) matrix
).
–
These
only
transform points
about the origin
.
•
Translation is an example of an
affine
transformation
(a linear transform followed by a
linear transform).
–
You need to look at the problem in a higher
dimension…
–
…enter
homogeneous space
!
Translation, cont.
•
But, by using so

called
homogeneous
coordinates
, we can.
•
To visualize this, we'll look at a (x

)shear
matrix in 2d.
1
ℎ
0
1
•
Suppose
shearX
is 0.5:
Translations, cont.
•
Represent 2d points in 3d space, with w as 1.0
x
y
x
y
w
2D
3D
Translations, cont.
•
Now transform by a 3d shear in the x direction
by 2.0 units
x
y
x
y
w
2D
3D
1
0
2
0
1
0
0
0
1
Tanslations
, cont.
•
So…a shear in 3d, produces a
translation
in
2d.
•
We can do a translation in 3d by shearing in
4d.
–
We can't really visualize it
–
you just have to trust
the math
1
0
0
0
1
0
0
0
1
0
0
0
1
tz
ty
tx
T
Translation, cont.
•
Note, if you put a w of 0, the translation doesn't
affect the vector
–
Useful for vectors.
•
W should be:
–
1
if this is a point.
–
0
if this is a vector (direction + magnitude).
•
We'll see another application of this in
perspective projections.
–
In this case we'll end up with w values
after the
transformation
that are not 0 nor 1.
–
In this case we'll have to do a w

divide (homogeneous
divide)
Translation, cont.
•
Now, we can do translation, by using a 4x4
translation matrix like this:
•
Take our problem cases from before:
1
0
0
0
1
0
0
0
1
0
0
0
1
tz
ty
tx
T
1
3
2
1
*
1
0
0
0
6
1
0
0
5
0
1
0
4
0
0
1
1
3
7
5
1
7
9
2
*
1
0
0
0
6
1
0
0
5
0
1
0
4
0
0
1
1
13
14
2
Because it’s a point
It’s still a point
Translation, cont.
•
An extra bonus:
0
0
0
4
*
1
0
0
0
6
1
0
0
5
0
1
0
4
0
0
1
0
0
0
4
This one’s a vector
It’s still a vector
Note how the vector is unchanged.
Remember that you can re

draw a
vector anywhere and it still has the
same length and magnitude
–
here’s
proof!
4x4 Transformation matrices
•
OK, so now that we’re using 4x4 matrices for
translation, what about all the other matrices?
•
You can use 4x4 matrix, with the “old” 3x3
(linear) matrix embedded in the upper

left.
–
Or…think of it this affine transformation as a linear
transform (the 3x3 upper

left part) and a 0

vector
translation afterwards.
•
Example:
RotZ
now becomes:
1
0
0
0
0
1
0
0
0
0
)
cos(
)
sin(
0
0
)
sin(
)
cos(
Z
Rot
Matrix Inverse (6.2)
•
The inverse of the matrix ( )has the property:
•
If M is a transform matrix, its inverse is the
opposite transform.
•
In general, the inverse is non

trivial to compute.
–
But in graphics…the "building block" matrices we've
seen so far do have an easy inverse.
I
M
M
1
*
1
M
Matrix Inverse, cont.
1
0
0
0
1
0
0
0
1
0
0
0
1
1
tz
ty
tx
T
1
0
0
0
0
/
1
0
0
0
0
/
1
0
0
0
0
/
1
1
sz
sy
sx
S
Rotation matrices are an example of an
orthonormal
matrix
. The basis vectors of these
matrices have unit length and are perpendicular to each other.
Q: How can you determine if three axes: p, q, and r are perpendicular?
A: p

dot

q, p

dot

r, and q

dot

r are all 0.
The special thing about
orthonormal
matrices is:
T
M
M
1
Transform Matrix Concatenation (5.6)
•
Suppose you want to apply the following
transforms to an object (in this order):
–
Translate (10,0,0)
–
Scales (2, 2, 2)
–
Rotate 15 degrees around the z

axis.
Transform Matrix Concatenation (8.7)
•
Numerically:
•
After Multiplying by the translation (10,0,0) matrix:
•
After multiplying by the scale (2,2,2) matrix:
•
After multiplying by the rotation of 15 degrees around the
z axis:
1
0
5
.
3
2
,
1
0
6
0
,
1
0
5
.
3
2
,
1
0
0
2
,
1
0
0
2
4
3
2
1
0
p
p
p
p
p
1
0
5
.
3
8
'
,
1
0
6
10
'
,
1
0
5
.
3
12
'
,
1
0
0
12
'
,
1
0
0
8
'
4
3
2
1
0
p
p
p
p
p
1
0
0
0
0
1
0
0
0
0
1
0
10
0
0
1
T
1
0
7
16
'
'
,
1
0
12
20
'
'
,
1
0
7
24
'
'
,
1
0
0
24
'
'
,
1
0
0
16
'
'
4
3
2
1
0
p
p
p
p
p
1
0
9
.
10
643
.
13
'
'
'
,
1
0
767
.
16
213
.
16
'
'
'
,
1
0
973
.
12
37
.
21
'
'
'
,
1
0
212
.
6
182
.
23
'
'
'
,
1
0
14
.
4
455
.
15
'
'
'
4
3
2
1
0
p
p
p
p
p
1
0
0
0
0
2
0
0
0
0
2
0
0
0
0
2
S
1
0
0
0
0
1
0
0
0
0
)
15
cos(
)
15
sin(
0
0
)
15
sin(
)
15
cos(
R
Transform Matrix Concatenation, cont.
•
You can get the same
net
effect by
concatenating these 3 transforms.
1
0
0
0
0
1
0
0
0
0
1
0
10
0
0
1
*
1
0
0
0
0
2
0
0
0
0
2
0
0
0
0
2
*
1
0
0
0
0
1
0
0
0
0
)
15
cos(
)
15
sin(
0
0
)
15
sin(
)
15
cos(
*
*
T
S
R
C
1
0
0
0
0
2
0
0
0
0
2
0
20
0
0
2
*
1
0
0
0
0
1
0
0
0
0
)
15
cos(
)
15
sin(
0
0
)
15
sin(
)
15
cos(
1
0
0
0
0
2
0
0
18
.
5
0
93
.
1
52
.
0
32
.
19
0
52
.
0
93
.
1
1
0
0
0
0
2
0
0
)
15
sin(
*
20
0
)
15
cos(
*
2
)
15
sin(
*
2
)
15
cos(
*
20
0
)
15
sin(
*
2
)
15
cos(
*
2
VERY IMPORTANT: In a right

handed system, multiply in the
reverse
order you want to
apply the transform (In a left

handed system, you do it in the forward order).
You
won’t
get the same composite matrix if you don’t follow this rule.
Transform Matrix Concatenation, cont.
•
Let’s test it.
1
0
5
.
3
2
,
1
0
6
0
,
1
0
5
.
3
2
,
1
0
0
2
,
1
0
0
2
4
3
2
1
0
p
p
p
p
p
1
0
0
0
0
2
0
0
18
.
5
0
93
.
1
52
.
0
32
.
19
0
52
.
0
93
.
1
C
Transform each of the points by C, and we get…
1
0
9
.
10
643
.
13
'
,
1
0
767
.
16
213
.
16
'
,
1
0
973
.
12
37
.
21
'
,
1
0
212
.
6
182
.
23
'
,
1
0
14
.
4
455
.
15
'
4
3
2
1
0
p
p
p
p
p
Which is the same points we got by transforming by the individual matrices!
Big Deal, right?
No!
To see why, we’ll need to analyze the costs…(see next slide)
Matrix multiplication costs
•
Matrix4x4 * Matrix4x4 uses 16 dot

products of Vector4’s.
•
Matrix4x4 * Vector4 uses 4 dot

products of Vector4’s.
•
Each Vector4

dot

Vector4 operation involves:
–
4 multiplies
–
3 adds
•
So…
–
Matrix4x4 * Matrix4 = 64 multiplies, 48 adds
–
Matrix4x4 * Vector4 = 16 multiplies, 12 adds
•
Note: The GPU has specialized circuitry for doing this
–
often much more quickly than the CPU, but it still takes
time.
Analyzing the costs
•
Let’s say we have 3 transform matrices we want to
apply: M1, M2, M3 to n points.
–
MethodI
: Apply the transforms individually.
for
i
in range(n):
p1 = M1 * points[
i
]
p2 = M2 * p1
p3 = M3 * p2
# The transformed point
–
MethodII
: Create a combined transform. Then use it to
transform all n points.
C = M3 * M2 * M1
for
i
in range(n):
p3 = C * points[
i
] # The transformed point
Analyzing the costs, cont
•
What are the costs of the two methods?
–
MethodI
:
•
N * 3 * (Matrix4x4 times Vector4 cost)
•
Which is:
–
N * 3 * 16 multiplies
–
N * 3 * 12 adds
–
MethodII
:
•
2 * (Matrix4x4 times Matrix4x4 cost) + N * (Matrix4x4
times Vector4 cost)
•
Which is:
–
2 * 64 + N * 16 multiplies
–
2 * 48 + N * 12 adds
•
Which is better?
Analyzing the costs, cont
•
Answer: It depends on N?
–
MethodI
:
•
N * 3 * 16 multiplies
•
N * 3 * 12 adds
–
MethodII
:
•
2 * 64 + N * 16 multiplies
•
2 * 48 + N * 12 adds
Comparison of Method1
vs
Method2 (using 3 transforms)
Method1
Method2
Verdict?
How much better?
N
mult
add
mult
add
1
48
36
144
108
Method1
33.3%
1/3 of Method2's mult's
2
96
72
160
120
Method1
66.7%
2/3 of Method2's mult's
4
192
144
192
144
Draw
8
384
288
256
192
Method2
66.7%
2/3 of Method1's mult's
16
768
576
384
288
Method2
50.0%
1/2 of Method1's mult's
32
1536
1152
640
480
Method2
41.7%
64
3072
2304
1152
864
Method2
37.5%
128
6144
4608
2176
1632
Method2
35.4%
256
12288
9216
4224
3168
Method2
34.4%
512
24576
18432
8320
6240
Method2
33.9%
1024
49152
36864
16512
12384
Method2
33.6%
2048
98304
73728
32896
24672
Method2
33.5%
4096
196608
147456
65664
49248
Method2
33.4%
8192
393216
294912
131200
98400
Method2
33.4%
16384
786432
589824
262272
196704
Method2
33.3%
32768
1572864
1179648
524416
393312
Method2
33.3%
65536
3145728
2359296
1048704
786528
Method2
33.3%
131072
6291456
4718592
2097280
1572960
Method2
33.3%
262144
12582912
9437184
4194432
3145824
Method2
33.3%
524288
25165824
18874368
8388736
6291552
Method2
33.3%
1048576
50331648
37748736
16777344
12583008
Method2
33.3%
2097152
100663296
75497472
33554560
25165920
Method2
33.3%
4194304
201326592
150994944
67108992
50331744
Method2
33.3%
1/3 of Method1's
mults
Note: the 3 above and in the
1/3 below is because we’re
doing 3 transforms.
If concatenating more than p
matrices, we would have 1/p
of the cost of
MethodI
.
Verdict: Matrix concatenation, except
for very small meshes, is
always
faster
than individual transforms.
Rotation Transform, not around the origin
•
A good example of matrix concatenation.
•
Normally, a z

axis rotation of 45 degrees on
this:
•
Produces this:
45
Rotation Transform, not around the origin, cont.
•
Rotation always happens about the
world
origin
.
•
But what if we want this:
•
To transform to this (rotate about a
local
origin
)?
45
Rotation Transform, not around the
origin, cont.
•
We can’t change how rotation works…
•
Hint: there are 3 (basic) transforms we need
to apply.
–
1) Translate the center of the object to the world
origin.
–
2) Rotate around the z

axis.
–
3) Translate the object by the
inverse
of Step1.
Rotation Transform, not around the
origin, cont.
•
1) Translate the center of the object to the world origin.
•
2) Rotate around the z

axis.
•
3) Translate the object by the
inverse
of Step1.
1
0
0
0
0
1
0
0
5
.
2
0
1
0
6
0
0
1
T
1
0
0
0
0
1
0
0
0
0
)
45
cos(
)
45
sin(
0
0
)
45
sin(
)
45
cos(
R
1
0
0
0
0
1
0
0
5
.
2
0
1
0
6
0
0
1
1
T
Rotation Transform, not around the origin, cont.
•
Now, we can combine these transforms into one composite
matrix.
•
Remember to multiply in the reverse order.
1
0
0
0
0
1
0
0
5
.
2
0
1
0
6
0
0
1
T
1
0
0
0
0
1
0
0
0
0
)
45
cos(
)
45
sin(
0
0
)
45
sin(
)
45
cos(
R
1
0
0
0
0
1
0
0
5
.
2
0
1
0
6
0
0
1
1
T
First step.
Second step.
Third step.
T
R
T
RotLocalZ
*
*
1
1
0
0
0
0
1
0
0
*
)
cos(
*
)
sin(
0
)
cos(
)
sin(
*
)
sin(
*
)
cos(
0
)
sin(
)
cos(
ty
ty
tx
tx
ty
tx
RotLocalZ
So…to rotate
θ
radians around the point (
tx,ty,tz
), use this matrix…
1
0
0
0
1
0
0
0
1
0
0
0
1
1
0
0
0
0
1
0
0
0
0
)
cos(
)
sin(
0
0
)
sin(
)
cos(
1
0
0
0
1
0
0
0
1
0
0
0
1
tz
ty
tx
tz
ty
tx
Rotation about an arbitrary axis.
•
Let’s say you want to rotate
θ
around a non

standard axis.
•
Let’s say the axis is the vector
0
7
.
0
4
.
0
Rotation about an arbitrary axis, cont.
•
Three steps:
–
Align the purple axis with (any) of the 3 (world)
axes
–
this is a rotation.
–
Rotate
θ
degrees about that world axis.
–
Undo the first rotation.
Rotation about an arbitrary axis, cont.
•
R1
= Align the purple axis with (any) of the 3
(world) axes
–
this is a rotation.
•
R2
= Rotate
θ
degrees about that world axis.
•
R3
= Undo the first rotation.
•
R
axis
= R3 * R2 * R1
•
R2 is easy
–
we’ve already seen that. Let’s
assume we’re rotating around the x

axis (but it
could be any of the 3 world axes)
•
R1 and R3
seem
hard unless you think in terms of
basis vectors…
Rotation about an arbitrary axis, cont.
•
Let’s take
R3 (
Undo the first rotation) first.
–
A rotation.
–
Rotates the
worldX
axis to the axis we’re rotating
about.
•
So…
localX
= given axis.
–
The other local (basis) vectors aren’t shown, but
they need to be perpendicular to
localX
.
•
It actually doesn’t matter what they are
–
just that
they’re perpendicular to
localX
.
•
Have we ever done anything like this before?
•
YES! With the camera’s local coordinate system in
the
raytracer
!
Rotation about an arbitrary axis, cont.
1.
LocalX
= b0=given axis.
(make sure it’s normalized)
2.
LocalY
= b1=
LocalX
x
(normalize it afterwards)
3.
LocalZ
= b2=
LocalX
x
LocalY
One problem: In Step2, if
LocalX
is
[0,1,0]T
,
the result of cross

product’ing
it
will be the zero vector (which means we won’t have a valid rotation
matrix). Because of this, you often use this rule instead:
0
1
0
1
0
0
0
1
0
LocalXx
LocalXx
LocalY
if
if
0
1
0
LocalX
0
1
0
LocalX
where
9
.
0
2
1
0
3
b
b
b
R
Rotation about an arbitrary axis, cont.
•
Now, for R1.
–
Recall: This is the opposite of R3.
–
In other words, the
inverse
.
–
Recall also: The inverse of an
orthonormal
matrix
is simply its transpose!
•
All rotation matrices are
orthonormal
(test it if you’re
not sure)
T
R
R
3
1
Comments 0
Log in to post a comment