HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
timeSampleArray.h
Go to the documentation of this file.
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 PXR_IMAGING_HD_TIME_SAMPLE_ARRAY_H
25 #define PXR_IMAGING_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"
35 
37 
38 /// Resample two neighboring samples.
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 
45 /// Specialization for HdQuatf: spherical linear interpolation.
47  const GfQuatf &v0,
48  const GfQuatf &v1)
49 {
50  return GfSlerp(double(alpha), v0, v1);
51 }
52 
53 /// Specialization for VtArray: component-wise resampling.
54 template <typename T>
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 
66 /// Resample a function described by an ordered array of samples,
67 /// using a linear reconstruction filter evaluated at the given
68 /// parametric position u. The function is considered constant
69 /// outside the supplied sample range.
70 template <typename T>
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 
111 /// An array of a value sampled over time, in struct-of-arrays layout.
112 /// This is provided as a convenience for time-sampling attributes.
113 /// This type has static capacity but dynamic size, providing
114 /// a limited ability to handle variable sampling without requiring
115 /// heap allocation.
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 
132  times = rhs.times;
133  values = rhs.values;
134  count = rhs.count;
135  return *this;
136  }
137 
138  /// Resize the internal buffers.
139  void Resize(unsigned int newSize) {
140  times.resize(newSize);
141  values.resize(newSize);
142  count = newSize;
143  }
144 
145  /// Convience method for invoking HdResampleRawTimeSamples
146  /// on this HdTimeSampleArray.
147  TYPE Resample(float u) const {
148  return HdResampleRawTimeSamples(u, count, times.data(), values.data());
149  }
150 
151  /// Unbox an HdTimeSampleArray holding boxed VtValue<VtArray<T>>
152  /// samples into an aray holding VtArray<T> samples.
153  ///
154  /// Similar to VtValue::Get(), this will issue a coding error if the
155  /// VtValue is not holding the expected type.
156  ///
157  /// \see VtValue::Get()
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 
176 
177 #endif // PXR_IMAGING_HD_TIME_SAMPLE_ARRAY_H
void resize(size_type newSize, const value_type &v=value_type())
Definition: smallVector.h:454
HdTimeSampleArray(const HdTimeSampleArray &rhs)
GLclampf GLclampf GLclampf alpha
Definition: glew.h:1520
TfSmallVector< float, CAPACITY > times
void UnboxFrom(HdTimeSampleArray< VtValue, CAPACITY > const &box)
PXR_NAMESPACE_OPEN_SCOPE T HdResampleNeighbors(float alpha, const T &v0, const T &v1)
Resample two neighboring samples.
GLint GLsizei const GLuint64 * values
Definition: glew.h:3612
#define TF_CODING_ERROR
TYPE Resample(float u) const
T GfLerp(double alpha, const T &a, const T &b)
Definition: math.h:193
Definition: quatf.h:60
GF_API GfQuatd GfSlerp(double alpha, const GfQuatd &q0, const GfQuatd &q1)
void Resize(unsigned int newSize)
Resize the internal buffers.
#define TF_WARN
Definition: types.h:166
TfSmallVector< TYPE, CAPACITY > values
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1346
GLdouble GLdouble GLdouble r
Definition: glew.h:1406
GLuint GLuint GLsizei count
Definition: glew.h:1253
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
GLfloat v0
Definition: glew.h:1848
T HdResampleRawTimeSamples(float u, size_t numSamples, const float *us, const T *vs)
GLfloat GLfloat v1
Definition: glew.h:1852
HdTimeSampleArray & operator=(const HdTimeSampleArray &rhs)
value_type * data()
Definition: smallVector.h:768