46 #ifndef FASTRACK_UTILS_SEARCHABLE_SET_H 47 #define FASTRACK_UTILS_SEARCHABLE_SET_H 52 #include <flann/flann.h> 57 template <
typename N,
typename S>
67 bool Insert(
const typename N::Ptr& node);
70 std::vector<typename N::Ptr>
KnnSearch(
const S& query,
size_t k)
const;
73 std::vector<typename N::Ptr>
RadiusSearch(
const S& query,
double r)
const;
79 std::unique_ptr<flann::KDTreeIndex<flann::L2<double> > >
index_;
87 template <
typename N,
typename S>
91 for (
size_t ii = 0; ii <
index_->size(); ++ii) {
92 double* point =
index_->getPoint(ii);
99 template <
typename N,
typename S>
102 ROS_WARN(
"SearchableSet: Constructing without initial node.");
109 template <
typename N,
typename S>
112 ROS_WARN(
"SearchableSet: Tried to insert a null node.");
117 const VectorXd x = node->state.ToVector();
118 flann::Matrix<double> flann_point(
new double[x.size()], 1, x.size());
120 for (
size_t ii = 0; ii < x.size(); ii++) flann_point[0][ii] = x(ii);
125 const int kNumTrees = 1;
126 index_.reset(
new flann::KDTreeIndex<flann::L2<double> >(
127 flann_point, flann::KDTreeIndexParams(kNumTrees)));
134 const double kRebuildThreshold = 2.0;
135 index_->addPoints(flann_point, kRebuildThreshold);
145 template <
typename N,
typename S>
149 ROS_WARN(
"SearchableSet: Cannot search empty index.");
150 return std::vector<typename N::Ptr>();
154 VectorXd x = query.ToVector();
155 const flann::Matrix<double> flann_query(x.data(), 1, x.size());
158 std::vector<std::vector<int> > query_match_indices;
159 std::vector<std::vector<double> > query_squared_distances;
161 const int num_neighbors_found =
index_->knnSearch(
162 flann_query, query_match_indices, query_squared_distances,
163 static_cast<int>(k), flann::SearchParams(-1, 0.0,
false));
166 std::vector<typename N::Ptr> neighbors;
167 for (
size_t ii = 0; ii < num_neighbors_found; ii++)
168 neighbors.push_back(
registry_[query_match_indices[0][ii]]);
174 template <
typename N,
typename S>
178 ROS_WARN(
"SearchableSet: cannot search empty index.");
179 return std::vector<typename N::Ptr>();
183 VectorXd x = query.ToVector();
184 const flann::Matrix<double> flann_query(x.data(), 1, x.size());
187 std::vector<std::vector<int> > query_match_indices;
188 std::vector<std::vector<double> > query_squared_distances;
191 int num_neighbors_found =
index_->radiusSearch(
192 flann_query, query_match_indices, query_squared_distances, r * r,
193 flann::SearchParams(-1, 0.0,
false));
195 std::vector<typename N::Ptr> neighbors;
196 for (
size_t ii = 0; ii < num_neighbors_found; ii++)
197 neighbors.push_back(
registry_[query_match_indices[0][ii]]);
SearchableSet(const typename N::Ptr &node)
N::Ptr InitialNode() const
std::unique_ptr< flann::KDTreeIndex< flann::L2< double > > > index_
std::vector< typename N::Ptr > RadiusSearch(const S &query, double r) const
bool Insert(const typename N::Ptr &node)
std::vector< typename N::Ptr > registry_
std::vector< typename N::Ptr > KnnSearch(const S &query, size_t k) const