Vector and Affine Math Computer Science Department The Universit of Teas at Austin
Vectors A vector is a direction and a magnitude Does NOT include a point of reference Usuall thought of as an arrow in space Vectors can be added together and multiplied b scalars Zero vector has no length or direction
Vector Spaces Set of vectors Closed under the following operations Vector addition: v + v 2 = v 3 Scalar multiplication: s v = v 2 Linear combinations: Scalars come from some field F e.g. real or comple numbers Linear independence Basis Dimension n i= a i v = v i
Vector Space Aioms Vector addition is associative and commutative Vector addition has a unique) identit element the 0 vector) Each vector has an additive inverse So we can define vector subtraction as adding an inverse Scalar multiplication has an identit element ) Scalar multiplication distributes over vector addition and field addition Multiplications are compatible abv)=ab)v)
Coordinate Representation Pick a basis, order the vectors in it, then all vectors in the space can be represented as sequences of coordinates, i.e. coefficients of the basis vectors, in order. Eample: Cartesian 3-space Basis: [i j k] Linear combination: i + j + zk Coordinate representation: [ z] a [ z] + b[ 2 2 z2] = [ a + b2 a + b2 az + bz2]
Row and Column Vectors We can represent a vector, v =,), in the plane as a column vector as a row vector [ ]
Linear Transformations Given vector spaces V and W A function f :V W is a linear map or linear transformation if f a v +... + a m v m ) = a f v )+... + a m f v m )
Transformation Representation We can represent a 2-D transformation M b a matri M = a b c d If v is a column vector, M goes on the left: If v is a row vector, M T goes on the right: We will use column vectors.!!!! v = Mv = a b c d v = vm T = a b c d
Two-dimensional transformations Heres all ou get with a 2 2 transformation matri M: = a b c d So: = a + b = c + d We will develop some intimac with the elements a, b, c, d
Identit Suppose we choose a=d=, b=c=0: Gives the identit matri: 0 0 Doesnt change anthing
Scaling Suppose b=c=0, but let a and d take on an positive value: Gives a scaling matri: a 0 0 d 2 2 Provides differential non-uniform) scaling in and : 2 2 2 0 0 2 = a = d 2 2 2 0 0 2
Reflection Suppose b=c=0, but let either a or d go negative. Eamples: 0 0 0 0
Shear Now leave a=d= and eperiment with b The matri b 0 gives: = + b = 0
Effect on unit square Lets see how a general 2 2 transformation M affects the unit square: a c b p q r s d [ ] = [ p q r s ] a b 0 0 = 0 a a + b b c d 0 0 0 c c + d d s r p q
Effect on unit square, cont. Observe: Origin invariant under M M can be determined just b knowing how the corners,0) and 0,) are mapped a and d give - and -scaling b and c give - and -shearing
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 : cosθ) 0 sinθ) 0 sinθ) cosθ) Thus M R = Rθ) = cosθ) sinθ) sinθ) ) cosθ)
Linear transformations The unit square observations also tell us the 22 matri transformation implies that we are representing a vector in a new coordinate sstem: v = Mv! = a b c d =! u w where u=[a c] T and w=[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.!! = u + w
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?
Points A point is a location in space Cannot be added or multiplied together Subtract two points to get the vector between them Points are not vectors
Affine transformations In order to incorporate the idea that both the basis and the origin can change, we augment the linear space u, w with an origin t. Note that while u and w are basis vectors, the origin t is a point. We call u, w, and t basis and origin) a frame for an affine space. Then, we can represent a change of frame as: p! = u + w + t This change of frame is also known as an affine transformation. How do we write an affine transformation with matrices?
Basic Vector Arithmetic! u = r s t! v = z! u + v = r + s + t + z! av = a a az v = 2 + 2 + z 2 normv) = v v
Parametric line segment Or line, or ra, or just linear interpolation p = p 0 + tp p 0 ) = t)p 0 + tp! z! = 0 0 z 0! + t 0 0 z z 0! = t) 0 + t t) 0 + t t)z 0 + tz Line segment Ra Line 0 t 0 t t
Vector dot product u v = r + s + tz = u v cosφ)
Projection Projection u component parallel to v) w = u v v v v Rejection u component orthogonal to v) u w Particularl useful when vectors are normalized
Cross product w = u v = i j k r s t z w is orthogonal to u and v w = u v sinφ) w area of parallelogram use right-hand rule u v = v u) = u v) w u v w) sz t t rz r s
Determinants a b c d = ad bc a b c d e f g h i = a e f h i b d f g i + c d e g h = aei afh + bfg bdi + cdh ceg detm T ) = detm) detab) = deta)detb) if detm) = 0, M is singular, has no inverse
Plane equation Given normal vector N orthogonal to the plane and an point p in the plane N p + d = 0! a b c For a triangle! N = normv 2 v 0 ) v v 0 )) Order matters, usuall CCW z + d = a + b + cz + d = 0
Homogeneous Coordinates To represent transformations among affine frames, we can loft the problem up into 3-space, adding a third component to ever point: p! = Mp = a b t c d t 0 0 = u w t = u + w + t Note that [a c 0] T and [b d 0] T represent vectors and [t t ] T, [ ] T and [ ] T represent points.
Homogeneous coordinates This allows us to perform translation as well as the linear transformations as a matri operation: p = M T p 0 t = 0 t 0 0 = + t = + t 0 0 2 0 0
Rotation about arbitrar points Until now, we have onl considered rotation about the origin. With homogeneous coordinates, ou can specif a rotation, R q, about an point q = [q q ] T with a matri: q θ. Translate q to origin 2. Rotate 3. Translate back Line up the matrices for these step in right to left order and multipl. Note: Transformation order is important!!
Points and vectors From now on, we can represent points as have an additional coordinate of w=. 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 matri b a vector? These representations reflect some of the rules of affine operations on points and vectors: vector + vector vector vector point point point + vector point + point One useful combination of affine operations is: pt) = p 0 + tv Q: What does this describe?
Barcentric coordinates A set of points can be used to create an affine frame. Consider a triangle ABC and a point p: A p C B We can form a frame with an origin C and the vectors from C to the other vertices: u = A C v = B C t = C We can then write P in this coordinate frame p = αu + βv + t The coordinates α, β, γ) are called the barcentric coordinates of p relative to A, B, and C.
Computing barcentric coordinates For the triangle eample we can compute the barcentric coordinates of P: α Cramer s rule gives the solution: A B C p * * * α A + βb + γc = A B C * β * = p * )* γ )* )* p B C A p C A B p α = p B C A B C β = A p C A B C γ = A B p A B C A B C A B C A B C Computing the determinant of the denominator gives: B C B C + A C A C + A B A B
Cross products Consider the cross-product of two vectors, u and v. What is the geometric interpretation of this cross-product? A cross-product can be computed as: u v = i j k u u u z v v v z = u v z u z v )i + u z v u v z )j + u v u v )k u v z u z v ) = u z v u v z ) u v u v ) What happens when u and v lie in the - plane? What is the area of the triangle the span?
Barcentric coords from area ratios Now, let s rearrange the equation from two slides ago: B C B C + A C A C + A B A B = B A )C A ) B A )C A ) 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: α = SAreapBC) SAreaABC) β = SAreaApC) SAreaABC) γ = SAreaABp) SAreaABC) Where SAreaRST) is the signed area of a triangle, which can be computed with cross-products.
Affine and conve combinations Note that we seem to have added points together, which we said was illegal, but as long as the have coefficients that sum to one, it s ok. We call this an affine combination. More generall is a proper affine combination if: n i= α = i p = α p + + α n p n Note that if the α i s are all positive, the result is more specificall called a conve combination. Q: Wh is it called a conve combination?
Basic 3-D transformations: scaling Some of the 3-D transformations are just like the 2-D ones. For eample, scaling: z z z = s 0 0 0 0 s 0 0 0 0 s z 0 0 0 0 z
Translation in 3D z z z = 0 0 t 0 0 t 0 0 t z 0 0 0 z
Rotation in 3D Rotation now has more possibilities in 3D: 0 0 0 ) 0 cosθ) sinθ) 0 R θ) = ) 0 sinθ) cosθ) 0) ) 0 0 0 cosθ) 0 sinθ) 0 ) 0 0 0 R θ) = ) sinθ) 0 cosθ) 0) ) 0 0 0 cosθ) sinθ) 0 0 ) sinθ) cosθ) 0 0 R z θ) = ) 0 0 0) ) 0 0 0 z R z R R Use right hand rule
Shearing in 3D Shearing is also more complicated. Here is one eample: We call this a shear with respect to the -z plane. z z z = b 0 0 0 0 0 0 0 0 0 0 0 z
Preservation of affine combinations A transformation F is an affine transformation if it preserves affine combinations: where the p i are points, and: Fα p + + α n p n ) = α Fp ) + + α n Fp n ) i= = 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 α 0 0 0 = 0 0 0 z 0 0 0 i
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! ratio = pq qr = s t = p q q r
Net Lecture More Math and Transforms
Programming tips 3D graphics, whether OpenGL or Direct3D or an other API, can be frustrating You write a bunch of code and the result is Nothing but black window; where did our rendering go??
Things to Tr Set our clear color to something other than black! It is eas to draw things black accidentall so don t make black the clear color But black is the initial clear color Did ou draw something for one frame, but the net frame draws nothing? Are ou using depth buffering? Did ou forget to clear the depth buffer? Remember there are near and far clip planes so clipping in Z, not just X Y Have ou checked for glgeterror? Call glgeterror once per frame while debugging so ou can see errors that occur For release code, take out the glgeterror calls Not sure what state ou are in? Use glgetintegerv or glgetfloatv or other quer functions to make sure that OpenGL s state is what ou think it is Use glutswapbuffers to flush our rendering and show to the visible window Likewise glfinish makes sure all pending commands have finished Tr reading http://www.slideshare.net/mark_kilgard/avoiding-9-common-opengl-pitfalls This is well worth the time wasted debugging a problem that could be avoided
Thanks Material for these slides provided b Christian Miller