-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtinyply.h
886 lines (776 loc) · 33.2 KB
/
tinyply.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
/*
* tinyply 2.2 (https://github.com/ddiakopoulos/tinyply)
*
* A single-header, zero-dependency (except the C++ STL) public domain implementation
* of the PLY mesh file format. Requires C++11; errors are handled through exceptions.
*
* This software is in the public domain. Where that dedication is not
* recognized, you are granted a perpetual, irrevocable license to copy,
* distribute, and modify this file as you see fit.
*
* Authored by Dimitri Diakopoulos (http://www.dimitridiakopoulos.com)
*
* tinyply.h may be included in many files, however in a single compiled file,
* the implementation must be created with the following defined
* before including the header.
* #define TINYPLY_IMPLEMENTATION
*/
////////////////////////
// tinyply header //
////////////////////////
#ifndef tinyply_h
#define tinyply_h
#include <vector>
#include <string>
#include <stdint.h>
#include <sstream>
#include <memory>
#include <unordered_map>
#include <map>
namespace tinyply
{
enum class Type : uint8_t
{
INVALID,
INT8,
UINT8,
INT16,
UINT16,
INT32,
UINT32,
FLOAT32,
FLOAT64
};
struct PropertyInfo
{
int stride;
std::string str;
};
static std::map<Type, PropertyInfo> PropertyTable
{
{ Type::INT8, { 1, "char" } },
{ Type::UINT8, { 1, "uchar" } },
{ Type::INT16, { 2, "short" } },
{ Type::UINT16, { 2, "ushort" } },
{ Type::INT32, { 4, "int" } },
{ Type::UINT32, { 4, "uint" } },
{ Type::FLOAT32, { 4, "float" } },
{ Type::FLOAT64, { 8, "double" } },
{ Type::INVALID, { 0, "INVALID" } }
};
class Buffer
{
uint8_t * alias{ nullptr };
struct delete_array { void operator()(uint8_t * p) { delete[] p; } };
std::unique_ptr<uint8_t, decltype(Buffer::delete_array())> data;
size_t size;
public:
Buffer() {};
Buffer(const size_t size) : data(new uint8_t[size], delete_array()), size(size) { alias = data.get(); } // allocating
Buffer(uint8_t * ptr) { alias = ptr; } // non-allocating, todo: set size?
uint8_t * get() { return alias; }
size_t size_bytes() const { return size; }
};
struct PlyData
{
Type t;
size_t count;
Buffer buffer;
bool isList;
};
struct PlyProperty
{
PlyProperty(std::istream & is);
PlyProperty(Type type, std::string & _name) : name(_name), propertyType(type) {}
PlyProperty(Type list_type, Type prop_type, std::string & _name, size_t list_count)
: name(_name), propertyType(prop_type), isList(true), listType(list_type), listCount(list_count) {}
std::string name;
Type propertyType;
bool isList{ false };
Type listType{ Type::INVALID };
size_t listCount{ 0 };
};
struct PlyElement
{
PlyElement(std::istream & istream);
PlyElement(const std::string & _name, size_t count) : name(_name), size(count) {}
std::string name;
size_t size;
std::vector<PlyProperty> properties;
};
struct PlyFile
{
struct PlyFileImpl;
std::unique_ptr<PlyFileImpl> impl;
PlyFile();
~PlyFile();
/*
* The ply format requires an ascii header. This can be used to determine at
* runtime which properties or elements exist in the file. Limited validation of the
* header is performed; it is assumed the header correctly reflects the contents of the
* payload. This function may throw. Returns true on success, false on failure.
*/
bool parse_header(std::istream & is);
/*
* Execute a read operation. Data must be requested via `request_properties_from_element(...)`
* prior to calling this function.
*/
void read(std::istream & is);
/*
* `write` performs no validation and assumes that the data passed into
* `add_properties_to_element` is well-formed.
*/
void write(std::ostream & os, bool isBinary);
/*
* These functions are valid after a call to `parse_header(...)`. In the case of
* writing, get_comments() may also be used to add new comments to the ply header.
*/
std::vector<PlyElement> get_elements() const;
std::vector<std::string> get_info() const;
std::vector<std::string> & get_comments();
/*
* In the general case where |list_size_hint| is zero, `read` performs a two-pass
* parse to support variable length lists. The most general use of the
* ply format is storing triangle meshes. When this fact is known a-priori, we can pass
* an expected list length that will apply to this element. Doing so results in an up-front
* memory allocation and a single-pass import, a 2x performance optimization.
*/
std::shared_ptr<PlyData> request_properties_from_element(const std::string & elementKey,
const std::initializer_list<std::string> propertyKeys, const uint32_t list_size_hint = 0);
void add_properties_to_element(const std::string & elementKey,
const std::initializer_list<std::string> propertyKeys,
const Type type,
const size_t count,
uint8_t * data,
const Type listType,
const size_t listCount);
};
} // end namespace tinyply
#endif // end tinyply_h
////////////////////////////////
// tinyply implementation //
////////////////////////////////
#ifdef TINYPLY_IMPLEMENTATION
#include <algorithm>
#include <functional>
#include <type_traits>
#include <iostream>
#include <cstring>
using namespace tinyply;
using namespace std;
template<typename T, typename T2> inline T2 endian_swap(const T & v) { return v; }
template<> inline uint16_t endian_swap<uint16_t, uint16_t>(const uint16_t & v) { return (v << 8) | (v >> 8); }
template<> inline uint32_t endian_swap<uint32_t, uint32_t>(const uint32_t & v) { return (v << 24) | ((v << 8) & 0x00ff0000) | ((v >> 8) & 0x0000ff00) | (v >> 24); }
template<> inline uint64_t endian_swap<uint64_t, uint64_t>(const uint64_t & v)
{
return (((v & 0x00000000000000ffLL) << 56) |
((v & 0x000000000000ff00LL) << 40) |
((v & 0x0000000000ff0000LL) << 24) |
((v & 0x00000000ff000000LL) << 8) |
((v & 0x000000ff00000000LL) >> 8) |
((v & 0x0000ff0000000000LL) >> 24) |
((v & 0x00ff000000000000LL) >> 40) |
((v & 0xff00000000000000LL) >> 56));
}
template<> inline int16_t endian_swap<int16_t, int16_t>(const int16_t & v) { uint16_t r = endian_swap<uint16_t, uint16_t>(*(uint16_t*)&v); return *(int16_t*)&r; }
template<> inline int32_t endian_swap<int32_t, int32_t>(const int32_t & v) { uint32_t r = endian_swap<uint32_t, uint32_t>(*(uint32_t*)&v); return *(int32_t*)&r; }
template<> inline int64_t endian_swap<int64_t, int64_t>(const int64_t & v) { uint64_t r = endian_swap<uint64_t, uint64_t>(*(uint64_t*)&v); return *(int64_t*)&r; }
template<> inline float endian_swap<uint32_t, float>(const uint32_t & v) { union { float f; uint32_t i; }; i = endian_swap<uint32_t, uint32_t>(v); return f; }
template<> inline double endian_swap<uint64_t, double>(const uint64_t & v) { union { double d; uint64_t i; }; i = endian_swap<uint64_t, uint64_t>(v); return d; }
inline uint32_t hash_fnv1a(const std::string & str)
{
static const uint32_t fnv1aBase32 = 0x811C9DC5u;
static const uint32_t fnv1aPrime32 = 0x01000193u;
uint32_t result = fnv1aBase32;
for (auto & c : str) { result ^= static_cast<uint32_t>(c); result *= fnv1aPrime32; }
return result;
}
inline Type property_type_from_string(const std::string & t)
{
if (t == "int8" || t == "char") return Type::INT8;
else if (t == "uint8" || t == "uchar") return Type::UINT8;
else if (t == "int16" || t == "short") return Type::INT16;
else if (t == "uint16" || t == "ushort") return Type::UINT16;
else if (t == "int32" || t == "int") return Type::INT32;
else if (t == "uint32" || t == "uint") return Type::UINT32;
else if (t == "float32" || t == "float") return Type::FLOAT32;
else if (t == "float64" || t == "double") return Type::FLOAT64;
return Type::INVALID;
}
typedef std::function<void(void * dest, const char * src, bool be)> cast_t;
struct PlyFile::PlyFileImpl
{
struct PlyDataCursor
{
size_t byteOffset{ 0 };
size_t totalSizeBytes{ 0 };
};
struct ParsingHelper
{
std::shared_ptr<PlyData> data;
std::shared_ptr<PlyDataCursor> cursor;
uint32_t list_size_hint;
};
struct PropertyLookup
{
ParsingHelper * helper{ nullptr };
bool skip{ false };
size_t prop_stride{ 0 }; // precomputed
size_t list_stride{ 0 }; // precomputed
};
std::unordered_map<uint32_t, ParsingHelper> userData;
bool isBinary = false;
bool isBigEndian = false;
std::vector<PlyElement> elements;
std::vector<std::string> comments;
std::vector<std::string> objInfo;
uint8_t scratch[64]; // large enough for max list size
void read(std::istream & is);
void write(std::ostream & os, bool isBinary);
std::shared_ptr<PlyData> request_properties_from_element(const std::string & elementKey,
const std::initializer_list<std::string> propertyKeys,
const uint32_t list_size_hint);
void add_properties_to_element(const std::string & elementKey,
const std::initializer_list<std::string> propertyKeys,
const Type type, const size_t count, uint8_t * data, const Type listType, const size_t listCount);
size_t read_property_binary(const Type & t, const size_t & stride, void * dest, size_t & destOffset, std::istream & is);
size_t read_property_ascii(const Type & t, const size_t & stride, void * dest, size_t & destOffset, std::istream & is);
std::vector<std::vector<PropertyLookup>> make_property_lookup_table()
{
std::vector<std::vector<PropertyLookup>> element_property_lookup;
for (auto & element : elements)
{
std::vector<PropertyLookup> lookups;
for (auto & property : element.properties)
{
PropertyLookup f;
auto cursorIt = userData.find(hash_fnv1a(element.name + property.name));
if (cursorIt != userData.end()) f.helper = &cursorIt->second;
else f.skip = true;
f.prop_stride = PropertyTable[property.propertyType].stride;
if (property.isList) f.list_stride = PropertyTable[property.listType].stride;
lookups.push_back(f);
}
element_property_lookup.push_back(lookups);
}
return element_property_lookup;
}
bool parse_header(std::istream & is);
void parse_data(std::istream & is, bool firstPass);
void read_header_format(std::istream & is);
void read_header_element(std::istream & is);
void read_header_property(std::istream & is);
void read_header_text(std::string line, std::istream & is, std::vector<std::string> & place, int erase = 0);
void write_header(std::ostream & os);
void write_ascii_internal(std::ostream & os);
void write_binary_internal(std::ostream & os);
void write_property_ascii(Type t, std::ostream & os, uint8_t * src, size_t & srcOffset);
void write_property_binary(Type t, std::ostream & os, uint8_t * src, size_t & srcOffset, const size_t & stride);
};
PlyProperty::PlyProperty(std::istream & is) : isList(false)
{
std::string type;
is >> type;
if (type == "list")
{
std::string countType;
is >> countType >> type;
listType = property_type_from_string(countType);
isList = true;
}
propertyType = property_type_from_string(type);
is >> name;
}
PlyElement::PlyElement(std::istream & is)
{
is >> name >> size;
}
template<typename T> inline T ply_read_ascii(std::istream & is)
{
T data;
is >> data;
return data;
}
template<typename T, typename T2>
inline void endian_swap_buffer(uint8_t * data_ptr, const size_t num_bytes, const size_t stride)
{
for (size_t count = 0; count < num_bytes; count += stride)
{
*(reinterpret_cast<T2 *>(data_ptr)) = endian_swap<T, T2>(*(reinterpret_cast<const T *>(data_ptr)));
data_ptr += stride;
}
}
template<typename T> void ply_cast_ascii(void * dest, std::istream & is)
{
*(static_cast<T *>(dest)) = ply_read_ascii<T>(is);
}
int64_t find_element(const std::string & key, const std::vector<PlyElement> & list)
{
for (size_t i = 0; i < list.size(); i++) if (list[i].name == key) return i;
return -1;
}
int64_t find_property(const std::string & key, const std::vector<PlyProperty> & list)
{
for (size_t i = 0; i < list.size(); ++i) if (list[i].name == key) return i;
return -1;
}
bool PlyFile::PlyFileImpl::parse_header(std::istream & is)
{
std::string line;
while (std::getline(is, line))
{
std::istringstream ls(line);
std::string token;
ls >> token;
if (token == "ply" || token == "PLY" || token == "") continue;
else if (token == "comment") read_header_text(line, ls, comments, 8);
else if (token == "format") read_header_format(ls);
else if (token == "element") read_header_element(ls);
else if (token == "property") read_header_property(ls);
else if (token == "obj_info") read_header_text(line, ls, objInfo, 9);
else if (token == "end_header") break;
else return false; // unexpected header field
}
return true;
}
void PlyFile::PlyFileImpl::read_header_text(std::string line, std::istream & is, std::vector<std::string>& place, int erase)
{
place.push_back((erase > 0) ? line.erase(0, erase) : line);
}
void PlyFile::PlyFileImpl::read_header_format(std::istream & is)
{
std::string s;
(is >> s);
if (s == "binary_little_endian") isBinary = true;
else if (s == "binary_big_endian") isBinary = isBigEndian = true;
}
void PlyFile::PlyFileImpl::read_header_element(std::istream & is)
{
elements.emplace_back(is);
}
void PlyFile::PlyFileImpl::read_header_property(std::istream & is)
{
if (!elements.size()) throw std::runtime_error("no elements defined; file is malformed");
elements.back().properties.emplace_back(is);
}
size_t PlyFile::PlyFileImpl::read_property_binary(const Type & t, const size_t & stride, void * dest, size_t & destOffset, std::istream & is)
{
destOffset += stride;
is.read((char*)dest, stride);
return stride;
}
size_t PlyFile::PlyFileImpl::read_property_ascii(const Type & t, const size_t & stride, void * dest, size_t & destOffset, std::istream & is)
{
destOffset += stride;
switch (t)
{
case Type::INT8: *((int8_t *)dest) = ply_read_ascii<int32_t>(is); break;
case Type::UINT8: *((uint8_t *)dest) = ply_read_ascii<uint32_t>(is); break;
case Type::INT16: ply_cast_ascii<int16_t>(dest, is); break;
case Type::UINT16: ply_cast_ascii<uint16_t>(dest, is); break;
case Type::INT32: ply_cast_ascii<int32_t>(dest, is); break;
case Type::UINT32: ply_cast_ascii<uint32_t>(dest, is); break;
case Type::FLOAT32: ply_cast_ascii<float>(dest, is); break;
case Type::FLOAT64: ply_cast_ascii<double>(dest, is); break;
case Type::INVALID: throw std::invalid_argument("invalid ply property");
}
return stride;
}
void PlyFile::PlyFileImpl::write_property_ascii(Type t, std::ostream & os, uint8_t * src, size_t & srcOffset)
{
switch (t)
{
case Type::INT8: os << static_cast<int32_t>(*reinterpret_cast<int8_t*>(src)); break;
case Type::UINT8: os << static_cast<uint32_t>(*reinterpret_cast<uint8_t*>(src)); break;
case Type::INT16: os << *reinterpret_cast<int16_t*>(src); break;
case Type::UINT16: os << *reinterpret_cast<uint16_t*>(src); break;
case Type::INT32: os << *reinterpret_cast<int32_t*>(src); break;
case Type::UINT32: os << *reinterpret_cast<uint32_t*>(src); break;
case Type::FLOAT32: os << *reinterpret_cast<float*>(src); break;
case Type::FLOAT64: os << *reinterpret_cast<double*>(src); break;
case Type::INVALID: throw std::invalid_argument("invalid ply property");
}
os << " ";
srcOffset += PropertyTable[t].stride;
}
void PlyFile::PlyFileImpl::write_property_binary(Type t, std::ostream & os, uint8_t * src, size_t & srcOffset, const size_t & stride)
{
os.write((char *)src, stride);
srcOffset += stride;
}
void PlyFile::PlyFileImpl::read(std::istream & is)
{
std::vector<std::shared_ptr<PlyData>> buffers;
for (auto & entry : userData) buffers.push_back(entry.second.data);
// Discover if we can allocate up front without parsing the file twice
uint32_t list_hints = 0;
for (auto & b : buffers) for (auto & entry : userData) list_hints += entry.second.list_size_hint;
// No list hints? Then we need to calculate how much memory to allocate
if (list_hints == 0) parse_data(is, true);
// Count the number of properties (required for allocation)
// e.g. if we have properties x y and z requested, we ensure
// that their buffer points to the same PlyData
std::unordered_map<PlyData*, int32_t> unique_data_count;
for (auto & ptr : buffers) unique_data_count[ptr.get()] += 1;
// Since group-requested properties share the same cursor,
// we need to find unique cursors so we only allocate once
std::sort(buffers.begin(), buffers.end());
buffers.erase(std::unique(buffers.begin(), buffers.end()), buffers.end());
// We sorted by ptrs on PlyData, need to remap back onto its cursor in the userData table
for (auto & b : buffers)
{
for (auto & entry : userData)
{
if (entry.second.data == b && b->buffer.get() == nullptr)
{
// If we didn't receive any list hints, it means we did two passes over the
// file to compute the total length of all (potentially) variable-length lists
if (list_hints == 0)
{
b->buffer = Buffer(entry.second.cursor->totalSizeBytes);
}
else
{
// otherwise, we can allocate up front, skipping the first pass.
const size_t list_size_multiplier = (entry.second.data->isList ? entry.second.list_size_hint : 1);
auto bytes_per_property = entry.second.data->count * PropertyTable[entry.second.data->t].stride * list_size_multiplier;
bytes_per_property *= unique_data_count[b.get()];
b->buffer = Buffer(bytes_per_property);
}
}
}
}
// Populate the data
parse_data(is, false);
if (isBigEndian)
{
for (auto & b : buffers)
{
uint8_t * data_ptr = b->buffer.get();
const size_t stride = PropertyTable[b->t].stride;
const size_t buffer_size_bytes = b->buffer.size_bytes();
switch (b->t)
{
case Type::INT16: endian_swap_buffer<int16_t, int16_t>(data_ptr, buffer_size_bytes, stride); break;
case Type::UINT16: endian_swap_buffer<uint16_t, uint16_t>(data_ptr, buffer_size_bytes, stride); break;
case Type::INT32: endian_swap_buffer<int32_t, int32_t>(data_ptr, buffer_size_bytes, stride); break;
case Type::UINT32: endian_swap_buffer<uint32_t, uint32_t>(data_ptr, buffer_size_bytes, stride); break;
case Type::FLOAT32: endian_swap_buffer<uint32_t, float>(data_ptr, buffer_size_bytes, stride); break;
case Type::FLOAT64: endian_swap_buffer<uint64_t, double>(data_ptr, buffer_size_bytes, stride); break;
}
}
}
}
void PlyFile::PlyFileImpl::write(std::ostream & os, bool _isBinary)
{
// reset cursors
for (auto & d : userData) { d.second.cursor->byteOffset = 0; }
if (_isBinary) write_binary_internal(os);
else write_ascii_internal(os);
}
void PlyFile::PlyFileImpl::write_binary_internal(std::ostream & os)
{
isBinary = true;
write_header(os);
uint8_t listSize[4] = { 0, 0, 0, 0 };
size_t dummyCount = 0;
auto element_property_lookup = make_property_lookup_table();
size_t element_idx = 0;
for (auto & e : elements)
{
for (size_t i = 0; i < e.size; ++i)
{
size_t property_index = 0;
for (auto & p : e.properties)
{
auto & f = element_property_lookup[element_idx][property_index];
auto * helper = f.helper;
if (p.isList)
{
std::memcpy(listSize, &p.listCount, sizeof(uint32_t));
write_property_binary(p.listType, os, listSize, dummyCount, f.list_stride);
write_property_binary(p.propertyType, os, (helper->data->buffer.get() + helper->cursor->byteOffset), helper->cursor->byteOffset, f.prop_stride * p.listCount);
}
else
{
write_property_binary(p.propertyType, os, (helper->data->buffer.get() + helper->cursor->byteOffset), helper->cursor->byteOffset, f.prop_stride);
}
property_index++;
}
}
element_idx++;
}
}
void PlyFile::PlyFileImpl::write_ascii_internal(std::ostream & os)
{
write_header(os);
for (auto & e : elements)
{
for (size_t i = 0; i < e.size; ++i)
{
for (auto & p : e.properties)
{
auto & helper = userData[hash_fnv1a(e.name + p.name)];
if (p.isList)
{
os << p.listCount << " ";
for (int j = 0; j < p.listCount; ++j)
{
write_property_ascii(p.propertyType, os, (helper.data->buffer.get() + helper.cursor->byteOffset), helper.cursor->byteOffset);
}
}
else
{
write_property_ascii(p.propertyType, os, (helper.data->buffer.get() + helper.cursor->byteOffset), helper.cursor->byteOffset);
}
}
os << "\n";
}
}
}
void PlyFile::PlyFileImpl::write_header(std::ostream & os)
{
const std::locale & fixLoc = std::locale("C");
os.imbue(fixLoc);
os << "ply\n";
if (isBinary) os << ((isBigEndian) ? "format binary_big_endian 1.0" : "format binary_little_endian 1.0") << "\n";
else os << "format ascii 1.0\n";
for (const auto & comment : comments) os << "comment " << comment << "\n";
for (auto & e : elements)
{
os << "element " << e.name << " " << e.size << "\n";
for (const auto & p : e.properties)
{
if (p.isList)
{
os << "property list " << PropertyTable[p.listType].str << " "
<< PropertyTable[p.propertyType].str << " " << p.name << "\n";
}
else
{
os << "property " << PropertyTable[p.propertyType].str << " " << p.name << "\n";
}
}
}
os << "end_header\n";
}
std::shared_ptr<PlyData> PlyFile::PlyFileImpl::request_properties_from_element(const std::string & elementKey,
const std::initializer_list<std::string> propertyKeys,
const uint32_t list_size_hint)
{
// Each key in `propertyKey` gets an entry into the userData map (keyed by a hash of
// element name and property name), but groups of properties (requested from the
// public api through this function) all share the same `ParsingHelper`. When it comes
// time to .read(), we check the number of unique PlyData shared pointers
// and allocate a single buffer that will be used by each individual property.
ParsingHelper helper;
helper.data = std::make_shared<PlyData>();
helper.data->count = 0;
helper.data->isList = false;
helper.data->t = Type::INVALID;
helper.cursor = std::make_shared<PlyDataCursor>();
helper.list_size_hint = list_size_hint;
if (elements.empty()) throw std::runtime_error("header had no elements defined. malformed file?");
if (elementKey.empty()) throw std::invalid_argument("`elementKey` argument is empty");
if (!propertyKeys.size()) throw std::invalid_argument("`propertyKeys` argument is empty");
const int64_t elementIndex = find_element(elementKey, elements);
std::vector<std::string> keys_not_found;
// Sanity check if the user requested element is in the pre-parsed header
if (elementIndex >= 0)
{
// We found the element
const PlyElement & element = elements[elementIndex];
helper.data->count = element.size;
// Find each of the keys
for (auto key : propertyKeys)
{
const int64_t propertyIndex = find_property(key, element.properties);
if (propertyIndex >= 0)
{
// We found the property
const PlyProperty & property = element.properties[propertyIndex];
helper.data->t = property.propertyType;
helper.data->isList = property.isList;
auto result = userData.insert(std::pair<uint32_t, ParsingHelper>(hash_fnv1a(element.name + property.name), helper));
if (result.second == false)
{
throw std::invalid_argument("element-property key has already been requested: " + hash_fnv1a(element.name + property.name));
}
}
else keys_not_found.push_back(key);
}
}
else throw std::invalid_argument("the element key was not found in the header: " + elementKey);
if (keys_not_found.size())
{
std::stringstream ss;
for (auto & str : keys_not_found) ss << str << ", ";
throw std::invalid_argument("the following property keys were not found in the header: " + ss.str());
}
return helper.data;
}
void PlyFile::PlyFileImpl::add_properties_to_element(const std::string & elementKey,
const std::initializer_list<std::string> propertyKeys,
const Type type, const size_t count, uint8_t * data, const Type listType, const size_t listCount)
{
ParsingHelper helper;
helper.data = std::make_shared<PlyData>();
helper.data->count = count;
helper.data->t = type;
helper.data->buffer = Buffer(data);
helper.cursor = std::make_shared<PlyDataCursor>();
auto create_property_on_element = [&](PlyElement & e)
{
for (auto key : propertyKeys)
{
PlyProperty newProp = (listType == Type::INVALID) ? PlyProperty(type, key) : PlyProperty(listType, type, key, listCount);
userData.insert(std::pair<uint32_t, ParsingHelper>(hash_fnv1a(elementKey + key), helper));
e.properties.push_back(newProp);
}
};
const int64_t idx = find_element(elementKey, elements);
if (idx >= 0)
{
PlyElement & e = elements[idx];
create_property_on_element(e);
}
else
{
PlyElement newElement = (listType == Type::INVALID) ? PlyElement(elementKey, count) : PlyElement(elementKey, count);
create_property_on_element(newElement);
elements.push_back(newElement);
}
}
void PlyFile::PlyFileImpl::parse_data(std::istream & is, bool firstPass)
{
std::function<void(PropertyLookup & f, const PlyProperty & p, uint8_t * dest, size_t & destOffset, std::istream & is)> read;
std::function<size_t(PropertyLookup & f, const PlyProperty & p, std::istream & is)> skip;
const auto start = is.tellg();
size_t listSize = 0;
size_t dummyCount = 0;
std::string skip_ascii_buffer;
// Special case mirroring read_property_binary but for list types; this
// has an additional big endian check to flip the data in place immediately
// after reading. We do this as a performance optimization; endian flipping is
// done on regular properties as a post-process after reading (also for optimization)
// but we need the correct little-endian list count as we read the file.
auto read_list_binary = [this](const Type & t, void * dst, size_t & destOffset, std::istream & _is)
{
const size_t stride = PropertyTable[t].stride; // @todo - this is already precomputed
destOffset += stride;
_is.read((char*)dst, stride);
if (isBigEndian)
{
switch (t)
{
case Type::INT16: endian_swap<int16_t, int16_t>(*(int16_t*)dst); break;
case Type::UINT16: endian_swap<uint16_t, uint16_t>(*(uint16_t*)dst); break;
case Type::INT32: endian_swap<int32_t, int32_t>(*(int32_t*)dst); break;
case Type::UINT32: endian_swap<uint32_t, uint32_t>(*(uint32_t*)dst); break;
}
}
return stride;
};
if (isBinary)
{
read = [this, &listSize, &dummyCount, &read_list_binary](PropertyLookup & f, const PlyProperty & p, uint8_t * dest, size_t & destOffset, std::istream & _is)
{
if (!p.isList)
{
read_property_binary(p.propertyType, f.prop_stride, dest + destOffset, destOffset, _is);
}
else
{
read_list_binary(p.listType, &listSize, dummyCount, _is); // the list size
read_property_binary(p.propertyType, f.prop_stride * listSize, dest + destOffset, destOffset, _is); // properties in list
}
};
skip = [this, &listSize, &dummyCount, &read_list_binary](PropertyLookup & f, const PlyProperty & p, std::istream & _is)
{
if (!p.isList)
{
_is.read((char*)scratch, f.prop_stride);
return f.prop_stride;
}
read_list_binary(p.listType, &listSize, dummyCount, _is); // the list size (does not count for memory alloc)
return read_property_binary(p.propertyType, f.prop_stride * listSize, scratch, dummyCount, _is);
};
}
else
{
read = [this, &listSize, &dummyCount](PropertyLookup & f, const PlyProperty & p, uint8_t * dest, size_t & destOffset, std::istream & _is)
{
if (!p.isList)
{
read_property_ascii(p.propertyType, f.prop_stride, dest + destOffset, destOffset, _is);
}
else
{
read_property_ascii(p.listType, f.list_stride, &listSize, dummyCount, _is); // the list size
for (size_t i = 0; i < listSize; ++i)
{
read_property_ascii(p.propertyType, f.prop_stride, dest + destOffset, destOffset, _is);
}
}
};
skip = [this, &listSize, &dummyCount, &skip_ascii_buffer](PropertyLookup & f, const PlyProperty & p, std::istream & _is)
{
skip_ascii_buffer.clear();
if (p.isList)
{
read_property_ascii(p.listType, f.list_stride, &listSize, dummyCount, _is); // the list size
for (size_t i = 0; i < listSize; ++i) _is >> skip_ascii_buffer; // properties in list
return listSize * f.prop_stride;
}
_is >> skip_ascii_buffer;
return f.prop_stride;
};
}
auto element_property_lookup = make_property_lookup_table();
size_t element_idx = 0;
size_t property_index = 0;
for (auto & element : elements)
{
for (size_t count = 0; count < element.size; ++count)
{
property_index = 0;
for (auto & property : element.properties)
{
auto & f = element_property_lookup[element_idx][property_index];
if (!f.skip)
{
auto * helper = f.helper;
if (firstPass) helper->cursor->totalSizeBytes += skip(f, property, is);
else read(f, property, helper->data->buffer.get(), helper->cursor->byteOffset, is);
}
else skip(f, property, is);
property_index++;
}
}
element_idx++;
}
// Reset istream reader to the beginning
if (firstPass) is.seekg(start, is.beg);
}
// Wrap the public interface:
PlyFile::PlyFile() { impl.reset(new PlyFileImpl()); };
PlyFile::~PlyFile() { };
bool PlyFile::parse_header(std::istream & is) { return impl->parse_header(is); }
void PlyFile::read(std::istream & is) { return impl->read(is); }
void PlyFile::write(std::ostream & os, bool isBinary) { return impl->write(os, isBinary); }
std::vector<PlyElement> PlyFile::get_elements() const { return impl->elements; }
std::vector<std::string> & PlyFile::get_comments() { return impl->comments; }
std::vector<std::string> PlyFile::get_info() const { return impl->objInfo; }
std::shared_ptr<PlyData> PlyFile::request_properties_from_element(const std::string & elementKey,
const std::initializer_list<std::string> propertyKeys,
const uint32_t list_size_hint)
{
return impl->request_properties_from_element(elementKey, propertyKeys, list_size_hint);
}
void PlyFile::add_properties_to_element(const std::string & elementKey,
const std::initializer_list<std::string> propertyKeys,
const Type type, const size_t count, uint8_t * data, const Type listType, const size_t listCount)
{
return impl->add_properties_to_element(elementKey, propertyKeys, type, count, data, listType, listCount);
}
#endif // end TINYPLY_IMPLEMENTATION