- Main Page
- Namespaces
- Classes
- Files

- File List

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;
106 friend class LimitStencilTableFactory;
107
108 int * _size;
109 Index * _indices;
110 float * _weights;
111 };
112
125 class StencilTable {
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:
209 StencilTable() : _numControlVertices(0) {}
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;
278 friend class LimitStencilTableFactory;
279
280 float * _duWeights, // pointer to stencil u derivative limit weights
281 * _dvWeights; // pointer to stencil v derivative limit weights
282 };
283
287 class LimitStencilTable : public StencilTable {
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:
339 friend class LimitStencilTableFactory;
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
385 StencilTable::generateOffsets() {
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
404 StencilTable::GetStencil(Index i) const {
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
415 StencilTable::operator[] (Index index) const {
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
OpenSubdiv::OPENSUBDIV_VERSION::Far::LimitStencil::GetDvWeights
float const * GetDvWeights() const
Definition: stencilTable.h:261
OpenSubdiv::OPENSUBDIV_VERSION::Far::LimitStencil::LimitStencil
LimitStencil(int *size, Index *indices, float *weights, float *duWeights, float *dvWeights)
Constructor.
Definition: stencilTable.h:245
OpenSubdiv::OPENSUBDIV_VERSION::Far::Stencil::Next
void Next()
Advance to the next stencil in the table.
Definition: stencilTable.h:97
OpenSubdiv::OPENSUBDIV_VERSION::Far::StencilTable::GetControlIndices
std::vector< Index > const & GetControlIndices() const
Returns the indices of the control vertices.
Definition: stencilTable.h:162
OpenSubdiv::OPENSUBDIV_VERSION::Far::LimitStencil::GetDuWeights
float const * GetDuWeights() const
Definition: stencilTable.h:256
OpenSubdiv::OPENSUBDIV_VERSION::Far::StencilTable::Clear
void Clear()
Clears the stencils from the table.
OpenSubdiv::OPENSUBDIV_VERSION::Far::StencilTable::StencilTable
StencilTable(int numControlVerts)
Definition: stencilTable.h:210
OpenSubdiv::OPENSUBDIV_VERSION::Far::StencilTable::_indices
std::vector< Index > _indices
Definition: stencilTable.h:221
OpenSubdiv::OPENSUBDIV_VERSION::Far::LimitStencilTable::UpdateDerivs
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
OpenSubdiv
Definition: error.h:30
OpenSubdiv::OPENSUBDIV_VERSION::Far::LimitStencil
Limit point stencil descriptor.
Definition: stencilTable.h:229
OpenSubdiv::OPENSUBDIV_VERSION::Far::StencilTable::UpdateValues
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
OpenSubdiv::OPENSUBDIV_VERSION::Far::Stencil::_indices
Index * _indices
Definition: stencilTable.h:109
OpenSubdiv::OPENSUBDIV_VERSION::Far::StencilTable::_numControlVertices
int _numControlVertices
Definition: stencilTable.h:218
OpenSubdiv::OPENSUBDIV_VERSION::Far::Stencil::Stencil
Stencil(int *size, Index *indices, float *weights)
Constructor.
Definition: stencilTable.h:61
OpenSubdiv::OPENSUBDIV_VERSION::Far::StencilTableFactory
A specialized factory for StencilTable.
Definition: stencilTableFactory.h:48
OpenSubdiv::OPENSUBDIV_VERSION::Far::StencilTable::GetNumControlVertices
int GetNumControlVertices() const
Returns the number of control vertices indexed in the table.
Definition: stencilTable.h:144
OpenSubdiv::OPENSUBDIV_VERSION::Far::StencilTable::GetWeights
std::vector< float > const & GetWeights() const
Returns the stencil interpolation weights.
Definition: stencilTable.h:167
OpenSubdiv::OPENSUBDIV_VERSION::Far::Stencil::Stencil
Stencil()
Default constructor.
Definition: stencilTable.h:51
OpenSubdiv::OPENSUBDIV_VERSION::Far::StencilTable::GregoryBasis
friend class GregoryBasis
Definition: stencilTable.h:216
OpenSubdiv::OPENSUBDIV_VERSION::Far::StencilTable::operator[]
Stencil operator[](Index index) const
Returns the stencil at index i in the table.
Definition: stencilTable.h:415
OpenSubdiv::OPENSUBDIV_VERSION::Far::Stencil::_weights
float * _weights
Definition: stencilTable.h:110
OpenSubdiv::OPENSUBDIV_VERSION::Far::StencilTable::_offsets
std::vector< Index > _offsets
Definition: stencilTable.h:221
OpenSubdiv::OPENSUBDIV_VERSION::Far::StencilTable::resize
void resize(int nstencils, int nelems)
Definition: stencilTable.h:396
OpenSubdiv::OPENSUBDIV_VERSION::Far::Stencil::GetVertexIndices
Index const * GetVertexIndices() const
Returns the control vertices indices.
Definition: stencilTable.h:87
OpenSubdiv::OPENSUBDIV_VERSION::Far::StencilTable::GetStencil
Stencil GetStencil(Index i) const
Returns a Stencil at index i in the table.
Definition: stencilTable.h:404
OpenSubdiv::OPENSUBDIV_VERSION::Far::StencilTable
Table of subdivision stencils.
Definition: stencilTable.h:125
OpenSubdiv::OPENSUBDIV_VERSION::Far::StencilTable::update
void update(T const *controlValues, T *values, std::vector< float > const &valueWeights, Index start, Index end) const
Definition: stencilTable.h:352
OpenSubdiv::OPENSUBDIV_VERSION::Far::StencilTable::StencilTable
StencilTable()
Definition: stencilTable.h:209
OpenSubdiv::OPENSUBDIV_VERSION::Far::StencilTable::~StencilTable
virtual ~StencilTable()
Definition: stencilTable.h:136
OpenSubdiv::OPENSUBDIV_VERSION::Far::LimitStencil::Next
void Next()
Advance to the next stencil in the table.
Definition: stencilTable.h:266
OpenSubdiv::OPENSUBDIV_VERSION::Far::LimitStencilTable::GetDuWeights
std::vector< float > const & GetDuWeights() const
Returns the 'u' derivative stencil interpolation weights.
Definition: stencilTable.h:301
OpenSubdiv::OPENSUBDIV_VERSION::Far::LimitStencilTableFactory
A specialized factory for LimitStencilTable.
Definition: stencilTableFactory.h:143
OpenSubdiv::OPENSUBDIV_VERSION::Far::Index
Vtr::Index Index
Definition: types.h:41
OpenSubdiv::OPENSUBDIV_VERSION::Far::Stencil::Stencil
Stencil(Stencil const &other)
Copy constructor.
Definition: stencilTable.h:70
OpenSubdiv::OPENSUBDIV_VERSION::Far::Stencil::_size
int * _size
Definition: stencilTable.h:108
OpenSubdiv::OPENSUBDIV_VERSION::Far::StencilTable::generateOffsets
void generateOffsets()
Definition: stencilTable.h:385
OpenSubdiv::OPENSUBDIV_VERSION::Far::StencilTable::GetOffsets
std::vector< Index > const & GetOffsets() const
Returns the offset to a given stencil (factory may leave empty)
Definition: stencilTable.h:157
OpenSubdiv::OPENSUBDIV_VERSION::Far::StencilTable::_weights
std::vector< float > _weights
Definition: stencilTable.h:223
OpenSubdiv::OPENSUBDIV_VERSION::Far::LimitStencilTable::GetDvWeights
std::vector< float > const & GetDvWeights() const
Returns the 'v' derivative stencil interpolation weights.
Definition: stencilTable.h:306
OpenSubdiv::OPENSUBDIV_VERSION::Far::Stencil::GetSizePtr
int * GetSizePtr() const
Returns the size of the stencil as a pointer.
Definition: stencilTable.h:82
OpenSubdiv::OPENSUBDIV_VERSION::Far::LimitStencilTable::Clear
void Clear()
Clears the stencils from the table.
OpenSubdiv::OPENSUBDIV_VERSION::Far::StencilTable::GetNumStencils
int GetNumStencils() const
Returns the number of stencils in the table.
Definition: stencilTable.h:139
OpenSubdiv::OPENSUBDIV_VERSION::Far::Stencil
Vertex stencil descriptor.
Definition: stencilTable.h:46
OpenSubdiv::OPENSUBDIV_VERSION::Far::StencilTable::GetSizes
std::vector< int > const & GetSizes() const
Returns the number of control vertices of each stencil in the table.
Definition: stencilTable.h:152
OpenSubdiv::OPENSUBDIV_VERSION::Far::StencilTable::_sizes
std::vector< int > _sizes
Definition: stencilTable.h:220
OpenSubdiv::OPENSUBDIV_VERSION::Far::Stencil::GetWeights
float const * GetWeights() const
Returns the interpolation weights.
Definition: stencilTable.h:92
OpenSubdiv::OPENSUBDIV_VERSION::Far::Stencil::GetSize
int GetSize() const
Returns the size of the stencil.
Definition: stencilTable.h:77
OpenSubdiv::OPENSUBDIV_VERSION::Far::LimitStencilTable
Table of limit subdivision stencils.
Definition: stencilTable.h:287