CS3 INTRODUCTION TO COMPUTER GRAPHICS Geometric Transformations D and 3D
CS3 INTRODUCTION TO COMPUTER GRAPHICS Grading Plan to be out Wednesdas one week after the due date
CS3 INTRODUCTION TO COMPUTER GRAPHICS How do we use Geometric Transformations? (/) } Objects in a scene are a collection of points } These objects have location, orientation, size } Correspond to transformations: Translation (T), Rotation (R), and Scaling (S) 3
CS3 INTRODUCTION TO COMPUTER GRAPHICS How do we use Geometric Transformations? (/) } A scene has a camera/view point from which the scene is viewed } The camera has some location and some orientation in 3-space } These correspond to Translation and Rotation transformations 4
CS3 INTRODUCTION TO COMPUTER GRAPHICS Linear Transformations (/3) } We represent vectors as bold-italic letters (v) and scalars as italic letters (c) 5
CS3 INTRODUCTION TO COMPUTER GRAPHICS Linear Transformations (/3) } Definition of a linear function f: } f(v+w) = f(v) + f(w) for all v and w in the domain of f } f(cv) = cf(v) for all scalars c and elements v in the domain } Example: f(x) = f(x, x ) := (3x +x, -3x +4x ) } Check linearit f(e ) } f(v+w) = f(v +w, v +w ) e = (3(v +w )+(v +w ), -3(v +w )+4(v +w )) e = (3v +v, -3v +4v ) + (3w +w, -3w +4w ) = f(v) + f(w) } We can check the second propert the same wa f(e ) 6
CS3 INTRODUCTION TO COMPUTER GRAPHICS Is X linear? } Is rotation linear? } Is scaling linear? } Is Translation linear? 7
CS3 INTRODUCTION TO COMPUTER GRAPHICS Linear Transformations (3/3) } Graphical use: transformations of points around the origin (leaves the origin invariant) } These include Scaling and Rotations } Translation is not a linear function (moves the origin) 8
CS3 INTRODUCTION TO COMPUTER GRAPHICS Linear Transformations as Matrices (/) } Linear transformations can be represented as matrices } Let s start with D transformations. These can be represented b x matrices: T = a c b d 9
CS3 INTRODUCTION TO COMPUTER GRAPHICS Linear Transformations as Matrices (/) } A transformation of an arbitrar column vector x = has the form: x x + + = = dx cx bx ax x x d c b a x x T
CS3 INTRODUCTION TO COMPUTER GRAPHICS Scaling in D } Scale x b 3, b (S x = 3, S = ) 3
CS3 INTRODUCTION TO COMPUTER GRAPHICS Scaling in D Side effect: House shifts position relative to origin } Scale x b 3, b (S x = 3, S = ) } v = (original vertex); } v = (new vertex) } v = Sv x ' ' x S= s x s 6 9 3
CS3 INTRODUCTION TO COMPUTER GRAPHICS Rotation in D (/) } Rotate b θ about the origin } Derive R θ b determining how e and e should be transformed: θ } (first column of R θ ) } (second column of R θ ) e = sinθ cosθ R θ = cosθ sinθ sinθ cosθ 3
CS3 INTRODUCTION TO COMPUTER GRAPHICS Rotation in D (/) } Let s tr matrix-vector multiplication } R θ * v = cosθ sinθ sinθ x cosθ = x x cosθ sinθ+ sinθ cosθ = x' ' = v' } } x'= xcosθ sinθ '= xsinθ + cosθ } Other properties of rotation: } Preserves lengths in objects and angles between parts of objects (rigid-bod) } For objects not centered at the origin, an unwanted translation might be introduced (rotation is alwas about the origin) 4
CS3 INTRODUCTION TO COMPUTER GRAPHICS What about translation? } Translation is not a linear transformation (the origin is not invariant) } Therefore, it can t be represented as a x invertible matrix } Question: Is there another solution? } Answer: Yes, v = v + t, where t = dx d } However, using vector addition is not consistent with our method of treating transformations as matrices 5
CS3 INTRODUCTION TO COMPUTER GRAPHICS Homogeneous coordinate } Represent a D point } B a 3D vector x x 6
CS3 INTRODUCTION TO COMPUTER GRAPHICS Homogeneous coordinate } Represent a D point } B a 3D vector x x 7
CS3 INTRODUCTION TO COMPUTER GRAPHICS Back to Translation } Our translation matrix (T) can now be represented b embedding the translation vector in the right column: Dimension of T? } To verif that this is the right matrix, multipl it b our homogenized point: } Coordinates have been translated, and v is still homogeneous 8
CS3 INTRODUCTION TO COMPUTER GRAPHICS //3 Back to Translation } Our translation matrix (T) can now be represented b embedding the translation vector in the right column: } To verif that this is the right matrix, multipl it b our homogenized point: } Coordinates have been translated, and v is still homogeneous = d dx T Tv = v' + + = = d dx x x d dx 9
CS3 INTRODUCTION TO COMPUTER GRAPHICS Transformations Homogenized } Let s homogenize our all matrices! Doesn t affect linearit of scaling and rotation } Our new transformation matrices look like this Transformation Scaling Rotation Translation Matrix s x s } Note: These transformations are called affine transformations cosθ sinθ sinθ cosθ dx d
CS3 INTRODUCTION TO COMPUTER GRAPHICS Examples } Scaling: Scale b 5 in the x direction, 7 in the 5 7 } Rotation: Rotate b 3 o cos(3) sin(3) sin(3) cos(3) } Translation: Translate b -6 in the x direction, +8 in the 6 8 //3
CS3 INTRODUCTION TO COMPUTER GRAPHICS Before we continue! Vectors vs. Points } Up until now, we ve onl used the notion of a point in our D space } We now present a distinction between points and vectors } We used homogeneous coordinates to more convenientl represent translation; hence points are represented as (x,, ) T } A vector can be rotated/scaled, not translated (can think of it as alwas starting at origin), don t use the homogeneous coordinate: (x,, ) T } For now, let s focus on just our points (tpicall vertices) //3
CS3 INTRODUCTION TO COMPUTER GRAPHICS Inverses } How do we find the inverse of a transformation? } Take the inverse of the transformation matrix (thanks to homogenization, the are all invertible!): 3
CS3 INTRODUCTION TO COMPUTER GRAPHICS Composition of Transformations (D) (/) } We now have a number of tools at our disposal; we can combine them! } An object in a scene uses man transformations in sequence. How do we represent this in terms of functions? } Think of a transformation that first translates b (, 3) M, then scales b (.5,.3) M. } Our composition is equivalent to M M v //3 4
CS3 INTRODUCTION TO COMPUTER GRAPHICS Composition of Transformations (D) (/) } Compositions of transformation matrices to form a more complex transformation } For example, TRSv, which scales, rotates, then translates a point: dxcosθ sinθ sx x d sinθ cosθ s } We appl the matrices from right to left. Associativit allows composition. One matrix does S, R, and T. No need to multiple 3 times. } Important: order matters! Matrix multiplication is NOT commutative. } Be sure to check out the Transformation Game at http://www.cs.brown.edu/exploratories/freesoftware/ repositor/edu/brown/cs/exploratories/applets/transformationgame/transformation_game_guide.html } Let s see an example //3 5
CS3 INTRODUCTION TO COMPUTER GRAPHICS Not commutative 6 Translate b x = 6, =, then rotate b 45 o 5 4 3 3 4 5 6 7 8 9 Rotate b 45 o, then translate b x = 6, = Y 6 5 4 3 3 4 5 6 7 8 9 X //3 6
CS3 INTRODUCTION TO COMPUTER GRAPHICS //3 Composition (an example) (D) (/) } Start: Goal: } Important concept: make the problem simpler } Translate object to origin first, scale, rotate, and translate back: } Appl to all vertices = 3 3 3 3 cos9 sin 9 sin 9 cos9 3 3 RST T -Uniform scale 3x -Rotate 9 o -Both around object s center, not the origin 7
CS3 INTRODUCTION TO COMPUTER GRAPHICS Composition (an example) (D) (/) } T - RST } But what if we mixed up the order? Let s tr RT - ST: cos9 sin9 3 3 3 sin9 cos9 3 3 3 } Oops! We scaled properl, but when we rotated the object, it s center was not at the origin, so its position was shifted. Order matters! //3 8
CS3 INTRODUCTION TO COMPUTER GRAPHICS Inverses Revisited } What is the inverse of a sequence of transformations? } (M M M n ) - = M n - M n- - M - } Inverse of a sequence of transformations is the composition of the inverses of each transformation in reverse order (wh?) } Sa we want to do the opposite transformation of the example on slide 7. What will our sequence look like? } (T - RST) - = T - S - R - T } We still translate to the origin first, then translate back at the end! T - S - R - T = 3/ 3 3 / 3 cos9 sin 9 sin 9 cos9 3 3 //3 9
CS3 INTRODUCTION TO COMPUTER GRAPHICS Question } You have an object at (3, 4). } What is the transformation to } Rotate b 3 degrees with respect to the object } Scale b along x and 3 along with respect to the object } Translate b (, -) in the world coordinate frame? //3 3
CS3 INTRODUCTION TO COMPUTER GRAPHICS Question } You have an object at (3, 4). } What is the transformation to } Rotate b 3 degrees with respect to the object } Scale b along x and 3 along with respect to the object } Translate b (, -) in the world coordinate frame? } What is the inverse? //3 3
CS3 INTRODUCTION TO COMPUTER GRAPHICS Aside: Skewing/shearing } Skew an object to the side, like shearing a card deck b displacing each card relative to the previous one } What phsical situations mirror this behavior? } Squares become parallelograms; x-coordinates skew to right, stas the same Y 6 5 4 3 θ π θ = 4 3 4 5 6 7 8 9 Skew θ = tanθ D non-homogeneous X } Notice that the base of the house (at = ) remains horizontal but shifts right. Wh? Skew θ tanθ = D homogeneous //3 3
CS3 INTRODUCTION TO COMPUTER GRAPHICS //3 } Windowing transformation maps contents of a D clip rectangle ( window ) to a rectangle on the screen, e.g., interior canvas of a window manager s window ( viewport ), so also known as a window-to-viewport transformation } Sends rectangle with bounding coordinates (u, v i ), (u, v ) to (x, ), (x, ) } The transformation matrix here is: Aside: Windowing Transformations ) ) /( ( ) ) /( ( ) ) /( ( ) ) /( ( v v v v v v u u u x x u u u x x 33
CS3 INTRODUCTION TO COMPUTER GRAPHICS Aside: Homogeneous Coordinates (/3) } Allow expression of all three D transformations as 3x3 matrices } We start with the point P d on the x plane and appl a mapping to bring it to the w- plane in the hperspace } P d (x,) à P h (wx, w, w), w } The resulting (x, ) coordinates in our new point P h are different from the original (x,), since x = wx, = w } P h (x,, w), w //3 34
CS3 INTRODUCTION TO COMPUTER GRAPHICS Aside: Homogeneous Coordinates (/3) } Once we have this point, we can appl a homogenized version of our transformation matrices (next slides) to get a new point in the hperspace } Finall, we want to obtain the corresponding point in D-space, so perform the inverse of the previous mapping (divide all entries b w) } The vertex v = is now represented as v = x x //3 35
CS3 INTRODUCTION TO COMPUTER GRAPHICS Aside: Homogeneous Coordinates (3/3) } The transformations we use will alwas map points in the hperplane defined b w = to other such points. } In other words, we want our transformations T to map points v = to points v = x' ' } How do we achieve this with the matrices we have alread derived? } For linear transformations (i.e. scaling and rotation), embed the existing matrix in the upper-left of a new 3x3 matrix: x a c b d //3 36
CS3 INTRODUCTION TO COMPUTER GRAPHICS Dimension++ (3D!) } How should we treat geometric transformations in 3D? } Just add one more coordinate/axis! } A point is represented as x z } Homogeneous coordinate is x z //3 37
CS3 INTRODUCTION TO COMPUTER GRAPHICS Transformations in 3D Transformation Matrix Comments Scaling sx s Looks just like the D version. We just added an s z term. Rotation (see next slide) In D, onl one axis of rotation; now there are infinitel man! Must take all into account. See next slide... s z Translation dx d dz Similar to the D version, just with one more entr dz, representing change in the z- direction. //3 38
CS3 INTRODUCTION TO COMPUTER GRAPHICS Rodrigues s Formula } Rotation b angle θ around vector u = [u x u u z ] T } Note: this is an arbitrar unit vector u in xz-space } Here s a not so friendl rotation matrix u u x x cosθ u u z + u x ( cosθ ) ( cosθ ) + u ( cosθ ) u sinθ sinθ } This is called the coordinate form of Rodrigues s formula } Let s tr a different approach z u u x u ( cosθ ) u cosθ u z + u ( cosθ ) ( cosθ ) + u z x sinθ sinθ u u x u u z z ( cosθ ) + u ( cosθ ) u cosθ + u z x sinθ sinθ ( cosθ ) //3 39
CS3 INTRODUCTION TO COMPUTER GRAPHICS Rotating axis b axis } Ever rotation can be represented as the composition of 3 different angles of counterclockwise rotation around 3 axes, namel } x-axis in the z plane b ψ; -axis in the xz plane b θ; z-axis in the x plane b ϕ } Also known as Euler angles, make problem of rotation much easier } R z : rotation around x axis, R x : rotation about axis, R x : rotation about z axis } Note these differ onl in how the 3x3 submatrix is embedded in the homogeneous matrix } You can compose these matrices to form a composite rotation matrix //3 4
CS3 INTRODUCTION TO COMPUTER GRAPHICS Inverses and Composition in 3D! } Inverses are once again parallel to their D versions Transformation Inverse Matrix Scaling Rotation Translation /s x /s /s z cosφ sinφ sinφ cosφ, cosψ sinψ sinψ cosψ dx d dz } Composition works exactl the same wa, cosθ sinθ sinθ cosθ //3 4
CS3 INTRODUCTION TO COMPUTER GRAPHICS //3 Example in 3D! } Let s take some 3D object, sa a cube centered at (,,) } Rotate in object s space b 3 o around x axis, 6 o around, and 9 o around z } Scale in object space b in the x, in the, 3 in the z } Translate b (,,4) in world space } Transformation sequence: TT - S x R x R xz R z T o, where T translates to (,): cos3 sin 3 sin 3 cos3 cos6 sin 6 sin 6 cos6 cos9 sin 9 sin 9 cos9 3 4 T T - S x R x R xz R z T 4
CS3 INTRODUCTION TO COMPUTER GRAPHICS Transformations and the scene graph (/4) } Objects can be complex: Scene Graph ROBOT upper bod lower bod head trunkarm arm stanchion base } 3D scenes are often stored in a directed acclic graph (DAG) called a scene graph } WPF (Windows Presentation Foundation) } Open Scene Graph (used in the Cave) } Sun s Java3D } X3D (VRML was a precursor to X3D) } Tpical scene graph format: } objects (cubes, sphere, cone, polhedra etc.): stored as nodes (default: unit size at origin) } attributes (color, texture map, etc.): stored as separate nodes } Transformations: also nodes //3 43
CS3 INTRODUCTION TO COMPUTER GRAPHICS Transformations and the scene graph (3/4) } Notion of a cumulative transformation matrix that builds as ou move up the tree (CTM), appending higher level transformation matrices to the front of the sequence M M M 3 M 4 } Example: } For object (o), CTM = M M 5 } For o, CTM = M M 3 } For o3, CTM = M M 4 M 5 } For a vertex v in o3, position in world coordinate sstem is CTM v = (M M 4 M 5 ) v object nodes (geometr) transformation nodes group nodes //3 44