All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
fvarLevel.h
Go to the documentation of this file.
1 //
2 // Copyright 2014 DreamWorks Animation LLC.
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 #ifndef OPENSUBDIV3_VTR_FVAR_LEVEL_H
25 #define OPENSUBDIV3_VTR_FVAR_LEVEL_H
26 
27 #include "../version.h"
28 
29 #include "../sdc/types.h"
30 #include "../sdc/crease.h"
31 #include "../sdc/options.h"
32 #include "../vtr/types.h"
33 #include "../vtr/level.h"
34 
35 #include <vector>
36 #include <cassert>
37 #include <cstring>
38 
39 
40 namespace OpenSubdiv {
41 namespace OPENSUBDIV_VERSION {
42 
43 namespace Vtr {
44 namespace internal {
45 
46 //
47 // FVarLevel:
48 // A "face-varying channel" includes the topology for a set of face-varying
49 // data, relative to the topology of the Level with which it is associated.
50 //
51 // Analogous to a set of vertices and face-vertices that define the topology for
52 // the geometry, a channel requires a set of "values" and "face-values". The
53 // "values" are indices of entries in a set of face-varying data, just as vertices
54 // are indices into a set of vertex data. The face-values identify a value for
55 // each vertex of the face, and so define topology for the values that may be
56 // unique to each channel.
57 //
58 // In addition to the value size and the vector of face-values (which matches the
59 // size of the geometry's face-vertices), tags are associated with each component
60 // to identify deviations of the face-varying topology from the vertex topology.
61 // And since there may be a one-to-many mapping between vertices and face-varying
62 // values, that mapping is also allocated.
63 //
64 // It turns out that the mapping used is able to completely encode the set of
65 // face-values and is more amenable to refinement. Currently the face-values
66 // take up almost half the memory of this representation, so if memory does
67 // become a concern, we do not need to store them. The only reason we do so now
68 // is that the face-value interface for specifying base topology and inspecting
69 // subsequent levels is very familiar to that of face-vertices for clients. So
70 // having them available for such access is convenient.
71 //
72 // Regarding scope and access...
73 // Unclear at this early state, but leaning towards nesting this class within
74 // Level, given the intimate dependency between the two.
75 // Everything is being declared public for now to facilitate access until it's
76 // clearer how this functionality will be provided.
77 //
78 class FVarLevel {
79 public:
80  //
81  // Component tags -- trying to minimize the types needed here:
82  //
83  // Tag per Edge:
84  // - facilitates topological analysis around each vertex
85  // - required during refinement to spawn one or more edge-values
86  //
87  struct ETag {
88  ETag() { }
89 
90  void clear() { std::memset(this, 0, sizeof(ETag)); }
91 
92  typedef unsigned char ETagSize;
93 
94  ETagSize _mismatch : 1; // local FVar topology does not match
95  ETagSize _disctsV0 : 1; // discontinuous at vertex 0
96  ETagSize _disctsV1 : 1; // discontinuous at vertex 1
97  ETagSize _linear : 1; // linear boundary constraints
98 
100  };
101 
102  //
103  // Tag per Value:
104  // - informs both refinement and interpolation
105  // - every value spawns a child value in refinement
106  // - includes a subset of Level::VTag to be later combined with a VTag
107  //
108  struct ValueTag {
109  ValueTag() { }
110 
111  void clear() { std::memset(this, 0, sizeof(ValueTag)); }
112 
113  bool isMismatch() const { return _mismatch; }
114  bool isCrease() const { return _crease; }
115  bool isCorner() const { return !_crease; }
116  bool isSemiSharp() const { return _semiSharp; }
117  bool isInfSharp() const { return !_semiSharp && !_crease; }
118  bool isDepSharp() const { return _depSharp; }
119  bool hasCreaseEnds() const { return _crease || _semiSharp; }
120 
121  bool hasInfSharpEdges() const { return _infSharpEdges; }
122  bool hasInfIrregularity() const { return _infIrregular; }
123 
124  typedef unsigned char ValueTagSize;
125 
126  // If there is no mismatch, no other members should be inspected
127  ValueTagSize _mismatch : 1; // local FVar topology does not match
128  ValueTagSize _xordinary : 1; // local FVar topology is extra-ordinary
129  ValueTagSize _nonManifold : 1; // local FVar topology is non-manifold
130  ValueTagSize _crease : 1; // value is a crease, otherwise a corner
131  ValueTagSize _semiSharp : 1; // value is a corner decaying to crease
132  ValueTagSize _depSharp : 1; // value is a corner by dependency on another
133 
134  ValueTagSize _infSharpEdges : 1; // value is a corner by inf-sharp features
135  ValueTagSize _infIrregular : 1; // value span includes inf-sharp irregularity
136 
138  };
139 
142 
143  //
144  // Simple struct containing the "end faces" of a crease, i.e. the faces which
145  // contain the FVar values to be used when interpolating the crease. (Prefer
146  // the struct over std::pair for its member names)
147  //
148  struct CreaseEndPair {
151  };
152 
155 
157 
160 
161 public:
162  FVarLevel(Level const& level);
163  ~FVarLevel();
164 
165  // Queries for the entire channel:
166  Level const& getLevel() const { return _level; }
167 
168  int getNumValues() const { return _valueCount; }
169  int getNumFaceValuesTotal() const { return (int) _faceVertValues.size(); }
170 
171  bool isLinear() const { return _isLinear; }
172  bool hasLinearBoundaries() const { return _hasLinearBoundaries; }
173  bool hasSmoothBoundaries() const { return ! _hasLinearBoundaries; }
174  bool hasCreaseEnds() const { return hasSmoothBoundaries(); }
175 
176  Sdc::Options getOptions() const { return _options; }
177 
178  // Queries per face:
179  ConstIndexArray getFaceValues(Index fIndex) const;
181 
182  // Queries per edge:
183  ETag getEdgeTag(Index eIndex) const { return _edgeTags[eIndex]; }
184  bool edgeTopologyMatches(Index eIndex) const { return !getEdgeTag(eIndex)._mismatch; }
185 
186  // Queries per vertex (and its potential sibling values):
187  int getNumVertexValues(Index v) const { return _vertSiblingCounts[v]; }
188  Index getVertexValueOffset(Index v, Sibling i = 0) const { return _vertSiblingOffsets[v] + i; }
189 
190  Index getVertexValue(Index v, Sibling i = 0) const { return _vertValueIndices[getVertexValueOffset(v,i)]; }
191 
192  Index findVertexValueIndex(Index vertexIndex, Index valueIndex) const;
193 
194  // Methods to access/modify array properties per vertex:
195  ConstIndexArray getVertexValues(Index vIndex) const;
197 
200 
203 
206 
207  // Queries per value:
208  ValueTag getValueTag(Index valueIndex) const { return _vertValueTags[valueIndex]; }
209  bool valueTopologyMatches(Index valueIndex) const { return !getValueTag(valueIndex)._mismatch; }
210 
211  CreaseEndPair getValueCreaseEndPair(Index valueIndex) const { return _vertValueCreaseEnds[valueIndex]; }
212 
213  // Tag queries related to faces (use Level methods for those returning Level::VTag/ETag)
214  void getFaceValueTags(Index faceIndex, ValueTag valueTags[]) const;
215 
216  ValueTag getFaceCompositeValueTag(Index faceIndex) const;
217 
218  // Higher-level topological queries, i.e. values in a neighborhood:
219  void getEdgeFaceValues(Index eIndex, int fIncToEdge, Index valuesPerVert[2]) const;
220  void getVertexEdgeValues(Index vIndex, Index valuesPerEdge[]) const;
221  void getVertexCreaseEndValues(Index vIndex, Sibling sibling, Index endValues[2]) const;
222 
223  // Initialization and allocation helpers:
224  void setOptions(Sdc::Options const& options);
225  void resizeVertexValues(int numVertexValues);
226  void resizeValues(int numValues);
227  void resizeComponents();
228 
229  // Topological analysis methods -- tagging and face-value population:
230  void completeTopologyFromFaceValues(int regBoundaryValence);
233 
234  struct ValueSpan;
235  void gatherValueSpans(Index vIndex, ValueSpan * vValueSpans) const;
236 
237  // Debugging methods:
238  bool validate() const;
239  void print() const;
240  void buildFaceVertexSiblingsFromVertexFaceSiblings(std::vector<Sibling>& fvSiblings) const;
241 
242 private:
243  // Just as Refinements build Levels, FVarRefinements build FVarLevels...
244  friend class FVarRefinement;
245 
246  Level const & _level;
247 
248  // Linear interpolation options vary between channels:
249  Sdc::Options _options;
250 
251  bool _isLinear;
252  bool _hasLinearBoundaries;
253  bool _hasDependentSharpness;
254  int _valueCount;
255 
256  //
257  // Vectors recording face-varying topology including tags that help propagate
258  // data through the refinement hierarchy. Vectors are not sparse but most use
259  // 8-bit values relative to the local topology.
260  //
261  // The vector of face-values is actually redundant here, but is constructed as
262  // it is most convenient for clients. It represents almost half the memory of
263  // the topology (4 32-bit integers per face) and not surprisingly, populating
264  // it takes a considerable amount of the refinement time (1/3). We can reduce
265  // both if we are willing to compute these on demand for clients.
266  //
267  // Per-face (matches face-verts of corresponding level):
268  std::vector<Index> _faceVertValues;
269 
270  // Per-edge:
271  std::vector<ETag> _edgeTags;
272 
273  // Per-vertex:
274  std::vector<Sibling> _vertSiblingCounts;
275  std::vector<int> _vertSiblingOffsets;
276  std::vector<Sibling> _vertFaceSiblings;
277 
278  // Per-value:
279  std::vector<Index> _vertValueIndices;
280  std::vector<ValueTag> _vertValueTags;
281  std::vector<CreaseEndPair> _vertValueCreaseEnds;
282 };
283 
284 //
285 // Access/modify the values associated with each face:
286 //
287 inline ConstIndexArray
289 
290  int vCount = _level.getNumFaceVertices(fIndex);
291  int vOffset = _level.getOffsetOfFaceVertices(fIndex);
292  return ConstIndexArray(&_faceVertValues[vOffset], vCount);
293 }
294 inline IndexArray
296 
297  int vCount = _level.getNumFaceVertices(fIndex);
298  int vOffset = _level.getOffsetOfFaceVertices(fIndex);
299  return IndexArray(&_faceVertValues[vOffset], vCount);
300 }
301 
304 
305  int vCount = _level.getNumVertexFaces(vIndex);
306  int vOffset = _level.getOffsetOfVertexFaces(vIndex);
307  return ConstSiblingArray(&_vertFaceSiblings[vOffset], vCount);
308 }
311 
312  int vCount = _level.getNumVertexFaces(vIndex);
313  int vOffset = _level.getOffsetOfVertexFaces(vIndex);
314  return SiblingArray(&_vertFaceSiblings[vOffset], vCount);
315 }
316 
317 inline ConstIndexArray
319 {
320  int vCount = getNumVertexValues(vIndex);
321  int vOffset = getVertexValueOffset(vIndex);
322  return ConstIndexArray(&_vertValueIndices[vOffset], vCount);
323 }
324 inline IndexArray
326 {
327  int vCount = getNumVertexValues(vIndex);
328  int vOffset = getVertexValueOffset(vIndex);
329  return IndexArray(&_vertValueIndices[vOffset], vCount);
330 }
331 
334 {
335  int vCount = getNumVertexValues(vIndex);
336  int vOffset = getVertexValueOffset(vIndex);
337  return ConstValueTagArray(&_vertValueTags[vOffset], vCount);
338 }
341 {
342  int vCount = getNumVertexValues(vIndex);
343  int vOffset = getVertexValueOffset(vIndex);
344  return ValueTagArray(&_vertValueTags[vOffset], vCount);
345 }
346 
349 {
350  int vCount = getNumVertexValues(vIndex);
351  int vOffset = getVertexValueOffset(vIndex);
352  return ConstCreaseEndPairArray(&_vertValueCreaseEnds[vOffset], vCount);
353 }
356 {
357  int vCount = getNumVertexValues(vIndex);
358  int vOffset = getVertexValueOffset(vIndex);
359  return CreaseEndPairArray(&_vertValueCreaseEnds[vOffset], vCount);
360 }
361 
362 inline Index
363 FVarLevel::findVertexValueIndex(Index vertexIndex, Index valueIndex) const {
364 
365  if (_level.getDepth() > 0) return valueIndex;
366 
367  Index vvIndex = getVertexValueOffset(vertexIndex);
368  while (_vertValueIndices[vvIndex] != valueIndex) {
369  ++ vvIndex;
370  }
371  return vvIndex;
372 }
373 
374 //
375 // Methods related to tagging:
376 //
377 inline Level::ETag
379 {
380  if (this->_mismatch) {
381  levelTag._boundary = true;
382  levelTag._infSharp = true;
383  }
384  return levelTag;
385 }
386 inline Level::VTag
388 {
389  if (this->_mismatch) {
390  //
391  // Semi-sharp FVar values are always tagged and treated as corners
392  // (at least three sharp edges (two boundary edges and one interior
393  // semi-sharp) and/or vertex is semi-sharp) until the sharpness has
394  // decayed, but they ultimately lie on the inf-sharp crease of the
395  // FVar boundary. Consider this when tagging inf-sharp features.
396  //
397  if (this->isCorner()) {
399  } else {
401  }
402  if (this->isCrease() || this->isSemiSharp()) {
403  levelTag._infSharp = false;
404  levelTag._infSharpCrease = true;
405  levelTag._corner = false;
406  } else {
407  levelTag._infSharp = true;
408  levelTag._infSharpCrease = false;
409  levelTag._corner = !this->_infIrregular && !this->_infSharpEdges;
410  }
411  levelTag._infSharpEdges = true;
412  levelTag._infIrregular = this->_infIrregular;
413 
414  levelTag._boundary = true;
415  levelTag._xordinary = this->_xordinary;
416 
417  levelTag._nonManifold |= this->_nonManifold;
418  }
419  return levelTag;
420 }
421 
422 } // end namespace internal
423 } // end namespace Vtr
424 
425 } // end namespace OPENSUBDIV_VERSION
426 using namespace OPENSUBDIV_VERSION;
427 } // end namespace OpenSubdiv
428 
429 #endif /* OPENSUBDIV3_VTR_FVAR_LEVEL_H */
ValueTag getFaceCompositeValueTag(Index faceIndex) const
void buildFaceVertexSiblingsFromVertexFaceSiblings(std::vector< Sibling > &fvSiblings) const
void completeTopologyFromFaceValues(int regBoundaryValence)
void getVertexCreaseEndValues(Index vIndex, Sibling sibling, Index endValues[2]) const
ConstArray< Index > ConstIndexArray
Definition: types.h:80
void getFaceValueTags(Index faceIndex, ValueTag valueTags[]) const
ConstIndexArray getVertexValues(Index vIndex) const
Definition: fvarLevel.h:318
Index getVertexValueOffset(Index v, Sibling i=0) const
Definition: fvarLevel.h:188
void getEdgeFaceValues(Index eIndex, int fIncToEdge, Index valuesPerVert[2]) const
ConstIndexArray getFaceValues(Index fIndex) const
Definition: fvarLevel.h:288
Index getVertexValue(Index v, Sibling i=0) const
Definition: fvarLevel.h:190
CreaseEndPair getValueCreaseEndPair(Index valueIndex) const
Definition: fvarLevel.h:211
Vtr::ConstArray< CreaseEndPair > ConstCreaseEndPairArray
Definition: fvarLevel.h:153
int getNumVertexFaces(Index vertIndex) const
Definition: level.h:419
ConstValueTagArray getVertexValueTags(Index vIndex) const
Definition: fvarLevel.h:333
All supported options applying to subdivision scheme.
Definition: options.h:51
int getNumFaceVertices(Index faceIndex) const
Definition: level.h:407
ConstSiblingArray getVertexFaceSiblings(Index vIndex) const
Definition: fvarLevel.h:303
void gatherValueSpans(Index vIndex, ValueSpan *vValueSpans) const
int getOffsetOfVertexFaces(Index vertIndex) const
Definition: level.h:420
bool valueTopologyMatches(Index valueIndex) const
Definition: fvarLevel.h:209
int getOffsetOfFaceVertices(Index faceIndex) const
Definition: level.h:408
ConstCreaseEndPairArray getVertexValueCreaseEnds(Index vIndex) const
Definition: fvarLevel.h:348
Index findVertexValueIndex(Index vertexIndex, Index valueIndex) const
Definition: fvarLevel.h:363
ValueTag getValueTag(Index valueIndex) const
Definition: fvarLevel.h:208
void getVertexEdgeValues(Index vIndex, Index valuesPerEdge[]) const