Why study transformation
Modeling
- translation
- rotation
- scaling
Viewing
- (3D to 2D) projection
2D transformations
- Scale
[ x ′ y ′ ] = [ s x 0 0 s y ] [ x y ] \begin{bmatrix}x'\\y'\end{bmatrix}=\begin{bmatrix}s_x&0\\0&s_y\end{bmatrix}\begin{bmatrix}x\\y\end{bmatrix} [x′y′]=[sx00sy][xy]- Reflection Matrix
[ x ′ y ′ ] = [ − 1 0 0 1 ] [ x y ] \begin{bmatrix}x'\\y'\end{bmatrix}=\begin{bmatrix}-1&0\\0&1\end{bmatrix}\begin{bmatrix}x\\y\end{bmatrix} [x′y′]=[−1001][xy] - Shear Matrix
[ x ′ y ′ ] = [ 1 a 0 1 ] [ x y ] \begin{bmatrix}x'\\y'\end{bmatrix}=\begin{bmatrix}1&a\\0&1\end{bmatrix}\begin{bmatrix}x\\y\end{bmatrix} [x′y′]=[10a1][xy]
- Reflection Matrix
- Rotate
R θ = [ cos θ − sin θ sin θ cos θ ] R_\theta=\begin{bmatrix}\cos{\theta}&-\sin{\theta}\\\sin{\theta}&\cos{\theta}\end{bmatrix} Rθ=[cosθsinθ−sinθcosθ] - Linear Transforms = Matrices
[ x ′ y ′ ] = [ a b c d ] [ x y ] \begin{bmatrix}x'\\y'\end{bmatrix}=\begin{bmatrix}a&b\\c&d\end{bmatrix}\begin{bmatrix}x\\y\end{bmatrix} [x′y′]=[acbd][xy]
x ′ = M x x'=\bold{M}x x′=Mx
Homogeneous coordinates
Why Homogeneous Coordinates
- Translation cannot be represented in matrix form
[ x ′ y ′ ] = [ a b c d ] [ x y ] + [ t x t y ] \begin{bmatrix}x'\\y'\end{bmatrix}=\begin{bmatrix}a&b\\c&d\end{bmatrix}\begin{bmatrix}x\\y\end{bmatrix}+\begin{bmatrix}t_x\\t_y\end{bmatrix} [x′y′]=[acbd][xy]+[txty]
(So, translation is NOT linear transform!) - But we don’t want translation to be a special case
- Is there a unified way to represent all transformations? (and what’s the cost?)
Solution: Homogenous Coordinates
Add a third coordinate (w-coordinate)
- 2D point = ( x , y , 1 ) T (x,y,1)^T (x,y,1)T
- 2D vector = ( x , y , 0 ) T (x,y,0)^T (x,y,0)T
Matrix representation of translations
( x ′ y ′ w ′ ) = ( 1 0 t x 0 1 t y 0 0 1 ) ⋅ ( x y 1 ) = ( x + t x y + t y 1 ) \begin{pmatrix}x'\\y'\\w'\end{pmatrix} =\begin{pmatrix}1&0&t_x\\0&1&t_y\\0&0&1\end{pmatrix}\cdot \begin{pmatrix}x\\y\\1\end{pmatrix}=\begin{pmatrix}x+t_x\\y+t_y\\1\end{pmatrix} x′y′w′ = 100010txty1 ⋅ xy1 = x+txy+ty1
Valid operation if w-coordinate of result is 1 or 0
- vector + vector = vector
- point - point = vector
- point + vector = point
- point + point = ??
In homogeneous coordinates, ( x y w ) \begin{pmatrix}x\\y\\w\end{pmatrix} xyw is the 2D point ( x / w y / w 1 ) \begin{pmatrix}x/w\\y/w\\1\end{pmatrix} x/wy/w1 , w ≠ 0 w\neq0 w=0
Affine Transformations
Affine map = linear map + translation
( x ′ y ′ ) = ( a b c d ) ⋅ ( x y ) + ( t x t y ) \begin{pmatrix}x'\\y'\end{pmatrix} =\begin{pmatrix}a&b\\c&d\end{pmatrix}\cdot\begin{pmatrix}x\\y\end{pmatrix}+\begin{pmatrix}t_x\\t_y\end{pmatrix} (x′y′)=(acbd)⋅(xy)+(txty)
Using homogenous coordinates:
( x ′ y ′ 1 ) = ( a b t x c d t y 0 0 1 ) ⋅ ( x y 1 ) = ( x y 1 ) \begin{pmatrix}x'\\y'\\1\end{pmatrix} =\begin{pmatrix}a&b&t_x\\c&d&t_y\\0&0&1\end{pmatrix}\cdot \begin{pmatrix}x\\y\\1\end{pmatrix}=\begin{pmatrix}x\\y\\1\end{pmatrix} x′y′1 = ac0bd0txty1 ⋅ xy1 = xy1
2D Transformations
- Scale
S ( s x , s y ) = ( s x 0 0 0 s y 0 0 0 1 ) S(s_x,s_y)=\begin{pmatrix}s_x&0&0\\0&s_y&0\\0&0&1\end{pmatrix} S(sx,sy)= sx000sy0001 - Rotation
R ( α ) = ( cos α − sin α 0 sin α cos α 0 0 0 1 ) R(\alpha)=\begin{pmatrix}\cos{\alpha}&-\sin{\alpha}&0\\\sin{\alpha}&\cos{\alpha}&0\\0&0&1\end{pmatrix} R(α)= cosαsinα0−sinαcosα0001 - Translation
T ( t x , t y ) = ( 1 0 t x 0 1 t y 0 0 1 ) T(t_x,t_y)=\begin{pmatrix}1&0&t_x\\0&1&t_y\\0&0&1\end{pmatrix} T(tx,ty)= 100010txty1
Inverse Transform
M − 1 M^{-1} M−1 is the inverse of transform M M M in both a matrix and geometric sense
Composite Transform
Transform Odering Matters!
Matrix multiplication is not commutative
R 45 ⋅ T ( 1 , 0 ) ≠ T ( 1 , 0 ) ⋅ R 45 R_{45}\cdot T_{(1,0)}\neq T_{(1,0)}\cdot R_{45} R45⋅T(1,0)=T(1,0)⋅R45
Note that matrices are applied right to left:
T ( 1 , 0 ) ⋅ R 45 [ x y 1 ] = [ 1 0 1 0 1 0 0 0 1 ] [ cos 4 5 ∘ − sin 4 5 ∘ 0 sin 4 5 ∘ cos 4 5 ∘ 0 0 0 1 ] [ x y 1 ] T_{(1,0)}\cdot R_{45}\begin{bmatrix}x\\y\\1\end{bmatrix}= \begin{bmatrix} 1&0&1\\ 0&1&0\\ 0&0&1 \end{bmatrix} \begin{bmatrix} \cos{45^\circ}&-\sin{45^\circ}&0\\ \sin{45^\circ}&\cos{45^\circ}&0\\ 0&0&1 \end{bmatrix} \begin{bmatrix} x\\y\\1 \end{bmatrix} T(1,0)⋅R45 xy1 = 100010101 cos45∘sin45∘0−sin45∘cos45∘0001 xy1
Sequence of affine transforms A 1 A_1 A1, A 2 A_2 A2, A 3 A_3 A3, …
- Compose by matrix multiplication
- Very important for performance!
A n ( … A 2 ( A 1 ( x ) ) ) = A n ⋯ A 2 ⋅ A 1 ⋅ ( x y 1 ) A_n(\dots A_2(A_1(x)))=A_n\cdots A_2\cdot A_1\cdot\begin{pmatrix}x\\y\\1\end{pmatrix} An(…A2(A1(x)))=An⋯A2⋅A1⋅ xy1
- Very important for performance!
Decomposing Complex Transforms
How to rotate around a given point c?
- Translate center to origin
- Rotate
- Translate back
3D Transforms
Use homogeneous coordinates again:
- 3D point = ( x , y , z , 1 ) T (x,y,z,1)^T (x,y,z,1)T
- 3D vector = ( x , y , z , 0 ) T (x,y,z,0)^T (x,y,z,0)T
In general, ( x , y , z , w ) ( w ≠ 0 ) (x,y,z,w)(w\neq0) (x,y,z,w)(w=0) is the 3D point: ( x / w , y / w , z / w ) (x/w,y/w,z/w) (x/w,y/w,z/w)
Use 4 × 4 4\times4 4×4 matrices for affine transformations
( x ′ y ′ z ′ 1 ) = ( a b c t x d e f t u g h i t z 0 0 0 1 ) ⋅ ( x y z 1 ) \begin{pmatrix} x'\\y'\\z'\\1 \end{pmatrix}= \begin{pmatrix} a&b&c&t_x\\d&e&f&t_u\\g&h&i&t_z\\0&0&0&1 \end{pmatrix} \cdot \begin{pmatrix} x\\y\\z\\1 \end{pmatrix} x′y′z′1 = adg0beh0cfi0txtutz1 ⋅ xyz1