-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpointcloudio.h
667 lines (592 loc) · 22.9 KB
/
pointcloudio.h
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
#ifndef POINTCLOUDLOADER_H
#define POINTCLOUDLOADER_H
#include <stdio.h>
#include <locale.h>
#include <fstream>
#include <sstream>
#include <iostream>
#include <QObject>
#include "pointcloud.h"
class PointCloudIO : public QObject
{
Q_OBJECT
public:
void saveGeometry(const Geometry *geometry, const std::string &filename)
{
FILE *fp = fopen(filename.c_str(), "wb");
if (fp == NULL)
throw "Could not open file: " + filename;
emit save(QString("circles"));
size_t numCircles = geometry->numCircles();
fwrite(&numCircles, sizeof(size_t), 1, fp);
for (size_t i = 0; i < numCircles; i++)
{
if (i % 100 == 0)
{
emit saveProgress(i / (float)numCircles);
}
const Circle *circle = geometry->circle(i);
Eigen::Vector3f color = circle->color();
Eigen::Vector2f center = circle->center();
float radius = circle->radius();
fwrite(color.data(), sizeof(float), 3, fp);
fwrite(center.data(), sizeof(float), 2, fp);
fwrite(&radius, sizeof(float), 1, fp);
const std::vector<size_t> &inliers = circle->inliers();
size_t numInliers = inliers.size();
fwrite(&numInliers, sizeof(size_t), 1, fp);
fwrite(inliers.data(), sizeof(size_t), numInliers, fp);
}
emit save(QString("planes"));
size_t numPlanes = geometry->numPlanes();
fwrite(&numPlanes, sizeof(size_t), 1, fp);
for (size_t i = 0; i < numPlanes; i++)
{
if (i % 100 == 0)
{
emit saveProgress(i / (float)numPlanes);
}
const Plane *plane = geometry->plane(i);
Eigen::Vector3f color = plane->color();
Eigen::Vector3f center = plane->center();
Eigen::Vector3f normal = plane->normal();
Eigen::Vector3f basisU = plane->basisU();
Eigen::Vector3f basisV = plane->basisV();
fwrite(color.data(), sizeof(float), 3, fp);
fwrite(center.data(), sizeof(float), 3, fp);
fwrite(normal.data(), sizeof(float), 3, fp);
fwrite(basisU.data(), sizeof(float), 3, fp);
fwrite(basisV.data(), sizeof(float), 3, fp);
const std::vector<size_t> &inliers = plane->inliers();
size_t numInliers = inliers.size();
fwrite(&numInliers, sizeof(size_t), 1, fp);
fwrite(inliers.data(), sizeof(size_t), numInliers, fp);
}
emit save(QString("cylinders"));
size_t numCylinders = geometry->numCylinders();
fwrite(&numCylinders, sizeof(size_t), 1, fp);
for (size_t i = 0; i < numCylinders; i++)
{
if (i % 100 == 0)
{
emit saveProgress(i / (float)numCylinders);
}
const Cylinder *cylinder = geometry->cylinder(i);
Eigen::Vector3f color = cylinder->color();
Eigen::Vector3f center = cylinder->center();
Eigen::Vector3f axis = cylinder->axis();
float radius = cylinder->radius();
float height = cylinder->height();
fwrite(color.data(), sizeof(float), 3, fp);
fwrite(center.data(), sizeof(float), 3, fp);
fwrite(axis.data(), sizeof(float), 3, fp);
fwrite(&radius, sizeof(float), 1, fp);
fwrite(&height, sizeof(float), 1, fp);
const std::vector<size_t> &inliers = cylinder->inliers();
size_t numInliers = inliers.size();
fwrite(&numInliers, sizeof(size_t), 1, fp);
fwrite(inliers.data(), sizeof(size_t), numInliers, fp);
}
fclose(fp);
}
void saveConnectivity(const ConnectivityGraph *connectivity, const std::string &filename)
{
FILE *fp = fopen(filename.c_str(), "wb");
if (fp == NULL)
throw "Could not open file: " + filename;
emit save(QString("connectivity"));
size_t size = connectivity->numPoints();
for (size_t i = 0; i < size; i++)
{
if (i % 1000 == 0)
{
emit saveProgress(i / (float)size);
}
size_t numNeighbors = connectivity->neighbors(i).size();
fwrite(&numNeighbors, sizeof(size_t), 1, fp);
std::vector<size_t> edges;
std::vector<float> distances;
for (const size_t &p : connectivity->neighbors(i))
{
edges.push_back(p);
distances.push_back(0);
}
fwrite(edges.data(), sizeof(size_t), numNeighbors, fp);
fwrite(distances.data(), sizeof(float), numNeighbors, fp);
}
fwrite(connectivity->groupIndices().data(), sizeof(size_t), size, fp);
fclose(fp);
}
template <size_t DIMENSION>
void saveAsPCL(const PointCloud<DIMENSION> *pointCloud, const std::string &filename)
{
FILE *fp = fopen(filename.c_str(), "wb");
if (fp == NULL)
throw "Could not open file: " + filename;
size_t size = pointCloud->size();
fwrite(&size, sizeof(size_t), 1, fp);
size_t mode = static_cast<size_t>(pointCloud->mode());
fwrite(&mode, sizeof(size_t), 1, fp);
for (size_t i = 0; i < size; i++)
{
if (i % 1000 == 0)
{
emit saveProgress(i / (float)size);
}
const Point<DIMENSION> &point = pointCloud->at(i);
float intensity = point.intensity();
float normalConfidence = point.normalConfidence();
float curvature = point.curvature();
fwrite(point.position().data(), sizeof(float), DIMENSION, fp);
if (pointCloud->hasMode(PointCloud<DIMENSION>::Mode::COLOR))
fwrite(point.color().data(), sizeof(float), DIMENSION, fp);
if (pointCloud->hasMode(PointCloud<DIMENSION>::Mode::INTENSITY))
fwrite(&intensity, sizeof(float), 1, fp);
if (pointCloud->hasMode(PointCloud<DIMENSION>::Mode::NORMAL))
fwrite(point.normal().data(), sizeof(float), DIMENSION, fp);
if (pointCloud->hasMode(PointCloud<DIMENSION>::Mode::NORMAL_CONFIDENCE))
fwrite(&normalConfidence, sizeof(float), 1, fp);
if (pointCloud->hasMode(PointCloud<DIMENSION>::Mode::CURVATURE))
fwrite(&curvature, sizeof(float), 1, fp);
}
fclose(fp);
if (pointCloud->hasConnectivity())
{
std::string connectivityFilename = filename.substr(0, filename.find_last_of('.')) + ".con";
saveConnectivity(pointCloud->connectivity(), connectivityFilename);
}
std::string geometryFilename = filename.substr(0, filename.find_last_of('.')) + ".geo";
saveGeometry(pointCloud->geometry(), geometryFilename);
}
void saveAsPoints(const PointCloud3d *pointCloud, const std::string &filename)
{
FILE *fp = fopen(filename.c_str(), "wb");
if (fp == NULL)
throw "Could not open file: " + filename;
int size = static_cast<int>(pointCloud->size());
fwrite(&size, sizeof(int), 1, fp);
emit save(QString("positions"));
for (int i = 0; i < size; i++)
{
if (i % 1000 == 0)
{
emit saveProgress(i / (float)size);
}
fwrite(pointCloud->at(i).position().data(), sizeof(float), 3, fp);
}
emit save(QString("normals"));
for (int i = 0; i < size; i++)
{
if (i % 1000 == 0)
{
emit saveProgress(i / (float)size);
}
fwrite(pointCloud->at(i).normal().data(), sizeof(float), 3, fp);
}
fclose(fp);
}
void saveAsXYZ(const PointCloud3d *pointCloud, const std::string &filename)
{
FILE *fp = fopen(filename.c_str(), "wb");
if (fp == NULL)
throw "Could not open file: " + filename;
for (size_t i = 0; i < pointCloud->size(); i++)
{
if (i % 1000 == 0)
{
emit saveProgress(i / (float)pointCloud->size());
}
Eigen::Vector3f position = pointCloud->at(i).position();
fprintf(fp, "%f %f %f", position.x(), position.y(), position.z());
if (pointCloud->hasMode(PointCloud3d::Mode::COLOR))
{
Eigen::Vector3f color = pointCloud->at(i).color();
fprintf(fp, " %d %d %d", (int)(255 * color.x()), (int)(255 * color.y()), (int)(255 * color.z()));
}
fprintf(fp, "\n");
}
fclose(fp);
}
void saveAsPTX(const PointCloud3d *pointCloud, const std::string &filename)
{
FILE *fp = fopen(filename.c_str(), "wb");
if (fp == NULL)
throw "Could not open file: " + filename;
for (size_t i = 0; i < pointCloud->size(); i++)
{
if (i % 1000 == 0)
{
emit saveProgress(i / (float)pointCloud->size());
}
Eigen::Vector3f position = pointCloud->at(i).position();
fprintf(fp, "%f %f %f", position.x(), -position.z(), position.y());
if (pointCloud->hasMode(PointCloud3d::Mode::INTENSITY))
{
fprintf(fp, "%f", pointCloud->at(i).intensity());
}
else
{
fprintf(fp, "%f", 0.0f);
}
if (pointCloud->hasMode(PointCloud3d::Mode::COLOR))
{
Eigen::Vector3f color = pointCloud->at(i).color();
fprintf(fp, " %d %d %d", (int)(255 * color.x()), (int)(255 * color.y()), (int)(255 * color.z()));
}
fprintf(fp, "\n");
}
fclose(fp);
}
void save(const PointCloud3d *pointCloud, const std::string &filename)
{
std::string extension = filename.substr(filename.find_last_of('.') + 1);
if (extension == "pcl")
{
PointCloudIO::saveAsPCL(pointCloud, filename);
}
else if (extension == "points")
{
PointCloudIO::saveAsPoints(pointCloud, filename);
}
else if (extension == "xyz")
{
PointCloudIO::saveAsXYZ(pointCloud, filename);
}
else if (extension == "ptx")
{
PointCloudIO::saveAsPTX(pointCloud, filename);
}
else
{
throw "Extension not supported: " + extension;
}
}
Geometry* loadGeometry(const std::string &filename)
{
FILE *fp = fopen(filename.c_str(), "rb");
if (fp == NULL) return NULL;
Geometry *geometry = new Geometry;
emit load(QString("circles"));
size_t numCircles;
fread(&numCircles, sizeof(size_t), 1, fp);
for (size_t i = 0; i < numCircles; i++)
{
if (i % 100 == 0)
{
emit loadProgress(i / (float)numCircles);
}
float color[3];
float center[2];
float radius;
fread(color, sizeof(float), 3, fp);
fread(center, sizeof(float), 2, fp);
fread(&radius, sizeof(float), 1, fp);
Circle *circle = new Circle(Eigen::Vector2f(center), radius);
circle->color(Eigen::Vector3f(color));
size_t numInliers;
fread(&numInliers, sizeof(size_t), 1, fp);
std::vector<size_t> inliers(numInliers);
fread(inliers.data(), sizeof(size_t), numInliers, fp);
circle->inliers(inliers);
geometry->addCircle(circle);
}
emit load(QString("planes"));
size_t numPlanes;
fread(&numPlanes, sizeof(size_t), 1, fp);
std::cout << "Planes=" << numPlanes << std::endl;
for (size_t i = 0; i < numPlanes; i++)
{
if (i % 100 == 0)
{
emit loadProgress(i / (float)numPlanes);
}
float color[3];
float center[3];
float normal[3];
float basisU[3];
float basisV[3];
fread(color, sizeof(float), 3, fp);
fread(center, sizeof(float), 3, fp);
fread(normal, sizeof(float), 3, fp);
fread(basisU, sizeof(float), 3, fp);
fread(basisV, sizeof(float), 3, fp);
Plane *plane = new Plane(Eigen::Vector3f(center), Eigen::Vector3f(normal),
Eigen::Vector3f(basisU), Eigen::Vector3f(basisV));
plane->color(Eigen::Vector3f(color));
size_t numInliers;
fread(&numInliers, sizeof(size_t), 1, fp);
std::cout << Eigen::Vector3f(center).transpose() << " " << Eigen::Vector3f(normal).transpose() << " " << numInliers << std::endl;
std::vector<size_t> inliers(numInliers);
fread(inliers.data(), sizeof(size_t), numInliers, fp);
plane->inliers(inliers);
geometry->addPlane(plane);
}
emit load(QString("cylinders"));
size_t numCylinders;
fread(&numCylinders, sizeof(size_t), 1, fp);
std::cout << "Cylinders=" << numCylinders << std::endl;
for (size_t i = 0; i < numCylinders; i++)
{
if (i % 100 == 0)
{
emit loadProgress(i / (float)numCylinders);
}
float color[3];
float center[3];
float axis[3];
float radius;
float height;
fread(color, sizeof(float), 3, fp);
fread(center, sizeof(float), 3, fp);
fread(axis, sizeof(float), 3, fp);
fread(&radius, sizeof(float), 1, fp);
fread(&height, sizeof(float), 1, fp);
std::cout << "[" << center[0] << " " << center[1] << " " << center[2] << "] [" << axis[0] << " " << axis[1] << " " << axis[2] << "]" << std::endl;
Cylinder *cylinder = new Cylinder(Eigen::Vector3f(center), Eigen::Vector3f(axis),
radius, height);
cylinder->color(Eigen::Vector3f(color));
size_t numInliers;
fread(&numInliers, sizeof(size_t), 1, fp);
std::vector<size_t> inliers(numInliers);
fread(inliers.data(), sizeof(size_t), numInliers, fp);
cylinder->inliers(inliers);
geometry->addCylinder(cylinder);
}
fclose(fp);
return geometry;
}
ConnectivityGraph* loadConnectivity(size_t size, const std::string &filename)
{
FILE *fp = fopen(filename.c_str(), "rb");
if (fp == NULL) return NULL;
ConnectivityGraph *connectivity = new ConnectivityGraph(size);
emit load(QString("connectivity"));
for (size_t i = 0; i < size; i++)
{
if (i % 1000 == 0)
{
emit loadProgress(i / (float)size);
}
size_t numEdges;
fread(&numEdges, sizeof(size_t), 1, fp);
size_t *edges = new size_t[numEdges];
fread(edges, sizeof(size_t), numEdges, fp);
float *distances = new float[numEdges];
fread(distances, sizeof(float), numEdges, fp);
std::vector<size_t> vEdges(numEdges);
for (size_t i = 0; i < numEdges; i++)
{
vEdges[i] = edges[i];
}
connectivity->addNode(i, vEdges);
delete[] edges;
delete[] distances;
}
std::vector<size_t> groupIndices(size);
fread(groupIndices.data(), sizeof(size_t), size, fp);
connectivity->setGroupIndices(groupIndices);
fclose(fp);
return connectivity;
}
template <size_t DIMENSION>
PointCloud<DIMENSION>* loadFromPCL(const std::string &filename, bool importConnectivity = true,
bool importGeometry = true)
{
FILE *fp = fopen(filename.c_str(), "rb");
if (fp == NULL)
throw "Could not open file: " + filename;
size_t size;
fread(&size, sizeof(size_t), 1, fp);
size_t mode;
fread(&mode, sizeof(size_t), 1, fp);
std::vector<Point<DIMENSION> > points(size);
for (size_t i = 0; i < size; i++)
{
if (i % 1000 == 0)
{
emit loadProgress(i / (float)size);
}
float position[DIMENSION];
float color[DIMENSION];
float intensity;
float normal[DIMENSION];
float normalConfidence;
float curvature;
fread(position, sizeof(float), DIMENSION, fp);
if (mode & PointCloud<DIMENSION>::COLOR)
fread(color, sizeof(float), DIMENSION, fp);
if (mode & PointCloud<DIMENSION>::INTENSITY)
fread(&intensity, sizeof(float), 1, fp);
if (mode & PointCloud<DIMENSION>::NORMAL)
fread(normal, sizeof(float), DIMENSION, fp);
if (mode & PointCloud<DIMENSION>::NORMAL_CONFIDENCE)
fread(&normalConfidence, sizeof(float), 1, fp);
if (mode & PointCloud<DIMENSION>::CURVATURE)
fread(&curvature, sizeof(float), 1, fp);
typename Point<DIMENSION>::Vector positionv(position);
typename Point<DIMENSION>::Vector colorv(color);
typename Point<DIMENSION>::Vector normalv(normal);
normalv = normalv.normalized();
points[i] = Point<DIMENSION>(positionv, colorv, intensity, normalv, normalConfidence, curvature);
}
fclose(fp);
PointCloud<DIMENSION> *pointCloud = new PointCloud<DIMENSION>(points, mode);
if (importConnectivity)
{
std::string connectivityFilename = filename.substr(0, filename.find_last_of('.')) + ".con";
pointCloud->connectivity(loadConnectivity(points.size(), connectivityFilename));
}
if (importGeometry)
{
std::string geometryFilename = filename.substr(0, filename.find_last_of('.')) + ".geo";
Geometry *geometry = loadGeometry(geometryFilename);
if (geometry != NULL) pointCloud->geometry(geometry);
}
return pointCloud;
}
PointCloud3d* loadFromPoints(const std::string &filename)
{
FILE *fp = fopen(filename.c_str(), "rb");
if (fp == NULL)
throw "Could not open file: " + filename;
int size;
fread(&size, sizeof(int), 1, fp);
std::vector<Eigen::Vector3f> positions(size);
std::vector<Eigen::Vector3f> normals(size);
emit load(QString("positions"));
for (int i = 0; i < size; i++)
{
if (i % 1000 == 0)
{
emit loadProgress(i / (float)size);
}
float position[3];
fread(position, sizeof(float), 3, fp);
positions[i] = Eigen::Vector3f(position);
}
emit load(QString("normals"));
for (int i = 0; i < size; i++)
{
if (i % 1000 == 0)
{
emit loadProgress(i / (float)size);
}
float normal[3];
fread(normal, sizeof(float), 3, fp);
normals[i] = Eigen::Vector3f(normal).normalized();
}
emit load(QString("points"));
std::vector<Point3d> points(size);
for (int i = 0; i < size; i++)
{
if (i % 1000 == 0)
{
emit loadProgress(i / (float)size);
}
points[i].position(positions[i]);
points[i].normal(normals[i]);
}
fclose(fp);
return new PointCloud3d(points, PointCloud3d::NORMAL);
}
PointCloud3d* loadFromXYZ(const std::string &filename)
{
std::ifstream input(filename.c_str());
if (!input.good())
throw "Could not open file: " + filename;
std::vector<Point3d> points;
bool hasColor = false;
std::string line;
while (std::getline(input, line))
{
float x, y, z, r = 0, g = 0, b = 0;
sscanf(line.c_str(), "%f %f %f %f %f %f", &x, &y, &z, &r, &g, &b);
r /= 255.0f;
g /= 255.0f;
b /= 255.0f;
if (r > 0 || g > 0 || b > 0) hasColor = true;
points.push_back(Point3d(Eigen::Vector3f(x, y, z), Eigen::Vector3f(r, g, b)));
}
input.close();
if (hasColor)
return new PointCloud3d(points, PointCloud3d::Mode::COLOR);
else
return new PointCloud3d(points);
}
PointCloud3d* loadFromPTX(const std::string &filename)
{
std::ifstream input(filename.c_str());
if (!input.good())
throw "Could not open file: " + filename;
//setlocale(LC_ALL, ".OCP");
std::vector<Point3d> points;
bool hasIntensity = false;
bool hasColor = false;
std::string line;
//int count = 0;
std::locale("En_US");
while (std::getline(input, line))
{
float x, y, z, intensity = 0, r = 0, g = 0, b = 0;
std::stringstream ss;
ss << line;
ss >> x >> y >> z;
if (!ss.eof())
{
ss >> intensity;
if (!ss.eof())
{
ss >> r >> g >> b;
}
}
//sscanf(line.c_str(), "%f %f %f %f %f %f %f", &x, &y, &z, &intensity, &r, &g, &b);
r /= 255.0f;
g /= 255.0f;
b /= 255.0f;
if (intensity > 0) hasIntensity = true;
if (r > 0 || g > 0 || b > 0) hasColor = true;
points.push_back(Point3d(Eigen::Vector3f(x, z, -y), Eigen::Vector3f(r, g, b), intensity));
//std::cout << x << " " << y << " " << z << " " << line << std::endl;
//if (count++ > 10) getchar();
}
input.close();
if (hasColor && hasIntensity)
return new PointCloud3d(points, PointCloud3d::Mode::COLOR | PointCloud3d::Mode::INTENSITY);
else if (hasColor)
return new PointCloud3d(points, PointCloud3d::Mode::COLOR);
else
return new PointCloud3d(points);
}
PointCloud3d* load(const std::string &filename)
{
std::string extension = filename.substr(filename.find_last_of('.') + 1);
PointCloud3d *pointCloud;
if (extension == "pcl")
{
pointCloud = PointCloudIO::loadFromPCL<3>(filename);
}
else if (extension == "points")
{
pointCloud = PointCloudIO::loadFromPoints(filename);
}
else if (extension == "xyz")
{
pointCloud = PointCloudIO::loadFromXYZ(filename);
}
else if (extension == "ptx")
{
pointCloud = PointCloudIO::loadFromPTX(filename);
}
else
{
throw "Extension not supported: " + extension;
}
return pointCloud;
}
signals:
void loadProgress(float);
void load(const QString&);
void saveProgress(float);
void save(const QString&);
};
#endif // POINTCLOUDLOADER_H