@@ -245,16 +245,16 @@ class BVH : public Hittable {
245
245
continue ;
246
246
}
247
247
248
- /* Divide `centroids_bounds[axis]` into `NUM_BUCKETS` equally-sized regions (each of these
249
- regions is called a "bucket") along the current axis. For each bucket, we then need to
250
- compute its corresponding `BucketInfo`: the number of primitives whose centroids lie
251
- inside that bucket, and the AABB of all those centroids. The `BucketInfo` for bucket
248
+ /* Divide `centroids_bounds[axis]` into `NUM_BUCKETS` equally-sized regions (each of
249
+ these regions is called a "bucket") along the current axis. For each bucket, we then
250
+ need to compute its corresponding `BucketInfo`: the number of primitives whose centroids
251
+ lie inside that bucket, and the AABB of all those centroids. The `BucketInfo` for bucket
252
252
`i` is stored in `buckets[i]`. */
253
253
std::vector<BucketInfo> buckets (NUM_BUCKETS);
254
254
/* To compute the `BucketInfo` for all buckets (the number of primitives whose centroids
255
255
lie inside each bucket, as well as the AABB for the centroids), we iterate through the
256
- all the primitives in `curr_primitives`. For each, determine which bucket (region) it lies
257
- in, and update that bucket correspondingly . */
256
+ all the primitives in `curr_primitives`. For each primitive, we determine which bucket
257
+ (region) it lies in, and update that bucket accordingly . */
258
258
for (auto &primitive : curr_primitives) {
259
259
260
260
/* Compute the bucket along the current axis in which the current primitive's AABB's
@@ -368,8 +368,8 @@ class BVH : public Hittable {
368
368
* static_cast <double >(num_primitives_after_split);
369
369
}
370
370
371
- /* Update `min_split_cost`, `optimal_split_bucket`, and `optimal_split_axis` with all the
372
- buckets we tested along the current `axis` */
371
+ /* Update `min_split_cost`, `optimal_split_bucket`, and `optimal_split_axis` with all
372
+ the buckets we tested along the current `axis` */
373
373
for (size_t i = 0 ; i < NUM_BUCKETS - 1 ; ++i) {
374
374
/* Again, we are looking for the split that leads to the minimum cost. */
375
375
if (costs[i] < min_split_cost) {
@@ -410,14 +410,16 @@ class BVH : public Hittable {
410
410
distribution to the current INTERIOR node's left and right children. */
411
411
if (curr_primitives.size () > MAX_PRIMITIVES_IN_NODE || min_split_cost < leaf_cost) {
412
412
413
- /* Split `objects` into two sets, the first containing all objects that fall into bucket
414
- at most `optimal_split_bucket` along the axis `optimal_split_axis`, and the second containing
415
- all other objects. To do this, we use `std::partition` on `objects`. */
413
+ /* Split `objects` into two sets along the axis `optimal_split_axis`, the first of which
414
+ will contain all primitives whose bucket number is at most `optimal_split_bucket`, and
415
+ the second of which will contain all the other primitives. To perform the split, we
416
+ apply `std::partition` to `objects`. */
416
417
auto mid = std::partition (curr_primitives.begin (), curr_primitives.end (),
417
- [&](const std::shared_ptr<Hittable> &object) {
418
- /* Copied from above; determine which bucket the current `object`'s centroid lies
419
- in */
420
- auto offset = (object->get_aabb ().centroid ()[optimal_split_axis]
418
+ [&](const std::shared_ptr<Hittable> &primitive) {
419
+ /* First, we determine which bucket the current `primitive`'s centroid lies in.
420
+ The code to determine the bucket for a given primitive is the same as used
421
+ previously. */
422
+ auto offset = (primitive->get_aabb ().centroid ()[optimal_split_axis]
421
423
- centroids_bounds[optimal_split_axis].min )
422
424
/ centroids_bounds[optimal_split_axis].size ();
423
425
auto curr_bucket = static_cast <size_t >(static_cast <double >(NUM_BUCKETS) * offset);
@@ -437,8 +439,11 @@ class BVH : public Hittable {
437
439
axis `optimal_split_axis` that fall in buckets at most `optimal_split_bucket`; the
438
440
primitives with axis coordinates at most the optimal coordinate split threshold),
439
441
and the right child (`right_child`) will be built over the primitives to the right
440
- of the partition. */
441
- auto left_child = build_bvh_tree (curr_primitives.subspan (0 , mid)); /* `std::span::subspan`! */
442
+ of the partition. After applying `std::partition` to `curr_primitives`, this is
443
+ equivalent to saying that `left_child` will be built over `curr_primitives[0..mid)`,
444
+ and that `right_child` will be built over `curr_primitives[mid..)`. This is very
445
+ conveniently expressed using `std::span::subspan`. */
446
+ auto left_child = build_bvh_tree (curr_primitives.subspan (0 , mid));
442
447
auto right_child = build_bvh_tree (curr_primitives.subspan (mid));
443
448
444
449
/* Again, in this case, the current node will be an interior node. */
@@ -531,7 +536,7 @@ class BVH : public Hittable {
531
536
}
532
537
533
538
/* Flattens the BVH tree constructed in `build_bvh_tree` (rooted at `tree_root`), and
534
- stores the result in `linear_bvh_nodes`. This consumes the BVH tree in the result ,
539
+ stores the result in `linear_bvh_nodes`. This consumes the BVH tree in the process ,
535
540
freeing all associated memory. */
536
541
auto flatten_bvh_tree (std::unique_ptr<BVHTreeNode> tree_root) {
537
542
/* Because we computed `total_bvhnodes`, we are able to allocate the exact
0 commit comments