3d geometry for computer graphics - taudcor/graphics/cg-slides/3d... · 2013-06-03 · how to...

52
3d Geometry for Computer Graphics Lesson 1: Basics & PCA

Upload: others

Post on 22-Jul-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

3d Geometry for Computer Graphics

Lesson 1: Basics & PCA

Page 2: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

3d geometry

Page 3: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

3d geometry

Page 4: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

3d geometry

Page 5: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Why??

We represent objects using mainly linear primitives:

pointslines, segmentsplanes, polygons

Need to know how to compute distances, transformations, projections…

Page 6: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

How to approach geometric problems

We have two ways:1. Employ our geometric intuition2. Formalize everything and employ our algebra skills

Often we first do No.1 and then solve with No.2For complex problems No.1 is not always easy…

Page 7: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Motivation – Shape Matching

What is the best transformation that aligns the unicorn with the lion?There are tagged feature points in both sets that are matched by the user

Page 8: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Motivation – Shape Matching

The above is not a good alignment….

Regard the shapes as sets of points and try to “match” these setsusing a linear transformation

Page 9: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Motivation – Shape Matching

Regard the shapes as sets of points and try to “match” these setsusing a linear transformation

To find the best rotation we need to know SVD…

Page 10: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Applications for Shape Matching

Mesh ComparisonDeformation TransferMorphing

Page 11: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Principal Component Analysis

x

y

Given a set of points,find the best line that approximates it

Page 12: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Principal Component Analysis

x

y

x’

y’

Given a set of points,find the best line that approximates it

Page 13: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Principal Component Analysis

x

y

When we learn PCA (Principal Component Analysis),we’ll know how to find these axes that minimize

the sum of distances2

x’

y’

Page 14: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

PCA and SVD

PCA and SVD are important tools not only in graphics but also in statistics, computer vision and more.

To learn about them, we first need to get familiar with eigenvalue decomposition.So, today we’ll start with linear algebra basics reminder.

SVD: A = UΛV T

Λ is diagonal contains the singular values of A

Page 15: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Vector spaceInformal definition:

V ≠ ∅ (a non-empty set of vectors)

v, w ∈ V ⇒ v + w ∈ V (closed under addition)

v ∈ V, α is scalar ⇒ αv ∈ V (closed under multiplication by scalar)

Formal definition includes axioms about associativity and distributivity of the + and ⋅ operators.

0 ∈ V always!

Page 16: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Subspace - exampleLet l be a 2D line though the originL = {p – O | p ∈ l} is a linear subspace of R2

x

y

O

Page 17: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Subspace - exampleLet π be a plane through the origin in 3DV = {p – O | p ∈ π} is a linear subspace of R3

y

x

z

O

Page 18: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Linear independenceThe vectors {v1, v2, …, vk} are a linearly independent set if:α1v1 + α2v2 + … + αkvk = 0 ⇔ αi = 0 ∀ i

It means that none of the vectors can be obtained as a linear combination of the others.

Page 19: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Linear independence - exampleParallel vectors are always dependent:

Orthogonal vectors are always independent.

vw

v = 2.4 w ⇒ v + (−2.4)w = 0

Page 20: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Basis of V{v1, v2, …, vn} are linearly independent{v1, v2, …, vn} span the whole vector space V:

V = {α1v1 + α2v2 + … + αnvn | αi is scalar}

Any vector in V is a unique linear combination of the basis.The number of basis vectors is called the dimension of V.

Page 21: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Basis - exampleThe standard basis of R3 – three unit orthogonal vectors x, y, z: (sometimes called i, j, k or e1, e2, e3)

y

z

x

Page 22: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Matrix representationLet {v1, v2, …, vn} be a basis of VEvery v∈V has a unique representation

v = α1v1 + α2v2 + … + αnvn

Denote v by the column-vector:

The basis vectors are therefore denoted:

1

n

α

α

⎛ ⎞⎜ ⎟= ⎜ ⎟⎜ ⎟⎝ ⎠

v

⎟⎟⎟⎟⎟

⎜⎜⎜⎜⎜

⎟⎟⎟⎟⎟

⎜⎜⎜⎜⎜

⎟⎟⎟⎟⎟

⎜⎜⎜⎜⎜

1

00

,

0

10

,

0

01

Page 23: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Linear operatorsA : V → W is called linearoperator if:

A(v + w) = A(v) + A(w)A(α v) = α A(v)

In particular, A(0) = 0Linear operators we know:

ScalingRotation, reflectionTranslation is not linear – moves the origin

Page 24: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Linear operators - illustrationRotation is a linear operator:

v

w

v+w

R(v+w)

v

w

Page 25: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Linear operators - illustrationRotation is a linear operator:

v

w

v+w

R(v+w)

v

wR(w)

Page 26: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Linear operators - illustrationRotation is a linear operator:

v

w

v+w

R(v+w)

v

wR(w) R(v)

Page 27: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Linear operators - illustrationRotation is a linear operator:

v

w

v+w

R(v+w)

v

wR(w) R(v)

R(v)+R(w)

R(v+w) = R(v) + R(w)

Page 28: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Matrix representation of linear operatorsLook at A(v1),…, A(vn) where {v1, v2, …, vn}is a basis.For all other vectors:

v = α1v1 + α2v2 + … + αnvn

A(v) = α1A(v1) + α2A(v2) + … + αnA(vn)So, knowing what A does to the basis is enoughThe matrix representing A is:

1 2

| | |( ) ( ) ( )| | |

A nM A A A⎛ ⎞⎜ ⎟= ⎜ ⎟⎜ ⎟⎝ ⎠

v v v

Page 29: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Matrix representation of linear operators

1 2 1

1| | | |

0( ) ( ) ( ) ( )| | | |

0

nA A A A

⎛ ⎞⎛ ⎞ ⎛ ⎞⎜ ⎟⎜ ⎟ ⎜ ⎟⎜ ⎟ =⎜ ⎟ ⎜ ⎟⎜ ⎟⎜ ⎟ ⎜ ⎟⎜ ⎟⎝ ⎠ ⎝ ⎠⎜ ⎟

⎝ ⎠

v v v v

1 2 2

0| | | |

1( ) ( ) ( ) ( )| | | |

0

nA A A A

⎛ ⎞⎛ ⎞ ⎛ ⎞⎜ ⎟⎜ ⎟ ⎜ ⎟⎜ ⎟ =⎜ ⎟ ⎜ ⎟⎜ ⎟⎜ ⎟ ⎜ ⎟⎜ ⎟⎝ ⎠ ⎝ ⎠⎜ ⎟

⎝ ⎠

v v v v

Page 30: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Matrix operations

Addition, subtraction, scalar multiplication –simple…Multiplication of matrix by column vector:

111 1 1 1

1

row ,

row ,

i iin

m mn n mi i mi

a ba a b

a a b a b

⎛ ⎞< >⎛ ⎞⎛ ⎞ ⎛ ⎞⎜ ⎟

⎜ ⎟⎜ ⎟ ⎜ ⎟⎜ ⎟= =⎜ ⎟⎜ ⎟ ⎜ ⎟⎜ ⎟⎜ ⎟⎜ ⎟ ⎜ ⎟< >⎜ ⎟⎝ ⎠⎝ ⎠ ⎝ ⎠⎜ ⎟⎝ ⎠

b

b

A b

Page 31: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Matrix by vector multiplicationSometimes a better way to look at it:

Ab is a linear combination of A’s columns!

1

1 2 1 1 2 2

| | | | | |

| | | | | |n n n

n

bb b b

b

⎛ ⎞⎛ ⎞ ⎛ ⎞ ⎛ ⎞ ⎛ ⎞⎜ ⎟⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟= + + +⎜ ⎟⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟⎜ ⎟⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟⎝ ⎠⎝ ⎠ ⎝ ⎠ ⎝ ⎠ ⎝ ⎠

a a a a a a…

Page 32: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Matrix operations

Transposition: make the rows to be the columns

(AB)T = BTAT

⎟⎟⎟

⎜⎜⎜

⎛=

⎟⎟⎟

⎜⎜⎜

mnn

mT

mnm

n

aa

aa

aa

aa

1

111

1

111

Page 33: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Matrix operations

Inner product can in matrix form:

, T T< >= =v w v w w v

Page 34: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Matrix propertiesMatrix A (n×n) is non-singular if ∃B, AB = BA = IB = A−1 is called the inverse of AA is non-singular ⇔ det A ≠ 0

If A is non-singular then the equation Ax=b has one unique solution for each b.A is non-singular ⇔ the rows of A are linearly independent (and so are the columns).

Page 35: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Orthogonal matrices

Matrix A (n×n) is orthogonal if A−1 = AT

Follows: AAT = ATA = IThe rows of A are orthonormal vectors!Proof:

I = ATA = v1v2

vn

= viTvj = δij

v1 v2 vn

⇒ <vi, vi> = 1 ⇒ ||vi|| = 1; <vi, vj> = 0

Page 36: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Orthogonal operatorsA is orthogonal matrix ⇒ A represents a linear operator that preserves inner product (i.e., preserves lengths and angles):

Therefore, ||Av|| = ||v|| and ∠(Av,Aw) = ∠(v,w)

, ( ) ( ), .

T T T

T T

A A A A A AI

< > = = =

= = = < >

v w v w v wv w v w v w

Page 37: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Orthogonal operators - exampleRotation by α around the z-axis in R3 :

In fact, any orthogonal 3×3 matrix represents a rotation around some axis and/or a reflection

detA = +1 rotation onlydetA = −1 with reflection

⎟⎟⎟

⎜⎜⎜

⎛−=

1000cossin0sincos

αααα

R

Page 38: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Eigenvectors and eigenvaluesLet A be a square n×n matrixv is eigenvector of A if:

Av = λv (λ is a scalar)v ≠ 0

The scalar λ is called eigenvalue

Av = λv ⇒ A(αv) = λ(α v) ⇒ α v is also eigenvectorAv = λv, Aw = λw ⇒ A(v+w) = λ(v+w)Therefore, eigenvectors of the same λ form a linear subspace.

Page 39: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Eigenvectors and eigenvaluesAn eigenvector spans an axis (subspace of dimension 1) that is invariant to A – it remains the same under the transformation.Example – the axis of rotation:

O

Eigenvector of therotation transformation

Page 40: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Finding eigenvaluesFor which λ is there a non-zero solution to Ax = λx ?Ax = λx ⇔ Ax – λx = 0 ⇔ Ax – λIx = 0 ⇔ (A−λI) x = 0So, non trivial solution exists ⇔ det(A –λ I) = 0

ΔA(λ) = det(A –λ I) is a polynomial of degree n.It is called the characteristic polynomial of A.The roots of ΔA are the eigenvalues of A.Therefore, there are always at least complex eigenvalues. If n is odd, there is at least one real eigenvalue.

Page 41: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Example of computing ΔA

⎟⎟⎟

⎜⎜⎜

−−=

411303201

A

)32)(3()3(2)3()1()3(2)3)4()(1(

41133

201det)(

2

2

+−−=

=−+−−=−++−−−=

=⎟⎟⎟

⎜⎜⎜

−−−−

−=Δ

λλλ

λλλλλλλ

λλ

λλA

Cannot be factorized over ROver C: )21)(21( ii −+

Page 42: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Computing eigenvectors

Solve the equation (A – λI)x = 0We’ll get a subspace of solutions.

Page 43: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Spectra and diagonalization

The set of all the eigenvalues of A is called the spectrum of A.A is diagonalizable if A has n independent eigenvectors. Then:

1 1 1

2 2 2

n n n

AA

A

λλ

λ

==

=

v vv v

v v

A v2v1 vn= v2v1 vn

λ1λ2

λn

AV = VD

Page 44: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Spectra and diagonalization

Therefore, A = VDV−1, where D is diagonalA represents a scaling along the eigenvector axes!

A = v2v1 vn

λ1λ2

λn

v2v1 vn

−1

A = VDV−1

1 1 1

2 2 2

n n n

AA

A

λλ

λ

==

=

v vv v

v v

Page 45: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Spectra and diagonalization

A Drotationreflection

orthonormalbasis

otherorthonormal

basis

V

Page 46: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Spectra and diagonalization

A

Page 47: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Spectra and diagonalization

A

Page 48: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Spectra and diagonalization

A

Page 49: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Spectra and diagonalization

A

eigenbasis

Page 50: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Spectra and diagonalization

A is called normal if AAT = ATA. Important example: symmetric matrices A = AT.It can be proved that normal n×n matrices have exactly n linearly independent eigenvectors (over C).If A is symmetric, all eigenvalues of A are real, and it has n independent real orthonormaleigenvectors.

Page 51: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Why SVD…

Diagonalizable matrix is essentially a scaling.Most matrices are not diagonalizable – they do other things along with scaling (such as rotation)

So, to understand how general matrices behave, only eigenvalues are not enoughSVD tells us how general linear transformations behave, and other things…

Page 52: 3d Geometry for Computer Graphics - TAUdcor/Graphics/cg-slides/3d... · 2013-06-03 · How to approach geometric problems We have two ways: 1. Employ our geometric intuition 2. Formalize

Next Week: Mysteries of the PCA & SVD