stencilTable.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 OPENSUBDIV3_FAR_STENCILTABLE_H
26 #define OPENSUBDIV3_FAR_STENCILTABLE_H
27 
28 #include "../version.h"
29 
30 #include "../far/types.h"
31 
32 #include <cassert>
33 #include <cstring>
34 #include <vector>
35 #include <iostream>
36 
37 namespace OpenSubdiv {
38 namespace OPENSUBDIV_VERSION {
39 
40 namespace Far {
41 
46 class Stencil {
47 
48 public:
49 
51  Stencil() {}
52 
61  Stencil(int * size,
62  Index * indices,
63  float * weights)
64  : _size(size),
65  _indices(indices),
66  _weights(weights) {
67  }
68 
70  Stencil(Stencil const & other) {
71  _size = other._size;
72  _indices = other._indices;
73  _weights = other._weights;
74  }
75 
77  int GetSize() const {
78  return *_size;
79  }
80 
82  int * GetSizePtr() const {
83  return _size;
84  }
85 
87  Index const * GetVertexIndices() const {
88  return _indices;
89  }
90 
92  float const * GetWeights() const {
93  return _weights;
94  }
95 
97  void Next() {
98  int stride = *_size;
99  ++_size;
100  _indices += stride;
101  _weights += stride;
102  }
103 
104 protected:
105  friend class StencilTableFactory;
107 
108  int * _size;
110  float * _weights;
111 };
112 
126  StencilTable(int numControlVerts,
127  std::vector<int> const& offsets,
128  std::vector<int> const& sizes,
129  std::vector<int> const& sources,
130  std::vector<float> const& weights,
131  bool includeCoarseVerts,
132  size_t firstOffset);
133 
134 public:
135 
137  int GetNumStencils() const {
138  return (int)_sizes.size();
139  }
140 
142  int GetNumControlVertices() const {
143  return _numControlVertices;
144  }
145 
147  Stencil GetStencil(Index i) const;
148 
150  std::vector<int> const & GetSizes() const {
151  return _sizes;
152  }
153 
155  std::vector<Index> const & GetOffsets() const {
156  return _offsets;
157  }
158 
160  std::vector<Index> const & GetControlIndices() const {
161  return _indices;
162  }
163 
165  std::vector<float> const & GetWeights() const {
166  return _weights;
167  }
168 
170  Stencil operator[] (Index index) const;
171 
186  template <class T>
187  void UpdateValues(T const *controlValues, T *values, Index start=-1, Index end=-1) const {
188  update(controlValues, values, _weights, start, end);
189  }
190 
192  void Clear();
193 
194 protected:
195 
196  // Update values by applying cached stencil weights to new control values
197  template <class T> void update( T const *controlValues, T *values,
198  std::vector<float> const & valueWeights, Index start, Index end) const;
199 
200  // Populate the offsets table from the stencil sizes in _sizes (factory helper)
201  void generateOffsets();
202 
203  // Resize the table arrays (factory helper)
204  void resize(int nstencils, int nelems);
205 
206 protected:
208  StencilTable(int numControlVerts)
209  : _numControlVertices(numControlVerts)
210  { }
211 
212  friend class StencilTableFactory;
213  // XXX: temporarily, GregoryBasis class will go away.
214  friend class GregoryBasis;
215 
216  int _numControlVertices; // number of control vertices
217 
218  std::vector<int> _sizes; // number of coeffiecient for each stencil
219  std::vector<Index> _offsets, // offset to the start of each stencil
220  _indices; // indices of contributing coarse vertices
221  std::vector<float> _weights; // stencil weight coefficients
222 };
223 
224 
227 class LimitStencil : public Stencil {
228 
229 public:
230 
243  LimitStencil( int* size,
244  Index * indices,
245  float * weights,
246  float * duWeights,
247  float * dvWeights )
248  : Stencil(size, indices, weights),
249  _duWeights(duWeights),
250  _dvWeights(dvWeights) {
251  }
252 
254  float const * GetDuWeights() const {
255  return _duWeights;
256  }
257 
259  float const * GetDvWeights() const {
260  return _dvWeights;
261  }
262 
264  void Next() {
265  int stride = *_size;
266  ++_size;
267  _indices += stride;
268  _weights += stride;
269  _duWeights += stride;
270  _dvWeights += stride;
271  }
272 
273 private:
274 
275  friend class StencilTableFactory;
277 
278  float * _duWeights, // pointer to stencil u derivative limit weights
279  * _dvWeights; // pointer to stencil v derivative limit weights
280 };
281 
286  LimitStencilTable(int numControlVerts,
287  std::vector<int> const& offsets,
288  std::vector<int> const& sizes,
289  std::vector<int> const& sources,
290  std::vector<float> const& weights,
291  std::vector<float> const& duWeights,
292  std::vector<float> const& dvWeights,
293  bool includeCoarseVerts,
294  size_t firstOffset);
295 
296 public:
297 
299  std::vector<float> const & GetDuWeights() const {
300  return _duWeights;
301  }
302 
304  std::vector<float> const & GetDvWeights() const {
305  return _dvWeights;
306  }
307 
325  template <class T>
326  void UpdateDerivs(T const *controlValues, T *uderivs, T *vderivs,
327  int start=-1, int end=-1) const {
328 
329  update(controlValues, uderivs, _duWeights, start, end);
330  update(controlValues, vderivs, _dvWeights, start, end);
331  }
332 
334  void Clear();
335 
336 private:
338 
339  // Resize the table arrays (factory helper)
340  void resize(int nstencils, int nelems);
341 
342 private:
343  std::vector<float> _duWeights, // u derivative limit stencil weights
344  _dvWeights; // v derivative limit stencil weights
345 };
346 
347 
348 // Update values by appling cached stencil weights to new control values
349 template <class T> void
350 StencilTable::update(T const *controlValues, T *values,
351  std::vector<float> const &valueWeights, Index start, Index end) const {
352 
353  int const * sizes = &_sizes.at(0);
354  Index const * indices = &_indices.at(0);
355  float const * weights = &valueWeights.at(0);
356 
357  if (start>0) {
358  assert(start<(Index)_offsets.size());
359  sizes += start;
360  indices += _offsets[start];
361  weights += _offsets[start];
362  values += start;
363  }
364 
365  if (end<start or end<0) {
366  end = GetNumStencils();
367  }
368 
369  int nstencils = end - std::max(0, start);
370  for (int i=0; i<nstencils; ++i, ++sizes) {
371 
372  // Zero out the result accumulators
373  values[i].Clear();
374 
375  // For each element in the array, add the coefs contribution
376  for (int j=0; j<*sizes; ++j, ++indices, ++weights) {
377  values[i].AddWithWeight( controlValues[*indices], *weights );
378  }
379  }
380 }
381 
382 inline void
384  Index offset=0;
385  int noffsets = (int)_sizes.size();
386  _offsets.resize(noffsets);
387  for (int i=0; i<(int)_sizes.size(); ++i ) {
388  _offsets[i]=offset;
389  offset+=_sizes[i];
390  }
391 }
392 
393 inline void
394 StencilTable::resize(int nstencils, int nelems) {
395  _sizes.resize(nstencils);
396  _indices.resize(nelems);
397  _weights.resize(nelems);
398 }
399 
400 // Returns a Stencil at index i in the table
401 inline Stencil
403  assert((not _offsets.empty()) and i<(int)_offsets.size());
404 
405  Index ofs = _offsets[i];
406 
407  return Stencil( const_cast<int*>(&_sizes[i]),
408  const_cast<Index *>(&_indices[ofs]),
409  const_cast<float *>(&_weights[ofs]) );
410 }
411 
412 inline Stencil
414  return GetStencil(index);
415 }
416 
417 inline void
418 LimitStencilTable::resize(int nstencils, int nelems) {
419  StencilTable::resize(nstencils, nelems);
420  _duWeights.resize(nelems);
421  _dvWeights.resize(nelems);
422 }
423 
424 
425 } // end namespace Far
426 
427 } // end namespace OPENSUBDIV_VERSION
428 using namespace OPENSUBDIV_VERSION;
429 
430 } // end namespace OpenSubdiv
431 
432 #endif // OPENSUBDIV3_FAR_STENCILTABLE_H
LimitStencil(int *size, Index *indices, float *weights, float *duWeights, float *dvWeights)
Constructor.
Definition: stencilTable.h:243
void Next()
Advance to the next stencil in the table.
Definition: stencilTable.h:97
std::vector< Index > const & GetControlIndices() const
Returns the indices of the control vertices.
Definition: stencilTable.h:160
void Clear()
Clears the stencils from 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.
Definition: stencilTable.h:326
void UpdateValues(T const *controlValues, T *values, Index start=-1, Index end=-1) const
Updates point values based on the control values.
Definition: stencilTable.h:187
Stencil(int *size, Index *indices, float *weights)
Constructor.
Definition: stencilTable.h:61
int GetNumControlVertices() const
Returns the number of control vertices indexed in the table.
Definition: stencilTable.h:142
std::vector< float > const & GetWeights() const
Returns the stencil interpolation weights.
Definition: stencilTable.h:165
Stencil operator[](Index index) const
Returns the stencil at index i in the table.
Definition: stencilTable.h:413
void resize(int nstencils, int nelems)
Definition: stencilTable.h:394
Index const * GetVertexIndices() const
Returns the control vertices indices.
Definition: stencilTable.h:87
Stencil GetStencil(Index i) const
Returns a Stencil at index i in the table.
Definition: stencilTable.h:402
void update(T const *controlValues, T *values, std::vector< float > const &valueWeights, Index start, Index end) const
Definition: stencilTable.h:350
void Next()
Advance to the next stencil in the table.
Definition: stencilTable.h:264
std::vector< float > const & GetDuWeights() const
Returns the 'u' derivative stencil interpolation weights.
Definition: stencilTable.h:299
Stencil(Stencil const &other)
Copy constructor.
Definition: stencilTable.h:70
std::vector< Index > const & GetOffsets() const
Returns the offset to a given stencil (factory may leave empty)
Definition: stencilTable.h:155
std::vector< float > const & GetDvWeights() const
Returns the 'v' derivative stencil interpolation weights.
Definition: stencilTable.h:304
int * GetSizePtr() const
Returns the size of the stencil as a pointer.
Definition: stencilTable.h:82
void Clear()
Clears the stencils from the table.
int GetNumStencils() const
Returns the number of stencils in the table.
Definition: stencilTable.h:137
std::vector< int > const & GetSizes() const
Returns the number of control vertices of each stencil in the table.
Definition: stencilTable.h:150
float const * GetWeights() const
Returns the interpolation weights.
Definition: stencilTable.h:92
int GetSize() const
Returns the size of the stencil.
Definition: stencilTable.h:77
Table of limit subdivision stencils.
Definition: stencilTable.h:285