Point Cloud Library (PCL)  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
kdtree_flann.h
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Point Cloud Library (PCL) - www.pointclouds.org
00005  *  Copyright (c) 2010-2011, Willow Garage, Inc.
00006  *
00007  *  All rights reserved.
00008  *
00009  *  Redistribution and use in source and binary forms, with or without
00010  *  modification, are permitted provided that the following conditions
00011  *  are met:
00012  *
00013  *   * Redistributions of source code must retain the above copyright
00014  *     notice, this list of conditions and the following disclaimer.
00015  *   * Redistributions in binary form must reproduce the above
00016  *     copyright notice, this list of conditions and the following
00017  *     disclaimer in the documentation and/or other materials provided
00018  *     with the distribution.
00019  *   * Neither the name of Willow Garage, Inc. nor the names of its
00020  *     contributors may be used to endorse or promote products derived
00021  *     from this software without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  * $Id: kdtree_flann.h 3749 2011-12-31 22:58:01Z rusu $
00037  *
00038  */
00039 
00040 #ifndef PCL_KDTREE_KDTREE_FLANN_H_
00041 #define PCL_KDTREE_KDTREE_FLANN_H_
00042 
00043 #include <cstdio>
00044 #include <flann/flann.hpp>
00045 #include <pcl/kdtree/kdtree.h>
00046 #include <boost/thread/mutex.hpp>
00047 
00048 namespace pcl
00049 {
00056   template <typename PointT, typename Dist = flann::L2_Simple<float> >
00057   class KdTreeFLANN : public pcl::KdTree<PointT>
00058   {
00059     using KdTree<PointT>::input_;
00060     using KdTree<PointT>::indices_;
00061     using KdTree<PointT>::epsilon_;
00062     using KdTree<PointT>::sorted_;
00063     using KdTree<PointT>::point_representation_;
00064 
00065     typedef typename KdTree<PointT>::PointCloud PointCloud;
00066     typedef typename KdTree<PointT>::PointCloudConstPtr PointCloudConstPtr;
00067 
00068     typedef boost::shared_ptr<std::vector<int> > IndicesPtr;
00069     typedef boost::shared_ptr<const std::vector<int> > IndicesConstPtr;
00070 
00071     typedef flann::Index<Dist> FLANNIndex;
00072 
00073     public:
00074       // Boost shared pointers
00075       typedef boost::shared_ptr<KdTreeFLANN<PointT> > Ptr;
00076       typedef boost::shared_ptr<const KdTreeFLANN<PointT> > ConstPtr;
00077 
00083       KdTreeFLANN (bool sorted = true) : pcl::KdTree<PointT> (sorted), flann_index_(NULL), cloud_(NULL)
00084       {
00085         param_k_ = flann::SearchParams (-1 ,epsilon_);
00086         param_radius_ = flann::SearchParams (-1 ,epsilon_, sorted);
00087         cleanup ();
00088       }
00089 
00098       KdTreeFLANN (KdTreeFLANN& tree) : pcl::KdTree<PointT> (tree)
00099       {
00100         shallowCopy (tree);
00101       }
00102 
00106       inline void
00107       setEpsilon (double eps)
00108       {
00109         epsilon_ = eps;
00110         param_k_ = flann::SearchParams (-1 ,epsilon_);
00111         param_radius_ = flann::SearchParams (-1 ,epsilon_, sorted_);
00112       }
00113 
00114       inline Ptr makeShared () { return Ptr (new KdTreeFLANN<PointT> (*this)); } 
00115 
00119       KdTreeFLANN& operator= (const KdTreeFLANN& tree)
00120       {
00121         if (this != &tree) 
00122           shallowCopy (tree);
00123         return (*this);
00124       }
00125 
00126 
00130       inline void 
00131       shallowCopy (const KdTreeFLANN& tree)
00132       {
00133         flann_index_ = tree.flann_index_;
00134         cloud_ = tree.cloud_;
00135         index_mapping_ = tree.index_mapping_;
00136         dim_ = tree.dim_;
00137         sorted_ = tree.sorted_;
00138       }
00139 
00140 
00144       virtual ~KdTreeFLANN ()
00145       {
00146         cleanup ();
00147       }
00148 
00153       void 
00154       setInputCloud (const PointCloudConstPtr &cloud, const IndicesConstPtr &indices = IndicesConstPtr ());
00155 
00164       int 
00165       nearestKSearch (const PointT &point, int k, 
00166                       std::vector<int> &k_indices, std::vector<float> &k_sqr_distances);
00167 
00177       inline int 
00178       nearestKSearch (const PointCloud &cloud, int index, int k, 
00179                       std::vector<int> &k_indices, std::vector<float> &k_sqr_distances)
00180       {
00181         if (index >= (int)cloud.points.size () || index < 0)
00182           return (0);
00183         return (nearestKSearch (cloud.points[index], k, k_indices, k_sqr_distances));
00184       }
00185 
00195       inline int 
00196       nearestKSearch (int index, int k, 
00197                       std::vector<int> &k_indices, std::vector<float> &k_sqr_distances)
00198       {
00199         if (indices_ == NULL)
00200         {
00201           if (index >= (int)input_->points.size () || index < 0)
00202             return (0);
00203           return (nearestKSearch (input_->points[index], k, k_indices, k_sqr_distances));
00204         }
00205         else
00206         {
00207           if (index >= (int)indices_->size() || index < 0)
00208             return (0);
00209           return (nearestKSearch (input_->points[(*indices_)[index]], k, k_indices, k_sqr_distances));
00210         }
00211       }
00212 
00221       int 
00222       radiusSearch (const PointT &point, double radius, std::vector<int> &k_indices,
00223                     std::vector<float> &k_sqr_distances, int max_nn = -1) const;
00224 
00234       inline int 
00235       radiusSearch (const PointCloud &cloud, int index, double radius, 
00236                     std::vector<int> &k_indices, std::vector<float> &k_sqr_distances, 
00237                     int max_nn = -1) const
00238       {
00239         if (index >= (int)cloud.points.size () || index < 0)
00240           return (0);
00241         return (radiusSearch(cloud.points[index], radius, k_indices, k_sqr_distances, max_nn));
00242       }
00243 
00253       inline int 
00254       radiusSearch (int index, double radius, std::vector<int> &k_indices,
00255                     std::vector<float> &k_sqr_distances, int max_nn = -1) const
00256       {
00257         if (indices_ == NULL)
00258         {
00259           if (index >= (int)input_->points.size () || index < 0)
00260             return (0);
00261           return (radiusSearch (input_->points[index], radius, k_indices, k_sqr_distances, max_nn));
00262         }
00263         else
00264         {
00265           if (index >= (int)indices_->size () || index < 0)
00266             return (0);
00267           return (radiusSearch (input_->points[(*indices_)[index]], radius, k_indices, k_sqr_distances, max_nn));
00268         }
00269       }
00270 
00271     private:
00273       void 
00274       cleanup ();
00275 
00277       bool 
00278       initParameters ();
00279 
00281       void 
00282       initData ();
00283 
00288       void 
00289       convertCloudToArray (const PointCloud &cloud);
00290 
00296       void 
00297       convertCloudToArray (const PointCloud &cloud, const std::vector<int> &indices);
00298 
00299     private:
00301       virtual std::string 
00302       getName () const { return ("KdTreeFLANN"); }
00303 
00304       boost::mutex m_lock_;
00305 
00307       FLANNIndex* flann_index_;
00308 
00310       float* cloud_;
00311       
00313       std::vector<int> index_mapping_;
00314       
00316       bool identity_mapping_;
00317 
00319       int dim_;
00320 
00322       flann::SearchParams param_k_;
00323 
00325       flann::SearchParams param_radius_;
00326    };
00327 }
00328 
00329 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines