00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
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
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
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