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 protected:
210  StencilTable(int numControlVerts)
211  : _numControlVertices(numControlVerts)
212  { }
213 
214  friend class StencilTableFactory;
215  // XXX: temporarily, GregoryBasis class will go away.
216  friend class GregoryBasis;
217 
218  int _numControlVertices; // number of control vertices
219 
220  std::vector<int> _sizes; // number of coeffiecient for each stencil
221  std::vector<Index> _offsets, // offset to the start of each stencil
222  _indices; // indices of contributing coarse vertices
223  std::vector<float> _weights; // stencil weight coefficients
224 };
225 
226 
229 class LimitStencil : public Stencil {
230 
231 public:
232 
245  LimitStencil( int* size,
246  Index * indices,
247  float * weights,
248  float * duWeights,
249  float * dvWeights )
250  : Stencil(size, indices, weights),
251  _duWeights(duWeights),
252  _dvWeights(dvWeights) {
253  }
254 
256  float const * GetDuWeights() const {
257  return _duWeights;
258  }
259 
261  float const * GetDvWeights() const {
262  return _dvWeights;
263  }
264 
266  void Next() {
267  int stride = *_size;
268  ++_size;
269  _indices += stride;
270  _weights += stride;
271  _duWeights += stride;
272  _dvWeights += stride;
273  }
274 
275 private:
276 
277  friend class StencilTableFactory;
279 
280  float * _duWeights, // pointer to stencil u derivative limit weights
281  * _dvWeights; // pointer to stencil v derivative limit weights
282 };
283 
288  LimitStencilTable(int numControlVerts,
289  std::vector<int> const& offsets,
290  std::vector<int> const& sizes,
291  std::vector<int> const& sources,
292  std::vector<float> const& weights,
293  std::vector<float> const& duWeights,
294  std::vector<float> const& dvWeights,
295  bool includeCoarseVerts,
296  size_t firstOffset);
297 
298 public:
299 
301  std::vector<float> const & GetDuWeights() const {
302  return _duWeights;
303  }
304 
306  std::vector<float> const & GetDvWeights() const {
307  return _dvWeights;
308  }
309 
327  template <class T>
328  void UpdateDerivs(T const *controlValues, T *uderivs, T *vderivs,
329  int start=-1, int end=-1) const {
330 
331  update(controlValues, uderivs, _duWeights, start, end);
332  update(controlValues, vderivs, _dvWeights, start, end);
333  }
334 
336  void Clear();
337 
338 private:
340 
341  // Resize the table arrays (factory helper)
342  void resize(int nstencils, int nelems);
343 
344 private:
345  std::vector<float> _duWeights, // u derivative limit stencil weights
346  _dvWeights; // v derivative limit stencil weights
347 };
348 
349 
350 // Update values by appling cached stencil weights to new control values
351 template <class T> void
352 StencilTable::update(T const *controlValues, T *values,
353  std::vector<float> const &valueWeights, Index start, Index end) const {
354 
355  int const * sizes = &_sizes.at(0);
356  Index const * indices = &_indices.at(0);
357  float const * weights = &valueWeights.at(0);
358 
359  if (start>0) {
360  assert(start<(Index)_offsets.size());
361  sizes += start;
362  indices += _offsets[start];
363  weights += _offsets[start];
364  values += start;
365  }
366 
367  if (end<start or end<0) {
368  end = GetNumStencils();
369  }
370 
371  int nstencils = end - std::max(0, start);
372  for (int i=0; i<nstencils; ++i, ++sizes) {
373 
374  // Zero out the result accumulators
375  values[i].Clear();
376 
377  // For each element in the array, add the coefs contribution
378  for (int j=0; j<*sizes; ++j, ++indices, ++weights) {
379  values[i].AddWithWeight( controlValues[*indices], *weights );
380  }
381  }
382 }
383 
384 inline void
386  Index offset=0;
387  int noffsets = (int)_sizes.size();
388  _offsets.resize(noffsets);
389  for (int i=0; i<(int)_sizes.size(); ++i ) {
390  _offsets[i]=offset;
391  offset+=_sizes[i];
392  }
393 }
394 
395 inline void
396 StencilTable::resize(int nstencils, int nelems) {
397  _sizes.resize(nstencils);
398  _indices.resize(nelems);
399  _weights.resize(nelems);
400 }
401 
402 // Returns a Stencil at index i in the table
403 inline Stencil
405  assert((not _offsets.empty()) and i<(int)_offsets.size());
406 
407  Index ofs = _offsets[i];
408 
409  return Stencil( const_cast<int*>(&_sizes[i]),
410  const_cast<Index *>(&_indices[ofs]),
411  const_cast<float *>(&_weights[ofs]) );
412 }
413 
414 inline Stencil
416  return GetStencil(index);
417 }
418 
419 inline void
420 LimitStencilTable::resize(int nstencils, int nelems) {
421  StencilTable::resize(nstencils, nelems);
422  _duWeights.resize(nelems);
423  _dvWeights.resize(nelems);
424 }
425 
426 
427 } // end namespace Far
428 
429 } // end namespace OPENSUBDIV_VERSION
430 using namespace OPENSUBDIV_VERSION;
431 
432 } // end namespace OpenSubdiv
433 
434 #endif // OPENSUBDIV3_FAR_STENCILTABLE_H
LimitStencil(int *size, Index *indices, float *weights, float *duWeights, float *dvWeights)
Constructor.
Definition: stencilTable.h:245
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 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:328
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
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:415
void resize(int nstencils, int nelems)
Definition: stencilTable.h:396
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:404
void update(T const *controlValues, T *values, std::vector< float > const &valueWeights, Index start, Index end) const
Definition: stencilTable.h:352
void Next()
Advance to the next stencil in the table.
Definition: stencilTable.h:266
std::vector< float > const & GetDuWeights() const
Returns the 'u' derivative stencil interpolation weights.
Definition: stencilTable.h:301
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 'v' derivative stencil interpolation weights.
Definition: stencilTable.h:306
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:287