HighMap library (C++)
Loading...
Searching...
No Matches
bvh.hpp
Go to the documentation of this file.
1/* Copyright (c) 2026 Otto Link. Distributed under the terms of the GNU General
2 Public License. The full license is in the file LICENSE, distributed with
3 this software. */
4
10#pragma once
11
12#include <algorithm>
13#include <cstddef>
14#include <cstdint>
15#include <limits>
16#include <vector>
17
18#include <glm/glm.hpp>
19
20namespace hmap
21{
22
26struct AABB2D
27{
28 glm::vec2 min{std::numeric_limits<float>::infinity()};
29 glm::vec2 max{-std::numeric_limits<float>::infinity()};
30
31 constexpr AABB2D() noexcept = default;
32
33 constexpr AABB2D(const glm::vec2 &min, const glm::vec2 &max) noexcept
34 : min(min), max(max)
35 {
36 }
37
38 constexpr AABB2D(float min_x, float min_y, float max_x, float max_y) noexcept
39 : min(min_x, min_y), max(max_x, max_y)
40 {
41 }
42
43 constexpr bool contains(const glm::vec2 &p) const noexcept
44 {
45 return p.x >= min.x && p.x <= max.x && p.y >= min.y && p.y <= max.y;
46 }
47
48 constexpr bool overlaps(const AABB2D &other) const noexcept
49 {
50 return min.x <= other.max.x && max.x >= other.min.x &&
51 min.y <= other.max.y && max.y >= other.min.y;
52 }
53
54 constexpr void expand(const glm::vec2 &p) noexcept
55 {
56 min = glm::min(min, p);
57 max = glm::max(max, p);
58 }
59
60 constexpr void expand(const AABB2D &other) noexcept
61 {
62 min = glm::min(min, other.min);
63 max = glm::max(max, other.max);
64 }
65
66 constexpr glm::vec2 center() const noexcept
67 {
68 return 0.5f * (min + max);
69 }
70
71 constexpr glm::vec2 extent() const noexcept
72 {
73 return max - min;
74 }
75};
76
81{
82 glm::vec2 p0{0.f};
83 glm::vec2 p1{0.f};
84 float v0{0.f};
85 float v1{0.f};
86 float s0{0.f};
87 float s1{0.f};
88 size_t k0{0};
89 size_t k1{0};
90 uint32_t path_index{0};
91 uint32_t id{0};
92 glm::vec2 d{0.f};
93 float inv_len_sq{0.f};
94
95 constexpr AABB2D bounds(float padding = 0.f) const noexcept
96 {
97 return AABB2D(std::min(p0.x, p1.x) - padding,
98 std::min(p0.y, p1.y) - padding,
99 std::max(p0.x, p1.x) + padding,
100 std::max(p0.y, p1.y) + padding);
101 }
102
103 constexpr glm::vec2 dir() const noexcept
104 {
105 return d;
106 }
107
108 constexpr float len_sq() const noexcept
109 {
110 return d.x * d.x + d.y * d.y;
111 }
112};
113
117class BVH2D
118{
119public:
120 struct Node
121 {
123 int32_t left{-1};
124 int32_t right{-1};
125 uint32_t seg_start{0};
126 uint32_t seg_count{0};
127
128 constexpr bool is_leaf() const noexcept
129 {
130 return left < 0;
131 }
132 };
133
134 BVH2D() = default;
135
139 explicit BVH2D(const std::vector<Segment2D> &segments, float padding = 0.f);
140
144 void build(const std::vector<Segment2D> &segments, float padding = 0.f);
145
149 void clear() noexcept;
150
151 bool empty() const noexcept
152 {
153 return nodes.empty();
154 }
155
156 const AABB2D &root_bbox() const noexcept
157 {
158 return nodes[0].bbox;
159 }
160
165 void query_box(const AABB2D &query_box,
166 std::vector<uint32_t> &out_seg_indices) const;
167
171 void query_point(const glm::vec2 &p,
172 std::vector<uint32_t> &out_seg_indices) const;
173
174 const std::vector<Segment2D> &get_segments() const noexcept
175 {
176 return segments;
177 }
178
179 const std::vector<Node> &get_nodes() const noexcept
180 {
181 return nodes;
182 }
183
184private:
185 std::vector<Node> nodes;
186 std::vector<Segment2D> segments;
187
188 int32_t build_recursive(uint32_t start, uint32_t count, float padding);
189};
190
191} // namespace hmap
void query_box(const AABB2D &query_box, std::vector< uint32_t > &out_seg_indices) const
Query all segments whose padded bounding boxes overlap with query_box.
Definition bvh.cpp:108
const AABB2D & root_bbox() const noexcept
Definition bvh.hpp:156
void clear() noexcept
Clear BVH.
Definition bvh.cpp:30
bool empty() const noexcept
Definition bvh.hpp:151
const std::vector< Segment2D > & get_segments() const noexcept
Definition bvh.hpp:174
void query_point(const glm::vec2 &p, std::vector< uint32_t > &out_seg_indices) const
Query all segments whose padded bounding boxes contain point p.
Definition bvh.cpp:140
const std::vector< Node > & get_nodes() const noexcept
Definition bvh.hpp:179
BVH2D()=default
void build(const std::vector< Segment2D > &segments, float padding=0.f)
Rebuild BVH with given segments and padding.
Definition bvh.cpp:17
Definition opencv.hpp:20
2D Axis-Aligned Bounding Box (AABB).
Definition bvh.hpp:27
glm::vec2 min
Definition bvh.hpp:28
constexpr void expand(const glm::vec2 &p) noexcept
Definition bvh.hpp:54
constexpr AABB2D() noexcept=default
constexpr glm::vec2 center() const noexcept
Definition bvh.hpp:66
glm::vec2 max
Definition bvh.hpp:29
constexpr AABB2D(float min_x, float min_y, float max_x, float max_y) noexcept
Definition bvh.hpp:38
constexpr glm::vec2 extent() const noexcept
Definition bvh.hpp:71
constexpr bool overlaps(const AABB2D &other) const noexcept
Definition bvh.hpp:48
constexpr bool contains(const glm::vec2 &p) const noexcept
Definition bvh.hpp:43
constexpr void expand(const AABB2D &other) noexcept
Definition bvh.hpp:60
Definition bvh.hpp:121
uint32_t seg_start
Definition bvh.hpp:125
int32_t right
Right child index or -1 if leaf.
Definition bvh.hpp:124
constexpr bool is_leaf() const noexcept
Definition bvh.hpp:128
uint32_t seg_count
Definition bvh.hpp:126
int32_t left
Left child index or -1 if leaf.
Definition bvh.hpp:123
AABB2D bbox
Definition bvh.hpp:122
2D Directed line segment with metadata.
Definition bvh.hpp:81
uint32_t path_index
Definition bvh.hpp:90
size_t k0
Definition bvh.hpp:88
float inv_len_sq
Definition bvh.hpp:93
constexpr float len_sq() const noexcept
Definition bvh.hpp:108
glm::vec2 p1
Definition bvh.hpp:83
constexpr AABB2D bounds(float padding=0.f) const noexcept
Definition bvh.hpp:95
glm::vec2 p0
Definition bvh.hpp:82
constexpr glm::vec2 dir() const noexcept
Definition bvh.hpp:103
float v1
Definition bvh.hpp:85
float s1
Definition bvh.hpp:87
float s0
Definition bvh.hpp:86
glm::vec2 d
Definition bvh.hpp:92
size_t k1
Definition bvh.hpp:89
float v0
Definition bvh.hpp:84