|
29 | 29 | #pragma once
|
30 | 30 |
|
31 | 31 | #include "nanoflann.hpp"
|
32 |
| - |
33 | 32 | #include <vector>
|
34 | 33 |
|
35 |
| -// ===== This example shows how to use nanoflann with these types of containers: ======= |
36 |
| -//typedef std::vector<std::vector<double> > my_vector_of_vectors_t; |
37 |
| -//typedef std::vector<Eigen::VectorXd> my_vector_of_vectors_t; // This requires #include <Eigen/Dense> |
38 |
| -// ===================================================================================== |
39 |
| - |
40 |
| - |
41 |
| -/** A simple vector-of-vectors adaptor for nanoflann, without duplicating the storage. |
42 |
| - * The i'th vector represents a point in the state space. |
43 |
| - * |
44 |
| - * \tparam DIM If set to >0, it specifies a compile-time fixed dimensionality for the points in the data set, allowing more compiler optimizations. |
45 |
| - * \tparam num_t The type of the point coordinates (typically, double or float). |
46 |
| - * \tparam Distance The distance metric to use: nanoflann::metric_L1, nanoflann::metric_L2, nanoflann::metric_L2_Simple, etc. |
47 |
| - * \tparam IndexType The type for indices in the KD-tree index (typically, size_t of int) |
48 |
| - */ |
49 |
| -template <class VectorOfVectorsType, typename num_t = double, int DIM = -1, class Distance = nanoflann::metric_L2, typename IndexType = size_t> |
| 34 | +// ===== This example shows how to use nanoflann with these types of containers: |
| 35 | +// using my_vector_of_vectors_t = std::vector<std::vector<double> > ; |
| 36 | +// |
| 37 | +// The next one requires #include <Eigen/Dense> |
| 38 | +// using my_vector_of_vectors_t = std::vector<Eigen::VectorXd> ; |
| 39 | +// ============================================================================= |
| 40 | + |
| 41 | +/** A simple vector-of-vectors adaptor for nanoflann, without duplicating the |
| 42 | + * storage. The i'th vector represents a point in the state space. |
| 43 | + * |
| 44 | + * \tparam DIM If set to >0, it specifies a compile-time fixed dimensionality |
| 45 | + * for the points in the data set, allowing more compiler optimizations. |
| 46 | + * \tparam num_t The type of the point coordinates (typ. double or float). |
| 47 | + * \tparam Distance The distance metric to use: nanoflann::metric_L1, |
| 48 | + * nanoflann::metric_L2, nanoflann::metric_L2_Simple, etc. |
| 49 | + * \tparam IndexType The type for indices in the KD-tree index |
| 50 | + * (typically, size_t of int) |
| 51 | + */ |
| 52 | +template < |
| 53 | + class VectorOfVectorsType, typename num_t = double, int DIM = -1, |
| 54 | + class Distance = nanoflann::metric_L2, typename IndexType = size_t> |
50 | 55 | struct KDTreeVectorOfVectorsAdaptor
|
51 | 56 | {
|
52 |
| - typedef KDTreeVectorOfVectorsAdaptor<VectorOfVectorsType, num_t, DIM,Distance> self_t; |
53 |
| - typedef typename Distance::template traits<num_t, self_t>::distance_t metric_t; |
54 |
| - typedef nanoflann::KDTreeSingleIndexAdaptor< metric_t, self_t, DIM, IndexType> index_t; |
55 |
| - |
56 |
| - index_t* index; //! The kd-tree index for the user to call its methods as usual with any other FLANN index. |
57 |
| - |
58 |
| - /// Constructor: takes a const ref to the vector of vectors object with the data points |
59 |
| - KDTreeVectorOfVectorsAdaptor(const size_t /* dimensionality */, const VectorOfVectorsType &mat, const int leaf_max_size = 10) : m_data(mat) |
60 |
| - { |
61 |
| - assert(mat.size() != 0 && mat[0].size() != 0); |
62 |
| - const size_t dims = mat[0].size(); |
63 |
| - if (DIM>0 && static_cast<int>(dims) != DIM) |
64 |
| - throw std::runtime_error("Data set dimensionality does not match the 'DIM' template argument"); |
65 |
| - index = new index_t( static_cast<int>(dims), *this /* adaptor */, nanoflann::KDTreeSingleIndexAdaptorParams(leaf_max_size ) ); |
66 |
| - index->buildIndex(); |
67 |
| - } |
68 |
| - |
69 |
| - ~KDTreeVectorOfVectorsAdaptor() { |
70 |
| - delete index; |
71 |
| - } |
72 |
| - |
73 |
| - const VectorOfVectorsType &m_data; |
74 |
| - |
75 |
| - /** Query for the \a num_closest closest points to a given point (entered as query_point[0:dim-1]). |
76 |
| - * Note that this is a short-cut method for index->findNeighbors(). |
77 |
| - * The user can also call index->... methods as desired. |
78 |
| - * \note nChecks_IGNORED is ignored but kept for compatibility with the original FLANN interface. |
79 |
| - */ |
80 |
| - //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 |
81 |
| - inline void query(const num_t *query_point, const size_t num_closest, IndexType *out_indices, num_t *out_distances_sq) const |
82 |
| - { |
83 |
| - nanoflann::KNNResultSet<num_t, IndexType> resultSet(num_closest); |
84 |
| - resultSet.init(out_indices, out_distances_sq); |
85 |
| - index->findNeighbors(resultSet, query_point, nanoflann::SearchParams()); |
86 |
| - } |
87 |
| - |
88 |
| - /** @name Interface expected by KDTreeSingleIndexAdaptor |
89 |
| - * @{ */ |
90 |
| - |
91 |
| - const self_t & derived() const { |
92 |
| - return *this; |
93 |
| - } |
94 |
| - self_t & derived() { |
95 |
| - return *this; |
96 |
| - } |
97 |
| - |
98 |
| - // Must return the number of data points |
99 |
| - inline size_t kdtree_get_point_count() const { |
100 |
| - return m_data.size(); |
101 |
| - } |
102 |
| - |
103 |
| - // Returns the dim'th component of the idx'th point in the class: |
104 |
| - inline num_t kdtree_get_pt(const size_t idx, const size_t dim) const { |
105 |
| - return m_data[idx][dim]; |
106 |
| - } |
107 |
| - |
108 |
| - // Optional bounding-box computation: return false to default to a standard bbox computation loop. |
109 |
| - // Return true if the BBOX was already computed by the class and returned in "bb" so it can be avoided to redo it again. |
110 |
| - // Look at bb.size() to find out the expected dimensionality (e.g. 2 or 3 for point clouds) |
111 |
| - template <class BBOX> |
112 |
| - bool kdtree_get_bbox(BBOX & /*bb*/) const { |
113 |
| - return false; |
114 |
| - } |
115 |
| - |
116 |
| - /** @} */ |
| 57 | + using self_t = KDTreeVectorOfVectorsAdaptor< |
| 58 | + VectorOfVectorsType, num_t, DIM, Distance, IndexType>; |
| 59 | + using metric_t = |
| 60 | + typename Distance::template traits<num_t, self_t>::distance_t; |
| 61 | + using index_t = |
| 62 | + nanoflann::KDTreeSingleIndexAdaptor<metric_t, self_t, DIM, IndexType>; |
| 63 | + |
| 64 | + /** The kd-tree index for the user to call its methods as usual with any |
| 65 | + * other FLANN index */ |
| 66 | + index_t* index = nullptr; |
| 67 | + |
| 68 | + /// Constructor: takes a const ref to the vector of vectors object with the |
| 69 | + /// data points |
| 70 | + KDTreeVectorOfVectorsAdaptor( |
| 71 | + const size_t /* dimensionality */, const VectorOfVectorsType& mat, |
| 72 | + const int leaf_max_size = 10, const unsigned int n_thread_build = 1) |
| 73 | + : m_data(mat) |
| 74 | + { |
| 75 | + assert(mat.size() != 0 && mat[0].size() != 0); |
| 76 | + const size_t dims = mat[0].size(); |
| 77 | + if (DIM > 0 && static_cast<int>(dims) != DIM) |
| 78 | + throw std::runtime_error( |
| 79 | + "Data set dimensionality does not match the 'DIM' template " |
| 80 | + "argument"); |
| 81 | + index = new index_t( |
| 82 | + static_cast<int>(dims), *this /* adaptor */, |
| 83 | + nanoflann::KDTreeSingleIndexAdaptorParams( |
| 84 | + leaf_max_size, nanoflann::KDTreeSingleIndexAdaptorFlags::None, |
| 85 | + n_thread_build)); |
| 86 | + } |
| 87 | + |
| 88 | + ~KDTreeVectorOfVectorsAdaptor() { delete index; } |
| 89 | + |
| 90 | + const VectorOfVectorsType& m_data; |
| 91 | + |
| 92 | + /** Query for the \a num_closest closest points to a given point |
| 93 | + * (entered as query_point[0:dim-1]). |
| 94 | + * Note that this is a short-cut method for index->findNeighbors(). |
| 95 | + * The user can also call index->... methods as desired. |
| 96 | + */ |
| 97 | + inline void query( |
| 98 | + const num_t* query_point, const size_t num_closest, |
| 99 | + IndexType* out_indices, num_t* out_distances_sq) const |
| 100 | + { |
| 101 | + nanoflann::KNNResultSet<num_t, IndexType> resultSet(num_closest); |
| 102 | + resultSet.init(out_indices, out_distances_sq); |
| 103 | + index->findNeighbors(resultSet, query_point); |
| 104 | + } |
| 105 | + |
| 106 | + /** @name Interface expected by KDTreeSingleIndexAdaptor |
| 107 | + * @{ */ |
| 108 | + |
| 109 | + const self_t& derived() const { return *this; } |
| 110 | + self_t& derived() { return *this; } |
| 111 | + |
| 112 | + // Must return the number of data points |
| 113 | + inline size_t kdtree_get_point_count() const { return m_data.size(); } |
| 114 | + |
| 115 | + // Returns the dim'th component of the idx'th point in the class: |
| 116 | + inline num_t kdtree_get_pt(const size_t idx, const size_t dim) const |
| 117 | + { |
| 118 | + return m_data[idx][dim]; |
| 119 | + } |
| 120 | + |
| 121 | + // Optional bounding-box computation: return false to default to a standard |
| 122 | + // bbox computation loop. |
| 123 | + // Return true if the BBOX was already computed by the class and returned |
| 124 | + // in "bb" so it can be avoided to redo it again. Look at bb.size() to |
| 125 | + // find out the expected dimensionality (e.g. 2 or 3 for point clouds) |
| 126 | + template <class BBOX> |
| 127 | + bool kdtree_get_bbox(BBOX& /*bb*/) const |
| 128 | + { |
| 129 | + return false; |
| 130 | + } |
| 131 | + |
| 132 | + /** @} */ |
| 133 | + |
117 | 134 | }; // end of KDTreeVectorOfVectorsAdaptor
|
0 commit comments