HighMap library (C++)
Loading...
Searching...
No Matches
kd_tree.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 <cstddef>
13#include <memory>
14#include <utility>
15#include <vector>
16
17#include <glm/glm.hpp>
18
19namespace hmap
20{
21
22class Cloud;
23struct Point;
24
34class KDTree
35{
36public:
37 // ==========================================================================
38 // Constructors & Destructor
39 // ==========================================================================
40
45
50
56 KDTree(const std::vector<float> &x, const std::vector<float> &y);
57
62 explicit KDTree(const std::vector<Point> &points);
63
68 explicit KDTree(const Cloud &cloud);
69
73 KDTree(KDTree &&other) noexcept;
74
78 KDTree &operator=(KDTree &&other) noexcept;
79
80 KDTree(const KDTree &) = delete;
81 KDTree &operator=(const KDTree &) = delete;
82
83 // ==========================================================================
84 // Capacity & Status
85 // ==========================================================================
86
91 bool empty() const noexcept;
92
97 size_t size() const noexcept;
98
99 // ==========================================================================
100 // Queries
101 // ==========================================================================
102
112 glm::vec2 compute_neighbor_distance_range(size_t k_neighbors) const;
113
123 size_t nearest(float x_query, float y_query) const;
124
128 size_t nearest(const Point &p) const;
129
133 size_t nearest(const glm::vec2 &xy) const;
134
142 std::pair<size_t, float> nearest_with_distance_squared(float x_query,
143 float y_query) const;
144
149 std::pair<size_t, float> nearest_with_distance_squared(const Point &p) const;
150
155 std::pair<size_t, float> nearest_with_distance_squared(
156 const glm::vec2 &xy) const;
157
167 void neighbor_search(float x_query,
168 float y_query,
169 size_t k_neighbors,
170 std::vector<size_t> &indices,
171 std::vector<float> &distances) const;
172
176 void neighbor_search(const Point &p,
177 size_t k_neighbors,
178 std::vector<size_t> &indices,
179 std::vector<float> &distances) const;
180
184 void neighbor_search(const glm::vec2 &xy,
185 size_t k_neighbors,
186 std::vector<size_t> &indices,
187 std::vector<float> &distances) const;
188
197 std::vector<std::pair<size_t, float>> radius_search(float x_query,
198 float y_query,
199 float radius) const;
200
204 std::vector<std::pair<size_t, float>> radius_search(const Point &p,
205 float radius) const;
206
210 std::vector<std::pair<size_t, float>> radius_search(const glm::vec2 &xy,
211 float radius) const;
212
213 struct Impl;
214
215private:
216 std::unique_ptr<Impl> p_impl;
217};
218
219} // namespace hmap
Represents a collection of unordered points in 2D space.
Definition cloud.hpp:61
size_t size() const noexcept
Get the number of points indexed by the KD-tree.
Definition kd_tree.cpp:254
glm::vec2 compute_neighbor_distance_range(size_t k_neighbors) const
Estimate the range of neighbor distances across the dataset.
Definition kd_tree.cpp:263
KDTree()
Construct an empty KD-tree.
std::pair< size_t, float > nearest_with_distance_squared(float x_query, float y_query) const
Find the index and squared distance of the nearest neighbor.
Definition kd_tree.cpp:315
bool empty() const noexcept
Check whether the KD-tree is empty.
Definition kd_tree.cpp:249
size_t nearest(float x_query, float y_query) const
Find the index of the nearest neighbor to a 2D query position.
Definition kd_tree.cpp:300
KDTree(KDTree &&other) noexcept
Move constructor.
~KDTree()
Destructor.
KDTree & operator=(KDTree &&other) noexcept
Move assignment operator.
std::vector< std::pair< size_t, float > > radius_search(float x_query, float y_query, float radius) const
Perform a radius-based neighbor search.
Definition kd_tree.cpp:366
KDTree & operator=(const KDTree &)=delete
KDTree(const KDTree &)=delete
void neighbor_search(float x_query, float y_query, size_t k_neighbors, std::vector< size_t > &indices, std::vector< float > &distances) const
Perform a k-nearest neighbor search.
Definition kd_tree.cpp:335
A class to represent and manipulate 2D points that can carry a value.
Definition point.hpp:26
Definition opencv.hpp:20
Definition kd_tree.cpp:84