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 
136  virtual ~StencilTable() {};
137 
139  int GetNumStencils() const {
140  return (int)_sizes.size();
141  }
142 
144  int GetNumControlVertices() const {
145  return _numControlVertices;
146  }
147 
149  Stencil GetStencil(Index i) const;
150 
152  std::vector<int> const & GetSizes() const {
153  return _sizes;
154  }
155 
157  std::vector<Index> const & GetOffsets() const {
158  return _offsets;
159  }
160 
162  std::vector<Index> const & GetControlIndices() const {
163  return _indices;
164  }
165 
167  std::vector<float> const & GetWeights() const {
168  return _weights;
169  }
170 
172  Stencil operator[] (Index index) const;
173 
188  template <class T>
189  void UpdateValues(T const *controlValues, T *values, Index start=-1, Index end=-1) const {
190  update(controlValues, values, _weights, start, end);
191  }
192 
194  void Clear();
195 
196 protected:
197 
198  // Update values by applying cached stencil weights to new control values
199  template <class T> void update( T const *controlValues, T *values,
200  std::vector<float> const & valueWeights, Index start, Index end) const;
201 
202  // Populate the offsets table from the stencil sizes in _sizes (factory helper)
203  void generateOffsets();
204 
205  // Resize the table arrays (factory helper)
206  void resize(int nstencils, int nelems);
207 
208  // Reserves the table arrays (factory helper)
209  void reserve(int nstencils, int nelems);
210 
211  // Reallocates the table arrays to remove excess capacity (factory helper)
212  void shrinkToFit();
213 
214  // Performs any final operations on internal tables (factory helper)
215  void finalize();
216 
217 protected:
219  StencilTable(int numControlVerts)
220  : _numControlVertices(numControlVerts)
221  { }
222 
223  friend class StencilTableFactory;
224  friend class PatchTableBuilder;
225 
226  int _numControlVertices; // number of control vertices
227 
228  std::vector<int> _sizes; // number of coefficients for each stencil
229  std::vector<Index> _offsets, // offset to the start of each stencil
230  _indices; // indices of contributing coarse vertices
231  std::vector<float> _weights; // stencil weight coefficients
232 };
233 
234 
237 class LimitStencil : public Stencil {
238 
239 public:
240 
259  LimitStencil( int* size,
260  Index * indices,
261  float * weights,
262  float * duWeights=0,
263  float * dvWeights=0,
264  float * duuWeights=0,
265  float * duvWeights=0,
266  float * dvvWeights=0)
267  : Stencil(size, indices, weights),
268  _duWeights(duWeights),
269  _dvWeights(dvWeights),
270  _duuWeights(duuWeights),
271  _duvWeights(duvWeights),
272  _dvvWeights(dvvWeights) {
273  }
274 
276  float const * GetDuWeights() const {
277  return _duWeights;
278  }
279 
281  float const * GetDvWeights() const {
282  return _dvWeights;
283  }
284 
286  float const * GetDuuWeights() const {
287  return _duuWeights;
288  }
289 
291  float const * GetDuvWeights() const {
292  return _duvWeights;
293  }
294 
296  float const * GetDvvWeights() const {
297  return _dvvWeights;
298  }
299 
301  void Next() {
302  int stride = *_size;
303  ++_size;
304  _indices += stride;
305  _weights += stride;
306  if (_duWeights) _duWeights += stride;
307  if (_dvWeights) _dvWeights += stride;
308  if (_duuWeights) _duuWeights += stride;
309  if (_duvWeights) _duvWeights += stride;
310  if (_dvvWeights) _dvvWeights += stride;
311  }
312 
313 private:
314 
315  friend class StencilTableFactory;
317 
318  float * _duWeights, // pointer to stencil u derivative limit weights
319  * _dvWeights, // pointer to stencil v derivative limit weights
320  * _duuWeights, // pointer to stencil uu derivative limit weights
321  * _duvWeights, // pointer to stencil uv derivative limit weights
322  * _dvvWeights; // pointer to stencil vv derivative limit weights
323 };
324 
329  LimitStencilTable(int numControlVerts,
330  std::vector<int> const& offsets,
331  std::vector<int> const& sizes,
332  std::vector<int> const& sources,
333  std::vector<float> const& weights,
334  std::vector<float> const& duWeights,
335  std::vector<float> const& dvWeights,
336  std::vector<float> const& duuWeights,
337  std::vector<float> const& duvWeights,
338  std::vector<float> const& dvvWeights,
339  bool includeCoarseVerts,
340  size_t firstOffset);
341 
342 public:
343 
346 
348  LimitStencil operator[] (Index index) const;
349 
351  std::vector<float> const & GetDuWeights() const {
352  return _duWeights;
353  }
354 
356  std::vector<float> const & GetDvWeights() const {
357  return _dvWeights;
358  }
359 
361  std::vector<float> const & GetDuuWeights() const {
362  return _duuWeights;
363  }
364 
366  std::vector<float> const & GetDuvWeights() const {
367  return _duvWeights;
368  }
369 
371  std::vector<float> const & GetDvvWeights() const {
372  return _dvvWeights;
373  }
374 
392  template <class T>
393  void UpdateDerivs(T const *controlValues, T *uderivs, T *vderivs,
394  int start=-1, int end=-1) const {
395 
396  update(controlValues, uderivs, _duWeights, start, end);
397  update(controlValues, vderivs, _dvWeights, start, end);
398  }
399 
420  template <class T>
421  void Update2ndDerivs(T const *controlValues, T *uuderivs, T *uvderivs, T *vvderivs,
422  int start=-1, int end=-1) const {
423 
424  update(controlValues, uuderivs, _duuWeights, start, end);
425  update(controlValues, uvderivs, _duvWeights, start, end);
426  update(controlValues, vvderivs, _dvvWeights, start, end);
427  }
428 
430  void Clear();
431 
432 private:
434 
435  // Resize the table arrays (factory helper)
436  void resize(int nstencils, int nelems);
437 
438 private:
439  std::vector<float> _duWeights, // u derivative limit stencil weights
440  _dvWeights, // v derivative limit stencil weights
441  _duuWeights, // uu derivative limit stencil weights
442  _duvWeights, // uv derivative limit stencil weights
443  _dvvWeights; // vv derivative limit stencil weights
444 };
445 
446 
447 // Update values by applying cached stencil weights to new control values
448 template <class T> void
449 StencilTable::update(T const *controlValues, T *values,
450  std::vector<float> const &valueWeights, Index start, Index end) const {
451 
452  int const * sizes = &_sizes.at(0);
453  Index const * indices = &_indices.at(0);
454  float const * weights = &valueWeights.at(0);
455 
456  if (start>0) {
457  assert(start<(Index)_offsets.size());
458  sizes += start;
459  indices += _offsets[start];
460  weights += _offsets[start];
461  values += start;
462  }
463 
464  if (end<start || end<0) {
465  end = GetNumStencils();
466  }
467 
468  int nstencils = end - std::max(0, start);
469  for (int i=0; i<nstencils; ++i, ++sizes) {
470 
471  // Zero out the result accumulators
472  values[i].Clear();
473 
474  // For each element in the array, add the coef's contribution
475  for (int j=0; j<*sizes; ++j, ++indices, ++weights) {
476  values[i].AddWithWeight( controlValues[*indices], *weights );
477  }
478  }
479 }
480 
481 inline void
483  Index offset=0;
484  int noffsets = (int)_sizes.size();
485  _offsets.resize(noffsets);
486  for (int i=0; i<(int)_sizes.size(); ++i ) {
487  _offsets[i]=offset;
488  offset+=_sizes[i];
489  }
490 }
491 
492 inline void
493 StencilTable::resize(int nstencils, int nelems) {
494  _sizes.resize(nstencils);
495  _indices.resize(nelems);
496  _weights.resize(nelems);
497 }
498 
499 inline void
500 StencilTable::reserve(int nstencils, int nelems) {
501  _sizes.reserve(nstencils);
502  _indices.reserve(nelems);
503  _weights.reserve(nelems);
504 }
505 
506 inline void
508  std::vector<int>(_sizes).swap(_sizes);
509  std::vector<Index>(_indices).swap(_indices);
510  std::vector<float>(_weights).swap(_weights);
511 }
512 
513 inline void
515  shrinkToFit();
516  generateOffsets();
517 }
518 
519 // Returns a Stencil at index i in the table
520 inline Stencil
522  assert((! _offsets.empty()) && i<(int)_offsets.size());
523 
524  Index ofs = _offsets[i];
525 
526  return Stencil( const_cast<int*>(&_sizes[i]),
527  const_cast<Index *>(&_indices[ofs]),
528  const_cast<float *>(&_weights[ofs]) );
529 }
530 
531 inline Stencil
533  return GetStencil(index);
534 }
535 
536 inline void
537 LimitStencilTable::resize(int nstencils, int nelems) {
538  StencilTable::resize(nstencils, nelems);
539  _duWeights.resize(nelems);
540  _dvWeights.resize(nelems);
541 }
542 
543 // Returns a LimitStencil at index i in the table
544 inline LimitStencil
546  assert((! GetOffsets().empty()) && i<(int)GetOffsets().size());
547 
548  Index ofs = GetOffsets()[i];
549 
550  if (!_duWeights.empty() && !_dvWeights.empty() &&
551  !_duuWeights.empty() && !_duvWeights.empty() && !_dvvWeights.empty()) {
552  return LimitStencil( const_cast<int *>(&GetSizes()[i]),
553  const_cast<Index *>(&GetControlIndices()[ofs]),
554  const_cast<float *>(&GetWeights()[ofs]),
555  const_cast<float *>(&GetDuWeights()[ofs]),
556  const_cast<float *>(&GetDvWeights()[ofs]),
557  const_cast<float *>(&GetDuuWeights()[ofs]),
558  const_cast<float *>(&GetDuvWeights()[ofs]),
559  const_cast<float *>(&GetDvvWeights()[ofs]) );
560  } else if (!_duWeights.empty() && !_dvWeights.empty()) {
561  return LimitStencil( const_cast<int *>(&GetSizes()[i]),
562  const_cast<Index *>(&GetControlIndices()[ofs]),
563  const_cast<float *>(&GetWeights()[ofs]),
564  const_cast<float *>(&GetDuWeights()[ofs]),
565  const_cast<float *>(&GetDvWeights()[ofs]) );
566  } else {
567  return LimitStencil( const_cast<int *>(&GetSizes()[i]),
568  const_cast<Index *>(&GetControlIndices()[ofs]),
569  const_cast<float *>(&GetWeights()[ofs]) );
570  }
571 }
572 
573 inline LimitStencil
575  return GetLimitStencil(index);
576 }
577 
578 
579 } // end namespace Far
580 
581 } // end namespace OPENSUBDIV_VERSION
582 using namespace OPENSUBDIV_VERSION;
583 
584 } // end namespace OpenSubdiv
585 
586 #endif // OPENSUBDIV3_FAR_STENCILTABLE_H
Stencil(Stencil const &other)
Copy constructor.
Definition: stencilTable.h:70
std::vector< float > const & GetDvWeights() const
Returns the &#39;v&#39; derivative stencil interpolation weights.
Definition: stencilTable.h:356
void Update2ndDerivs(T const *controlValues, T *uuderivs, T *uvderivs, T *vvderivs, int start=-1, int end=-1) const
Updates 2nd derivative values based on the control values.
Definition: stencilTable.h:421
int * GetSizePtr() const
Returns the size of the stencil as a pointer.
Definition: stencilTable.h:82
int GetNumControlVertices() const
Returns the number of control vertices indexed in the table.
Definition: stencilTable.h:144
int GetNumStencils() const
Returns the number of stencils in the table.
Definition: stencilTable.h:139
void reserve(int nstencils, int nelems)
Definition: stencilTable.h:500
float const * GetDvvWeights() const
Returns the vv derivative weights.
Definition: stencilTable.h:296
float const * GetWeights() const
Returns the interpolation weights.
Definition: stencilTable.h:92
std::vector< float > const & GetDuuWeights() const
Returns the &#39;uu&#39; derivative stencil interpolation weights.
Definition: stencilTable.h:361
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:393
void Clear()
Clears the stencils from the table.
int GetSize() const
Returns the size of the stencil.
Definition: stencilTable.h:77
void resize(int nstencils, int nelems)
Definition: stencilTable.h:493
void Next()
Advance to the next stencil in the table.
Definition: stencilTable.h:301
float const * GetDvWeights() const
Returns the v derivative weights.
Definition: stencilTable.h:281
float const * GetDuuWeights() const
Returns the uu derivative weights.
Definition: stencilTable.h:286
std::vector< Index > const & GetOffsets() const
Returns the offset to a given stencil (factory may leave empty)
Definition: stencilTable.h:157
std::vector< float > const & GetDvvWeights() const
Returns the &#39;vv&#39; derivative stencil interpolation weights.
Definition: stencilTable.h:371
void Clear()
Clears the stencils from the table.
void Next()
Advance to the next stencil in the table.
Definition: stencilTable.h:97
std::vector< int > const & GetSizes() const
Returns the number of control vertices of each stencil in the table.
Definition: stencilTable.h:152
std::vector< float > const & GetWeights() const
Returns the stencil interpolation weights.
Definition: stencilTable.h:167
std::vector< float > const & GetDuWeights() const
Returns the &#39;u&#39; derivative stencil interpolation weights.
Definition: stencilTable.h:351
std::vector< float > const & GetDuvWeights() const
Returns the &#39;uv&#39; derivative stencil interpolation weights.
Definition: stencilTable.h:366
Stencil GetStencil(Index i) const
Returns a Stencil at index i in the table.
Definition: stencilTable.h:521
Index const * GetVertexIndices() const
Returns the control vertices&#39; indices.
Definition: stencilTable.h:87
std::vector< Index > const & GetControlIndices() const
Returns the indices of the control vertices.
Definition: stencilTable.h:162
LimitStencil GetLimitStencil(Index i) const
Returns a LimitStencil at index i in the table.
Definition: stencilTable.h:545
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:189
LimitStencil operator[](Index index) const
Returns the limit stencil at index i in the table.
Definition: stencilTable.h:574
float const * GetDuvWeights() const
Returns the uv derivative weights.
Definition: stencilTable.h:291
Table of limit subdivision stencils.
Definition: stencilTable.h:328
Stencil operator[](Index index) const
Returns the stencil at index i in the table.
Definition: stencilTable.h:532
float const * GetDuWeights() const
Returns the u derivative weights.
Definition: stencilTable.h:276
Stencil(int *size, Index *indices, float *weights)
Constructor.
Definition: stencilTable.h:61
LimitStencil(int *size, Index *indices, float *weights, float *duWeights=0, float *dvWeights=0, float *duuWeights=0, float *duvWeights=0, float *dvvWeights=0)
Constructor.
Definition: stencilTable.h:259
void update(T const *controlValues, T *values, std::vector< float > const &valueWeights, Index start, Index end) const
Definition: stencilTable.h:449