CS 536 Computer Graphics Transformations Week 9, Lecture 18 2D Transformations David Breen, William Regli and Maxim Peysakhov Department of Computer Science Drexel University 1 3 2D Affine Transformations 2D Affine Transformations All represented as matrix operations on vectors! Parallel lines preserved, angles/lengths not Scale Rotate Translate Reflect Shear 4 Pics/Math courtesy of Dave Mount @ UMD-CP Example 1: rotation and non uniform scale on unit cube Example 2: shear first in x, then in y Note: Preserves parallels Does not preserve lengths and angles 5 2D Transforms: Translation 2D Transforms: Scale Rigid motion of points to new locations Stretching of points along axes: Defined with column vectors: In matrix form: as 6 or just: 7 1
2D Transforms: Rotation 2D Transforms: Rotation Rotation of points about the origin Substitute the 1 st two equations into the 2 nd two to get the general equation Positive Angle: CCW Negative Angle: CW rcos(θ + φ) rcos(φ) Matrix form: x= x cos(θ) y sin(θ) or just: 8 y= x sin(θ) + y cos(θ) 9 Homogeneous Coordinates Observe: translation is treated differently from scaling and rotation Homogeneous coordinates: allows all transformations to be treated as matrix multiplications Example: A 2D point (x,y) is the line (x,y,w), where w is any real #, in 3D homogenous coordinates. To get the point, homogenize by dividing by w (i.e. w=1) 10 Recall our Affine Transformations 11 Pics/Math courtesy of Dave Mount @ UMD-CP Matrix Representation of 2D Affine Transformations Translation: Scale: Rotation: Composition of 2D Transforms Rotate about a point P1 Translate P1 to origin Rotate Translate back to P1 P = T P T Shear: Reflection: F y = 12 13 2
Composition of 2D Transforms Composition of 2D Transforms Scale object around point P1 P1 to origin Scale Translate back to P1 Compose into T Scale + rotate object around point P1 and move to P2 P1 to origin Scale Rotate Translate to P2 P = T P P = T P 14 15 Composition of 2D Transforms Be sure to multiple transformations in proper order! P = (T (R (S (T P)))) P = ((T (R (S T))) P) P = T P The Window-to-Viewport Transformation Problem: Screen windows cannot display the whole world (window management) How to transform and clip: Objects to Windows to Screen 16 18 Pics/Math courtesy of Dave Mount @ UMD-CP Window-to-Viewport Transformation Given a window and a viewport, what is the transformation from WCS to VPCS? Three steps: Translate Scale Translate Transforming World Coordinates to Viewports 3 steps 1. Translate 2. Scale 3. Translate Overall Transformation: 19 P= M wv P 20 3
3D Transformations Representation of 3D Transformations Z axis represents depth Right Handed System When looking down at the origin, positive rotation is CCW 24 Left Handed System When looking down, positive rotation is in CW More natural interpretation for (into screen) displays, big z means far 25 3D Homogenous Coordinates Homogenous coordinates for 2D space requires 3D vectors & matrices Homogenous coordinates for 3D space requires 4D vectors & matrices [x,y,z,w] 26 3D Transformations: Scale Scale Parameters for each axis direction 27 3D Transformations: Translate Translation 3D Transformations: Rotation One rotation for each world coordinate axis 28 29 4
c = cos(θ) s = sin(θ) t = (1 - c) Rotation Around an Arbitrary Axis Rotate a point P around axis n (x,y,z) by angle θ # tx 2 + c txy + sz txz sy 0& % ( txy sz ty 2 + c tyz + sx 0 R = % ( % txz + sy tyz sx tz 2 + c 0( % ( $ 0 0 0 1 Graphics Gems I, p. 466 & 498 30 Rotation Around an Arbitrary Axis Also can be expressed as the Rodrigues Formula P rot = P cos(ϑ ) + (n P)sin(ϑ ) + n(n P)(1 cos(ϑ )) 31 Improved Rotations Euler Angles have problems How to interpolate keyframes? Angles aren t independent Interpolation can create Gimble Lock, i.e. loss of a degree of freedom when axes align Solution: Quaternions! 32 33 p = (0, x ) 34 35 5
36 37 What about interpolating multiple keyframes? A & B are quaternions slerp Spherical linear interpolation Need to take equals steps on the sphere 38 Shoemake suggests using Bezier curves on the sphere Offers a variation of the De Casteljau algorithm using slerp and quaternion control points See K. Shoemake, Animating rotation with quaternion curves, Proc. SIGGRAPH 85 39 Reflection: 3D Transformations: Reflect Reflection corresponds to negative scale factors about x-y plane s x = -1 s y = 1 original " $ about y-z plane F x = $ $ $ # 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 % & 40 s x = -1 s y = -1 s x = 1 s y = -1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E Addison-Wesley 2012 6
3D Transformations: Shear Shear: (function of y)! 1 sh x 0 0 $ # & H x = # 0 1 0 0 & # 0 0 1 0 & # & " 0 0 0 1 % x = x +sh x y 43 3D Transformations: Shear " X % " y z 1 Sh x Sh x 0% " X% $ $ x z $ $ Y Sh = $ y 1 Sh y 0 $ Y x y $ Z $ Sh z Sh z 1 0 $ Z $ $ $ # 1 & # 0 0 0 1& # 1 & X = X + Sh x y Y + Sh x z Z Y = Sh y x X +Y + Sh y z Z Z = Sh z x X + Sh z y Y + Z 45 Example: Composition of 3D Transformations Goal: Transform P 1 P 2 and P 1 P 3 Example (Cont.) Process 1. Translate P 1 to (0,0,0) 2. Rotate about y 3. Rotate about x 4. Rotate about z (4) (1) (2-3) 46 47 What we ve really done is transform the local coordinate system R x, R y, R z to align with the origin x,y,z Final Result Example 2: Composition of 3D Transformations Airplane defined in x,y,z Problem: want to point it in Dir of Flight (DOF) centered at point P Note: DOF is a vector Process: Rotate plane Move to P 48 49 7
Example 2 (cont.) Z p axis to be DOF X p axis to be a horizontal vector perpendicular to DOF y x DOF Y p, vector perpendicular to both Z p and X p (i.e.z p x X p ) Transformations to Change Coordinate Systems Issue: the world has many different relative frames of reference How do we transform among them? Example: CAD Assemblies & Animation Models 50 51 Transformations to Change Coordinate Systems 4 coordinate systems 1 point P Coordinate System Example (1) Translate the House to the origin M M M 1 2 2 3 3 4 = T (4,2) = T (2,3) S(0.5,0.5) = T (6.7,1.8) R(45 ) M 1 2 = T(x 1,y 1 ) M 2 1 = ( M 1 2 ) 1 = T( x 1, y 1 ) P 1 52 The matrix M ij that maps points from coordinate system j to i is the inverse of the matrix M ji that maps points from coordinate system j to coordinate system i. 53 Coordinate System Example (2) Transformation Composition: M 5 1 = M 5 4 M 4 3 M 3 2 M 2 1 54 World Coordinates and Local Coordinates To move the tricycle, we need to know how all of its parts relate to the WCS Example: front wheel rotates on the ground wrt the front wheel s z axis: P (wo) = T(αr,0,0) R z (α) P (wh ) Coordinates of P in wheel coordinate system: P (wh) = R z (α) P (wh ) 55 8
Properties of Transformation Matrices Note that matrix multiplication is not commutative i.e. in general M 1 M 2 M 2 M 1 T reflection around y axis T rotation in the plane 9