-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
ply_io.cpp
1727 lines (1610 loc) · 62.6 KB
/
ply_io.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
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Software License Agreement (BSD License)
*
* Copyright (c) 2009, 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/point_types.h>
#include <pcl/common/io.h>
#include <pcl/common/pcl_filesystem.h>
#include <pcl/io/ply_io.h>
#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <string>
#include <tuple>
#include <boost/algorithm/string.hpp> // for split
std::tuple<std::function<void ()>, std::function<void ()> >
pcl::PLYReader::elementDefinitionCallback (const std::string& element_name, std::size_t count)
{
if (element_name == "vertex")
{
cloud_->data.clear ();
cloud_->fields.clear ();
// Cloud dimensions may have already been set from obj_info fields
if (cloud_->width == 0 || cloud_->height == 0)
{
cloud_->width = static_cast<std::uint32_t> (count);
cloud_->height = 1;
}
cloud_->is_dense = true;
cloud_->point_step = 0;
cloud_->row_step = 0;
vertex_count_ = 0;
return {[this] { vertexBeginCallback(); }, [this] { vertexEndCallback(); }};
}
if ((element_name == "face") && polygons_)
{
polygons_->reserve (count);
return {[this] { faceBeginCallback(); }, [this] { faceEndCallback(); }};
}
if (element_name == "camera")
{
cloud_->is_dense = true;
return {};
}
if (element_name == "range_grid")
{
range_grid_->reserve (count);
return {[this] { rangeGridBeginCallback(); }, [this] { rangeGridEndCallback(); }};
}
return {};
}
bool
pcl::PLYReader::endHeaderCallback ()
{
cloud_->data.resize (static_cast<std::size_t>(cloud_->point_step) * cloud_->width * cloud_->height);
return true;
}
template<typename Scalar> void
pcl::PLYReader::appendScalarProperty (const std::string& name, const std::size_t& size)
{
cloud_->fields.emplace_back();
::pcl::PCLPointField ¤t_field = cloud_->fields.back ();
current_field.name = name;
current_field.offset = cloud_->point_step;
current_field.datatype = pcl::traits::asEnum<Scalar>::value;
current_field.count = static_cast<std::uint32_t> (size);
cloud_->point_step += static_cast<std::uint32_t> (pcl::getFieldSize (pcl::traits::asEnum<Scalar>::value) * size);
}
bool
pcl::PLYReader::amendProperty (const std::string& old_name, const std::string& new_name, std::uint8_t new_datatype)
{
const auto fieldIndex = pcl::getFieldIndex(*cloud_, old_name);
if (fieldIndex == -1) {
return false;
}
auto& field = cloud_->fields[fieldIndex];
field.name = new_name;
if (new_datatype > 0 && new_datatype != field.datatype)
field.datatype = new_datatype;
return true;
}
namespace pcl
{
template <>
std::function<void (pcl::io::ply::float32)>
PLYReader::scalarPropertyDefinitionCallback (const std::string& element_name, const std::string& property_name)
{
if (element_name == "vertex")
{
appendScalarProperty<pcl::io::ply::float32> (property_name, 1);
return ([this] (pcl::io::ply::float32 value) { vertexScalarPropertyCallback<pcl::io::ply::float32> (value); });
}
if (element_name == "camera")
{
if (property_name == "view_px")
{
return [this] (const float& value) { originXCallback (value); };
}
if (property_name == "view_py")
{
return [this] (const float& value) { originYCallback (value); };
}
if (property_name == "view_pz")
{
return [this] (const float& value) { originZCallback (value); };
}
if (property_name == "x_axisx")
{
return [this] (const float& value) { orientationXaxisXCallback (value); };
}
if (property_name == "x_axisy")
{
return [this] (const float& value) { orientationXaxisYCallback (value); };
}
if (property_name == "x_axisz")
{
return [this] (const float& value) { orientationXaxisZCallback (value); };
}
if (property_name == "y_axisx")
{
return [this] (const float& value) { orientationYaxisXCallback (value); };
}
if (property_name == "y_axisy")
{
return [this] (const float& value) { orientationYaxisYCallback (value); };
}
if (property_name == "y_axisz")
{
return [this] (const float& value) { orientationYaxisZCallback (value); };
}
if (property_name == "z_axisx")
{
return [this] (const float& value) { orientationZaxisXCallback (value); };
}
if (property_name == "z_axisy")
{
return [this] (const float& value) { orientationZaxisYCallback (value); };
}
if (property_name == "z_axisz")
{
return [this] (const float& value) { orientationZaxisZCallback (value); };
}
}
return {};
}
template <> std::function<void (pcl::io::ply::uint8)>
PLYReader::scalarPropertyDefinitionCallback (const std::string& element_name, const std::string& property_name)
{
if (element_name == "vertex")
{
if ((property_name == "red") || (property_name == "green") || (property_name == "blue") ||
(property_name == "diffuse_red") || (property_name == "diffuse_green") || (property_name == "diffuse_blue"))
{
if ((property_name == "red") || (property_name == "diffuse_red"))
appendScalarProperty<pcl::io::ply::float32> ("rgb");
return [this, property_name] (pcl::io::ply::uint8 color) { vertexColorCallback (property_name, color); };
}
if (property_name == "alpha")
{
if (!amendProperty("rgb", "rgba", pcl::PCLPointField::UINT32))
{
PCL_ERROR("[pcl::PLYReader::scalarPropertyDefinitionCallback] 'rgb' was not "
"found in cloud_->fields!,"
" can't amend property '%s' to get new type 'rgba' \n",
property_name.c_str());
return {};
}
return [this] (pcl::io::ply::uint8 alpha) { vertexAlphaCallback (alpha); };
}
if (property_name == "intensity")
{
appendScalarProperty<pcl::io::ply::float32> (property_name);
return [this] (pcl::io::ply::uint8 intensity) { vertexIntensityCallback (intensity); };
}
appendScalarProperty<pcl::io::ply::uint8> (property_name);
return ([this] (pcl::io::ply::uint8 value) { vertexScalarPropertyCallback<pcl::io::ply::uint8> (value); });
}
return {};
}
template <> std::function<void (pcl::io::ply::int32)>
PLYReader::scalarPropertyDefinitionCallback (const std::string& element_name, const std::string& property_name)
{
if (element_name == "vertex")
{
appendScalarProperty<pcl::io::ply::int32> (property_name, 1);
return ([this] (pcl::io::ply::uint32 value) { vertexScalarPropertyCallback<pcl::io::ply::uint32> (value); });
}
if (element_name == "camera")
{
if (property_name == "viewportx")
{
return [this] (const int& width) { cloudWidthCallback (width); };
}
if (property_name == "viewporty")
{
return [this] (const int& height) { cloudHeightCallback (height); };
}
return {};
}
return {};
}
template <typename Scalar> std::function<void (Scalar)>
PLYReader::scalarPropertyDefinitionCallback (const std::string& element_name, const std::string& property_name)
{
if (element_name == "vertex")
{
appendScalarProperty<Scalar> (property_name, 1);
return ([this] (Scalar value) { vertexScalarPropertyCallback<Scalar> (value); });
}
return {};
}
template<typename T> inline
std::enable_if_t<std::is_floating_point<T>::value>
unsetDenseFlagIfNotFinite(T value, PCLPointCloud2* cloud)
{
//MSVC is missing bool std::isfinite(IntegralType arg); variant, so we implement an own template specialization for this
if (!std::isfinite(value))
cloud->is_dense = false;
}
template<typename T> inline
std::enable_if_t<std::is_integral<T>::value>
unsetDenseFlagIfNotFinite(T /* value */, PCLPointCloud2* /* cloud */)
{
}
template<typename Scalar> void
PLYReader::vertexScalarPropertyCallback (Scalar value)
{
try
{
unsetDenseFlagIfNotFinite(value, cloud_);
cloud_->at<Scalar>(vertex_count_, vertex_offset_before_) = value;
vertex_offset_before_ += static_cast<int> (sizeof (Scalar));
}
catch(const std::out_of_range&)
{
PCL_WARN ("[pcl::PLYReader::vertexScalarPropertyCallback] Incorrect data index specified (%lu)!\n", vertex_count_ * cloud_->point_step + vertex_offset_before_);
assert(false);
}
}
template <typename SizeType> void
PLYReader::vertexListPropertyBeginCallback (const std::string& name, SizeType size)
{
// Adjust size only once
if (vertex_count_ == 0)
{
auto finder = cloud_->fields.rbegin ();
for (; finder != cloud_->fields.rend (); ++finder)
if (finder->name == name)
break;
assert (finder != cloud_->fields.rend ());
finder->count = size;
}
}
template<typename ContentType> void
PLYReader::vertexListPropertyContentCallback (ContentType value)
{
try
{
unsetDenseFlagIfNotFinite(value, cloud_);
cloud_->at<ContentType>(vertex_count_, vertex_offset_before_) = value;
vertex_offset_before_ += static_cast<int> (sizeof (ContentType));
}
catch(const std::out_of_range&)
{
PCL_WARN ("[pcl::PLYReader::vertexListPropertyContentCallback] Incorrect data index specified (%lu)!\n", vertex_count_ * cloud_->point_step + vertex_offset_before_);
assert(false);
}
}
template <typename SizeType, typename ContentType>
std::tuple<std::function<void (SizeType)>, std::function<void (ContentType)>, std::function<void ()> >
pcl::PLYReader::listPropertyDefinitionCallback (const std::string& element_name, const std::string& property_name)
{
if ((element_name == "range_grid") && (property_name == "vertex_indices" || property_name == "vertex_index"))
{
return std::tuple<std::function<void (SizeType)>, std::function<void (ContentType)>, std::function<void ()> > (
[this] (SizeType size) { rangeGridVertexIndicesBeginCallback (size); },
[this] (ContentType vertex_index) { rangeGridVertexIndicesElementCallback (vertex_index); },
[this] { rangeGridVertexIndicesEndCallback (); }
);
}
if ((element_name == "face") && (property_name == "vertex_indices" || property_name == "vertex_index") && polygons_)
{
return std::tuple<std::function<void (SizeType)>, std::function<void (ContentType)>, std::function<void ()> > (
[this] (SizeType size) { faceVertexIndicesBeginCallback (size); },
[this] (ContentType vertex_index) { faceVertexIndicesElementCallback (vertex_index); },
[this] { faceVertexIndicesEndCallback (); }
);
}
if (element_name == "vertex")
{
cloud_->fields.emplace_back();
pcl::PCLPointField ¤t_field = cloud_->fields.back ();
current_field.name = property_name;
current_field.offset = cloud_->point_step;
current_field.datatype = pcl::traits::asEnum<ContentType>::value;
current_field.count = 1u; // value will be updated once first vertex is read
if (sizeof (ContentType) + cloud_->point_step < std::numeric_limits<std::uint32_t>::max ())
cloud_->point_step += static_cast<std::uint32_t> (sizeof (ContentType));
else
cloud_->point_step = static_cast<std::uint32_t> (std::numeric_limits<std::uint32_t>::max ());
do_resize_ = true;
return std::tuple<std::function<void (SizeType)>, std::function<void (ContentType)>, std::function<void ()> > (
[this, property_name](SizeType size) { this->vertexListPropertyBeginCallback(property_name, size); },
[this] (ContentType value) { vertexListPropertyContentCallback (value); },
[this] { vertexListPropertyEndCallback (); }
);
}
PCL_WARN("[pcl::PLYReader::listPropertyDefinitionCallback] no fitting callbacks. element_name=%s, property_name=%s\n", element_name.c_str(), property_name.c_str());
return {};
}
}
void
pcl::PLYReader::vertexColorCallback (const std::string& color_name, pcl::io::ply::uint8 color)
{
if ((color_name == "red") || (color_name == "diffuse_red"))
{
r_ = static_cast<std::int32_t>(color);
rgb_offset_before_ = vertex_offset_before_;
}
if ((color_name == "green") || (color_name == "diffuse_green"))
{
g_ = static_cast<std::int32_t>(color);
}
if ((color_name == "blue") || (color_name == "diffuse_blue"))
{
b_ = static_cast<std::int32_t>(color);
std::int32_t rgb = r_ << 16 | g_ << 8 | b_;
try
{
cloud_->at<std::int32_t>(vertex_count_, rgb_offset_before_) = rgb;
vertex_offset_before_ += static_cast<int> (sizeof (pcl::io::ply::float32));
}
catch(const std::out_of_range&)
{
PCL_WARN ("[pcl::PLYReader::vertexColorCallback] Incorrect data index specified (%lu)!\n", vertex_count_ * cloud_->point_step + rgb_offset_before_);
assert(false);
}
}
}
void
pcl::PLYReader::vertexAlphaCallback (pcl::io::ply::uint8 alpha)
{
// get anscient rgb value and store it in rgba
rgba_ = cloud_->at<std::uint32_t>(vertex_count_, rgb_offset_before_);
// append alpha
a_ = static_cast<std::uint32_t>(alpha);
rgba_ |= a_ << 24;
// put rgba back
cloud_->at<std::uint32_t>(vertex_count_, rgb_offset_before_) = rgba_;
}
void
pcl::PLYReader::vertexIntensityCallback (pcl::io::ply::uint8 intensity)
{
pcl::io::ply::float32 intensity_ (intensity);
try
{
cloud_->at<pcl::io::ply::float32>(vertex_count_, vertex_offset_before_) = intensity_;
vertex_offset_before_ += static_cast<int> (sizeof (pcl::io::ply::float32));
}
catch(const std::out_of_range&)
{
PCL_WARN ("[pcl::PLYReader::vertexIntensityCallback] Incorrect data index specified (%lu)!\n", vertex_count_ * cloud_->point_step + vertex_offset_before_);
assert(false);
}
}
void
pcl::PLYReader::vertexBeginCallback ()
{
vertex_offset_before_ = 0;
}
void
pcl::PLYReader::vertexEndCallback ()
{
// Resize data if needed
if (vertex_count_ == 0 && do_resize_)
{
cloud_->point_step = vertex_offset_before_;
cloud_->row_step = cloud_->point_step * cloud_->width;
cloud_->data.resize (static_cast<std::size_t>(cloud_->point_step) * cloud_->width * cloud_->height);
}
++vertex_count_;
}
void
pcl::PLYReader::rangeGridBeginCallback ()
{
range_grid_->emplace_back();
}
void
pcl::PLYReader::rangeGridVertexIndicesBeginCallback (pcl::io::ply::uint8 size)
{
range_grid_->back ().reserve (size);
}
void
pcl::PLYReader::rangeGridVertexIndicesElementCallback (pcl::io::ply::int32 vertex_index)
{
range_grid_->back ().push_back (vertex_index);
}
void
pcl::PLYReader::rangeGridVertexIndicesEndCallback () {}
void
pcl::PLYReader::rangeGridEndCallback () {}
void
pcl::PLYReader::faceBeginCallback ()
{
polygons_->emplace_back();
}
void
pcl::PLYReader::faceVertexIndicesBeginCallback (pcl::io::ply::uint8 size)
{
polygons_->back ().vertices.reserve (size);
}
void
pcl::PLYReader::faceVertexIndicesElementCallback (pcl::io::ply::int32 vertex_index)
{
polygons_->back ().vertices.push_back (vertex_index);
}
void
pcl::PLYReader::faceVertexIndicesEndCallback () { }
void
pcl::PLYReader::faceEndCallback () {}
void
pcl::PLYReader::objInfoCallback (const std::string& line)
{
std::vector<std::string> st;
boost::split (st, line, boost::is_any_of (std::string ( "\t ")), boost::token_compress_on);
assert (st[0].substr (0, 8) == "obj_info");
{
if (st.size() >= 3)
{
if (st[1] == "num_cols")
cloudWidthCallback (atoi (st[2].c_str ()));
else if (st[1] == "num_rows")
cloudHeightCallback (atoi (st[2].c_str ()));
else if (st[1] == "echo_rgb_offset_x")
originXCallback (static_cast<float> (atof (st[2].c_str ())));
else if (st[1] == "echo_rgb_offset_y")
originYCallback (static_cast<float> (atof (st[2].c_str ())));
else if (st[1] == "echo_rgb_offset_z")
originZCallback (static_cast<float> (atof (st[2].c_str ())));
}
}
}
void
pcl::PLYReader::vertexListPropertyEndCallback () {}
bool
pcl::PLYReader::parse (const std::string& istream_filename)
{
pcl::io::ply::ply_parser ply_parser;
ply_parser.info_callback ([&, this] (std::size_t line_number, const std::string& message) { infoCallback (istream_filename, line_number, message); });
ply_parser.warning_callback ([&, this] (std::size_t line_number, const std::string& message) { warningCallback (istream_filename, line_number, message); });
ply_parser.error_callback ([&, this] (std::size_t line_number, const std::string& message) { errorCallback (istream_filename, line_number, message); });
ply_parser.obj_info_callback ([this] (const std::string& line) { objInfoCallback (line); });
ply_parser.element_definition_callback ([this] (const std::string& element_name, std::size_t count) { return elementDefinitionCallback (element_name, count); });
ply_parser.end_header_callback ([this] { return endHeaderCallback (); });
pcl::io::ply::ply_parser::scalar_property_definition_callbacks_type scalar_property_definition_callbacks;
pcl::io::ply::ply_parser::at<pcl::io::ply::float64> (scalar_property_definition_callbacks) = [this] (const std::string& element_name, const std::string& property_name) { return scalarPropertyDefinitionCallback<pcl::io::ply::float64> (element_name, property_name); };
pcl::io::ply::ply_parser::at<pcl::io::ply::float32> (scalar_property_definition_callbacks) = [this] (const std::string& element_name, const std::string& property_name) { return scalarPropertyDefinitionCallback<pcl::io::ply::float32> (element_name, property_name); };
pcl::io::ply::ply_parser::at<pcl::io::ply::int8> (scalar_property_definition_callbacks) = [this] (const std::string& element_name, const std::string& property_name) { return scalarPropertyDefinitionCallback<pcl::io::ply::int8> (element_name, property_name); };
pcl::io::ply::ply_parser::at<pcl::io::ply::uint8> (scalar_property_definition_callbacks) = [this] (const std::string& element_name, const std::string& property_name) { return scalarPropertyDefinitionCallback<pcl::io::ply::uint8> (element_name, property_name); };
pcl::io::ply::ply_parser::at<pcl::io::ply::int32> (scalar_property_definition_callbacks) = [this] (const std::string& element_name, const std::string& property_name) { return scalarPropertyDefinitionCallback<pcl::io::ply::int32> (element_name, property_name); };
pcl::io::ply::ply_parser::at<pcl::io::ply::uint32> (scalar_property_definition_callbacks) = [this] (const std::string& element_name, const std::string& property_name) { return scalarPropertyDefinitionCallback<pcl::io::ply::uint32> (element_name, property_name); };
pcl::io::ply::ply_parser::at<pcl::io::ply::int16> (scalar_property_definition_callbacks) = [this] (const std::string& element_name, const std::string& property_name) { return scalarPropertyDefinitionCallback<pcl::io::ply::int16> (element_name, property_name); };
pcl::io::ply::ply_parser::at<pcl::io::ply::uint16> (scalar_property_definition_callbacks) = [this] (const std::string& element_name, const std::string& property_name) { return scalarPropertyDefinitionCallback<pcl::io::ply::uint16> (element_name, property_name); };
ply_parser.scalar_property_definition_callbacks (scalar_property_definition_callbacks);
pcl::io::ply::ply_parser::list_property_definition_callbacks_type list_property_definition_callbacks;
pcl::io::ply::ply_parser::at<pcl::io::ply::uint8, pcl::io::ply::int32> (list_property_definition_callbacks) = [this] (const std::string& element_name, const std::string& property_name) { return listPropertyDefinitionCallback<pcl::io::ply::uint8, pcl::io::ply::int32> (element_name, property_name); };
pcl::io::ply::ply_parser::at<pcl::io::ply::uint8, pcl::io::ply::uint32> (list_property_definition_callbacks) = [this] (const std::string& element_name, const std::string& property_name) { return listPropertyDefinitionCallback<pcl::io::ply::uint8, pcl::io::ply::int32> (element_name, property_name); };
pcl::io::ply::ply_parser::at<pcl::io::ply::uint32, pcl::io::ply::float64> (list_property_definition_callbacks) = [this] (const std::string& element_name, const std::string& property_name) { return listPropertyDefinitionCallback<pcl::io::ply::uint32, pcl::io::ply::float64> (element_name, property_name); };
pcl::io::ply::ply_parser::at<pcl::io::ply::uint32, pcl::io::ply::float32> (list_property_definition_callbacks) = [this] (const std::string& element_name, const std::string& property_name) { return listPropertyDefinitionCallback<pcl::io::ply::uint32, pcl::io::ply::float32> (element_name, property_name); };
pcl::io::ply::ply_parser::at<pcl::io::ply::uint32, pcl::io::ply::uint32> (list_property_definition_callbacks) = [this] (const std::string& element_name, const std::string& property_name) { return listPropertyDefinitionCallback<pcl::io::ply::uint32, pcl::io::ply::uint32> (element_name, property_name); };
pcl::io::ply::ply_parser::at<pcl::io::ply::uint32, pcl::io::ply::int32> (list_property_definition_callbacks) = [this] (const std::string& element_name, const std::string& property_name) { return listPropertyDefinitionCallback<pcl::io::ply::uint32, pcl::io::ply::int32> (element_name, property_name); };
pcl::io::ply::ply_parser::at<pcl::io::ply::uint32, pcl::io::ply::uint16> (list_property_definition_callbacks) = [this] (const std::string& element_name, const std::string& property_name) { return listPropertyDefinitionCallback<pcl::io::ply::uint32, pcl::io::ply::uint16> (element_name, property_name); };
pcl::io::ply::ply_parser::at<pcl::io::ply::uint32, pcl::io::ply::int16> (list_property_definition_callbacks) = [this] (const std::string& element_name, const std::string& property_name) { return listPropertyDefinitionCallback<pcl::io::ply::uint32, pcl::io::ply::int16> (element_name, property_name); };
pcl::io::ply::ply_parser::at<pcl::io::ply::uint32, pcl::io::ply::uint8> (list_property_definition_callbacks) = [this] (const std::string& element_name, const std::string& property_name) { return listPropertyDefinitionCallback<pcl::io::ply::uint32, pcl::io::ply::uint8> (element_name, property_name); };
pcl::io::ply::ply_parser::at<pcl::io::ply::uint32, pcl::io::ply::int8> (list_property_definition_callbacks) = [this] (const std::string& element_name, const std::string& property_name) { return listPropertyDefinitionCallback<pcl::io::ply::uint32, pcl::io::ply::int8> (element_name, property_name); };
ply_parser.list_property_definition_callbacks (list_property_definition_callbacks);
return ply_parser.parse (istream_filename);
}
////////////////////////////////////////////////////////////////////////////////////////
int
pcl::PLYReader::readHeader (const std::string &file_name, pcl::PCLPointCloud2 &cloud,
Eigen::Vector4f &origin, Eigen::Quaternionf &orientation,
int &, int &, unsigned int &, const int)
{
// Silence compiler warnings
cloud_ = &cloud;
range_grid_ = new std::vector<std::vector<int> >;
cloud_->width = cloud_->height = 0;
origin = Eigen::Vector4f::Zero ();
orientation = Eigen::Quaternionf::Identity ();
origin_ = Eigen::Vector4f::Zero ();
orientation_ = Eigen::Matrix3f::Identity ();
if (!parse (file_name))
{
PCL_ERROR ("[pcl::PLYReader::readHeader] problem parsing header!\n");
return (-1);
}
cloud_->row_step = cloud_->point_step * cloud_->width;
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////
int
pcl::PLYReader::read (const std::string &file_name, pcl::PCLPointCloud2 &cloud,
Eigen::Vector4f &origin, Eigen::Quaternionf &orientation, int &ply_version, const int)
{
// kept only for backward compatibility
int data_type;
unsigned int data_idx;
if (!pcl_fs::exists (file_name))
{
PCL_ERROR ("[pcl::PLYReader::read] File (%s) not found!\n",file_name.c_str ());
return (-1);
}
if (this->readHeader (file_name, cloud, origin, orientation, ply_version, data_type, data_idx))
{
PCL_ERROR ("[pcl::PLYReader::read] problem parsing header!\n");
return (-1);
}
// a range_grid element was found ?
std::size_t r_size;
if ((r_size = (*range_grid_).size ()) > 0 && r_size != vertex_count_)
{
//cloud.header = cloud_->header;
std::vector<std::uint8_t> data ((*range_grid_).size () * cloud.point_step);
const static float f_nan = std::numeric_limits <float>::quiet_NaN ();
const static double d_nan = std::numeric_limits <double>::quiet_NaN ();
for (std::size_t r = 0; r < r_size; ++r)
{
if ((*range_grid_)[r].empty ())
{
for (const auto &field : cloud_->fields)
if (field.datatype == ::pcl::PCLPointField::FLOAT32)
{
const auto idx = r * cloud_->point_step + field.offset;
if (idx + sizeof (float) > data.size())
{
PCL_ERROR ("[pcl::PLYReader::read] invalid data index (%lu)!\n", idx);
return (-1);
}
memcpy (&data[idx],
reinterpret_cast<const char*> (&f_nan), sizeof (float));
}
else if (field.datatype == ::pcl::PCLPointField::FLOAT64)
{
const auto idx = r * cloud_->point_step + field.offset;
if (idx + sizeof (double) > data.size())
{
PCL_ERROR ("[pcl::PLYReader::read] invalid data index (%lu)!\n", idx);
return (-1);
}
memcpy (&data[idx],
reinterpret_cast<const char*> (&d_nan), sizeof (double));
}
else
{
const auto idx = r * cloud_->point_step + field.offset;
if (idx + pcl::getFieldSize (field.datatype) * field.count > data.size())
{
PCL_ERROR ("[pcl::PLYReader::read] invalid data index (%lu)!\n", idx);
return (-1);
}
std::fill_n(&data[idx],
pcl::getFieldSize (field.datatype) * field.count, 0);
}
}
else
{
const std::size_t srcIdx = (*range_grid_)[r][0] * cloud_->point_step;
if (srcIdx + cloud_->point_step > cloud_->data.size())
{
PCL_ERROR ("[pcl::PLYReader::read] invalid data index (%lu)!\n", srcIdx);
return (-1);
}
memcpy (&data[r* cloud_->point_step], &cloud_->data[srcIdx], cloud_->point_step);
}
}
cloud_->data.swap (data);
}
orientation = Eigen::Quaternionf (orientation_);
origin = origin_;
for (auto &field : cloud_->fields)
{
if (field.name == "nx")
field.name = "normal_x";
if (field.name == "ny")
field.name = "normal_y";
if (field.name == "nz")
field.name = "normal_z";
}
return (0);
}
////////////////////////////////////////////////////////////////////////////////////////
int
pcl::PLYReader::read (const std::string &file_name, pcl::PolygonMesh &mesh,
Eigen::Vector4f &origin, Eigen::Quaternionf &orientation,
int &ply_version, const int offset)
{
PCL_DEBUG("[pcl::PLYReader::read] Reading PolygonMesh from file: %s.\n", file_name.c_str());
// kept only for backward compatibility
int data_type;
unsigned int data_idx;
polygons_ = &(mesh.polygons);
if (!pcl_fs::exists (file_name))
{
PCL_ERROR ("[pcl::PLYReader::read] File (%s) not found!\n",file_name.c_str ());
return (-1);
}
if (this->readHeader (file_name, mesh.cloud, origin, orientation, ply_version, data_type, data_idx, offset))
{
PCL_ERROR ("[pcl::PLYReader::read] problem parsing header!\n");
return (-1);
}
// a range_grid element was found ?
std::size_t r_size;
if ((r_size = (*range_grid_).size ()) > 0 && r_size != vertex_count_)
{
//cloud.header = cloud_->header;
std::vector<std::uint8_t> data ((*range_grid_).size () * mesh.cloud.point_step);
const static float f_nan = std::numeric_limits <float>::quiet_NaN ();
const static double d_nan = std::numeric_limits <double>::quiet_NaN ();
for (std::size_t r = 0; r < r_size; ++r)
{
if ((*range_grid_)[r].empty ())
{
for (const auto &field : cloud_->fields)
if (field.datatype == ::pcl::PCLPointField::FLOAT32)
{
const auto idx = r * cloud_->point_step + field.offset;
if (idx + sizeof (float) > data.size())
{
PCL_ERROR ("[pcl::PLYReader::read] invalid data index (%lu)!\n", idx);
return (-1);
}
memcpy (&data[idx],
reinterpret_cast<const char*> (&f_nan), sizeof (float));
}
else if (field.datatype == ::pcl::PCLPointField::FLOAT64)
{
const auto idx = r * cloud_->point_step + field.offset;
if (idx + sizeof (double) > data.size())
{
PCL_ERROR ("[pcl::PLYReader::read] invalid data index (%lu)!\n", idx);
return (-1);
}
memcpy (&data[idx],
reinterpret_cast<const char*> (&d_nan), sizeof (double));
}
else
{
const auto idx = r * cloud_->point_step + field.offset;
if (idx + pcl::getFieldSize (field.datatype) * field.count > data.size())
{
PCL_ERROR ("[pcl::PLYReader::read] invalid data index (%lu)!\n", idx);
return (-1);
}
std::fill_n(&data[idx],
pcl::getFieldSize (field.datatype) * field.count, 0);
}
}
else
{
const std::size_t srcIdx = (*range_grid_)[r][0] * cloud_->point_step;
if (srcIdx + cloud_->point_step > cloud_->data.size())
{
PCL_ERROR ("[pcl::PLYReader::read] invalid data index (%lu)!\n", srcIdx);
return (-1);
}
memcpy (&data[r* cloud_->point_step], &cloud_->data[srcIdx], cloud_->point_step);
}
}
cloud_->data.swap (data);
}
orientation = Eigen::Quaternionf (orientation_);
origin = origin_;
for (auto &field : cloud_->fields)
{
if (field.name == "nx")
field.name = "normal_x";
if (field.name == "ny")
field.name = "normal_y";
if (field.name == "nz")
field.name = "normal_z";
}
return (0);
}
////////////////////////////////////////////////////////////////////////////////////////
int
pcl::PLYReader::read (const std::string &file_name, pcl::PolygonMesh &mesh, const int offset)
{
Eigen::Vector4f origin;
Eigen::Quaternionf orientation;
int ply_version;
return read (file_name, mesh, origin, orientation, ply_version, offset);
}
////////////////////////////////////////////////////////////////////////////////////////
std::string
pcl::PLYWriter::generateHeader (const pcl::PCLPointCloud2 &cloud,
const Eigen::Vector4f &origin,
const Eigen::Quaternionf &,
bool binary,
bool use_camera,
int valid_points)
{
std::ostringstream oss;
oss.imbue (std::locale::classic ()); // mostly to make sure that no thousands separator is printed
// Begin header
oss << "ply";
if (!binary)
oss << "\nformat ascii 1.0";
else
{
if (cloud.is_bigendian)
oss << "\nformat binary_big_endian 1.0";
else
oss << "\nformat binary_little_endian 1.0";
}
oss << "\ncomment PCL generated";
if (!use_camera)
{
oss << "\nobj_info is_cyberware_data 0"
"\nobj_info is_mesh 0"
"\nobj_info is_warped 0"
"\nobj_info is_interlaced 0";
oss << "\nobj_info num_cols " << cloud.width;
oss << "\nobj_info num_rows " << cloud.height;
oss << "\nobj_info echo_rgb_offset_x " << origin[0];
oss << "\nobj_info echo_rgb_offset_y " << origin[1];
oss << "\nobj_info echo_rgb_offset_z " << origin[2];
oss << "\nobj_info echo_rgb_frontfocus 0.0"
"\nobj_info echo_rgb_backfocus 0.0"
"\nobj_info echo_rgb_pixelsize 0.0"
"\nobj_info echo_rgb_centerpixel 0"
"\nobj_info echo_frames 1"
"\nobj_info echo_lgincr 0.0";
}
oss << "\nelement vertex "<< valid_points;
for (const auto &field : cloud.fields)
{
if (field.name == "normal_x")
{
oss << "\nproperty float nx";
}
else if (field.name == "normal_y")
{
oss << "\nproperty float ny";
}
else if (field.name == "normal_z")
{
oss << "\nproperty float nz";
}
else if (field.name == "rgb")
{
oss << "\nproperty uchar red"
"\nproperty uchar green"
"\nproperty uchar blue";
}
else if (field.name == "rgba")
{
oss << "\nproperty uchar red"
"\nproperty uchar green"
"\nproperty uchar blue"
"\nproperty uchar alpha";
}
else
{
oss << "\nproperty";
if (field.count != 1)
oss << " list uint";
switch (field.datatype)
{
case pcl::PCLPointField::INT8 : oss << " char "; break;
case pcl::PCLPointField::UINT8 : oss << " uchar "; break;
case pcl::PCLPointField::INT16 : oss << " short "; break;
case pcl::PCLPointField::UINT16 : oss << " ushort "; break;
case pcl::PCLPointField::INT32 : oss << " int "; break;
case pcl::PCLPointField::UINT32 : oss << " uint "; break;
case pcl::PCLPointField::FLOAT32 : oss << " float "; break;
case pcl::PCLPointField::FLOAT64 : oss << " double "; break;
default :
{
PCL_ERROR ("[pcl::PLYWriter::generateHeader] unknown data field type!\n");
return ("");
}
}
oss << field.name;
}
}
// vtk requires face entry to load PLY
oss << "\nelement face 0";
if (use_camera)
{
oss << "\nelement camera 1"
"\nproperty float view_px"
"\nproperty float view_py"
"\nproperty float view_pz"
"\nproperty float x_axisx"
"\nproperty float x_axisy"
"\nproperty float x_axisz"
"\nproperty float y_axisx"
"\nproperty float y_axisy"
"\nproperty float y_axisz"
"\nproperty float z_axisx"
"\nproperty float z_axisy"
"\nproperty float z_axisz"
"\nproperty float focal"
"\nproperty float scalex"
"\nproperty float scaley"
"\nproperty float centerx"
"\nproperty float centery"
"\nproperty int viewportx"
"\nproperty int viewporty"
"\nproperty float k1"
"\nproperty float k2";
}
else if (cloud.height > 1)
{
oss << "\nelement range_grid " << cloud.width * cloud.height;
oss << "\nproperty list uchar int vertex_indices";
}
// End header
oss << "\nend_header\n";
return (oss.str ());
}
////////////////////////////////////////////////////////////////////////////////////////
int
pcl::PLYWriter::writeASCII (const std::string &file_name,
const pcl::PCLPointCloud2 &cloud,
const Eigen::Vector4f &origin,
const Eigen::Quaternionf &orientation,
int precision,
bool use_camera)
{
if (cloud.data.empty ())
{
PCL_ERROR ("[pcl::PLYWriter::writeASCII] Input point cloud has no data!\n");
return (-1);
}
std::ofstream fs;
fs.precision (precision);
// Open file
fs.open (file_name.c_str ());
if (!fs)
{
PCL_ERROR ("[pcl::PLYWriter::writeASCII] Error during opening (%s)!\n", file_name.c_str ());
return (-1);
}
unsigned int nr_points = cloud.width * cloud.height;
// Write the header information if available
if (use_camera)
{
fs << generateHeader (cloud, origin, orientation, false, use_camera, nr_points);
writeContentWithCameraASCII (nr_points, cloud, origin, orientation, fs);
}
else
{
std::ostringstream os;
int nr_valid_points;
writeContentWithRangeGridASCII (nr_points, cloud, os, nr_valid_points);
fs << generateHeader (cloud, origin, orientation, false, use_camera, nr_valid_points);
fs << os.str ();
}
// Close file
fs.close ();
return (0);
}
void
pcl::PLYWriter::writeContentWithCameraASCII (int nr_points,
const pcl::PCLPointCloud2 &cloud,
const Eigen::Vector4f &origin,
const Eigen::Quaternionf &orientation,
std::ofstream& fs)
{
// Iterate through the points
for (int i = 0; i < nr_points; ++i)
{
for (std::size_t d = 0; d < cloud.fields.size (); ++d)
{
int count = cloud.fields[d].count;
if (count == 0)
count = 1; //workaround
if (count > 1)
fs << count << " ";
for (int c = 0; c < count; ++c)
{
switch (cloud.fields[d].datatype)
{
case pcl::PCLPointField::INT8:
{
fs << boost::numeric_cast<int> (cloud.at<char>(i, cloud.fields[d].offset + c * sizeof (char)));
break;
}
case pcl::PCLPointField::UINT8:
{
fs << boost::numeric_cast<int> (cloud.at<unsigned char>(i, cloud.fields[d].offset + c * sizeof (unsigned char)));