![]() |
Vulpes
0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
|
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 |
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.
| Dim | The number of dimensions in the data points (k-dimensions). |
|
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.
| data | A vector of k-dimensional data points. |
|
inlineexplicit |
|
inlineexplicit |
|
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.
| boxsize | Apply 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)$. |
| leafsize | The maximum number of points in a leaf node. |
| median | If true, the tree will use median-based splitting. |
| compact | If 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. |
|
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.
| leafsize | The maximum number of points in a leaf node. |
| median | If true, the tree will use median-based splitting. |
| compact | If 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. |
|
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.
| median | If true, the tree will use median-based splitting. |
| compact | If true, the tree will compact the data to minimize memory usage. |
|
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.
|
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
| root | Pointer to the root node of the KDTree. |
| node | Pointer to the current node being processed. |
|
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.
| PNorm | The p-norm to use for distance calculation. |
| Periodic | Whether to use periodic boundary conditions. |
| points | Pointer to the input vector or points. |
| radiuses | Pointer to the array of radius values. |
| n_queries | The number of query points. |
| eps | Approximation tolerance (default is exact search). |
| return_length | Whether to return the length of each result set. |
| sort_output | Whether to sort the output by distance. |
|
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.
| PNorm | The p-norm to use for distance calculation. |
| Periodic | Whether to use periodic boundary conditions. |
| points | The input points stored in a std::vector. |
| radiuses | A vector containing radius values for each point. |
| eps | Approximation tolerance (default is exact search). |
| return_length | Whether to return the length of each result set. |
| sort_output | Whether to sort the output by distance. |
|
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.
| PNorm | The p-norm to use for distance calculation. |
| Periodic | Whether to use periodic boundary conditions. |
| other | The other KDTree to query against. |
| r | The radius within which to search. |
| eps | Approximation tolerance (default is exact search). |
|
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.
| PNorm | The p-norm to use for distance calculation (e.g., 2.0 for Euclidean). |
| Periodic | Whether to use periodic boundary conditions. |
| points | Pointer to the input vector of points. |
| n_points | The number of input points. |
| k | The number of nearest neighbors to find. |
| eps | Approximate nearest neighbor search tolerance (default is exact search). |
| distance_upper_bound | Upper bound on the search radius. |
|
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.
| PNorm | The p-norm to use for distance calculation. |
| Periodic | Whether to use periodic boundary conditions. |
| points | The input points stored in a std::vector. |
| k | The number of nearest neighbors to find. |
| eps | Approximate nearest neighbor search tolerance (default is exact search). |
| distance_upper_bound | Upper bound on the search radius. |
|
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.
| PNorm | The p-norm to use for distance calculation. |
| Periodic | Whether to use periodic boundary conditions. |
| r | The radius within which to search for pairs. |
| eps | Approximation tolerance (default is exact search). |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |