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
9#pragma once
10#include <vector>
11
12#include <glm/glm.hpp>
13
14#include <nanoflann.hpp>
15
16namespace hmap
17{
18
28{
30 const std::vector<float> &x;
31
33 const std::vector<float> &y;
34
40 NanoflannPointCloudAdaptor(const std::vector<float> &x_,
41 const std::vector<float> &y_);
42
47 inline size_t kdtree_get_point_count() const;
48
55 inline float kdtree_get_pt(const size_t idx, int dim) const;
56
61 template <class BBOX> bool kdtree_get_bbox(BBOX &) const
62 {
63 return false;
64 }
65};
66
70using KDTree = nanoflann::KDTreeSingleIndexAdaptor<
71 nanoflann::L2_Simple_Adaptor<float, NanoflannPointCloudAdaptor>,
73 2>;
74
88{
90 const std::vector<float> &x;
91
93 const std::vector<float> &y;
94
97
100
106 KDTreeContext(const std::vector<float> &x_, const std::vector<float> &y_);
107
117 glm::vec2 compte_neighbor_distance_range(size_t k_neighbors) const;
118
128 void neighbor_search(float x_query,
129 float y_query,
130 size_t k_neighbors,
131 std::vector<size_t> &indices,
132 std::vector<float> &distances) const;
133
144 std::vector<nanoflann::ResultItem<unsigned int, float>> radius_search(
145 float x_query,
146 float y_query,
147 float radius) const;
148};
149
150} // namespace hmap
Definition algebra.hpp:23
nanoflann::KDTreeSingleIndexAdaptor< nanoflann::L2_Simple_Adaptor< float, NanoflannPointCloudAdaptor >, NanoflannPointCloudAdaptor, 2 > KDTree
Alias for a 2D KD-tree using L2 (Euclidean) distance.
Definition kd_tree.hpp:73
Context wrapper for KD-tree operations.
Definition kd_tree.hpp:88
NanoflannPointCloudAdaptor adaptor
Definition kd_tree.hpp:96
glm::vec2 compte_neighbor_distance_range(size_t k_neighbors) const
Estimate the range of neighbor distances.
Definition kd_tree.cpp:47
KDTree index
Definition kd_tree.hpp:99
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:74
const std::vector< float > & x
Definition kd_tree.hpp:90
const std::vector< float > & y
Definition kd_tree.hpp:93
std::vector< nanoflann::ResultItem< unsigned int, float > > radius_search(float x_query, float y_query, float radius) const
Perform a radius-based neighbor search.
Definition kd_tree.cpp:93
Adaptor exposing a 2D point cloud to nanoflann.
Definition kd_tree.hpp:28
float kdtree_get_pt(const size_t idx, int dim) const
Get a coordinate component of a point.
Definition kd_tree.cpp:25
size_t kdtree_get_point_count() const
Get the number of points in the dataset.
Definition kd_tree.cpp:20
const std::vector< float > & y
Definition kd_tree.hpp:33
const std::vector< float > & x
Definition kd_tree.hpp:30
bool kdtree_get_bbox(BBOX &) const
Optional bounding-box computation (unused).
Definition kd_tree.hpp:61