Affine transformations Brian Curless CSE 557 Fall 2014 1
Reading Required: Shirle, Sec. 2.4, 2.7 Shirle, Ch. 5.1-5.3 Shirle, Ch. 6 Further reading: Fole, et al, Chapter 5.1-5.5. David F. Rogers and J. Alan Adams, Mathematical Elements for Computer Graphics, 2 nd Ed., McGraw- Hill, New York, 1990, Chapter 2. 2
Geometric transformations Geometric transformations will map points in one space to points in another: (',, z ) = f (,, z). These transformations can be ver simple, such as scaling each coordinate, or comple, such as nonlinear twists and bends. We'll focus on transformations that can be represented easil with matri operations. 3
Vector representation We can represent a point, p = (,), in the plane or p=(,,z) in 3D space as column vectors z as row vectors z 4
Canonical aes 5
Vector length and dot products 6
Vector cross products 7
Representation, cont. We can represent a 2-D transformation M b a matri a c b d If p is a column vector, M goes on the left: p' Mp ' a b ' c d If p is a row vector, M T goes on the right: p' pm ' ' We will use column vectors. T a c b d 8
Two-dimensional transformations Here's all ou get with a 2 2 transformation matri M: So: ' a b ' c d ' a b ' c d We will develop some intimac with the elements a, b, c, d 9
Identit Suppose we choose a=d=1, b=c=0: Gives the identit matri: 1 0 0 1 Doesn't move the points at all 10
Scaling Suppose we set b=c=0, but let a and d take on an positive value: Gives a scaling matri: a 0 0 d Provides differential (non-uniform) scaling in and : ' a ' d 2 2 2 0 0 2 1 1 1 2 1 2 2 12 0 0 2 1 1 2 11
Suppose we keep b=c=0, but let either a or d go negative. Eamples: 1 0 0 1 1 0 0 1 12
Now let's leave a=d=1 and eperiment with b.... The matri gives: 1 b 0 1 ' b ' 1 1 1 1 0 1 1 1 13
Effect on unit square Let's see how a general 2 2 transformation M affects the unit square: a c b d p q r s p' q' r' s' a b0 1 1 0 0 a a b b c d 0 0 1 1 0 c c d d 1 s r p 1 q 14
Effect on unit square, cont. Observe: Origin invariant under M M can be determined just b knowing how the corners (1,0) and (0,1) are mapped a and d give -and -scaling b and c give -and -shearing 15
Rotation From our observations of the effect on the unit square, it should be eas to write down a matri for rotation about the origin : 1 1 1 0 0 1 Thus, MR( ) 16
Degrees of freedom For an transformation, we can count its degrees of freedom the number of independent (though not necessaril unique) parameters needed to specif the transformation. One wa to count them is to add up all the apparentl free variables and subtract the number of equations that constrain them. How man degrees of freedom does an arbitrar 2X2 transformation have? How man degrees of freedom does a 2D rotation have? 17
Linear transformations The unit square observations also tell us the 22 matri transformation implies that we are representing a point in a new coordinate sstem: p' Mp a b c d u v u v where u=[a c] T and v=[b d] T are vectors that define a new basis for a linear space. The transformation to this new basis (a.k.a., change of basis) is a linear transformation. 18
Limitations of the 2 2 matri A 2 2 linear transformation matri allows Scaling Rotation Reflection Shearing Q: What important operation does that leave out? 19
Affine transformations In order to incorporate the idea that both the basis and the origin can change, we augment the linear space u, v with an origin t. We call u, v, and t (basis and origin) a frame for an affine space. Then, we can represent a change of frame as: p' u v t This change of frame is also known as an affine transformation. How do we write an affine transformation with matrices? 20
Homogeneous coordinates Idea is to loft the problem up into 3-space, adding a third component to ever point: 1 Adding the third w component puts us in homogenous coordinates. And then transform with a 3 3 matri: ' 1 0 t ' T( t) 0 1 t w ' 1 0 0 11 1 1 1 0 1 0 1 1 2 0 0 1 1 1... gives translation! 21
Anatom of an affine matri In matri form, 2D affine transformations alwas look like this: a b t A t M c d t 0 0 1 0 0 1 2D affine transformations alwas have a bottom row of [0 0 1]. An affine point is a linear point with an added w-coordinate which is alwas 1: p aff plin 1 1 Appling an affine transformation gives another affine point: Mp Ap lin aff 1 t 22
Rotation about arbitrar points Until now, we have onl considered rotation about the origin. With homogeneous coordinates, ou can specif a rotation, q, about an point q = [q q ] T with a matri: q θ 1. Translate q to origin 2. Rotate 3. Translate back Note: Transformation order is important!! 23
Points and vectors Vectors have an additional coordinate of w=0. Thus, a change of origin has no effect on vectors. Q: What happens if we multipl a vector b an affine matri? These representations reflect some of the rules of affine operations on points and vectors: vector + vector scalar vector point - point point + vector point + point One useful combination of affine operations is: p() t po tu Q: What does this describe? 24
Barcentric coordinates A set of points can be used to create an affine frame. Consider a triangle ABC and a point P: We can form a frame with an origin C and the vectors from C to the other vertices: u A C vbc tc We can then write P in this coordinate frame: P u vt The coordinates (a, b, g) are called the barcentric coordinates of P relative to A, B, and C. 25
Computing barcentric coordinates Starting from a triangle in the - plane we can compute the barcentric coordinates of P: A B C P A BC A B C P 1 1 1 1 Simple matri analsis gives the solution: P B C A P C A B P P B C A P C A B P 1 1 1 1 1 1 1 1 1 A B C A B C A B C A B C A B C A B C 1 1 1 1 1 1 1 1 1 Computing the determinant of the denominator gives: BC BC AC AC AB AB 26
Cross products in 2D Recall the 3D cross product: ˆ ˆ zˆ uv u u u ( uv uv ) ˆ ( uv uv ) ˆ ( uv uv ) zˆ uv uv uv z v v v z z z z z uv z z uv z z uv What happens when u and v lie in the - plane? 27
Barcentric coords from area ratios Now, let s rearrange the equation from two slides ago: BC BC AC AC AB AB The determinant is then just the z-component of (B-A) (C-A), which is two times the area of triangle ABC! Thus, we find: ( B A )( C A ) ( B A )( C A ) SArea( PBC) SArea( APC) SArea( ABP) SArea( ABC) SArea( ABC) SArea( ABC) Where SArea(D) is the signed area of a triangle, which can be computed with cross-products. What does it mean for a barcentric coordinate to be negative? 28
Affine, vector, and conve combinations Note that we seem to have constructed a point b adding points together, which we said was illegal, but as long as the have coefficients that sum to one, it s ok. More generall: P A A 1 1 n n is an affine combination if: It is a vector combination if: i1 0 And it is a conve combination if: n i1 Q: Wh is it called a conve combination? n 1 i n i1 1 i i and 0 i 29
Basic 3-D transformations: scaling Some of the 3-D transformations are just like the 2-D ones. For eample, scaling: ' s 0 0 0 ' 0 s 0 0 z ' 0 0 sz 0z 1 0 0 0 11 z z 30
Translation in 3D ' 1 0 0 t ' 0 1 0 t z ' 0 0 1 tz z 1 0 0 0 11 z z 31
Rotation in 3D How man degrees of freedom are there in an arbitrar 3D rotation? 32
Rotation in 3D (cont d) These are the rotations about the canonical aes: 1 0 0 0 0 cos sin 0 R ( ) 0 sin cos 0 0 0 0 1 cos 0 sin 0 0 1 0 0 R ( ) sin 0 cos 0 0 0 0 1 cos sin 0 0 sin cos 0 0 R z ( ) 0 0 1 0 0 0 0 1 R R R z Use right hand rule A general rotation can be specified in terms of a product of these three matrices. How else might ou specif a rotation? 33
Shearing in 3D Shearing is also more complicated. Here is one eample: ' 1 b 0 0 ' 0 1 0 0 z ' 0 0 1 0z 1 0 0 0 11 z z We call this a shear with respect to the -z plane. 34
Preservation of affine combinations A transformation F is an affine transformation if it preserves affine combinations: F( A A ) F( A ) F( A ) 1 1 n n 1 1 n n where the A i are points, and: Clearl, the matri form of F has this propert. One special eample is a matri that drops a dimension. For eample: This transformation, known as an orthographic projection is an affine transformation. We ll use this fact later n i1 1 1 0 0 0 0 1 0 0 z 0 0 0 1 1 1 i 35
Properties of affine transformations Here are some useful properties of affine transformations: Lines map to lines Parallel lines remain parallel Midpoints map to midpoints (in fact, ratios are alwas preserved) s p : t q r s p' q' : t r' pq s ratio qr t p'q' q'r' 36
Affine transformations in OpenGL OpenGL maintains a modelview matri that holds the current transformation M. The modelview matri is applied to points (usuall vertices of polgons) before drawing. It is modified b commands including: glloadidentit() set M to identit gltranslatef(t, t, t z ) translate b (t, t, t z ) M I M MT glrotatef(θ,,, z) rotate b angle θ about ais (,, z) M MR glscalef(s, s, s z ) scale b (s, s, s z ) M MS Note that OpenGL adds transformations b postmultiplication of the modelview matri. 37