From 34af1c91bd76d8b81a3db6dc500b9b297a113f83 Mon Sep 17 00:00:00 2001 From: alemuntoni Date: Tue, 23 Mar 2021 16:52:56 +0100 Subject: [PATCH] ConstPerMeshAttributeHandle --- vcg/complex/allocate.h | 34 ++++++++++++++++++++++++++++++++++ vcg/complex/base.h | 13 ++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/vcg/complex/allocate.h b/vcg/complex/allocate.h index 170ac390..fa56dd27 100644 --- a/vcg/complex/allocate.h +++ b/vcg/complex/allocate.h @@ -2121,6 +2121,15 @@ public: return false; } + template + static + bool IsValidHandle(const MeshType & m, const typename MeshType::template ConstPerMeshAttributeHandle & 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; + } + template static typename MeshType::template PerMeshAttributeHandle @@ -2159,6 +2168,13 @@ public: return AddPerMeshAttribute(m,name); } + template + static + typename MeshType::template ConstPerMeshAttributeHandle + GetPerMeshAttribute(const MeshType & m, std::string name = std::string("")){ + return FindPerMeshAttribute(m,name); + } + template static typename MeshType::template PerMeshAttributeHandle @@ -2185,6 +2201,24 @@ public: return typename MeshType:: template PerMeshAttributeHandle(nullptr,0); } + template + static + typename MeshType::template ConstPerMeshAttributeHandle + FindPerMeshAttribute( const MeshType & m, const std::string & name){ + if (!name.empty()){ + PointerToAttribute h1; h1._name = name; + typename std::set ::iterator i; + i =m.mesh_attr.find(h1); + if(i!=m.mesh_attr.end()){ + if((*i)._sizeof == sizeof(ATTR_TYPE) ){ + return typename MeshType::template ConstPerMeshAttributeHandle((*i)._handle,(*i).n_attr); + } + } + } + + return typename MeshType:: template ConstPerMeshAttributeHandle(nullptr,0); + } + template static void GetAllPerMeshAttribute(const MeshType & m, std::vector &all){ typename std::set :: iterator i; diff --git a/vcg/complex/base.h b/vcg/complex/base.h index 2afd27a1..adb0316e 100644 --- a/vcg/complex/base.h +++ b/vcg/complex/base.h @@ -387,7 +387,18 @@ public: Attribute * _handle; int n_attr; - ATTR_TYPE & operator ()(){ return *((ATTR_TYPE*)((Attribute *)_handle)->DataBegin());} + ATTR_TYPE & operator ()(){ return *((ATTR_TYPE*) (_handle->DataBegin()));} + }; + + template + class ConstPerMeshAttributeHandle{ + public: + ConstPerMeshAttributeHandle(){_handle=nullptr;} + ConstPerMeshAttributeHandle(const void *ah,const int & n):_handle ( (const Attribute *)ah ),n_attr(n){} + + const Attribute * _handle; + int n_attr; + const ATTR_TYPE & operator ()(){ return *((const ATTR_TYPE*)(_handle->DataBegin()));} }; // Some common Handle typedefs to simplify use