34
CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This lecture: Monte Carlo Integration Review random variables and probability Sampling from distributions Sampling from shapes Numerical calculation of illumination Pat Hanrahan / Matt Pharr, Spring 2019

Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

CS348b Lecture 6

Monte Carlo 1: Integration

Previous lecture: Analytical illumination formula

This lecture: Monte Carlo Integration

■ Review random variables and probability

■ Sampling from distributions

■ Sampling from shapes

■ Numerical calculation of illumination

Pat Hanrahan / Matt Pharr, Spring 2019

Page 2: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

Irradiance from the Environment

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2019

2

( ) ( , )cosiH

E x L x dω θ ω= ∫

ω ω θ ωΦ =2 ( , ) ( , )cosi id x L x dA d

( , ) ( , )cosidE x L x dω ω θ ω=

Light meter

ω( , )iL x

dA

θdω

Page 3: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

Irradiance from a Uniform Area Source

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2019

2

( ) cos

cosH

E x L d

L d

L

θ ω

θ ωΩ

=

=

= Ω

∫!

A

Ω!

Ω

Direct Illumination

Page 4: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

Uniform Triangle Light Source

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2019

1 1

n n

i i ii iA γ

= =

= •∑ ∑ N N! !

�iedge i

~N

~Ni

Page 5: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

Penumbras and Umbras

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2019

Page 6: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

CS348b Lecture 6

Lighting and Soft Shadows

Challenges

1 Occluders

- Complex geometry

- Number of occluders

2 Non-uniform light sources

Pat Hanrahan / Matt Pharr, Spring 2019

2

( ) ( , )cosiH

E x L x dω θ ω= ∫

Source: Agrawala. Ramamoorthi, Heirich, Moll, 2000

Page 7: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

Monte Carlo Illumination Calculation

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2019

1 shadow ray per eye ray

Center Random

Page 8: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

CS348b Lecture 6

Monte Carlo Algorithms

Advantages

■ Easy to implement

■ Easy to think about (but be careful of subtleties)

■ Robust when used with complex integrands (lights, BRDFs) and domains (shapes)

■ Efficient for high dimensional integrals

■ Efficient when only need solution at a few points

Disadvantages

■ Noisy

■ Slow (many samples needed for convergence)

Pat Hanrahan / Matt Pharr, Spring 2019

Page 9: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

CS348b Lecture 6

Random Variables

is a random variable

A random variable takes on different values

(represents a distribution of potential values)

probability distribution function (PDF)

Pat Hanrahan / Matt Pharr, Spring 2019

X

~ ( )X p x

Page 10: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

CS348b Lecture 6

Discrete Probability Distributions

Discrete values

with probability

Pat Hanrahan / Matt Pharr, Spring 2019

0ip ≥

11

n

iip

=

=∑

ip

ix

ipix

Page 11: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

CS348b Lecture 6

Discrete Probability Distributions

Cumulative PDF

Pat Hanrahan / Matt Pharr, Spring 2019

=

=∑1

j

j ii

P p

1

0

jP

≤ ≤0 1iP

=1nP

jP

Page 12: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

CS348b Lecture 6

Discrete Probability Distributions

Construction of samples

To randomly select an event,

Select if

Pat Hanrahan / Matt Pharr, Spring 2019

1i iP U P− < ≤

U

1

0

Uniform random variable

3x

ix

Page 13: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

CS348b Lecture 6

Continuous Probability Distributions

PDF

CDF

Pat Hanrahan / Matt Pharr, Spring 2019

1

0

( ) Pr( )P x X x= <

Pr( ) ( )

( ) ( )

X p x dx

P P

β

α

α β

β α

≤ ≤ =

= −

( )p x

10

Uniform

( ) 0p x ≥

0

( ) ( )x

P x p x dx= ∫(1) 1P =

( )P x

Page 14: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

CS348b Lecture 6

Sampling Continuous Distributions

Cumulative probability distribution function

Construction of samples

Solve for

Must know the formula for:

1. The integral of

2. The inverse function

Pat Hanrahan / Matt Pharr, Spring 2019

U

X

1

0

( ) Pr( )P x X x= <

p(x)

P�1(x)

X = P�1(U)

Page 15: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

Sampling a Circle

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2019

12 1 1 2 22

00 0 0 0 0

2rA r dr d r dr d

π ππ

θ θ θ π# $

= = = =% &' (

∫ ∫ ∫ ∫

( , ) ( ) ( )p r p r pθ θ=

2

( ) 2( )p r rP r r

=

=

12 Uθ π=

rdθ

dr

1( )21( )2

p

P

θπ

θ θπ

=

=

1( , ) ( , ) rp r dr d r dr d p rθ θ θ θπ π

= ⇒ =

2r U=

Page 16: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

Sampling a Circle

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2019

1

2

2 Ur Uθ π=

=1

2

2 U

r U

θ π=

=

RIGHT = Equi-ArealWRONG ≠ Equi-Areal

Page 17: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

classShape{public://ShapeInterface…virtual~Shape();virtualBounds3fObjectBound()const=0;virtualBounds3fWorldBound()const;virtualboolIntersect(constRay&ray,Float*tHit,SurfaceInteraction*isect,booltestAlphaTexture=true)const=0;…virtualFloatArea()const=0;…//Sampleapointonthesurfaceoftheshape//andreturnthePDFwithrespecttoareaonthesurface.virtualInteractionSample(constPoint2f&u,Float*pdf)const=0;virtualFloatPdf(constInteraction&)const{return1/Area();}..};

Page 18: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

Rejection Sampling

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2019

Area of circle / Area of square

Efficiency?

do{

X=Uniform(-1,1)

Y=Uniform(-1,1)

}while(X*X+Y*Y>1);

Page 19: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

Sampling 2D Directions

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2019

do{

X=Uniform(-1,1);

Y=Uniform(-1,1);

}while(X*X+Y*Y>1);

R=sqrt(X*X+Y*Y)

dx=X/R

dy=Y/R

Page 20: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

Computing Area of a Circle

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2019

A=0

for(i=0;i<N;i++){

X=Uniform(-1,1);

Y=Uniform(-1,1);

if(X*X+Y*Y<1)

A+=1;

}

A=4*A/N

Page 21: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

Definite integral

Expectation of

Random variables

Estimator

Monte Carlo Integration

1

0

( ) ( )I f f x dx≡ ∫1

0

[ ] ( ) ( )E f f x p x dx≡ ∫

~ ( )iX p x( )i iY f X=

1

1 N

N ii

F YN =

= ∑

f

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2019

Page 22: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

Unbiased Estimator

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2019

1

1 11

1 01

1 01

0

1[ ] [ ]

1 1[ ] [ ( )]

1 ( ) ( )

1 ( )

( )

N

N ii

N N

i ii i

N

i

N

i

E F E YN

E Y E f XN N

f x p x dxN

f x dxN

f x dx

=

= =

=

=

=

= =

=

=

=

∑ ∑

∑∫

∑∫

[ ] ( )NE F I f=

Assume uniform probability distribution for now

[ ] [ ]i ii i

E Y E Y=∑ ∑[ ] [ ]E aY aE Y=

Properties

Page 23: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

Direct Lighting: Hemispherical Integral

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2019

dA

θdω

2

( ) ( , ) cosH

E x L x dω θ ω= ∫

ω( , )iL x

Page 24: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

Direct Lighting: Solid Angle Sampling

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2019

2

( ) 1H

p dω ω =∫

Sample hemisphere uniformly

dA

θdω

ω( , )iL x

Page 25: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

Direct Lighting: Solid Angle Sampling

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2019

Estimator

dA

θdω

ω( , )iL x

Page 26: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

Direct Lighting: Hemisphere Sampling

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2019

Hemisphere

16 shadow rays

Page 27: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

Direct Lighting: Area Integral

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2019

A!x!

x

θθ "

0 blocked( , )

1 visibleV x x

!" = #

$Radiance

Visibility

Integral

Page 28: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

Direct Lighting: Area Integral

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2019

A!x!

x

θθ "

Page 29: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

Direct Lighting: Area Sampling

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2019

A!x!

x

θθ "

Sample shape uniformly by area

Page 30: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

Direct Lighting: Area Sampling

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2019

A!

ω"

x!

x

θθ " Estimator

Page 31: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

Direct Lighting: Area Sampling

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2019

Area

16 shadow rays

Page 32: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

Random Sampling Introduces Noise

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2019

1 shadow ray per eye ray

Center Random

Page 33: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

Quality Improves with More Rays

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2019

16 shadow rays1 shadow ray

Area Area

Page 34: Monte Carlo 1: Integrationgraphics.stanford.edu/.../06_mc1/06_mc1_slides.pdf · CS348b Lecture 6 Monte Carlo 1: Integration Previous lecture: Analytical illumination formula This

Why is Area Better than Hemisphere?

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2019

Hemisphere

16 shadow rays

Area

16 shadow rays