Geometry (Many slides adapted from Octavia Camps and Amitabh Varshney) Goals • Represent points, lines and triangles as column vectors. • Represent motion as matrices. • Move geometric objects with matrix multiplication. • Refresh memory about geometry and linear algebra Vectors v ( x1 , x2 , , xn ) • Ordered set of numbers: (1,2,3,4) v • Example: (x,y,z) coordinates of pt in If space. n 2 i 1 i x v 1, v is a unit vecto r Vector Addition v w ( x1 , x2 ) ( y1 , y2 ) ( x1 y1 , x2 y2 ) v V+w w Scalar Product av a( x1 , x2 ) (ax1 , ax2 ) av v Inner (dot) Product v w v.w ( x1 , x2 ).( y1 , y2 ) x1 y1 x2 . y2 The inner product is a SCALAR! v.w ( x1 , x2 ).( y1 , y2 ) || v || || w || cos v.w 0 v w Points Using these facts, we can represent points. Note: (x,y,z) = x(1,0,0) + y(0,1,0) + z(0,0,1) x = (x,y,z).(1,0,0) z = (x,y,z).(0,0,1) y = (x,y,z).(0,1,0) Lines • Line: y = mx + a `d • Line: sum of a point and a vector P = P1 + `d (where`d is a column vector) P1 P2 • Line: Affine sum of two points P = 1P1 + 2P2, where 1 + 2 = 1 P1 Line Segment: For 0 1, 2 1, P lies between P1 and P2 • Line: set of points equidistant from the origin in the direction of a unit vector. Plane and Triangle • Plane: sum of a point and two vectors P = P1 + `u + b` v Plane: set of points equidistant from origin in direction of a vector. `v P1 `u P3 • Triangle: Affine sum of three points with i 0 P = 1P1 + 2P2 + 3P3, where 1 + 2+ 3 = 1 P lies between P1, P2, P3 P1 P2 Generalizing ... Affine Sum of arbitrary number of points: Convex Hull P = 1P1 + 2P2 + … + nPn, where 1 + 2 + … + n = 1 and i 0 Normal of a Plane Plane: sum of a point and two vectors P = P1 + `u + b`v P - P1 = `u + b`v `v P1 `u If`n is orthogonal to`u and`v (n =`u `v ) : T T T `n P - P1) = `n `u + b`n `v = 0`n `v P1 `u Implicit Equation of a Plane T `n P - P1) = 0 a x x1 Let`n = b P= y P1 = y1 c z z1 Then, the equation of a plane becomes: a (x - x1) + b (y - y1) + c (z - z1) = 0 `v P1 `u `n `v ax+by+cz+d=0 P1 `u Thus, the coefficients of x, y, z in a plane equation define the normal. Normal of a Triangle Normal of the plane containing the triangle (P1, P2 , P3 `n ): `n = (P2 - P1) (P3 - P1) P1 P3 P2 P2 Normal pointing towards you P1 P3 Normal pointing away from you P2 P1 P3 • Models constructed with consistent ordering of triangle vertices : - all clockwise or all counter-clockwise. • Usually normals point out of the model. Normal of a Vertex in a Mesh `nv = (`n1 +`n2 + … +`nk) / k = `ni / k = average of adjacent triangle normals `nv `nk or better: `nv = Ai `ni) / (k Ai) ) = area-weighted average of adjacent triangle normals `n3 `n1 `n2 Geometry Continued • Much of last classes’ material in Appendix A Matrices Anm a11 a12 a 21 a22 a31 a32 an1 an 2 a1m a2 m a3m anm Sum: Cnm Anm Bnm cij aij bij A and B must have the same dimensions Matrices Product: Cn p Anm Bm p m cij aik bkj k 1 Identity Matrix: A and B must have compatible dimensions Ann Bnn Bnn Ann 1 0 0 0 1 0 I IA AI A 0 0 1 Matrices • Associative T*(U*(V*p)) = (T*U*V)*p • Distributive T*(u+v) = T*v + T*v Matrices Transpose: Cmn A nm cij a ji T If AT A ( A B) A B T T ( AB) B A T A is symmetric T T T Matrices Determinant: A must be square a11 a12 a11 a12 det a11a22 - a21a12 a21 a22 a21 a22 a11 a12 det a21 a22 a31 a32 a13 a22 a23 a11 a32 a33 a23 a33 - a12 a21 a23 a31 a33 a13 a21 a22 a31 a32 Euclidean transformations 2D Translation P’ t P 2D Translation Equation ty P P’ t y x P ( x, y ) t (t x , t y ) tx P' ( x t x , y t y ) Pt 2D Translation using Matrices ty P P’ t y x tx P ( x, y ) t (t x , t y ) t x x t x 1 0 t x P' y y t y 0 1 t y 1 P Scaling P’ P Scaling Equation P’ s.y P y P ( x, y ) P' ( sx, sy ) P' s P x s.x sx s 0 x P' sy 0 s y P' S P S Rotation P P’ Rotation Equations Counter-clockwise rotation by an angle Y’ x' cos y ' sin P’ P y X’ x P' R.P - sin x cos y Degrees of Freedom x' cos y ' sin R is 2x2 - sin x cos y 4 elements BUT! There is only 1 degree of freedom: The 4 elements must satisfy the following constraints: R RT RT R I det( R ) 1 Transformations can be composed • Matrix multiplication is associative. • Combine series of transformations into one matrix. (example, whiteboard). • In general, the order matters. (example, whiteboard). • 2D Rotations can be interchanged. Why? Rotation and Translation ( cos -sin tx sin cos ty 0 0 x y 1 )( ) 1 Rotation, Scaling and Translation ( a -b tx b a ty 0 0 1 x y 1 )( ) Rotation about an arbitrary point • Can translate to origin, rotate, translate back. (example, whiteboard). • This is also rotation with one translation. – Intuitively, amount of rotation is same either way. – But a translation is added. Stretching Equation P ( x, y ) P ' ( s x x, s y y ) P’ Sy.y sx x sx P' s y 0 y P y x Sx.x 0 x sy y S P' S P Linear Transformation a b x P' c d y sin - cos sin sy - cos SVD cos s x 0 sin sx cos sy sin 0 0 sin s y - cos 0 sin - cos 1 cos x sin y cos x sin y Affine Transformation x a b tx P' y c d ty 1 Viewing Position • Express world in new coordinate system. • If origins same, this is done by taking inner product with new coordinates. • Otherwise, we must translate. Simple 3D Rotation cos - sin 0 sin cos 0 0 x 0 y 1 z 1 1 1 x y z 2 . . . 2 2 Rotation about z axis. Rotates x,y coordinates. Leaves z coordinates fixed. x y z n n n Full 3D Rotation cos R - sin 0 sin cos 0 0 cos b 0 0 1 - sin b 0 sin b 1 0 1 0 0 cos 0 cos b 0 - sin 0 sin cos • Any rotation can be expressed as combination of three rotations about three axes. 1 0 0 RR 0 1 0 0 0 1 T • Rows (and columns) of R are orthonormal vectors. • R has determinant 1 (not -1). 3D Rotation + Translation • Just like 2D case 3D Viewing Position • Rows of rotation matrix correspond to new coordinate axis. Rotation about a known axis • Suppose we want to rotate about u. • Find R so that u will be the new z axis. – u is third row of R. – Second row is anything orthogonal to u. – Third row is cross-product of first two. – Make sure matrix has determinant 1.