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