diff --git a/vcg/complex/trimesh/allocate.h b/vcg/complex/trimesh/allocate.h index 9170e046..4b6b111a 100644 --- a/vcg/complex/trimesh/allocate.h +++ b/vcg/complex/trimesh/allocate.h @@ -886,6 +886,7 @@ public: i = m.face_attr.find(h); assert(i ==m.face_attr.end() );// an attribute with this name exists } + h._sizeof = sizeof(ATTR_TYPE); h._handle = (void*) new SimpleTempData(m.face); m.attrn++; h.n_attr = m.attrn; @@ -903,14 +904,23 @@ public: template static typename MeshType::template PerFaceAttributeHandle - GetPerFaceAttribute( const MeshType & m, const std::string & name){ + GetPerFaceAttribute( MeshType & m, const std::string & name){ assert(!name.empty()); PtrToAttr h1; h1._name = name; - typename std::set ::const_iterator i; + typename std::set ::iterator i; i =m.face_attr.find(h1); - if(i!=m.face_attr.end()) + if(i!=m.face_attr.end()){ + if( (*i)._padding != 0 ){ + PtrToAttr attr = (*i); // copy the PointerToAttribute + m.face_attr.erase(i); // remove it from the set + FixPaddedPerFaceAttribute(m,attr); + std::pair new_i = m.face_attr.insert(attr); // insert the modified PointerToAttribute + assert(new_i.second); + i = new_i.first; + } return typename MeshType::template PerFaceAttributeHandle((*i)._handle,(*i).n_attr); + } else return typename MeshType:: template PerFaceAttributeHandle(NULL,0); } @@ -959,24 +969,35 @@ public: i = m.mesh_attr.find(h); assert(i ==m.mesh_attr.end() );// an attribute with this name exists } + h._sizeof = sizeof(ATTR_TYPE); h._handle = (void*) new Attribute(); m.attrn++; h.n_attr = m.attrn; std::pair < AttrIterator , bool> res = m.mesh_attr.insert(h); return typename MeshType::template PerMeshAttributeHandle(res.first->_handle,res.first->n_attr); } - + template static typename MeshType::template PerMeshAttributeHandle - GetPerMeshAttribute( const MeshType & m, const std::string & name){ + GetPerMeshAttribute( MeshType & m, const std::string & name){ assert(!name.empty()); PtrToAttr h1; h1._name = name; - typename std::set ::const_iterator i; + typename std::set ::iterator i; i =m.mesh_attr.find(h1); - if(i!=m.mesh_attr.end()) + if(i!=m.mesh_attr.end()){ + if( (*i)._padding != 0 ){ + PtrToAttr attr = (*i); // copy the PointerToAttribute + m.mesh_attr.erase(i); // remove it from the set + FixPaddedPerMeshAttribute(m,attr); + std::pair new_i = m.mesh_attr.insert(attr); // insert the modified PointerToAttribute + assert(new_i.second); + i = new_i.first; + } + return typename MeshType::template PerMeshAttributeHandle((*i)._handle,(*i).n_attr); + } else return typename MeshType:: template PerMeshAttributeHandle(NULL,0); } @@ -1013,16 +1034,74 @@ public: // copy the padded container in the new one _handle->Resize(m.vert.size()); - for(int i = 0; i < m.vert.size(); ++i){ + 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 , - (void*) &(ptr[i * pa._sizeof + pa._padding]) ,sizeof(ATTR_TYPE)); + (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,PtrToAttr & pa){ + + // create the container of the right type + SimpleTempData* _handle = new SimpleTempData(m.face); + + // copy the padded container in the new one + _handle->Resize(m.face.size()); + for(unsigned int i = 0; i < m.face.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 FixPaddedPerMeshAttribute ( MeshType & m,PtrToAttr & pa){ + + // create the container of the right type + Attribute * _handle = new Attribute(); + + // copy the padded container in the new one + ATTR_TYPE * dest = _handle->attribute; + char * ptr = (char*)( ((Attribute *)pa._handle)->DataBegin()); + memcpy((void*)_handle->attribute ,(void*) &(ptr[0]) ,sizeof(ATTR_TYPE)); + + // remove the padded container + delete ( (Attribute *) pa._handle); + + // update the pointer to data + pa._sizeof = sizeof(ATTR_TYPE); + // update the pointer to data pa._handle = _handle;