Point Cloud Library (PCL)  1.7.0
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 #ifndef PCL_KDTREE_KDTREE_FLANN_H_
42 #define PCL_KDTREE_KDTREE_FLANN_H_
43 
44 #include <pcl/kdtree/kdtree.h>
45 
46 // Forward declarations
47 namespace flann
48 {
49  struct SearchParams;
50  template <typename T> struct L2_Simple;
51  template <typename T> class Index;
52 }
53 
54 namespace pcl
55 {
56  // Forward declarations
57  template <typename T> class PointRepresentation;
58 
59  /** \brief KdTreeFLANN is a generic type of 3D spatial locator using kD-tree structures. The class is making use of
60  * the FLANN (Fast Library for Approximate Nearest Neighbor) project by Marius Muja and David Lowe.
61  *
62  * \author Radu B. Rusu, Marius Muja
63  * \ingroup kdtree
64  */
65  template <typename PointT, typename Dist = ::flann::L2_Simple<float> >
66  class KdTreeFLANN : public pcl::KdTree<PointT>
67  {
68  public:
76 
79 
80  typedef boost::shared_ptr<std::vector<int> > IndicesPtr;
81  typedef boost::shared_ptr<const std::vector<int> > IndicesConstPtr;
82 
83  typedef ::flann::Index<Dist> FLANNIndex;
84 
85  // Boost shared pointers
86  typedef boost::shared_ptr<KdTreeFLANN<PointT> > Ptr;
87  typedef boost::shared_ptr<const KdTreeFLANN<PointT> > ConstPtr;
88 
89  /** \brief Default Constructor for KdTreeFLANN.
90  * \param[in] sorted set to true if the application that the tree will be used for requires sorted nearest neighbor indices (default). False otherwise.
91  *
92  * By setting sorted to false, the \ref radiusSearch operations will be faster.
93  */
94  KdTreeFLANN (bool sorted = true);
95 
96  /** \brief Copy constructor
97  * \param[in] tree the tree to copy into this
98  */
100 
101  /** \brief Copy operator
102  * \param[in] tree the tree to copy into this
103  */
104  inline KdTreeFLANN<PointT>&
106  {
108  flann_index_ = k.flann_index_;
109  cloud_ = k.cloud_;
110  index_mapping_ = k.index_mapping_;
111  identity_mapping_ = k.identity_mapping_;
112  dim_ = k.dim_;
113  total_nr_points_ = k.total_nr_points_;
114  param_k_ = k.param_k_;
115  param_radius_ = k.param_radius_;
116  return (*this);
117  }
118 
119  /** \brief Set the search epsilon precision (error bound) for nearest neighbors searches.
120  * \param[in] eps precision (error bound) for nearest neighbors searches
121  */
122  void
123  setEpsilon (float eps);
124 
125  void
126  setSortedResults (bool sorted);
127 
128  inline Ptr makeShared () { return Ptr (new KdTreeFLANN<PointT> (*this)); }
129 
130  /** \brief Destructor for KdTreeFLANN.
131  * Deletes all allocated data arrays and destroys the kd-tree structures.
132  */
133  virtual ~KdTreeFLANN ()
134  {
135  cleanup ();
136  }
137 
138  /** \brief Provide a pointer to the input dataset.
139  * \param[in] cloud the const boost shared pointer to a PointCloud message
140  * \param[in] indices the point indices subset that is to be used from \a cloud - if NULL the whole cloud is used
141  */
142  void
143  setInputCloud (const PointCloudConstPtr &cloud, const IndicesConstPtr &indices = IndicesConstPtr ());
144 
145  /** \brief Search for k-nearest neighbors for the given query point.
146  *
147  * \attention This method does not do any bounds checking for the input index
148  * (i.e., index >= cloud.points.size () || index < 0), and assumes valid (i.e., finite) data.
149  *
150  * \param[in] point a given \a valid (i.e., finite) query point
151  * \param[in] k the number of neighbors to search for
152  * \param[out] k_indices the resultant indices of the neighboring points (must be resized to \a k a priori!)
153  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points (must be resized to \a k
154  * a priori!)
155  * \return number of neighbors found
156  *
157  * \exception asserts in debug mode if the index is not between 0 and the maximum number of points
158  */
159  int
160  nearestKSearch (const PointT &point, int k,
161  std::vector<int> &k_indices, std::vector<float> &k_sqr_distances) const;
162 
163  /** \brief Search for all the nearest neighbors of the query point in a given radius.
164  *
165  * \attention This method does not do any bounds checking for the input index
166  * (i.e., index >= cloud.points.size () || index < 0), and assumes valid (i.e., finite) data.
167  *
168  * \param[in] point a given \a valid (i.e., finite) query point
169  * \param[in] radius the radius of the sphere bounding all of p_q's neighbors
170  * \param[out] k_indices the resultant indices of the neighboring points
171  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points
172  * \param[in] max_nn if given, bounds the maximum returned neighbors to this value. If \a max_nn is set to
173  * 0 or to a number higher than the number of points in the input cloud, all neighbors in \a radius will be
174  * returned.
175  * \return number of neighbors found in radius
176  *
177  * \exception asserts in debug mode if the index is not between 0 and the maximum number of points
178  */
179  int
180  radiusSearch (const PointT &point, double radius, std::vector<int> &k_indices,
181  std::vector<float> &k_sqr_distances, unsigned int max_nn = 0) const;
182 
183  private:
184  /** \brief Internal cleanup method. */
185  void
186  cleanup ();
187 
188  /** \brief Converts a PointCloud to the internal FLANN point array representation. Returns the number
189  * of points.
190  * \param cloud the PointCloud
191  */
192  void
193  convertCloudToArray (const PointCloud &cloud);
194 
195  /** \brief Converts a PointCloud with a given set of indices to the internal FLANN point array
196  * representation. Returns the number of points.
197  * \param[in] cloud the PointCloud data
198  * \param[in] indices the point cloud indices
199  */
200  void
201  convertCloudToArray (const PointCloud &cloud, const std::vector<int> &indices);
202 
203  private:
204  /** \brief Class getName method. */
205  virtual std::string
206  getName () const { return ("KdTreeFLANN"); }
207 
208  /** \brief A FLANN index object. */
209  boost::shared_ptr<FLANNIndex> flann_index_;
210 
211  /** \brief Internal pointer to data. */
212  float* cloud_;
213 
214  /** \brief mapping between internal and external indices. */
215  std::vector<int> index_mapping_;
216 
217  /** \brief whether the mapping bwwteen internal and external indices is identity */
218  bool identity_mapping_;
219 
220  /** \brief Tree dimensionality (i.e. the number of dimensions per point). */
221  int dim_;
222 
223  /** \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). */
224  int total_nr_points_;
225 
226  /** \brief The KdTree search parameters for K-nearest neighbors. */
227  boost::shared_ptr<flann::SearchParams> param_k_;
228 
229  /** \brief The KdTree search parameters for radius search. */
230  boost::shared_ptr<flann::SearchParams> param_radius_;
231  };
232 }
233 
234 #ifdef PCL_NO_PRECOMPILE
235 #include <pcl/kdtree/impl/kdtree_flann.hpp>
236 #endif
237 
238 #endif