Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
kdtree.h
Go to the documentation of this file.
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  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * $Id: kdtree.h 6121 2012-07-03 18:53:38Z aichim $
37  *
38  */
39 
40 #ifndef PCL_KDTREE_KDTREE_H_
41 #define PCL_KDTREE_KDTREE_H_
42 
43 #include <limits.h>
44 #include <pcl/pcl_macros.h>
45 #include <pcl/point_cloud.h>
47 #include <pcl/common/io.h>
48 
49 namespace pcl
50 {
55  template <typename PointT>
56  class KdTree
57  {
58  public:
59  typedef boost::shared_ptr <std::vector<int> > IndicesPtr;
60  typedef boost::shared_ptr <const std::vector<int> > IndicesConstPtr;
61 
63  typedef boost::shared_ptr<PointCloud> PointCloudPtr;
64  typedef boost::shared_ptr<const PointCloud> PointCloudConstPtr;
65 
67  //typedef boost::shared_ptr<PointRepresentation> PointRepresentationPtr;
68  typedef boost::shared_ptr<const PointRepresentation> PointRepresentationConstPtr;
69 
70  // Boost shared pointers
71  typedef boost::shared_ptr<KdTree<PointT> > Ptr;
72  typedef boost::shared_ptr<const KdTree<PointT> > ConstPtr;
73 
77  KdTree (bool sorted = true) : input_(), indices_(),
78  epsilon_(0.0f), min_pts_(1), sorted_(sorted),
79  point_representation_ (new DefaultPointRepresentation<PointT>)
80  {
81  };
82 
87  virtual void
89  {
90  input_ = cloud;
91  indices_ = indices;
92  }
93 
95  inline IndicesConstPtr
96  getIndices () const
97  {
98  return (indices_);
99  }
100 
102  inline PointCloudConstPtr
103  getInputCloud () const
104  {
105  return (input_);
106  }
107 
111  inline void
113  {
114  point_representation_ = point_representation;
115  setInputCloud (input_, indices_); // Makes sense in derived classes to reinitialize the tree
116  }
117 
121  {
122  return (point_representation_);
123  }
124 
126  virtual ~KdTree () {};
127 
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 
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 
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 
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 
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 
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 
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 
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 
320  virtual inline void
321  setEpsilon (float eps)
322  {
323  epsilon_ = eps;
324  }
325 
327  inline float
328  getEpsilon () const
329  {
330  return (epsilon_);
331  }
332 
336  inline void
337  setMinPts (int min_pts)
338  {
339  min_pts_ = min_pts;
340  }
341 
343  inline int
344  getMinPts () const
345  {
346  return (min_pts_);
347  }
348 
349  protected:
351  PointCloudConstPtr input_;
352 
354  IndicesConstPtr indices_;
355 
357  float epsilon_;
358 
360  int min_pts_;
361 
363  bool sorted_;
364 
366  PointRepresentationConstPtr point_representation_;
367 
369  virtual std::string
370  getName () const = 0;
371  };
372 }
373 
374 #endif //#ifndef _PCL_KDTREE_KDTREE_H_