fix bug end iterator++

This commit is contained in:
Paolo Cignoni 2006-02-28 12:13:49 +00:00
parent 40e4a353bd
commit baad6db4b9
1 changed files with 253 additions and 243 deletions

View File

@ -21,9 +21,12 @@
* * * *
****************************************************************************/ ****************************************************************************/
/**************************************************************************** /****************************************************************************
History History
$Log: not supported by cvs2svn $ $Log: not supported by cvs2svn $
Revision 1.25 2005/11/10 15:37:58 cignoni
Removed flags clearing (now it should be in the constructor of face and vertex)
Revision 1.24 2005/10/13 09:32:11 cignoni Revision 1.24 2005/10/13 09:32:11 cignoni
Re-inserted the cFFp and cVFp access. If only the const version of the member function exists, the compiler will call it Re-inserted the cFFp and cVFp access. If only the const version of the member function exists, the compiler will call it
when a non-const object invokes that function when a non-const object invokes that function
@ -113,265 +116,272 @@ Initial commit
#include <vector> #include <vector>
namespace vcg { namespace vcg {
namespace tri { namespace tri {
/** \addtogroup trimesh */ /** \addtogroup trimesh */
/*@{*/ /*@{*/
/// 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 trimesh. /// It provides static memeber to add either vertex or faces to a trimesh.
template <class AllocateMeshType> template <class AllocateMeshType>
class Allocator class Allocator
{
public:
typedef AllocateMeshType MeshType;
typedef typename MeshType::VertexType VertexType;
typedef typename MeshType::VertexPointer VertexPointer;
typedef typename MeshType::VertexIterator VertexIterator;
typedef typename MeshType::FaceType FaceType;
typedef typename MeshType::FacePointer FacePointer;
typedef typename MeshType::FaceIterator FaceIterator;
/** 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.
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);
*/
template<class SimplexPointerType>
class PointerUpdater
{
public:
void Clear(){newBase=oldBase=newEnd=oldEnd=0;preventUpdateFlag=false;};
void Update(SimplexPointerType &vp)
{
vp=newBase+(vp-oldBase);
}
bool NeedUpdate() {if(newBase!=oldBase && !preventUpdateFlag) return true; else return false;}
SimplexPointerType oldBase;
SimplexPointerType newBase;
SimplexPointerType newEnd;
SimplexPointerType oldEnd;
bool preventUpdateFlag; /// when true no update is considered necessary.
};
/** Function to add n vertices to the mesh. The second parameter hold a vector of
pointers to pointer to elements of the mesh that should be updated after a
possible vector realloc.
@param n Il numero di vertici che si vuole aggiungere alla mesh.
@param local_var Vettore di variabili locali che rappresentano puntatori a vertici.
restituisce l'iteratore al primo elemento aggiunto.
*/
static VertexIterator AddVertices(MeshType &m,int n, PointerUpdater<VertexPointer> &pu)
{
VertexIterator last=m.vert.end();
pu.Clear();
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();
for(int i=0; i<n; ++i)
{
m.vert.push_back(VertexType());
// m.vert.back().ClearFlags(); // No more necessary, since 9/2005 flags are set to zero in the constuctor.
}
m.vn+=n;
pu.newBase = &*m.vert.begin();
if(pu.NeedUpdate())
{ {
FaceIterator fi;
for (fi=m.face.begin(); fi!=m.face.end(); ++fi)
if(!(*fi).IsD())
{
pu.Update((*fi).V(0));
pu.Update((*fi).V(1));
pu.Update((*fi).V(2));
}
// e poiche' lo spazio e' cambiato si ricalcola anche last da zero public:
unsigned int siz=(unsigned int) m.vert.size()-n; typedef AllocateMeshType MeshType;
typedef typename MeshType::VertexType VertexType;
typedef typename MeshType::VertexPointer VertexPointer;
typedef typename MeshType::VertexIterator VertexIterator;
typedef typename MeshType::FaceType FaceType;
typedef typename MeshType::FacePointer FacePointer;
typedef typename MeshType::FaceIterator FaceIterator;
last = m.vert.begin(); /** This class is used when allocating new vertexes and faces to update
advance(last,siz); 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
(e.g. in case you are building all the connections by hand as in a importer);
return last;// deve restituire l'iteratore alla prima faccia aggiunta; */
} template<class SimplexPointerType>
class PointerUpdater
{
public:
void Clear(){newBase=oldBase=newEnd=oldEnd=0;preventUpdateFlag=false;};
void Update(SimplexPointerType &vp)
{
vp=newBase+(vp-oldBase);
}
bool NeedUpdate() {if(newBase!=oldBase && !preventUpdateFlag) return true; else return false;}
static VertexIterator AddVertices(MeshType &m, int n) SimplexPointerType oldBase;
{ SimplexPointerType newBase;
PointerUpdater<VertexPointer> pu; SimplexPointerType newEnd;
return AddVertices(m, n,pu); SimplexPointerType oldEnd;
} bool preventUpdateFlag; /// when true no update is considered necessary.
};
/** Function to add n faces to the mesh. /** Function to add n vertices to the mesh. The second parameter hold a vector of
@param n Il numero di facce che si vuole aggiungere alla mesh pointers to pointer to elements of the mesh that should be updated after a
*/ possible vector realloc.
static FaceIterator AddFaces(MeshType &m, int n,std::vector<FacePointer *> &local_var) @param n Il numero di vertici che si vuole aggiungere alla mesh.
{ @param local_var Vettore di variabili locali che rappresentano puntatori a vertici.
PointerUpdater<FacePointer> pu; restituisce l'iteratore al primo elemento aggiunto.
return AddFaces(m,n,pu,local_var); */
} static VertexIterator AddVertices(MeshType &m,int n, PointerUpdater<VertexPointer> &pu)
/** Function to add n faces to the mesh. {
NOTA: Aggiorna fn; VertexIterator last;
*/ pu.Clear();
static FaceIterator AddFaces(MeshType &m, int n, PointerUpdater<FacePointer> &pu,std::vector<FacePointer *> &local_var) 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();
FaceIterator last = m.face.end();
pu.Clear();
if(m.face.empty()) {
pu.oldBase=0; // if the vector is empty we cannot find the last valid element
} else {
pu.oldBase=&*m.face.begin();
last=m.face.end();
}
for(int i=0; i<n; ++i)
{
m.face.push_back(FaceType());
m.face.back().ClearFlags();
}
m.fn+=n; for(int i=0; i<n; ++i)
{
pu.newBase = &*m.face.begin(); m.vert.push_back(VertexType());
// m.vert.back().ClearFlags(); // No more necessary, since 9/2005 flags are set to zero in the constuctor.
}
if(pu.NeedUpdate()) m.vn+=n;
{
/* std::vector<FacePointer *>::iterator it;
for (it=local_var.begin();it<local_var.end();it++)
{
pu.Update((FacePointer&)(*it));
}*/
typename std::vector<FaceType **>::iterator jit; pu.newBase = &*m.vert.begin();
for(jit=local_var.begin(); jit!=local_var.end(); ++jit) if(pu.NeedUpdate())
if((**jit) !=0 ) {
{ FaceIterator fi;
//FaceType **f =(**jit); for (fi=m.face.begin(); fi!=m.face.end(); ++fi)
pu.Update(**jit); if(!(*fi).IsD())
} {
pu.Update((*fi).V(0));
pu.Update((*fi).V(1));
pu.Update((*fi).V(2));
}
FaceIterator fi; // e poiche' lo spazio e' cambiato si ricalcola anche last da zero
for (fi=m.face.begin(); fi!=m.face.end(); ++fi)
if(!(*fi).IsD())
{
if(FaceType::HasFFAdjacency())
{
pu.Update((*fi).FFp(0));
pu.Update((*fi).FFp(1));
pu.Update((*fi).FFp(2));
}
if(FaceType::HasVFAdjacency())
{
if ((*fi).VFp(0)!=0)
pu.Update((*fi).VFp(0));
if ((*fi).VFp(1)!=0)
pu.Update((*fi).VFp(1));
if ((*fi).VFp(2)!=0)
pu.Update((*fi).VFp(2));
}
}
VertexIterator vi;
for (vi=m.vert.begin(); vi!=m.vert.end(); ++vi)
if(!(*vi).IsD())
{
if(VertexType::HasVFAdjacency())
if ((*vi).VFp()!=0)
pu.Update((*vi).VFp());
}
// e poiche' lo spazio e' cambiato si ricalcola anche last da zero
unsigned int siz=m.face.size()-n;
last = m.face.begin(); }
advance(last,siz); unsigned int siz=(unsigned int)m.vert.size()-n;
} //if(last!=(VertexIterator)0)
//{
return last; last = m.vert.begin();
} advance(last,siz);
//}
//else last=m.vert.begin();
/** Function to add n faces to the mesh.
@param n Il numero di facce che si vuole aggiungere alla mesh
*/
static FaceIterator AddFaces(MeshType &m, int n)
{
PointerUpdater<FacePointer> pu;
return AddFaces(m,n,pu);
}
/** Function to add n faces to the mesh.
NOTA: Aggiorna fn;
*/
static FaceIterator AddFaces(MeshType &m, int n, PointerUpdater<FacePointer> &pu)
{
FaceIterator last = m.face.end();
pu.Clear();
if(m.face.empty()) {
pu.oldBase=0; // if the vector is empty we cannot find the last valid element
} else {
pu.oldBase=&*m.face.begin();
last=m.face.end();
}
m.face.resize(m.face.size()+n);
/* for(int i=0; i<n; ++i)
{
m.face.push_back(FaceType());
m.face.back().ClearFlags();
}*/
m.fn+=n;
pu.newBase = &*m.face.begin();
if(pu.NeedUpdate())
{
FaceIterator fi;
for (fi=m.face.begin(); fi!=m.face.end(); ++fi)
if(!(*fi).IsD())
{
if(FaceType::HasFFAdjacency())
{
if ((*fi).cFFp(0)!=0) pu.Update((*fi).FFp(0));
if ((*fi).cFFp(1)!=0) pu.Update((*fi).FFp(1));
if ((*fi).cFFp(2)!=0) pu.Update((*fi).FFp(2));
}
if(FaceType::HasVFAdjacency())
{
//update pointers to chain of face incident in a vertex
//update them only if they are different from zero
if ((*fi).cVFp(0)!=0) pu.Update((*fi).VFp(0));
if ((*fi).cVFp(1)!=0) pu.Update((*fi).VFp(1));
if ((*fi).cVFp(2)!=0) pu.Update((*fi).VFp(2));
}
}
VertexIterator vi;
for (vi=m.vert.begin(); vi!=m.vert.end(); ++vi)
if(!(*vi).IsD())
{
if(VertexType::HasVFAdjacency())
if ((*vi).cVFp()!=0)
pu.Update((FaceType * &)(*vi).VFp());
// Note the above cast is probably not useful if you have correctly defined
// your vertex type with the correct name of the facetype as a template argument;
// pu.Update((FaceType*)(*vi).VFp()); compiles on old gcc and borland
// pu.Update((*vi).VFp()); compiles on .net and newer gcc
} return last;// deve restituire l'iteratore alla prima faccia aggiunta;
// e poiche' lo spazio e' cambiato si ricalcola anche last da zero }
unsigned int siz=(unsigned int)m.face.size()-n;
last = m.face.begin();
advance(last,siz);
}
return last; static VertexIterator AddVertices(MeshType &m, int n)
} {
PointerUpdater<VertexPointer> pu;
return AddVertices(m, n,pu);
}
}; // end class
/*@}*/ /** Function to add n faces to the mesh.
} // End Namespace TriMesh @param n Il numero di facce che si vuole aggiungere alla mesh
*/
static FaceIterator AddFaces(MeshType &m, int n,std::vector<FacePointer *> &local_var)
{
PointerUpdater<FacePointer> pu;
return AddFaces(m,n,pu,local_var);
}
/** Function to add n faces to the mesh.
NOTA: Aggiorna fn;
*/
static FaceIterator AddFaces(MeshType &m, int n, PointerUpdater<FacePointer> &pu,std::vector<FacePointer *> &local_var)
{
FaceIterator last = m.face.end();
pu.Clear();
if(m.face.empty()) {
pu.oldBase=0; // if the vector is empty we cannot find the last valid element
} else {
pu.oldBase=&*m.face.begin();
last=m.face.end();
}
for(int i=0; i<n; ++i)
{
m.face.push_back(FaceType());
m.face.back().ClearFlags();
}
m.fn+=n;
pu.newBase = &*m.face.begin();
if(pu.NeedUpdate())
{
/* std::vector<FacePointer *>::iterator it;
for (it=local_var.begin();it<local_var.end();it++)
{
pu.Update((FacePointer&)(*it));
}*/
typename std::vector<FaceType **>::iterator jit;
for(jit=local_var.begin(); jit!=local_var.end(); ++jit)
if((**jit) !=0 )
{
//FaceType **f =(**jit);
pu.Update(**jit);
}
FaceIterator fi;
for (fi=m.face.begin(); fi!=m.face.end(); ++fi)
if(!(*fi).IsD())
{
if(FaceType::HasFFAdjacency())
{
pu.Update((*fi).FFp(0));
pu.Update((*fi).FFp(1));
pu.Update((*fi).FFp(2));
}
if(FaceType::HasVFAdjacency())
{
if ((*fi).VFp(0)!=0)
pu.Update((*fi).VFp(0));
if ((*fi).VFp(1)!=0)
pu.Update((*fi).VFp(1));
if ((*fi).VFp(2)!=0)
pu.Update((*fi).VFp(2));
}
}
VertexIterator vi;
for (vi=m.vert.begin(); vi!=m.vert.end(); ++vi)
if(!(*vi).IsD())
{
if(VertexType::HasVFAdjacency())
if ((*vi).VFp()!=0)
pu.Update((*vi).VFp());
}
// e poiche' lo spazio e' cambiato si ricalcola anche last da zero
unsigned int siz=m.face.size()-n;
last = m.face.begin();
advance(last,siz);
}
return last;
}
/** Function to add n faces to the mesh.
@param n Il numero di facce che si vuole aggiungere alla mesh
*/
static FaceIterator AddFaces(MeshType &m, int n)
{
PointerUpdater<FacePointer> pu;
return AddFaces(m,n,pu);
}
/** Function to add n faces to the mesh.
NOTA: Aggiorna fn;
*/
static FaceIterator AddFaces(MeshType &m, int n, PointerUpdater<FacePointer> &pu)
{
FaceIterator last = m.face.end();
pu.Clear();
if(m.face.empty()) {
pu.oldBase=0; // if the vector is empty we cannot find the last valid element
} else {
pu.oldBase=&*m.face.begin();
last=m.face.end();
}
m.face.resize(m.face.size()+n);
/* for(int i=0; i<n; ++i)
{
m.face.push_back(FaceType());
m.face.back().ClearFlags();
}*/
m.fn+=n;
pu.newBase = &*m.face.begin();
if(pu.NeedUpdate())
{
FaceIterator fi;
for (fi=m.face.begin(); fi!=m.face.end(); ++fi)
if(!(*fi).IsD())
{
if(FaceType::HasFFAdjacency())
{
if ((*fi).cFFp(0)!=0) pu.Update((*fi).FFp(0));
if ((*fi).cFFp(1)!=0) pu.Update((*fi).FFp(1));
if ((*fi).cFFp(2)!=0) pu.Update((*fi).FFp(2));
}
if(FaceType::HasVFAdjacency())
{
//update pointers to chain of face incident in a vertex
//update them only if they are different from zero
if ((*fi).cVFp(0)!=0) pu.Update((*fi).VFp(0));
if ((*fi).cVFp(1)!=0) pu.Update((*fi).VFp(1));
if ((*fi).cVFp(2)!=0) pu.Update((*fi).VFp(2));
}
}
VertexIterator vi;
for (vi=m.vert.begin(); vi!=m.vert.end(); ++vi)
if(!(*vi).IsD())
{
if(VertexType::HasVFAdjacency())
if ((*vi).cVFp()!=0)
pu.Update((FaceType * &)(*vi).VFp());
// Note the above cast is probably not useful if you have correctly defined
// your vertex type with the correct name of the facetype as a template argument;
// pu.Update((FaceType*)(*vi).VFp()); compiles on old gcc and borland
// pu.Update((*vi).VFp()); compiles on .net and newer gcc
}
// e poiche' lo spazio e' cambiato si ricalcola anche last da zero
unsigned int siz=(unsigned int)m.face.size()-n;
last = m.face.begin();
advance(last,siz);
}
return last;
}
}; // end class
/*@}*/
} // End Namespace TriMesh
} // End Namespace vcg } // End Namespace vcg
#endif #endif