Point Cloud Library (PCL)  1.12.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 
93  ~KdTree () override = default;
94 
95  /** \brief Provide a pointer to the point representation to use to convert points into k-D vectors.
96  * \param[in] point_representation the const boost shared pointer to a PointRepresentation
97  */
98  void
99  setPointRepresentation (const PointRepresentationConstPtr &point_representation);
100 
101  /** \brief Get a pointer to the point representation used when converting points into k-D vectors. */
104  {
105  return (tree_->getPointRepresentation ());
106  }
107 
108  /** \brief Sets whether the results have to be sorted or not.
109  * \param[in] sorted_results set to true if the radius search results should be sorted
110  */
111  void
112  setSortedResults (bool sorted_results) override;
113 
114  /** \brief Set the search epsilon precision (error bound) for nearest neighbors searches.
115  * \param[in] eps precision (error bound) for nearest neighbors searches
116  */
117  void
118  setEpsilon (float eps);
119 
120  /** \brief Get the search epsilon precision (error bound) for nearest neighbors searches. */
121  inline float
122  getEpsilon () const
123  {
124  return (tree_->getEpsilon ());
125  }
126 
127  /** \brief Provide a pointer to the input dataset.
128  * \param[in] cloud the const boost shared pointer to a PointCloud message
129  * \param[in] indices the point indices subset that is to be used from \a cloud
130  */
131  void
132  setInputCloud (const PointCloudConstPtr& cloud,
133  const IndicesConstPtr& indices = IndicesConstPtr ()) override;
134 
135  /** \brief Search for the k-nearest neighbors for the given query point.
136  * \param[in] point the given query point
137  * \param[in] k the number of neighbors to search for
138  * \param[out] k_indices the resultant indices of the neighboring points (must be resized to \a k a priori!)
139  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points (must be resized to \a k
140  * a priori!)
141  * \return number of neighbors found
142  */
143  int
144  nearestKSearch (const PointT &point, int k,
145  Indices &k_indices,
146  std::vector<float> &k_sqr_distances) const override;
147 
148  /** \brief Search for all the nearest neighbors of the query point in a given radius.
149  * \param[in] point the given query point
150  * \param[in] radius the radius of the sphere bounding all of p_q's neighbors
151  * \param[out] k_indices the resultant indices of the neighboring points
152  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points
153  * \param[in] max_nn if given, bounds the maximum returned neighbors to this value. If \a max_nn is set to
154  * 0 or to a number higher than the number of points in the input cloud, all neighbors in \a radius will be
155  * returned.
156  * \return number of neighbors found in radius
157  */
158  int
159  radiusSearch (const PointT& point, double radius,
160  Indices &k_indices,
161  std::vector<float> &k_sqr_distances,
162  unsigned int max_nn = 0) const override;
163  protected:
164  /** \brief A pointer to the internal KdTree object. */
166  };
167  }
168 }
169 
170 #ifdef PCL_NO_PRECOMPILE
171 #include <pcl/search/impl/kdtree.hpp>
172 #else
173 #define PCL_INSTANTIATE_KdTree(T) template class PCL_EXPORTS pcl::search::KdTree<T>;
174 #endif
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
shared_ptr< const PointRepresentation< PointT > > ConstPtr
search::KdTree is a wrapper class which inherits the pcl::KdTree class for performing search function...
Definition: kdtree.h:62
typename Tree::ConstPtr KdTreeConstPtr
Definition: kdtree.h:79
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
void setEpsilon(float eps)
Set the search epsilon precision (error bound) for nearest neighbors searches.
Definition: kdtree.hpp:69
PointRepresentationConstPtr getPointRepresentation() const
Get a pointer to the point representation used when converting points into k-D vectors.
Definition: kdtree.h:103
shared_ptr< KdTree< PointT, Tree > > Ptr
Definition: kdtree.h:75
float getEpsilon() const
Get the search epsilon precision (error bound) for nearest neighbors searches.
Definition: kdtree.h:122
shared_ptr< const KdTree< PointT, Tree > > ConstPtr
Definition: kdtree.h:76
void setInputCloud(const PointCloudConstPtr &cloud, const IndicesConstPtr &indices=IndicesConstPtr()) override
Provide a pointer to the input dataset.
Definition: kdtree.hpp:76
void setSortedResults(bool sorted_results) override
Sets whether the results have to be sorted or not.
Definition: kdtree.hpp:61
~KdTree() override=default
Destructor for KdTree.
typename Search< PointT >::PointCloudConstPtr PointCloudConstPtr
Definition: kdtree.h:65
typename PointRepresentation< PointT >::ConstPtr PointRepresentationConstPtr
Definition: kdtree.h:80
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
KdTree(bool sorted=true)
Constructor for KdTree.
Definition: kdtree.hpp:45
typename Tree::Ptr KdTreePtr
Definition: kdtree.h:78
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
KdTreePtr tree_
A pointer to the internal KdTree object.
Definition: kdtree.h:165
typename Search< PointT >::PointCloud PointCloud
Definition: kdtree.h:64
Generic search class.
Definition: search.h:75
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: search.h:79
shared_ptr< const Indices > IndicesConstPtr
Definition: pcl_base.h:59
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133
A point structure representing Euclidean xyz coordinates, and the RGB color.