Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
octree.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: octree.h 6031 2012-06-26 10:54:13Z jkammerl $
37  */
38 
39 #ifndef PCL_SEARCH_OCTREE_H
40 #define PCL_SEARCH_OCTREE_H
41 
42 #include <pcl/search/search.h>
44 
45 namespace pcl
46 {
47  namespace search
48  {
65  template<typename PointT, typename LeafTWrap = pcl::octree::OctreeContainerDataTVector<int>, typename BranchTWrap = pcl::octree::OctreeContainerEmpty<int>,
66  typename OctreeT = pcl::octree::OctreeBase<int, LeafTWrap, BranchTWrap > >
67  class Octree: public Search<PointT>
68  {
69  public:
70  // public typedefs
71  typedef boost::shared_ptr<std::vector<int> > IndicesPtr;
72  typedef boost::shared_ptr<const std::vector<int> > IndicesConstPtr;
73 
75  typedef boost::shared_ptr<PointCloud> PointCloudPtr;
76  typedef boost::shared_ptr<const PointCloud> PointCloudConstPtr;
77 
78  // Boost shared pointers
79  typedef boost::shared_ptr<pcl::octree::OctreePointCloudSearch<PointT, LeafTWrap, BranchTWrap> > Ptr;
80  typedef boost::shared_ptr<const pcl::octree::OctreePointCloudSearch<PointT, LeafTWrap, BranchTWrap> > ConstPtr;
82 
86 
90  Octree (const double resolution)
91  : Search<PointT> ("Octree")
92  , tree_ (new pcl::octree::OctreePointCloudSearch<PointT, LeafTWrap, BranchTWrap> (resolution))
93  {
94  }
95 
97  virtual
99  {
100  }
101 
105  inline void
107  {
108  tree_->deleteTree ();
109  tree_->setInputCloud (cloud);
110  tree_->addPointsFromInputCloud ();
111  input_ = cloud;
112  }
113 
118  inline void
119  setInputCloud (const PointCloudConstPtr &cloud, const IndicesConstPtr& indices)
120  {
121  tree_->deleteTree ();
122  tree_->setInputCloud (cloud, indices);
123  tree_->addPointsFromInputCloud ();
124  input_ = cloud;
125  indices_ = indices;
126  }
127 
137  inline int
138  nearestKSearch (const PointCloud &cloud, int index, int k, std::vector<int> &k_indices,
139  std::vector<float> &k_sqr_distances) const
140  {
141  return (tree_->nearestKSearch (cloud, index, k, k_indices, k_sqr_distances));
142  }
143 
152  inline int
153  nearestKSearch (const PointT &point, int k, std::vector<int> &k_indices,
154  std::vector<float> &k_sqr_distances) const
155  {
156  return (tree_->nearestKSearch (point, k, k_indices, k_sqr_distances));
157  }
158 
170  inline int
171  nearestKSearch (int index, int k, std::vector<int> &k_indices, std::vector<float> &k_sqr_distances) const
172  {
173  return (tree_->nearestKSearch (index, k, k_indices, k_sqr_distances));
174  }
175 
185  inline int
186  radiusSearch (const PointCloud &cloud,
187  int index,
188  double radius,
189  std::vector<int> &k_indices,
190  std::vector<float> &k_sqr_distances,
191  unsigned int max_nn = 0) const
192  {
193  tree_->radiusSearch (cloud, index, radius, k_indices, k_sqr_distances, max_nn);
194  if (sorted_results_)
195  this->sortResults (k_indices, k_sqr_distances);
196  return (static_cast<int> (k_indices.size ()));
197  }
198 
207  inline int
208  radiusSearch (const PointT &p_q,
209  double radius,
210  std::vector<int> &k_indices,
211  std::vector<float> &k_sqr_distances,
212  unsigned int max_nn = 0) const
213  {
214  tree_->radiusSearch (p_q, radius, k_indices, k_sqr_distances, max_nn);
215  if (sorted_results_)
216  this->sortResults (k_indices, k_sqr_distances);
217  return (static_cast<int> (k_indices.size ()));
218  }
219 
229  inline int
230  radiusSearch (int index, double radius, std::vector<int> &k_indices,
231  std::vector<float> &k_sqr_distances, unsigned int max_nn = 0) const
232  {
233  tree_->radiusSearch (index, radius, k_indices, k_sqr_distances, max_nn);
234  if (sorted_results_)
235  this->sortResults (k_indices, k_sqr_distances);
236  return (static_cast<int> (k_indices.size ()));
237  }
238 
239 
247  inline void
248  approxNearestSearch (const PointCloudConstPtr &cloud, int query_index, int &result_index,
249  float &sqr_distance)
250  {
251  return (tree_->approxNearestSearch (cloud->points[query_index], result_index, sqr_distance));
252  }
253 
259  inline void
260  approxNearestSearch (const PointT &p_q, int &result_index, float &sqr_distance)
261  {
262  return (tree_->approxNearestSearch (p_q, result_index, sqr_distance));
263  }
264 
272  inline void
273  approxNearestSearch (int query_index, int &result_index, float &sqr_distance)
274  {
275  return (tree_->approxNearestSearch (query_index, result_index, sqr_distance));
276  }
277 
278  };
279  }
280 }
281 
282 #define PCL_INSTANTIATE_Octree(T) template class PCL_EXPORTS pcl::search::Octree<T>;
283 
284 #endif // PCL_SEARCH_OCTREE_H