cgv
Loading...
Searching...
No Matches
bvh.h
1#pragma once
2
3#include <limits>
4#include <memory>
5#include <vector>
6
7#include <cgv/math/fray.h>
8#include <cgv/math/fvec.h>
9#include <cgv/media/axis_aligned_box.h>
10
11#include "lib_begin.h"
12
13namespace cgv {
14namespace data {
15
19 float t = std::numeric_limits<float>::max();
24};
25
28public:
30 virtual ~ray_intersectable() {}
31
33 virtual cgv::box3 get_bounds() const = 0;
34
43 virtual bool intersect(const cgv::ray3& ray, ray_intersection_info& info) const = 0;
44};
45
48struct bvh_node {
52 std::vector<size_t> primitive_indices;
54 std::unique_ptr<bvh_node> child_a;
56 std::unique_ptr<bvh_node> child_b;
57};
58
61struct bvh_result {
63 bool is_hit = false;
67 size_t primitive_index = std::numeric_limits<size_t>::max();
69 const ray_intersectable* primitive = nullptr;
70};
71
119class CGV_API bvh {
120public:
129 void build(const std::vector<const ray_intersectable*>& primitives, size_t max_depth = 8);
130
134 bvh_result closest_intersection(const cgv::ray3& ray) const;
135
136private:
137 // Keep a list of primitives inside the hierarchy. The nodes contain indices that point to this list.
138 std::vector<const ray_intersectable*> _primitives;
140 int _max_depth = 8;
142 std::unique_ptr<bvh_node> _root = std::make_unique<bvh_node>();
143
149 void split(bvh_node* node, int depth = 0);
150};
151
152} // namespace data
153} // namespace cgv
154
155#include <cgv/config/lib_end.h>
A Bounding Volume Hierarchy(BVH) using axis-aligned bounding boxes to partition primitives into a bin...
Definition bvh.h:119
An interface to define objects that are intersectable with a ray.
Definition bvh.h:27
virtual bool intersect(const cgv::ray3 &ray, ray_intersection_info &info) const =0
Test if the object is intersected by the given ray.
virtual cgv::box3 get_bounds() const =0
Return the axis-aligned bounding box of the primitive.
this header is dependency free
Definition print.h:11
A single node of a bounding volume hierarchy.
Definition bvh.h:48
std::vector< size_t > primitive_indices
The list of primitive indices contained in this node. After building the tree, only leaf nodes will h...
Definition bvh.h:52
cgv::box3 bounds
The node's axis aligned bounding box.
Definition bvh.h:50
std::unique_ptr< bvh_node > child_b
The second child node of this node.
Definition bvh.h:56
std::unique_ptr< bvh_node > child_a
The first child node of this node.
Definition bvh.h:54
The result of a BVH intersection test.
Definition bvh.h:61
size_t primitive_index
The index of the hit primitive as given during BVH construction.
Definition bvh.h:67
ray_intersection_info intersection
Intersection information of the hit.
Definition bvh.h:65
bool is_hit
True if a primitive was hit.
Definition bvh.h:63
const ray_intersectable * primitive
A pointer to the hit primitive.
Definition bvh.h:69
Holds information about a single intersection of a ray and primitive.
Definition bvh.h:17
cgv::vec3 normal
The intersection surface normal.
Definition bvh.h:21
cgv::vec2 uv
The intersection surface texture coordinates.
Definition bvh.h:23
float t
The ray parameter.
Definition bvh.h:19
Struct template for fixed n-dimensional rays with arbitrary data type defined by origin and direction...
Definition fray.h:11