2IV60 Computer Graphics Basic Math for CG Jack van Wijk TU/e.

63
2IV60 Computer Graphics Basic Math for CG Jack van Wijk TU/e

Transcript of 2IV60 Computer Graphics Basic Math for CG Jack van Wijk TU/e.

2IV60 Computer GraphicsBasic 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 + timeH&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

x

y (x,y)

H&B A-1

r radiansin : Angle

sin

cos

:space ),( to),( from

ation transformCoordinate

ry

rx

yxr

3D coordinates 1

3D Cartesian coordinates:

x y

(x,y,z)

Right-handed Left-handedH&B A-1

z

xy

(x,y,z)z

3D coordinates 2

3D Cartesian coordinates:

x (thumb) y (index)

(x,y,z)

Right-handed Left-handedH&B A-1

z (middle) (x,y,z)z (middle)

x (thumb)y (index)

3D coordinates 3

Cylinder coordinates:

x y

(x,y,z)

H&B A-1

z

zz

ry

rx

sin

cos

3D coordinates 4Spherical coordinates:

x y

(x,y,z)

H&B A-1

z

colatitude :

2/ :Also

latitudeor elevation :

longitudeor azimuth :

cos

sinsin

sincos

rz

ry

rx

r

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

),(

),( 1212

1

yx VV

yyxx

PPV 2

x

yP2

P1

x1 x2

y2

y1

H&B A-2

V: directed line segment, or difference between two points

Length of a vector:

Vectors 3

(3D)

)Pythagoras :(2D

222

22

zyx

yx

VVV

VV

V

V

H&B A-2

Direction of a vector: Direction angles.

Unit vector V :

Magnitude info is removed, direction is kept.

Vectors 4

H&B A-2

VVVzyx VVV

cos,cos,cos

xy

z

|| V

VV

Add components, put vector head to tail

Vector addition

H&B A-2

x

y

x

y

VV

WW V+W

),( zzyyxx WVW,VWV WV

Multiplication components with scalar s

Scalar multiplication of vector

H&B A-2

x

y

x

y

V

),( zyx sV,sVsVs V

2V

Infinite line through P with direction V:

L(t) = P + Vt, t

Combining vector operations

H&B A-2

x

y VP t

t : parameter along line

t [a, b]: line segment

Scalar product or dot product:Product of parallel components, gives 1 real value

Vector multiplication 1

H&B A-2

V

zzyyxx WVWVWV

cosWVWV

W

|W| cos |V|

Scalar product:

Vector multiplication 2

H&B A-2

??)( :eAssociativ

:veDistributi

:eCommutativ

cos

ZWVZ)(WV

ZVWVZ)(WV

VWWV

WVWV

zzyyxx WVWVWV

Vector product or cross product: gives a vector (in 3D)

n perpendicular to V and W

Vector multiplication 3

H&B A-2

V)

,

,(

sin

xyyx

zxxz

yzzy

WVWV

WVWV

WVWV

WVnWV

W

n

VW

Scalar product:

Vector multiplication 5

H&B A-2

ZVWVZ)(WV

ZWVZ)(WV

VWWV

:veDistributi

)( :eassociativNot

:ativeAnticommut

Scalar product: • scalar• Test if vectors are

perpendicular

• cos • project,…

Vector multiplication 6

H&B A-2

Vector product:

• vector

• Get a vector perpendicular to two given vectors

• sin

• surface area,…

W

W

Exercise 1

• Given a point P.

• Requested: Reflect a point Q with respect to P.

x

yP

Q

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.

P

Q

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.

x

y VP

t

W

W

Q

Q’

L(t)

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 position R’ of R?

R

P

Q R’

Circle in space

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

x

yC()

r

P A

B

C()

PA

B

|A|=1, |B|=1, A.B = 0

Exercise 3

R

P

Q R’

A

B

A = (R – Q) / |R – Q|

B = (R – Q)(P – Q) / | (R – Q)(P – Q) |

R’ = Q + |R – Q| cos A + |R – Q| sin B

Use:

• Scalar product, cross product

• coordinate independent definitions

• vector algebra

Don’t use:

• arccos, arcsin

• y = f(x)

System of linear equations:

Such systems occur in many, many applications.

They are studied in Linear Algebra.

Very short intro to Linear Algebra

H&B A-5

zyxw

zyxv

zyxu

5

35

432

System of linear equations:

Typical questions:- Given u, v, w, what are x, y, z?- Can we find a unique solution?

H&B A-5

zyxw

zyxv

zyxu

5

35

432

Very short intro to Linear Algebra

System of linear equations:

Crucial in computer graphics:- Transforming geometric objects- Change of coordinates

H&B A-5

zyxw

zyxv

zyxu

5

35

432

Very short intro to Linear Algebra

H&B A-5

yxy

yxx

,

,

yx

yx

32'

23 '

:scoordinateIn

)32(

)23(

with,'

),(:

:'tion Transforma

V

U

VUP

P

PP

Example transformation

x’

y’

xyU

VP’

x

yP: (x,y)

Matrix:- Mathematical objects with operations

Matrix in computer graphics:- Defines a coordinate frame- Defines a transformation- Handy tool for manipulating transformations

What is a matrix?

H&B A-5

Matrix: rectangular array of elements

Element: quantity (value, expression, function, …)

Examples:

Matrix

H&B A-5

z

y

x

aaaxe

xex

x

,,,6.100.046.5

1.201.060.332122

r c matrix: r rows, c columns

mij : element at row i and column j.

Matrix

H&B A-5

rcrr

c

c

mmm

mmm

mmm

21

22221

11211

M

r c matrix: r rows, c columns

mij : element at row i and column j.

Matrix

H&B A-5

rcrr

c

c

mmm

mmm

mmm

21

22221

11211

M

r c elements

r c matrix: r rows, c columns

mij : element at row i and column j.

Matrix

H&B A-5

rcrr

c

c

mmm

mmm

mmm

21

22221

11211

M

c columns

r c matrix: r rows, c columns

mij : element at row i and column j.

Matrix

H&B A-5

rcrr

c

c

mmm

mmm

mmm

21

22221

11211

Mr rows

Matrix

H&B A-5

rv

v

v

2

1

V

Column vector:matrix with c =1

Used for vectors(and points)

cvvv 21V

Row vector:matrix with r =1

vV

Scalar:matrix with r=1and c=1

Matrix as collection of column vectors:

Matrix

H&B A-5

zzz

yyy

xxx

wvu

wvu

wvu

WVUM

UV

W

Matrix contains an axis-frame:

• Multiplication with scalar– simple

• Addition– simple

• Matrix-matrix multiplication– More difficult, but the most important

Operations on matrices

H&B A-5

Matrix M multiplied with scalar s:

Scalar Matrix multiplication

H&B A-5

y

x

y

x

sma

smsmsm

smsmsm

smsmsm

s ijij

rcrr

c

c

2

43

26

123 :example

i.e.,,

21

22221

11211

MA

Scalar Matrix addition

H&B A-5

y

x

y

x

bam

bababa

bababa

bababa

bbb

bbb

bbb

aaa

aaa

aaa

ijijij

rcrcrrrr

cc

cc

rcrr

c

c

rcrr

c

c

28

243

02

104

26

123 :example

i.e.,,

2211

2222222121

1112121111

21

22221

11211

21

22221

11211

BAM

Scalar Matrix addition

H&B A-5

ijijij

rcrcrrrr

cc

cc

rcrr

c

c

rcrr

c

c

bam

bababa

bababa

bababa

bbb

bbb

bbb

aaa

aaa

aaa

i.e.,,

2211

2222222121

1112121111

21

22221

11211

21

22221

11211

BAM

• Just add elements pairwise

• A and B must have the same number of rows and columns

• Generalization of vector and scalar addition

Matrix Matrix multiplication

H&B A-5

n

kkjikij

pqpp

q

q

mnmmn

n

n

bac

bbb

bbb

bbb

aaa

aaa

aaa

1

21

22221

11211

2

22221

11211

ABC

i = 2 ; k 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

Matrix Matrix multiplication

H&B A-5

2015

1613

1611

45203510

44213411

43223312

43

21

50

41

32

i = 3 j = 2 i=3, j=2

Example:

Matrix Matrix multiplication

H&B A-5

BA

AB

BA

6

5

4

,321

Example:

Matrix Matrix multiplication

H&B A-5

BA

AB

BA

32635241

6

5

4

321

6

5

4

,321

Example:

Matrix Matrix multiplication

H&B A-5

18126

15105

1284

362616

352515

342414

321

6

5

4

32635241

6

5

4

321

6

5

4

,321

BA

AB

BA

Example:

AB BA

Matrix Matrix multiplication

H&B A-5

• AB BA

Matrix matrix multiplication is not commutative!

Order matters!

• A(B+C) = AB+AC

Matrix matrix multiplication is distributive

Example transformation sequence (coordinate version)

H&B A-5

:''Then

'.''','''':'''

;',':'

XX

dycxybyaxxXX

syrxyqypxxXX

Unclear!

Error-prone!

.)()(''

,)()(''

or

);()(''

),()(''

ydscqxdrcpy

ybsaqxbrapx

syrxdqypxcy

syrxbqypxax

H&B A-5

y

x

dscqdrcp

bsaqbrap

y

x

sr

qp

dc

ba

y

x

XX

y

x

dc

ba

y

xXX

y

x

sr

qp

y

xXX

''

''

:''Then

.'

'

''

'':'''

;'

':'

Example transformation sequence (matrix vector version)

H&B A-5

BAX'X'

BX''X'

AXX

:''Then

:'''

;':'

XX

XX

XX

Example transformation sequence (compact matrix vector version)

.;

,''

'''',

'

''',

:detail more some Here

dc

ba

sr

qp

y

x

y

x

y

x

BA

XXX

Matrix vector notation allows for compactness

and genericity!

H&B A-5

Matrix TransposeTranspose matrix:

Interchange rows and columns.

r c matrix M c r matrix MT

z

y

x

zyx

mm

aaa

aaa

aaa

aaa

aaa

aaa

TT

ijTji

rccc

r

r

T

rcrr

c

c

][,

63

52

41

654

321 :examples

i.e.,,

21

22212

12111

21

22221

11211

M

M

H&B A-5

Matrix InverseSimple algebra puzzle. Let ax = b. What is x?

Done

1 equals Use

right with andleft Multiply

1

111

1

bax

aabaaxa

abax

Done!

equals Use

right with andleft Multiply

1

111

1

VMU

IMMVMMUM

MVMU

Linear algebra puzzle. Let MU = V. What is U?

H&B A-5

Matrix Inverse

effect. no has with gmultiplyin

, ofeffect theundoes""

diagonal. on the s1' all with , a is

. if0

if1 i.e., ,

100

010

001

:matrixidentity theis where

,

with, denoted is matrix (square) a of Inverse

1

11

1

I

MM

I

II

I

IMMMM

MM

atrixdiagonal m

ji

ji

nn

ij

H&B A-5

Matrix Inverse examples 4/14 1

2/32/5

12

45

231

10

01

2/34152/5425

2/32132/5223

2/32/5

12

45

23

H&B A-5

Matrix Inverse examples

?1

dc

ba

ac

bd

bcaddc

ba 11

10

01)( bcad

adcbbaab

baabbcad

ac

bd

dc

ba

?24

121

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.

Overview

• Coordinates, points, vectors– definitions, operations, examples

• Matrices– definitions, operations, examples