2IV60 Computer Graphics Basic Math for CG Jack van Wijk TU/e Overview • Coordinates, points, vectors • Matrices H&B A-1 nD Space nD Space: n (typically) n : number of dimensions Examples: • 1D space: time, along a line or curve • 2D space: plane, sphere • 3D space: the world we live in • 4D space: 3D + time H&B A-2 Coordinates 2D Cartesian coordinates: x y (x,y) (x,y) y x Standard Screen (output, input) H&B A-1 Polar coordinates Coordinate transform ation y from ( r , ) to ( x, y ) space : x r cos (x,y) y r sin Angle : in radians r x H&B A-1 3D coordinates 1 3D Cartesian coordinates: z x (x,y,z) y Right-handed z (x,y,z) x y Left-handed H&B A-1 3D coordinates 2 3D Cartesian coordinates: z (middle) x (thumb) (x,y,z) y (index) Right-handed z (middle) (x,y,z) y (index) x (thumb) Left-handed H&B A-1 3D coordinates 3 Cylinder coordinates: (x,y,z) z x y x r cos y r sin zz H&B A-1 3D coordinates 4 Spherical coordinates: (x,y,z) z r x y x r cos sin y r sin sin z r cos : azimuth or longitude : elevation or latitude Also : / 2 : colatitude H&B A-1 Points • Point: position in nD space • Notation: P (H&B), also P, p, p en p • (x,y,z) (H&B), also (x1, x2, x3), (Px, Py, Pz), (r, , z), ( r, , ), … H&B A-2 Vectors 1 • Vector: – “arrow” – multiple interpretations (displacement, velocity, force, …) – has a magnitude and direction – has no position • Notation: V (H&B), also V, v, v en v • (Vx, Vy, Vz) (H&B), also (x, y, z), (x1, x2, x3) H&B A-2 Vectors 2 y V P2 P1 ( x2 x1 , y2 y1 ) (Vx ,Vy ) P2 y2 P1 y1 V: directed line segment, or difference between two points x1 x2 x H&B A-2 Vectors 3 Length of a vector: V Vx2 V y2 (2D : P ythagoras ) V V V V 2 x 2 y 2 z (3D) H&B A-2 Vectors 4 Direction of a vector: Direction angles. Vy Vx Vz cos , cos , cos V V V z Unit vector V : V V |V| x y Magnitude info is removed, direction is kept. H&B A-2 Vector addition Add components, put vector head to tail y y W V+W W V V x x V W (Vx Wx ,Vy Wy ,Vz Wz ) H&B A-2 Scalar multiplication of vector Multiplication components with scalar s y y 2V V x x sV (sVx ,sVy , sVz ) H&B A-2 Combining vector operations Infinite line through P with direction V: L(t) = P + Vt, t y V P t x t : parameter along line t [a, b]: line segment H&B A-2 Vector multiplication 1 Scalar product or dot product: Product of parallel components, gives 1 real value W V V W V W cos VxWx V yWy VzWz |W| cos |V| H&B A-2 Vector multiplication 2 Scalar product: V W V W cos VxWx V yW y VzWz Commutative : V W W V Distributive : V (W Z) V W V Z Associative : V (W Z) (V W) Z ?? H&B A-2 Vector multiplication 3 Vector product or cross product: gives a vector (in 3D) n perpendicular to V and W (V yWz VzW y , VW n V W n V W sin W VzWx VxWz , VxWy V yWx ) V H&B A-2 Vector multiplication 5 Scalar product: Anticommutative : V W W V Not associative : V (W Z) (V W) Z Distributive : V (W Z) V W V Z H&B A-2 Vector multiplication 6 Scalar product: • scalar • Test if vectors are perpendicular • cos • project,… Vector product: • vector • Get a vector perpendicular to two given vectors • sin • surface area,… H&B A-2 Exercise 1 • Given a point P. • Requested: Reflect a point Q with respect to P. Q W y W x P Q’ W=P–Q Q’ = Q + 2W = 2P – Q or: = P + (P – Q ) We don’t need coordinates! Exercise 1 • Given a point P. • Requested: Reflect a point Q with respect to P. Q P Q’ Alternative P is halfway Q and Q’: P = (Q + Q’)/2 2P = Q + Q’ Q’ = 2P – Q Exercise 2 • Given a line L: L(t) = P + Vt . • Requested: Reflect a point Q with respect to L. Q W y V P t x W L(t) Q’ We know: Q’ = Q + 2 W W = L(t) – Q W. V = 0 Exercise 2 We know: L(t) = P + Vt Q’ = Q + 2 W W = L(t) – Q W. V = 0 Substitute to get t: (L(t) – Q).V = 0 (P + Vt – Q).V = 0 V .V t + (P – Q).V = 0 t = ((Q – P).V) / (V .V) Then: Q’ = Q + 2 (P + Vt – Q) = 2P – Q + V((Q – P).V / V .V) Steps to be made • Write down what you know • Eliminate, substitute, etc. to get he result • Check the result – Does it make sense? – Is there a simpler derivation? Exercise 3 • Given a triangle with vertices P, Q en R, where the angle PQR is perpendicular. • Requested: Rotate the triangle around the line PQ over an angle . What is the new P position R’ of R? Q R’ R Circle in space y C() C() r P A A B B x P |A|=1, |B|=1, A.B = 0 C() = (r cos , r sin , 0) = (0,0,0) + r cos (1,0,0) + r sin (0,1,0) = P + r cos A + r sin B Exercise 3 A = (R – Q) / |R – Q| B = (R – Q)(P – Q) / | (R – Q)(P – Q) | R’ = Q + |R – Q| cos A + |R – Q| sin B P B Q R’ A R Use: • Scalar product, cross product • coordinate independent definitions • vector algebra Don’t use: • arccos, arcsin • y = f(x) Very short intro to Linear Algebra System of linear equations: u 2x 3y 4z v x 5 y 3z w 5x y z Such systems occur in many, many applications. They are studied in Linear Algebra. H&B A-5 Very short intro to Linear Algebra System of linear equations: u 2x 3y 4z v x 5 y 3z w 5x y z Typical questions: - Given u, v, w, what are x, y, z? - Can we find a unique solution? H&B A-5 Very short intro to Linear Algebra System of linear equations: u 2x 3y 4z v x 5 y 3z w 5x y z Crucial in computer graphics: - Transforming geometric objects - Change of coordinates H&B A-5 Example transformation T ransformation P P': P : ( x, y ) P' xU yV, with U (3,2) V (2,3) In coordinates : x' 3x 2 y y ' 2 x 3 y y P: (x,y) x y’ P’ V y U x x’ H&B A-5 What is a matrix? Matrix: - Mathematical objects with operations Matrix in computer graphics: - Defines a coordinate frame - Defines a transformation - Handy tool for manipulating transformations H&B A-5 Matrix Matrix: rectangular array of elements Element: quantity (value, expression, function, …) Examples: 3.60 0.01 2.1 e 5.46 0.00 1.6 , 2 x e x x , 2 x a1 a2 x a3 , y z H&B A-5 Matrix r c matrix: r rows, c columns mij : element at row i and column j. m11 m12 m1c m m m 22 2c M 21 mr1 mr 2 mrc H&B A-5 Matrix r c matrix: r rows, c columns mij : element at row i and column j. m11 m12 m1c m m m 22 2c M 21 mr1 mr 2 mrc r c elements H&B A-5 Matrix r c matrix: r rows, c columns mij : element at row i and column j. m11 m12 m1c m m m 22 2c M 21 mr1 mr 2 mrc c columns H&B A-5 Matrix r c matrix: r rows, c columns mij : element at row i and column j. m11 m12 m1c m m m 21 22 2c r rows M mr1 mr 2 mrc H&B A-5 Matrix Column vector: matrix with c =1 v1 v 2 V vr Row vector: matrix with r =1 V v1 v2 vc Scalar: matrix with r=1and c=1 V v Used for vectors (and points) H&B A-5 Matrix Matrix as collection of column vectors: u x M U V W u y u z vx vy vz wx wy wz W Matrix contains an axis-frame: V U H&B A-5 Operations on matrices • Multiplication with scalar – simple • Addition – simple • Matrix-matrix multiplication – More difficult, but the most important H&B A-5 Scalar Matrix multiplication Matrix M multiplied with scalar s: sm11 sm12 sm sm 21 22 A sM smr1 smr 2 3x 12 x example : 3 6 2 y 2 sm1c sm2c , i.e., aij smij smrc 4 y H&B A-5 Scalar Matrix addition a11 a12 a 21 a22 M AB ar1 ar 2 a11 b11 a b 21 21 ar1 br1 a1c b11 a2c b21 arc br1 a12 b12 a22 b22 ar 2 br 2 b12 b1c b22 b2c br 2 brc a1c b1c a2c b2c , i.e., mij aij bij arc brc 3x 12 4 10 3x 4 2 example : 6 2 y 2 0 8 2 y H&B A-5 Scalar Matrix addition a11 a12 a 21 a22 M AB ar1 ar 2 a11 b11 a b 21 21 ar1 br1 a1c b11 b12 a2c b21 b22 arc br1 br 2 a12 b12 a1c b1c a22 b22 a2c b2c ar 2 br 2 arc brc b1c b2c brc , i.e., mij aij bij • Just add elements pairwise • A and B must have the same number of rows and columns • Generalization of vector and scalar addition H&B A-5 Matrix Matrix multiplication a11 a C AB 21 amn n cij aik bkj k 1 a12 a22 am 2 a1n a2 n amn i = 2 ; k b11 b12 b1q b b b 2q 21 22 b p1 b p 2 b pq j = 1 ; k • cij: dot product of row vector ai and column vector bj • #columns A must be the same as #rows B: n = p • C: m q matrix H&B A-5 Matrix Matrix multiplication Example: 3 2 2 1 3 3 2 2 3 4 11 16 1 4 1 2 1 1 4 3 1 2 4 4 13 16 3 4 0 0 1 5 3 0 2 5 4 15 5 20 i=3 j=2 i=3, j=2 H&B A-5 Matrix Matrix multiplication Example: 4 A 1 2 3 , B 5 6 AB BA H&B A-5 Matrix Matrix multiplication Example: 4 A 1 2 3 , B 5 6 4 AB 1 2 3 5 1 4 2 5 3 6 32 6 BA H&B A-5 Matrix Matrix multiplication Example: 4 A 1 2 3 , B 5 AB BA 6 4 AB 1 2 3 5 1 4 2 5 3 6 32 6 4 4 1 4 2 4 3 4 8 12 BA 5 1 2 3 5 1 5 2 5 3 5 10 15 6 6 1 6 2 6 3 6 12 18 H&B A-5 Matrix Matrix multiplication • AB BA Matrix matrix multiplication is not commutative! Order matters! • A(B+C) = AB+AC Matrix matrix multiplication is distributive H&B A-5 Example transformation sequence (coordinate version) X ' X : x' px qy, X ' ' X ': x' ' ax'by' , y ' rx sy; y ' ' cx ' dy'. T hen X ' ' X : x' ' a ( px qy ) b( rx sy), y ' ' c( px qy ) d (rx sy); or x' ' ( ap br ) x ( aq bs) y , y ' ' (cp dr ) x (cq ds) y. Unclear! Error-prone! H&B A-5 Example transformation sequence (matrix vector version) x' p X ' X : y ' r x' ' a X ' ' X ': y ' ' c q x ; s y b x' . d y ' T hen X ' ' X : x' ' a b p q x ap br aq bs x y ' ' c d r s y cp dr cq ds y H&B A-5 Example transformation sequence (compact matrix vector version) X ' X : X' AX ; X ' ' X ': X'' BX' T hen X ' ' X : X'' BAX Here some more detail : x x' x' ' X , X' ' , X' ' , y y ' y ' ' p q a b A ;B . r s c d Matrix vector notation allows for compactness and genericity! H&B A-5 Matrix Transpose M a11 a12 a1c a a a 2c 21 22 a a a r2 rc r1 Transpose matrix: Interchange rows and columns. r c matrix M c r matrix MT a11 a21 ar1 a a a 12 22 r 2 , i.e., mT m MT ji ij a a a 2c rc 1c 1 4 1 2 3 2 5 , examples : 4 5 6 3 6 T [x y x z ]T y z H&B A-5 Matrix Inverse Simple algebra puzzle. Let ax = b. What is x? ax b Multiplyleft and right with a 1 a 1ax a 1b Use a 1a equals 1 x a 1b Done Linear algebra puzzle. Let MU = V. What is U? MU V Multiplyleft and right with M 1 M 1MU M 1V Use M 1M equals I U M 1V Done! H&B A-5 Matrix Inverse Inverse of a n n (square) matrix M is denoted M 1 , with MM 1 M 1M I, where I is the identity matrix : 1 0 I 0 0 0 1 0 1 if i j , i.e., I ij . 0 if i j 0 1 I is a diagonal matrix, with all 1' s on the diagonal. M 1 " undoes" the effect of M, multiplying with I has no effect. H&B A-5 Matrix Inverse examples 41 1/ 4 1 2 1 3 2 5 4 5 / 2 3 / 2 2 1 3 2 2 5 / 2 3 1 2 3 / 2 1 0 3 2 5 4 5 / 2 3 / 2 5 2 4 5 / 2 5 1 4 3 / 2 0 1 H&B A-5 Matrix Inverse examples a b c d a b c d 1 ? 1 1 d b a ad bc c a b d b ad bc ab ba 1 0 (ad bc) c d c a ab ba cb ad 0 1 1 2 1 4 2 ? Does not exist, cannot be inverted. H&B A-5 Matrix Inverse - Does not always exist - In general: if determinant = | M | = 0, matrix cannot be inverted - Inverse for n = 1 and n = 2: easy - Inverse for higher n: use library function - Important special case: orthonormal matrix. H&B A-5 Overview • Coordinates, points, vectors – definitions, operations, examples • Matrices – definitions, operations, examples