46 #ifndef FASTRACK_UTILS_KDTREE_MAP_H 47 #define FASTRACK_UTILS_KDTREE_MAP_H 52 #include <flann/flann.h> 57 template <
int K,
typename V>
66 bool Insert(
const std::pair<VectorKd, V>& entry);
67 bool Insert(
const VectorKd& key,
const V& value) {
68 return Insert({key, value});
72 template <
typename Container>
73 bool Insert(
const Container& pairs) {
74 for (
const std::pair<VectorKd, V>& entry : pairs)
75 if (!
Insert(entry))
return false;
81 std::vector<std::pair<VectorKd, V>>
KnnSearch(
const VectorKd& query,
85 std::vector<std::pair<VectorKd, V>>
RadiusSearch(
const VectorKd& query,
89 const std::vector<std::pair<VectorKd, V>>&
Registry()
const {
97 std::unique_ptr<flann::KDTreeIndex<flann::L2<double>>>
index_;
104 template <
int K,
typename V>
110 flann::Matrix<double> flann_point(
registry_.back().first.data(), 1, K);
114 constexpr
int kNumTrees = 1;
115 index_.reset(
new flann::KDTreeIndex<flann::L2<double>>(
116 flann_point, flann::KDTreeIndexParams(kNumTrees)));
122 constexpr
float kRebuildThreshold = 2.0;
123 index_->addPoints(flann_point, kRebuildThreshold);
130 template <
int K,
typename V>
131 std::vector<std::pair<typename KdtreeMap<K, V>::VectorKd, V>>
133 std::vector<std::pair<VectorKd, V>> neighbors;
136 ROS_WARN_THROTTLE(1.0,
"KdtreeMap: Cannot search empty index.");
141 flann::Matrix<double> flann_query(
new double[K], 1, K);
142 for (
size_t ii = 0; ii < K; ii++) flann_query[0][ii] = query(ii);
145 std::vector<std::vector<int>> query_match_indices;
146 std::vector<std::vector<double>> query_squared_distances;
148 const int num_neighbors_found =
index_->knnSearch(
149 flann_query, query_match_indices, query_squared_distances,
150 static_cast<int>(k), flann::SearchParams(FLANN_CHECKS_UNLIMITED));
153 for (
size_t ii = 0; ii < num_neighbors_found; ii++)
154 neighbors.push_back(
registry_[query_match_indices[0][ii]]);
157 delete[] flann_query.ptr();
163 template <
int K,
typename V>
164 std::vector<std::pair<typename KdtreeMap<K, V>::VectorKd, V>>
166 std::vector<std::pair<VectorKd, V>> neighbors;
169 ROS_WARN(
"KdtreeMap: cannot search empty index.");
174 flann::Matrix<double> flann_query(
new double[K], 1, K);
175 for (
size_t ii = 0; ii < K; ii++) flann_query[0][ii] = query(ii);
178 std::vector<std::vector<int>> query_match_indices;
179 std::vector<std::vector<double>> query_squared_distances;
182 constexpr
double kExactSearch = 0.0;
183 constexpr
bool kDoNotSort =
false;
184 const int num_neighbors_found =
index_->radiusSearch(
185 flann_query, query_match_indices, query_squared_distances, r * r,
186 flann::SearchParams(FLANN_CHECKS_UNLIMITED, kExactSearch, kDoNotSort));
189 for (
size_t ii = 0; ii < num_neighbors_found; ii++)
190 neighbors.push_back(
registry_[query_match_indices[0][ii]]);
193 delete[] flann_query.ptr();
std::vector< std::pair< VectorKd, V > > KnnSearch(const VectorKd &query, size_t k) const
const std::vector< std::pair< VectorKd, V > > & Registry() const
bool Insert(const Container &pairs)
Eigen::Matrix< double, K, 1 > VectorKd
std::vector< std::pair< VectorKd, V > > registry_
std::unique_ptr< flann::KDTreeIndex< flann::L2< double > > > index_
std::vector< std::pair< VectorKd, V > > RadiusSearch(const VectorKd &query, double r) const
bool Insert(const std::pair< VectorKd, V > &entry)
bool Insert(const VectorKd &key, const V &value)