// // 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::Type 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(); } void TopologyRefiner::Clear() { _levels.clear(); _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 // template void computePtexIndices(Vtr::Level const & coarseLevel, std::vector & ptexIndices) { int nfaces = coarseLevel.getNumFaces(); ptexIndices.resize(nfaces+1); int ptexID=0; for (int i = 0; i < nfaces; ++i) { ptexIndices[i] = ptexID; Vtr::IndexArray fverts = coarseLevel.getFaceVertices(i); ptexID += fverts.size()==Sdc::TypeTraits::RegularFaceValence() ? 1 : fverts.size(); } // last entry contains the number of ptex texture faces ptexIndices[nfaces]=ptexID; } void TopologyRefiner::initializePtexIndices() const { std::vector & indices = const_cast &>(_ptexIndices); switch (GetSchemeType()) { case Sdc::TYPE_BILINEAR: computePtexIndices(getLevel(0), indices); break; case Sdc::TYPE_CATMARK : computePtexIndices(getLevel(0), indices); break; case Sdc::TYPE_LOOP : computePtexIndices(getLevel(0), indices); break; } } int TopologyRefiner::GetNumPtexFaces() const { if (_ptexIndices.empty()) { initializePtexIndices(); } // see computePtexIndices() 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) { IndexArray 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::TYPE_CATMARK); if (_ptexIndices.empty()) { initializePtexIndices(); } Vtr::Level const & level = getLevel(0); IndexArray fedges = level.getFaceEdges(face); if (fedges.size()==4) { // Regular ptex quad face for (int i=0; i<4; ++i) { int edge = fedges[i]; IndexArray efaces = level.getEdgeFaces(edge); Index adjface = getAdjacentFace(level, edge, face); if (adjface==-1) { adjFaces[i] = -1; // boundary or non-manifold adjEdges[i] = 0; } else { IndexArray 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 = maxLevel; Sdc::Split splitType = (_subdivType == Sdc::TYPE_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 <= maxLevel; ++i) { refineOptions._faceTopologyOnly = fullTopology ? false : (i == maxLevel); 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(int subdivLevel, bool fullTopology, bool useSingleCreasePatch) { 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 = subdivLevel; _useSingleCreasePatch = useSingleCreasePatch; // // Initialize refinement options for Vtr: // Vtr::Refinement::Options refineOptions; refineOptions._sparse = true; refineOptions._faceTopologyOnly = !fullTopology; Sdc::Split splitType = (_subdivType == Sdc::TYPE_LOOP) ? Sdc::SPLIT_TO_TRIS : Sdc::SPLIT_TO_QUADS; for (int i = 1; i <= subdivLevel; ++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::IndexArray const 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::IndexArray const fVerts = level.getFaceVertices(face); for (int i = 0; i < fVerts.size(); ++i) { IndexArray const 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 compFaceTag = level.getFaceCompositeVTag(faceVerts); if (compFaceTag._incomplete) { continue; } bool selectFace = false; if (compFaceTag._xordinary) { selectFace = true; } else if (compFaceTag._rule & Sdc::Crease::RULE_DART) { // Get this case out of the way before testing hard features selectFace = true; } else if (compFaceTag._nonManifold) { // Warrants further inspection -- isolate for now // - will want to defer inf-sharp treatment to below selectFace = true; } else if (!(compFaceTag._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. selectFace = true; } else if (compFaceTag._semiSharp) { // if this is regular and the adjacent edges have same sharpness // and no vertex corner sharpness, // we can stop refinning and use single-crease patch. if (considerSingleCreasePatch) { selectFace = ! level.isSingleCreasePatch(face); } else { selectFace = true; } } else { // This leaves us with at least one Smooth vertex (and so two smooth adjacent edges // of the quad) and the rest hard Creases or Corners. This includes the regular // corner and boundary cases that we don't want to isolate, but leaves a few others // that do warrant isolation -- needing further inspection. // // For now go with the boundary cases and don't isolate... selectFace = false; } if (selectFace) { selector.selectFace(face); } } } #ifdef _VTR_COMPUTE_MASK_WEIGHTS_ENABLED void TopologyRefiner::ComputeMaskWeights() { assert(_subdivType == Sdc::TYPE_CATMARK); for (int i = 0; i < _maxLevel; ++i) { _refinements[i]->computeMaskWeights(); } } #endif } // end namespace Far } // end namespace OPENSUBDIV_VERSION } // end namespace OpenSubdiv