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