Go to the documentation of this file.00001
00063 #include <boost/program_options.hpp>
00064
00065 #include <pcl/io/pcd_io.h>
00066 #include <pcl/io/ply_io.h>
00067 #include <pcl/surface/organized_fast_mesh.h>
00068 #include <pcl/PolygonMesh.h>
00069
00070 #include <cob_3d_mapping_filters/downsample_filter.h>
00071 #include <cob_3d_mapping_common/point_types.h>
00072 #include <cob_3d_mapping_common/stop_watch.h>
00073 #include <cob_3d_mapping_common/sensor_model.h>
00074 #include <cob_3d_features/organized_normal_estimation_omp.h>
00075 #include <cob_3d_segmentation/impl/fast_segmentation.hpp>
00076
00077 #include <cob_3d_meshing/mesh_types.h>
00078 #include <cob_3d_meshing/mesh_conversion.h>
00079 #include <cob_3d_meshing/mesh_decimation.h>
00080
00081
00082
00083 std::string file_in_;
00084 std::string file_out_;
00085 bool save_pcl_;
00086 int n_vertices_;
00087
00088
00089 int readOptions(int argc, char** argv)
00090 {
00091 using namespace boost::program_options;
00092 options_description options("Options");
00093 options.add_options()
00094 ("help,h", "produce help message")
00095 ("in,i", value<std::string>(&file_in_), "input folder with data points")
00096 ("out,o", value<std::string>(&file_out_), "out file with data points")
00097 ("n_vertices,n", value<int>(&n_vertices_)->default_value(10000),
00098 "decimate to n vertices")
00099 ("pcl", "save pcl results")
00100 ;
00101
00102 positional_options_description p_opt;
00103 p_opt.add("in", 1).add("out",1);
00104 variables_map vm;
00105 store(command_line_parser(argc, argv)
00106 .options(options).positional(p_opt).run(), vm);
00107 notify(vm);
00108
00109 if(vm.count("help") || argc == 1)
00110 { std::cout << options << std::endl; return(-1); }
00111 if(vm.count("pcl")) save_pcl_ = true;
00112 else save_pcl_ = false;
00113
00114 return 0;
00115 }
00116
00117
00118 int main(int argc, char** argv)
00119 {
00120 if(readOptions(argc, argv)<0) return 0;
00121
00122 pcl::PointCloud<pcl::PointXYZRGB>::Ptr
00123 input(new pcl::PointCloud<pcl::PointXYZRGB>);
00124 pcl::PointCloud<pcl::PointXYZRGB>::Ptr
00125 down(new pcl::PointCloud<pcl::PointXYZRGB>);
00126 pcl::PointCloud<pcl::Normal>::Ptr
00127 normals(new pcl::PointCloud<pcl::Normal>);
00128 pcl::PointCloud<PointLabel>::Ptr
00129 labels(new pcl::PointCloud<PointLabel>);
00130
00131 cob_3d_features::OrganizedNormalEstimationOMP
00132 <pcl::PointXYZRGB, pcl::Normal, PointLabel> one;
00133
00134 cob_3d_segmentation::FastSegmentation
00135 <pcl::PointXYZRGB, pcl::Normal, PointLabel> seg;
00136
00137 cob_3d_mapping_filters::DownsampleFilter
00138 <pcl::PointXYZRGB> dsf;
00139
00140 pcl::OrganizedFastMesh
00141 <pcl::PointXYZRGB> ofm;
00142
00143 pcl::PCDReader r;
00144 r.read(file_in_, *input);
00145
00146 PrecisionStopWatch t, tt;
00147
00148 tt.precisionStart();
00149 t.precisionStart();
00150 dsf.setInputCloud(input);
00151 dsf.filter(*down);
00152 std::cout << "DownsampleFilter took "
00153 << t.precisionStop() << "s." << std::endl;
00154
00155 t.precisionStart();
00156 one.setInputCloud(down);
00157 one.setOutputLabels(labels);
00158 one.setPixelSearchRadius(8,2,2);
00159 one.setSkipDistantPointThreshold(8);
00160 one.compute(*normals);
00161 std::cout << "OrganizedNormalEstimationOMP took "
00162 << t.precisionStop() << "s." << std::endl;
00163
00164 t.precisionStart();
00165 seg.setInputCloud(down);
00166 seg.setNormalCloudIn(normals);
00167 seg.setLabelCloudInOut(labels);
00168 seg.setSeedMethod(cob_3d_segmentation::SEED_RANDOM);
00169 seg.compute();
00170
00171 std::map<int,Eigen::Vector4f> params;
00172
00173 typedef typename cob_3d_segmentation::FastSegmentation
00174 <pcl::PointXYZRGB, pcl::Normal, PointLabel>::ClusterPtr ClusterPtr;
00175 for (ClusterPtr c = seg.clusters()->begin(); c!=seg.clusters()->end(); ++c)
00176 {
00177 if(c->size() < 5) {continue;}
00178 seg.clusters()->computeClusterComponents(c);
00179 float d = fabs(c->getCentroid().dot(c->pca_point_comp3));
00180 params[c->id()] = Eigen::Vector4f(c->pca_point_comp3(0),
00181 c->pca_point_comp3(1),
00182 c->pca_point_comp3(2),
00183 d);
00184 }
00185 std::cout << "FastSegmentation took "
00186 << t.precisionStop() << "s." << std::endl;
00187
00188 t.precisionStart();
00189 typedef cob_3d_meshing::MeshProperties::Normals<pcl::Normal> PropNormalT;
00190 typedef cob_3d_meshing::Mesh<PropNormalT> MeshT;
00191 PropNormalT prop_normal_hdl(normals);
00192 MeshT mesh( prop_normal_hdl );
00193 cob_3d_meshing::MeshConversion<>::fromPointCloud<pcl::PointXYZRGB,MeshT>(down,mesh);
00194 cob_3d_meshing::MeshDecimation<MeshT>::quadratic(&mesh, n_vertices_);
00195 std::cout << "MeshSimplification took "
00196 << t.precisionStop() << "s." << std::endl;
00197
00198 std::cout << "Total Process took "
00199 << tt.precisionStop() << "s." << std::endl;
00200
00201 pcl::PolygonMesh pcl_mesh;
00202 t.precisionStart();
00203 ofm.setInputCloud(down);
00204 ofm.setTriangulationType(pcl::OrganizedFastMesh<pcl::PointXYZRGB>::TRIANGLE_ADAPTIVE_CUT);
00205 ofm.reconstruct(pcl_mesh);
00206 std::cout << "OrganizedFastMesh took "
00207 << t.precisionStop() << "s." << std::endl;
00208
00209
00210 try
00211 {
00212 if(save_pcl_) pcl::io::savePLYFile(file_out_, pcl_mesh);
00213 else
00214 {
00215 if ( !mesh.savePLY(file_out_) )
00216 {
00217 std::cerr << "Cannot write mesh to file " << file_out_ << std::endl;
00218 return 1;
00219 }
00220 }
00221 }
00222 catch( std::exception& x )
00223 {
00224 std::cerr << x.what() << std::endl;
00225 return 1;
00226 }
00227
00228 return 0;
00229 }