Point Cloud Library (PCL)  1.7.1
octree.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  *
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 the copyright holder(s) 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$
37  */
38 
39 #ifndef PCL_SEARCH_OCTREE_H
40 #define PCL_SEARCH_OCTREE_H
41 
42 #include <pcl/search/search.h>
43 #include <pcl/octree/octree_search.h>
44 
45 namespace pcl
46 {
47  namespace search
48  {
49  /** \brief @b search::Octree is a wrapper class which implements nearest neighbor search operations based on the
50  * pcl::octree::Octree structure.
51  *
52  * The octree pointcloud class needs to be initialized with its voxel
53  * resolution. Its bounding box is automatically adjusted according to the
54  * pointcloud dimension or it can be predefined. Note: The tree depth
55  * equates to the resolution and the bounding box dimensions of the
56  * octree.
57  *
58  * \note typename: PointT: type of point used in pointcloud
59  * \note typename: LeafT: leaf node class (usuallt templated with integer indices values)
60  * \note typename: OctreeT: octree implementation ()
61  *
62  * \author Julius Kammerl
63  * \ingroup search
64  */
65  template<typename PointT,
66  typename LeafTWrap = pcl::octree::OctreeContainerPointIndices,
67  typename BranchTWrap = pcl::octree::OctreeContainerEmpty,
69  class Octree: public Search<PointT>
70  {
71  public:
72  // public typedefs
73  typedef boost::shared_ptr<pcl::search::Octree<PointT,LeafTWrap,BranchTWrap,OctreeT> > Ptr;
74  typedef boost::shared_ptr<const pcl::search::Octree<PointT,LeafTWrap,BranchTWrap,OctreeT> > ConstPtr;
75 
76  typedef boost::shared_ptr<std::vector<int> > IndicesPtr;
77  typedef boost::shared_ptr<const std::vector<int> > IndicesConstPtr;
78 
80  typedef boost::shared_ptr<PointCloud> PointCloudPtr;
81  typedef boost::shared_ptr<const PointCloud> PointCloudConstPtr;
82 
83  // Boost shared pointers
84  typedef boost::shared_ptr<pcl::octree::OctreePointCloudSearch<PointT, LeafTWrap, BranchTWrap> > OctreePointCloudSearchPtr;
85  typedef boost::shared_ptr<const pcl::octree::OctreePointCloudSearch<PointT, LeafTWrap, BranchTWrap> > OctreePointCloudSearchConstPtr;
86  OctreePointCloudSearchPtr tree_;
87 
91 
92  /** \brief Octree constructor.
93  * \param[in] resolution octree resolution at lowest octree level
94  */
95  Octree (const double resolution)
96  : Search<PointT> ("Octree")
97  , tree_ (new pcl::octree::OctreePointCloudSearch<PointT, LeafTWrap, BranchTWrap> (resolution))
98  {
99  }
100 
101  /** \brief Empty Destructor. */
102  virtual
104  {
105  }
106 
107  /** \brief Provide a pointer to the input dataset.
108  * \param[in] cloud the const boost shared pointer to a PointCloud message
109  */
110  inline void
111  setInputCloud (const PointCloudConstPtr &cloud)
112  {
113  tree_->deleteTree ();
114  tree_->setInputCloud (cloud);
115  tree_->addPointsFromInputCloud ();
116  input_ = cloud;
117  }
118 
119  /** \brief Provide a pointer to the input dataset.
120  * \param[in] cloud the const boost shared pointer to a PointCloud message
121  * \param[in] indices the point indices subset that is to be used from \a cloud
122  */
123  inline void
124  setInputCloud (const PointCloudConstPtr &cloud, const IndicesConstPtr& indices)
125  {
126  tree_->deleteTree ();
127  tree_->setInputCloud (cloud, indices);
128  tree_->addPointsFromInputCloud ();
129  input_ = cloud;
130  indices_ = indices;
131  }
132 
133  /** \brief Search for the k-nearest neighbors for the given query point.
134  * \param[in] cloud the point cloud data
135  * \param[in] index the index in \a cloud representing the query point
136  * \param[in] k the number of neighbors to search for
137  * \param[out] k_indices the resultant indices of the neighboring points (must be resized to \a k a priori!)
138  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points (must be resized to \a k
139  * a priori!)
140  * \return number of neighbors found
141  */
142  inline int
143  nearestKSearch (const PointCloud &cloud, int index, int k, std::vector<int> &k_indices,
144  std::vector<float> &k_sqr_distances) const
145  {
146  return (tree_->nearestKSearch (cloud, index, k, k_indices, k_sqr_distances));
147  }
148 
149  /** \brief Search for the k-nearest neighbors for the given query point.
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  inline int
158  nearestKSearch (const PointT &point, int k, std::vector<int> &k_indices,
159  std::vector<float> &k_sqr_distances) const
160  {
161  return (tree_->nearestKSearch (point, k, k_indices, k_sqr_distances));
162  }
163 
164  /** \brief Search for the k-nearest neighbors for the given query point (zero-copy).
165  *
166  * \param[in] index the index representing the query point in the
167  * dataset given by \a setInputCloud if indices were given in
168  * setInputCloud, index will be the position in the indices vector
169  * \param[in] k the number of neighbors to search for
170  * \param[out] k_indices the resultant indices of the neighboring points (must be resized to \a k a priori!)
171  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points (must be resized to \a k
172  * a priori!)
173  * \return number of neighbors found
174  */
175  inline int
176  nearestKSearch (int index, int k, std::vector<int> &k_indices, std::vector<float> &k_sqr_distances) const
177  {
178  return (tree_->nearestKSearch (index, k, k_indices, k_sqr_distances));
179  }
180 
181  /** \brief search for all neighbors of query point that are within a given radius.
182  * \param cloud the point cloud data
183  * \param index the index in \a cloud representing the query point
184  * \param radius the radius of the sphere bounding all of p_q's neighbors
185  * \param k_indices the resultant indices of the neighboring points
186  * \param k_sqr_distances the resultant squared distances to the neighboring points
187  * \param max_nn if given, bounds the maximum returned neighbors to this value
188  * \return number of neighbors found in radius
189  */
190  inline int
191  radiusSearch (const PointCloud &cloud,
192  int index,
193  double radius,
194  std::vector<int> &k_indices,
195  std::vector<float> &k_sqr_distances,
196  unsigned int max_nn = 0) const
197  {
198  tree_->radiusSearch (cloud, index, radius, k_indices, k_sqr_distances, max_nn);
199  if (sorted_results_)
200  this->sortResults (k_indices, k_sqr_distances);
201  return (static_cast<int> (k_indices.size ()));
202  }
203 
204  /** \brief search for all neighbors of query point that are within a given radius.
205  * \param p_q the given query point
206  * \param radius the radius of the sphere bounding all of p_q's neighbors
207  * \param k_indices the resultant indices of the neighboring points
208  * \param k_sqr_distances the resultant squared distances to the neighboring points
209  * \param max_nn if given, bounds the maximum returned neighbors to this value
210  * \return number of neighbors found in radius
211  */
212  inline int
213  radiusSearch (const PointT &p_q,
214  double radius,
215  std::vector<int> &k_indices,
216  std::vector<float> &k_sqr_distances,
217  unsigned int max_nn = 0) const
218  {
219  tree_->radiusSearch (p_q, radius, k_indices, k_sqr_distances, max_nn);
220  if (sorted_results_)
221  this->sortResults (k_indices, k_sqr_distances);
222  return (static_cast<int> (k_indices.size ()));
223  }
224 
225  /** \brief search for all neighbors of query point that are within a given radius.
226  * \param index index representing the query point in the dataset given by \a setInputCloud.
227  * If indices were given in setInputCloud, index will be the position in the indices vector
228  * \param radius radius of the sphere bounding all of p_q's neighbors
229  * \param k_indices the resultant indices of the neighboring points
230  * \param k_sqr_distances the resultant squared distances to the neighboring points
231  * \param max_nn if given, bounds the maximum returned neighbors to this value
232  * \return number of neighbors found in radius
233  */
234  inline int
235  radiusSearch (int index, double radius, std::vector<int> &k_indices,
236  std::vector<float> &k_sqr_distances, unsigned int max_nn = 0) const
237  {
238  tree_->radiusSearch (index, radius, k_indices, k_sqr_distances, max_nn);
239  if (sorted_results_)
240  this->sortResults (k_indices, k_sqr_distances);
241  return (static_cast<int> (k_indices.size ()));
242  }
243 
244 
245  /** \brief Search for approximate nearest neighbor at the query point.
246  * \param[in] cloud the point cloud data
247  * \param[in] query_index the index in \a cloud representing the query point
248  * \param[out] result_index the resultant index of the neighbor point
249  * \param[out] sqr_distance the resultant squared distance to the neighboring point
250  * \return number of neighbors found
251  */
252  inline void
253  approxNearestSearch (const PointCloudConstPtr &cloud, int query_index, int &result_index,
254  float &sqr_distance)
255  {
256  return (tree_->approxNearestSearch (cloud->points[query_index], result_index, sqr_distance));
257  }
258 
259  /** \brief Search for approximate nearest neighbor at the query point.
260  * \param[in] p_q the given query point
261  * \param[out] result_index the resultant index of the neighbor point
262  * \param[out] sqr_distance the resultant squared distance to the neighboring point
263  */
264  inline void
265  approxNearestSearch (const PointT &p_q, int &result_index, float &sqr_distance)
266  {
267  return (tree_->approxNearestSearch (p_q, result_index, sqr_distance));
268  }
269 
270  /** \brief Search for approximate nearest neighbor at the query point.
271  * \param query_index index representing the query point in the dataset given by \a setInputCloud.
272  * If indices were given in setInputCloud, index will be the position in the indices vector.
273  * \param result_index the resultant index of the neighbor point
274  * \param sqr_distance the resultant squared distance to the neighboring point
275  * \return number of neighbors found
276  */
277  inline void
278  approxNearestSearch (int query_index, int &result_index, float &sqr_distance)
279  {
280  return (tree_->approxNearestSearch (query_index, result_index, sqr_distance));
281  }
282 
283  };
284  }
285 }
286 
287 #ifdef PCL_NO_PRECOMPILE
288 #include <pcl/octree/impl/octree_search.hpp>
289 #else
290 #define PCL_INSTANTIATE_Octree(T) template class PCL_EXPORTS pcl::search::Octree<T>;
291 #endif
292 
293 #endif // PCL_SEARCH_OCTREE_H
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
search for all neighbors of query point that are within a given radius.
Definition: octree.h:213
boost::shared_ptr< PointCloud > PointCloudPtr
Definition: octree.h:80
boost::shared_ptr< const pcl::search::Octree< PointT, LeafTWrap, BranchTWrap, OctreeT > > ConstPtr
Definition: octree.h:74
pcl::PointCloud< PointT > PointCloud
Definition: octree.h:79
bool sorted_results_
Definition: search.h:412
Generic search class.
Definition: search.h:73
Octree container class that does not store any information.
IndicesConstPtr indices_
Definition: search.h:411
boost::shared_ptr< pcl::octree::OctreePointCloudSearch< PointT, LeafTWrap, BranchTWrap > > OctreePointCloudSearchPtr
Definition: octree.h:84
void approxNearestSearch(const PointCloudConstPtr &cloud, int query_index, int &result_index, float &sqr_distance)
Search for approximate nearest neighbor at the query point.
Definition: octree.h:253
Octree class.
Definition: octree_base.h:63
boost::shared_ptr< std::vector< int > > IndicesPtr
Definition: octree.h:76
Octree(const double resolution)
Octree constructor.
Definition: octree.h:95
boost::shared_ptr< const pcl::octree::OctreePointCloudSearch< PointT, LeafTWrap, BranchTWrap > > OctreePointCloudSearchConstPtr
Definition: octree.h:85
void sortResults(std::vector< int > &indices, std::vector< float > &distances) const
Definition: search.hpp:195
boost::shared_ptr< pcl::search::Octree< PointT, LeafTWrap, BranchTWrap, OctreeT > > Ptr
Definition: octree.h:73
OctreePointCloudSearchPtr tree_
Definition: octree.h:86
PointCloudConstPtr input_
Definition: search.h:410
Octree container class that does store a vector of point indices.
boost::shared_ptr< const PointCloud > PointCloudConstPtr
Definition: octree.h:81
int nearestKSearch(int index, int k, std::vector< int > &k_indices, std::vector< float > &k_sqr_distances) const
Search for the k-nearest neighbors for the given query point (zero-copy).
Definition: octree.h:176
void approxNearestSearch(const PointT &p_q, int &result_index, float &sqr_distance)
Search for approximate nearest neighbor at the query point.
Definition: octree.h:265
virtual ~Octree()
Empty Destructor.
Definition: octree.h:103
void setInputCloud(const PointCloudConstPtr &cloud)
Provide a pointer to the input dataset.
Definition: octree.h:111
search::Octree is a wrapper class which implements nearest neighbor search operations based on the pc...
Definition: octree.h:69
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 neighbors of query point that are within a given radius.
Definition: octree.h:235
boost::shared_ptr< const std::vector< int > > IndicesConstPtr
Definition: octree.h:77
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 neighbors of query point that are within a given radius.
Definition: octree.h:191
A point structure representing Euclidean xyz coordinates, and the RGB color.
void setInputCloud(const PointCloudConstPtr &cloud, const IndicesConstPtr &indices)
Provide a pointer to the input dataset.
Definition: octree.h:124
PointCloud represents the base class in PCL for storing collections of 3D points. ...
int nearestKSearch(const PointT &point, int k, std::vector< int > &k_indices, std::vector< float > &k_sqr_distances) const
Search for the k-nearest neighbors for the given query point.
Definition: octree.h:158
int nearestKSearch(const PointCloud &cloud, int index, int k, std::vector< int > &k_indices, std::vector< float > &k_sqr_distances) const
Search for the k-nearest neighbors for the given query point.
Definition: octree.h:143
void approxNearestSearch(int query_index, int &result_index, float &sqr_distance)
Search for approximate nearest neighbor at the query point.
Definition: octree.h:278