opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
bvh.h
Go to the documentation of this file.
1#pragma once
2
8
9#include <cstdint>
10#include <cstddef>
11#include <functional>
12#include <optional>
13#include <span>
14#include <vector>
15
16namespace osc { struct Ray; }
17
18namespace osc
19{
20 // a bounding volume hierarchy (BVH) of numerically IDed `AABB`s
21 //
22 // the `AABB`s may be computed from triangles, commonly called a "triangle BVH"
23 class BVH final {
24 public:
25 void clear();
26
27 // triangle `BVH`es
28 //
29 // `prim.id()` will refer to the index of the first vertex in the triangle
31 std::span<const Vector3> vertices,
32 std::span<const uint16_t> indices
33 );
35 std::span<const Vector3> vertices,
36 std::span<const uint32_t> indices
37 );
38
39 // returns the position of the closest ray-triangle collision along the ray, if any
40 std::optional<BVHCollision> closest_ray_indexed_triangle_collision(
41 std::span<const Vector3> vertices,
42 std::span<const uint16_t> indices,
43 const Ray&
44 ) const;
45 std::optional<BVHCollision> closest_ray_indexed_triangle_collision(
46 std::span<const Vector3> vertices,
47 std::span<const uint32_t> indices,
48 const Ray&
49 ) const;
50
51 // `AABB` `BVH`es
52 //
53 // `prim.id()` will refer to the index of the `AABB`
54 void build_from_aabbs(std::span<const AABB>);
55
56 // calls the callback with each collision between the `Ray` and an `AABB` in
57 // the `BVH`
58 void for_each_ray_aabb_collision(const Ray&, const std::function<void(BVHCollision)>&) const;
59
60 // returns `true` if the `BVH` contains no `BVHNode`s
61 [[nodiscard]] bool empty() const;
62
63 // returns the maximum depth of the `BVH` tree
64 size_t max_depth() const;
65
66 // returns the `AABB` of the root node, or `std::nullopt` if there are no nodes in
67 // the tree
68 std::optional<AABB> bounds() const;
69
70 // calls the given function with each leaf node in the tree
71 void for_each_leaf_node(const std::function<void(const BVHNode&)>&) const;
72
73 // calls the given function with each leaf or inner node in the tree
74 void for_each_leaf_or_inner_node(const std::function<void(const BVHNode&)>&) const;
75
76 private:
77 // nodes in the hierarchy
78 std::vector<BVHNode> nodes_;
79
80 // primitives (triangles, `AABB`s) that the nodes reference
81 std::vector<BVHPrim> prims_;
82 };
83}
Definition bvh_node.h:12
Definition bvh.h:23
void build_from_aabbs(std::span< const AABB >)
std::optional< BVHCollision > closest_ray_indexed_triangle_collision(std::span< const Vector3 > vertices, std::span< const uint32_t > indices, const Ray &) const
std::optional< BVHCollision > closest_ray_indexed_triangle_collision(std::span< const Vector3 > vertices, std::span< const uint16_t > indices, const Ray &) const
void for_each_ray_aabb_collision(const Ray &, const std::function< void(BVHCollision)> &) const
std::optional< AABB > bounds() const
void build_from_indexed_triangles(std::span< const Vector3 > vertices, std::span< const uint16_t > indices)
void for_each_leaf_or_inner_node(const std::function< void(const BVHNode &)> &) const
void build_from_indexed_triangles(std::span< const Vector3 > vertices, std::span< const uint32_t > indices)
void clear()
bool empty() const
void for_each_leaf_node(const std::function< void(const BVHNode &)> &) const
size_t max_depth() const
Definition custom_decoration_generator.h:5
constexpr U to(T &&value)
Definition conversion.h:56
Definition bvh_collision.h:11
Definition ray.h:13