/**************************************************************************** * VCGLib o o * * Visual and Computer Graphics Library o o * * _ O _ * * Copyright(C) 2004-2016 \/)\/ * * Visual Computing Lab /\/| * * ISTI - Italian National Research Council | * * \ * * All rights reserved. * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * * for more details. * * * ****************************************************************************/ #ifndef CUT_TREE_H #define CUT_TREE_H #include #include #include #include namespace vcg { namespace tri { template class CutTree { public: typedef typename MeshType::ScalarType ScalarType; typedef typename MeshType::CoordType CoordType; typedef typename MeshType::VertexType VertexType; typedef typename MeshType::VertexPointer VertexPointer; typedef typename MeshType::VertexIterator VertexIterator; typedef typename MeshType::EdgeIterator EdgeIterator; typedef typename MeshType::EdgeType EdgeType; typedef typename MeshType::FaceType FaceType; typedef typename MeshType::FacePointer FacePointer; typedef typename MeshType::FaceIterator FaceIterator; typedef Box3 Box3Type; typedef typename face::Pos PosType; typedef typename tri::UpdateTopology::PEdge PEdge; MeshType &base; CutTree(MeshType &_m) :base(_m){} // Perform a simple optimization of the three applying simple shortcuts: // if the endpoints of two consecutive edges are connected by an edge existing on base mesh just use that edges void OptimizeTree(KdTree &kdtree, MeshType &t) { tri::Allocator::CompactEveryVector(t); int lastEn=t.en; do { lastEn=t.en; tri::UpdateTopology::VertexEdge(t); // First simple loop that search for 2->1 moves. for(VertexIterator vi=t.vert.begin();vi!=t.vert.end();++vi) { std::vector starVec; edge::VVStarVE(&*vi,starVec); if(starVec.size()==2) // middle vertex has to be 1-manifold { PosType pos; if(ExistEdge(kdtree,starVec[0]->P(),starVec[1]->P(),pos)) edge::VEEdgeCollapse(t,&*vi); } } tri::Allocator::CompactEveryVector(t); } while(t.en &kdtree, CoordType &p0, CoordType &p1, PosType &fpos) { ScalarType locEps = SquaredDistance(p0,p1)/100000.0; VertexType *v0=0,*v1=0; unsigned int veInd; ScalarType sqdist; kdtree.doQueryClosest(p0,veInd,sqdist); if(sqdistVFp(),v0); assert(fpos.V()==v0); PosType startPos=fpos; do { fpos.FlipE(); fpos.FlipF(); if(fpos.VFlip()== v1) return true; } while(startPos!=fpos); } return false; } int findNonVisitedEdgesDuringRetract(VertexType * vp, EdgeType * &ep) { std::vector starVec; edge::VEStarVE(&*vp,starVec); int cnt =0; for(size_t i=0;iIsV()) { cnt++; ep = starVec[i]; } } return cnt; } bool IsBoundaryVertexOnBase(KdTree &kdtree, const CoordType &p) { VertexType *v0=0; unsigned int veInd; ScalarType sqdist; kdtree.doQueryClosest(p,veInd,sqdist); if(sqdist>0) { assert(0); } v0 = &base.vert[veInd]; return v0->IsB(); } /** * @brief Retract * @param t the edgemesh containing the visit tree. * */ void Retract(KdTree &kdtree, MeshType &t) { printf("Retracting a tree of %i edges and %i vertices\n",t.en,t.vn); tri::UpdateTopology::VertexEdge(t); tri::Allocator::CompactEveryVector(t); std::stack vertStack; // Put on the stack all the vertex with just a single incident edge. ForEachVertex(t, [&](VertexType &v){ if(edge::VEDegree(&v) ==1) vertStack.push(&v); }); tri::UpdateFlags::EdgeClearV(t); tri::UpdateFlags::VertexClearV(t); int unvisitedEdgeNum = t.en; while((!vertStack.empty()) && (unvisitedEdgeNum > 2) ) { VertexType *vp = vertStack.top(); vertStack.pop(); vp->C()=Color4b::Blue; EdgeType *ep=0; int eCnt = findNonVisitedEdgesDuringRetract(vp,ep); if(eCnt==1) // We have only one non visited edge over vp { assert(!ep->IsV()); ep->SetV(); --unvisitedEdgeNum; VertexType *otherVertP; if(ep->V(0)==vp) otherVertP = ep->V(1); else otherVertP = ep->V(0); vertStack.push(otherVertP); } } assert(unvisitedEdgeNum >0); for(size_t i =0; i::DeleteEdge(t,t.edge[i]) ; } assert(t.en >0); tri::Clean::RemoveUnreferencedVertex(t); tri::Allocator::CompactEveryVector(t); } /** \brief Main function * * It builds a cut tree that open the mesh into a topological disk * * */ void Build(MeshType &dualMesh, int startingFaceInd=0) { tri::UpdateTopology::FaceFace(base); tri::UpdateTopology::VertexFace(base); BuildVisitTree(dualMesh,startingFaceInd); // BuildDijkstraVisitTree(dualMesh,startingFaceInd); VertexConstDataWrapper vdw(base); KdTree kdtree(vdw); Retract(kdtree,dualMesh); OptimizeTree(kdtree, dualMesh); tri::UpdateBounding::Box(dualMesh); } /* Auxiliary class for keeping the heap of vertices to visit and their estimated distance */ struct FaceDist{ FaceDist(FacePointer _f):f(_f),dist(_f->Q()){} FacePointer f; ScalarType dist; bool operator < (const FaceDist &o) const { if( dist != o.dist) return dist > o.dist; return f::max()) { tri::RequireFFAdjacency(base); tri::RequirePerFaceMark(base); tri::RequirePerFaceQuality(base); typename MeshType::template PerFaceAttributeHandle parentHandle = tri::Allocator::template GetPerFaceAttribute(base, "parent"); std::vector seedVec; seedVec.push_back(&base.face[startingFaceInd]); std::vector Heap; tri::UnMarkAll(base); tri::UpdateQuality::FaceConstant(base,0); ForEachVertex(base, [&](VertexType &v){ tri::Allocator::AddVertex(dualMesh,v.cP()); }); // Initialize the face heap; // All faces in the heap are already marked; Q() store the distance from the source faces; for(size_t i=0;iQ()=0; Heap.push_back(FaceDist(seedVec[i])); } // Main Loop int boundary=0; std::make_heap(Heap.begin(),Heap.end()); int vCnt=0; int eCnt=0; int fCnt=0; // The main idea is that in the heap we maintain all the faces to be visited. int nonDiskCnt=0; while(!Heap.empty() && nonDiskCnt<10) { int eulerChi= vCnt-eCnt+fCnt; if(eulerChi==1) nonDiskCnt=0; else ++nonDiskCnt; // printf("HeapSize %i: %i - %i + %i = %i\n",Heap.size(), vCnt,eCnt,fCnt,eulerChi); pop_heap(Heap.begin(),Heap.end()); FacePointer currFp = (Heap.back()).f; if(tri::IsMarked(base,currFp)) { // printf("Found an already visited face %f %f \n",Heap.back().dist, Heap.back().f->Q()); //assert(Heap.back().dist != currFp->Q()); Heap.pop_back(); continue; } Heap.pop_back(); ++fCnt; eCnt+=3; tri::Mark(base,currFp); // printf("pop face %i \n", tri::Index(base,currFp)); for(int i=0;i<3;++i) { if(!currFp->V(i)->IsV()) {++vCnt; currFp->V(i)->SetV();} FacePointer nextFp = currFp->FFp(i); if( tri::IsMarked(base,nextFp) ) { eCnt-=1; printf("is marked\n"); if(nextFp != parentHandle[currFp] ) { if(currFp>nextFp){ tri::Allocator::AddEdge(dualMesh,tri::Index(base,currFp->V0(i)), tri::Index(base,currFp->V1(i))); } } } else // add it to the heap; { // printf("is NOT marked\n"); parentHandle[nextFp] = currFp; ScalarType nextDist = currFp->Q() + Distance(Barycenter(*currFp),Barycenter(*nextFp)); int adjMarkedNum=0; for(int k=0;k<3;++k) if(tri::IsMarked(base,nextFp->FFp(k))) ++adjMarkedNum; if(nextDist < maxDistanceThr || adjMarkedNum>1) { nextFp->Q() = nextDist; Heap.push_back(FaceDist(nextFp)); push_heap(Heap.begin(),Heap.end()); } else { // printf("boundary %i\n",++boundary); tri::Allocator::AddEdge(dualMesh,tri::Index(base,currFp->V0(i)), tri::Index(base,currFp->V1(i))); } } } } // End while printf("fulltree %i vn %i en \n",dualMesh.vn, dualMesh.en); int dupVert=tri::Clean::RemoveDuplicateVertex(dualMesh,false); printf("Removed %i dup vert\n",dupVert); int dupEdge=tri::Clean::RemoveDuplicateEdge(dualMesh); printf("Removed %i dup edges %i\n",dupEdge,dualMesh.EN()); tri::Clean::RemoveUnreferencedVertex(dualMesh); tri::io::ExporterPLY::Save(dualMesh,"fulltree.ply",tri::io::Mask::IOM_EDGEINDEX); tri::UpdateColor::PerFaceQualityRamp(base); tri::io::ExporterPLY::Save(base,"colored_Bydistance.ply",tri::io::Mask::IOM_FACECOLOR); } // \brief This function build a cut tree. // // First we make a bread first FF face visit. // Each time that we encounter a visited face we add to the tree the edge // that brings to the already visited face. // this structure build a dense graph and we retract this graph retracting each // leaf until we remains with just the loops that cuts the object. void BuildVisitTree(MeshType &dualMesh, int startingFaceInd=0) { tri::UpdateFlags::FaceClearV(base); tri::UpdateFlags::VertexBorderFromFaceAdj(base); std::vector > visitStack; // the stack contain the pos on the 'starting' face. base.face[startingFaceInd].SetV(); for(int i=0;i<3;++i) visitStack.push_back(PosType(&(base.face[startingFaceInd]),i,base.face[startingFaceInd].V(i))); int cnt=1; while(!visitStack.empty()) { std::swap(visitStack.back(),visitStack[rand()%visitStack.size()]); PosType c = visitStack.back(); visitStack.pop_back(); assert(c.F()->IsV()); c.F()->C() = Color4b::ColorRamp(0,base.fn,cnt); c.FlipF(); if(!c.F()->IsV()) { ++cnt; c.F()->SetV(); c.FlipE();c.FlipV(); visitStack.push_back(c); c.FlipE();c.FlipV(); visitStack.push_back(c); } else { tri::Allocator::AddEdge(dualMesh,c.V()->P(),c.VFlip()->P()); } } assert(cnt==base.fn); tri::Clean::RemoveDuplicateVertex(dualMesh); tri::io::ExporterPLY::Save(dualMesh,"fulltree.ply",tri::io::Mask::IOM_EDGEINDEX); } }; } // end namespace tri } // end namespace vcg #endif // CUT_TREE_H