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_COMPONENT_INTERFACES_H
25 #define OPENSUBDIV3_VTR_COMPONENT_INTERFACES_H
26 
27 #include "../version.h"
28 
29 #include "../sdc/types.h"
30 #include "../sdc/crease.h"
31 #include "../vtr/types.h"
32 #include "../vtr/stackBuffer.h"
33 
34 #include <vector>
35 
36 
37 namespace OpenSubdiv {
38 namespace OPENSUBDIV_VERSION {
39 
40 namespace Vtr {
41 namespace internal {
42 
43 //
44 //  Simple classes supporting the interfaces required of generic topological
45 //  types in the Scheme mask queries, e.g. <typename FACE, VERTEX, etc.>
46 //
47 //  These are not used with Vtr but arguably belong with it as the details to
48 //  write these efficiently depends very much on intimate details of Vtr's
49 //  implementation, e.g. the use of tag bits, subdivision Rules, etc.
50 //
51 
52 
53 //
54 //  For <typename FACE>, which provides information in the neighborhood of a face:
55 //
56 class FaceInterface {
57 public:
FaceInterface()58     FaceInterface() { }
FaceInterface(int vertCount)59     FaceInterface(int vertCount) : _vertCount(vertCount) { }
~FaceInterface()60     ~FaceInterface() { }
61 
62 public:  //  Generic interface expected of <typename FACE>:
GetNumVertices()63     int GetNumVertices() const { return _vertCount; }
64 
65 private:
66     int _vertCount;
67 };
68 
69 
70 //
71 //  For <typename EDGE>, which provides information in the neighborhood of an edge:
72 //
73 class EdgeInterface {
74 public:
EdgeInterface()75     EdgeInterface() { }
EdgeInterface(Level const & level)76     EdgeInterface(Level const& level) : _level(&level) { }
~EdgeInterface()77     ~EdgeInterface() { }
78 
SetIndex(int edgeIndex)79     void SetIndex(int edgeIndex) { _eIndex = edgeIndex; }
80 
81 public:  //  Generic interface expected of <typename EDGE>:
GetNumFaces()82     int   GetNumFaces() const { return _level->getEdgeFaces(_eIndex).size(); }
GetSharpness()83     float GetSharpness() const { return _level->getEdgeSharpness(_eIndex); }
84 
GetChildSharpnesses(Sdc::Crease const &,float s[2])85     void GetChildSharpnesses(Sdc::Crease const&, float s[2]) const {
86         //  Need to use the Refinement here to identify the two child edges:
87         s[0] = s[1] = GetSharpness() - 1.0f;
88     }
89 
GetNumVerticesPerFace(int vertsPerFace[])90     void GetNumVerticesPerFace(int vertsPerFace[]) const {
91         ConstIndexArray eFaces = _level->getEdgeFaces(_eIndex);
92         for (int i = 0; i < eFaces.size(); ++i) {
93             vertsPerFace[i] = _level->getFaceVertices(eFaces[i]).size();
94         }
95     }
96 
97 private:
98     const Level* _level;
99 
100     int _eIndex;
101 };
102 
103 
104 //
105 //  For <typename VERTEX>, which provides information in the neighborhood of a vertex:
106 //
107 class VertexInterface {
108 public:
VertexInterface()109     VertexInterface() { }
VertexInterface(Level const & parent,Level const & child)110     VertexInterface(Level const& parent, Level const& child) : _parent(&parent), _child(&child) { }
~VertexInterface()111     ~VertexInterface() { }
112 
SetIndex(int parentIndex,int childIndex)113     void SetIndex(int parentIndex, int childIndex) {
114         _pIndex = parentIndex;
115         _cIndex = childIndex;
116         _eCount = _parent->getVertexEdges(_pIndex).size();
117         _fCount = _parent->getVertexFaces(_pIndex).size();
118     }
119 
120 public:  //  Generic interface expected of <typename VERT>:
GetNumEdges()121     int GetNumEdges() const { return _eCount; }
GetNumFaces()122     int GetNumFaces() const { return _fCount; }
123 
GetSharpness()124     float  GetSharpness() const { return _parent->getVertexSharpness(_pIndex); }
GetSharpnessPerEdge(float pSharpness[])125     float* GetSharpnessPerEdge(float pSharpness[]) const {
126         ConstIndexArray pEdges = _parent->getVertexEdges(_pIndex);
127         for (int i = 0; i < _eCount; ++i) {
128             pSharpness[i] = _parent->getEdgeSharpness(pEdges[i]);
129         }
130         return pSharpness;
131     }
132 
GetChildSharpness(Sdc::Crease const &)133     float  GetChildSharpness(Sdc::Crease const&) const { return _child->getVertexSharpness(_cIndex); }
GetChildSharpnessPerEdge(Sdc::Crease const & crease,float cSharpness[])134     float* GetChildSharpnessPerEdge(Sdc::Crease const& crease, float cSharpness[]) const {
135         internal::StackBuffer<float,16> pSharpness(_eCount);
136         GetSharpnessPerEdge(pSharpness);
137         crease.SubdivideEdgeSharpnessesAroundVertex(_eCount, pSharpness, cSharpness);
138         return cSharpness;
139     }
140 
141 private:
142     const Level* _parent;
143     const Level* _child;
144 
145     int _pIndex;
146     int _cIndex;
147     int _eCount;
148     int _fCount;
149 };
150 
151 } // end namespace internal
152 } // end namespace Vtr
153 
154 } // end namespace OPENSUBDIV_VERSION
155 using namespace OPENSUBDIV_VERSION;
156 } // end namespace OpenSubdiv
157 
158 #endif /* OPENSUBDIV3_VTR_COMPONENT_INTERFACES_H */
159