Point Cloud Library (PCL)  1.7.0
search.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-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_SEARCH_SEARCH_H_
40 #define PCL_SEARCH_SEARCH_H_
41 
42 #include <pcl/point_cloud.h>
43 #include <pcl/for_each_type.h>
44 #include <pcl/common/concatenate.h>
45 
46 namespace pcl
47 {
48  namespace search
49  {
50  /** \brief Generic search class. All search wrappers must inherit from this.
51  *
52  * Each search method must implement 2 different types of search:
53  * - \b nearestKSearch - search for K-nearest neighbors.
54  * - \b radiusSearch - search for all nearest neighbors in a sphere of a given radius
55  *
56  * The input to each search method can be given in 3 different ways:
57  * - as a query point
58  * - as a (cloud, index) pair
59  * - as an index
60  *
61  * For the latter option, it is assumed that the user specified the input
62  * via a \ref setInputCloud () method first.
63  *
64  * \note In case of an error, all methods are supposed to return 0 as the number of neighbors found.
65  *
66  * \note libpcl_search deals with three-dimensional search problems. For higher
67  * level dimensional search, please refer to the libpcl_kdtree module.
68  *
69  * \author Radu B. Rusu
70  * \ingroup search
71  */
72  template<typename PointT>
73  class Search
74  {
75  public:
77  typedef typename PointCloud::Ptr PointCloudPtr;
79 
80  typedef boost::shared_ptr<pcl::search::Search<PointT> > Ptr;
81  typedef boost::shared_ptr<const pcl::search::Search<PointT> > ConstPtr;
82 
83  typedef boost::shared_ptr<std::vector<int> > IndicesPtr;
84  typedef boost::shared_ptr<const std::vector<int> > IndicesConstPtr;
85 
86  /** Constructor. */
87  Search (const std::string& name = "", bool sorted = false);
88 
89  /** Destructor. */
90  virtual
92  {
93  }
94 
95  /** \brief Returns the search method name
96  */
97  virtual const std::string&
98  getName () const;
99 
100  /** \brief sets whether the results should be sorted (ascending in the distance) or not
101  * \param[in] sorted should be true if the results should be sorted by the distance in ascending order.
102  * Otherwise the results may be returned in any order.
103  */
104  virtual void
105  setSortedResults (bool sorted);
106 
107  /** \brief Gets whether the results should be sorted (ascending in the distance) or not
108  * Otherwise the results may be returned in any order.
109  */
110  virtual bool
111  getSortedResults ();
112 
113 
114  /** \brief Pass the input dataset that the search will be performed on.
115  * \param[in] cloud a const pointer to the PointCloud data
116  * \param[in] indices the point indices subset that is to be used from the cloud
117  */
118  virtual void
119  setInputCloud (const PointCloudConstPtr& cloud,
120  const IndicesConstPtr &indices = IndicesConstPtr ());
121 
122  /** \brief Get a pointer to the input point cloud dataset. */
123  virtual PointCloudConstPtr
124  getInputCloud () const
125  {
126  return (input_);
127  }
128 
129  /** \brief Get a pointer to the vector of indices used. */
130  virtual IndicesConstPtr
131  getIndices () const
132  {
133  return (indices_);
134  }
135 
136  /** \brief Search for the k-nearest neighbors for the given query point.
137  * \param[in] point the given query point
138  * \param[in] k the number of neighbors to search for
139  * \param[out] k_indices the resultant indices of the neighboring points (must be resized to \a k a priori!)
140  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points (must be resized to \a k
141  * a priori!)
142  * \return number of neighbors found
143  */
144  virtual int
145  nearestKSearch (const PointT &point, int k, std::vector<int> &k_indices,
146  std::vector<float> &k_sqr_distances) const = 0;
147 
148  /** \brief Search for k-nearest neighbors for the given query point.
149  * This method accepts a different template parameter for the point type.
150  * \param[in] point the given query point
151  * \param[in] k the number of neighbors to search for
152  * \param[out] k_indices the resultant indices of the neighboring points (must be resized to \a k a priori!)
153  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points (must be resized to \a k
154  * a priori!)
155  * \return number of neighbors found
156  */
157  template <typename PointTDiff> inline int
158  nearestKSearchT (const PointTDiff &point, int k,
159  std::vector<int> &k_indices, std::vector<float> &k_sqr_distances) const
160  {
161  PointT p;
162  // Copy all the data fields from the input cloud to the output one
163  typedef typename pcl::traits::fieldList<PointT>::type FieldListInT;
164  typedef typename pcl::traits::fieldList<PointTDiff>::type FieldListOutT;
165  typedef typename pcl::intersect<FieldListInT, FieldListOutT>::type FieldList;
166  pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointTDiff, PointT> (point, p));
167  return (nearestKSearch (p, k, k_indices, k_sqr_distances));
168  }
169 
170  /** \brief Search for k-nearest neighbors for the given query point.
171  *
172  * \attention This method does not do any bounds checking for the input index
173  * (i.e., index >= cloud.points.size () || index < 0), and assumes valid (i.e., finite) data.
174  *
175  * \param[in] cloud the point cloud data
176  * \param[in] index a \a valid index in \a cloud representing a \a valid (i.e., finite) query point
177  * \param[in] k the number of neighbors to search for
178  * \param[out] k_indices the resultant indices of the neighboring points (must be resized to \a k a priori!)
179  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points (must be resized to \a k
180  * a priori!)
181  *
182  * \return number of neighbors found
183  *
184  * \exception asserts in debug mode if the index is not between 0 and the maximum number of points
185  */
186  virtual int
187  nearestKSearch (const PointCloud &cloud, int index, int k,
188  std::vector<int> &k_indices,
189  std::vector<float> &k_sqr_distances) const;
190 
191  /** \brief Search for k-nearest neighbors for the given query point (zero-copy).
192  *
193  * \attention This method does not do any bounds checking for the input index
194  * (i.e., index >= cloud.points.size () || index < 0), and assumes valid (i.e., finite) data.
195  *
196  * \param[in] index a \a valid index representing a \a valid query point in the dataset given
197  * by \a setInputCloud. If indices were given in setInputCloud, index will be the position in
198  * the indices vector.
199  *
200  * \param[in] k the number of neighbors to search for
201  * \param[out] k_indices the resultant indices of the neighboring points (must be resized to \a k a priori!)
202  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points (must be resized to \a k
203  * a priori!)
204  * \return number of neighbors found
205  *
206  * \exception asserts in debug mode if the index is not between 0 and the maximum number of points
207  */
208  virtual int
209  nearestKSearch (int index, int k,
210  std::vector<int> &k_indices,
211  std::vector<float> &k_sqr_distances) const;
212 
213  /** \brief Search for the k-nearest neighbors for the given query point.
214  * \param[in] cloud the point cloud data
215  * \param[in] indices a vector of point cloud indices to query for nearest neighbors
216  * \param[in] k the number of neighbors to search for
217  * \param[out] k_indices the resultant indices of the neighboring points, k_indices[i] corresponds to the neighbors of the query point i
218  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points, k_sqr_distances[i] corresponds to the neighbors of the query point i
219  */
220  virtual void
221  nearestKSearch (const PointCloud& cloud, const std::vector<int>& indices,
222  int k, std::vector< std::vector<int> >& k_indices,
223  std::vector< std::vector<float> >& k_sqr_distances) const;
224 
225  /** \brief Search for the k-nearest neighbors for the given query point. Use this method if the query points are of a different type than the points in the data set (e.g. PointXYZRGBA instead of PointXYZ).
226  * \param[in] cloud the point cloud data
227  * \param[in] indices a vector of point cloud indices to query for nearest neighbors
228  * \param[in] k the number of neighbors to search for
229  * \param[out] k_indices the resultant indices of the neighboring points, k_indices[i] corresponds to the neighbors of the query point i
230  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points, k_sqr_distances[i] corresponds to the neighbors of the query point i
231  * \note This method copies the input point cloud of type PointTDiff to a temporary cloud of type PointT and performs the batch search on the new cloud. You should prefer the single-point search if you don't use a search algorithm that accelerates batch NN search.
232  */
233  template <typename PointTDiff> void
234  nearestKSearchT (const pcl::PointCloud<PointTDiff> &cloud, const std::vector<int>& indices, int k, std::vector< std::vector<int> > &k_indices,
235  std::vector< std::vector<float> > &k_sqr_distances) const
236  {
237  // Copy all the data fields from the input cloud to the output one
238  typedef typename pcl::traits::fieldList<PointT>::type FieldListInT;
239  typedef typename pcl::traits::fieldList<PointTDiff>::type FieldListOutT;
240  typedef typename pcl::intersect<FieldListInT, FieldListOutT>::type FieldList;
241 
243  if (indices.empty ())
244  {
245  pc.resize (cloud.size());
246  for (size_t i = 0; i < cloud.size(); i++)
247  {
248  pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointTDiff, PointT> (
249  cloud[i], pc[i]));
250  }
251  nearestKSearch (pc,std::vector<int>(),k,k_indices,k_sqr_distances);
252  }
253  else
254  {
255  pc.resize (indices.size());
256  for (size_t i = 0; i < indices.size(); i++)
257  {
258  pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointTDiff, PointT> (
259  cloud[indices[i]], pc[i]));
260  }
261  nearestKSearch (pc,std::vector<int>(),k,k_indices,k_sqr_distances);
262  }
263  }
264 
265  /** \brief Search for all the nearest neighbors of the query point in a given radius.
266  * \param[in] point the given query point
267  * \param[in] radius the radius of the sphere bounding all of p_q's neighbors
268  * \param[out] k_indices the resultant indices of the neighboring points
269  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points
270  * \param[in] max_nn if given, bounds the maximum returned neighbors to this value. If \a max_nn is set to
271  * 0 or to a number higher than the number of points in the input cloud, all neighbors in \a radius will be
272  * returned.
273  * \return number of neighbors found in radius
274  */
275  virtual int
276  radiusSearch (const PointT& point, double radius, std::vector<int>& k_indices,
277  std::vector<float>& k_sqr_distances, unsigned int max_nn = 0) const = 0;
278 
279  /** \brief Search for all the nearest neighbors of the query point in a given radius.
280  * \param[in] point the given query point
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  template <typename PointTDiff> inline int
290  radiusSearchT (const PointTDiff &point, double radius, std::vector<int> &k_indices,
291  std::vector<float> &k_sqr_distances, unsigned int max_nn = 0) const
292  {
293  PointT p;
294  // Copy all the data fields from the input cloud to the output one
295  typedef typename pcl::traits::fieldList<PointT>::type FieldListInT;
296  typedef typename pcl::traits::fieldList<PointTDiff>::type FieldListOutT;
297  typedef typename pcl::intersect<FieldListInT, FieldListOutT>::type FieldList;
298  pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointTDiff, PointT> (point, p));
299  return (radiusSearch (p, radius, k_indices, k_sqr_distances, max_nn));
300  }
301 
302  /** \brief Search for all the nearest neighbors of the query point in a given radius.
303  *
304  * \attention This method does not do any bounds checking for the input index
305  * (i.e., index >= cloud.points.size () || index < 0), and assumes valid (i.e., finite) data.
306  *
307  * \param[in] cloud the point cloud data
308  * \param[in] index a \a valid index in \a cloud representing a \a valid (i.e., finite) query point
309  * \param[in] radius the radius of the sphere bounding all of p_q's neighbors
310  * \param[out] k_indices the resultant indices of the neighboring points
311  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points
312  * \param[in] max_nn if given, bounds the maximum returned neighbors to this value. If \a max_nn is set to
313  * 0 or to a number higher than the number of points in the input cloud, all neighbors in \a radius will be
314  * returned.
315  * \return number of neighbors found in radius
316  *
317  * \exception asserts in debug mode if the index is not between 0 and the maximum number of points
318  */
319  virtual int
320  radiusSearch (const PointCloud &cloud, int index, double radius,
321  std::vector<int> &k_indices, std::vector<float> &k_sqr_distances,
322  unsigned int max_nn = 0) const;
323 
324  /** \brief Search for all the nearest neighbors of the query point in a given radius (zero-copy).
325  *
326  * \attention This method does not do any bounds checking for the input index
327  * (i.e., index >= cloud.points.size () || index < 0), and assumes valid (i.e., finite) data.
328  *
329  * \param[in] index a \a valid index representing a \a valid query point in the dataset given
330  * by \a setInputCloud. If indices were given in setInputCloud, index will be the position in
331  * the indices vector.
332  *
333  * \param[in] radius the radius of the sphere bounding all of p_q's neighbors
334  * \param[out] k_indices the resultant indices of the neighboring points
335  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points
336  * \param[in] max_nn if given, bounds the maximum returned neighbors to this value. If \a max_nn is set to
337  * 0 or to a number higher than the number of points in the input cloud, all neighbors in \a radius will be
338  * returned.
339  * \return number of neighbors found in radius
340  *
341  * \exception asserts in debug mode if the index is not between 0 and the maximum number of points
342  */
343  virtual int
344  radiusSearch (int index, double radius, std::vector<int> &k_indices,
345  std::vector<float> &k_sqr_distances, unsigned int max_nn = 0) const;
346 
347  /** \brief Search for all the nearest neighbors of the query point in a given radius.
348  * \param[in] cloud the point cloud data
349  * \param[in] indices the indices in \a cloud. If indices is empty, neighbors will be searched for all points.
350  * \param[in] radius the radius of the sphere bounding all of p_q's neighbors
351  * \param[out] k_indices the resultant indices of the neighboring points, k_indices[i] corresponds to the neighbors of the query point i
352  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points, k_sqr_distances[i] corresponds to the neighbors of the query point i
353  * \param[in] max_nn if given, bounds the maximum returned neighbors to this value. If \a max_nn is set to
354  * 0 or to a number higher than the number of points in the input cloud, all neighbors in \a radius will be
355  * returned.
356  */
357  virtual void
358  radiusSearch (const PointCloud& cloud,
359  const std::vector<int>& indices,
360  double radius,
361  std::vector< std::vector<int> >& k_indices,
362  std::vector< std::vector<float> > &k_sqr_distances,
363  unsigned int max_nn = 0) const;
364 
365  /** \brief Search for all the nearest neighbors of the query points in a given radius.
366  * \param[in] cloud the point cloud data
367  * \param[in] indices a vector of point cloud indices to query for nearest neighbors
368  * \param[in] radius the radius of the sphere bounding all of p_q's neighbors
369  * \param[out] k_indices the resultant indices of the neighboring points, k_indices[i] corresponds to the neighbors of the query point i
370  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points, k_sqr_distances[i] corresponds to the neighbors of the query point i
371  * \param[in] max_nn if given, bounds the maximum returned neighbors to this value. If \a max_nn is set to
372  * 0 or to a number higher than the number of points in the input cloud, all neighbors in \a radius will be
373  * returned.
374  * \note This method copies the input point cloud of type PointTDiff to a temporary cloud of type PointT and performs the batch search on the new cloud. You should prefer the single-point search if you don't use a search algorithm that accelerates batch NN search.
375  */
376  template <typename PointTDiff> void
378  const std::vector<int>& indices,
379  double radius,
380  std::vector< std::vector<int> > &k_indices,
381  std::vector< std::vector<float> > &k_sqr_distances,
382  unsigned int max_nn = 0) const
383  {
384  // Copy all the data fields from the input cloud to the output one
385  typedef typename pcl::traits::fieldList<PointT>::type FieldListInT;
386  typedef typename pcl::traits::fieldList<PointTDiff>::type FieldListOutT;
387  typedef typename pcl::intersect<FieldListInT, FieldListOutT>::type FieldList;
388 
390  if (indices.empty ())
391  {
392  pc.resize (cloud.size ());
393  for (size_t i = 0; i < cloud.size (); ++i)
394  pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointTDiff, PointT> (cloud[i], pc[i]));
395  radiusSearch (pc, std::vector<int> (), radius, k_indices, k_sqr_distances, max_nn);
396  }
397  else
398  {
399  pc.resize (indices.size ());
400  for (size_t i = 0; i < indices.size (); ++i)
401  pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointTDiff, PointT> (cloud[indices[i]], pc[i]));
402  radiusSearch (pc, std::vector<int>(), radius, k_indices, k_sqr_distances, max_nn);
403  }
404  }
405 
406  protected:
407  void
408  sortResults (std::vector<int>& indices, std::vector<float>& distances) const;
409 
413  std::string name_;
414 
415  private:
416  struct Compare
417  {
418  Compare (const std::vector<float>& distances)
419  : distances_ (distances)
420  {
421  }
422 
423  bool
424  operator () (int first, int second) const
425  {
426  return (distances_ [first] < distances_[second]);
427  }
428 
429  const std::vector<float>& distances_;
430  };
431  }; // class Search
432  } // namespace search
433 } // namespace pcl
434 
435 #ifdef PCL_NO_PRECOMPILE
436 #include <pcl/search/impl/search.hpp>
437 #endif
438 
439 #endif //#ifndef _PCL_SEARCH_SEARCH_H_