<<

9/22 /2011

Transformations.

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

Lecture 2 1 2

Types of Transformations

– Once the models are prepared, we need to place them in the environment – Geometric Transformations – Objects are defined in their own local coordinate • system • • scaling – We need to translate, rotate and scale them to put them •• Linear (preserves parallel lines) into the world coordinate system • Non-uniform scales, shears or skews – Projection (preserves lines) • Perspective projection • Parallel projection – Non-linear (lines become curves) • Twists, bends, warps, morphs,

3 22/09/2011 Lecture 1 4

2D Translations. 2D Scaling from the origin.

Point P defined as yxP ),,( Point P defined as P(x, y), ′′′ ′′′ Perform a scale (stretch) to Point yxP ),( aby factor sx along the x axis, translate to Point yxP ),( a distance d x parallel to x axis, d y parallel to axis.y along s and s along the axis.y ′ += ′ += y x dyydxx y ′ = ,. ′ = .ysyxsx Define the column vec tors x y Define the P’ ′ P x x  d x  P = , P′ = ,T =   s 0     ′ = x y y  dy  S   P’  0 sy  Now P Now P′ = P +T x′ s 0  x ′ ⋅= = x PSP or  ′  .  y   0 sy  y

5 6

1 9/22 /2011

2D Rotation about the origin. 2D Rotation about the origin.

y y θ P’(x’,y’) P’(x’,y’)

P(x,y) P(x,y) r r = rx cos. φ = ry sin. φ θ y r φ r x x x

7 8

2D Rotation about the origin. 2D Rotation about the origin.

x′ = r cos(. θ +φ = r cos.) φ cos. θ − r sin. φ sin. θ x′ = r cos(. θ +φ = r cos.) φ cos. θ − r sin. φ sin. θ ′ = θ +φ = φ θ + φ θ ′ = θ +φ = φ θ + φ θ y y r sin(. r cos.) sin. r sin. cos. y r sin(. r cos.) sin. r sin. cos.

Substituting for r : P’(x’,y’) = rx cos. φ P(x,y) = ry sin. φ r = rx cos. φ = ry sin. φ Gives us : θ y ′ = xx cos. θ − y sin. θ φ r x ′ = xy sin. θ + y cos. θ x

9 10

2D Rotation about the origin. Transformations. • Translation. ′ = xx cos. θ − y sin. θ – P′=T + P ′ = xy sin. θ + y cos. θ • Scale P′=S ⋅ P Rewriting in matrix form gives us : – • Rotation x′ cosθ −sin θx ′ θ − θ = . x  cos sin  x P′=R ⋅ P  ′  θ θ     =  .  – y sin cos y y′ sin θ cos θ  y • We would like all transformations to be multiplications so we can concatenate them cos θ − sin θ  • ⇒ express points in homogenous coordinates . Define the matrix R =  , P′ ⋅= PR sin θ cos θ 

11 12

2 9/22 /2011

Homogeneous coordinates Translations in homogenised coordinates • Add an extra coordinate, W, to a point. – P(x,y,W). • Transformation matrices for 2D translation • Two sets of represent the are now 3x3. 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   01 d x  x x = + dx (x/W,y/W,1).       x y′ = 10 d . y y′ = + dy • If W=0, point is said to be at infinity.    y    y  1   00 1  1 1 =1

13 14

Concatenation. Concatenation.

⋅ • When we perform 2 translations on the same point The matrix product (dT x1,d y1) (dT x2 ,d y2 ) is : P′ = ddT ),( ⋅ P yx 11 1 0 d x1  1 0 d x2  ′′ = ⋅ ′     P dT d yx 22 ),( P = 0 1 d y1 .0 1 d y2  ? P′′ = ddT ⋅ dT d ),(),( =⋅ (dTP + d ,d + d )⋅ P yx 11 yx 22 x1 yx 12 y2 0 0 1  0 0 1 

weSo expect : Matrix product is variously referred to as compounding , ⋅ = + + concatenation , or composition ddT yx 11 dT d yx 22 ),(),( (dT x1 d ,d yx 12 d y2 )

15 16

Concatenation. Properties of translations.

⋅ = The matrix product (dT x1,d y1) (dT x2 ,d y2 ) is : 1. T (0,0) I ⋅ = + + + 2. ssT yx ttT yx ),(),( (sT x , st yx t y ) 1 0 d x1  1 0 dx2  1 0 d x1 d x2        ⋅ = ⋅ = + 3. ssT yx ttT yx ),(),( ttT yx ssT yx ),(),( 0 1 d y1 .0 1 d y2  0 1 d y1 d y2  ),(T 4. 1- ss ),(T = T (−s ,−s ) 0 0 1  0 0 1  0 0 1  yx x y

Matrix product is variously referred to as compounding , concatenation , or composition. Note : 3. translation matrices are commutative.

17 18

3 9/22 /2011

Homogeneous form of scale. Concatenation of scales.

Recall the (x,y) form of Scale :   sx 0 ⋅ ssS ),( =   The matrix product sS 1,( syx 1) (sS x2 , sy2 ) is : yx 0 s  y  ⋅ sx1 0 0  sx2 0 0  sx1 sx2 0 0     =  ⋅  In homogeneous coordinates :  0 sy1 0 . 0 sy2 0   0 sy1 sy2 0       sx 0 0  0 0 1   0 0 1   0 0 1 =   ssS yx ),(  0 sy 0 Only diagonal elements in the matrix - easy to multiply!  0 0 1

19 20

Homogeneous form of rotation. Orthogonality of rotation matrices. x′ cos θ − sin θ 0  x       y′ = sin θ cos θ 0 . y       cos θ − sin θ 0   cos θ sin θ 0   1   0 0 1  1       θ =  θ θ  T θ −= θ θ  R )( sin cos 0 , R )(  sin cos 0  For rotation matrices,  0 0 1   0 0 1  −1 θ = −θ R )( R( ). cos −θ − sin −θ 0   cos θ sin θ 0  −θ =  −θ −θ  −= θ θ  Rotation matrices are orthogonal , i.e : R )( sin cos 0   sin cos 0  R−1 θ )( = RT θ )(  0 0 1   0 0 1 

21 22

How are transforms combined?

Other properties of rotation. Scale then Translate R )0( = I (5,3) Scale(2,2) (2,2) Translate(3,1) R θ ⋅ R φ)()( = R(θ +φ) (1,1) (3,1) (0,0) (0,0)

and Use matrix multiplication: p' = T ( S p ) = TS p θ ⋅ φ = φ ⋅ θ R R )()( R R )()( 1 0 3 2 0 0 2 0 3 But this is only because the axis of rotation TS = 0 1 1 0 2 0 = 0 2 1 same theis same 0 0 1 0 0 1 0 0 1 For 3D rotations , need to be more careful Caution: matrix multiplication is NOT commutative! 23 02/10/09 Lecture 4 24

4 9/22 /2011

Non-commutative Composition Non-commutative Composition

Scale then Translate: p' = T ( S p ) = TS p Scale then Translate: p' = T ( S p ) = TS p

(5,3) 1 0 3 2 0 0 2 0 3 Scale(2,2) (2,2) Translate(3,1) (1,1) (3,1) TS = 0 1 1 0 2 0 = 0 2 1 (0,0) (0,0) 0 0 1 0 0 1 0 0 1

Translate then Scale: p' = S ( T p ) = ST p Translate then Scale: p' = S ( T p ) = ST p

(8,4) 2 0 0 1 0 3 2 0 6 (4,2) Translate(3,1) Scale(2,2) (6,2) (1,1) (3,1) ST = 0 2 0 0 1 1 = 0 2 2

(0,0) 0 0 1 0 0 1 0 0 1

02/10/09 Lecture 4 25 02/10/09 Lecture 4 26

How are transforms combined? Non-commutative Composition

Rotate then Translate Rotate then Translate: p' = T ( R p ) = TR p

(3,sqrt(2)) 1 0 3 0 -1 3 (0,sqrt(2)) 0 -1 0 (1,1) Rotate 45 deg Translate(3,0) TR = 0 1 1 1 0 0 0 0 1 (0,0) (0,0) (3,0) 0 0 1 0 0 1 0 0 1 Translate then Rotate = Translate then Rotate: p' = R ( T p ) = RT p

(1,1) Translate(3,0) (3/sqrt(2),3/sqrt(2)) Rotate 45 deg 0 -1 0 1 0 3 0 -1 -1 (0,0) (0,0) (3,0) RT = 1 0 0 0 1 1 = 1 0 3 0 0 1 0 0 1 0 0 1 Caution: matrix multiplication is NOT commutative! 02/10/09 Lecture 4 27 02/10/09 Lecture 4 28

Types of transformations. Transformations of coordinate systems. • Rotation and translation • Have been discussing transformations as – and distances are preserved transforming points. – Unit cube is always unit cube • Always need to think the transformation in the – Rigid-Body transformations. world coordinate system • Rotation, translation and scale. • Sometimes this might not be so convenient – Angles & distances not preserved. – i.e. rotating objects at its location – But parallel lines are.

29 30

5 9/22 /2011

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

31 32

Quiz Solution • I sat in the car, and find the side mirror is 0.4m on • The side mirror position is locally my right and 0.3m in my front (0,4,0.3) • I started my car and drove 5m forward, turned 30 • The matrix of first driving degrees to right, moved 5m forward again, and forward 5m is turned 45 degrees to the right, and stopped  001  • What is the position of the side mirror now,   T = 510 relative to where I was sitting in the beginning? 1    100 

33 34

Solution Solution

• The matrix to turn to the right The local-to-global transformation matrix at the last configuration of the car is 30 and 45 degrees (rotating -30   3 1   1 1    0  0 and -45 degrees around the  001  2 2  001  2 2    1 3   1 1  TRTR = − 0 − 0 21  510  2 2  510  2 2  origin) are            100  100  100  100   3 1   1 1   0  0  2 2   2 2   1 3   1 1  The final position of the side mirror can be R −= 0 , R −= 0 , respective ly 1  2 2  2  2 2  computed by TR 1TR 2 p which is around         (2.89331, 9.0214)  0 10   0 10 

35 36

6 9/22 /2011

This is convenient for character animation / robotics Transformations of coordinate • In robotics / animation, we systems. often want to know what is Define P i)( as a point in coordinate system i

the current 3D location of Define M ← ji theas transform that converts a point in system toj a point in system i P i)( = M ⋅ P j)( the end effectors (like the ← ji hand) and j)( = ⋅ k )( P M ←kj P • Can concatenate matrices we obtain by substituti :on = ⋅ from the origin of the body M ← M ← M ←kjjiki towards the end effecter It alsocan be shown that : = −1 M ← M ← jiij

37 Lecture 4 38

3D Transformations. Translation in 3D. • Use homogeneous coordinates, just as in 2D case. • Transformations are now 4x4 matrices. Simple extension to the 3D case: • We will use a right-handed (world) coordinate

system - ( z out of page ). 1 0 0 d x    0 1 0 d dT d d ),,( =  y  zyx 0 0 1 d   z  0 0 0 1 

02/10/09 Lecture 4 39 40

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

s x 0 0 0 θ − θ   cos sin 00  0 s 0 0  θ θ  =  y  sin cos 00  sssS zyx ),,( R θ )( =   z 0 0 s z 0  0 0 01       0 0 0 1  0 0 10 

41 42

7 9/22 /2011

θ Rotating About the y-axis Rotating About the x-axis R x( ) θ Ry( )

 x  1 0 0 0  x x′  cos θ 0 sin θ 0  x             y′ 0 cos θ −sin θ 0 y y  0 1 0 0 y   = ⋅    = ⋅   ′  θ θ     ′ − θ θ     z  0 sin cos 0  z z   sin 0 cos 0  z              1  0 0 0 1 1  1  0 0 0 1 1

43 44

Rotation about an Rotate( k, θ) Rotation About the z-axis y arbitrary axis θ θ Rz( ) • About ( ux, uy, uz), a unit vector on an arbitrary axis u x z

 x′ cos θ −sin θ 00   x 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  y  sin cos 00   y =   = ⋅  z' uzux(1-c)-uys uyuz(1-c)+u xs uzuz(1-c)+c 0 z  z   0 0 1 0  z        1   0 0 10  1 1 0 0 0 1 1

where c = cos θ & s = sin θ

45 46

Rotation Shearing

• Not commutative if the axis of rotation are not parallel α β ≠ β α Rx ( )Ry ( ) Ry ( )Rx ( ) 1 a 00   0001      a 001  0010     0100   0100       1000   1000 

02/10/09 48

8 9/22 /2011

Calculating the world Normal Vectors • We also need to know the direction of the normal coordinates of all vertices vectors in the world coordinate system • For each object, there is a local-to-global transformation matrix • This is going to be used at the shading operation • We only want to rotate the normal vector • So we apply the transformations to all the vertices of each object • Do not want to translate it • We now know the world coordinates of all the points in the scene

49 50

Normal Vectors - (2) Viewing • We need to set elements of the translation • Now we have the world coordinates of all part to zero the vertices • Now we want to convert the scene so that it appears in front of the camera

r11 r11 r11 tx  r11 r11 r11 0     r r r t r r r 0  11 11 11 y  ⇒  11 11 11  r r r t  r r r 0  11 11 11 z   11 11 11   0 0 0 1   0 0 0 1

51 52

View Transformation View Transformation

We want to know the positions in the camera coordinate system 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 vw = Mw←c vc Point in the camera coordinate world coordinate system Point in the Camera-to-world world coordinate transformation Mw←c -1 vc = Mw ← c vw

= Mc ← w vw

Lecture 4 53 54

9 9/22 /2011

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)

55

10