Point Cloud Library (PCL)  1.11.1-dev
kdtree_flann.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2012, Willow Garage, Inc.
6  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id: kdtree_flann.h 36261 2011-02-26 01:34:42Z mariusm $
38  *
39  */
40 
41 #pragma once
42 
43 #include <pcl/kdtree/kdtree.h>
44 #include <flann/util/params.h>
45 
46 #include <memory>
47 
48 // Forward declarations
49 namespace flann
50 {
51  template <typename T> struct L2_Simple;
52  template <typename T> class Index;
53 }
54 
55 namespace pcl
56 {
57  /** \brief KdTreeFLANN is a generic type of 3D spatial locator using kD-tree structures. The class is making use of
58  * the FLANN (Fast Library for Approximate Nearest Neighbor) project by Marius Muja and David Lowe.
59  *
60  * \author Radu B. Rusu, Marius Muja
61  * \ingroup kdtree
62  */
63  template <typename PointT, typename Dist = ::flann::L2_Simple<float> >
64  class KdTreeFLANN : public pcl::KdTree<PointT>
65  {
66  public:
74 
77 
78  using IndicesPtr = shared_ptr<std::vector<int> >;
79  using IndicesConstPtr = shared_ptr<const std::vector<int> >;
80 
82 
83  // Boost shared pointers
84  using Ptr = shared_ptr<KdTreeFLANN<PointT, Dist> >;
85  using ConstPtr = shared_ptr<const KdTreeFLANN<PointT, Dist> >;
86 
87  /** \brief Default Constructor for KdTreeFLANN.
88  * \param[in] sorted set to true if the application that the tree will be used for requires sorted nearest neighbor indices (default). False otherwise.
89  *
90  * By setting sorted to false, the \ref radiusSearch operations will be faster.
91  */
92  KdTreeFLANN (bool sorted = true);
93 
94  /** \brief Copy constructor
95  * \param[in] k the tree to copy into this
96  */
98 
99  /** \brief Copy operator
100  * \param[in] k the tree to copy into this
101  */
104  {
106  flann_index_ = k.flann_index_;
107  cloud_ = k.cloud_;
108  index_mapping_ = k.index_mapping_;
109  identity_mapping_ = k.identity_mapping_;
110  dim_ = k.dim_;
111  total_nr_points_ = k.total_nr_points_;
112  param_k_ = k.param_k_;
113  param_radius_ = k.param_radius_;
114  return (*this);
115  }
116 
117  /** \brief Set the search epsilon precision (error bound) for nearest neighbors searches.
118  * \param[in] eps precision (error bound) for nearest neighbors searches
119  */
120  void
121  setEpsilon (float eps) override;
122 
123  void
124  setSortedResults (bool sorted);
125 
126  inline Ptr makeShared () { return Ptr (new KdTreeFLANN<PointT, Dist> (*this)); }
127 
128  /** \brief Destructor for KdTreeFLANN.
129  * Deletes all allocated data arrays and destroys the kd-tree structures.
130  */
132  {
133  cleanup ();
134  }
135 
136  /** \brief Provide a pointer to the input dataset.
137  * \param[in] cloud the const boost shared pointer to a PointCloud message
138  * \param[in] indices the point indices subset that is to be used from \a cloud - if NULL the whole cloud is used
139  */
140  void
141  setInputCloud (const PointCloudConstPtr &cloud, const IndicesConstPtr &indices = IndicesConstPtr ()) override;
142 
143  /** \brief Search for k-nearest neighbors for the given query point.
144  *
145  * \attention This method does not do any bounds checking for the input index
146  * (i.e., index >= cloud.size () || index < 0), and assumes valid (i.e., finite) data.
147  *
148  * \param[in] point a given \a valid (i.e., finite) query point
149  * \param[in] k the number of neighbors to search for
150  * \param[out] k_indices the resultant indices of the neighboring points (must be resized to \a k a priori!)
151  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points (must be resized to \a k
152  * a priori!)
153  * \return number of neighbors found
154  *
155  * \exception asserts in debug mode if the index is not between 0 and the maximum number of points
156  */
157  int
158  nearestKSearch (const PointT &point, unsigned int k,
159  std::vector<int> &k_indices, std::vector<float> &k_sqr_distances) const override;
160 
161  /** \brief Search for all the nearest neighbors of the query point in a given radius.
162  *
163  * \attention This method does not do any bounds checking for the input index
164  * (i.e., index >= cloud.size () || index < 0), and assumes valid (i.e., finite) data.
165  *
166  * \param[in] point a given \a valid (i.e., finite) query point
167  * \param[in] radius the radius of the sphere bounding all of p_q's neighbors
168  * \param[out] k_indices the resultant indices of the neighboring points
169  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points
170  * \param[in] max_nn if given, bounds the maximum returned neighbors to this value. If \a max_nn is set to
171  * 0 or to a number higher than the number of points in the input cloud, all neighbors in \a radius will be
172  * returned.
173  * \return number of neighbors found in radius
174  *
175  * \exception asserts in debug mode if the index is not between 0 and the maximum number of points
176  */
177  int
178  radiusSearch (const PointT &point, double radius, std::vector<int> &k_indices,
179  std::vector<float> &k_sqr_distances, unsigned int max_nn = 0) const override;
180 
181  private:
182  /** \brief Internal cleanup method. */
183  void
184  cleanup ();
185 
186  /** \brief Converts a PointCloud to the internal FLANN point array representation. Returns the number
187  * of points.
188  * \param cloud the PointCloud
189  */
190  void
191  convertCloudToArray (const PointCloud &cloud);
192 
193  /** \brief Converts a PointCloud with a given set of indices to the internal FLANN point array
194  * representation. Returns the number of points.
195  * \param[in] cloud the PointCloud data
196  * \param[in] indices the point cloud indices
197  */
198  void
199  convertCloudToArray (const PointCloud &cloud, const std::vector<int> &indices);
200 
201  private:
202  /** \brief Class getName method. */
203  std::string
204  getName () const override { return ("KdTreeFLANN"); }
205 
206  /** \brief A FLANN index object. */
207  std::shared_ptr<FLANNIndex> flann_index_;
208 
209  /** \brief Internal pointer to data. TODO: replace with std::shared_ptr<float[]> with C++17*/
210  std::shared_ptr<float> cloud_;
211 
212  /** \brief mapping between internal and external indices. */
213  std::vector<int> index_mapping_;
214 
215  /** \brief whether the mapping between internal and external indices is identity */
216  bool identity_mapping_;
217 
218  /** \brief Tree dimensionality (i.e. the number of dimensions per point). */
219  int dim_;
220 
221  /** \brief The total size of the data (either equal to the number of points in the input cloud or to the number of indices - if passed). */
222  uindex_t total_nr_points_;
223 
224  /** \brief The KdTree search parameters for K-nearest neighbors. */
225  ::flann::SearchParams param_k_;
226 
227  /** \brief The KdTree search parameters for radius search. */
228  ::flann::SearchParams param_radius_;
229  };
230 }
231 
232 #ifdef PCL_NO_PRECOMPILE
233 #include <pcl/kdtree/impl/kdtree_flann.hpp>
234 #endif
pcl::KdTree< FeatureT >::IndicesConstPtr
shared_ptr< const std::vector< int > > IndicesConstPtr
Definition: kdtree.h:58
pcl
Definition: convolution.h:46
pcl::KdTreeFLANN::makeShared
Ptr makeShared()
Definition: kdtree_flann.h:126
pcl::KdTree
KdTree represents the base spatial locator class for kd-tree implementations.
Definition: kdtree.h:54
pcl::KdTree< FeatureT >::ConstPtr
shared_ptr< const KdTree< FeatureT > > ConstPtr
Definition: kdtree.h:69
pcl::IndicesConstPtr
shared_ptr< const Indices > IndicesConstPtr
Definition: pcl_base.h:59
pcl::KdTreeFLANN::~KdTreeFLANN
~KdTreeFLANN()
Destructor for KdTreeFLANN.
Definition: kdtree_flann.h:131
pcl::PointCloud< FeatureT >
pcl::PointXYZRGB
A point structure representing Euclidean xyz coordinates, and the RGB color.
Definition: point_types.hpp:628
pcl::KdTree< FeatureT >::IndicesPtr
shared_ptr< std::vector< int > > IndicesPtr
Definition: kdtree.h:57
pcl::KdTreeFLANN::nearestKSearch
int nearestKSearch(const PointT &point, unsigned int k, std::vector< int > &k_indices, std::vector< float > &k_sqr_distances) const override
Search for k-nearest neighbors for the given query point.
Definition: kdtree_flann.hpp:132
pcl::KdTreeFLANN::operator=
KdTreeFLANN< PointT, Dist > & operator=(const KdTreeFLANN< PointT, Dist > &k)
Copy operator.
Definition: kdtree_flann.h:103
flann::Index
Definition: kdtree_flann.h:52
pcl::KdTreeFLANN
KdTreeFLANN is a generic type of 3D spatial locator using kD-tree structures.
Definition: kdtree_flann.h:64
pcl::KdTreeFLANN::radiusSearch
int radiusSearch(const PointT &point, double radius, std::vector< int > &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.
Definition: kdtree_flann.hpp:172
pcl::KdTree< FeatureT >::Ptr
shared_ptr< KdTree< FeatureT > > Ptr
Definition: kdtree.h:68
flann
Definition: kdtree_flann.h:49
pcl::KdTreeFLANN::KdTreeFLANN
KdTreeFLANN(bool sorted=true)
Default Constructor for KdTreeFLANN.
Definition: kdtree_flann.hpp:49
pcl::KdTreeFLANN::setEpsilon
void setEpsilon(float eps) override
Set the search epsilon precision (error bound) for nearest neighbors searches.
Definition: kdtree_flann.hpp:74
flann::L2_Simple
Definition: kdtree_flann.h:51
pcl::uindex_t
detail::int_type_t< detail::index_type_size, false > uindex_t
Type used for an unsigned index in PCL.
Definition: types.h:118
pcl::KdTreeFLANN::setInputCloud
void setInputCloud(const PointCloudConstPtr &cloud, const IndicesConstPtr &indices=IndicesConstPtr()) override
Provide a pointer to the input dataset.
Definition: kdtree_flann.hpp:92
pcl::KdTreeFLANN::Ptr
shared_ptr< KdTreeFLANN< PointT, Dist > > Ptr
Definition: kdtree_flann.h:84
pcl::KdTreeFLANN::setSortedResults
void setSortedResults(bool sorted)
Definition: kdtree_flann.hpp:83
pcl::KdTree< FeatureT >::PointCloudConstPtr
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: kdtree.h:62