Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
search.h
Go to the documentation of this file.
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  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * $Id: search.h 4914 2012-03-05 17:26:23Z rusu $
37  *
38  */
39 
40 #ifndef PCL_SEARCH_SEARCH_H_
41 #define PCL_SEARCH_SEARCH_H_
42 
43 #include <pcl/point_cloud.h>
44 #include <pcl/common/io.h>
45 
46 namespace pcl
47 {
48  namespace search
49  {
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 
87  Search (const std::string& name = "", bool sorted = false)
88  : input_ ()
89  , indices_ ()
90  , sorted_results_ (sorted)
91  , name_ (name)
92  {
93  }
94 
96  virtual
98  {
99  }
100 
103  virtual const std::string&
104  getName () const
105  {
106  return (name_);
107  }
108 
113  virtual void
114  setSortedResults (bool sorted)
115  {
116  sorted_results_ = sorted;
117  }
118 
123  virtual void
125  {
126  input_ = cloud;
127  indices_ = indices;
128  }
129 
131  virtual PointCloudConstPtr
132  getInputCloud () const
133  {
134  return (input_);
135  }
136 
138  virtual IndicesConstPtr
139  getIndices () const
140  {
141  return (indices_);
142  }
143 
152  virtual int
153  nearestKSearch (const PointT &point, int k, std::vector<int> &k_indices,
154  std::vector<float> &k_sqr_distances) const = 0;
155 
165  template <typename PointTDiff> inline int
166  nearestKSearchT (const PointTDiff &point, int k,
167  std::vector<int> &k_indices, std::vector<float> &k_sqr_distances) const
168  {
169  PointT p;
170  // Copy all the data fields from the input cloud to the output one
171  typedef typename pcl::traits::fieldList<PointT>::type FieldListInT;
172  typedef typename pcl::traits::fieldList<PointTDiff>::type FieldListOutT;
173  typedef typename pcl::intersect<FieldListInT, FieldListOutT>::type FieldList;
174  pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointTDiff, PointT> (point, p));
175  return (nearestKSearch (p, k, k_indices, k_sqr_distances));
176  }
177 
194  virtual int
195  nearestKSearch (const PointCloud &cloud, int index, int k,
196  std::vector<int> &k_indices, std::vector<float> &k_sqr_distances) const
197  {
198  assert (index >= 0 && index < static_cast<int> (cloud.points.size ()) && "Out-of-bounds error in nearestKSearch!");
199  return (nearestKSearch (cloud.points[index], k, k_indices, k_sqr_distances));
200  }
201 
219  virtual int
220  nearestKSearch (int index, int k,
221  std::vector<int> &k_indices, std::vector<float> &k_sqr_distances) const
222  {
223  if (indices_ == NULL)
224  {
225  assert (index >= 0 && index < static_cast<int> (input_->points.size ()) && "Out-of-bounds error in nearestKSearch!");
226  return (nearestKSearch (input_->points[index], k, k_indices, k_sqr_distances));
227  }
228  else
229  {
230  assert (index >= 0 && index < static_cast<int> (indices_->size ()) && "Out-of-bounds error in nearestKSearch!");
231  if (index >= static_cast<int> (indices_->size ()) || index < 0)
232  return (0);
233  return (nearestKSearch (input_->points[(*indices_)[index]], k, k_indices, k_sqr_distances));
234  }
235  }
236 
244  virtual void
245  nearestKSearch (const PointCloud& cloud, const std::vector<int>& indices, int k, std::vector< std::vector<int> >& k_indices,
246  std::vector< std::vector<float> >& k_sqr_distances) const
247  {
248  if (indices.empty ())
249  {
250  k_indices.resize (cloud.size ());
251  k_sqr_distances.resize (cloud.size ());
252  for (size_t i = 0; i < cloud.size (); i++)
253  nearestKSearch (cloud, static_cast<int> (i), k, k_indices[i], k_sqr_distances[i]);
254  }
255  else
256  {
257  k_indices.resize (indices.size ());
258  k_sqr_distances.resize (indices.size ());
259  for (size_t i = 0; i < indices.size (); i++)
260  nearestKSearch (cloud, indices[i], k, k_indices[i], k_sqr_distances[i]);
261  }
262  }
263 
272  template <typename PointTDiff> void
273  nearestKSearchT (const pcl::PointCloud<PointTDiff> &cloud, const std::vector<int>& indices, int k, std::vector< std::vector<int> > &k_indices,
274  std::vector< std::vector<float> > &k_sqr_distances) const
275  {
276  // Copy all the data fields from the input cloud to the output one
277  typedef typename pcl::traits::fieldList<PointT>::type FieldListInT;
278  typedef typename pcl::traits::fieldList<PointTDiff>::type FieldListOutT;
279  typedef typename pcl::intersect<FieldListInT, FieldListOutT>::type FieldList;
280 
282  if (indices.empty ())
283  {
284  pc.resize (cloud.size());
285  for (size_t i = 0; i < cloud.size(); i++)
286  {
287  pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointTDiff, PointT> (
288  cloud[i], pc[i]));
289  }
290  nearestKSearch (pc,std::vector<int>(),k,k_indices,k_sqr_distances);
291  }
292  else
293  {
294  pc.resize (indices.size());
295  for (size_t i = 0; i < indices.size(); i++)
296  {
297  pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointTDiff, PointT> (
298  cloud[indices[i]], pc[i]));
299  }
300  nearestKSearch (pc,std::vector<int>(),k,k_indices,k_sqr_distances);
301  }
302  }
303 
314  virtual int
315  radiusSearch (const PointT& point, double radius, std::vector<int>& k_indices,
316  std::vector<float>& k_sqr_distances, unsigned int max_nn = 0) const = 0;
317 
328  template <typename PointTDiff> inline int
329  radiusSearchT (const PointTDiff &point, double radius, std::vector<int> &k_indices,
330  std::vector<float> &k_sqr_distances, unsigned int max_nn = 0) const
331  {
332  PointT p;
333  // Copy all the data fields from the input cloud to the output one
334  typedef typename pcl::traits::fieldList<PointT>::type FieldListInT;
335  typedef typename pcl::traits::fieldList<PointTDiff>::type FieldListOutT;
336  typedef typename pcl::intersect<FieldListInT, FieldListOutT>::type FieldList;
337  pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointTDiff, PointT> (point, p));
338  return (radiusSearch (p, radius, k_indices, k_sqr_distances, max_nn));
339  }
340 
358  virtual int
359  radiusSearch (const PointCloud &cloud, int index, double radius,
360  std::vector<int> &k_indices, std::vector<float> &k_sqr_distances,
361  unsigned int max_nn = 0) const
362  {
363  assert (index >= 0 && index < static_cast<int> (cloud.points.size ()) && "Out-of-bounds error in radiusSearch!");
364  return (radiusSearch(cloud.points[index], radius, k_indices, k_sqr_distances, max_nn));
365  }
366 
386  virtual int
387  radiusSearch (int index, double radius, std::vector<int> &k_indices,
388  std::vector<float> &k_sqr_distances, unsigned int max_nn = 0) const
389  {
390  if (indices_ == NULL)
391  {
392  assert (index >= 0 && index < static_cast<int> (input_->points.size ()) && "Out-of-bounds error in radiusSearch!");
393  return (radiusSearch (input_->points[index], radius, k_indices, k_sqr_distances, max_nn));
394  }
395  else
396  {
397  assert (index >= 0 && index < static_cast<int> (indices_->size ()) && "Out-of-bounds error in radiusSearch!");
398  return (radiusSearch (input_->points[(*indices_)[index]], radius, k_indices, k_sqr_distances, max_nn));
399  }
400  }
401 
412  virtual void
413  radiusSearch (const PointCloud& cloud,
414  const std::vector<int>& indices,
415  double radius,
416  std::vector< std::vector<int> >& k_indices,
417  std::vector< std::vector<float> > &k_sqr_distances,
418  unsigned int max_nn = 0) const
419  {
420  if (indices.empty ())
421  {
422  k_indices.resize (cloud.size ());
423  k_sqr_distances.resize (cloud.size ());
424  for (size_t i = 0; i < cloud.size (); i++)
425  radiusSearch (cloud, static_cast<int> (i), radius,k_indices[i], k_sqr_distances[i], max_nn);
426  }
427  else
428  {
429  k_indices.resize (indices.size ());
430  k_sqr_distances.resize (indices.size ());
431  for (size_t i = 0; i < indices.size (); i++)
432  radiusSearch (cloud,indices[i],radius,k_indices[i],k_sqr_distances[i], max_nn);
433  }
434  }
435 
436 
448  template <typename PointTDiff> void
450  const std::vector<int>& indices,
451  double radius,
452  std::vector< std::vector<int> > &k_indices,
453  std::vector< std::vector<float> > &k_sqr_distances,
454  unsigned int max_nn = 0) const
455  {
456  // Copy all the data fields from the input cloud to the output one
457  typedef typename pcl::traits::fieldList<PointT>::type FieldListInT;
458  typedef typename pcl::traits::fieldList<PointTDiff>::type FieldListOutT;
459  typedef typename pcl::intersect<FieldListInT, FieldListOutT>::type FieldList;
460 
462  if (indices.empty ())
463  {
464  pc.resize (cloud.size ());
465  for (size_t i = 0; i < cloud.size (); ++i)
466  pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointTDiff, PointT> (cloud[i], pc[i]));
467  radiusSearch (pc, std::vector<int> (), radius, k_indices, k_sqr_distances, max_nn);
468  }
469  else
470  {
471  pc.resize (indices.size ());
472  for (size_t i = 0; i < indices.size (); ++i)
473  pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointTDiff, PointT> (cloud[indices[i]], pc[i]));
474  radiusSearch (pc, std::vector<int>(), radius, k_indices, k_sqr_distances, max_nn);
475  }
476  }
477 
478  protected:
479  void sortResults (std::vector<int>& indices, std::vector<float>& distances) const;
480  PointCloudConstPtr input_;
481  IndicesConstPtr indices_;
482  bool sorted_results_;
483  std::string name_;
484 
485  private:
486  struct Compare
487  {
488  Compare (const std::vector<float>& distances)
489  : distances_ (distances)
490  {
491  }
492 
493  bool operator () (int first, int second) const
494  {
495  return distances_ [first] < distances_[second];
496  }
497 
498  const std::vector<float>& distances_;
499  };
500  }; // class Search
501 
502  // implementation
503  template<typename PointT> void
504  Search<PointT>::sortResults (std::vector<int>& indices, std::vector<float>& distances) const
505  {
506  std::vector<int> order (indices.size ());
507  for (size_t idx = 0; idx < order.size (); ++idx)
508  order [idx] = static_cast<int> (idx);
509 
510  Compare compare (distances);
511  sort (order.begin (), order.end (), compare);
512 
513  std::vector<int> sorted (indices.size ());
514  for (size_t idx = 0; idx < order.size (); ++idx)
515  sorted [idx] = indices[order [idx]];
516 
517  indices = sorted;
518 
519  // sort the according distances.
520  sort (distances.begin (), distances.end ());
521  }
522  } // namespace search
523 } // namespace pcl
524 
525 #endif //#ifndef _PCL_SEARCH_GENERIC_SEARCH_H_