Computer Graphics (CS 543) Lecture 3 (Part 1): Linear Algebra for Graphics (Points, Scalars, Vectors) Prof Emmanuel Agu Computer Science Dept. Worcester Polytechnic Institute (WPI)
Points, Scalars and Vectors Points, vectors defined relative to a coordinate system Point: Location in coordinate system Example: Point (5,4) Cannot add or scale points y 5 (5,4) 4 (0,0) x
Vectors Magnitude Direction NO position Can be added, scaled, rotated Length CG vectors: 2, 3 or 4 dimensions Angle
Vector-Point Relationship Subtract 2 points = vector v = Q – P P v point + vector = point Q P + v = Q
Vector Operations Define vectors Then vector addition: a ( , ) a a a 1 , 2 3 b a ( , ) a b a b a b b ( , ) b b b 1 1 , 2 2 3 3 1 , 2 3 a a+b b
Vector Operations Define scalar, s Note vector subtraction: Scaling vector by a scalar b s a a ( , , ) a s a s a s 1 2 3 ( ( ), ( ), ( )) a b a b a b 1 1 2 2 3 3 a a-b a 2.5a b
Vector Operations: Examples Scaling vector by a scalar • Vector addition: s b a ( , , ) a ( , ) a s a s a s a b a b a b 1 2 3 1 1 , 2 2 3 3 For example, if a =(2,5,6) and b = (-2,7,1) and s =6, then a b ( , ) ( 0 , 12 , 7 ) a b a b a b 1 1 , 2 2 3 3 a ( , , ) ( 12 , 30 , 36 ) s a s a s a s 1 2 3
Affine Combination Given a vector a ( , ,..., ) a a a a 1 , 2 3 n ......... 1 a a a 1 2 n Affine combination: Sum of all components = 1 Convex affine = affine + no negative component i.e , ,......... a a a non negative 1 2 n
Magnitude of a Vector Magnitude of a 2 2 2 | a | .......... a a a 1 2 n Normalizing a vector (unit vector) a vector a ˆ a magnitude Note magnitude of normalized vector = 1. i.e 2 2 2 .......... 1 a a a 1 2 n
Magnitude of a Vector Example: if a = (2, 5, 6) 2 2 2 Magnitude of a | a | 2 5 6 65 Normalizing a 2 5 6 ˆ a , , 65 65 65
Convex Hull Smallest convex object containing P 1 , P 2 ,…..P n Formed by “shrink wrapping” points 11
Dot Product (Scalar product) Dot product, a b ........ d a b a b a b 1 1 2 2 3 3 For example, if a =(2,3,1) and b= (0,4,-1) then b ( 2 0 ) ( 3 4 ) ( 1 1 ) a 0 12 1 11
Properties of Dot Products Symmetry (or commutative): a b b a Linearity: ) ( a c b a b c b Homogeneity: ( a ) b ( a b ) s s 2 And b b b
Angle Between Two Vectors y b c b cos , b sin b b c c cos , c sin c c b b c b c cos c b x b b b Sign of b.c : c c c b.c > 0 b.c < 0 b.c = 0
Angle Between Two Vectors Problem: Find angle b/w vectors b = (3,4) and c = (5,2) Step 1: Find magnitudes of vectors b and c 2 2 | b | 3 4 25 5 2 2 | c | 5 2 29 Step 2: Normalize vectors b and c 3 4 5 2 ˆ ˆ c , b , 5 5 29 29
Angle Between Two Vectors ˆ b ˆ c Step 3: Find angle as dot product 3 4 5 2 ˆ ˆ b c , , 5 5 29 29 15 8 23 ˆ c ˆ b 0 . 85422 5 29 5 29 5 29 Step 4: Find angle as inverse cosine cos( 0 . 85422 ) 31 . 326
Standard Unit Vectors y Define i 1 , 0 , 0 i k j 0 , 1 , 0 0 j k 0 , 0 , 1 z x So that any vector, v , , i j k a b c a b c
Cross Product (Vector product) If a , , a a a b , , b b b x y z x y z Then a b ( ) i ( ) j ( ) k a b a b a b a b a b a b y z z y x z z x x y y x Remember using determinant i j k a a a x y z b b b x y z Note: a x b is perpendicular to a and b
Cross Product Note: a x b is perpendicular to both a and b a x b 0 a b
Cross Product (Vector product) Calculate a x b if a = (3,0,2) and b = (4,1,8) b 4 , 1 , 8 a 3 , 0 , 2 Using determinant i j k 3 0 2 4 1 8 Then a b ( 0 2 ) i ( 24 8 ) j ( 3 0 ) k 2 i 16 j 3 k
Normal for Triangle using Cross Product Method n p 2 n · ( p - p 0 ) = 0 plane n = ( p 2 - p 0 ) × ( p 1 - p 0 ) p p 1 normalize n n/ |n| p 0 Note that right-hand rule determines outward face
Newell Method for Normal Vectors Problems with cross product method: calculation difficult by hand, tedious If 2 vectors almost parallel, cross product is small Numerical inaccuracy may result p 1 p 2 p 0 Proposed by Martin Newell at Utah (teapot guy ) Uses formulae, suitable for computer Compute during mesh generation Robust!
Newell Method Example Example: Find normal of polygon with vertices P0 = (6,1,4), P1=(7,0,9) and P2 = (1,1,2) Using simple cross product: ((7,0,9)-(6,1,4)) X ((1,1,2)-(6,1,4)) = (2,-23,-5) P1 - P0 P2 P2 - P0 (1,1,2) PO (6,1,4) P1 (7,0,9)
Newell Method for Normal Vectors Formulae: Normal N = (mx, my, mz) 1 N m y y z z ( ) ( ) x i next i i next i i 0 1 N m z z x x ( ) ( ) y i next i i next i 0 i 1 N m x x y y ( ) ( ) z i next i i next i 0 i
Newell Method for Normal Vectors Calculate x component of normal x y z 1 N m y y z z ( ) ( ) x i next i i next i 6 1 4 P0 0 i P1 7 0 9 ( 1 )( 13 ) ( 1 )( 11 ) ( 0 )( 6 ) m x 1 1 2 P2 13 11 0 m x 6 1 4 P0 2 m x
Newell Method for Normal Vectors Calculate y component of normal x y z 1 N m z z x x y i next ( i ) i next ( i ) 6 1 4 P0 0 i P1 7 0 9 ( 5 )( 13 ) ( 7 )( 8 ) ( 2 )( 7 ) m y 1 1 2 P2 65 56 14 m y 6 1 4 P0 23 m y
Newell Method for Normal Vectors Calculate z component of normal x y z 1 N m x x y y 6 1 4 ( ) ( ) P0 z i next i i next i 0 i P1 7 0 9 ( 1 )( 1 ) ( 6 )( 1 ) ( 5 )( 2 ) m z 1 1 2 P2 1 6 10 m z 6 1 4 P0 5 m z Note: Using Newell method yields same result as Cross product method (2,-23,-5)
Finding Vector Reflected From a Surface a = original vector n = normal vector r = reflected vector m = projection of a along n e = projection of a orthogonal to n n Note: Θ 1 = Θ 2 a r e a m m -m Θ 1 r e m Θ 2 r a 2 m e e
Forms of Equation of a Line Two-dimensional forms of a line Explicit: y = mx +h Implicit: ax + by +c =0 α P 1 Parametric: x( a ) = a x 0 + (1- a )x 1 1 - α P α y( a ) = a y 0 + (1- a )y 1 P o Parametric form of line More robust and general than other forms Extends to curves and surfaces
Convexity An object is convex iff for any two points in the object all points on the line segment between these points are also in the object P P Q Q not convex convex
References Angel and Shreiner, Interactive Computer Graphics, 6 th edition, Chapter 3 Hill and Kelley, Computer Graphics using OpenGL, 3 rd edition, Sections 4.2 - 4.4
Recommend
More recommend