Arty
bvh.h
1 #ifndef BVH_H
2 #define BVH_H
3 
4 #include <memory>
5 #include <cstdint>
6 
7 #include "float3.h"
8 #include "intersect.h"
9 #include "bbox.h"
10 
12 class Bvh {
13 public:
15  void build(const float3* verts, const uint32_t* indices, size_t num_tris);
16 
18  void traverse(const Ray& ray, Hit& hit, bool any = false) const;
19 
21  size_t node_count() const { return num_nodes; }
22 
23 private:
24  void build(const BBox*, const float3*, size_t);
25 
26  friend struct BvhBuilder;
27 
28  struct Node {
29  float3 min;
30  union {
31  int32_t child;
32  int32_t first_prim;
33  };
34  float3 max;
35  union {
36  int32_t num_prims;
37  int32_t axis;
38  };
39  };
40 
41  std::unique_ptr<Node[]> nodes;
42  std::unique_ptr<uint32_t[]> prim_ids;
43  std::unique_ptr<PrecomputedTri[]> tris;
44  size_t num_nodes;
45 };
46 
47 #endif // BVH_H
void build(const float3 *verts, const uint32_t *indices, size_t num_tris)
Builds a BVH given a list of vertices and a list of indices.
Bounding box represented by its two extreme points.
Definition: bbox.h:9
Definition: float3.h:10
Bounding Volume Hierarchy.
Definition: bvh.h:12
size_t node_count() const
Returns the number of nodes in the BVH.
Definition: bvh.h:21
Ray-triangle hit information.
Definition: intersect.h:28
void traverse(const Ray &ray, Hit &hit, bool any=false) const
Traverses the BVH in order to find the closest intersection, or any intersection if &#39;any&#39; is set...
Ray defined as org + t * dir, with t in [tmin, tmax].
Definition: intersect.h:11