crop_to_hull.cpp
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) 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 the copyright holder(s) 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$
00037   *
00038   */
00039   
00040 #include <pcl/filters/crop_hull.h>
00041 #include <pcl/io/pcd_io.h>
00042 #include <pcl/console/print.h>
00043 #include <pcl/console/parse.h>
00044 #include <pcl/console/time.h>
00045 #include <pcl/surface/concave_hull.h>
00046 
00047 
00048 using namespace pcl;
00049 using namespace pcl::io;
00050 using namespace pcl::console;
00051 
00052 typedef PointXYZ PointT;
00053 typedef PointCloud<PointT> CloudT;
00054 
00055 const static double default_alpha = 1e3f;
00056 
00057 static void
00058 printHelp (int, char **argv)
00059 {
00060   print_error ("Syntax is: %s hull_cloud.pcd input.pcd output.pcd <options>\n", argv[0]);
00061   print_info ("  where options are:\n");
00062   print_info ("                     -alpha X = the hull alpha value (0+) (default: ");
00063   print_value ("%f", default_alpha);
00064   print_info (")\n");
00065 }
00066 
00067 static bool
00068 loadCloud (std::string const& filename, CloudT &cloud)
00069 {
00070   TicToc tt;
00071   print_highlight ("Loading ");
00072   print_value ("%s ", filename.c_str ());
00073 
00074   tt.tic ();
00075   if (loadPCDFile (filename, cloud) < 0)
00076     return (false);
00077 
00078   print_info ("[done, ");
00079   print_value ("%g", tt.toc ());
00080   print_info (" ms : ");
00081   print_value ("%d", cloud.width * cloud.height);
00082   print_info (" points]\n");
00083   print_info ("Available dimensions: ");
00084   print_value ("%s\n", pcl::getFieldsList (cloud).c_str ());
00085 
00086   return (true);
00087 }
00088 
00089 static void
00090 saveCloud (std::string const& filename, CloudT const& cloud)
00091 {
00092   TicToc tt;
00093   tt.tic ();
00094 
00095   print_highlight ("Saving ");
00096   print_value ("%s ", filename.c_str ());
00097 
00098   pcl::io::savePCDFile (filename, cloud);
00099 
00100   print_info ("[done, ");
00101   print_value ("%g", tt.toc ());
00102   print_info (" ms : ");
00103   print_value ("%d", cloud.width * cloud.height);
00104   print_info (" points]\n");
00105 }
00106 
00107 static void
00108 cropToHull (CloudT::Ptr output, CloudT::Ptr input, CloudT::Ptr hull_cloud, std::vector<pcl::Vertices> const& polygons, int dim)
00109 {
00110   TicToc tt;
00111   tt.tic ();
00112 
00113   print_highlight ("Cropping ");
00114 
00115   CropHull<PointT> crop_filter;
00116   crop_filter.setInputCloud (input);
00117   crop_filter.setHullCloud (hull_cloud);
00118   crop_filter.setHullIndices (polygons);
00119   crop_filter.setDim (dim);
00120   
00121   crop_filter.filter (*output);
00122 
00123   print_info ("[done, ");
00124   print_value ("%g", tt.toc ());
00125   print_info (" ms : ");
00126   print_value ("%d", output->size());
00127   print_info (" points passed crop]\n");
00128 }
00129 
00130 static CloudT::Ptr
00131 calculateHull (std::vector<pcl::Vertices>& polygons, int& dim, CloudT::Ptr cloud, double alpha)
00132 {
00133   pcl::ConcaveHull<PointT> hull_calculator;
00134   CloudT::Ptr hull (new CloudT);
00135   hull_calculator.setInputCloud (cloud);
00136   hull_calculator.setAlpha (alpha);
00137   hull_calculator.reconstruct (*hull, polygons);
00138   
00139   dim = hull_calculator.getDimension ();
00140   return hull;
00141 }
00142 
00143 int
00144 main (int argc, char** argv)
00145 {
00146   print_info ("Filter a point cloud using the convex hull of another point "
00147               "cloud. For more information, use: %s -h\n", argv[0]);
00148 
00149   if (argc < 4)
00150   {
00151     printHelp (argc, argv);
00152     return (-1);
00153   }
00154 
00155   // Parse the command line arguments for .pcd files
00156   std::vector<int> p_file_indices;
00157   p_file_indices = parse_file_extension_argument (argc, argv, ".pcd");
00158   if (p_file_indices.size () != 3)
00159   {
00160     print_error ("Need at least three pcd files to continue.\n");
00161     return (-1);
00162   }
00163 
00164   // Command line parsing
00165   double alpha = default_alpha;
00166   parse_argument (argc, argv, "-alpha", alpha);
00167 
00168   CloudT::Ptr hull_cloud (new CloudT);
00169   CloudT::Ptr hull_points (new CloudT);
00170   CloudT::Ptr input_cloud (new CloudT);
00171   CloudT::Ptr output_cloud (new CloudT);
00172   std::vector<pcl::Vertices> hull_polygons;
00173   int dim = 0;  
00174 
00175   if (!loadCloud (argv[p_file_indices[0]], *hull_cloud))
00176     return (-1);
00177     
00178   if (!loadCloud (argv[p_file_indices[1]], *input_cloud))
00179     return (-1);
00180   
00181   hull_points = calculateHull (hull_polygons, dim, hull_cloud, alpha);
00182 
00183   cropToHull (output_cloud, input_cloud, hull_points, hull_polygons, dim);
00184   
00185 
00186   if (output_cloud->size ())
00187     saveCloud (argv[p_file_indices[2]], *output_cloud);
00188   else
00189     print_error ("No points passed crop.\n");
00190 
00191   return (0);
00192 }
00193 
00194 


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:23:20