Antkeeper  0.0.1
bvh.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef ANTKEEPER_GEOM_BVH_HPP
21 #define ANTKEEPER_GEOM_BVH_HPP
22 
26 #include <cstdint>
27 #include <functional>
28 #include <span>
29 #include <vector>
30 
31 namespace geom {
32 
33 class brep_mesh;
34 
38 class bvh
39 {
40 public:
41  using visitor_type = std::function<void(std::uint32_t)>;
42 
48  explicit bvh(std::span<const bvh_primitive> primitives);
49 
55  explicit bvh(const brep_mesh& mesh);
56 
58  constexpr bvh() noexcept = default;
59 
65  void build(std::span<const bvh_primitive> primitives);
66 
72  void build(const brep_mesh& mesh);
73 
75  void clear();
76 
83  inline void visit(const geom::ray<float, 3>& ray, const visitor_type& f) const
84  {
85  if (m_node_count)
86  {
87  visit(m_nodes.front(), ray, f);
88  }
89  }
90 
92  [[nodiscard]] inline constexpr const std::vector<bvh_node>& nodes() const noexcept
93  {
94  return m_nodes;
95  }
96 
97 private:
98  void update_bounds(bvh_node& node, const std::span<const bvh_primitive>& primitives);
99 
106  void subdivide(bvh_node& node, const std::span<const bvh_primitive>& primitives);
107  void visit(const bvh_node& node, const geom::ray<float, 3>& ray, const visitor_type& f) const;
108 
109  std::vector<std::uint32_t> m_primitive_indices;
110  std::vector<bvh_node> m_nodes;
111  std::uint32_t m_node_count{};
112 };
113 
114 } // namespace geom
115 
116 #endif // ANTKEEPER_GEOM_BVH_HPP
Boundary representation (B-rep) of a mesh.
Definition: brep-mesh.hpp:34
Bounding volume hierarchy (BVH).
Definition: bvh.hpp:39
void clear()
Clears the BVH.
Definition: bvh.cpp:96
constexpr const std::vector< bvh_node > & nodes() const noexcept
Returns the BVH nodes.
Definition: bvh.hpp:92
void build(std::span< const bvh_primitive > primitives)
Constructs a BVH from a set of primitives.
Definition: bvh.cpp:39
constexpr bvh() noexcept=default
Constructs an empty BVH.
void visit(const geom::ray< float, 3 > &ray, const visitor_type &f) const
Visits the primitive indices of all BVH nodes that intersect a ray.
Definition: bvh.hpp:83
std::function< void(std::uint32_t)> visitor_type
Definition: bvh.hpp:41
Geometric algorithms.
Single node in a bounding volume hierarchy.
Definition: bvh-node.hpp:32
BVH primitive.
Half of a line proceeding from an initial point.
Definition: ray.hpp:38