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