2D Transformations CMPT 361 Introduction to Computer Graphics Torsten Möller © Machiraju/Zhang/Möller Graphics Pipeline Hardware Modelling Transform Visibility Illumination + Shading Perception, Color © Machiraju/Zhang/Möller Interaction Texture/ Realism 2 Reading • Chapter 3 of Angel • Chapter 5 of Foley, van Dam, … © Machiraju/Zhang/Möller 3 Schedule • • • • • • • • Geometry basics Affine transformations Use of homogeneous coordinates Concatenation of transformations 3D transformations Transformation of coordinate systems Transform the transforms Transformations in OpenGL © Machiraju/Zhang/Möller 4 Basic transformations • The most basic ones – – – – – Translation Scaling Rotation Shear And others, e.g., perspective transform, projection, etc. • Basic types of transformations – Rigid-body: preserves length and angle – Affine: preserves parallel lines, not angles or lengths © Machiraju/Zhang/Möller – Free-form: anything goes 5 Translation in 2D P =P +T x = x + dx y = y + dy x y ⇥ = © Machiraju/Zhang/Möller x y ⇥ + dx dy ⇥ 6 Scaling in 2D P =S P x = sx x y = sy y • Uniform sx = sy • Non-uniform sx ⇥= sy x y ⇥ = © Machiraju/Zhang/Möller sx 0 0 sy ⇥ x y ⇥ 7 Rotation about origin • Positive angles: counterclockwise • For negative angles cos( sin( ) = cos( ) ) = sin( ) P =R x = x cos( ) y sin( ) y = x sin( ) + y cos( ) ⇥ ⇥ x cos( ) sin( ) = y sin( ©)Machiraju/Zhang/Möller cos( ) x y ⇥ P 8 Derivation of rotation matrix • Make use of polar coordinates: (r, ) x = r cos( ) y = r sin( ) x = r cos(⇥ + ) = r cos(⇥) cos( ) (x, y) x = r cos(⇥ + ) y = r sin(⇥ + ) (r, ) (x, y) r sin(⇥) sin( ) = x cos( ) y sin( ) y = r sin(⇥ + ) = r sin(⇥) cos( ) + r cos(⇥) sin( ) = y cos( ) + x sin( ) © Machiraju/Zhang/Möller 9 Shearing in 2D P = SHy x y P = SHx x y ⇥ = 1 a 0 1 ⇥ P x y ⇥ ⇥ = 1 0 b 1 ⇥ P x y ⇥ x = x + ay y =y © Machiraju/Zhang/Möller 10 Schedule • • • • • • • • Geometry basics Affine transformations Use of homogeneous coordinates Concatenation of transformations 3D transformations Transformation of coordinate systems Transform the transforms Transformations in OpenGL © Machiraju/Zhang/Möller 11 Homogeneous coordinates • Only translation cannot be expressed as matrix-vector multiplications • But we can add a third coordinate – Homogeneous coordinates for 2D points – (x, y) turns into (x, y, W) – if (x, y, W) and (x', y', W’ ) are multiples of one another, they represent the same point – typically, W ≠ 0. – points with W = 0 are points at infinity © Machiraju/Zhang/Möller 12 2D Homogeneous Coordinates • Cartesian coordinates of the homogenous point (x, y, W): x/W, y/W (divide through by W) • Our typical homogenized points: (x, y, 1) • Connection to 3D? – (x, y, 1) represents a 3D point on the plane W = 1 – A homogeneous point is a line in 3D, through the origin © Machiraju/Zhang/Möller 13 Transformation in homogeneous coordinates • General form of affine (linear) transformation • E.g., 2D translation in homogeneous coordinates © Machiraju/Zhang/Möller 14 Homogeneous Coords (3) • 2D line equation – Duality of 3D points to a line a • 3D plane equation: – Duality of 4D points to a plane • Homog. Coords in 1D? ax + by + c = 0 ⇤ ⌅ x ⇥ b c ⇧ y ⌃=0 1 ax + by + cx + d = 0 ⇤ ⌅ x ⇥⌥ y a b c d ⌥ ⇧ z ⌃=0 1 © Machiraju/Zhang/Möller 15 Basic 2D transformations © Machiraju/Zhang/Möller 16 Inverse of transformations • Inverse of T(dx, dy) = T(−dx, −dy) T(−dx, −dy) = T(dx, dy)−1 • R(θ)−1 = R(−θ) • S(sx, sy)−1 = S(1/sx, 1/sy) • Sh(ax)−1 = Sh(−ax) © Machiraju/Zhang/Möller 17 Schedule • • • • • • • • Geometry basics Affine transformations Use of homogeneous coordinates Concatenation of transformations 3D transformations Transformation of coordinate systems Transform the transforms Transformations in OpenGL © Machiraju/Zhang/Möller 18 Compound transformations • Often we need many transforms to build objects (or to “direct” them) – e.g., rotation about an arbitrary point/line? • Concatenate basic transforms sequentially • This corresponds to multiplication of the transform matrices, thanks to homogeneous coordinates © Machiraju/Zhang/Möller 19 Compound translation • What happens when a point goes through T(dx1, dy1) and then T(dx2, dy2)? • Combined translation: T(dx1+dx2, dy1+dy2) ⇥ ⇥ ⇥ 1 0 dx2 1 0 dx1 1 0 dx1 + dx2 ⇤ 0 1 d y2 ⌅ ⇤ 0 1 d y1 ⌅ = ⇤ 0 1 d y1 + d y2 ⌅ 0 0 1 0 0 1 0 0 1 Concatenation of transformations: matrix multiplication © Machiraju/Zhang/Möller 20 Compound rotation • What happens when a point goes through R(θ) and then R(φ)? • Combined rotation: R(θ + φ) • Scaling or shearing (in one direction) is similar • These concatenations are commutative © Machiraju/Zhang/Möller 21 Commutativity • Transformations that do commute: – – – – – Translate – translate Rotate – rotate (around the same axis) Scale – scale Uniform scale – rotate Shear in x (y) – shear in x (y), etc. • In general, the order in which transformations are composed is important • After all, matrix multiplications are not commutative © Machiraju/Zhang/Möller 22 Commutativity • the order in which we compose matrices is important: matrix multiplication does not y y commute X then Z y x z z x x z y y Z then X z x © Machiraju/Zhang/Möller z x 23 Rotation about an arbitrary point • Break it down into easier problems: – Translate to the origin: T(−x1, −y1) – Rotate: R(q) – And translate back: T(x1, y1) © Machiraju/Zhang/Möller 24 Rotation about an arbitrary point • Compound transformation (non-commutative): T(x1, y1) • R(q) • T(− x1, − y1) – P’ = T(x1, y1)R(q)T(− x1, − y1)P = AP – Why combine these matrices into a single A? © Machiraju/Zhang/Möller 25 Another example • Compound transformation: T(x2, y2) • R(90o) • S(sx, sy) • T(−x1, −y1) © Machiraju/Zhang/Möller 26 Rigid-body transformation • A transformation matrix • where the upper 2 × 2 submatrix is orthonormal, preserves angles and lengths – called rigid-body transformation – any sequence of translation and rotation give a rigid-body transformation © Machiraju/Zhang/Möller 27 Affine transformation • Preserves parallelism, not lengths or angles Unit cube Rot(−45o) Scale in x • Product of a sequence of translation, scaling, rotation, and shear is affine © Machiraju/Zhang/Möller 28 Types of Transforms • Continuous/Discrete, one to one ; invertible • Classification – Euclidean (preserves angles) • translation, rotation, uniform scale – Affine (preserves parallel lines) • Euclidean plus non-uniform scale, shearing – Projective (preserves lines) – Isometry (preserves distance) • Reflection, rigid body motion – Nonlinear (lines©become curves) Machiraju/Zhang/Möller • Twists, Bends, warps, morphs 29 Window-To-Viewport Transformation • Important example of compound transform • “World coords”: space users work in • need to map world to the screen – window in world-coordinate space and – viewport in screen coordinate space © Machiraju/Zhang/Möller 30 Window-To-Viewport Transformation (2) • one can specify non-uniform scaling • results in distorted images • What property of window and viewport indicates whether the mapping will distort the images? © Machiraju/Zhang/Möller 31 Window-To-Viewport Transformation (3) • Specify transform in matrix form Mwv = T umin vmin ⇥ ⇥S umax xmax vmax ymax umin xmin vmin ymin © Machiraju/Zhang/Möller ⇥ ⇥T xmin ymin ⇥ 32