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 <cstddef>
11#include <vector>
12
13#include <glm/glm.hpp>
14
15#include <nanoflann.hpp>
16
17namespace hmap
18{
19
29{
31 const std::vector<float> &x;
32
34 const std::vector<float> &y;
35
41 NanoflannPointCloudAdaptor(const std::vector<float> &x_,
42 const std::vector<float> &y_);
43
48 inline size_t kdtree_get_point_count() const;
49
56 inline float kdtree_get_pt(const size_t idx, int dim) const;
57
62 template <class BBOX> bool kdtree_get_bbox(BBOX &) const
63 {
64 return false;
65 }
66};
67
71using KDTree = nanoflann::KDTreeSingleIndexAdaptor<
72 nanoflann::L2_Simple_Adaptor<float, NanoflannPointCloudAdaptor>,
74 2>;
75
89{
91 const std::vector<float> &x;
92
94 const std::vector<float> &y;
95
98
101
107 KDTreeContext(const std::vector<float> &x_, const std::vector<float> &y_);
108
118 glm::vec2 compte_neighbor_distance_range(size_t k_neighbors) const;
119
129 void neighbor_search(float x_query,
130 float y_query,
131 size_t k_neighbors,
132 std::vector<size_t> &indices,
133 std::vector<float> &distances) const;
134
145 std::vector<nanoflann::ResultItem<unsigned int, float>> radius_search(
146 float x_query,
147 float y_query,
148 float radius) const;
149};
150
151} // 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:74
Context wrapper for KD-tree operations.
Definition kd_tree.hpp:89
NanoflannPointCloudAdaptor adaptor
Definition kd_tree.hpp:97
glm::vec2 compte_neighbor_distance_range(size_t k_neighbors) const
Estimate the range of neighbor distances.
Definition kd_tree.cpp:54
KDTree index
Definition kd_tree.hpp:100
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:81
const std::vector< float > & x
Definition kd_tree.hpp:91
const std::vector< float > & y
Definition kd_tree.hpp:94
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:100
Adaptor exposing a 2D point cloud to nanoflann.
Definition kd_tree.hpp:29
float kdtree_get_pt(const size_t idx, int dim) const
Get a coordinate component of a point.
Definition kd_tree.cpp:32
size_t kdtree_get_point_count() const
Get the number of points in the dataset.
Definition kd_tree.cpp:27
const std::vector< float > & y
Definition kd_tree.hpp:34
const std::vector< float > & x
Definition kd_tree.hpp:31
bool kdtree_get_bbox(BBOX &) const
Optional bounding-box computation (unused).
Definition kd_tree.hpp:62