00001 00063 #include "cob_3d_mapping_tools/point_generator.h" 00064 #include <cob_3d_mapping_common/label_defines.h> 00065 00066 #include <boost/program_options.hpp> 00067 00068 #include <pcl/io/pcd_io.h> 00069 #include <pcl/point_cloud.h> 00070 #include <pcl/visualization/pcl_visualizer.h> 00071 #include <pcl/visualization/point_cloud_handlers.h> 00072 #include <pcl/kdtree/kdtree_flann.h> 00073 #include <pcl/filters/voxel_grid.h> 00074 00075 #include <math.h> 00076 00077 using namespace std; 00078 using namespace pcl; 00079 00080 typedef visualization::PointCloudColorHandlerRGBField<PointXYZRGBA> ColorHdlRGBA; 00081 typedef Eigen::Vector3f Vec; 00082 00083 string file; 00084 bool visualize; 00085 string folder; 00086 float noise; 00087 00088 void readOptions(int argc, char* argv[]) 00089 { 00090 using namespace boost::program_options; 00091 options_description options("Options"); 00092 options.add_options() 00093 ("help", "produce help message") 00094 ("out,o", value<string>(&file)->default_value(""), 00095 "output file, set to \"\" for no output") 00096 ("noise,n", value<float>(&noise)->default_value(0.00f), 00097 "gaussian noise level") 00098 ("vis,v", value<bool>(&visualize)->default_value(true), 00099 "enable visualization") 00100 ("folder,f", value<string>(&folder)->default_value(""), 00101 "save each form separated, ignores -o") 00102 ; 00103 00104 positional_options_description p_opt; 00105 p_opt.add("out", 1); 00106 variables_map vm; 00107 store(command_line_parser(argc, argv).options(options).positional(p_opt).run(), vm); 00108 notify(vm); 00109 00110 if (vm.count("help")) 00111 { 00112 cout << "creates a synthetic scene consisting of spheres and cylinders with various sizes. " 00113 << "The sizes are hardcoded." << endl; 00114 cout << options << endl; 00115 exit(0); 00116 } 00117 } 00118 00122 string fl2label(const float & f, const size_t & precision) 00123 { 00124 if (f == 0) 00125 return string(precision, '0'); 00126 00127 stringstream ss; 00128 ss << f; 00129 string s = ss.str(); 00130 s = s.substr(2,s.length()); 00131 if (precision > s.length()) 00132 s += string(precision - s.length(), '0'); 00133 return (s); 00134 } 00135 00139 int main(int argc, char** argv) 00140 { 00141 readOptions(argc, argv); 00142 00143 float step = 0.005f; 00144 float tmp[] = {0.02f, 0.03f, 0.04f, 0.05f, 0.06f, 0.07f, 0.08f, 0.09f, 0.10f, 00145 0.11f, 0.12f, 0.13f, 0.14f, 0.15f}; 00146 vector<float> radii(tmp, tmp + sizeof(tmp) / sizeof(float)); 00147 vector<PointCloud<PointXYZRGBA>::Ptr > pcs; 00148 vector<PointCloud<PointXYZRGBA>::Ptr > pcc; 00149 PointCloud<PointXYZRGBA>::Ptr pc_ptr; 00150 PointCloud<PointXYZRGBA>::Ptr scene(new PointCloud<PointXYZRGBA>); 00151 00152 for (size_t i=0; i<radii.size(); i++) 00153 { 00154 pc_ptr.reset(new PointCloud<PointXYZRGBA>); 00155 pcs.push_back(pc_ptr); 00156 pc_ptr.reset(new PointCloud<PointXYZRGBA>); 00157 pcc.push_back(pc_ptr); 00158 } 00159 00160 cob_3d_mapping_tools::PointGenerator<PointXYZRGBA> pg; 00161 if (noise != 0) pg.setGaussianNoise(noise); 00162 00163 Vec pos_s(-0.2f,0.0f,1.0f); 00164 Vec pos_c(0.2f,0.0f,1.0f); 00165 for (size_t i=0; i<radii.size(); i++) 00166 { 00167 pos_s = pos_s + Vec( -(radii[i] - radii[i-1]), 0.0f, radii[i] + 0.05f); 00168 pg.setOutputCloud(pcs[i]); 00169 pg.generateSphere(step, pos_s, -pos_s, radii[i], 0.45 * M_PI); 00170 *scene += *pcs[i]; 00171 00172 pos_c = pos_c + Vec(0.0f, 0.0f, radii[i] + 0.05f); 00173 pg.setOutputCloud(pcc[i]); 00174 Vec dir = pos_c.cross(Vec::UnitY()); 00175 dir = 2 * radii[i] * dir.normalized(); 00176 pg.generateCylinder(step, pos_c, -dir, -Vec::UnitY() * radii[i], M_PI); 00177 *scene += *pcc[i]; 00178 } 00179 pc_ptr.reset(new PointCloud<PointXYZRGBA>); 00180 pg.setOutputCloud(pc_ptr); 00181 pg.generateEdge(step, Vec(0.1f,0.0f,0.9f), -Vec::UnitX() * 0.2f, Vec::UnitZ() * 0.075f); 00182 *scene += *pc_ptr; 00183 00184 if (folder != "") 00185 { 00186 for (size_t i=0; i<radii.size(); i++) 00187 { 00188 io::savePCDFileASCII(folder + "cyl_" + fl2label(radii[i], 2) + "r.pcd", *pcc[i]); 00189 io::savePCDFileASCII(folder + "sph_" + fl2label(radii[i], 2) + "r.pcd", *pcs[i]); 00190 } 00191 io::savePCDFileASCII(folder + "edge_.pcd", *pc_ptr); 00192 cout << "Saved " << 2*radii.size() << " Point Clouds to: " << folder << endl; 00193 } 00194 else if (file != "") 00195 { 00196 io::savePCDFileASCII(file, *scene); 00197 cout << "Saved Point Cloud to: " << file << endl; 00198 } 00199 00200 if (visualize) 00201 { 00202 visualization::PCLVisualizer v; 00203 v.setBackgroundColor(255, 255, 255); 00204 v.addCoordinateSystem(0.05); 00205 ColorHdlRGBA hdl(scene); 00206 v.addPointCloud<PointXYZRGBA>(scene, hdl, "cloud"); 00207 00208 while(!v.wasStopped()) 00209 { 00210 v.spinOnce(100); 00211 usleep(100000); 00212 } 00213 } 00214 else 00215 { 00216 cout << "Visualization disabled." << endl; 00217 } 00218 return 0; 00219 00220 }