03 Transformations
03 Transformations
3. Linear and Af ne
Transformations
fi
Last class
• Triangle rasterization
• Object-order vs. image-order rendering
• Sampling and aliasing
Transformations
Translation Rotation
Scaling ???
Applications: Instancing
Cristian Goga
Transformation matrices
As you probably know, we can represent many transformations by matrices:
2.0 2.0
0.5 0.5 0.5
1.5 1.5
0 -0.5 0.5
-1.5 1.0-1.0 1.5
-0.5 2.0 0.5 1.0 1.5 2.0 -1.5 -1.0 -0.5 0.5 1.0
1.0
Translation 1.0 Rotation
-0.5 -0.5 -0.5
0.5 0.5
-1.0 -1.0 -1.0
-1.5 -1.5
-1.0 -0.5 0.5 -1.5
1.0 1.5 2.0
-1.5 -1.0 -0.5 0.5 1.0-1.5 1.5 2.0
Scaling ???
-0.5 -0.5
What can’t matrices do?
vnew ≠ Avold
2.0
1.5
1.0
So
rk
in
0.5
e
&
Al
ex
a
20
07
-1.0 -0.5 0.5 1.0 1.5 2.0
Translation Nonlinear deformation
-0.5
(not yet at least…)
-1.0
Transformations
Transformations are just functions that map points to points
T : ℝⁿ → ℝⁿ
[1]
2
≠
A vector only has coordinates once you make an (arbitrary) choice of basis
[1] [0.5]
2 1
Outcomes of operations should be independent of arbitrary choices!
• scalar multiplication: ℝ × V → V
• vector addition: V × V → V
satisfying various identities, e.g. u + v = v + u, a(u + v) = au + av, etc.
To do geometry, we also need a third operation:
Think of these three operations as the “public API” of the vector data type.
Try to always write your algorithm and code in terms of these, not by manipulating
coordinates individually! Then it will work for vectors in 2D, 3D, and any n dimensions.
Example: Find the multiple of u that is as close as possible to v.
(u ⋅ u)
u⋅v
w= u
(u ⋅ u)
u⋅v
w= u
∑ ∫
u⋅v= uivi ⟨u, v⟩ = u(x)v(x) dx
Bases
A basis is just a set of vectors {e1, e2, …} such that any vector can be written uniquely as a
linear combination of them.
v1
[1]
v
v= v2 in this basis v = v1e1 + v2e2 + ⋯
2
e2
⋮
e1
What happens when you apply a matrix A to the basis vectors?
v Av
a2
e2
e1 a1
• Interpretation 1: A matrix transforms the basis vectors to its columns; all other vectors
follow.
1.0 1.0
0.5 0.5
[ 0.5 ] [0.2]
-0.5 -0.5
−0.8 1.1
a1 = image of e1 ≈ , a2 = image of e2 ≈
-1.0 -1.0
[ 0.5 0.2]
-1.5 −0.8 1.1 -1.5
A≈
2.0 2.0
1.5 1.5
1.0 1.0
[ sin θ cos θ ]
cos θ −sin θ
0.5 0.5
2.0 2.0
-1.0 -1.0
[ 0 1]
0.5 0.5
sx 0 −1 0
-1.5 [ 0 sy]-1.5
-1.0 -0.5 0.5 1.0 1.5 2.0 -1.5 -1.0 -0.5 0.5 1.0 1.5 2.0
Scaling Re ection
-0.5 -0.5
fl
Puzzle:
It’s just as easy to scale x and y by different amounts as it is to scale by the same amount.
So why do video players show black bars instead of scaling the image to ll the screen?
fi
Invariants
Different types of transformations preserve different quantities:
v → Av → B(Av) = (BA)v
Column interpretation:
Av
a2 Ba1
B [a1 a2 ⋯] = [Ba1 Ba2 ⋯]
a1 Ba2 (BA)v
AB ≠ BA
1.5 1.5 1.5
0.5
Stretch x, 0.5
Rotate 0.5
shrink y by 90°
2.0 2.0 2.0
-0.5 0.5 1.0 -1.51.5 -1.02.0 -0.5 0.5 1.0 1.5 2.0 -1.5 -1.0 -0.5 0.5
1.5 1.5 1.5
-0.5 -0.5 -0.5
0.5
Rotate 0.5
Stretch x, 0.5
-1.5
by 90° -1.5
shrink y -1.5
-0.5 0.5 1.0 1.5 2.0 -1.5 -1.0 -0.5 0.5 1.0 1.5-1.5 2.0-1.0 -0.5 0.5
x
Rotations about the coordinate axes:
z
Not at all!
0 −nz ny
where [n]× = nz 0 −nx
−ny nx 0
How? Hints:
Tannous 2018
Homework exercise
Given unit vectors u and v, nd a way to construct a rotation matrix R which maps u to v,
i.e. Ru = v. Is it unique, or are there many different such rotations?
Translations 1.5
1.0
Move all points by a constant displacement
0.5
T(p) = p + t
t
v p v?
1.0 1.0 1.0
p p
0.5 0.5 0.5
v
5p3 = ?
p1
• Point-vector addition: A × V → A
• Point subtraction: A × A → V
with the obvious properties e.g. (p + u) + v = p + (u + v), p + (q − p) = q, etc.
fi
Example: midpoint of two points p and q
p
m = ½(p + q)? m
m = p + ½(q − p)
= q + ½(p − q)
(Exercise: Check that this can be done using only the af ne space operations)
fi
fi
Coordinate frames
v
To specify a vector numerically, we need a basis e2
v1
e1
v = v1e1 + v2e2 + ⋯ v= v2 in the basis
⋮
p1 p
p2 e2
p = p1e1 + p2e2 + ⋯ + o so maybe we should write p = ?
⋮
o e1
1
p1
p2
Write a point as an (n+1)-tuple p = to mean p = p1e1 + p2e2 + ⋯ + o.
⋮
1
[ 0 1]
A 0
Linear transformations are now , mapping ei → Aei and o → o
sx 0 0 px sx px
e.g. 0 sy 0 py = sy py
0 0 1 1 1
[0 1]
I t
Translation by a vector t: , mapping ei → ei but o → o + t
1 0 tx px px + tx
e.g. 0 1 ty py = py + ty
0 0 1 1 1
What about vectors?
v1
v2
v = v1e1 + v2e2 + ⋯ + 0o v=
⋮
0
Apply a translation:
1 0 tx vx vx
0 1 ty vy = vy
0 0 1 0 0
Homogeneous coordinates
Add an extra coordinate w at the end.
• Points: w = 1
• Vectors: w = 0
Transformations become (n+1)×(n+1) matrices
[ 0 1]
A 0
• Linear transformations:
[0 1]
I t
• Translations:
1 0 tx
0 1 ty
0 0 1
w=1 w=1
px
py
1
vx x x
vy
y 0 y
[ 0 1]
A t
General af ne transformation:
• Maps parallel lines to parallel lines, but does not preserve the origin
• Composition: just matrix multiplication again.
fi
2.0 2.0 2.0 2.0
Example: Rotate by given angle θ about given point p (instead of about origin)
1.5 1.5 1.5 1.5
p
0.5 0.5 0.5 0.5
-1.0 -1.0
Translate by −p -1.0
Rotate by θ -1.0Translate by p
about origin
-1.5 -1.5 -1.5 -1.5
p = p1e1 + p2e2 + ⋯ + o
p
Write coords of e1, e2, … and o in global frame:
∙ ∙
∙ ∙
ei = ⋮ , o= ⋮
0 1
∙ ∙ ⋯ ∙ p1
∙ ∙ ⋯ ∙ p2
Then pglobal = ⋮ ⋮ ⋱ ⋮ ⋮ Change of coordinates looks
0 0 ⋯ 1 1 exactly like a transformation matrix!
e1 e2 o
Active transformation: Moves points
to new locations in the same frame
f (x,y) = x2 + y2 − r2