CS770/870 Spring 2017 Transformations Coordinate sstems 2D Transformations Homogeneous coordinates Matrices, vectors, points Coordinate Sstems Coordinate sstems used in graphics Screen coordinates: the phsical location on a specific displa device Window coordinates: screen coords relative to window origin Normalized device coordinates (NDC: a device independent abstraction of displa window World coordinates: a generic coordinate sstem that an application can use to define objects for displa 0,0 s s 0,0 w w 01/29/2017 1 2 Screen Coordinates GUI Window Coordinates Phsical device coords Location of piels S b S piels 640480, 1024768, 1280800, 1440900, etc. Variation is difficult for application program; leads to device dependent code S -1, S -1 GUI windows add another complication GUI Window sstem gives the application a portion of the screen that is w h piels, for arbitrar w and h Window sstem maps window coordinates to screen coordinates w-1, h-1 3 4
Normalized Device Coordinates An abstraction of displa device coordinates (screen and/or GUI window Floating point (, coordinates from 0 to 1 goes up 1, 1 World Coordinates The application should be able to define objects in a wa natural for the domain: 0 to 100 feet for a house floor plan 0 to 4000 miles for a map of the US etc. World coordinates are simpl 2D or 3D float or double values that can represent locations and sizes of object to be displaed 5 6 Mapping World Coordinates to Device Coordinates Application needs to tell the graphics sstem: what portion of the world is to be displaed, called the window on the world. where on the screen the window should be mapped, called the viewport. In 2D the window is a rectangle in world coordinates the viewport is a rectangle in normalized device coordinates Some graphics sstems allow a rotation as well 7 Window to Viewport Mapping Window: ( lo, lo to ( hi, hi Viewport: (u lo, v lo to (u hi, v hi lo, lo World Coordinates ( hi, hi Viewport Window u lo, v lo NDC (uv 1, 1 u hi, v hi 8
Window to Viewport Mapping Window to Viewport Mapping 2 Given (, in world, what is its (u,v in ndc? Consider the coordinate first Let S be the width of the window = hi - lo Let S u be the width of the viewport = u hi - u lo what of the wa from lo to hi is? ratio = ( - lo / S it is eactl that of the wa from u lo to u hi in ndc: uratio = ( u - u lo / S u so, ( u - u lo / S u = ( - lo / S or, u = u lo + ( - lo / S * S u Similarl, v = v lo + ( - lo / S * S v u = u lo + ( - lo / S * S u v = v lo + ( - lo / S * S v lo, lo - lo World Coordinates + S S hi, hi u lo, u lo u-u lo NDC + S u 1, 1 u hi, v hi S v 9 10 Clipping What if (, is outside the window? it should not be visible in the viewport cant just ignore a point outside window; its part of a line or polgon or more comple object must displa onl the portion of each object that is inside the window must do it efficientl well re-visit clipping later 2D Transformations Wed like to define an object with 1 set of coordinate specifications, then transform it to another location, size, and/or orientation. Translation b (d, d Scale b (s, s Rotation b α about the origin (0,0 11 12
2D Translation 2D Scale Translation b ( d, d for each (, in the object = + d ; = + d for each (, in the object = * s; = * s 3*s, 3*s 2*s, 2*s d 3, 3 2, 2 1*s, 1*s d 1, 1 CS770/870 Fall 2014 Bergeron 13 14 2D Rotation 2D Ais Ordering Conventions Rotation of α degrees about the origin, where α > 0 is counter clockwise rotation = cos α - sin α = sin α + cos α Note: equations onl make sense if the aes labels are known If we sa the aes are labeled, we assume that is horizontal, increasing to the right, and is vertical increasing upward It s pure convention What is a positive angle? if aes are, an angle >0 rotates -ais towards another convention. v coord sstem uv coord sstem u coord sstem 15 16
Affine Transformations Affine transformations leave the origin (0,0 unchanged Scale and rotation are affine transformations Translation is not affine 2D affine transformations can be elegantl represented in matri format as 22 matrices unifies the representation of scale and rotation provides an eas implementation of composition of transformations: rotate, then scale, then rotate Linear Algebra Linear algebra isn t scar: it s notation. Consider the 2 linear equations on the right The meaning of the equations is all in the constants (a,b,c,d; the variables (u,v,, are just placeholders. We encapsulate the constants in a 22 matri and the variables as 21 vectors. u = a + b v = c + d " u v " a c " b d 17 18 Linear Algebra Operators To utilize this notation efficientl, we can define some operators on matrices and vectors: Multiplication of a matri times a vector let s us recover the underling equations The dot product (also called inner product let s us combine 2 vectors into a scalar; it s definition leads to convenient computations of vector lengths and the angle between two vectors The cross product (outer product let s us easil compute a new vector that is orthogonal to 2 vectors Dot (inner Product Given two vectors of the same length, the dot product is the sum of the products of corresponding entries: " a b " a+b which reall should be written as a row vector * a column vector " a b " a+b 19 20
Matri times a vector Consider the 22 matri and a 21 vector: " a b " c d The product of the matri * vector is defined as " a + b c + d Each entr is the dot product of a row of the input matri with the input vector (thus the row*col form of the dot product definition 21 " Matri times Matri The row/column dot product convention etends to matri products: Row i of the left operand is dotted with column j of the right operand to get position (i,j in the result: a c b d r s " t u " ar + bt as + bu cr + dt cs + du All this etends easil to 3D and more 22 Matrices for Affine Transforms Matri Multiplication Scale: = S, = S " S 0 0 S Rotation: = cos α - sin α, = sin α + cos α cosα sinα sinα cosα ( " 0 1 ais (1,0 ais (0,1 ais (0,1 - ais (-1,0 1 0 Assumes aes are labeled. +90 rotation: cos 90 = 0, sin 90 = 1 23 Matri multiplication is just a series of matrivector multiplications Each column of the right hand matri is multiplied b the left to get the net column of result a b u * " c d " w au + bw a + b " cu + dw c + d Matri multiplication is associative (M1 * (M2 * V = (M1 * M2 * V 24
Transformation Composition Suppose ou want to scale an object b ( s, t, then rotate b α degrees Create S and R matrices Let M = R*S foreach point in object, p newp = M * p Simpler and more efficient than first transforming all points b S and then transforming the results b R. Wh? Are Affine Transforms Smmetric? Given two affine transforms, M 1 and M 2 Does M 1 * M 2 = M 2 * M 1? Consider S = scale b ( 1, 2, R = rotate b 90 Does S*R = R*S Surel, not. What about 2 rotations? es (in 2D, but not 3D What about 2 scales? es Rotation and uniform scale? es 25 26 THE Problem Translation is not affine but we cant live without it How can we combine translation with scale and rotate into one unified model? Homogeneous coordinates Well present it as a convenient engineering trick There is a rigorous mathematical eplanation 27 Translation Transformation = + d, = + d Consider the 33 matri and the 31 vector: " 1 0 d 0 1 d 0 0 1 " 1 The product of this matri times the vector is: " + d + d 1 Its just what we want (if we ignore the etra coord But what about scale and rotation? 28
Revised Scale/Rotate Matrices Epand the Scale and Rotate matrices to 33 " S 0 0 0 S 0 0 0 1 cosα sinα 0 sinα cosα 0 0 0 1( You should verif that multipling these matrices b [ 1] produce same results as before. Homogeneous Coordinates The 33 matri representation for 2D transformations is called homogeneous coordinates The 3rd coordinate is called the homogeneous coordinate and is usuall designated with a w In this model, rotation, scale and translation can be represented in a single uniform notation and can be composed in arbitrar order 29 30 Wh does it work? There is a comple mathematical eplanation and an intuitive geometric one. 2D Shear Transform Shear of b a factor of : = + *h turns a rectangle into a parallelogram Which do ou want? Shear of b a factor of : = + * h similar result 31 32
What is a 2D shear in 1D? Suppose ou represent all our objects with =1; this becomes a 1D world with scale and translation operations. A 2D shear becomes a 1D translation at =1 =1 Similarl, a 3D shear at w=1 is a 2D translation in the w=1 plane 33 2D Shear Matri 22 shear matri " 1 h h 1 33 homogeneous shear matri for 2D shear " 1 h 0 h 1 0 0 0 1 Verif that these are correct 34 3D Shear Matri A complete 3D shear matri is " 1 h h z h 1 h z h z h z 1 " 1 0 h w 0 1 h w 0 0 1 But, when we use the matri as a 2D homogeneous matri, all the factors ecept h z and h z are 0 and those should be renamed as h w and h w or even d, d " 1 0 d 0 1 d 0 0 1 35 Scale/Rotation about a Point Scale/Rotation leave the origin fied we sa that the occur about the origin Often, want the transformation to occur about an arbitrar point, perhaps the objects center Can do this with a composite transformation that: translates the desired center point to the origin applies the scale or rotate (or both translates the origin back to the original center point The composite is applied to all points 36
Arbitrar Rotation Rotate b α about (c, c T(-c, -c : Translate (c, c to ( R(α: Rotation b α T(c, c : Translate ( to (c, c (c, c T(-c, -c R(α T(c, c (c, c Arbitrar Rotation Matrices Rotate b α about (c, c T 1, Translate (c, c to ( R, Rotation T 2, Translate ( to (c, c Composite C = T 2 RT 1 for each point P: P= CP 1 0 c ( T 1 = 0 1 c ( 0 0 1 ( cosα sinα 0 R = sinα cosα 0 0 0 1( " 1 0 c T 2 = 0 1 c 0 0 1 37 38 Multiplication Eercise Review/Preview C=T 2 RT 1 1. T 2 *R r 0 *c 0 r 0 *c 1 r 0 *c 2 etc 2. (T 2 R*T 1 " 1 0 c " cosα sinα 0 " 1 0 c C = 0 1 c sinα cosα 0 0 1 c 0 0 1 0 0 1 0 0 1 cosα sinα c 1 0 c = sinα cosα c 0 1 c 0 0 1 ( 0 0 1 ( cosα sinα c cosα + c sinα + c = sinα cosα c sinα c cosα + c 0 0 1 ( Review Coordinate sstems 2D Transformations Homogeneous coordinates Matrices, vectors, points Preview 3D Transformations Homogeneous coords Matrices, vectors, points 39 40