### KDTreeEigenMatrixAdaptor Usage Example Source: https://jlblancoc.github.io/nanoflann/structnanoflann_1_1KDTreeEigenMatrixAdaptor.html Demonstrates how to initialize and build a KD-tree using Eigen Matrix data. Ensure the MatrixType and dimensionality are correctly defined. ```cpp Eigen::Matrix mat; // Fill out "mat"... typedef KDTreeEigenMatrixAdaptor< Eigen::Matrix > my_kd_tree_t; const int max_leaf = 10; my_kd_tree_t mat_index(dimdim, mat, max_leaf ); mat_index.index->buildIndex(); mat_index.index->... ``` -------------------------------- ### searchLevel Source: https://jlblancoc.github.io/nanoflann/classnanoflann_1_1KDTreeSingleIndexDynamicAdaptor__.html Performs an exact search in the tree starting from a specific node. ```APIDOC ## void searchLevel(RESULTSET & _result_set_, const ElementType * _vec_, const NodePtr _node_, DistanceType _mindistsq_, distance_vector_t & _dists_, const float _epsError_) ### Description Performs an exact search in the tree starting from a node. ### Parameters #### Path Parameters - **_result_set_** (RESULTSET &) - Required - The result set object. - **_vec_** (const ElementType *) - Required - The query vector. - **_node_** (const NodePtr) - Required - The starting node. - **_mindistsq_** (DistanceType) - Required - Minimum distance squared. - **_dists_** (distance_vector_t &) - Required - Distance vector. - **_epsError_** (const float) - Required - Epsilon error value. ``` -------------------------------- ### Dynamic KD-Tree Adaptor Initialization Source: https://jlblancoc.github.io/nanoflann/nanoflann_8hpp_source.html Constructor and member definitions for the dynamic KD-Tree adaptor, supporting runtime index updates. ```cpp template 1503 class KDTreeSingleIndexDynamicAdaptor_ : public KDTreeBaseClass, Distance, DatasetAdaptor, DIM, IndexType> 1504 { 1505 public: 1506 typedef typename Distance::ElementType ElementType; 1507 typedef typename Distance::DistanceType DistanceType; 1508 1512 std::vector vind; 1513 1514 size_t m_leaf_max_size; 1515 1516 1520 DatasetAdaptor &dataset; 1521 1522 KDTreeSingleIndexAdaptorParams index_params; 1523 1524 std::vector &treeIndex; 1525 1526 size_t m_size; 1527 size_t m_size_at_index_build; 1528 int dim; 1529 1530 1531 typedef typename nanoflann::KDTreeBaseClass, Distance, DatasetAdaptor, DIM, IndexType>::Node Node; 1532 typedef Node* NodePtr; 1533 1534 typedef typename nanoflann::KDTreeBaseClass, Distance, DatasetAdaptor, DIM, IndexType>::Interval Interval; 1536 typedef typename nanoflann::KDTreeBaseClass, Distance, DatasetAdaptor, DIM, IndexType>::BoundingBox BoundingBox; 1537 1539 typedef typename nanoflann::KDTreeBaseClass, Distance, DatasetAdaptor, DIM, IndexType>::distance_vector_t distance_vector_t; 1540 1542 NodePtr root_node; 1543 BoundingBox root_bbox; 1544 1552 PooledAllocator pool; 1553 1554 public: 1555 1556 Distance distance; 1557 1571 KDTreeSingleIndexDynamicAdaptor_(const int dimensionality, DatasetAdaptor& inputData, std::vector& treeIndex_, const KDTreeSingleIndexAdaptorParams& params = KDTreeSingleIndexAdaptorParams()) : 1572 dataset(inputData), index_params(params), treeIndex(treeIndex_), root_node(NULL), distance(inputData) 1573 { 1574 m_size = 0; 1575 m_size_at_index_build = 0; 1576 dim = dimensionality; 1577 if (DIM>0) dim=DIM; 1578 m_leaf_max_size = params.leaf_max_size; 1579 } ``` -------------------------------- ### KDTreeSingleIndexAdaptor::searchLevel Source: https://jlblancoc.github.io/nanoflann/classnanoflann_1_1KDTreeSingleIndexAdaptor.html Performs an exact search in the tree starting from a node. This is a core function for nearest neighbor searches. ```APIDOC ## KDTreeSingleIndexAdaptor::searchLevel ### Description Performs an exact search in the tree starting from a node. ### Method `bool searchLevel(RESULTSET & _result_set_, const ElementType * _vec_, const NodePtr _node_, DistanceType _mindistsq_, distance_vector_t & _dists_, const float _epsError_) const` ### Template Parameters * **RESULTSET**: Should be any ResultSet ### Returns true if the search should be continued, false if the results are sufficient ``` -------------------------------- ### Dataset Get Element Source: https://jlblancoc.github.io/nanoflann/nanoflann_8hpp_source.html Retrieves a specific component of an element from the dataset. Used internally for accessing point coordinates. ```cpp inline ElementType dataset_get(size_t idx, int component) const { return dataset.kdtree_get_pt(idx,component); } ``` -------------------------------- ### NANOFLANN_VERSION Macro Source: https://jlblancoc.github.io/nanoflann/group__nanoflann__grp.html Documentation for the library versioning macro. ```APIDOC ## Macro: NANOFLANN_VERSION ### Description Defines the current version of the nanoflann library. ### Definition #define NANOFLANN_VERSION 0x123 ### Details Library version is represented in the format 0xMmP where M is Major, m is minor, and P is patch. ``` -------------------------------- ### nanoflann::KDTreeSingleIndexDynamicAdaptor::addPoints Source: https://jlblancoc.github.io/nanoflann/nanoflann_8hpp_source.html Method to add a range of points to a dynamic KD-tree. It specifies the start and end indices of the points to be added. ```cpp void addPoints(IndexType start, IndexType end) ``` -------------------------------- ### KDTreeSingleIndexAdaptor Constructor Source: https://jlblancoc.github.io/nanoflann/nanoflann_8hpp_source.html Initializes the KDTreeSingleIndexAdaptor with dimensionality, dataset, and parameters. It sets up the dataset size and prepares indices for tree construction. ```cpp KDTreeSingleIndexAdaptor(const int dimensionality, const DatasetAdaptor& inputData, const KDTreeSingleIndexAdaptorParams& params = KDTreeSingleIndexAdaptorParams() ) : dataset(inputData), index_params(params), root_node(NULL), distance(inputData) { m_size = dataset.kdtree_get_point_count(); m_size_at_index_build = m_size; dim = dimensionality; if (DIM>0) dim=DIM; m_leaf_max_size = params.leaf_max_size; // Create a permutable array of indices to the input vectors. init_vind(); } ``` -------------------------------- ### KDTreeSingleIndexDynamicAdaptor Constructor Source: https://jlblancoc.github.io/nanoflann/classnanoflann_1_1KDTreeSingleIndexDynamicAdaptor__.html Initializes the KD-Tree structure with the provided dataset and parameters. ```APIDOC ## Constructor: KDTreeSingleIndexDynamicAdaptor_ ### Description Initializes the KD-Tree index. The dimensionality is determined by the DIM template parameter if greater than 0, otherwise by the provided dimensionality parameter. ### Parameters - **dimensionality** (int) - Required - The length of each point in the dataset. - **inputData** (DatasetAdaptor&) - Required - The dataset containing the input features. - **treeIndex** (std::vector&) - Required - The index structure for the tree. - **params** (KDTreeSingleIndexAdaptorParams) - Optional - Configuration parameters, primarily the maximum leaf node size. ``` -------------------------------- ### Get Dataset Point Component Source: https://jlblancoc.github.io/nanoflann/nanoflann_8hpp_source.html Retrieves a specific component of a data point from the dataset. Used internally for accessing point coordinates. ```cpp inline ElementType dataset_get(size_t idx, int component) const { return dataset.kdtree_get_pt(idx,component); } ``` -------------------------------- ### Container Selection Utility Source: https://jlblancoc.github.io/nanoflann/nanoflann_8hpp_source.html Template metaprogramming to select between CArray and std::vector based on dimension. ```cpp 833 template 834 struct array_or_vector_selector 835 { 836 typedef CArray container_t; 837 }; 839 template 840 struct array_or_vector_selector<-1,T> { 841 typedef std::vector container_t; 842 }; ``` -------------------------------- ### Initialize Point Indices Source: https://jlblancoc.github.io/nanoflann/nanoflann_8hpp_source.html Prepares a permutable array of indices corresponding to the input data points. This is used internally for tree construction and manipulation. ```cpp void init_vind() { // Create a permutable array of indices to the input vectors. m_size = dataset.kdtree_get_point_count(); if (vind.size()!=m_size) vind.resize(m_size); for (size_t i = 0; i < m_size; i++) vind[i] = i; } ``` -------------------------------- ### Load KD-Tree Index from File Source: https://jlblancoc.github.io/nanoflann/classnanoflann_1_1KDTreeSingleIndexDynamicAdaptor__.html Loads a previously saved KD-tree index from a file stream. The index is reconstructed and ready for queries. ```cpp void loadIndex(FILE *stream) ``` -------------------------------- ### KDTreeEigenMatrixAdaptor Constructor Source: https://jlblancoc.github.io/nanoflann/structnanoflann_1_1KDTreeEigenMatrixAdaptor.html Initializes the KDTreeEigenMatrixAdaptor. It requires the dimensionality of the points, a const reference to the Eigen Matrix containing the data, and an optional maximum leaf size for the tree nodes. ```cpp KDTreeEigenMatrixAdaptor< MatrixType, DIM, Distance >::KDTreeEigenMatrixAdaptor(const int _dimensionality_, const MatrixType & _mat_, const int _leaf_max_size_ = 10) ``` -------------------------------- ### Constructor: KDTreeEigenMatrixAdaptor Source: https://jlblancoc.github.io/nanoflann/structnanoflann_1_1KDTreeEigenMatrixAdaptor.html Initializes the KD-tree index using an existing Eigen matrix as the data source. ```APIDOC ## Constructor KDTreeEigenMatrixAdaptor ### Description Initializes the KD-tree index for the provided Eigen matrix. This constructor takes a reference to the matrix, avoiding data duplication. ### Parameters #### Path Parameters - **dimensionality** (int) - Required - The dimensionality of the points in the data set. - **mat** (const MatrixType &) - Required - The Eigen matrix containing the data points. - **leaf_max_size** (int) - Optional - The maximum number of points in a leaf node (default: 10). ``` -------------------------------- ### Dynamic KD-Tree Constructor Source: https://jlblancoc.github.io/nanoflann/nanoflann_8hpp_source.html Constructor for the dynamic KD-tree. Initializes parameters, dataset, and the internal tree structure. Throws an error if the dataset is not empty. ```C++ KDTreeSingleIndexDynamicAdaptor(const int dimensionality, DatasetAdaptor& inputData, const KDTreeSingleIndexAdaptorParams& params = KDTreeSingleIndexAdaptorParams() , const int maximumPointCount = 1e9) : dataset(inputData), index_params(params), distance(inputData) { if (dataset.kdtree_get_point_count()) throw std::runtime_error("[nanoflann] cannot handle non empty point cloud."); treeCount = log2(maximumPointCount); pointCount = 0; dim = dimensionality; treeIndex.clear(); if (DIM>0) dim=DIM; m_leaf_max_size = params.leaf_max_size; init(); } ``` -------------------------------- ### KDTreeSingleIndexDynamicAdaptor Constructor Source: https://jlblancoc.github.io/nanoflann/classnanoflann_1_1KDTreeSingleIndexDynamicAdaptor__.html Constructs a KDTreeSingleIndexDynamicAdaptor_. Requires dimensionality, a dataset adaptor, and an optional parameter set. ```cpp KDTreeSingleIndexDynamicAdaptor_(const int dimensionality, DatasetAdaptor &inputData, std::vector &treeIndex_, const KDTreeSingleIndexAdaptorParams ¶ms=KDTreeSingleIndexAdaptorParams()) ``` -------------------------------- ### nanoflann::KDTreeSingleIndexAdaptor Constructor Source: https://jlblancoc.github.io/nanoflann/nanoflann_8hpp_source.html Constructor for KDTreeSingleIndexAdaptor. It takes the dimensionality, the dataset adaptor, and optional parameters. ```cpp KDTreeSingleIndexAdaptor(const int dimensionality, const DatasetAdaptor &inputData, const KDTreeSingleIndexAdaptorParams ¶ms=KDTreeSingleIndexAdaptorParams()) ``` -------------------------------- ### KD-Tree Configuration Structures Source: https://jlblancoc.github.io/nanoflann/nanoflann_8hpp_source.html Structures for configuring KD-tree index parameters and search behavior. ```cpp 548 struct KDTreeSingleIndexAdaptorParams 549 { 550 KDTreeSingleIndexAdaptorParams(size_t _leaf_max_size = 10) : 551 leaf_max_size(_leaf_max_size) 552 {} 553 554 size_t leaf_max_size; 555 }; 558 struct SearchParams 559 { 561 SearchParams(int checks_IGNORED_ = 32, float eps_ = 0, bool sorted_ = true ) : 562 checks(checks_IGNORED_), eps(eps_), sorted(sorted_) {} 563 564 int checks; 565 float eps; 566 bool sorted; 567 }; ``` -------------------------------- ### Build KD-Tree Index Source: https://jlblancoc.github.io/nanoflann/nanoflann_8hpp_source.html Constructs the KD-tree by computing the bounding box and recursively dividing the data points. This method should be called before performing any searches. ```cpp void buildIndex() { init_vind(); this->freeIndex(*this); m_size_at_index_build = m_size; if(m_size == 0) return; computeBoundingBox(root_bbox); root_node = this->divideTree(*this, 0, m_size, root_bbox ); // construct the tree } ``` -------------------------------- ### KDTreeSingleIndexDynamicAdaptor_ Constructor Source: https://jlblancoc.github.io/nanoflann/nanoflann_8hpp_source.html Constructs a dynamically sized KD-tree index. Requires dimensionality, a dataset adaptor, a tree index vector, and optional parameters. ```cpp KDTreeSingleIndexDynamicAdaptor_(const int dimensionality, DatasetAdaptor &inputData, std::vector< int > &treeIndex_, const KDTreeSingleIndexAdaptorParams ¶ms=KDTreeSingleIndexAdaptorParams()) ``` -------------------------------- ### KDTreeSingleIndexAdaptor Methods Source: https://jlblancoc.github.io/nanoflann/classnanoflann_1_1KDTreeSingleIndexAdaptor-members.html Key methods for building the index and performing nearest neighbor searches. ```APIDOC ## buildIndex() ### Description Builds the KD-tree index from the provided dataset. ## knnSearch(const ElementType *query_point, const size_t num_closest, IndexType *out_indices, DistanceType *out_distances_sq, const int=10) ### Description Performs a K-nearest neighbor search for a given query point. ### Parameters - **query_point** (ElementType*) - Required - Pointer to the query point coordinates. - **num_closest** (size_t) - Required - Number of nearest neighbors to find. - **out_indices** (IndexType*) - Required - Array to store the indices of the found neighbors. - **out_distances_sq** (DistanceType*) - Required - Array to store the squared distances of the found neighbors. ## findNeighbors(RESULTSET &result, const ElementType *vec, const SearchParams &searchParams) ### Description Generic method to find neighbors based on a result set and search parameters. ### Parameters - **result** (RESULTSET) - Required - The result set object to store findings. - **vec** (ElementType*) - Required - The query vector. - **searchParams** (SearchParams) - Required - Configuration for the search process. ``` -------------------------------- ### KDTreeSingleIndexDynamicAdaptor Class Overview Source: https://jlblancoc.github.io/nanoflann/classnanoflann_1_1KDTreeSingleIndexDynamicAdaptor__.html Provides an overview of the KDTreeSingleIndexDynamicAdaptor class, its purpose, and the required interface for the DatasetAdaptor. ```APIDOC ## class nanoflann::KDTreeSingleIndexDynamicAdaptor_< Distance, DatasetAdaptor, DIM, IndexType > ### Description This class provides the k-d trees and other information for indexing a set of points for nearest-neighbor matching. The `DatasetAdaptor` must provide a specific interface for data access. ### Template Parameters * **DatasetAdaptor**: The user-provided adaptor for data points. * **Distance**: The distance metric to use (e.g., `nanoflann::metric_L1`, `nanoflann::metric_L2`). * **DIM**: Dimensionality of data points (default is -1, meaning dynamic). * **IndexType**: The type for indexing, typically `size_t` or `int`. ### DatasetAdaptor Interface Requirements The `DatasetAdaptor` must provide the following methods: * `inline size_t kdtree_get_point_count() const`: Returns the number of data points. * `inline T kdtree_get_pt(const size_t idx, int dim) const`: Returns the `dim`-th component of the `idx`-th point. * `template bool kdtree_get_bbox(BBOX &bb) const` (Optional): Computes and returns the bounding box. If it returns `false`, a default computation is used. ``` -------------------------------- ### Binary File I/O Utilities Source: https://jlblancoc.github.io/nanoflann/nanoflann_8hpp_source.html Template functions for saving and loading values or vectors to and from a binary file stream. ```cpp template void save_value(FILE* stream, const T& value, size_t count = 1) { fwrite(&value, sizeof(value),count, stream); } template void save_value(FILE* stream, const std::vector& value) { size_t size = value.size(); fwrite(&size, sizeof(size_t), 1, stream); fwrite(&value[0], sizeof(T), size, stream); } template void load_value(FILE* stream, T& value, size_t count = 1) { size_t read_cnt = fread(&value, sizeof(value), count, stream); if (read_cnt != count) { throw std::runtime_error("Cannot read from file"); } } template void load_value(FILE* stream, std::vector& value) { size_t size; size_t read_cnt = fread(&size, sizeof(size_t), 1, stream); if (read_cnt!=1) { throw std::runtime_error("Cannot read from file"); } value.resize(size); read_cnt = fread(&value[0], sizeof(T), size, stream); if (read_cnt!=size) { throw std::runtime_error("Cannot read from file"); } } ``` -------------------------------- ### L2_Simple_Adaptor Implementation Source: https://jlblancoc.github.io/nanoflann/nanoflann_8hpp_source.html Provides a simple L2 distance metric adaptor for KD-tree data sources. ```cpp 372 L2_Simple_Adaptor(const DataSource &_data_source) : data_source(_data_source) { } 374 inline DistanceType evalMetric(const T* a, const size_t b_idx, size_t size) const { 375 DistanceType result = DistanceType(); 376 for (int i=0; i 384 inline DistanceType accum_dist(const U a, const V b, int ) const 385 { 386 return (a-b)*(a-b); 387 } 388 }; ``` -------------------------------- ### nanoflann Class Methods Source: https://jlblancoc.github.io/nanoflann/functions.html Overview of key methods available in nanoflann classes for index management and searching. ```APIDOC ## Class Methods Overview ### Index Construction - **buildIndex()**: Constructs the KD-tree index for the dataset. - **loadIndex()**: Loads a previously saved index. - **freeIndex()**: Frees memory associated with the index. ### Search Operations - **knnSearch()**: Performs a K-Nearest Neighbor search. - **findNeighbors()**: General method to find neighbors within the index. ### Data Management - **addPoint() / addPoints()**: Adds new data points to the index. - **dataset_get()**: Accesses the underlying dataset. ### Memory Management - **allocate() / malloc()**: Memory allocation for internal structures. - **free_all()**: Releases all allocated memory. ``` -------------------------------- ### buildIndex Source: https://jlblancoc.github.io/nanoflann/classnanoflann_1_1KDTreeSingleIndexAdaptor.html Builds the KD-tree index for the associated dataset. ```APIDOC ## buildIndex ### Description Builds the index for the dataset associated with the KDTreeSingleIndexAdaptor. ### Method void ### Endpoint nanoflann::KDTreeSingleIndexAdaptor::buildIndex() ``` -------------------------------- ### KDTreeSingleIndexDynamicAdaptor Methods Source: https://jlblancoc.github.io/nanoflann/classnanoflann_1_1KDTreeSingleIndexDynamicAdaptor__-members.html Core methods for building the index and performing nearest neighbor searches. ```APIDOC ## buildIndex() ### Description Builds the KD-tree index for the provided dataset. ## computeBoundingBox(BoundingBox &bbox) ### Description Computes the bounding box of the dataset stored in the index. ## findNeighbors(RESULTSET &result, const ElementType *vec, const SearchParams &searchParams) const ### Description Performs a generic neighbor search using a result set object. ### Parameters #### Request Body - **result** (RESULTSET) - Required - The result set object to store found neighbors. - **vec** (ElementType*) - Required - The query point coordinates. - **searchParams** (SearchParams) - Required - Configuration parameters for the search. ## knnSearch(const ElementType *query_point, const size_t num_closest, IndexType *out_indices, DistanceType *out_distances_sq, const int=10) const ### Description Performs a K-Nearest Neighbor search. ### Parameters #### Request Body - **query_point** (ElementType*) - Required - The point to search neighbors for. - **num_closest** (size_t) - Required - The number of neighbors to find. - **out_indices** (IndexType*) - Required - Array to store the indices of the found neighbors. - **out_distances_sq** (DistanceType*) - Required - Array to store the squared distances of the found neighbors. ``` -------------------------------- ### Load KD-Tree from File Source: https://jlblancoc.github.io/nanoflann/classnanoflann_1_1KDTreeSingleIndexDynamicAdaptor__.html Loads a KD-tree structure from a file stream. The tree is reconstructed and assigned to the provided node pointer. ```cpp void load_tree(FILE *stream, NodePtr &tree) ``` -------------------------------- ### KDTreeSingleIndexDynamicAdaptor Constructor Source: https://jlblancoc.github.io/nanoflann/nanoflann_8hpp_source.html Constructs a KD-tree index with dynamic capabilities. Takes dimensionality, dataset, optional parameters, and maximum point count. ```cpp KDTreeSingleIndexDynamicAdaptor(const int dimensionality, DatasetAdaptor &inputData, const KDTreeSingleIndexAdaptorParams ¶ms=KDTreeSingleIndexAdaptorParams(), const int maximumPointCount=1e9) ``` -------------------------------- ### SO2_Adaptor Implementation Source: https://jlblancoc.github.io/nanoflann/nanoflann_8hpp_source.html Implements distance metrics for SO2 (2D rotation) data, handling periodic boundary conditions. ```cpp 396 template 397 struct SO2_Adaptor 398 { 399 typedef T ElementType; 400 typedef _DistanceType DistanceType; 401 402 const DataSource &data_source; 403 404 SO2_Adaptor(const DataSource &_data_source) : data_source(_data_source) { } 405 406 inline DistanceType evalMetric(const T* a, const size_t b_idx, size_t size) const { 407 DistanceType result = DistanceType(); 408 result = data_source.kdtree_get_pt(b_idx, 0) - a[0]; 409 if (result > M_PI) 410 result -= 2. * M_PI; 411 else if (result < -M_PI) 412 result += 2. * M_PI; 413 return result; 414 } 415 416 template 417 inline DistanceType accum_dist(const U a, const V b, int ) const 418 { 419 DistanceType result = DistanceType(); 420 result = b - a; 421 if (result > M_PI) 422 result -= 2. * M_PI; 423 else if (result < -M_PI) 424 result += 2. * M_PI; 425 return result; 426 } 427 }; ``` -------------------------------- ### KDTreeSingleIndexAdaptor::init_vind Source: https://jlblancoc.github.io/nanoflann/nanoflann_8hpp_source.html Initializes the vind vector, likely used for internal indexing. ```APIDOC ## KDTreeSingleIndexAdaptor::init_vind ### Description Initializes or resets the `vind` member, which is typically used for internal point indexing or ordering during tree construction or operations. ### Method `void init_vind()` ``` -------------------------------- ### nanoflann::SO3_acosInnerProdQuat_Adaptor Class Members Source: https://jlblancoc.github.io/nanoflann/structnanoflann_1_1SO3__acosInnerProdQuat__Adaptor-members.html Reference documentation for the members of the SO3_acosInnerProdQuat_Adaptor class within the nanoflann library. ```APIDOC ## Class: nanoflann::SO3_acosInnerProdQuat_Adaptor ### Description Adaptor class for SO3 acos inner product quaternion distance calculations. ### Members - **SO3_acosInnerProdQuat_Adaptor(const DataSource &data_source)** - Constructor initializing with a data source. - **accum_dist(const U a, const V b, int)** - Calculates accumulated distance between two values. - **evalMetric(const T *a, const size_t b_idx, size_t size)** - Evaluates the metric for a given data point index. - **data_source** - Reference to the underlying data source. - **DistanceType** - Typedef for the distance calculation type. - **ElementType** - Typedef for the element type. ``` -------------------------------- ### nanoflann::PooledAllocator Class Methods Source: https://jlblancoc.github.io/nanoflann/classnanoflann_1_1PooledAllocator.html API documentation for the memory allocation methods provided by the PooledAllocator class. ```APIDOC ## PooledAllocator::allocate ### Description Allocates a generic type T using the memory pool. ### Parameters - **count** (size_t) - Optional (default=1) - Number of instances to allocate. ### Returns - **T*** - Pointer to the allocated memory buffer. --- ## PooledAllocator::malloc ### Description Returns a pointer to a piece of new memory of the given size in bytes allocated from the pool. ### Parameters - **req_size** (size_t) - Required - Size in bytes to allocate. ### Returns - **void*** - Pointer to the allocated memory. --- ## PooledAllocator::free_all ### Description Frees all allocated memory chunks managed by the pool. ``` -------------------------------- ### Load KD-Tree from File Source: https://jlblancoc.github.io/nanoflann/nanoflann_8hpp_source.html Recursively loads a KD-tree structure from a file stream. This reconstructs the index from a saved state. ```cpp void load_tree(FILE* stream, NodePtr& tree) { tree = pool.allocate(); load_value(stream, *tree); if (tree->child1!=NULL) { load_tree(stream, tree->child1); } if (tree->child2!=NULL) { load_tree(stream, tree->child2); } } ``` -------------------------------- ### nanoflann Core Classes and Structures Source: https://jlblancoc.github.io/nanoflann/classes.html Overview of key classes and structures within the nanoflann library, including index adaptors, result sets, and metric traits. ```APIDOC ## nanoflann Core Classes and Structures ### Description This section outlines the primary classes and data structures used in the nanoflann library for building and querying approximate nearest neighbor (ANN) indices. ### Classes and Structures **Index Adaptors:** - `KDTreeSingleIndexAdaptorParams`: Parameters for configuring a single-indexed KD-tree. - `KDTreeSingleIndexDynamicAdaptor`: A dynamic adaptor for KD-tree indices. - `KDTreeSingleIndexDynamicAdaptor_`: Internal class for dynamic KD-tree adaptation. - `KDTreeEigenMatrixAdaptor`: Adaptor for KD-tree indices using Eigen matrices. - `KDTreeSingleIndexAdaptor`: Base adaptor for single-indexed KD-trees. - `KDTreeBaseClass`: Base class for KD-tree indices. - `KDTreeBaseClass::Interval`: Represents an interval within a KD-tree node. - `KDTreeBaseClass::Node`: Represents a node in the KD-tree. **Result Sets:** - `KNNResultSet`: Manages the results of k-nearest neighbor searches. - `RadiusResultSet`: Manages the results of radius searches. **Metric and Adaptors:** - `metric_SO2_Adaptor`: Adaptor for SO2 metric. - `metric_SO3_acosInnerProdQuat_Adaptor`: Adaptor for SO3 metric using acosInnerProdQuat. - `SO3_InnerProdQuat_Adaptor`: Adaptor for SO3 metric using InnerProdQuat. - `L1_Adaptor`: Adaptor for L1 metric. - `L2_Adaptor`: Adaptor for L2 metric. - `L2_Simple_Adaptor`: Adaptor for L2_Simple metric. - `metric_SO2`: Defines the SO2 metric. - `metric_SO3_acosInnerProdQuat`: Defines the SO3 metric using acosInnerProdQuat. - `metric_SO3_InnerProdQuat`: Defines the SO3 metric using InnerProdQuat. - `metric_L1`: Defines the L1 metric. - `metric_L2`: Defines the L2 metric. - `metric_L2_Simple`: Defines the L2_Simple metric. - `Metric`: Generic metric interface. **Utilities and Allocators:** - `CArray`: A C-style array wrapper. - `PooledAllocator`: An allocator that pools memory. - `array_or_vector_selector`: Selects between array and vector. - `array_or_vector_selector<-1, T>`: Specialization for selecting array or vector. - `IndexDist_Sorter`: Helper for sorting indices by distance. **Search Parameters:** - `SearchParams`: Structure to hold parameters for search operations. ### Usage Notes - The library is header-only, meaning you can include the necessary headers directly into your C++ projects. - Choose the appropriate index adaptor and metric based on your data and performance requirements. - `SearchParams` can be used to control search behavior, such as the number of leaf nodes to visit. ``` -------------------------------- ### KDTreeEigenMatrixAdaptor Query Method Source: https://jlblancoc.github.io/nanoflann/nanoflann_8hpp_source.html Performs a k-nearest neighbor search. It initializes a KNNResultSet and then uses the KD-tree index to find the nearest neighbors to the given query point. ```cpp inline void query(const num_t *query_point, const size_t num_closest, IndexType *out_indices, num_t *out_distances_sq, const int /* nChecks_IGNORED */ = 10) const { nanoflann::KNNResultSet resultSet(num_closest); resultSet.init(out_indices, out_distances_sq); index->findNeighbors(resultSet, query_point, nanoflann::SearchParams()); } ``` -------------------------------- ### Initialize Dynamic KD-Tree Source: https://jlblancoc.github.io/nanoflann/nanoflann_8hpp_source.html Initializes the internal structure of the dynamic KD-tree, creating the necessary number of sub-trees based on the 'treeCount'. ```C++ void init() { typedef KDTreeSingleIndexDynamicAdaptor_ my_kd_tree_t; std::vector index_(treeCount, my_kd_tree_t(dim /*dim*/, dataset, treeIndex, index_params)); index=index_; } ``` -------------------------------- ### nanoflann::L2_Simple_Adaptor Class Members Source: https://jlblancoc.github.io/nanoflann/structnanoflann_1_1L2__Simple__Adaptor-members.html Overview of the methods and typedefs available in the L2_Simple_Adaptor class. ```APIDOC ## Class: nanoflann::L2_Simple_Adaptor ### Description Adaptor class for L2 distance calculations in nanoflann. ### Members - **accum_dist(const U a, const V b, int)**: Calculates the accumulated distance between two values. - **evalMetric(const T *a, const size_t b_idx, size_t size)**: Evaluates the distance metric between a pointer and a data source index. - **L2_Simple_Adaptor(const DataSource &_data_source)**: Constructor initializing with a data source. - **data_source**: Reference to the underlying data source. ### Typedefs - **DistanceType**: The type used for distance calculations. - **ElementType**: The type of elements stored in the data source. ``` -------------------------------- ### Build KD-Tree Index Source: https://jlblancoc.github.io/nanoflann/classnanoflann_1_1KDTreeSingleIndexDynamicAdaptor__.html Builds the KD-tree index from the provided dataset. This operation should be called after the index is constructed and before any search operations. ```cpp void buildIndex() ``` -------------------------------- ### KDTreeEigenMatrixAdaptor Constructor Source: https://jlblancoc.github.io/nanoflann/nanoflann_8hpp_source.html Initializes a KDTreeEigenMatrixAdaptor for a given dimensionality and matrix. It validates the dimensionality against the matrix columns and the template argument DIM. The KD-tree is built upon initialization. ```cpp KDTreeEigenMatrixAdaptor(const int dimensionality, const MatrixType &mat, const int leaf_max_size = 10) : m_data_matrix(mat) { const IndexType dims = mat.cols(); if (dims!=dimensionality) throw std::runtime_error("Error: 'dimensionality' must match column count in data matrix"); if (DIM>0 && static_cast(dims)!=DIM) throw std::runtime_error("Data set dimensionality does not match the 'DIM' template argument"); index = new index_t( dims, *this /* adaptor */, nanoflann::KDTreeSingleIndexAdaptorParams(leaf_max_size ) ); index->buildIndex(); } ``` -------------------------------- ### KDTreeEigenMatrixAdaptor Constructor Source: https://jlblancoc.github.io/nanoflann/nanoflann_8hpp_source.html Constructs a KD-tree index specifically for Eigen matrix data. Requires dimensionality, the matrix, and an optional leaf size. ```cpp KDTreeEigenMatrixAdaptor(const int dimensionality, const MatrixType &mat, const int leaf_max_size=10) ``` -------------------------------- ### allocate Source: https://jlblancoc.github.io/nanoflann/nanoflann_8hpp_source.html General memory allocation function. ```APIDOC ## allocate ### Description A general-purpose function for allocating memory, likely used by various components within nanoflann. ### Method `T * allocate(size_t count=1)` ### Parameters - **count** (size_t): The number of elements to allocate memory for. Defaults to 1. ### Return Value `T*`: A pointer to the allocated memory. ``` -------------------------------- ### nanoflann Classes Overview Source: https://jlblancoc.github.io/nanoflann/group__kdtrees__grp.html This section lists the main classes provided by the nanoflann library for building and querying k-d trees. ```APIDOC ## Classes This section details the primary classes available in the nanoflann library for Approximate Nearest Neighbor (ANN) search using k-d trees. ### Class: nanoflann::KDTreeSingleIndexAdaptor **Description:** A k-d tree index adaptor for datasets where points are accessed via a dataset adaptor. This is a single-indexed version. ### Class: nanoflann::KDTreeSingleIndexDynamicAdaptor_ **Description:** A dynamic version of the single-indexed k-d tree adaptor. The underscore suffix suggests it might be an internal or base class. ### Class: nanoflann::KDTreeSingleIndexDynamicAdaptor **Description:** A dynamic k-d tree index adaptor, allowing for potential modifications or dynamic behavior in the index structure. ### Class: nanoflann::KDTreeEigenMatrixAdaptor **Description:** A specialized k-d tree adaptor designed for datasets represented as Eigen matrices. This allows for efficient integration with Eigen's matrix library. ``` -------------------------------- ### KDTreeSingleIndexAdaptor::buildIndex Source: https://jlblancoc.github.io/nanoflann/nanoflann_8hpp_source.html Builds the KD-tree index from the provided dataset. ```APIDOC ## KDTreeSingleIndexAdaptor::buildIndex ### Description Constructs the KD-tree index based on the data points in the associated dataset. ### Method `void buildIndex()` ``` -------------------------------- ### DatasetAdaptor Interface for KDTreeSingleIndexAdaptor Source: https://jlblancoc.github.io/nanoflann/classnanoflann_1_1KDTreeSingleIndexAdaptor.html The DatasetAdaptor class must provide these methods for the KDTreeSingleIndexAdaptor to function correctly. Ensure these methods are available and correctly implemented. ```c++ inline size_t kdtree_get_point_count() const { ... } ``` ```c++ inline T kdtree_get_pt(const size_t idx, int dim) const { ... } ``` ```c++ template bool kdtree_get_bbox(BBOX &bb) const { bb[0].low = ...; bb[0].high = ...; // 0th dimension limits bb[1].low = ...; bb[1].high = ...; // 1st dimension limits ... return true; } ``` -------------------------------- ### loadIndex Source: https://jlblancoc.github.io/nanoflann/classnanoflann_1_1KDTreeSingleIndexDynamicAdaptor__.html Loads a previously saved KD-tree index from a binary file stream. Note that the dataset itself is not stored and must be available to the index object. ```APIDOC ## void loadIndex ### Description Loads a previous index from a binary file. IMPORTANT NOTE: The set of data points is NOT stored in the file, so the index object must be constructed associated to the same source of data points used while building the index. See the example: examples/saveload_example.cpp ### Method void ### Endpoint N/A (Member function) ### Parameters #### Path Parameters - **_stream_** (FILE *) - Required - The file stream to load the index from. ### Request Example N/A ### Response N/A ``` -------------------------------- ### nanoflann::metric_SO3_InnerProdQuat Source: https://jlblancoc.github.io/nanoflann/structnanoflann_1_1metric__SO3__InnerProdQuat.html Documentation for the metaprogramming helper traits class for the SO3_InnerProdQuat metric. ```APIDOC ## Struct: nanoflann::metric_SO3_InnerProdQuat ### Description Metaprogramming helper traits class for the SO3_InnerProdQuat metric. ### Header #include ### File include/nanoflann.hpp ``` -------------------------------- ### Load KD-Tree from File Source: https://jlblancoc.github.io/nanoflann/nanoflann_8hpp_source.html Recursively loads a KD-tree structure from a file stream. It reconstructs the tree in memory using a pool allocator. ```cpp void load_tree(FILE* stream, NodePtr& tree) { tree = pool.allocate(); load_value(stream, *tree); if (tree->child1!=NULL) { load_tree(stream, tree->child1); } if (tree->child2!=NULL) { load_tree(stream, tree->child2); } } ``` -------------------------------- ### nanoflann::SearchParams Struct Source: https://jlblancoc.github.io/nanoflann/structnanoflann_1_1SearchParams.html Configuration parameters for KDTreeSingleIndexAdaptor::findNeighbors(). ```APIDOC ## Struct: nanoflann::SearchParams ### Description Defines search options for the KDTreeSingleIndexAdaptor::findNeighbors() method. ### Constructor `SearchParams(int checks_IGNORED_ = 32, float eps_ = 0, bool sorted_ = true)` ### Attributes - **checks** (int) - Ignored parameter (Kept for compatibility with the FLANN interface). - **eps** (float) - Search for eps-approximate neighbours (default: 0). - **sorted** (bool) - Only for radius search, require neighbours sorted by distance (default: true). ``` -------------------------------- ### nanoflann::L1_Adaptor Class Members Source: https://jlblancoc.github.io/nanoflann/structnanoflann_1_1L1__Adaptor-members.html Overview of the methods and types available in the L1_Adaptor class. ```APIDOC ## Class: nanoflann::L1_Adaptor ### Description Provides an L1 distance metric adaptor for use with nanoflann data sources. ### Methods - **L1_Adaptor(const DataSource &data_source)**: Constructor initializing the adaptor with a data source. - **accum_dist(const U a, const V b, int)**: Calculates the accumulated distance between two values. - **evalMetric(const T *a, const size_t b_idx, size_t size, DistanceType worst_dist)**: Evaluates the L1 distance metric against a data source element. ### Typedefs - **DistanceType**: The type used for distance calculations. - **ElementType**: The type of the elements stored in the data source. ### Members - **data_source**: Reference to the associated data source. ``` -------------------------------- ### nanoflann::CArray Class Source: https://jlblancoc.github.io/nanoflann/classnanoflann_1_1CArray.html Documentation for the CArray class, a STL container wrapper for compile-time constant-sized arrays, adapted from the MRPT project. ```APIDOC ## class nanoflann::CArray< T, N > ### Description A STL container (as wrapper) for arrays of constant size defined at compile time. This class is an adapted version from Boost, modified for its integration within MRPT. ### Include `#include ` ### Public Types - **static_size** (enum) - { N } - **value_type** (typedef T) - **iterator** (typedef T *) - **const_iterator** (typedef T *) - **reference** (typedef T &) - **const_reference** (typedef const T &) - **size_type** (typedef std::size_t) - **difference_type** (typedef std::ptrdiff_t) - **reverse_iterator** (typedef std::reverse_iterator< iterator >) - **const_reverse_iterator** (typedef std::reverse_iterator< const_iterator >) ### Public Member Functions - **begin** () - returns iterator - **begin** () const - returns const_iterator - **end** () - returns iterator - **end** () const - returns const_iterator - **rbegin** () - returns reverse_iterator - **rbegin** () const - returns const_reverse_iterator - **rend** () - returns reverse_iterator - **rend** () const - returns const_reverse_iterator - **operator[]** (size_type i) - returns reference - **operator[]** (size_type i) const - returns const_reference - **at** (size_type i) - returns reference - **at** (size_type i) const - returns const_reference - **front** () - returns reference - **front** () const - returns const_reference - **back** () - returns reference - **back** () const - returns const_reference - **resize** (const size_t nElements) - void - **swap** (CArray< T, N > &y) - void - **data** () const - returns const T * - **data** () - returns T * - **operator=** (const CArray< T2, N > &rhs) - returns CArray< T, N > & - **assign** (const T &value) - void - **assign** (const size_t n, const T &value) - void ### Static Public Member Functions - **size** () - returns size_type - **empty** () - returns bool - **max_size** () - returns size_type ### Public Attributes - **elems** [N] (T) ### Detailed Description A STL container (as wrapper) for arrays of constant size defined at compile time (class imported from the MRPT project). This code is an adapted version from Boost, modified for its integration within MRPT (JLBC, Dec/2009). See http://www.josuttis.com/cppcode for details and the latest version. See http://www.boost.org/libs/array for Documentation. (C) Copyright Nicolai M. Josuttis 2001. Permission to copy, use, modify, sell and distribute this software is granted provided this copyright notice appears in all copies. This software is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose. * 29 Jan 2004 - minor fixes (Nico Josuttis) * 04 Dec 2003 - update to synch with library TR1 (Alisdair Meredith) * 23 Aug 2002 - fix for Non-MSVC compilers combined with MSVC libraries. * 05 Aug 2001 - minor update (Nico Josuttis) * 20 Jan 2001 - STLport fix (Beman Dawes) * 29 Sep 2000 - Initial Revision (Nico Josuttis) Jan 30, 2004 ### Member Function Documentation #### void nanoflann::CArray< T, N >::resize(const size_t _nElements) This method has no effects in this class, but raises an exception if the expected size does not match. * * * ### Source File - include/nanoflann.hpp ``` -------------------------------- ### CArray Container Methods Source: https://jlblancoc.github.io/nanoflann/nanoflann_8hpp_source.html Fixed-size array implementation providing standard container accessors and assignment operations. ```cpp 801 reference back() { return elems[N-1]; } 802 const_reference back() const { return elems[N-1]; } 803 // size is constant 804 static inline size_type size() { return N; } 805 static bool empty() { return false; } 806 static size_type max_size() { return N; } 807 enum { static_size = N }; 809 inline void resize(const size_t nElements) { if (nElements!=N) throw std::logic_error("Try to change the size of a CArray."); } 810 // swap (note: linear complexity in N, constant for given instantiation) 811 void swap (CArray& y) { std::swap_ranges(begin(),end(),y.begin()); } 812 // direct access to data (read-only) 813 const T* data() const { return elems; } 814 // use array as C array (direct read/write access to data) 815 T* data() { return elems; } 816 // assignment with type conversion 817 template CArray& operator= (const CArray& rhs) { 818 std::copy(rhs.begin(),rhs.end(), begin()); 819 return *this; 820 } 821 // assign one value to all elements 822 inline void assign (const T& value) { for (size_t i=0;i= size()) { throw std::out_of_range("CArray<>: index out of range"); } } 828 }; // end of CArray ``` -------------------------------- ### KDTreeSingleIndexAdaptor Save/Load Source: https://jlblancoc.github.io/nanoflann/classnanoflann_1_1KDTreeSingleIndexAdaptor.html Information on saving and loading the KD-tree index. Note that the dataset itself is not stored. ```APIDOC ## KDTreeSingleIndexAdaptor Save/Load ### Description Stores the index in a binary file. IMPORTANT NOTE: The set of data points is NOT stored in the file, so when loading the index object it must be constructed associated to the same source of data points used while building it. See the example: examples/saveload_example.cpp ### See Also * loadIndex ``` -------------------------------- ### Class: KDTreeEigenMatrixAdaptor Source: https://jlblancoc.github.io/nanoflann/structnanoflann_1_1KDTreeEigenMatrixAdaptor-members.html The KDTreeEigenMatrixAdaptor class provides an interface to use Eigen matrices with the nanoflann KD-tree implementation. ```APIDOC ## Class: nanoflann::KDTreeEigenMatrixAdaptor ### Description This class acts as an adaptor to allow nanoflann to perform nearest neighbor searches directly on Eigen matrix structures. ### Methods - **KDTreeEigenMatrixAdaptor(const int dimensionality, const MatrixType &mat, const int leaf_max_size=10)**: Constructor to initialize the adaptor with a matrix and leaf size. - **query(const num_t *query_point, const size_t num_closest, IndexType *out_indices, num_t *out_distances_sq, const int=10) const**: Performs a nearest neighbor search for a given query point. - **kdtree_get_point_count() const**: Returns the number of points in the matrix. - **kdtree_get_pt(const IndexType idx, int dim) const**: Retrieves the value of a specific dimension for a given point index. - **kdtree_distance(const num_t *p1, const IndexType idx_p2, IndexType size) const**: Calculates the distance between a point and a point in the matrix. - **kdtree_get_bbox(BBOX &) const**: Retrieves the bounding box of the dataset. ```