# Transformation

Document Sample

```					 Computer Graphics
2D Transformations

Course Website: http://www.comp.dit.ie/bmacnamee
2
of
25
Contents
In today’s lecture we’ll cover the following:
– Why transformations
– Transformations
• Translation
• Scaling
• Rotation
– Homogeneous coordinates
– Matrix multiplications
– Combining transformations
3
of
25
Why Transformations?
In graphics, once we have an object
Images taken from Hearn & Baker, “Computer Graphics with OpenGL” (2004)

described, transformations are used to move
that object, scale it and rotate it
4
of
25
2D Transforms

• What am I talking about when I say
“transforms”?             (x’,y’)

• Translation

(x,y)

• Scaling

• Rotation
5
of
25
Translation
Simply moves an object from one position to
another
xnew = xold + dx                            ynew = yold + dy
y    6

5

4

3

2

1

0
1   2   3   4   5   6   7   8   9   10
x
Note: House shifts position relative to origin
6
of
25
Translation Example
y

6

5

4
(2, 3)
3

2

1
(1, 1)                (3, 1)

0            1     2      3        4   5    6   7   8   9   10   x
7
of
25
2D Translations.

Point P defined as P( x, y ),
translate to Point P( x, y) a distance d x parallel to x axis, d y parallel to y axis.
x  x  d x y   y  d y
Define the column vectors
 x        x     d x                                         P’
P    , P    , T                                          P
 y        y     dy 
Now
P  P  T
8
of
25
Scaling
Scalar multiplies all coordinates
WATCH OUT: Objects grow and move!
xnew = Sx × xold      ynew = Sy × yold
y
6

5

4

3                                    6                    9 
3                    3
                      
2

1           2            3
1            1
              
0
1          2   3    4    5          6   7   8   9          10
x
Note: House shifts position relative to origin
9
of
25                                   Scaling
• Scaling a coordinate means multiplying each of its
components by a scalar
• Uniform scaling means this scalar is the same for all
components:

2
10
of
25                                      Scaling
• Non-uniform scaling: different scalars per
component:

X  2,
Y  0.5

• How can we represent this in matrix form?
11
of
25
Scaling Example

y

6

5

4
(2, 3)
3

2

1
(1, 1)            (3, 1)

0        1     2      3        4   5   6     7   8   9   10
12
of
25
2D Scaling from the origin.

Point P defined as P ( x, y ),
Performa scale (stretch)to Point P( x, y) by a factor s x along the x axis,
and s y along the y axis.
x  s x . x,     y  s y . y
Define the matrix                                                           P’
P
sx           0
S
0            sy 

Now
 x   s x   0  x 
P  S  P or             y    0       . 
            s y   y
13
of
25
Rotation
Rotates all coordinates by a specified angle
xnew = xold × cosθ – yold × sinθ
ynew = xold × sinθ + yold × cosθ
Points are always rotated about the origin
y
6

5

4

3

2

1

6

0
1   2        3   4   5   6   7   8   9   10
x
14
of
25
Rotation Example
y

6

5

4
(4, 3)
3

2

1
(3, 1)                    (5, 1)

0   1   2            3     4      5            6   7   8   9   10
15
of
25

y


P’(x’,y’)

P(x,y)
r

r
x
16
of
25

y

P’(x’,y’)

r
P(x,y)   x  r. cos 
y  r. sin 
                           y
r

x
x
17
of
25

x  r. cos(   )  r. cos  . cos  r. sin  . sin 
y       y  r. sin(   )  r. cos  . sin   r. sin  . cos

P’(x’,y’)

P(x,y)
r                                       x  r. cos 
y  r. sin 
                           y
r

x
x
18
of
25

x  r. cos(   )  r. cos  . cos  r. sin  . sin 
y  r. sin(   )  r. cos  . sin   r. sin  . cos
Substituting for r :

x  r. cos 
y  r. sin 
Gives us :

x  x. cos  y. sin 
y  x. sin   y. cos
19
of
25

x  x. cos  y. sin 
y  x. sin   y. cos

Rewriting in matrix form gives us :

 x cos          sin    x 
 y   sin              . y 
cos   
  

cos            sin  
Define the matrix R                         , P  R  P
 sin          cos 
20
of
25
Let’s do an example
• Two transforms:
– Scale x and y by a factor of 2
– Translate points (+3, +2)
• Let’s pick a single point in object space
– (1, 2)
21
of
25
Two Transform Paths

(2,1)
y
x

Translate Then Scale
22
of
25
Two Transform Paths

(5,3)
y
x
(+3, +2)

Translate Then Scale
23
of
25
Two Transform Paths
(10, 6)

y
x

(*2, *2)

Translate Then Scale
24
of
25
Two Transform Paths

(2,1)
y
x

Scale Then Translate
25
of
25
Two Transform Paths

(4,2)

y
x     (*2, *2)

Scale Then Translate
26
of
25
Two Transform Paths

(7,4)
y

x
(+3, +2)

Scale Then Translate
27
of
25
Homogeneous Coordinates
• Homogeneous representation of points:
points
– All multiples of this vector are considered to
represent the same 3D point
– why bother? need unified representation
 x   x  w
 x   
       y   y  w
 y             w  0
 z   z   z  w
  1   w 
          
28
of
25
Geometrically In 2D
• Cartesian Coordinates:

y
 x
 
 y
x               
29
of
25
Geometrically In 2D
• Homogeneous Coordinates:
 x  w
w
 y  w
      
 w 
      

 x
 y                    w=1
 
1 
 
y

x
30
of
25
Homogeneous Coordinates
A point (x, y) can be re-written in
homogeneous coordinates as (xh, yh, h)
The homogeneous parameter h is a non-
zero value such that:
xh                yh
x                y
h                 h
We can then write any point (x, y) as (hx, hy, h)
We can conveniently choose h = 1 so that
(x, y) becomes (x, y, 1)
31
of
25
Why Homogeneous Coordinates?
Mathematicians commonly use
homogeneous coordinates as they allow
scaling factors to be removed from
equations
We will see in a moment that all of the
transformations we discussed previously
can be represented as 3*3 matrices
Using homogeneous coordinates allows us
use matrix multiplication to calculate
transformations – extremely efficient!
32
of
25
Homogeneous Translation

The translation of a point by (dx, dy) can be
written in matrix form as:
1   0    dx 
0   1    dy 
            
0
    0    1 
Representing the point as a homogeneous
column vector we perform the calculation as:
1 0 dx  x  1* x  0 * y  dx *1  x  dx 
0 1 dy    y   0 * x  1* y  dy *1   y  dy 
                                               
0 0 1  1   0 * x  0 * y  1*1   1 
                                               
33
of
25
Remember Matrix Multiplication
Recall how matrix multiplication takes place:

a   b     c   x  a * x  b * y  c * z 
d   e          y   d * x  e * y  f * z 
f   
                                               
g
    h     i  z  g * x  h* y  i* z 
                               
34
of
25
Homogenous Coordinates
To make operations easier, 2-D points are
written as homogenous coordinate column
vectors
1 0 dx  x   x  dx 
0 1 dy   y    y  dy : v'  T (dx, dy)v
Translation:
                      
0 0 1  1   1 
                      

 sx 0 0  x   sx  x 
 0 s y 0    y    s y  y  : v '  S ( sx , s y ) v
Scaling:
                           
 0 0 1 1   1 
                           
35
of
25
Homogenous Coordinates (cont…)

Rotation:

cos       sin    0  x  cos  x  sin   y 
 sin     cos      0   y   sin   x  cos  y  : v'  R( )v
                                                   
 0
            0       1 1  
                   1          

36
of
25
Inverse Transformations
Transformations can easily be reversed
using inverse transformations
1 0  dx
1
T 0 1  dy                1         
                s    0 0
0 0 1 
                         x        
1 
S 1   0      0
 cos sin  0                sy 
1  sin  cos 0          0    0 1
R                                    
          
 0
         0 1 
37
of
25
Combining Transformations
A number of transformations can be
combined into one matrix to make things
easy
– Allowed by the fact that we use homogenous
coordinates
Imagine rotating a polygon around a point
other than the origin
– Transform to centre point to origin
– Rotate around origin
– Transform back to centre point
38
of
25
Combining Transformations (cont…)
House (H )             T (dx, dy) H
1                                     2

R( )T (dx, dy) H   T (dx,dy) R( )T (dx, dy) H

3                                 4
39
of
Composing of Affine
25
Transformations
• Example: Rotation around arbitrary center
40
of
Composing of Affine
25
Transformations
• Example: Rotation around arbitrary center
– Step 1: translate coordinate system to
rotation center
41
of
Composing of Affine
25
Transformations
• Example: Rotation around arbitrary center
– Step 2: perform rotation
42
of
Composing of Affine
25
Transformations
• Example: Rotation around arbitrary center
– Step 3: back to original coordinate system
43
of
25
Combining Transformations (cont…)
The three transformation matrices are
combined as follows
1 0  dx cos  sin  0 1 0 dx  x 
0 1  dy    sin  cos 0  0 1 dy    y 
                                     
0 0 1   0
                    0   1 0 0 1  1 
            
v'  T (dx,dy) R( )T (dx, dy)v

REMEMBER: Matrix multiplication is not
commutative so order matters
44
of
25
Two Transform Paths

Scale then Translate
45
of
25

Concatenation of scales.

S 1s  (
matrixx, y)S 2s is
( 1 s 2
s
The product x, y) :

s 0 0
x1   s 0 0   x2 0 0
x2     s s
x1      
      
 s 0. 0 s 0  0 s 
0 y1               s 0 
         y2     y1y2 


0 0 1
0 0 1  0
      
      0 1 

diagonal the
in matrix
Onlyelements-   mult
!
easy to
46
of                 How are transforms combined?
25

Scale then Translate
(5,3
Scale(2,2)‫‏‬      (2,2    Translate(3,1          )‫‏‬
(1,1                       )‫‏‬      )‫‏‬              (3,1
(0,0   )‫‏‬                  (0,0                           )‫‏‬
)‫‏‬                         )‫‏‬

Use matrix multiplication: p' = T ( S p ) = TS
1 0 3            2 0 0             2 0 3
TS =      0 1 1            0 2 0     =       0 2 1
0 0 1            0 0 1             0 0 1

Caution: matrix multiplication is NOT commutative
02/10/09             Lecture 4                     46
47
of                 Non-commutative Composition
25

Scale then Translate: p' = T ( S p ) = TS p
(5,3
Scale(2,2)‫‏‬             (2,2      Translate(3,1                 )‫‏‬
(1,1                              )‫‏‬        )‫‏‬              (3,1
(0,0   )‫‏‬                  (0,0                                    )‫‏‬
)‫‏‬                         )‫‏‬

Translate then Scale: p' = S ( T p ) = ST p
(8,4)‫‏‬
Translate(3,1               (4,2    Scale(2,2)‫‏‬
(1,1                                                               (6,2
)‫‏‬                (3,1      )‫‏‬
)‫‏‬                                                                 )‫‏‬
(0,0                              )‫‏‬
)‫‏‬

02/10/09             Lecture 4                              47
48
of               Non-commutative Composition
25

Scale then Translate: p' = T ( S p ) = TS p
1 0 3         2 0 0       2 0 3
TS =      0 1 1         0 2 0   =   0 2 1
0 0 1         0 0 1       0 0 1

Translate then Scale: p' = S ( T p ) = ST p
2 0 0         1 0 3       2 0 6
ST =      0 2 0         0 1 1   =   0 2 2
0 0 1         0 0 1       0 0 1
02/10/09       Lecture 4               48
49
of               How are transforms combined?
25

Rotate then Translate
(0,sqrt(2))‫‏‬                             (3,sqrt(2))‫‏‬
(1,1    Rotate 45                            Translate(3,0
)‫‏‬      deg‫‏‬                                 )‫‏‬
(0,0                          (0,0                                             (3,0
)‫‏‬                            )‫‏‬                                               )‫‏‬

Translate then Rotate

(1,1   Translate(3,0                                          (3/sqrt(2),3/sqrt(2
Rotate 45
)‫‏‬     )‫‏‬                                                     ))‫‏‬
deg‫‏‬
(0,0                          (0,0         (3,0)‫‏‬
)‫‏‬                            )‫‏‬

Caution: matrix multiplication is NOT commutative
02/10/09             Lecture 4                                 49
50
of              Non-commutative Composition
25

Rotate then Translate: p' = T ( R p ) = TR p
1 0 3         0 -1 0       0 -1 3
TR =      0 1 1         1 0 0        0 0 1
0 0 1         0 0 1        0 0 1
=
Translate then Rotate: p' = R ( T p ) = RT p
0 -1 0        1 0 3        0 -1 -1
RT =      1 0 0         0 1 1    =   1 0 3
0 0 1         0 0 1        0 0 1
02/10/09       Lecture 4                50
51
of
25
Summary
In this lecture we have taken a look at:
– 2D Transformations
• Translation
• Scaling
• Rotation
– Homogeneous coordinates
– Matrix multiplications
– Combining transformations
Next time we’ll start to look at how we take
these abstract shapes etc and get them on-
screen
52
of
25
Exercises 1
Translate the shape below by (7, 2)
y

6

5

4

(2, 3)
3

(1, 2)                (3, 2)
2

1
(2, 1)

0            1     2      3        4   5   6   7   8   9   10   x
53
of
25
Exercises 2
Scale the shape below by 3 in x and 2 in y
y

6

5

4

(2, 3)
3

(1, 2)                    (3, 2)
2

1
(2, 1)

0            1     2      3        4   5   6   7   8   9   10   x
54
of
25
Exercises 3
Rotate the shape below by 30° about the origin
y

6

5

4

(7, 3)
3

(6, 2)                    (8, 2)
2

1
(7, 1)

0   1   2   3   4   5            6     7      8        9   10   x
55
of
25
Exercise 4
Write out the homogeneous matrices for the
previous three transformations
Translation      Scaling        Rotation
 __ __ __   __ __ __   __ __ __ 
 __ __ __   __ __ __   __ __ __ 
                                
 __ __ __   __ __ __   __ __ __ 
                                
56
of
25
Exercises 5
Using matrix multiplication calculate the rotation
of the shape below by 45° about its centre (5, 3)
y
5
(5, 4)
4

(4, 3)                    (6, 3)
3

2
(5, 2)
1

0   1   2   3        4     5      6        7   8     9   10   x
57
of
25
Equations
Translation:
xnew = xold + dx        ynew = yold + dy

Scaling:
xnew = Sx × xold         ynew = Sy × yold
Rotation
xnew = xold × cosθ – yold × sinθ
ynew = xold × sinθ + yold × cosθ
58
of
25
Quiz
• I sat in the car, and realized the side mirror is
0.4m on my right and 0.3m in my front
• I started my car and drove 5m forward, turned
30 degrees to right, moved 5m forward again,
and turned 45 degrees to the right, and
stopped
• What is the position of the side mirror now,
relative to where I was sitting in the
beginning?
59
of
25

3D Transformations.
• Use homogeneous coordinates, just as in 2D
case.
• Transformations are now 4x4 matrices.
• We will use a right-handed (world) coordinate
y
system - ( z out of page ).

x
z (out of page)‫‏‬
60
of
25

Translation in 3D.

Simple extension to the 3D case:

1             0 0 d
x
0             1 0 d
(x y z 
Td,d,d)                    y

0             0 1 d
z
                    
0             00 1  
61
of
25

Scale in 3D.

Simple extension to the 3D case:

x
s            0 0 0
0            s 0 0
(x y z 
Ss,s ,s)               y   
0            0 s 0
z

                  
0            0 0 1
62
of
25

Rotation in 3D
• Need to specify which axis the rotation is
• z-axis rotation is the same as the 2D case.
    0
cos sin           
0
  cos 0
sin             
0
R 
z( )
                 
0   0 1          
0
                 
0   0 0          
1
63
of
25

x 
 1 0         x
0 0
              
   cos θ 0  

y 0     θ sin  y

   sin cos 0 z
z    0 θ    θ  
              
              1
0 1
 
1   0 0        
64
of
Ry()‫‏‬

  cos 0 sin 0  
x    θ     θ  x
              
y  0 1 0 0  
             y
   θ 0 cos0 z
z  sin    θ  
              
   0 0 0 1  1

1              
65
of
Rz()‫‏‬

   θ  θ 0 0  
x cos sin       x
              
   θ cos 0 0  

y sin     θ     y

z  0          
 
0 10 z
              
  0    0 01 1
 
1              
66
of
25

Rotation in 3D
• For rotation about the x and y axes:

 0 00
1        
cos0  
sin
0                        
 cos 0
R 
0
  
sin
 R

, y )
 
0 100
            
(
x)
 sin 0
0    
cos
(
 
 0
sin  
cos
0                        
              
     
00 0 1    0 001
67
y
Rotate(k,
25
arbitrary axis
θ
• About (ux, uy, uz), a unit
u
vector on an arbitrary axis
x
z

x'   uxux(1-c)+c   uzux(1-c)-     uxuz(1-       0   x
y'     uyux(1-        uzs        c)+uys         0   y
=
z'     c)+uzs     uzux(1-c)+c uyuz(1-c)-uxs     0   z
1 uzux(1-c)-uys     uyuz(1-    uzuz(1-c)+c      1   1
0         c)+uxs           0
where c = cos θ & s = sin θ
0
68
of
25

Transform Left-Right, Right-Left
Transforms between world coordinates and viewing coordinates. That is:
between a right-handed set and a left-handed set.


1 0                   0
0
0 1                   0
0
M L L R
 M 
                       
R
0 0                   1
0
                       
0 0                    
01
69
of
25
Shearing

1   a 0 0   1   0 0 0
0            a   1 0 0
    1 0 0
            
0   0 1 0   0   0 1 0
                     
0   0 0 1   0   0 0 1
70
of
25
Calculating the world coordinates
of all vertices
• For each object, there is a local-to-
global transformation matrix
• So we apply the transformations to all
the vertices of each object
• We now know the world coordinates of
all the points in the scene

70

```
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 7 posted: 3/14/2012 language: pages: 70
Description: graphics lecture