Skip to content

Commit e6ac95a

Browse files
committed
Use const ref params
* Pass vec, pt, ray, color params by const ref * Update text for vec, pt, ray, color pass-by-ref Resolves #1250
1 parent 258dc3a commit e6ac95a

26 files changed

+127
-114
lines changed

books/RayTracingInOneWeekend.html

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@
322322
double operator[](int i) const { return e[i]; }
323323
double& operator[](int i) { return e[i]; }
324324

325-
vec3& operator+=(const vec3 &v) {
325+
vec3& operator+=(const vec3& v) {
326326
e[0] += v.e[0];
327327
e[1] += v.e[1];
328328
e[2] += v.e[2];
@@ -355,47 +355,47 @@
355355

356356
// Vector Utility Functions
357357

358-
inline std::ostream& operator<<(std::ostream &out, const vec3 &v) {
358+
inline std::ostream& operator<<(std::ostream& out, const vec3& v) {
359359
return out << v.e[0] << ' ' << v.e[1] << ' ' << v.e[2];
360360
}
361361

362-
inline vec3 operator+(const vec3 &u, const vec3 &v) {
362+
inline vec3 operator+(const vec3& u, const vec3& v) {
363363
return vec3(u.e[0] + v.e[0], u.e[1] + v.e[1], u.e[2] + v.e[2]);
364364
}
365365

366-
inline vec3 operator-(const vec3 &u, const vec3 &v) {
366+
inline vec3 operator-(const vec3& u, const vec3& v) {
367367
return vec3(u.e[0] - v.e[0], u.e[1] - v.e[1], u.e[2] - v.e[2]);
368368
}
369369

370-
inline vec3 operator*(const vec3 &u, const vec3 &v) {
370+
inline vec3 operator*(const vec3& u, const vec3& v) {
371371
return vec3(u.e[0] * v.e[0], u.e[1] * v.e[1], u.e[2] * v.e[2]);
372372
}
373373

374-
inline vec3 operator*(double t, const vec3 &v) {
374+
inline vec3 operator*(double t, const vec3& v) {
375375
return vec3(t*v.e[0], t*v.e[1], t*v.e[2]);
376376
}
377377

378-
inline vec3 operator*(const vec3 &v, double t) {
378+
inline vec3 operator*(const vec3& v, double t) {
379379
return t * v;
380380
}
381381

382-
inline vec3 operator/(vec3 v, double t) {
382+
inline vec3 operator/(const vec3& v, double t) {
383383
return (1/t) * v;
384384
}
385385

386-
inline double dot(const vec3 &u, const vec3 &v) {
386+
inline double dot(const vec3& u, const vec3& v) {
387387
return u.e[0] * v.e[0]
388388
+ u.e[1] * v.e[1]
389389
+ u.e[2] * v.e[2];
390390
}
391391

392-
inline vec3 cross(const vec3 &u, const vec3 &v) {
392+
inline vec3 cross(const vec3& u, const vec3& v) {
393393
return vec3(u.e[1] * v.e[2] - u.e[2] * v.e[1],
394394
u.e[2] * v.e[0] - u.e[0] * v.e[2],
395395
u.e[0] * v.e[1] - u.e[1] * v.e[0]);
396396
}
397397

398-
inline vec3 unit_vector(vec3 v) {
398+
inline vec3 unit_vector(const vec3& v) {
399399
return v / v.length();
400400
}
401401

@@ -424,7 +424,7 @@
424424

425425
using color = vec3;
426426

427-
void write_color(std::ostream &out, color pixel_color) {
427+
void write_color(std::ostream& out, const color& pixel_color) {
428428
// Write the translated [0,255] value of each color component.
429429
out << int(255.999 * pixel_color.x()) << ' '
430430
<< int(255.999 * pixel_color.y()) << ' '
@@ -1108,7 +1108,7 @@
11081108

11091109
class sphere : public hittable {
11101110
public:
1111-
sphere(point3 _center, double _radius) : center(_center), radius(_radius) {}
1111+
sphere(const point3& _center, double _radius) : center(_center), radius(_radius) {}
11121112

11131113
bool hit(const ray& r, double ray_tmin, double ray_tmax, hit_record& rec) const override {
11141114
vec3 oc = center - r.origin();
@@ -2013,7 +2013,7 @@
20132013
and the number of samples involved:
20142014

20152015
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
2016-
void write_color(std::ostream &out, color pixel_color, int samples_per_pixel) {
2016+
void write_color(std::ostream& out, const color& pixel_color, int samples_per_pixel) {
20172017
auto r = pixel_color.x();
20182018
auto g = pixel_color.y();
20192019
auto b = pixel_color.z();
@@ -2225,7 +2225,7 @@
22252225
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
22262226
...
22272227

2228-
inline vec3 unit_vector(vec3 v) {
2228+
inline vec3 unit_vector(const vec3& v) {
22292229
return v / v.length();
22302230
}
22312231

@@ -2651,7 +2651,7 @@
26512651
}
26522652
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
26532653

2654-
void write_color(std::ostream &out, color pixel_color, int samples_per_pixel) {
2654+
void write_color(std::ostream& out, const color& pixel_color, int samples_per_pixel) {
26552655
auto r = pixel_color.x();
26562656
auto g = pixel_color.y();
26572657
auto b = pixel_color.z();
@@ -2777,7 +2777,7 @@
27772777
class sphere : public hittable {
27782778
public:
27792779
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
2780-
sphere(point3 _center, double _radius, shared_ptr<material> _material)
2780+
sphere(const point3& _center, double _radius, shared_ptr<material> _material)
27812781
: center(_center), radius(_radius), mat(_material) {}
27822782
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
27832783

@@ -3877,7 +3877,7 @@
38773877
`random_in_unit_sphere()`, just for two dimensions.
38783878

38793879
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
3880-
inline vec3 unit_vector(vec3 u) {
3880+
inline vec3 unit_vector(const vec3& u) {
38813881
return v / v.length();
38823882
}
38833883

books/RayTracingTheNextWeek.html

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,12 @@
176176
public:
177177
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
178178
// Stationary Sphere
179-
sphere(point3 _center, double _radius, shared_ptr<material> _material)
179+
sphere(const point3& _center, double _radius, shared_ptr<material> _material)
180180
: center1(_center), radius(_radius), mat(_material), is_moving(false) {}
181181

182182
// Moving Sphere
183-
sphere(point3 _center1, point3 _center2, double _radius, shared_ptr<material> _material)
183+
sphere(const point3& _center1, const point3& _center2, double _radius,
184+
shared_ptr<material> _material)
184185
: center1(_center1), radius(_radius), mat(_material), is_moving(true)
185186
{
186187
center_vec = _center2 - _center1;
@@ -735,7 +736,7 @@
735736
public:
736737
// Stationary Sphere
737738
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
738-
sphere(point3 _center, double _radius, shared_ptr<material> _material)
739+
sphere(const point3& _center, double _radius, shared_ptr<material> _material)
739740
: center1(_center), radius(_radius), mat(_material), is_moving(false)
740741
{
741742
auto rvec = vec3(radius, radius, radius);
@@ -770,7 +771,8 @@
770771
public:
771772
...
772773
// Moving Sphere
773-
sphere(point3 _center1, point3 _center2, double _radius, shared_ptr<material> _material)
774+
sphere(const point3& _center1, const point3& _center2, double _radius,
775+
shared_ptr<material> _material)
774776
: center1(_center1), radius(_radius), mat(_material), is_moving(true)
775777
{
776778
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
@@ -1222,7 +1224,7 @@
12221224

12231225
class solid_color : public texture {
12241226
public:
1225-
solid_color(color c) : color_value(c) {}
1227+
solid_color(const color& c) : color_value(c) {}
12261228

12271229
solid_color(double red, double green, double blue) : solid_color(color(red,green,blue)) {}
12281230

@@ -1290,7 +1292,7 @@
12901292
checker_texture(double _scale, shared_ptr<texture> _even, shared_ptr<texture> _odd)
12911293
: inv_scale(1.0 / _scale), even(_even), odd(_odd) {}
12921294

1293-
checker_texture(double _scale, color c1, color c2)
1295+
checker_texture(double _scale, const color& c1, const color& c2)
12941296
: inv_scale(1.0 / _scale),
12951297
even(make_shared<solid_color>(c1)),
12961298
odd(make_shared<solid_color>(c2))
@@ -1693,7 +1695,7 @@
16931695

16941696
~rtw_image() { STBI_FREE(data); }
16951697

1696-
bool load(const std::string filename) {
1698+
bool load(const std::string& filename) {
16971699
// Loads image data from the given file name. Returns true if the load succeeded.
16981700
auto n = bytes_per_pixel; // Dummy out parameter: original components per pixel
16991701
data = stbi_load(filename.c_str(), &image_width, &image_height, &n, bytes_per_pixel);
@@ -1749,7 +1751,6 @@
17491751
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
17501752
#include "rtw_stb_image.h"
17511753
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1752-
#include "perlin.h"
17531754

17541755
...
17551756

@@ -1948,9 +1949,13 @@
19481949
<div class='together'>
19491950
Now if we create an actual texture that takes these floats between 0 and 1 and creates grey colors:
19501951

1951-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1952+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
19521953
#include "perlin.h"
1954+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
19531955

1956+
...
1957+
1958+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
19541959
class noise_texture : public texture {
19551960
public:
19561961
noise_texture() {}
@@ -2279,8 +2284,15 @@
22792284
...
22802285
private:
22812286
...
2282-
static double perlin_interp(vec3 c[2][2][2], double u, double v, double w) {
2287+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ delete
2288+
static double trilinear_interp(double c[2][2][2], double u, double v, double w) {
2289+
...
2290+
}
2291+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
2292+
2293+
22832294
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
2295+
static double perlin_interp(const vec3 c[2][2][2], double u, double v, double w) {
22842296
auto uu = u*u*(3-2*u);
22852297
auto vv = v*v*(3-2*v);
22862298
auto ww = w*w*(3-2*w);
@@ -2297,9 +2309,8 @@
22972309
}
22982310

22992311
return accum;
2300-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
23012312
}
2302-
...
2313+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
23032314
};
23042315
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23052316
[Listing [perlin-interp]: <kbd>[perlin.h]</kbd> Perlin interpolation function so far]
@@ -3050,7 +3061,7 @@
30503061
class diffuse_light : public material {
30513062
public:
30523063
diffuse_light(shared_ptr<texture> a) : emit(a) {}
3053-
diffuse_light(color c) : emit(make_shared<solid_color>(c)) {}
3064+
diffuse_light(const color& c) : emit(make_shared<solid_color>(c)) {}
30543065

30553066
bool scatter(const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered)
30563067
const override {
@@ -3842,7 +3853,7 @@
38423853
: boundary(b), neg_inv_density(-1/d), phase_function(make_shared<isotropic>(a))
38433854
{}
38443855

3845-
constant_medium(shared_ptr<hittable> b, double d, color c)
3856+
constant_medium(shared_ptr<hittable> b, double d, const color& c)
38463857
: boundary(b), neg_inv_density(-1/d), phase_function(make_shared<isotropic>(c))
38473858
{}
38483859

@@ -3913,7 +3924,7 @@
39133924
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
39143925
class isotropic : public material {
39153926
public:
3916-
isotropic(color c) : albedo(make_shared<solid_color>(c)) {}
3927+
isotropic(const color& c) : albedo(make_shared<solid_color>(c)) {}
39173928
isotropic(shared_ptr<texture> a) : albedo(a) {}
39183929

39193930
bool scatter(const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered)

books/RayTracingTheRestOfYourLife.html

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,7 +2291,7 @@
22912291
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
22922292
class isotropic : public material {
22932293
public:
2294-
isotropic(color c) : albedo(make_shared<solid_color>(c)) {}
2294+
isotropic(const color& c) : albedo(make_shared<solid_color>(c)) {}
22952295
isotropic(shared_ptr<texture> a) : albedo(a) {}
22962296

22972297

@@ -2709,7 +2709,7 @@
27092709
return 0.0;
27102710
}
27112711

2712-
virtual vec3 random(const vec3& o) const {
2712+
virtual vec3 random(const point3& origin) const {
27132713
return vec3(1, 0, 0);
27142714
}
27152715
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
@@ -3167,7 +3167,7 @@
31673167
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
31683168
class isotropic : public material {
31693169
public:
3170-
isotropic(color c) : albedo(make_shared<solid_color>(c)) {}
3170+
isotropic(const color& c) : albedo(make_shared<solid_color>(c)) {}
31713171
isotropic(shared_ptr<texture> a) : albedo(a) {}
31723172

31733173

@@ -3496,8 +3496,8 @@
34963496
return 1 / solid_angle;
34973497
}
34983498

3499-
vec3 random(const point3& o) const override {
3500-
vec3 direction = center1 - o;
3499+
vec3 random(const point3& origin) const override {
3500+
vec3 direction = center1 - origin;
35013501
auto distance_squared = direction.length_squared();
35023502
onb uvw;
35033503
uvw.build_from_w(direction);
@@ -3595,9 +3595,9 @@
35953595
return sum;
35963596
}
35973597

3598-
vec3 random(const vec3& o) const override {
3598+
vec3 random(const point3& origin) const override {
35993599
auto int_size = int(objects.size());
3600-
return objects[random_int(0, int_size-1)]->random(o);
3600+
return objects[random_int(0, int_size-1)]->random(origin);
36013601
}
36023602
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
36033603

@@ -3659,7 +3659,7 @@
36593659
`write_color()` function to replace any `NaN` components with zero:
36603660

36613661
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
3662-
void write_color(std::ostream &out, color pixel_color, int samples_per_pixel) {
3662+
void write_color(std::ostream& out, const color& pixel_color, int samples_per_pixel) {
36633663
auto r = pixel_color.x();
36643664
auto g = pixel_color.y();
36653665
auto b = pixel_color.z();

src/InOneWeekend/color.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ inline double linear_to_gamma(double linear_component)
2525
return 0;
2626
}
2727

28-
void write_color(std::ostream &out, color pixel_color, int samples_per_pixel) {
28+
void write_color(std::ostream& out, const color& pixel_color, int samples_per_pixel) {
2929
auto r = pixel_color.x();
3030
auto g = pixel_color.y();
3131
auto b = pixel_color.z();
@@ -36,7 +36,7 @@ void write_color(std::ostream &out, color pixel_color, int samples_per_pixel) {
3636
g *= scale;
3737
b *= scale;
3838

39-
// Apply a linear to gamma transform for gamma 2
39+
// Apply the linear to gamma transform.
4040
r = linear_to_gamma(r);
4141
g = linear_to_gamma(g);
4242
b = linear_to_gamma(b);

src/InOneWeekend/rtw_stb_image.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class rtw_image {
4545

4646
~rtw_image() { STBI_FREE(data); }
4747

48-
bool load(const std::string filename) {
48+
bool load(const std::string& filename) {
4949
// Loads image data from the given file name. Returns true if the load succeeded.
5050
auto n = bytes_per_pixel; // Dummy out parameter: original components per pixel
5151
data = stbi_load(filename.c_str(), &image_width, &image_height, &n, bytes_per_pixel);

src/InOneWeekend/sphere.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
class sphere : public hittable {
2020
public:
21-
sphere(point3 _center, double _radius, shared_ptr<material> _material)
21+
sphere(const point3& _center, double _radius, shared_ptr<material> _material)
2222
: center(_center), radius(_radius), mat(_material) {}
2323

2424
bool hit(const ray& r, interval ray_t, hit_record& rec) const override {

0 commit comments

Comments
 (0)