Point Cloud Library (PCL)  1.7.1
kdtree.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2009-2011, 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  */
38 
39 #ifndef PCL_KDTREE_KDTREE_H_
40 #define PCL_KDTREE_KDTREE_H_
41 
42 #include <limits.h>
43 #include <pcl/pcl_macros.h>
44 #include <pcl/point_cloud.h>
45 #include <pcl/point_representation.h>
46 #include <pcl/common/io.h>
47 
48 namespace pcl
49 {
50  /** \brief KdTree represents the base spatial locator class for kd-tree implementations.
51  * \author Radu B Rusu, Bastian Steder, Michael Dixon
52  * \ingroup kdtree
53  */
54  template <typename PointT>
55  class KdTree
56  {
57  public:
58  typedef boost::shared_ptr <std::vector<int> > IndicesPtr;
59  typedef boost::shared_ptr <const std::vector<int> > IndicesConstPtr;
60 
62  typedef boost::shared_ptr<PointCloud> PointCloudPtr;
63  typedef boost::shared_ptr<const PointCloud> PointCloudConstPtr;
64 
66  //typedef boost::shared_ptr<PointRepresentation> PointRepresentationPtr;
67  typedef boost::shared_ptr<const PointRepresentation> PointRepresentationConstPtr;
68 
69  // Boost shared pointers
70  typedef boost::shared_ptr<KdTree<PointT> > Ptr;
71  typedef boost::shared_ptr<const KdTree<PointT> > ConstPtr;
72 
73  /** \brief Empty constructor for KdTree. Sets some internal values to their defaults.
74  * \param[in] sorted set to true if the application that the tree will be used for requires sorted nearest neighbor indices (default). False otherwise.
75  */
76  KdTree (bool sorted = true) : input_(), indices_(),
77  epsilon_(0.0f), min_pts_(1), sorted_(sorted),
79  {
80  };
81 
82  /** \brief Provide a pointer to the input dataset.
83  * \param[in] cloud the const boost shared pointer to a PointCloud message
84  * \param[in] indices the point indices subset that is to be used from \a cloud - if NULL the whole cloud is used
85  */
86  virtual void
88  {
89  input_ = cloud;
90  indices_ = indices;
91  }
92 
93  /** \brief Get a pointer to the vector of indices used. */
94  inline IndicesConstPtr
95  getIndices () const
96  {
97  return (indices_);
98  }
99 
100  /** \brief Get a pointer to the input point cloud dataset. */
101  inline PointCloudConstPtr
102  getInputCloud () const
103  {
104  return (input_);
105  }
106 
107  /** \brief Provide a pointer to the point representation to use to convert points into k-D vectors.
108  * \param[in] point_representation the const boost shared pointer to a PointRepresentation
109  */
110  inline void
112  {
113  point_representation_ = point_representation;
114  if (!input_) return;
115  setInputCloud (input_, indices_); // Makes sense in derived classes to reinitialize the tree
116  }
117 
118  /** \brief Get a pointer to the point representation used when converting points into k-D vectors. */
121  {
122  return (point_representation_);
123  }
124 
125  /** \brief Destructor for KdTree. Deletes all allocated data arrays and destroys the kd-tree structures. */
126  virtual ~KdTree () {};
127 
128  /** \brief Search for k-nearest neighbors for the given query point.
129  * \param[in] p_q the given query point
130  * \param[in] k the number of neighbors to search for
131  * \param[out] k_indices the resultant indices of the neighboring points (must be resized to \a k a priori!)
132  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points (must be resized to \a k
133  * a priori!)
134  * \return number of neighbors found
135  */
136  virtual int
137  nearestKSearch (const PointT &p_q, int k,
138  std::vector<int> &k_indices, std::vector<float> &k_sqr_distances) const = 0;
139 
140  /** \brief Search for k-nearest neighbors for the given query point.
141  *
142  * \attention This method does not do any bounds checking for the input index
143  * (i.e., index >= cloud.points.size () || index < 0), and assumes valid (i.e., finite) data.
144  *
145  * \param[in] cloud the point cloud data
146  * \param[in] index a \a valid index in \a cloud representing a \a valid (i.e., finite) query point
147  * \param[in] k the number of neighbors to search for
148  * \param[out] k_indices the resultant indices of the neighboring points (must be resized to \a k a priori!)
149  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points (must be resized to \a k
150  * a priori!)
151  *
152  * \return number of neighbors found
153  *
154  * \exception asserts in debug mode if the index is not between 0 and the maximum number of points
155  */
156  virtual int
157  nearestKSearch (const PointCloud &cloud, int index, int k,
158  std::vector<int> &k_indices, std::vector<float> &k_sqr_distances) const
159  {
160  assert (index >= 0 && index < static_cast<int> (cloud.points.size ()) && "Out-of-bounds error in nearestKSearch!");
161  return (nearestKSearch (cloud.points[index], k, k_indices, k_sqr_distances));
162  }
163 
164  /** \brief Search for k-nearest neighbors for the given query point.
165  * This method accepts a different template parameter for the point type.
166  * \param[in] point the given query point
167  * \param[in] k the number of neighbors to search for
168  * \param[out] k_indices the resultant indices of the neighboring points (must be resized to \a k a priori!)
169  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points (must be resized to \a k
170  * a priori!)
171  * \return number of neighbors found
172  */
173  template <typename PointTDiff> inline int
174  nearestKSearchT (const PointTDiff &point, int k,
175  std::vector<int> &k_indices, std::vector<float> &k_sqr_distances) const
176  {
177  PointT p;
178  // Copy all the data fields from the input cloud to the output one
179  typedef typename pcl::traits::fieldList<PointT>::type FieldListInT;
180  typedef typename pcl::traits::fieldList<PointTDiff>::type FieldListOutT;
181  typedef typename pcl::intersect<FieldListInT, FieldListOutT>::type FieldList;
182  pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointTDiff, PointT> (point, p));
183  return (nearestKSearch (p, k, k_indices, k_sqr_distances));
184  }
185 
186  /** \brief Search for k-nearest neighbors for the given query point (zero-copy).
187  *
188  * \attention This method does not do any bounds checking for the input index
189  * (i.e., index >= cloud.points.size () || index < 0), and assumes valid (i.e., finite) data.
190  *
191  * \param[in] index a \a valid index representing a \a valid query point in the dataset given
192  * by \a setInputCloud. If indices were given in setInputCloud, index will be the position in
193  * the indices vector.
194  *
195  * \param[in] k the number of neighbors to search for
196  * \param[out] k_indices the resultant indices of the neighboring points (must be resized to \a k a priori!)
197  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points (must be resized to \a k
198  * a priori!)
199  * \return number of neighbors found
200  *
201  * \exception asserts in debug mode if the index is not between 0 and the maximum number of points
202  */
203  virtual int
204  nearestKSearch (int index, int k,
205  std::vector<int> &k_indices, std::vector<float> &k_sqr_distances) const
206  {
207  if (indices_ == NULL)
208  {
209  assert (index >= 0 && index < static_cast<int> (input_->points.size ()) && "Out-of-bounds error in nearestKSearch!");
210  return (nearestKSearch (input_->points[index], k, k_indices, k_sqr_distances));
211  }
212  else
213  {
214  assert (index >= 0 && index < static_cast<int> (indices_->size ()) && "Out-of-bounds error in nearestKSearch!");
215  return (nearestKSearch (input_->points[(*indices_)[index]], k, k_indices, k_sqr_distances));
216  }
217  }
218 
219  /** \brief Search for all the nearest neighbors of the query point in a given radius.
220  * \param[in] p_q the given query point
221  * \param[in] radius the radius of the sphere bounding all of p_q's neighbors
222  * \param[out] k_indices the resultant indices of the neighboring points
223  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points
224  * \param[in] max_nn if given, bounds the maximum returned neighbors to this value. If \a max_nn is set to
225  * 0 or to a number higher than the number of points in the input cloud, all neighbors in \a radius will be
226  * returned.
227  * \return number of neighbors found in radius
228  */
229  virtual int
230  radiusSearch (const PointT &p_q, double radius, std::vector<int> &k_indices,
231  std::vector<float> &k_sqr_distances, unsigned int max_nn = 0) const = 0;
232 
233  /** \brief Search for all the nearest neighbors of the query point in a given radius.
234  *
235  * \attention This method does not do any bounds checking for the input index
236  * (i.e., index >= cloud.points.size () || index < 0), and assumes valid (i.e., finite) data.
237  *
238  * \param[in] cloud the point cloud data
239  * \param[in] index a \a valid index in \a cloud representing a \a valid (i.e., finite) query point
240  * \param[in] radius the radius of the sphere bounding all of p_q's neighbors
241  * \param[out] k_indices the resultant indices of the neighboring points
242  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points
243  * \param[in] max_nn if given, bounds the maximum returned neighbors to this value. If \a max_nn is set to
244  * 0 or to a number higher than the number of points in the input cloud, all neighbors in \a radius will be
245  * returned.
246  * \return number of neighbors found in radius
247  *
248  * \exception asserts in debug mode if the index is not between 0 and the maximum number of points
249  */
250  virtual int
251  radiusSearch (const PointCloud &cloud, int index, double radius,
252  std::vector<int> &k_indices, std::vector<float> &k_sqr_distances,
253  unsigned int max_nn = 0) const
254  {
255  assert (index >= 0 && index < static_cast<int> (cloud.points.size ()) && "Out-of-bounds error in radiusSearch!");
256  return (radiusSearch(cloud.points[index], radius, k_indices, k_sqr_distances, max_nn));
257  }
258 
259  /** \brief Search for all the nearest neighbors of the query point in a given radius.
260  * \param[in] point the given query point
261  * \param[in] radius the radius of the sphere bounding all of p_q's neighbors
262  * \param[out] k_indices the resultant indices of the neighboring points
263  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points
264  * \param[in] max_nn if given, bounds the maximum returned neighbors to this value. If \a max_nn is set to
265  * 0 or to a number higher than the number of points in the input cloud, all neighbors in \a radius will be
266  * returned.
267  * \return number of neighbors found in radius
268  */
269  template <typename PointTDiff> inline int
270  radiusSearchT (const PointTDiff &point, double radius, std::vector<int> &k_indices,
271  std::vector<float> &k_sqr_distances, unsigned int max_nn = 0) const
272  {
273  PointT p;
274  // Copy all the data fields from the input cloud to the output one
275  typedef typename pcl::traits::fieldList<PointT>::type FieldListInT;
276  typedef typename pcl::traits::fieldList<PointTDiff>::type FieldListOutT;
277  typedef typename pcl::intersect<FieldListInT, FieldListOutT>::type FieldList;
278  pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointTDiff, PointT> (point, p));
279  return (radiusSearch (p, radius, k_indices, k_sqr_distances, max_nn));
280  }
281 
282  /** \brief Search for all the nearest neighbors of the query point in a given radius (zero-copy).
283  *
284  * \attention This method does not do any bounds checking for the input index
285  * (i.e., index >= cloud.points.size () || index < 0), and assumes valid (i.e., finite) data.
286  *
287  * \param[in] index a \a valid index representing a \a valid query point in the dataset given
288  * by \a setInputCloud. If indices were given in setInputCloud, index will be the position in
289  * the indices vector.
290  *
291  * \param[in] radius the radius of the sphere bounding all of p_q's neighbors
292  * \param[out] k_indices the resultant indices of the neighboring points
293  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points
294  * \param[in] max_nn if given, bounds the maximum returned neighbors to this value. If \a max_nn is set to
295  * 0 or to a number higher than the number of points in the input cloud, all neighbors in \a radius will be
296  * returned.
297  * \return number of neighbors found in radius
298  *
299  * \exception asserts in debug mode if the index is not between 0 and the maximum number of points
300  */
301  virtual int
302  radiusSearch (int index, double radius, std::vector<int> &k_indices,
303  std::vector<float> &k_sqr_distances, unsigned int max_nn = 0) const
304  {
305  if (indices_ == NULL)
306  {
307  assert (index >= 0 && index < static_cast<int> (input_->points.size ()) && "Out-of-bounds error in radiusSearch!");
308  return (radiusSearch (input_->points[index], radius, k_indices, k_sqr_distances, max_nn));
309  }
310  else
311  {
312  assert (index >= 0 && index < static_cast<int> (indices_->size ()) && "Out-of-bounds error in radiusSearch!");
313  return (radiusSearch (input_->points[(*indices_)[index]], radius, k_indices, k_sqr_distances, max_nn));
314  }
315  }
316 
317  /** \brief Set the search epsilon precision (error bound) for nearest neighbors searches.
318  * \param[in] eps precision (error bound) for nearest neighbors searches
319  */
320  virtual inline void
321  setEpsilon (float eps)
322  {
323  epsilon_ = eps;
324  }
325 
326  /** \brief Get the search epsilon precision (error bound) for nearest neighbors searches. */
327  inline float
328  getEpsilon () const
329  {
330  return (epsilon_);
331  }
332 
333  /** \brief Minimum allowed number of k nearest neighbors points that a viable result must contain.
334  * \param[in] min_pts the minimum number of neighbors in a viable neighborhood
335  */
336  inline void
337  setMinPts (int min_pts)
338  {
339  min_pts_ = min_pts;
340  }
341 
342  /** \brief Get the minimum allowed number of k nearest neighbors points that a viable result must contain. */
343  inline int
344  getMinPts () const
345  {
346  return (min_pts_);
347  }
348 
349  protected:
350  /** \brief The input point cloud dataset containing the points we need to use. */
352 
353  /** \brief A pointer to the vector of point indices to use. */
355 
356  /** \brief Epsilon precision (error bound) for nearest neighbors searches. */
357  float epsilon_;
358 
359  /** \brief Minimum allowed number of k nearest neighbors points that a viable result must contain. */
360  int min_pts_;
361 
362  /** \brief Return the radius search neighbours sorted **/
363  bool sorted_;
364 
365  /** \brief For converting different point structures into k-dimensional vectors for nearest-neighbor search. */
367 
368  /** \brief Class getName method. */
369  virtual std::string
370  getName () const = 0;
371  };
372 }
373 
374 #endif //#ifndef _PCL_KDTREE_KDTREE_H_