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