-
Notifications
You must be signed in to change notification settings - Fork 122
/
point_set_serializer_vg.cpp
556 lines (445 loc) · 15.7 KB
/
point_set_serializer_vg.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
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
/*
Copyright (C) 2017 Liangliang Nan
https://3d.bk.tudelft.nl/liangliang/ - liangliang.nan@gmail.com
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "point_set_serializer_vg.h"
#include <cassert>
#include "../basic/basic_types.h"
#include "../basic/logger.h"
#include "../basic/progress.h"
#include "../basic/color.h"
#include "../model/point_set.h"
//#define TRANSLATE_RELATIVE_TO_FIRST_POINT
/*
// file format definition
num_points: num
x y z
...
num_colors: num // can be 0; if not, it must equal to num_points
r g b // all values are floating point numbers in range [0,1]
...
num_normals: num // can be 0; if not, it must equal to num_points
nx ny nz // all values are floating point numbers in range [0,1]
num_groups: num // can be 0
group_type: type // integer: PLANE = 0, CYLINDER = 1, SPHERE = 2, CONE = 3, TORUS = 4, GENERAL = 5
num_group_parameters: NUM_GROUP_PARAMETERS // integer: the number of parameters for this group type.
group_parameters: float[NUM_GROUP_PARAMETERS] // NUM_GROUP_PARAMETERS values in range [0,1]. Parameters of the group.
group_label: label // string: the label of the first group.
group_color: color (r, g, b) // vec3: three values in range [0,1]. The color of the group.
group_num_points: num // integer: can be 0. The number of points in the group.
idx ... // integers: indices of the points in the group. Total number is group_num_points.
num_children: num // can be 0
group_type: type
num_group_parameters: NUM_GROUP_PARAMETERS
group_parameters: float[NUM_GROUP_PARAMETERS]
group_label: label
group_color: color (r, g, b)
group_num_points: num
idx ...
group_type: type
num_group_parameters: NUM_GROUP_PARAMETERS
group_parameters: float[NUM_GROUP_PARAMETERS]
group_label: label
group_color: color (r, g, b)
group_num_points: num
idx ...
...
*/
void PointSetSerializer_vg::save_vg(const PointSet* pset, const std::string& file_name) {
// open file
std::ofstream output(file_name.c_str());
if (output.fail()) {
Logger::err("-") << "could not open file\'" << file_name << "\'" << std::endl;
return;
}
output.precision(16);
//////////////////////////////////////////////////////////////////////////
const std::vector<vec3>& points = pset->points();
const std::vector<vec3>& colors = pset->colors();
const std::vector<vec3>& normals = pset->normals();
const std::vector<VertexGroup::Ptr>& groups = pset->groups();
ProgressLogger progress(points.size() + colors.size() + normals.size() + groups.size());
output << "num_points: " << points.size() << std::endl;
for (std::size_t i = 0; i < points.size(); ++i) {
output << points[i] << " ";
progress.next();
}
output << std::endl;
output << "num_colors: " << colors.size() << std::endl;
for (std::size_t i = 0; i < colors.size(); ++i) {
output << colors[i] << " ";
progress.next();
}
output << std::endl;
output << "num_normals: " << normals.size() << std::endl;
for (std::size_t i = 0; i < normals.size(); ++i) {
output << normals[i] << " ";
progress.next();
}
output << std::endl;
output << "num_groups: " << groups.size() << std::endl;
for (std::size_t i = 0; i < groups.size(); ++i) {
VertexGroup* g = groups[i];
write_ascii_group(output, g);
// children
const std::vector<VertexGroup*>& children = g->children();
output << "num_children: " << children.size() << std::endl;
for (unsigned int j = 0; j < children.size(); ++j) {
VertexGroup* chld = children[j];
write_ascii_group(output, chld);
}
progress.next();
}
}
/*
group_type: type // integer: PLANE = 0, CYLINDER = 1, SPHERE = 2, CONE = 3, TORUS = 4, GENERAL = 5
num_group_parameters: NUM_GROUP_PARAMETERS // integer: the number of parameters for this group type.
group_parameters: float[NUM_GROUP_PARAMETERS] // NUM_GROUP_PARAMETERS values in range [0,1]. Parameters of this group.
group_label: label // string: the label of this group.
group_color: color (r, g, b) // vec3: three values in range [0,1]. The color of this group.
group_num_points: num // integer: can be 0. The number of points in this group.
idx ... // integers: indices of the points in this group. Total number is group_num_points.
*/
void PointSetSerializer_vg::write_ascii_group(std::ostream& output, VertexGroup* g) {
//int type = g->type();
int type = 0;
output << "group_type: " << type << std::endl;
const std::vector<float>& para = get_group_parameters(g);
output << "num_group_parameters: " << para.size() << std::endl;
output << "group_parameters: ";
for (std::size_t i = 0; i < para.size(); ++i)
output << para[i] << " ";
output << std::endl;
std::string label = g->label();
output << "group_label: " << label << std::endl;
Color c = g->color();
output << "group_color: " << c.r() << " " << c.g() << " " << c.b() << std::endl;
std::size_t num_point = g->size();
output << "group_num_point: " << num_point << std::endl;
for (std::size_t i = 0; i < g->size(); ++i) {
output << g->at(i) << " ";
}
output << std::endl;
}
void PointSetSerializer_vg::load_vg(PointSet* pset, const std::string& file_name) {
std::ifstream input(file_name.c_str());
if (input.fail()) {
Logger::err("-") << "could not open file\'" << file_name << "\'" << std::endl;
return;
}
// get length of file
input.seekg(0, input.end);
std::streamoff length = input.tellg();
input.seekg(0, input.beg);
ProgressLogger progress(length);
std::string dumy;
std::size_t num;
input >> dumy >> num;
std::vector<vec3>& points = pset->points();
points.resize(num);
#ifdef TRANSLATE_RELATIVE_TO_FIRST_POINT
double x0, y0, z0;
input >> x0 >> y0 >> z0;
points[0] = vec3(0, 0, 0);
double x, y, z;
for (int i = 1; i < num; ++i) {
input >> x >> y >> z;
points[i] = vec3(x-x0, y-y0, z-z0);
std::streamoff pos = input.tellg();
progress.notify(pos);
}
#else
for (int i = 0; i < num; ++i) {
input >> points[i];
std::streamoff pos = input.tellg();
progress.notify(pos);
}
#endif
input >> dumy >> num;
std::vector<vec3>& colors = pset->colors();
colors.resize(num);
for (int i = 0; i < num; ++i) {
input >> colors[i];
// in case the color values are in [0, 255], converting to [0, 1]
if (colors[i].x > 1.0f || colors[i].y > 1.0f || colors[i].z > 1.0f)
colors[i] /= 255.0f;
std::streamoff pos = input.tellg();
progress.notify(pos);
}
input >> dumy >> num;
std::vector<vec3>& normals = pset->normals();
normals.resize(num);
for (int i = 0; i < num; ++i) {
input >> normals[i];
std::streamoff pos = input.tellg();
progress.notify(pos);
}
//////////////////////////////////////////////////////////////////////////
std::size_t num_groups = 0;
input >> dumy >> num_groups;
for (int i = 0; i<num_groups; ++i) {
VertexGroup::Ptr g = read_ascii_group(input);
if (!g)
continue;
if (!g->empty()) {
g->set_point_set(pset);
pset->groups().push_back(g);
}
int num_children = 0;
input >> dumy >> num_children;
for (int j = 0; j<num_children; ++j) {
VertexGroup::Ptr chld = read_ascii_group(input);
if (!chld->empty()) {
chld->set_point_set(pset);
g->add_child(chld);
}
}
std::streamoff pos = input.tellg();
progress.notify(pos);
}
}
VertexGroup* PointSetSerializer_vg::read_ascii_group(std::istream& input) {
std::string dumy;
int type;
input >> dumy >> type;
int num;
input >> dumy >> num;
if (num != 4)
return nullptr; // bad/unknown data
std::vector<float> para(num);
input >> dumy;
for (int i = 0; i < num; ++i)
input >> para[i];
std::string label;
input >> dumy >> label;
float r, g, b;
input >> dumy >> r >> g >> b;
// in case the color values are in [0, 255], converting to [0, 1]
if (r > 1.0f || g > 1.0f || b > 1.0f) {
r /= 255.0f; g /= 255.0f; b /= 255.0f;
}
Color color(r, g, b);
int num_points;
input >> dumy >> num_points;
VertexGroup* grp = new VertexGroup;
assign_group_parameters(grp, para);
for (int i = 0; i < num_points; ++i) {
int idx;
input >> idx;
grp->push_back(idx);
}
grp->set_label(label);
grp->set_color(color);
return grp;
}
void PointSetSerializer_vg::load_bvg(PointSet* pset, const std::string& file_name) {
std::ifstream input(file_name.c_str(), std::fstream::binary);
if (input.fail()) {
Logger::err("-") << "could not open file\'" << file_name << "\'" << std::endl;
return;
}
int num;
input.read((char*)(&num), sizeof(int));
if (num <= 0) {
Logger::err("-") << "no point exists in file\'" << file_name << "\'" << std::endl;
return;
}
// read the points block
std::vector<vec3>& points = pset->points();
points.resize(num);
input.read((char*)points.data(), num * sizeof(vec3));
// read the colors block if exists
input.read((char*)(&num), sizeof(int));
if (num > 0) {
if (num != points.size()) {
Logger::err("-") << "color-point number not match" << std::endl;
return;
}
std::vector<vec3>& colors = pset->colors();
colors.resize(num);
input.read((char*)colors.data(), num * sizeof(vec3));
}
// read the normals block if exists
input.read((char*)(&num), sizeof(int));
if (num > 0) {
if (num != points.size()) {
Logger::err("-") << "normal-point number not match" << std::endl;
return;
}
std::vector<vec3>& normals = pset->normals();
normals.resize(num);
input.read((char*)normals.data(), num * sizeof(vec3));
}
//////////////////////////////////////////////////////////////////////////
int num_groups = 0;
input.read((char*)&num_groups, sizeof(int));
for (int i = 0; i < num_groups; ++i) {
VertexGroup::Ptr g = read_binary_group(input);
if (!g)
continue;
if (!g->empty()) {
g->set_point_set(pset);
pset->groups().push_back(g);
}
int num_children = 0;
input.read((char*)&num_children, sizeof(int));
for (int j = 0; j < num_children; ++j) {
VertexGroup::Ptr chld = read_binary_group(input);
if (!chld->empty()) {
chld->set_point_set(pset);
g->add_child(chld);
}
else
delete chld;
}
}
}
void PointSetSerializer_vg::save_bvg(const PointSet* pset, const std::string& file_name) {
// open file
std::ofstream output(file_name.c_str(), std::fstream::binary);
if (output.fail()) {
Logger::err("-") << "could not open file\'" << file_name << "\'" << std::endl;
return;
}
// write the points block
const std::vector<vec3>& points = pset->points();
std::size_t num = points.size();
output.write((char*)&num, sizeof(int));
output.write((char*)points.data(), num * sizeof(vec3));
const std::vector<vec3>& colors = pset->colors();
num = colors.size();
output.write((char*)&num, sizeof(int));
if (num > 0)
output.write((char*)colors.data(), num * sizeof(vec3));
const std::vector<vec3>& normals = pset->normals();
num = normals.size();
output.write((char*)&num, sizeof(int));
if (num > 0)
output.write((char*)normals.data(), num * sizeof(vec3));
//////////////////////////////////////////////////////////////////////////
const std::vector<VertexGroup::Ptr>& groups = pset->groups();
std::size_t num_groups = groups.size();
output.write((char*)&num_groups, sizeof(int));
for (std::size_t i = 0; i < num_groups; ++i) {
VertexGroup* g = groups[i];
write_binary_group(output, g);
// children
const std::vector<VertexGroup*>& children = g->children();
std::size_t chld_num = children.size();
output.write((char*)&chld_num, sizeof(int));
for (std::size_t j = 0; j < chld_num; ++j) {
VertexGroup* chld = children[j];
write_binary_group(output, chld);
}
}
}
// string are stored as array of chars in binary file
std::string PointSetSerializer_vg::read_binary_string(std::istream& input) {
int size_int = sizeof(int);
int size_char = sizeof(char);
int num_char = 0;
input.read((char*)&num_char, size_int);
std::string str;
for (int i = 0; i < num_char; ++i) {
char c;
input.read(&c, size_char);
if (c != ' ')
str.push_back(c);
else
str.push_back('-');
}
return str;
}
// string are stored as array of chars in binary file
void PointSetSerializer_vg::write_binary_string(std::ostream& output, const std::string& str) {
int size_int = sizeof(int);
int size_char = sizeof(char);
std::size_t num_char = str.size();
output.write((char*)&num_char, size_int);
for (std::size_t i = 0; i < num_char; ++i) {
char c = str[i];
if (c == ' ')
c = '-';
output.write(&c, size_char);
}
}
// for binary file format, no string stuff except labels. we add size info before each label
VertexGroup* PointSetSerializer_vg::read_binary_group(std::istream& input) {
int type;
input.read((char*)&type, sizeof(int));
int num;
input.read((char*)&num, sizeof(int));
if (num != 4)
return nullptr; // bad/unknown data
std::vector<float> para(num);
input.read((char*)para.data(), num * sizeof(float));
VertexGroup* grp = new VertexGroup;
assign_group_parameters(grp, para);
//////////////////////////////////////////////////////////////////////////
// Liangliang: check why this doesn't work in release mode
//int label_size = 0;
//std::string label;
//input.read((char*)&label_size, sizeof(int));
//input.read((char*)&label, label_size);
//////////////////////////////////////////////////////////////////////////
std::string label = read_binary_string(input);
grp->set_label(label);
float arr[3];
input.read((char*)arr, 3 * sizeof(float));
Color color(arr);
grp->set_color(color);
int num_points = 0;
input.read((char*)&num_points, sizeof(int));
grp->resize(num_points);
input.read((char*)grp->data(), num_points * sizeof(int));
return grp;
}
// for binary file format, no string stuff except labels. we add size info before each label
void PointSetSerializer_vg::write_binary_group(std::ostream& output, VertexGroup* g) {
//int type = g->type();
int type = 0;
output.write((char*)&type, sizeof(int));
int num = 4;
output.write((char*)&num, sizeof(int));
const std::vector<float>& para = get_group_parameters(g);
output.write((char*)para.data(), sizeof(float) * num);
//////////////////////////////////////////////////////////////////////////
// Liangliang: check why not work
// std::string label = g->label();
// int label_size = sizeof(label) + 1;
// output.write((char*)&label_size, sizeof(int));
// output.write(label.c_str(), label_size + 1);
//////////////////////////////////////////////////////////////////////////
std::string label = g->label();
write_binary_string(output, label);
Color c = g->color();
output.write((char*)c.data(), sizeof(float) * 3);
std::size_t num_point = g->size();
output.write((char*)&num_point, sizeof(int));
output.write((char*)g->data(), num_point * sizeof(int));
}
std::vector<float> PointSetSerializer_vg::get_group_parameters(VertexGroup* g) {
int num = 4;
std::vector<float> para(num);
para[0] = static_cast<float>(g->plane().a());
para[1] = static_cast<float>(g->plane().b());
para[2] = static_cast<float>(g->plane().c());
para[3] = static_cast<float>(g->plane().d());
return para;
}
void PointSetSerializer_vg::assign_group_parameters(VertexGroup* g, std::vector<float>& para) {
int num = 4;
assert(para.size() == num);
g->set_plane(Plane3d(para[0], para[1], para[2], para[3]));
}