All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sparseValueWriter.h
Go to the documentation of this file.
1 //
2 // Copyright 2018 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 USDUTILS_SPARSE_VALUE_WRITER_H
25 #define USDUTILS_SPARSE_VALUE_WRITER_H
26 
31 
32 #include "pxr/pxr.h"
33 
34 #include "pxr/base/vt/value.h"
35 
36 #include "pxr/usd/usdUtils/api.h"
37 #include "pxr/usd/usd/attribute.h"
38 #include "pxr/usd/usd/timeCode.h"
39 
40 PXR_NAMESPACE_OPEN_SCOPE
41 
89 public:
99  USDUTILS_API
101  const VtValue &defaultValue=VtValue());
102 
114  USDUTILS_API
115  UsdUtilsSparseAttrValueWriter(const UsdAttribute &attr, VtValue *defaultValue);
116 
126  USDUTILS_API
127  bool SetTimeSample(const VtValue &value, const UsdTimeCode time);
128 
135  USDUTILS_API
136  bool SetTimeSample(VtValue *value, const UsdTimeCode time);
137 
139  const UsdAttribute & GetAttr() const {
140  return _attr;
141  }
142 
143 private:
144  // Helper method to initialize the sparse authoring scheme.
145  void _InitializeSparseAuthoring(VtValue *defaultValue);
146 
147  // The attribute whose time-samples will be authored via public API.
148  const UsdAttribute _attr;
149 
150  // The time at which previous time-sample was authored.
151  UsdTimeCode _prevTime = UsdTimeCode::Default();
152 
153  // The value at previously authored time-sample.
154  VtValue _prevValue;
155 
156  // Whether a time-sample was written at _prevTime (with value=_prevValue).
157  bool _didWritePrevValue=true;
158 };
159 
230 public:
236  USDUTILS_API
237  bool SetAttribute(const UsdAttribute &attr,
238  const VtValue &value,
239  const UsdTimeCode time=UsdTimeCode::Default());
240 
246  USDUTILS_API
247  bool SetAttribute(const UsdAttribute &attr,
248  VtValue *value,
249  const UsdTimeCode time=UsdTimeCode::Default());
250 
252  template<typename T>
253  bool SetAttribute(const UsdAttribute &attr,
254  T &value,
255  const UsdTimeCode time=UsdTimeCode::Default())
256  {
257  VtValue val = VtValue::Take(value);
258  return SetAttribute(attr, &val, time);
259  }
260 
263  USDUTILS_API
264  void Clear() {
265  _attrValueWriterMap.clear();
266  }
267 
268 private:
269  // Templated helper method used by the two public SetAttribute() methods.
270  template <typename T>
271  bool _SetAttributeImpl(const UsdAttribute &attr,
272  T &value,
273  const UsdTimeCode time);
274 
275  struct _AttrHash {
276  inline size_t operator() (const UsdAttribute &attr) const {
277  return hash_value(attr);
278  }
279  };
280 
281  using _AttrValueWriterMap = std::unordered_map<UsdAttribute,
283  _AttrHash>;
284  _AttrValueWriterMap _attrValueWriterMap;
285 };
286 
287 PXR_NAMESPACE_CLOSE_SCOPE
288 
289 #endif
A utility class for authoring time-varying attribute values with simple run-length encoding...
bool SetAttribute(const UsdAttribute &attr, T &value, const UsdTimeCode time=UsdTimeCode::Default())
Scenegraph object for authoring and retrieving numeric, string, and array valued data, sampled over time.
Definition: attribute.h:176
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.
Utility class that manages sparse authoring of a set of UsdAttributes.
static VtValue Take(T &obj)
Create a new VtValue, taking its contents from obj.
Definition: value.h:679
Represent a time value, which may be either numeric, holding a double value, or a sentinel value UsdT...
Definition: timeCode.h:85
const UsdAttribute & GetAttr() const
Returns the attribute that&#39;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 void Clear()
Clears the internal map, thereby releasing all the memory used by the sparse value-writers.
static constexpr UsdTimeCode Default()
Produce a UsdTimeCode representing the sentinel value for &#39;default&#39;.
Definition: timeCode.h:113
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:182
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.