Math v0.1.0
Bvh

Bounding Volume Hierarchy container. (BVH tree) More...

#include <bvh.hpp>

Classes

struct  Node
 BVH node container. More...
 

Public Member Functions

 Bvh (const uint8 *vertices, const uint8 *indices, const Aabb &rootAabb, uint32 indexCount, uint32 vertexSize, uint32 indexSize, const f32x4 *centroids=nullptr)
 Creates a new BVH from the triangle array.
 
 Bvh (const Aabb *aabbs, const Aabb &rootAabb, uint32 aabbCount, const f32x4 *centroids=nullptr)
 Creates a new BVH from the AABB array.
 
 Bvh ()=default
 Creates a new empty BVH.
 
const vector< Node > & getNodes () const noexcept
 Returns BVH node array.
 
const vector< uint32 > & getPrimitives () const noexcept
 Returns BVH primitive array.
 
const vector< f32x4 > & getCentroids () const noexcept
 Returns BVH centroid array.
 
stack< Node * > & getNodeStack () noexcept
 Returns BVH node stack cache.
 
void recreate (const uint8 *vertices, const uint8 *indices, const Aabb &rootAabb, uint32 indexCount, uint32 vertexSize, uint32 indexSize, const f32x4 *centroids=nullptr)
 Recreates BVH from the triangle array.
 
void recreate (const Aabb *aabbs, const Aabb &rootAabb, uint32 aabbCount, const f32x4 *centroids=nullptr)
 Recreates BVH from the AABB array.
 
uint32 collectInFrustum (const Plane *planes, uint8 planeCount, uint32 *primitives, stack< Node * > *nodeStack=nullptr)
 Collects all primitives inside specified frustum.
 
void traverse (const std::function< bool(const Bvh::Node &)> &isIntersected, const std::function< void(uint32, uint32)> &onLeaf)
 Traverses BVH tree.
 

Protected Attributes

vector< Nodenodes
 
vector< uint32primitives
 
vector< f32x4centroids
 
stack< Node * > nodeStack
 

Detailed Description

Bounding Volume Hierarchy container. (BVH tree)

A data structure commonly used in ray tracing, and collision detection to efficiently manage and process large sets of geometric objects, such as triangles or 3D models. The BVH organizes these objects into a hierarchical tree of bounding volumes (usually axis-aligned bounding boxes or spheres), allowing for faster queries related to visibility, collisions, and intersections.

Constructor & Destructor Documentation

◆ Bvh() [1/2]

Bvh ( const uint8 * vertices,
const uint8 * indices,
const Aabb & rootAabb,
uint32 indexCount,
uint32 vertexSize,
uint32 indexSize,
const f32x4 * centroids = nullptr )

Creates a new BVH from the triangle array.

Parameters
[in]verticestarget vertex array
[in]indicestarget index array
[in]rootAabbroot node AABB
indexCountindex array size
vertexSizesize of the vertex in bytes
indexSizesize of the index in bytes
[in]centroidsprecalculated centroid array

◆ Bvh() [2/2]

Bvh ( const Aabb * aabbs,
const Aabb & rootAabb,
uint32 aabbCount,
const f32x4 * centroids = nullptr )

Creates a new BVH from the AABB array.

Parameters
[in]aabbstarget AABB array
[in]rootAabbroot node AABB
aabbCountAABB array size
[in]centroidsprecalculated centroid array

Member Function Documentation

◆ recreate() [1/2]

void recreate ( const uint8 * vertices,
const uint8 * indices,
const Aabb & rootAabb,
uint32 indexCount,
uint32 vertexSize,
uint32 indexSize,
const f32x4 * centroids = nullptr )

Recreates BVH from the triangle array.

Parameters
[in]verticestarget vertex array
[in]indicestarget index array
[in]rootAabbroot node AABB
indexCountindex array size
vertexSizesize of the vertex in bytes
indexSizesize of the index in bytes
[in]centroidsprecalculated centroid array or null

◆ recreate() [2/2]

void recreate ( const Aabb * aabbs,
const Aabb & rootAabb,
uint32 aabbCount,
const f32x4 * centroids = nullptr )

Recreates BVH from the AABB array.

Parameters
[in]aabbstarget AABB array
[in]rootAabbroot node AABB
aabbCountAABB array size
[in]centroidsprecalculated centroid array or null

◆ collectInFrustum()

uint32 collectInFrustum ( const Plane * planes,
uint8 planeCount,
uint32 * primitives,
stack< Node * > * nodeStack = nullptr )

Collects all primitives inside specified frustum.

Parameters
[in]planestarget frustum planes
planeCountfrustum plane count
[out]primitivescollected primitives array
[in]nodeStacklocal node stack or null

◆ traverse()

void traverse ( const std::function< bool(const Bvh::Node &)> & isIntersected,
const std::function< void(uint32, uint32)> & onLeaf )
inline

Traverses BVH tree.

Parameters
[in]isIntersectedcalls on each node to check for intersection
[in]onLeafcalls on leaf nodes to process primitives