-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.cpp
190 lines (159 loc) · 5.01 KB
/
demo.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
/*******************************************************************************
* Copyright (C) 2022 MINIEYE L4 Department. All Rights Reserved
*
******************************************************************************
*/
#include <iostream>
#include <unordered_map>
#include <string>
#include <stdint.h>
#define PCDIO_ENABLE_CONSOLE_LOG
#include "pcdio/pcdio.h"
/**
* example to convert between custom pointcloud and pcl::PCLPointCloud2
*/
struct KittiPoint {
float x = 0;
float y = 0;
float z = 0;
float intensity = 0;
};
using KittiPointCloud = std::vector<KittiPoint>;
/**
* Save kitti pointcloud to kitti bin file
*/
bool SaveKittiPointCloud(const std::string& filename,
const KittiPointCloud& pointcloud) {
std::ofstream of(filename, std::ios::binary);
if (!of) {
std::cerr << "[ERROR] fail to open file for writing: " << filename
<< std::endl;
return false;
}
auto ptr = reinterpret_cast<const char*>(pointcloud.data());
size_t size = pointcloud.size() * sizeof(KittiPoint);
if (0 == size) {
std::cout << "[WARN] 0 point to write: " << filename << "\n";
} else {
of.write(ptr, size);
}
of.close();
return true;
}
bool FromPCLPointCloud2(const pcl::PCLPointCloud2& pc2,
KittiPointCloud& pointcloud) {
std::unordered_map<std::string, pcl::PCLPointField> fields;
for (auto& f : pc2.fields) {
fields[f.name] = f;
}
// check x,y,z
std::string xyz_name[] = {"x", "y", "z"};
for (int i = 0; i < 3; i++) {
if (fields.end() == fields.find(xyz_name[i])) {
std::cerr << "[ERROR] not find field: " << xyz_name[i] << "\n";
return false;
}
}
auto ii = fields.find("intensity");
bool has_intensity = fields.end() != ii;
if (!has_intensity) {
std::cout << "[WARN] not find field: intensity\n";
}
bool intensity_uint8 = false;
if (ii->second.datatype == pcl::PCLPointField::UINT8) {
intensity_uint8 = true;
} else if (ii->second.datatype == pcl::PCLPointField::FLOAT32) {
intensity_uint8 = false;
} else {
std::cerr << "[ERROR] unsupport inensity datatype: " << ii->second.datatype
<< "\n";
return false;
}
size_t N = pc2.width * pc2.height;
pointcloud.clear();
pointcloud.resize(N);
// we assume [count = 1]
uint32_t ox = fields["x"].offset;
uint32_t oy = fields["y"].offset;
uint32_t oz = fields["z"].offset;
uint32_t oi = ii->second.offset;
for (int i = 0; i < N; i++) {
auto& p = pointcloud[i];
p.x = pc2.at<float>(i, ox);
p.y = pc2.at<float>(i, oy);
p.z = pc2.at<float>(i, oz);
if (intensity_uint8) {
p.intensity = static_cast<float>(pc2.at<uint8_t>(i, oi)) / UINT8_MAX;
} else {
p.intensity = pc2.at<float>(i, oi);
}
}
return true;
}
bool ToPCLPointCloud2(const KittiPointCloud& pointcloud,
pcl::PCLPointCloud2& pc2) {
pc2.height = 1;
pc2.width = pointcloud.size();
pc2.point_step = sizeof(KittiPoint);
pc2.row_step = pc2.point_step * pc2.width;
pc2.is_dense = 0;
pc2.fields.clear();
pcl::PCLPointField field;
field.name = "x";
field.count = 1;
field.datatype = pcl::PCLPointField::FLOAT32;
field.offset = 0;
pc2.fields.push_back(field);
field.name = "y";
field.offset += sizeof(float);
pc2.fields.push_back(field);
field.name = "z";
field.offset += sizeof(float);
pc2.fields.push_back(field);
field.name = "intensity";
field.offset += sizeof(float);
pc2.fields.push_back(field);
size_t size = pointcloud.size() * sizeof(KittiPoint);
pc2.data.resize(size);
memcpy(pc2.data.data(), pointcloud.data(), size);
return true;
}
int main(int argc, char const* argv[]) {
if (argc < 2) {
std::cerr << "[ERROR] no enough argument\n"
<< "usage: " << argv[0] << " <pcd_file_name>\n";
return 1;
}
pcl::PCLPointCloud2 cloud;
if (0 != pcl::io::loadPCDFile(argv[1], cloud)) {
std::cerr << "[ERROR] failed to read pcd file: " << argv[1] << "\n";
return 2;
}
std::cout << "\nwidth: " << cloud.width
<< "\nheight: " << cloud.height
<< "\npoint_step: " << cloud.point_step
<< "\nrow_step: " << cloud.row_step
<< "\nis_dense: " << static_cast<int>(cloud.is_dense)
<< "\nfields num: " << cloud.fields.size() << std::endl;
for (auto& f : cloud.fields) {
std::cout << f << "\n";
}
std::cout << "[INFO] try to save as new file\n";
if (0 != pcl::io::savePCDFile("new.pcd", cloud)) {
std::cerr << "[ERROR] failed to write new.pcd\n";
} else {
std::cout << "[INFO] success to write new.pcd\n";
}
KittiPointCloud kitti_point_cloud;
if (!FromPCLPointCloud2(cloud, kitti_point_cloud)) {
std::cerr << "[ERROR] failed to convert PCD to KITTI pointcloud\n";
return 0;
}
if (SaveKittiPointCloud("kitti.bin", kitti_point_cloud)) {
std::cout << "[INFO] save the PCD file to KITTI bin file: kitti.bin"
<< std::endl;
} else {
std::cerr << "[ERROR] failed to save KITTI point cloud" << std::endl;
}
return 0;
}