All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
timeSampleArray.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 HD_TIME_SAMPLE_ARRAY_H
25 #define HD_TIME_SAMPLE_ARRAY_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hd/api.h"
29 #include "pxr/imaging/hd/version.h"
30 #include "pxr/base/vt/value.h"
31 #include "pxr/base/gf/math.h"
32 #include "pxr/base/gf/quatf.h"
33 #include "pxr/base/tf/diagnostic.h"
34 #include "pxr/base/tf/smallVector.h"
35 
36 PXR_NAMESPACE_OPEN_SCOPE
37 
39 template <typename T>
40 inline T HdResampleNeighbors(float alpha, const T& v0, const T& v1)
41 {
42  return GfLerp(alpha, v0, v1);
43 }
44 
46 inline GfQuatf HdResampleNeighbors(float alpha,
47  const GfQuatf &v0,
48  const GfQuatf &v1)
49 {
50  return GfSlerp(double(alpha), v0, v1);
51 }
52 
54 template <typename T>
55 inline VtArray<T> HdResampleNeighbors(float alpha,
56  const VtArray<T>& v0,
57  const VtArray<T>& v1)
58 {
59  VtArray<T> r(v0.size());
60  for (size_t i=0; i < r.size(); ++i) {
61  r[i] = HdResampleNeighbors(alpha, v0[i], v1[i]);
62  }
63  return r;
64 }
65 
70 template <typename T>
71 T HdResampleRawTimeSamples(
72  float u,
73  size_t numSamples,
74  const float *us,
75  const T *vs)
76 {
77  if (numSamples == 0) {
78  TF_CODING_ERROR("HdResample: Zero samples provided");
79  return T();
80  }
81 
82  size_t i=0;
83  for (; i < numSamples; ++i) {
84  if (us[i] == u) {
85  // Fast path for exact parameter match.
86  return vs[i];
87  }
88  if (us[i] > u) {
89  break;
90  }
91  }
92  if (i == 0) {
93  // u is before the first sample.
94  return vs[0];
95  } else if (i == numSamples) {
96  // u is after the last sample.
97  return vs[numSamples-1];
98  } else if (us[i] == us[i-1]) {
99  // Neighboring samples have identical parameter.
100  // Arbitrarily choose a sample.
101  TF_WARN("HdResampleRawTimeSamples: overlapping samples at %f; "
102  "using first sample", us[i]);
103  return vs[i-1];
104  } else {
105  // Linear blend of neighboring samples.
106  float alpha = (us[i]-u) / (us[i]-us[i-1]);
107  return HdResampleNeighbors(alpha, vs[i-1], vs[i]);
108  }
109 }
110 
116 template<typename TYPE, unsigned int CAPACITY>
118 {
120  times.resize(CAPACITY);
121  values.resize(CAPACITY);
122  count = 0;
123  }
124 
126  times = rhs.times;
127  values = rhs.values;
128  count = rhs.count;
129  }
130 
131  HdTimeSampleArray& operator=(const HdTimeSampleArray& rhs) {
132  times = rhs.times;
133  values = rhs.values;
134  count = rhs.count;
135  return *this;
136  }
137 
139  void Resize(unsigned int newSize) {
140  times.resize(newSize);
141  values.resize(newSize);
142  count = newSize;
143  }
144 
147  TYPE Resample(float u) const {
148  return HdResampleRawTimeSamples(u, count, times.data(), values.data());
149  }
150 
159  Resize(box.count);
160  times = box.times;
161  for (size_t i=0; i < box.count; ++i) {
162  if (box.values[i].GetArraySize() > 0) {
163  values[i] = box.values[i].template Get<TYPE>();
164  } else {
165  values[i] = TYPE();
166  }
167  }
168  }
169 
170  size_t count;
173 };
174 
175 PXR_NAMESPACE_CLOSE_SCOPE
176 
177 #endif // HD_TIME_SAMPLE_ARRAY_H
value_type * data()
Direct access to the underlying array.
Definition: smallVector.h:746
#define TF_WARN(...)
Issue a warning, but continue execution.
Definition: diagnostic.h:153
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
Definition: diagnostic.h:87
TYPE Resample(float u) const
Convience method for invoking HdResampleRawTimeSamples on this HdTimeSampleArray. ...
Basic type: a quaternion, a complex number with a real coefficient and three imaginary coefficients...
Definition: quatf.h:60
GF_API GfQuatd GfSlerp(double alpha, const GfQuatd &q0, const GfQuatd &q1)
Spherically linearly interpolate between q0 and q1.
void resize(size_type newSize, const value_type &v=value_type())
Resize the vector to newSize and insert copies of .
Definition: smallVector.h:444
void Resize(unsigned int newSize)
Resize the internal buffers.
T GfLerp(double alpha, const T &a, const T &b)
Linear interpolation function.
Definition: math.h:190
size_t size() const
Return the total number of elements in this array.
Definition: array.h:428
void UnboxFrom(HdTimeSampleArray< VtValue, CAPACITY > const &box)
Unbox an HdTimeSampleArray holding boxed VtValue&lt;VtArray&lt;T&gt;&gt; samples into an aray holding VtArray&lt;T&gt; ...
An array of a value sampled over time, in struct-of-arrays layout.