-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
Copy pathply_parser.cpp
611 lines (589 loc) · 21.8 KB
/
ply_parser.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
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
/*
* Software License Agreement (BSD License)
*
* Point Cloud Library (PCL) - www.pointclouds.org
* Copyright (c) 2007-2012, Ares Lagae
* Copyright (c) 2012, Willow Garage, Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Willow Garage, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*
*/
#include <pcl/io/ply/ply_parser.h>
bool pcl::io::ply::ply_parser::parse (const std::string& filename)
{
std::ifstream istream (filename.c_str (), std::ios::in | std::ios::binary);
std::string line;
line_number_ = 0;
std::size_t number_of_format_statements = 0;
std::size_t number_of_element_statements = 0;
std::size_t number_of_property_statements = 0;
std::size_t number_of_obj_info_statements = 0;
std::size_t number_of_comment_statements = 0;
format_type format = pcl::io::ply::unknown;
std::vector<std::shared_ptr<element>> elements;
char line_delim = '\n';
int char_ignore_count = 0;
// magic
char magic[4];
istream.read (magic, 4);
// Check if CR/LF, setup delim and char skip
if (magic[3] == '\r')
{
istream.ignore (1);
line_delim = '\r';
char_ignore_count = 1;
}
++line_number_;
if (!istream)
{
if (error_callback_)
error_callback_ (line_number_, "parse error: couldn't read the magic string");
return false;
}
if ((magic[0] != 'p') || (magic[1] != 'l') || (magic[2] != 'y'))
{
if (error_callback_)
error_callback_ (line_number_, "parse error: wrong magic string");
return false;
}
if (magic_callback_)
magic_callback_ ();
while (std::getline (istream, line, line_delim))
{
istream.ignore (char_ignore_count);
++line_number_;
std::istringstream stringstream (line);
stringstream.unsetf (std::ios_base::skipws);
stringstream >> std::ws;
if (stringstream.eof ())
{
if (warning_callback_)
warning_callback_ (line_number_, "ignoring line '" + line + "'");
}
else
{
std::string keyword;
stringstream >> keyword;
// format
if (keyword == "format")
{
std::string format_string, version;
char space_format_format_string, space_format_string_version;
stringstream >> space_format_format_string >> std::ws >> format_string >> space_format_string_version >> std::ws >> version;
if (!stringstream ||
!stringstream.eof () ||
!isspace (space_format_format_string) ||
!isspace (space_format_string_version))
{
if (error_callback_)
error_callback_ (line_number_, "parse error: failed to parse the format statement");
return false;
}
if (format_string == "ascii")
{
format = ascii_format;
}
else if (format_string == "binary_big_endian")
{
format = binary_big_endian_format;
}
else if (format_string == "binary_little_endian")
{
format = binary_little_endian_format;
}
else
{
if (error_callback_)
{
error_callback_ (line_number_, "parse error: unknown format");
}
return false;
}
if (version != "1.0")
{
if (error_callback_)
{
error_callback_ (line_number_, "version '" + version + "' is not supported");
}
return false;
}
if (number_of_format_statements > 0)
{
if (error_callback_)
{
error_callback_ (line_number_, "parse error: more than 1 format statement");
}
return false;
}
++number_of_format_statements;
if (format_callback_)
{
format_callback_ (format, version);
}
}
// element
else if (keyword == "element")
{
std::string name;
std::size_t count;
char space_element_name, space_name_count;
stringstream >> space_element_name >> std::ws >> name >> space_name_count >> std::ws >> count;
if (!stringstream ||
!stringstream.eof () ||
!isspace (space_element_name) ||
!isspace (space_name_count))
{
if (error_callback_)
{
error_callback_ (line_number_, "parse error: invalid element statement");
}
return false;
}
auto iterator = elements.cbegin ();
while (iterator != elements.cend ())
{
if ((*iterator)->name == name)
break;
++iterator;
}
if (iterator != elements.end ())
{
if (error_callback_)
{
error_callback_ (line_number_, "parse error: invalid elements");
}
return false;
}
++number_of_element_statements;
element_callbacks_type element_callbacks;
if (element_definition_callbacks_)
{
element_callbacks = element_definition_callbacks_ (name, count);
}
elements.emplace_back(new element (name, count, std::get<0>(element_callbacks), std::get<1>(element_callbacks)));
current_element_ = elements.back ().get ();
}
// property
else if (keyword == "property")
{
std::string type_or_list;
char space_property_type_or_list;
stringstream >> space_property_type_or_list >> std::ws >> type_or_list;
if (!stringstream || !isspace (space_property_type_or_list))
{
if (error_callback_)
{
error_callback_ (line_number_, "parse error: invalid property statement");
}
return false;
}
if (type_or_list != "list") {
std::string name;
std::string& type = type_or_list;
char space_type_name;
stringstream >> space_type_name >> std::ws >> name;
if (!stringstream || !isspace (space_type_name))
{
if (error_callback_)
{
error_callback_ (line_number_, "parse error");
}
return false;
}
if (number_of_element_statements == 0)
{
if (error_callback_)
{
error_callback_ (line_number_, "parse error");
}
return false;
}
auto iterator = current_element_->properties.cbegin ();
while (iterator != current_element_->properties.cend ())
{
if ((*iterator)->name == name)
break;
++iterator;
}
if (iterator != current_element_->properties.end ())
{
if (error_callback_)
error_callback_ (line_number_, "parse error");
return false;
}
if ((type == type_traits<int8>::name ()) || (type == type_traits<int8>::old_name ()))
{
parse_scalar_property_definition<int8>(name);
}
else if ((type == type_traits<int16>::name ()) || (type == type_traits<int16>::old_name ()))
{
parse_scalar_property_definition<int16>(name);
}
else if ((type == type_traits<int32>::name ()) || (type == type_traits<int32>::old_name ()))
{
parse_scalar_property_definition<int32>(name);
}
else if ((type == type_traits<uint8>::name ()) || (type == type_traits<uint8>::old_name ()))
{
parse_scalar_property_definition<uint8>(name);
}
else if ((type == type_traits<uint16>::name ()) || (type == type_traits<uint16>::old_name ()))
{
parse_scalar_property_definition<uint16>(name);
}
else if ((type == type_traits<uint32>::name ()) || (type == type_traits<uint32>::old_name ()))
{
parse_scalar_property_definition<uint32>(name);
}
else if ((type == type_traits<float32>::name ()) || (type == type_traits<float32>::old_name ()))
{
parse_scalar_property_definition<float32>(name);
}
else if ((type == type_traits<float64>::name ()) || (type == type_traits<float64>::old_name ()))
{
parse_scalar_property_definition<float64>(name);
}
else
{
if (error_callback_)
{
error_callback_ (line_number_, "parse error: unknown type");
}
return false;
}
++number_of_property_statements;
}
else
{
std::string name;
std::string size_type_string, scalar_type_string;
char space_list_size_type, space_size_type_scalar_type, space_scalar_type_name;
stringstream >> space_list_size_type >> std::ws >> size_type_string >> space_size_type_scalar_type >> std::ws >> scalar_type_string >> space_scalar_type_name >> std::ws >> name;
if (!stringstream ||
!isspace (space_list_size_type) ||
!isspace (space_size_type_scalar_type) ||
!isspace (space_scalar_type_name))
{
if (error_callback_)
error_callback_ (line_number_, "parse error: invalid list statement");
return false;
}
if (number_of_element_statements == 0)
{
if (error_callback_)
error_callback_ (line_number_, "parse error");
return false;
}
auto iterator = current_element_->properties.cbegin ();
while (iterator != current_element_->properties.cend ())
{
if ((*iterator)->name == name)
break;
++iterator;
}
if (iterator != current_element_->properties.end ())
{
if (error_callback_)
error_callback_ (line_number_, "parse error");
return false;
}
if ((size_type_string == type_traits<uint8>::name ()) || (size_type_string == type_traits<uint8>::old_name ()))
{
using size_type = uint8;
if ((scalar_type_string == type_traits<int8>::name ()) || (scalar_type_string == type_traits<int8>::old_name ()))
{
parse_list_property_definition<size_type, int8>(name);
}
else if ((scalar_type_string == type_traits<int16>::name ()) || (scalar_type_string == type_traits<int16>::old_name ()))
{
parse_list_property_definition<size_type, int16>(name);
}
else if ((scalar_type_string == type_traits<int32>::name ()) || (scalar_type_string == type_traits<int32>::old_name ()))
{
parse_list_property_definition<size_type, int32>(name);
}
else if ((scalar_type_string == type_traits<uint8>::name ()) || (scalar_type_string == type_traits<uint8>::old_name ()))
{
parse_list_property_definition<size_type, uint8>(name);
}
else if ((scalar_type_string == type_traits<uint16>::name ()) || (scalar_type_string == type_traits<uint16>::old_name ()))
{
parse_list_property_definition<size_type, uint16>(name);
}
else if ((scalar_type_string == type_traits<uint32>::name ()) || (scalar_type_string == type_traits<uint32>::old_name ()))
{
parse_list_property_definition<size_type, uint32>(name);
}
else if ((scalar_type_string == type_traits<float32>::name ()) || (scalar_type_string == type_traits<float32>::old_name ()))
{
parse_list_property_definition<size_type, float32>(name);
}
else if ((scalar_type_string == type_traits<float64>::name ()) || (scalar_type_string == type_traits<float64>::old_name ()))
{
parse_list_property_definition<size_type, float64>(name);
}
else
{
if (error_callback_)
{
error_callback_ (line_number_, "parse error: unknown scalar type");
}
return false;
}
}
else if ((size_type_string == type_traits<uint16>::name ()) || (size_type_string == type_traits<uint16>::old_name ()))
{
using size_type = uint16;
if ((scalar_type_string == type_traits<int8>::name ()) || (scalar_type_string == type_traits<int8>::old_name ()))
{
parse_list_property_definition<size_type, int8>(name);
}
else if ((scalar_type_string == type_traits<int16>::name ()) || (scalar_type_string == type_traits<int16>::old_name ()))
{
parse_list_property_definition<size_type, int16>(name);
}
else if ((scalar_type_string == type_traits<int32>::name ()) || (scalar_type_string == type_traits<int32>::old_name ()))
{
parse_list_property_definition<size_type, int32>(name);
}
else if ((scalar_type_string == type_traits<uint8>::name ()) || (scalar_type_string == type_traits<uint8>::old_name ()))
{
parse_list_property_definition<size_type, uint8>(name);
}
else if ((scalar_type_string == type_traits<uint16>::name ()) || (scalar_type_string == type_traits<uint16>::old_name ()))
{
parse_list_property_definition<size_type, uint16>(name);
}
else if ((scalar_type_string == type_traits<uint32>::name ()) || (scalar_type_string == type_traits<uint32>::old_name ()))
{
parse_list_property_definition<size_type, uint32>(name);
}
else if ((scalar_type_string == type_traits<float32>::name ()) || (scalar_type_string == type_traits<float32>::old_name ()))
{
parse_list_property_definition<size_type, float32>(name);
}
else if ((scalar_type_string == type_traits<float64>::name ()) || (scalar_type_string == type_traits<float64>::old_name ()))
{
parse_list_property_definition<size_type, float64>(name);
}
else
{
if (error_callback_)
error_callback_ (line_number_, "parse error: unknown scalar type");
return false;
}
}
else if ((size_type_string == type_traits<uint32>::name ()) || (size_type_string == type_traits<uint32>::old_name ()))
{
using size_type = uint32;
if ((scalar_type_string == type_traits<int8>::name ()) || (scalar_type_string == type_traits<int8>::old_name ()))
{
parse_list_property_definition<size_type, int8>(name);
}
else if ((scalar_type_string == type_traits<int16>::name ()) || (scalar_type_string == type_traits<int16>::old_name ()))
{
parse_list_property_definition<size_type, int16>(name);
}
else if ((scalar_type_string == type_traits<int32>::name ()) || (scalar_type_string == type_traits<int32>::old_name ()))
{
parse_list_property_definition<size_type, int32>(name);
}
else if ((scalar_type_string == type_traits<uint8>::name ()) || (scalar_type_string == type_traits<uint8>::old_name ()))
{
parse_list_property_definition<size_type, uint8>(name);
}
else if ((scalar_type_string == type_traits<uint16>::name ()) || (scalar_type_string == type_traits<uint16>::old_name ()))
{
parse_list_property_definition<size_type, uint16>(name);
}
else if ((scalar_type_string == type_traits<uint32>::name ()) || (scalar_type_string == type_traits<uint32>::old_name ()))
{
parse_list_property_definition<size_type, uint32>(name);
}
else if ((scalar_type_string == type_traits<float32>::name ()) || (scalar_type_string == type_traits<float32>::old_name ()))
{
parse_list_property_definition<size_type, float32>(name);
}
else if ((scalar_type_string == type_traits<float64>::name ()) || (scalar_type_string == type_traits<float64>::old_name ()))
{
parse_list_property_definition<size_type, float64>(name);
}
else
{
if (error_callback_)
error_callback_ (line_number_, "parse error: unknown scalar type");
return false;
}
}
else
{
if (error_callback_)
error_callback_ (line_number_, "parse error");
return false;
}
++number_of_property_statements;
}
}
// comment
else if (keyword == "comment")
{
if (comment_callback_)
comment_callback_ (line);
++number_of_comment_statements;
}
// obj_info
else if (keyword == "obj_info")
{
if (obj_info_callback_)
obj_info_callback_ (line);
++number_of_obj_info_statements;
}
// end_header
else if (keyword == "end_header")
{
if (end_header_callback_)
{
if (!end_header_callback_ ())
return true;
}
break;
}
// unknown keyword
else
{
if (warning_callback_)
warning_callback_ (line_number_, "ignoring line '" + line + "'");
}
}
}
if (number_of_format_statements == 0)
{
if (error_callback_)
error_callback_ (line_number_, "parse error: a format statement is required");
return false;
}
// ascii
if (format == ascii_format)
{
for (auto element_iterator = elements.cbegin ();
element_iterator != elements.cend ();
++element_iterator)
{
struct element& element = *(element_iterator->get ());
for (std::size_t element_index = 0; element_index < element.count; ++element_index)
{
if (element.begin_element_callback)
element.begin_element_callback ();
if (!std::getline (istream, line, line_delim))
{
if (error_callback_)
error_callback_ (line_number_, "parse error");
return false;
}
istream.ignore (char_ignore_count);
++line_number_;
std::istringstream stringstream (line);
stringstream.unsetf (std::ios_base::skipws);
stringstream >> std::ws;
for (auto property_iterator = element.properties.cbegin ();
property_iterator != element.properties.cend ();
++property_iterator)
{
struct property& property = *(property_iterator->get ());
if (!property.parse (*this, format, stringstream))
return false;
}
if (!stringstream.eof ())
{
if (error_callback_)
error_callback_ (line_number_, "parse error");
return false;
}
if (element.end_element_callback)
element.end_element_callback ();
}
}
istream >> std::ws;
if (istream.fail () || !istream.eof () || istream.bad ())
{
if (error_callback_)
error_callback_ (line_number_, "parse error");
return false;
}
return true;
}
// binary
std::streampos data_start = istream.tellg ();
istream.close ();
istream.open (filename.c_str (), std::ios::in | std::ios::binary);
istream.seekg (data_start);
for (auto element_iterator = elements.cbegin ();
element_iterator != elements.cend ();
++element_iterator)
{
struct element& element = *(element_iterator->get ());
for (std::size_t element_index = 0; element_index < element.count; ++element_index)
{
if (element.begin_element_callback) {
element.begin_element_callback ();
}
for (auto property_iterator = element.properties.cbegin ();
property_iterator != element.properties.cend ();
++property_iterator)
{
struct property& property = *(property_iterator->get ());
if (!property.parse (*this, format, istream))
{
return false;
}
}
if (element.end_element_callback)
{
element.end_element_callback ();
}
}
}
if (istream.fail () || istream.bad ())
{
if (error_callback_)
{
error_callback_ (line_number_, "parse error: failed to read from the binary stream");
}
return false;
}
if (istream.rdbuf ()->sgetc () != std::char_traits<char>::eof ())
{
if (warning_callback_)
warning_callback_ (line_number_, "ignoring extra data at the end of binary stream");
}
return true;
}