-
Notifications
You must be signed in to change notification settings - Fork 215
/
BatchTableToGltfStructuralMetadata.cpp
1979 lines (1754 loc) · 63.8 KB
/
BatchTableToGltfStructuralMetadata.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
#include "BatchTableToGltfStructuralMetadata.h"
#include "BatchTableHierarchyPropertyValues.h"
#include <CesiumGltf/ExtensionExtMeshFeatures.h>
#include <CesiumGltf/ExtensionKhrDracoMeshCompression.h>
#include <CesiumGltf/ExtensionModelExtStructuralMetadata.h>
#include <CesiumGltf/Model.h>
#include <CesiumGltf/PropertyType.h>
#include <CesiumGltf/PropertyTypeTraits.h>
#include <CesiumUtility/Assert.h>
#include <CesiumUtility/Log.h>
#include <glm/glm.hpp>
#include <rapidjson/writer.h>
#include <limits>
#include <map>
#include <type_traits>
#include <unordered_set>
using namespace CesiumGltf;
using namespace Cesium3DTilesContent::CesiumImpl;
using namespace CesiumUtility;
namespace Cesium3DTilesContent {
namespace {
/**
* Indicates how a JSON value can be interpreted as a primitive type. Does not
* correspond one-to-one with types / component types in
* EXT_structural_metadata.
*/
struct MaskedType {
bool isInt8;
bool isUint8;
bool isInt16;
bool isUint16;
bool isInt32;
bool isUint32;
bool isInt64;
bool isUint64;
bool isFloat32;
bool isFloat64;
bool isBool;
MaskedType() : MaskedType(true){};
MaskedType(bool defaultValue)
: isInt8(defaultValue),
isUint8(defaultValue),
isInt16(defaultValue),
isUint16(defaultValue),
isInt32(defaultValue),
isUint32(defaultValue),
isInt64(defaultValue),
isUint64(defaultValue),
isFloat32(defaultValue),
isFloat64(defaultValue),
isBool(defaultValue) {}
/**
* Merges another MaskedType into this one.
*/
void operator&=(const MaskedType& source) {
isInt8 &= source.isInt8;
isUint8 &= source.isUint8;
isInt16 &= source.isInt16;
isUint16 &= source.isUint16;
isInt32 &= source.isInt32;
isUint32 &= source.isUint32;
isInt64 &= source.isInt64;
isUint64 &= source.isUint64;
isFloat32 &= source.isFloat32;
isFloat64 &= source.isFloat64;
isBool &= source.isBool;
}
/**
* Whether this is incompatible with every primitive type. Fully-incompatible
* types will be treated as strings.
*/
bool isIncompatible() const noexcept {
return !isInt8 && !isUint8 && !isInt16 && !isUint16 && !isInt32 &&
!isUint32 && !isInt64 && !isUint64 && !isFloat32 && !isFloat64 &&
!isBool;
}
};
/**
* Indicates how the elements of an array JSON value can be interpreted. Does
* not correspond one-to-one with types / component types in
* EXT_structural_metadata.
*
* To avoid complications while parsing, this implementation disallows array
* elements that are also arrays. The nested arrays will be treated as strings.
*/
struct MaskedArrayType {
MaskedType elementType;
uint32_t minArrayCount;
uint32_t maxArrayCount;
MaskedArrayType() : MaskedArrayType(true){};
MaskedArrayType(bool defaultValue)
: elementType(defaultValue),
minArrayCount(std::numeric_limits<uint32_t>::max()),
maxArrayCount(std::numeric_limits<uint32_t>::min()){};
MaskedArrayType(
MaskedType inElementType,
uint32_t inMinArrayCount,
uint32_t inMaxArrayCount)
: elementType(inElementType),
minArrayCount(inMinArrayCount),
maxArrayCount(inMaxArrayCount) {}
/**
* Merges another MaskedArrayType into this one.
*/
void operator&=(const MaskedArrayType& source) {
elementType &= source.elementType;
minArrayCount = glm::min(minArrayCount, source.minArrayCount);
maxArrayCount = glm::max(maxArrayCount, source.maxArrayCount);
}
/**
* Whether this is incompatible with every primitive type. Fully-incompatible
* types will be treated as strings.
*/
bool isIncompatible() const noexcept { return elementType.isIncompatible(); }
};
/**
* Represents information about a batch table property, indicating its
* compatibility with C++ types and whether it has encountered any null values.
*/
struct CompatibleTypes {
private:
/**
* std::monostate represents "complete" compatibility, in that nothing has
* been determined to be incompatible yet. Once something is either a
* MaskedType or MaskedArrayType, they are considered incompatible with the
* other type.
*/
std::variant<std::monostate, MaskedType, MaskedArrayType> _type;
/**
* Whether the property has encountered a null value. A
* property may contain null values even though all other values are of the
* same non-null type. In this case, it can simply replace the null with a
* "noData" value in the EXT_structural_metadata property.
*/
bool _hasNullValue = false;
/**
* The following booleans track possible "noData" (sentinel) values for the
* property.
*
* We don't want to spend too much effort finding a "noData" value, because
* with any given property there can be multiple candidates. Thus, there are
* only a few values that are reserved as potential sentinel values:
*
* - 0, for signed or unsigned integers
* - -1, for signed integers
* - "null", for strings
*
* If a property does not contain one of these values, then it may be used as
* the "noData" value in the property. The sentinel value will then be copied
* to the buffer, instead of the null value.
*/
bool _canUseZeroSentinel = true;
bool _canUseNegativeOneSentinel = true;
bool _canUseNullStringSentinel = true;
public:
CompatibleTypes() : _type(){};
CompatibleTypes(const MaskedType& maskedType) : _type(maskedType){};
CompatibleTypes(const MaskedArrayType& maskedArrayType)
: _type(maskedArrayType){};
/**
* Whether this is exclusively compatible with array types. This indicates an
* exclusively array property, as opposed to a newly initialized one that is
* "compatible" with everything.
*/
bool isExclusivelyArray() const noexcept {
return std::holds_alternative<MaskedArrayType>(_type);
}
/**
* Whether this property is with at least one unsigned integer type. Does not
* count arrays.
*/
bool isCompatibleWithUnsignedInteger() const noexcept {
if (std::holds_alternative<MaskedArrayType>(_type)) {
return false;
}
if (std::holds_alternative<std::monostate>(_type)) {
return true;
}
MaskedType type = std::get<MaskedType>(_type);
return type.isUint8 || type.isUint16 || type.isUint32 || type.isUint64;
}
/**
* Whether this property is compatible with at least one signed integer type.
* Does not count arrays.
*/
bool isCompatibleWithSignedInteger() const noexcept {
if (std::holds_alternative<MaskedArrayType>(_type)) {
return false;
}
if (std::holds_alternative<std::monostate>(_type)) {
return true;
}
MaskedType type = std::get<MaskedType>(_type);
return type.isInt8 || type.isInt16 || type.isInt32 || type.isInt64;
}
/**
* Whether this property is compatible with every type. This only really
* happens when a CompatibleTypes is initialized and never modified.
*/
bool isFullyCompatible() const noexcept {
return std::holds_alternative<std::monostate>(_type);
}
/**
* Whether this property is incompatible with every primitive type.
* Fully-incompatible properties will be treated as string properties.
*/
bool isIncompatible() const noexcept {
if (std::holds_alternative<MaskedType>(_type)) {
return std::get<MaskedType>(_type).isIncompatible();
}
if (std::holds_alternative<MaskedArrayType>(_type)) {
return std::get<MaskedArrayType>(_type).isIncompatible();
}
// std::monostate means compatibility with all types.
return false;
}
/**
* Marks as incompatible with every primitive type. Fully-incompatible
* properties will be treated as string properties.
*/
void makeIncompatible() noexcept { _type = MaskedType(false); }
/**
* Merges a MaskedType into this BatchTableProperty.
*/
void operator&=(const MaskedType& inMaskedType) noexcept {
if (std::holds_alternative<MaskedType>(_type)) {
MaskedType& maskedType = std::get<MaskedType>(_type);
maskedType &= inMaskedType;
return;
}
if (std::holds_alternative<MaskedArrayType>(_type)) {
makeIncompatible();
return;
}
_type = inMaskedType;
}
/**
* Merges a MaskedArrayType into this CompatibleTypes.
*/
void operator&=(const MaskedArrayType& inArrayType) noexcept {
if (std::holds_alternative<MaskedArrayType>(_type)) {
MaskedArrayType& arrayType = std::get<MaskedArrayType>(_type);
arrayType &= inArrayType;
return;
}
if (std::holds_alternative<MaskedType>(_type)) {
makeIncompatible();
return;
}
_type = inArrayType;
}
/**
* Merges another CompatibleTypes into this one.
*/
void operator&=(const CompatibleTypes& inTypes) noexcept {
if (std::holds_alternative<std::monostate>(inTypes._type)) {
// The other CompatibleTypes is compatible with everything, so it does not
// change this one.
} else
if (std::holds_alternative<MaskedArrayType>(inTypes._type)) {
const MaskedArrayType& arrayType =
std::get<MaskedArrayType>(inTypes._type);
operator&=(arrayType);
} else {
const MaskedType& maskedType = std::get<MaskedType>(inTypes._type);
operator&=(maskedType);
}
_hasNullValue |= inTypes._hasNullValue;
_canUseZeroSentinel &= inTypes._canUseZeroSentinel;
_canUseNegativeOneSentinel &= inTypes._canUseNegativeOneSentinel;
_canUseNullStringSentinel &= inTypes._canUseNullStringSentinel;
}
/**
* Derives MaskedType info from this CompatibleTypes. If this property
* is only compatible with arrays, this will return an incompatible
* MaskedType.
*/
MaskedType toMaskedType() const noexcept {
if (std::holds_alternative<MaskedType>(_type)) {
return std::get<MaskedType>(_type);
}
bool isArray = std::holds_alternative<MaskedArrayType>(_type);
return MaskedType(!isArray);
}
/**
* Derives MaskedArrayType info from this CompatibleTypes. If this
* property is not compatible with arrays, this will return an incompatible
* MaskedArrayType.
*/
MaskedArrayType toMaskedArrayType() const noexcept {
if (std::holds_alternative<MaskedArrayType>(_type)) {
return std::get<MaskedArrayType>(_type);
}
bool isNonArray = std::holds_alternative<MaskedType>(_type);
return MaskedArrayType(!isNonArray);
}
/**
* Gets whether the property of this type includes a null value.
*/
bool hasNullValue() const noexcept { return _hasNullValue; }
/**
* Sets whether the property includes a null value. If a null value has been
* encountered, a sentinel value may potentially be provided.
*/
void setHasNullValue(bool value) noexcept { _hasNullValue = value; }
/**
* Gets a possible sentinel value for this type. If no value can be used, this
* returns std::nullopt.
*/
const std::optional<CesiumUtility::JsonValue>
getSentinelValue() const noexcept {
if (isCompatibleWithSignedInteger()) {
if (_canUseZeroSentinel) {
return 0;
}
if (_canUseNegativeOneSentinel) {
return -1;
}
}
if (isCompatibleWithUnsignedInteger()) {
return _canUseZeroSentinel
? std::make_optional<CesiumUtility::JsonValue>(0)
: std::nullopt;
}
if (isIncompatible() && _canUseNullStringSentinel) {
return "null";
}
return std::nullopt;
}
/**
* Removes any sentinel values that are incompatible with the property. This
* also removes the sentinel values that equal the given value.
*
* This is helpful for when a property contains a sentinel value as non-null
* data; the sentinel value can then be removed from consideration.
*/
void removeSentinelValues(CesiumUtility::JsonValue value) noexcept {
if (value.isNumber()) {
// Don't try to use string as sentinels for numbers.
_canUseNullStringSentinel = false;
if (value.isInt64()) {
auto intValue = value.getInt64();
_canUseZeroSentinel &= (intValue != 0);
_canUseNegativeOneSentinel &= (intValue != -1);
} else if (value.isUint64()) {
_canUseZeroSentinel &= (value.getUint64() != 0);
// Since the value is truly a uint64, -1 cannot be used.
_canUseNegativeOneSentinel = false;
}
} else if (value.isString()) {
// Don't try to use numbers as sentinels for strings.
_canUseZeroSentinel = false;
_canUseNegativeOneSentinel = false;
auto stringValue = value.getString();
if (stringValue == "null") {
_canUseNullStringSentinel = false;
}
}
}
};
struct BinaryProperty {
int64_t batchTableByteOffset;
int64_t gltfByteOffset;
int64_t byteLength;
};
struct GltfPropertyTableType {
std::string type;
size_t componentCount;
};
const std::map<std::string, GltfPropertyTableType> batchTableTypeToGltfType = {
{"SCALAR", GltfPropertyTableType{ClassProperty::Type::SCALAR, 1}},
{"VEC2", GltfPropertyTableType{ClassProperty::Type::VEC2, 2}},
{"VEC3", GltfPropertyTableType{ClassProperty::Type::VEC3, 3}},
{"VEC4", GltfPropertyTableType{ClassProperty::Type::VEC4, 4}},
};
struct GltfPropertyTableComponentType {
std::string componentType;
size_t componentTypeSize;
};
const std::map<std::string, GltfPropertyTableComponentType>
batchTableComponentTypeToGltfComponentType = {
{"BYTE",
GltfPropertyTableComponentType{
ClassProperty::ComponentType::INT8,
sizeof(int8_t)}},
{"UNSIGNED_BYTE",
GltfPropertyTableComponentType{
ClassProperty::ComponentType::UINT8,
sizeof(uint8_t)}},
{"SHORT",
GltfPropertyTableComponentType{
ClassProperty::ComponentType::INT16,
sizeof(int16_t)}},
{"UNSIGNED_SHORT",
GltfPropertyTableComponentType{
ClassProperty::ComponentType::UINT16,
sizeof(uint16_t)}},
{"INT",
GltfPropertyTableComponentType{
ClassProperty::ComponentType::INT32,
sizeof(int32_t)}},
{"UNSIGNED_INT",
GltfPropertyTableComponentType{
ClassProperty::ComponentType::UINT32,
sizeof(uint32_t)}},
{"FLOAT",
GltfPropertyTableComponentType{
ClassProperty::ComponentType::FLOAT32,
sizeof(float)}},
{"DOUBLE",
GltfPropertyTableComponentType{
ClassProperty::ComponentType::FLOAT64,
sizeof(double)}},
};
int64_t roundUp(int64_t num, int64_t multiple) noexcept {
return ((num + multiple - 1) / multiple) * multiple;
}
template <typename T> bool isInRangeForSignedInteger(int64_t value) noexcept {
// This only works if sizeof(T) is smaller than int64_t
static_assert(
!std::is_same_v<T, uint64_t> && !std::is_same_v<T, float> &&
!std::is_same_v<T, double>);
return value >= static_cast<int64_t>(std::numeric_limits<T>::lowest()) &&
value <= static_cast<int64_t>(std::numeric_limits<T>::max());
}
template <typename T>
bool isInRangeForUnsignedInteger(uint64_t value) noexcept {
static_assert(!std::is_signed_v<T>);
return value >= static_cast<uint64_t>(std::numeric_limits<T>::lowest()) &&
value <= static_cast<uint64_t>(std::numeric_limits<T>::max());
}
template <typename OffsetType>
void copyStringBuffer(
const rapidjson::StringBuffer& rapidjsonStrBuffer,
const std::vector<uint64_t>& rapidjsonOffsets,
std::vector<std::byte>& buffer,
std::vector<std::byte>& offsetBuffer) {
buffer.resize(rapidjsonStrBuffer.GetLength());
std::memcpy(buffer.data(), rapidjsonStrBuffer.GetString(), buffer.size());
offsetBuffer.resize(sizeof(OffsetType) * rapidjsonOffsets.size());
OffsetType* offset = reinterpret_cast<OffsetType*>(offsetBuffer.data());
for (size_t i = 0; i < rapidjsonOffsets.size(); ++i) {
offset[i] = static_cast<OffsetType>(rapidjsonOffsets[i]);
}
}
class ArrayOfPropertyValues {
public:
class const_iterator {
public:
const_iterator(const rapidjson::Value* p) : _p(p) {}
const_iterator& operator++() {
++this->_p;
return *this;
}
bool operator==(const const_iterator& rhs) const {
return this->_p == rhs._p;
}
bool operator!=(const const_iterator& rhs) const {
return this->_p != rhs._p;
}
const rapidjson::Value& operator*() const { return *this->_p; }
const rapidjson::Value* operator->() const { return this->_p; }
private:
const rapidjson::Value* _p;
};
ArrayOfPropertyValues(const rapidjson::Value& propertyValues)
: _propertyValues(propertyValues) {}
const_iterator begin() const {
return const_iterator(this->_propertyValues.Begin());
}
const_iterator end() const {
return const_iterator(this->_propertyValues.End());
}
int64_t size() const { return this->_propertyValues.Size(); }
private:
const rapidjson::Value& _propertyValues;
};
template <typename TValueIter>
MaskedType getCompatibleTypesForNumber(const TValueIter& it) {
MaskedType type(false);
if (it->IsInt64()) {
const int64_t value = it->GetInt64();
type.isInt8 = isInRangeForSignedInteger<int8_t>(value);
type.isUint8 = isInRangeForSignedInteger<uint8_t>(value);
type.isInt16 = isInRangeForSignedInteger<int16_t>(value);
type.isUint16 = isInRangeForSignedInteger<uint16_t>(value);
type.isInt32 = isInRangeForSignedInteger<int32_t>(value);
type.isUint32 = isInRangeForSignedInteger<uint32_t>(value);
type.isInt64 = true;
type.isUint64 = value >= 0;
type.isFloat32 = it->IsLosslessFloat();
type.isFloat64 = it->IsLosslessDouble();
} else if (it->IsUint64()) {
// Only uint64_t can represent a value that fits in a uint64_t but not in
// an int64_t.
type.isUint64 = true;
} else if (it->IsLosslessFloat()) {
type.isFloat32 = true;
type.isFloat64 = true;
} else if (it->IsDouble()) {
type.isFloat64 = true;
}
return type;
}
template <typename TValueGetter>
CompatibleTypes findCompatibleTypes(const TValueGetter& propertyValue) {
CompatibleTypes compatibleTypes;
for (auto it = propertyValue.begin(); it != propertyValue.end(); ++it) {
if (it->IsBool()) {
// Don't allow booleans to be converted to numeric 0 or 1.
MaskedType booleanType(false);
booleanType.isBool = true;
compatibleTypes &= booleanType;
continue;
}
if (it->IsNumber()) {
compatibleTypes &= getCompatibleTypesForNumber(it);
// Check that the value does not equal one of the possible sentinel
// values.
if (it->IsInt64()) {
compatibleTypes.removeSentinelValues(it->GetInt64());
} else if (it->IsUint64()) {
compatibleTypes.removeSentinelValues(it->GetUint64());
}
continue;
}
if (it->IsArray()) {
// Iterate over all of the elements in the array
// and determine their compatible type.
CompatibleTypes arrayElementCompatibleTypes =
findCompatibleTypes(ArrayOfPropertyValues(*it));
// If the elements inside the array are also arrays, this will return a
// completely incompatible MaskedType, which means the elements will be
// treated like strings.
MaskedType elementType = arrayElementCompatibleTypes.toMaskedType();
MaskedArrayType arrayType(elementType, it->Size(), it->Size());
compatibleTypes &= arrayType;
continue;
}
if (it->IsNull()) {
compatibleTypes.setHasNullValue(true);
// If the value is null, check if there is still a possible sentinel
// values. If none exist, default the type to string.
if (!compatibleTypes.getSentinelValue()) {
compatibleTypes.makeIncompatible();
}
continue;
}
// If this code is reached, the value is a string or something else.
compatibleTypes.makeIncompatible();
// If this is a string, check that the value does not equal one of the
// possible sentinel values.
if (it->IsString()) {
compatibleTypes.removeSentinelValues(it->GetString());
}
}
// If no sentinel value is available, then it's not possible to accurately
// represent the null value of this property. Make it a string property
// instead.
if (compatibleTypes.hasNullValue() && !compatibleTypes.getSentinelValue()) {
compatibleTypes.makeIncompatible();
}
return compatibleTypes;
}
int32_t addBufferToGltf(Model& gltf, std::vector<std::byte>&& buffer) {
const size_t gltfBufferIndex = gltf.buffers.size();
Buffer& gltfBuffer = gltf.buffers.emplace_back();
gltfBuffer.byteLength = static_cast<int64_t>(buffer.size());
gltfBuffer.cesium.data = std::move(buffer);
const size_t bufferViewIndex = gltf.bufferViews.size();
BufferView& bufferView = gltf.bufferViews.emplace_back();
bufferView.buffer = static_cast<int32_t>(gltfBufferIndex);
bufferView.byteOffset = 0;
bufferView.byteLength = gltfBuffer.byteLength;
return static_cast<int32_t>(bufferViewIndex);
}
template <typename TValueGetter>
void updateExtensionWithJsonStringProperty(
Model& gltf,
ClassProperty& classProperty,
const PropertyTable& propertyTable,
PropertyTableProperty& propertyTableProperty,
const TValueGetter& propertyValue) {
rapidjson::StringBuffer rapidjsonStrBuffer;
std::vector<uint64_t> rapidjsonOffsets;
rapidjsonOffsets.reserve(static_cast<size_t>(propertyTable.count + 1));
rapidjsonOffsets.emplace_back(0);
std::optional<std::string> noDataValue;
if (classProperty.noData) {
noDataValue = classProperty.noData->getString();
}
auto it = propertyValue.begin();
for (int64_t i = 0; i < propertyTable.count; ++i) {
if (it == propertyValue.end()) {
rapidjsonOffsets.emplace_back(rapidjsonStrBuffer.GetLength());
continue;
}
if (!it->IsString() || (it->IsNull() && !noDataValue)) {
// Everything else that is not string will be serialized by json
rapidjson::Writer<rapidjson::StringBuffer> writer(rapidjsonStrBuffer);
it->Accept(writer);
} else {
// Because serialized string json will add double quotations in the
// buffer which is not needed by us, we will manually add the string to
// the buffer
const auto& rapidjsonStr = it->IsNull() ? *noDataValue : it->GetString();
rapidjsonStrBuffer.Reserve(it->GetStringLength());
for (rapidjson::SizeType j = 0; j < it->GetStringLength(); ++j) {
rapidjsonStrBuffer.PutUnsafe(rapidjsonStr[j]);
}
}
rapidjsonOffsets.emplace_back(rapidjsonStrBuffer.GetLength());
++it;
}
const uint64_t totalSize = rapidjsonOffsets.back();
std::vector<std::byte> buffer;
std::vector<std::byte> offsetBuffer;
if (isInRangeForUnsignedInteger<uint8_t>(totalSize)) {
copyStringBuffer<uint8_t>(
rapidjsonStrBuffer,
rapidjsonOffsets,
buffer,
offsetBuffer);
propertyTableProperty.stringOffsetType =
PropertyTableProperty::StringOffsetType::UINT8;
} else if (isInRangeForUnsignedInteger<uint16_t>(totalSize)) {
copyStringBuffer<uint16_t>(
rapidjsonStrBuffer,
rapidjsonOffsets,
buffer,
offsetBuffer);
propertyTableProperty.stringOffsetType =
PropertyTableProperty::StringOffsetType::UINT16;
} else if (isInRangeForUnsignedInteger<uint32_t>(totalSize)) {
copyStringBuffer<uint32_t>(
rapidjsonStrBuffer,
rapidjsonOffsets,
buffer,
offsetBuffer);
propertyTableProperty.stringOffsetType =
PropertyTableProperty::StringOffsetType::UINT32;
} else {
copyStringBuffer<uint64_t>(
rapidjsonStrBuffer,
rapidjsonOffsets,
buffer,
offsetBuffer);
propertyTableProperty.stringOffsetType =
PropertyTableProperty::StringOffsetType::UINT64;
}
classProperty.type = ClassProperty::Type::STRING;
propertyTableProperty.values = addBufferToGltf(gltf, std::move(buffer));
propertyTableProperty.stringOffsets =
addBufferToGltf(gltf, std::move(offsetBuffer));
}
template <typename T, typename TRapidJson = T, typename TValueGetter>
void updateExtensionWithJsonScalarProperty(
Model& gltf,
ClassProperty& classProperty,
const PropertyTable& propertyTable,
PropertyTableProperty& propertyTableProperty,
const TValueGetter& propertyValue,
const std::string& componentTypeName) {
CESIUM_ASSERT(propertyValue.size() >= propertyTable.count);
classProperty.type = ClassProperty::Type::SCALAR;
classProperty.componentType = componentTypeName;
// Create a new buffer for this property.
const size_t byteLength =
sizeof(T) * static_cast<size_t>(propertyTable.count);
std::vector<std::byte> buffer(byteLength);
T* p = reinterpret_cast<T*>(buffer.data());
auto it = propertyValue.begin();
std::optional<T> noDataValue;
if (classProperty.noData) {
noDataValue = classProperty.noData->getSafeNumber<T>();
}
for (int64_t i = 0; i < propertyTable.count; ++i, ++p, ++it) {
if (it->IsNull()) {
*p = *noDataValue;
} else {
*p = static_cast<T>(it->template Get<TRapidJson>());
}
}
propertyTableProperty.values = addBufferToGltf(gltf, std::move(buffer));
}
template <typename TValueGetter>
void updateExtensionWithJsonBooleanProperty(
Model& gltf,
ClassProperty& classProperty,
const PropertyTable& propertyTable,
PropertyTableProperty& propertyTableProperty,
const TValueGetter& propertyValue) {
CESIUM_ASSERT(propertyValue.size() >= propertyTable.count);
std::vector<std::byte> buffer(static_cast<size_t>(
glm::ceil(static_cast<double>(propertyTable.count) / 8.0)));
auto it = propertyValue.begin();
for (rapidjson::SizeType i = 0;
i < static_cast<rapidjson::SizeType>(propertyTable.count);
++i) {
const bool value = it->GetBool();
const size_t byteIndex = i / 8;
const size_t bitIndex = i % 8;
buffer[byteIndex] =
static_cast<std::byte>(value << bitIndex) | buffer[byteIndex];
++it;
}
classProperty.type = ClassProperty::Type::BOOLEAN;
propertyTableProperty.values = addBufferToGltf(gltf, std::move(buffer));
}
template <
typename TRapidjson,
typename ValueType,
typename OffsetType,
typename TValueGetter>
void copyVariableLengthScalarArraysToBuffers(
std::vector<std::byte>& valueBuffer,
std::vector<std::byte>& offsetBuffer,
size_t numOfElements,
const PropertyTable& propertyTable,
const TValueGetter& propertyValue) {
valueBuffer.resize(sizeof(ValueType) * numOfElements);
offsetBuffer.resize(
sizeof(OffsetType) * static_cast<size_t>(propertyTable.count + 1));
ValueType* value = reinterpret_cast<ValueType*>(valueBuffer.data());
OffsetType* offsetValue = reinterpret_cast<OffsetType*>(offsetBuffer.data());
OffsetType prevOffset = 0;
auto it = propertyValue.begin();
for (int64_t i = 0; i < propertyTable.count; ++i) {
const auto& jsonArrayMember = *it;
*offsetValue = prevOffset;
++offsetValue;
for (const auto& valueJson : jsonArrayMember.GetArray()) {
*value = static_cast<ValueType>(valueJson.template Get<TRapidjson>());
++value;
}
prevOffset = static_cast<OffsetType>(prevOffset + jsonArrayMember.Size());
++it;
}
*offsetValue = prevOffset;
}
template <typename TRapidjson, typename ValueType, typename TValueGetter>
void updateScalarArrayProperty(
Model& gltf,
ClassProperty& classProperty,
PropertyTableProperty& propertyTableProperty,
const PropertyTable& propertyTable,
const MaskedArrayType& arrayType,
const TValueGetter& propertyValue) {
CESIUM_ASSERT(propertyValue.size() >= propertyTable.count);
classProperty.type = ClassProperty::Type::SCALAR;
classProperty.componentType =
convertPropertyComponentTypeToString(static_cast<PropertyComponentType>(
TypeToPropertyType<ValueType>::component));
classProperty.array = true;
// Handle fixed-length arrays.
if (arrayType.minArrayCount == arrayType.maxArrayCount) {
const size_t arrayCount = static_cast<size_t>(arrayType.minArrayCount);
const size_t numOfValues =
static_cast<size_t>(propertyTable.count) * arrayCount;
std::vector<std::byte> valueBuffer(sizeof(ValueType) * numOfValues);
ValueType* value = reinterpret_cast<ValueType*>(valueBuffer.data());
auto it = propertyValue.begin();
for (int64_t i = 0; i < propertyTable.count; ++i) {
const auto& jsonArrayMember = *it;
for (const auto& valueJson : jsonArrayMember.GetArray()) {
*value = static_cast<ValueType>(valueJson.template Get<TRapidjson>());
++value;
}
++it;
}
classProperty.count = arrayCount;
propertyTableProperty.values =
addBufferToGltf(gltf, std::move(valueBuffer));
return;
}
// Handle variable-length arrays.
// Compute total size of the value buffer.
size_t totalNumElements = 0;
auto it = propertyValue.begin();
for (int64_t i = 0; i < propertyTable.count; ++i) {
const auto& jsonArrayMember = *it;
totalNumElements += jsonArrayMember.Size();
++it;
}
PropertyComponentType offsetType = PropertyComponentType::None;
std::vector<std::byte> valueBuffer;
std::vector<std::byte> offsetBuffer;
const uint64_t maxOffsetValue = totalNumElements * sizeof(ValueType);
if (isInRangeForUnsignedInteger<uint8_t>(maxOffsetValue)) {
copyVariableLengthScalarArraysToBuffers<TRapidjson, ValueType, uint8_t>(
valueBuffer,
offsetBuffer,
totalNumElements,
propertyTable,
propertyValue);
offsetType = PropertyComponentType::Uint8;
} else if (isInRangeForUnsignedInteger<uint16_t>(maxOffsetValue)) {
copyVariableLengthScalarArraysToBuffers<TRapidjson, ValueType, uint16_t>(
valueBuffer,
offsetBuffer,
totalNumElements,
propertyTable,
propertyValue);
offsetType = PropertyComponentType::Uint16;
} else if (isInRangeForUnsignedInteger<uint32_t>(maxOffsetValue)) {
copyVariableLengthScalarArraysToBuffers<TRapidjson, ValueType, uint32_t>(
valueBuffer,
offsetBuffer,
totalNumElements,
propertyTable,
propertyValue);
offsetType = PropertyComponentType::Uint32;
} else if (isInRangeForUnsignedInteger<uint64_t>(maxOffsetValue)) {
copyVariableLengthScalarArraysToBuffers<TRapidjson, ValueType, uint64_t>(
valueBuffer,
offsetBuffer,
totalNumElements,
propertyTable,
propertyValue);
offsetType = PropertyComponentType::Uint64;
}
propertyTableProperty.values = addBufferToGltf(gltf, std::move(valueBuffer));
propertyTableProperty.arrayOffsets =
addBufferToGltf(gltf, std::move(offsetBuffer));
propertyTableProperty.arrayOffsetType =
convertPropertyComponentTypeToString(offsetType);
}
template <typename OffsetType, typename TValueGetter>
void copyStringsToBuffers(
std::vector<std::byte>& valueBuffer,
std::vector<std::byte>& offsetBuffer,
size_t totalByteLength,
size_t numOfString,
const PropertyTable& propertyTable,
const TValueGetter& propertyValue) {
valueBuffer.resize(totalByteLength);
offsetBuffer.resize((numOfString + 1) * sizeof(OffsetType));
OffsetType offset = 0;
size_t offsetIndex = 0;
auto it = propertyValue.begin();
for (int64_t i = 0; i < propertyTable.count; ++i) {
const auto& arrayMember = *it;
for (const auto& str : arrayMember.GetArray()) {
OffsetType byteLength = static_cast<OffsetType>(
str.GetStringLength() * sizeof(rapidjson::Value::Ch));
std::memcpy(valueBuffer.data() + offset, str.GetString(), byteLength);
std::memcpy(
offsetBuffer.data() + offsetIndex * sizeof(OffsetType),
&offset,
sizeof(OffsetType));
offset = static_cast<OffsetType>(offset + byteLength);
++offsetIndex;
}
++it;
}
std::memcpy(
offsetBuffer.data() + offsetIndex * sizeof(OffsetType),
&offset,
sizeof(OffsetType));
}
template <typename OffsetType, typename TValueGetter>
void copyArrayOffsetsForStringArraysToBuffer(
std::vector<std::byte>& offsetBuffer,
const PropertyTable& propertyTable,
const TValueGetter& propertyValue) {
OffsetType prevOffset = 0;