Point Cloud Library (PCL)  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
kdtree.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) 2009-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.h 3749 2011-12-31 22:58:01Z rusu $
00037  *
00038  */
00039 
00040 #ifndef PCL_KDTREE_KDTREE_H_
00041 #define PCL_KDTREE_KDTREE_H_
00042 
00043 #include <limits.h>
00044 #include <pcl/pcl_macros.h>
00045 #include <pcl/point_cloud.h>
00046 #include <pcl/point_representation.h>
00047 #include <pcl/common/io.h>
00048 
00049 namespace pcl
00050 {
00055   template <typename PointT>
00056   class KdTree
00057   {
00058     typedef boost::shared_ptr <std::vector<int> > IndicesPtr;
00059     typedef boost::shared_ptr <const std::vector<int> > IndicesConstPtr;
00060 
00061     public:
00062       typedef pcl::PointCloud<PointT> PointCloud;
00063       typedef boost::shared_ptr<PointCloud> PointCloudPtr;
00064       typedef boost::shared_ptr<const PointCloud> PointCloudConstPtr;
00065 
00066       typedef pcl::PointRepresentation<PointT> PointRepresentation;
00067       //typedef boost::shared_ptr<PointRepresentation> PointRepresentationPtr;
00068       typedef boost::shared_ptr<const PointRepresentation> PointRepresentationConstPtr;
00069 
00070       // Boost shared pointers
00071       typedef boost::shared_ptr<KdTree<PointT> > Ptr;
00072       typedef boost::shared_ptr<const KdTree<PointT> > ConstPtr;
00073 
00077       KdTree (bool sorted = true) : input_(), indices_(), 
00078                                     epsilon_(0.0), min_pts_(1), sorted_(sorted)
00079       {
00080         point_representation_.reset (new DefaultPointRepresentation<PointT>);
00081       };
00082 
00083 
00088       virtual inline void
00089       setInputCloud (const PointCloudConstPtr &cloud, const IndicesConstPtr &indices = IndicesConstPtr ())
00090       {
00091         input_   = cloud;
00092         indices_ = indices;
00093       }
00094 
00096       inline IndicesConstPtr const
00097       getIndices ()
00098       {
00099         return (indices_);
00100       }
00101 
00103       inline PointCloudConstPtr
00104       getInputCloud ()
00105       {
00106         return (input_);
00107       }
00108 
00112       inline void
00113       setPointRepresentation (const PointRepresentationConstPtr &point_representation)
00114       {
00115         point_representation_ = point_representation;
00116         setInputCloud (input_, indices_);  // Makes sense in derived classes to reinitialize the tree
00117       }
00118 
00120       inline PointRepresentationConstPtr const
00121       getPointRepresentation ()
00122       {
00123         return (point_representation_);
00124       }
00125 
00127       virtual ~KdTree () {};
00128 
00138       virtual int 
00139       nearestKSearch (const PointCloud &cloud, int index, int k, 
00140                       std::vector<int> &k_indices, std::vector<float> &k_sqr_distances) = 0;
00141 
00150       virtual int 
00151       nearestKSearch (const PointT &p_q, int k, 
00152                       std::vector<int> &k_indices, std::vector<float> &k_sqr_distances) = 0;
00153 
00163       template <typename PointTDiff> inline int 
00164       nearestKSearchT (const PointTDiff &point, int k, 
00165                        std::vector<int> &k_indices, std::vector<float> &k_sqr_distances)
00166       {
00167         PointT p;
00168         // Copy all the data fields from the input cloud to the output one
00169         typedef typename pcl::traits::fieldList<PointT>::type FieldListInT;
00170         typedef typename pcl::traits::fieldList<PointTDiff>::type FieldListOutT;
00171         typedef typename pcl::intersect<FieldListInT, FieldListOutT>::type FieldList;
00172         pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointTDiff, PointT> (point, p));
00173         return (nearestKSearch (p, k, k_indices, k_sqr_distances));
00174       }
00175 
00185       virtual int 
00186       nearestKSearch (int index, int k, std::vector<int> &k_indices, std::vector<float> &k_sqr_distances) = 0;
00187 
00197       virtual int 
00198       radiusSearch (const PointCloud &cloud, int index, double radius, std::vector<int> &k_indices,
00199                     std::vector<float> &k_sqr_distances, int max_nn = INT_MAX) const = 0;
00200 
00209       virtual int 
00210       radiusSearch (const PointT &p_q, double radius, std::vector<int> &k_indices,
00211                     std::vector<float> &k_sqr_distances, int max_nn = INT_MAX) const = 0;
00212 
00221       template <typename PointTDiff> inline int 
00222       radiusSearchT (const PointTDiff &point, double radius, std::vector<int> &k_indices,
00223                      std::vector<float> &k_sqr_distances, int max_nn = -1) const
00224       {
00225         PointT p;
00226         // Copy all the data fields from the input cloud to the output one
00227         typedef typename pcl::traits::fieldList<PointT>::type FieldListInT;
00228         typedef typename pcl::traits::fieldList<PointTDiff>::type FieldListOutT;
00229         typedef typename pcl::intersect<FieldListInT, FieldListOutT>::type FieldList;
00230         pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointTDiff, PointT> (point, p));
00231         return (radiusSearch (p, radius, k_indices, k_sqr_distances, max_nn));
00232       }
00233 
00243       virtual int 
00244       radiusSearch (int index, double radius, std::vector<int> &k_indices,
00245                     std::vector<float> &k_sqr_distances, int max_nn = INT_MAX) const = 0;
00246 
00250       virtual inline void
00251       setEpsilon (double eps)
00252       {
00253         epsilon_ = eps;
00254       }
00255 
00257       inline double
00258       getEpsilon ()
00259       {
00260         return (epsilon_);
00261       }
00262 
00266       inline void
00267       setMinPts (int min_pts)
00268       {
00269         min_pts_ = min_pts;
00270       }
00271 
00273       inline float
00274       getMinPts ()
00275       {
00276         return (min_pts_);
00277       }
00278 
00279     protected:
00281       PointCloudConstPtr input_;
00282 
00284       IndicesConstPtr indices_;
00285 
00287       double epsilon_;
00288 
00290       int min_pts_;
00291 
00293       bool sorted_;
00294 
00296       PointRepresentationConstPtr point_representation_;
00297 
00299       virtual std::string 
00300       getName () const = 0;
00301   };
00302 }
00303 
00304 #endif  //#ifndef _PCL_KDTREE_KDTREE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines