// // Copyright 2014 DreamWorks Animation LLC. // // Licensed under the Apache License, Version 2.0 (the "Apache License") // with the following modification; you may not use this file except in // compliance with the Apache License and the following modification to it: // Section 6. Trademarks. is deleted and replaced with: // // 6. Trademarks. This License does not grant permission to use the trade // names, trademarks, service marks, or product names of the Licensor // and its affiliates, except as required to comply with Section 4(c) of // the License and to reproduce the content of the NOTICE file. // // You may obtain a copy of the Apache License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the Apache License with the above modification is // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. See the Apache License for the specific // language governing permissions and limitations under the Apache License. // #include "../far/topologyRefiner.h" #include "../vtr/sparseSelector.h" #include "../vtr/quadRefinement.h" #include "../vtr/triRefinement.h" #include #include namespace OpenSubdiv { namespace OPENSUBDIV_VERSION { namespace Far { // // Relatively trivial construction/destruction -- the base level (level[0]) needs // to be explicitly initialized after construction and refinement then applied // TopologyRefiner::TopologyRefiner(Sdc::SchemeType schemeType, Sdc::Options schemeOptions) : _subdivType(schemeType), _subdivOptions(schemeOptions), _isUniform(true), _hasHoles(false), _useSingleCreasePatch(false), _maxLevel(0) { // Need to revisit allocation scheme here -- want to use smart-ptrs for these // but will probably have to settle for explicit new/delete... _levels.reserve(10); _levels.push_back(new Vtr::Level); } TopologyRefiner::~TopologyRefiner() { for (int i=0; i<(int)_levels.size(); ++i) { delete _levels[i]; } for (int i=0; i<(int)_refinements.size(); ++i) { delete _refinements[i]; } } void TopologyRefiner::Unrefine() { if (_levels.size()) { for (int i=1; i<(int)_levels.size(); ++i) { delete _levels[i]; } _levels.resize(1); } for (int i=0; i<(int)_refinements.size(); ++i) { delete _refinements[i]; } _refinements.clear(); } // // Accessors to the topology information: // int TopologyRefiner::GetNumVerticesTotal() const { int sum = 0; for (int i = 0; i < (int)_levels.size(); ++i) { sum += _levels[i]->getNumVertices(); } return sum; } int TopologyRefiner::GetNumEdgesTotal() const { int sum = 0; for (int i = 0; i < (int)_levels.size(); ++i) { sum += _levels[i]->getNumEdges(); } return sum; } int TopologyRefiner::GetNumFacesTotal() const { int sum = 0; for (int i = 0; i < (int)_levels.size(); ++i) { sum += _levels[i]->getNumFaces(); } return sum; } int TopologyRefiner::GetNumFaceVerticesTotal() const { int sum = 0; for (int i = 0; i < (int)_levels.size(); ++i) { sum += _levels[i]->getNumFaceVerticesTotal(); } return sum; } int TopologyRefiner::GetNumFVarValuesTotal(int channel) const { int sum = 0; for (int i = 0; i < (int)_levels.size(); ++i) { sum += _levels[i]->getNumFVarValues(channel); } return sum; } int TopologyRefiner::GetNumHoles(int level) const { int sum = 0; Vtr::Level const & lvl = getLevel(level); for (Index face = 0; face < lvl.getNumFaces(); ++face) { if (lvl.isHole(face)) { ++sum; } } return sum; } // // Ptex information accessors // void TopologyRefiner::initializePtexIndices() const { Vtr::Level const & coarseLevel = getLevel(0); std::vector & ptexIndices = const_cast &>(_ptexIndices); int nfaces = coarseLevel.getNumFaces(); ptexIndices.resize(nfaces+1); int ptexID=0; int regFaceSize = Sdc::SchemeTypeTraits::GetRegularFaceSize(GetSchemeType()); for (int i = 0; i < nfaces; ++i) { ptexIndices[i] = ptexID; Vtr::ConstIndexArray fverts = coarseLevel.getFaceVertices(i); ptexID += fverts.size()==regFaceSize ? 1 : fverts.size(); } // last entry contains the number of ptex texture faces ptexIndices[nfaces]=ptexID; } int TopologyRefiner::GetNumPtexFaces() const { if (_ptexIndices.empty()) { initializePtexIndices(); } return _ptexIndices.back(); } int TopologyRefiner::GetPtexIndex(Index f) const { if (_ptexIndices.empty()) { initializePtexIndices(); } assert(f<(int)_ptexIndices.size()); return _ptexIndices[f]; } namespace { // Returns the face adjacent to 'face' along edge 'edge' inline Index getAdjacentFace(Vtr::Level const & level, Index edge, Index face) { Far::ConstIndexArray adjFaces = level.getEdgeFaces(edge); if (adjFaces.size()!=2) { return -1; } return (adjFaces[0]==face) ? adjFaces[1] : adjFaces[0]; } } void TopologyRefiner::GetPtexAdjacency(int face, int quadrant, int adjFaces[4], int adjEdges[4]) const { assert(GetSchemeType()==Sdc::SCHEME_CATMARK); if (_ptexIndices.empty()) { initializePtexIndices(); } Vtr::Level const & level = getLevel(0); ConstIndexArray fedges = level.getFaceEdges(face); if (fedges.size()==4) { // Regular ptex quad face for (int i=0; i<4; ++i) { int edge = fedges[i]; Index adjface = getAdjacentFace(level, edge, face); if (adjface==-1) { adjFaces[i] = -1; // boundary or non-manifold adjEdges[i] = 0; } else { ConstIndexArray aedges = level.getFaceEdges(adjface); if (aedges.size()==4) { adjFaces[i] = _ptexIndices[adjface]; adjEdges[i] = aedges.FindIndexIn4Tuple(edge); assert(adjEdges[i]!=-1); } else { // neighbor is a sub-face adjFaces[i] = _ptexIndices[adjface] + (aedges.FindIndex(edge)+1)%aedges.size(); adjEdges[i] = 3; } assert(adjFaces[i]!=-1); } } } else { // Ptex sub-face 'quadrant' (non-quad) // // Ptex adjacency pattern for non-quads: // // v2 /* o // / \ // / \ // /0 3\ // / \ // o_ 1 2 _o // / -_ _- \ // / 2 -o- 1 \ // /3 | 0\ // / 1|2 \ // / 0 | 3 \ // o----------o----------o // v0 v1 */ assert(quadrant>=0 and quadrantgetNumVertices() > 0); // Make sure the base level has been initialized // // Allocate the stack of levels and the refinements between them: // _isUniform = true; _maxLevel = options.refinementLevel; Sdc::Split splitType = (_subdivType == Sdc::SCHEME_LOOP) ? Sdc::SPLIT_TO_TRIS : Sdc::SPLIT_TO_QUADS; // // Initialize refinement options for Vtr -- adjusting full-topology for the last level: // Vtr::Refinement::Options refineOptions; refineOptions._sparse = false; for (int i = 1; i <= (int)options.refinementLevel; ++i) { refineOptions._faceTopologyOnly = options.fullTopologyInLastLevel ? false : (i == options.refinementLevel); Vtr::Level& parentLevel = getLevel(i-1); Vtr::Level& childLevel = *(new Vtr::Level); Vtr::Refinement* refinement = 0; if (splitType == Sdc::SPLIT_TO_QUADS) { refinement = new Vtr::QuadRefinement(parentLevel, childLevel, _subdivOptions); } else { refinement = new Vtr::TriRefinement(parentLevel, childLevel, _subdivOptions); } refinement->refine(refineOptions); _levels.push_back(&childLevel); _refinements.push_back(refinement); } } void TopologyRefiner::RefineAdaptive(AdaptiveOptions options) { assert(_levels[0]->getNumVertices() > 0); // Make sure the base level has been initialized // // Allocate the stack of levels and the refinements between them: // _isUniform = false; _maxLevel = options.isolationLevel; _useSingleCreasePatch = options.useSingleCreasePatch; // // Initialize refinement options for Vtr: // Vtr::Refinement::Options refineOptions; refineOptions._sparse = true; refineOptions._faceTopologyOnly = not options.fullTopologyInLastLevel; Sdc::Split splitType = (_subdivType == Sdc::SCHEME_LOOP) ? Sdc::SPLIT_TO_TRIS : Sdc::SPLIT_TO_QUADS; for (int i = 1; i <= (int)options.isolationLevel; ++i) { // Keeping full topology on for debugging -- may need to go back a level and "prune" // its topology if we don't use the full depth refineOptions._faceTopologyOnly = false; Vtr::Level& parentLevel = getLevel(i-1); Vtr::Level& childLevel = *(new Vtr::Level); Vtr::Refinement* refinement = 0; if (splitType == Sdc::SPLIT_TO_QUADS) { refinement = new Vtr::QuadRefinement(parentLevel, childLevel, _subdivOptions); } else { refinement = new Vtr::TriRefinement(parentLevel, childLevel, _subdivOptions); } // // Initialize a Selector to mark a sparse set of components for refinement. If // nothing was selected, discard the new refinement and child level, trim the // maximum level and stop refinining any further. Otherwise, refine and append // the new refinement and child. // // Note that if we support the "full topology at last level" option properly, // we should prune the previous level generated, as it is now the last... // Vtr::SparseSelector selector(*refinement); selectFeatureAdaptiveComponents(selector); if (selector.isSelectionEmpty()) { _maxLevel = i - 1; delete refinement; delete &childLevel; break; } refinement->refine(refineOptions); _levels.push_back(&childLevel); _refinements.push_back(refinement); //childLevel.print(refinement); //assert(childLevel.validateTopology()); } } // // Method for selecting components for sparse refinement based on the feature-adaptive needs // of patch generation. // // It assumes we have a freshly initialized Vtr::SparseSelector (i.e. nothing already selected) // and will select all relevant topological features for inclusion in the subsequent sparse // refinement. // // This was originally written specific to the quad-centric Catmark scheme and was since // generalized to support Loop given the enhanced tagging of components based on the scheme. // Any further enhancements here, e.g. new approaches for dealing with infinitely sharp // creases, should be aware of the intended generality. Ultimately it may not be worth // trying to keep this general and we will be better off specializing it for each scheme. // The fact that this method is intimately tied to patch generation also begs for it to // become part of a class that encompasses both the feature adaptive tagging and the // identification of the intended patch that result from it. // void TopologyRefiner::selectFeatureAdaptiveComponents(Vtr::SparseSelector& selector) { Vtr::Level const& level = selector.getRefinement().parent(); int regularFaceSize = selector.getRefinement()._regFaceSize; bool considerSingleCreasePatch = _useSingleCreasePatch && (regularFaceSize == 4); for (Vtr::Index face = 0; face < level.getNumFaces(); ++face) { if (level.isHole(face)) { continue; } Vtr::ConstIndexArray faceVerts = level.getFaceVertices(face); // // Testing irregular faces is only necessary at level 0, and potentially warrants // separating out as the caller can detect these: // if (faceVerts.size() != regularFaceSize) { // // We need to also ensure that all adjacent faces to this are selected, so we // select every face incident every vertex of the face. This is the only place // where other faces are selected as a side effect and somewhat undermines the // whole intent of the per-face traversal. // Vtr::ConstIndexArray fVerts = level.getFaceVertices(face); for (int i = 0; i < fVerts.size(); ++i) { ConstIndexArray fVertFaces = level.getVertexFaces(fVerts[i]); for (int j = 0; j < fVertFaces.size(); ++j) { selector.selectFace(fVertFaces[j]); } } continue; } // // Combine the tags for all vertices of the face and quickly accept/reject based on // the presence/absence of properties where we can (further inspection is likely to // be necessary in some cases, particularly when we start trying to be clever about // minimizing refinement for inf-sharp creases, etc.): // Vtr::Level::VTag compFaceVTag = level.getFaceCompositeVTag(faceVerts); if (compFaceVTag._incomplete) { continue; } bool selectFace = false; if (compFaceVTag._xordinary) { selectFace = true; } else if (compFaceVTag._nonManifold) { // Warrants further inspection in future -- isolate for now // - will want to defer inf-sharp treatment to below selectFace = true; } else if (compFaceVTag._rule == Sdc::Crease::RULE_SMOOTH) { // Avoid isolation when ALL vertices are Smooth. All vertices must be regular by // now and all vertices Smooth implies they are all interior vertices. (If any // adjacent faces are not regular, this face will have been previously selected). selectFace = false; } else if (compFaceVTag._rule & Sdc::Crease::RULE_DART) { // Any occurrence of a Dart vertex requires isolation selectFace = true; } else if (not (compFaceVTag._rule & Sdc::Crease::RULE_SMOOTH)) { // None of the vertices is Smooth, so we have all vertices either Crease or Corner. // Though some may be regular patches, this currently warrants isolation as we only // support regular patches with one corner or one boundary, i.e. with one or more // smooth interior vertices. selectFace = true; } else if (compFaceVTag._semiSharp) { // Any semi-sharp feature at or around the vertex warrants isolation -- unless we // optimize for the single-crease patch, i.e. only edge sharpness of a constant value // along the entire regular patch boundary (quickly exclude the Corner case first): if (considerSingleCreasePatch && not (compFaceVTag._rule & Sdc::Crease::RULE_CORNER)) { selectFace = not level.isSingleCreasePatch(face); } else { selectFace = true; } } else if (not compFaceVTag._boundary) { // At this point we are left with a mix of smooth and inf-sharp features. If not // on a boundary, the interior inf-sharp features need isolation -- unless we are // again optimizing for the single-crease patch, infinitely sharp in this case. // // Note this case of detecting a single-crease patch, while similar to the above, // is kept separate for the inf-sharp case: a separate and much more efficient // test can be made for the inf-sharp case, and there are other opportunities here // to optimize for regular patches at infinitely sharp corners. if (considerSingleCreasePatch && not (compFaceVTag._rule & Sdc::Crease::RULE_CORNER)) { selectFace = not level.isSingleCreasePatch(face); } else { selectFace = true; } } else if (not (compFaceVTag._rule & Sdc::Crease::RULE_CORNER)) { // We are now left with boundary faces -- if no Corner vertex, we have a mix of both // regular Smooth and Crease vertices on a boundary face, which can only be a regular // boundary patch, so don't isolate. selectFace = false; } else { // This is the last case with at least one Corner (infinitely-sharp) vertex and one // Smooth (interior) vertex. Distinguish the regular corner case from others -- this // is where the _corner tag on the vertex would help but we still need ensure that no // vertex other than the corner is sharp, and so inspection of each is unavoidable... uint boundaryCount = level._vertTags[faceVerts[0]]._boundary; uint infSharpCount = level._vertTags[faceVerts[0]]._infSharp; for (int i = 1; i < faceVerts.size(); ++i) { boundaryCount += level._vertTags[faceVerts[i]]._boundary; infSharpCount += level._vertTags[faceVerts[i]]._infSharp; } selectFace = (boundaryCount != 3) || (infSharpCount != 1); } if (selectFace) { selector.selectFace(face); } } } } // end namespace Far } // end namespace OPENSUBDIV_VERSION } // end namespace OpenSubdiv