Skip to content

Commit b0cd6c1

Browse files
committed
bvh_node: don't modify source object list
Instead of reordering the vector of scene objects passed to the `bvh_node` constructor, it now creates a local copy instead. For some reason, this is actually slightly faster. Resolves #701
1 parent b8a5fad commit b0cd6c1

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

CHANGELOG.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@ Change Log -- Ray Tracing in One Weekend
1212
- Fix: Listings 33, 39: Add consistent function signature for `trilinear_interp` (#722)
1313

1414
### _The Next Week_
15+
- Change: `bvh_node` no longer reorders the source vector of scene objects; uses local copy
16+
instead (#701)
1517
- Delete: remove unused u,v,w variables in initial `perlin::noise()` function (#684)
1618
- Fix: Listing 15: In `bvh.h`, add missing `hittable_list.h` include (#690)
1719
- Fix: Listing 33, 34, 38: Change implicit casts to explicit ones (#692)
1820
- Fix: Listing 40: Change `perlin.h` in the caption to `texture.h` (#698)
1921
- Fix: Listing 70: Add missing `bvh.h` (#694)
2022
- Fix: Listing 70 and `main.cc`: Change a fuzz value of a metal sphere to 1.0 which is the maximum value (#694)
2123

24+
### _The Next Week_
25+
- Change: `bvh_node` no longer reorders the source vector of scene objects; uses local copy
26+
instead (#701)
27+
2228

2329
----------------------------------------------------------------------------------------------------
2430
# v3.2.0 (2020-07-18)
@@ -59,8 +65,6 @@ significant change and improvement. We're hoping that changes to books one and t
5965
but that's never worked out for us before. Ah, dreams.
6066

6167
### Common
62-
- Bug: Found a bug in book 3 source `isotropic::scatter()` method. Commented out, using default
63-
(as it was previously). (#669)
6468
- Delete: vestigial `vec3::write_color()` method (now in color.h)
6569
- Change: All images and figures renamed to follow more logical convention, using the following
6670
pattern: `{fig,img}-<book>.<sequence>-<title>.<filetype>` (#495)
@@ -76,6 +80,8 @@ but that's never worked out for us before. Ah, dreams.
7680
because it already caught an existing bug in _The Rest of Your Life_ source. This change
7781
includes commenting out the book 3 `isotropic::scatter()` method, which was accidentally ignored
7882
anyway. (#639, #669)
83+
- Fix: Found a bug in book 3 source `isotropic::scatter()` method. Commented out, using default
84+
(as it was previously). (#669)
7985
- New: each book gets a section of recommended citation examples (#500)
8086

8187
### _In One Weekend_

books/RayTracingTheNextWeek.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,7 @@
861861
#define BVH_H
862862

863863
#include "rtweekend.h"
864+
864865
#include "hittable.h"
865866
#include "hittable_list.h"
866867

@@ -869,12 +870,12 @@
869870
public:
870871
bvh_node();
871872

872-
bvh_node(hittable_list& list, double time0, double time1)
873+
bvh_node(const hittable_list& list, double time0, double time1)
873874
: bvh_node(list.objects, 0, list.objects.size(), time0, time1)
874875
{}
875876

876877
bvh_node(
877-
std::vector<shared_ptr<hittable>>& objects,
878+
const std::vector<shared_ptr<hittable>>& src_objects,
878879
size_t start, size_t end, double time0, double time1);
879880

880881
virtual bool hit(
@@ -951,6 +952,8 @@
951952
std::vector<shared_ptr<hittable>>& objects,
952953
size_t start, size_t end, double time0, double time1
953954
) {
955+
auto objects = src_objects; // Create a modifiable array of the source scene objects
956+
954957
int axis = random_int(0,2);
955958
auto comparator = (axis == 0) ? box_x_compare
956959
: (axis == 1) ? box_y_compare

src/TheNextWeek/bvh.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ class bvh_node : public hittable {
2323
public:
2424
bvh_node();
2525

26-
bvh_node(hittable_list& list, double time0, double time1)
26+
bvh_node(const hittable_list& list, double time0, double time1)
2727
: bvh_node(list.objects, 0, list.objects.size(), time0, time1)
2828
{}
2929

3030
bvh_node(
31-
std::vector<shared_ptr<hittable>>& objects,
31+
const std::vector<shared_ptr<hittable>>& src_objects,
3232
size_t start, size_t end, double time0, double time1);
3333

3434
virtual bool hit(
@@ -68,9 +68,11 @@ bool box_z_compare (const shared_ptr<hittable> a, const shared_ptr<hittable> b)
6868

6969

7070
bvh_node::bvh_node(
71-
std::vector<shared_ptr<hittable>>& objects,
71+
const std::vector<shared_ptr<hittable>>& src_objects,
7272
size_t start, size_t end, double time0, double time1
7373
) {
74+
auto objects = src_objects; // Create a modifiable array of the source scene objects
75+
7476
int axis = random_int(0,2);
7577
auto comparator = (axis == 0) ? box_x_compare
7678
: (axis == 1) ? box_y_compare

src/TheRestOfYourLife/bvh.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "rtweekend.h"
1515

1616
#include "hittable.h"
17+
#include "hittable_list.h"
1718

1819
#include <algorithm>
1920

@@ -22,12 +23,12 @@ class bvh_node : public hittable {
2223
public:
2324
bvh_node();
2425

25-
bvh_node(hittable_list& list, double time0, double time1)
26+
bvh_node(const hittable_list& list, double time0, double time1)
2627
: bvh_node(list.objects, 0, list.objects.size(), time0, time1)
2728
{}
2829

2930
bvh_node(
30-
std::vector<shared_ptr<hittable>>& objects,
31+
const std::vector<shared_ptr<hittable>>& src_objects,
3132
size_t start, size_t end, double time0, double time1);
3233

3334
virtual bool hit(
@@ -67,9 +68,11 @@ bool box_z_compare (const shared_ptr<hittable> a, const shared_ptr<hittable> b)
6768

6869

6970
bvh_node::bvh_node(
70-
std::vector<shared_ptr<hittable>>& objects,
71+
const std::vector<shared_ptr<hittable>>& src_objects,
7172
size_t start, size_t end, double time0, double time1
7273
) {
74+
auto objects = src_objects; // Create a modifiable array of the source scene objects
75+
7376
int axis = random_int(0,2);
7477
auto comparator = (axis == 0) ? box_x_compare
7578
: (axis == 1) ? box_y_compare

0 commit comments

Comments
 (0)