MinGfx Toolkit
1.0
A minimal library for writing cross-platform (Windows, OSX, linux) graphics programs.
|
A Bounding Volume Hierarchy (BVH) data structure that can be used to accelerate ray-object intersection tests by carving up space into a hierarchy of partitions represented in a tree.
Each node of the tree is represented as an AABB (Axis-Aligned Bounding Box) that contains all of the nodes under it. Different objects can be stored inside each bounding box. For example, when a BVH is created for a mesh, each leaf node can contain a AABB that contains just a single triangle. Or, when a BVH is created for an entire scene, you could have each leaf node contain an entire mesh or other object within the scene. In each case, use AABB's set_user_data() and user_data() methods to store a handle for whetever you want to store inside the nodes.
#include <bvh.h>
Public Member Functions | |
BVH () | |
Initializes the class with an empty hierarchy. More... | |
virtual | ~BVH () |
void | CreateFromMesh (const Mesh &mesh) |
Creates a bounding volume hierarchy where each leaf node contains a single triangle from the mesh. More... | |
void | CreateFromListOfBoxes (const std::vector< AABB > &boxes) |
Creates a BVH where each leaf node contains one of the boxes passed in to the function. More... | |
std::vector< int > | IntersectAndReturnUserData (const Ray &r) const |
Traverse the BVH to find leaf nodes whose AABBs are intersected by the ray. More... | |
mingfx::BVH::BVH | ( | ) |
Initializes the class with an empty hierarchy.
|
virtual |
void mingfx::BVH::CreateFromListOfBoxes | ( | const std::vector< AABB > & | boxes | ) |
Creates a BVH where each leaf node contains one of the boxes passed in to the function.
void mingfx::BVH::CreateFromMesh | ( | const Mesh & | mesh | ) |
Creates a bounding volume hierarchy where each leaf node contains a single triangle from the mesh.
For leaf nodes, the triangle index can be retrieved with:
The user_data will be -1 for non-leaf nodes. Once the structure has been created, it can be used to perform fast ray-mesh intersection tests. See Ray::FastIntersectMesh().
std::vector<int> mingfx::BVH::IntersectAndReturnUserData | ( | const Ray & | r | ) | const |
Traverse the BVH to find leaf nodes whose AABBs are intersected by the ray.
These are candidates to test more thoroughly using whatever ray-object intersection test is appropriate for the objects stored inside the AABB. This routine returns the user_data for each AABB leaf node. In the case of a BVH created using CreateFromMesh, this means it stores the indices to the mesh triangles that should be tested for ray-triangle intersection.