dataSourceAttribute.h
1 //
2 // Copyright 2020 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_IMAGING_USD_IMAGING_DATA_SOURCE_ATTRIBUTE_H
25 #define PXR_USD_IMAGING_USD_IMAGING_DATA_SOURCE_ATTRIBUTE_H
26 
27 #include "pxr/usd/usd/attribute.h"
28 #include "pxr/usd/usd/attributeQuery.h"
29 #include "pxr/usdImaging/usdImaging/api.h"
30 #include "pxr/usdImaging/usdImaging/dataSourceStageGlobals.h"
31 #include "pxr/imaging/hd/dataSource.h"
32 #include "pxr/imaging/hd/dataSourceTypeDefs.h"
33 
34 PXR_NAMESPACE_OPEN_SCOPE
35 
40 template <typename T>
41 class UsdImagingDataSourceAttribute : public HdTypedSampledDataSource<T>
42 {
43 public:
44 
45  HD_DECLARE_DATASOURCE(UsdImagingDataSourceAttribute<T>);
46 
49  VtValue GetValue(HdSampledDataSource::Time shutterOffset) override
50  {
51  return VtValue(GetTypedValue(shutterOffset));
52  }
53 
56  T GetTypedValue(HdSampledDataSource::Time shutterOffset) override
57  {
58  // Zero-initialization for numerical types.
59  T result{};
60  UsdTimeCode time = _stageGlobals.GetTime();
61  if (time.IsNumeric()) {
62  time = UsdTimeCode(time.GetValue() + shutterOffset);
63  }
64  _usdAttrQuery.Get<T>(&result, time);
65  return result;
66  }
67 
72  HdSampledDataSource::Time startTime,
73  HdSampledDataSource::Time endTime,
74  std::vector<HdSampledDataSource::Time> *outSampleTimes) override
75  {
76  UsdTimeCode time = _stageGlobals.GetTime();
77  if (!_usdAttrQuery.ValueMightBeTimeVarying() ||
78  !time.IsNumeric()) {
79  return false;
80  }
81 
82  GfInterval interval(
83  time.GetValue() + startTime,
84  time.GetValue() + endTime);
85  std::vector<double> timeSamples;
86  _usdAttrQuery.GetTimeSamplesInInterval(interval, &timeSamples);
87 
88  // Add boundary timesamples, if necessary.
89  if (timeSamples.empty() || timeSamples[0] > interval.GetMin()) {
90  timeSamples.insert(timeSamples.begin(), interval.GetMin());
91  }
92  if (timeSamples.back() < interval.GetMax()) {
93  timeSamples.push_back(interval.GetMax());
94  }
95 
96  // We need to convert the time array because usd uses double and
97  // hydra (and prman) use float :/.
98  outSampleTimes->resize(timeSamples.size());
99  for (size_t i = 0; i < timeSamples.size(); ++i) {
100  (*outSampleTimes)[i] = timeSamples[i] - time.GetValue();
101  }
102 
103  return true;
104  }
105 
106 private:
107 
122  UsdImagingDataSourceAttribute(
123  const UsdAttribute &usdAttr,
124  const UsdImagingDataSourceStageGlobals &stageGlobals,
125  const SdfPath &sceneIndexPath = SdfPath::EmptyPath(),
126  const HdDataSourceLocator &timeVaryingFlagLocator =
128 
130  UsdImagingDataSourceAttribute(
131  const UsdAttributeQuery &usdAttrQuery,
132  const UsdImagingDataSourceStageGlobals &stageGlobals,
133  const SdfPath &sceneIndexPath = SdfPath::EmptyPath(),
134  const HdDataSourceLocator &timeVaryingFlagLocator =
136 
137 private:
138  UsdAttributeQuery _usdAttrQuery;
139  const UsdImagingDataSourceStageGlobals &_stageGlobals;
140 };
141 
142 // ----------------------------------------------------------------------------
143 
144 
148 USDIMAGING_API
149 HdSampledDataSourceHandle
150 UsdImagingDataSourceAttributeNew(
151  const UsdAttribute &usdAttr,
152  const UsdImagingDataSourceStageGlobals &stageGlobals,
153  const SdfPath &sceneIndexPath = SdfPath::EmptyPath(),
154  const HdDataSourceLocator &timeVaryingFlagLocator =
156 
158 USDIMAGING_API
159 HdSampledDataSourceHandle
160 UsdImagingDataSourceAttributeNew(
161  const UsdAttributeQuery &usdAttrQuery,
162  const UsdImagingDataSourceStageGlobals &stageGlobals,
163  const SdfPath &sceneIndexPath = SdfPath::EmptyPath(),
164  const HdDataSourceLocator &timeVaryingFlagLocator =
166 
167 // ----------------------------------------------------------------------------
168 
169 template<typename T>
170 UsdImagingDataSourceAttribute<T>::UsdImagingDataSourceAttribute(
171  const UsdAttribute &usdAttr,
172  const UsdImagingDataSourceStageGlobals &stageGlobals,
173  const SdfPath &sceneIndexPath,
174  const HdDataSourceLocator &timeVaryingFlagLocator)
175  : _usdAttrQuery(usdAttr)
176  , _stageGlobals(stageGlobals)
177 {
178  if (!timeVaryingFlagLocator.IsEmpty()) {
179  if (_usdAttrQuery.ValueMightBeTimeVarying()) {
180  _stageGlobals.FlagAsTimeVarying(
181  sceneIndexPath, timeVaryingFlagLocator);
182  }
183  }
184 }
185 
186 template<typename T>
187 UsdImagingDataSourceAttribute<T>::UsdImagingDataSourceAttribute(
188  const UsdAttributeQuery &usdAttrQuery,
189  const UsdImagingDataSourceStageGlobals &stageGlobals,
190  const SdfPath &sceneIndexPath,
191  const HdDataSourceLocator &timeVaryingFlagLocator)
192  : _usdAttrQuery(usdAttrQuery)
193  , _stageGlobals(stageGlobals)
194 {
195  if (!timeVaryingFlagLocator.IsEmpty()) {
196  if (_usdAttrQuery.ValueMightBeTimeVarying()) {
197  _stageGlobals.FlagAsTimeVarying(
198  sceneIndexPath, timeVaryingFlagLocator);
199  }
200  }
201 }
202 
203 PXR_NAMESPACE_CLOSE_SCOPE
204 
205 #endif // PXR_USD_IMAGING_USD_IMAGING_DATA_SOURCE_ATTRIBUTE_H
virtual bool GetContributingSampleTimesForInterval(Time startTime, Time endTime, std::vector< Time > *outSampleTimes)=0
Given a shutter window of interest (startTime and endTime relative to the current frame),...
Object for efficiently making repeated queries for attribute values.
Scenegraph object for authoring and retrieving numeric, string, and array valued data,...
Definition: attribute.h:176
bool IsNumeric() const
Return true if this time represents a numeric value, false otherwise.
Definition: timeCode.h:146
virtual VtValue GetValue(Time shutterOffset)=0
Returns the value of this data source at frame-relative time shutterOffset.
Represents an object that can identify the location of a data source.
Represent a time value, which may be either numeric, holding a double value, or a sentinel value UsdT...
Definition: timeCode.h:85
This class is used as a context object with global stage information, that gets passed down to dataso...
A data source that represents a USD Attribute.
A datasource representing a concretely-typed sampled value.
Definition: dataSource.h:204
A basic mathematical interval class.
Definition: interval.h:49
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:290
virtual T GetTypedValue(Time shutterOffset)=0
Returns the value of this data source at frame-relative time shutterOffset, as type T.
static HD_API const HdDataSourceLocator & EmptyLocator()
Returns a common empty locator.
double GetValue() const
Return the numeric value for this time.
Definition: timeCode.h:152
static SDF_API const SdfPath & EmptyPath()
The empty path value, equivalent to SdfPath().
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:166