-
Notifications
You must be signed in to change notification settings - Fork 216
Expand file tree
/
Copy pathpoint_io.cpp
More file actions
353 lines (275 loc) 路 11.1 KB
/
Copy pathpoint_io.cpp
File metadata and controls
353 lines (275 loc) 路 11.1 KB
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include <filesystem>
#include "point_io.hpp"
namespace fs = std::filesystem;
std::string getVertexLine(std::ifstream &reader) {
std::string line;
// Skip comments
do {
std::getline(reader, line);
if (line.find("element") == 0)
return line;
else if (line.find("comment") == 0)
continue;
else if (line.find("obj_info") == 0)
continue;
else
throw std::runtime_error("Invalid PLY file");
} while (true);
}
size_t getVertexCount(const std::string &line) {
// Split line into tokens
std::vector<std::string> tokens;
std::istringstream iss(line);
std::string token;
while (std::getline(iss, token, ' '))
tokens.push_back(token);
if (tokens.size() < 3)
throw std::runtime_error("Invalid PLY file");
if (tokens[0] != "element" && tokens[1] != "vertex")
throw std::runtime_error("Invalid PLY file");
return std::stoi(tokens[2]);
}
PointSet *readPointSet(const std::string &filename) {
PointSet *r;
const fs::path p(filename);
if (p.extension().string() == ".ply") r = fastPlyReadPointSet(filename);
else if (p.extension().string() == ".bin") r = colmapReadPointSet(filename);
else r = pdalReadPointSet(filename);
return r;
}
PointSet *fastPlyReadPointSet(const std::string &filename) {
std::ifstream reader(filename, std::ios::binary);
if (!reader.is_open())
throw std::runtime_error("Cannot open file " + filename);
auto *r = new PointSet();
std::string line;
std::getline(reader, line);
line.erase(std::remove(line.begin(), line.end(), '\r'), line.end());
if (line != "ply")
throw std::runtime_error("Invalid PLY file (header does not start with ply)");
std::getline(reader, line);
line.erase(std::remove(line.begin(), line.end(), '\r'), line.end());
// We are reading an ascii ply
bool ascii = line == "format ascii 1.0";
const auto vertexLine = getVertexLine(reader);
const auto count = getVertexCount(vertexLine);
std::cout << "Reading " << count << " points" << std::endl;
checkHeader(reader, "x");
checkHeader(reader, "y");
checkHeader(reader, "z");
int c = 0;
bool hasViews = false;
bool hasNormals = false;
bool hasColors = false;
size_t redIdx = 0, greenIdx = 1, blueIdx = 2;
std::getline(reader, line);
line.erase(std::remove(line.begin(), line.end(), '\r'), line.end());
while (line != "end_header") {
if (hasHeader(line, "nx") || hasHeader(line, "normal_x") || hasHeader(line, "normalx")) hasNormals = true;
if (hasHeader(line, "red")) {
hasColors = true;
redIdx = c;
}
if (hasHeader(line, "green")) {
hasColors = true;
greenIdx = c;
}
if (hasHeader(line, "blue")) {
hasColors = true;
blueIdx = c;
}
if (hasHeader(line, "views")) hasViews = true;
if (c++ > 100) break;
std::getline(reader, line);
line.erase(std::remove(line.begin(), line.end(), '\r'), line.end());
}
size_t colorIdxMin = std::min<size_t>(std::min<size_t>(redIdx, greenIdx), blueIdx);
redIdx -= colorIdxMin;
greenIdx -= colorIdxMin;
blueIdx -= colorIdxMin;
if (redIdx + greenIdx + blueIdx != 3) throw std::runtime_error("red/green/blue properties need to be contiguous");
r->points.resize(count);
if (hasNormals) r->normals.resize(count);
if (hasColors) r->colors.resize(count);
if (hasViews) r->views.resize(count);
// if (hasNormals) std::cout << "N";
// if (hasColors) std::cout << "C";
// if (hasViews) std::cout << "V";
// std::cout << std::endl;
// Read points
if (ascii) {
uint16_t buf;
for (size_t i = 0; i < count; i++) {
reader >> r->points[i][0]
>> r->points[i][1]
>> r->points[i][2];
if (hasNormals) {
reader >> r->normals[i][0]
>> r->normals[i][1]
>> r->normals[i][2];
}
if (hasColors) {
reader >> buf;
r->colors[i][redIdx] = static_cast<uint8_t>(buf);
reader >> buf;
r->colors[i][greenIdx] = static_cast<uint8_t>(buf);
reader >> buf;
r->colors[i][blueIdx] = static_cast<uint8_t>(buf);
}
if (hasViews) {
reader >> buf;
r->views[i] = static_cast<uint8_t>(buf);
}
}
}
else {
// Read points
uint8_t color[3];
for (size_t i = 0; i < count; i++) {
reader.read(reinterpret_cast<char *>(r->points[i].data()), sizeof(float) * 3);
if (hasNormals) {
reader.read(reinterpret_cast<char *>(r->normals[i].data()), sizeof(float) * 3);
}
if (hasColors) {
reader.read(reinterpret_cast<char *>(&color), sizeof(uint8_t) * 3);
r->colors[i][redIdx] = color[0];
r->colors[i][greenIdx] = color[1];
r->colors[i][blueIdx] = color[2];
}
if (hasViews) {
reader.read(reinterpret_cast<char *>(&r->views[i]), sizeof(uint8_t));
}
}
}
// std::vector<size_t> classes(255, 0);
// for (size_t idx = 0; idx < count; idx++) {
// std::cout << r->points[idx][0] << " ";
// std::cout << r->points[idx][1] << " ";
// std::cout << r->points[idx][2] << " ";
// std::cout << std::to_string(r->colors[idx][0]) << " ";
// std::cout << std::to_string(r->colors[idx][1]) << " ";
// std::cout << std::to_string(r->colors[idx][2]) << " ";
// std::cout << std::endl;
// if (idx > 9) exit(1);
// }
// for (size_t i = 0; i < classes.size(); i++){
// std::cout << i << ": " << classes[i] << std::endl;
// }
// exit(1);
reader.close();
return r;
}
PointSet *pdalReadPointSet(const std::string &filename) {
#ifdef WITH_PDAL
pdal::StageFactory factory;
const std::string driver = pdal::StageFactory::inferReaderDriver(filename);
if (driver.empty()) {
throw std::runtime_error("Can't infer point cloud reader from " + filename);
}
auto *r = new PointSet();
pdal::Stage *s = factory.createStage(driver);
pdal::Options opts;
opts.add("filename", filename);
s->setOptions(opts);
auto *table = new pdal::PointTable();
std::cout << "Reading points from " << filename << std::endl;
s->prepare(*table);
const pdal::PointViewSet pvSet = s->execute(*table);
r->pointView = *pvSet.begin();
const pdal::PointViewPtr pView = r->pointView;
if (pView->empty()) {
throw std::runtime_error("No points could be fetched");
}
std::cout << "Number of points: " << pView->size() << std::endl;
const size_t count = pView->size();
const pdal::PointLayoutPtr layout(table->layout());
r->points.resize(count);
bool hasColors = false;
bool largeColors = false;
if (layout->hasDim(pdal::Dimension::Id::Green)) {
r->colors.resize(count);
hasColors = true;
for (pdal::PointId idx = 0; idx < count; ++idx) {
if (pView->getFieldAs<uint16_t>(pdal::Dimension::Id::Green, idx) > 255) {
largeColors = true;
break;
}
}
}
for (pdal::PointId idx = 0; idx < count; ++idx) {
auto p = pView->point(idx);
r->points[idx][0] = p.getFieldAs<float>(pdal::Dimension::Id::X);
r->points[idx][1] = p.getFieldAs<float>(pdal::Dimension::Id::Y);
r->points[idx][2] = p.getFieldAs<float>(pdal::Dimension::Id::Z);
if (hasColors) {
if (largeColors) {
r->colors[idx][0] = static_cast<uint8_t>((p.getFieldAs<double>(pdal::Dimension::Id::Red) / 65535.0) * 255.0);
r->colors[idx][1] = static_cast<uint8_t>((p.getFieldAs<double>(pdal::Dimension::Id::Green) / 65535.0) * 255.0);
r->colors[idx][2] = static_cast<uint8_t>((p.getFieldAs<double>(pdal::Dimension::Id::Blue) / 65535.0) * 255.0);
}
else {
r->colors[idx][0] = p.getFieldAs<uint8_t>(pdal::Dimension::Id::Red);
r->colors[idx][1] = p.getFieldAs<uint8_t>(pdal::Dimension::Id::Green);
r->colors[idx][2] = p.getFieldAs<uint8_t>(pdal::Dimension::Id::Blue);
}
}
}
// std::vector<std::size_t> classes (255, 0);
// for (size_t idx = 0; idx < count; idx++) {
// std::cout << r->points[idx][0] << " ";
// std::cout << r->points[idx][1] << " ";
// std::cout << r->points[idx][2] << " ";
// std::cout << std::to_string(r->colors[idx][0]) << " ";
// std::cout << std::to_string(r->colors[idx][1]) << " ";
// std::cout << std::to_string(r->colors[idx][2]) << " ";
// std::cout << std::endl;
// if (idx > 9) exit(1);
// }
// for (size_t i = 0; i < classes.size(); i++){
// std::cout << i << ": " << classes[i] << std::endl;
// }
// exit(1);
return r;
#else
fs::path p(filename);
throw std::runtime_error("Unsupported file extension " + p.extension().string() + ", build program with PDAL support for additional file types support.");
#endif
}
PointSet *colmapReadPointSet(const std::string &filename){
std::ifstream reader(filename, std::ios::binary);
if (!reader.is_open()) throw std::runtime_error("Cannot open " + filename);
auto *r = new PointSet();
size_t numPoints = readBinary<uint64_t>(reader);
std::cout << "Reading " << numPoints << " points" << std::endl;
r->points.resize(numPoints);
r->colors.resize(numPoints);
for (size_t i = 0; i < numPoints; i++){
readBinary<uint64_t>(reader); // point ID
r->points[i][0] = readBinary<double>(reader);
r->points[i][1] = readBinary<double>(reader);
r->points[i][2] = readBinary<double>(reader);
r->colors[i][0] = readBinary<uint8_t>(reader);
r->colors[i][1] = readBinary<uint8_t>(reader);
r->colors[i][2] = readBinary<uint8_t>(reader);
readBinary<double>(reader); // error
size_t trackLen = readBinary<uint64_t>(reader);
for (size_t j = 0; j < trackLen; j++){
readBinary<uint32_t>(reader); // imageId
readBinary<uint32_t>(reader); // point2D Idx
}
}
reader.close();
return r;
}
void checkHeader(std::ifstream &reader, const std::string &prop) {
std::string line;
std::getline(reader, line);
line.erase(std::remove(line.begin(), line.end(), '\r'), line.end());
if (line.substr(line.length() - prop.length(), prop.length()) != prop) {
throw std::runtime_error("Invalid PLY file (expected 'property * " + prop + "', but found '" + line + "')");
}
}
bool hasHeader(const std::string &line, const std::string &prop) {
//std::cout << line << " -> " << prop << " : " << line.substr(line.length() - prop.length(), prop.length()) << std::endl;
return line.substr(0, 8) == "property" && line.substr(line.length() - prop.length(), prop.length()) == prop;
}