From 39f12e9fddcfbb96226e342ef33c9f60e436fb0c Mon Sep 17 00:00:00 2001 From: cignoni Date: Mon, 7 Dec 2009 09:05:20 +0000 Subject: [PATCH] added a function to permutate the vertex vector according to a given permutation. --- vcg/complex/trimesh/allocate.h | 72 +++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 28 deletions(-) diff --git a/vcg/complex/trimesh/allocate.h b/vcg/complex/trimesh/allocate.h index 4b6b111a..90871a69 100644 --- a/vcg/complex/trimesh/allocate.h +++ b/vcg/complex/trimesh/allocate.h @@ -200,14 +200,14 @@ namespace vcg { {} template - void ReorderAttribute(ATTR_CONT &c,std::vector & newVertIndex, MeshType &m){ + void ReorderAttribute(ATTR_CONT &c,std::vector & newVertIndex, MeshType & /* m */){ typename std::set::iterator ai; for(ai = c.begin(); ai != c.end(); ++ai) ((typename MeshType::PointerToAttribute)(*ai)).Reorder(newVertIndex); } 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(m.vn); @@ -567,6 +567,47 @@ namespace vcg { e.SetD(); --m.en; } + + /* + Function to rearrange the vertex vector according to a given index permutation + the permutation is vector such that after calling this function + + m.vert[ newVertIndex[i] ] = m.vert[i]; + + e.g. newVertIndex[i] is the new index of the vertex i + + */ + static void PermutateVertexVector(MeshType &m, std::vector &newVertIndex ) + { + for(unsigned int i=0;i(newVertIndex,m.vert); + + // reorder the optional atttributes in m.vert_attr to reflect the changes + ReorderAttribute(m.vert_attr,newVertIndex,m); + + m.vert.resize(m.vn); + + // 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]; + } + } /* Function to compact all the vertices that have been deleted and put them to the end of the vector. @@ -589,37 +630,12 @@ namespace vcg { { if(!m.vert[i].IsD()) { - if(pos!=i) - m.vert[pos]=m.vert[i]; newVertIndex[i]=pos; ++pos; } } assert((int)pos==m.vn); - - // 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) - ReorderVert(newVertIndex,m.vert); - - // reorder the optional atttributes in m.vert_attr to reflect the changes - ReorderAttribute(m.vert_attr,newVertIndex,m); - - m.vert.resize(m.vn); - - // 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(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]; - } - + PermutateVertexVector(m,newVertIndex); } /*