diff --git a/vcg/complex/trimesh/allocate.h b/vcg/complex/trimesh/allocate.h index d238191b..f4a84770 100644 --- a/vcg/complex/trimesh/allocate.h +++ b/vcg/complex/trimesh/allocate.h @@ -682,6 +682,61 @@ public: m.face_attr.erase(i); } + /// Per Mesh Attributes + template + static + typename MeshType::template PerMeshAttributeHandle + AddPerMeshAttribute( MeshType & m, std::string name){ + HWIte i; + HandlesWrapper h; + h._name = name; + if(!name.empty()){ + i = m.mesh_attr.find(h); + assert(i ==m.mesh_attr.end() );// an attribute with this name exists + } + h._handle = (void*) new Attribute(); + std::pair < HandlesIterator , bool> res = m.mesh_attr.insert(h); + return typename MeshType::template PerMeshAttributeHandle(res.first->_handle); + } + + template + static + typename MeshType::template PerMeshAttributeHandle + GetPerMeshAttribute( const MeshType & m, const std::string & name){ + assert(!name.empty()); + HandlesWrapper h1; h1._name = name; + typename std::set ::const_iterator i; + + i =m.mesh_attr.find(h1); + if(i!=m.mesh_attr.end()) + return typename MeshType::template PerMeshAttributeHandle((*i)._handle); + else + return typename MeshType:: template PerMeshAttributeHandle(NULL); + } + + template + static + void + DeletePerMeshAttribute( MeshType & m,typename MeshType::template PerMeshAttributeHandle & h){ + typename std::set ::iterator i; + for( i = m.mesh_attr.begin(); i != m.mesh_attr.end(); ++i) + if( (*i)._handle == h._handle ){ + delete (( Attribute *)(*i)._handle); + m.mesh_attr.erase(i); + return;} + assert(0); + } + + template + static + void DeletePerMeshAttribute( MeshType & m, std::string name){ + HandlesIterator i; + HandlesWrapper h1; h1._name = name; + i = m.mesh_attr.find(h1); + assert(i!=m.mesh_attr.end()); + delete ((Attribute *)(*i)._handle); + m.mesh_attr.erase(i); + } }; // end class diff --git a/vcg/complex/trimesh/base.h b/vcg/complex/trimesh/base.h index 9e477e1f..d41c1f7c 100644 --- a/vcg/complex/trimesh/base.h +++ b/vcg/complex/trimesh/base.h @@ -222,9 +222,17 @@ class TriMesh{ ATTR_TYPE & operator [](const RefType & i){return (*_handle)[i];} }; + template + class PerMeshAttributeHandle{ + public: + PerMeshAttributeHandle(void *ah):_handle ( (Attribute *)ah ){} + Attribute * _handle; + ATTR_TYPE & operator ()(){ return *((Attribute *)_handle)->attribute;} + }; - std::set< HandlesWrapper> vert_attr; - std::set< HandlesWrapper> face_attr; + std::set< HandlesWrapper > vert_attr; + std::set< HandlesWrapper > face_attr; + std::set< HandlesWrapper > mesh_attr; /// La camera Camera camera; // intrinsic @@ -261,17 +269,26 @@ public: delete ((SimpleTempDataBase*)(*i)._handle); for( i = face_attr.begin(); i != face_attr.end(); ++i) delete ((SimpleTempDataBase*)(*i)._handle); + for( i = mesh_attr.begin(); i != mesh_attr.end(); ++i) + delete ((AttributeBase*)(*i)._handle); } - inline int MemUsed() const - { - return sizeof(MeshType)+sizeof(VertexType)*vert.size()+sizeof(FaceType)*face.size(); - } + int Mem(const int & nv, const int & nf) const { + typename std::set< HandlesWrapper>::const_iterator i; + int size = 0; + size += sizeof(TriMesh)+sizeof(VertexType)*nv+sizeof(FaceType)*nf; - inline int MemNeeded() const - { - return sizeof(MeshType)+sizeof(VertexType)*vn+sizeof(FaceType)*fn; + for( i = vert_attr.begin(); i != vert_attr.end(); ++i) + size += ((SimpleTempDataBase*)(*i)._handle)->SizeOf()*nv; + for( i = face_attr.begin(); i != face_attr.end(); ++i) + size += ((SimpleTempDataBase*)(*i)._handle)->SizeOf()*nf; + for( i = mesh_attr.begin(); i != mesh_attr.end(); ++i) + size += ((AttributeBase*)(*i)._handle)->SizeOf(); + + return size; } + int MemUsed() const {return Mem(vert.size(),face.size());} + inline int MemNeeded() const {return Mem(vn,fn);} @@ -459,6 +476,15 @@ bool HasPerFaceAttribute(const MESH_TYPE &m, std::string name){ return (ai!= m.face_attr.end() ) ; } +template +bool HasPerMeshAttribute(const MESH_TYPE &m, std::string name){ + typename std::set< typename MESH_TYPE::HandlesWrapper>::const_iterator ai; + typename MESH_TYPE::HandlesWrapper h; + h._name = name; + ai = m.mesh_attr.find(h); + return (ai!= m.mesh_attr.end() ) ; +} + /*@}*/ /*@}*/ } // end namespace