-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
183 lines (150 loc) · 5.83 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*********************************
HEADERS
**********************************/
#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/io/vtk_lib_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/console/print.h>
#include <pcl/console/parse.h>
#include <pcl/console/time.h>
#include <iostream>
#include <fstream>
#include <string>
void printUsage (const char* progName){
std::cout << "\nUsage: " << progName << " <file.ply> -o <output dir>" << std::endl;
}
using namespace std;
int main(int argc, char **argv){
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGB>());
pcl::PolygonMesh cl;
std::vector<int> filenames;
bool file_is_pcd = false;
bool file_is_ply = false;
bool file_is_txt = false;
bool file_is_xyz = false;
if(argc < 4 or argc > 4){
printUsage (argv[0]);
return -1;
}
pcl::console::TicToc tt;
pcl::console::print_highlight ("Loading ");
filenames = pcl::console::parse_file_extension_argument(argc, argv, ".ply");
if(filenames.size()<=0){
filenames = pcl::console::parse_file_extension_argument(argc, argv, ".pcd");
if(filenames.size()<=0){
filenames = pcl::console::parse_file_extension_argument(argc, argv, ".txt");
if(filenames.size()<=0){
filenames = pcl::console::parse_file_extension_argument(argc, argv, ".xyz");
if(filenames.size()<=0){
printUsage (argv[0]);
return -1;
}else if(filenames.size() == 1){
file_is_xyz = true;
}
}else if(filenames.size() == 1){
file_is_txt = true;
}
}else if(filenames.size() == 1){
file_is_pcd = true;
}
}
else if(filenames.size() == 1){
file_is_ply = true;
}else{
printUsage (argv[0]);
return -1;
}
std::string output_dir;
if(pcl::console::parse_argument(argc, argv, "-o", output_dir) == -1){
PCL_ERROR ("Need an output directory! Please use <input cloud> -o <output dir>\n");
return(-1);
}
if(file_is_pcd){
pcl::console::print_info("\nFound pcd file.\n");
return -1;
}else if(file_is_ply){
pcl::io::loadPLYFile(argv[filenames[0]],*cloud);
if(cloud->points.size()<=0 or cloud->points.at(0).x <=0 and cloud->points.at(0).y <=0 and cloud->points.at(0).z <=0){
pcl::console::print_warn("\nloadPLYFile could not read the cloud, attempting to loadPolygonFile...\n");
pcl::io::loadPolygonFile(argv[filenames[0]], cl);
pcl::fromPCLPointCloud2(cl.cloud, *cloud);
if(cloud->points.size()<=0 or cloud->points.at(0).x <=0 and cloud->points.at(0).y <=0 and cloud->points.at(0).z <=0){
pcl::console::print_warn("\nloadPolygonFile could not read the cloud, attempting to PLYReader...\n");
pcl::PLYReader plyRead;
plyRead.read(argv[filenames[0]],*cloud);
if(cloud->points.size()<=0 or cloud->points.at(0).x <=0 and cloud->points.at(0).y <=0 and cloud->points.at(0).z <=0){
pcl::console::print_error("\nError. ply file is not compatible.\n");
return -1;
}
}
}
pcl::console::print_info("\nFound ply file.\n");
pcl::console::print_info ("[done, ");
pcl::console::print_value ("%g", tt.toc ());
pcl::console::print_info (" ms : ");
pcl::console::print_value ("%d", cloud->size ());
pcl::console::print_info (" points]\n");
}else if(file_is_txt){
std::ifstream file(argv[filenames[0]]);
if(!file.is_open()){
std::cout << "Error: Could not find "<< argv[filenames[0]] << std::endl;
return -1;
}
std::cout << "file opened." << std::endl;
double x_,y_,z_;
unsigned int r, g, b;
while(file >> x_ >> y_ >> z_ >> r >> g >> b){
pcl::PointXYZRGB pt;
pt.x = x_;
pt.y = y_;
pt.z= z_;
uint8_t r_, g_, b_;
r_ = uint8_t(r);
g_ = uint8_t(g);
b_ = uint8_t(b);
uint32_t rgb_ = ((uint32_t)r_ << 16 | (uint32_t)g_ << 8 | (uint32_t)b_);
pt.rgb = *reinterpret_cast<float*>(&rgb_);
cloud->points.push_back(pt);
//std::cout << "pointXYZRGB:" << pt << std::endl;
}
pcl::console::print_info("\nFound txt file.\n");
pcl::console::print_info ("[done, ");
pcl::console::print_value ("%g", tt.toc ());
pcl::console::print_info (" ms : ");
pcl::console::print_value ("%d", cloud->points.size ());
pcl::console::print_info (" points]\n");
}else if(file_is_xyz){
std::ifstream file(argv[filenames[0]]);
if(!file.is_open()){
std::cout << "Error: Could not find "<< argv[filenames[0]] << std::endl;
return -1;
}
std::cout << "file opened." << std::endl;
double x_,y_,z_;
while(file >> x_ >> y_ >> z_){
pcl::PointXYZRGB pt;
pt.x = x_;
pt.y = y_;
pt.z= z_;
cloud->points.push_back(pt);
//std::cout << "pointXYZRGB:" << pt << std::endl;
}
pcl::console::print_info("\nFound xyz file.\n");
pcl::console::print_info ("[done, ");
pcl::console::print_value ("%g", tt.toc ());
pcl::console::print_info (" ms : ");
pcl::console::print_value ("%d", cloud->points.size ());
pcl::console::print_info (" points]\n");
}
cloud->width = (int) cloud->points.size ();
cloud->height = 1;
cloud->is_dense = true;
output_dir += "/cloudConverted.pcd";
std::string sav = "saved point cloud in:";
sav += output_dir;
pcl::console::print_info(sav.c_str());
std::cout << std::endl;
pcl::io::savePCDFileBinary(output_dir.c_str(),*cloud);
return 0;
}