All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
sparseValueWriter.h
Go to the documentation of this file.
1//
2// Copyright 2018 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_UTILS_SPARSE_VALUE_WRITER_H
8#define PXR_USD_USD_UTILS_SPARSE_VALUE_WRITER_H
9
14
15#include "pxr/pxr.h"
16
17#include "pxr/base/vt/value.h"
18
19#include "pxr/usd/usdUtils/api.h"
20#include "pxr/usd/usd/attribute.h"
21#include "pxr/usd/usd/timeCode.h"
22
23PXR_NAMESPACE_OPEN_SCOPE
24
72public:
82 USDUTILS_API
84 const VtValue &defaultValue=VtValue());
85
97 USDUTILS_API
99
109 USDUTILS_API
110 bool SetTimeSample(const VtValue &value, const UsdTimeCode time);
111
118 USDUTILS_API
119 bool SetTimeSample(VtValue *value, const UsdTimeCode time);
120
122 const UsdAttribute & GetAttr() const {
123 return _attr;
124 }
125
126private:
127 // Helper method to initialize the sparse authoring scheme.
128 void _InitializeSparseAuthoring(VtValue *defaultValue);
129
130 // The attribute whose time-samples will be authored via public API.
131 const UsdAttribute _attr;
132
133 // The time at which previous time-sample was authored.
134 UsdTimeCode _prevTime = UsdTimeCode::Default();
135
136 // The value at previously authored time-sample.
137 VtValue _prevValue;
138
139 // Whether a time-sample was written at _prevTime (with value=_prevValue).
140 bool _didWritePrevValue=true;
141};
142
213public:
219 USDUTILS_API
220 bool SetAttribute(const UsdAttribute &attr,
221 const VtValue &value,
222 const UsdTimeCode time=UsdTimeCode::Default());
223
229 USDUTILS_API
230 bool SetAttribute(const UsdAttribute &attr,
231 VtValue *value,
232 const UsdTimeCode time=UsdTimeCode::Default());
233
235 template<typename T>
236 bool SetAttribute(const UsdAttribute &attr,
237 T &value,
239 {
240 VtValue val = VtValue::Take(value);
241 return SetAttribute(attr, &val, time);
242 }
243
246 USDUTILS_API
247 void Clear() {
248 _attrValueWriterMap.clear();
249 }
250
253 USDUTILS_API
254 std::vector<UsdUtilsSparseAttrValueWriter>
256
257private:
258 // Templated helper method used by the two public SetAttribute() methods.
259 template <typename T>
260 bool _SetAttributeImpl(const UsdAttribute &attr,
261 T &value,
262 const UsdTimeCode time);
263
264 struct _AttrHash {
265 inline size_t operator() (const UsdAttribute &attr) const {
266 return hash_value(attr);
267 }
268 };
269
270 using _AttrValueWriterMap = std::unordered_map<UsdAttribute,
272 _AttrHash>;
273 _AttrValueWriterMap _attrValueWriterMap;
274};
275
276PXR_NAMESPACE_CLOSE_SCOPE
277
278#endif
Scenegraph object for authoring and retrieving numeric, string, and array valued data,...
Definition: attribute.h:160
Represent a time value, which may be either numeric, holding a double value, or a sentinel value UsdT...
Definition: timeCode.h:67
static constexpr UsdTimeCode Default()
Produce a UsdTimeCode representing the sentinel value for 'default'.
Definition: timeCode.h:95
A utility class for authoring time-varying attribute values with simple run-length encoding,...
USDUTILS_API UsdUtilsSparseAttrValueWriter(const UsdAttribute &attr, VtValue *defaultValue)
The constructor initializes the data required for run-length encoding of time-samples.
USDUTILS_API bool SetTimeSample(const VtValue &value, const UsdTimeCode time)
Sets a new time-sample on the attribute with given value at the given time.
const UsdAttribute & GetAttr() const
Returns the attribute that's held in the sparse value writer.
USDUTILS_API UsdUtilsSparseAttrValueWriter(const UsdAttribute &attr, const VtValue &defaultValue=VtValue())
The constructor initializes the data required for run-length encoding of time-samples.
USDUTILS_API bool SetTimeSample(VtValue *value, const UsdTimeCode time)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Utility class that manages sparse authoring of a set of UsdAttributes.
USDUTILS_API bool SetAttribute(const UsdAttribute &attr, VtValue *value, const UsdTimeCode time=UsdTimeCode::Default())
This is an overloaded member function, provided for convenience. It differs from the above function o...
USDUTILS_API bool SetAttribute(const UsdAttribute &attr, const VtValue &value, const UsdTimeCode time=UsdTimeCode::Default())
Sets the value of attr to value at time time.
USDUTILS_API void Clear()
Clears the internal map, thereby releasing all the memory used by the sparse value-writers.
bool SetAttribute(const UsdAttribute &attr, T &value, const UsdTimeCode time=UsdTimeCode::Default())
This is an overloaded member function, provided for convenience. It differs from the above function o...
USDUTILS_API std::vector< UsdUtilsSparseAttrValueWriter > GetSparseAttrValueWriters() const
Returns a new vector of UsdUtilsSparseAttrValueWriter populated from the attrValueWriter map.
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:147
static VtValue Take(T &obj)
Create a new VtValue, taking its contents from obj.
Definition: value.h:886
size_t hash_value(const TfToken &x)
Overload hash_value for TfToken.
Definition: token.h:437