28 glm::vec2
min{std::numeric_limits<float>::infinity()};
29 glm::vec2
max{-std::numeric_limits<float>::infinity()};
31 constexpr AABB2D() noexcept = default;
33 constexpr
AABB2D(const glm::vec2 &
min, const glm::vec2 &
max) noexcept
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)
43 constexpr bool contains(
const glm::vec2 &p)
const noexcept
45 return p.x >=
min.x && p.x <=
max.x && p.y >=
min.y && p.y <=
max.y;
50 return min.x <= other.max.x &&
max.x >= other.min.x &&
51 min.y <= other.max.y &&
max.y >= other.min.y;
54 constexpr void expand(
const glm::vec2 &p)
noexcept
62 min = glm::min(
min, other.min);
63 max = glm::max(
max, other.max);
66 constexpr glm::vec2
center() const noexcept
71 constexpr glm::vec2
extent() const noexcept
98 std::min(
p0.y,
p1.y) - padding,
99 std::max(
p0.x,
p1.x) + padding,
100 std::max(
p0.y,
p1.y) + padding);
103 constexpr glm::vec2
dir() const noexcept
110 return d.x *
d.x +
d.y *
d.y;
139 explicit BVH2D(
const std::vector<Segment2D> &segments,
float padding = 0.f);
144 void build(
const std::vector<Segment2D> &segments,
float padding = 0.f);
149 void clear() noexcept;
153 return nodes.empty();
158 return nodes[0].bbox;
166 std::vector<uint32_t> &out_seg_indices)
const;
172 std::vector<uint32_t> &out_seg_indices)
const;
185 std::vector<Node> nodes;
186 std::vector<Segment2D> segments;
188 int32_t build_recursive(uint32_t start, uint32_t count,
float padding);
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
void build(const std::vector< Segment2D > &segments, float padding=0.f)
Rebuild BVH with given segments and padding.
Definition bvh.cpp:17
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
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