All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
stencilTables.h
Go to the documentation of this file.
1 //
2 // Copyright 2013 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 
25 #ifndef FAR_STENCILTABLES_H
26 #define FAR_STENCILTABLES_H
27 
28 #include "../version.h"
29 
30 #include <cassert>
31 #include <vector>
32 
33 namespace OpenSubdiv {
34 namespace OPENSUBDIV_VERSION {
35 
36 namespace Far {
37 
42 class Stencil {
43 
44 public:
45 
47  Stencil() {}
48 
57  Stencil(unsigned char * size,
58  int * indices,
59  float * weights)
60  : _size(size),
61  _indices(indices),
62  _weights(weights) {
63  }
64 
66  Stencil(Stencil const & other) {
67  _size = other._size;
68  _indices = other._indices;
69  _weights = other._weights;
70  }
71 
72  int GetSize() const {
73  return *_size;
74  }
75 
77  int const * GetVertexIndices() const {
78  return _indices;
79  }
80 
82  float const * GetWeights() const {
83  return _weights;
84  }
85 
87  void Next() {
88  int stride = *_size;
89  ++_size;
90  _indices += stride;
91  _weights += stride;
92  }
93 
94 protected:
95  friend class StencilTablesFactory;
96 
97  unsigned char * _size;
98  int * _indices;
99  float * _weights;
100 };
101 
115 
116 public:
117 
119  int GetNumStencils() const {
120  return (int)_sizes.size();
121  }
122 
123  int GetNumControlVertices() const {
124  return _numControlVertices;
125  }
126 
128  Stencil GetStencil(int i) const;
129 
131  std::vector<unsigned char> const & GetSizes() const {
132  return _sizes;
133  }
134 
136  std::vector<int> const & GetOffsets() const {
137  return _offsets;
138  }
139 
141  std::vector<int> const & GetControlIndices() const {
142  return _indices;
143  }
144 
146  std::vector<float> const & GetWeights() const {
147  return _weights;
148  }
149 
164  template <class T>
165  void UpdateValues(T const *controlValues, T *values, int start=-1, int end=-1) const {
166 
167  _Update(controlValues, values, _weights, start, end);
168  }
169 
170 private:
171 
172  // Update values by appling cached stencil weights to new control values
173  template <class T> void _Update( T const *controlValues, T *values,
174  std::vector<float> const & valueWeights, int start, int end) const;
175 
176 private:
177 
178  friend class StencilTablesFactory;
179 
180  int _numControlVertices; // number of control vertices
181 
182  std::vector<unsigned char> _sizes; // number of coeffiecient for each stencil
183  std::vector<int> _offsets, // offset to the start of each stencil
184  _indices; // indices of contributing coarse vertices
185  std::vector<float> _weights; // stencil weight coefficients
186 };
187 
188 
191 class LimitStencil : public Stencil {
192 
193 public:
194 
207  LimitStencil( unsigned char * size,
208  int * indices,
209  float * weights,
210  float * duWeights,
211  float * dvWeights )
212  : Stencil(size, indices, weights),
213  _duWeights(duWeights),
214  _dvWeights(dvWeights) {
215  }
216 
218  float const * GetDuWeights() const {
219  return _duWeights;
220  }
221 
223  float const * GetDvWeights() const {
224  return _dvWeights;
225  }
226 
228  void Next() {
229  int stride = *_size;
230  ++_size;
231  _indices += stride;
232  _weights += stride;
233  _duWeights += stride;
234  _dvWeights += stride;
235  }
236 
237 private:
238  float * _duWeights, // pointer to stencil u derivative limit weights
239  * _dvWeights; // pointer to stencil v derivative limit weights
240 };
241 
246 
247 public:
248 
250  std::vector<float> const & GetDuWeights() const {
251  return _duWeights;
252  }
253 
255  std::vector<float> const & GetDvWeights() const {
256  return _dvWeights;
257  }
258 
276  template <class T>
277  void UpdateDerivs(T const *controlValues, T *uderivs, T *vderivs,
278  int start=-1, int end=-1) const {
279 
280  _Update(controlValues, uderivs, _duWeights, start, end);
281  _Update(controlValues, vderivs, _dvWeights, start, end);
282  }
283 
284 
285 private:
286  std::vector<float> _duWeights, // u derivative limit stencil weights
287  _dvWeights; // v derivative limit stencil weights
288 };
289 
290 
291 // Update values by appling cached stencil weights to new control values
292 template <class T> void
293 StencilTables::_Update(T const *controlValues, T *values,
294  std::vector<float> const &valueWeights, int start, int end) const {
295 
296  int const * indices = &_indices.at(0);
297  float const * weights = &valueWeights.at(0);
298 
299  if (start>0) {
300  assert(start<(int)_offsets.size());
301  indices += _offsets[start];
302  weights += _offsets[start];
303  values += start;
304  }
305 
306  if (end<start or end<0) {
307  end = GetNumStencils();
308  }
309 
310  int nstencils = end - std::max(0, start);
311  for (int i=0; i<nstencils; ++i) {
312 
313  // Zero out the result accumulators
314  values[i].Clear();
315 
316  // For each element in the array, add the coefs contribution
317  for (int j=0; j<_sizes[i]; ++j, ++indices, ++weights) {
318  values[i].AddWithWeight( controlValues[*indices], *weights );
319  }
320  }
321 }
322 
323 // Returns a Stencil at index i in the table
324 inline Stencil
326 
327  int ofs = _offsets[i];
328 
329  return Stencil( const_cast<unsigned char *>(&_sizes[i]),
330  const_cast<int *>(&_indices[ofs]),
331  const_cast<float *>(&_weights[ofs]) );
332 }
333 
334 } // end namespace Far
335 
336 } // end namespace OPENSUBDIV_VERSION
337 using namespace OPENSUBDIV_VERSION;
338 
339 } // end namespace OpenSubdiv
340 
341 #endif // FAR_STENCILTABLES_H
342 
std::vector< unsigned char > const & GetSizes() const
Returns the number of control vertices of each stencil in the table.
LimitStencil(unsigned char *size, int *indices, float *weights, float *duWeights, float *dvWeights)
Constructor.
std::vector< float > const & GetWeights() const
Returns the stencil interpolation weights.
int const * GetVertexIndices() const
Returns the control vertices indices.
Definition: stencilTables.h:77
std::vector< int > const & GetOffsets() const
Returns the offset to a given stencil (factory may leave empty)
void Next()
Advance to the next stencil in the table.
Definition: stencilTables.h:87
std::vector< int > const & GetControlIndices() const
Returns the indices of the control vertices.
std::vector< float > const & GetDvWeights() const
Returns the &#39;v&#39; derivative stencil interpolation weights.
void UpdateValues(T const *controlValues, T *values, int start=-1, int end=-1) const
Updates point values based on the control values.
int GetNumStencils() const
Returns the number of stencils in the table.
float const * GetWeights() const
Returns the interpolation weights.
Definition: stencilTables.h:82
Stencil GetStencil(int i) const
Returns a Stencil at index i in the tables.
void Next()
Advance to the next stencil in the table.
void UpdateDerivs(T const *controlValues, T *uderivs, T *vderivs, int start=-1, int end=-1) const
Updates derivative values based on the control values.
Stencil(Stencil const &other)
Copy constructor.
Definition: stencilTables.h:66
std::vector< float > const & GetDuWeights() const
Returns the &#39;u&#39; derivative stencil interpolation weights.
Stencil(unsigned char *size, int *indices, float *weights)
Constructor.
Definition: stencilTables.h:57