Point Cloud Library (PCL)  1.9.1-dev
kdtree_flann.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2012, 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: kdtree_flann.h 36261 2011-02-26 01:34:42Z mariusm $
38  *
39  */
40 
41 #pragma once
42 
43 #include <pcl/kdtree/kdtree.h>
44 #include <flann/util/params.h>
45 
46 #include <boost/shared_array.hpp>
47 
48 // Forward declarations
49 namespace flann
50 {
51  template <typename T> struct L2_Simple;
52  template <typename T> class Index;
53 }
54 
55 namespace pcl
56 {
57  // Forward declarations
58  template <typename T> class PointRepresentation;
59 
60  /** \brief KdTreeFLANN is a generic type of 3D spatial locator using kD-tree structures. The class is making use of
61  * the FLANN (Fast Library for Approximate Nearest Neighbor) project by Marius Muja and David Lowe.
62  *
63  * \author Radu B. Rusu, Marius Muja
64  * \ingroup kdtree
65  */
66  template <typename PointT, typename Dist = ::flann::L2_Simple<float> >
67  class KdTreeFLANN : public pcl::KdTree<PointT>
68  {
69  public:
77 
80 
81  using IndicesPtr = boost::shared_ptr<std::vector<int> >;
82  using IndicesConstPtr = boost::shared_ptr<const std::vector<int> >;
83 
85 
86  // Boost shared pointers
87  using Ptr = boost::shared_ptr<KdTreeFLANN<PointT, Dist> >;
88  using ConstPtr = boost::shared_ptr<const KdTreeFLANN<PointT, Dist> >;
89 
90  /** \brief Default Constructor for KdTreeFLANN.
91  * \param[in] sorted set to true if the application that the tree will be used for requires sorted nearest neighbor indices (default). False otherwise.
92  *
93  * By setting sorted to false, the \ref radiusSearch operations will be faster.
94  */
95  KdTreeFLANN (bool sorted = true);
96 
97  /** \brief Copy constructor
98  * \param[in] k the tree to copy into this
99  */
101 
102  /** \brief Copy operator
103  * \param[in] k the tree to copy into this
104  */
106  operator = (const KdTreeFLANN<PointT, Dist>& k)
107  {
109  flann_index_ = k.flann_index_;
110  cloud_ = k.cloud_;
111  index_mapping_ = k.index_mapping_;
112  identity_mapping_ = k.identity_mapping_;
113  dim_ = k.dim_;
114  total_nr_points_ = k.total_nr_points_;
115  param_k_ = k.param_k_;
116  param_radius_ = k.param_radius_;
117  return (*this);
118  }
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) override;
125 
126  void
127  setSortedResults (bool sorted);
128 
129  inline Ptr makeShared () { return Ptr (new KdTreeFLANN<PointT, Dist> (*this)); }
130 
131  /** \brief Destructor for KdTreeFLANN.
132  * Deletes all allocated data arrays and destroys the kd-tree structures.
133  */
135  {
136  cleanup ();
137  }
138 
139  /** \brief Provide a pointer to the input dataset.
140  * \param[in] cloud the const boost shared pointer to a PointCloud message
141  * \param[in] indices the point indices subset that is to be used from \a cloud - if NULL the whole cloud is used
142  */
143  void
144  setInputCloud (const PointCloudConstPtr &cloud, const IndicesConstPtr &indices = IndicesConstPtr ()) override;
145 
146  /** \brief Search for k-nearest neighbors for the given query point.
147  *
148  * \attention This method does not do any bounds checking for the input index
149  * (i.e., index >= cloud.points.size () || index < 0), and assumes valid (i.e., finite) data.
150  *
151  * \param[in] point a given \a valid (i.e., finite) query point
152  * \param[in] k the number of neighbors to search for
153  * \param[out] k_indices the resultant indices of the neighboring points (must be resized to \a k a priori!)
154  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points (must be resized to \a k
155  * a priori!)
156  * \return number of neighbors found
157  *
158  * \exception asserts in debug mode if the index is not between 0 and the maximum number of points
159  */
160  int
161  nearestKSearch (const PointT &point, int k,
162  std::vector<int> &k_indices, std::vector<float> &k_sqr_distances) const override;
163 
164  /** \brief Search for all the nearest neighbors of the query point in a given radius.
165  *
166  * \attention This method does not do any bounds checking for the input index
167  * (i.e., index >= cloud.points.size () || index < 0), and assumes valid (i.e., finite) data.
168  *
169  * \param[in] point a given \a valid (i.e., finite) query point
170  * \param[in] radius the radius of the sphere bounding all of p_q's neighbors
171  * \param[out] k_indices the resultant indices of the neighboring points
172  * \param[out] k_sqr_distances the resultant squared distances to the neighboring points
173  * \param[in] max_nn if given, bounds the maximum returned neighbors to this value. If \a max_nn is set to
174  * 0 or to a number higher than the number of points in the input cloud, all neighbors in \a radius will be
175  * returned.
176  * \return number of neighbors found in radius
177  *
178  * \exception asserts in debug mode if the index is not between 0 and the maximum number of points
179  */
180  int
181  radiusSearch (const PointT &point, double radius, std::vector<int> &k_indices,
182  std::vector<float> &k_sqr_distances, unsigned int max_nn = 0) const override;
183 
184  private:
185  /** \brief Internal cleanup method. */
186  void
187  cleanup ();
188 
189  /** \brief Converts a PointCloud to the internal FLANN point array representation. Returns the number
190  * of points.
191  * \param cloud the PointCloud
192  */
193  void
194  convertCloudToArray (const PointCloud &cloud);
195 
196  /** \brief Converts a PointCloud with a given set of indices to the internal FLANN point array
197  * representation. Returns the number of points.
198  * \param[in] cloud the PointCloud data
199  * \param[in] indices the point cloud indices
200  */
201  void
202  convertCloudToArray (const PointCloud &cloud, const std::vector<int> &indices);
203 
204  private:
205  /** \brief Class getName method. */
206  std::string
207  getName () const override { return ("KdTreeFLANN"); }
208 
209  /** \brief A FLANN index object. */
210  boost::shared_ptr<FLANNIndex> flann_index_;
211 
212  /** \brief Internal pointer to data. */
213  boost::shared_array<float> cloud_;
214 
215  /** \brief mapping between internal and external indices. */
216  std::vector<int> index_mapping_;
217 
218  /** \brief whether the mapping between internal and external indices is identity */
219  bool identity_mapping_;
220 
221  /** \brief Tree dimensionality (i.e. the number of dimensions per point). */
222  int dim_;
223 
224  /** \brief The total size of the data (either equal to the number of points in the input cloud or to the number of indices - if passed). */
225  int total_nr_points_;
226 
227  /** \brief The KdTree search parameters for K-nearest neighbors. */
228  ::flann::SearchParams param_k_;
229 
230  /** \brief The KdTree search parameters for radius search. */
231  ::flann::SearchParams param_radius_;
232  };
233 }
234 
235 #ifdef PCL_NO_PRECOMPILE
236 #include <pcl/kdtree/impl/kdtree_flann.hpp>
237 #endif
KdTreeFLANN is a generic type of 3D spatial locator using kD-tree structures.
Definition: kdtree_flann.h:67
boost::shared_ptr< std::vector< int > > IndicesPtr
Definition: kdtree.h:58
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
boost::shared_ptr< const KdTree< FeatureT > > ConstPtr
Definition: kdtree.h:71
boost::shared_ptr< KdTree< FeatureT > > Ptr
Definition: kdtree.h:70
boost::shared_ptr< const PointCloud > PointCloudConstPtr
Definition: kdtree.h:63
boost::shared_ptr< const std::vector< int > > IndicesConstPtr
Definition: kdtree.h:59
A point structure representing Euclidean xyz coordinates, and the RGB color.
~KdTreeFLANN()
Destructor for KdTreeFLANN.
Definition: kdtree_flann.h:134
KdTree represents the base spatial locator class for kd-tree implementations.
Definition: kdtree.h:55