Computer Graphics (CS 4731) Lecture 12: Linear Algebra for Graphics - - PowerPoint PPT Presentation
Computer Graphics (CS 4731) Lecture 12: Linear Algebra for Graphics - - PowerPoint PPT Presentation
Computer Graphics (CS 4731) Lecture 12: 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
Points, Scalars and Vectors
Points, vectors defined relative to a coordinate system
Point: Location in coordinate system
Example: Point (5,4)
y x
(5,4) (0,0) 5 4
Vectors
Magnitude Direction NO position Can be added, scaled, rotated CG vectors: 2, 3 or 4 dimensions
Length
Angle
Points
Cannot add or scale points Subtract 2 points = vector
Point
Vector‐Point Relationship
Diff. b/w 2 points = vector
v = Q – P
point + vector = point
P + v = Q
P Q v
Vector Operations
Define vectors
) , (
3 2 , 1
a a a a ) , (
3 2 , 1
b b b b ) , (
3 3 2 2 , 1 1
b a b a b a b a
Then vector addition:
a a+b b
Vector Operations
Define scalar, s Scaling vector by a scalar
) , , (
3 2 1
s a s a s a s a )) ( ), ( ), ( (
3 3 2 2 1 1
b a b a b a b a
Note vector subtraction:
a b a-b a 2.5a
Vector Operations: Examples
Scaling vector by a scalar
For example, if a=(2,5,6) and b=(‐2,7,1)
and s=6, then
) , , (
3 2 1
s a s a s a s a
- Vector addition:
) , (
3 3 2 2 , 1 1
b a b a b a b a ) 7 , 12 , ( ) , (
3 3 2 2 , 1 1
b a b a b a b a ) 36 , 30 , 12 ( ) , , (
3 2 1
s a s a s a s a
Affine Combination
Given a vector Affine combination: Sum of all components = 1 Convex affine = affine + no negative component
i.e
1 .........
2 1
n
a a a
negative non a a a
n
,......... ,
2 1
) ,..., , (
3 2 , 1 n
a a a a a
Magnitude of a Vector
Magnitude of a Normalizing a vector (unit vector) Note magnitude of normalized vector = 1. i.e
2 2 2 2 1
.......... | |
n
a a a a magnitude vector a a a ˆ
1 ..........
2 2 2 2 1
n
a a a
Magnitude of a Vector
Example: if a = (2, 5, 6) Magnitude of a Normalizing a
65 6 5 2 | |
2 2 2
a 65 6 , 65 5 , 65 2 ˆ a
12
Convex Hull
Smallest convex object containing P1P2,…..Pn Formed by “shrink wrapping” points
Dot Product (Scalar product)
Dot product, For example, if a=(2,3,1) and b=(0,4,‐1)
then
3 3 2 2 1 1
........ b a b a b a d b a ) 1 1 ( ) 4 3 ( ) 2 ( b a 11 1 12
Properties of Dot Products
Symmetry (or commutative): Linearity: Homogeneity: And
a b b a ) ( ) ( b a b a s s b c b a b c a ) ( b b b
2
Angle Between Two Vectors
c x b y
b
c
b b
sin , cos b b b
c c
sin , cos c c c
cos c b c b
Sign of b.c: b.c > 0
c b b
b.c = 0
b c c
b.c < 0
Angle Between Two Vectors
Find the angle b/w the vectors b = (3,4) and c =
(5,2)
Angle Between Two Vectors
Find the angle b/w vectors b = (3,4) and c = (5,2) Step 1: Find magnitudes of vectors b and c Step 2: Normalize vectors b and c
5 4 , 5 3 ˆ b
29 2 , 29 5 c ˆ
5 25 4 3 | |
2 2
b 29 2 5 | |
2 2
c
Angle Between Two Vectors
Step 3: Find angle as dot product Step 4: Find angle as inverse cosine
29 2 , 29 5 5 4 , 5 3 ˆ ˆ c b
326 . 31 ) 85422 . cos(
c b ˆ ˆ
85422 . 29 5 23 29 5 8 29 5 15 ˆ ˆ c b
Standard Unit Vectors
, , 1 i
Define
, 1 , j
1 , , k
So that any vector,
k j i v c b a c b a , ,
y z x i j k
Cross Product (Vector product)
z y x
a a a , , a
z y x
b b b , , b
If Then
k j i b a ) ( ) ( ) (
x y y x x z z x y z z y
b a b a b a b a b a b a
Remember using determinant
z y x z y x
b b b a a a k j i
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 a b
Cross Product
Calculate a x b if a = (3,0,2) and b = (4,1,8)
Cross Product (Vector product)
8 , 1 , 4 b
Then
k j i b a ) 3 ( ) 8 24 ( ) 2 (
Using determinant
Calculate a x b if a = (3,0,2) and b = (4,1,8)
2 , , 3 a
8 1 4 2 3 k j i
k j i 3 16 2
Normal for Triangle using Cross Product Method
p0 p1 p2 n plane n·(p - p0 ) = 0 n = (p2 - p0 ) ×(p1 - p0 ) normalize n n/ |n| p 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
Proposed by Martin Newell at Utah (teapot guy)
Uses formulae, suitable for computer Compute during mesh generation Robust!
p0 p2 p1
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)
PO P2 P1 (6,1,4) (7,0,9) (1,1,2) P1 - P0 P2 - P0
Newell Method for Normal Vectors
Formulae: Normal N = (mx, my, mz)
) ( 1 ) ( i next i N i i next i x
z z y y m
) ( 1 ) ( i next i N i i next i y
x x z z m
) ( 1 ) ( i next i N i i next i z
y y x x m
Newell Method for Normal Vectors
Calculate x component of normal
) ( 1 ) ( i next i N i i next i x
z z y y m
4 1 6 2 1 1 9 7 4 1 6 z y x
P0 P1 P2 P0
2 11 13 ) 6 )( ( ) 11 )( 1 ( ) 13 )( 1 (
x x x
m m m
Newell Method for Normal Vectors
Calculate y component of normal
4 1 6 2 1 1 9 7 4 1 6 z y x
P0 P1 P2 P0
23 14 56 65 ) 7 )( 2 ( ) 8 )( 7 ( ) 13 )( 5 (
y y y
m m m
) ( 1 ) ( i next i N i i next i y
x x z z m
Newell Method for Normal Vectors
Calculate z component of normal
4 1 6 2 1 1 9 7 4 1 6 z y x
P0 P1 P2 P0
5 10 6 1 ) 2 )( 5 ( ) 1 )( 6 ( ) 1 )( 1 (
z z z
m m m
) ( 1 ) ( i next i N i i next i z
y y x x m
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
m e
- m
r n a e
m a r m e r m a e 2
Θ1 Θ2 Note: Θ1 = Θ2
Consider all points of the form
P()=P0 + d Line: Set of all points that pass through P0 in direction
- f vector d
Lines
Parametric Form
Two‐dimensional forms of a line
Explicit: y = mx +h Implicit: ax + by +c =0 Parametric:
x() = x0 + (1-)x1 y() = y0 + (1-)y1
Parametric form of line
More robust and general than other forms Extends to curves and surfaces Po P1 α Pα 1 - α
Convexity
An object is convex iff for any two points in the
- bject all points on the line segment between these
points are also in the object
P Q Q P convex not convex
Curves and Surfaces
Curves: 1‐parameter non‐linear functions of the
form P()
Surfaces: two‐parameter functions P(, )
Linear functions give planes and polygons