Geometric Transformations CENG 477 Introduction to Computer Graphics Fall 2015-2016 Computer Engineering METU 2D Geometric Transformations Basic Geometric Transformations ● ● ● ● Geometric transformations are used to transform the objects and the camera in a scene (for animation or modelling) and are also used to transform World Coordinates to View Coordinates Given the shape, transform all the points of the shape? Transform the points and/or vectors describing it. For example: Polygon: corner points Circle, Ellipse: center point(s), point at angle 0 Some transformations preserves some of the attributes like sizes, angles, ratios of the shape. Translation ● Simply move the object to a relative position. x' = x + t x P' y' = y + t y P t x x x' P = T = P '= y y' t y P '= P + T Rotation ● ● ● A rotation is defined by a rotation axis and a rotation angle. For 2D rotation, the parameters are rotation angle (θ) and the rotation point (xr,yr). We reposition the object in a circular path arround the rotation point (pivot point) θ yr xr Rotation ● When (xr,yr)=(0,0) we have P' x r cos( ) r cos cos r sin sin y r sin( ) r cos sin r sin cos The original coordinates are: x r cos y r sin Substituting them in the first equation we get: In the matrix form we have: where cos R sin sin cos x x cos y sin y x sin y cos P R P P Rotation ● Rotation around an arbitrary point (xr,yr) P' x xr ( x xr ) cos ( y yr ) sin y yr ( x xr ) sin ( y yr ) cos ● P (xr,yr) This equations can be written as matrix operations (we will see when we discuss homogeneous coordinates). Scaling ● Change the size of an object. Input: scaling factors (sx,sy) x' = xsx sx S= 0 y' = ys y P' P 0 s y P' = S P non-uniform vs. uniform scaling Homogenous Coordinates ● ● ● All transformations can be represented by matrix operations. Translation is additive, rotation and scaling is multiplicative (+ additive if you rotate around an arbitrary point or scale around a fixed point); making the operations complicated. Adding another dimension to transformations make translation also representable by multiplication. Cartesian coordinates vs homogenous coordinates. xh x= h yh y= h xh h x P = yh = h y h h ● ● Many points in homogenous coordinates can represent the same point in Cartesian coordinates. In homogenous coordinates, all transformations can be written as matrix multiplications. Transformations in Homogenous C. ● Translation 1 0 t x T t x ,t y = 0 1 t y 0 0 1 P' = T t x ,t y P ● Rotation cosθ sin θ 0 Rθ = sin θ cosθ 0 0 0 1 P' = Rθ P ● Scaling s x 0 0 S s x , s y = 0 s y 0 0 0 1 P = S s x , s y P Composite Transformations ● Application of a sequence of transformations to a point: P M 2 M1 P MP Composite Transformations ● ● First: composition of similar type transformations If we apply to successive translations to a point: P T(t2 x , t2 y ) {T(t1x , t1 y ) P} {T(t2 x , t2 y ) T(t1x , t1 y )} P 1 0 t2 x 1 0 t1x 1 0 t1x t2 x T t2 x ,t2 y T t1x ,t1 y = 0 1 t2 y 0 1 t1 y = 0 1 t1 y t2 y = T t1x + t2 x ,t1 y + t2 y 0 0 1 0 0 1 0 0 1 Composite Transformations cosθ sin θ R θ R φ = sin θ cosθ 0 0 cosθcosφ sin θsin φ cosθsin φ sin θcosφ sin θcosφ + cosθsin φ sin θsin φ + cosθcosφ 0 0 s2 x Ss2 x , s2 y Ss1x , s1 y = 0 0 0 s2 y 0 0 s1x 0 0 1 0 0 s1 y 0 0 cos 0 sin 1 0 sin cos 0 0 0 = 1 0 cos(θ + φ) sin( θ + φ) 0 0 = sin( θ + φ) cos(θ + φ) 0 = R θ + φ 1 0 0 1 0 s1x s2 x 0 = 0 1 0 0 s1 y s2 y 0 0 0 = Ss1x s2 x , s1 y s2 y 1 Rotation around a pivot point – Translate the object so that the pivot point moves to the origin – Rotate around origin – Translate the object so that the pivot point is back to its original position T xr , yr R θ T xr , yr = 1 0 xr cosθ sin θ 0 1 0 xr 0 1 y sin θ cosθ 0 0 1 y = r r 0 0 1 0 0 1 0 0 1 cosθ sin θ xr 1 cosθ + yr sin θ sin θ cosθ y 1 cosθ x sin θ r r 0 0 1 T xr , yr R Txr , yr Scaling with respect to a fixed point – Translate to origin – Scale – Translate back T x f , y f Tx f , y f Ss x , s y T x f , y f = 1 0 0 1 0 0 x f sx y f 0 1 0 sx 0 0 0 sy 0 0 sy 0 0 1 0 x f 0 0 1 y f = 1 0 0 1 x f 1 s x y f 1 s y 1 S s x , s y Tx f , y f Order of matrix compositions ● Matrix composition is not commutative. So, be careful when applying a sequence of transformations. pivot same pivot Other Transformations ● Reflection 1 0 0 0 1 0 0 0 1 1 0 0 0 1 0 0 0 1 1 0 0 0 1 0 0 0 1 ● Shear: Deform the shape like shifted slices. (0,1) (1,1) (2,1) (3,1) x' x sh x y 1 sh x 0 0 1 0 0 0 1 y' y Transformations Between the Coordinate Systems ● ● Between different systems: Polar coordinates to cartesian coordinates Between two cartesian coordinate systems. For example, relative coordinates or window to viewport transformation. ● How to transform from x,y to x',y' ? ● Superimpose x',y' to x,y ● Transformation: – – y y0 Translate so that (x0,y0) moves to (0,0) of x,y Rotate x' axis onto x axis x0 x y R θ T x0, y0 x ● Alternate method for rotation: Specify a vector V for positive y' axis: unit vecto r in the y' direction : V v= = v x ,v y V y V unit vecto r in the x' direction, rotate v clockwise 90 u = v y ,vx = u x ,u y x ● Elements of any rotation matrix can be expressed as elements of a set of orthogonal unit vectors: u x R = vx 0 uy vy 0 0 v y 0 = vx 1 0 vx vy 0 0 0 1 P P0 v= P P0 y P P0 x ● Example: P0 = 2,1 P = 3.5,3 1.5,2 = 1.5 , 2 = 0.6,0.8 P P0 v= = P P0 1.52 + 2 2 2.5 2.5 u = 0.8,0.6 M = R u , v T 2,1 = 0.8 0.6 0 1 0 2 = 0.8 0.6 1 0.6 0.8 0 0 1 1 0.6 0.8 2 0 0 1 0 0 1 0 0 1 3 4 4 Let triangle T be defined as 1 2 1 three column vectors: 1 1 1 M T = 0.8 1 1.6 0.6 2 1.2 1 1 1 y P MT T P0 x Affine Transformations ● Coordinate transformations of the form: x' = axx x + axy y + bx y' = a yx x + a yy y + by ● ● Translation, rotation, scaling, reflection, shear. Any affine transformation can be expressed as the combination of these. Rotation, translation, reflection: preserve angles, lengths, parallel lines 3 DIMENSIONAL TRANSFORMATIONS 3D Transformations ● ● ● z x,y,z coordinates. Usual notation: Right handed coordinate system Analogous to 2D we have 4 dimensions in homogenous coordinates. x Basic transformations: – Translation – Rotation – Scaling y y x z x z y Translation ● move the object to a relative position. x 1 y 0 z 0 1 0 0 0 tx x 1 0 t y y 0 1 tz z 0 0 1 1 y P P x z y P T P x z Rotation ● Rotation arround the coordinate axes y y x z x z x axis y x z y axis Counterclockwise when looking along the positive half towards origin z axis Rotation around coordinate axes ● ● ● Arround x Arround y Arround z 0 1 0 cos R x ( ) 0 sin 0 0 0 sin cos 0 R y ( ) sin 0 0 sin 1 0 cos sin R z ( ) 0 0 cos 0 0 cos 0 0 sin cos 0 0 0 0 0 1 0 0 0 1 0 0 0 0 1 0 0 1 P R x ( ) P P R y ( ) P P R z ( ) P Rotation Arround a Parallel Axis Rotating the object around a line parallel to one of the axes: Translate to axis, rotate, translate back. ● P T(0, y p , z p ) R x ( ) T(0, y p , z p ) P y y y y x x z x x z z z Translate Rotate Translate back Figure from the textbook Rotation Around an Arbitrary Axis ● ● y Translate the object so that the rotation axis passes though the origin Rotate the object so that the rotation axis is aligned with one of the coordinate axes ● Make the specified rotation ● Reverse the axis rotation ● Translate back x z Rotation Around an Arbitrary Axis Rotation Around an Arbitrary Axis V P2 P1 ( x2 x1 , y2 y1 , z2 z1 ) u is the unit vector along V: u V (a, b, c) V First step: Translate P1 to origin: 1 0 T 0 0 0 0 x1 1 0 y1 0 1 z1 0 0 1 Next step: Align u with the z axis we need two rotations: rotate around x axis to get u onto the xz plane, rotate around y axis to get u aligned with z axis. Rotation Around an Arbitrary Axis Align u with the z axis 1) rotate around x axis to get u into the xz plane, 2) rotate around y axis to get u aligned with the z axis y y y α α x x β z u u' u z uz u z x Dot product and Cross Product ● v dot u = vx * ux + vy * uy + vz * uz. That equals also to |v|*|u|*cos(a) if a is the angle between v and u vectors. Dot product is zero if vectors are perpendicular. v x u is a vector that is perpendicular to both vectors you multiply. Its length is |v|*|u|*sin(a), that is an area of parallelogram built on them. If v and u are parallel then the product is the null vector. Rotation Around an Arbitrary Axis Align u with the z axis 1) rotate around x axis to get u into the xz plane, 2) rotate around y axis to get u aligned with the z axis We need cosine and sine of α for rotation u u' α uz z u (0, b, c) Projection of u on yz plane x u u z c cos u u z d d b2 c 2 u u z u x u u z sin u xb b d sin cos c d sin b d 1 0 R x ( ) 0 0 0 c d b d 0 0 b d c d 0 0 0 0 1 Rotation Around an Arbitrary Axis Align u with the z axis 1) rotate around x axis to get u into the xz plane, 2) rotate around y axis to get u aligned with the z axis u u z cos d u u z β u u z u y u u z sin u y (a) cos d d 0 R y ( ) a 0 x sin a 0 a 0 1 0 0 0 d 0 0 0 1 u u''= (a,0,d) z R ( ) T( x1 , y1 , z1 ) R x ( ) R y ( ) R z ( ) R y ( ) R x ( ) T( x1 , y1 , z1 ) Rotation, ... Alternative Method Any rotation around origin can be represented by 3 orthogonal unit vectors: r11 r12 r r22 21 R r31 r32 0 0 r13 0 r23 0 r33 0 0 1 This matrix can be thought of as rotating the unit r1*, r2*, and r3* vectors onto x, y, and z axes. So, to align a given rotation axis u onto the z axis, we can define an (orthogonal) coordinate system and form this R matrix Define a new coordinate system (ux , uy , uz ) with the given rotation axis u using: uz u uy u ux u ux ux uy uz Rotation, ... Alternative Method uz u (a, b, c) u u x (a, b, c) (1,0,0) (0, c,b) (0, c / d ,b / d ) 2 2 u ux u ux b c ux uy uz (0, c / d ,b / d ) (a, b, c) (d ,a b / d ,a c / d ) uy d R 0 a 0 a b d c d b ac d b d c 0 0 0 0 0 1 Check if this is equal to R y ( ) Rx ( ) Scaling ● Change the coordinates of the object by scaling factors. y P sx 0 0 0 P x' x x 0 sy 0 0 y y' z' 0 0 sz 0 z z 1 0 0 0 1 1 y P' SP P S P x z Scaling with respect to a Fixed Point Translate to origin, scale, translate back ● P T( x f , y f , z f ) S T( x f , y f , z f ) P y x y x z y y x x z z z Translate Scale Translate back Scaling with respect to a Fixed Point 1 0 0 1 T( x f , y f , z f ) S T( x f , y f , z f ) 0 0 0 0 1 0 0 x f s x 0 0 s x x f 0 1 0 y 0 s 0 s y f y y f 0 0 1 z f 0 0 s z s z z f 0 0 0 1 0 0 0 1 sx 0 T( x f , y f , z f ) S T( x f , y f , z f ) 0 0 1 0 x f sx y f 0 zf 0 1 0 0 sy 0 0 0 0 sz 0 0 0 0 sy 0 0 0 0 sz 0 x f (1 s x ) y f (1 s y ) z f (1 s z ) 1 0 1 0 0 0 0 1 0 0 0 1 0 0 1 0 0 xf y f zf 1 Reflection Reflection over planes, lines or points ● y y y x z 1 0 0 0 0 0 1 0 0 0 0 1 x x z z 0 1 0 0 y 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 1 0 0 0 x z 0 1 0 0 0 0 1 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 Shear ● Deform the shape depending on another dimension SH z 1 0 0 0 0 1 0 0 a b 1 0 0 0 0 1 x and y value depends on z value of the shape SH x 1 a b 0 0 1 0 0 0 0 1 0 0 0 0 1 y and z value depends on x value of the shape