-
Notifications
You must be signed in to change notification settings - Fork 4
/
ply.hpp
398 lines (288 loc) · 11.2 KB
/
ply.hpp
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#pragma once
#include <iostream>
#include <filesystem>
#include <vector>
#include "FPCFilter.h"
namespace FPCFilter {
class PlyPoint {
public:
float x;
float y;
float z;
uint8_t red;
uint8_t blue;
uint8_t green;
uint8_t views;
PlyPoint(float x, float y, float z, uint8_t red, uint8_t green, uint8_t blue, uint8_t views) : x(x), y(y), z(z), red(red), green(green), blue(blue), views(views) {}
};
class PlyExtra {
public:
float nx;
float ny;
float nz;
PlyExtra(float nx, float ny, float nz) : nx(nx), ny(ny), nz(nz) {}
};
class PlyFile {
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
throw std::invalid_argument("Invalid PLY file");
} while (true);
}
int 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::invalid_argument("Invalid PLY file");
if (tokens[0] != "element" && tokens[1] != "vertex")
throw std::invalid_argument("Invalid PLY file");
return std::stoi(tokens[2]);
}
public:
std::vector<PlyExtra> extras;
std::vector<PlyPoint> points;
bool hasNormals() {
return !extras.empty();
}
PlyFile(const std::string& path, const std::function<bool(const float x, const float y, const float z)> filter = nullptr) {
std::ifstream reader(path);
if (!reader.is_open())
throw std::invalid_argument(std::string("Cannot open file ") + path);
std::string line;
std::getline(reader, line);
if (line != "ply")
throw std::invalid_argument("Invalid PLY file");
std::getline(reader, line);
// We are reading an ascii ply
if (line == "format ascii 1.0") {
/*
Expected header:
element vertex 62217
property float x
property float y
property float z
property uchar diffuse_red
property uchar diffuse_green
property uchar diffuse_blue
property uchar views
end_header
*/
const auto vertexLine = getVertexLine(reader);
const auto count = getVertexCount(vertexLine);
std::getline(reader, line);
if (line != "property float x")
throw std::invalid_argument("Invalid PLY file (expected 'property float x')");
std::getline(reader, line);
if (line != "property float y")
throw std::invalid_argument("Invalid PLY file (expected 'property float y')");
std::getline(reader, line);
if (line != "property float z")
throw std::invalid_argument("Invalid PLY file (expected 'property float z')");
std::getline(reader, line);
if (line != "property uchar diffuse_red")
throw std::invalid_argument("Invalid PLY file (expected 'property uchar diffuse_red')");
std::getline(reader, line);
if (line != "property uchar diffuse_green")
throw std::invalid_argument("Invalid PLY file (expected 'property uchar diffuse_green')");
std::getline(reader, line);
if (line != "property uchar diffuse_blue")
throw std::invalid_argument("Invalid PLY file (expected 'property uchar diffuse_blue')");
std::getline(reader, line);
if (line != "property uchar views")
throw std::invalid_argument("Invalid PLY file (expected 'property uchar views')");
std::getline(reader, line);
if (line != "end_header")
throw std::invalid_argument("Invalid PLY file (expected 'end_header')");
points.reserve(count);
if (filter) {
// Read points
for (auto i = 0; i < count; i++) {
float x, y, z;
int red, green, blue;
int views;
reader >> x >> y >> z >> red >> green >> blue >> views;
if (filter(x, y, z))
points.emplace_back(x, y, z, red, green, blue, views);
}
}
else {
// Read points
for (auto i = 0; i < count; i++) {
float x, y, z;
int red, green, blue;
int views;
reader >> x >> y >> z >> red >> green >> blue >> views;
points.emplace_back(x, y, z, red, green, blue, views);
}
}
// Otherwise it's a binary ply
} else if (line == "format binary_little_endian 1.0") {
/*
Expected header:
element vertex 2751907
property float32 x
property float32 y
property float32 z
property uint8 red
property uint8 green
property uint8 blue
property float32 nx
property float32 ny
property float32 nz
property uint8 views
end_header
*/
const auto vertexLine = getVertexLine(reader);
const auto count = getVertexCount(vertexLine);
std::getline(reader, line);
if (line != "property float32 x")
throw std::invalid_argument("Invalid PLY file (expected 'property float32 x')");
std::getline(reader, line);
if (line != "property float32 y")
throw std::invalid_argument("Invalid PLY file (expected 'property float32 y')");
std::getline(reader, line);
if (line != "property float32 z")
throw std::invalid_argument("Invalid PLY file (expected 'property float32 z')");
std::getline(reader, line);
if (line != "property uint8 red")
throw std::invalid_argument("Invalid PLY file (expected 'property uint8 red')");
std::getline(reader, line);
if (line != "property uint8 green")
throw std::invalid_argument("Invalid PLY file (expected 'property uint8 green')");
std::getline(reader, line);
if (line != "property uint8 blue")
throw std::invalid_argument("Invalid PLY file (expected 'property uint8 blue')");
std::getline(reader, line);
if (line != "property float32 nx")
throw std::invalid_argument("Invalid PLY file (expected 'property float32 nx')");
std::getline(reader, line);
if (line != "property float32 ny")
throw std::invalid_argument("Invalid PLY file (expected 'property float32 ny')");
std::getline(reader, line);
if (line != "property float32 nz")
throw std::invalid_argument("Invalid PLY file (expected 'property float32 nz')");
std::getline(reader, line);
if (line != "property uint8 views")
throw std::invalid_argument("Invalid PLY file (expected 'property uint8 views')");
std::getline(reader, line);
if (line != "end_header")
throw std::invalid_argument("Invalid PLY file (expected 'end_header')");
reader = std::ifstream(path, std::ifstream::binary);
for (auto n = 0; n < 14; n++)
std::getline(reader, line);
points.reserve(count);
extras.reserve(count);
if (filter) {
// Read points
for (auto i = 0; i < count; i++) {
float x, y, z;
float nx, ny, nz;
uint8_t red, green, blue;
uint8_t views;
reader.read(reinterpret_cast<char*>(&x), sizeof(float));
reader.read(reinterpret_cast<char*>(&y), sizeof(float));
reader.read(reinterpret_cast<char*>(&z), sizeof(float));
reader.read(reinterpret_cast<char*>(&red), sizeof(uint8_t));
reader.read(reinterpret_cast<char*>(&green), sizeof(uint8_t));
reader.read(reinterpret_cast<char*>(&blue), sizeof(uint8_t));
reader.read(reinterpret_cast<char*>(&nx), sizeof(float));
reader.read(reinterpret_cast<char*>(&ny), sizeof(float));
reader.read(reinterpret_cast<char*>(&nz), sizeof(float));
reader.read(reinterpret_cast<char*>(&views), sizeof(uint8_t));
if (filter(x, y, z)) {
points.emplace_back(x, y, z, red, green, blue, views);
extras.emplace_back(nx, ny, nz);
}
}
}
else {
// Read points
for (auto i = 0; i < count; i++) {
float x, y, z;
float nx, ny, nz;
uint8_t red, green, blue;
uint8_t views;
reader.read(reinterpret_cast<char*>(&x), sizeof(float));
reader.read(reinterpret_cast<char*>(&y), sizeof(float));
reader.read(reinterpret_cast<char*>(&z), sizeof(float));
reader.read(reinterpret_cast<char*>(&red), sizeof(uint8_t));
reader.read(reinterpret_cast<char*>(&green), sizeof(uint8_t));
reader.read(reinterpret_cast<char*>(&blue), sizeof(uint8_t));
reader.read(reinterpret_cast<char*>(&nx), sizeof(float));
reader.read(reinterpret_cast<char*>(&ny), sizeof(float));
reader.read(reinterpret_cast<char*>(&nz), sizeof(float));
reader.read(reinterpret_cast<char*>(&views), sizeof(uint8_t));
points.emplace_back(x, y, z, red, green, blue, views);
extras.emplace_back(nx, ny, nz);
}
}
} else
throw std::invalid_argument("Invalid PLY file");
reader.close();
}
void write(std::ostream& o) {
const auto cnt = this->points.size();
o << "ply" << std::endl;
o << "format binary_little_endian 1.0" << std::endl;
o << "comment Generated by FPCFilter v" << FPCFilter_VERSION_MAJOR << "." << FPCFilter_VERSION_MINOR << std::endl;
o << "element vertex " << cnt << std::endl;
o << "property float x" << std::endl;
o << "property float y" << std::endl;
o << "property float z" << std::endl;
const auto hasNormals = this->hasNormals();
if (hasNormals)
{
o << "property float nx" << std::endl;
o << "property float ny" << std::endl;
o << "property float nz" << std::endl;
}
o << "property uchar red" << std::endl;
o << "property uchar blue" << std::endl;
o << "property uchar green" << std::endl;
o << "property uchar views" << std::endl;
o << "end_header" << std::endl;
if (hasNormals)
{
for (auto n = 0; n < cnt; n++)
{
const auto point = this->points[n];
const auto extra = this->extras[n];
o.write(reinterpret_cast<const char*>(&point.x), sizeof(float));
o.write(reinterpret_cast<const char*>(&point.y), sizeof(float));
o.write(reinterpret_cast<const char*>(&point.z), sizeof(float));
o.write(reinterpret_cast<const char*>(&extra.nx), sizeof(float));
o.write(reinterpret_cast<const char*>(&extra.ny), sizeof(float));
o.write(reinterpret_cast<const char*>(&extra.nz), sizeof(float));
o.write(reinterpret_cast<const char*>(&point.red), sizeof(uint8_t));
o.write(reinterpret_cast<const char*>(&point.blue), sizeof(uint8_t));
o.write(reinterpret_cast<const char*>(&point.green), sizeof(uint8_t));
o.write(reinterpret_cast<const char*>(&point.views), sizeof(uint8_t));
}
} else
{
for (auto n = 0; n < cnt; n++)
{
const auto point = this->points[n];
o.write(reinterpret_cast<const char*>(&point.x), sizeof(float));
o.write(reinterpret_cast<const char*>(&point.y), sizeof(float));
o.write(reinterpret_cast<const char*>(&point.z), sizeof(float));
o.write(reinterpret_cast<const char*>(&point.red), sizeof(uint8_t));
o.write(reinterpret_cast<const char*>(&point.blue), sizeof(uint8_t));
o.write(reinterpret_cast<const char*>(&point.green), sizeof(uint8_t));
o.write(reinterpret_cast<const char*>(&point.views), sizeof(uint8_t));
}
}
}
};
}