38
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 2021

Monte Carlo 1: Integration

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Monte Carlo 1: Integration

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 2021

Page 2: Monte Carlo 1: Integration

Irradiance from the Environment

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2021

2

( ) ( , )cosiH

E x L x dω θ ω= ∫

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

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

dA

θdω

Page 3: Monte Carlo 1: Integration

Irradiance from a Uniform Area Source

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2021

2

( ) cos

cosH

E x L d

L d

L

θ ω

θ ωΩ

=

=

= Ω

∫!

A

Ω!

Ω

Direct Illumination

Constant

Page 4: Monte Carlo 1: Integration

Uniform Triangle Source

CS348b Lecture 5 Pat Hanrahan / Matt Pharr, Spring 2021

Page 5: Monte Carlo 1: Integration

Uniform Triangle Source

CS348b Lecture 5 Pat Hanrahan / Matt Pharr, Spring 2021

Page 6: Monte Carlo 1: Integration

Uniform Triangle Source

CS348b Lecture 5 Pat Hanrahan / Matt Pharr, Spring 2021

Page 7: Monte Carlo 1: Integration

Consider 1 Edge

CS348b Lecture 5 Pat Hanrahan / Matt Pharr, Spring 2021

γ θ γ= = •! !

cos EA N N

Area of sector

θ

Projected area of sector

N̂�N̂E

Page 8: Monte Carlo 1: Integration

Lambert’s Formula

CS348b Lecture 5 Pat Hanrahan / Matt Pharr, Spring 2021

1 1

n n

i i ii iA γ

= =

= •∑ ∑ N N! !

3

1i

iA

=∑

1A2A−

3A−

Page 9: Monte Carlo 1: Integration

Penumbras and Umbras

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2021

Page 10: Monte Carlo 1: Integration

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 2021

2

( ) ( , )cosiH

E x L x dω θ ω= ∫

Source: Agrawala. Ramamoorthi, Heirich, Moll, 2000

Page 11: Monte Carlo 1: Integration

Monte Carlo Illumination Calculation

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2021

1 shadow ray per eye ray

Center Random

Page 12: Monte Carlo 1: Integration

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 2021

Page 13: Monte Carlo 1: Integration

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 2021

X

~ ( )X p x

Page 14: Monte Carlo 1: Integration

CS348b Lecture 6

Discrete Probability Distributions

Discrete values

with probability

Pat Hanrahan / Matt Pharr, Spring 2021

0ip ≥

11

n

iip

=

=∑

ip

ix

ipix

Page 15: Monte Carlo 1: Integration

CS348b Lecture 6

Discrete Probability Distributions

Cumulative PDF

Pat Hanrahan / Matt Pharr, Spring 2021

=

=∑1

j

j ii

P p

1

0

jP

≤ ≤0 1iP

=1nP

jP

Page 16: Monte Carlo 1: Integration

CS348b Lecture 6

Discrete Probability Distributions

Construction of samples

To randomly select an event,

Select if

Pat Hanrahan / Matt Pharr, Spring 2021

U

1

0

Uniform random variable

3x

ix<latexit sha1_base64="yRBqiuPNTVsPiSCrR70z+Hod6JE=">AAAB/nicbVDLSsNAFJ3UV62vqLhyM1gEN5akiLpwUXDTZQT7gCaEyfSmHTp5MDMRSyj4K25cKOLW73Dn3zhts9DWAxcO59zLvfcEKWdSWda3UVpZXVvfKG9WtrZ3dvfM/YO2TDJBoUUTnohuQCRwFkNLMcWhmwogUcChE4xup37nAYRkSXyvxil4ERnELGSUKC355pHj5+zcnuAb7D4y7HLAjs98s2rVrBnwMrELUkUFHN/8cvsJzSKIFeVEyp5tpcrLiVCMcphU3ExCSuiIDKCnaUwikF4+O3+CT7XSx2EidMUKz9TfEzmJpBxHge6MiBrKRW8q/uf1MhVeezmL00xBTOeLwoxjleBpFrjPBFDFx5oQKpi+FdMhEYQqnVhFh2AvvrxM2vWafVmr311UG80ijjI6RifoDNnoCjVQEzmohSjK0TN6RW/Gk/FivBsf89aSUcwcoj8wPn8A8A+UNg==</latexit>

Pi�1 < ⇠ Pi

Page 17: Monte Carlo 1: Integration

CS348b Lecture 6

Continuous Probability Distributions

PDF

CDF

Pat Hanrahan / Matt Pharr, Spring 2021

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 18: Monte Carlo 1: Integration

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 2021

X

1

0

( ) Pr( )P x X x= <

p(x)

P�1(x)

<latexit sha1_base64="poJU4zu/HQi3EuGMKW3Zj1hQG2k=">AAAB+HicbVBNS8NAEJ34WetHox69LBahHixJEfUiFLz0WMF+QBvLZrtpl242YXcj1tBf4sWDIl79Kd78N27bHLT1wcDjvRlm5vkxZ0o7zre1srq2vrGZ28pv7+zuFez9g6aKEklog0Q8km0fK8qZoA3NNKftWFIc+py2/NHN1G89UKlYJO70OKZeiAeCBYxgbaSeXWija1S/T8/cSan7yE57dtEpOzOgZeJmpAgZ6j37q9uPSBJSoQnHSnVcJ9ZeiqVmhNNJvpsoGmMywgPaMVTgkCovnR0+QSdG6aMgkqaERjP190SKQ6XGoW86Q6yHatGbiv95nUQHV17KRJxoKsh8UZBwpCM0TQH1maRE87EhmEhmbkVkiCUm2mSVNyG4iy8vk2al7F6UK7fnxWotiyMHR3AMJXDhEqpQgzo0gEACz/AKb9aT9WK9Wx/z1hUrmzmEP7A+fwB6yJG1</latexit>

X = P�1(⇠)<latexit sha1_base64="MqtDJzWRv1yXB1W2anb4Ko1ap9c=">AAAB6nicbVBNS8NAEJ34WetX1aOXxSJ4KkkR9Vjw0mNF+wFtKJvtpl262YTdiVhCf4IXD4p49Rd589+4bXPQ1gcDj/dmmJkXJFIYdN1vZ219Y3Nru7BT3N3bPzgsHR23TJxqxpsslrHuBNRwKRRvokDJO4nmNAokbwfj25nffuTaiFg94CThfkSHSoSCUbTSfe9J9Etlt+LOQVaJl5My5Gj0S1+9QczSiCtkkhrT9dwE/YxqFEzyabGXGp5QNqZD3rVU0YgbP5ufOiXnVhmQMNa2FJK5+nsio5ExkyiwnRHFkVn2ZuJ/XjfF8MbPhEpS5IotFoWpJBiT2d9kIDRnKCeWUKaFvZWwEdWUoU2naEPwll9eJa1qxbuqVO8uy7V6HkcBTuEMLsCDa6hBHRrQBAZDeIZXeHOk8+K8Ox+L1jUnnzmBP3A+fwBf/Y3i</latexit>

Page 19: Monte Carlo 1: Integration

Sampling a Circle

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2021

12 1 1 2 22

00 0 0 0 0

2rA r dr d r dr d

π ππ

θ θ θ π# $

= = = =% &' (

∫ ∫ ∫ ∫

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

dr

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

= ⇒ =

1( )21( )2

p

P

θπ

θ θπ

=

=

<latexit sha1_base64="QhPUEPNmTE6hSUJfFXkbVTcmmV8=">AAAB/XicbVDLSsNAFJ3UV62v+Ni5GSyCq5IUUTdCwU2XFewDmhAm00k7dDIJMzdiLcVfceNCEbf+hzv/xmmbhbYeuHA4517uvSdMBdfgON9WYWV1bX2juFna2t7Z3bP3D1o6yRRlTZqIRHVCopngkjWBg2CdVDESh4K1w+HN1G/fM6V5Iu9glDI/Jn3JI04JGCmwjzwYMCD4Glexl3LsPfDADeyyU3FmwMvEzUkZ5WgE9pfXS2gWMwlUEK27rpOCPyYKOBVsUvIyzVJCh6TPuoZKEjPtj2fXT/CpUXo4SpQpCXim/p4Yk1jrURyazpjAQC96U/E/r5tBdOWPuUwzYJLOF0WZwJDgaRS4xxWjIEaGEKq4uRXTAVGEggmsZEJwF19eJq1qxb2oVG/Py7V6HkcRHaMTdIZcdIlqqI4aqIkoekTP6BW9WU/Wi/VufcxbC1Y+c4j+wPr8AVgfk+M=</latexit>

✓ = 2⇡⇠1

2

( ) 2( )p r rP r r

=

=

<latexit sha1_base64="TzYaKJxlhHxzmdNgUllm8hDFUxM=">AAAB+HicbVBNS8NAEJ3Ur1o/GvXoZbEInkpSRL0IBS89VrAf0Iaw2W7apZtN3N2INfSXePGgiFd/ijf/jds2B219MPB4b4aZeUHCmdKO820V1tY3NreK26Wd3b39sn1w2FZxKgltkZjHshtgRTkTtKWZ5rSbSIqjgNNOML6Z+Z0HKhWLxZ2eJNSL8FCwkBGsjeTbZYmuUV/dS436j8yv+XbFqTpzoFXi5qQCOZq+/dUfxCSNqNCEY6V6rpNoL8NSM8LptNRPFU0wGeMh7RkqcESVl80Pn6JTowxQGEtTQqO5+nsiw5FSkygwnRHWI7XszcT/vF6qwysvYyJJNRVksShMOdIxmqWABkxSovnEEEwkM7ciMsISE22yKpkQ3OWXV0m7VnUvqrXb80q9kcdRhGM4gTNw4RLq0IAmtIBACs/wCm/Wk/VivVsfi9aClc8cwR9Ynz9sOJJR</latexit>

r =p⇠2

Page 20: Monte Carlo 1: Integration

Sampling a Circle

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2021

RIGHT = Equi-ArealWRONG ≠ Equi-Areal

<latexit sha1_base64="TzYaKJxlhHxzmdNgUllm8hDFUxM=">AAAB+HicbVBNS8NAEJ3Ur1o/GvXoZbEInkpSRL0IBS89VrAf0Iaw2W7apZtN3N2INfSXePGgiFd/ijf/jds2B219MPB4b4aZeUHCmdKO820V1tY3NreK26Wd3b39sn1w2FZxKgltkZjHshtgRTkTtKWZ5rSbSIqjgNNOML6Z+Z0HKhWLxZ2eJNSL8FCwkBGsjeTbZYmuUV/dS436j8yv+XbFqTpzoFXi5qQCOZq+/dUfxCSNqNCEY6V6rpNoL8NSM8LptNRPFU0wGeMh7RkqcESVl80Pn6JTowxQGEtTQqO5+nsiw5FSkygwnRHWI7XszcT/vF6qwysvYyJJNRVksShMOdIxmqWABkxSovnEEEwkM7ciMsISE22yKpkQ3OWXV0m7VnUvqrXb80q9kcdRhGM4gTNw4RLq0IAmtIBACs/wCm/Wk/VivVsfi9aClc8cwR9Ynz9sOJJR</latexit>

r =p⇠2

<latexit sha1_base64="g8vThQKXbofRrgUwfm7vfror8IY=">AAAB8HicbVBNSwMxEJ2tX7V+VT16CRbBU9ktol6EgpceK9gPaZeSTbNtaJJdkqxYlv4KLx4U8erP8ea/MdvuQVsfDDzem2FmXhBzpo3rfjuFtfWNza3idmlnd2//oHx41NZRoghtkYhHqhtgTTmTtGWY4bQbK4pFwGknmNxmfueRKs0ieW+mMfUFHkkWMoKNlR4UukH9JzaoDcoVt+rOgVaJl5MK5GgOyl/9YUQSQaUhHGvd89zY+ClWhhFOZ6V+ommMyQSPaM9SiQXVfjo/eIbOrDJEYaRsSYPm6u+JFAutpyKwnQKbsV72MvE/r5eY8NpPmYwTQyVZLAoTjkyEsu/RkClKDJ9agoli9lZExlhhYmxGJRuCt/zyKmnXqt5ltXZ3Uak38jiKcAKncA4eXEEdGtCEFhAQ8Ayv8OYo58V5dz4WrQUnnzmGP3A+fwCND4+e</latexit>

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⇠1

Page 21: Monte Carlo 1: Integration

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 22: Monte Carlo 1: Integration

Rejection Sampling

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2021

Area of circle / Area of square

Efficiency?

do{

X=Uniform(-1,1)

Y=Uniform(-1,1)

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

Page 23: Monte Carlo 1: Integration

Sampling 2D Directions

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2021

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 24: Monte Carlo 1: Integration

Computing Area of a Circle

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2021

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 25: Monte Carlo 1: Integration

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 2021

Page 26: Monte Carlo 1: Integration

Unbiased Estimator

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2021

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 27: Monte Carlo 1: Integration

Direct Lighting: Hemispherical Integral

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2021

dA

θdω

2

( ) ( , ) cosH

E x L x dω θ ω= ∫

ω( , )iL x

Page 28: Monte Carlo 1: Integration

Direct Lighting: Solid Angle Sampling

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2021

2

( ) 1H

p dω ω =∫

Sample hemisphere uniformly

dA

θdω

ω( , )iL x

Page 29: Monte Carlo 1: Integration

Direct Lighting: Solid Angle Sampling

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2021

Estimator

dA

θdω

ω( , )iL x

Page 30: Monte Carlo 1: Integration

Direct Lighting: Hemisphere Sampling

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2021

Hemisphere

16 shadow rays

Page 31: Monte Carlo 1: Integration

Direct Lighting: Area Integral

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2021

A!x!

x

θθ "

0 blocked( , )

1 visibleV x x

!" = #

$Radiance

Visibility

Integral

Page 32: Monte Carlo 1: Integration

Direct Lighting: Area Integral

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2021

A!x!

x

θθ "

Page 33: Monte Carlo 1: Integration

Direct Lighting: Area Sampling

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2021

A!x!

x

θθ "

Sample shape uniformly by area

Page 34: Monte Carlo 1: Integration

Direct Lighting: Area Sampling

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2021

A!

ω"

x!

x

θθ " Estimator

Page 35: Monte Carlo 1: Integration

Direct Lighting: Area Sampling

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2021

Area

16 shadow rays

Page 36: Monte Carlo 1: Integration

Random Sampling Introduces Noise

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2021

1 shadow ray per eye ray

Center Random

Page 37: Monte Carlo 1: Integration

Quality Improves with More Rays

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2021

16 shadow rays1 shadow ray

Area Area

Page 38: Monte Carlo 1: Integration

Why is Area Better than Hemisphere?

CS348b Lecture 6 Pat Hanrahan / Matt Pharr, Spring 2021

Hemisphere

16 shadow rays

Area

16 shadow rays