Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
flann_search.h
Go to the documentation of this file.
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  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * $Id: flann_search.h 5170 2012-03-18 04:21:56Z rusu $
37  */
38 
39 #ifndef PCL_SEARCH_FLANN_SEARCH_H_
40 #define PCL_SEARCH_FLANN_SEARCH_H_
41 
42 #include <pcl/search/search.h>
43 #include <pcl/common/time.h>
45 
46 namespace flann
47 {
48  template<typename T> class NNIndex;
49  template<typename T> class L2;
50  template<typename T> class L2_Simple;
51  template<typename T> class Matrix;
52 }
53 
54 namespace pcl
55 {
56  namespace search
57  {
58 
66  template<typename PointT, typename FlannDistance=flann::L2_Simple <float> >
67  class FlannSearch: public Search<PointT>
68  {
69  typedef typename Search<PointT>::PointCloud PointCloud;
70  typedef typename Search<PointT>::PointCloudConstPtr PointCloudConstPtr;
71 
72  typedef boost::shared_ptr<std::vector<int> > IndicesPtr;
73  typedef boost::shared_ptr<const std::vector<int> > IndicesConstPtr;
75  typedef boost::shared_ptr<flann::NNIndex <FlannDistance > > IndexPtr;
76  typedef boost::shared_ptr<flann::Matrix <float> > MatrixPtr;
77  typedef boost::shared_ptr<const flann::Matrix <float> > MatrixConstPtr;
78 
80  //typedef boost::shared_ptr<PointRepresentation> PointRepresentationPtr;
81  typedef boost::shared_ptr<const PointRepresentation> PointRepresentationConstPtr;
82 
86 
87  public:
88  typedef boost::shared_ptr<FlannSearch<PointT> > Ptr;
89  typedef boost::shared_ptr<const FlannSearch<PointT> > ConstPtr;
90 
97  {
98  public:
103  virtual IndexPtr createIndex (MatrixConstPtr data)=0;
104  };
105 
109  {
110  public:
115  KdTreeIndexCreator (unsigned int max_leaf_size=15) : max_leaf_size_ (max_leaf_size){}
120  virtual IndexPtr createIndex (MatrixConstPtr data);
121  private:
122  unsigned int max_leaf_size_;
123  };
124 
125  FlannSearch (bool sorted = true, FlannIndexCreator* creator = new KdTreeIndexCreator());
126 
128  virtual
129  ~FlannSearch ();
130 
131 
132  //void
133  //setInputCloud (const PointCloudConstPtr &cloud, const IndicesConstPtr &indices = IndicesConstPtr ());
134 
138  inline void
139  setEpsilon (double eps)
140  {
141  eps_ = eps;
142  }
143 
145  inline double
147  {
148  return (eps_);
149  }
150 
155  inline virtual void
156  setInputCloud (const PointCloudConstPtr& cloud, const IndicesConstPtr& indices = IndicesConstPtr ());
157 
166  int
167  nearestKSearch (const PointT &point, int k, std::vector<int> &k_indices, std::vector<float> &k_sqr_distances) const;
168 
169 
177  virtual void
178  nearestKSearch (const PointCloud& cloud, const std::vector<int>& indices, int k,
179  std::vector< std::vector<int> >& k_indices, std::vector< std::vector<float> >& k_sqr_distances) const;
180 
191  int
192  radiusSearch (const PointT& point, double radius,
193  std::vector<int> &k_indices, std::vector<float> &k_sqr_distances,
194  unsigned int max_nn = 0) const;
195 
204  virtual void
205  radiusSearch (const PointCloud& cloud, const std::vector<int>& indices, double radius, std::vector< std::vector<int> >& k_indices,
206  std::vector< std::vector<float> >& k_sqr_distances, unsigned int max_nn=0) const;
207 
211  inline void
212  setPointRepresentation (const PointRepresentationConstPtr &point_representation)
213  {
214  point_representation_ = point_representation;
215  setInputCloud (input_, indices_); // re-create the tree, since point_represenation might change things such as the scaling of the point clouds.
216  }
217 
219  inline PointRepresentationConstPtr const
221  {
222  return (point_representation_);
223  }
224 
225  protected:
226 
229  void convertInputToFlannMatrix();
230 
233  IndexPtr index_;
234 
237  FlannIndexCreator *creator_;
238 
241  MatrixPtr input_flann_;
242 
245  float eps_;
246  bool input_copied_for_flann_;
247 
248  PointRepresentationConstPtr point_representation_;
249 
250  int dim_;
251 
252  std::vector<int> index_mapping_;
253  bool identity_mapping_;
254 
255  };
256  }
257 }
258 
259 #define PCL_INSTANTIATE_FlannSearch(T) template class PCL_EXPORTS pcl::search::FlannSearch<T>;
260 
261 #endif // PCL_SEARCH_KDTREE_H_
262