Loading...
Searching...
No Matches
primvar.h
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_USD_USD_GEOM_PRIMVAR_H
8#define PXR_USD_USD_GEOM_PRIMVAR_H
9
10#include "pxr/pxr.h"
11#include "pxr/usd/usdGeom/api.h"
12#include "pxr/usd/usd/attribute.h"
14
15#include <atomic>
16#include <string>
17#include <vector>
18
19PXR_NAMESPACE_OPEN_SCOPE
20
21
248{
249public:
250
251 // Default constructor returns an invalid Primvar. Exists for
252 // container classes
254 {
255 /* NOTHING */
256 }
257
259 USDGEOM_API
261
263 USDGEOM_API
265
275 USDGEOM_API
276 explicit UsdGeomPrimvar(const UsdAttribute &attr);
277
283 USDGEOM_API
285
294 USDGEOM_API
295 bool SetInterpolation(const TfToken &interpolation);
296
300 USDGEOM_API
302
322 USDGEOM_API
323 int GetElementSize() const;
324
330 USDGEOM_API
331 bool SetElementSize(int eltSize);
332
336 USDGEOM_API
338
339
344 USDGEOM_API
345 static bool IsPrimvar(const UsdAttribute &attr);
346
347
352 USDGEOM_API
353 static bool IsValidPrimvarName(const TfToken& name);
354
357 USDGEOM_API
358 static TfToken StripPrimvarsName(const TfToken& name);
359
362 USDGEOM_API
363 static bool IsValidInterpolation(const TfToken &interpolation);
364
371 USDGEOM_API
373 TfToken *interpolation, int *elementSize) const;
374
375 // ---------------------------------------------------------------
377 // ---------------------------------------------------------------
379
383 operator UsdAttribute const& () const { return _attr; }
384
386 UsdAttribute const &GetAttr() const { return _attr; }
387
391 bool IsDefined() const { return IsPrimvar(_attr); }
392
395 bool HasValue() const { return _attr.HasValue(); }
396
399 bool HasAuthoredValue() const { return _attr.HasAuthoredValue(); }
400
404 explicit operator bool() const {
405 return IsDefined() ? &UsdGeomPrimvar::_attr : 0;
406 }
407
409 TfToken const &GetName() const { return _attr.GetName(); }
410
417 USDGEOM_API
419
426 USDGEOM_API
428
430 TfToken GetBaseName() const { return _attr.GetBaseName(); }
431
433 TfToken GetNamespace() const { return _attr.GetNamespace(); }
434
436 std::vector<std::string> SplitName() const { return _attr.SplitName(); };
437
439 SdfValueTypeName GetTypeName() const { return _attr.GetTypeName(); }
440
445 template <typename T>
446 bool Get(T* value, UsdTimeCode time = UsdTimeCode::Default()) const {
447 return _attr.Get(value, time);
448 }
449
451 template <typename T>
452 bool Set(const T& value, UsdTimeCode time = UsdTimeCode::Default()) const {
453 return _attr.Set(value, time);
454 }
455
463 USDGEOM_API
464 bool GetTimeSamples(std::vector<double>* times) const;
465
472 USDGEOM_API
474 std::vector<double>* times) const;
475
483 USDGEOM_API
485
487
488 // ---------------------------------------------------------------
514
521 USDGEOM_API
522 bool SetIndices(const VtIntArray &indices,
523 UsdTimeCode time = UsdTimeCode::Default()) const;
524
529 USDGEOM_API
530 bool GetIndices(VtIntArray *indices,
531 UsdTimeCode time = UsdTimeCode::Default()) const;
532
536 USDGEOM_API
537 void BlockIndices() const;
538
544 USDGEOM_API
545 bool IsIndexed() const;
546
549 USDGEOM_API
551
554 USDGEOM_API
556
569 USDGEOM_API
570 bool SetUnauthoredValuesIndex(int unauthoredValuesIndex) const;
571
575 USDGEOM_API
577
584 template <typename ScalarType>
586 UsdTimeCode time = UsdTimeCode::Default()) const;
587
595 USDGEOM_API
597 UsdTimeCode time=UsdTimeCode::Default()) const;
598
610 USDGEOM_API
611 static bool ComputeFlattened(VtValue *value, const VtValue &attrVal,
612 const VtIntArray &indices,
613 std::string *errString);
614
626 USDGEOM_API
627 static bool ComputeFlattened(VtValue *value, const VtValue &attrVal,
628 const VtIntArray &indices,
629 int elementSize,
630 std::string *errString);
631
632
634
635 // ---------------------------------------------------------------
665 // ---------------------------------------------------------------
666
670 USDGEOM_API
671 bool IsIdTarget() const;
672
677 USDGEOM_API
678 bool SetIdTarget(const SdfPath& path) const;
679
681
684 friend bool operator==(const UsdGeomPrimvar &lhs, const UsdGeomPrimvar &rhs) {
685 return lhs.GetAttr() == rhs.GetAttr();
686 }
687
690 friend bool operator!=(const UsdGeomPrimvar &lhs, const UsdGeomPrimvar &rhs) {
691 return !(lhs == rhs);
692 }
693
697 friend bool operator<(const UsdGeomPrimvar &lhs, const UsdGeomPrimvar &rhs) {
698 return lhs.GetAttr().GetPath() < rhs.GetAttr().GetPath();
699 }
700
701 // Specialize TfHashAppend for TfHash
702 template <typename HashState>
703 friend void TfHashAppend(HashState& h, const UsdGeomPrimvar& obj) {
704 h.Append(obj.GetAttr());
705 }
706
707 // hash_value overload for std/boost hash.
708 friend size_t hash_value(const UsdGeomPrimvar &obj) {
709 return TfHash{}(obj);
710 }
711
712
713private:
714 friend class UsdGeomImageable;
715 friend class UsdGeomPrimvarsAPI;
716
719 static bool _IsNamespaced(const TfToken& name);
720
728 static TfToken _MakeNamespaced(const TfToken& name, bool quiet=false);
729
730 static TfToken const &_GetNamespacePrefix();
731
751 UsdGeomPrimvar(const UsdPrim& prim, const TfToken& attrName,
752 const SdfValueTypeName &typeName);
753
754 UsdAttribute _attr;
755
756 // Gets or creates the indices attribute corresponding to the primvar.
757 UsdAttribute _GetIndicesAttr(bool create) const;
758
759 // Helper method for computing the flattened value of an indexed primvar.
760 template<typename ScalarType>
761 static bool _ComputeFlattenedHelper(const VtArray<ScalarType> &authored,
762 const VtIntArray &indices,
763 int elementSize,
764 VtArray<ScalarType> *value,
765 std::string *errString);
766
767 // Helper function to evaluate the flattened array value of a primvar given
768 // the attribute value and the indices array.
769 template <typename ArrayType>
770 static bool _ComputeFlattenedArray(const VtValue &attrVal,
771 const VtIntArray &indices,
772 int elementSize,
773 VtValue *value,
774 std::string *errString);
775
776 // Should only be called if _idTargetRelName is set
777 UsdRelationship _GetIdTargetRel(bool create) const;
778
779 // Compute & cache whether or not this primvar can be an idtarget. After a
780 // call to this function, _idTargetStatus will be either IdTargetImpossible
781 // or IdTargetPossible. If the result is "possible" then _idTargetRelName
782 // will contain the relationship name. This function returns true if
783 // _idTargetStatus was set to IdTargetPossible, else false.
784 bool _ComputeIdTargetPossibility() const;
785
786 enum _IdTargetStatus {
787 IdTargetUninitialized,
788 IdTargetInitializing,
789 IdTargetImpossible,
790 IdTargetPossible };
791
792 mutable TfToken _idTargetRelName;
793 mutable std::atomic<_IdTargetStatus> _idTargetStatus;
794};
795
796// We instantiate the following so we can check and provide the correct value
797// for Id attributes.
798template <>
799USDGEOM_API bool UsdGeomPrimvar::Get(std::string* value, UsdTimeCode time) const;
800
801template <>
802USDGEOM_API bool UsdGeomPrimvar::Get(VtStringArray* value, UsdTimeCode time) const;
803
804template <>
805USDGEOM_API bool UsdGeomPrimvar::Get(VtValue* value, UsdTimeCode time) const;
806
807template <typename ScalarType>
808bool
810{
811 VtArray<ScalarType> authored;
812 if (!Get(&authored, time))
813 return false;
814
815 if (!IsIndexed()) {
816 *value = authored;
817 return true;
818 }
819
820 VtIntArray indices;
821 if (!GetIndices(&indices, time)) {
822 TF_WARN("No indices authored for indexed primvar <%s>.",
823 _attr.GetPath().GetText());
824 return false;
825 }
826
827 // If the authored array is empty, there's nothing to do.
828 if (authored.empty())
829 return false;
830
831 std::string errString;
832 bool res = _ComputeFlattenedHelper(authored, indices, GetElementSize(), value, &errString);
833 if (!errString.empty()) {
834 TF_WARN("For primvar %s: %s",
835 UsdDescribe(_attr).c_str(), errString.c_str());
836 }
837 return res;
838}
839
840template<typename ScalarType>
841bool
842UsdGeomPrimvar::_ComputeFlattenedHelper(const VtArray<ScalarType> &authored,
843 const VtIntArray &indices,
844 int elementSize,
845 VtArray<ScalarType> *value,
846 std::string *errString)
847{
848 TF_VERIFY(elementSize >= 1);
849 value->resize(indices.size() * elementSize);
850 bool success = true;
851
852 std::vector<size_t> invalidIndexPositions;
853 for (size_t i=0; i < indices.size(); i++) {
854 if (indices[i] < 0 || static_cast<size_t>((indices[i] + 1) * elementSize) > authored.size()) {
855 invalidIndexPositions.push_back(i);
856 success = false;
857 continue;
858 }
859
860 const size_t indicesIdx = indices[i] * elementSize;
861 const size_t valuesIdx = i * elementSize;
862 for (size_t j=0; j < static_cast<size_t>(elementSize); j++) {
863 size_t index = indicesIdx + j;
864 (*value)[valuesIdx + j] = authored[index];
865 }
866 }
867
868 if (!invalidIndexPositions.empty() && errString) {
869 *errString = TfStringPrintf(
870 "Found %ld invalid indices into authored array of size %ld with"
871 " element size of %i:",
872 invalidIndexPositions.size(),
873 authored.size(), elementSize);
874
875 // Print a maximum of 5 invalid index positions.
876 size_t numElementsToPrint = std::min(invalidIndexPositions.size(),
877 size_t(5));
878 for (size_t i = 0; i < numElementsToPrint ; ++i) {
879 int invalidIndex = indices[invalidIndexPositions[i]];
880 int authoredStartIndex = invalidIndex * elementSize;
881
882 *errString += TfStringPrintf(
883 "\n\t Invalid index %i at position %ld refers to %s of the"
884 " authored array, which is out of bounds",
885 invalidIndex,
886 invalidIndexPositions[i],
887 elementSize == 1 ? TfStringPrintf("index %i", authoredStartIndex).c_str()
888 : TfStringPrintf("indices [%i,...,%i]", authoredStartIndex, authoredStartIndex + elementSize - 1).c_str());
889 }
890 }
891 return success;
892}
893
894
895PXR_NAMESPACE_CLOSE_SCOPE
896
897#endif // USD_PRIMVAR_H
A basic mathematical interval class.
Definition: interval.h:33
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:274
SDF_API const char * GetText() const
Returns the string representation of this path as a c string.
Represents a value type name, i.e.
Definition: valueTypeName.h:72
A user-extensible hashing mechanism for use with runtime hash tables.
Definition: hash.h:472
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
Scenegraph object for authoring and retrieving numeric, string, and array valued data,...
Definition: attribute.h:183
USD_API bool HasValue() const
Return true if this attribute has an authored default value, authored time samples or a fallback valu...
bool Set(const T &value, UsdTimeCode time=UsdTimeCode::Default()) const
Set the value of this attribute in the current UsdEditTarget to value at UsdTimeCode time,...
Definition: attribute.h:511
USD_API SdfValueTypeName GetTypeName() const
Return the "scene description" value type name for this attribute.
bool Get(T *value, UsdTimeCode time=UsdTimeCode::Default()) const
Perform value resolution to fetch the value of this attribute at the requested UsdTimeCode time,...
Definition: attribute.h:468
USD_API bool HasAuthoredValue() const
Return true if this attribute has either an authored default value or authored time samples.
Base class for all prims that may require rendering or visualization of some sort.
Definition: imageable.h:58
Schema wrapper for UsdAttribute for authoring and introspecting attributes that are primvars.
Definition: primvar.h:248
USDGEOM_API bool HasAuthoredElementSize() const
Has elementSize been explicitly authored on this Primvar?
USDGEOM_API bool GetTimeSamples(std::vector< double > *times) const
Populates a vector with authored sample times for this primvar.
TfToken GetBaseName() const
Definition: primvar.h:430
USDGEOM_API UsdGeomPrimvar(const UsdAttribute &attr)
Speculative constructor that will produce a valid UsdGeomPrimvar when attr already represents an attr...
static USDGEOM_API bool IsValidInterpolation(const TfToken &interpolation)
Validate that the provided interpolation is a valid setting for interpolation as defined by Interpola...
USDGEOM_API bool ComputeFlattened(VtValue *value, UsdTimeCode time=UsdTimeCode::Default()) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool ComputeFlattened(VtArray< ScalarType > *value, UsdTimeCode time=UsdTimeCode::Default()) const
Computes the flattened value of the primvar at time.
Definition: primvar.h:809
static USDGEOM_API bool IsPrimvar(const UsdAttribute &attr)
Test whether a given UsdAttribute represents valid Primvar, which implies that creating a UsdGeomPrim...
USDGEOM_API bool SetIndices(const VtIntArray &indices, UsdTimeCode time=UsdTimeCode::Default()) const
Sets the indices value of the indexed primvar at time.
USDGEOM_API bool NameContainsNamespaces() const
Does this primvar contain any namespaces other than the "primvars:" namespace?
static USDGEOM_API bool ComputeFlattened(VtValue *value, const VtValue &attrVal, const VtIntArray &indices, int elementSize, std::string *errString)
Computes the flattened value of attrValue given indices and elementSize.
friend bool operator!=(const UsdGeomPrimvar &lhs, const UsdGeomPrimvar &rhs)
Inequality comparison.
Definition: primvar.h:690
USDGEOM_API int GetUnauthoredValuesIndex() const
Returns the index that represents unauthored values in the indices array.
USDGEOM_API bool IsIdTarget() const
Returns true if the primvar is an Id primvar.
USDGEOM_API UsdGeomPrimvar & operator=(const UsdGeomPrimvar &other)
Copy assign.
USDGEOM_API bool GetTimeSamplesInInterval(const GfInterval &interval, std::vector< double > *times) const
Populates a vector with authored sample times in interval.
static USDGEOM_API TfToken StripPrimvarsName(const TfToken &name)
Returns the name, devoid of the "primvars:" token if present, otherwise returns the name unchanged.
TfToken GetNamespace() const
Definition: primvar.h:433
static USDGEOM_API bool ComputeFlattened(VtValue *value, const VtValue &attrVal, const VtIntArray &indices, std::string *errString)
Computes the flattened value of attrValue given indices, assuming an elementSize of 1.
friend bool operator==(const UsdGeomPrimvar &lhs, const UsdGeomPrimvar &rhs)
Equality comparison.
Definition: primvar.h:684
USDGEOM_API bool SetElementSize(int eltSize)
Set the elementSize for this Primvar.
USDGEOM_API bool SetInterpolation(const TfToken &interpolation)
Set the Primvar's interpolation.
USDGEOM_API bool SetUnauthoredValuesIndex(int unauthoredValuesIndex) const
Set the index that represents unauthored values in the indices array.
USDGEOM_API int GetElementSize() const
Return the "element size" for this Primvar, which is 1 if unauthored.
USDGEOM_API TfToken GetPrimvarName() const
Returns the primvar's name, devoid of the "primvars:" namespace.
USDGEOM_API void GetDeclarationInfo(TfToken *name, SdfValueTypeName *typeName, TfToken *interpolation, int *elementSize) const
Convenience function for fetching all information required to properly declare this Primvar.
bool Set(const T &value, UsdTimeCode time=UsdTimeCode::Default()) const
Set the attribute value of the Primvar at time.
Definition: primvar.h:452
USDGEOM_API UsdAttribute CreateIndicesAttr() const
Returns the existing indices attribute if the primvar is indexed or creates a new one.
USDGEOM_API UsdGeomPrimvar(const UsdGeomPrimvar &other)
Copy construct.
bool IsDefined() const
Return true if the underlying UsdAttribute::IsDefined(), and in addition the attribute is identified ...
Definition: primvar.h:391
std::vector< std::string > SplitName() const
Definition: primvar.h:436
bool HasAuthoredValue() const
Return true if the underlying attribute has an unblocked, authored value.
Definition: primvar.h:399
bool Get(T *value, UsdTimeCode time=UsdTimeCode::Default()) const
Get the attribute value of the Primvar at time .
Definition: primvar.h:446
static USDGEOM_API bool IsValidPrimvarName(const TfToken &name)
Test whether a given name represents a valid name of a primvar, which implies that creating a UsdGeom...
SdfValueTypeName GetTypeName() const
Definition: primvar.h:439
TfToken const & GetName() const
Definition: primvar.h:409
UsdAttribute const & GetAttr() const
Explicit UsdAttribute extractor.
Definition: primvar.h:386
USDGEOM_API TfToken GetInterpolation() const
Return the Primvar's interpolation, which is UsdGeomTokens->constant if unauthored.
USDGEOM_API bool SetIdTarget(const SdfPath &path) const
This primvar must be of String or StringArray type for this method to succeed.
USDGEOM_API bool IsIndexed() const
Returns true if the primvar is indexed, i.e., if it has an associated "indices" attribute.
USDGEOM_API UsdAttribute GetIndicesAttr() const
Returns a valid indices attribute if the primvar is indexed.
bool HasValue() const
Return true if the underlying attribute has a value, either from authored scene description or a fall...
Definition: primvar.h:395
USDGEOM_API bool HasAuthoredInterpolation() const
Has interpolation been explicitly authored on this Primvar?
USDGEOM_API void BlockIndices() const
Block the indices that were previously set.
friend bool operator<(const UsdGeomPrimvar &lhs, const UsdGeomPrimvar &rhs)
Less-than operator.
Definition: primvar.h:697
USDGEOM_API bool GetIndices(VtIntArray *indices, UsdTimeCode time=UsdTimeCode::Default()) const
Returns the value of the indices array associated with the indexed primvar at time.
USDGEOM_API bool ValueMightBeTimeVarying() const
Return true if it is possible, but not certain, that this primvar's value changes over time,...
UsdGeomPrimvarsAPI encodes geometric "primitive variables", as UsdGeomPrimvar, which interpolate acro...
Definition: primvarsAPI.h:66
SdfPath GetPath() const
Return the complete scene path to this object on its UsdStage, which may (UsdPrim) or may not (all ot...
Definition: object.h:186
const TfToken & GetName() const
Return the full name of this object, i.e.
Definition: object.h:221
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a "Prim" as ...
Definition: prim.h:117
USD_API TfToken GetBaseName() const
Return this property's name with all namespace prefixes removed, i.e.
USD_API TfToken GetNamespace() const
Return this property's complete namespace prefix.
USD_API std::vector< std::string > SplitName() const
Return this property's name elements including namespaces and its base name as the final element.
A UsdRelationship creates dependencies between scenegraph objects by allowing a prim to target other ...
Definition: relationship.h:111
Represent a time value, which may be either numeric, holding a double value, or a sentinel value UsdT...
Definition: timeCode.h:72
static constexpr UsdTimeCode Default()
Produce a UsdTimeCode representing the sentinel value for 'default'.
Definition: timeCode.h:113
Represents an arbitrary dimensional rectangular container class.
Definition: array.h:213
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:152
USD_API std::string UsdDescribe(const UsdObject &)
Return a human-readable description.
size_t size() const
Return the total number of elements in this array.
Definition: array.h:474
bool empty() const
Return true if this array contains no elements, false otherwise.
Definition: array.h:502
void resize(size_t newSize)
Resize this array.
Definition: array.h:545
#define TF_WARN(...)
Issue a warning, but continue execution.
Definition: diagnostic.h:132
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
Definition: diagnostic.h:266
TF_API std::string TfStringPrintf(const char *fmt,...)
Returns a string formed by a printf()-like specification.