Sei sulla pagina 1di 74

Computer Graphics

Lecture 6 & 7
Transformations

Dr. Muhammad Ahsan Ansari

1
Transformations.
What is a transformation?
• P=T(P)
What does it do?
Transform the coordinates / normal vectors of objects
Why use them?
• Modelling
-Moving the objects to the desired location in the
environment
-Multiple instances of a prototype shape
-Kinematics of linkages/skeletons – character animation
• Viewing
– Virtual camera: parallel and perspective projections

2
Types of Transformations

– Geometric Transformations
• Translation
• Rotation
• scaling
• Linear (preserves parallel lines)
• Non-uniform scales, shears or skews
– Projection (preserves lines)
• Perspective projection
• Parallel projection
– Non-linear (lines become curves)
• Twists, bends, warps, morphs,

3
Geometric Transformation
– Once the models are prepared, we need to place them in
the environment
– Objects are defined in their own local coordinate
system
– We need to translate, rotate and scale them to put them
into the world coordinate system

15/01/20 Lecture 1 4
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    , T   
 y  y  dy  P’
Now P

P  P  T

5
2D Scaling from the origin.
Point P defined as P ( x, y ),
Perform a 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 s y 
Now
 x   s x 0  x 
P  S  P or  y   0 . 
   s y   y

6
2D Rotation about the origin.


P’(x’,y’)

P(x,y)
r

r
x

7
2D Rotation about the origin.
y

P’(x’,y’)

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

x
x

8
2D Rotation about the origin.
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

9
2D Rotation about the origin.
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

10
2D Rotation about the origin.
x  x. cos  y. sin 
y  x. sin   y. cos
Rewriting in matrix form gives us :

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

cos  sin  
Define the matrix R    , P  R  P
 sin  cos 
11
Transformations.
• Translation.
– P=T + P
• Scale
– P=S  P
• Rotation  x cos  sin    x 
 y   sin  cos . y 
– P=R  P     
• We would like all transformations to be multiplications so
we can concatenate them
•  express points in homogenous coordinates.

12
Example: 2D Geometric
Transformation
Modeling
Coordinates

World Coordinates
Example: 2D Scaling
Modeling
Coordinates

Scale(0.3, 0.3)

World Coordinates
Example: 2D Rotation
Modeling
Coordinates

Scale(0.3, 0.3)
Rotate(-90)

World Coordinates
Example: 2D Translation
Modeling
Coordinates

Scale(0.3, 0.3)
Rotate(-90)
Translate(5, 3)

World Coordinates
Example: 2D Geometric
Transformation
Modeling
Coordinates
Again?

World Coordinates
Example: 2D Geometric
Transformation
Modeling
Coordinates
Scale
Translate

Scale
Rotate
Translate
World Coordinates
Basic 2D Transformations
• Translation

– x  x  tx
– y  y  ty
• Scale
– x   x  sx
– y  y  sy
• Rotation
– x   x  cosθ - y  sinθ
– y  y  sinθ  y  cosθ
• Shear
– 
x  x  hx  y

y  y  hy  x
Basic 2D Transformations
• Translation

– x  x  tx
– y  y  ty
• Scale
– x   x  sx
– y  y  sy
• Rotation
– x   x  cosθ - y  sinθ Transformations
– y  y  sinθ  y  cosθ can be combined
• Shear (with simple algebra)
– 
x  x  hx  y

y  y  hy  x
Basic 2D Transformations
• Translation

– x  x  tx
– y  y  ty
• Scale
– x   x  sx
– y  y  sy
• Rotation
– x   x  cosθ - y  sinθ
– y  y  sinθ  y  cosθ
• Shear x  x  sx
–  y  y  sy
x  x  hx  y

y  y  hy  x
Basic 2D Transformations
• Translation

– x  x  tx
– y  y  ty
• Scale
– x   x  sx
– y  y  sy
• Rotation
– x   x  cosθ - y  sinθ
– y  y  sinθ  y  cosθ
• Shear x  ((x  sx)  cos  (y  sy)  sin )
–  y  ((x  sx)  sin  (y  sy)  cos )
x  x  hx  y

y  y  hy  x
Basic 2D Transformations
• Translation

– x  x  tx
– y  y  ty
• Scale
– x   x  sx
– y  y  sy
• Rotation
– x   x  cosθ - y  sinθ
– y  y  sinθ  y  cosθ
• Shear x   ((x  sx)  cos  (y  sy)  sin )  tx
–  y  ((x  sx)  sin  (y  sy)  cos )  ty
x  x  hx  y

y  y  hy  x
Basic 2D Transformations
• Translation

– x  x  tx
– y  y  ty
• Scale
– x   x  sx
– y  y  sy
• Rotation
– x   x  cosθ - y  sinθ
– y  y  sinθ  y  cosθ
• Shear x   ((x  sx)  cos  (y  sy)  sin )  tx
– x   x  hx  y y  ((x  sx)  sin  (y  sy)  cos )  ty
– y  y  hy  x
Matrix Representation
• Represent a 2D Transformation by a Matrix
a b 
c d 
 

• Apply the Transformation to a Point


x  ax  by  x   a b   x 
    y
y  cx  dy y 
   c d  
Transformation
Point
Matrix
Matrix Representation
• Transformations can be combined by matrix
multiplication
 x   a b  e f  i j   x 
 y   c d   g     
h  k l   y 
   
Transformation
Matrix
Matrices are a convenient and efficient way
to represent a sequence of transformations
2×2 Matrices
• What types of transformations can be
represented with a 2×2 matrix?
2D Identity
x  x  x  1 0  x 
 y  0 1  y 
y  y     

2D Scaling
x  sx  x  x   sx 0   x 
y  sy  y  y  0 sy   y 
    
2×2 Matrices
• What types of transformations can be
represented with a 2×2 matrix?
2D Rotation
x  cos  x  sin   y  x  cos  sin    x 
 y  sin  cos   y 
y  sin   x  cos  y     
2D Shearing
x  x  shx  y  x  1 shx   x 
y  shy  x  y  y   shy 1  y 
    
2×2 Matrices
• What types of transformations can be
represented with a 2×2 matrix?
2D Mirror over Y axis
x   x  x    1 0   x 
y  y  y    0 1   y 
    
2D Mirror over (0,0)
x   x  x    1 0   x 
y   y  y  0  1  y 
    
2×2 Matrices
• What types of transformations can be
represented with a 2×2 matrix?
2D Translation
x   x  tx
y  y  ty NO!!

Only linear 2D transformations


can be Represented with 2x2 matrix
2D Translation
• 2D translation can be represented by a 3×3
matrix
– Point represented with homogeneous coordinates
 x  1 0 tx   x 
x   x  tx  y  0 1 ty   y 
y  y  ty     
1  0 0 1  1 
Homogeneous coordinates
• Add an extra coordinate, W, to a point.
– P(x,y,W).
• Two sets of homogeneous coordinates represent the same
point if they are a multiple of each other.
– (2,5,3) and (4,10,6) represent the same point.
• If W 0 , divide by it to get Cartesian coordinates of point
(x/W,y/W,1).
• If W=0, point is said to be at infinity.

32
Translations in homogenised
coordinates
• Transformation matrices for 2D translation
are now 3x3.

 x  1 0 d x   x  x  x  d x
 y  0 1 d . y 
   y   y  y  d y
 1  0 0 1   1  11

33
Concatenation.

• When we perform 2 translations on the same point


P  T (d x1 , d y1 )  P
P  T (d x 2 , d y 2 )  P
P  T (d x1 , d y1 )  T (d x 2 , d y 2 )  P  T (d x1  d x 2 , d y1  d y 2 )  P

So we expect :
T (d x1 , d y1 )  T (d x 2 , d y 2 )  T (d x1  d x 2 , d y1  d y 2 )

34
Concatenation.

The matrix product T (d x1 , d y1 )  T (d x 2 , d y 2 ) is :


1 0 d x1  1 0 d x 2 
0 1 d .0 1 d   ?
 y1   y2 

0 0 1  0 0 1 
Matrix product is variously referred to as compounding,
concatenation, or composition

35
Concatenation.
The matrix product T (d x1 , d y1 )  T (d x 2 , d y 2 ) is :
1 0 d x1  1 0 d x 2  1 0 d x1  d x 2 
0 1 d .0 1 d   0 1 d  d 
 y1   y2   y1 y2 

0 0 1  0 0 1  0 0 1 

Matrix product is variously referred to as compounding, concatenation, or


composition.

36
Properties of translations.
1. T (0,0)  I
2. T ( s x , s y )  T (t x , t y )  T ( s x  t x , s y  t y )
3. T ( s x , s y )  T (t x , t y )  T (t x , t y )  T ( s x , s y )
4. T -1 ( s x , s y )  T ( s x , s y )

Note : 3. translation matrices are commutative.

37
Homogeneous form of scale.
Recall the (x,y) form of Scale : sx 0
S (sx , s y )  
0 s y 

In homogeneous coordinates :

sx 0 0
S ( s x , s y )   0 sy 0
 0 0 1

38
Concatenation of scales.

The matrix product S ( s x1 , s y1 )  S ( s x 2 , s y 2 ) is :


s x1 0 0  s x2 0 0  s x1  s x2 0 0
0 s 0  . 0 s 0    0 s  s 0 
 y1  y2   y1 y2 
 0 0 1   0 0 1   0 0 1
Only diagonal elements in the matrix - easy to multiply !

39
Homogeneous form of rotation.
 x  cos  sin  0   x
 y   sin  cos 0 . y 
  
 1   0 0 1   1 

For rotation matrices,


R ( )  R( ).
1

Rotation matrices are orthogonal, i.e :


R 1 ( )  R T ( )

40
Orthogonality of rotation
matrices.
cos  sin  0   cos sin  0 
R( )   sin  cos 0 , RT ( )   sin  cos 0 
 0 0 1   0 0 1 

cos   sin   0   cos sin  0 


R( )   sin   cos  0    sin  cos 0 
 0 0 1   0 0 1 

41
Other properties of rotation.
R(0)  I
R( )  R( )  R(   )

and
R( )  R( )  R( )  R ( )
But this is only because the axis of rotation
is the same
For 3D rotations, need to be more careful

42
How are transforms combined?
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 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

Caution: matrix multiplication is NOT commutative!


02/10/09 Lecture 4 43
Non-commutative Composition
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 44
Non-commutative Composition
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 45
How are transforms combined?
Rotate then Translate
(0,sqrt(2)) (3,sqrt(2))
(1,1) Rotate 45 deg Translate(3,0)

(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 46
Non-commutative Composition
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 47
Types of transformations.
• Rotation and translation
– Angles and distances are preserved
– Unit cube is always unit cube
– Rigid-Body transformations.
• Rotation, translation and scale.
– Angles & distances not preserved.
– But parallel lines are.

48
Transformations of coordinate
systems.
• Have been discussing transformations as
transforming points.
• Always need to think the transformation in the
world coordinate system
• Sometimes this might not be so convenient
– i.e. rotating objects at its location

49
Transformations of coordinate
systems - Example
• Concatenate local
transformation matrices
from left to right
• Can obtain the local –
world transformation
matrix
• p’,p’’,p’’’ are the world
coordinates of p after
each transformation
50
Transformations of coordinate
systems - example

• is the world coordinate of point p after n


transformations
51
Quiz
• I sat in the car, and find 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?

52
Solution
• The side mirror position is locally
(0,4,0.3)
• The matrix of first driving
forward 5m is
1 0 0 
T1  0 1 5
0 0 1

53
Solution
• The matrix to turn to the right 30
and 45 degrees (rotating -30 and
-45 degrees around the origin)
are
 3 1   1 1 
 0  0
 2 2   2 2 
1 3 1 1
R1   0, R2   0, respectively
 2 2   2 2 
   
   
 0 0 1   0 0 1 

54
Solution
The local-to-global transformation matrix at
the last configuration of the car is
  3 1   1 1 
  0   0
 1 0 0  2 2  1 0 0  2 2 
TR1TR2     1 3
0    1 1
0
0 1 5   2 2  0 1 5   2 2 
    
    
0 0 1   0 0 1  0 0 1   0 0 1 

The final position of the side mirror can be


computed by TR1TR2 p which is around
(2.89331, 9.0214)

55
This is convenient for character
animation / robotics
• In robotics / animation, we
often want to know what is
the current 3D location of
the end effectors (like the
hand)
• Can concatenate matrices
from the origin of the body
towards the end effecter

56
Transformations of coordinate
systems.
Define P ( i ) as a point in coordinate system i
Define M i  j as the transform that converts a point in system j to a point in system i
P (i )  M i  j  P ( j )
and
P ( j )  M j k  P ( k )
we obtain by substitution :
M i k  M i  j  M j k
It can also be shown that :
M j i  M i1 j

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

Lecture 4 58
Translation in 3D.
Simple extension to the 3D case:

1 0 0 dx 
0 1 0 d y 
T (d x , d y , d z )  
0 0 1 dz 
 
0 0 0 1

59
Scale in 3D.
Simple extension to the 3D case:

s x 0 0 0
0 sy 0 0
S (s x , s y , s z )  
0 0 sz 0
 
0 0 0 1

60
Rotation in 3D
• Need to specify which axis the rotation is about.
• z-axis rotation is the same as the 2D case.

cos  sin  0 0
 sin  cos 0 0
R z ( )  
 0 0 1 0
 
 0 0 0 1

61
Rotating About the x-axis Rx()

 x  1 0 0 0  x 
     
 y   0 cos θ  sin θ 0  y 
 z    0 sin θ cos θ 0 z  
     
 1  0 0 0 1   1 
  

62
Rotating About the y-axis
Ry()

 x   cos θ 0 sin θ 0   x 
     
 y  0 1 0 0  y 
 z     sin θ 
0 cos θ 0 z
 
     
1  0 0 0 1   1 
  

63
Rotation About the z-axis
Rz()

 x   cos θ  sin θ 0 0  x 
     
 y   sin θ cos θ 0 0  y 
 z  0 0 1 0 z  
     
1  0 0 0 1   1 
  

64
Rotation about an y
Rotate(k, θ)

arbitrary axis θ
• About (ux, uy, uz), a unit
vector on an arbitrary axis u
x
z

x' uxux(1-c)+c uzux(1-c)-uzs uxuz(1-c)+uys 0 x


y' uyux(1-c)+uzs uzux(1-c)+c uyuz(1-c)-uxs 0 y
=
z' uzux(1-c)-uys uyuz(1-c)+uxs uzuz(1-c)+c 0 z
1 0 0 0 1 1

where c = cos θ & s = sin θ


65
Rotation
• Not commutative if the axis of rotation are not
parallel

Rx ( ) R y (  )  R y (  ) Rx ( )
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

67
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

68
Normal Vectors
• We also need to know the direction of the normal
vectors in the world coordinate system
• This is going to be used at the shading operation
• We only want to rotate the normal vector
• Do not want to translate it

69
Normal Vectors - (2)
• We need to set elements of the translation
part to zero

r11 r11 r11 t x  r11 r11 r11 0


r r11 r11  
t y  r11 r11 r11 
0
 11 
r11 r11 r11 t z  r11 r11 r11 0
   
0 0 0 1  0 0 0 1

70
Viewing
• Now we have the world coordinates of all
the vertices
• Now we want to convert the scene so that it
appears in front of the camera

71
View Transformation
We want to know the positions in the camera coordinate system
We can compute the camera-to-world transformation matrix using
the orientation and translation of the camera from the origin of the
world coordinate system

Mw←c

Lecture 4 72
View Transformation
We want to know the positions in the camera coordinate system

vw = Mw←c vc Point in the


camera coordinate

Point in the Camera-to-world


world coordinate transformation

vc = M -1
w←c vw
= Mc ←w vw

73
Summary.
• Transformations: translation, rotation and scaling
• Using homogeneous transformation, 2D (3D)
transformations can be represented by multiplication of a
3x3 (4x4) matrix.

• Multiplication from left-to-right can be considered as the


transformation of the coordinate system

• Need to multiply the camera matrix from the left at the


end

• Reading: Foley et al. Chapter 5, Appendix 2 sections A1 to


A5 for revision and further background (Chapter 5)
74

Potrebbero piacerti anche