Part - #2 Geometric Transformation (2D & 3D) March, 2019
14-Sep-22 2 Intro & General Information Geometric Transformations Construction (translate, rotate, scale, mirror) Viewing (projections, zooming) Animation (processes, vibration)
Coordinate Systems In order to specify the geometry of a given solid, it is necessary to use a variety of coordinate systems. Its Major classifications are: World Coordinate System: Also known as the "universe" or sometimes " model " coordinate system. This is the base reference system for the overall model, ( generally in 3D ), to which all other model coordinates relate User Coordinate System: Also known as “ working ” coordinate system. When it is difficult to define certain geometries using WCS , In such cases user coordinate system can be defined relative to the WCS. Display Coordinates: This refers to the actual coordinates to be used for displaying the image on the screen.
14-Sep-22 4 General Information Transformation of a point is basic in GT. It can be formulated as follows: Given a point P that belongs to a geometric model find the corresponding point P* in the new position such that P * = f( P , transformation parameters) The transformation parameters should provide ONE-TO-ONE-MAPPING. Multiple transformations can be combined to yield a single transformation which should have the same effect as the sequential application of original ones. CONCATENATION / kənˌkatnˈāSH (ə)n/ Equation of P * for graphics hardware should be in matrix notation: P * = [T] P , where [T] is the transformation matrix.
Geometric Transformations Sometimes also called modeling transformations Geometric transformations: Changing an object’s position (translation), orientation (rotation) or size (scaling) Modeling transformations: Constructing a scene or hierarchical description of a complex object Others transformations: reflection and shearing operations 5
1. Geometric Transformations What is transformation? It is the backbone of computer graphics , enabling us to manipulate the shape, size, and location of the object. Changing something to something else via rules. Why are they important to graphics? It can be used to effect the following changes in a geometric object : 1. Change the location 2. Change the shape 3. Change the size 4. Rotate 5. Copy
2. Types of Transformations i. Geometric/Object Transformation This transformation alters the coordinate values of the object. Basic operations are scaling, translation, rotation and combination of one or more of these basic transformations . Object transformation = Move (transform) an object in the 2D/3D space. ii. Visual/ Coordinate System Transformation In this transformation there is no change in either the geometry or the coordinates of the object . A copy of the object is placed at the desired sight, without changing the coordinate values of the object. Coordinate system transformation = Move (transform) the coordinate system. View the objects from the new coordinate system.
8 3. Types of Geometric Transformations Translation Rotation Scaling Reflection/Mirroring.
14-Sep-22 10 Translation Translation is a rigid-body transformation (Euclidean) when each entity of the model remains parallel, or each point moves an equal distance in a given direction: P * = P + d (for both 2D and 3D). In a scalar form (for 3D): x * = x + x d y * = y + y d z * = z + z d C (11,7) A (8,5) B (10,8) A * C * B * y x O Question : Find the coordinates of vertices A * , B * , and C * of the translated triangle. The distance vector of translation: D = [-7 -4] T. Verify that the lengths of the edges are unchanged.
i . Translation - 2D Moving an object is called a translation . We translate a point by adding to the x and y coordinates , respectively, the amount the point should be shifted in the x and y directions. We translate an object by translating each vertex in the object.
Basic 2D Geometric Transformations 2D Translation x’ = x + t x , y’ = y + t y P’=P+T Translation moves the object without deformation (rigid-body transformation) P P’ T 12
Basic 2D Geometric Transformations (cont.) 2D Translation To move a line segment, apply the transformation equation to each of the two line endpoints and redraw the line between new endpoints To move a polygon, apply the transformation equation to coordinates of each vertex and regenerate the polygon using the new set of vertex coordinates 13
Scaling is used to change the size of an entity or a model. P * = [S] P s x 0 0 For general case [S] = 0 s y 0 , 0 0 s z If 0 < s < 1 - compression If s > 1 - stretching s x = s y = s z - uniform scaling, otherwise - non-uniform 14-Sep-22 14 Scaling where s x , s y , and s z are the scaling factors in the X, Y, and Z directions respectively. Question : The larger circle is the scaled copy of the smaller one. Can you say that we have a uniform scaling? Why? Define y* and R*. O(4,2) O * (10,y * ) O x y R 1 R *
ii. Scaling Changing the size of an object is called a scale. We scale an object by scaling the x and y coordinates of each vertex in the object.
2D Rotation Rotation axis Rotation angle rotation point or pivot point ( x r ,y r ) y r x r θ Basic 2D Geometric Transformations (cont.) 16 2D Geometric Transformations
2D Rotation If θ is positive counterclockwise rotation If θ is negative clockwise rotation Remember: cos ( a + b ) = cos a cos b - sin a sin b cos ( a - b ) = cos a sin b + sin a cos b Basic 2D Geometric Transformations (cont.) 17 2D Geometric Transformations
14-Sep-22 18 Rotation Rotation is a non-commutative transformation (depends on sequence). P * P x y * x * y X Y Z Question : Let the length of a major and minor axes of an ellipse with the center on the origin of the CS be 2a and 2b respectively, and - the angle between the major axis and the x-axis. Then, derive the expression of an ellipse in the (O,x,y) system.
iii. Rotation about the origin Consider rotation about the origin by Θ degrees Radius stays the same , angle increases by Θ.
2D Rotation At first, suppose the pivot point is at the origin x’=r cos ( θ+Φ ) = r cos θ cos Φ - r sin θ sin Φ y’=r sin( θ+Φ ) = r cos θ sin Φ + r sin θ cos Φ x = r cos Φ, y = r sin Φ x’=x cos θ - y sin θ y’=x sin θ + y cos θ Φ (x,y) r r θ (x’,y’) Basic 2D Geometric Transformations (cont.) 20 2D Geometric Transformations
Basic 2D Geometric Transformations 2D Rotation P’=R·P Φ (x,y) r r θ (x’,y’) 21 2D Geometric Transformations
3. Transformations as Matrices
Cont’d… Example: Translate the rectangle (2, 2), (2, 8), (10, 8), (10, 2) 2 units along x-axis and 3 units along y-axis.
Cont’d…
2D Scaling Scaling is used to alter the size of an object Simple 2D scaling is performed by multiplying object positions (x, y) by scaling factors s x and s y x’ = x · s x y’ = y · s x or P’ = S·P Basic 2D Geometric Transformations (cont.) 25
2D Scaling Any positive value can be used as scaling factor Values less than 1 reduce the size of the object Values greater than 1 enlarge the object If scaling factor is 1 then the object stays unchanged If s x = s y , we call it uniform scaling If scaling factor <1, then the object moves closer to the origin and If scaling factor >1, then the object moves farther from the origin x’ x Basic 2D Geometric Transformations (cont.) 26
2D Scaling Why does scaling also reposition object? Answer: See the matrix (multiplication) Still no clue? Basic 2D Geometric Transformations (cont.) 27
2D Scaling We can control the location of the scaled object by choosing a position called the fixed point ( x f ,y f ) x’ – x f = (x – x f ) s x y’ – y f = (y – y f ) s y x’=x · s x + x f (1 – s x ) y’=y · s y + y f (1 – s y ) Polygons are scaled by applying the above formula to each vertex, then regenerating the polygon using the transformed vertices Basic 2D Geometric Transformations (cont.) 28
Example: If the triangle A(1, 1), B(2, 1), C(1, 3) is scaled by a factor 2, find the new coordinates of the triangle.
Cont’d…
Cont’d… Example: Rotate the rectangle (0, 0), (2, 0), (2, 2), (0, 2) by 30o ccw about its centroid and find the new coordinates of the rectangle.
Summary
4. Homogeneous Coordinates In order to represent a translation as a matrix multiplication operation we use 3 x 3 matrices and pad the points to become 3 x 1 matrices. This coordinate system (using three values to represent a 2D point ) is called homogeneous coordinates . Let the added extra coordinate be 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. If W ≠ , divide by it to get Cartesian coordinates of point: (x/W, y/W, 1). If W = 0 , point is said to be at infinity.
Cont’d… Transformation matrices for 2D translation are now 3x3.
5. Concatenation of transformation
Cont’d…
Properties of Translations
Homogeneous form of scale Concatenation of scales
14-Sep-22 40 Homogeneous Transformation When we scale then rotate, the transformed image is given by: P * = ([R][S]) P where [S], [R], [R] [S] are 3x3 transformation matrices. This is not the case for a translation ( P * = P + d ). The goal is to find a [D] such that P + d = [D] P in order to perform valid matrix multiplication. This is found by using a homogeneous coordinates. Homogeneous Transformation maps n-dimensional space into (n+1)- dim. 3D representation of the point vector - P = [x, y, z] T Homogeneous rep. of the same vector - P = [xw, yw, zw, w] T where w = 1
Matrix Representations and Homogeneous Coordinates Many graphics applications involve sequences of geometric transformations Animations Design and picture construction applications We will now consider matrix representations of these operations Sequences of transformations can be efficiently processed using matrices 41 2D Geometric Transformations
Matrix Representations and Homogeneous Coordinates (cont.) P’ = M 1 · P + M 2 P and P’ are column vectors M 1 is a 2 by 2 array containing multiplicative factors M 2 is a 2 element column matrix containing translational terms For translation M 1 is the identity matrix For rotation or scaling, M 2 contains the translational terms associated with the pivot point or scaling fixed point 42 2D Geometric Transformations
To produce a sequence of operations, 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 transformations, without calculating intermediate coordinate values Matrix Representations and Homogeneous Coordinates (cont.) 43 2D Geometric Transformations
Multiplicative and translational terms for a 2D geometric transformation can be combined into a single matrix if we expand the representations to 3 by 3 matrices We can use the third column for translation terms, and all transformation equations can be expressed as matrix multiplications Matrix Representations and Homogeneous Coordinates (cont.) 44 2D Geometric Transformations
Expand each 2D coordinate ( x,y ) to three element representation ( x h ,y h ,h ) called homogeneous coordinates h is the homogeneous parameter such that x = x h /h, y = y h /h, infinite homogeneous representations for a point A convenient choice is to choose h = 1 Matrix Representations and Homogeneous Coordinates (cont.) 45 2D Geometric Transformations
14-Sep-22 46 Homogeneous Transformation The transformation matrices in new (homogeneous) representation:
2D Translation Matrix or, P’ = T (t x ,t y ) · P Matrix Representations and Homogeneous Coordinates (cont.) 47 2D Geometric Transformations
2D Rotation Matrix or, P’ = R (θ)· P Matrix Representations and Homogeneous Coordinates (cont.) 48 2D Geometric Transformations
2D Scaling Matrix or, P’ = S ( s x ,s y )· P Matrix Representations and Homogeneous Coordinates (cont.) 49 2D Geometric Transformations
Inverse Transformations 2D Inverse Translation Matrix By the way: 50 2D Geometric Transformations
Inverse Transformations (cont.) 2D Inverse Rotation Matrix : If θ is negative clockwise In Only sine function is affected Therefore we can say Is that true? Proof: It’s up to you 52 2D Geometric Transformations
2D Composite Transformations We can setup a sequence of transformations as a composite transformation matrix by calculating the product of the individual transformations P’=M 2 ·M 1 ·P =M·P 54 2D Geometric Transformations
2D Composite Transformations (cont.) Composite 2D Translations If two successive translation are applied to a point P , then the final transformed location P ' is calculated as 55 2D Geometric Transformations
Don’t forget: Successive translations are additive Successive scalings are multiplicative For example: If we triple the size of an object twice, the final size is nine (9) times the original 9 times? Why? Proof: Again up to you 2D Composite Transformations (cont.) 58 2D Geometric Transformations
General Pivot Point Rotation Steps: Translate the object so that the pivot point is moved to the coordinate origin. Rotate the object about the origin. Translate the object so that the pivot point is returned to its original position. 59 2D Geometric Transformations
General Pivot Point Rotation 60 2D Geometric Transformations
General Fixed Point Scaling Steps: Translate the object so that the fixed point coincides with the coordinate origin. Scale the object about the origin. Translate the object so that the pivot point is returned to its original position. 62 2D Geometric Transformations
General Fixed Point Scaling (cont.) (x r, y r ) (x r, y r ) 63 2D Geometric Transformations
General 2D Fixed-Point Scaling: General Fixed Point Scaling (cont.) 64 2D Geometric Transformations
2D Composite Transformations (cont.) General 2D scaling directions: Above: scaling parameters were along x and y directions What about arbitrary directions? Answer: See next slides 65 2D Geometric Transformations
General 2D Scaling Directions Scaling parameters s 1 and s 2 along orthogonal directions defined by the angular displacement θ . 66 2D Geometric Transformations
General 2D Scaling Directions (cont.) General procedure: Rotate so that directions coincides with x and y axes Apply scaling transformation Rotate back The composite matrix: 67 2D Geometric Transformations
2D Composite Transformations (cont.) Matrix Concatenation Properties: Matrix multiplication is associative ! M 3 · M 2 · M 1 = (M 3 · M 2 ) · M 1 = M 3 · ( M 2 · M 1 ) A composite matrix can be created by multiplicating left-to-right (premultiplication) or right-to-left (postmultiplication) Matrix multiplication is not commutative ! M 2 · M 1 ≠ M 1 · M 2 68 2D Geometric Transformations
2D Composite Transformations (cont.) Matrix Concatenation Properties: But: Two successive rotations Two successive translations Two successive scalings are commutative! Why? Proof: You got it: Up to you 69 2D Geometric Transformations
14-Sep-22 70 Mirror Plane* => Negate the corresponding coordinate Mirror through Line* => Reflect through 2 planes intersecting at the axis Point* => Reflect through 3 planes intersecting at the point * plane - principal plane, line - X, Y, or Z axes, point - CS origin P * = [M] P , where [M] = = Question : Define the signs (in the matrix) for the reflections (mirroring) through: a) x = 0, y = 0, z = 0 planes b) X, Y, and Z axes c) the CS origin
Reversing the order in which a sequence of transformations is performed may affect the transformed position of an object. In (a), an object is first translated in the x direction, then rotated counterclockwise through an angle of 45°. In (b), the object is first rotated 45° counterclockwise, then translated in the x direction 71 2D Geometric Transformations
Other 2D Transformations Reflection Transformation that produces a mirror image of an object 72 2D Geometric Transformations
Reflection 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) (previous slide) Other 2D Transformations (cont.) 73 2D Geometric Transformations
Other 2D Transformations (cont.) Reflection Reflection about the line x=0 (the y axis) 74 2D Geometric Transformations
R eflection about the origin Other 2D Transformations (cont.) 75 2D Geometric Transformations
Reflection about the line y=x Other 2D Transformations (cont.) 76 2D Geometric Transformations
Reflection about the line y=-x Other 2D Transformations (cont.) 77 2D Geometric Transformations
Shear Transformation that distorts the shape of an object such that the transformed shape appears as the object w as composed of internal layers that had been caused to slide over each other y x (0,1) (1,1) (1,0) (0,0) y x (2,1) (3,1) (1,0) (0,0) sh x =2 Other 2D Transformations (cont.) 78 2D Geometric Transformations
Shear An x-direction shear relative to the x axis An y-direction shear relative to the y axis Other 2D Transformations (cont.) 79 2D Geometric Transformations
Shear x-direction shear relative to other reference lines Other 2D Transformations (cont.) 80 2D Geometric Transformations
Example A unit square (a) is transformed to a shifted parallelogram (b) with sh x = 0.5 and y ref = −1 in the shear matrix from Slide 56 81 2D Geometric Transformations
Shear y -direction shear relative to the line x = x ref Other 2D Transformations (cont.) 82 2D Geometric Transformations
Example A unit square (a) is turned into a shifted parallelogram (b) with parameter values sh y = 0.5 and x ref = −1 in the y -direction shearing transformation from Slide 58 83 2D Geometric Transformations
This slide is intentionally left blank Your responsibility to fill it Raster Methods for Transformations and OpenGL 84 2D Geometric Transformations
Transformation Between Coordinate Systems Individual objects may be defined in their local cartesian reference system. The local coordinates must be transformed to position the objects within the scene coordinate system. 85 2D Geometric Transformations
Steps for coordinate transformation Translate so that the origin ( x , y ) of the x ′- y ′ system is moved to the origin of the x-y system. Rotate the x′ axis on to the axis x. Transformation Between Coordinate Systems 86 2D Geometric Transformations
y x x′ y' θ x y Transformation Between Coordinate Systems (cont.) 87 2D Geometric Transformations
y x x′ x y y′ θ Transformation Between Coordinate Systems (cont.) 88 2D Geometric Transformations
y x x′ x y y′ Transformation Between Coordinate Systems (cont.) 89 2D Geometric Transformations
Transformation Between Coordinate Systems (cont.) 90 2D Geometric Transformations
An alternative method: -Specify a vector V that indicates the direction for the positive y′ axis. Let -Obtain the unit vector u =(u x ,u y ) along the x′ axis by rotating v 90 clockwise. Transformation Between Coordinate Systems (cont.) 91 2D Geometric Transformations
Elements of any rotation matrix can be expressed as elements of or t hogonal unit vectors. That is, the rotation matrix can be written as Transformation Between Coordinate Systems (cont.) 92 2D Geometric Transformations
y x x′ y′ x y V Transformation Between Coordinate Systems (cont.) 93 2D Geometric Transformations
OpenGL Geometric Transformation Functions A separate function is available for each of the basic geometric transformations AND All transformations are specified in three dimensions Why? Answer: Remember; OpenGL was developed as 3D library But how to perform 2D transformations? Answer: Set z = 0 94 2D Geometric Transformations
Basic OpenGL Geometric Transformations Translation glTranslate * ( tx , ty , tz ); * is either f or d tx , ty and tz are any real number For 2D, set tz =0.0 Rotation glRotate * (theta, vx , vy , vz ); * is either f or d theta is rotation angle in degrees (internally converted to radian) Vector v=( vx , vy , vz ) defines the orientation for a rotation axis that passes through the coordinate origin For 2D, set v z= 1 .0 and vx=vy=0.0 95 2D Geometric Transformations
Basic OpenGL Geometric Transformations (cont.) Scaling glScale * ( sx , sy , sz ); * is either f or d sx , sy and sz are any real number Negative values generate reflection Zero values can cause error because inverse matrix cannot be calculated A ll routines construct a 4 x 4 transformation matrix OpenGL uses composite matrices Be careful with the order 96 2D Geometric Transformations
OpenGL Matrix Operations glMatrixMode ( . ); Projection Mode: Determines how the scene is projected onto the screen Modelview Mode: Used for storing and combining geometric transformations Texture Mode: Used for mapping texture patterns to surfaces Color Mode: Used to convert from one color mode to another 97 2D Geometric Transformations
OpenGL Matrix Operations Modelview matrix, used to store and combine geometric transformations glMatrixMode (GL_MODELVIEW); A call to a transformation routine generates a matrix that is multiplied by the current matrix To assign the identity matrix to the current matrix glLoadIdentity (); 98 2D Geometric Transformations
OpenGL Matrix Operations (cont.) Alternatively: gl Load Matrix * ( elements16 ); To assign other values to the elements of the current matrix In column-major order: First four elements in first column Second four elements in second column Third four elements in third column Fourth four elements in fourth column 99 2D Geometric Transformations
OpenGL Matrix Operations (cont.) Concatenating a specified matrix with current matrix: gl Mult Matrix * ( otherElements16 ); Current matrix is postmultiplied (right-to-left) by the specified matrix Warning: Matrix notation m jk means: In OpenGL: j column, k row In mathematics: j row, k column 100 2D Geometric Transformations
OpenGL Matrix Stacks OpenGL maintains a matrix stack for transformations Initially the modelview stack contains only the identity matrix More about it: Coming soon 101 2D Geometric Transformations
OpenGL Transformation Routines For example, assume we want to do in the following order: translate by +2, -3, +4, rotate by 45 around axis formed between origin and 1, 1, 1 scale with respect to the origin by 2 in each direction. Our code would be glMatrixMode (GL_MODELVIEW); glLoadIdentity (); //start with identity glScalef (2.0,2.0,2.0); //Note: Start with the LAST operation glRotatef (45.0,1.0,1.0,1.0); glTranslatef (2.0,-3.0, 4.0); // End with the FIRST operation 102 2D Geometric Transformations
References Donald Hearn, M. Pauline Baker, Warren R. Carithers , “Computer Graphics with OpenGL, 4th Edition”; Pearson, 2011 Sumanta Guha, “Computer Graphics Through OpenGL: From Theory to Experiments”, CRC Press, 2010 Edward Angel, “Interactive Computer Graphics. A Top-Down Approach Using OpenGL”, Addison-Wesley, 2005 105
Ex. 1: Rotation of object about arbitrary point P (x, y) by θ Translate point (x, y) to origin by (-x, -y) Rotate by θ Translate the point by (x, y)
6. Reflections/Mirroring
More Reflections
3 D - Rigid body transformations Methods for object modeling transformation in three dimensions are extended from two dimensional methods by including consideration for the z coordinate. Preserve lines, angles and distances. Generalize from 2D by including z coordinate Straight forward for translation and scale Rotation more difficult Homogeneous coordinates: 4 components Transformation matrices: 4×4 éléments.
3D Point: We will consider points as column vectors. Thus, a typical point with coordinates (x, y, z) is represented as: 3D Point Homogenous Coordinate: A 3D point P is represented in homogeneous coordinates by a 4-dim, Vector. We don't lose anything. The main advantage: it is easier to compose translation and rotation . Everything is matrix multiplication .
i . 3D Translation In homogeneous coordinates , 3D transformations are represented by 4×4 matrixes:. P is translated to P' by: An object is translated in 3D dimensional by transforming each of the defining points of the objects .
Cont’d… An object represented as a set of polygon surfaces, is translated by translate each vertex of each surface and redraw the polygon facets in the new position.
ii. 3D Rotation In general, rotations are specified by a rotation axis and an angle. In two-dimensions there is only one choice of a rotation axis that leaves points in the plane. The easiest rotation axes are those that parallel to the coordinate axis. Positive rotation angles produce counterclockwise rotations about a coordinate axis, if we are looking along the positive half of the axis toward the coordinate origin.
Rotation about Z-axis: : For z axis same as 2D rotation: P′ = R z ( θ) ⋅ P Rotation about X-axis: P′ = R x ( θ) ⋅ P
General 3D Rotations about axis // to Coordinate Axis Rotation axis parallel with coordinate axis (Example x axis ):
iii. 3D Scaling About origin : changes the size of the object and repositions the object relative to the coordinate original . P′ = S ⋅ P
Scale about any arbitrary point:
iV . 3D Reflections About an axis: equivalent to 180°rotation about that axis. About a plane: A reflection through the xy plane. A reflections through the xz and the yz planes are defined similarly.
Exercise You sat in the car, and find the side mirror 40mm on your right and 30mm in your front. You started your car and drove 200mm forward, turned 30 degrees to right, moved 200mm forward again, and turned 45 degrees to the right, and stopped. What is the position of the side mirror now, relative to where you were sitting in the beginning?
Solution The side mirror position is locally (40, 30). The matrix of first driving forward 200mm is:
Cont’d… The matrix to turn to the right 30 deg and 45 degrees (rotating -30 and -45 degrees about the origin) are: The 3 rd transformation is moving by 200mm along an axis oriented 60 degree with the x-axis.