Point Cloud Library (PCL)  1.9.1-dev
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 #pragma once
40 
41 #include <limits.h>
42 #include <pcl/pcl_macros.h>
43 #include <pcl/point_cloud.h>
44 #include <pcl/point_representation.h>
45 #include <pcl/common/io.h>
46 #include <pcl/common/copy_point.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
87  setInputCloud (const PointCloudConstPtr &cloud, const IndicesConstPtr &indices = IndicesConstPtr ())
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
111  setPointRepresentation (const PointRepresentationConstPtr &point_representation)
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. */
119  inline PointRepresentationConstPtr
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  copyPoint (point, p);
179  return (nearestKSearch (p, k, k_indices, k_sqr_distances));
180  }
181 
182  /** \brief Search for k-nearest neighbors for the given query point (zero-copy).
183  *
184  * \attention This method does not do any bounds checking for the input index
185  * (i.e., index >= cloud.points.size () || index < 0), and assumes valid (i.e., finite) data.
186  *
187  * \param[in] index a \a valid index representing a \a valid query point in the dataset given
188  * by \a setInputCloud. If indices were given in setInputCloud, index will be the position in
189  * the indices vector.
190  *
191  * \param[in] k the number of neighbors to search for
192  * \param[out] k_indices the resultant indices of the neighboring points (must be resized to \a k a priori!)
193  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points (must be resized to \a k
194  * a priori!)
195  * \return number of neighbors found
196  *
197  * \exception asserts in debug mode if the index is not between 0 and the maximum number of points
198  */
199  virtual int
200  nearestKSearch (int index, int k,
201  std::vector<int> &k_indices, std::vector<float> &k_sqr_distances) const
202  {
203  if (indices_ == NULL)
204  {
205  assert (index >= 0 && index < static_cast<int> (input_->points.size ()) && "Out-of-bounds error in nearestKSearch!");
206  return (nearestKSearch (input_->points[index], k, k_indices, k_sqr_distances));
207  }
208  else
209  {
210  assert (index >= 0 && index < static_cast<int> (indices_->size ()) && "Out-of-bounds error in nearestKSearch!");
211  return (nearestKSearch (input_->points[(*indices_)[index]], k, k_indices, k_sqr_distances));
212  }
213  }
214 
215  /** \brief Search for all the nearest neighbors of the query point in a given radius.
216  * \param[in] p_q the given query point
217  * \param[in] radius the radius of the sphere bounding all of p_q's neighbors
218  * \param[out] k_indices the resultant indices of the neighboring points
219  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points
220  * \param[in] max_nn if given, bounds the maximum returned neighbors to this value. If \a max_nn is set to
221  * 0 or to a number higher than the number of points in the input cloud, all neighbors in \a radius will be
222  * returned.
223  * \return number of neighbors found in radius
224  */
225  virtual int
226  radiusSearch (const PointT &p_q, double radius, std::vector<int> &k_indices,
227  std::vector<float> &k_sqr_distances, unsigned int max_nn = 0) const = 0;
228 
229  /** \brief Search for all the nearest neighbors of the query point in a given radius.
230  *
231  * \attention This method does not do any bounds checking for the input index
232  * (i.e., index >= cloud.points.size () || index < 0), and assumes valid (i.e., finite) data.
233  *
234  * \param[in] cloud the point cloud data
235  * \param[in] index a \a valid index in \a cloud representing a \a valid (i.e., finite) query point
236  * \param[in] radius the radius of the sphere bounding all of p_q's neighbors
237  * \param[out] k_indices the resultant indices of the neighboring points
238  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points
239  * \param[in] max_nn if given, bounds the maximum returned neighbors to this value. If \a max_nn is set to
240  * 0 or to a number higher than the number of points in the input cloud, all neighbors in \a radius will be
241  * returned.
242  * \return number of neighbors found in radius
243  *
244  * \exception asserts in debug mode if the index is not between 0 and the maximum number of points
245  */
246  virtual int
247  radiusSearch (const PointCloud &cloud, int index, double radius,
248  std::vector<int> &k_indices, std::vector<float> &k_sqr_distances,
249  unsigned int max_nn = 0) const
250  {
251  assert (index >= 0 && index < static_cast<int> (cloud.points.size ()) && "Out-of-bounds error in radiusSearch!");
252  return (radiusSearch(cloud.points[index], radius, k_indices, k_sqr_distances, max_nn));
253  }
254 
255  /** \brief Search for all the nearest neighbors of the query point in a given radius.
256  * \param[in] point the given query point
257  * \param[in] radius the radius of the sphere bounding all of p_q's neighbors
258  * \param[out] k_indices the resultant indices of the neighboring points
259  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points
260  * \param[in] max_nn if given, bounds the maximum returned neighbors to this value. If \a max_nn is set to
261  * 0 or to a number higher than the number of points in the input cloud, all neighbors in \a radius will be
262  * returned.
263  * \return number of neighbors found in radius
264  */
265  template <typename PointTDiff> inline int
266  radiusSearchT (const PointTDiff &point, double radius, std::vector<int> &k_indices,
267  std::vector<float> &k_sqr_distances, unsigned int max_nn = 0) const
268  {
269  PointT p;
270  copyPoint (point, p);
271  return (radiusSearch (p, radius, k_indices, k_sqr_distances, max_nn));
272  }
273 
274  /** \brief Search for all the nearest neighbors of the query point in a given radius (zero-copy).
275  *
276  * \attention This method does not do any bounds checking for the input index
277  * (i.e., index >= cloud.points.size () || index < 0), and assumes valid (i.e., finite) data.
278  *
279  * \param[in] index a \a valid index representing a \a valid query point in the dataset given
280  * by \a setInputCloud. If indices were given in setInputCloud, index will be the position in
281  * the indices vector.
282  *
283  * \param[in] radius the radius of the sphere bounding all of p_q's neighbors
284  * \param[out] k_indices the resultant indices of the neighboring points
285  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points
286  * \param[in] max_nn if given, bounds the maximum returned neighbors to this value. If \a max_nn is set to
287  * 0 or to a number higher than the number of points in the input cloud, all neighbors in \a radius will be
288  * returned.
289  * \return number of neighbors found in radius
290  *
291  * \exception asserts in debug mode if the index is not between 0 and the maximum number of points
292  */
293  virtual int
294  radiusSearch (int index, double radius, std::vector<int> &k_indices,
295  std::vector<float> &k_sqr_distances, unsigned int max_nn = 0) const
296  {
297  if (indices_ == NULL)
298  {
299  assert (index >= 0 && index < static_cast<int> (input_->points.size ()) && "Out-of-bounds error in radiusSearch!");
300  return (radiusSearch (input_->points[index], radius, k_indices, k_sqr_distances, max_nn));
301  }
302  else
303  {
304  assert (index >= 0 && index < static_cast<int> (indices_->size ()) && "Out-of-bounds error in radiusSearch!");
305  return (radiusSearch (input_->points[(*indices_)[index]], radius, k_indices, k_sqr_distances, max_nn));
306  }
307  }
308 
309  /** \brief Set the search epsilon precision (error bound) for nearest neighbors searches.
310  * \param[in] eps precision (error bound) for nearest neighbors searches
311  */
312  virtual inline void
313  setEpsilon (float eps)
314  {
315  epsilon_ = eps;
316  }
317 
318  /** \brief Get the search epsilon precision (error bound) for nearest neighbors searches. */
319  inline float
320  getEpsilon () const
321  {
322  return (epsilon_);
323  }
324 
325  /** \brief Minimum allowed number of k nearest neighbors points that a viable result must contain.
326  * \param[in] min_pts the minimum number of neighbors in a viable neighborhood
327  */
328  inline void
329  setMinPts (int min_pts)
330  {
331  min_pts_ = min_pts;
332  }
333 
334  /** \brief Get the minimum allowed number of k nearest neighbors points that a viable result must contain. */
335  inline int
336  getMinPts () const
337  {
338  return (min_pts_);
339  }
340 
341  protected:
342  /** \brief The input point cloud dataset containing the points we need to use. */
343  PointCloudConstPtr input_;
344 
345  /** \brief A pointer to the vector of point indices to use. */
346  IndicesConstPtr indices_;
347 
348  /** \brief Epsilon precision (error bound) for nearest neighbors searches. */
349  float epsilon_;
350 
351  /** \brief Minimum allowed number of k nearest neighbors points that a viable result must contain. */
352  int min_pts_;
353 
354  /** \brief Return the radius search neighbours sorted **/
355  bool sorted_;
356 
357  /** \brief For converting different point structures into k-dimensional vectors for nearest-neighbor search. */
358  PointRepresentationConstPtr point_representation_;
359 
360  /** \brief Class getName method. */
361  virtual std::string
362  getName () const = 0;
363  };
364 }
boost::shared_ptr< std::vector< int > > IndicesPtr
Definition: kdtree.h:58
pcl::PointCloud< PointT > PointCloud
Definition: kdtree.h:61
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:320
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:409
bool sorted_
Return the radius search neighbours sorted.
Definition: kdtree.h:355
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:44
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:111
boost::shared_ptr< KdTree< PointT > > Ptr
Definition: kdtree.h:70
virtual ~KdTree()
Destructor for KdTree.
Definition: kdtree.h:126
PointCloudConstPtr getInputCloud() const
Get a pointer to the input point cloud dataset.
Definition: kdtree.h:102
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:157
IndicesConstPtr getIndices() const
Get a pointer to the vector of indices used.
Definition: kdtree.h:95
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:352
float epsilon_
Epsilon precision (error bound) for nearest neighbors searches.
Definition: kdtree.h:349
virtual void setInputCloud(const PointCloudConstPtr &cloud, const IndicesConstPtr &indices=IndicesConstPtr())
Provide a pointer to the input dataset.
Definition: kdtree.h:87
PointRepresentationConstPtr point_representation_
For converting different point structures into k-dimensional vectors for nearest-neighbor search...
Definition: kdtree.h:358
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:247
PointRepresentationConstPtr getPointRepresentation() const
Get a pointer to the point representation used when converting points into k-D vectors.
Definition: kdtree.h:120
PointCloud represents the base class in PCL for storing collections of 3D points. ...
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:336
PointCloudConstPtr input_
The input point cloud dataset containing the points we need to use.
Definition: kdtree.h:343
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:294
virtual std::string getName() const =0
Class getName method.
IndicesConstPtr indices_
A pointer to the vector of point indices to use.
Definition: kdtree.h:346
boost::shared_ptr< const PointRepresentation > PointRepresentationConstPtr
Definition: kdtree.h:67
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:200
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:174
boost::shared_ptr< const std::vector< int > > IndicesConstPtr
Definition: kdtree.h:59
virtual void setEpsilon(float eps)
Set the search epsilon precision (error bound) for nearest neighbors searches.
Definition: kdtree.h:313
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:329
boost::shared_ptr< PointCloud > PointCloudPtr
Definition: kdtree.h:62
boost::shared_ptr< const KdTree< PointT > > ConstPtr
Definition: kdtree.h:71
pcl::PointRepresentation< PointT > PointRepresentation
Definition: kdtree.h:65
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:55
KdTree(bool sorted=true)
Empty constructor for KdTree.
Definition: kdtree.h:76
boost::shared_ptr< const PointCloud > PointCloudConstPtr
Definition: kdtree.h:63
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:266