From 7d6a210e75cde151835d2f9b62f6f3044e84005b Mon Sep 17 00:00:00 2001 From: cignoni Date: Tue, 11 Dec 2007 11:36:03 +0000 Subject: [PATCH] Added the CompactVertexVector garbage collecting function. --- vcg/complex/trimesh/allocate.h | 50 ++++++++++++++++++++++- vcg/simplex/vertexplus/component_ocf.h | 55 ++++++++++++++++++++------ 2 files changed, 92 insertions(+), 13 deletions(-) diff --git a/vcg/complex/trimesh/allocate.h b/vcg/complex/trimesh/allocate.h index cfca05e8..72af86c7 100644 --- a/vcg/complex/trimesh/allocate.h +++ b/vcg/complex/trimesh/allocate.h @@ -24,6 +24,9 @@ History $Log: not supported by cvs2svn $ +Revision 1.37 2007/10/16 16:46:53 cignoni +Added Allocator::DeleteFace and Allocator::DeleteVertex; Now the use of SetD() should be deprecated. + Revision 1.36 2007/01/11 10:24:25 cignoni Added test in AddVertices to do not update un-initalized vert references (for newly allocated faces) @@ -153,6 +156,11 @@ Initial commit namespace vcg { namespace tri { /** \addtogroup trimesh */ + + template + void Reorder( vector &newVertIndex, vector_type &vert) + {} + /*@{*/ /// Class to safely add vertexes and faces to a mesh updating all the involved pointers. /// It provides static memeber to add either vertex or faces to a trimesh. @@ -356,8 +364,48 @@ namespace vcg { --m.vn; } + /* + 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 ) + { + // newVertIndex [ ] gives you the new position of the vertex in the vector; + vector newVertIndex(m.vert.size()); - }; // end class + size_t pos=0; + size_t i=0; + + for(i=0;i(newVertIndex,m.vert); + m.vert.resize(m.vn); + 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(oldIndex >=0 && oldIndex < newVertIndex.size()); + (*fi).V(i) = vbase+newVertIndex[oldIndex]; + } + + } +}; // end class + + /*@}*/ } // End Namespace TriMesh } // End Namespace vcg diff --git a/vcg/simplex/vertexplus/component_ocf.h b/vcg/simplex/vertexplus/component_ocf.h index 8489a36c..219c6910 100644 --- a/vcg/simplex/vertexplus/component_ocf.h +++ b/vcg/simplex/vertexplus/component_ocf.h @@ -24,6 +24,9 @@ History $Log: not supported by cvs2svn $ +Revision 1.9 2006/12/11 23:42:00 ganovelli +bug Index()() instead of Index() + Revision 1.8 2006/12/04 11:17:42 ganovelli added forward declaration of TriMesh @@ -97,29 +100,52 @@ public: void pop_back(); void resize(const unsigned int & _size) { - int oldsize = BaseType::size(); - BaseType::resize(_size); - if(oldsize<_size){ - ThisTypeIterator firstnew = BaseType::begin(); - advance(firstnew,oldsize); - _updateOVP(firstnew,(*this).end()); - } - if(ColorEnabled) CV.resize(_size); + int oldsize = BaseType::size(); + BaseType::resize(_size); + if(oldsize<_size){ + ThisTypeIterator firstnew = BaseType::begin(); + advance(firstnew,oldsize); + _updateOVP(firstnew,(*this).end()); + } + if(ColorEnabled) CV.resize(_size); if(NormalEnabled) NV.resize(_size); } - void reserve(const unsigned int & _size) + + void reserve(const unsigned int & _size) { BaseType::reserve(_size); if (ColorEnabled) CV.reserve(_size); if (NormalEnabled) NV.reserve(_size); } - void _updateOVP(ThisTypeIterator lbegin, ThisTypeIterator lend) -{ + void _updateOVP(ThisTypeIterator lbegin, ThisTypeIterator lend) + { ThisTypeIterator vi; for(vi=lbegin;vi!=lend;++vi) (*vi)._ovp=this; - } + } + +// this function is called by the specialized Reorder function, that is called whenever someone call the allocator::CompactVertVector +void Reorder(std::vector &newVertIndex ) +{ + size_t pos=0; + size_t i=0; + if(ColorEnabled) assert( CV.size() == newVertIndex.size() ); + if(NormalEnabled) assert( NV.size() == newVertIndex.size() ); + + for(i=0;i::max() ) + { + assert(newVertIndex[i] <= i); + if(ColorEnabled) CV[newVertIndex[i]] = CV[i]; + if(NormalEnabled) NV[newVertIndex[i]] = NV[i];; + } + } +} + + + //////////////////////////////////////// // Enabling Eunctions @@ -295,6 +321,11 @@ namespace tri else return VertexType::HasQuality(); } + template < class VertexType > + void Reorder( std::vector &newVertIndex, vert::vector_ocf< VertexType > &vertVec) + { + vertVec.Reorder(newVertIndex); + } } }// end namespace vcg #endif