Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
vulpes::datastructure::KDTree< Dim > Class Template Reference

KDTree is a data structure for efficiently querying nearest neighbors and performing spatial searches in a k-dimensional space. More...

#include <kdtree.hpp>

Public Member Functions

 KDTree (const std::vector< std::array< double, Dim > > &data)
 Constructs a KDTree from a vector of data points.
 KDTree (const double *data, int64_t size)
 Constructs a KDTree using a raw pointer to the data.
 KDTree (const double *data, const std::vector< int64_t > &indices)
 Constructs a KDTree using a raw pointer to the data.
void build (int64_t leafsize, bool median, bool compact)
 Builds the KDTree.
void build (const std::array< double, 2 *Dim > &boxsize, int64_t leafsize, bool median, bool compact)
 Builds the KDTree with specified bounding box sizes.
template<double PNorm, bool Periodic = false>
std::pair< Eigen::MatrixXd, Eigen::Matrix< int64_t, -1, -1 > > queryKnn (const double *points, int64_t n_points, int64_t k, double eps=0, double distance_upper_bound=std::numeric_limits< double >::max()) const
 Queries the KDTree for k-nearest neighbors (KNN) of a set of points.
template<double PNorm, bool Periodic = false>
std::pair< Eigen::MatrixXd, Eigen::Matrix< int64_t, -1, -1 > > queryKnn (const std::vector< std::array< double, Dim > > &points, int64_t k, double eps=0, double distance_upper_bound=std::numeric_limits< double >::max()) const
 Queries the KDTree for k-nearest neighbors (KNN) of a set of points.
template<double PNorm, bool Periodic = false>
std::vector< std::vector< scipy::ckdtree_intp_t > > queryBall (const double *points, const double *radiuses, int64_t n_queries, double eps=0.0, bool return_length=false, bool sort_output=true) const
 Queries the KDTree for all points within a given radius of a set of input points.
template<double PNorm, bool Periodic = false>
std::vector< std::vector< scipy::ckdtree_intp_t > > queryBall (const std::vector< std::array< double, Dim > > &points, const std::vector< double > &radiuses, double eps=0.0, bool return_length=false, bool sort_output=true) const
 Queries the KDTree for all points within a given radius of a set of input points.
template<double PNorm, bool Periodic = false>
std::vector< scipy::ordered_pair > queryPairs (double r, double eps=0.0) const
 Finds all pairs of points whose distance is within a given radius.
template<double PNorm, bool Periodic = false>
std::vector< std::vector< scipy::ckdtree_intp_t > > queryBallTree (const KDTree< Dim > &other, double r, double eps=0.0) const
 Finds all points within a given radius from another KDTree.

Protected Member Functions

void buildRun (bool median, bool compact)
 Internal function to build the KDTree.
void postInitTraverse (scipy::ckdtreenode *root, scipy::ckdtreenode *node)
 Performs post-initialization traversal of the tree to adjust node pointers.
void compututeBounds ()
 Computes the bounding box (max and min values) for the data points.

Protected Attributes

bool m_is_built = false
int64_t m_leafsize = -1
std::vector< scipy::ckdtreenode > m_tree_buffer
std::vector< double > m_data
std::vector< int64_t > m_indices
const double * m_raw_data = nullptr
int64_t m_raw_data_size = 0
std::array< double, Dim > m_maxs {{-std::numeric_limits<double>::max()}}
std::array< double, Dim > m_mins {{std::numeric_limits<double>::max()}}
std::array< double, 2 *Dim > m_boxsize = {0}
scipy::ckdtree< Dim > m_tree

Detailed Description

template<int64_t Dim>
class vulpes::datastructure::KDTree< Dim >

KDTree is a data structure for efficiently querying nearest neighbors and performing spatial searches in a k-dimensional space.

This class template provides an interface for constructing a KD-tree from a set of points in k-dimensional space, and supports various spatial queries such as k-nearest neighbor searches and ball queries. The implementation relies on a code inspired by scipy.spatial.cKDTree.

The algorithm used is described in Maneewongvatana and Mount 1999. The general idea is that the kd-tree is a binary tree, each of whose nodes represents an axis-aligned hyperrectangle. Each node specifies an axis and splits the set of points based on whether their coordinate along that axis is greater than or less than a particular value. During construction, the axis and splitting point are chosen by the "sliding midpoint" rule, which ensures that the cells do not all become long and thin. The tree can be queried for the r closest neighbors of any given point (optionally returning only those within some maximum distance of the point). It can also be queried, with a substantial gain in efficiency, for the r approximate closest neighbors. For large dimensions (20 is already large) do not expect this to run significantly faster than brute force. High-dimensional nearest-neighbor queries are a substantial open problem in computer science.

Template Parameters
DimThe number of dimensions in the data points (k-dimensions).
Note
The algorithm is particularly suited for bulk-loading data. If your task necessitates re-building the tree (e.g. to track particles) better suited datastuctures are the R-tree and quadtree/octree

Constructor & Destructor Documentation

◆ KDTree() [1/3]

template<int64_t Dim>
vulpes::datastructure::KDTree< Dim >::KDTree ( const std::vector< std::array< double, Dim > > & data)
inlineexplicit

Constructs a KDTree from a vector of data points.

Each point is represented as a std::array<double, Dim>. The data is copied internally.

Parameters
dataA vector of k-dimensional data points.

◆ KDTree() [2/3]

template<int64_t Dim>
vulpes::datastructure::KDTree< Dim >::KDTree ( const double * data,
int64_t size )
inlineexplicit

Constructs a KDTree using a raw pointer to the data.

This constructor is useful if the data is already available in a contiguous array of doubles. The KDTree will use the raw data directly, without copying.

Parameters
dataA pointer to the raw data array.
sizeThe number of doubles in the data array.

◆ KDTree() [3/3]

template<int64_t Dim>
vulpes::datastructure::KDTree< Dim >::KDTree ( const double * data,
const std::vector< int64_t > & indices )
inlineexplicit

Constructs a KDTree using a raw pointer to the data.

This constructor is useful if the data is already available in a sparse array of doubles. The KDTree will use the raw data directly, without copying.

Parameters
dataA pointer to the raw data array.
indicesThe indices of the points.

Member Function Documentation

◆ build() [1/2]

template<int64_t Dim>
void vulpes::datastructure::KDTree< Dim >::build ( const std::array< double, 2 *Dim > & boxsize,
int64_t leafsize,
bool median,
bool compact )
inline

Builds the KDTree with specified bounding box sizes.

This version of the build method allows specifying a bounding box size, which is necessary for periodic boundary conditions.

Parameters
boxsizeApply a m-d toroidal topology to the KDTree. The topology is generated by $x_i + n_i L_i$ where $n_i$ are integers and $L_i$ is the boxsize along i-th dimension. The input data shall be wrapped into $[0, L_i)$.
leafsizeThe maximum number of points in a leaf node.
medianIf true, the tree will use median-based splitting.
compactIf true, the kd-tree is built to shrink the hyperrectangles to the actual data range. This usually gives a more compact tree that is robust against degenerated input data and gives faster queries at the expense of longer build time.

◆ build() [2/2]

template<int64_t Dim>
void vulpes::datastructure::KDTree< Dim >::build ( int64_t leafsize,
bool median,
bool compact )
inline

Builds the KDTree.

This function constructs the tree structure. The tree can be built using either a median-based or sliding-midpoint strategy for splitting nodes, and the user can choose to compact the data.

Parameters
leafsizeThe maximum number of points in a leaf node.
medianIf true, the tree will use median-based splitting.
compactIf true, the kd-tree is built to shrink the hyperrectangles to the actual data range. This usually gives a more compact tree that is robust against degenerated input data and gives faster queries at the expense of longer build time.

◆ buildRun()

template<int64_t Dim>
void vulpes::datastructure::KDTree< Dim >::buildRun ( bool median,
bool compact )
inlineprotected

Internal function to build the KDTree.

This function is responsible for setting up the KDTree structure, computing the bounds for the points, and invoking the tree building process.

Parameters
medianIf true, the tree will use median-based splitting.
compactIf true, the tree will compact the data to minimize memory usage.

◆ compututeBounds()

template<int64_t Dim>
void vulpes::datastructure::KDTree< Dim >::compututeBounds ( )
inlineprotected

Computes the bounding box (max and min values) for the data points.

This function calculates the minimum and maximum coordinates in each dimension, which are used to bound the KDTree.

◆ postInitTraverse()

template<int64_t Dim>
void vulpes::datastructure::KDTree< Dim >::postInitTraverse ( scipy::ckdtreenode * root,
scipy::ckdtreenode * node )
inlineprotected

Performs post-initialization traversal of the tree to adjust node pointers.

Fixes the pointers within the KDTree nodes. The pointers are meaningless if the underlying vector reallocates the buffer during the resize. Thus everything must be fixex once the tree stops growing. The same is done in the cython code used in scipy

Parameters
rootPointer to the root node of the KDTree.
nodePointer to the current node being processed.

◆ queryBall() [1/2]

template<int64_t Dim>
template<double PNorm, bool Periodic = false>
std::vector< std::vector< scipy::ckdtree_intp_t > > vulpes::datastructure::KDTree< Dim >::queryBall ( const double * points,
const double * radiuses,
int64_t n_queries,
double eps = 0.0,
bool return_length = false,
bool sort_output = true ) const
inline

Queries the KDTree for all points within a given radius of a set of input points.

This method finds all points within a specified radius around each query point. It uses the specified p-norm for distance calculations.

Template Parameters
PNormThe p-norm to use for distance calculation.
PeriodicWhether to use periodic boundary conditions.
Parameters
pointsPointer to the input vector or points.
radiusesPointer to the array of radius values.
n_queriesThe number of query points.
epsApproximation tolerance (default is exact search).
return_lengthWhether to return the length of each result set.
sort_outputWhether to sort the output by distance.
Returns
A vector of vectors containing the indices of points found within each radius.

◆ queryBall() [2/2]

template<int64_t Dim>
template<double PNorm, bool Periodic = false>
std::vector< std::vector< scipy::ckdtree_intp_t > > vulpes::datastructure::KDTree< Dim >::queryBall ( const std::vector< std::array< double, Dim > > & points,
const std::vector< double > & radiuses,
double eps = 0.0,
bool return_length = false,
bool sort_output = true ) const
inline

Queries the KDTree for all points within a given radius of a set of input points.

This overload allows querying with std::vector data.

Template Parameters
PNormThe p-norm to use for distance calculation.
PeriodicWhether to use periodic boundary conditions.
Parameters
pointsThe input points stored in a std::vector.
radiusesA vector containing radius values for each point.
epsApproximation tolerance (default is exact search).
return_lengthWhether to return the length of each result set.
sort_outputWhether to sort the output by distance.
Returns
A vector of vectors containing the indices of points found within each radius.

◆ queryBallTree()

template<int64_t Dim>
template<double PNorm, bool Periodic = false>
std::vector< std::vector< scipy::ckdtree_intp_t > > vulpes::datastructure::KDTree< Dim >::queryBallTree ( const KDTree< Dim > & other,
double r,
double eps = 0.0 ) const
inline

Finds all points within a given radius from another KDTree.

This method performs a ball query between the KDTree and another KDTree, finding all points within the given radius r across the two trees.

Template Parameters
PNormThe p-norm to use for distance calculation.
PeriodicWhether to use periodic boundary conditions.
Parameters
otherThe other KDTree to query against.
rThe radius within which to search.
epsApproximation tolerance (default is exact search).
Returns
A vector of indices representing the points found in both trees within the radius.

◆ queryKnn() [1/2]

template<int64_t Dim>
template<double PNorm, bool Periodic = false>
std::pair< Eigen::MatrixXd, Eigen::Matrix< int64_t, -1, -1 > > vulpes::datastructure::KDTree< Dim >::queryKnn ( const double * points,
int64_t n_points,
int64_t k,
double eps = 0,
double distance_upper_bound = std::numeric_limits<double>::max() ) const
inline

Queries the KDTree for k-nearest neighbors (KNN) of a set of points.

Finds the k-nearest neighbors for a set of input points, using a specified distance metric (PNorm). This version allows querying with raw data pointers.

Template Parameters
PNormThe p-norm to use for distance calculation (e.g., 2.0 for Euclidean).
PeriodicWhether to use periodic boundary conditions.
Parameters
pointsPointer to the input vector of points.
n_pointsThe number of input points.
kThe number of nearest neighbors to find.
epsApproximate nearest neighbor search tolerance (default is exact search).
distance_upper_boundUpper bound on the search radius.
Returns
A pair of matrices, where the first matrix contains distances and the second matrix contains indices of the nearest neighbors.

◆ queryKnn() [2/2]

template<int64_t Dim>
template<double PNorm, bool Periodic = false>
std::pair< Eigen::MatrixXd, Eigen::Matrix< int64_t, -1, -1 > > vulpes::datastructure::KDTree< Dim >::queryKnn ( const std::vector< std::array< double, Dim > > & points,
int64_t k,
double eps = 0,
double distance_upper_bound = std::numeric_limits<double>::max() ) const
inline

Queries the KDTree for k-nearest neighbors (KNN) of a set of points.

This overload allows querying with std::vector data, which is useful when the data is in std::array format.

Template Parameters
PNormThe p-norm to use for distance calculation.
PeriodicWhether to use periodic boundary conditions.
Parameters
pointsThe input points stored in a std::vector.
kThe number of nearest neighbors to find.
epsApproximate nearest neighbor search tolerance (default is exact search).
distance_upper_boundUpper bound on the search radius.
Returns
A pair of matrices, where the first matrix contains distances and the second matrix contains indices of the nearest neighbors.

◆ queryPairs()

template<int64_t Dim>
template<double PNorm, bool Periodic = false>
std::vector< scipy::ordered_pair > vulpes::datastructure::KDTree< Dim >::queryPairs ( double r,
double eps = 0.0 ) const
inline

Finds all pairs of points whose distance is within a given radius.

This method queries the KDTree for all pairs of points whose distances are within a specified radius r.

Template Parameters
PNormThe p-norm to use for distance calculation.
PeriodicWhether to use periodic boundary conditions.
Parameters
rThe radius within which to search for pairs.
epsApproximation tolerance (default is exact search).
Returns
A vector of pairs (indices) representing the points within the radius.

Member Data Documentation

◆ m_boxsize

template<int64_t Dim>
std::array<double, 2 * Dim> vulpes::datastructure::KDTree< Dim >::m_boxsize = {0}
protected

◆ m_data

template<int64_t Dim>
std::vector<double> vulpes::datastructure::KDTree< Dim >::m_data
protected

◆ m_indices

template<int64_t Dim>
std::vector<int64_t> vulpes::datastructure::KDTree< Dim >::m_indices
protected

◆ m_is_built

template<int64_t Dim>
bool vulpes::datastructure::KDTree< Dim >::m_is_built = false
protected

◆ m_leafsize

template<int64_t Dim>
int64_t vulpes::datastructure::KDTree< Dim >::m_leafsize = -1
protected

◆ m_maxs

template<int64_t Dim>
std::array<double, Dim> vulpes::datastructure::KDTree< Dim >::m_maxs {{-std::numeric_limits<double>::max()}}
protected

◆ m_mins

template<int64_t Dim>
std::array<double, Dim> vulpes::datastructure::KDTree< Dim >::m_mins {{std::numeric_limits<double>::max()}}
protected

◆ m_raw_data

template<int64_t Dim>
const double* vulpes::datastructure::KDTree< Dim >::m_raw_data = nullptr
protected

◆ m_raw_data_size

template<int64_t Dim>
int64_t vulpes::datastructure::KDTree< Dim >::m_raw_data_size = 0
protected

◆ m_tree

template<int64_t Dim>
scipy::ckdtree<Dim> vulpes::datastructure::KDTree< Dim >::m_tree
protected

◆ m_tree_buffer

template<int64_t Dim>
std::vector<scipy::ckdtreenode> vulpes::datastructure::KDTree< Dim >::m_tree_buffer
protected

The documentation for this class was generated from the following file:
  • /__w/vulpes/vulpes/vulpes/datastructure/kdtree.hpp