Chapter 5 Geometric Transformations Topics 5.1 Basic Two-Dimensional Geometric Transformations 5.2 Matrix Representations and Homogeneous Coordinates 5.3 Inverse Transformations 5.4 Two-Dimensional Composite Transformations 5.5 Other Two-Dimensional Transformations 5.6 Raster Methods for Geometric Transformations 5.7 OpenGL Raster Transformations 5.8 Transformations between Two-Dimensional Coordinate Systems 5.9 Geometric Transformations in Three-Dimensional Space 5.10 Three-Dimensional Translation 5.11 Three-Dimensional Rotation 5.12 Three-Dimensional Scaling 5.13 Composite Three-Dimensional Transformations 5.14 Other Three-Dimensional Transformations 5.15 Transformations between Three-Dimensional Coordinate Systems 5.16 Affine Transformations 5.17 OpenGL Geometric-Transformation Functions 5.18 Summary 5.1 Basic Two-Dimensional Geometric Transformations Operations that are applied to the geometric description of an object to change its position, orientation, or size are called geometric transformations. Geometric transformations can be used to describe how objects might move around in a scene during an animation sequence or simply to view them from another angle. geometric transformations Translation Rotation Scaling Reflection shearing Two-Dimensional Translation We perform a translation on a single coordinate point by adding offsets to its coordinates so as to generate a new coordinate position. To translate a two-dimensional position, we add translation distances, tx and ty to the original coordinates (x,y) to obtain the new coordinate position (x’,y’), x x tx ' y y ty ' The two-dimensional translation equations in the matrix form จากรู ป x x tx ให้ y y y ty x P y ' ' ' ' ' P (x , y ) T P ( x, y ) t x T t y และ ty tx x จะได้ ' x ' P ' y x ' x t x ' y y t y P P T ' Two-Dimensional Rotation We generate a rotation transformation of an object by specifying a rotation axis and a rotation angle. A two-dimensional rotation of an object is obtained by repositioning the object along a circular path in the xy plane. Parameters for the two-dimensional rotation are The rotation angle θ A position (x,y) – rotation point (pivot point) The two-dimensional rotation x r cos( ) r cos cos r sin sin ' y r sin( ) r cos sin r sin cos ' y Polar coordinate system x r cos ' ' ' y r sin O (x , y ) O ( x, y ) P ( 0 ,0 ) x x x cos y sin ' y x sin y cos ' The two-dimensional rotation x ' cos - sin x ' sin cos y y y ' ' ' O R O ' O (x , y ) O ( x, y ) โดยที่ x P ( 0 ,0 ) และ cos - sin R sin cos x O y Rotation ' x ' O ' y matrix Ex. 1 1.0 0.5 Rotation of a point about an arbitrary pivot position x ( x x r ) cos ( y y r ) sin x r ' y ( x x r ) sin ( y y r ) cos y r ' y ' ' ' O (x , y ) x ' cos - sin x x r x r ' cos y y r y r y sin O ( x, y ) O R O P ' * P ( xr , yr ) x โดยที่ ' x x * O ' y y Two-Dimensional Scaling To alter the size of an object, we apply a scaling transformation. A simple twodimensional scaling operation is performed by multiplying object positions (x,y) by scaling factors sx and sy to produce the transformed coordinates (x’,y’). x x sx ' y y sy ' x' sx 0 x ' y 0 s y y P SP ' Any positive values can be assigned to the scaling factors. Values less than 1 reduce the size of object; Values greater than 1 produce enlargements. Uniform scaling – scaling values have the same value Differential scaling – unequal of the scaling factor Scaling relative to a chosen fixed point x x s x x f (1 s x ) ' y y s y y f (1 s y ) ' y P1 Pf ( x f , y f ) x ' s x 0 x 1 s x 0 x f ' 0 s 0 1 -s y y y y yf P2 P S P S Pf ' P3 x * 5.2 Matrix Representations and Homogeneous Coordinates Many graphics applications involve sequences of geometric transformations. Hence we consider how the matrix representations can be reformulated so that such transformation sequence can be efficiently processed. Each of three basic two-dimensional transformations (translation, rotation and scaling) can be expressed in the general matrix form P and P ’ = column vectors, coordinate position ' P M1 = 2 by 2 array containing multiplicative factors, for translation M1 is the identity matrix M2 = two-element column matrix containing translational terms, for rotation or scaling M2 contains the translational terms associated with the pivot point or scaling fixed point M1 P M 2 P M1 P M 2 ' x ' 1 ' y 0 0 x t x t 1 y y Translation x ' cos - sin x x r x r ' y sin cos y y r y r Rotation x ' cos - sin x cos - sin x r x r ' y sin cos y sin cos y r y r x ' s x 0 x 1 s x 0 x f ' 0 s 0 1 -s y y y y yf Scaling To produce a sequence of transformations such as scaling followed by rotation then translation, we could calculate the transformed coordinates one step at a time. A more efficient approach is to combine the transformations so that the final coordinate positions are obtained directly from the initial coordinates, without calculating intermediate coordinate values. Homogeneous Coordinates Multiplicative and translational terms for a two-dimensional geometric transformations can be combined into a single matrix if we expand the representations to 3 by 3 matrices. Then we can use the third column of a transformation matrix for the translation terms, and all transformation equations can be expressed as matrix multiplications. But to do so, we also need to expand the matrix representation for a two-dimensional coordinate position to a three-element column matrix. A standard technique for accomplishing this is to expand each two-dimensional coordinateposition representation (x,y) to a threeelement representation (xh,yh,h), called homogeneous coordinates, where the homogeneous parameter h is a nonzero value such that xh yh x , y h h A convenient choice is simply to set h=1. Each two-dimensional position is then represented with homogeneous coordinate (x,y,1). The term “homogeneous coordinates” is used in mathematics to refer to the effect of this representation on Cartesian equations. x' 1 ' y 0 1 0 0 1 0 tx x ty y 1 1 Translation P T (t x , t y ) P ' Rotation x sx 0 0 x ' y 0 s y 0 y 1 1 0 0 1 P R ( ) P ' ' P S (sx , s y ) P ' x ' cos - sin ' y sin cos 1 0 0 Scaling 0x 0 y 1 1 5.3 Inverse Transformations T 1 1 0 0 0 - tx 1 - ty 0 1 Inverse translation matrix Inverse rotation matrix R 1 cos sin sin cos 0 0 0 0 1 S Inverse scaling matrix 1 1 s 0 0 x 1 0 0 sy 0 0 1 5.4 Two-Dimensional Composite Transformations Using matrix representations, we can set up a sequence of transformations as a composite transformation matrix by calculating the product of the individual transformations. Thus, if we ant to apply two transformations to point position P, the transformed location would be calculated as P M 2 M1P ' P M P ' (1,3) (2,3) (3,2) (-2,1) (1,1) (-1,1) (2,1) (a) (b) (-2,-1) (-1,-1) (1.63,2.37) (0.77,1.87) (-1.37,0.37) (2.63,0.63) (-2.23,-0.13) (1.77,0.13) (c) (-0.37,-1.37) (-1.23,-1.87) (d) Composite Two-dimensional Translations P T ( t 2 x , t 2 y ) {T ( t1 x , t1 y ) P } ' P {T ( t 2 x , t 2 y ) T ( t1 x , t1 y )} P ' composite transform ation matrix 1 0 t 2 x 1 0 t1 x 1 0 t1 x t 2 x 0 1 t 2 y 0 1 t1 y 0 1 t1 y t 2 y 0 0 1 0 0 1 0 0 1 T ( t 2 x , t 2 y ) T ( t1 x , t1 y ) T ( t1 x t 2 x , t1 y t 2 y ) Composite Two-dimensional Rotations P R ( 2 ) { R ( 1 ) P } ' P { R ( 2 ) R ( 1 )} P ' composite transform ation matrix R ( 2 ) R ( 1 ) R ( 1 2 ) Composite Two-dimensional Scaling composite transform ation matrix S2x 0 0 S1x 0 0 S1x S 2 x 0 0 S 2 y 0 0 S1 y 0 0 S1 y S 2 y 0 0 0 0 0 1 0 1 0 0 1 S ( s 2 x , s 2 y ) S ( s1 x , s1 y ) S ( s1 x s 2 x , s1 y s 2 y ) General Two-dimensional PivotPoint Rotation A transformation sequence for rotating an object about a sepcified pivot point using the rotation matrix R(θ). Translate the object so that the pivot-point position is moved to the coordinate origin. Rotate the object about the coordinate origin. Translate the object so that the pivot point is returned to its original position. (xr,yr) (a) (xr,yr) (b) (xr,yr) (c) (xr,yr) (d) 1 0 0 0 1 0 x r cos - sin y r sin cos 0 1 0 cos - sin sin cos 0 0 0 1 0 0 1 0 0 - xr 1 - yr 0 1 x r ( 1- cos θ) y r sin θ y r ( 1- cos θ) x r sin θ 1 T ( x r , y r ) R ( ) T ( x r , y r ) R ( x r , y r , ) General Two-dimensional FixedPoint Scaling A transformation sequence to produce a twodimensional scaling with respect to a selected fixed position (xf,yf). Translate the object so that the fixed point coincides with the coordinate origin. Scale the object with respect to the coordinate origin. Use the inverse of the translation in step (1) to return the object to its original position. (xf,yf) (a) (xf,yf) (b) (xf,yf) (c) (xf,yf) (d) 1 0 0 1 0 0 x f s x 0 0 1 0 - x f y f 0 s y 0 0 1 - y f 1 0 0 1 0 0 1 s x 0 x f ( 1-s x ) 1 s y y f ( 1-s y ) 0 0 1 T ( x f , y f ) S ( s x , s y ) T ( x f , y f ) S ( x f , y f , ( s x , s y )) General Two-dimensional Scaling Directions We can scale an object in other directions by rotating the object to align the desired scaling directions with the coordinate axes before applying the scaling transformation. Suppose we want to apply scaling factors with values specified by parameters s1 and s2 in the directions shown in fig. y s2 x s1 The composite matrix resulting from the product of - rotation so that the directions for s1 and s2 coincide with the x and y axes - scaling transformation S(s1,s2) - opposite rotation to return points to their original orientations s1Cos 2 s 2 Sin 2 -1 R ( ) S ( s1 , s 2 ) R ( ) ( s 2 s1 ) Cos Sin 0 ( s 2 s1 ) Cos Sin s1 Sin s 2 Cos 2 2 0 0 0 1 y y (2,2) (1/2,3/2) (0,1) (1,1) (3/2,1/2) (0,0) (1,0) s1= 1 x s2=2 (0,0) x Matrix Concatenation Properties Transformation products may not be commutative. The matrix product M2M1 is not equal to M1M2. This means that if we want to translate and rotate an object, we must be careful about the order in which the composite matrix is evaluated. Reversing the order in which a sequence of transformations is performed may affect the transformed position of an object. 2 Rotation 1 Translation 1 Rotation 2 Translation General Two-dimensional Composite Transformations and Computational Efficiency x ' rs xx ' y rs yx 1 0 rs xy trs x x rs yy trs y y 0 1 1 rs** are the multiplicative rotation-scaling terms (rotation angles, scaling factors) trsx and trsy are the translation terms (translation distances, pivot-point and fixedpoint coordinates, rotation angles, scaling parameters) Example Scale and rotate about its centroid coordinates (xc,yc) and translate T (t x , t y ) R ( x c , y c , ) S ( x c , y c , s x , s y ) s x cos s x sin 0 - s y sin s y cos 0 x c (1 s x cos ) y c s y sin t x y c (1 s y cos ) x c s x sin t y 1 Two-dimensional Rigid-body Transformation If a transformation matrix includes only translation and rotation parameters, it is a rigid-body transformation matrix. rxx r yx 0 rxy tr x r yy tr y 0 1 r** are the multiplicative rotation terms (rotation angles) trx and try are the translation terms (translation distances, pivot-point and fixed-point coordinates, rotation angles) 5.5 Other Two-Dimensional Transformations Reflection For a two-dimensional reflection, the image is generated relative to an axis of reflection by rotating the object 180º about the reflection axis. Reflection about the line y=0 (the x axis) 1 0 0 0 -1 0 0 0 1 Reflection about the line x=0 (the y axis) 1 0 0 0 1 0 0 0 1 Reflection about any reflection point in the xy plane Reflection point 1 0 0 0 -1 0 0 1 0 Reflection about the reflection axis y=x 0 1 0 0 0 0 0 1 1 1. Rotate the line y=x with respect to the original through a 45 angle 2. Reflection with respect to the x axis 3. Rotate the line y=x black to its original position with a counterclockwise rotation through 45 Reflection about the reflection axis y=-x 0 -1 0 -1 0 0 0 0 1 1. Clockwise rotate the line y=-x with respect to the original through a 45 angle 2. Reflection with respect to the y axis 3. Rotate the line y=-x black to its original position with a counterclockwise rotation through 45 y = -x Shear A transformation that 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 is called a shear. An x-direction shear 1 0 0 sh x 0 1 0 0 1 transforma tion matrix y (0,1) y (2,1) (1,1) shx = 2 (0,0) (1,0) (0,0) (1,0) (3,1) We can generate x-direction shears relative to other reference lines sh x -sh x y ref 1 0 0 1 1 0 0 y (0,1) transforma tion matrix y (1,1) (1,1) (2,1) shx = ½ yref = -1 (0,0) (1,0) yref = -1 (0,0) (1/2,0) yref = -1 (3/2,0) A y-direction shears relative to the line x=xref is generated with 1 sh y 0 0 0 1 -sh y x ref 0 1 y transforma tion matrix y (1,2) (0,3/2) (0,1) (1,1) (1,1) shy = ½ xref = -1 xref = -1 (0,0) (1,0) (0,1/2) xref = -1 (0,0) Shear operations can be expressed as sequences of basic transformations. The x-direction shear matrix 1 0 0 sh x 0 1 0 0 1 can be represented as a composite transformation involving a series of rotation and scaling metrices. 5.6 Raster Methods for Geometric Transformations Functions that manipulate rectangular pixel arrays are called raster operations and moving a block of pixel values from one position to another is termed a block transfer, a bitblt or a pixblt. All bit settings in the rectangular area are copied as a block into another part of the frame buffer. We can erase the pattern at the original location by assigning the background color to all pixels within that block. Array rotations that are not multiples of 90º 1 4 7 10 3 5 6 8 9 11 12 2 (a) 3 6 2 5 1 4 9 12 8 11 7 10 12 11 10 9 8 7 6 5 4 3 2 1 (b) (c) The original array is shown in (a), the positions of the array elements after a 90º counterclockwise rotation are shown in (b), and the position of the array elements after a 180º rotation are shown in (c). 5.7 OpenGL Raster Transformations A translation of a rectangular array of pixelcolor values from one buffer area to another can be accomplished in OpenGL as a copy operation: glCopyPixels(xmin, ymin, width, height, GL_COLOR); This array of pixels is to be copied to a rectangular area of a refresh buffer whose lower-left corner is at the location specified by the current raster position. We can rotate a block of pixel-color values in 90-degree increments by first saving the block in an array, glReadPixels(xmin, ymin, width, height, GL_RGB, GL_UNSIGNED_BYTE, colorArray); then rearranging the elements of the array and placing it back in the refresh buffer. glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, colorArray); We set the scaling facters with glPixelZoom(sx,sy); 5.8 Transformations between TwoDimensional Coordinate Systems y’ y x’ θ yn xn x A Cartesian x’y’ system specified with coordinate origin (xn,yn) and orientation angle θ in a Cartesian xy reference frame. To transform object descriptions from xy coordinates to x’y’ coordinates, we set up a transformation that superimposes the axes onto the x’y’ axes. This is done in two steps: Translate so that the origin (xn,yn) of the x’y’ system is moved to the origin (0,0) of the xy system. Rotation the x’ axis onto the x axis. 1)Translatio n 1 0 x n T ( x n , y n ) 0 1 y n 0 0 1 y’ y y y’ P x’ y x’ y’ θ yn P θ xn x x x’ x 2 ) clockwise rotation cos sin 0 2.1) R ( ) - sin cos 0 0 0 1 y y’ y y’ P x’ y y P y’ θ x’ x composite matrix x x M xy , x ' y ' R ( ) T ( x n , y n ) x OR 2.2) An alternate method for describing the orientation of the x’y’ coordinate system is to specify a vector V that indicates the direction for the positive y’ axis. We can specify vector V as a point in the xy reference frame relative to the origin of the xy system, which we can convert to the unit vector, y’ V y P x’ θ yn xn x v V V (v x , v y ) We obtain the unit vector u along the x’ axis by applying a 90º clockwise rotation to vector v u ( v y , v x ) (u x , u y ) The matrix to rotate the x’y’ system into coincidence with the xy system can be written as u x u y 0 R v x v y 0 0 0 1 OR In the case that we coordinates of P0 and P1 are known, v P1 - P0 P1 - P0 y’ V y P x’ P1 yn θ P0 xn x (v x , v y ) (xf,yf) (xf,yf) (a) (b) (xf,yf) (c) (xf,yf) (d) (xf,yf) (e) (xf,yf) (xf,yf) (f)