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