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 <climits>
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  using IndicesPtr = boost::shared_ptr<std::vector<int> >;
59  using IndicesConstPtr = boost::shared_ptr<const std::vector<int> >;
60 
62  using PointCloudPtr = boost::shared_ptr<PointCloud>;
63  using PointCloudConstPtr = boost::shared_ptr<const PointCloud>;
64 
66  //using PointRepresentationPtr = boost::shared_ptr<PointRepresentation>;
67  using PointRepresentationConstPtr = boost::shared_ptr<const PointRepresentation>;
68 
69  // Boost shared pointers
70  using Ptr = boost::shared_ptr<KdTree<PointT> >;
71  using ConstPtr = boost::shared_ptr<const KdTree<PointT> >;
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_(),
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  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_ == nullptr)
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  assert (index >= 0 && index < static_cast<int> (indices_->size ()) && "Out-of-bounds error in nearestKSearch!");
209  return (nearestKSearch (input_->points[(*indices_)[index]], k, k_indices, k_sqr_distances));
210  }
211 
212  /** \brief Search for all the nearest neighbors of the query point in a given radius.
213  * \param[in] p_q the given query point
214  * \param[in] radius the radius of the sphere bounding all of p_q's neighbors
215  * \param[out] k_indices the resultant indices of the neighboring points
216  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points
217  * \param[in] max_nn if given, bounds the maximum returned neighbors to this value. If \a max_nn is set to
218  * 0 or to a number higher than the number of points in the input cloud, all neighbors in \a radius will be
219  * returned.
220  * \return number of neighbors found in radius
221  */
222  virtual int
223  radiusSearch (const PointT &p_q, double radius, std::vector<int> &k_indices,
224  std::vector<float> &k_sqr_distances, unsigned int max_nn = 0) const = 0;
225 
226  /** \brief Search for all the nearest neighbors of the query point in a given radius.
227  *
228  * \attention This method does not do any bounds checking for the input index
229  * (i.e., index >= cloud.points.size () || index < 0), and assumes valid (i.e., finite) data.
230  *
231  * \param[in] cloud the point cloud data
232  * \param[in] index a \a valid index in \a cloud representing a \a valid (i.e., finite) query point
233  * \param[in] radius the radius of the sphere bounding all of p_q's neighbors
234  * \param[out] k_indices the resultant indices of the neighboring points
235  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points
236  * \param[in] max_nn if given, bounds the maximum returned neighbors to this value. If \a max_nn is set to
237  * 0 or to a number higher than the number of points in the input cloud, all neighbors in \a radius will be
238  * returned.
239  * \return number of neighbors found in radius
240  *
241  * \exception asserts in debug mode if the index is not between 0 and the maximum number of points
242  */
243  virtual int
244  radiusSearch (const PointCloud &cloud, int index, double radius,
245  std::vector<int> &k_indices, std::vector<float> &k_sqr_distances,
246  unsigned int max_nn = 0) const
247  {
248  assert (index >= 0 && index < static_cast<int> (cloud.points.size ()) && "Out-of-bounds error in radiusSearch!");
249  return (radiusSearch(cloud.points[index], radius, k_indices, k_sqr_distances, max_nn));
250  }
251 
252  /** \brief Search for all the nearest neighbors of the query point in a given radius.
253  * \param[in] point the given query point
254  * \param[in] radius the radius of the sphere bounding all of p_q's neighbors
255  * \param[out] k_indices the resultant indices of the neighboring points
256  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points
257  * \param[in] max_nn if given, bounds the maximum returned neighbors to this value. If \a max_nn is set to
258  * 0 or to a number higher than the number of points in the input cloud, all neighbors in \a radius will be
259  * returned.
260  * \return number of neighbors found in radius
261  */
262  template <typename PointTDiff> inline int
263  radiusSearchT (const PointTDiff &point, double radius, std::vector<int> &k_indices,
264  std::vector<float> &k_sqr_distances, unsigned int max_nn = 0) const
265  {
266  PointT p;
267  copyPoint (point, p);
268  return (radiusSearch (p, radius, k_indices, k_sqr_distances, max_nn));
269  }
270 
271  /** \brief Search for all the nearest neighbors of the query point in a given radius (zero-copy).
272  *
273  * \attention This method does not do any bounds checking for the input index
274  * (i.e., index >= cloud.points.size () || index < 0), and assumes valid (i.e., finite) data.
275  *
276  * \param[in] index a \a valid index representing a \a valid query point in the dataset given
277  * by \a setInputCloud. If indices were given in setInputCloud, index will be the position in
278  * the indices vector.
279  *
280  * \param[in] radius the radius of the sphere bounding all of p_q's neighbors
281  * \param[out] k_indices the resultant indices of the neighboring points
282  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points
283  * \param[in] max_nn if given, bounds the maximum returned neighbors to this value. If \a max_nn is set to
284  * 0 or to a number higher than the number of points in the input cloud, all neighbors in \a radius will be
285  * returned.
286  * \return number of neighbors found in radius
287  *
288  * \exception asserts in debug mode if the index is not between 0 and the maximum number of points
289  */
290  virtual int
291  radiusSearch (int index, double radius, std::vector<int> &k_indices,
292  std::vector<float> &k_sqr_distances, unsigned int max_nn = 0) const
293  {
294  if (indices_ == nullptr)
295  {
296  assert (index >= 0 && index < static_cast<int> (input_->points.size ()) && "Out-of-bounds error in radiusSearch!");
297  return (radiusSearch (input_->points[index], radius, k_indices, k_sqr_distances, max_nn));
298  }
299  assert (index >= 0 && index < static_cast<int> (indices_->size ()) && "Out-of-bounds error in radiusSearch!");
300  return (radiusSearch (input_->points[(*indices_)[index]], radius, k_indices, k_sqr_distances, max_nn));
301  }
302 
303  /** \brief Set the search epsilon precision (error bound) for nearest neighbors searches.
304  * \param[in] eps precision (error bound) for nearest neighbors searches
305  */
306  virtual inline void
307  setEpsilon (float eps)
308  {
309  epsilon_ = eps;
310  }
311 
312  /** \brief Get the search epsilon precision (error bound) for nearest neighbors searches. */
313  inline float
314  getEpsilon () const
315  {
316  return (epsilon_);
317  }
318 
319  /** \brief Minimum allowed number of k nearest neighbors points that a viable result must contain.
320  * \param[in] min_pts the minimum number of neighbors in a viable neighborhood
321  */
322  inline void
323  setMinPts (int min_pts)
324  {
325  min_pts_ = min_pts;
326  }
327 
328  /** \brief Get the minimum allowed number of k nearest neighbors points that a viable result must contain. */
329  inline int
330  getMinPts () const
331  {
332  return (min_pts_);
333  }
334 
335  protected:
336  /** \brief The input point cloud dataset containing the points we need to use. */
338 
339  /** \brief A pointer to the vector of point indices to use. */
341 
342  /** \brief Epsilon precision (error bound) for nearest neighbors searches. */
343  float epsilon_;
344 
345  /** \brief Minimum allowed number of k nearest neighbors points that a viable result must contain. */
346  int min_pts_;
347 
348  /** \brief Return the radius search neighbours sorted **/
349  bool sorted_;
350 
351  /** \brief For converting different point structures into k-dimensional vectors for nearest-neighbor search. */
353 
354  /** \brief Class getName method. */
355  virtual std::string
356  getName () const = 0;
357  };
358 }
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.
boost::shared_ptr< std::vector< int > > IndicesPtr
Definition: kdtree.h:58
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:426
bool sorted_
Return the radius search neighbours sorted.
Definition: kdtree.h:349
int getMinPts() const
Get the minimum allowed number of k nearest neighbors points that a viable result must contain...
Definition: kdtree.h:330
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:291
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
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
PointRepresentationConstPtr getPointRepresentation() const
Get a pointer to the point representation used when converting points into k-D vectors.
Definition: kdtree.h:120
PointCloudConstPtr getInputCloud() const
Get a pointer to the input point cloud dataset.
Definition: kdtree.h:102
virtual ~KdTree()
Destructor for KdTree.
Definition: kdtree.h:126
boost::shared_ptr< const KdTree< FeatureT > > ConstPtr
Definition: kdtree.h:71
boost::shared_ptr< PointCloud > PointCloudPtr
Definition: kdtree.h:62
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
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
int min_pts_
Minimum allowed number of k nearest neighbors points that a viable result must contain.
Definition: kdtree.h:346
boost::shared_ptr< const PointRepresentation > PointRepresentationConstPtr
Definition: kdtree.h:67
float epsilon_
Epsilon precision (error bound) for nearest neighbors searches.
Definition: kdtree.h:343
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:352
boost::shared_ptr< KdTree< FeatureT > > Ptr
Definition: kdtree.h:70
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:244
float getEpsilon() const
Get the search epsilon precision (error bound) for nearest neighbors searches.
Definition: kdtree.h:314
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
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.
PointCloudConstPtr input_
The input point cloud dataset containing the points we need to use.
Definition: kdtree.h:337
virtual std::string getName() const =0
Class getName method.
IndicesConstPtr indices_
A pointer to the vector of point indices to use.
Definition: kdtree.h:340
IndicesConstPtr getIndices() const
Get a pointer to the vector of indices used.
Definition: kdtree.h:95
boost::shared_ptr< const PointCloud > PointCloudConstPtr
Definition: kdtree.h:63
DefaultPointRepresentation extends PointRepresentation to define default behavior for common point ty...
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:307
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:323
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
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:263
Defines all the PCL and non-PCL macros used.
KdTree(bool sorted=true)
Empty constructor for KdTree.
Definition: kdtree.h:76