ConstPerVertexAttributeHandle

This commit is contained in:
alemuntoni 2021-03-23 15:27:13 +01:00
parent 3bfe5793f6
commit 7c601cc837
3 changed files with 65 additions and 28 deletions

View File

@ -1434,6 +1434,17 @@ public:
return false; return false;
} }
/*! \brief Check if a const handle to a Per-Vertex Attribute is valid
*/
template <class ATTR_TYPE>
static
bool IsValidHandle( const MeshType & m, const typename MeshType::template ConstPerVertexAttributeHandle<ATTR_TYPE> & 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;
}
/*! \brief Add a Per-Vertex Attribute of the given ATTR_TYPE with the given name. /*! \brief Add a Per-Vertex Attribute of the given ATTR_TYPE with the given name.
No attribute with that name must exists (even of different type) No attribute with that name must exists (even of different type)
@ -1483,19 +1494,19 @@ public:
return AddPerVertexAttribute<ATTR_TYPE>(m,name); return AddPerVertexAttribute<ATTR_TYPE>(m,name);
} }
/*! \brief gives a handle to a per-vertex attribute with a given name and ATTR_TYPE /*! \brief gives a const 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. \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). Otherwise, returns an invalid handle (check it using IsValidHandle).
*/ */
template <class ATTR_TYPE> template <class ATTR_TYPE>
static static
typename MeshType::template PerVertexAttributeHandle<ATTR_TYPE> typename MeshType::template ConstPerVertexAttributeHandle<ATTR_TYPE>
GetPerVertexAttribute( const MeshType & m, std::string name = std::string("")){ GetConstPerVertexAttribute( const MeshType & m, std::string name = std::string("")){
typename MeshType::template PerVertexAttributeHandle<ATTR_TYPE> h; typename MeshType::template ConstPerVertexAttributeHandle<ATTR_TYPE> h;
if(!name.empty()){ if(!name.empty()){
return FindPerVertexAttribute<ATTR_TYPE>(m,name); return FindConstPerVertexAttribute<ATTR_TYPE>(m,name);
} }
return typename MeshType:: template PerVertexAttributeHandle<ATTR_TYPE>(nullptr,0); return typename MeshType:: template ConstPerVertexAttributeHandle<ATTR_TYPE>(nullptr,0);
} }
/*! \brief Try to retrieve an handle to an attribute with a given name and ATTR_TYPE /*! \brief Try to retrieve an handle to an attribute with a given name and ATTR_TYPE
@ -1531,8 +1542,8 @@ public:
* Input mesh is const. * Input mesh is const.
*/ */
template <class ATTR_TYPE> template <class ATTR_TYPE>
static typename MeshType::template PerVertexAttributeHandle<ATTR_TYPE> static typename MeshType::template ConstPerVertexAttributeHandle<ATTR_TYPE>
FindPerVertexAttribute( const MeshType & m, const std::string & name) FindConstPerVertexAttribute( const MeshType & m, const std::string & name)
{ {
assert(!name.empty()); assert(!name.empty());
PointerToAttribute h1; h1._name = name; PointerToAttribute h1; h1._name = name;
@ -1541,9 +1552,9 @@ public:
i =m.vert_attr.find(h1); i =m.vert_attr.find(h1);
if(i!=m.vert_attr.end()) if(i!=m.vert_attr.end())
if((*i)._sizeof == sizeof(ATTR_TYPE) ){ if((*i)._sizeof == sizeof(ATTR_TYPE) ){
return typename MeshType::template PerVertexAttributeHandle<ATTR_TYPE>((*i)._handle,(*i).n_attr); return typename MeshType::template ConstPerVertexAttributeHandle<ATTR_TYPE>((*i)._handle,(*i).n_attr);
} }
return typename MeshType:: template PerVertexAttributeHandle<ATTR_TYPE>(nullptr,0); return typename MeshType:: template ConstPerVertexAttributeHandle<ATTR_TYPE>(nullptr,0);
} }
/*! \brief query the mesh for all the attributes per vertex /*! \brief query the mesh for all the attributes per vertex
@ -1556,8 +1567,8 @@ public:
for(i = m.vert_attr.begin(); i != m.vert_attr.end(); ++i ) for(i = m.vert_attr.begin(); i != m.vert_attr.end(); ++i )
if(!(*i)._name.empty()) if(!(*i)._name.empty())
{ {
typename MeshType:: template PerVertexAttributeHandle<ATTR_TYPE> hh; typename MeshType:: template ConstPerVertexAttributeHandle<ATTR_TYPE> hh;
hh = Allocator<MeshType>:: template FindPerVertexAttribute <ATTR_TYPE>(m,(*i)._name); hh = Allocator<MeshType>:: template FindConstPerVertexAttribute <ATTR_TYPE>(m,(*i)._name);
if(IsValidHandle<ATTR_TYPE>(m,hh)) if(IsValidHandle<ATTR_TYPE>(m,hh))
all.push_back((*i)._name); all.push_back((*i)._name);
} }

View File

@ -297,6 +297,25 @@ public:
void resize(size_t /*size*/) { }; void resize(size_t /*size*/) { };
}; };
template <class ATTR_TYPE, class CONT>
class ConstAttributeHandle{
public:
ConstAttributeHandle(){_handle=(SimpleTempData<CONT,ATTR_TYPE> *)nullptr;}
ConstAttributeHandle( const void *ah,const int & n):_handle ( (const SimpleTempData<CONT,ATTR_TYPE> *)ah ),n_attr(n){}
//pointer to the SimpleTempData that stores the attribute
const SimpleTempData<CONT,ATTR_TYPE> * _handle;
// its attribute number
int n_attr;
// access function
template <class RefType>
const ATTR_TYPE & operator [](const RefType & i) const {return (*_handle)[i];}
void resize(size_t /*size*/) { };
};
template <class ATTR_TYPE> template <class ATTR_TYPE>
class PerVertexAttributeHandle: public AttributeHandle<ATTR_TYPE,VertContainer>{ class PerVertexAttributeHandle: public AttributeHandle<ATTR_TYPE,VertContainer>{
public: public:
@ -304,6 +323,13 @@ public:
PerVertexAttributeHandle( void *ah,const int & n):AttributeHandle<ATTR_TYPE,VertContainer>(ah,n){} PerVertexAttributeHandle( void *ah,const int & n):AttributeHandle<ATTR_TYPE,VertContainer>(ah,n){}
}; };
template <class ATTR_TYPE>
class ConstPerVertexAttributeHandle: public ConstAttributeHandle<ATTR_TYPE,VertContainer>{
public:
ConstPerVertexAttributeHandle():ConstAttributeHandle<ATTR_TYPE,VertContainer>(){}
ConstPerVertexAttributeHandle( const void *ah,const int & n):ConstAttributeHandle<ATTR_TYPE,VertContainer>(ah,n){}
};
template <class ATTR_TYPE> template <class ATTR_TYPE>
class PerFaceAttributeHandle: public AttributeHandle<ATTR_TYPE,FaceContainer>{ class PerFaceAttributeHandle: public AttributeHandle<ATTR_TYPE,FaceContainer>{

View File

@ -93,7 +93,7 @@ namespace vcg {
return Save(m,filename,binary,pi,cb); return Save(m,filename,binary,pi,cb);
} }
static int Save(const SaveMeshType &m, const char * filename, bool binary, PlyInfo &pi, CallBackPos *cb=0) // V1.0 static int Save(const SaveMeshType &m, const char * filename, bool binary, const PlyInfo &pi, CallBackPos *cb=0) // V1.0
{ {
FILE * fpout; FILE * fpout;
const char * hbin = "binary_little_endian"; const char * hbin = "binary_little_endian";
@ -112,7 +112,7 @@ namespace vcg {
fpout = fopen(filename,"wb"); fpout = fopen(filename,"wb");
if(fpout==NULL) { if(fpout==NULL) {
pi.status=::vcg::ply::E_CANTOPEN; //pi.status=::vcg::ply::E_CANTOPEN;
return ::vcg::ply::E_CANTOPEN; return ::vcg::ply::E_CANTOPEN;
} }
fprintf(fpout, fprintf(fpout,
@ -360,12 +360,12 @@ namespace vcg {
VertexIterator vi; VertexIterator vi;
SimpleTempData<typename SaveMeshType::VertContainer,int> indices(m.vert); SimpleTempData<typename SaveMeshType::VertContainer,int> indices(m.vert);
std::vector<typename SaveMeshType:: template PerVertexAttributeHandle<float > > thfv(pi.VertDescriptorVec.size()); std::vector<typename SaveMeshType:: template ConstPerVertexAttributeHandle<float > > thfv(pi.VertDescriptorVec.size());
std::vector<typename SaveMeshType:: template PerVertexAttributeHandle<double> > thdv(pi.VertDescriptorVec.size()); std::vector<typename SaveMeshType:: template ConstPerVertexAttributeHandle<double> > thdv(pi.VertDescriptorVec.size());
std::vector<typename SaveMeshType:: template PerVertexAttributeHandle<int > > thiv(pi.VertDescriptorVec.size()); std::vector<typename SaveMeshType:: template ConstPerVertexAttributeHandle<int > > thiv(pi.VertDescriptorVec.size());
std::vector<typename SaveMeshType:: template PerVertexAttributeHandle<short > > thsv(pi.VertDescriptorVec.size()); std::vector<typename SaveMeshType:: template ConstPerVertexAttributeHandle<short > > thsv(pi.VertDescriptorVec.size());
std::vector<typename SaveMeshType:: template PerVertexAttributeHandle<char > > thcv(pi.VertDescriptorVec.size()); std::vector<typename SaveMeshType:: template ConstPerVertexAttributeHandle<char > > thcv(pi.VertDescriptorVec.size());
std::vector<typename SaveMeshType:: template PerVertexAttributeHandle<unsigned char> > thuv(pi.VertDescriptorVec.size()); std::vector<typename SaveMeshType:: template ConstPerVertexAttributeHandle<unsigned char> > thuv(pi.VertDescriptorVec.size());
for(size_t i=0;i<pi.VertDescriptorVec.size();i++) for(size_t i=0;i<pi.VertDescriptorVec.size();i++)
{ {
@ -374,12 +374,12 @@ namespace vcg {
assert(vcg::tri::HasPerVertexAttribute(m,pi.VertAttrNameVec[i])); assert(vcg::tri::HasPerVertexAttribute(m,pi.VertAttrNameVec[i]));
switch (pi.VertDescriptorVec[i].stotype1) switch (pi.VertDescriptorVec[i].stotype1)
{ {
case ply::T_FLOAT : thfv[i] = vcg::tri::Allocator<SaveMeshType>::template FindPerVertexAttribute<float>(m,pi.VertAttrNameVec[i]); break; case ply::T_FLOAT : thfv[i] = vcg::tri::Allocator<SaveMeshType>::template FindConstPerVertexAttribute<float>(m,pi.VertAttrNameVec[i]); break;
case ply::T_DOUBLE : thdv[i] = vcg::tri::Allocator<SaveMeshType>::template FindPerVertexAttribute<double>(m,pi.VertAttrNameVec[i]); break; case ply::T_DOUBLE : thdv[i] = vcg::tri::Allocator<SaveMeshType>::template FindConstPerVertexAttribute<double>(m,pi.VertAttrNameVec[i]); break;
case ply::T_INT : thiv[i] = vcg::tri::Allocator<SaveMeshType>::template FindPerVertexAttribute<int >(m,pi.VertAttrNameVec[i]); break; case ply::T_INT : thiv[i] = vcg::tri::Allocator<SaveMeshType>::template FindConstPerVertexAttribute<int >(m,pi.VertAttrNameVec[i]); break;
case ply::T_SHORT : thsv[i] = vcg::tri::Allocator<SaveMeshType>::template FindPerVertexAttribute<short >(m,pi.VertAttrNameVec[i]); break; case ply::T_SHORT : thsv[i] = vcg::tri::Allocator<SaveMeshType>::template FindConstPerVertexAttribute<short >(m,pi.VertAttrNameVec[i]); break;
case ply::T_CHAR : thcv[i] = vcg::tri::Allocator<SaveMeshType>::template FindPerVertexAttribute<char>(m,pi.VertAttrNameVec[i]); break; case ply::T_CHAR : thcv[i] = vcg::tri::Allocator<SaveMeshType>::template FindConstPerVertexAttribute<char>(m,pi.VertAttrNameVec[i]); break;
case ply::T_UCHAR : thuv[i] = vcg::tri::Allocator<SaveMeshType>::template FindPerVertexAttribute<unsigned char>(m,pi.VertAttrNameVec[i]); break; case ply::T_UCHAR : thuv[i] = vcg::tri::Allocator<SaveMeshType>::template FindConstPerVertexAttribute<unsigned char>(m,pi.VertAttrNameVec[i]); break;
default : assert(0); default : assert(0);
} }
} }