1+ /* ********************************
2+ HEADERS
3+ **********************************/
4+ #include < pcl/io/pcd_io.h>
5+ #include < pcl/io/ply_io.h>
6+ #include < pcl/io/vtk_lib_io.h>
7+
8+ #include < pcl/visualization/pcl_visualizer.h>
9+
10+ #include < pcl/surface/bilateral_upsampling.h>
11+ #include < pcl/surface/mls.h>
12+
13+ #include < pcl/point_cloud.h>
14+ #include < pcl/kdtree/kdtree_flann.h>
15+ #include < pcl/kdtree/kdtree.h>
16+
17+ #include < pcl/search/search.h>
18+
19+ #include < pcl/features/feature.h>
20+ #include < pcl/point_types.h>
21+
22+ #include < pcl/common/transforms.h>
23+ #include < pcl/segmentation/sac_segmentation.h>
24+
25+ #include < pcl/console/print.h>
26+ #include < pcl/console/parse.h>
27+ #include < pcl/console/time.h>
28+
29+ #include < iostream>
30+ #include < fstream>
31+ #include < string>
32+
33+ void printUsage (const char * progName){
34+ std::cout << " \n Use: " << progName << " <file>" << std::endl <<
35+ " support: .pcd .ply .txt .xyz" << std::endl <<
36+ " [q] to exit" << std::endl;
37+ }
38+
39+
40+ int main (int argc, char **argv){
41+
42+ pcl::PointCloud<pcl::PointXYZRGB>::Ptr input_cloud (new pcl::PointCloud<pcl::PointXYZRGB>());
43+
44+ pcl::PolygonMesh cl;
45+ std::vector<int > filenames;
46+ bool file_is_pcd = false ;
47+ bool file_is_ply = false ;
48+ bool file_is_txt = false ;
49+ bool file_is_xyz = false ;
50+
51+
52+ if (argc < 2 or argc > 2 ){
53+ printUsage (argv[0 ]);
54+ return -1 ;
55+ }
56+
57+ pcl::console::TicToc tt;
58+ pcl::console::print_highlight (" Loading " );
59+
60+ filenames = pcl::console::parse_file_extension_argument (argc, argv, " .ply" );
61+ if (filenames.size ()<=0 ){
62+ filenames = pcl::console::parse_file_extension_argument (argc, argv, " .pcd" );
63+ if (filenames.size ()<=0 ){
64+ filenames = pcl::console::parse_file_extension_argument (argc, argv, " .txt" );
65+ if (filenames.size ()<=0 ){
66+ filenames = pcl::console::parse_file_extension_argument (argc, argv, " .xyz" );
67+ if (filenames.size ()<=0 ){
68+ printUsage (argv[0 ]);
69+ return -1 ;
70+ }else if (filenames.size () == 1 ){
71+ file_is_xyz = true ;
72+ }
73+ }else if (filenames.size () == 1 ){
74+ file_is_txt = true ;
75+ }
76+ }else if (filenames.size () == 1 ){
77+ file_is_pcd = true ;
78+ }
79+ }else if (filenames.size () == 1 ){
80+ file_is_ply = true ;
81+ }else {
82+ printUsage (argv[0 ]);
83+ return -1 ;
84+ }
85+
86+ if (file_is_pcd){
87+ if (pcl::io::loadPCDFile (argv[filenames[0 ]], *input_cloud) < 0 ){
88+ std::cout << " Error loading point cloud " << argv[filenames[0 ]] << " \n " ;
89+ printUsage (argv[0 ]);
90+ return -1 ;
91+ }
92+ pcl::console::print_info (" \n Found pcd file.\n " );
93+ pcl::console::print_info (" [done, " );
94+ pcl::console::print_value (" %g" , tt.toc ());
95+ pcl::console::print_info (" ms : " );
96+ pcl::console::print_value (" %d" , input_cloud->size ());
97+ pcl::console::print_info (" points]\n " );
98+ }else if (file_is_ply){
99+ pcl::io::loadPLYFile (argv[filenames[0 ]],*input_cloud);
100+ if (input_cloud->points .size ()<=0 or input_cloud->points .at (0 ).x <=0 and input_cloud->points .at (0 ).y <=0 and input_cloud->points .at (0 ).z <=0 ){
101+ pcl::console::print_warn (" \n loadPLYFile could not read the cloud, attempting to loadPolygonFile...\n " );
102+ pcl::io::loadPolygonFile (argv[filenames[0 ]], cl);
103+ pcl::fromPCLPointCloud2 (cl.cloud , *input_cloud);
104+ if (input_cloud->points .size ()<=0 or input_cloud->points .at (0 ).x <=0 and input_cloud->points .at (0 ).y <=0 and input_cloud->points .at (0 ).z <=0 ){
105+ pcl::console::print_warn (" \n loadPolygonFile could not read the cloud, attempting to PLYReader...\n " );
106+ pcl::PLYReader plyRead;
107+ plyRead.read (argv[filenames[0 ]],*input_cloud);
108+ if (input_cloud->points .size ()<=0 or input_cloud->points .at (0 ).x <=0 and input_cloud->points .at (0 ).y <=0 and input_cloud->points .at (0 ).z <=0 ){
109+ pcl::console::print_error (" \n Error. ply file is not compatible.\n " );
110+ return -1 ;
111+ }
112+ }
113+ }
114+
115+ pcl::console::print_info (" \n Found ply file.\n " );
116+ pcl::console::print_info (" [done, " );
117+ pcl::console::print_value (" %g" , tt.toc ());
118+ pcl::console::print_info (" ms : " );
119+ pcl::console::print_value (" %d" , input_cloud->points .size ());
120+ pcl::console::print_info (" points]\n " );
121+
122+ }else if (file_is_txt){
123+ std::ifstream file (argv[filenames[0 ]]);
124+ if (!file.is_open ()){
125+ std::cout << " Error: Could not find " << argv[filenames[0 ]] << std::endl;
126+ return -1 ;
127+ }
128+
129+ std::cout << " file opened." << std::endl;
130+ double x_,y_,z_;
131+ unsigned int r, g, b;
132+
133+ while (file >> x_ >> y_ >> z_ >> r >> g >> b){
134+ pcl::PointXYZRGB pt;
135+ pt.x = x_;
136+ pt.y = y_;
137+ pt.z = z_;
138+
139+ uint8_t r_, g_, b_;
140+ r_ = uint8_t (r);
141+ g_ = uint8_t (g);
142+ b_ = uint8_t (b);
143+
144+ uint32_t rgb_ = ((uint32_t )r_ << 16 | (uint32_t )g_ << 8 | (uint32_t )b_);
145+ pt.rgb = *reinterpret_cast <float *>(&rgb_);
146+
147+ input_cloud->points .push_back (pt);
148+ // std::cout << "pointXYZRGB:" << pt << std::endl;
149+ }
150+
151+ pcl::console::print_info (" \n Found txt file.\n " );
152+ pcl::console::print_info (" [done, " );
153+ pcl::console::print_value (" %g" , tt.toc ());
154+ pcl::console::print_info (" ms : " );
155+ pcl::console::print_value (" %d" , input_cloud->points .size ());
156+ pcl::console::print_info (" points]\n " );
157+
158+ }else if (file_is_xyz){
159+ std::ifstream file (argv[filenames[0 ]]);
160+ if (!file.is_open ()){
161+ std::cout << " Error: Could not find " << argv[filenames[0 ]] << std::endl;
162+ return -1 ;
163+ }
164+
165+ std::cout << " file opened." << std::endl;
166+ double x_,y_,z_;
167+
168+ while (file >> x_ >> y_ >> z_){
169+
170+ pcl::PointXYZRGB pt;
171+ pt.x = x_;
172+ pt.y = y_;
173+ pt.z = z_;
174+
175+ input_cloud->points .push_back (pt);
176+ // std::cout << "pointXYZRGB:" << pt << std::endl;
177+ }
178+
179+ pcl::console::print_info (" \n Found xyz file.\n " );
180+ pcl::console::print_info (" [done, " );
181+ pcl::console::print_value (" %g" , tt.toc ());
182+ pcl::console::print_info (" ms : " );
183+ pcl::console::print_value (" %d" , input_cloud->points .size ());
184+ pcl::console::print_info (" points]\n " );
185+ }
186+
187+ input_cloud->width = (int ) input_cloud->points .size ();
188+ input_cloud->height = 1 ;
189+ input_cloud->is_dense = true ;
190+
191+ pcl::PointCloud<pcl::PointXYZRGB>::Ptr output_cloud (new pcl::PointCloud<pcl::PointXYZRGB> ());
192+ pcl::PointCloud<pcl::PointXYZRGB>::Ptr dense_points (new pcl::PointCloud<pcl::PointXYZRGB> ());
193+
194+ pcl::search::KdTree<pcl::PointXYZRGB>::Ptr kd_tree (new pcl::search::KdTree<pcl::PointXYZRGB>);
195+ pcl::PointCloud<pcl::PointNormal>::Ptr mls_points (new pcl::PointCloud<pcl::PointNormal>());
196+ pcl::MovingLeastSquares<pcl::PointXYZRGB, pcl::PointXYZRGB> mls;
197+
198+ double search_radius = 0.03 ; // 0.03
199+ double sampling_radius = 0.005 ; // 0.005
200+ double step_size = 0.005 ; // 0.005
201+ double gauss_param = (double )std::pow (search_radius,2 );
202+ int pol_order = 2 ;
203+ unsigned int num_threats = 1 ;
204+
205+ mls.setComputeNormals (true );
206+ mls.setInputCloud (input_cloud);
207+ mls.setSearchMethod (kd_tree);
208+ mls.setSearchRadius (search_radius);
209+ mls.setUpsamplingMethod (pcl::MovingLeastSquares<pcl::PointXYZRGB, pcl::PointXYZRGB>::UpsamplingMethod::SAMPLE_LOCAL_PLANE);
210+ mls.setUpsamplingRadius (sampling_radius);
211+ mls.setUpsamplingStepSize (step_size);
212+ mls.setPolynomialOrder (pol_order);
213+ mls.setSqrGaussParam (gauss_param);// (the square of the search radius works best in general)
214+ mls.setCacheMLSResults (true );// Set whether the mls results should be stored for each point in the input cloud.
215+ mls.setNumberOfThreads (num_threats);
216+ // mls.setDilationVoxelSize();//Used only in the VOXEL_GRID_DILATION upsampling method
217+ // mls.setPointDensity(15); //15
218+ mls.process (*dense_points);
219+
220+ *output_cloud = *input_cloud;
221+ *output_cloud += *dense_points;
222+
223+ pcl::console::print_info (" \n New points: " );
224+ pcl::console::print_value (" %d" , dense_points->points .size ());
225+
226+ pcl::console::print_info (" \n Output cloud points: " );
227+ pcl::console::print_value (" %d" , output_cloud->points .size ());
228+ pcl::console::print_info (" \n " );
229+
230+ boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer (new pcl::visualization::PCLVisualizer (" VISUALIZER" ));
231+
232+ int PORT1 = 0 ;
233+ viewer->createViewPort (0.0 , 0.0 , 0.5 , 1.0 , PORT1);
234+ viewer->setBackgroundColor (0 , 0 , 0 , PORT1);
235+ viewer->addText (" ORIGINAL" , 10 , 10 , " PORT1" , PORT1);
236+
237+ int PORT2 = 0 ;
238+ viewer->createViewPort (0.5 , 0.0 , 1.0 , 1.0 , PORT2);
239+ viewer->setBackgroundColor (0 , 0 , 0 , PORT2);
240+ viewer->addText (" UPSAMPLING" , 10 , 10 , " PORT2" , PORT2);
241+
242+ viewer->removeAllPointClouds (0 );
243+
244+ if (input_cloud->points [0 ].r <= 0 and input_cloud->points [0 ].g <= 0 and input_cloud->points [0 ].b <= 0 ){
245+ pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZRGB> color_handler (input_cloud,255 ,255 ,0 );
246+ viewer->addPointCloud (input_cloud,color_handler," Original" ,PORT1);
247+ }else {
248+ viewer->addPointCloud (input_cloud," Original" ,PORT1);
249+ }
250+
251+ if (output_cloud->points [0 ].r <= 0 and output_cloud->points [0 ].g <= 0 and output_cloud->points [0 ].b <= 0 ){
252+ pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZRGB> color_handler (output_cloud,255 ,255 ,0 );
253+ viewer->addPointCloud (output_cloud,color_handler," transform1 rvec" ,PORT2);
254+ }else {
255+ viewer->addPointCloud (output_cloud," transform1 rvec" ,PORT2);
256+ }
257+
258+ pcl::io::savePCDFile (" upsampled_cloud.pcd" , *output_cloud);
259+
260+ pcl::PointXYZ p1, p2, p3;
261+ p1.getArray3fMap () << 1 , 0 , 0 ;
262+ p2.getArray3fMap () << 0 , 1 , 0 ;
263+ p3.getArray3fMap () << 0 ,0.1 ,1 ;
264+
265+ viewer->addCoordinateSystem (1 ," original_usc" ,PORT1);
266+ viewer->addText3D (" x" , p1, 0.2 , 1 , 0 , 0 , " x_" ,PORT1);
267+ viewer->addText3D (" y" , p2, 0.2 , 0 , 1 , 0 , " y_" ,PORT1);
268+ viewer->addText3D (" z" , p3,0.2 , 0 , 0 , 1 , " z_" ,PORT1);
269+
270+ viewer->addCoordinateSystem (1 ," transform_ucs" ,PORT2);
271+ viewer->addText3D (" x" , p1, 0.2 , 1 , 0 , 0 , " x_" ,PORT2);
272+ viewer->addText3D (" y" , p2, 0.2 , 0 , 1 , 0 , " y_" ,PORT2);
273+ viewer->addText3D (" z" , p3,0.2 , 0 , 0 , 1 , " z_" ,PORT2);
274+
275+ viewer->setPosition (0 ,0 );
276+ viewer->initCameraParameters ();
277+ viewer->resetCamera ();
278+
279+ std::cout << " \n Press [q] to exit" << std::endl;
280+
281+ while (!viewer->wasStopped ()) {
282+ viewer->spin ();
283+ }
284+
285+ return 0 ;
286+
287+ }
0 commit comments