Skip to content

Commit c406f5c

Browse files
authored
BVH: implicit copy of source and sort in place
- Removed the const qualifier for the `objects` vector in the BVH constructor arguments. This makes an implicit (hence modifiable) copy in the un-indexed constructor. This should be ok though because we don't need the scene objects array after the BVH has been built. - Because the indexed constructor is now non-const, the object sorting now happens in place, without requiring the construction of a new copy of the subsection in question. - Removed the second unnecessary branch within condition "object_span == 2" per issue #1327. - Added Markus Boos to credits. Resolves #1327 Resolves #1388
1 parent dbaf803 commit c406f5c

File tree

5 files changed

+25
-43
lines changed

5 files changed

+25
-43
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ then.
2929
- Change - Reworked the AABB chapter (#1236)
3030
- New - add section on alternative 2D primitives such as triangle, ellipse and annulus (#1204,
3131
#1205)
32+
- Change - changed bvh construction (removed const qualifer for objects vector) so sorting is done
33+
in place and copying of vector is avoided, better bvh build performance (#1388, #1391)
3234

3335
### The Rest of Your Life
3436

books/RayTracingTheNextWeek.html

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -883,9 +883,9 @@
883883

884884
class bvh_node : public hittable {
885885
public:
886-
bvh_node(const hittable_list& list) : bvh_node(list.objects, 0, list.objects.size()) {}
886+
bvh_node(hittable_list list) : bvh_node(list.objects, 0, list.objects.size()) {}
887887

888-
bvh_node(const std::vector<shared_ptr<hittable>>& src_objects, size_t start, size_t end) {
888+
bvh_node(std::vector<shared_ptr<hittable>>& objects, size_t start, size_t end) {
889889
// To be implemented later.
890890
}
891891

@@ -940,27 +940,20 @@
940940
class bvh_node : public hittable {
941941
public:
942942
...
943-
bvh_node(const std::vector<shared_ptr<hittable>>& src_objects, size_t start, size_t end) {
943+
bvh_node(std::vector<shared_ptr<hittable>>& objects, size_t start, size_t end) {
944944
int axis = random_int(0,2);
945945

946946
auto comparator = (axis == 0) ? box_x_compare
947947
: (axis == 1) ? box_y_compare
948948
: box_z_compare;
949949

950-
auto objects = src_objects; // A modifiable array of the source scene objects
951-
952950
size_t object_span = end - start;
953951

954952
if (object_span == 1) {
955953
left = right = objects[start];
956954
} else if (object_span == 2) {
957-
if (comparator(objects[start], objects[start+1])) {
958-
left = objects[start];
959-
right = objects[start+1];
960-
} else {
961-
left = objects[start+1];
962-
right = objects[start];
963-
}
955+
left = objects[start];
956+
right = objects[start+1];
964957
} else {
965958
std::sort(objects.begin() + start, objects.begin() + end, comparator);
966959

@@ -1078,12 +1071,12 @@
10781071
class bvh_node : public hittable {
10791072
public:
10801073
...
1081-
bvh_node(const std::vector<shared_ptr<hittable>>& src_objects, size_t start, size_t end) {
1074+
bvh_node(std::vector<shared_ptr<hittable>>& objects, size_t start, size_t end) {
10821075
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
10831076
// Build the bounding box of the span of source objects.
10841077
bbox = aabb::empty;
1085-
for (int object_index=start; object_index < end; object_index++)
1086-
bbox = aabb(bbox, src_objects[object_index]->bounding_box());
1078+
for (size_t object_index=start; object_index < end; object_index++)
1079+
bbox = aabb(bbox, objects[object_index]->bounding_box());
10871080
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
10881081

10891082
...
@@ -1099,11 +1092,11 @@
10991092
class bvh_node : public hittable {
11001093
public:
11011094
...
1102-
bvh_node(const std::vector<shared_ptr<hittable>>& src_objects, size_t start, size_t end) {
1095+
bvh_node(std::vector<shared_ptr<hittable>>& objects, size_t start, size_t end) {
11031096
// Build the bounding box of the span of source objects.
11041097
bbox = aabb::empty;
1105-
for (int object_index=start; object_index < end; object_index++)
1106-
bbox = aabb(bbox, src_objects[object_index]->bounding_box());
1098+
for (size_t object_index=start; object_index < end; object_index++)
1099+
bbox = aabb(bbox, objects[object_index]->bounding_box());
11071100

11081101

11091102
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight

books/acknowledgments.md.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
- [Manas Kale](https://github.com/manas96)
5454
- Marcus Ottosson
5555
- [Mark Craig](https://github.com/mrmcsoftware)
56+
- Markus Boos
5657
- Matthew Heimlich
5758
- Nakata Daisuke
5859
- [Nate Rupsis](https://github.com/rupsis)

src/TheNextWeek/bvh.h

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,27 @@
2222

2323
class bvh_node : public hittable {
2424
public:
25-
bvh_node(const hittable_list& list) : bvh_node(list.objects, 0, list.objects.size()) {}
25+
bvh_node(hittable_list list) : bvh_node(list.objects, 0, list.objects.size()) {}
2626

27-
bvh_node(const std::vector<shared_ptr<hittable>>& src_objects, size_t start, size_t end) {
27+
bvh_node(std::vector<shared_ptr<hittable>>& objects, size_t start, size_t end) {
2828
// Build the bounding box of the span of source objects.
2929
bbox = aabb::empty;
3030
for (size_t object_index=start; object_index < end; object_index++)
31-
bbox = aabb(bbox, src_objects[object_index]->bounding_box());
31+
bbox = aabb(bbox, objects[object_index]->bounding_box());
3232

3333
int axis = bbox.longest_axis();
3434

3535
auto comparator = (axis == 0) ? box_x_compare
3636
: (axis == 1) ? box_y_compare
3737
: box_z_compare;
3838

39-
auto objects = src_objects; // A modifiable array of the source scene objects
40-
4139
size_t object_span = end - start;
4240

4341
if (object_span == 1) {
4442
left = right = objects[start];
4543
} else if (object_span == 2) {
46-
if (comparator(objects[start], objects[start+1])) {
47-
left = objects[start];
48-
right = objects[start+1];
49-
} else {
50-
left = objects[start+1];
51-
right = objects[start];
52-
}
44+
left = objects[start];
45+
right = objects[start+1];
5346
} else {
5447
std::sort(objects.begin() + start, objects.begin() + end, comparator);
5548

src/TheRestOfYourLife/bvh.h

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,27 @@
2222

2323
class bvh_node : public hittable {
2424
public:
25-
bvh_node(const hittable_list& list) : bvh_node(list.objects, 0, list.objects.size()) {}
25+
bvh_node(hittable_list list) : bvh_node(list.objects, 0, list.objects.size()) {}
2626

27-
bvh_node(const std::vector<shared_ptr<hittable>>& src_objects, size_t start, size_t end) {
27+
bvh_node(std::vector<shared_ptr<hittable>>& objects, size_t start, size_t end) {
2828
// Build the bounding box of the span of source objects.
2929
bbox = aabb::empty;
30-
for (int object_index=start; object_index < end; object_index++)
31-
bbox = aabb(bbox, src_objects[object_index]->bounding_box());
30+
for (size_t object_index=start; object_index < end; object_index++)
31+
bbox = aabb(bbox, objects[object_index]->bounding_box());
3232

3333
int axis = bbox.longest_axis();
3434

3535
auto comparator = (axis == 0) ? box_x_compare
3636
: (axis == 1) ? box_y_compare
3737
: box_z_compare;
3838

39-
auto objects = src_objects; // A modifiable array of the source scene objects
40-
4139
size_t object_span = end - start;
4240

4341
if (object_span == 1) {
4442
left = right = objects[start];
4543
} else if (object_span == 2) {
46-
if (comparator(objects[start], objects[start+1])) {
47-
left = objects[start];
48-
right = objects[start+1];
49-
} else {
50-
left = objects[start+1];
51-
right = objects[start];
52-
}
44+
left = objects[start];
45+
right = objects[start+1];
5346
} else {
5447
std::sort(objects.begin() + start, objects.begin() + end, comparator);
5548

0 commit comments

Comments
 (0)