From 3b619721168b220aaf69c0059629ba354a8210bb Mon Sep 17 00:00:00 2001 From: cignoni Date: Thu, 15 Dec 2011 07:23:51 +0000 Subject: [PATCH] Implemented the correct management of per edge attribute. It was half done... --- vcg/complex/allocate.h | 115 +++++++++++++++++++++++++++-------------- 1 file changed, 75 insertions(+), 40 deletions(-) diff --git a/vcg/complex/allocate.h b/vcg/complex/allocate.h index f7191e96..895ad877 100644 --- a/vcg/complex/allocate.h +++ b/vcg/complex/allocate.h @@ -938,28 +938,25 @@ public: template static typename MeshType::template PerVertexAttributeHandle - GetPerVertexAttribute( MeshType & m, const std::string & name){ - assert(!name.empty()); - PointerToAttribute h1; h1._name = name; - typename std::set :: iterator i; + GetPerVertexAttribute( MeshType & m, const std::string & name){ + assert(!name.empty()); + PointerToAttribute h1; h1._name = name; + typename std::set :: iterator i; - i =m.vert_attr.find(h1); - if(i!=m.vert_attr.end()) - if((*i)._sizeof == sizeof(ATTR_TYPE) ){ - if( (*i)._padding != 0 ){ - PointerToAttribute attr = (*i); // copy the PointerToAttribute - m.vert_attr.erase(i); // remove it from the set - FixPaddedPerVertexAttribute(m,attr); - std::pair new_i = m.vert_attr.insert(attr); // insert the modified PointerToAttribute - assert(new_i.second); - i = new_i.first; - } - - return typename MeshType::template PerVertexAttributeHandle((*i)._handle,(*i).n_attr); + i =m.vert_attr.find(h1); + if(i!=m.vert_attr.end()) + if((*i)._sizeof == sizeof(ATTR_TYPE) ){ + if( (*i)._padding != 0 ){ + PointerToAttribute attr = (*i); // copy the PointerToAttribute + m.vert_attr.erase(i); // remove it from the set + FixPaddedPerVertexAttribute(m,attr); + std::pair new_i = m.vert_attr.insert(attr); // insert the modified PointerToAttribute + assert(new_i.second); + i = new_i.first; + } + return typename MeshType::template PerVertexAttributeHandle((*i)._handle,(*i).n_attr); } - - return typename MeshType:: template PerVertexAttributeHandle(NULL,0); - + return typename MeshType:: template PerVertexAttributeHandle(NULL,0); } template @@ -1027,9 +1024,9 @@ public: assert(i ==m.edge_attr.end() );// an attribute with this name exists } h._sizeof = sizeof(ATTR_TYPE); - h._padding = 0; - h._typename = typeid(ATTR_TYPE).name(); - h._handle = (void*) new SimpleTempData(m.edge); + h._padding = 0; +// h._typename = typeid(ATTR_TYPE).name(); + h._handle = new SimpleTempData(m.edge); m.attrn++; h.n_attr = m.attrn; std::pair < AttrIterator , bool> res = m.edge_attr.insert(h); @@ -1046,18 +1043,28 @@ public: template static typename MeshType::template PerEdgeAttributeHandle - GetPerEdgeAttribute( const MeshType & m, const std::string & name){ - assert(!name.empty()); - PointerToAttribute h1; h1._name = name; - typename std::set ::const_iterator i; + GetPerEdgeAttribute( MeshType & m, const std::string & name){ + assert(!name.empty()); + PointerToAttribute h1; h1._name = name; + typename std::set ::const_iterator i; - i =m.edge_attr.find(h1); + i =m.edge_attr.find(h1); + if(i!=m.edge_attr.end()) + if((*i)._sizeof == sizeof(ATTR_TYPE) ){ + if( (*i)._padding != 0 ){ + PointerToAttribute attr = (*i); // copy the PointerToAttribute + m.edge_attr.erase(i); // remove it from the set + FixPaddedPerEdgeAttribute(m,attr); + std::pair new_i = m.edge_attr.insert(attr); // insert the modified PointerToAttribute + assert(new_i.second); + i = new_i.first; + } + return typename MeshType::template PerEdgeAttributeHandle((*i)._handle,(*i).n_attr); + } +// if((*i)._typename == typeid(ATTR_TYPE).name() ) +// return typename MeshType:: template PerVertexAttributeHandle(NULL,0); - if(i!=m.edge_attr.end()) - if((*i)._typename == typeid(ATTR_TYPE).name() ) - return typename MeshType:: template PerVertexAttributeHandle(NULL,0); - - return typename MeshType:: template PerFaceAttributeHandle(NULL,0); + return typename MeshType:: template PerEdgeAttributeHandle(NULL,0); } template @@ -1276,7 +1283,7 @@ public: } static - void DeletePerMeshAttribute( MeshType & m, std::string name){ + void DeletePerMeshAttribute( MeshType & m, std::string name){ AttrIterator i; PointerToAttribute h1; h1._name = name; i = m.mesh_attr.find(h1); @@ -1286,8 +1293,8 @@ public: } template - static - void FixPaddedPerVertexAttribute ( MeshType & m,PointerToAttribute & pa){ + static + void FixPaddedPerVertexAttribute (MeshType & m, PointerToAttribute & pa){ // create the container of the right type SimpleTempData* _handle = new SimpleTempData(m.vert); @@ -1297,11 +1304,11 @@ public: for(unsigned int i = 0; i < m.vert.size(); ++i){ ATTR_TYPE * dest = &(*_handle)[i]; char * ptr = (char*)( ((SimpleTempDataBase *)pa._handle)->DataBegin()); - memcpy((void*)dest , + memcpy((void*)dest , (void*) &(ptr[i * pa._sizeof ]) ,sizeof(ATTR_TYPE)); } - // remove the padded container + // remove the padded container delete ((SimpleTempDataBase*) pa._handle); // update the pointer to data @@ -1310,10 +1317,38 @@ public: // update the pointer to data pa._handle = _handle; - // zero the padding + // zero the padding pa._padding = 0; } - + template + static + void FixPaddedPerEdgeAttribute (MeshType & m, PointerToAttribute & pa){ + + // create the container of the right type + SimpleTempData* _handle = new SimpleTempData(m.edge); + + // copy the padded container in the new one + _handle->Resize(m.edge.size()); + for(unsigned int i = 0; i < m.edge.size(); ++i){ + ATTR_TYPE * dest = &(*_handle)[i]; + char * ptr = (char*)( ((SimpleTempDataBase *)pa._handle)->DataBegin()); + memcpy((void*)dest , + (void*) &(ptr[i * pa._sizeof ]) ,sizeof(ATTR_TYPE)); + } + + // remove the padded container + delete ((SimpleTempDataBase*) pa._handle); + + // update the pointer to data + pa._sizeof = sizeof(ATTR_TYPE); + + // update the pointer to data + pa._handle = _handle; + + // zero the padding + pa._padding = 0; + } + template static void FixPaddedPerFaceAttribute ( MeshType & m,PointerToAttribute & pa){