All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 USDGEOM_PRIMVAR_H
25 #define USDGEOM_PRIMVAR_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/usd/usdGeom/api.h"
29 #include "pxr/usd/usd/attribute.h"
30 #include "pxr/usd/usdGeom/tokens.h"
31 
32 #include <string>
33 #include <vector>
34 
35 PXR_NAMESPACE_OPEN_SCOPE
36 
37 
261 {
262 public:
263 
264  // Default constructor returns an invalid Primvar. Exists for
265  // container classes
267  {
268  /* NOTHING */
269  }
270 
280  USDGEOM_API
281  explicit UsdGeomPrimvar(const UsdAttribute &attr);
282 
288  USDGEOM_API
289  TfToken GetInterpolation() const;
290 
299  USDGEOM_API
300  bool SetInterpolation(const TfToken &interpolation);
301 
305  USDGEOM_API
306  bool HasAuthoredInterpolation() const;
307 
327  USDGEOM_API
328  int GetElementSize() const;
329 
335  USDGEOM_API
336  bool SetElementSize(int eltSize);
337 
341  USDGEOM_API
342  bool HasAuthoredElementSize() const;
343 
344 
349  USDGEOM_API
350  static bool IsPrimvar(const UsdAttribute &attr);
351 
354  USDGEOM_API
355  static bool IsValidInterpolation(const TfToken &interpolation);
356 
363  USDGEOM_API
364  void GetDeclarationInfo(TfToken *name, SdfValueTypeName *typeName,
365  TfToken *interpolation, int *elementSize) const;
366 
367  // ---------------------------------------------------------------
369  // ---------------------------------------------------------------
371 
375  operator UsdAttribute const& () const { return _attr; }
376 
378  UsdAttribute const &GetAttr() const { return _attr; }
379 
383  bool IsDefined() const { return IsPrimvar(_attr); }
384 
387  bool HasValue() const { return _attr.HasValue(); }
388 
391  bool HasAuthoredValue() const { return _attr.HasAuthoredValue(); }
392 
396  explicit operator bool() const {
397  return IsDefined() ? &UsdGeomPrimvar::_attr : 0;
398  }
399 
401  TfToken const &GetName() const { return _attr.GetName(); }
402 
409  USDGEOM_API
410  TfToken GetPrimvarName() const;
411 
418  USDGEOM_API
419  bool NameContainsNamespaces() const;
420 
422  TfToken GetBaseName() const { return _attr.GetBaseName(); }
423 
425  TfToken GetNamespace() const { return _attr.GetNamespace(); }
426 
428  std::vector<std::string> SplitName() const { return _attr.SplitName(); };
429 
431  SdfValueTypeName GetTypeName() const { return _attr.GetTypeName(); }
432 
437  template <typename T>
438  bool Get(T* value, UsdTimeCode time = UsdTimeCode::Default()) const {
439  return _attr.Get(value, time);
440  }
441 
443  template <typename T>
444  bool Set(const T& value, UsdTimeCode time = UsdTimeCode::Default()) const {
445  return _attr.Set(value, time);
446  }
447 
455  USDGEOM_API
456  bool GetTimeSamples(std::vector<double>* times) const;
457 
464  USDGEOM_API
465  bool GetTimeSamplesInInterval(const GfInterval& interval,
466  std::vector<double>* times) const;
467 
475  USDGEOM_API
476  bool ValueMightBeTimeVarying() const;
477 
479 
480  // ---------------------------------------------------------------
506 
513  USDGEOM_API
514  bool SetIndices(const VtIntArray &indices,
515  UsdTimeCode time = UsdTimeCode::Default()) const;
516 
521  USDGEOM_API
522  bool GetIndices(VtIntArray *indices,
523  UsdTimeCode time = UsdTimeCode::Default()) const;
524 
528  USDGEOM_API
529  void BlockIndices() const;
530 
536  USDGEOM_API
537  bool IsIndexed() const;
538 
541  USDGEOM_API
543 
546  USDGEOM_API
548 
561  USDGEOM_API
562  bool SetUnauthoredValuesIndex(int unauthoredValuesIndex) const;
563 
567  USDGEOM_API
568  int GetUnauthoredValuesIndex() const;
569 
576  template <typename ScalarType>
578  UsdTimeCode time = UsdTimeCode::Default()) const;
579 
587  USDGEOM_API
588  bool ComputeFlattened(VtValue *value,
589  UsdTimeCode time=UsdTimeCode::Default()) const;
590 
601  USDGEOM_API
602  static bool ComputeFlattened(VtValue *value, const VtValue &attrVal,
603  const VtIntArray &indices,
604  std::string *errString);
605 
606 
608 
609  // ---------------------------------------------------------------
639  // ---------------------------------------------------------------
640 
644  USDGEOM_API
645  bool IsIdTarget() const;
646 
651  USDGEOM_API
652  bool SetIdTarget(const SdfPath& path) const;
653 
655 
658  friend bool operator==(const UsdGeomPrimvar &lhs, const UsdGeomPrimvar &rhs) {
659  return lhs.GetAttr() == rhs.GetAttr();
660  }
661 
664  friend bool operator!=(const UsdGeomPrimvar &lhs, const UsdGeomPrimvar &rhs) {
665  return !(lhs == rhs);
666  }
667 
671  friend bool operator<(const UsdGeomPrimvar &lhs, const UsdGeomPrimvar &rhs) {
672  return lhs.GetAttr().GetPath() < rhs.GetAttr().GetPath();
673  }
674 
675  // hash_value overload for std/boost hash.
676  USDGEOM_API
677  friend size_t hash_value(const UsdGeomPrimvar &obj) {
678  return hash_value(obj.GetAttr());
679  }
680 
681 
682 private:
683  friend class UsdGeomImageable;
684  friend class UsdGeomPrimvarsAPI;
685 
688  static bool _IsNamespaced(const TfToken& name);
689 
697  static TfToken _MakeNamespaced(const TfToken& name, bool quiet=false);
698 
699  static TfToken const &_GetNamespacePrefix();
700 
720  UsdGeomPrimvar(const UsdPrim& prim, const TfToken& attrName,
721  const SdfValueTypeName &typeName);
722 
723  UsdAttribute _attr;
724 
725  // upon construction, we'll take note of the attrType. If we're a type
726  // that could possibly have an Id associated with it, we'll store that name
727  // so we don't have to pay the cost of constructing that token per-Get().
728  void _SetIdTargetRelName();
729 
730  // Gets or creates the indices attribute corresponding to the primvar.
731  UsdAttribute _GetIndicesAttr(bool create) const;
732 
733  // Helper method for computing the flattened value of an indexed primvar.
734  template<typename ScalarType>
735  static bool _ComputeFlattenedHelper(const VtArray<ScalarType> &authored,
736  const VtIntArray &indices,
737  VtArray<ScalarType> *value,
738  std::string *errString);
739 
740  // Helper function to evaluate the flattened array value of a primvar given
741  // the attribute value and the indices array.
742  template <typename ArrayType>
743  static bool _ComputeFlattenedArray(const VtValue &attrVal,
744  const VtIntArray &indices,
745  VtValue *value,
746  std::string *errString);
747 
748  // Should only be called if _idTargetRelName is set
749  UsdRelationship _GetIdTargetRel(bool create) const;
750  TfToken _idTargetRelName;
751 };
752 
753 // We instantiate the following so we can check and provide the correct value
754 // for Id attributes.
755 template <>
756 USDGEOM_API bool UsdGeomPrimvar::Get(std::string* value, UsdTimeCode time) const;
757 
758 template <>
759 USDGEOM_API bool UsdGeomPrimvar::Get(VtStringArray* value, UsdTimeCode time) const;
760 
761 template <>
762 USDGEOM_API bool UsdGeomPrimvar::Get(VtValue* value, UsdTimeCode time) const;
763 
764 template <typename ScalarType>
765 bool
767 {
768  VtArray<ScalarType> authored;
769  if (!Get(&authored, time))
770  return false;
771 
772  if (!IsIndexed()) {
773  *value = authored;
774  return true;
775  }
776 
777  VtIntArray indices;
778  if (!GetIndices(&indices, time)) {
779  TF_WARN("No indices authored for indexed primvar <%s>.",
780  _attr.GetPath().GetText());
781  return false;
782  }
783 
784  // If the authored array is empty, there's nothing to do.
785  if (authored.empty())
786  return false;
787 
788  std::string errString;
789  bool res = _ComputeFlattenedHelper(authored, indices, value, &errString);
790  if (!errString.empty()) {
791  TF_WARN("For primvar %s: %s",
792  UsdDescribe(_attr).c_str(), errString.c_str());
793  }
794  return res;
795 }
796 
797 template<typename ScalarType>
798 bool
799 UsdGeomPrimvar::_ComputeFlattenedHelper(const VtArray<ScalarType> &authored,
800  const VtIntArray &indices,
801  VtArray<ScalarType> *value,
802  std::string *errString)
803 {
804  value->resize(indices.size());
805  bool success = true;
806 
807  std::vector<size_t> invalidIndexPositions;
808  for (size_t i=0; i < indices.size(); i++) {
809  int index = indices[i];
810  if (index >= 0 && (size_t)index < authored.size()) {
811  (*value)[i] = authored[index];
812  } else {
813  invalidIndexPositions.push_back(i);
814  success = false;
815  }
816  }
817 
818  if (!invalidIndexPositions.empty()) {
819  std::vector<std::string> invalidPositionsStrVec;
820  // Print a maximum of 5 invalid index positions.
821  size_t numElementsToPrint = std::min(invalidIndexPositions.size(),
822  size_t(5));
823  invalidPositionsStrVec.reserve(numElementsToPrint);
824  for (size_t i = 0; i < numElementsToPrint ; ++i) {
825  invalidPositionsStrVec.push_back(
826  TfStringify(invalidIndexPositions[i]));
827  }
828 
829  if (errString) {
830  *errString = TfStringPrintf(
831  "Found %ld invalid indices at positions [%s%s] that are out of "
832  "range [0,%ld).", invalidIndexPositions.size(),
833  TfStringJoin(invalidPositionsStrVec, ", ").c_str(),
834  invalidIndexPositions.size() > 5 ? ", ..." : "",
835  authored.size());
836  }
837  }
838 
839  return success;
840 }
841 
842 
843 PXR_NAMESPACE_CLOSE_SCOPE
844 
845 #endif // USD_PRIMVAR_H
USDGEOM_API bool SetElementSize(int eltSize)
Set the elementSize for this Primvar.
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:466
TF_API std::string TfStringPrintf(const char *fmt,...)
Returns a string formed by a printf()-like specification.
friend bool operator!=(const UsdGeomPrimvar &lhs, const UsdGeomPrimvar &rhs)
Inequality comparison.
Definition: primvar.h:664
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...
#define TF_WARN(...)
Issue a warning, but continue execution.
Definition: diagnostic.h:153
USDGEOM_API int GetUnauthoredValuesIndex() const
Returns the index that represents unauthored values in the indices array.
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:193
static USDGEOM_API bool IsPrimvar(const UsdAttribute &attr)
Test whether a given UsdAttribute represents valid Primvar, which implies that creating a UsdGeomPrim...
USD_API TfToken GetNamespace() const
Return this property&#39;s complete namespace prefix.
Represents a value type name, i.e.
Definition: valueTypeName.h:83
USDGEOM_API bool HasAuthoredElementSize() const
Has elementSize been explicitly authored on this Primvar?
Scenegraph object for authoring and retrieving numeric, string, and array valued data, sampled over time.
Definition: attribute.h:176
bool HasValue() const
Return true if the underlying attribute has a value, either from authored scene description or a fall...
Definition: primvar.h:387
void resize(size_t newSize)
Resize this array.
Definition: array.h:481
USDGEOM_API bool GetTimeSamplesInInterval(const GfInterval &interval, std::vector< double > *times) const
Populates a vector with authored sample times in interval.
USD_API std::string UsdDescribe(const UsdObject &)
Return a human-readable description.
USDGEOM_API UsdAttribute GetIndicesAttr() const
Returns a valid indices attribute if the primvar is indexed.
UsdGeomPrimvarsAPI encodes geometric &quot;primitive variables&quot;, as UsdGeomPrimvar, which interpolate ac...
Definition: primvarsAPI.h:82
UsdAttribute const & GetAttr() const
Explicit UsdAttribute extractor.
Definition: primvar.h:378
USDGEOM_API bool SetInterpolation(const TfToken &interpolation)
Set the Primvar&#39;s interpolation.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
bool Set(const T &value, UsdTimeCode time=UsdTimeCode::Default()) const
Set the attribute value of the Primvar at time.
Definition: primvar.h:444
USD_API std::vector< std::string > SplitName() const
Return this property&#39;s name elements including namespaces and its base name as the final element...
USD_API bool HasAuthoredValue() const
Return true if this attribute has either an authored default value or authored time samples...
Represent a time value, which may be either numeric, holding a double value, or a sentinel value UsdT...
Definition: timeCode.h:85
bool HasAuthoredValue() const
Return true if the underlying attribute has an unblocked, authored value.
Definition: primvar.h:391
SdfValueTypeName GetTypeName() const
Definition: primvar.h:431
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a &quot;Prim&quot; as ...
Definition: prim.h:131
TfToken GetNamespace() const
Definition: primvar.h:425
USDGEOM_API bool ValueMightBeTimeVarying() const
Return true if it is possible, but not certain, that this primvar&#39;s value changes over time...
USD_API bool HasValue() const
Return true if this attribute has an authored default value, authored time samples or a fallback valu...
A basic mathematical interval class.
Definition: interval.h:50
std::vector< std::string > SplitName() const
Definition: primvar.h:428
size_t size() const
Return the total number of elements in this array.
Definition: array.h:428
Represents an arbitrary dimensional rectangular container class.
Definition: array.h:193
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287
std::string TfStringJoin(ForwardIterator begin, ForwardIterator end, const char *separator=" ")
Concatenates the strings (begin, end), with default separator.
Definition: stringUtils.h:364
bool Get(T *value, UsdTimeCode time=UsdTimeCode::Default()) const
Get the attribute value of the Primvar at time .
Definition: primvar.h:438
USDGEOM_API TfToken GetInterpolation() const
Return the Primvar&#39;s interpolation, which is UsdGeomTokens-&gt;constant if unauthored.
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:435
A UsdRelationship creates dependencies between scenegraph objects by allowing a prim to target other ...
Definition: relationship.h:128
USDGEOM_API bool HasAuthoredInterpolation() const
Has interpolation 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.
USDGEOM_API UsdAttribute CreateIndicesAttr() const
Returns the existing indices attribute if the primvar is indexed or creates a new one...
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 IsIdTarget() const
Returns true if the primvar is an Id primvar.
bool ComputeFlattened(VtArray< ScalarType > *value, UsdTimeCode time=UsdTimeCode::Default()) const
Computes the flattened value of the primvar at time.
Definition: primvar.h:766
USDGEOM_API bool SetIndices(const VtIntArray &indices, UsdTimeCode time=UsdTimeCode::Default()) const
Sets the indices value of the indexed primvar at time.
static constexpr UsdTimeCode Default()
Produce a UsdTimeCode representing the sentinel value for &#39;default&#39;.
Definition: timeCode.h:113
USDGEOM_API bool NameContainsNamespaces() const
Does this primvar contain any namespaces other than the &quot;primvars:&quot; namespace?
TfToken GetBaseName() const
Definition: primvar.h:422
const TfToken & GetName() const
Return the full name of this object, i.e.
Definition: object.h:228
USDGEOM_API bool SetUnauthoredValuesIndex(int unauthoredValuesIndex) const
Set the index that represents unauthored values in the indices array.
USDGEOM_API void BlockIndices() const
Block the indices that were previously set.
bool IsDefined() const
Return true if the underlying UsdAttribute::IsDefined(), and in addition the attribute is identified ...
Definition: primvar.h:383
Schema wrapper for UsdAttribute for authoring and introspecting attributes that are primvars...
Definition: primvar.h:260
USDGEOM_API bool IsIndexed() const
Returns true if the primvar is indexed, i.e., if it has an associated &quot;indices&quot; attribute.
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.
std::enable_if<!std::is_enum< T >::value, std::string >::type TfStringify(const T &v)
Convert an arbitrary type into a string.
Definition: stringUtils.h:523
USDGEOM_API TfToken GetPrimvarName() const
Returns the primvar&#39;s name, devoid of the &quot;primvars:&quot; namespace.
bool empty() const
Return true if this array contains no elements, false otherwise.
Definition: array.h:444
friend bool operator==(const UsdGeomPrimvar &lhs, const UsdGeomPrimvar &rhs)
Equality comparison.
Definition: primvar.h:658
Base class for all prims that may require rendering or visualization of some sort.
Definition: imageable.h:74
USD_API SdfValueTypeName GetTypeName() const
Return the &quot;scene description&quot; value type name for this attribute.
USDGEOM_API int GetElementSize() const
Return the &quot;element size&quot; for this Primvar, which is 1 if unauthored.
friend bool operator<(const UsdGeomPrimvar &lhs, const UsdGeomPrimvar &rhs)
Less-than operator.
Definition: primvar.h:671
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:182
static USDGEOM_API bool IsValidInterpolation(const TfToken &interpolation)
Validate that the provided interpolation is a valid setting for interpolation as defined by Interpola...
USD_API TfToken GetBaseName() const
Return this property&#39;s name with all namespace prefixes removed, i.e.
SDF_API const char * GetText() const
Returns the string representation of this path as a c string.
TfToken const & GetName() const
Definition: primvar.h:401