From 4b387c3f670c39241e9a5891bb0081c4dd923dc8 Mon Sep 17 00:00:00 2001 From: alemuntoni Date: Tue, 26 Jan 2021 12:50:29 +0100 Subject: [PATCH] const correctness for user defined mesh attributes --- vcg/complex/allocate.h | 104 ++++++++++++++++++++++++++++++++++------- 1 file changed, 88 insertions(+), 16 deletions(-) diff --git a/vcg/complex/allocate.h b/vcg/complex/allocate.h index 9a75492b..5ada3e88 100644 --- a/vcg/complex/allocate.h +++ b/vcg/complex/allocate.h @@ -1427,8 +1427,8 @@ public: */ template static - bool IsValidHandle( MeshType & m, const typename MeshType::template PerVertexAttributeHandle & a){ - if(a._handle == NULL) return false; + bool IsValidHandle( const MeshType & m, const typename MeshType::template PerVertexAttributeHandle & a){ + if(a._handle == nullptr) return false; for(AttrIterator i = m.vert_attr.begin(); i!=m.vert_attr.end();++i) if ( (*i).n_attr == a.n_attr ) return true; return false; @@ -1482,6 +1482,21 @@ public: } return AddPerVertexAttribute(m,name); } + + /*! \brief gives a handle to a per-vertex attribute with a given name and ATTR_TYPE + \returns a valid handle. If the name is not empty and an attribute with that name and type exists returns a handle to it. + Otherwise, returns an invalid handle (check it using IsValidHandle). + */ + template + static + typename MeshType::template PerVertexAttributeHandle + GetPerVertexAttribute( const MeshType & m, std::string name = std::string("")){ + typename MeshType::template PerVertexAttributeHandle h; + if(!name.empty()){ + return FindPerVertexAttribute(m,name); + } + return typename MeshType:: template PerVertexAttributeHandle(nullptr,0); + } /*! \brief Try to retrieve an handle to an attribute with a given name and ATTR_TYPE \returns a invalid handle if no attribute with that name and type exists. @@ -1507,7 +1522,28 @@ public: } return typename MeshType::template PerVertexAttributeHandle((*i)._handle,(*i).n_attr); } - return typename MeshType:: template PerVertexAttributeHandle(NULL,0); + return typename MeshType:: template PerVertexAttributeHandle(nullptr,0); + } + + /** + * Same as the one above, but without modifying the attribute if it is found. + * (A "find" function should never modify the container in which is looking for..) + * Input mesh is const. + */ + template + static typename MeshType::template PerVertexAttributeHandle + FindPerVertexAttribute( const 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) ){ + return typename MeshType::template PerVertexAttributeHandle((*i)._handle,(*i).n_attr); + } + return typename MeshType:: template PerVertexAttributeHandle(nullptr,0); } /*! \brief query the mesh for all the attributes per vertex @@ -1573,8 +1609,8 @@ public: /// Per Edge Attributes template static - bool IsValidHandle( MeshType & m, const typename MeshType::template PerEdgeAttributeHandle & a){ - if(a._handle == NULL) return false; + bool IsValidHandle( const MeshType & m, const typename MeshType::template PerEdgeAttributeHandle & a){ + if(a._handle == nullptr) return false; for(AttrIterator i = m.edge_attr.begin(); i!=m.edge_attr.end();++i) if ( (*i).n_attr == a.n_attr ) return true; return false; @@ -1649,7 +1685,7 @@ public: return typename MeshType::template PerEdgeAttributeHandle((*i)._handle,(*i).n_attr); } - return typename MeshType:: template PerEdgeAttributeHandle(NULL,0); + return typename MeshType:: template PerEdgeAttributeHandle(nullptr,0); } template @@ -1697,8 +1733,8 @@ public: /// Per Face Attributes template static - bool IsValidHandle( MeshType & m, const typename MeshType::template PerFaceAttributeHandle & a){ - if(a._handle == NULL) return false; + bool IsValidHandle( const MeshType & m, const typename MeshType::template PerFaceAttributeHandle & a){ + if(a._handle == nullptr) return false; for(AttrIterator i = m.face_attr.begin(); i!=m.face_attr.end();++i) if ( (*i).n_attr == a.n_attr ) return true; return false; @@ -1733,7 +1769,7 @@ public: return AddPerFaceAttribute(m,std::string("")); } - /*! \brief gives a handle to a per-edge attribute with a given name and ATTR_TYPE + /*! \brief gives a handle to a per-face attribute with a given name and ATTR_TYPE \returns a valid handle. If the name is not empty and an attribute with that name and type exists returns a handle to it. Otherwise return a hanlde to a newly created. */ @@ -1749,6 +1785,21 @@ public: } return AddPerFaceAttribute(m,name); } + + /*! \brief gives a handle to a per-face attribute with a given name and ATTR_TYPE + \returns a valid handle. If the name is not empty and an attribute with that name and type exists returns a handle to it. + Otherwise, returns an invalid handle (check it using IsValidHandle). + */ + template + static + typename MeshType::template PerFaceAttributeHandle + GetPerFaceAttribute( const MeshType & m, std::string name = std::string("")){ + typename MeshType::template PerFaceAttributeHandle h; + if(!name.empty()){ + return FindPerFaceAttribute(m,name); + } + return typename MeshType:: template PerFaceAttributeHandle(nullptr,0); + } template static @@ -1771,7 +1822,28 @@ public: } return typename MeshType::template PerFaceAttributeHandle((*i)._handle,(*i).n_attr); } - return typename MeshType:: template PerFaceAttributeHandle(NULL,0); + return typename MeshType:: template PerFaceAttributeHandle(nullptr,0); + } + + /** + * Same as the one above, but without modifying the attribute if it is found. + * (A "find" function should never modify the container in which is looking for..) + * Input mesh is const. + */ + template + static + typename MeshType::template PerFaceAttributeHandle + FindPerFaceAttribute( const MeshType & m, const std::string & name){ + assert(!name.empty()); + PointerToAttribute h1; h1._name = name; + typename std::set ::iterator i; + + i =m.face_attr.find(h1); + if(i!=m.face_attr.end()) + if((*i)._sizeof == sizeof(ATTR_TYPE) ){ + return typename MeshType::template PerFaceAttributeHandle((*i)._handle,(*i).n_attr); + } + return typename MeshType:: template PerFaceAttributeHandle(nullptr,0); } template @@ -1816,9 +1888,9 @@ public: /// Per Tetra Attributes template - static bool IsValidHandle(MeshType & m, const typename MeshType::template PerTetraAttributeHandle & a) + static bool IsValidHandle(const MeshType & m, const typename MeshType::template PerTetraAttributeHandle & a) { - if (a._handle == NULL) + if (a._handle == nullptr) return false; for (AttrIterator i = m.tetra_attr.begin(); i != m.tetra_attr.end(); ++i) if ((*i).n_attr == a.n_attr) @@ -1894,7 +1966,7 @@ public: } return typename MeshType::template PerTetraAttributeHandle((*i)._handle, (*i).n_attr); } - return typename MeshType::template PerTetraAttributeHandle(NULL, 0); + return typename MeshType::template PerTetraAttributeHandle(nullptr, 0); } template @@ -1947,8 +2019,8 @@ public: /// Per Mesh Attributes template static - bool IsValidHandle( MeshType & m, const typename MeshType::template PerMeshAttributeHandle & a){ - if(a._handle == NULL) return false; + bool IsValidHandle(const MeshType & m, const typename MeshType::template PerMeshAttributeHandle & a){ + if(a._handle == nullptr) return false; for(AttrIterator i = m.mesh_attr.begin(); i!=m.mesh_attr.end();++i) if ( (*i).n_attr == a.n_attr ) return true; return false; @@ -2015,7 +2087,7 @@ public: return typename MeshType::template PerMeshAttributeHandle((*i)._handle,(*i).n_attr); } - return typename MeshType:: template PerMeshAttributeHandle(NULL,0); + return typename MeshType:: template PerMeshAttributeHandle(nullptr,0); } template