2 D Transformation Reading Hearn Baker Chapter 7
2 D Transformation Reading: Hearn & Baker Chapter 7
Outline q q Coordinate systems 2 D transformations CSC 625 2
Coordinate Systems q q q The use of coordinate systems is fundamental to computer graphics Coordinate systems are used to describe the locations of points in space Multiple coordinate systems make graphics algorithms easier to understand implement CSC 625 3
Coordinate Systems q Different coordinate systems represent the same point in different ways (2, 3) (1, 2) v v y y u u x q x Some operations are easier in one coordinate system than in another ■ For instance, given a point and a box, which coordinate system makes it easy to determine if the point is in the box? CSC 625 4
Coordinate Systems q Before being turned into pixels by graphics hardware, a piece of geometry goes through a number of transformations… CSC 625 5
Geometric Transformation q q q What’s the purpose for geometric transformation? Fundamental operations in computer graphics Operations that are applied to the geometric description of an object to change its ■ ■ ■ q Position Orientation Size For example, in animation, a computer animator develops a video sequence by moving the “camera” position or the objects in a scene along specified paths CSC 625 6
Geometric Transformation q Geometric transformations will map points in one space to points in another: (x , y , z ) f x, y, z q q These transformations can be very simple, such as scaling each coordinate, or complex, such as non-linear twists and bends 2 D geometric transformation ■ q 3 D geometric transformation ■ q q The shape remains on the 2 D plane The shape is in the 3 D space We will focus on transformations that can be represented easily with matrix operations Let us start in 2 D… CSC 625 7
Outline □ □ Coordinate systems 2 D transformations ■ ■ ■ Translation Rotation Scaling Homogeneous coordinates Composite transformations CSC 625 8
2 D Transformation q q q Translation Rotation Scaling Shearing Reflection CSC 625 9
Representation q We can represent a point, p = (x, y), in the plane As a column vector □ Or as a row vector q p x, y , CSC 625 10
Representation q We can represent a 2 -D transformation M by a matrix q If p is a column vector, M goes on the left: q If p is a row vector, MT goes on the right: q We use column vector throughout this class CSC 625 11
Translation q q q (x, y) Original vertex Translation distances (tx , t y ) (x , y ) New vertex y (x, y) x x x t x y y t y □ Matrix/vector representation (x , y ) y ? p p t ty tx CSC 625 x 12
Rotation q q (x, y) Original vertex Rotation angle (counter clockwise) (x r , yr ) Rotation (pivot) point (x , y ) New vertex CSC 625 13
Rotation q q q The simple case: pivot point is the coordinate origin (x, y) Original vertex x cos( ) y sin( ) x Rotation angle (0, 0) Rotation (pivot) point y x sin( ) y cos( ) (x , y ) New vertex How to memorize this formula? Matrix representation p R p x iy cos i sin x cos iy cos ix sin i 2 y sin x cos y sin i x sin y cos CSC 625 14
Rotation q Property of rotation matrix ■ Orthogonal matrix: q ■ The inverse of an orthogonal matrix is just its transpose The determinant is one det R cos sin 1 ■ Composition of two rotations R 1 R 2 R 1 2 CSC 625 cos 1 2 cos 1 cos 2 sin 1 sin 2 sin 1 cos 2 cos 1 sin 2 15
Rotation What if the rotation angle is measured in clockwise? q Rotation through clockwise angle θ is equivalent to the rotation through counter clockwise angle 2∏-θ q CSC 625 16
Rotation q q q The general case: arbitrary pivot position (x, y) Original vertex Rotation angle (x r , yr ) Rotation (pivot) point (x , y ) New vertex p R p t t x xr (x xr ) cos( ) ( y yr ) sin( ) y yr (x xr ) sin( ) ( y yr ) cos( ) CSC 625 17
Scaling q q q Change the size of the object (s x , s y ) Scaling factors Fixed point (remain unchanged after the scaling transformation) (x f , y f ) y y (x f , y f ) x CSC 625 18
Scaling q q Fixed point is the coordinate origin (x, y) Original vertex (s x , s y ) Scaling factors (x , y ) New vertex y (x, y) x x x s x y y s y q Matrix representation y p S p (x s x , y s y ) x CSC 625 19
Scaling q q Arbitrary fixed point Original vertex Scaling factors New vertex y (x f , yf ) (x, y) (s x , s y ) (x , y ) x x f (x x f )s x y y f (x x f )s y (x , y ) (x, y) y (x f , y f ) x x p S p t t CSC 625 20
2 D Transformation q q Translation Rotation p p t p R p Pivot point is at the origin ■ Arbitrary pivot point ■ p R p t t q Scaling reference point is at the origin ■ Arbitrary fixed point ■ p S p t t q Each of the three basic 2 D transformations can be expressed in the general matrix form p M 1 p m 2 Where M 1 is a 2 by 2 matrix, m 2 is a column 2 -vector CSC 625 21
Why Use Matrices? q Graphics applications involve sequences of geometric transformations q For example, scaling (fixed point at the origin) followed by a rotation (rotation point at the origin) p 1 S p 0 p 2 R p 1 R S p 0 CSC 625 22
Why Use Matrices? q A common task in computer graphics: ■ Given 1000 points. Apply 100 transformations to every point. CSC 625 23
Why Use Matrices? q Matrix representation is very useful in graphics application. Rotation and scaling are OK ■ Translation is a problem! ■ q Consider the transformation: scaling followed by translation and then rotation p 1 S p 0 p 2 p 1 t p 3 R p 2 R S p 0 R t q Is there a unified approach to combine the transformations so that the final coordinate positions are obtained directly from the initial coordinate by the products of the transformation matrices? CSC 625 24
Homogeneous Coordinates q q q Multiplicative and translational terms for a 2 D transformation can be combined into a single matrix if we expand the representations to 3 by 3 matrices. We need to represent coordinates in 2 dimensions with a 3 vector Homogeneous coordinates seem unintuitive, but they make graphics operations much easier CSC 625 25
Homogeneous Coordinates q We expand each 2 D coordinate-position representation (x, y) to a 3 D coordinate-position representation (xh, yh, h), where the homogeneous parameter h is a nonzero value such that x q q q xh , y h yh h Thus, there an infinite number of equivalent homogeneous representations for each coordinate point (x, y) A convenient choice is simply to set h=1 Translation can now be done with matrix multiplication! CSC 625 26
Why Is It Called Homogeneous Coordinate? q A multivariate polynomial with all terms having the same degree is called homogeneous polynomial. ■ q For instance, f(x, y) = x 3 + 2 x 2 y – y 3 A non-homogeneous polynomial can be to a homogeneous polynomial using homogeneous coordinates! ■ For instance, f(x, y) = x 3 + 2 xy – y 2 +5 is non-homogeneous ■ Replace x, y with xh/h and yh/h, then f(x, y) = 0 becomes f(xh/h, yh/h) = 0, (xh/h)3 + 2(xh/h)(yh/h) – (yh/h)2 +5 = 0 ■ Multiplying it with h 3 will clear the denominators g(xh, yh, h) = x 3 + 2 x y h – y 2 h +5 h 3 = 0 h CSC 625 h h h 27
Translation Matrix q Using homogeneous coordinates, we can represent the translation using the following matrix multiplication P T(tx , t y ) P CSC 625 28
Rotation Matrix q Rotation about general pivot point P R( ) P CSC 625 29
Rotation Matrix q Rotation about arbitrary pivot point p R p t t P T(xr , yr ) R( ) T( xr , yr ) P Translate the object such that the pivot point is moved to the origin Rotate the object about the coordinate origin Translate the object such that the pivot point is returned to the original position CSC 625 30
Scaling Matrix q Scaling relative to the coordinate origin P S(s x , s y ) P CSC 625 31
Scaling Matrix q Scaling relative to arbitrary fixed point p S p t t P T(x f , y f ) S(s x , s y ) T( x f , y f ) P Let P=(xf , yf , 1)T. Verify that P’=(xf , yf , 1)T The fixed point is really fixed ☺ CSC 625 32
Advantages of Homogeneous Coordinates q Unified view of transformation as matrix multiplication ■ q Easier in hardware and software To compose transformations, simply multiply matrices ■ Keep in mind the order of the transformations: AB is generally not the same as BA CSC 625 33
Inverse Matrix q q The inverse translation matrix (undo the original translation) Negate the translation distance Verify that T 1 (t , t ) T(t , t ) I x y x CSC 625 y 34
Inverse Matrix q q The inverse rotation matrix Negate the rotation angle Verify that R 1( ) R( ) I CSC 625 35
Inverse Matrix q q The inverse scaling matrix Replace the scaling factors with their reciprocals Verify that S 1 (s , s ) S(s , s ) I x y CSC 625 36
Other 2 D Transformation q y Reflection to the x-axis x x y y q x Reflection to the y-axis x x y y (x, y) (x’, y’) y (x, y) x CSC 625 37
Other 2 D Transformation q Reflection about arbitrary line l: y=kx+b, k≠ 0 ■ Step 1: translate by (0, -b) Step 2: rotate by , where tan 1 (k ) ■ Step 3: reflect through x-axis ■ Step 4: rotate by ■ Step 5: translate by (0, b) ■ y 0, b (x’, y’) (x, y) x CSC 625 38
Other 2 D Transformation q Shearing ■ q The transformed shape appears as if the object were composed of internal layers that had been caused to slide over each other An x-direction shear relative to the x-axis is as follows: x x hx y y y (x, y) (x’, y’) CSC 625 39
How are Transformations Combined? q Scale then translate 1 0 3� 2 0 0� � 2 0 3� � � TS � 0 1 1� 0� 2 0� � 0 2 1� � � 0 0 0 1� � � � q Use matrix multiplication p T(Sp) TSp Caution: matrix multiplication is not commutative! CSC 625 40
The Order of a Sequences of Transformations q The order of a sequence of transformations DOES matter! q Multiplication of matrices is NOT commutative! In general, AB BA ■ Reversing the order in which a sequence of transformations is performed may affect the transformed position of an object! ■ q Scale then translate q Translate then scale p T(Sp) TSp p S(Tp) STp CSC 625 41
The Order of a Sequences of Transformations q Scale then translate q Translate then scale CSC 625 42
The Order of a Sequence of Transformations q Special case A sequence of transformations that are all of the same kind ■ The multiplication of transformation matrices is commutative. ■ q For instance, two successive 2 D rotations could be performed in either order and the final position would be the same. ■ q This is not true for 3 D case. We will see it later. Check two successive translations and two successive scalings yourself. CSC 625 43
Summary of 2 D Transformations q Translation q Rotation q Scaling CSC 625 44
Exercise q With reference to the following figure, a house has a center at (10, 5). Find a 3× 3 transformation matrix that rotates the house 90 degrees about its center. CSC 625 45
Exercise q Assuming two-dimensional transformations, show that the following pairs of operations do or do not commute: Two rotations commute. ■ Two translations commute. ■ A rotation and a uniform scaling (Sx=Sy) commute. ■ A rotation and a non-uniform scaling do not commute. ■ CSC 625 46
Exercise q Given a unit circle centered at p, two perpendicular unit 2 vectors u and v, suppose we want to perform a non-uniform scale about p by 2 in direction u and 0. 5 in direction v. The angle between the vector u and x-axis isθ(measured in counter clockwise order). Find the appropriate matrix product to achieve this transformation. y y v v u u p p x x CSC 625 47
Exercise q Do perpendicular lines map to perpendicular lines under 2 D transformations (translations, rotations, scaling, reflection, shearing)? If so, then prove it. If not, give a counter example. CSC 625 48
3 D Transformation Reading: Hearn & Baker Chapter 9
Outline q q q Homogeneous coordinates Translation, scaling Rotation Composite transformations Change the orthonormal basis Transformation in Open. GL CSC 625 50
3 D Homogeneous Coordinates q q Points in R 3 are represented as homogeneous vector in R 4 Given an h∈R, such that h≠ 0 , then the point (x, y, z) ∈ R 3 is represented as hx, hy, hz, h T R 4 in homogeneous coordinates q Conversely, a point (x’, y’, z’, h’) ∈R 4 in the homogeneous coordinate system corresponds to the point in the world coordinate system CSC 625 51
Outline q q q Homogeneous coordinates Translation, scaling Rotation Composite transformations Change the orthonormal basis Transformation in Open. GL CSC 625 52
3 D Translation q q Objects are usually defined relative to their own coordinate system. We can translate points in space to new positions by adding offsets to their coordinates, as shown in the following vector equation. CSC 625 53
3 D Scaling q q Objects can be scaled to different sizes. If the scaling is uniform, the shape is preserved. Scaling relative to the origin CSC 625 54
3 D Scaling q To scale about a fixed point, (xf, yf, zf), we apply the transformation T(xf, yf, zf)S(sx, sy, sz)T(-xf, -yf, -zf) CSC 625 55
Outline q q q Homogeneous coordinates Translation, scaling Rotation Composite transformations Change the orthonormal basis Transformation in Open. GL CSC 625 56
3 D Rotation q Recall that the 2 D rotation ■ q The fixed point and rotation angle 3 D rotation is much more complicated than the 2 D case! x x cos( ) y sin( ) y x sin( ) y cos( ) CSC 625 57
3 D Rotation q q q 3 D rotation is defined by a rotation angle and a rotation axis A general 3 D rotation axis is defined by two points P 1 and P 2 We can translate the axis such that P 1 is at the origin Then we perform the 3 D rotation Finally, we translate it back to the original axis CSC 625 58
3 D Rotation q q q We consider the special case that the rotation axis passes through the origin A unit vector n indicates the axis of rotation A scalar θ indicates the angle of rotation CSC 625 59
3 D Rotation q 3 D rotation in terms of angle-axis parameters Rotate the point p in a counterclockwise direction about the ray defined by unit vector n=(nx, ny, nz). The angle of rotation is θ. □ We need to put it into a matrix representation! q CSC 625 60
3 D Rotation q 3 D rotation matrix in homogeneous coordinates where c cos , s sin θ CSC 625 61
3 D Rotation q q Special cases Rotations about a coordinate axis are counterclockwise, when looking along the positive half of the axis toward the origin CSC 625 62
3 D Rotation q q q Rotation around x axis Looking along the positive half of the x-axis toward the origin X-component remains unchanged Z Y X Z CSC 625 Y 63
3 D Rotation q q q Rotation around y axis Looking along the positive half of the y-axis toward the origin y-component remains unchanged X Y X Z CSC 625 Z 64
3 D Rotation q q q Rotation around z axis Looking along the positive half of the z-axis toward the origin z-component remains unchanged Y Y X Eye X Z CSC 625 65
3 D Shearing q Shearing is also more complicated ■ q The transformed shape appears as if the object were composed of internal layers that had been caused to slide over each other Here is one example of shear with respect to x-z plane x x by y y z z CSC 625 66
Outline q q q Homogeneous coordinates Translation, scaling Rotation Composite transformations Change the orthonormal basis Transformation in Open. GL CSC 625 67
Composite Transformations q q The order of a sequences of transformations DOES matter! Multiplication of matrices is NOT commutative! ■ q In general, reversing the order in which a sequence of transformations is performed may affect the transformed position of an object! Commutativity ■ ■ ■ Scaling: Is S 1 S 2 = S 2 S 1? Translation: Is T 1 T 2 = T 2 T 1? Rotation: Is R 1 R 2 = R 2 R 1? q ■ the difference between 2 D rotations and 3 D rotations Scaling then translation: Is ST=TS? CSC 625 68
Outline q q q Homogeneous coordinates Translation, scaling Rotation Composite transformations Change the orthonormal basis Transformation in Open. GL CSC 625 69
Change The Orthonormal Basis q q q Transformations between 3 D coordinate systems (you will see its applications in 3 D viewing) Given two orthonormal coordinate systems XYZ and UVW, which share the coordinate origin, find a transformation from one to the other Answer: find a 3 D rotation matrix z w v u y x CSC 625 70
Change The Orthonormal Basis q General case CSC 625 71
Exercise q Given the following 3 D transformation matrix: ■ ■ ■ (i) What are the coordinates of the point (1, 1, 1)T in the transformed coordinate system? (ii) Describe what transformation the matrix M performs when applied to 3 D points. Note that the above transformation is a composition of two simple transformations. (iii) Find the inverse matrix of M. CSC 625 72
Outline q q q Homogeneous coordinates Translation, scaling Rotation Composite transformations Change the orthonormal basis Transformation in Open. GL CSC 625 73
Transformations in Open. GL q Translation by (x, y, z) gl. Translate{fd}( x, y, z ) q Rotation around the axis passes through the origin and (x, y, z) gl. Rotate{fd}( angle, x, y, z ) ■ q angle is in degrees Scaling along the x, y, and z axes gl. Scale{fd}( x, y, z ) q To scale (or rotate) with a fixed point other than the coordinate origin, we must first translate, then scale (or rotate) and then undo the translation with another translation CSC 625 74
Transformation Tutorial q The transformation tutorial program demonstrates how the basic transformations of rotation, translation and scaling operate in Open. GL CSC 625 75
- Slides: 75