Beruflich Dokumente
Kultur Dokumente
Mathematical Fundamentals
& Transformation Matrices
Matrix Math
1
9/13/2016
Matrix Math
1
rows x cols
Matrix Addition
1 2
3 4
1 2
3 4
9/13/2016
Matrix Addition
1 2
3 4
1 2
3 4
2
6
4
8
Scalar Multiplication
1 2
3 4
9/13/2016
Scalar Multiplication
1 2
3 4
5 10
15 20
Matrix Multiplication
1 2 3
4 5 6
1
4
7
2
5
8
3
6
9
9/13/2016
Matrix Multiplication
1 2 3
4 5 6
1
4
7
2
5
8
3
6
9
30
36
42
66
81
96
Matrix Multiplication
Inner dimensions must match
Result is outer dimension
Examples:
2x3 X 3x3 = 2x3
3x4 X 4x5 = 3x5
2x3 X 4x3 = cannot multiply
Question: Does AB = BA?
Hint: let A be 2x3 and B be 3x2
10
9/13/2016
11
Transformations
View a complex shape as a set of points
To transform the shape, one transforms each of the
points that define the shape
Major types of transformations:
Translation
Rotation
Scaling
12
9/13/2016
Translation
The equation to translate a single point
(x, y) by tx in the x direction and ty in the
y direction:
x = x + tx
y = y + ty
Resulting in the new point (x, y)
13
Translation
These equation can be represented using matrices:
P=
x
y
P =
x
y
T=
tx
ty
14
9/13/2016
Rotation
The equations to rotate a point through an angle, q
(ccw), around the origin:
x = x cos(q) y sin(q)
y = x sin(q) + y cos(q)
In matrix form:
R=
cos(q) -sin(q)
sin(q) cos(q)
15
Scaling
The equations to scale a point along the coordinate
axes:
x = x sx
y = y sy
Using matrix math: P = S P
16
9/13/2016
Scaling
Scaling is used to grow/shrink objects centered on the
origin
s numbers bigger than 1 grow objects
s numbers smaller than 1 shrink objects
uniform scaling: both s numbers the same
17
Transformation Sequences
In general you need to perform a sequence of
transformation:
This is a rotation followed by a translation
P = R P + T
Do a dimension check to make sure the math will work
out:
R is 2x2 and P is 2x1, thus R P is 2x1
R P is 2x1 and T is 2x1, thus P is 2x1
18
9/13/2016
Transformation Sequences
However, it is often more efficient if we can combine
operations into a single matrix calculation
How can this be done?
Here are the equations:
x = (x cos(q) y sin(q) ) + tx
y = (x sin(q) + y cos(q) ) + ty
19
Transformation Sequences
Put them into matrix form:
cos(q) sin(q) tx
sin(q) cos(q)
ty
x
y
1
10
9/13/2016
Homogeneous Coordinates
We resolve this using homogeneous coordinates
Lots of theory here, but
Basically boils down to all points being represented as
a triple (x, y, 1)
Thus, we need to fix our transformation matrix so that
P comes out as a triple
21
Homogeneous Coordinates
x
y
1
cos(q) -sin(q) tx
sin(q) cos(q) ty
0
0
1
x
y
1
22
11
9/13/2016
2D Transformation Matrices
Translation: T(x, y)
Rotation: R(q)
Scaling: S(x, y)
1 0 tx
0 1 ty
0 0 1
cos(q)
sin(q)
0
-sin(q)
cos(q)
0
0
0
1
sx 0 0
0 sy 0
0 0 1
23
Transformation Composition
1.
2.
3.
24
12
9/13/2016
25
26
13
9/13/2016
1 0 0
Reflection
Other
Transformations
about x axis (left)
about y axis (right)
Shear
along x axis (left)
along y axis (right)
Identity
0 -1 0
0 0 1
-1 0
0 1
0 0
1 shx 0
0 1 0
0 0 1
1 0 0
shy 1 0
0 0 1
1
0
0
0
1
0
0
0
1
0
0
1
27
14
9/13/2016
3D Transformations
Ill be using a right-handed coordinate system
Positive z-axis is sticking out of the screen
Dashed lines will be used
for lines that stick into
the screen
29
3D Transformations
Points now have 3 coordinates (x, y, z)
Written in homogeneous coordinates as (x, y, z, 1)
Since the points are now 4x1 matrices, the
transformation matrices need to be 4x4
30
15
9/13/2016
3D Translation
T(x, y, z):
tx
ty
tz
31
3D Rotation
3D rotation is a bit harder than in 2D
In 2D we always rotated about the z-axis
Now we have a choice of 3 axes, Rx, Ry, Rz
Rotation is still ccw about the axis
Looking from the positive half of the axis back towards the
origin (same as it was in 2D)
32
16
9/13/2016
Rz(q)
Extending our 2D rotation equations to 3D:
x = x cos(q) y sin(q)
y = x sin(q) + y cos(q) same as in 2D
z = z
the height stays the same
Rz(q):
33
Rz(q)
Extending our 2D rotation equations to 3D:
x = x cos(q) y sin(q)
y = x sin(q) + y cos(q) same as in 2D
z = z
the height stays the same
Rz(q):
cos(q) -sin(q)
sin(q) cos(q)
0
0
0
0
0
0
1
0
0
0
0
1
34
17
9/13/2016
Ry(q):
0
0
0
cos(q)
sin(q)
0
-sin(q)
cos(q)
0
0
0
1
cos(q)
sin(q)
0
-sin(q)
0
1
0
0
0
cos(q)
0
0
0
1
35
3.
4.
5.
36
18
9/13/2016
37
Pre-multiplication of Transformations
Recall that matrix multiplication is not
commutative: AB != BA
However, it is associative: (AB)C = A(BC)
This implies that you dont have to perform the matrix
multiplies right-to-left
19
9/13/2016
Pre-multiplication of Transformations
The old method produced (for 100k points)
44.8M multiplies and 33.6M adds
Vectors
Another important mathematical concept used in
graphics is the Vector
40
20
9/13/2016
Vector Projections
41
Vector Projections
42
21
9/13/2016
43
cos a = Vx / |V|
cos g = Vz / |V|
44
22
9/13/2016
Vector Normalization
Normalizing a vector means shrinking or stretching it so
its magnitude is 1
Note that the last calculation doesnt come out to exactly 1. This is because of the error
introduced by using only 2 decimal places in the calculations above.
45
Vector Addition
Equation:
V3 = V1 + V2 = (V1x + V2x , V1y + V2y , V1z + V2z)
Visually:
46
23
9/13/2016
Vector Subtraction
Equation:
V3 = V1 - V2 = (V1x - V2x , V1y - V2y , V1z - V2z)
Visually:
47
Dot Product
The dot product of 2 vectors is a scalar
V1 . V2 = (V1x V2x) + (V1y V2y ) + (V1z V2z )
Or, perhaps more importantly for graphics:
V1 . V2 = |V1| |V2| cos(q)
where q is the angle between the 2 vectors
and q is in the range 0 q
48
24
9/13/2016
Dot Product
Why is dot product important for graphics?
It is zero if the 2 vectors are perpendicular
cos(90) = 0
49
Dot Product
The Dot Product computation can be simplified when
it is known that the vectors are unit vectors
V1 . V2 = cos(q)
because |V1| and |V2| are both 1
Saves 6 squares, 4 additions, and 2 sqrts
50
25
9/13/2016
Cross Product
The cross product of 2 vectors is a vector
V1 x V2 = ( V1y V2z - V1z V2y ,
V1z V2x - V1x V2z ,
V1x V2y - V1y V2x )
Note that if you are big into linear algebra there is also a
51
Cross Product
Again, just as with the dot product, there is a more
graphical definition:
V1 x V2 = u |V1| |V2| sin(q)
where q is the angle between the 2 vectors
and q is in the range 0 q
and u is the unit vector that is perpendicular
to both vectors
Why u?
52
26
9/13/2016
Cross Product
The direction of u is determined by the right hand
rule
The perpendicular definition leaves an ambiguity in
53
54
27
9/13/2016
28