Two implementation details stand in the way of a fully-working bounding volume hierarchy (BVH) that uses axis-aligned bounding boxes (AABBs to accelerate ray-scene intersections. First, we need the ability to construct an axis-aligned bounding box from a given object or collection of objects. Secondly, we need to be able to intersect a ray with an AABB.
An axis-aligned bounding box can be described by its minimum and maximum extent in each dimension , , and . So we might store one in a struct such as
1struct AABB
2 xmin::Float
3 xmax::Float
4 ymin::Float
5 ymax::Float
6 zmin::Float
7 zmax::Float
8 contained_objects::Array
9end
Our first set of tasks is to make sure we can compute an AABB for each type of object that our scenes may contain. In our raytracer, these are spheres and triangles. Since we'll be packing objects into a hierarchy of AABBs, we'll also need to know how to compute the AABB of a collection of AABBs.
Write pseudocode to construct an AABB for a sphere:
x1function sphere_aabb(center::Vec3, radius::Float64)
2
3end
Write pseudocode to construct an AABB for a triangle:
xxxxxxxxxx
31function tri_aabb(a::Vec3, b::Vec3, c::Vec3)
2
3end
Write pseudocode to construct an AABB that contains a collection of AABBs. As a reminder, the type Array{AABB}, 1}
signifies a 1-dimensional Array of type AABB
:
xxxxxxxxxx
31function aabbs_aabb(objs::Array{AABB, 1})
2
3end
At this point, we can use these primitives to construct a hierarchy of bounding boxes using the procedure described in lecture. Then all we need to do is determine whether ray hits an AABB (in which case we need to check whether it hits each of its contained objects) or misses the AABB (in which case we can prune its entire subtree!
Let's build up to the 3D case by starting simpler. The observation here is that a an axis-aligned 3D cuboid can be expressed as the intersection of three "slabs", where a "slab" is the region of space between two parallel (axis-aligned, in this case) planes.
If you have time, go ahead and sketch out some pseudocode for bounding volume hierarchy construction:
Triangle
or a Sphere
, write pseudocode construct a bounding volume hierarchy. At each level of the tree, divide the objects in half by sorting their position (e.g., the point at their center) along a coordinate axis and create a subtree containing each half of the objects. Cycle through the , , and axes at subsequent levels of the tree.