diff --git a/vcg/complex/allocate.h b/vcg/complex/allocate.h index c81368ba..3a6a6b85 100644 --- a/vcg/complex/allocate.h +++ b/vcg/complex/allocate.h @@ -1434,6 +1434,17 @@ public: return false; } + /*! \brief Check if a const handle to a Per-Vertex Attribute is valid + */ + template + static + bool IsValidHandle( const MeshType & m, const typename MeshType::template ConstPerVertexAttributeHandle & 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. No attribute with that name must exists (even of different type) @@ -1482,20 +1493,20 @@ public: } return AddPerVertexAttribute(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. 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; + typename MeshType::template ConstPerVertexAttributeHandle + GetConstPerVertexAttribute( const MeshType & m, std::string name = std::string("")){ + typename MeshType::template ConstPerVertexAttributeHandle h; if(!name.empty()){ - return FindPerVertexAttribute(m,name); + return FindConstPerVertexAttribute(m,name); } - return typename MeshType:: template PerVertexAttributeHandle(nullptr,0); + return typename MeshType:: template ConstPerVertexAttributeHandle(nullptr,0); } /*! \brief Try to retrieve an handle to an attribute with a given name and ATTR_TYPE @@ -1524,15 +1535,15 @@ public: } 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) + static typename MeshType::template ConstPerVertexAttributeHandle + FindConstPerVertexAttribute( const MeshType & m, const std::string & name) { assert(!name.empty()); PointerToAttribute h1; h1._name = name; @@ -1541,9 +1552,9 @@ public: 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 ConstPerVertexAttributeHandle((*i)._handle,(*i).n_attr); } - return typename MeshType:: template PerVertexAttributeHandle(nullptr,0); + return typename MeshType:: template ConstPerVertexAttributeHandle(nullptr,0); } /*! \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 ) if(!(*i)._name.empty()) { - typename MeshType:: template PerVertexAttributeHandle hh; - hh = Allocator:: template FindPerVertexAttribute (m,(*i)._name); + typename MeshType:: template ConstPerVertexAttributeHandle hh; + hh = Allocator:: template FindConstPerVertexAttribute (m,(*i)._name); if(IsValidHandle(m,hh)) all.push_back((*i)._name); } diff --git a/vcg/complex/base.h b/vcg/complex/base.h index 599cc975..678d715e 100644 --- a/vcg/complex/base.h +++ b/vcg/complex/base.h @@ -297,6 +297,25 @@ public: void resize(size_t /*size*/) { }; }; + template + class ConstAttributeHandle{ + public: + ConstAttributeHandle(){_handle=(SimpleTempData *)nullptr;} + ConstAttributeHandle( const void *ah,const int & n):_handle ( (const SimpleTempData *)ah ),n_attr(n){} + + + //pointer to the SimpleTempData that stores the attribute + const SimpleTempData * _handle; + + // its attribute number + int n_attr; + + // access function + template + const ATTR_TYPE & operator [](const RefType & i) const {return (*_handle)[i];} + void resize(size_t /*size*/) { }; + }; + template class PerVertexAttributeHandle: public AttributeHandle{ public: @@ -304,6 +323,13 @@ public: PerVertexAttributeHandle( void *ah,const int & n):AttributeHandle(ah,n){} }; + template + class ConstPerVertexAttributeHandle: public ConstAttributeHandle{ + public: + ConstPerVertexAttributeHandle():ConstAttributeHandle(){} + ConstPerVertexAttributeHandle( const void *ah,const int & n):ConstAttributeHandle(ah,n){} + }; + template class PerFaceAttributeHandle: public AttributeHandle{ diff --git a/wrap/io_trimesh/export_ply.h b/wrap/io_trimesh/export_ply.h index 05364d73..c871d8d2 100644 --- a/wrap/io_trimesh/export_ply.h +++ b/wrap/io_trimesh/export_ply.h @@ -93,7 +93,7 @@ namespace vcg { 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; const char * hbin = "binary_little_endian"; @@ -112,7 +112,7 @@ namespace vcg { fpout = fopen(filename,"wb"); if(fpout==NULL) { - pi.status=::vcg::ply::E_CANTOPEN; + //pi.status=::vcg::ply::E_CANTOPEN; return ::vcg::ply::E_CANTOPEN; } fprintf(fpout, @@ -360,12 +360,12 @@ namespace vcg { VertexIterator vi; SimpleTempData indices(m.vert); - std::vector > thfv(pi.VertDescriptorVec.size()); - std::vector > thdv(pi.VertDescriptorVec.size()); - std::vector > thiv(pi.VertDescriptorVec.size()); - std::vector > thsv(pi.VertDescriptorVec.size()); - std::vector > thcv(pi.VertDescriptorVec.size()); - std::vector > thuv(pi.VertDescriptorVec.size()); + std::vector > thfv(pi.VertDescriptorVec.size()); + std::vector > thdv(pi.VertDescriptorVec.size()); + std::vector > thiv(pi.VertDescriptorVec.size()); + std::vector > thsv(pi.VertDescriptorVec.size()); + std::vector > thcv(pi.VertDescriptorVec.size()); + std::vector > thuv(pi.VertDescriptorVec.size()); for(size_t i=0;i::template FindPerVertexAttribute(m,pi.VertAttrNameVec[i]); break; - case ply::T_DOUBLE : thdv[i] = vcg::tri::Allocator::template FindPerVertexAttribute(m,pi.VertAttrNameVec[i]); break; - case ply::T_INT : thiv[i] = vcg::tri::Allocator::template FindPerVertexAttribute(m,pi.VertAttrNameVec[i]); break; - case ply::T_SHORT : thsv[i] = vcg::tri::Allocator::template FindPerVertexAttribute(m,pi.VertAttrNameVec[i]); break; - case ply::T_CHAR : thcv[i] = vcg::tri::Allocator::template FindPerVertexAttribute(m,pi.VertAttrNameVec[i]); break; - case ply::T_UCHAR : thuv[i] = vcg::tri::Allocator::template FindPerVertexAttribute(m,pi.VertAttrNameVec[i]); break; + case ply::T_FLOAT : thfv[i] = vcg::tri::Allocator::template FindConstPerVertexAttribute(m,pi.VertAttrNameVec[i]); break; + case ply::T_DOUBLE : thdv[i] = vcg::tri::Allocator::template FindConstPerVertexAttribute(m,pi.VertAttrNameVec[i]); break; + case ply::T_INT : thiv[i] = vcg::tri::Allocator::template FindConstPerVertexAttribute(m,pi.VertAttrNameVec[i]); break; + case ply::T_SHORT : thsv[i] = vcg::tri::Allocator::template FindConstPerVertexAttribute(m,pi.VertAttrNameVec[i]); break; + case ply::T_CHAR : thcv[i] = vcg::tri::Allocator::template FindConstPerVertexAttribute(m,pi.VertAttrNameVec[i]); break; + case ply::T_UCHAR : thuv[i] = vcg::tri::Allocator::template FindConstPerVertexAttribute(m,pi.VertAttrNameVec[i]); break; default : assert(0); } }