Intended Behavior:
The class BVH_Node is supposed to be a binary tree of AABBs. This implementation is a one-class approach which only stores the root.
class BVH_Node:
def __init__(self, list: HittableList):
"""Constructs a BVH node from a `HittableList`."""
self.bbox: AABB # (ROOT)
self.left: 'BVH_Node'
self.right: 'BVH_Node'
# list.objects is passed by mutable and passed by reference
self.construct_bvh_tree(list.objects, 0, len(list.objects))
...
def construct_bvh_tree(self, _src_objects: list[Hittable], start: int, end: int) :
"""
Constructs a BVH node from a subarray of a list of objects. Creates AABBs from subarrays of Hittables.
"""
objects = _src_objects[:]
axis = random.randint(0, 2)
axis_key = self.x_axis_key if axis == 0 else self.x_axis_key if axis == 1 else self.z_axis_key
object_span = end - start
if object_span == 1:
self.left = self.right = objects[start]
elif object_span == 2:
if self.box_compare(objects[start], objects[start+1], axis):
self.left = objects[start];
self.right = objects[start+1];
else:
self.left = objects[start+1];
self.right = objects[start];
else:
sys.stderr.write(f"start: {type(start)}\n")
sys.stderr.write(f"objects span: {type(object_span)}\n")
sys.stderr.write(f"end: {type(end)}\n")
objects[start:end] = sorted(objects[start:end], key=axis_key)
mid: int = (start + object_span) // 2
# BUG (POTENTIAL): Assignment may be incorrect
self.left = self.construct_bvh_tree(objects, start, mid)
self.right = self.construct_bvh_tree(objects, mid, end)
self.bbox = AABB.merge(self.left.bounding_box, self.right.bounding_box)