Loading...
Searching...
No Matches
primvar.h
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the Apache License, Version 2.0 (the "Apache License")
5// with the following modification; you may not use this file except in
6// compliance with the Apache License and the following modification to it:
7// Section 6. Trademarks. is deleted and replaced with:
8//
9// 6. Trademarks. This License does not grant permission to use the trade
10// names, trademarks, service marks, or product names of the Licensor
11// and its affiliates, except as required to comply with Section 4(c) of
12// the License and to reproduce the content of the NOTICE file.
13//
14// You may obtain a copy of the Apache License at
15//
16// http://www.apache.org/licenses/LICENSE-2.0
17//
18// Unless required by applicable law or agreed to in writing, software
19// distributed under the Apache License with the above modification is
20// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21// KIND, either express or implied. See the Apache License for the specific
22// language governing permissions and limitations under the Apache License.
23//
24#ifndef PXR_USD_USD_GEOM_PRIMVAR_H
25#define PXR_USD_USD_GEOM_PRIMVAR_H
26
27#include "pxr/pxr.h"
28#include "pxr/usd/usdGeom/api.h"
29#include "pxr/usd/usd/attribute.h"
31
32#include <atomic>
33#include <string>
34#include <vector>
35
36PXR_NAMESPACE_OPEN_SCOPE
37
38
262{
263public:
264
265 // Default constructor returns an invalid Primvar. Exists for
266 // container classes
268 {
269 /* NOTHING */
270 }
271
273 USDGEOM_API
275
277 USDGEOM_API
279
289 USDGEOM_API
290 explicit UsdGeomPrimvar(const UsdAttribute &attr);
291
297 USDGEOM_API
299
308 USDGEOM_API
309 bool SetInterpolation(const TfToken &interpolation);
310
314 USDGEOM_API
316
336 USDGEOM_API
337 int GetElementSize() const;
338
344 USDGEOM_API
345 bool SetElementSize(int eltSize);
346
350 USDGEOM_API
352
353
358 USDGEOM_API
359 static bool IsPrimvar(const UsdAttribute &attr);
360
361
366 USDGEOM_API
367 static bool IsValidPrimvarName(const TfToken& name);
368
371 USDGEOM_API
372 static TfToken StripPrimvarsName(const TfToken& name);
373
376 USDGEOM_API
377 static bool IsValidInterpolation(const TfToken &interpolation);
378
385 USDGEOM_API
387 TfToken *interpolation, int *elementSize) const;
388
389 // ---------------------------------------------------------------
391 // ---------------------------------------------------------------
393
397 operator UsdAttribute const& () const { return _attr; }
398
400 UsdAttribute const &GetAttr() const { return _attr; }
401
405 bool IsDefined() const { return IsPrimvar(_attr); }
406
409 bool HasValue() const { return _attr.HasValue(); }
410
413 bool HasAuthoredValue() const { return _attr.HasAuthoredValue(); }
414
418 explicit operator bool() const {
419 return IsDefined() ? &UsdGeomPrimvar::_attr : 0;
420 }
421
423 TfToken const &GetName() const { return _attr.GetName(); }
424
431 USDGEOM_API
433
440 USDGEOM_API
442
444 TfToken GetBaseName() const { return _attr.GetBaseName(); }
445
447 TfToken GetNamespace() const { return _attr.GetNamespace(); }
448
450 std::vector<std::string> SplitName() const { return _attr.SplitName(); };
451
453 SdfValueTypeName GetTypeName() const { return _attr.GetTypeName(); }
454
459 template <typename T>
460 bool Get(T* value, UsdTimeCode time = UsdTimeCode::Default()) const {
461 return _attr.Get(value, time);
462 }
463
465 template <typename T>
466 bool Set(const T& value, UsdTimeCode time = UsdTimeCode::Default()) const {
467 return _attr.Set(value, time);
468 }
469
477 USDGEOM_API
478 bool GetTimeSamples(std::vector<double>* times) const;
479
486 USDGEOM_API
488 std::vector<double>* times) const;
489
497 USDGEOM_API
499
501
502 // ---------------------------------------------------------------
528
535 USDGEOM_API
536 bool SetIndices(const VtIntArray &indices,
537 UsdTimeCode time = UsdTimeCode::Default()) const;
538
543 USDGEOM_API
544 bool GetIndices(VtIntArray *indices,
545 UsdTimeCode time = UsdTimeCode::Default()) const;
546
550 USDGEOM_API
551 void BlockIndices() const;
552
558 USDGEOM_API
559 bool IsIndexed() const;
560
563 USDGEOM_API
565
568 USDGEOM_API
570
583 USDGEOM_API
584 bool SetUnauthoredValuesIndex(int unauthoredValuesIndex) const;
585
589 USDGEOM_API
591
598 template <typename ScalarType>
600 UsdTimeCode time = UsdTimeCode::Default()) const;
601
609 USDGEOM_API
611 UsdTimeCode time=UsdTimeCode::Default()) const;
612
624 USDGEOM_API
625 static bool ComputeFlattened(VtValue *value, const VtValue &attrVal,
626 const VtIntArray &indices,
627 std::string *errString);
628
640 USDGEOM_API
641 static bool ComputeFlattened(VtValue *value, const VtValue &attrVal,
642 const VtIntArray &indices,
643 int elementSize,
644 std::string *errString);
645
646
648
649 // ---------------------------------------------------------------
679 // ---------------------------------------------------------------
680
684 USDGEOM_API
685 bool IsIdTarget() const;
686
691 USDGEOM_API
692 bool SetIdTarget(const SdfPath& path) const;
693
695
698 friend bool operator==(const UsdGeomPrimvar &lhs, const UsdGeomPrimvar &rhs) {
699 return lhs.GetAttr() == rhs.GetAttr();
700 }
701
704 friend bool operator!=(const UsdGeomPrimvar &lhs, const UsdGeomPrimvar &rhs) {
705 return !(lhs == rhs);
706 }
707
711 friend bool operator<(const UsdGeomPrimvar &lhs, const UsdGeomPrimvar &rhs) {
712 return lhs.GetAttr().GetPath() < rhs.GetAttr().GetPath();
713 }
714
715 // Specialize TfHashAppend for TfHash
716 template <typename HashState>
717 friend void TfHashAppend(HashState& h, const UsdGeomPrimvar& obj) {
718 h.Append(obj.GetAttr());
719 }
720
721 // hash_value overload for std/boost hash.
722 friend size_t hash_value(const UsdGeomPrimvar &obj) {
723 return TfHash{}(obj);
724 }
725
726
727private:
728 friend class UsdGeomImageable;
729 friend class UsdGeomPrimvarsAPI;
730
733 static bool _IsNamespaced(const TfToken& name);
734
742 static TfToken _MakeNamespaced(const TfToken& name, bool quiet=false);
743
744 static TfToken const &_GetNamespacePrefix();
745
765 UsdGeomPrimvar(const UsdPrim& prim, const TfToken& attrName,
766 const SdfValueTypeName &typeName);
767
768 UsdAttribute _attr;
769
770 // Gets or creates the indices attribute corresponding to the primvar.
771 UsdAttribute _GetIndicesAttr(bool create) const;
772
773 // Helper method for computing the flattened value of an indexed primvar.
774 template<typename ScalarType>
775 static bool _ComputeFlattenedHelper(const VtArray<ScalarType> &authored,
776 const VtIntArray &indices,
777 int elementSize,
778 VtArray<ScalarType> *value,
779 std::string *errString);
780
781 // Helper function to evaluate the flattened array value of a primvar given
782 // the attribute value and the indices array.
783 template <typename ArrayType>
784 static bool _ComputeFlattenedArray(const VtValue &attrVal,
785 const VtIntArray &indices,
786 int elementSize,
787 VtValue *value,
788 std::string *errString);
789
790 // Should only be called if _idTargetRelName is set
791 UsdRelationship _GetIdTargetRel(bool create) const;
792
793 // Compute & cache whether or not this primvar can be an idtarget. After a
794 // call to this function, _idTargetStatus will be either IdTargetImpossible
795 // or IdTargetPossible. If the result is "possible" then _idTargetRelName
796 // will contain the relationship name. This function returns true if
797 // _idTargetStatus was set to IdTargetPossible, else false.
798 bool _ComputeIdTargetPossibility() const;
799
800 enum _IdTargetStatus {
801 IdTargetUninitialized,
802 IdTargetInitializing,
803 IdTargetImpossible,
804 IdTargetPossible };
805
806 mutable TfToken _idTargetRelName;
807 mutable std::atomic<_IdTargetStatus> _idTargetStatus;
808};
809
810// We instantiate the following so we can check and provide the correct value
811// for Id attributes.
812template <>
813USDGEOM_API bool UsdGeomPrimvar::Get(std::string* value, UsdTimeCode time) const;
814
815template <>
816USDGEOM_API bool UsdGeomPrimvar::Get(VtStringArray* value, UsdTimeCode time) const;
817
818template <>
819USDGEOM_API bool UsdGeomPrimvar::Get(VtValue* value, UsdTimeCode time) const;
820
821template <typename ScalarType>
822bool
824{
825 VtArray<ScalarType> authored;
826 if (!Get(&authored, time))
827 return false;
828
829 if (!IsIndexed()) {
830 *value = authored;
831 return true;
832 }
833
834 VtIntArray indices;
835 if (!GetIndices(&indices, time)) {
836 TF_WARN("No indices authored for indexed primvar <%s>.",
837 _attr.GetPath().GetText());
838 return false;
839 }
840
841 // If the authored array is empty, there's nothing to do.
842 if (authored.empty())
843 return false;
844
845 std::string errString;
846 bool res = _ComputeFlattenedHelper(authored, indices, GetElementSize(), value, &errString);
847 if (!errString.empty()) {
848 TF_WARN("For primvar %s: %s",
849 UsdDescribe(_attr).c_str(), errString.c_str());
850 }
851 return res;
852}
853
854template<typename ScalarType>
855bool
856UsdGeomPrimvar::_ComputeFlattenedHelper(const VtArray<ScalarType> &authored,
857 const VtIntArray &indices,
858 int elementSize,
859 VtArray<ScalarType> *value,
860 std::string *errString)
861{
862 TF_VERIFY(elementSize >= 1);
863 value->resize(indices.size() * elementSize);
864 bool success = true;
865
866 std::vector<size_t> invalidIndexPositions;
867 for (size_t i=0; i < indices.size(); i++) {
868 if (indices[i] < 0 || static_cast<size_t>((indices[i] + 1) * elementSize) > authored.size()) {
869 invalidIndexPositions.push_back(i);
870 success = false;
871 continue;
872 }
873
874 const size_t indicesIdx = indices[i] * elementSize;
875 const size_t valuesIdx = i * elementSize;
876 for (size_t j=0; j < static_cast<size_t>(elementSize); j++) {
877 size_t index = indicesIdx + j;
878 (*value)[valuesIdx + j] = authored[index];
879 }
880 }
881
882 if (!invalidIndexPositions.empty() && errString) {
883 *errString = TfStringPrintf(
884 "Found %ld invalid indices into authored array of size %ld with"
885 " element size of %i:",
886 invalidIndexPositions.size(),
887 authored.size(), elementSize);
888
889 // Print a maximum of 5 invalid index positions.
890 size_t numElementsToPrint = std::min(invalidIndexPositions.size(),
891 size_t(5));
892 for (size_t i = 0; i < numElementsToPrint ; ++i) {
893 int invalidIndex = indices[invalidIndexPositions[i]];
894 int authoredStartIndex = invalidIndex * elementSize;
895
896 *errString += TfStringPrintf(
897 "\n\t Invalid index %i at position %ld refers to %s of the"
898 " authored array, which is out of bounds",
899 invalidIndex,
900 invalidIndexPositions[i],
901 elementSize == 1 ? TfStringPrintf("index %i", authoredStartIndex).c_str()
902 : TfStringPrintf("indices [%i,...,%i]", authoredStartIndex, authoredStartIndex + elementSize - 1).c_str());
903 }
904 }
905 return success;
906}
907
908
909PXR_NAMESPACE_CLOSE_SCOPE
910
911#endif // USD_PRIMVAR_H
A basic mathematical interval class.
Definition: interval.h:50
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:290
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:88
A user-extensible hashing mechanism for use with runtime hash tables.
Definition: hash.h:477
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:88
Scenegraph object for authoring and retrieving numeric, string, and array valued data,...
Definition: attribute.h:176
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:477
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:436
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:75
Schema wrapper for UsdAttribute for authoring and introspecting attributes that are primvars.
Definition: primvar.h:262
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:444
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:823
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:704
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:447
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:698
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:466
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:405
std::vector< std::string > SplitName() const
Definition: primvar.h:450
bool HasAuthoredValue() const
Return true if the underlying attribute has an unblocked, authored value.
Definition: primvar.h:413
bool Get(T *value, UsdTimeCode time=UsdTimeCode::Default()) const
Get the attribute value of the Primvar at time .
Definition: primvar.h:460
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:453
TfToken const & GetName() const
Definition: primvar.h:423
UsdAttribute const & GetAttr() const
Explicit UsdAttribute extractor.
Definition: primvar.h:400
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:409
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:711
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:83
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:203
const TfToken & GetName() const
Return the full name of this object, i.e.
Definition: object.h:238
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a "Prim" as ...
Definition: prim.h:134
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:128
Represent a time value, which may be either numeric, holding a double value, or a sentinel value UsdT...
Definition: timeCode.h:84
static constexpr UsdTimeCode Default()
Produce a UsdTimeCode representing the sentinel value for 'default'.
Definition: timeCode.h:112
Represents an arbitrary dimensional rectangular container class.
Definition: array.h:228
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:164
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:489
bool empty() const
Return true if this array contains no elements, false otherwise.
Definition: array.h:515
void resize(size_t newSize)
Resize this array.
Definition: array.h:558
#define TF_WARN(...)
Issue a warning, but continue execution.
Definition: diagnostic.h:149
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
Definition: diagnostic.h:283
TF_API std::string TfStringPrintf(const char *fmt,...)
Returns a string formed by a printf()-like specification.