-
Notifications
You must be signed in to change notification settings - Fork 68
/
happly.h
2017 lines (1708 loc) · 64 KB
/
happly.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
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
#pragma once
/* A header-only implementation of the .ply file format.
* https://github.com/nmwsharp/happly
* By Nicholas Sharp - [email protected]
*
* Version 2, July 20, 2019
*/
/*
MIT License
Copyright (c) 2018 Nick Sharp
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// clang-format off
/*
=== Changelog ===
Significant changes to the file recorded here.
- Version 5 (Aug 22, 2020) Minor: skip blank lines before properties in ASCII files
- Version 4 (Sep 11, 2019) Change internal list format to be flat. Other small perf fixes and cleanup.
- Version 3 (Aug 1, 2019) Add support for big endian and obj_info
- Version 2 (July 20, 2019) Catch exceptions by const reference.
- Version 1 (undated) Initial version. Unnamed changes before version numbering.
*/
// clang-format on
#include <array>
#include <cctype>
#include <fstream>
#include <iostream>
#include <limits>
#include <memory>
#include <sstream>
#include <string>
#include <type_traits>
#include <vector>
#include <climits>
// General namespace wrapping all Happly things.
namespace happly {
// Enum specifying binary or ASCII filetypes. Binary can be little-endian
// (default) or big endian.
enum class DataFormat { ASCII, Binary, BinaryBigEndian };
// Type name strings
// clang-format off
template <typename T> std::string typeName() { return "unknown"; }
template<> inline std::string typeName<int8_t>() { return "char"; }
template<> inline std::string typeName<uint8_t>() { return "uchar"; }
template<> inline std::string typeName<int16_t>() { return "short"; }
template<> inline std::string typeName<uint16_t>() { return "ushort"; }
template<> inline std::string typeName<int32_t>() { return "int"; }
template<> inline std::string typeName<uint32_t>() { return "uint"; }
template<> inline std::string typeName<float>() { return "float"; }
template<> inline std::string typeName<double>() { return "double"; }
// Template hackery that makes getProperty<T>() and friends pretty while automatically picking up smaller types
namespace {
// A pointer for the equivalent/smaller equivalent of a type (eg. when a double is requested a float works too, etc)
// long int is intentionally absent to avoid platform confusion
template <class T> struct TypeChain { bool hasChildType = false; typedef T type; };
template <> struct TypeChain<int64_t> { bool hasChildType = true; typedef int32_t type; };
template <> struct TypeChain<int32_t> { bool hasChildType = true; typedef int16_t type; };
template <> struct TypeChain<int16_t> { bool hasChildType = true; typedef int8_t type; };
template <> struct TypeChain<uint64_t> { bool hasChildType = true; typedef uint32_t type; };
template <> struct TypeChain<uint32_t> { bool hasChildType = true; typedef uint16_t type; };
template <> struct TypeChain<uint16_t> { bool hasChildType = true; typedef uint8_t type; };
template <> struct TypeChain<double> { bool hasChildType = true; typedef float type; };
template <class T> struct CanonicalName { typedef T type; };
template <> struct CanonicalName<char> { typedef int8_t type; };
template <> struct CanonicalName<unsigned char> { typedef uint8_t type; };
template <> struct CanonicalName<size_t> { typedef std::conditional<std::is_same<std::make_signed<size_t>::type, int>::value, uint32_t, uint64_t>::type type; };
// Used to change behavior of >> for 8bit ints, which does not do what we want.
template <class T> struct SerializeType { typedef T type; };
template <> struct SerializeType<uint8_t> { typedef int32_t type; };
template <> struct SerializeType< int8_t> { typedef int32_t type; };
// Give address only if types are same (used below when conditionally copying data)
// last int/char arg is to resolve ambiguous overloads, just always pass 0 and the int version will be preferred
template <typename S, typename T>
S* addressIfSame(T&, char) {
throw std::runtime_error("tried to take address for types that are not same");
return nullptr;}
template <typename S>
S* addressIfSame(S& t, int) {return &t;}
// clang-format on
} // namespace
/**
* @brief A generic property, which is associated with some element. Can be plain Property or a ListProperty, of some
* type. Generally, the user should not need to interact with these directly, but they are exposed in case someone
* wants to get clever.
*/
class Property {
public:
/**
* @brief Create a new Property with the given name.
*
* @param name_
*/
Property(const std::string& name_) : name(name_){};
virtual ~Property(){};
std::string name;
/**
* @brief Reserve memory.
*
* @param capacity Expected number of elements.
*/
virtual void reserve(size_t capacity) = 0;
/**
* @brief (ASCII reading) Parse out the next value of this property from a list of tokens.
*
* @param tokens The list of property tokens for the element.
* @param currEntry Index in to tokens, updated after this property is read.
*/
virtual void parseNext(const std::vector<std::string>& tokens, size_t& currEntry) = 0;
/**
* @brief (binary reading) Copy the next value of this property from a stream of bits.
*
* @param stream Stream to read from.
*/
virtual void readNext(std::istream& stream) = 0;
/**
* @brief (binary reading) Copy the next value of this property from a stream of bits.
*
* @param stream Stream to read from.
*/
virtual void readNextBigEndian(std::istream& stream) = 0;
/**
* @brief (reading) Write a header entry for this property.
*
* @param outStream Stream to write to.
*/
virtual void writeHeader(std::ostream& outStream) = 0;
/**
* @brief (ASCII writing) write this property for some element to a stream in plaintext
*
* @param outStream Stream to write to.
* @param iElement index of the element to write.
*/
virtual void writeDataASCII(std::ostream& outStream, size_t iElement) = 0;
/**
* @brief (binary writing) copy the bits of this property for some element to a stream
*
* @param outStream Stream to write to.
* @param iElement index of the element to write.
*/
virtual void writeDataBinary(std::ostream& outStream, size_t iElement) = 0;
/**
* @brief (binary writing) copy the bits of this property for some element to a stream
*
* @param outStream Stream to write to.
* @param iElement index of the element to write.
*/
virtual void writeDataBinaryBigEndian(std::ostream& outStream, size_t iElement) = 0;
/**
* @brief Number of element entries for this property
*
* @return
*/
virtual size_t size() = 0;
/**
* @brief A string naming the type of the property
*
* @return
*/
virtual std::string propertyTypeName() = 0;
};
namespace {
/**
* Check if the platform is little endian.
* (not foolproof, but will work on most platforms)
*
* @return true if little endian
*/
bool isLittleEndian() {
int32_t oneVal = 0x1;
char* numPtr = (char*)&oneVal;
return (numPtr[0] == 1);
}
/**
* Swap endianness.
*
* @param value Value to swap.
*
* @return Swapped value.
*/
template <typename T>
T swapEndian(T val) {
char* bytes = reinterpret_cast<char*>(&val);
for (unsigned int i = 0; i < sizeof(val) / 2; i++) {
std::swap(bytes[sizeof(val) - 1 - i], bytes[i]);
}
return val;
}
// The following specializations for single-byte types are used to avoid compiler warnings.
template <> int8_t swapEndian<int8_t>(int8_t val) { return val; }
template <> uint8_t swapEndian<uint8_t>(uint8_t val) { return val; }
// Unpack flattened list from the convention used in TypedListProperty
template <typename T>
std::vector<std::vector<T>> unflattenList(const std::vector<T>& flatList, const std::vector<size_t> flatListStarts) {
size_t outerCount = flatListStarts.size() - 1;
// Put the output here
std::vector<std::vector<T>> outLists(outerCount);
if (outerCount == 0) {
return outLists; // quick out for empty
}
// Copy each sublist
for (size_t iOuter = 0; iOuter < outerCount; iOuter++) {
size_t iFlatStart = flatListStarts[iOuter];
size_t iFlatEnd = flatListStarts[iOuter + 1];
outLists[iOuter].insert(outLists[iOuter].begin(), flatList.begin() + iFlatStart, flatList.begin() + iFlatEnd);
}
return outLists;
}
}; // namespace
/**
* @brief A property which takes a single value (not a list).
*/
template <class T>
class TypedProperty : public Property {
public:
/**
* @brief Create a new Property with the given name.
*
* @param name_
*/
TypedProperty(const std::string& name_) : Property(name_) {
if (typeName<T>() == "unknown") {
// TODO should really be a compile-time error
throw std::runtime_error("Attempted property type does not match any type defined by the .ply format.");
}
};
/**
* @brief Create a new property and initialize with data.
*
* @param name_
* @param data_
*/
TypedProperty(const std::string& name_, const std::vector<T>& data_) : Property(name_), data(data_) {
if (typeName<T>() == "unknown") {
throw std::runtime_error("Attempted property type does not match any type defined by the .ply format.");
}
};
virtual ~TypedProperty() override{};
/**
* @brief Reserve memory.
*
* @param capacity Expected number of elements.
*/
virtual void reserve(size_t capacity) override { data.reserve(capacity); }
/**
* @brief (ASCII reading) Parse out the next value of this property from a list of tokens.
*
* @param tokens The list of property tokens for the element.
* @param currEntry Index in to tokens, updated after this property is read.
*/
virtual void parseNext(const std::vector<std::string>& tokens, size_t& currEntry) override {
data.emplace_back();
std::istringstream iss(tokens[currEntry]);
typename SerializeType<T>::type tmp; // usually the same type as T
iss >> tmp;
data.back() = tmp;
currEntry++;
};
/**
* @brief (binary reading) Copy the next value of this property from a stream of bits.
*
* @param stream Stream to read from.
*/
virtual void readNext(std::istream& stream) override {
data.emplace_back();
stream.read((char*)&data.back(), sizeof(T));
}
/**
* @brief (binary reading) Copy the next value of this property from a stream of bits.
*
* @param stream Stream to read from.
*/
virtual void readNextBigEndian(std::istream& stream) override {
data.emplace_back();
stream.read((char*)&data.back(), sizeof(T));
data.back() = swapEndian(data.back());
}
/**
* @brief (reading) Write a header entry for this property.
*
* @param outStream Stream to write to.
*/
virtual void writeHeader(std::ostream& outStream) override {
outStream << "property " << typeName<T>() << " " << name << "\n";
}
/**
* @brief (ASCII writing) write this property for some element to a stream in plaintext
*
* @param outStream Stream to write to.
* @param iElement index of the element to write.
*/
virtual void writeDataASCII(std::ostream& outStream, size_t iElement) override {
outStream.precision(std::numeric_limits<T>::max_digits10);
outStream << static_cast<typename SerializeType<T>::type>(data[iElement]); // case is usually a no-op
}
/**
* @brief (binary writing) copy the bits of this property for some element to a stream
*
* @param outStream Stream to write to.
* @param iElement index of the element to write.
*/
virtual void writeDataBinary(std::ostream& outStream, size_t iElement) override {
outStream.write((char*)&data[iElement], sizeof(T));
}
/**
* @brief (binary writing) copy the bits of this property for some element to a stream
*
* @param outStream Stream to write to.
* @param iElement index of the element to write.
*/
virtual void writeDataBinaryBigEndian(std::ostream& outStream, size_t iElement) override {
auto value = swapEndian(data[iElement]);
outStream.write((char*)&value, sizeof(T));
}
/**
* @brief Number of element entries for this property
*
* @return
*/
virtual size_t size() override { return data.size(); }
/**
* @brief A string naming the type of the property
*
* @return
*/
virtual std::string propertyTypeName() override { return typeName<T>(); }
/**
* @brief The actual data contained in the property
*/
std::vector<T> data;
};
/**
* @brief A property which is a list of value (eg, 3 doubles). Note that lists are always variable length per-element.
*/
template <class T>
class TypedListProperty : public Property {
public:
/**
* @brief Create a new Property with the given name.
*
* @param name_
*/
TypedListProperty(const std::string& name_, int listCountBytes_) : Property(name_), listCountBytes(listCountBytes_) {
if (typeName<T>() == "unknown") {
throw std::runtime_error("Attempted property type does not match any type defined by the .ply format.");
}
flattenedIndexStart.push_back(0);
};
/**
* @brief Create a new property and initialize with data
*
* @param name_
* @param data_
*/
TypedListProperty(const std::string& name_, const std::vector<std::vector<T>>& data_) : Property(name_) {
if (typeName<T>() == "unknown") {
throw std::runtime_error("Attempted property type does not match any type defined by the .ply format.");
}
// Populate list with data
flattenedIndexStart.push_back(0);
for (const std::vector<T>& vec : data_) {
for (const T& val : vec) {
flattenedData.emplace_back(val);
}
flattenedIndexStart.push_back(flattenedData.size());
}
};
virtual ~TypedListProperty() override{};
/**
* @brief Reserve memory.
*
* @param capacity Expected number of elements.
*/
virtual void reserve(size_t capacity) override {
flattenedData.reserve(3 * capacity); // optimize for triangle meshes
flattenedIndexStart.reserve(capacity + 1);
}
/**
* @brief (ASCII reading) Parse out the next value of this property from a list of tokens.
*
* @param tokens The list of property tokens for the element.
* @param currEntry Index in to tokens, updated after this property is read.
*/
virtual void parseNext(const std::vector<std::string>& tokens, size_t& currEntry) override {
std::istringstream iss(tokens[currEntry]);
size_t count;
iss >> count;
currEntry++;
size_t currSize = flattenedData.size();
size_t afterSize = currSize + count;
flattenedData.resize(afterSize);
for (size_t iFlat = currSize; iFlat < afterSize; iFlat++) {
std::istringstream iss(tokens[currEntry]);
typename SerializeType<T>::type tmp; // usually the same type as T
iss >> tmp;
flattenedData[iFlat] = tmp;
currEntry++;
}
flattenedIndexStart.emplace_back(afterSize);
}
/**
* @brief (binary reading) Copy the next value of this property from a stream of bits.
*
* @param stream Stream to read from.
*/
virtual void readNext(std::istream& stream) override {
// Read the size of the list
size_t count = 0;
stream.read(((char*)&count), listCountBytes);
// Read list elements
size_t currSize = flattenedData.size();
size_t afterSize = currSize + count;
flattenedData.resize(afterSize);
if (count > 0) {
stream.read((char*)&flattenedData[currSize], count * sizeof(T));
}
flattenedIndexStart.emplace_back(afterSize);
}
/**
* @brief (binary reading) Copy the next value of this property from a stream of bits.
*
* @param stream Stream to read from.
*/
virtual void readNextBigEndian(std::istream& stream) override {
// Read the size of the list
size_t count = 0;
stream.read(((char*)&count), listCountBytes);
if (listCountBytes == 8) {
count = (size_t)swapEndian((uint64_t)count);
} else if (listCountBytes == 4) {
count = (size_t)swapEndian((uint32_t)count);
} else if (listCountBytes == 2) {
count = (size_t)swapEndian((uint16_t)count);
}
// Read list elements
size_t currSize = flattenedData.size();
size_t afterSize = currSize + count;
flattenedData.resize(afterSize);
if (count > 0) {
stream.read((char*)&flattenedData[currSize], count * sizeof(T));
}
flattenedIndexStart.emplace_back(afterSize);
// Swap endian order of list elements
for (size_t iFlat = currSize; iFlat < afterSize; iFlat++) {
flattenedData[iFlat] = swapEndian(flattenedData[iFlat]);
}
}
/**
* @brief (reading) Write a header entry for this property. Note that we already use "uchar" for the list count type.
*
* @param outStream Stream to write to.
*/
virtual void writeHeader(std::ostream& outStream) override {
// NOTE: We ALWAYS use uchar as the list count output type
outStream << "property list uchar " << typeName<T>() << " " << name << "\n";
}
/**
* @brief (ASCII writing) write this property for some element to a stream in plaintext
*
* @param outStream Stream to write to.
* @param iElement index of the element to write.
*/
virtual void writeDataASCII(std::ostream& outStream, size_t iElement) override {
size_t dataStart = flattenedIndexStart[iElement];
size_t dataEnd = flattenedIndexStart[iElement + 1];
// Get the number of list elements as a uchar, and ensure the value fits
size_t dataCount = dataEnd - dataStart;
if (dataCount > std::numeric_limits<uint8_t>::max()) {
throw std::runtime_error(
"List property has an element with more entries than fit in a uchar. See note in README.");
}
outStream << dataCount;
outStream.precision(std::numeric_limits<T>::max_digits10);
for (size_t iFlat = dataStart; iFlat < dataEnd; iFlat++) {
outStream << " " << static_cast<typename SerializeType<T>::type>(flattenedData[iFlat]); // cast is usually a no-op
}
}
/**
* @brief (binary writing) copy the bits of this property for some element to a stream
*
* @param outStream Stream to write to.
* @param iElement index of the element to write.
*/
virtual void writeDataBinary(std::ostream& outStream, size_t iElement) override {
size_t dataStart = flattenedIndexStart[iElement];
size_t dataEnd = flattenedIndexStart[iElement + 1];
// Get the number of list elements as a uchar, and ensure the value fits
size_t dataCount = dataEnd - dataStart;
if (dataCount > std::numeric_limits<uint8_t>::max()) {
throw std::runtime_error(
"List property has an element with more entries than fit in a uchar. See note in README.");
}
uint8_t count = static_cast<uint8_t>(dataCount);
outStream.write((char*)&count, sizeof(uint8_t));
outStream.write((char*)&flattenedData[dataStart], count * sizeof(T));
}
/**
* @brief (binary writing) copy the bits of this property for some element to a stream
*
* @param outStream Stream to write to.
* @param iElement index of the element to write.
*/
virtual void writeDataBinaryBigEndian(std::ostream& outStream, size_t iElement) override {
size_t dataStart = flattenedIndexStart[iElement];
size_t dataEnd = flattenedIndexStart[iElement + 1];
// Get the number of list elements as a uchar, and ensure the value fits
size_t dataCount = dataEnd - dataStart;
if (dataCount > std::numeric_limits<uint8_t>::max()) {
throw std::runtime_error(
"List property has an element with more entries than fit in a uchar. See note in README.");
}
uint8_t count = static_cast<uint8_t>(dataCount);
outStream.write((char*)&count, sizeof(uint8_t));
for (size_t iFlat = dataStart; iFlat < dataEnd; iFlat++) {
T value = swapEndian(flattenedData[iFlat]);
outStream.write((char*)&value, sizeof(T));
}
}
/**
* @brief Number of element entries for this property
*
* @return
*/
virtual size_t size() override { return flattenedIndexStart.size() - 1; }
/**
* @brief A string naming the type of the property
*
* @return
*/
virtual std::string propertyTypeName() override { return typeName<T>(); }
/**
* @brief The (flattened) data for the property, as formed by concatenating all of the individual element lists
* together.
*/
std::vector<T> flattenedData;
/**
* @brief Indices in to flattenedData. The i'th element gives the index in to flattenedData where the element's data
* begins. A final entry is included which is the length of flattenedData. Size is N_elem + 1.
*/
std::vector<size_t> flattenedIndexStart;
/**
* @brief The number of bytes used to store the count for lists of data.
*/
int listCountBytes = -1;
};
/**
* @brief Helper function to construct a new property of the appropriate type.
*
* @param name The name of the property to construct.
* @param typeStr A string naming the type according to the format.
* @param isList Is this a plain property, or a list property?
* @param listCountTypeStr If a list property, the type of the count varible.
*
* @return A new Property with the proper type.
*/
inline std::unique_ptr<Property> createPropertyWithType(const std::string& name, const std::string& typeStr,
bool isList, const std::string& listCountTypeStr) {
// == Figure out how many bytes the list count field has, if this is a list type
// Note: some files seem to use signed types here, we read the width but always parse as if unsigned
int listCountBytes = -1;
if (isList) {
if (listCountTypeStr == "uchar" || listCountTypeStr == "uint8" || listCountTypeStr == "char" ||
listCountTypeStr == "int8") {
listCountBytes = 1;
} else if (listCountTypeStr == "ushort" || listCountTypeStr == "uint16" || listCountTypeStr == "short" ||
listCountTypeStr == "int16") {
listCountBytes = 2;
} else if (listCountTypeStr == "uint" || listCountTypeStr == "uint32" || listCountTypeStr == "int" ||
listCountTypeStr == "int32") {
listCountBytes = 4;
} else {
throw std::runtime_error("Unrecognized list count type: " + listCountTypeStr);
}
}
// = Unsigned int
// 8 bit unsigned
if (typeStr == "uchar" || typeStr == "uint8") {
if (isList) {
return std::unique_ptr<Property>(new TypedListProperty<uint8_t>(name, listCountBytes));
} else {
return std::unique_ptr<Property>(new TypedProperty<uint8_t>(name));
}
}
// 16 bit unsigned
else if (typeStr == "ushort" || typeStr == "uint16") {
if (isList) {
return std::unique_ptr<Property>(new TypedListProperty<uint16_t>(name, listCountBytes));
} else {
return std::unique_ptr<Property>(new TypedProperty<uint16_t>(name));
}
}
// 32 bit unsigned
else if (typeStr == "uint" || typeStr == "uint32") {
if (isList) {
return std::unique_ptr<Property>(new TypedListProperty<uint32_t>(name, listCountBytes));
} else {
return std::unique_ptr<Property>(new TypedProperty<uint32_t>(name));
}
}
// = Signed int
// 8 bit signed
if (typeStr == "char" || typeStr == "int8") {
if (isList) {
return std::unique_ptr<Property>(new TypedListProperty<int8_t>(name, listCountBytes));
} else {
return std::unique_ptr<Property>(new TypedProperty<int8_t>(name));
}
}
// 16 bit signed
else if (typeStr == "short" || typeStr == "int16") {
if (isList) {
return std::unique_ptr<Property>(new TypedListProperty<int16_t>(name, listCountBytes));
} else {
return std::unique_ptr<Property>(new TypedProperty<int16_t>(name));
}
}
// 32 bit signed
else if (typeStr == "int" || typeStr == "int32") {
if (isList) {
return std::unique_ptr<Property>(new TypedListProperty<int32_t>(name, listCountBytes));
} else {
return std::unique_ptr<Property>(new TypedProperty<int32_t>(name));
}
}
// = Float
// 32 bit float
else if (typeStr == "float" || typeStr == "float32") {
if (isList) {
return std::unique_ptr<Property>(new TypedListProperty<float>(name, listCountBytes));
} else {
return std::unique_ptr<Property>(new TypedProperty<float>(name));
}
}
// 64 bit float
else if (typeStr == "double" || typeStr == "float64") {
if (isList) {
return std::unique_ptr<Property>(new TypedListProperty<double>(name, listCountBytes));
} else {
return std::unique_ptr<Property>(new TypedProperty<double>(name));
}
}
else {
throw std::runtime_error("Data type: " + typeStr + " cannot be mapped to .ply format");
}
}
/**
* @brief An element (more properly an element type) in the .ply object. Tracks the name of the elemnt type (eg,
* "vertices"), the number of elements of that type (eg, 1244), and any properties associated with that element (eg,
* "position", "color").
*/
class Element {
public:
/**
* @brief Create a new element type.
*
* @param name_ Name of the element type (eg, "vertices")
* @param count_ Number of instances of this element.
*/
Element(const std::string& name_, size_t count_) : name(name_), count(count_) {}
std::string name;
size_t count;
std::vector<std::unique_ptr<Property>> properties;
/**
* @brief Check if a property exists.
*
* @param target The name of the property to get.
*
* @return Whether the target property exists.
*/
bool hasProperty(const std::string& target) {
for (std::unique_ptr<Property>& prop : properties) {
if (prop->name == target) {
return true;
}
}
return false;
}
/**
* @brief Check if a property exists with the requested type.
*
* @tparam T The type of the property
* @param target The name of the property to get.
*
* @return Whether the target property exists.
*/
template <class T>
bool hasPropertyType(const std::string& target) {
for (std::unique_ptr<Property>& prop : properties) {
if (prop->name == target) {
TypedProperty<T>* castedProp = dynamic_cast<TypedProperty<T>*>(prop.get());
if (castedProp) {
return true;
}
return false;
}
}
return false;
}
/**
* @brief A list of the names of all properties
*
* @return Property names
*/
std::vector<std::string> getPropertyNames() {
std::vector<std::string> names;
for (std::unique_ptr<Property>& p : properties) {
names.push_back(p->name);
}
return names;
}
/**
* @brief Low-level method to get a pointer to a property. Users probably don't need to call this.
*
* @param target The name of the property to get.
*
* @return A (unique_ptr) pointer to the property.
*/
std::unique_ptr<Property>& getPropertyPtr(const std::string& target) {
for (std::unique_ptr<Property>& prop : properties) {
if (prop->name == target) {
return prop;
}
}
throw std::runtime_error("PLY parser: element " + name + " does not have property " + target);
}
/**
* @brief Add a new (plain, not list) property for this element type.
*
* @tparam T The type of the property
* @param propertyName The name of the property
* @param data The data for the property. Must have the same length as the number of elements.
*/
template <class T>
void addProperty(const std::string& propertyName, const std::vector<T>& data) {
if (data.size() != count) {
throw std::runtime_error("PLY write: new property " + propertyName + " has size which does not match element");
}
// If there is already some property with this name, remove it
for (size_t i = 0; i < properties.size(); i++) {
if (properties[i]->name == propertyName) {
properties.erase(properties.begin() + i);
i--;
}
}
// Copy to canonical type. Often a no-op, but takes care of standardizing widths across platforms.
std::vector<typename CanonicalName<T>::type> canonicalVec(data.begin(), data.end());
properties.push_back(
std::unique_ptr<Property>(new TypedProperty<typename CanonicalName<T>::type>(propertyName, canonicalVec)));
}
/**
* @brief Add a new list property for this element type.
*
* @tparam T The type of the property (eg, "double" for a list of doubles)
* @param propertyName The name of the property
* @param data The data for the property. Outer vector must have the same length as the number of elements.
*/
template <class T>
void addListProperty(const std::string& propertyName, const std::vector<std::vector<T>>& data) {
if (data.size() != count) {
throw std::runtime_error("PLY write: new property " + propertyName + " has size which does not match element");
}
// If there is already some property with this name, remove it
for (size_t i = 0; i < properties.size(); i++) {
if (properties[i]->name == propertyName) {
properties.erase(properties.begin() + i);
i--;
}
}
// Copy to canonical type. Often a no-op, but takes care of standardizing widths across platforms.
std::vector<std::vector<typename CanonicalName<T>::type>> canonicalListVec;
for (const std::vector<T>& subList : data) {
canonicalListVec.emplace_back(subList.begin(), subList.end());
}
properties.push_back(std::unique_ptr<Property>(
new TypedListProperty<typename CanonicalName<T>::type>(propertyName, canonicalListVec)));
}
/**
* @brief Get a vector of a data from a property for this element. Automatically promotes to larger types. Throws if
* requested data is unavailable.
*
* @tparam T The type of data requested
* @param propertyName The name of the property to get.
*
* @return The data.
*/
template <class T>
std::vector<T> getProperty(const std::string& propertyName) {
// Find the property
std::unique_ptr<Property>& prop = getPropertyPtr(propertyName);
// Get a copy of the data with auto-promoting type magic
return getDataFromPropertyRecursive<T, T>(prop.get());
}
/**
* @brief Get a vector of a data from a property for this element. Unlike getProperty(), only returns if the ply
* record contains a type that matches T exactly. Throws if * requested data is unavailable.
*
* @tparam T The type of data requested
* @param propertyName The name of the property to get.
*
* @return The data.
*/
template <class T>
std::vector<T> getPropertyType(const std::string& propertyName) {
// Find the property
std::unique_ptr<Property>& prop = getPropertyPtr(propertyName);
TypedProperty<T>* castedProp = dynamic_cast<TypedProperty<T>*>(prop.get());
if (castedProp) {
return castedProp->data;
}
// No match, failure
throw std::runtime_error("PLY parser: property " + prop->name + " is not of type type " + typeName<T>() +
". Has type " + prop->propertyTypeName());
}
/**
* @brief Get a vector of lists of data from a property for this element. Automatically promotes to larger types.
* Throws if requested data is unavailable.
*
* @tparam T The type of data requested
* @param propertyName The name of the property to get.
*
* @return The data.
*/
template <class T>
std::vector<std::vector<T>> getListProperty(const std::string& propertyName) {
// Find the property
std::unique_ptr<Property>& prop = getPropertyPtr(propertyName);
// Get a copy of the data with auto-promoting type magic
return getDataFromListPropertyRecursive<T, T>(prop.get());
}
/**
* @brief Get a vector of a data from a property for this element. Unlike getProperty(), only returns if the ply
* record contains a type that matches T exactly. Throws if * requested data is unavailable.
*
* @tparam T The type of data requested
* @param propertyName The name of the property to get.
*
* @return The data.
*/
template <class T>
std::vector<std::vector<T>> getListPropertyType(const std::string& propertyName) {
// Find the property
std::unique_ptr<Property>& prop = getPropertyPtr(propertyName);
TypedListProperty<T>* castedProp = dynamic_cast<TypedListProperty<T>*>(prop.get());
if (castedProp) {
return unflattenList(castedProp->flattenedData, castedProp->flattenedIndexStart);
}