Introducton Monte Carlo Integraton Dgtal Image Synthess Yung-Yu Chuang 11/9/005 The ntegral equatons generally don t have analytc solutons, so we must turn to numercal methods. L ( o p,ωo) = L e ( p,ωo) + s f (p,ωo,ω ) L (p,ω ) cosθ d Standard methods lke Trapezodal ntegraton or Gaussan quadrature are not effectve for hgh-dmensonal and dscontnuous ntegrals. ω wth sldes by Pat Hanrahan and Torsten Moller Smple ntegraton Trapezodal rule
Randomzed algorthms Las Vegas v.s. Monte Carlo Las Vegas: gves the rght answer by usng randomness. Monte Carlo: gves the rght answer on the average. Results depend on random numbers used, but statstcally lkely to be close to the rght answer. Monte Carlo ntegraton Monte Carlo ntegraton: uses samplng to estmate the values of ntegrals. It only requres to be able to evaluate the ntegrand at arbtrary ponts, makng t easy to mplement and applcable to many problems. If n samples are used, ts converges at the rate of O(n -1/ ). That s, to cut the error n half, t s necessary to evaluate four tmes as many samples. Images by Monte Carlo methods are often nosy. Monte Carlo methods Basc concepts X s a random varable Applyng a functon to a random varable gves another random varable, Y=f(X). CDF (cumulatve dstrbuton functon) P( x) Pr{ X x} PDF (probablty densty functon): nonnegatve, sum to 1 dp( x) p( x) dx canoncal unform random varable ξ (provded by standard lbrary and easy to transform to other dstrbutons)
Dscrete probablty dstrbutons Contnuous probablty dstrbutons Expected values Average value of a functon f(x) over some dstrbuton of values p(x) over ts doman D p [ f ( x) ] E f ( x) p( x) dx = D Example: cos functon over [0, π], p s unform E p = π 1 0 π [ cos( x) ] cos x dx = 0 Varance Expected devaton from the expected value Fundamental concept of quantfyng the error n Monte Carlo methods [( f ( x) E[ f ( )]) ] V[ f ( x)] = E x
Propertes [ af ( x) ] ae[ f ( x) ] E = E f ( X ) = E ) [ af ( x) ] a V [ f ( x) ] V = V [ f ( X ] [ f x) ] = E ( f ( x) ) [ ] E[ f ( )] ( x Monte Carlo estmator Assume that we want to evaluate the ntegral of f(x) over [a,b] Gven a unform random varable X over [a,b], Monte Carlo estmator says that the expected value E[F N ] of the estmator F N equals the ntegral N b a F = f ( X ) N N = 1 General Monte Carlo estmator Gven a random varable X drawn from an arbtrary PDF p(x), then the estmator s F N = 1 N N = 1 p( X ) f ( X ) Choosng samples How to sample an arbtrary dstrbuton from a varable of unform dstrbuton? Inverson Rejecton Transform Although the converge rate of MC estmator s O(N 1/ ), slower than other ntegral methods, ts converge rate s ndependent of the dmenson, makng t the only practcal method for hgh dmensonal ntegral
Inverson method Inverson method Compute CDF P(x) Compute P -1 (x) Obtan ξ Compute X =P -1 (ξ) Example: exponental dstrbuton Example: power functon, for example, Blnn s Fresnel term Compute CDF P(x) Compute P -1 (x) Obtan ξ Compute X =P -1 (ξ)
Samplng a crcle Samplng a crcle Rejecton method Rejecton method Sometmes, we can t ntegrate nto CDF or nvert CDF Rejecton method s a fart-throwng method wthout performng the above steps 1. Fnd q(x) so that p(x)<cq(x). Dart throwng a. Choose a par (X, ξ), where X s sampled from q(x) b. If (ξ<p(x)/cq(x)) return X Essentally, we pck a pont (X, ξcq(x)). If t les beneath p(x) then we are fne.
Example: samplng a unt sphere vod RejectonSampleDsk(float *x, float *y) { float sx, sy; do { sx = 1.f -.f * RandomFloat(); sy = 1.f -.f * RandomFloat(); } whle (sx*sx + sy*sy > 1.f) *x = sx; *y = sy; } π/4~78.5% good samples, gets worse n hgher dmensons, for example, for sphere, π/6~5.3% Transformng between dstrbutons Transform a random varable X from dstrbuton p x (x) to a random varable Y wth dstrbuton p y (x) Y=y(X), y s one-to-one,.e. monotonc Hence, Py ( y) = Pr{ Y y( x)} = Pr{ X x} = Px ( x) PDF: dpy ( y) dpx ( x) = dx dx p y dy ( y) = dx dpy ( y) dy dy dx p x (x) p y ( y) = dy dx 1 p ( x) x Example p x ( x) = x Y = sn X Transform Gven X wth p x (x) and p y (y), try to use X to generate Y. p y ( y) = (cos x) 1 p ( x) = x x cos x = sn 1 1 y y
Multple dmensons Multple dmensons Multdmensonal samplng Samplng a hemsphere
Samplng a hemsphere Samplng a hemsphere Samplng a dsk Samplng a dsk
Shrley s mappng Samplng a trangle Samplng a trangle Multple mportance samplng
Multple mportance samplng Multple mportance samplng Monte Carlo for renderng equaton L ( o p,ωo) = L e ( p,ωo) + s f (p,ωo,ω ) L (p,ω ) cosθ d Importance samplng: sample ω accordng to BxDF f and L (especally for lght sources) If we don t need anythng about f and L, use cosne-weghted samplng of hemsphere to fnd a sampled ω ω Cosne weghted hemsphere p( ω) cosθ 1 = p( ω) dω H = π π 0 0 1 c cosθ snθdθdφ 1 = cπ π 0 cosθ snθdθ 1 c = π 1 p( θ, φ) = cosθ snθ π dω = snθdθdφ
Cosne weghted hemsphere 1 p( θ, φ) = cosθ snθ π π 1 p( θ ) = cosθ snθdφ = cosθ snθ = sn θ 0 π p( θ, φ) 1 p( φ θ ) = = p ( θ ) π 1 1 1 1 P( θ ) = cos θ + = ξ1 θ = cos (1 ξ1) φ P( φ θ ) = = ξ φ = πξ π Cosne weghted hemsphere Malley s method: unformly generates ponts on the unt dsk and then generates drectons by projectng them up to the hemsphere above t. Vector CosneSampleHemsphere(float u1,float u){ Vector ret; ConcentrcSampleDsk(u1, u, &ret.x, &ret.y); ret.z = sqrtf(max(0.f,1.f - ret.x*ret.x - ret.y*ret.y)); return ret; } Cosne weghted hemsphere Why Malley s method works Unt dsk samplng ( r, φ) Map to hemsphere here r, ) p y ( y) = dy dx 1 p ( x) x r p = π ( φ (snθ, φ) Y = ( r, φ ), X = ( θ, φ) Samplng reflecton functons Spectrum BxDF::Sample_f(const Vector &wo, Vector *w, float u1, float u, float *pdf){ *w = CosneSampleHemsphere(u1, u); f (wo.z < 0.) w->z *= -1.f; *pdf = Pdf(wo, *w); return f(wo, *w); } For those who modfed Sample_f, Pdf must be changed accordngly float BxDF::Pdf(Vector &wo, Vector &w) { return SameHemsphere(wo, w)? fabsf(w.z) * INV_PI : 0.f; } Ths functon s useful for multple mportance samplng.
Samplng mcrofacet model Too complcated to sample accordng to f, sample D nstead. Samplng Blnn mcrofacet model Blnn dstrbuton: e + e D ( ωh) = ( ωh n) π Generate ω h accordng to the above functon cos + θ = e 1 h ξ1 φ h = πξ Convert ω h to ω ω = ω + ( ω ω ) ω o o h h ω o ω h ω Samplng Blnn mcrofacet model Convert half-angle Pdf to ncomng-angle Pdf, that s, change from a densty n term of ω h to one n terms of ω Samplng ansotropc mcrofacet model Ansotropc model (after Ashkhmn and Shrley) for the frst quadrant of the unt hemsphere D( ω ) = h ( e x + 1)( e y + 1)( ω n) h e cos φ sn x + e y φ
Samplng BSDF (mxture of BxDFs) Sample from the densty that s the sum of ndvdual denstes Three unform random numbers are used, the frst one determnes whch BxDF to be sampled and then sample that BxDF usng the other two random numbers Samplng lght sources Drect llumnaton from lght sources makes an mportant contrbuton, so t s crucal to be able to Sp: samples drectons from a pont p to the lght Sr: Generates random rays from the lght source (for bdrectonal lght transport algorthms such as bdrectonal path tracng and photon mappng) Pont lghts Sp: delta dstrbuton, treat smlar to specular BxDF Sr: samplng of a unform sphere Spotlghts Sp: the same as a pont lght Sr: samplng of a cone (do not consder the falloff)
Drectonal lghts Sp: lke pont lghts Sr: create a vrtual dsk of the same radus as scene s boundng sphere and then sample the dsk unformly. Area lghts Defned by shape Add shape samplng functons for Shape Sp: uses a densty wth respect to sold angle from the pont p Pont Sample(Pont &P, float u1, float u, Normal *Ns) Sr: generates ponts on the shape accordng to a densty wth respect to surface area Pont Sample(float u1, float u, Normal *Ns) Infnte area lghts Sp: normal gven: cosne weghted samplng Otherwse: unform sphercal samplng Does not take drectonal radance dstrbuton nto account Sr: Unformly sample two ponts p 1 and p on the sphere Use p1 as the orgn and p -p 1 as the drecton It can be shown that p -p 1 s unformly dstrbuted (L et. al. 003)