added Allocator::IsValidHandle which returns true if an handle to an attribute is valid or if the attribute

has been removed. This modification affects timesh/allocate.h and trimesh/base.h
This commit is contained in:
ganovelli 2009-01-15 17:41:59 +00:00
parent 12e32feb59
commit 87b8c55144
2 changed files with 106 additions and 25 deletions

View File

@ -701,7 +701,17 @@ namespace vcg {
}
public:
/// Per Vertex Attributes
template <class ATTR_TYPE>
static
bool IsValidHandle( MeshType & m, const typename MeshType::template PerVertexAttributeHandle<ATTR_TYPE> & a){
for(HandlesIterator i = m.vert_attr.begin(); i!=m.vert_attr.end();++i)
if ( (*i).n_attr == a.n_attr ) return true;
return false;
}
template <class ATTR_TYPE>
static
typename MeshType::template PerVertexAttributeHandle<ATTR_TYPE>
@ -714,8 +724,10 @@ public:
assert(i ==m.vert_attr.end() );// an attribute with this name exists
}
h._handle = (void*) new SimpleTempData<VertContainer,ATTR_TYPE>(m.vert);
m.attrn++;
h.n_attr = m.attrn;
std::pair < HandlesIterator , bool> res = m.vert_attr.insert(h);
return typename MeshType::template PerVertexAttributeHandle<ATTR_TYPE>(res.first->_handle);
return typename MeshType::template PerVertexAttributeHandle<ATTR_TYPE>(res.first->_handle,res.first->n_attr );
}
template <class ATTR_TYPE>
@ -735,9 +747,9 @@ public:
i =m.vert_attr.find(h1);
if(i!=m.vert_attr.end())
return typename MeshType::template PerVertexAttributeHandle<ATTR_TYPE>((*i)._handle);
return typename MeshType::template PerVertexAttributeHandle<ATTR_TYPE>((*i)._handle,(*i).n_attr);
else
return typename MeshType:: template PerVertexAttributeHandle<ATTR_TYPE>(NULL);
return typename MeshType:: template PerVertexAttributeHandle<ATTR_TYPE>(NULL,0);
}
template <class ATTR_TYPE>
@ -765,6 +777,14 @@ public:
}
/// Per Edge Attributes
template <class ATTR_TYPE>
static
bool IsValidHandle( MeshType & m, const typename MeshType::template PerEdgeAttributeHandle<ATTR_TYPE> & a){
for(HandlesIterator i = m.edge_attr.begin(); i!=m.edge_attr.end();++i)
if ( (*i).n_attr == a.n_attr ) return true;
return false;
}
template <class ATTR_TYPE>
static
typename MeshType::template PerEdgeAttributeHandle<ATTR_TYPE>
@ -777,8 +797,17 @@ public:
assert(i ==m.edge_attr.end() );// an attribute with this name exists
}
h._handle = (void*) new SimpleTempData<EdgeContainer,ATTR_TYPE>(m.edge);
m.attrn++;
h.n_attr = m.attrn;
std::pair < HandlesIterator , bool> res = m.edge_attr.insert(h);
return typename MeshType::template PerEdgeAttributeHandle<ATTR_TYPE>(res.first->_handle);
return typename MeshType::template PerEdgeAttributeHandle<ATTR_TYPE>(res.first->_handle,res.first->n_attr);
}
template <class ATTR_TYPE>
static
typename MeshType::template PerVertexAttributeHandle<ATTR_TYPE>
AddPerEdgeAttribute( MeshType & m){
return AddPerEdgeAttribute<ATTR_TYPE>(m,std::string(""));
}
template <class ATTR_TYPE>
@ -791,9 +820,9 @@ public:
i =m.edge_attr.find(h1);
if(i!=m.edge_attr.end())
return typename MeshType::template PerFaceAttributeHandle<ATTR_TYPE>((*i)._handle);
return typename MeshType::template PerFaceAttributeHandle<ATTR_TYPE>((*i)._handle,(*i).n_attr);
else
return typename MeshType:: template PerFaceAttributeHandle<ATTR_TYPE>(NULL);
return typename MeshType:: template PerFaceAttributeHandle<ATTR_TYPE>(NULL,0);
}
template <class ATTR_TYPE>
@ -821,6 +850,14 @@ public:
}
/// Per Face Attributes
template <class ATTR_TYPE>
static
bool IsValidHandle( MeshType & m, const typename MeshType::template PerFaceAttributeHandle<ATTR_TYPE> & a){
for(HandlesIterator i = m.face_attr.begin(); i!=m.face_attr.end();++i)
if ( (*i).n_attr == a.n_attr ) return true;
return false;
}
template <class ATTR_TYPE>
static
typename MeshType::template PerFaceAttributeHandle<ATTR_TYPE>
@ -833,10 +870,19 @@ public:
assert(i ==m.face_attr.end() );// an attribute with this name exists
}
h._handle = (void*) new SimpleTempData<FaceContainer,ATTR_TYPE>(m.face);
m.attrn++;
h.n_attr = m.attrn;
std::pair < HandlesIterator , bool> res = m.face_attr.insert(h);
return typename MeshType::template PerFaceAttributeHandle<ATTR_TYPE>(res.first->_handle);
return typename MeshType::template PerFaceAttributeHandle<ATTR_TYPE>(res.first->_handle,res.first->n_attr);
}
template <class ATTR_TYPE>
static
typename MeshType::template PerFaceAttributeHandle<ATTR_TYPE>
AddPerFaceAttribute( MeshType & m){
return AddPerFaceAttribute<ATTR_TYPE>(m,std::string(""));
}
template <class ATTR_TYPE>
static
typename MeshType::template PerFaceAttributeHandle<ATTR_TYPE>
@ -847,9 +893,9 @@ public:
i =m.face_attr.find(h1);
if(i!=m.face_attr.end())
return typename MeshType::template PerFaceAttributeHandle<ATTR_TYPE>((*i)._handle);
return typename MeshType::template PerFaceAttributeHandle<ATTR_TYPE>((*i)._handle,(*i).n_attr);
else
return typename MeshType:: template PerFaceAttributeHandle<ATTR_TYPE>(NULL);
return typename MeshType:: template PerFaceAttributeHandle<ATTR_TYPE>(NULL,0);
}
template <class ATTR_TYPE>
@ -877,6 +923,14 @@ public:
}
/// Per Mesh Attributes
template <class ATTR_TYPE>
static
bool IsValidHandle( MeshType & m, const typename MeshType::template PerMeshAttributeHandle<ATTR_TYPE> & a){
for(HandlesIterator i = m.mesh_attr.begin(); i!=m.mesh_attr.end();++i)
if ( (*i).n_attr == a.n_attr ) return true;
return false;
}
template <class ATTR_TYPE>
static
typename MeshType::template PerMeshAttributeHandle<ATTR_TYPE>
@ -889,8 +943,10 @@ public:
assert(i ==m.mesh_attr.end() );// an attribute with this name exists
}
h._handle = (void*) new Attribute<ATTR_TYPE>();
m.attrn++;
h.n_attr = m.attrn;
std::pair < HandlesIterator , bool> res = m.mesh_attr.insert(h);
return typename MeshType::template PerMeshAttributeHandle<ATTR_TYPE>(res.first->_handle);
return typename MeshType::template PerMeshAttributeHandle<ATTR_TYPE>(res.first->_handle,res.first->n_attr);
}
template <class ATTR_TYPE>
@ -903,9 +959,9 @@ public:
i =m.mesh_attr.find(h1);
if(i!=m.mesh_attr.end())
return typename MeshType::template PerMeshAttributeHandle<ATTR_TYPE>((*i)._handle);
return typename MeshType::template PerMeshAttributeHandle<ATTR_TYPE>((*i)._handle,(*i).n_attr);
else
return typename MeshType:: template PerMeshAttributeHandle<ATTR_TYPE>(NULL);
return typename MeshType:: template PerMeshAttributeHandle<ATTR_TYPE>(NULL,0);
}
template <class ATTR_TYPE>

View File

@ -236,20 +236,33 @@ class TriMesh: public TriMeshEdgeHolder<VertContainerType,FaceContainerType,Edge
//
std::vector<std::string> normalmaps;
int attrn; // total numer of attribute created
class HandlesWrapper{
public:
void * _handle; std::string _name;
int n_attr;
void Resize(const int & sz){((SimpleTempDataBase<VertContainer>*)_handle)->Resize(sz);}
void Reorder(std::vector<size_t> & newVertIndex){((SimpleTempDataBase<VertContainer>*)_handle)->Reorder(newVertIndex);}
const bool operator<(const HandlesWrapper b) const { return(_name.empty()&&b._name.empty())?(_handle < b._handle):( _name < b._name);}
};
std::set< HandlesWrapper > vert_attr;
std::set< HandlesWrapper > edge_attr;
std::set< HandlesWrapper > face_attr;
std::set< HandlesWrapper > mesh_attr;
template <class ATTR_TYPE>
class PerVertexAttributeHandle{
public:
PerVertexAttributeHandle(){}
PerVertexAttributeHandle( void *ah):_handle ( (SimpleTempData<VertContainer,ATTR_TYPE> *)ah ){}
PerVertexAttributeHandle(){_handle=NULL;}
PerVertexAttributeHandle( void *ah,const int & n):_handle ( (SimpleTempData<VertContainer,ATTR_TYPE> *)ah ),n_attr(n){}
PerVertexAttributeHandle operator = ( const PerVertexAttributeHandle & pva){
(SimpleTempData<VertContainer,ATTR_TYPE> *)pva._handle;
n_attr = pva.n_attr;
}
SimpleTempData<VertContainer,ATTR_TYPE> * _handle;
int n_attr; // its attribute number
template <class RefType>
ATTR_TYPE & operator [](const RefType & i){return (*_handle)[i];}
};
@ -257,9 +270,14 @@ class TriMesh: public TriMeshEdgeHolder<VertContainerType,FaceContainerType,Edge
template <class ATTR_TYPE>
class PerFaceAttributeHandle{
public:
PerFaceAttributeHandle(){}
PerFaceAttributeHandle(void *ah):_handle ( (SimpleTempData<FaceContainer,ATTR_TYPE> *)ah ){}
PerFaceAttributeHandle(){_handle=NULL;}
PerFaceAttributeHandle(void *ah,const int & n):_handle ( (SimpleTempData<FaceContainer,ATTR_TYPE> *)ah ),n_attr(n){}
PerFaceAttributeHandle operator = ( const PerFaceAttributeHandle & pva){
(SimpleTempData<FaceContainer,ATTR_TYPE> *)pva._handle;
n_attr = pva.n_attr;
}
SimpleTempData<FaceContainer,ATTR_TYPE> * _handle;
int n_attr; // its attribute number
template <class RefType>
ATTR_TYPE & operator [](const RefType & i){return (*_handle)[i];}
};
@ -267,9 +285,14 @@ class TriMesh: public TriMeshEdgeHolder<VertContainerType,FaceContainerType,Edge
template <class ATTR_TYPE>
class PerEdgeAttributeHandle{
public:
PerEdgeAttributeHandle(){}
PerEdgeAttributeHandle(void *ah):_handle ( (SimpleTempData<EdgeContainer,ATTR_TYPE> *)ah ){}
PerEdgeAttributeHandle(){_handle=NULL;}
PerEdgeAttributeHandle(void *ah,const int & n):_handle ( (SimpleTempData<EdgeContainer,ATTR_TYPE> *)ah ),n_attr(n){}
PerEdgeAttributeHandle operator = ( const PerEdgeAttributeHandle & pva){
(SimpleTempData<EdgeContainer,ATTR_TYPE> *)pva._handle;
n_attr = pva.n_attr;
}
SimpleTempData<EdgeContainer,ATTR_TYPE> * _handle;
int n_attr; // its attribute number
template <class RefType>
ATTR_TYPE & operator [](const RefType & i){return (*(SimpleTempData<EdgeContainer,ATTR_TYPE>*)_handle)[i];}
};
@ -277,16 +300,17 @@ class TriMesh: public TriMeshEdgeHolder<VertContainerType,FaceContainerType,Edge
template <class ATTR_TYPE>
class PerMeshAttributeHandle{
public:
PerMeshAttributeHandle(){}
PerMeshAttributeHandle(void *ah):_handle ( (Attribute<ATTR_TYPE> *)ah ){}
PerMeshAttributeHandle(){_handle=NULL;}
PerMeshAttributeHandle(void *ah,const int & n):_handle ( (Attribute<ATTR_TYPE> *)ah ),n_attr(n){}
PerMeshAttributeHandle operator = ( const PerMeshAttributeHandle & pva){
(Attribute<ATTR_TYPE> *)pva._handle;
n_attr = pva.n_attr;
}
Attribute<ATTR_TYPE> * _handle;
int n_attr;
ATTR_TYPE & operator ()(){ return *((Attribute<ATTR_TYPE> *)_handle)->attribute;}
};
std::set< HandlesWrapper > vert_attr;
std::set< HandlesWrapper > edge_attr;
std::set< HandlesWrapper > face_attr;
std::set< HandlesWrapper > mesh_attr;
/// La camera
Camera<ScalarType> camera; // intrinsic
@ -313,6 +337,7 @@ public:
{
fn = vn = 0;
imark = 0;
attrn = 0;
}
/// destructor