Point Cloud Library (PCL)  1.7.0
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 #ifndef PCL_SEARCH_KDTREE_H_
41 #define PCL_SEARCH_KDTREE_H_
42 
43 #include <pcl/search/search.h>
44 #include <pcl/kdtree/kdtree_flann.h>
45 
46 namespace pcl
47 {
48  // Forward declarations
49  template <typename T> class PointRepresentation;
50 
51  namespace search
52  {
53  /** \brief @b search::KdTree is a wrapper class which inherits the pcl::KdTree class for performing search
54  * functions using KdTree structure. KdTree is a generic type of 3D spatial locator using kD-tree structures.
55  * The class is making use of the FLANN (Fast Library for Approximate Nearest Neighbor) project
56  * by Marius Muja and David Lowe.
57  *
58  * \author Radu B. Rusu
59  * \ingroup search
60  */
61  template<typename PointT>
62  class KdTree: public Search<PointT>
63  {
64  public:
67 
68  typedef boost::shared_ptr<std::vector<int> > IndicesPtr;
69  typedef boost::shared_ptr<const std::vector<int> > IndicesConstPtr;
70 
78 
79  typedef boost::shared_ptr<KdTree<PointT> > Ptr;
80  typedef boost::shared_ptr<const KdTree<PointT> > ConstPtr;
81 
82  typedef boost::shared_ptr<pcl::KdTreeFLANN<PointT> > KdTreeFLANNPtr;
83  typedef boost::shared_ptr<const pcl::KdTreeFLANN<PointT> > KdTreeFLANNConstPtr;
84  typedef boost::shared_ptr<const PointRepresentation<PointT> > PointRepresentationConstPtr;
85 
86  /** \brief Constructor for KdTree.
87  *
88  * \param[in] sorted set to true if the nearest neighbor search results
89  * need to be sorted in ascending order based on their distance to the
90  * query point
91  *
92  */
93  KdTree (bool sorted = true);
94 
95  /** \brief Destructor for KdTree. */
96  virtual
98  {
99  }
100 
101  /** \brief Provide a pointer to the point representation to use to convert points into k-D vectors.
102  * \param[in] point_representation the const boost shared pointer to a PointRepresentation
103  */
104  void
105  setPointRepresentation (const PointRepresentationConstPtr &point_representation);
106 
107  /** \brief Get a pointer to the point representation used when converting points into k-D vectors. */
110  {
111  return (tree_->getPointRepresentation ());
112  }
113 
114  /** \brief Sets whether the results have to be sorted or not.
115  * \param[in] sorted_results set to true if the radius search results should be sorted
116  */
117  void
118  setSortedResults (bool sorted_results);
119 
120  /** \brief Set the search epsilon precision (error bound) for nearest neighbors searches.
121  * \param[in] eps precision (error bound) for nearest neighbors searches
122  */
123  void
124  setEpsilon (float eps);
125 
126  /** \brief Get the search epsilon precision (error bound) for nearest neighbors searches. */
127  inline float
128  getEpsilon () const
129  {
130  return (tree_->getEpsilon ());
131  }
132 
133  /** \brief Provide a pointer to the input dataset.
134  * \param[in] cloud the const boost shared pointer to a PointCloud message
135  * \param[in] indices the point indices subset that is to be used from \a cloud
136  */
137  void
138  setInputCloud (const PointCloudConstPtr& cloud,
139  const IndicesConstPtr& indices = IndicesConstPtr ());
140 
141  /** \brief Search for the k-nearest neighbors for the given query point.
142  * \param[in] point the given query point
143  * \param[in] k the number of neighbors to search for
144  * \param[out] k_indices the resultant indices of the neighboring points (must be resized to \a k a priori!)
145  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points (must be resized to \a k
146  * a priori!)
147  * \return number of neighbors found
148  */
149  int
150  nearestKSearch (const PointT &point, int k,
151  std::vector<int> &k_indices,
152  std::vector<float> &k_sqr_distances) const;
153 
154  /** \brief Search for all the nearest neighbors of the query point in a given radius.
155  * \param[in] point the given query point
156  * \param[in] radius the radius of the sphere bounding all of p_q's neighbors
157  * \param[out] k_indices the resultant indices of the neighboring points
158  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points
159  * \param[in] max_nn if given, bounds the maximum returned neighbors to this value. If \a max_nn is set to
160  * 0 or to a number higher than the number of points in the input cloud, all neighbors in \a radius will be
161  * returned.
162  * \return number of neighbors found in radius
163  */
164  int
165  radiusSearch (const PointT& point, double radius,
166  std::vector<int> &k_indices,
167  std::vector<float> &k_sqr_distances,
168  unsigned int max_nn = 0) const;
169  protected:
170  /** \brief A pointer to the internal KdTreeFLANN object. */
172  };
173  }
174 }
175 
176 #define PCL_INSTANTIATE_KdTree(T) template class PCL_EXPORTS pcl::search::KdTree<T>;
177 
178 #endif // PCL_SEARCH_KDTREE_H_
179