Upcoming SlideShare
×

# Geometric transformation cg

1,101 views

Published on

Published in: Education
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
1,101
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
0
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Geometric transformation cg

1. 1. Introduction to Computer Graphics CS 445 / 645 Lecture 5 TransformationsM.C. Escher – Smaller and Smaller (1956)
2. 2. Modeling TransformationsSpecify transformations for objects • Allows definitions of objects in own coordinate systems • Allows use of object definition multiple times in a scene – Remember how OpenGL provides a transformation stack because they are so frequently reusedChapter 5 from Hearn and Baker H&B Figure 109
3. 3. Overview2D Transformations • Basic 2D transformations • Matrix representation • Matrix composition3D Transformations • Basic 3D transformations • Same as 2D
4. 4. 2D Modeling Transformations Modeling Coordinates Scale y Translate x Scale Rotate Translate World Coordinates
5. 5. 2D Modeling Transformations Modeling Coordinates y x Let’s look at this in detail… World Coordinates
6. 6. 2D Modeling Transformations Modeling Coordinates y x Initial location at (0, 0) with x- and y-axes aligned
7. 7. 2D Modeling Transformations Modeling Coordinates y x Scale .3, .3 Rotate -90 Translate 5, 3
8. 8. 2D Modeling Transformations Modeling Coordinates y x Scale .3, .3 Rotate -90 Translate 5, 3
9. 9. 2D Modeling Transformations Modeling Coordinates y x Scale .3, .3 Rotate -90 Translate 5, 3 World Coordinates
10. 10. ScalingScaling a coordinate means multiplying each of its components by a scalarUniform scaling means this scalar is the same for all components: ×2
11. 11. ScalingNon-uniform scaling: different scalars per component: X × 2, Y × 0.5How can we represent this in matrix form?
12. 12. Scaling Scaling operation:  x ax   y  = by      Or, in matrix form:  x  a 0  x   y  = 0  y  b      scaling matrix
13. 13. 2-D Rotation (x’, y’) (x, y) x’ = x cos(θ) - y sin(θ) θ y’ = x sin(θ) + y cos(θ)
14. 14. 2-D Rotation x = r cos (φ) y = r sin (φ) x’ = r cos (φ + θ) y’ = r sin (φ + θ) (x’, y’) Trig Identity… (x, y) x’ = r cos(φ) cos(θ) – r sin(φ) sin(θ) y’ = r sin(φ) sin(θ) + r cos(φ) cos(θ) θ φ Substitute… x’ = x cos(θ) - y sin(θ) y’ = x sin(θ) + y cos(θ)
15. 15. 2-D RotationThis is easy to capture in matrix form:  x  cos(θ ) − sin (θ )  x   y  = sin (θ ) cos(θ )  y      Even though sin(θ) and cos(θ) are nonlinear functions of θ, • x’ is a linear combination of x and y • y’ is a linear combination of x and y
16. 16. Basic 2D TransformationsTranslation: • x’ = x + tx • y’ = y + tyScale: • x’ = x * sx • y’ = y * syShear: Transformations • x’ = x + hx*y can be combined • y’ = y + hy*x (with simple algebra)Rotation: • x’ = x*cosΘ - y*sinΘ • y’ = x*sinΘ + y*cosΘ
17. 17. Basic 2D TransformationsTranslation: • x’ = x + tx • y’ = y + tyScale: • x’ = x * sx • y’ = y * syShear: • x’ = x + hx*y • y’ = y + hy*xRotation: • x’ = x*cosΘ - y*sinΘ • y’ = x*sinΘ + y*cosΘ
18. 18. Basic 2D TransformationsTranslation: • x’ = x + tx • y’ = y + tyScale: (x,y) • x’ = x * sx (x’,y’) • y’ = y * syShear: • x’ = x + hx*y x’ = x*sx • y’ = y + hy*x y’ = y*syRotation: • x’ = x*cosΘ - y*sinΘ • y’ = x*sinΘ + y*cosΘ
19. 19. Basic 2D TransformationsTranslation: • x’ = x + tx • y’ = y + tyScale: • x’ = x * sx • y’ = y * syShear: (x’,y’) • x’ = x + hx*y x’ = (x*sx)*cosΘ - (y*sy)*sinΘ • y’ = y + hy*x y’ = (x*sx)*sinΘ + (y*sy)*cosΘRotation: • x’ = x*cosΘ - y*sinΘ • y’ = x*sinΘ + y*cosΘ
20. 20. Basic 2D TransformationsTranslation: • x’ = x + tx • y’ = y + tyScale: (x’,y’) • x’ = x * sx • y’ = y * syShear: • x’ = x + hx*y x’ = ((x*sx)*cosΘ - (y*sy)*sinΘ) + tx y’ = ((x*sx)*sinΘ + (y*sy)*cosΘ) + ty • y’ = y + hy*xRotation: • x’ = x*cosΘ - y*sinΘ • y’ = x*sinΘ + y*cosΘ
21. 21. Basic 2D TransformationsTranslation: • x’ = x + tx • y’ = y + tyScale: • x’ = x * sx • y’ = y * syShear: • x’ = x + hx*y x’ = ((x*sx)*cosΘ - (y*sy)*sinΘ) + tx y’ = ((x*sx)*sinΘ + (y*sy)*cosΘ) + ty • y’ = y + hy*xRotation: • x’ = x*cosΘ - y*sinΘ • y’ = x*sinΘ + y*cosΘ
22. 22. Overview2D Transformations • Basic 2D transformations • Matrix representation • Matrix composition3D Transformations • Basic 3D transformations • Same as 2D
23. 23. Matrix RepresentationRepresent 2D transformation by a matrix a b c  d Multiply matrix by column vector ⇔ apply transformation to point  x = a b  x  x = ax + by  y  c    d  y    y = cx + dy
24. 24. Matrix RepresentationTransformations combined by multiplication  x = a b  e f  i j  x   y  c    d g  h k  l  y    Matrices are a convenient and efficient way to represent a sequence of transformations!
25. 25. 2x2 MatricesWhat types of transformations can be represented with a 2x2 matrix? 2D Identity? x = x  x = 1 0  x  y = y  y  0    1 y    2D Scale around (0,0)? x = s x * x  x  s x 0  x  y = s y * y  y  =  0 s y  y      
26. 26. 2x2 MatricesWhat types of transformations can be represented with a 2x2 matrix? 2D Rotate around (0,0)? x = cos Θ* x − sin Θ* y  x  cos Θ − sin Θ x  y = sin Θ* x + cos Θ* y  y  = sin Θ cos Θ  y       2D Shear? x = x + shx * y  x   1 shx  x  y = shy * x + y  y  = sh 1  y     y  
27. 27. 2x2 MatricesWhat types of transformations can be represented with a 2x2 matrix? 2D Mirror about 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      
28. 28. 2x2 MatricesWhat types of transformations can be represented with a 2x2 matrix? 2D Translation? x = x + t x NO! y = y + t y Only linear 2D transformations can be represented with a 2x2 matrix
29. 29. Linear TransformationsLinear transformations are combinations of … • Scale,  x a b  x   y  = c • Rotation, • Shear, and    d  y    • MirrorProperties of linear transformations: • Satisfies: T ( s1p1 + s2p 2 ) = s1T (p1 ) + s2T (p 2 ) • Origin maps to origin • Lines map to lines • Parallel lines remain parallel • Ratios are preserved • Closed under composition
30. 30. Homogeneous CoordinatesQ: How can we represent translation as a 3x3 matrix? x = x + t x y = y + t y
31. 31. Homogeneous CoordinatesHomogeneous coordinates  x • represent coordinates in 2  x  homogeneous coords   dimensions with a 3-vector  y     → y    1   Homogeneous coordinates seem unintuitive, but they make graphics operations much easier
32. 32. Homogeneous CoordinatesQ: How can we represent translation as a 3x3 matrix? x = x + t x y = y + t yA: Using the rightmost column: 1 0 tx    Translation = 0 1 ty 0 0 1  
33. 33. Translation Homogeneous CoordinatesExample of translationα  x  1 0 t x   x   x + t x   y  = 0 1 t   y  =  y + t     y    y  1  0 0 1   1   1         tx = 2 ty = 1
34. 34. Homogeneous CoordinatesAdd a 3rd coordinate to every 2D point • (x, y, w) represents a point at location (x/w, y/w) • (x, y, 0) represents a point at infinity • (0, 0, 0) is not allowed y 2 (2,1,1) or (4,2,2) or (6,3,3) 1Convenient coordinate 1 2 xsystem torepresent many usefultransformations
35. 35. Basic 2D TransformationsBasic 2D transformations as 3x3 matrices  x  s x 0 0  x   x  1 0 t x  x   y  =  0 sy 0  y   y  = 0         1 t y  y    1   0    0 11     1  0    0 1 1    Translate Scale  x cos Θ − sin Θ 0  x   x   1 shx 0  x   y  = sin Θ 0  y   y  = sh    cos Θ      y 1 0  y    1   0    0 11    1   0    0 11    Rotate Shear
36. 36. Affine TransformationsAffine transformations are combinations of … • Linear transformations, and  x  a b c  x   y  = d e f  y  • Translations w  0    0 1 w  Properties of affine transformations: • Origin does not necessarily map to origin • Lines map to lines • Parallel lines remain parallel • Ratios are preserved • Closed under composition
37. 37. Projective TransformationsProjective transformations …  x  a b c  x  • Affine transformations, and  y  = d e f  y  w g    h i w   • Projective warpsProperties of projective transformations: • Origin does not necessarily map to origin • Lines map to lines • Parallel lines do not necessarily remain parallel • Ratios are not preserved • Closed under composition
38. 38. Overview2D Transformations • Basic 2D transformations • Matrix representation • Matrix composition3D Transformations • Basic 3D transformations • Same as 2D
39. 39. Matrix CompositionTransformations can be combined by matrix multiplication  x   1 0 tx cos Θ − sin Θ 0sx 0 0  x   y  =  0 1 ty sin Θ cos Θ 0 0 sy   y  0  w  0     0 1  0   0 1 0  0 1 w    p’ = T(tx,ty) R(Θ) S(sx,sy) p
40. 40. Matrix CompositionMatrices are a convenient and efficient way to represent a sequence of transformations • General purpose representation • Hardware matrix multiply p’ = (T * (R * (S*p) ) ) p’ = (T*R*S) * p
41. 41. Matrix CompositionBe aware: order of transformations matters – Matrix multiplication is not commutative p’ = T * R * S * p “Global” “Local”
42. 42. Matrix CompositionWhat if we want to rotate and translate?• Ex: Rotate line segment by 45 degrees about endpoint a and lengthen a a
43. 43. Multiplication Order – Wrong WayOur line is defined by two endpoints • Applying a rotation of 45 degrees, R(45), affects both points • We could try to translate both endpoints to return endpoint a to its original position, but by how much? a a a Wrong Correct R(45) T(-3) R(45) T(3)
44. 44. Multiplication Order - CorrectIsolate endpoint a from rotation effects a• First translate line so a is at origin: T (-3) a• Then rotate line 45 degrees: R(45) a• Then translate back so a is where it was: T(3) a
45. 45. Matrix CompositionWill this sequence of operations work? 1 0 − 3 cos(45) − sin( 45) 0 1 0 3  a x   a x  0 1 0   sin( 45) cos(45) 0 0 1 0 a  = a      y   y  0 0 1   0   0 1  0 0 1   1   1      
46. 46. Matrix CompositionAfter correctly ordering the matricesMultiply matrices togetherWhat results is one matrix – store it (on stack)!Multiply this matrix by the vector of each vertexAll vertices easily transformed with one matrixmultiply
47. 47. Overview2D Transformations • Basic 2D transformations • Matrix representation • Matrix composition3D Transformations • Basic 3D transformations • Same as 2D
48. 48. 3D TransformationsSame idea as 2D transformations • Homogeneous coordinates: (x,y,z,w) • 4x4 transformation matrices  x   a b c d  x   y  e f g h  y   z  =  i j k l  z  w m    n o p w  
49. 49. Basic 3D Transformations  x  s x 0 0 0  x   x 1 0 0 0  x   y   0  y  0 1 0 0  y   = sy 0 0  y     z  = 0 0 1 0  z   z   0 0 sz 0  z  w  0    0 0 1w        w   0 0 0 1w  Identity Scale  x  1 0 0 t x  x   y  0  x −1 0 0 0 x   = 1 0 t y  y     y   0 1 0 0 y   z  0 0 1 t z  z   z =  0 0 1 0 z       w   0    0 0 1 w   w  0 0 0 1 w  Translation Mirror about Y/Z plane
50. 50. Basic 3D Transformations  x cos Θ − sin Θ 0 0 x  Rotate around Z axis:  y  sin Θ cos Θ 0 0 y   z  =  0 0 1 0 z  w   0    0 0 1w    x   cos Θ 0 sin Θ 0 x   y   0 1 0 0 y  Rotate around Y axis:   =     z  − sin Θ 0 cos Θ 0 z       w   0 0 0 1w   x 1 0 0 0 x  Rotate around X axis:  y  0 cos Θ − sin Θ 0 y   z  = 0 sin Θ cos Θ 0 z  w  0    0 0 1w  
51. 51. Reverse RotationsQ: How do you undo a rotation of θ, R(θ )?A: Apply the inverse of the rotation… R-1(θ ) = R(-θ )How to construct R-1(θ ) = R(-θ )• Inside the rotation matrix: cos(θ) = cos(-θ) – The cosine elements of the inverse rotation matrix are unchanged• The sign of the sine elements will flipTherefore… R-1(θ ) = R(-θ ) = RT(θ )
52. 52. SummaryCoordinate systems• World vs. modeling coordinates2-D and 3-D transformations• Trigonometry and geometry• Matrix representations• Linear vs. affine transformationsMatrix operations• Matrix composition