40#ifndef PCL_SEARCH_IMPL_FLANN_SEARCH_H_
41#define PCL_SEARCH_IMPL_FLANN_SEARCH_H_
43#include <flann/algorithms/kdtree_index.h>
44#include <flann/algorithms/kdtree_single_index.h>
45#include <flann/algorithms/kmeans_index.h>
47#include <pcl/search/flann_search.h>
48#include <pcl/kdtree/kdtree_flann.h>
50#include <pcl/kdtree/impl/kdtree_flann.hpp>
53template <
typename Po
intT,
typename FlannDistance>
57 return (
static_cast<IndexPtr> (
new flann::KDTreeSingleIndex<FlannDistance> (*data,
static_cast<flann::KDTreeSingleIndexParams
> (max_leaf_size_))));
61template <
typename Po
intT,
typename FlannDistance>
65 return (
static_cast<IndexPtr> (
new flann::KMeansIndex<FlannDistance> (*data,flann::KMeansIndexParams ())));
69template <
typename Po
intT,
typename FlannDistance>
73 return (
static_cast<IndexPtr> (
new flann::KDTreeIndex<FlannDistance> (*data,
static_cast<flann::KDTreeIndexParams
> (trees_))));
77template <
typename Po
intT,
typename FlannDistance>
85template <
typename Po
intT,
typename FlannDistance>
93template <
typename Po
intT,
typename FlannDistance>
bool
105template <
typename Po
intT,
typename FlannDistance>
int
108 assert (
point_representation_->isValid (point) &&
"Invalid (NaN, Inf) point coordinates given to nearestKSearch!");
111 float* data =
nullptr;
118 float* cdata = can_cast ?
const_cast<float*
> (
reinterpret_cast<const float*
> (&point)): data;
124 flann::SearchParams p;
128 if (indices.size() !=
static_cast<unsigned int> (k))
129 indices.resize (k,-1);
130 if (dists.size() !=
static_cast<unsigned int> (k))
139 for (std::size_t i = 0; i < static_cast<unsigned int> (k); ++i)
141 auto& neighbor_index = indices[i];
149template <
typename Po
intT,
typename FlannDistance>
void
151 const PointCloud& cloud,
const Indices& indices,
int k, std::vector<Indices>& k_indices,
152 std::vector< std::vector<float> >& k_sqr_distances)
const
154 if (indices.empty ())
156 k_indices.resize (cloud.size ());
157 k_sqr_distances.resize (cloud.size ());
159 if (! cloud.is_dense)
161 for (std::size_t i = 0; i < cloud.size(); i++)
163 assert (
point_representation_->isValid (cloud[i]) &&
"Invalid (NaN, Inf) point coordinates given to nearestKSearch!");
173 data =
new float[
dim_*cloud.size ()];
174 for (std::size_t i = 0; i < cloud.size (); ++i)
176 float* out = data+i*
dim_;
183 float* cdata = can_cast ?
const_cast<float*
> (
reinterpret_cast<const float*
> (&cloud[0])): data;
189 flann::SearchParams p;
199 k_indices.resize (indices.size ());
200 k_sqr_distances.resize (indices.size ());
202 if (! cloud.is_dense)
204 for (std::size_t i = 0; i < indices.size(); i++)
206 assert (
point_representation_->isValid (cloud [indices[i]]) &&
"Invalid (NaN, Inf) point coordinates given to nearestKSearch!");
210 float* data=
new float [
dim_*indices.size ()];
211 for (std::size_t i = 0; i < indices.size (); ++i)
213 float* out = data+i*
dim_;
218 flann::SearchParams p;
228 for (
auto &k_index : k_indices)
230 for (
auto &neighbor_index : k_index)
239template <
typename Po
intT,
typename FlannDistance>
int
242 unsigned int max_nn)
const
244 assert (
point_representation_->isValid (point) &&
"Invalid (NaN, Inf) point coordinates given to radiusSearch!");
247 float* data =
nullptr;
254 float* cdata = can_cast ?
const_cast<float*
> (
reinterpret_cast<const float*
> (&point)) : data;
257 flann::SearchParams p;
260 p.max_neighbors = max_nn > 0 ? max_nn : -1;
262 std::vector<Indices> i (1);
263 std::vector<std::vector<float> > d (1);
272 for (
auto &neighbor_index : indices)
281template <
typename Po
intT,
typename FlannDistance>
void
283 const PointCloud& cloud,
const Indices& indices,
double radius, std::vector<Indices>& k_indices,
284 std::vector< std::vector<float> >& k_sqr_distances,
unsigned int max_nn)
const
286 if (indices.empty ())
288 k_indices.resize (cloud.size ());
289 k_sqr_distances.resize (cloud.size ());
291 if (! cloud.is_dense)
293 for (std::size_t i = 0; i < cloud.size(); i++)
295 assert (
point_representation_->isValid (cloud[i]) &&
"Invalid (NaN, Inf) point coordinates given to radiusSearch!");
301 float* data =
nullptr;
304 data =
new float[
dim_*cloud.size ()];
305 for (std::size_t i = 0; i < cloud.size (); ++i)
307 float* out = data+i*
dim_;
312 float* cdata = can_cast ?
const_cast<float*
> (
reinterpret_cast<const float*
> (&cloud[0])) : data;
315 flann::SearchParams p;
320 p.max_neighbors = max_nn != 0 ? max_nn : -1;
322 *
index_, m, k_indices, k_sqr_distances,
static_cast<float>(radius * radius), p);
328 k_indices.resize (indices.size ());
329 k_sqr_distances.resize (indices.size ());
331 if (! cloud.is_dense)
333 for (std::size_t i = 0; i < indices.size(); i++)
335 assert (
point_representation_->isValid (cloud [indices[i]]) &&
"Invalid (NaN, Inf) point coordinates given to radiusSearch!");
339 float* data =
new float [
dim_ * indices.size ()];
340 for (std::size_t i = 0; i < indices.size (); ++i)
342 float* out = data+i*
dim_;
347 flann::SearchParams p;
352 p.max_neighbors = max_nn != 0 ? max_nn : -1;
354 *
index_, m, k_indices, k_sqr_distances,
static_cast<float>(radius * radius), p);
360 for (
auto &k_index : k_indices)
362 for (
auto &neighbor_index : k_index)
371template <
typename Po
intT,
typename FlannDistance>
void
400 for (std::size_t i = 0; i < original_no_of_points; ++i)
402 const PointT& point = (*input_)[i];
424 for (std::size_t indices_index = 0; indices_index < original_no_of_points; ++indices_index)
426 index_t cloud_index = (*indices_)[indices_index];
427 const PointT& point = (*input_)[cloud_index];
445#define PCL_INSTANTIATE_FlannSearch(T) template class PCL_EXPORTS pcl::search::FlannSearch<T>;
DefaultPointRepresentation extends PointRepresentation to define default behavior for common point ty...
virtual IndexPtr createIndex(MatrixConstPtr data)
Create a FLANN Index from the input data.
IndexPtr createIndex(MatrixConstPtr data) override
Create a FLANN Index from the input data.
virtual IndexPtr createIndex(MatrixConstPtr data)
Create a FLANN Index from the input data.
void convertInputToFlannMatrix()
converts the input data to a format usable by FLANN
int radiusSearch(const PointT &point, double radius, Indices &k_indices, std::vector< float > &k_sqr_distances, unsigned int max_nn=0) const override
Search for all the nearest neighbors of the query point in a given radius.
std::size_t total_nr_points_
shared_ptr< flann::NNIndex< FlannDistance > > IndexPtr
int checks_
Number of checks to perform for approximate NN search using the multiple randomized tree index.
shared_ptr< const flann::Matrix< float > > MatrixConstPtr
FlannSearch(bool sorted=true, FlannIndexCreatorPtr creator=FlannIndexCreatorPtr(new KdTreeIndexCreator()))
~FlannSearch() override
Destructor for FlannSearch.
MatrixPtr input_flann_
Input data in FLANN format.
bool setInputCloud(const PointCloudConstPtr &cloud, const IndicesConstPtr &indices=IndicesConstPtr()) override
Provide a pointer to the input dataset.
PointRepresentationConstPtr point_representation_
shared_ptr< FlannIndexCreator > FlannIndexCreatorPtr
int nearestKSearch(const PointT &point, int k, Indices &k_indices, std::vector< float > &k_sqr_distances) const override
Search for the k-nearest neighbors for the given query point.
typename Search< PointT >::PointCloudConstPtr PointCloudConstPtr
typename Search< PointT >::PointCloud PointCloud
FlannIndexCreatorPtr creator_
The index creator, used to (re-) create the index when the search data is passed.
IndexPtr index_
The FLANN index.
bool input_copied_for_flann_
float eps_
Epsilon for approximate NN search.
shared_ptr< flann::Matrix< float > > MatrixPtr
PointCloudConstPtr input_
Search(const std::string &name="", bool sorted=false)
Constructor.
pcl::IndicesConstPtr IndicesConstPtr
detail::int_type_t< detail::index_type_size, detail::index_type_signed > index_t
Type used for an index in PCL.
IndicesAllocator<> Indices
Type used for indices in PCL.
int radius_search(const FlannIndex &index, const Query &query, Indices &indices, Distances &dists, float radius, const SearchParams ¶ms)
Compatibility template function to allow use of various types of indices with FLANN.
int knn_search(const FlannIndex &index, const Query &query, Indices &indices, Distances &dists, unsigned int k, const SearchParams ¶ms)
Compatibility template function to allow use of various types of indices with FLANN.