From 766b1b3f8929079fa88b68314cde731f079b175c Mon Sep 17 00:00:00 2001 From: ganovelli Date: Fri, 22 Oct 2010 16:01:58 +0000 Subject: [PATCH] CompactFaceVector and CompactVertexVector now may return a PointerUpdater --- vcg/complex/trimesh/allocate.h | 108 ++++++++++++++++++++++++--------- 1 file changed, 78 insertions(+), 30 deletions(-) diff --git a/vcg/complex/trimesh/allocate.h b/vcg/complex/trimesh/allocate.h index c6604899..3aa51b9f 100644 --- a/vcg/complex/trimesh/allocate.h +++ b/vcg/complex/trimesh/allocate.h @@ -73,7 +73,7 @@ namespace vcg { } template - void ResizeAttribute(ATTR_CONT &c,const int & sz , MeshType & /* m */){ + void ResizeAttribute(ATTR_CONT &c,const int & sz , MeshType &m){ typename std::set::iterator ai; for(ai =c.begin(); ai != c.end(); ++ai) ((typename MeshType::PointerToAttribute)(*ai)).Resize(sz); @@ -127,17 +127,21 @@ namespace vcg { void Clear(){newBase=oldBase=newEnd=oldEnd=0;}; void Update(SimplexPointerType &vp) { - if(vp>=newBase && vp=newBase && vp=oldBase); assert(vp remap; + bool preventUpdateFlag; /// when true no update is considered necessary. }; @@ -578,60 +582,71 @@ namespace vcg { e.g. newVertIndex[i] is the new index of the vertex i */ - static void PermutateVertexVector(MeshType &m, std::vector &newVertIndex ) + static void PermutateVertexVector(MeshType &m, PointerUpdater &pu) { for(unsigned int i=0;i(newVertIndex,m.vert); + ReorderVert(pu.remap,m.vert); // reorder the optional atttributes in m.vert_attr to reflect the changes - ReorderAttribute(m.vert_attr,newVertIndex,m); + ReorderAttribute(m.vert_attr,pu.remap,m); + // setup the pointer updater + pu.oldBase = &m.vert[0]; + pu.oldEnd = &m.vert.back()+1; + + // resize m.vert.resize(m.vn); + + // setup the pointer updater + pu.newBase = (m.vert.empty())?0:&m.vert[0]; + pu.newEnd = (m.vert.empty())?0:&m.vert.back()+1; + // resize the optional atttributes in m.vert_attr to reflect the changes ResizeAttribute(m.vert_attr,m.vn,m); FaceIterator fi; - VertexPointer vbase=&m.vert[0]; for(fi=m.face.begin();fi!=m.face.end();++fi) if(!(*fi).IsD()) for(unsigned int i=0;i<3;++i) { - size_t oldIndex = (*fi).V(i) - vbase; - assert(vbase <= (*fi).V(i) && oldIndex < newVertIndex.size()); - (*fi).V(i) = vbase+newVertIndex[oldIndex]; + size_t oldIndex = (*fi).V(i) - pu.oldBase; + assert(pu.oldBase <= (*fi).V(i) && oldIndex < pu.remap.size()); + (*fi).V(i) = pu.newBase+pu.remap[oldIndex]; } + } - + + /* Function to compact all the vertices that have been deleted and put them to the end of the vector. after this pass the isD test in the scanning of vertex vector, is no more strongly necessary. It should not be called when TemporaryData is active; */ - static void CompactVertexVector( MeshType &m ) + static void CompactVertexVector( MeshType &m, PointerUpdater &pu ) { // If already compacted fast return please! if(m.vn==(int)m.vert.size()) return; // newVertIndex [ ] gives you the new position of the vertex in the vector; - std::vector newVertIndex(m.vert.size(),std::numeric_limits::max() ); + pu.remap.resize( m.vert.size(),std::numeric_limits::max() ); size_t pos=0; size_t i=0; @@ -640,27 +655,39 @@ namespace vcg { { if(!m.vert[i].IsD()) { - newVertIndex[i]=pos; + pu.remap[i]=pos; ++pos; } } assert((int)pos==m.vn); - PermutateVertexVector(m,newVertIndex); + + PermutateVertexVector(m, pu); + } + /* + Function to compact all the verices that have been deleted and put them to the end of the vector. + Wrapper if not PointerUpdater is not wanted + */ + static void CompactVertexVector( MeshType &m ) { + PointerUpdater pu; + CompactVertexVector(m,pu); + } + + /* Function to compact all the vertices that have been deleted and put them to the end of the vector. after this pass the isD test in the scanning of vertex vector, is no more strongly necessary. It should not be called when TemporaryData is active; */ - static void CompactFaceVector( MeshType &m ) + static void CompactFaceVector( MeshType &m, PointerUpdater &pu ) { // If already compacted fast return please! if(m.fn==(int)m.face.size()) return; // newFaceIndex [ ] gives you the new position of the face in the vector; - std::vector newFaceIndex(m.face.size(),std::numeric_limits::max() ); + pu.remap.resize( m.face.size(),std::numeric_limits::max() ); size_t pos=0; size_t i=0; @@ -688,7 +715,7 @@ namespace vcg { m.face[pos].FFi(j) = m.face[i].cFFi(j); } } - newFaceIndex[i]=pos; + pu.remap[i]=pos; ++pos; } } @@ -696,10 +723,10 @@ namespace vcg { // call a templated reordering function that manage any additional data internally stored by the vector // for the default std::vector no work is needed (some work is typically needed for the OCF stuff) - ReorderFace(newFaceIndex,m.face); + ReorderFace(pu.remap,m.face); // reorder the optional atttributes in m.face_attr to reflect the changes - ReorderAttribute(m.face_attr,newFaceIndex,m); + ReorderAttribute(m.face_attr,pu.remap,m); // Loop on the vertices to correct VF relation VertexIterator vi; @@ -711,13 +738,20 @@ namespace vcg { if ((*vi).cVFp()!=0) { size_t oldIndex = (*vi).cVFp() - fbase; - assert(fbase <= (*vi).cVFp() && oldIndex < newFaceIndex.size()); - (*vi).VFp() = fbase+newFaceIndex[oldIndex]; + assert(fbase <= (*vi).cVFp() && oldIndex < pu.remap.size()); + (*vi).VFp() = fbase+pu.remap[oldIndex]; } } + // Loop on the faces to correct VF and FF relations + pu.oldBase = &m.face[0]; + pu.oldEnd = &m.face.back()+1; m.face.resize(m.fn); + pu.newBase = (m.face.empty())?0:&m.face[0]; + pu.newEnd = (m.face.empty())?0:&m.face.back()+1; + + // resize the optional atttributes in m.face_attr to reflect the changes ResizeAttribute(m.face_attr,m.fn,m); @@ -730,20 +764,34 @@ namespace vcg { if ((*fi).cVFp(i)!=0) { size_t oldIndex = (*fi).VFp(i) - fbase; - assert(fbase <= (*fi).VFp(i) && oldIndex < newFaceIndex.size()); - (*fi).VFp(i) = fbase+newFaceIndex[oldIndex]; + assert(fbase <= (*fi).VFp(i) && oldIndex < pu.remap.size()); + (*fi).VFp(i) = fbase+pu.remap[oldIndex]; } if(HasFFAdjacency(m)) for(i=0;i<3;++i) if ((*fi).cFFp(i)!=0) { size_t oldIndex = (*fi).FFp(i) - fbase; - assert(fbase <= (*fi).FFp(i) && oldIndex < newFaceIndex.size()); - (*fi).FFp(i) = fbase+newFaceIndex[oldIndex]; + assert(fbase <= (*fi).FFp(i) && oldIndex < pu.remap.size()); + (*fi).FFp(i) = fbase+pu.remap[oldIndex]; } } + + + } + /* + Function to compact all the face that have been deleted and put them to the end of the vector. + Wrapper if not PointerUpdater is wanted + */ + static void CompactFaceVector( MeshType &m ) { + PointerUpdater pu; + CompactFaceVector(m,pu); + } + + + public: /// Per Vertex Attributes