Point Cloud Library (PCL)  1.14.1-dev
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 
99  ~Octree () override = default;
100 
101  /** \brief Provide a pointer to the input dataset.
102  * \param[in] cloud the const boost shared pointer to a PointCloud message
103  */
104  inline void
106  {
107  tree_->deleteTree ();
108  tree_->setInputCloud (cloud);
109  tree_->addPointsFromInputCloud ();
110  input_ = cloud;
111  }
112 
113  /** \brief Provide a pointer to the input dataset.
114  * \param[in] cloud the const boost shared pointer to a PointCloud message
115  * \param[in] indices the point indices subset that is to be used from \a cloud
116  */
117  inline bool
118  setInputCloud (const PointCloudConstPtr &cloud, const IndicesConstPtr& indices) override
119  {
120  tree_->deleteTree ();
121  tree_->setInputCloud (cloud, indices);
122  tree_->addPointsFromInputCloud ();
123  input_ = cloud;
124  indices_ = indices;
125  return true;
126  }
127 
128  /** \brief Search for the k-nearest neighbors for the given query point.
129  * \param[in] cloud the point cloud data
130  * \param[in] index the index in \a cloud representing the query point
131  * \param[in] k the number of neighbors to search for
132  * \param[out] k_indices the resultant indices of the neighboring points (must be resized to \a k a priori!)
133  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points (must be resized to \a k
134  * a priori!)
135  * \return number of neighbors found
136  */
137  inline int
138  nearestKSearch (const PointCloud &cloud, index_t index, int k, Indices &k_indices,
139  std::vector<float> &k_sqr_distances) const override
140  {
141  return (tree_->nearestKSearch (cloud, index, k, k_indices, k_sqr_distances));
142  }
143 
144  /** \brief Search for the k-nearest neighbors for the given query point.
145  * \param[in] point the given query point
146  * \param[in] k the number of neighbors to search for
147  * \param[out] k_indices the resultant indices of the neighboring points (must be resized to \a k a priori!)
148  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points (must be resized to \a k
149  * a priori!)
150  * \return number of neighbors found
151  */
152  inline int
153  nearestKSearch (const PointT &point, int k, Indices &k_indices,
154  std::vector<float> &k_sqr_distances) const override
155  {
156  return (tree_->nearestKSearch (point, k, k_indices, k_sqr_distances));
157  }
158 
159  /** \brief Search for the k-nearest neighbors for the given query point (zero-copy).
160  *
161  * \param[in] index the index representing the query point in the
162  * dataset given by \a setInputCloud if indices were given in
163  * setInputCloud, index will be the position in the indices vector
164  * \param[in] k the number of neighbors to search for
165  * \param[out] k_indices the resultant indices of the neighboring points (must be resized to \a k a priori!)
166  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points (must be resized to \a k
167  * a priori!)
168  * \return number of neighbors found
169  */
170  inline int
171  nearestKSearch (index_t index, int k, Indices &k_indices, std::vector<float> &k_sqr_distances) const override
172  {
173  return (tree_->nearestKSearch (index, k, k_indices, k_sqr_distances));
174  }
175 
176  /** \brief search for all neighbors of query point that are within a given radius.
177  * \param cloud the point cloud data
178  * \param index the index in \a cloud representing the query point
179  * \param radius the radius of the sphere bounding all of p_q's neighbors
180  * \param k_indices the resultant indices of the neighboring points
181  * \param k_sqr_distances the resultant squared distances to the neighboring points
182  * \param max_nn if given, bounds the maximum returned neighbors to this value
183  * \return number of neighbors found in radius
184  */
185  inline int
186  radiusSearch (const PointCloud &cloud,
187  index_t index,
188  double radius,
189  Indices &k_indices,
190  std::vector<float> &k_sqr_distances,
191  unsigned int max_nn = 0) const override
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 
199  /** \brief search for all neighbors of query point that are within a given radius.
200  * \param p_q the given query point
201  * \param radius the radius of the sphere bounding all of p_q's neighbors
202  * \param k_indices the resultant indices of the neighboring points
203  * \param k_sqr_distances the resultant squared distances to the neighboring points
204  * \param max_nn if given, bounds the maximum returned neighbors to this value
205  * \return number of neighbors found in radius
206  */
207  inline int
208  radiusSearch (const PointT &p_q,
209  double radius,
210  Indices &k_indices,
211  std::vector<float> &k_sqr_distances,
212  unsigned int max_nn = 0) const override
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 
220  /** \brief search for all neighbors of query point that are within a given radius.
221  * \param index index representing the query point in the dataset given by \a setInputCloud.
222  * If indices were given in setInputCloud, index will be the position in the indices vector
223  * \param radius radius of the sphere bounding all of p_q's neighbors
224  * \param k_indices the resultant indices of the neighboring points
225  * \param k_sqr_distances the resultant squared distances to the neighboring points
226  * \param max_nn if given, bounds the maximum returned neighbors to this value
227  * \return number of neighbors found in radius
228  */
229  inline int
230  radiusSearch (index_t index, double radius, Indices &k_indices,
231  std::vector<float> &k_sqr_distances, unsigned int max_nn = 0) const override
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 
240  /** \brief Search for approximate nearest neighbor at the query point.
241  * \param[in] cloud the point cloud data
242  * \param[in] query_index the index in \a cloud representing the query point
243  * \param[out] result_index the resultant index of the neighbor point
244  * \param[out] sqr_distance the resultant squared distance to the neighboring point
245  * \return number of neighbors found
246  */
247  inline void
248  approxNearestSearch (const PointCloudConstPtr &cloud, index_t query_index, index_t &result_index,
249  float &sqr_distance)
250  {
251  return (tree_->approxNearestSearch ((*cloud)[query_index], result_index, sqr_distance));
252  }
253 
254  /** \brief Search for approximate nearest neighbor at the query point.
255  * \param[in] p_q the given query point
256  * \param[out] result_index the resultant index of the neighbor point
257  * \param[out] sqr_distance the resultant squared distance to the neighboring point
258  */
259  inline void
260  approxNearestSearch (const PointT &p_q, index_t &result_index, float &sqr_distance)
261  {
262  return (tree_->approxNearestSearch (p_q, result_index, sqr_distance));
263  }
264 
265  /** \brief Search for approximate nearest neighbor at the query point.
266  * \param query_index index representing the query point in the dataset given by \a setInputCloud.
267  * If indices were given in setInputCloud, index will be the position in the indices vector.
268  * \param result_index the resultant index of the neighbor point
269  * \param sqr_distance the resultant squared distance to the neighboring point
270  * \return number of neighbors found
271  */
272  inline void
273  approxNearestSearch (index_t query_index, index_t &result_index, float &sqr_distance)
274  {
275  return (tree_->approxNearestSearch (query_index, result_index, sqr_distance));
276  }
277  /** \brief Search for points within rectangular search area
278  * \param[in] min_pt lower corner of search area
279  * \param[in] max_pt upper corner of search area
280  * \param[out] k_indices the resultant point indices
281  * \return number of points found within search area
282  */
283  inline uindex_t
284  boxSearch(const Eigen::Vector3f &min_pt, const Eigen::Vector3f &max_pt, Indices &k_indices) const
285  {
286  return (tree_->boxSearch(min_pt, max_pt, k_indices));
287  }
288  };
289  }
290 }
291 
292 #ifdef PCL_NO_PRECOMPILE
293 #include <pcl/octree/impl/octree_search.hpp>
294 #else
295 #define PCL_INSTANTIATE_Octree(T) template class PCL_EXPORTS pcl::search::Octree<T>;
296 #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
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:284
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:171
void setInputCloud(const PointCloudConstPtr &cloud)
Provide a pointer to the input dataset.
Definition: octree.h:105
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:208
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:248
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:230
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:273
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:186
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:260
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:153
bool setInputCloud(const PointCloudConstPtr &cloud, const IndicesConstPtr &indices) override
Provide a pointer to the input dataset.
Definition: octree.h:118
~Octree() override=default
Empty Destructor.
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:138
Generic search class.
Definition: search.h:75
PointCloudConstPtr input_
Definition: search.h:402
void sortResults(Indices &indices, std::vector< float > &distances) const
Definition: search.hpp:189
IndicesConstPtr indices_
Definition: search.h:403
pcl::IndicesConstPtr IndicesConstPtr
Definition: search.h:85
bool sorted_results_
Definition: search.h:404
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.