48
CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ® , Fourth Edition by Donald Hearn, M. Pauline Baker, and Warren R. Carithers - Fundamentals of Computer Graphics, Third Edition by by Peter Shirley and Steve Marschner - Computer Graphics by F. S. Hill 1

CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

Embed Size (px)

Citation preview

Page 1: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

1

CMPE 466COMPUTER GRAPHICSChapter 7

2D Geometric Transformations

Instructor: D. Arifler

Material based on- Computer Graphics with OpenGL®, Fourth Edition by Donald Hearn, M. Pauline Baker, and Warren R. Carithers- Fundamentals of Computer Graphics, Third Edition by by Peter Shirley and Steve Marschner- Computer Graphics by F. S. Hill

Page 2: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

2

Basic geometric transformations

• Translation• Rotation• Scaling

Page 3: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

3

2D translation

Figure 7-1 Translating a point from position P to position P’ using a translation vector T.

Page 4: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

4

2D translation equations

Translation is a rigid-body transformation: Objects are moved withoutdeformation.

Page 5: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

5

2D translation exampleFigure 7-2 Moving a polygon from position (a) to position (b) with the translation vector (−5.50, 3.75).

Page 6: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

6

2D translation example program

Page 7: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

7

2D rotation

• All points of the object are transformed to new positions by rotating the points through a specified rotation angle about the rotation axis (in 2D, rotation pivot or pivot point)

Figure 7-3 Rotation of an object through angle θ about the pivot point (xr , yr ).

Page 8: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

8

2D rotation

Figure 7-4 Rotation of a point from position (x, y ) to position (x', y' ) through an angle θ relative to the coordinate origin. The original angular displacement of the point from the x axis is Φ.

Setting

we have

Page 9: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

9

2D rotation in matrix form

Rotation is a rigid-body transformation: Objects are moved withoutdeformation.

Equations can be compactly expressed in matrix form:

Page 10: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

10

Rotation about a general pivot pointFigure 7-5 Rotating a point from position (x , y ) to position (x' , y' ) through an angle θ about rotation point (xr , yr ).

Page 11: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

11

2D rotation example

Page 12: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

12

2D scaling

sx and sy are scaling factorssx scales an object in x directionsy scales an object in y direction

If sx=sy, we have uniform scaling: Object proportions are maintained.If sx≠sy, we have differential scaling. Negative scaling factors resizes and reflects the object about one or more of the coordinate axes.

Page 13: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

13

2D scaling

Figure 7-6 Turning a square (a) into a rectangle (b) with scaling factors sx = 2 and sy = 1.

Scaling factors greater than 1 produce enlargements.

Page 14: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

14

2D scaling

Figure 7-7 A line scaled with Equation 7-12 using sx = sy = 0.5 is reduced in size and moved closer to the coordinate origin.

Positive scaling values less than 1 reduce the size of objects.

Page 15: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

15

Scaling relative to a fixed pointFigure 7-8 Scaling relative to a chosen fixed point (xf , yf ). The distance from each polygon vertex to the fixed point is scaled by Equations 7-13.

Fixed point remainsunchanged after the scaling transformation.

Page 16: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

16

2D scaling relative to a fixed point

Page 17: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

17

2D scaling example

Page 18: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

18

Matrix representations and homogeneous coordinates• Multiplicative and translational terms for a 2D

transformation can be combined into a single matrix• This expands representations to 3x3 matrices

• Third column is used for translation terms

• Result: All transformation equations can be expressed as matrix multiplications

• Homogeneous coordinates: (xh, yh, h)• Carry out operations on points and vectors “homogeneously”• h: Non-zero homogeneous parameter such that

• We can also write: (hx, hy, h)• h=1 is a convenient choice so that we have (x, y, 1)• Other values of h are useful in 3D viewing transformations

Page 19: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

19

2D translation matrix

Page 20: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

20

2D rotation matrix

Page 21: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

21

2D scaling matrix

Page 22: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

22

Inverse transformations

Inverse translation

Inverse rotation

Inverse scaling

Page 23: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

23

Composite transformations

• Composite transformation matrix is formed by calculating the product of individual transformations

• Successive translations (additive)

Page 24: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

24

Composite transformations

• Successive rotations (additive)

• Successive scaling (multiplicative)

Page 25: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

25

2D pivot-point rotationFigure 7-9 A transformation sequence for rotating an object about a specified pivot point using the rotation matrix R(θ) of transformation 7-19.

Page 26: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

26

2D pivot-point rotation

Note the order of operations:

Page 27: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

27

2D fixed-point scaling

Figure 7-10 A transformation sequence for scaling an object with respect to a specified fixed position using the scaling matrix S(sx , sy ) of transformation 7-21.

Page 28: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

28

2D fixed-point scaling

Page 29: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

29

Matrix concatenation properties

• Multiplication is associative

• Multiplication is NOT commutative• Unless the sequence of transformations are all of the same kind• M2M1 is not equal to M1M2 in general

Page 30: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

30

Computational efficiency

• Formulation of a concatenated matrix may be more efficient• Requires fewer multiply/add operations

• Rotation calculations require trigonometric evaluations• In animations with small-angle rotations, approximations (e.g.

power series) and iterative calculations can reduce complexity

Page 31: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

31

More on 2D rotationFigure 7-14 The rotation matrix for revolving an object from position (a) to position (b) can be constructed with the values of the unit orientation vectors u' and v' relative to the original orientation.

Assign the elements of u’ to the first row of the rotation matrix and the elements of v’ to the second row.

Page 32: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

32

Other transformations: reflection

Figure 7-16 Reflection of an object about the x axis.

Page 33: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

33

Reflection

Figure 7-17 Reflection of an object about the y axis.

Page 34: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

34

ReflectionFigure 7-18 Reflection of an object relative to the coordinate origin. This transformation can be accomplished with a rotation in the xy plane about the coordinate origin.

Page 35: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

35

ReflectionFigure 7-19 Reflection of an object relative to an axis perpendicular to the xy plane and passing through point Preflect.

Page 36: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

36

Reflection

Figure 7-20 Reflection of an object with respect to the line y = x .

Page 37: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

37

Other transformations: shear

• Distorts the shape of an object such that the transformed shape appears as if the object were composed of internal layers that had been caused to slide over each other

Figure 7-23 A unit square (a) is converted to a parallelogram (b) using the x -direction shear matrix 7-57 with shx = 2.

Page 38: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

38

ShearFigure 7-24 A unit square (a) is transformed to a shifted parallelogram (b) with shx = 0.5 and yref = −1 in the shear matrix 7-59.

Page 39: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

39

ShearFigure 7-25 A unit square (a) is turned into a shifted parallelogram (b) with parameter values shy = 0.5 and xref = −1 in the y -direction shearing transformation 7-61.

Page 40: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

40

Transformations between 2D coordinate systems

Figure 7-30 A Cartesian x' y' system positioned at (x0, y0) with orientation θ in an xy Cartesian system.

Figure 7-31 Position of the reference frames shown in Figure 7-30 after translating the origin of the x' y' system to the coordinate origin of the xy system.

Page 41: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

41

Transformations between coordinate systems

Page 42: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

42

Alternative method

Figure 7-32 Cartesian system x' y' with origin at P0 = (x0, y0) and y' axis parallel to vector V.

Page 43: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

43

Transformations

Figure 7-33 A Cartesian x ' y' system defined with two coordinate positions, P0 and P1, within an xy reference frame.

Page 44: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

44

OpenGL matrix operations• glMatrixMode ( GL_MODELVIEW )

• Designates the matrix that is to be used for projection transformation (current matrix)

• glLoadIdentity ( )• Assigns the identity matrix to the current matrix

• Note: OpenGL stores matrices in column-major order• Reference to a matrix element mjk in OpenGL is a reference to the

element in column j and row k• glMultMatrix* ( ) post-multiplies the current matrix

• In OpenGL, the transformation specified last is the one applied first

Page 45: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

45

OpenGL transformation example

Page 46: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

46

OpenGL transformation exampleFigure 7-34 Translating a rectangle using the OpenGL function glTranslatef (−200.0, −50.0, 0.0).

Page 47: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

47

OpenGL transformation exampleFigure 7-35 Rotating a rectangle about the z axis using the OpenGL function glRotatef (90.0, 0.0, 0.0, 1.0).

Page 48: CMPE 466 COMPUTER GRAPHICS Chapter 7 2D Geometric Transformations Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth Edition

48

OpenGL transformation example

Figure 7-36 Scaling and reflecting a rectangle using the OpenGL function glScalef (−0.5, 1.0, 1.0).