Rendering: Monte Carlo Integration II Bernhard Kerbl Research - - PowerPoint PPT Presentation

โ–ถ
rendering monte carlo integration ii
SMART_READER_LITE
LIVE PREVIEW

Rendering: Monte Carlo Integration II Bernhard Kerbl Research - - PowerPoint PPT Presentation

Rendering: Monte Carlo Integration II Bernhard Kerbl Research Division of Computer Graphics Institute of Visual Computing & Human-Centered Technology TU Wien, Austria With slides based on material by Jaakko Lehtinen, used with


slide-1
SLIDE 1

Rendering: Monte Carlo Integration II

Bernhard Kerbl

Research Division of Computer Graphics Institute of Visual Computing & Human-Centered Technology TU Wien, Austria

With slides based on material by Jaakko Lehtinen, used with permission

เถฑ

slide-2
SLIDE 2

Integrating the cosine-weighted radiance ๐‘€๐‘—(๐‘ฆ, ๐œ•) at a point ๐‘ฆ Integral of the light function

  • ver the hemisphere, w.r.t.

direction/solid angle at ๐œ• Letโ€™s find a solution!

How do we integrate over the hemisphere? How do we do it smartly?

Todayโ€™s Goal

Rendering โ€“ Monte Carlo Integration I 2

slide-3
SLIDE 3

Sampling a Unit Disk

Imagine we have a disk-shaped surface with radius ๐‘  = 1 that registers incoming light (color) from directional light sources As an exercise, we want to approximate the total incoming light over the diskโ€™s surface area We integrate over an area of size ๐œŒ We will use the Monte Carlo integral for that

Rendering โ€“ Monte Carlo Integration II 3

2 2 r = 1

slide-4
SLIDE 4

Uniformly Sampling the Unit Disk

If we can manage to uniformly sample the disk, then we can compute the Monte Carlo integral as a simple average ร— ๐œŒ By drawing uniform samples in ๐‘ฆ and ๐‘ง, we cannot cover the area precisely Inscribed square: information lost Circumscribed square: unnecessary samples

Rendering โ€“ Monte Carlo Integration II 4

slide-5
SLIDE 5

Uniformly Sampling the Unit Disk

If we can manage to uniformly sample the disk, then we can compute the Monte Carlo integral as a simple average By drawing uniform samples in ๐‘ฆ and ๐‘ง, we cannot cover the area precisely Inscribed square: information lost Circumscribed square: unnecessary samples

Rendering โ€“ Monte Carlo Integration II 5

slide-6
SLIDE 6

Uniformly Sampling the Unit Disk

If we can manage to uniformly sample the disk, then we can compute the Monte Carlo integral as a simple average By drawing uniform samples in ๐‘ฆ and ๐‘ง, we cannot cover the area precisely Inscribed square: information lost Circumscribed square: unnecessary samples

Rendering โ€“ Monte Carlo Integration II 6

slide-7
SLIDE 7

If we can manage to uniformly sample the disk, then we can compute the Monte Carlo integral as a simple average By drawing uniform samples in ๐‘ฆ and ๐‘ง, we cannot cover the area precisely Inscribed square: information lost Circumscribed square: unnecessary samples

This is actually somewhat ok!

Uniformly Sampling the Unit Disk

Rendering โ€“ Monte Carlo Integration II 7

slide-8
SLIDE 8

Rejection Sampling

Requires a PDF ๐‘ž๐‘Œ(๐‘ฆ) and a constant ๐‘‘ such that ๐‘” ๐‘ฆ < ๐‘‘๐‘ž๐‘Œ(๐‘ฆ) Draw ๐œŠ๐‘— and ๐‘Œ๐‘— from their respective distributions. If the point (๐‘Œ๐‘—, ๐œŠ๐‘—๐‘‘๐‘ž๐‘Œ ๐‘Œ๐‘— ) lies under ๐‘” ๐‘ฆ , then the sample is accepted

loop forever: sample ๐‘Œ from ๐‘ž๐‘Œโ€™s distribution if ๐œŠ๐‘— โ‹… ๐‘‘๐‘ž๐‘Œ ๐‘Œ๐‘— < ๐‘” ๐‘Œ๐‘— then return ๐‘Œ๐‘—

Rendering โ€“ Monte Carlo Integration II 8

slide-9
SLIDE 9

Rejection Sampling

Requires a PDF ๐‘ž๐‘Œ(๐‘ฆ) and a constant ๐‘‘ such that ๐‘” ๐‘ฆ < ๐‘‘๐‘ž๐‘Œ(๐‘ฆ) Draw ๐œŠ๐‘— and ๐‘Œ๐‘— from their respective distributions. If the point (๐‘Œ๐‘—, ๐œŠ๐‘—๐‘‘๐‘ž๐‘Œ ๐‘Œ๐‘— ) lies under ๐‘” ๐‘ฆ , then the sample is accepted

loop forever: sample ๐‘Œ from ๐‘ž๐‘Œโ€™s distribution if ๐œŠ๐‘— โ‹… ๐‘‘๐‘ž๐‘Œ ๐‘Œ๐‘— < ๐‘” ๐‘Œ๐‘— then return ๐‘Œ๐‘—

Unit disk: ๐‘” ๐‘ฆ, ๐‘ง = เต1 ๐‘—๐‘” ( ๐‘ฆ2 + ๐‘ง2 โ‰ค 1) ๐‘๐‘ขโ„Ž๐‘“๐‘ ๐‘ฅ๐‘—๐‘ก๐‘“ , ๐‘ž ๐‘ฆ, ๐‘ง = 1

4 , ๐‘‘ = 4

Rendering โ€“ Monte Carlo Integration II 9

slide-10
SLIDE 10

Back to the Unit Disk

We do not want to waste samples if we can avoid it Instead, find a way to generate uniform samples on the disk Second attempt: draw from 2D polar coordinates

Polar coordinates defined by radius ๐‘  โˆˆ [0,1) and angle ๐œ„ โˆˆ [0,2๐œŒ) Transformation to cartesian coordinates: ๐‘ฆ = ๐‘  sin ๐œ„ y = ๐‘  cos ๐œ„

Rendering โ€“ Monte Carlo Integration II 10

slide-11
SLIDE 11

Uniformly Sampling the Unit Disk?

Convert two ๐œŠ to ranges 0, 1 , [0,2๐œŒ) for polar coordinates Convert to cartesian coordinates

Rendering โ€“ Monte Carlo Integration II 11

void sampleUnitDisk() { std::default_random_engine r_rand_eng(0xdecaf); std::default_random_engine theta_rand_eng(0xcaffe); std::uniform_real_distribution<double> uniform_dist(0.0, 1.0); for (int i = 0; i < NUM_SAMPLES; i++) { auto r = uniform_dist(r_rand_eng); auto theta = uniform_dist(theta_rand_eng) * 2 * M_PI; auto x = r * sin(theta); auto y = r * cos(theta); samples2D[i] = std::make_pair(x, y); } }

slide-12
SLIDE 12

We successfully sampled the unit disk in the proper range However, the distribution is not uniform with respect to the area Samples clump together at center Averaging those samples will give us a skewed result for the integral!

Clumping

  • 1
  • 0,5

0,5 1

  • 1
  • 0,5

0,5 1 Rendering โ€“ Monte Carlo Integration II 12

slide-13
SLIDE 13

Uniformly Sampling the Unit Disk: A Solution

The area of a disk is proportional to ๐‘ 2, times a constant factor ๐œŒ If we see the disk as concentric rings of width ฮ”๐‘ , the ๐‘˜ inner rings up to radius ๐‘ 

๐‘˜ = ๐‘˜ฮ”๐‘  should contain ๐‘ ๐‘˜ ๐‘  2

๐‘‚ out of ๐‘‚ total samples Conversely, the ๐‘—๐‘ขโ„Ž sample should lie in the ring at radius ๐‘ ๐‘— = ๐‘ 

๐‘— ๐‘‚

Since ๐œŠ is uniform in [0, 1), we can switch ๐‘˜

๐‘‚ for ๐œŠ to get ๐‘ ๐‘— = ๐‘  ๐œŠ๐‘—

Rendering โ€“ Monte Carlo Integration II 13

slide-14
SLIDE 14

Uniformly Sampling the Unit Disk: A Solution

It works, and it is not even a bad way to arrive at the correct solution However, for more complex scenarios, we might struggle to find the solution so easily With the tools we introduced earlier, we can formalize this process for arbitrary setups

Rendering โ€“ Monte Carlo Integration II 14

  • 1
  • 0,5

0,5 1

  • 1
  • 0,5

0,5 1

  • 1
  • 0,5

0,5 1

  • 1
  • 0,5

0,5 1

slide-15
SLIDE 15

Letโ€™s transform a regular grid from polar to cartesian coordinates

Polar To Cartesian Coordinates

Rendering โ€“ Monte Carlo Integration II 15

๐‘Œ = ๐‘  cos(๐œ„), ๐‘ = ๐‘  sin(๐œ„) ๐‘  ๐œ„

slide-16
SLIDE 16

Take 100k samples, transform and see which box they end up in

First Attempt to Learn the PDF

Rendering โ€“ Monte Carlo Integration II 16

๐‘Œ = ๐œŠ1 cos(2๐œŒ๐œŠ2), ๐‘ = ๐œŠ1 sin(2๐œŒ๐œŠ2) ฮพ1, ฮพ2

slide-17
SLIDE 17

Knowing the PDF

If we know the effect of a transformation ๐‘ˆ on the PDF, we can

Use it in the Monte Carlo integral to weight our samples, or Compensate to get a uniform sampling method after transformation

Rendering โ€“ Monte Carlo Integration II 17

๐ฝ๐‘œ๐‘ž๐‘ฃ๐‘ข (๐œŠ1, ๐œŠ2) ๐ท๐‘๐‘ ๐‘ข๐‘“๐‘ก๐‘—๐‘๐‘œ (๐‘ฆ, ๐‘ง) ๐‘„๐‘๐‘š๐‘๐‘  (r, ๐œ„)

๐‘ˆ(๐‘ , ๐œ„)

slide-18
SLIDE 18

Computing the PDF after a Transformation

Assume a random variable ๐ต and a bijective transformation ๐‘ˆ that yields another variable ๐ถ = ๐‘ˆ ๐ต Bijectivity dictates that ๐‘ = ๐‘ˆ(๐‘) must be either monotonically increasing or decreasing with ๐‘ This implies that there is a unique ๐ถi for every ๐ตi, and vice versa In this case, the CDFs for the two variables fulfill ๐‘„๐ถ ๐‘ˆ(๐‘) = ๐‘„

๐ต(๐‘)

Rendering โ€“ Monte Carlo Integration II 18

slide-19
SLIDE 19

Computing the PDF after a Transformation

If ๐‘ = ๐‘ˆ(๐‘) and ๐‘ increases with ๐‘, we have:

๐‘’๐‘„๐ถ(๐‘) ๐‘’๐‘

= ๐‘’๐‘„๐ต(๐‘)

๐‘’๐‘

If ๐‘ decreases with ๐‘ (e.g. ๐‘ = โˆ’๐‘), we have: โˆ’

๐‘’๐‘„๐ถ(๐‘) ๐‘’๐‘

= ๐‘’๐‘„๐ต(๐‘)

๐‘’๐‘

Since ๐‘ž๐ถ is the non-negative derivative of ๐‘„๐ถ, we can rewrite as: ๐‘ž๐ถ ๐‘ ๐‘’๐‘ ๐‘’๐‘ = ๐‘ž๐ต ๐‘ , ๐‘ž๐ถ ๐‘ = ๐‘’๐‘ ๐‘’๐‘

โˆ’1

๐‘ž๐ต ๐‘

Rendering โ€“ Monte Carlo Integration II 19

๐‘‰๐‘ก๐‘—๐‘œ๐‘•: ๐‘’๐‘„

๐‘Œ ๐‘ฆ

๐‘’๐‘ง = ๐‘ž๐‘Œ ๐‘ฆ ๐‘’๐‘ฆ ๐‘’๐‘ง

slide-20
SLIDE 20

Computing the PDF after a Transformation

Letโ€™s interpret ๐‘ž๐ถ ๐‘ =

๐‘’๐‘ ๐‘’๐‘ โˆ’1

๐‘ž๐ต ๐‘ It is the probability density of ๐ต, multiplied by

๐‘’๐‘ ๐‘’๐‘ โˆ’1 ๐‘’๐‘ ๐‘’๐‘ โˆ’1

has two intuitive interpretations: the change in sampling density at point ๐‘ if we transform ๐‘ by ๐‘ˆ

  • r,

the inverse change in the volume of an infinitesimal hypercube at point ๐‘ if we transform ๐‘ by ๐‘ˆ

Rendering โ€“ Monte Carlo Integration II 20

slide-21
SLIDE 21

Multidimensional Transformations

If we try to apply the above to the unit disk, we fail at ๐‘ฆ = ๐‘  sin ๐œ„ We canโ€™t evaluate

๐‘’๐‘ฆ ๐‘’๐‘  โˆ’1

: the transformation that produces one target variable is dependent on both input variables and vice-versa We cannot compute the change in the PDF between individual variables, we must take them all into account simultaneously Itโ€™s matrix time

Rendering โ€“ Monte Carlo Integration II 21

slide-22
SLIDE 22

Multidimensional Transformations

We write the set of ๐‘‚ values from a multidimensional variable ิฆ ๐ต as a vector ิฆ ๐‘ and the ๐‘‚ outputs of transformation ๐‘ˆ as a vector ๐‘: ิฆ ๐‘ = ๐‘1 โ‹ฎ ๐‘๐‘‚ , ๐‘ = ๐‘1 โ‹ฎ ๐‘๐‘‚ = ๐‘ˆ

1( ิฆ

๐‘) โ‹ฎ ๐‘ˆ๐‘‚( ิฆ ๐‘) = ๐‘ˆ( ิฆ ๐‘) Instead of quantifying the change in volume incurred by ๐‘ˆ ๐‘ ,

๐‘’๐‘ˆ ๐‘ ๐‘’๐‘

, our goal is now to quantify the change incurred by ๐‘ˆ ิฆ ๐‘

Rendering โ€“ Monte Carlo Integration II 22

slide-23
SLIDE 23

The Jacobian Matrix

For a transformation ๐‘ = ๐‘ˆ( ิฆ ๐‘), we can define the Jacobian matrix that contains all ๐‘

๐‘˜, ๐‘๐‘— combinations of partial differentials

๐พ๐‘ˆ( ิฆ ๐‘) =

๐œ–๐‘1 ๐œ–๐‘1

โ‹ฏ

๐œ–๐‘1 ๐œ–๐‘๐‘‚

โ‹ฎ โ‹ฑ โ‹ฎ

๐œ–๐‘๐‘ ๐œ–๐‘1

โ‹ฏ

๐œ–๐‘๐‘ ๐œ–๐‘๐‘‚

If we consider ิฆ ๐ตโ€™s domain as a space with ๐‘‚ axes, ๐พ๐‘ˆ( ิฆ ๐‘) gives the change of the edges of an infinitesimal hypercube from ิฆ ๐‘ to ๐‘ˆ( ิฆ ๐‘)

Rendering โ€“ Monte Carlo Integration II 23

slide-24
SLIDE 24

The Jacobian Matrix, Visualized

Change of edges of an infinitesimal hypercube with extent 1,1

๐พ๐‘ˆ( ิฆ ๐‘) = ๐œ–๐‘1 ๐œ–๐‘1 โ‹ฏ ๐œ–๐‘1 ๐œ–๐‘๐‘‚ โ‹ฎ โ‹ฑ โ‹ฎ ๐œ–๐‘๐‘‚ ๐œ–๐‘1 โ‹ฏ ๐œ–๐‘๐‘‚ ๐œ–๐‘๐‘‚

Rendering โ€“ Monte Carlo Integration II 24

๐‘ ิฆ ๐‘

1 ๐œ–๐‘1 ๐œ–๐‘1 ๐œ–๐‘2 ๐œ–๐‘1 1 ๐œ–๐‘1 ๐œ–๐‘2 ๐œ–๐‘2 ๐œ–๐‘2

slide-25
SLIDE 25

The Jacobian

The columns of a square matrix ๐‘ can be interpreted as the natural base vectors of a space 1 โ‹ฎ , 1 โ‹ฎ if they were transformed by ๐‘ The determinant ๐‘ of ๐‘ computes the volume

  • f a parallelepiped spanned by these vectors[3]

๐พ๐‘ˆ , called the Jacobian of ๐‘ˆ, gives the volume change at ิฆ ๐‘ by ๐‘ˆ

Rendering โ€“ Monte Carlo Integration II 25

๐‘

๐‘ฒ๐‘ผ ๐’ƒ

slide-26
SLIDE 26

Computing the PDF of a Transformation

Letโ€™s try polar coordinates again: ๐‘ฆ ๐‘ง = ๐‘ˆ ๐‘  ๐œ„ = ๐‘  sin ๐œ„ ๐‘  cos ๐œ„

๐œ–๐‘ˆ ๐‘  ๐œ„ ๐œ– ๐‘  ๐œ„

= ๐พ๐‘ˆ =

๐œ–๐‘ฆ ๐œ–๐‘  ๐œ–๐‘ฆ ๐œ–๐œ„ ๐œ–๐‘ง ๐œ–๐‘  ๐œ–๐‘ง ๐œ–๐œ„

= cos ๐œ„ โˆ’๐‘  sin ๐œ„ sin ๐œ„ ๐‘  cos ๐œ„ = r ๐‘ž ๐‘ฆ, ๐‘ง =

๐‘ž(๐‘ ,๐œ„) ๐‘ 

, or ๐‘ž ๐‘ , ๐œ„ = ๐‘  ๐‘ž ๐‘ฆ, ๐‘ง : a uniform density in ๐‘ฆ, ๐‘ง must be proportional to ๐‘  in (๐‘ , ๐œ„)

Rendering โ€“ Monte Carlo Integration II 26

slide-27
SLIDE 27

Compare PDFs After Transformation

Rendering โ€“ Monte Carlo Integration II 27

Measured Using ๐‘ž ๐‘ , ๐œ„ =

1 2๐œŒ and ๐‘ž ๐‘ฆ, ๐‘ง = ๐‘ž(๐‘ ,๐œ„) ๐‘ 

slide-28
SLIDE 28

How To Visualize the Computed PDF Conversion?

Python code, for the interested

Rendering โ€“ Monte Carlo Integration II 28

num_bins = 20 xlist = np.linspace(-1.0, 1.0, num_bins) ylist = np.linspace(-1.0, 1.0, num_bins) bin_volume = 4 / (num_bins*num_bins) X, Y = np.meshgrid(xlist, ylist) X += 1/num_bins Y += 1/num_bins r = np.sqrt(X*X+Y*Y) uniform_dist = 1/(2 * np.pi) pdf_transform = 1/r Z = uniform_dist * pdf_transform * bin_volume Z[r>1] = 0 fig,ax = plt.subplots(1,1) cp = plt.pcolor(X, Y, Z,cmap='viridis',norm=matplotlib.colors.LogNorm()) fig.colorbar(cp) # Add a colorbar to a plot plt.show()

slide-29
SLIDE 29

Sampling Joint PDFs Correctly

For independent variables, the joint PDF ๐‘ž(๐‘ฆ, ๐‘ง, โ€ฆ ) is ๐‘ž๐‘Œ ๐‘ฆ ๐‘ž๐‘ ๐‘ง โ€ฆ In general, this is an assumption that we should not rely on Furthermore, after a transformation, only the joint PDF is known The proper way to sample multiple variables ๐‘Œ, ๐‘ is to compute

the marginal density function ๐‘ž๐‘Œ ๐‘ฆ of one the conditional density function ๐‘ž๐‘ ๐‘ง|๐‘ฆ of the other

Rendering โ€“ Monte Carlo Integration II 29

slide-30
SLIDE 30

Marginal and Conditional Density Function

Assume we have obtained the joint PDF ๐‘ž ๐‘ฆ, ๐‘ง of variables ๐‘Œ, ๐‘ with ranges [๐‘๐‘Œ, ๐‘๐‘Œ) and [๐‘๐‘, ๐‘๐‘) In a 2D domain with ๐‘Œ, ๐‘ we can think of ๐‘ž๐‘Œ ๐‘ฆ as the average density of ๐‘ž ๐‘ฆ, ๐‘ง at a given ๐‘ฆ over all possible values ๐‘ง We can obtain the marginal density function for one of them by integrating out all the others, e.g.: ๐‘ž๐‘Œ ๐‘ฆ = ืฌ

๐‘๐‘ ๐‘๐‘ ๐‘ž ๐‘ฆ, ๐‘ง ๐‘’๐‘ง

We can then find ๐‘ž ๐‘ง|๐‘ฆ =

๐‘ž(๐‘ฆ,๐‘ง) ๐‘ž๐‘Œ(๐‘ฆ)

Rendering โ€“ Monte Carlo Integration II 30

slide-31
SLIDE 31

Adding More Variables

What to do for multiple variables, e.g. ๐‘Œ, ๐‘ and ๐‘Ž?

Find first marginal density ๐‘ž๐‘Œ ๐‘ฆ = ืฌ

๐‘๐‘Ž ๐‘๐‘Ž ืฌ ๐‘๐‘ ๐‘๐‘ ๐‘ž ๐‘ฆ, ๐‘ง, ๐‘จ ๐‘’๐‘ง ๐‘’๐‘จ

Find first conditional density ๐‘ž๐‘Œ ๐‘ง, ๐‘จ|๐‘ฆ = ๐‘ž ๐‘ฆ,๐‘ง,๐‘จ

๐‘ž๐‘Œ ๐‘ฆ

Find second marginal density ๐‘ž๐‘ ๐‘ง|๐‘ฆ = ืฌ

๐‘๐‘Ž ๐‘๐‘Ž ๐‘ž ๐‘ฆ, ๐‘ง, ๐‘จ ๐‘’๐‘จ

Find second conditional density ๐‘ž๐‘Œ ๐‘จ|๐‘ฆ, ๐‘ง = ๐‘ž ๐‘ง,๐‘จ|๐‘ฆ

๐‘ž๐‘ ๐‘ง|๐‘ฆ

Integrate + invert first marginal, first and second conditional densities Sample each of them Extend ad libitum to even more variables

Rendering โ€“ Monte Carlo Integration II 31

slide-32
SLIDE 32

Sampling the Unit Disk: The Formal Solution

The size of the sampling domain in cartesian coordinates is ๐œŒ Since we want uniform sampling and sample probabilities must integrate to 1, the PDF in cartesian coordinates is ๐‘ž ๐‘ฆ, ๐‘ง =

1 ๐œŒ

We know that ๐‘ž ๐‘ , ๐œ„ = ๐‘  ๐‘ž ๐‘ฆ, ๐‘ง , so we want ๐‘ž ๐‘ , ๐œ„ =

๐‘  ๐œŒ

๐‘ž๐‘† ๐‘  = ืฌ

2๐œŒ ๐‘ž ๐‘ , ๐œ„ ๐‘’๐œ„ = 2๐‘  and ๐‘ž ๐œ„|๐‘  = ๐‘ž(๐‘ ,๐œ„) ๐‘ž๐‘†(๐‘ ) = 1 2๐œŒ

Rendering โ€“ Monte Carlo Integration II 32

slide-33
SLIDE 33

Sampling the Unit Disk: The Formal Solution

If we draw samples for our ๐‘ , ๐œ„ with the above PDFs, we get uniform distribution in (๐‘ฆ, ๐‘ง) after applying transformation ๐‘ˆ

Rendering โ€“ Monte Carlo Integration II 33

๐‘  ๐œ„

slide-34
SLIDE 34

Sampling the Unit Disk: The Formal Solution

Integrate marginal and conditional PDFs and invert: we get the same solution as before:

๐‘  = P

๐‘† โˆ’1 ๐œŠ1 =

๐œŠ1 ๐œ„ = ๐‘„ฮ˜

โˆ’1 ๐œŠ2 = 2๐œŒ๐œŠ2

๐‘ž ๐œ„|๐‘  is constant: no matter what radius we are looking at, all positions on a ring of that radius (angle) should be equally likely Final integral: ๐‘†๐ป๐ถ๐‘ข๐‘๐‘ข๐‘๐‘š =

๐œŒ ๐‘‚ ฯƒ๐‘—=1 ๐‘‚

๐‘†๐ป๐ถ(๐‘†๐‘— ๐‘ก๐‘—๐‘œ ฮ˜๐‘—, ๐‘†๐‘— ๐‘‘๐‘๐‘ก ฮ˜๐‘—)

Rendering โ€“ Monte Carlo Integration II 34

slide-35
SLIDE 35

Moving on to the Hemisphere

This took as a while, but we have seen all the formal procedures We only need to switch from integrating planar area to points ๐œ•

  • n hemisphere surface (i.e., vectors ๐‘ฆ, ๐‘ง, ๐‘จ with length 1)

Use spherical coordinates and bijective ๐‘ˆ from (๐‘ , ๐œ„, ๐œš) to ๐‘ฆ, ๐‘ง, ๐‘จ : ๐‘ฆ = ๐‘  sin ๐œ„ cos ๐œš ๐‘ง = ๐‘  sin ๐œ„ sin ๐œš ๐‘จ = ๐‘  cos ๐œ„

Rendering โ€“ Monte Carlo Integration II 35

slide-36
SLIDE 36

Deriving Integration Over Hemisphere

Each direction ๐œ• represents an infinitesimal surface area portion ๐‘’๐œ• How do we integrate a function ๐‘”(๐œ•) with differential ๐‘’๐œ•? Integration over points on hemisphere surface ๐œ•, w.r.t. (๐œ„, ๐œš)

Rendering โ€“ Monte Carlo Integration II 36

๐œ„ ๐œš

๐œ• ๐‘’๐œ•

๐‘œ

slide-37
SLIDE 37

Deriving Integration Over Hemisphere

We assume a planar surface with an upright facing normal ๐‘œ We use the integral intervals ๐œ„ โˆˆ 0,

๐œŒ 2 , ๐œš โˆˆ [0, 2๐œŒ)

I.e., a curve from perpendicular to parallel for ๐œ„, a ring for ๐œš

Rendering โ€“ Monte Carlo Integration II 37

2๐œŒ ๐œŒ 2

๐‘œ

slide-38
SLIDE 38

Deriving Integration Over Hemisphere

We can split the surface along ๐œ„ into ribbons of width ฮ”๐œ„ โ†’ ๐‘’๐œ„ The upper edge of the ribbon is slightly shorter than the lower If we keep adding more and more ribbons, this difference vanishes

Rendering โ€“ Monte Carlo Integration II 38

ฮ”๐œ„

slide-39
SLIDE 39

Deriving Integration Over Hemisphere

As a ribbonโ€™s width goes to ๐‘’๐œ„, its area becomes its length times ๐‘’๐œ„ We can find this length by projecting the ribbon to the ground Using trigonometry, we find the length of a ribbon is 2๐œŒsin ๐œ„

Rendering โ€“ Monte Carlo Integration II 39

slide-40
SLIDE 40

Deriving Integration Over Hemisphere

As a ribbonโ€™s width goes to ๐‘’๐œ„, its area becomes its length times ๐‘’๐œ„ We can find this length by projecting the ribbon to the ground Using trigonometry, we find the length of a ribbon is 2๐œŒsin ๐œ„

Rendering โ€“ Monte Carlo Integration II 40

sin ๐œ„

๐œ„

๐‘œ

slide-41
SLIDE 41

The length of a ribbon spans the entire interval ๐œš โˆˆ [0, 2๐œŒ) Convert the length to an integral over ๐‘’๐œš: 2๐œŒsin ๐œ„ = ืฌ

2๐œŒ sin ๐œ„ ๐‘’๐œš

The final integral: ืฌ

ฮฉ ๐‘” ๐œ• ๐‘’๐œ• = ืฌ

๐œŒ 2 ืฌ

2๐œŒ ๐‘”(๐œ•) sin ๐œ„ ๐‘’๐œš ๐‘’๐œ„

Deriving Integration Over Hemisphere

Rendering โ€“ Monte Carlo Integration II 41

๐œ„ ๐œš

๐œ• ๐‘’๐œ•

๐‘œ

slide-42
SLIDE 42

Deriving PDF for Hemisphere Sampling

Integral of ๐‘”(๐œ•) over area ฮ”๐œ• = ืฌ

ฮ”๐œ• ๐‘”(๐œ•) ๐‘’๐œ•

Integral of ๐‘”(๐œ•) w.r.t. (๐‘’๐œ„, ๐‘’๐œš) = ืฌ

ฮ”๐œ„ ืฌ ฮ”๐œš ๐‘” ๐œ• sin ๐œ„ ๐‘’๐œš ๐‘’๐œ„

Integration domain and ๐‘” ๐œ• are identical, thus: ๐‘’๐œ• = sin ๐œ„ ๐‘’๐œš ๐‘’๐œ„ ๐œ• โ†’ ๐œ„, ๐œš is bijective, we have ๐‘ž ๐œ„, ๐œš d๐œ„ ๐‘’๐œš = ๐‘ž ๐œ• ๐‘’๐œ• and: ๐‘ž ๐œ„, ๐œš = sin ๐œ„ ๐‘ž ๐œ•

Rendering โ€“ Monte Carlo Integration II 42

ฮ”๐œ• ฮ”๐œ„

slide-43
SLIDE 43

Deriving PDF for Hemisphere Sampling, Formal

Target distribution in ๐œ•, which is (๐‘ฆ, ๐‘ง, ๐‘จ) with x2 + ๐‘ง2 + ๐‘จ2 = 1 The transformation ๐‘ˆ from (๐‘ , ๐œ„, ๐œš) to ๐‘ฆ, ๐‘ง, ๐‘จ : ๐‘ฆ = ๐‘  sin ๐œ„ cos ๐œš ๐‘ง = ๐‘  sin ๐œ„ sin ๐œš ๐‘จ = ๐‘  cos ๐œ„ The Jacobian of the transformation ๐‘ˆ gives ๐พ๐‘ˆ = ๐‘ 2 sin ๐œ„ Hence, we have ๐‘ž ๐‘ , ๐œ„, ๐œš = ๐‘ 2 sin ๐œ„ ๐‘ž(๐‘ฆ, ๐‘ง, ๐‘จ) = 1 sin ๐œ„ ๐‘ž(๐œ•)

Rendering โ€“ Monte Carlo Integration II 43

slide-44
SLIDE 44

Uniformly Sampling the Unit Hemisphere

The domain, i.e., the unit hemisphere surface area, is 2๐œŒ. Uniformly sampling the domain over ๐œ• implies ๐‘ž ๐œ• =

1 2๐œŒ

Hence, since ๐‘ž ๐œ„, ๐œš = ๐‘ 2 sin ๐œ„ ๐‘ž ๐œ• , we want ๐‘ž ๐œ„, ๐œš =

sin ๐œ„ 2๐œŒ

Marginal density ๐‘žฮ˜(๐œ„): ืฌ

2๐œŒ ๐‘ž ๐œ„, ๐œš ๐‘’๐œš = sin ๐œ„

Conditional density ๐‘ž ๐œš|๐œ„ : ๐‘ž ๐œ„,๐œš

๐‘žฮ˜(๐œ„) = 1 2๐œŒ

Rendering โ€“ Monte Carlo Integration II 44

slide-45
SLIDE 45

Uniformly Sampling the Unit Hemisphere โ€“ Complete

Antiderivative of ๐‘žฮ˜(๐œ„): ืฌ sin ๐œ„ ๐‘’๐œ„ = 1 โˆ’ cos ๐œ„ (added constant 1) Antiderivative of ๐‘ž ๐œš|๐œ„ : ืฌ

1 2๐œŒ ๐‘’๐œš = ๐œš 2๐œŒ

Invert them to get ๐œ„ = cosโˆ’1 ๐œŠ1 (cos is symmetric), ๐œš = 2๐œŒ๐œŠ2 Apply transformation ๐‘ˆ on (๐œ„, ๐œš) to obtain uniformly distributed ๐œ• Done!

Rendering โ€“ Monte Carlo Integration II 45

slide-46
SLIDE 46

Arbitrary Hemispheres

The orientation of sampled hemispheres depends on surface normal Use the tangent, bitangent and normal vectors of the intersection ๐œ•๐‘ฅ๐‘๐‘ ๐‘š๐‘’ = ๐‘ฆ โ‹… ๐‘ข๐‘๐‘œ๐‘•๐‘“๐‘œ๐‘ข + ๐‘ง โ‹… ๐‘๐‘—๐‘ข๐‘๐‘œ๐‘•๐‘“๐‘œ๐‘ข + ๐‘จ โ‹… ๐‘œ๐‘๐‘ ๐‘›๐‘๐‘š Or, if available, use ToWorld transformation methods

Rendering โ€“ Monte Carlo Integration II 46

slide-47
SLIDE 47

Applications for Uniform Hemisphere Sampling

Diffuse lighting based on last lectureโ€™s insights with constant ๐‘”

๐‘ 

What do we use for ๐‘€๐‘—? Full rendering equation: next time This time: ambient occlusion, direct lighting

Rendering โ€“ Monte Carlo Integration II 47

slide-48
SLIDE 48

Consider all unblocked directions around ๐‘ฆ as indirect light sources, integrate ๐‘Š ๐‘ฆ, ๐‘ฆ + ๐›ฝ๐œ•

cos ๐œ„ ๐œŒ

  • ver directions ๐œ• around the normal

Limit ray length to ๐›ฝ, return free if no intersection closer than ๐›ฝ

Ambient Occlusion

Rendering โ€“ Monte Carlo Integration II 48

slide-49
SLIDE 49

Fine geometric details on objects are accentuated by the absence of ambient light due to the shadows cast by close-by geometry Integrate over directions ๐œ• on the unit hemisphere defined by point ๐‘ฆ, normal ๐‘œ ๐‘Š ๐‘ฆ, ๐‘ฆ + ๐›ฝ๐œ• = แ‰Š1 ๐‘—๐‘” ๐‘ฆ โ†’ ๐‘ฆ + ๐›ฝ๐œ• ๐‘”๐‘ ๐‘“๐‘“ 0 ๐‘—๐‘” ๐‘ฆ โ†’ (๐‘ฆ + ๐›ฝ๐œ•) ๐‘๐‘š๐‘๐‘‘๐‘™๐‘“๐‘’

Ambient Occlusion

Rendering โ€“ Monte Carlo Integration II 49

slide-50
SLIDE 50

Cosine-Weighted Importance Sampling

Recap: variance is low if the sampling function mimics the signal We use ๐‘”

๐‘  = 1 ๐œŒ for ambient occlusion, therefore the contribution of

signal samples varies mostly with cos ๐œ„ It would be best to apply importance sampling: use a sampling strategy for ๐œ•, such that ๐‘ž ๐œ• โˆ cos ๐œ„ We have gone through all the necessary steps. Try to solve this formally with the inversion method as an exercise!

Rendering โ€“ Monte Carlo Integration II 50

slide-51
SLIDE 51

Smart Cosine-Weighted Importance Sampling

Malleyโ€™s method: uniformly pick ๐‘ฆ, ๐‘ง samples on the unit disk Project them to the hemisphere surface ๐‘จ = 1 โˆ’ ๐‘ฆ2 โˆ’ ๐‘ง2 Done! Your samples are now distributed with ๐‘ž ๐œ• โˆ cos ๐œ„ Why does this work? Try to come up with your own proof!

Rendering โ€“ Monte Carlo Integration II 51

slide-52
SLIDE 52

We can use Monte Carlo integration to compute the direct illumination from light sources in the scene at a point ๐‘ฆ Naรฏve version: sample unit hemisphere uniformly, hoping to hit light sources Check closest hit for each direction ๐œ• Use ๐‘€๐‘— ๐‘ฆ, ๐œ• = เต๐‘€๐‘“

๐‘š

๐‘—๐‘” โ„Ž๐‘—๐‘ข ๐‘ ๐‘š๐‘—๐‘•โ„Ž๐‘ข ๐‘š ๐‘๐‘ขโ„Ž๐‘“๐‘ ๐‘ฅ๐‘—๐‘ก๐‘“

๐‘ฆ ๐‘œ

Finding Light

Rendering โ€“ Monte Carlo Integration II 52

๐‘š

slide-53
SLIDE 53

Sampling a Light Source, Revisited

A special kind of importance sampling: integrate over light sources! Use ๐‘Š ๐‘ฆ, ๐‘ง = แ‰Š1 ๐‘—๐‘” ๐‘ž๐‘๐‘ขโ„Ž ๐‘”๐‘ ๐‘๐‘› ๐‘ฆ ๐‘ข๐‘ ๐‘ง ๐‘—๐‘ก ๐‘ฃ๐‘œ๐‘๐‘š๐‘๐‘‘๐‘™๐‘“๐‘’ ๐‘๐‘ขโ„Ž๐‘“๐‘ ๐‘ฅ๐‘—๐‘ก๐‘“ Pick ๐‘ง on light, e.g. uniformly

cos ๐œ„๐‘ง ๐‘’๐ต๐‘ง ๐‘ 2

: change in volume of infinitesimal 2D hypercube at ๐‘ง projected onto ๐‘ฆโ€™s hemisphere

Rendering โ€“ Monte Carlo Integration II 53

slide-54
SLIDE 54

Sampling a Light Source, Revisited

For inclusion of simple material color and BRDF, use ๐‘”

๐‘  = ๐œ ๐œŒ

Works extremely well if the light occupies only a small solid angle

Rendering โ€“ Monte Carlo Integration II 54 100 samples per pixel, hemisphere sampling 100 samples per pixel, light source sampling

slide-55
SLIDE 55

Hemisphere Sampling Illustrated

Rendering โ€“ Monte Carlo Integration II 55

slide-56
SLIDE 56

Light Source Sampling Illustrated

Rendering โ€“ Monte Carlo Integration II 56

slide-57
SLIDE 57

Monte Carlo Integration

But where does the actual integration step happen? In the basic case, directly in the main sampling loop for each pixel!

Static scene: Samples through pixels ๐‘ž always hit the same point ๐‘ฆ Once ๐‘ฆ has been hit, the sampling of its hemisphere follows PDF Return sampled values (colors), weighted by the corresponding PDF

Use ๐‘‚ samples for ๐‘ž, sum color values weighted by PDF, average:

Rendering โ€“ Monte Carlo Integration II 57

๐บ๐‘‚ = 1 ๐‘‚ เท

๐‘—=1 ๐‘‚ ๐‘€๐‘—(๐‘ฆ, ๐œ•)

๐‘ž(๐œ•)

slide-58
SLIDE 58

Monte Carlo Integration as Loop Over Pixel Samples

We achieve integration around ๐‘ฆ with multiple samples through ๐‘ž

A bit wasteful, but is a general, valid solution We will see in a second why this is convenient

Weight returned values by PDF, sum up and divide by ๐‘‚

Rendering โ€“ Monte Carlo Integration II 58

๐‘ฆ ๐‘ž

slide-59
SLIDE 59

Monte Carlo Integration as Loop Over Pixel Samples

Given: camera, pixel p, scene, pdf rgb = {0,0,0} for i in [0, N) do ray = rayThroughPixel(camera, p) x = findIntersection(ray, scene) color = getIntegratorValue(x) rgb += color end for rgb /= N function getIntegratorValue(x)

  • mega = getDirectionOnHemisphere(x, pdf)

Li = evaluateLight(x, omega, scene) return Li / pdf(omega)

Rendering โ€“ Monte Carlo Integration II 59

1 ๐‘‚ เท

๐‘—=1 ๐‘‚ ๐‘€๐‘—(๐‘ฆ, ๐œ•)

๐‘ž(๐œ•)

slide-60
SLIDE 60

Stratified Sampling

With random uniform sampling, we can get unlucky

E.g. all samples clump in a corner If we donโ€™t know anything of the integrand, we want a relatively uniform sampling

Not regular, though, because of alias patterns!

Subdivide domain into non-overlapping regions (e.g. a regular grid). Each region is called a stratum Pick new random sample in stratum with lowest number of samples

Rendering โ€“ Monte Carlo Integration II 60

slide-61
SLIDE 61

Monte Carlo Integration for Pixels

In the first lecture, we used supersampling to fight off aliasing Pixels are another instance where we use Monte Carlo integration Choosing samples within pixels is an instance of stratified sampling Uniform 2D distribution, average over a pixel rectangle: box filter

We will see more advanced methods for filtering in future lectures If we didnโ€™t use at least one sample per pixel, we would leave holes

Rendering โ€“ Monte Carlo Integration II 61

slide-62
SLIDE 62

Monte Carlo Integration for Pixels

Samples are randomly jittered in each stratum Ergo, we donโ€™t hit the same ๐‘ฆ with each pixel sample (๐‘ž๐‘ฆ, ๐‘ž๐‘ง) inside pixel ๐‘ž We just add two random variables! Instead of integrating over a hemisphere, we are integrating over all surface points visible to a pixel and their respective hemispheres The box filter over pixel color samples implements uniform integral

Rendering โ€“ Monte Carlo Integration II 62

เถฑ

๐‘ž๐‘ง

เถฑ

๐‘ž๐‘ฆ

๐‘€๐‘“(๐‘ฆ, ๐‘ค) ๐‘’๐‘ž๐‘ฆ ๐‘’๐‘ž๐‘ง ๐‘ค1 ๐‘ค2 ๐‘ฆ1 ๐‘ฆ2 ๐‘ž

slide-63
SLIDE 63

The Box Filter for a Pixel (Monte Carlo Integration)

Given: camera, pixel coordinates (pixel_x, pixel_y), scene rgb = {0,0,0} for i in [0, N) do px = pixel_x + uniform_random_sample() // ๐œŠ๐‘— = ๐‘„๐œŠ ๐œŠ๐‘— = ๐‘„๐œŠ

โˆ’1(๐œŠ๐‘—)

py = pixel_y + uniform_random_sample() ray = rayThroughPixelPos(camera, px, py)

x = findIntersection(ray, scene)

rgb += getIntegratorValue(x) end for rgb /= N

Rendering โ€“ Monte Carlo Integration II 63

Nothing else necessary: offsets in x and y are uniformly distributed, they are independent, the domain size of each pixel is 1x1=1 and so we donโ€™t have to change the integration at all.

slide-64
SLIDE 64

Low-Discrepancy Series

Stratified sampling is a special case of low-discrepancy sampling[4] Replace the built-in RNG with a sample generation algorithm that sacrifices randomness for good spatial distribution Great for:

Faster convergence (reduction of noise) GPUs (they donโ€™t love random numbers) Transparent and portable sampling behavior

Rendering โ€“ Monte Carlo Integration II 64

slide-65
SLIDE 65

Halton Sequences

For ๐‘œ-D, pick ๐‘œ different, co-prime bases (e.g. 2,3) Based on radical inverse: an integer ๐‘ can be written with base ๐‘ and ๐‘’ โˆˆ [0, ๐‘ โˆ’ 1] as ๐‘ = ฯƒ๐‘—=1

๐‘› ๐‘’๐‘—(๐‘)๐‘๐‘—โˆ’1

For ๐‘ = 2, ๐‘’ โˆˆ [0,1]: this is binary representation of integers: 13 = 0 ร— 20 + 1 ร— 21 + 1 ร— 22 + 1 ร— 23 Radical inverse with ๐‘› digits: ฮฆ ๐‘ = ฯƒ๐‘—=1

๐‘› ๐‘’๐‘—(๐‘) ๐‘๐‘—

Rendering โ€“ Monte Carlo Integration II 65

Default RNG 2,3 Halton Sequence

Jheald, Wikipedia, Halton Sequence โ€“ โ€œOwn workโ€

slide-66
SLIDE 66

Halton Sequences

Start at any integer value for each of the ๐‘œ variables

Need data structure to represent ๐‘ = ฯƒ๐‘—=1

๐‘› ๐‘’๐‘—(๐‘)๐‘๐‘—โˆ’1

Can be written for arbitrary ๐‘ with some bit fiddling

For each new ๐‘œ-D sample, increment all ๐‘œ integers Compute their radical inverse with their proper base Using same sequence for all pixels โ†’ patterns

Rendering โ€“ Monte Carlo Integration II 66

Default RNG 2,3 Halton Sequence

Jheald, Wikipedia, Halton Sequence โ€“ โ€œOwn workโ€

slide-67
SLIDE 67

Halton Sequences

Large primes will behave similarly โ†’ patterns Repetitive patterns should always be avoided! Option 1: use different starting values for different instances (e.g. for each pixel) Option 2: instead of incrementing ๐‘’๐‘—, cycle through random permutations of [0, ๐‘ โˆ’ 1] in each instance

E.g.: ๐‘ = 3, 0, 2, 1 instead of 0, 1, 2

Rendering โ€“ Monte Carlo Integration II 67

29,31 Halton Sequence 29,31 Halton Scrambled

slide-68
SLIDE 68

Moving Forward

Get comfortable with all approaches to integration and sampling

The mental image of โ€œarea under the curveโ€ eventually collapses (infinite-dimensional integral coming up next!) Transformations between sample domains may be non-trivial Once you grasp the underlying concepts, applying the math is easy

We have seen simpler explanations for the most important parts

Uniformly sampling a hemisphere Cosine-weighted sampling of a hemisphere

Rendering โ€“ Monte Carlo Integration II 68

slide-69
SLIDE 69

References and Further Reading

Slide set based mostly on chapter 13 of Physically Based Rendering: From Theory to Implementation [1] Steven Strogatz, Infinite Powers: How Calculus Reveals the Secrets of the Universe [2] Video, Why โ€œprobability of 0โ€ does not mean โ€œimpossibleโ€ | Probabilities of probabilities, part 2: https://www.youtube.com/watch?v=ZA4JkHKZM50 [3] Video, The determinant | Essence of linear algebra, chapter 6: https://www.youtube.com/watch?v=Ip3X9LOh2dk [4] SIGGRAPH 2012 Course: Advanced (Quasi-) Monte Carlo Methods for Image Synthesis, https://sites.google.com/site/qmcrendering/ [5] Wikipedia, Van der Corput Sequence, https://en.wikipedia.org/wiki/Van_der_Corput_sequence

Rendering โ€“ Monte Carlo Integration II 69