ait-survey

passthrough_filter

Jun 28th, 2015
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.72 KB | None | 0 0
  1.  
  2. /*
  3.  * Software License Agreement (BSD License)
  4.  *
  5.  *  Point Cloud Library (PCL) - www.pointclouds.org
  6.  *  Copyright (c) 2010-2011, Willow Garage, Inc.
  7.  *
  8.  *  All rights reserved.
  9.  *
  10.  *  Redistribution and use in source and binary forms, with or without
  11.  *  modification, are permitted provided that the following conditions
  12.  *  are met:
  13.  *
  14.  *   * Redistributions of source code must retain the above copyright
  15.  *     notice, this list of conditions and the following disclaimer.
  16.  *   * Redistributions in binary form must reproduce the above
  17.  *     copyright notice, this list of conditions and the following
  18.  *     disclaimer in the documentation and/or other materials provided
  19.  *     with the distribution.
  20.  *   * Neither the name of the copyright holder(s) nor the names of its
  21.  *     contributors may be used to endorse or promote products derived
  22.  *     from this software without specific prior written permission.
  23.  *
  24.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25.  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26.  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27.  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28.  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29.  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30.  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32.  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33.  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34.  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35.  *  POSSIBILITY OF SUCH DAMAGE.
  36.  *
  37.  * $Id$
  38.  */
  39.  
  40. #include <pcl/PCLPointCloud2.h>
  41. #include <pcl/point_types.h>
  42. #include <pcl/io/pcd_io.h>
  43. #include <pcl/console/print.h>
  44. #include <pcl/console/parse.h>
  45. #include <pcl/console/time.h>
  46. #include <pcl/filters/passthrough.h>
  47.  
  48.  
  49. using namespace std;
  50. using namespace pcl;
  51. using namespace pcl::io;
  52. using namespace pcl::console;
  53.  
  54. float default_min = 0.0f,
  55.       default_max = 1.0f;
  56. bool default_inside = true;
  57. bool default_keep_organized = true;
  58. std::string default_field_name = "z";
  59.  
  60. void
  61. printHelp (int, char **argv)
  62. {
  63.   print_error ("Syntax is: %s input.pcd output.pcd <options>\n", argv[0]);
  64.   print_info ("  where options are:\n");
  65.   print_info ("                     -field X = the field of the point cloud we want to apply the filter to (default: ");
  66.   print_value ("%s", default_field_name.c_str ()); print_info (")\n");
  67.   print_info ("                     -min X = lower limit of the filter (default: ");
  68.   print_value ("%f", default_min); print_info (")\n");
  69.   print_info ("                     -max X = upper limit of the filter (default: ");
  70.   print_value ("%f", default_max); print_info (")\n");
  71.   print_info ("                     -inside X = keep the points inside the [min, max] interval or not (default: ");
  72.   print_value ("%d", default_inside); print_info (")\n");
  73.   print_info ("                     -keep 0/1 = keep the points organized (1) or not (default: ");
  74.   print_value ("%d", default_keep_organized); print_info (")\n");
  75. }
  76.  
  77. bool
  78. loadCloud (const std::string &filename, pcl::PCLPointCloud2 &cloud)
  79. {
  80.   TicToc tt;
  81.   print_highlight ("Loading "); print_value ("%s ", filename.c_str ());
  82.  
  83.   tt.tic ();
  84.   if (loadPCDFile (filename, cloud) < 0)
  85.     return (false);
  86.   print_info ("[done, "); print_value ("%g", tt.toc ()); print_info (" ms : "); print_value ("%d", cloud.width * cloud.height); print_info (" points]\n");
  87.   print_info ("Available dimensions: "); print_value ("%s\n", pcl::getFieldsList (cloud).c_str ());
  88.  
  89.   return (true);
  90. }
  91.  
  92. void
  93. compute (const pcl::PCLPointCloud2::ConstPtr &input, pcl::PCLPointCloud2 &output,
  94.          std::string field_name, float min, float max, bool inside, bool keep_organized)
  95. {
  96.   // Estimate
  97.   TicToc tt;
  98.   tt.tic ();
  99.  
  100.   print_highlight (stderr, "Computing ");
  101.  
  102.   PassThrough<pcl::PCLPointCloud2> passthrough_filter;
  103.   passthrough_filter.setInputCloud (input);
  104.   passthrough_filter.setFilterFieldName (field_name);
  105.   passthrough_filter.setFilterLimits (min, max);
  106.   //passthrough_filter.setFilterLimitsNegative (!inside);
  107.   passthrough_filter.setFilterLimitsNegative (false);
  108.   //passthrough_filter.setKeepOrganized (keep_organized);
  109.   passthrough_filter.setKeepOrganized (false);
  110.   passthrough_filter.filter (output);
  111.  
  112.   print_info ("[done, "); print_value ("%g", tt.toc ()); print_info (" ms : "); print_value ("%d", output.width * output.height); print_info (" points]\n");
  113. }
  114.  
  115. void
  116. saveCloud (const std::string &filename, const pcl::PCLPointCloud2 &output)
  117. {
  118.   TicToc tt;
  119.   tt.tic ();
  120.  
  121.   print_highlight ("Saving "); print_value ("%s ", filename.c_str ());
  122.  
  123.   PCDWriter w;
  124.   w.writeBinaryCompressed (filename, output);
  125.  
  126.   print_info ("[done, "); print_value ("%g", tt.toc ()); print_info (" ms : "); print_value ("%d", output.width * output.height); print_info (" points]\n");
  127. }
  128.  
  129. int
  130. batchProcess (const vector<string> &pcd_files, string &output_dir,
  131.               std::string field_name, float min, float max, bool inside, bool keep_organized)
  132. {
  133.   vector<string> st;
  134.   for (size_t i = 0; i < pcd_files.size (); ++i)
  135.   {
  136.     // Load the first file
  137.     pcl::PCLPointCloud2::Ptr cloud (new pcl::PCLPointCloud2);
  138.     if (!loadCloud (pcd_files[i], *cloud))
  139.       return (-1);
  140.  
  141.     // Perform the feature estimation
  142.     pcl::PCLPointCloud2 output;
  143.     compute (cloud, output, field_name, min, max, inside, keep_organized);
  144.  
  145.     // Prepare output file name
  146.     string filename = pcd_files[i];
  147.     boost::trim (filename);
  148.     boost::split (st, filename, boost::is_any_of ("/\\"), boost::token_compress_on);
  149.    
  150.     // Save into the second file
  151.     stringstream ss;
  152.     ss << output_dir << "/" << st.at (st.size () - 1);
  153.     saveCloud (ss.str (), output);
  154.   }
  155.   return (0);
  156. }
  157.  
  158.  
  159. /* ---[ */
  160. int
  161. main (int argc, char** argv)
  162. {
  163.   print_info ("Filter a point cloud using the pcl::PassThroughFilterEstimate. For more information, use: %s -h\n", argv[0]);
  164.  
  165.   if (argc < 3)
  166.   {
  167.     printHelp (argc, argv);
  168.     return (-1);
  169.   }
  170.  
  171.   bool batch_mode = false;
  172.  
  173.   // Command line parsing
  174.   float min = default_min, max = default_max;
  175.   bool inside = default_inside;
  176.   bool keep_organized = default_keep_organized;
  177.   std::string field_name = default_field_name;
  178.   parse_argument (argc, argv, "-min", min);
  179.   parse_argument (argc, argv, "-max", max);
  180.   parse_argument (argc, argv, "-inside", inside);
  181.   parse_argument (argc, argv, "-field", field_name);
  182.   parse_argument (argc, argv, "-keep", keep_organized);
  183.   string input_dir, output_dir;
  184.   if (parse_argument (argc, argv, "-input_dir", input_dir) != -1)
  185.   {
  186.     PCL_INFO ("Input directory given as %s. Batch process mode on.\n", input_dir.c_str ());
  187.     if (parse_argument (argc, argv, "-output_dir", output_dir) == -1)
  188.     {
  189.       PCL_ERROR ("Need an output directory! Please use -output_dir to continue.\n");
  190.       return (-1);
  191.     }
  192.  
  193.     // Both input dir and output dir given, switch into batch processing mode
  194.     batch_mode = true;
  195.   }
  196.  
  197.   if (!batch_mode)
  198.   {
  199.     // Parse the command line arguments for .pcd files
  200.     std::vector<int> p_file_indices;
  201.     p_file_indices = parse_file_extension_argument (argc, argv, ".pcd");
  202.     if (p_file_indices.size () != 2)
  203.     {
  204.       print_error ("Need one input PCD file and one output PCD file to continue.\n");
  205.       return (-1);
  206.     }
  207.  
  208.     // Load the first file
  209.     pcl::PCLPointCloud2::Ptr cloud (new pcl::PCLPointCloud2);
  210.     if (!loadCloud (argv[p_file_indices[0]], *cloud))
  211.       return (-1);
  212.  
  213.     // Perform the feature estimation
  214.     pcl::PCLPointCloud2 output;
  215.     compute (cloud, output, field_name, min, max, inside, keep_organized);
  216.  
  217.     // Save into the second file
  218.     saveCloud (argv[p_file_indices[1]], output);
  219.   }
  220.   else
  221.   {
  222.     if (input_dir != "" && boost::filesystem::exists (input_dir))
  223.     {
  224.       vector<string> pcd_files;
  225.       boost::filesystem::directory_iterator end_itr;
  226.       for (boost::filesystem::directory_iterator itr (input_dir); itr != end_itr; ++itr)
  227.       {
  228.         // Only add PCD files
  229.         if (!is_directory (itr->status ()) && boost::algorithm::to_upper_copy (boost::filesystem::extension (itr->path ())) == ".PCD" )
  230.         {
  231.           pcd_files.push_back (itr->path ().string ());
  232.           PCL_INFO ("[Batch processing mode] Added %s for processing.\n", itr->path ().string ().c_str ());
  233.         }
  234.       }
  235.       batchProcess (pcd_files, output_dir, field_name, min, max, inside, keep_organized);
  236.     }
  237.     else
  238.     {
  239.       PCL_ERROR ("Batch processing mode enabled, but invalid input directory (%s) given!\n", input_dir.c_str ());
  240.       return (-1);
  241.     }
  242.   }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment