Point Cloud Library (PCL)  1.3.1
extract_clusters.h
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2010, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of Willow Garage, Inc. nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  * $Id: extract_clusters.h 3035 2011-11-01 04:29:18Z rusu $
00035  *
00036  */
00037 
00038 #ifndef PCL_EXTRACT_CLUSTERS_H_
00039 #define PCL_EXTRACT_CLUSTERS_H_
00040 
00041 #include <pcl/pcl_base.h>
00042 
00043 #include "pcl/search/pcl_search.h"
00044 
00045 namespace pcl
00046 {
00048 
00058   template <typename PointT> void extractEuclideanClusters (const PointCloud<PointT> &cloud, const boost::shared_ptr<search::Search<PointT> > &tree, float tolerance, std::vector<PointIndices> &clusters, unsigned int min_pts_per_cluster = 1, unsigned int max_pts_per_cluster = (std::numeric_limits<int>::max) ());
00059 
00061 
00072   template <typename PointT> void extractEuclideanClusters (const PointCloud<PointT> &cloud, const std::vector<int> &indices, const boost::shared_ptr<search::Search<PointT> > &tree, float tolerance, std::vector<PointIndices> &clusters, unsigned int min_pts_per_cluster = 1, unsigned int max_pts_per_cluster = (std::numeric_limits<int>::max) ());
00073 
00075 
00088   template <typename PointT, typename Normal> void 
00089   extractEuclideanClusters (
00090       const PointCloud<PointT> &cloud, const PointCloud<Normal> &normals, 
00091       float tolerance, const boost::shared_ptr<KdTree<PointT> > &tree, 
00092       std::vector<PointIndices> &clusters, double eps_angle, 
00093       unsigned int min_pts_per_cluster = 1, 
00094       unsigned int max_pts_per_cluster = (std::numeric_limits<int>::max) ())
00095   {
00096     if (tree->getInputCloud ()->points.size () != cloud.points.size ())
00097     {
00098       PCL_ERROR ("[pcl::extractEuclideanClusters] Tree built for a different point cloud dataset (%lu) than the input cloud (%lu)!\n", (unsigned long)tree->getInputCloud ()->points.size (), (unsigned long)cloud.points.size ());
00099       return;
00100     }
00101     if (cloud.points.size () != normals.points.size ())
00102     {
00103       PCL_ERROR ("[pcl::extractEuclideanClusters] Number of points in the input point cloud (%lu) different than normals (%lu)!\n", (unsigned long)cloud.points.size (), (unsigned long)normals.points.size ());
00104       return;
00105     }
00106 
00107     // Create a bool vector of processed point indices, and initialize it to false
00108     std::vector<bool> processed (cloud.points.size (), false);
00109 
00110     std::vector<int> nn_indices;
00111     std::vector<float> nn_distances;
00112     // Process all points in the indices vector
00113     for (size_t i = 0; i < cloud.points.size (); ++i)
00114     {
00115       if (processed[i])
00116         continue;
00117 
00118       std::vector<unsigned int> seed_queue;
00119       int sq_idx = 0;
00120       seed_queue.push_back (i);
00121 
00122       processed[i] = true;
00123 
00124       while (sq_idx < (int)seed_queue.size ())
00125       {
00126         // Search for sq_idx
00127         if (!tree->radiusSearch (seed_queue[sq_idx], tolerance, nn_indices, nn_distances))
00128         {
00129           sq_idx++;
00130           continue;
00131         }
00132 
00133         for (size_t j = 1; j < nn_indices.size (); ++j)             // nn_indices[0] should be sq_idx
00134         {
00135           if (processed[nn_indices[j]])                         // Has this point been processed before ?
00136             continue;
00137 
00138           //processed[nn_indices[j]] = true;
00139           // [-1;1]
00140           double dot_p = normals.points[i].normal[0] * normals.points[nn_indices[j]].normal[0] +
00141                          normals.points[i].normal[1] * normals.points[nn_indices[j]].normal[1] +
00142                          normals.points[i].normal[2] * normals.points[nn_indices[j]].normal[2];
00143           if ( fabs (acos (dot_p)) < eps_angle )
00144           {
00145             processed[nn_indices[j]] = true;
00146             seed_queue.push_back (nn_indices[j]);
00147           }
00148         }
00149 
00150         sq_idx++;
00151       }
00152 
00153       // If this queue is satisfactory, add to the clusters
00154       if (seed_queue.size () >= min_pts_per_cluster && seed_queue.size () <= max_pts_per_cluster)
00155       {
00156         pcl::PointIndices r;
00157         r.indices.resize (seed_queue.size ());
00158         for (size_t j = 0; j < seed_queue.size (); ++j)
00159           r.indices[j] = seed_queue[j];
00160 
00161         std::sort (r.indices.begin (), r.indices.end ());
00162         r.indices.erase (std::unique (r.indices.begin (), r.indices.end ()), r.indices.end ());
00163 
00164         r.header = cloud.header;
00165         clusters.push_back (r);   // We could avoid a copy by working directly in the vector
00166       }
00167     }
00168   }
00169 
00170 
00172 
00186   template <typename PointT, typename Normal> 
00187   void extractEuclideanClusters (
00188       const PointCloud<PointT> &cloud, const PointCloud<Normal> &normals, 
00189       const std::vector<int> &indices, const boost::shared_ptr<KdTree<PointT> > &tree, 
00190       float tolerance, std::vector<PointIndices> &clusters, double eps_angle, 
00191       unsigned int min_pts_per_cluster = 1, 
00192       unsigned int max_pts_per_cluster = (std::numeric_limits<int>::max) ())
00193   {
00194     // \note If the tree was created over <cloud, indices>, we guarantee a 1-1 mapping between what the tree returns
00195     //and indices[i]
00196     if (tree->getInputCloud ()->points.size () != cloud.points.size ())
00197     {
00198       PCL_ERROR ("[pcl::extractEuclideanClusters] Tree built for a different point cloud dataset (%lu) than the input cloud (%lu)!\n", (unsigned long)tree->getInputCloud ()->points.size (), (unsigned long)cloud.points.size ());
00199       return;
00200     }
00201     if (tree->getIndices ()->size () != indices.size ())
00202     {
00203       PCL_ERROR ("[pcl::extractEuclideanClusters] Tree built for a different set of indices (%lu) than the input set (%lu)!\n", (unsigned long)tree->getIndices ()->size (), (unsigned long)indices.size ());
00204       return;
00205     }
00206     if (cloud.points.size () != normals.points.size ())
00207     {
00208       PCL_ERROR ("[pcl::extractEuclideanClusters] Number of points in the input point cloud (%lu) different than normals (%lu)!\n", (unsigned long)cloud.points.size (), (unsigned long)normals.points.size ());
00209       return;
00210     }
00211     // Create a bool vector of processed point indices, and initialize it to false
00212     std::vector<bool> processed (indices.size (), false);
00213 
00214     std::vector<int> nn_indices;
00215     std::vector<float> nn_distances;
00216     // Process all points in the indices vector
00217     for (size_t i = 0; i < indices.size (); ++i)
00218     {
00219       if (processed[i])
00220         continue;
00221 
00222       std::vector<int> seed_queue;
00223       int sq_idx = 0;
00224       seed_queue.push_back (i);
00225 
00226       processed[i] = true;
00227 
00228       while (sq_idx < (int)seed_queue.size ())
00229       {
00230         // Search for sq_idx
00231         if (!tree->radiusSearch (seed_queue[sq_idx], tolerance, nn_indices, nn_distances))
00232         {
00233           sq_idx++;
00234           continue;
00235         }
00236 
00237         for (size_t j = 1; j < nn_indices.size (); ++j)             // nn_indices[0] should be sq_idx
00238         {
00239           if (processed[nn_indices[j]])                             // Has this point been processed before ?
00240             continue;
00241 
00242           //processed[nn_indices[j]] = true;
00243           // [-1;1]
00244           double dot_p =
00245             normals.points[indices[i]].normal[0] * normals.points[indices[nn_indices[j]]].normal[0] +
00246             normals.points[indices[i]].normal[1] * normals.points[indices[nn_indices[j]]].normal[1] +
00247             normals.points[indices[i]].normal[2] * normals.points[indices[nn_indices[j]]].normal[2];
00248           if ( fabs (acos (dot_p)) < eps_angle )
00249           {
00250             processed[nn_indices[j]] = true;
00251             seed_queue.push_back (nn_indices[j]);
00252           }
00253         }
00254 
00255         sq_idx++;
00256       }
00257 
00258       // If this queue is satisfactory, add to the clusters
00259       if (seed_queue.size () >= min_pts_per_cluster && seed_queue.size () <= max_pts_per_cluster)
00260       {
00261         pcl::PointIndices r;
00262         r.indices.resize (seed_queue.size ());
00263         for (size_t j = 0; j < seed_queue.size (); ++j)
00264           r.indices[j] = indices[seed_queue[j]];
00265 
00266         std::sort (r.indices.begin (), r.indices.end ());
00267         r.indices.erase (std::unique (r.indices.begin (), r.indices.end ()), r.indices.end ());
00268 
00269         r.header = cloud.header;
00270         clusters.push_back (r);
00271       }
00272     }
00273   }
00274 
00278 
00282   template <typename PointT>
00283   class EuclideanClusterExtraction: public PCLBase<PointT>
00284   {
00285     typedef PCLBase<PointT> BasePCLBase;
00286 
00287     public:
00288       typedef pcl::PointCloud<PointT> PointCloud;
00289       typedef typename PointCloud::Ptr PointCloudPtr;
00290       typedef typename PointCloud::ConstPtr PointCloudConstPtr;
00291 
00292       typedef typename pcl::search::Search<PointT> KdTree;
00293       typedef typename pcl::search::Search<PointT>::Ptr KdTreePtr;
00294 
00295       typedef PointIndices::Ptr PointIndicesPtr;
00296       typedef PointIndices::ConstPtr PointIndicesConstPtr;
00297 
00299 
00300       EuclideanClusterExtraction () : tree_ (), min_pts_per_cluster_ (1), 
00301                                       max_pts_per_cluster_ (std::numeric_limits<int>::max ())
00302       {};
00303 
00307       inline void setSearchMethod (const KdTreePtr &tree) { tree_ = tree; }
00308 
00310       inline KdTreePtr getSearchMethod () { return (tree_); }
00311 
00315       inline void setClusterTolerance (double tolerance) { cluster_tolerance_ = tolerance; }
00316 
00318       inline double getClusterTolerance () { return (cluster_tolerance_); }
00319 
00323       inline void setMinClusterSize (int min_cluster_size) { min_pts_per_cluster_ = min_cluster_size; }
00324 
00326       inline int getMinClusterSize () { return (min_pts_per_cluster_); }
00327 
00331       inline void setMaxClusterSize (int max_cluster_size) { max_pts_per_cluster_ = max_cluster_size; }
00332 
00334       inline int getMaxClusterSize () { return (max_pts_per_cluster_); }
00335 
00339       void extract (std::vector<PointIndices> &clusters);
00340 
00341     protected:
00342       // Members derived from the base class
00343       using BasePCLBase::input_;
00344       using BasePCLBase::indices_;
00345       using BasePCLBase::initCompute;
00346       using BasePCLBase::deinitCompute;
00347 
00349       KdTreePtr tree_;
00350 
00352       double cluster_tolerance_;
00353 
00355       int min_pts_per_cluster_;
00356 
00358       int max_pts_per_cluster_;
00359 
00361       virtual std::string getClassName () const { return ("EuclideanClusterExtraction"); }
00362 
00363   };
00364 
00368   inline bool 
00369     comparePointClusters (const pcl::PointIndices &a, const pcl::PointIndices &b)
00370   {
00371     return (a.indices.size () < b.indices.size ());
00372   }
00373 }
00374 
00375 #endif  //#ifndef PCL_EXTRACT_CLUSTERS_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines