-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathprim-types.hh
3769 lines (3004 loc) · 99.6 KB
/
prim-types.hh
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
// SPDX-License-Identifier: Apache 2.0
#pragma once
#ifdef _MSC_VER
#ifndef NOMINMAX
#define NOMINMAX
#endif
#endif
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <limits>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#if defined(TINYUSDZ_ENABLE_THREAD)
#include <mutex>
#include <thread>
#endif
//
#include "value-types.hh"
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
#endif
#include "nonstd/expected.hpp"
#include "nonstd/optional.hpp"
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#include "handle-allocator.hh"
#include "primvar.hh"
//
#include "value-eval-util.hh"
namespace tinyusdz {
// SpecType enum must be same order with pxrUSD's SdfSpecType(since enum value
// is stored in Crate directly)
enum class SpecType {
Unknown = 0, // must be 0
Attribute,
Connection,
Expression,
Mapper,
MapperArg,
Prim,
PseudoRoot,
Relationship,
RelationshipTarget,
Variant,
VariantSet,
Invalid, // or NumSpecTypes
};
enum class Orientation {
RightHanded, // 0
LeftHanded,
Invalid
};
enum class Visibility {
Inherited, // "inherited" (default)
Invisible, // "invisible"
Invalid
};
enum class Purpose {
Default, // 0
Render, // "render"
Proxy, // "proxy"
Guide, // "guide"
};
//
// USDZ extension: sceneLibrary
// https://developer.apple.com/documentation/arkit/usdz_schemas_for_ar/scenelibrary
//
enum class Kind {
Model,
Group,
Assembly,
Component,
Subcomponent,
SceneLibrary, // USDZ extension
UserDef, // Unknown or user defined Kind
Invalid
};
// Attribute interpolation
enum class Interpolation {
Constant, // "constant"
Uniform, // "uniform"
Varying, // "varying"
Vertex, // "vertex"
FaceVarying, // "faceVarying"
Invalid
};
// NOTE: Attribute cannot have ListEdit qualifier
enum class ListEditQual {
ResetToExplicit, // "unqualified"(no qualifier)
Append, // "append"
Add, // "add"
Delete, // "delete"
Prepend, // "prepend"
Order, // "order"
Invalid
};
enum class Axis { X, Y, Z, Invalid };
// metrics(UsdGeomLinearUnits in pxrUSD)
// To avoid linkage error, defined as static constexpr function.
struct Units {
static constexpr double Nanometers = 1e-9;
static constexpr double Micrometers = 1e-6;
static constexpr double Millimeters = 0.001;
static constexpr double Centimeters = 0.01;
static constexpr double Meters = 1.0;
static constexpr double Kilometers = 1000;
static constexpr double LightYears = 9.4607304725808e15;
static constexpr double Inches = 0.0254;
static constexpr double Feet = 0.3048;
static constexpr double Yards = 0.9144;
static constexpr double Miles = 1609.344;
};
// For PrimSpec
enum class Specifier {
Def, // 0
Over,
Class,
Invalid
};
enum class Permission {
Public, // 0
Private,
Invalid
};
enum class Variability {
Varying, // 0
Uniform,
Config,
Invalid
};
// Return false when invalid character(e.g. '%') exists in a given string.
// This function only validates `elementName` of a Prim(e.g. "dora", "xform1").
// If you want to validate a Prim path(e.g. "/root/xform1"),
// Use ValidatePrimPath() in path-util.hh
bool ValidatePrimElementName(const std::string &tok);
///
/// Simlar to SdfPath.
/// NOTE: We are doging refactoring of Path class, so the following comment may
/// not be correct.
///
/// We don't need the performance for USDZ, so use naiive implementation
/// to represent Path.
/// Path is something like Unix path, delimited by `/`, ':' and '.'
/// Square brackets('<', '>' is not included)
///
/// Root path is represented as prim path "/" and elementPath ""(empty).
///
/// Example:
///
/// - `/muda/bora.dora` : prim_part is `/muda/bora`, prop_part is `.dora`.
/// - `bora` : Could be Element(leaf) path or Relative path
///
/// ':' is a namespce delimiter(example `input:muda`).
///
/// Limitations:
///
/// - Relational attribute path(`[` `]`. e.g. `/muda/bora[/ari].dora`) is not
/// supported.
/// - Variant chars('{' '}') is not supported(yet).
/// - Relative path(e.g. '../') is not yet supported(TODO)
///
/// and have more limitatons.
///
class Path {
public:
// Similar to SdfPathNode
enum class PathType {
Prim,
PrimProperty,
RelationalAttribute,
MapperArg,
Target,
Mapper,
PrimVariantSelection,
Expression,
Root,
};
Path() : _valid(false) {}
static Path make_root_path() {
Path p = Path("/", "");
// elementPath is empty for root.
p._element = "";
p._valid = true;
return p;
}
// Create Path both from Prim Path and Prop
// If `prim` starts
// "/aaa", "bora" => /aaa.bora
// "/aaa", "" => /aaa (prim only)
// "", "bora" => .bora (property only)
//
// Note: This constructor may fail to extract elementName from given `prim`
// and `prop`. It is highly recommended to use AppendPrim() and AppendProperty
// to. construct Path hierarchy(e.g. `/aaa/xform/geom.points`) so that
// elementName is set correctly.
Path(const std::string &prim, const std::string &prop);
// : prim_part(prim), valid(true) {}
// Path(const std::string &prim, const std::string &prop)
// : prim_part(prim), prop_part(prop) {}
Path(const Path &rhs) = default;
Path &operator=(const Path &rhs) {
this->_valid = rhs._valid;
this->_prim_part = rhs._prim_part;
this->_prop_part = rhs._prop_part;
this->_element = rhs._element;
return (*this);
}
std::string full_path_name() const {
std::string s;
if (!_valid) {
s += "#INVALID#";
}
s += _prim_part;
if (_prop_part.empty()) {
return s;
}
s += "." + _prop_part;
return s;
}
const std::string &prim_part() const { return _prim_part; }
const std::string &prop_part() const { return _prop_part; }
const std::string &variant_part() const {
_variant_part_str =
"{" + _variant_part + "=" + _variant_selection_part + "}";
return _variant_part_str;
}
void set_path_type(const PathType ty) { _path_type = ty; }
bool get_path_type(PathType &ty) {
if (_path_type) {
ty = _path_type.value();
}
return false;
}
// IsPropertyPath: PrimProperty or RelationalAttribute
bool is_property_path() const {
if (_path_type) {
if ((_path_type.value() == PathType::PrimProperty ||
(_path_type.value() == PathType::RelationalAttribute))) {
return true;
}
}
// TODO: RelationalAttribute
if (_prim_part.empty()) {
return false;
}
if (_prop_part.size()) {
return true;
}
return false;
}
// Is Prim path?
bool is_prim_path() const {
if (_prop_part.size()) {
return false;
}
if (_prim_part.size()) {
return true;
}
return false;
}
// Is Prim's property path?
// True when both PrimPart and PropPart are not empty.
bool is_prim_property_path() const {
if (_prim_part.empty()) {
return false;
}
if (_prop_part.size()) {
return true;
}
return false;
}
bool is_valid() const { return _valid; }
bool is_empty() {
return (_prim_part.empty() && _variant_part.empty() && _prop_part.empty());
}
// static Path RelativePath() { return Path("."); }
// Append property path(change internal state)
Path append_property(const std::string &elem);
// Append prim or variantSelection path(change internal state)
Path append_element(const std::string &elem);
Path append_prim(const std::string &elem) {
return append_element(elem);
} // for legacy
// Const version. Does not change internal state.
const Path AppendProperty(const std::string &elem) const;
const Path AppendPrim(const std::string &elem) const;
const Path AppendElement(const std::string &elem) const;
// Get element name(the last element of Path. i.e. Prim's name, Property's
// name)
const std::string &element_name() const;
///
/// Split a path to the root(common ancestor) and its siblings
///
/// example:
///
/// - / -> [/, Empty]
/// - /bora -> [/bora, Empty]
/// - /bora/dora -> [/bora, /dora]
/// - /bora/dora/muda -> [/bora, /dora/muda]
/// - bora -> [Empty, bora]
/// - .muda -> [Empty, .muda]
///
std::pair<Path, Path> split_at_root() const;
///
/// TODO: Deprecate(use get_parent_path() instead)
///
/// Get parent Prim path.
/// If the given path is a root Prim path(e.g. "/bora"), same Path is
/// returned.
///
/// example:
///
/// - / -> invalid Path
/// - /bora -> /bora
/// - /bora/dora -> /bora
/// - /bora/dora.prop -> /bora/dora
/// - dora/bora -> dora
/// - dora -> invalid Path
/// - .dora -> invalid Path(path is property path)
Path get_parent_prim_path() const;
///
/// Get parent Path.
/// If the given path is the root path("/") same Path is returned.
///
/// example:
///
/// - / -> invalid Path
/// - /bora -> /
/// - /bora/dora -> /bora
/// - /bora/dora.prop -> /bora/dora
/// - dora/bora -> dora
/// - dora -> invalid Path
/// - .dora -> invalid Path(path is property path)
Path get_parent_path() const;
///
/// Check if this Path has same prefix for given Path
///
/// example.
/// rhs path: /bora/dora
///
/// /bora/dora/muda -> true
/// /bora/dora2 -> fase
///
/// If the prefix path contains prop part, compare it with ==
/// (assume no hierarchy in property part)
///
bool has_prefix(const Path &rhs) const;
///
/// @returns true if a path is '/' only
///
bool is_root_path() const {
if (!_valid) {
return false;
}
if ((_prim_part.size() == 1) && (_prim_part[0] == '/')) {
return true;
}
return false;
}
///
/// @returns true if a path is root prim: e.g. '/bora'
///
bool is_root_prim() const {
if (!_valid) {
return false;
}
if (is_root_path()) {
return false;
}
if ((_prim_part.size() > 1) && (_prim_part[0] == '/')) {
// no other '/' except for the fist one
if (_prim_part.find_last_of('/') == 0) {
return true;
}
}
return false;
}
bool is_absolute_path() const {
if (_prim_part.size() && _prim_part[0] == '/') {
return true;
}
return false;
}
bool is_relative_path() const {
if (_prim_part.size()) {
return !is_absolute_path();
}
return true; // prop part only
}
#if 0 // TODO: rmove
bool is_variant_selection_path() const {
if (!is_valid()) {
return false;
}
if (_variant_part.size()) {
return true;
}
return false;
}
#endif
// Strip '/'
Path &make_relative() {
if (is_absolute_path() && (_prim_part.size() > 1)) {
// Remove first '/'
_prim_part.erase(0, 1);
}
return *this;
}
const Path make_relative(Path &&rhs) {
(*this) = std::move(rhs);
return make_relative();
}
static const Path make_relative(const Path &rhs) {
Path p = rhs; // copy
return p.make_relative();
}
static bool LessThan(const Path &lhs, const Path &rhs);
// To sort paths lexicographically.
// TODO: consider abs and relative path correctly
bool operator<(const Path &rhs) const {
if (full_path_name() == rhs.full_path_name()) {
return false;
}
if (prim_part().empty() || rhs.prim_part().empty()) {
return prim_part().empty() && rhs.prim_part().size();
}
return LessThan(*this, rhs);
}
private:
std::string _prim_part; // e.g. /Model/MyMesh, MySphere
std::string _prop_part; // e.g. visibility (`.` is not included)
std::string _variant_part; // e.g. `variantColor` for {variantColor=green}
std::string _variant_selection_part; // e.g. `green` for {variantColor=green}
// . Could be empty({variantColor=}).
mutable std::string _variant_part_str; // str buffer for variant_part()
mutable std::string _element; // Element name
nonstd::optional<PathType> _path_type; // Currently optional.
bool _valid{false};
};
#if 0
///
/// Split Path by the delimiter(e.g. "/") then create lists.
///
class TokenizedPath {
public:
TokenizedPath() {}
TokenizedPath(const Path &path) {
std::string s = path.prop_part();
if (s.empty()) {
// ???
return;
}
if (s[0] != '/') {
// Path must start with "/"
return;
}
s.erase(0, 1);
char delimiter = '/';
size_t pos{0};
while ((pos = s.find(delimiter)) != std::string::npos) {
std::string token = s.substr(0, pos);
_tokens.push_back(token);
s.erase(0, pos + sizeof(char));
}
if (!s.empty()) {
// leaf element
_tokens.push_back(s);
}
}
private:
std::vector<std::string> _tokens;
};
#endif
bool operator==(const Path &lhs, const Path &rhs);
// variants in Prim Meta.
//
// e.g.
// variants = {
// string variant0 = "bora"
// string variant1 = "dora"
// }
// pxrUSD uses dict type for the content, but TinyUSDZ only accepts list of
// strings for now
//
using VariantSelectionMap = std::map<std::string, std::string>;
class MetaVariable;
// TODO: Use `Dictionary` and deprecate CustomDataType
using CustomDataType = std::map<std::string, MetaVariable>;
using Dictionary = CustomDataType; // alias to CustomDataType
///
/// Helper function to access CustomData(dictionary).
/// Recursively process into subdictionaries when a key contains namespaces(':')
///
bool HasCustomDataKey(const Dictionary &customData, const std::string &key);
bool GetCustomDataByKey(const Dictionary &customData, const std::string &key,
/* out */ MetaVariable *dst);
bool SetCustomDataByKey(const std::string &key, const MetaVariable &val,
/* inout */ Dictionary &customData);
void OverrideDictionary(Dictionary &customData, const Dictionary &src, const bool override_existing = true);
// Variable class for Prim and Attribute Metadataum.
//
// - Accepts limited number of types for value
// - No 'custom' keyword
// - 'None'(Value Block) is supported for some type(at least `references` and
// `payload` accepts None)
// - No TimeSamples, No Connection, No Relationship(`rel`)
// - Value must be assigned(e.g. "float myval = 1.3"). So no definition only
// syntax("float myval")
// - Can be string only(no type information)
// - Its variable name is interpreted as "comment"
//
class MetaVariable {
public:
MetaVariable &operator=(const MetaVariable &rhs) {
_name = rhs._name;
_value = rhs._value;
return *this;
}
template <typename T>
MetaVariable(const T &v) {
set_value(v);
}
MetaVariable(const MetaVariable &rhs) {
_name = rhs._name;
_value = rhs._value;
}
template <typename T>
MetaVariable(const std::string &name, const T &v) {
set_value(name, v);
}
// template <typename T>
// bool is() const {
// return value.index() == ValueType::index_of<T>();
// }
bool is_valid() const {
return _value.type_id() != value::TypeTraits<std::nullptr_t>::type_id();
}
//// TODO
// bool is_timesamples() const { return false; }
MetaVariable() = default;
//
// custom data must have some value, so no set_type()
// OK "float myval = 1"
// NG "float myval"
//
template <typename T>
void set_value(const T &v) {
// TODO: Check T is supported type for Metadatum.
_value = v;
_name = std::string(); // empty
}
template <typename T>
void set_value(const std::string &name, const T &v) {
// TODO: Check T is supported type for Metadatum.
_value = v;
_name = name;
}
template <typename T>
bool get_value(T *dst) const {
if (!dst) {
return false;
}
if (const T *v = _value.as<T>()) {
(*dst) = *v;
return true;
}
return false;
}
template <typename T>
nonstd::optional<T> get_value() const {
if (const T *v = _value.as<T>()) {
return *v;
}
return nonstd::nullopt;
}
void set_name(const std::string &name) { _name = name; }
const std::string &get_name() const { return _name; }
const value::Value &get_raw_value() const { return _value; }
value::Value &get_raw_value() { return _value; }
// No set_type_name()
const std::string type_name() const { return TypeName(*this); }
uint32_t type_id() const { return TypeId(*this); }
bool is_blocked() const {
return type_id() == value::TypeId::TYPE_ID_VALUEBLOCK;
}
private:
static std::string TypeName(const MetaVariable &v) {
return v._value.type_name();
}
static uint32_t TypeId(const MetaVariable &v) { return v._value.type_id(); }
private:
value::Value _value{nullptr};
std::string _name;
};
struct AssetInfo {
// builtin fields
value::AssetPath identifier;
std::string name;
std::vector<value::AssetPath> payloadAssetDependencies;
std::string version;
// Other fields
Dictionary _fields;
};
// USDZ AR class?
// Preliminary_Trigger,
// Preliminary_PhysicsGravitationalForce,
// Preliminary_InfiniteColliderPlane,
// Preliminary_ReferenceImage,
// Preliminary_Action,
// Preliminary_Text,
struct APISchemas {
// TinyUSDZ does not allow user-supplied API schema for now
enum class APIName {
MaterialBindingAPI, // "MaterialBindingAPI"
SkelBindingAPI, // "SkelBindingAPI"
ShapingAPI, // "ShapingAPI"(usdLux)
// USDZ AR extensions
Preliminary_AnchoringAPI,
Preliminary_PhysicsColliderAPI,
Preliminary_PhysicsMaterialAPI,
Preliminary_PhysicsRigidBodyAPI,
};
ListEditQual listOpQual{ListEditQual::ResetToExplicit}; // must be 'prepend'
// std::get<1>: instance name. For Multi-apply API Schema e.g.
// `material:MainMaterial` for `CollectionAPI:material:MainMaterial`
std::vector<std::pair<APIName, std::string>> names;
};
// SdfLayerOffset
struct LayerOffset {
double _offset{0.0};
double _scale{1.0};
};
// SdfReference
struct Reference {
value::AssetPath asset_path;
Path prim_path;
LayerOffset layerOffset;
Dictionary customData;
};
// SdfPayload
struct Payload {
value::AssetPath asset_path; // std::string in SdfPayload
Path prim_path;
LayerOffset layerOffset; // from 0.8.0
// No customData for Payload
};
// Metadata for Prim
struct PrimMetas {
nonstd::optional<bool> active; // 'active'
nonstd::optional<bool> hidden; // 'hidden'
nonstd::optional<Kind> kind; // 'kind'. user-defined kind value is stored in _kind_str;
std::string _kind_str;
nonstd::optional<Dictionary>
assetInfo; // 'assetInfo' // TODO: Use AssetInfo?
nonstd::optional<Dictionary> customData; // `customData`
nonstd::optional<value::StringData> doc; // 'documentation'
nonstd::optional<value::StringData>
comment; // 'comment' (String only metadata value)
nonstd::optional<APISchemas> apiSchemas; // 'apiSchemas'
nonstd::optional<Dictionary>
sdrMetadata; // 'sdrMetadata' (usdShade Prim only?)
nonstd::optional<bool> instanceable; // 'instanceable'
nonstd::optional<Dictionary> clips; // 'clips'
// String representation of Kind.
// For user-defined Kind, it returns `_kind_str`
const std::string get_kind() const;
//
// AssetInfo utility function
//
// Convert CustomDataType to AssetInfo
AssetInfo get_assetInfo(bool *authored = nullptr) const;
//
// Compositions
//
nonstd::optional<std::pair<ListEditQual, std::vector<Reference>>> references;
nonstd::optional<std::pair<ListEditQual, std::vector<Payload>>>
payload; // NOTE: not `payloads`
nonstd::optional<std::pair<ListEditQual, std::vector<Path>>>
inherits; // 'inherits'
nonstd::optional<std::pair<ListEditQual, std::vector<std::string>>>
variantSets; // 'variantSets'. Could be `token` but treat as
// `string`(Crate format uses `string`)
nonstd::optional<VariantSelectionMap> variants; // `variants`
nonstd::optional<std::pair<ListEditQual, std::vector<Path>>>
specializes; // 'specializes'
// USDZ extensions
nonstd::optional<std::string> sceneName; // 'sceneName'
// Omniverse extensions(TODO: Use UTF8 string type?)
// https://github.com/PixarAnimationStudios/USD/pull/2055
nonstd::optional<std::string> displayName; // 'displayName'
// Unregistered metadatum. value is represented as string.
std::map<std::string, std::string> unregisteredMetas;
Dictionary meta; // other non-buitin meta values. TODO: remove this variable
// and use `customData` instead, since pxrUSD does not allow
// non-builtin Prim metadatum
///
/// Update metadatum with rhs(authored metadataum only)
///
/// @param[in] override_authored true: override this.metadataum(authored or not-authored) when rhs.metadatum is authoerd, false override only when this.metadatum is not authored and rhs.metadataum is authored.
///
void update_from(const PrimMetas &rhs, bool override_authored = true);
#if 0
// String only metadataum.
// TODO: Represent as `MetaVariable`?
std::vector<value::StringData> stringData;
#endif
// FIXME: Find a better way to detect Prim meta is authored...
bool authored() const {
return (active || hidden || kind || customData || references || payload ||
inherits || variants || variantSets || specializes || displayName ||
sceneName || doc || comment || unregisteredMetas.size() || meta.size() || apiSchemas ||
sdrMetadata || assetInfo || instanceable || clips);
}
//
// Infos used indirectly.
//
// Used to display/traverse Prim items based on this array
// USDA: By appearance. USDC: "primChildren" TokenVector field
std::vector<value::token> primChildren;
// Used to display/traverse Property items based on this array
// USDA: By appearance. USDC: "properties" TokenVector field
std::vector<value::token> properties;
nonstd::optional<std::pair<ListEditQual, std::vector<Path>>> inheritPaths;
nonstd::optional<std::vector<value::token>> variantChildren;
nonstd::optional<std::vector<value::token>> variantSetChildren;
};
// For backward compatibility
using PrimMeta = PrimMetas;
// Metadata for Property(Relationship and Attribute)
// TODO: Rename to PropMetas
struct AttrMetas {
// frequently used items
// nullopt = not specified in USD data
nonstd::optional<Interpolation> interpolation; // 'interpolation'
nonstd::optional<uint32_t> elementSize; // usdSkel 'elementSize'
nonstd::optional<bool> hidden; // 'hidden'
nonstd::optional<value::StringData> comment; // `comment`
nonstd::optional<Dictionary> customData; // `customData`
nonstd::optional<double> weight; // usdSkel inbetween BlendShape weight.
// usdShade
nonstd::optional<value::token> connectability; // NOTE: applies to attr
nonstd::optional<value::token> outputName; // NOTE: applies to rel
nonstd::optional<value::token> renderType; // NOTE: applies to prop
nonstd::optional<Dictionary> sdrMetadata; // NOTE: applies to attr(also seen in prim meta)
nonstd::optional<std::string> displayName; // 'displayName'
//
// MaterialBinding
//
// Could be arbitrary token value so use `token[]` type.
// For now, either `weakerThanDescendants` or `strongerThanDescendants` are
// valid token.
nonstd::optional<value::token> bindMaterialAs; // 'bindMaterialAs' NOTE: applies to rel.
std::map<std::string, MetaVariable> meta; // other meta values
// String only metadataum.
// TODO: Represent as `MetaVariable`?
std::vector<value::StringData> stringData;
bool authored() const {
return (interpolation || elementSize || hidden || customData || weight ||
connectability || outputName || renderType || sdrMetadata || displayName || bindMaterialAs || meta.size() || stringData.size());
}
};
// For backward compatibility
using AttrMeta = AttrMetas;
using PropMetas = AttrMetas;
// Typed TimeSamples value
//
// double radius.timeSamples = { 0: 1.0, 1: None, 2: 3.0 }
//
// in .usd, are represented as
//
// 0: (1.0, false)
// 1: (2.0, true)
// 2: (3.0, false)
//
template <typename T>
struct TypedTimeSamples {
public:
struct Sample {
double t;
T value;
bool blocked{false};
};
bool empty() const { return _samples.empty(); }
void update() const {
std::sort(_samples.begin(), _samples.end(),
[](const Sample &a, const Sample &b) { return a.t < b.t; });
_dirty = false;
return;
}
// Get value at specified time.
// Return linearly interpolated value when TimeSampleInterpolationType is
// Linear. Returns nullopt when specified time is out-of-range.
bool get(T *dst, double t = value::TimeCode::Default(),
value::TimeSampleInterpolationType interp =
value::TimeSampleInterpolationType::Held) const {
if (!dst) {
return false;
}
if (empty()) {
return false;
}
if (_dirty) {
update();
}
if (value::TimeCode(t).is_default()) {
// FIXME: Use the first item for now.
// TODO: Handle bloked
(*dst) = _samples[0].value;
return true;
} else {
auto it = std::lower_bound(
_samples.begin(), _samples.end(), t,
[](const Sample &a, double tval) { return a.t < tval; });