fix bug end iterator++

This commit is contained in:
Paolo Cignoni 2006-01-19 14:18:08 +00:00
parent 56dbb91da2
commit f8400e07c4
1 changed files with 156 additions and 149 deletions

View File

@ -21,9 +21,12 @@
* * * *
****************************************************************************/ ****************************************************************************/
/**************************************************************************** /****************************************************************************
History History
$Log: not supported by cvs2svn $ $Log: not supported by cvs2svn $
Revision 1.6 2005/05/30 09:43:41 spinelli
vertexIterator sostituito con VertexIterator
Revision 1.5 2005/05/17 21:14:56 ganovelli Revision 1.5 2005/05/17 21:14:56 ganovelli
some typecast (crs4) some typecast (crs4)
@ -44,33 +47,33 @@ name of adhacency function updated
#define __VCGLIB_EDGEALLOCATOR #define __VCGLIB_EDGEALLOCATOR
namespace vcg { namespace vcg {
namespace edge { namespace edge {
/** \addtogroup edgemesh */ /** \addtogroup edgemesh */
/*@{*/ /*@{*/
/// Class to safely add vertexes and faces to a mesh updating all the involved pointers. /// 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 edgemesh. /// It provides static memeber to add either vertex or faces to a edgemesh.
template <class AllocateMeshType> template <class AllocateMeshType>
class Allocator class Allocator
{ {
public: public:
typedef AllocateMeshType MeshType; typedef AllocateMeshType MeshType;
typedef typename MeshType::VertexType VertexType; typedef typename MeshType::VertexType VertexType;
typedef typename MeshType::VertexPointer VertexPointer; typedef typename MeshType::VertexPointer VertexPointer;
typedef typename MeshType::VertexIterator VertexIterator; typedef typename MeshType::VertexIterator VertexIterator;
typedef typename MeshType::EdgeType EdgeType; typedef typename MeshType::EdgeType EdgeType;
typedef typename MeshType::EdgePointer EdgePointer; typedef typename MeshType::EdgePointer EdgePointer;
typedef typename MeshType::EdgeIterator EdgeIterator; typedef typename MeshType::EdgeIterator EdgeIterator;
/** This class is used when allocating new vertexes and faces to update /** This class is used when allocating new vertexes and faces to update
the pointers that can be changed when resizing the involved vectors of vertex or faces. the pointers that can be changed when resizing the involved vectors of vertex or faces.
It can also be used to prevent any update of the various mesh fields It can also be used to prevent any update of the various mesh fields
(e.g. in case you are building all the connections by hand as in a importer); (e.g. in case you are building all the connections by hand as in a importer);
*/ */
template<class SimplexPointerType> template<class SimplexPointerType>
class PointerUpdater class PointerUpdater
{ {
public: public:
void Clear(){newBase=oldBase=newEnd=oldEnd=0;preventUpdateFlag=false;}; void Clear(){newBase=oldBase=newEnd=oldEnd=0;preventUpdateFlag=false;};
void Update(SimplexPointerType &vp) void Update(SimplexPointerType &vp)
{ {
@ -83,22 +86,23 @@ public:
SimplexPointerType newEnd; SimplexPointerType newEnd;
SimplexPointerType oldEnd; SimplexPointerType oldEnd;
bool preventUpdateFlag; /// when true no update is considered necessary. bool preventUpdateFlag; /// when true no update is considered necessary.
}; };
/** Function to safely add n vertices to a mesh. /** Function to safely add n vertices to a mesh.
@param m The mesh to be expanded @param m The mesh to be expanded
@param n the number of vertexes to be added @param n the number of vertexes to be added
@param pu A PointerUpdater that stores the relocation that can be happened. @param pu A PointerUpdater that stores the relocation that can be happened.
*/ */
static VertexIterator AddVertices(MeshType &m,int n, PointerUpdater<VertexPointer> &pu) static VertexIterator AddVertices(MeshType &m,int n, PointerUpdater<VertexPointer> &pu)
{ {
VertexIterator last=m.vert.end(); VertexIterator last=m.vert.end();
pu.Clear(); pu.Clear();
if(m.vert.empty()) pu.oldBase=0; // if the vector is empty we cannot find the last valid element if(m.vert.empty()) pu.oldBase=0; // if the vector is empty we cannot find the last valid element
else pu.oldBase=&*m.vert.begin(); else pu.oldBase=&*m.vert.begin();
for(int i=0; i<n; ++i) for(int i=0; i<n; ++i)
{ {
m.vert.push_back(MeshType::VertexType()); m.vert.push_back(MeshType::VertexType());
@ -117,53 +121,55 @@ static VertexIterator AddVertices(MeshType &m,int n, PointerUpdater<VertexPointe
pu.Update((*ei).V(0)); pu.Update((*ei).V(0));
pu.Update((*ei).V(1)); pu.Update((*ei).V(1));
} }
}
// e poiche' lo spazio e' cambiato si ricalcola anche last da zero // e poiche' lo spazio e' cambiato si ricalcola anche last da zero
unsigned int siz=m.vert.size()-n; unsigned int siz=(unsigned int)m.vert.size()-n;
if(last!=(VertexIterator)0) //if(last!=(VertexIterator)0)
{ //{
last = m.vert.begin(); last = m.vert.begin();
advance(last,siz); advance(last,siz);
} //}
else last=m.vert.begin(); //else last=m.vert.begin();
}
return last;// deve restituire l'iteratore alla prima faccia aggiunta; return last;// deve restituire l'iteratore alla prima faccia aggiunta;
} }
static VertexIterator AddVertices(MeshType &m, int n) static VertexIterator AddVertices(MeshType &m, int n)
{ {
PointerUpdater<VertexPointer> pu; PointerUpdater<VertexPointer> pu;
return AddVertices(m, n,pu); return AddVertices(m, n,pu);
} }
/** Function to add n faces to the mesh. /** Function to add n faces to the mesh.
@param n Il numero di facce che si vuole aggiungere alla mesh @param n Il numero di facce che si vuole aggiungere alla mesh
*/ */
static EdgeIterator AddEdges(MeshType &m, int n) static EdgeIterator AddEdges(MeshType &m, int n)
{ {
PointerUpdater<EdgePointer> pu; PointerUpdater<EdgePointer> pu;
return AddEdges(m,n,pu); return AddEdges(m,n,pu);
} }
/** Function to add n faces to the mesh. /** Function to add n faces to the mesh.
NOTA: Aggiorna fn; NOTA: Aggiorna fn;
*/ */
static EdgeIterator AddEdges(MeshType &m, int n, PointerUpdater<EdgePointer> &pu) static EdgeIterator AddEdges(MeshType &m, int n, PointerUpdater<EdgePointer> &pu)
{ {
EdgeIterator last=(EdgeIterator)0; EdgeIterator last=m.edges.end();
pu.Clear(); pu.Clear();
if(m.edges.empty()) { if(m.edges.empty()) {
pu.oldBase=0; // if the vector is empty we cannot find the last valid element pu.oldBase=0; // if the vector is empty we cannot find the last valid element
last=0; //last=0;
} else { } else {
pu.oldBase=&*m.edges.begin(); pu.oldBase=&*m.edges.begin();
last=m.edges.end(); last=m.edges.end();
} }
for(int i=0; i<n; ++i)
m.edges.resize(m.edges.size()+n);
/*for(int i=0; i<n; ++i)
{ {
m.edges.push_back(MeshType::EdgeType()); m.edges.push_back(MeshType::EdgeType());
m.edges.back().ClearFlags(); m.edges.back().ClearFlags();
} }*/
m.en+=n; m.en+=n;
@ -188,22 +194,23 @@ static EdgeIterator AddEdges(MeshType &m, int n, PointerUpdater<EdgePointer> &pu
if(VertexType::HasVEAdjacency()) if(VertexType::HasVEAdjacency())
pu.Update((*vi).Ep()); pu.Update((*vi).Ep());
} }
}
// e poiche' lo spazio e' cambiato si ricalcola anche last da zero // e poiche' lo spazio e' cambiato si ricalcola anche last da zero
unsigned int siz=m.edges.size()-n; unsigned int siz=(unsigned int)m.edges.size()-(unsigned int)n;
if(last!=(EdgeIterator)0) //if(last!=(EdgeIterator)0)
{ // {
last = m.edges.begin(); last = m.edges.begin();
advance(last,siz); advance(last,siz);
} // }
else last=m.edges.begin(); //else last=m.edges.begin();
}
return last; return last;
} }
}; // end class }; // end class
/*@}*/ /*@}*/
} // End Namespace TriMesh } // End Namespace TriMesh
} // End Namespace vcg } // End Namespace vcg
#endif #endif