Skip to content
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

Inline clean #813

Merged
merged 8 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Change Log -- Ray Tracing in One Weekend
- Change: Class public/private access labels get two-space indents (#782)
- Change: `interval::clamp()` replaces standalone `clamp` utility function
- New: `rtw_image` class for easier image data loading, searches more locations (#807)
- Change: Cleaned up multiple cases where the `inline` keyword was unnecessary, and reorganized
some global utility functions as either private static, or in better locations.

### In One Weekend
- Added: More commentary about the choice between `double` and `float` (#752)
Expand Down
8 changes: 4 additions & 4 deletions books/RayTracingInOneWeekend.html
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
bool front_face;

inline void set_face_normal(const ray& r, const vec3& outward_normal) {
void set_face_normal(const ray& r, const vec3& outward_normal) {
front_face = dot(r.direction(), outward_normal) < 0;
normal = front_face ? outward_normal :-outward_normal;
}
Expand Down Expand Up @@ -1670,11 +1670,11 @@
class vec3 {
public:
...
inline static vec3 random() {
static vec3 random() {
return vec3(random_double(), random_double(), random_double());
}

inline static vec3 random(double min, double max) {
static vec3 random(double min, double max) {
return vec3(random_double(min,max), random_double(min,max), random_double(min,max));
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -2077,7 +2077,7 @@
double t;
bool front_face;

inline void set_face_normal(const ray& r, const vec3& outward_normal) {
void set_face_normal(const ray& r, const vec3& outward_normal) {
front_face = dot(r.direction(), outward_normal) < 0;
normal = front_face ? outward_normal :-outward_normal;
}
Expand Down
74 changes: 33 additions & 41 deletions books/RayTracingTheRestOfYourLife.html
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@
weighting function should be proportional to $1/pdf$. In fact it is exactly $1/pdf$:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
inline double pdf(double x) {
double pdf(double x) {
return 0.5*x;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
Expand Down Expand Up @@ -563,7 +563,7 @@
the machinery to get `x = random_double(0,2)`, and the code is:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
inline double pdf(double x) {
double pdf(double x) {
return 0.5;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
Expand Down Expand Up @@ -606,7 +606,7 @@
sample we get:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
inline double pdf(double x) {
double pdf(double x) {
return 3*x*x/8;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
Expand Down Expand Up @@ -667,7 +667,7 @@
the z axis:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
inline double pdf(const vec3& p) {
double pdf(const vec3& p) {
return 1 / (4*pi);
}

Expand Down Expand Up @@ -1264,11 +1264,6 @@
Let’s also start generating them as random vectors:

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

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

inline vec3 random_cosine_direction() {
auto r1 = random_double();
auto r2 = random_double();
Expand All @@ -1280,6 +1275,17 @@

return vec3(x, y, z);
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [random-cosine-direction]: <kbd>vec3.h</kbd> Random cosine direction utility function


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

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


int main() {
int N = 1000000;
Expand Down Expand Up @@ -1394,7 +1400,7 @@
public:
onb() {}

inline vec3 operator[](int i) const { return axis[i]; }
vec3 operator[](int i) const { return axis[i]; }

vec3 u() const { return axis[0]; }
vec3 v() const { return axis[1]; }
Expand Down Expand Up @@ -1725,18 +1731,6 @@
First, let’s try a cosine density:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
inline vec3 random_cosine_direction() {
auto r1 = random_double();
auto r2 = random_double();
auto z = sqrt(1-r2);

auto phi = 2*pi*r1;
auto x = cos(phi)*sqrt(r2);
auto y = sin(phi)*sqrt(r2);

return vec3(x, y, z);
}

class cosine_pdf : public pdf {
public:
cosine_pdf(const vec3& w) { uvw.build_from_w(w); }
Expand Down Expand Up @@ -2447,6 +2441,23 @@
The sphere class needs the two PDF-related functions:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
class sphere : public hittable {
...
private:
...
static vec3 random_to_sphere(double radius, double distance_squared) {
auto r1 = random_double();
auto r2 = random_double();
auto z = 1 + r2*(sqrt(1-radius*radius/distance_squared) - 1);

auto phi = 2*pi*r1;
auto x = cos(phi)*sqrt(1-z*z);
auto y = sin(phi)*sqrt(1-z*z);

return vec3(x, y, z);
}
};

double sphere::pdf_value(const point3& o, const vec3& v) const {
hit_record rec;
if (!this->hit(ray(o, v), interval(0.001, infinity), rec))
Expand All @@ -2469,25 +2480,6 @@
[Listing [sphere-pdf]: <kbd>[sphere.h]</kbd> Sphere with PDF]
</div>

<div class='together'>
With the utility function:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
inline vec3 random_to_sphere(double radius, double distance_squared) {
auto r1 = random_double();
auto r2 = random_double();
auto z = 1 + r2*(sqrt(1-radius*radius/distance_squared) - 1);

auto phi = 2*pi*r1;
auto x = cos(phi)*sqrt(1-z*z);
auto y = sin(phi)*sqrt(1-z*z);

return vec3(x, y, z);
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Listing [rand-to-sphere]: <kbd>[pdf.h]</kbd> The random_to_sphere utility function]
</div>

<div class='together'>
We can first try just sampling the sphere rather than the light:

Expand Down
2 changes: 1 addition & 1 deletion src/InOneWeekend/hittable.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class hit_record {
double t;
bool front_face;

inline void set_face_normal(const ray& r, const vec3& outward_normal) {
void set_face_normal(const ray& r, const vec3& outward_normal) {
front_face = dot(r.direction(), outward_normal) < 0;
normal = front_face ? outward_normal :-outward_normal;
}
Expand Down
2 changes: 1 addition & 1 deletion src/TheNextWeek/hittable.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class hit_record {
double v;
bool front_face;

inline void set_face_normal(const ray& r, const vec3& outward_normal) {
void set_face_normal(const ray& r, const vec3& outward_normal) {
front_face = dot(r.direction(), outward_normal) < 0;
normal = front_face ? outward_normal :-outward_normal;
}
Expand Down
12 changes: 0 additions & 12 deletions src/TheRestOfYourLife/cos_density.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,6 @@
#include <math.h>


inline vec3 random_cosine_direction() {
auto r1 = random_double();
auto r2 = random_double();
auto z = sqrt(1-r2);
auto phi = 2*pi*r1;
auto x = cos(phi)*sqrt(r2);
auto y = sin(phi)*sqrt(r2);

return vec3(x, y, z);
}


int main() {
int N = 1000000;

Expand Down
2 changes: 1 addition & 1 deletion src/TheRestOfYourLife/hittable.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class hit_record {
double v;
bool front_face;

inline void set_face_normal(const ray& r, const vec3& outward_normal) {
void set_face_normal(const ray& r, const vec3& outward_normal) {
front_face = dot(r.direction(), outward_normal) < 0;
normal = front_face ? outward_normal :-outward_normal;
}
Expand Down
2 changes: 1 addition & 1 deletion src/TheRestOfYourLife/integrate_x_sq.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <stdlib.h>


inline double pdf(double x) {
double pdf(double x) {
return 3*x*x/8;
}

Expand Down
2 changes: 1 addition & 1 deletion src/TheRestOfYourLife/onb.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class onb
public:
onb() {}

inline vec3 operator[](int i) const { return axis[i]; }
vec3 operator[](int i) const { return axis[i]; }

vec3 u() const { return axis[0]; }
vec3 v() const { return axis[1]; }
Expand Down
26 changes: 0 additions & 26 deletions src/TheRestOfYourLife/pdf.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,6 @@
#include "onb.h"


inline vec3 random_cosine_direction() {
auto r1 = random_double();
auto r2 = random_double();
auto z = sqrt(1-r2);

auto phi = 2*pi*r1;
auto x = cos(phi)*sqrt(r2);
auto y = sin(phi)*sqrt(r2);

return vec3(x, y, z);
}


inline vec3 random_to_sphere(double radius, double distance_squared) {
auto r1 = random_double();
auto r2 = random_double();
auto z = 1 + r2*(sqrt(1-radius*radius/distance_squared) - 1);

auto phi = 2*pi*r1;
auto x = cos(phi)*sqrt(1-z*z);
auto y = sin(phi)*sqrt(1-z*z);

return vec3(x, y, z);
}


class pdf {
public:
virtual ~pdf() {}
Expand Down
12 changes: 12 additions & 0 deletions src/TheRestOfYourLife/sphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ class sphere : public hittable {
u = phi / (2*pi);
v = theta / pi;
}

static vec3 random_to_sphere(double radius, double distance_squared) {
auto r1 = random_double();
auto r2 = random_double();
auto z = 1 + r2*(sqrt(1-radius*radius/distance_squared) - 1);

auto phi = 2*pi*r1;
auto x = cos(phi)*sqrt(1-z*z);
auto y = sin(phi)*sqrt(1-z*z);

return vec3(x, y, z);
}
};


Expand Down
2 changes: 1 addition & 1 deletion src/TheRestOfYourLife/sphere_importance.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <stdlib.h>


inline double pdf(const vec3& p) {
double pdf(const vec3& p) {
return 1.0 / (4.0*pi);
}

Expand Down
16 changes: 14 additions & 2 deletions src/common/vec3.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ class vec3 {
return (fabs(e[0]) < s) && (fabs(e[1]) < s) && (fabs(e[2]) < s);
}

inline static vec3 random() {
static vec3 random() {
return vec3(random_double(), random_double(), random_double());
}

inline static vec3 random(double min, double max) {
static vec3 random(double min, double max) {
return vec3(random_double(min,max), random_double(min,max), random_double(min,max));
}

Expand Down Expand Up @@ -165,5 +165,17 @@ inline vec3 refract(const vec3& uv, const vec3& n, double etai_over_etat) {
return r_out_perp + r_out_parallel;
}

inline vec3 random_cosine_direction() {
auto r1 = random_double();
auto r2 = random_double();
auto z = sqrt(1-r2);

auto phi = 2*pi*r1;
auto x = cos(phi)*sqrt(r2);
auto y = sin(phi)*sqrt(r2);

return vec3(x, y, z);
}


#endif