-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmain.cpp
299 lines (247 loc) · 8.51 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//
// Created by zzy on 3/14/18.
//
#include <omp.h>
#include <ctime>
#include <vector>
#include <string>
#include <dirent.h>
#include <algorithm>
#include <pcl/io/pcd_io.h>
#include <pcl/common/common_headers.h>
#include <sys/stat.h>
#include <pcl/io/boost.h>
#include <boost/program_options.hpp>
#include <fstream>
//namespace fs = std::experimental::filesystem::v1;
static std::vector<std::string> file_lists;
class CommandLineArgs
{
public:
CommandLineArgs(int argc, char **argv);
~CommandLineArgs(){}
bool process_command_line(
int argc,
char** argv);
std::string _bin_path;
std::string _pcd_path;
std::string _mode;
private:
CommandLineArgs(){}
boost::program_options::options_description *_desc;
};
CommandLineArgs::CommandLineArgs(int argc, char **argv)
{
_bin_path = "/home/kitti_velodyne_bin_to_pcd/bin/";
_pcd_path = "/home/kitti_velodyne_bin_to_pcd/pcd/";
_desc = new boost::program_options::options_description("Program Usage", 1024, 512);
_desc->add_options()
("help", "produce help message")
("b", boost::program_options::value<std::string>(&_bin_path)->required(), "bin file folder")
("p", boost::program_options::value<std::string>(&_pcd_path)->required(), "pcd file folder")
("m", boost::program_options::value<std::string>(&_mode)->required(), "mode - bin2pcd, pcd2bin")
;
process_command_line(argc, argv);
}
bool CommandLineArgs::process_command_line(
int argc,
char** argv)
{
try
{
boost::program_options::variables_map vm;
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, *_desc), vm);
if (vm.count("help"))
{
std::cout << *_desc << "\n";
return false;
}
// There must be an easy way to handle the relationship between the
// option "help" and "host"-"port"-"config"
// Yes, the magic is putting the po::notify after "help" option check
boost::program_options::notify(vm);
}
catch (std::exception &e)
{
std::cerr << "Error: " << e.what() << "\n";
return false;
}
catch (...)
{
std::cerr << "Unknown error!"
<< "\n";
return false;
}
return true;
}
void read_filelists(const std::string& dir_path,std::vector<std::string>& out_filelsits,std::string type)
{
struct dirent *ptr;
DIR *dir;
dir = opendir(dir_path.c_str());
out_filelsits.clear();
while ((ptr = readdir(dir)) != NULL){
std::string tmp_file = ptr->d_name;
if (tmp_file[0] == '.')continue;
if (type.size() <= 0){
out_filelsits.push_back(ptr->d_name);
}else{
if (tmp_file.size() < type.size())continue;
std::string tmp_cut_type = tmp_file.substr(tmp_file.size() - type.size(),type.size());
if (tmp_cut_type == type){
out_filelsits.push_back(ptr->d_name);
}
}
}
}
bool computePairNum(std::string pair1,std::string pair2)
{
return pair1 < pair2;
}
void sort_filelists(std::vector<std::string>& filists,std::string type)
{
if (filists.empty())return;
std::sort(filists.begin(),filists.end(),computePairNum);
}
void readKittiPclBinData(std::string &in_file, std::string& out_file)
{
// load point cloud
std::fstream input(in_file.c_str(), std::ios::in | std::ios::binary);
if(!input.good()){
std::cerr << "Could not read file: " << in_file << std::endl;
exit(EXIT_FAILURE);
}
input.seekg(0, std::ios::beg);
pcl::PointCloud<pcl::PointXYZI>::Ptr points (new pcl::PointCloud<pcl::PointXYZI>);
int i;
for (i=0; input.good() && !input.eof(); i++) {
pcl::PointXYZI point;
input.read((char *) &point.x, 3*sizeof(float));
input.read((char *) &point.intensity, sizeof(float));
points->push_back(point);
}
input.close();
std::cout << "Read KTTI point cloud with " << i << " points, writing to " << out_file << std::endl;
pcl::PCDWriter writer;
// Save DoN features
writer.write< pcl::PointXYZI > (out_file, *points, true);
}
void convertPCDtoBin(std::string &in_file, std::string& out_file)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile<pcl::PointXYZ>(in_file, *cloud) == -1) //* load the file
{
std::string err = "Couldn't read file " + in_file;
PCL_ERROR(err.c_str());
return;// (-1);
}
std::cout << "Loaded "
<< cloud->width * cloud->height
<< " data points from "
<< in_file
<< " with the following fields: "
<< std::endl;
int data_idx = 0;
std::ostringstream oss;
oss << pcl::PCDWriter::generateHeader(*cloud);// << "DATA binary\n";
oss.flush();
data_idx = static_cast<int>(oss.tellp());
std::vector<pcl::PCLPointField> fields;
std::vector<int> fields_sizes;
size_t fsize = 0;
size_t data_size = 0;
size_t nri = 0;
pcl::getFields (*cloud, fields);
// Compute the total size of the fields
for (const auto &field : fields)
{
if (field.name == "_")
{
continue;
}
int fs = field.count * pcl::getFieldSize (field.datatype);
fsize += fs;
fields_sizes.push_back (fs);
fields[nri++] = field;
}
fields.resize (nri);
data_size = cloud->points.size () * fsize;
const int memsize = cloud->points.size() * sizeof(float) * 4;
char *out = (char*)malloc( memsize);// 4 field x y z intensity
std::cout << "data_size size: " << data_size << std::endl;
// char buffer[100];
std::ofstream myFile (out_file.c_str(), std::ios::out | std::ios::binary);
for (size_t i = 0; i < cloud->points.size(); ++i)
{
int nrj = 0;
for (const auto &field : fields)
{
memcpy(out, reinterpret_cast<const char*> (&cloud->points[i]) + field.offset, fields_sizes[nrj++]);
//myFile.write (reinterpret_cast<const char*> (&cloud->points[i]) + field.offset, fields_sizes[nrj++]);
}
float intensity = 0;
memcpy(out, reinterpret_cast<const char*> (&intensity) , sizeof(intensity));
// myFile.write ( reinterpret_cast<const char*> (&intensity) , sizeof(intensity));
}
myFile.write(out, memsize);
myFile.close();
}
void updateRear(std::string &pathStr)
{
if (pathStr != "" && pathStr.back() != '/')
{
pathStr += "/";
}
}
int main(int argc, char **argv)
{
CommandLineArgs cmd_args(argc, argv);
// Create _outputFile folder if not exist
struct stat sb;
std::string folderPath = cmd_args._pcd_path;
if (! (stat(folderPath.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode)) )
{//...It is not a directory...
mkdir(folderPath.c_str(), 0755);
}
folderPath = cmd_args._bin_path;
if (! (stat(folderPath.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode)) )
{//...It is not a directory...
mkdir(folderPath.c_str(), 0755);
}
if(cmd_args._mode == "bin2pcd")
{
read_filelists( cmd_args._bin_path, file_lists, "bin" );
sort_filelists( file_lists, "bin" );
#pragma omp parallel num_threads(8)
#pragma omp parallel for
for (int i = 0; i < file_lists.size(); ++i)
{
std::string bin_file = cmd_args._bin_path + file_lists[i];
std::string tmp_str = file_lists[i].substr(0, file_lists[i].length() - 4) + ".pcd";
std::string pcd_file = cmd_args._pcd_path + tmp_str;
readKittiPclBinData( bin_file, pcd_file );
}
}
else if(cmd_args._mode == "pcd2bin")
{
read_filelists( cmd_args._pcd_path, file_lists, "pcd" );
sort_filelists( file_lists, "pcd" );
std::cout << "Run pcd2bin" << std::endl;
#pragma omp parallel num_threads(8)
#pragma omp parallel for
for (int i = 0; i < file_lists.size(); ++i)
{
std::string pcd_file = cmd_args._pcd_path + file_lists[i];
std::string tmp_str = file_lists[i].substr(0, file_lists[i].length() - 4) + ".bin";
std::string bin_file = cmd_args._bin_path + tmp_str;
std::cout << pcd_file << "\n"
<< bin_file << std::endl;
convertPCDtoBin( pcd_file, bin_file );
}
}
else
{
std::cout << "No mode provided" << std::endl;
}
return 0;
}