Skip to content

Commit c645aee

Browse files
authored
Merge pull request #813 from RayTracing/inline-clean
Inline clean
2 parents 7ac4ad9 + 30753a6 commit c645aee

File tree

13 files changed

+71
-91
lines changed

13 files changed

+71
-91
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Change Log -- Ray Tracing in One Weekend
1010
- Change: Class public/private access labels get two-space indents (#782)
1111
- Change: `interval::clamp()` replaces standalone `clamp` utility function
1212
- New: `rtw_image` class for easier image data loading, searches more locations (#807)
13+
- Change: Cleaned up multiple cases where the `inline` keyword was unnecessary, and reorganized
14+
some global utility functions as either private static, or in better locations.
1315
- Fix: Remove redundant `virtual` keyword for methods with `override` (#805)
1416

1517
### In One Weekend

books/RayTracingInOneWeekend.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@
10081008
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
10091009
bool front_face;
10101010

1011-
inline void set_face_normal(const ray& r, const vec3& outward_normal) {
1011+
void set_face_normal(const ray& r, const vec3& outward_normal) {
10121012
front_face = dot(r.direction(), outward_normal) < 0;
10131013
normal = front_face ? outward_normal :-outward_normal;
10141014
}
@@ -1668,11 +1668,11 @@
16681668
class vec3 {
16691669
public:
16701670
...
1671-
inline static vec3 random() {
1671+
static vec3 random() {
16721672
return vec3(random_double(), random_double(), random_double());
16731673
}
16741674

1675-
inline static vec3 random(double min, double max) {
1675+
static vec3 random(double min, double max) {
16761676
return vec3(random_double(min,max), random_double(min,max), random_double(min,max));
16771677
}
16781678
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -2075,7 +2075,7 @@
20752075
double t;
20762076
bool front_face;
20772077

2078-
inline void set_face_normal(const ray& r, const vec3& outward_normal) {
2078+
void set_face_normal(const ray& r, const vec3& outward_normal) {
20792079
front_face = dot(r.direction(), outward_normal) < 0;
20802080
normal = front_face ? outward_normal :-outward_normal;
20812081
}

books/RayTracingTheRestOfYourLife.html

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@
529529
weighting function should be proportional to $1/pdf$. In fact it is exactly $1/pdf$:
530530

531531
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
532-
inline double pdf(double x) {
532+
double pdf(double x) {
533533
return 0.5*x;
534534
}
535535
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
@@ -563,7 +563,7 @@
563563
the machinery to get `x = random_double(0,2)`, and the code is:
564564

565565
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
566-
inline double pdf(double x) {
566+
double pdf(double x) {
567567
return 0.5;
568568
}
569569
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
@@ -606,7 +606,7 @@
606606
sample we get:
607607

608608
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
609-
inline double pdf(double x) {
609+
double pdf(double x) {
610610
return 3*x*x/8;
611611
}
612612
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
@@ -667,7 +667,7 @@
667667
the z axis:
668668

669669
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
670-
inline double pdf(const vec3& p) {
670+
double pdf(const vec3& p) {
671671
return 1 / (4*pi);
672672
}
673673

@@ -1264,11 +1264,6 @@
12641264
Let’s also start generating them as random vectors:
12651265

12661266
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1267-
#include "rtweekend.h"
1268-
1269-
#include <iostream>
1270-
#include <math.h>
1271-
12721267
inline vec3 random_cosine_direction() {
12731268
auto r1 = random_double();
12741269
auto r2 = random_double();
@@ -1280,6 +1275,17 @@
12801275

12811276
return vec3(x, y, z);
12821277
}
1278+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1279+
[Listing [random-cosine-direction]: <kbd>vec3.h</kbd> Random cosine direction utility function
1280+
1281+
1282+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1283+
#include "rtweekend.h"
1284+
1285+
#include <iostream>
1286+
#include <iomanip>
1287+
#include <math.h>
1288+
12831289

12841290
int main() {
12851291
int N = 1000000;
@@ -1394,7 +1400,7 @@
13941400
public:
13951401
onb() {}
13961402

1397-
inline vec3 operator[](int i) const { return axis[i]; }
1403+
vec3 operator[](int i) const { return axis[i]; }
13981404

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

17271733
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1728-
inline vec3 random_cosine_direction() {
1729-
auto r1 = random_double();
1730-
auto r2 = random_double();
1731-
auto z = sqrt(1-r2);
1732-
1733-
auto phi = 2*pi*r1;
1734-
auto x = cos(phi)*sqrt(r2);
1735-
auto y = sin(phi)*sqrt(r2);
1736-
1737-
return vec3(x, y, z);
1738-
}
1739-
17401734
class cosine_pdf : public pdf {
17411735
public:
17421736
cosine_pdf(const vec3& w) { uvw.build_from_w(w); }
@@ -2438,6 +2432,23 @@
24382432
The sphere class needs the two PDF-related functions:
24392433

24402434
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
2435+
class sphere : public hittable {
2436+
...
2437+
private:
2438+
...
2439+
static vec3 random_to_sphere(double radius, double distance_squared) {
2440+
auto r1 = random_double();
2441+
auto r2 = random_double();
2442+
auto z = 1 + r2*(sqrt(1-radius*radius/distance_squared) - 1);
2443+
2444+
auto phi = 2*pi*r1;
2445+
auto x = cos(phi)*sqrt(1-z*z);
2446+
auto y = sin(phi)*sqrt(1-z*z);
2447+
2448+
return vec3(x, y, z);
2449+
}
2450+
};
2451+
24412452
double sphere::pdf_value(const point3& o, const vec3& v) const {
24422453
hit_record rec;
24432454
if (!this->hit(ray(o, v), interval(0.001, infinity), rec))
@@ -2460,25 +2471,6 @@
24602471
[Listing [sphere-pdf]: <kbd>[sphere.h]</kbd> Sphere with PDF]
24612472
</div>
24622473

2463-
<div class='together'>
2464-
With the utility function:
2465-
2466-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
2467-
inline vec3 random_to_sphere(double radius, double distance_squared) {
2468-
auto r1 = random_double();
2469-
auto r2 = random_double();
2470-
auto z = 1 + r2*(sqrt(1-radius*radius/distance_squared) - 1);
2471-
2472-
auto phi = 2*pi*r1;
2473-
auto x = cos(phi)*sqrt(1-z*z);
2474-
auto y = sin(phi)*sqrt(1-z*z);
2475-
2476-
return vec3(x, y, z);
2477-
}
2478-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2479-
[Listing [rand-to-sphere]: <kbd>[pdf.h]</kbd> The random_to_sphere utility function]
2480-
</div>
2481-
24822474
<div class='together'>
24832475
We can first try just sampling the sphere rather than the light:
24842476

src/InOneWeekend/hittable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class hit_record {
2424
double t;
2525
bool front_face;
2626

27-
inline void set_face_normal(const ray& r, const vec3& outward_normal) {
27+
void set_face_normal(const ray& r, const vec3& outward_normal) {
2828
front_face = dot(r.direction(), outward_normal) < 0;
2929
normal = front_face ? outward_normal :-outward_normal;
3030
}

src/TheNextWeek/hittable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class hit_record {
2929
double v;
3030
bool front_face;
3131

32-
inline void set_face_normal(const ray& r, const vec3& outward_normal) {
32+
void set_face_normal(const ray& r, const vec3& outward_normal) {
3333
front_face = dot(r.direction(), outward_normal) < 0;
3434
normal = front_face ? outward_normal :-outward_normal;
3535
}

src/TheRestOfYourLife/cos_density.cc

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,6 @@
1616
#include <math.h>
1717

1818

19-
inline vec3 random_cosine_direction() {
20-
auto r1 = random_double();
21-
auto r2 = random_double();
22-
auto z = sqrt(1-r2);
23-
auto phi = 2*pi*r1;
24-
auto x = cos(phi)*sqrt(r2);
25-
auto y = sin(phi)*sqrt(r2);
26-
27-
return vec3(x, y, z);
28-
}
29-
30-
3119
int main() {
3220
int N = 1000000;
3321

src/TheRestOfYourLife/hittable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class hit_record {
2929
double v;
3030
bool front_face;
3131

32-
inline void set_face_normal(const ray& r, const vec3& outward_normal) {
32+
void set_face_normal(const ray& r, const vec3& outward_normal) {
3333
front_face = dot(r.direction(), outward_normal) < 0;
3434
normal = front_face ? outward_normal :-outward_normal;
3535
}

src/TheRestOfYourLife/integrate_x_sq.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <stdlib.h>
1818

1919

20-
inline double pdf(double x) {
20+
double pdf(double x) {
2121
return 3*x*x/8;
2222
}
2323

src/TheRestOfYourLife/onb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class onb
1919
public:
2020
onb() {}
2121

22-
inline vec3 operator[](int i) const { return axis[i]; }
22+
vec3 operator[](int i) const { return axis[i]; }
2323

2424
vec3 u() const { return axis[0]; }
2525
vec3 v() const { return axis[1]; }

src/TheRestOfYourLife/pdf.h

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,6 @@
1616
#include "onb.h"
1717

1818

19-
inline vec3 random_cosine_direction() {
20-
auto r1 = random_double();
21-
auto r2 = random_double();
22-
auto z = sqrt(1-r2);
23-
24-
auto phi = 2*pi*r1;
25-
auto x = cos(phi)*sqrt(r2);
26-
auto y = sin(phi)*sqrt(r2);
27-
28-
return vec3(x, y, z);
29-
}
30-
31-
32-
inline vec3 random_to_sphere(double radius, double distance_squared) {
33-
auto r1 = random_double();
34-
auto r2 = random_double();
35-
auto z = 1 + r2*(sqrt(1-radius*radius/distance_squared) - 1);
36-
37-
auto phi = 2*pi*r1;
38-
auto x = cos(phi)*sqrt(1-z*z);
39-
auto y = sin(phi)*sqrt(1-z*z);
40-
41-
return vec3(x, y, z);
42-
}
43-
44-
4519
class pdf {
4620
public:
4721
virtual ~pdf() {}

0 commit comments

Comments
 (0)