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