เถฑ 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
Todayโs Goal Integrating the cosine-weighted radiance ๐ ๐ (๐ฆ, ๐) at a point ๐ฆ Integral of the light function over 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 ? Rendering โ Monte Carlo Integration I 2
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 r = 1 2 We integrate over an area of size ๐ We will use the Monte Carlo integral for that 2 Rendering โ Monte Carlo Integration II 3
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
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
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
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 This is actually somewhat ok! Rendering โ Monte Carlo Integration II 7
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
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 ๐ ๐ ๐๐ ( ๐ฆ 2 + ๐ง 2 โค 1) Unit disk: ๐ ๐ฆ, ๐ง = เต1 , ๐ ๐ฆ, ๐ง = 1 4 , ๐ = 4 0 ๐๐ขโ๐๐ ๐ฅ๐๐ก๐ Rendering โ Monte Carlo Integration II 9
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
Uniformly Sampling the Unit Disk? Convert two ๐ to ranges 0, 1 , [0,2๐) for polar coordinates Convert to cartesian coordinates 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); } } Rendering โ Monte Carlo Integration II 11
Clumping We successfully sampled the unit disk in the proper range 1 However, the distribution is not uniform with respect to the area 0,5 0 Samples clump together at center -0,5 Averaging those samples will give us a skewed result for the integral! -1 -1 -0,5 0 0,5 1 Rendering โ Monte Carlo Integration II 12
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 2 ๐ ๐ up to radius ๐ ๐ = ๐ฮ๐ should contain ๐ 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
Uniformly Sampling the Unit Disk: A Solution 1 It works, and it is not even a bad way to arrive at the correct solution 0,5 0 -0,5 However, for more complex scenarios, we -1 might struggle to find the solution so easily -1 -0,5 0 0,5 1 1 With the tools we introduced earlier, we can 0,5 formalize this process for arbitrary setups 0 -0,5 -1 -1 -0,5 0 0,5 1 Rendering โ Monte Carlo Integration II 14
Polar To Cartesian Coordinates Letโs transform a regular grid from polar to cartesian coordinates ๐ ๐ ๐ = ๐ cos(๐), ๐ = ๐ sin(๐) Rendering โ Monte Carlo Integration II 15
First Attempt to Learn the PDF Take 100k samples, transform and see which box they end up in ฮพ 1 , ฮพ 2 ๐ = ๐ 1 cos(2๐๐ 2 ), ๐ = ๐ 1 sin(2๐๐ 2 ) Rendering โ Monte Carlo Integration II 16
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 ๐ฝ๐๐๐ฃ๐ข (๐ 1 , ๐ 2 ) ๐ท๐๐ ๐ข๐๐ก๐๐๐ (๐ฆ, ๐ง) ๐๐๐๐๐ (r, ๐) ๐(๐ , ๐) Rendering โ Monte Carlo Integration II 17
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
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
Computing the PDF after a Transformation โ1 ๐๐ Letโs interpret ๐ ๐ถ ๐ = ๐ ๐ต ๐ ๐๐ โ1 ๐๐ It is the probability density of ๐ต , multiplied by ๐๐ โ1 ๐๐ has two intuitive interpretations: ๐๐ the change in sampling density at point ๐ if we transform ๐ by ๐ or , the inverse change in the volume of an infinitesimal hypercube at point ๐ if we transform ๐ by ๐ Rendering โ Monte Carlo Integration II 20
Multidimensional Transformations If we try to apply the above to the unit disk, we fail at ๐ฆ = ๐ sin ๐ โ1 ๐๐ฆ We canโt evaluate : 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
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
Recommend
More recommend