Skip to content

Commit

Permalink
Update alt programs + book to match
Browse files Browse the repository at this point in the history
Resolves #237
  • Loading branch information
hollasch committed Oct 28, 2019
1 parent 713881e commit 60dafbb
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 41 deletions.
63 changes: 36 additions & 27 deletions books/RayTracingTheRestOfYourLife.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
if(x*x + y*y < 1)
inside_circle++;
}
std::cout << "Estimate of Pi = " << 4*double(inside_circle) / N << "\n";
std::cout << "Estimate of Pi = " << 4*double(inside_circle) / N << '\n';
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [estpi-1]: <kbd>[pi.cc]</kbd> Estimating π]
Expand Down Expand Up @@ -161,10 +161,13 @@
inside_circle_stratified++;
}
}
std::cout << "Regular Estimate of Pi = " <<
4*double(inside_circle) / (sqrt_N*sqrt_N) << "\n";
std::cout << "Stratified Estimate of Pi = " <<
4*double(inside_circle_stratified) / (sqrt_N*sqrt_N) << "\n";

auto N = static_cast<double>(sqrt_N) * sqrt_N;
std::cout
<< "Regular Estimate of Pi = "
<< 4*double(inside_circle) / (sqrt_N*sqrt_N) << '\n'
<< "Stratified Estimate of Pi = "
<< 4*double(inside_circle_stratified) / (sqrt_N*sqrt_N) << '\n';
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [estpi-3]: <kbd>[pi.cc]</kbd> Estimating π, v3]
Expand Down Expand Up @@ -401,14 +404,14 @@
int inside_circle = 0;
int inside_circle_stratified = 0;
int N = 1000000;
double sum;
auto sum = 0.0;
for (int i = 0; i < N; i++) {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
auto x = sqrt(4*random_double());
sum += x*x / pdf(x);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
}
std::cout << "I =" << sum/N << "\n";
std::cout << "I = " << sum/N << '\n';
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [integ-xsq-2]: <kbd>[integrate_x_sq.cc]</kbd> Integrating $x^2$ with _pdf_]
Expand All @@ -432,14 +435,14 @@
int inside_circle = 0;
int inside_circle_stratified = 0;
int N = 1000000;
double sum;
auto sum = 0.0;
for (int i = 0; i < N; i++) {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
auto x = 2*random_double();
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
sum += x*x / pdf(x);
}
std::cout << "I =" << sum/N << "\n";
std::cout << "I = " << sum/N << '\n';
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [integ-xsq-3]: <kbd>[integrate_x_sq.cc]</kbd> Integrating $x^2$, v3]
Expand Down Expand Up @@ -478,14 +481,14 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
int N = 1;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
double sum;
auto sum = 0.0;
for (int i = 0; i < N; i++) {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
auto x = pow(8*random_double(), 1./3.);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
sum += x*x / pdf(x);
}
std::cout << "I =" << sum/N << "\n";
std::cout << "I = " << sum/N << '\n';
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [integ-xsq-4]: <kbd>[integrate_x_sq.cc]</kbd> Integrating $x^2$, final version]
Expand Down Expand Up @@ -525,8 +528,8 @@
vec3 random_in_unit_sphere() {
vec3 p;
do {
p = 2.0*vec3(random_double(),random_double(),random_double()) - vec3(1,1,1);
} while (dot(p,p) >= 1.0);
p = 2*vec3(random_double(),random_double(),random_double()) - vec3(1,1,1);
} while (p.squared_length() >= 1);
return p;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -539,10 +542,12 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
vec3 random_on_unit_sphere() {
vec3 p;
double len_squared;
do {
p = 2.0*vec3(random_double(),random_double(),random_double()) - vec3(1,1,1);
} while (dot(p,p) >= 1.0);
return unit_vector(p);
p = 2*vec3(random_double(),random_double(),random_double()) - vec3(1,1,1);
len_squared = p.squared_length();
} while (len_squared >= 1);
return p / sqrt(len_squared);
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [rand-on-sphere]: <kbd>[sphere_importance.cc]</kbd>
Expand All @@ -561,13 +566,13 @@

int main() {
int N = 1000000;
double sum;
auto sum = 0.0;
for (int i = 0; i < N; i++) {
vec3 d = random_on_unit_sphere();
auto cosine_squared = d.z()*d.z();
sum += cosine_squared / pdf(d);
vec3 d = random_on_unit_sphere();
auto cosine_squared = d.z()*d.z();
sum += cosine_squared / pdf(d);
}
std::cout << "I =" << sum/N << "\n";
std::cout << "I = " << sum/N << '\n';
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [main-sphereimp]: <kbd>[sphere_importance.cc]</kbd>
Expand Down Expand Up @@ -965,8 +970,7 @@
auto x = cos(2*pi*r1)*2*sqrt(r2*(1-r2));
auto y = sin(2*pi*r1)*2*sqrt(r2*(1-r2));
auto z = 1 - 2*r2;

std::cout << x << " " << y << " " << z << "\n";
std::cout << x << " " << y << " " << z << '\n';
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -1014,8 +1018,8 @@
auto z = 1 - r2;
sum += z*z*z / (1.0/(2.0*pi));
}
std::cout << "PI/2 = " << pi/2 << "\n";
std::cout << "Estimate = " << sum/N << "\n";
std::cout << "PI/2 = " << pi/2 << '\n';
std::cout << "Estimate = " << sum/N << '\n';
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [cos-cubed]: <kbd>[cos_cubed.cc]</kbd> Integration using $cos^3(x)$]
Expand All @@ -1041,6 +1045,11 @@
Let’s also start generating them as random vectors:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
#include "common/rtweekend.h"

#include <iostream>
#include <math.h>

inline vec3 random_cosine_direction() {
auto r1 = random_double();
auto r2 = random_double();
Expand All @@ -1058,8 +1067,8 @@
vec3 v = random_cosine_direction();
sum += v.z()*v.z()*v.z() / (v.z()/(pi));
}
std::cout << "PI/2 = " << pi/2 << "\n";
std::cout << "Estimate = " << sum/N << "\n";
std::cout << "PI/2 = " << pi/2 << '\n';
std::cout << "Estimate = " << sum/N << '\n';
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [cos-density]: <kbd>[cos_density.cc]</kbd> Integration with cosine density function]
Expand Down
4 changes: 2 additions & 2 deletions src/TheRestOfYourLife/cos_cubed.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ int main() {
auto z = 1 - r2;
sum += z*z*z / (1.0/(2.0*pi));
}
std::cout << "PI/2 = " << pi/2 << "\n";
std::cout << "Estimate = " << sum/N << "\n";
std::cout << "PI/2 = " << pi/2 << '\n';
std::cout << "Estimate = " << sum/N << '\n';
}
4 changes: 2 additions & 2 deletions src/TheRestOfYourLife/cos_density.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ int main() {
vec3 v = random_cosine_direction();
sum += v.z()*v.z()*v.z() / (v.z()/(pi));
}
std::cout << "PI/2 = " << pi/2 << "\n";
std::cout << "Estimate = " << sum/N << "\n";
std::cout << "PI/2 = " << pi/2 << '\n';
std::cout << "Estimate = " << sum/N << '\n';
}
8 changes: 2 additions & 6 deletions src/TheRestOfYourLife/pdf.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,21 @@ inline vec3 random_to_sphere(double radius, double distance_squared) {
return vec3(x, y, z);
}


vec3 random_in_unit_sphere() {
vec3 p;
do {
p = 2.0*vec3(random_double(),random_double(),random_double()) - vec3(1,1,1);
} while (dot(p,p) >= 1.0);
p = 2*vec3(random_double(),random_double(),random_double()) - vec3(1,1,1);
} while (p.squared_length() >= 1);
return p;
}



class pdf {
public:
virtual double value(const vec3& direction) const = 0;
virtual vec3 generate() const = 0;
virtual ~pdf() {}
};


class cosine_pdf : public pdf {
public:
cosine_pdf(const vec3& w) { uvw.build_from_w(w); }
Expand Down
1 change: 0 additions & 1 deletion src/TheRestOfYourLife/pi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ int main() {
}

auto N = static_cast<double>(sqrt_N) * sqrt_N;

std::cout
<< "Regular Estimate of Pi = "
<< 4*double(inside_circle) / N << '\n'
Expand Down
8 changes: 5 additions & 3 deletions src/TheRestOfYourLife/sphere_importance.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@

vec3 random_on_unit_sphere() {
vec3 p;
double len_squared;
do {
p = 2.0*vec3(random_double(),random_double(),random_double()) - vec3(1,1,1);
} while (dot(p,p) >= 1.0);
return unit_vector(p);
p = 2*vec3(random_double(), random_double(), random_double()) - vec3(1,1,1);
len_squared = p.squared_length();
} while (len_squared >= 1);
return p / sqrt(len_squared);
}

inline double pdf(const vec3& p) {
Expand Down

0 comments on commit 60dafbb

Please sign in to comment.