computer graphics (cs 4731) 12: linear algebra for...

36
Computer Graphics (CS 4731) Lecture 12: Linear Algebra for Graphics (Points, Scalars, Vectors) Prof Emmanuel Agu Computer Science Dept. Worcester Polytechnic Institute (WPI)

Upload: vodiep

Post on 13-Dec-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

Computer Graphics (CS 4731)Lecture 12: Linear Algebra for Graphics 

(Points, Scalars, Vectors)

Prof Emmanuel Agu

Computer Science Dept.Worcester Polytechnic Institute (WPI)

Page 2: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

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

Page 3: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

Vectors

Magnitude Direction NO position Can be added, scaled, rotated CG vectors: 2, 3 or 4 dimensions

Length

Angle

Page 4: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

Points

Cannot add or scale points Subtract 2 points = vector

Point

Page 5: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

Vector‐Point Relationship

Diff. b/w 2 points = vectorv = Q – P

point + vector = pointP + v = Q

P

Q

v

Page 6: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

Vector Operations

Define vectors),( 32,1 aaaa),( 32,1 bbbb ),( 3322,11 bababa ba

Then vector addition:

a a+b

b

Page 7: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

Vector Operations

Define scalar, s Scaling vector by a scalar

),,( 321 sasasas a))(),(),(( 332211 bababa

ba

Note vector subtraction:

a

b

a-ba

2.5a

Page 8: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

Vector Operations: Examples

Scaling vector by a scalar

For example, if a=(2,5,6) and b=(‐2,7,1) and s=6, then

),,( 321 sasasas a

•Vector addition:

),( 3322,11 bababa ba

)7,12,0(),( 3322,11 babababa

)36,30,12(),,( 321 sasasasa

Page 9: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

Affine Combination

Given a vector 

Affine combination: Sum of all components = 1

Convex affine = affine + no negative componenti.e

1.........21 naaa

negativenonaaa n ,........., 21

),...,,( 32,1 naaaaa

Page 10: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

Magnitude of a Vector

Magnitude of a

Normalizing a vector (unit vector)

Note magnitude of normalized vector = 1. i.e

222

21 ..........|| naaa a

magnitudevector

aaa

1.......... 222

21 naaa

Page 11: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

Magnitude of a Vector

Example: if a = (2, 5, 6)

Magnitude of a

Normalizing a

65652|| 222 a

656,

655,

652a

Page 12: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

12

Convex Hull Smallest convex object containing P1P2,…..Pn

Formed by “shrink wrapping” points

Page 13: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

Dot Product (Scalar product)

Dot product,

For example, if a=(2,3,1) and b=(0,4,‐1)then

332211 ........ bababad ba

)11()43()02( ba111120

Page 14: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

Properties of Dot Products

Symmetry (or commutative):

Linearity:

Homogeneity:

And

abba

)()( baba ss

bcbabca )(

bbb 2

Page 15: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

Angle Between Two Vectors

c

x

b

y

bc

bb sin,cos bbb

cc sin,cos ccc

coscbcb

Sign of b.c:

b.c > 0

c

bb

b.c = 0

b

cc

b.c < 0

Page 16: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

Angle Between Two Vectors

Find the angle b/w the vectors b = (3,4) and c = (5,2)

Page 17: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

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 

54,

53b

292,

295c

52543|| 22 b

2925|| 22 c

Page 18: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

Angle Between Two Vectors

Step 3: Find angle as dot product

Step 4: Find angle as inverse cosine   

292,

295

54,

53ˆˆ cb

326.31)85422.0cos(

cb ˆˆ

85422.0295

23295

8295

15ˆˆ cb

Page 19: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

Standard Unit Vectors

0,0,1i

Define

0,1,0j

1,0,0k

So that any vector,

kjiv cbacba ,,

y

z x

i

j

k

0

Page 20: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

Cross Product (Vector product)

zyx aaa ,,a zyx bbb ,,bIf

Thenkjiba )()()( xyyxxzzxyzzy babababababa

Remember using determinant

zyx

zyx

bbbaaakji

Note: a x b is perpendicular to a and b

Page 21: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

Cross Product

Note: a x b is perpendicular to both a and b

a x b

a0

b

Page 22: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

Cross Product

Calculate a x b if a = (3,0,2) and b = (4,1,8)

Page 23: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

Cross Product (Vector product)

8,1,4b

Then

kjiba )03()824()20(

Using determinant

Calculate a x b if a = (3,0,2) and b = (4,1,8)

2,0,3a

814203kji

kji 3162

Page 24: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

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

Page 25: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

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

Page 26: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

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

Page 27: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

Newell Method for Normal Vectors

Formulae: Normal N = (mx, my, mz)

)(

1

0)( inexti

N

iinextix zzyym

)(

1

0)( inexti

N

iinextiy xxzzm

)(

1

0)( inexti

N

iinextiz yyxxm

Page 28: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

Newell Method for Normal Vectors

Calculate x component of normal

)(

1

0)( inexti

N

iinextix zzyym

416211907416zyx

P0

P1

P2

P0201113

)6)(0()11)(1()13)(1(

x

x

x

mmm

Page 29: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

Newell Method for Normal Vectors

Calculate y component of normal

416211907416zyx

P0

P1

P2

P023

145665

)7)(2()8)(7()13)(5(

y

y

y

m

m

m

)(

1

0)( inexti

N

iinextiy xxzzm

Page 30: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

Newell Method for Normal Vectors

Calculate z component of normal

416211907416zyx

P0

P1

P2

P051061

)2)(5()1)(6()1)(1(

z

z

z

mmm

)(

1

0)( inexti

N

iinextiz yyxxm

Note: Using Newell method yields same result as Cross product method (2,-23,-5)

Page 31: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

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

na

e

marmermae

2

Θ1 Θ2

Note: Θ1 = Θ2

Page 32: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

Consider all points of the form P()=P0 + d Line: Set of all points that pass through P0 in direction 

of vector d

Lines

Page 33: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

Parametric Form

Two‐dimensional forms of a line Explicit: y = mx +h Implicit: ax + by +c =0 Parametric: 

x() = x0 + (1-)x1y() = y0 + (1-)y1

Parametric form of line More robust and general than other forms Extends to curves and surfaces

Po

P1

α

Pα1 - α

Page 34: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

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

Q Q

P

convex not convex

Page 35: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

Curves and Surfaces

Curves: 1‐parameter non‐linear functions  of the form P()

Surfaces: two‐parameter functions P(, ) Linear functions give planes and polygons

P() P(, )

Page 36: Computer Graphics (CS 4731) 12: Linear Algebra for ...web.cs.wpi.edu/~emmanuel/courses/cs4731/A14/slides/lecture12.pdf · Computer Graphics (CS 4731) Lecture 12: Linear Algebra for

References

Angel and Shreiner, Interactive Computer Graphics, 6th edition, Chapter 3

Hill and Kelley, Computer Graphics using OpenGL, 3rdedition, Sections 4.2 ‐ 4.4