Computer Graphics P4 Transformations Aleksandra Pizurica Ghent Universit Telecommunications and Information Processing Image Processing and Interpretation Group
Transformations in computer graphics Goal: introduce methodolog to Change coordinate sstem Move and deform objects Principle: transformations are applied to object vertices In 2D, point P(X,Y) is transformed to P (X,Y ); in 3D, P(X,Y,Z) P (X,Y,Z ) P4_2
Overview 2D transformations 3D transformations Quaternions Transformations in OpenGL P4_3
2D transformations
2D affine transformations Translate Shear Rotate Scale P4_5
Basic classes of geometric transformations General linear (preserve lines) Affine (preserve paralelism) Arbitrar shearing General scaling Conformal (preserve angles) Uniform scaling Rigid (preserve lengths) Translation Rotation P4_6
Elementar 2D transformations Translation T(T,T ) X X + T X Y Y + T Scaling S(S,S ) X X * S X Y Y * S Y Rotation R(θ) X X * cosθ Y*sinθ Y X * sinθ + Y*cosθ P4_7
Matri representation P4_8 Suppose we represent a 2D transformation b a matri d c b a d c b a + + transformed point transformation matri original point l k j i h g f e d c b a Wh is this useful? -A sequence of transformations matri multiplication Can we do it for an affine transform?
22 Matrices Which transformations can be represented b 22 matrices? -Lets look at some eamples: P4_9 S S 2D scale around (,) S S 2D identit 2D mirror over (,) - - 2D mirror over Y ais -
22 Matrices Which transformations can be represented b 22 matrices? -Lets look at some eamples: 2D rotation around (,) * cosθ *sinθ * sinθ+ *cosθ cosθ sinθ sinθ cosθ 2D Shear + H X * + H Y * H H P4_
22 Matrices Which transformations can be represented b 22 matrices? -Can we represent translation b a 22 matri? 2D Translation + T + T NO 22 matri! Onl linear2d transformations can be represented b a 22 matri Linear transformations satisf: T(s P +s 2 P 2 )s T (P )+s 2 T(P 2 ) are combinations of scale, rotation, share and mirror P4_
Matri representation for affine 2D transforms We want a representation where 2D translation is also represented b a matri (so that we can easil combine different transformations b multiplication) 2D matri representation of translation does not eist! What do we do? Solution: use a 33 matri 2D Translation + T + T T T Homogeneous coordinates P4_2
Homogeneous coordinates An equivalent formulation: adding a 3 rd coordinate to 2D point such that (, ) (W, W, W) (/W, /W) are Cartesian coordinates of the homogeneous point (,, W) (,, ) represents a point at infinit (,, ) not allowed 3 (2,3,) or (4,6,2) or (6,9,3) or W 2 (3,,) or (6,2,2) or 2 3 4 5 Homogeneous point is a line in 3D space P4_3
Homogeneous coordinates Even points infinitel far have a representation in homogeneous coordinates Points at infinit have their last coordinate equal to zero Eamples: P (,,); P 2 (,,); P 3 (,,), P 4 (2,,) 4 3 2 P P 2 P 4 P 3 2 3 4 5 P4_4
Matri form of elementar 2D transformations P4_5 T T Translation T(T,T ) S S Scaling S(S,S ) cos sin sin cos θ θ θ θ Rotation R(θ)
2D Shear transformation P4_6 H H General shear operation SH (H,H) Shear in the X-direction: H a SH b SH Shear in the Y-direction: H Shear can be represented as a combination of rotations and nonuniform scaling operations.
Compositions of 2D transformations Translate Rotate Eample: A comple transformation as a sequence of elementar transformations P4_7
Compositions of 2D transformations Inverse elementar transforms T - (T X, T Y ) T (-T X, -T Y ) S - (S X, S Y ) S (/S X, /S Y ) R - (θ) R (-θ) Comple transformations can be described as a combination (composition) of elementar transformations (X, Y,) T S (-) R (-) T (-) R (-) S (-) (X, Y, ) T Each rotation brings an etra parameter, scaling and translations two etra parameters Can this be simplified? P4_8
Compositions of 2D transformations We tr to simplif such epressions b Combining transformations, if possible Changing the order of transformations in order to make the combinations possible Sequential elementar transforms of the same tpe absorb each other T (T X, T Y ) T (T X2, T Y2 ) T (T X +T X2, T Y +T Y2 ) S (S X, S Y ) S (S X2, S Y2 ) S (S X.S X2, S Y.S Y2 ) R (θ ) R (θ 2 ) R (θ +θ 2 ) Changing transformation order is not alwas possible! Transformations that commute are onl Two elementar transformations of the same tpe Scaling with S S and Rotation P4_9
Compositions of 2D transformations Translation and rotation pseudo-commute T (T X,T Y ) R (θ) R (θ) T (T X2,T Y2 ) T (T X2,T Y2 ) R (θ) R (θ) T (T X,T Y ) P4_2
Compositions of 2D transformations Translation and scaling pseudo-commute S (S X, S Y ) T (T X,T Y ) T (T X2,T Y2 ) S (S X, S Y ) T(T X,T Y ) S(.5,3.) S (.5,3.) T (T X2,T Y2 ) No similar propert for rotation and scaling P4_2
Compositions of 2D transformations Hpothesis: Most general affine transform can alwas be represented as R(θ ) S (S X, S Y ) R(θ 2 ) T(T X,T Y ) This means: a unit square in the centre is reshaped to an arbitrar parallelogram, brought to an arbitrar position and rotated b an arbitrar angle Prove the hpothesis formall X ax + by + c Y dx + ey + f Set ct, ft Determine the parameters θ, θ 2, S X, S Y as a function of a, b, d, e P4_22
Efficienc: matri calculations The most general affine transformation P4_23 f e d c b a Multiplication of a 33 matri with column vector requires 9 multiplies and 6 additions Actuall we need onl 4 multiplies and 4 additions X ax + by + c Y dx + ey + f Even though matri representation is useful, practical application should make use of the special structure of the matri, for efficienc
Efficienc: creating successive views To produce an impression of a dnamicall rotating object, man successive views are needed *cosθ *sinθ *sinθ+ *cosθ The angle difference between the successive views is ver small (a few degrees). Can we simplif the calculation? Solution : Use approimation cosθ : *sinθ *sinθ+ What is wrong with this solution? A better solution *sinθ *sinθ+ Check the determinant of the matri in both cases! P4_24
3D transformations
3-D Transformations Generalization of 2D transformations Same principle: appl to vertices (which are now 3D points) P(X,Y,Z) is transformed to P (X,Y,Z ) We consider general 3D affine transformation X ax + by + cz + d Y ex + fy + gz + h Z ix + jy + kz + l Properties analogous to 2D case Lines map to lines (plane segments map to plane segments) Parallelism preserved A unit cube centered in the origin is transformed into an arbitrar parallelepiped, arbitraril positioned in space P4_26
3D transformations Convention that we will adopt: Right-hand side sstem z z z P4_27
3D rotation 3D rotation of a rigid object is described b three parameters, such as three angles of Euler In Euler angle formulation an arbitrar rotation is represented as a composition of three elemental rotations, each around a single coordinate ais http://en.wikipedia.org/wiki/euler_angles P4_28
3D rotation The rotations around, and z ais are also known as Roll, Pitch and Yaw ( heading ); terms coming from flight dnamics Pitch Yaw Longitudinal Roll Vertica Lateral P4_29
3D transformations Three elementar transforms Elementar translation Three elementar rotations Around the X-ais, Y-ais and Z-ais Elementar scaling In X-, Y-, en Z-direction Matri representation 4 4 matrices for 3-D, analogous to 3 3 matrices for 2-D using homogeneous coordinates W W ; Wz z W usual notation W : z z Each point in 3D space a line throught the origin in 4D space P4_3
3D Translation and Scaling Translation P4_3 T (T X, T Y, T Z ) z T T T z z X X + T X Y Y + T Y Z Z + T Z Elementar scaling S (S X, S Y, S Z ) z S S S z Y Y X X S X X Y S Y Y Z S Z Z
3D Rotation Rotation around -ais P4_32 cos sin sin cos θ θ θ θ R X (θ) R Y (ϕ) R Z (ψ) cos sin sin cos ϕ ϕ ϕ ϕ cos sin sin cos ψ ψ ψ ψ X X Y Y cos(θ) -Z sin(θ) Z Y sin(θ) + Z cos(θ Similarl for rotations around other two aes:
3D Shear Shear around the Z-ais X X + H X Z Y Y + H Y Z Z Z Can be represented in matri form as P4_33 z H H z Shear around the X-ais and Y-ais have a similar form
3D Transformations: properties Inverse transforms are defined as T - (T X, T Y, T Z ) T (-T X, -T Y, -T Z ) S - (S X, S Y, S Z ) S (/S X, /S Y, /S Z ) R X - (θ) R X (-θ); R Y - (φ) R Y (-φ); R Z - (ψ) R X (-ψ) The same absorption and pseudo-commutation are valid like in the 2D case Translation pseudo-commutes with scaling and with three rotations Scaling and rotations do not pseudo-commute Additional properties, specific for 3D (without proof) Two (different) rotations do not pseudo-commute (not even with an additional translation) Three (different) rotations and a translation do pseudo commute P4_34
3D Transformations: properties z. A general positioning transformation involves 6 parameters T (T X, T Y, T Z ) R X (θ) R Y (φ) R Z (ψ) Object deformation: 6 other parameters (scaling + rotation) A general affine transformation involves thus 2 parameters R X (θ) R Y (φ) R Z (ψ) S (S X,S Y,S Z ) R X (θ ) R Y (φ ) R Z (ψ ) T (T X,T Y,T Z ) There are man equivalent forms P4_35
Arbitrar displacement in 3D C B See eercises A C z Initial position z B A Desired position z C A () B B z C A (2) A z B C (3) C z B A (4) P4_36
Compositions of 3D transformations Rotation about arbitrar point: in this eample about z-ais From the book of Edward Angel: Interactive Computer Graphics A Top-Down Approach Using OpenGL P4_37
Quaternions and 3D rotation
Comple numbers and rotation Remember polar representation of a comple number c a + ib re iθ 2 r a + b 2, θ arctan( b / a) Denotebc the resultof rotatingc aboutthe originb φ: c re iθ iφ i( θ + φ) e re Quaternions (Sir William Rowan Hamilton, 843) are etensions of comple numbers in 3D, ielding elegant rotations in 3D a ( q, q); q qi + q2j + q3k i 2 2 2 j k ijk P4_39
Comple numbers and rotation A point in space p (, p) Rotationof the point p b θaboutthe vector v: where: θ θ r cos, sin v, 2 2 p rpr θ cos, sin 2 θ v 2 Whare quaternionsinterestingfor3drotationsin computer graphics? Euler angle representation suffers from Gimbal lock Simpler representation Lower computational compleit, but not necessaril after conversion to matri form r P4_4
Gimbal lock When the aes of two of the three gimbals align, "locking" into rotation in a degenerate 2D space. Eample: when the pitch (green) and aw (magenta) gimbals become aligned, changes to roll (blue) and aw appl the same rotation to the airplane. P4_4
Transformations in OpenGL
OpenGL function format function name dimensions glverte3f(,,z) belongs to GL librar,,z are floats glverte3fv(p) pis a pointer to an arra Angel: Interactive Computer Graphics 4E Addison-Wesle 25 P4_43
Transformations in OpenGL We canloadmatri withthe function glloadmatri(pointer_to_matri) Orset a matri to identitmatri withthe function glloadidentit() Rotation, translation and scaling are provided through glrotatef(angle,v,v,vz) gltranslatef(d,d,dz) glscalef(s,s,sz) P4_44
Summar 2D transformations arbitrar 2D affine transformation 6 parameters comple transformations as compositions of the elementar ones 3D transformations arbitrar 3D affine transformation 2 parameters comple transformations as compositions of the elementar ones Euler angles Quaternions advantage over Euler angle representation (Gimbal lock avoided) Transformations in OpenGL P4_45