All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
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 PatchTableFactory;
225  // XXX: temporarily, GregoryBasis class will go away.
226  friend class GregoryBasis;
227  // XXX: needed to call reserve().
230 
231  int _numControlVertices; // number of control vertices
232 
233  std::vector<int> _sizes; // number of coeffiecient for each stencil
234  std::vector<Index> _offsets, // offset to the start of each stencil
235  _indices; // indices of contributing coarse vertices
236  std::vector<float> _weights; // stencil weight coefficients
237 };
238 
239 
242 class LimitStencil : public Stencil {
243 
244 public:
245 
258  LimitStencil( int* size,
259  Index * indices,
260  float * weights,
261  float * duWeights,
262  float * dvWeights )
263  : Stencil(size, indices, weights),
264  _duWeights(duWeights),
265  _dvWeights(dvWeights) {
266  }
267 
269  float const * GetDuWeights() const {
270  return _duWeights;
271  }
272 
274  float const * GetDvWeights() const {
275  return _dvWeights;
276  }
277 
279  void Next() {
280  int stride = *_size;
281  ++_size;
282  _indices += stride;
283  _weights += stride;
284  _duWeights += stride;
285  _dvWeights += stride;
286  }
287 
288 private:
289 
290  friend class StencilTableFactory;
292 
293  float * _duWeights, // pointer to stencil u derivative limit weights
294  * _dvWeights; // pointer to stencil v derivative limit weights
295 };
296 
301  LimitStencilTable(int numControlVerts,
302  std::vector<int> const& offsets,
303  std::vector<int> const& sizes,
304  std::vector<int> const& sources,
305  std::vector<float> const& weights,
306  std::vector<float> const& duWeights,
307  std::vector<float> const& dvWeights,
308  bool includeCoarseVerts,
309  size_t firstOffset);
310 
311 public:
312 
315 
317  LimitStencil operator[] (Index index) const;
318 
320  std::vector<float> const & GetDuWeights() const {
321  return _duWeights;
322  }
323 
325  std::vector<float> const & GetDvWeights() const {
326  return _dvWeights;
327  }
328 
346  template <class T>
347  void UpdateDerivs(T const *controlValues, T *uderivs, T *vderivs,
348  int start=-1, int end=-1) const {
349 
350  update(controlValues, uderivs, _duWeights, start, end);
351  update(controlValues, vderivs, _dvWeights, start, end);
352  }
353 
355  void Clear();
356 
357 private:
359 
360  // Resize the table arrays (factory helper)
361  void resize(int nstencils, int nelems);
362 
363 private:
364  std::vector<float> _duWeights, // u derivative limit stencil weights
365  _dvWeights; // v derivative limit stencil weights
366 };
367 
368 
369 // Update values by appling cached stencil weights to new control values
370 template <class T> void
371 StencilTable::update(T const *controlValues, T *values,
372  std::vector<float> const &valueWeights, Index start, Index end) const {
373 
374  int const * sizes = &_sizes.at(0);
375  Index const * indices = &_indices.at(0);
376  float const * weights = &valueWeights.at(0);
377 
378  if (start>0) {
379  assert(start<(Index)_offsets.size());
380  sizes += start;
381  indices += _offsets[start];
382  weights += _offsets[start];
383  values += start;
384  }
385 
386  if (end<start or end<0) {
387  end = GetNumStencils();
388  }
389 
390  int nstencils = end - std::max(0, start);
391  for (int i=0; i<nstencils; ++i, ++sizes) {
392 
393  // Zero out the result accumulators
394  values[i].Clear();
395 
396  // For each element in the array, add the coefs contribution
397  for (int j=0; j<*sizes; ++j, ++indices, ++weights) {
398  values[i].AddWithWeight( controlValues[*indices], *weights );
399  }
400  }
401 }
402 
403 inline void
405  Index offset=0;
406  int noffsets = (int)_sizes.size();
407  _offsets.resize(noffsets);
408  for (int i=0; i<(int)_sizes.size(); ++i ) {
409  _offsets[i]=offset;
410  offset+=_sizes[i];
411  }
412 }
413 
414 inline void
415 StencilTable::resize(int nstencils, int nelems) {
416  _sizes.resize(nstencils);
417  _indices.resize(nelems);
418  _weights.resize(nelems);
419 }
420 
421 inline void
422 StencilTable::reserve(int nstencils, int nelems) {
423  _sizes.reserve(nstencils);
424  _indices.reserve(nelems);
425  _weights.reserve(nelems);
426 }
427 
428 inline void
430  std::vector<int>(_sizes).swap(_sizes);
431  std::vector<Index>(_indices).swap(_indices);
432  std::vector<float>(_weights).swap(_weights);
433 }
434 
435 inline void
437  shrinkToFit();
438  generateOffsets();
439 }
440 
441 // Returns a Stencil at index i in the table
442 inline Stencil
444  assert((not _offsets.empty()) and i<(int)_offsets.size());
445 
446  Index ofs = _offsets[i];
447 
448  return Stencil( const_cast<int*>(&_sizes[i]),
449  const_cast<Index *>(&_indices[ofs]),
450  const_cast<float *>(&_weights[ofs]) );
451 }
452 
453 inline Stencil
455  return GetStencil(index);
456 }
457 
458 inline void
459 LimitStencilTable::resize(int nstencils, int nelems) {
460  StencilTable::resize(nstencils, nelems);
461  _duWeights.resize(nelems);
462  _dvWeights.resize(nelems);
463 }
464 
465 // Returns a LimitStencil at index i in the table
466 inline LimitStencil
468  assert((not GetOffsets().empty()) and i<(int)GetOffsets().size());
469 
470  Index ofs = GetOffsets()[i];
471 
472  return LimitStencil( const_cast<int *>(&GetSizes()[i]),
473  const_cast<Index *>(&GetControlIndices()[ofs]),
474  const_cast<float *>(&GetWeights()[ofs]),
475  const_cast<float *>(&GetDuWeights()[ofs]),
476  const_cast<float *>(&GetDvWeights()[ofs]) );
477 }
478 
479 inline LimitStencil
481  return GetLimitStencil(index);
482 }
483 
484 
485 } // end namespace Far
486 
487 } // end namespace OPENSUBDIV_VERSION
488 using namespace OPENSUBDIV_VERSION;
489 
490 } // end namespace OpenSubdiv
491 
492 #endif // OPENSUBDIV3_FAR_STENCILTABLE_H
LimitStencil(int *size, Index *indices, float *weights, float *duWeights, float *dvWeights)
Constructor.
Definition: stencilTable.h:258
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:162
void reserve(int nstencils, int nelems)
Definition: stencilTable.h:422
void Clear()
Clears the stencils from the table.
LimitStencil GetLimitStencil(Index i) const
Returns a LimitStencil at index i in the table.
Definition: stencilTable.h:467
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:347
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:480
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:144
std::vector< float > const & GetWeights() const
Returns the stencil interpolation weights.
Definition: stencilTable.h:167
Stencil operator[](Index index) const
Returns the stencil at index i in the table.
Definition: stencilTable.h:454
void resize(int nstencils, int nelems)
Definition: stencilTable.h:415
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:443
void update(T const *controlValues, T *values, std::vector< float > const &valueWeights, Index start, Index end) const
Definition: stencilTable.h:371
void Next()
Advance to the next stencil in the table.
Definition: stencilTable.h:279
std::vector< float > const & GetDuWeights() const
Returns the &#39;u&#39; derivative stencil interpolation weights.
Definition: stencilTable.h:320
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:157
std::vector< float > const & GetDvWeights() const
Returns the &#39;v&#39; derivative stencil interpolation weights.
Definition: stencilTable.h:325
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:139
std::vector< int > const & GetSizes() const
Returns the number of control vertices of each stencil in the table.
Definition: stencilTable.h:152
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:300