Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -28,6 +28,8 @@ then.
- Change - Reworked the AABB chapter (#1236)
- New - add section on alternative 2D primitives such as triangle, ellipse and annulus (#1204,
#1205)
- Change - changed bvh construction (removed const qualifer for objects vector) so sorting is done
in place and copying of vector is avoided, better bvh build performance (#1388, #1391)

### The Rest of Your Life

Expand Down
29 changes: 11 additions & 18 deletions books/RayTracingTheNextWeek.html
Original file line number Diff line number Diff line change
Expand Up @@ -899,9 +899,9 @@

class bvh_node : public hittable {
public:
bvh_node(const hittable_list& list) : bvh_node(list.objects, 0, list.objects.size()) {}
bvh_node(hittable_list list) : bvh_node(list.objects, 0, list.objects.size()) {}

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

Expand Down Expand Up @@ -956,27 +956,20 @@
class bvh_node : public hittable {
public:
...
bvh_node(const std::vector<shared_ptr<hittable>>& src_objects, size_t start, size_t end) {
bvh_node(std::vector<shared_ptr<hittable>>& objects, size_t start, size_t end) {
int axis = random_int(0,2);

auto comparator = (axis == 0) ? box_x_compare
: (axis == 1) ? box_y_compare
: box_z_compare;

auto objects = src_objects; // A modifiable array of the source scene objects

size_t object_span = end - start;

if (object_span == 1) {
left = right = objects[start];
} else if (object_span == 2) {
if (comparator(objects[start], objects[start+1])) {
left = objects[start];
right = objects[start+1];
} else {
left = objects[start+1];
right = objects[start];
}
left = objects[start];
right = objects[start+1];
} else {
std::sort(objects.begin() + start, objects.begin() + end, comparator);

Expand Down Expand Up @@ -1094,12 +1087,12 @@
class bvh_node : public hittable {
public:
...
bvh_node(const std::vector<shared_ptr<hittable>>& src_objects, size_t start, size_t end) {
bvh_node(std::vector<shared_ptr<hittable>>& objects, size_t start, size_t end) {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
// Build the bounding box of the span of source objects.
bbox = aabb::empty;
for (int object_index=start; object_index < end; object_index++)
bbox = aabb(bbox, src_objects[object_index]->bounding_box());
for (size_t object_index=start; object_index < end; object_index++)
bbox = aabb(bbox, objects[object_index]->bounding_box());
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++

...
Expand All @@ -1115,11 +1108,11 @@
class bvh_node : public hittable {
public:
...
bvh_node(const std::vector<shared_ptr<hittable>>& src_objects, size_t start, size_t end) {
bvh_node(std::vector<shared_ptr<hittable>>& objects, size_t start, size_t end) {
// Build the bounding box of the span of source objects.
bbox = aabb::empty;
for (int object_index=start; object_index < end; object_index++)
bbox = aabb(bbox, src_objects[object_index]->bounding_box());
for (size_t object_index=start; object_index < end; object_index++)
bbox = aabb(bbox, objects[object_index]->bounding_box());


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
Expand Down
1 change: 1 addition & 0 deletions books/acknowledgments.md.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
- [Manas Kale](https://github.com/manas96)
- Marcus Ottosson
- [Mark Craig](https://github.com/mrmcsoftware)
- Markus Boos
- Matthew Heimlich
- Nakata Daisuke
- [Nate Rupsis](https://github.com/rupsis)
Expand Down
17 changes: 5 additions & 12 deletions src/TheNextWeek/bvh.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,27 @@

class bvh_node : public hittable {
public:
bvh_node(const hittable_list& list) : bvh_node(list.objects, 0, list.objects.size()) {}
bvh_node(hittable_list list) : bvh_node(list.objects, 0, list.objects.size()) {}

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

int axis = bbox.longest_axis();

auto comparator = (axis == 0) ? box_x_compare
: (axis == 1) ? box_y_compare
: box_z_compare;

auto objects = src_objects; // A modifiable array of the source scene objects

size_t object_span = end - start;

if (object_span == 1) {
left = right = objects[start];
} else if (object_span == 2) {
if (comparator(objects[start], objects[start+1])) {
left = objects[start];
right = objects[start+1];
} else {
left = objects[start+1];
right = objects[start];
}
left = objects[start];
right = objects[start+1];
} else {
std::sort(objects.begin() + start, objects.begin() + end, comparator);

Expand Down
19 changes: 6 additions & 13 deletions src/TheRestOfYourLife/bvh.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,27 @@

class bvh_node : public hittable {
public:
bvh_node(const hittable_list& list) : bvh_node(list.objects, 0, list.objects.size()) {}
bvh_node(hittable_list list) : bvh_node(list.objects, 0, list.objects.size()) {}

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

int axis = bbox.longest_axis();

auto comparator = (axis == 0) ? box_x_compare
: (axis == 1) ? box_y_compare
: box_z_compare;

auto objects = src_objects; // A modifiable array of the source scene objects

size_t object_span = end - start;

if (object_span == 1) {
left = right = objects[start];
} else if (object_span == 2) {
if (comparator(objects[start], objects[start+1])) {
left = objects[start];
right = objects[start+1];
} else {
left = objects[start+1];
right = objects[start];
}
left = objects[start];
right = objects[start+1];
} else {
std::sort(objects.begin() + start, objects.begin() + end, comparator);

Expand Down