Point Cloud Library (PCL)  1.11.1-dev
kdtree.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  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id$
38  */
39 
40 #pragma once
41 
42 #include <pcl/search/search.h>
43 #include <pcl/kdtree/kdtree_flann.h>
44 
45 namespace pcl
46 {
47  // Forward declarations
48  template <typename T> class PointRepresentation;
49 
50  namespace search
51  {
52  /** \brief @b search::KdTree is a wrapper class which inherits the pcl::KdTree class for performing search
53  * functions using KdTree structure. KdTree is a generic type of 3D spatial locator using kD-tree structures.
54  * The class is making use of the FLANN (Fast Library for Approximate Nearest Neighbor) project
55  * by Marius Muja and David Lowe.
56  *
57  * \author Radu B. Rusu
58  * \ingroup search
59  */
60  template<typename PointT, class Tree = pcl::KdTreeFLANN<PointT> >
61  class KdTree: public Search<PointT>
62  {
63  public:
66 
74 
75  using Ptr = shared_ptr<KdTree<PointT, Tree> >;
76  using ConstPtr = shared_ptr<const KdTree<PointT, Tree> >;
77 
78  using KdTreePtr = typename Tree::Ptr;
79  using KdTreeConstPtr = typename Tree::ConstPtr;
81 
82  /** \brief Constructor for KdTree.
83  *
84  * \param[in] sorted set to true if the nearest neighbor search results
85  * need to be sorted in ascending order based on their distance to the
86  * query point
87  *
88  */
89  KdTree (bool sorted = true);
90 
91  /** \brief Destructor for KdTree. */
92 
94  {
95  }
96 
97  /** \brief Provide a pointer to the point representation to use to convert points into k-D vectors.
98  * \param[in] point_representation the const boost shared pointer to a PointRepresentation
99  */
100  void
101  setPointRepresentation (const PointRepresentationConstPtr &point_representation);
102 
103  /** \brief Get a pointer to the point representation used when converting points into k-D vectors. */
106  {
107  return (tree_->getPointRepresentation ());
108  }
109 
110  /** \brief Sets whether the results have to be sorted or not.
111  * \param[in] sorted_results set to true if the radius search results should be sorted
112  */
113  void
114  setSortedResults (bool sorted_results) override;
115 
116  /** \brief Set the search epsilon precision (error bound) for nearest neighbors searches.
117  * \param[in] eps precision (error bound) for nearest neighbors searches
118  */
119  void
120  setEpsilon (float eps);
121 
122  /** \brief Get the search epsilon precision (error bound) for nearest neighbors searches. */
123  inline float
124  getEpsilon () const
125  {
126  return (tree_->getEpsilon ());
127  }
128 
129  /** \brief Provide a pointer to the input dataset.
130  * \param[in] cloud the const boost shared pointer to a PointCloud message
131  * \param[in] indices the point indices subset that is to be used from \a cloud
132  */
133  void
134  setInputCloud (const PointCloudConstPtr& cloud,
135  const IndicesConstPtr& indices = IndicesConstPtr ()) override;
136 
137  /** \brief Search for the k-nearest neighbors for the given query point.
138  * \param[in] point the given query point
139  * \param[in] k the number of neighbors to search for
140  * \param[out] k_indices the resultant indices of the neighboring points (must be resized to \a k a priori!)
141  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points (must be resized to \a k
142  * a priori!)
143  * \return number of neighbors found
144  */
145  int
146  nearestKSearch (const PointT &point, int k,
147  Indices &k_indices,
148  std::vector<float> &k_sqr_distances) const override;
149 
150  /** \brief Search for all the nearest neighbors of the query point in a given radius.
151  * \param[in] point the given query point
152  * \param[in] radius the radius of the sphere bounding all of p_q's neighbors
153  * \param[out] k_indices the resultant indices of the neighboring points
154  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points
155  * \param[in] max_nn if given, bounds the maximum returned neighbors to this value. If \a max_nn is set to
156  * 0 or to a number higher than the number of points in the input cloud, all neighbors in \a radius will be
157  * returned.
158  * \return number of neighbors found in radius
159  */
160  int
161  radiusSearch (const PointT& point, double radius,
162  Indices &k_indices,
163  std::vector<float> &k_sqr_distances,
164  unsigned int max_nn = 0) const override;
165  protected:
166  /** \brief A pointer to the internal KdTree object. */
168  };
169  }
170 }
171 
172 #ifdef PCL_NO_PRECOMPILE
173 #include <pcl/search/impl/kdtree.hpp>
174 #else
175 #define PCL_INSTANTIATE_KdTree(T) template class PCL_EXPORTS pcl::search::KdTree<T>;
176 #endif
pcl::search::Search
Generic search class.
Definition: search.h:74
pcl
Definition: convolution.h:46
pcl::search::KdTree::nearestKSearch
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: kdtree.hpp:87
pcl::search::KdTree::setInputCloud
void setInputCloud(const PointCloudConstPtr &cloud, const IndicesConstPtr &indices=IndicesConstPtr()) override
Provide a pointer to the input dataset.
Definition: kdtree.hpp:76
pcl::IndicesConstPtr
shared_ptr< const Indices > IndicesConstPtr
Definition: pcl_base.h:59
pcl::PointRepresentation::ConstPtr
shared_ptr< const PointRepresentation< PointT > > ConstPtr
Definition: point_representation.h:79
pcl::search::KdTree::radiusSearch
int radiusSearch(const PointT &point, double radius, Indices &k_indices, std::vector< float > &k_sqr_distances, unsigned int max_nn=0) const override
Search for all the nearest neighbors of the query point in a given radius.
Definition: kdtree.hpp:96
pcl::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: distances.h:55
pcl::search::KdTree< SceneT >::KdTreePtr
typename pcl::KdTreeFLANN< SceneT > ::Ptr KdTreePtr
Definition: kdtree.h:78
pcl::search::KdTree::setPointRepresentation
void setPointRepresentation(const PointRepresentationConstPtr &point_representation)
Provide a pointer to the point representation to use to convert points into k-D vectors.
Definition: kdtree.hpp:53
pcl::PointXYZRGB
A point structure representing Euclidean xyz coordinates, and the RGB color.
Definition: point_types.hpp:628
pcl::search::KdTree::setSortedResults
void setSortedResults(bool sorted_results) override
Sets whether the results have to be sorted or not.
Definition: kdtree.hpp:61
pcl::search::KdTree::getPointRepresentation
PointRepresentationConstPtr getPointRepresentation() const
Get a pointer to the point representation used when converting points into k-D vectors.
Definition: kdtree.h:105
pcl::search::KdTree< SceneT >::Ptr
shared_ptr< KdTree< SceneT, pcl::KdTreeFLANN< SceneT > > > Ptr
Definition: kdtree.h:75
pcl::search::KdTree::getEpsilon
float getEpsilon() const
Get the search epsilon precision (error bound) for nearest neighbors searches.
Definition: kdtree.h:124
pcl::search::Search::PointCloudConstPtr
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: search.h:79
pcl::search::KdTree< SceneT >::KdTreeConstPtr
typename pcl::KdTreeFLANN< SceneT > ::ConstPtr KdTreeConstPtr
Definition: kdtree.h:79
pcl::search::KdTree< SceneT >::PointRepresentationConstPtr
typename PointRepresentation< SceneT >::ConstPtr PointRepresentationConstPtr
Definition: kdtree.h:80
pcl::search::KdTree
search::KdTree is a wrapper class which inherits the pcl::KdTree class for performing search function...
Definition: kdtree.h:61
pcl::search::KdTree::KdTree
KdTree(bool sorted=true)
Constructor for KdTree.
Definition: kdtree.hpp:45
pcl::search::KdTree::tree_
KdTreePtr tree_
A pointer to the internal KdTree object.
Definition: kdtree.h:167
pcl::search::KdTree::~KdTree
~KdTree()
Destructor for KdTree.
Definition: kdtree.h:93
pcl::Indices
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:131
pcl::search::KdTree< SceneT >::ConstPtr
shared_ptr< const KdTree< SceneT, pcl::KdTreeFLANN< SceneT > > > ConstPtr
Definition: kdtree.h:76
pcl::search::KdTree< SceneT >::PointCloud
typename Search< SceneT >::PointCloud PointCloud
Definition: kdtree.h:64
pcl::KdTree< SceneT >::PointCloudConstPtr
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: kdtree.h:62
pcl::search::KdTree::setEpsilon
void setEpsilon(float eps)
Set the search epsilon precision (error bound) for nearest neighbors searches.
Definition: kdtree.hpp:69