-
Notifications
You must be signed in to change notification settings - Fork 868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Book 3.4 / Listing 16: Confusion #1537
Comments
Now, the confusing part... As I've mentioned in #1534 we've made a very unfortunate decision of using letter In Chapter 4 we are given function Then, in Listing 16 we stick this function into: double f(const vec3& d) {
auto cosine_squared = d.z()*d.z();
return cosine_squared;
} which up until now (see Listings 13, 14 and 15) was used for inverse of the CDF... Looking closer at the code in Listing 16, I've realized that while it looks almost identical to the code in Listings 13, 14 and 15, in this particular case |
If I may suggest, the code in Listings 13, 14 and 15 should be adjusted as follows: Listing 13: -double f(double d) {
+double inv_cdf(double d) {
return 2.0 * d;
}
double pdf(double x) {
return 0.5;
}
+
+double f(double x) {
+ return x * x;
+}
int main() {
...
for (int i = 0; i < N; i++) {
- auto x = f(random_double());
- sum += x*x / pdf(x);
+ auto x = inv_cdf(random_double());
+ sum += f(x) / pdf(x);
}
...
} Listing 14: -double f(double d) {
+double inv_cdfdouble d) {
return std::sqrt(4.0 * d);
}
double pdf(double x) {
return x / 2.0;
}
+
+double f(double x) {
+ return x * x;
+}
int main() {
...
for (int i = 0; i < N; i++) {
auto z = random_double();
...
- auto x = f(z);
- sum += x*x / pdf(x);
+ auto x = inv_cdf(z);
+ sum += f(x) / pdf(x);
}
...
} Listing 15: -double f(double d) {
+double inv_cdf(double d) {
return 8.0 * std::pow(d, 1.0/3.0);
}
double pdf(double x) {
return (3.0/8.0) * x*x;
}
+
+double f(double x) {
+ return x * x;
+}
int main() {
...
for (int i = 0; i < N; i++) {
auto z = random_double();
...
- auto x = f(z);
- sum += x*x / pdf(x);
+ auto x = inv_cdf(z);
+ sum += f(x) / pdf(x);
}
...
} With the above changes, Listing 16 can actually remain the same. The text surrounding Listings 13-15 might need to be tweaked as well to match up with the new function names. |
How do you feel about making the opposite change? That is, leave |
@dimitry-ishenko — I thought about this a bit more yesterday, and now I'm planning on proceeding with a variant of your suggestion. I'll name the inverse cumulative probability distribution function |
We use f(x) throughout the book to mean many different things. In book 3 section 3, we use f(x) to mean the inverse cumulative distribution of x. However, in section 4, we then switch to f(theta, phi) to mean a function that we integrate over the surface of the unit sphere. This appears in sphere_importance.cc in the same place that we had in integrate_x_sq.cc, confusing the two functions to mean very different things. Dimitry Ishenko suggested changing to a more explicit name to indicate the inverse cumulative distribution function, which helps make things much more clear. Resolves #1537
We use f(x) throughout the book to mean many different things. In book 3 section 3, we use f(x) to mean the inverse cumulative distribution of x. However, in section 4, we then switch to f(theta, phi) to mean a function that we integrate over the surface of the unit sphere. This appears in sphere_importance.cc in the same place that we had in integrate_x_sq.cc, confusing the two functions to mean very different things. Dimitry Ishenko suggested changing to a more explicit name to indicate the inverse cumulative distribution function, which helps make things much more clear. Resolves #1537
We use f(x) throughout the book to mean many different things. In book 3 section 3, we use f(x) to mean the inverse cumulative distribution of x. However, in section 4, we then switch to f(theta, phi) to mean a function that we integrate over the surface of the unit sphere. This appears in sphere_importance.cc in the same place that we had in integrate_x_sq.cc, confusing the two functions to mean very different things. Dimitry Ishenko suggested changing to a more explicit name to indicate the inverse cumulative distribution function, which helps make things much more clear. Resolves #1537
Completed in #1556 |
Let's start with analytical solution. The paragraph just below listing 16 says:
I remember very little advanced calc, but let me try...
NB: I've updated this post, which was initially using wrong formulas to compute surface integral.
General equation to compute an integral of function$f$ over surface $S$ is:
Using spherical coordinates, surface$S$ can be described in terms of a parameterized vector function $\vec v(\theta, \phi)$ , and it can be proven (I won't do that here) that:
For a sphere of radius$r$ , we can write the following parametric equations:
If you take partial derivatives$\frac {\partial \vec v}{\partial \theta}$ and $\frac {\partial \vec v}{\partial \phi}$ , and compute their cross-product, you will arrive at the following equation:
And, since we have a unit sphere, it becomes:$dS = \sin (\theta) d\theta d\phi$
So now, if we integrate our function$f(\theta, \phi) = \cos^2(\theta)$ over this unit sphere, we get:
Let's integrate over$\theta$ first:
Using u-substitution with$u = \cos(\theta)$ , we get $\frac {du}{d\theta} = -\sin(\theta)$ or $d\theta = -\frac {du}{\sin(\theta)}$ . Substitute $u$ and $d\theta$ into the above formula, and we get:
Now we integrate over$\phi$ :
QED
The text was updated successfully, but these errors were encountered: