HighMap library (C++)
Loading...
Searching...
No Matches
hmap::KDTree Class Reference

2D KD-Tree for spatial point queries. More...

#include <kd_tree.hpp>

Classes

struct  Impl

Public Member Functions

 KDTree ()
 Construct an empty KD-tree.
 ~KDTree ()
 Destructor.
 KDTree (const std::vector< float > &x, const std::vector< float > &y)
 Construct a KD-tree referencing separate coordinate arrays.
 KDTree (const std::vector< Point > &points)
 Construct a KD-tree referencing a vector of points.
 KDTree (const Cloud &cloud)
 Construct a KD-tree referencing a Cloud.
 KDTree (KDTree &&other) noexcept
 Move constructor.
KDTree & operator= (KDTree &&other) noexcept
 Move assignment operator.
 KDTree (const KDTree &)=delete
KDTree & operator= (const KDTree &)=delete
bool empty () const noexcept
 Check whether the KD-tree is empty.
size_t size () const noexcept
 Get the number of points indexed by the KD-tree.
glm::vec2 compute_neighbor_distance_range (size_t k_neighbors) const
 Estimate the range of neighbor distances across the dataset.
size_t nearest (float x_query, float y_query) const
 Find the index of the nearest neighbor to a 2D query position.
size_t nearest (const Point &p) const
 Find the index of the nearest neighbor to a Point.
size_t nearest (const glm::vec2 &xy) const
 Find the index of the nearest neighbor to a glm::vec2.
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.
std::pair< size_t, float > nearest_with_distance_squared (const Point &p) const
 Find the index and squared distance of the nearest neighbor to a Point.
std::pair< size_t, float > nearest_with_distance_squared (const glm::vec2 &xy) const
 Find the index and squared distance of the nearest neighbor to a glm::vec2.
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.
void neighbor_search (const Point &p, size_t k_neighbors, std::vector< size_t > &indices, std::vector< float > &distances) const
 Perform a k-nearest neighbor search for a Point.
void neighbor_search (const glm::vec2 &xy, size_t k_neighbors, std::vector< size_t > &indices, std::vector< float > &distances) const
 Perform a k-nearest neighbor search for a glm::vec2.
std::vector< std::pair< size_t, float > > radius_search (float x_query, float y_query, float radius) const
 Perform a radius-based neighbor search.
std::vector< std::pair< size_t, float > > radius_search (const Point &p, float radius) const
 Perform a radius-based neighbor search for a Point.
std::vector< std::pair< size_t, float > > radius_search (const glm::vec2 &xy, float radius) const
 Perform a radius-based neighbor search for a glm::vec2.

Detailed Description

2D KD-Tree for spatial point queries.

Supports construction from separate coordinate arrays (x, y), point vectors (std::vector<Point>), and Cloud objects.

Note
Input point data must remain valid for the lifetime of this KDTree object.

Constructor & Destructor Documentation

◆ KDTree() [1/6]

hmap::KDTree::KDTree ( )
default

Construct an empty KD-tree.

◆ ~KDTree()

hmap::KDTree::~KDTree ( )
default

Destructor.

◆ KDTree() [2/6]

hmap::KDTree::KDTree ( const std::vector< float > & x,
const std::vector< float > & y )

Construct a KD-tree referencing separate coordinate arrays.

Parameters
xVector of x coordinates.
yVector of y coordinates.

◆ KDTree() [3/6]

hmap::KDTree::KDTree ( const std::vector< Point > & points)
explicit

Construct a KD-tree referencing a vector of points.

Parameters
pointsVector of Point objects.

◆ KDTree() [4/6]

hmap::KDTree::KDTree ( const Cloud & cloud)
explicit

Construct a KD-tree referencing a Cloud.

Parameters
cloudCloud object.

◆ KDTree() [5/6]

hmap::KDTree::KDTree ( KDTree && other)
defaultnoexcept

Move constructor.

◆ KDTree() [6/6]

hmap::KDTree::KDTree ( const KDTree & )
delete

Member Function Documentation

◆ operator=() [1/2]

KDTree & hmap::KDTree::operator= ( KDTree && other)
defaultnoexcept

Move assignment operator.

◆ operator=() [2/2]

KDTree & hmap::KDTree::operator= ( const KDTree & )
delete

◆ empty()

bool hmap::KDTree::empty ( ) const
noexcept

Check whether the KD-tree is empty.

Returns
true if empty, false otherwise.

◆ size()

size_t hmap::KDTree::size ( ) const
noexcept

Get the number of points indexed by the KD-tree.

Returns
Number of points.

◆ compute_neighbor_distance_range()

glm::vec2 hmap::KDTree::compute_neighbor_distance_range ( size_t k_neighbors) const

Estimate the range of neighbor distances across the dataset.

Computes the minimum and maximum Euclidean distance to the k-th nearest neighbor across all indexed points.

Parameters
k_neighborsNumber of neighbors considered.
Returns
vec2(min_euclidean_distance, max_euclidean_distance)

◆ nearest() [1/3]

size_t hmap::KDTree::nearest ( float x_query,
float y_query ) const

Find the index of the nearest neighbor to a 2D query position.

Fast 1-nearest-neighbor query that performs no heap allocations.

Parameters
x_queryQuery x coordinate.
y_queryQuery y coordinate.
Returns
Index of the nearest neighbor point (0 if empty).

◆ nearest() [2/3]

size_t hmap::KDTree::nearest ( const Point & p) const

Find the index of the nearest neighbor to a Point.

◆ nearest() [3/3]

size_t hmap::KDTree::nearest ( const glm::vec2 & xy) const

Find the index of the nearest neighbor to a glm::vec2.

◆ nearest_with_distance_squared() [1/3]

std::pair< size_t, float > hmap::KDTree::nearest_with_distance_squared ( float x_query,
float y_query ) const

Find the index and squared distance of the nearest neighbor.

Parameters
x_queryQuery x coordinate.
y_queryQuery y coordinate.
Returns
Pair of (index, squared_distance).

◆ nearest_with_distance_squared() [2/3]

std::pair< size_t, float > hmap::KDTree::nearest_with_distance_squared ( const Point & p) const

Find the index and squared distance of the nearest neighbor to a Point.

◆ nearest_with_distance_squared() [3/3]

std::pair< size_t, float > hmap::KDTree::nearest_with_distance_squared ( const glm::vec2 & xy) const

Find the index and squared distance of the nearest neighbor to a glm::vec2.

◆ neighbor_search() [1/3]

void hmap::KDTree::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.

Parameters
x_queryQuery x coordinate.
y_queryQuery y coordinate.
k_neighborsNumber of neighbors to retrieve.
[out]indicesOutput indices of neighbors.
[out]distancesOutput squared distances to neighbors.

◆ neighbor_search() [2/3]

void hmap::KDTree::neighbor_search ( const Point & p,
size_t k_neighbors,
std::vector< size_t > & indices,
std::vector< float > & distances ) const

Perform a k-nearest neighbor search for a Point.

◆ neighbor_search() [3/3]

void hmap::KDTree::neighbor_search ( const glm::vec2 & xy,
size_t k_neighbors,
std::vector< size_t > & indices,
std::vector< float > & distances ) const

Perform a k-nearest neighbor search for a glm::vec2.

◆ radius_search() [1/3]

std::vector< std::pair< size_t, float > > hmap::KDTree::radius_search ( float x_query,
float y_query,
float radius ) const

Perform a radius-based neighbor search.

Parameters
x_queryQuery x coordinate.
y_queryQuery y coordinate.
radiusSearch radius (Euclidean distance).
Returns
Vector of (index, squared_distance) pairs.

◆ radius_search() [2/3]

std::vector< std::pair< size_t, float > > hmap::KDTree::radius_search ( const Point & p,
float radius ) const

Perform a radius-based neighbor search for a Point.

◆ radius_search() [3/3]

std::vector< std::pair< size_t, float > > hmap::KDTree::radius_search ( const glm::vec2 & xy,
float radius ) const

Perform a radius-based neighbor search for a glm::vec2.


The documentation for this class was generated from the following files: