Lecture 4: 2D Transformations

CS-184: Computer Graphics
Lecture #4: 2D Transformations
Prof. James O’Brien
University of California, Berkeley
V2009-F-04-1.0
Today
• 2D Transformations
“Primitive” Operations
• Scale, Rotate, Shear, Flip, Translate
• Homogenous Coordinates
• SVD
• Start thinking about rotations...
•
2
Introduction
• Transformation:
An operation that changes one configuration into another
• For images, shapes, etc.
A geometric transformation maps positions that define the object to
other positions
Linear transformation means the transformation is defined by a linear
function... which is what matrices are good for.
3
Some Examples
Uniform Scale
Rotation
Original
Nonuniform Scale
Shear
Images from Conan The Destroyer, 1984
4
Mapping Function
f (x) = x in old image
c(x) = [195, 120, 58]
c!x = c( f (x))
5
Linear -vs- Nonlinear
Nonlinear (swirl)
Linear (shear)
6
Geometric -vs- Color Space
Color Space Transform
(edge finding)
Linear Geometric
(flip)
7
Instancing
RHW
M.C. Escher, from Ghostscript 8.0 Distribution
8
Instancing
• Reuse
• Saves
geometric descriptions
memory
RHW
9
Linear is Linear
• Polygons
• Edges
defined by interpolation between two points
• Interior
•
defined by points
defined by interpolation between all points
Linear interpolation
10
Linear is Linear
• Composing
• Transform
two linear function is still linear
polygon by transforming vertices
Scale
11
Linear is Linear
• Composing
• Transform
two linear function is still linear
polygon by transforming vertices
f (x) = a + bx
g( f ) = c + d f
g(x) = c + d f (x) = c + ad + bdx
g(x) = a! + b!x
12
Points in Space
• Represent
point in space by vector in Rn
Relative to some origin!
• Relative to some coordinate axes!
•
• Later
we’ll add something extra...
pp ==[4,
[22],4]T
T
2
4
Origin, 0
13
Basic Transformations
transforms are: rotate, scale, and translate
iso
No
n-
un
ifo
rm
/is
/an
otr
op
ic
tro
pic
is a composite transformation!
ifo
rm
• Shear
Un
• Basic
Rotate
Translate
Scale
Shear -- not really “basic”
14
Linear Functions in 2D
x! = f (x, y) = c1 + c2x + c3y
y! = f (x, y) = d1 + d2x + d3y
! !" ! " !
" ! "
x
tx
Mxx Mxy
x
+
·
! =
y
ty
Myx Myy
y
x! = t + M · x
15
Rotations
Cos(θ ) − Sin(θ )
p' = 
p
 Sin(θ ) Cos(θ ) 
Rotate
45 degree rotation
y
.707 -.707
.707 .707
x
x
16
Rotations
• Rotations
are positive counter-clockwise
• Consistent
• Don’t
w/ right-hand rule
be different...
• Note:
•
•
rotate by zero degrees give identity
rotations are modulo 360 (or 2π )
17
Rotations
• Preserve
lengths and distance to origin
• Rotation
matrices are orthonormal
• Det(R)
• In
•
= 1 != −1
2D rotations commute...
But in 3D they won’t!
18
rm
-u
nif
o
sx
p' = 
0
No
n
Un
ifo
rm
/is
o
/an
tro
iso
pic
tro
pic
Scales
0
p
s y 
Scale
y
y
0.5 0
0 1.5
x
19
x
Scales
• Diagonal
matrices
Diagonal parts are scale in X and scale in Y directions
Negative values flip
• Two negatives make a positive (180 deg. rotation)
• Really, axis-aligned scales
•
•
Not axis-aligned...
20
Shears
 1
p' = 
 H xy
Shear
y
H yx 
p
1 
y
1
1
0
1
x
x
21
Shears
• Shears
are not really primitive transforms
• Related
• More
to non-axis-aligned scales
shortly.....
22
Translation
• This
is the not-so-useful way:
Translate
t x 
p' = p +  
t y 
Note that its not like the others.
23
Arbitrary Matrices
• For
everything but translations we have:
• Soon, translations
• What
x! = A · x
will be assimilated as well
does an arbitrary matrix mean?
24
Singular Value Decomposition
• For
any matrix, A , we can write SVD:
A = QSR T
where Q and R are orthonormal and S is diagonal
• Can
also write Polar Decomposition
A = QRSR T
where Q is still orthonormal
not the same Q
25
Decomposing Matrices
• We
can force Q and R to have Det=1 so they are
rotations
• Any
matrix is now:
Rotation:Rotation:Scale:Rotation
• See, shear is just a mix of rotations and scales
•
26
Composition
• Matrix
multiplication composites matrices
p' = BAp
“Apply A to p and then apply B to that result.”
p' = B( Ap) = (BA )p = Cp
• Several
translations composted to one
• Translations
still left out...
p' = B( Ap + t ) = BAp + Bt = Cp + u
27
Composition
• Matrix
multiplication composites matrices
p' = BAp
“Apply A to p and then apply B to that result.”
• Several
p' = B( Ap) = (BA )p = Cp
translations composted to one
• Translations
still left out...
p' = B( Ap + t ) = BAp + Bt = Cp + u
27
Composition
Transformations built
up from others
y
SVD builds from
scale and rotations
shear
shear
x
x
y
Also build other
ways
y
i.e. 45 deg rotation
built from shears
shear
x
x
28
Homogeneous Coordiantes
• Move
•
to one higher dimensional space
Append a 1 at the end of the vectors
 px 
p= 
 py 
•
 px 
~ = p 
p
 y
 1 
For directions the extra coordinate is a zero
29
Homogeneous Translation
1 0 t x   p x 
~ ' = 0 1 t   p 
p
y  y 

0 0 1   1 
~~
~' = A
p
p
The tildes are for clarity to
distinguish homogenized
from non-homogenized
vectors.
30
Homogeneous Others
0

~  A

A=
0


0 0 1 
Now everything looks the same...
Hence the term “homogenized!”
31
Compositing Matrices
• Rotations
• How
and scales always about the origin
to rotate/scale about another point?
-vs-
32
Rotate About Arb. Point
• Step
1: Translate point to origin
Translate (-C)
33
Rotate About Arb. Point
• Step
1: Translate point to origin
• Step
2: Rotate as desired
Translate (-C)
Rotate (θ)
34
Rotate About Arb. Point
• Step
1: Translate point to origin
• Step
2: Rotate as desired
• Step
3: Put back where it was
Translate (-C)
Rotate (θ)
Translate (C)
35
Rotate About Arb. Point
• Step
1: Translate point to origin
• Step
2: Rotate as desired
• Step
3: Put back where it was
Translate (-C)
Rotate (θ)
Translate (C)
~ ' = (−T)RTp
~ = Ap
~
p
35
Rotate About Arb. Point
• Step
1: Translate point to origin
• Step
2: Rotate as desired
• Step
3: Put back where it was
Translate (-C)
Rotate (θ)
Translate (C)
~ ' = (−T)RTp
~ = Ap
~
p
Don’t negate the 1...
35
Scale About Arb. Axis
• Diagonal
matrices scale about coordinate axes only:
Not axis-aligned
36
Scale About Arb. Axis
• Step
1: Translate axis to origin
37
Scale About Arb. Axis
• Step
1: Translate axis to origin
• Step
2: Rotate axis to align with one of the coordinate
axes
38
Scale About Arb. Axis
• Step
1: Translate axis to origin
• Step
2: Rotate axis to align with one of the coordinate
• Step
3: Scale as desired
axes
39
Scale About Arb. Axis
• Step
1: Translate axis to origin
• Step
2: Rotate axis to align with one of the coordinate
• Step
3: Scale as desired
axes
• Steps
4&5: Undo 2 and 1 (reverse order)
40
Order Matters!
• The
order that matrices appear in matters
• Some
• But
A · B != BA
special cases work, but they are special
matrices are associative
• Think
(A · B) · C = A · (B · C)
about efficiency when you have many points to
transform...
41
Matrix Inverses
• In
general: A−1 undoes effect of
• Special
A
cases:
Translation: negate x and ty
Rotation: transpose
• Scale: invert diagonal (axis-aligned scales)
t
•
•
• Others:
Invert matrix
• Invert SVD matrices
•
42
Point Vectors / Direction
• Points
in space have a 1 for the “w” coordinate
• What
should we have for a − b ?
•
•
•
•
•
•
w=0
Directions not the same as positions
Difference of positions is a direction
Position + direction is a position
Direction + direction is a direction
Position + position is nonsense
43
Somethings Require Care
For example normals do not transform normally
M(a × b) "= (Ma) × (Mb)
Use inverse transpose of the matrix for
normals.
See text book.
44
Suggested Reading
• Fundamentals
of Computer Graphics by Pete Shirley
Chapter 5
• And re-read chapter 4 if your linear algebra is rusty!
•
45