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