/**************************************************************************** * VCGLib o o * * Visual and Computer Graphics Library o o * * _ O _ * * Copyright(C) 2004 \/)\/ * * Visual Computing Lab /\/| * * ISTI - Italian National Research Council | * * \ * * All rights reserved. * * * * This program is Free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * * for more details. * * * ****************************************************************************/ /**************************************************************************** History $Log: not supported by cvs2svn $ Revision 1.1 2007/02/14 01:20:37 ganovelli working draft of VCG Mesh Image importer and exporter. Does not consider optional attributes. The mesh atributes are only vn and fn (no bbox, texture coordiantes) ****************************************************************************/ #ifndef __VCGLIB_IMPORT_VMI #define __VCGLIB_IMPORT_VMI /* VMI VCG Mesh Image. The vmi image file consists of a header containing the description of the vertex and face type, the length of vectors containing vertices of faces and the memory image of the object mesh as it is when passed to the function Save(SaveMeshType m) NOTE: THIS IS NOT A FILE FORMAT. IT IS ONLY USEFUL FOR DUMPING MESH IMAGES FOR DEBUG PURPOSE. Example of use: say you are running a time consuming mesh processing and you want to save intermediate state, but no file format support all the attributes you need in your vertex/face type. NOTE2: At the present if you add members to your TriMesh these will NOT be saved. More precisely, this file and import_vmi must be updated to reflect changes in vcg/complex/trimesh/base.h */ namespace vcg { namespace tri { namespace io { template struct DummyType{ char placeholder[N]; }; /* ------------------------- derivation chain for the vertex attribute ---------------------------*/ /** this class is for testing only the equality with the type optionally provided by the user when calling Open */ template struct Der:public T{ typedef typename std::set::iterator HWIte; template static void AddAttrib(MeshType &m, const char * name, unsigned int s, void * data){ switch(VoF) { case 0: if(s == sizeof(A)){ typename MeshType::template PerVertexAttributeHandle h = vcg::tri::Allocator:: template AddPerVertexAttribute(m,name); for(unsigned int i = 0; i < m.vert.size(); ++i) memcpy(&h[i], (void*) &((A*)data)[i],sizeof(A)); // we don't want the type conversion } else T::template AddAttrib<0>(m,name,s,data); break; case 1: if(s == sizeof(A)){ typename MeshType::template PerFaceAttributeHandle h = vcg::tri::Allocator:: template AddPerFaceAttribute(m,name); for(unsigned int i = 0; i < m.face.size(); ++i) memcpy(&h[i], (void*) &((A*)data)[i],sizeof(A)); // we don't want the type conversion } else T::template AddAttrib<0>(m,name,s,data); break; case 2: if(s == sizeof(A)){ typename MeshType::template PerMeshAttributeHandle h = vcg::tri::Allocator:: template AddPerMeshAttribute(m,name); memcpy(&h(), (void*) ((A*)data),sizeof(A)); // we don't want the type conversion } else T::template AddAttrib<2>(m,name,s,data); break; default:break; } } }; /** this class is for testing the list of increasing size types until one is larger than the size of the unknown type */ template struct DerK:public T{ typedef typename std::set::iterator HWIte; template static void AddAttrib(MeshType &m, const char * name, unsigned int s, void * data){ switch(VoF){ case 0: if(s == sizeof(A)){ typename MeshType::template PerVertexAttributeHandle h = vcg::tri::Allocator::template AddPerVertexAttribute(m,name); for(unsigned int i = 0; i < m.vert.size(); ++i) memcpy((void*) &(h[i]), (void*) &((A*)data)[i],sizeof(A)); // we don't want the type conversion } else if(s < sizeof(A)){ // padding int padd = sizeof(A) - s; typename MeshType::template PerVertexAttributeHandle h = vcg::tri::Allocator::template AddPerVertexAttribute(m,name); for(unsigned int i = 0; i < m.vert.size(); ++i){ char * dest = &((char*)(&h[i]))[0]; memcpy( (void *)dest , (void*) &((A*)data)[i],s); // we don't want the type conversion } typename MeshType::PointerToAttribute pa; pa._name = std::string(name); HWIte res = m.vert_attr.find(pa); pa = *res; m.vert_attr.erase(res); pa._padding = padd; std::pair new_pa = m.vert_attr.insert(pa); assert(new_pa.second); } else T::template AddAttrib<0>(m,name,s,data); break; case 1: if(s == sizeof(A)){ typename MeshType::template PerVertexAttributeHandle h = vcg::tri::Allocator::template AddPerVertexAttribute(m,name); for(unsigned int i = 0; i < m.vert.size(); ++i) memcpy((void*) &(h[i]), (void*) &((A*)data)[i],sizeof(A)); // we don't want the type conversion } else if(s < sizeof(A)){ // padding int padd = sizeof(A) - s; typename MeshType::template PerFaceAttributeHandle h = vcg::tri::Allocator::template AddPerFaceAttribute(m,name); for(unsigned int i = 0; i < m.face.size(); ++i){ char * dest = &((char*)(&h[i]))[0]; memcpy( (void *)dest , (void*) &((A*)data)[i],s); // we don't want the type conversion } typename MeshType::PointerToAttribute pa; pa._name = std::string(name); HWIte res = m.face_attr.find(pa); pa = *res; m.face_attr.erase(res); pa._padding = padd; std::pair new_pa = m.face_attr.insert(pa); assert(new_pa.second); } else T::template AddAttrib<1>(m,name,s,data); break; case 2: if(s == sizeof(A)){ typename MeshType::template PerMeshAttributeHandle h = vcg::tri::Allocator::template AddPerMeshAttribute(m,name); memcpy((void*)&h(), (void*)((A*)data),sizeof(A)); // we don't want the type conversion } else if(s < sizeof(A)){ // padding int padd = sizeof(A) - s; typename MeshType::template PerMeshAttributeHandle h = vcg::tri::Allocator::template AddPerMeshAttribute(m,name); char * dest = & ((char*)(&h()))[0]; memcpy( (void *)dest , (void*)((A*)data),s); // we don't want the type conversion typename MeshType::PointerToAttribute pa; pa._name = std::string(name); HWIte res = m.mesh_attr.find(pa); pa = *res; m.mesh_attr.erase(res); pa._padding = padd; std::pair new_pa = m.mesh_attr.insert(pa); assert(new_pa.second); } else T::template AddAttrib<2>(m,name,s,data); break; default: assert(0);break; } } }; /** This is the templated derivation chain */ template struct K { template static void AddAttrib(MeshType &m, const char * name, unsigned int s, void * data){ // if yohu got this your attribute is larger than 1048576. Honestly... assert(0); } }; template struct K0 : public DerK< MeshType, B0, K > {}; template struct K1 : public DerK< MeshType, B1, K0 > {}; template struct K2 : public DerK< MeshType, B2, K1 > {}; template struct K3 : public DerK< MeshType, B3, K2 > {}; template struct K4 : public DerK< MeshType, B4, K3 > {}; template struct K5 : public DerK< MeshType, B5, K4 > {}; template struct K6 : public DerK< MeshType, B6, K5 > {}; template struct K7 : public DerK< MeshType, B7, K6 > {}; template struct K8 : public DerK< MeshType, B8, K7 > {}; template struct K9 : public DerK< MeshType, B9, K8 > {}; template struct K10 : public DerK< MeshType, B10, K9 > {}; template struct K11 : public DerK< MeshType, B11, K10 > {}; template struct K12 : public DerK< MeshType, B12, K11 > {}; template , class B1 = DummyType<2048>, class B2 = DummyType<1024>, class B3 = DummyType<512>, class B4 = DummyType<256>, class B5 = DummyType<128>, class B6 = DummyType<64>, class B7 = DummyType<32>, class B8 = DummyType<16>, class B9 = DummyType<8>, class B10 = DummyType<4>, class B11 = DummyType<2>, class B12 = DummyType<1> > struct C0 : public DerK< MeshType, A0, K12 > {}; template struct C1 : public Der< MeshType, A1, C0 > {}; template struct C2 : public Der< MeshType, A2, C1 > {}; template struct C3 : public Der< MeshType, A3, C2 > {}; template struct AttrAll : public Der< MeshType, A4, C3 > {}; struct ReadString{ ReadString(FILE * f,std::string & out){ unsigned int l; fread(&l,4,1,f); char * buf = new char[l+1]; fread(buf,1,l,f);buf[l]='\0'; out = std::string(buf); delete [] buf; } }; struct ReadInt{ ReadInt(FILE *f, unsigned int & i){ fread(&i,1,4,f);}}; template struct LoadVertexOcf{ LoadVertexOcf(FILE*f,const CONT & vert){ // do nothing, it is a std::vector } }; template struct LoadVertexOcf >{ typedef typename OpenMeshType::VertexType VertexType; LoadVertexOcf( FILE * f, vertex::vector_ocf & vert){ std::string s; // vertex quality ReadString(f,s); if( s == std::string("HAS_VERTEX_QUALITY_OCF")) { vert.EnableQuality(); fread((void*)&vert.QV[0],sizeof(VertexType::QualityType),vert.size(),f); } // vertex color ReadString(f,s); if( s == std::string("HAS_VERTEX_COLOR_OCF")) { vert.EnableColor(); fread((void*)&vert.CV[0],sizeof(VertexType::ColorType),vert.size(),f); } // vertex normal ReadString(f,s); if( s == std::string("HAS_VERTEX_NORMAL_OCF")) { vert.EnableNormal(); fread((void*)&vert.NV[0],sizeof(VertexType::NormalType),vert.size(),f); } // vertex mark ReadString(f,s); if( s == std::string("HAS_VERTEX_MARK_OCF")) { vert.EnableMark(); fread((void*)&vert.MV[0],sizeof(VertexType::MarkType),vert.size(),f); } // vertex texcoord ReadString(f,s); if( s == std::string("HAS_VERTEX_TEXCOORD_OCF")) { vert.EnableTexCoord(); fread((void*)&vert.TV[0],sizeof(vertex::vector_ocf::TexCoordType),vert.size(),f); } // vertex-face adjacency ReadString(f,s); if( s == std::string("HAS_VERTEX_VFADJACENCY_OCF")) { vert.EnableVFAdjacency(); fread((void*)&vert.AV[0],sizeof(vertex::vector_ocf::VFAdjType),vert.size(),f); } // vertex curvature ReadString(f,s); if( s == std::string("HAS_VERTEX_CURVATURE_OCF")) { vert.EnableCurvature(); fread((void*)&vert.CuV[0],sizeof(VertexType::CurvatureType),vert.size(),f); } // vertex curvature dir ReadString(f,s); if( s == std::string("HAS_VERTEX_CURVATUREDIR_OCF")) { vert.EnableCurvatureDir(); fread((void*)&vert.CuDV[0],sizeof(VertexType::CurvatureDirType),vert.size(),f); } // vertex radius ReadString(f,s); if( s == std::string("HAS_VERTEX_RADIUS_OCF")) { vert.EnableRadius(); fread((void*)&vert.RadiusV[0],sizeof(vertex::vector_ocf::RadiusType),vert.size(),f); } } }; template struct LoadFaceOcf{ LoadFaceOcf(FILE * f, const CONT & face){ // do nothing, it is a std::vector } }; /* partial specialization for vector_ocf */ template struct LoadFaceOcf< OpenMeshType, face::vector_ocf >{ typedef typename OpenMeshType::FaceType FaceType; LoadFaceOcf( FILE * f, face::vector_ocf & face){ std::string s; // face quality ReadString(f,s); if( s == std::string("HAS_FACE_QUALITY_OCF")) { face.EnableQuality(); fread((void*)&face.QV[0],sizeof(FaceType::QualityType),face.size(),f); } // face color ReadString(f,s); if( s == std::string("HAS_FACE_COLOR_OCF")) { face.EnableColor(); fread((void*)&face.CV[0],sizeof(FaceType::ColorType),face.size(),f); } // face normal ReadString(f,s); if( s == std::string("HAS_FACE_NORMAL_OCF")) { face.EnableNormal(); fread((void*)&face.NV[0],sizeof(FaceType::NormalType),face.size(),f); } // face mark ReadString(f,s); if( s == std::string("HAS_FACE_MARK_OCF")) { face.EnableMark(); fread((void*)&face.MV[0],sizeof(FaceType::MarkType),face.size(),f); } // face wedgetexcoord ReadString(f,s); if( s == std::string("HAS_FACE_WEDGETEXCOORD_OCF")) { face.EnableWedgeTex(); fread((void*)&face.WTV[0],sizeof(FaceType::WedgeTexCoordType),face.size(),f); } // face-face adjacency ReadString(f,s); if( s == std::string("HAS_FACE_FFADJACENCY_OCF")) { face.EnableFFAdjacency(); fread((void*)&face.AF[0],sizeof(face::vector_ocf::AdjTypePack),face.size(),f); } // vertex-face adjacency ReadString(f,s); if( s == std::string("HAS_FACE_VFADJACENCY_OCF")) { face.EnableVFAdjacency(); fread((void*)&face.AV[0],sizeof(face::vector_ocf::AdjTypePack),face.size(),f); } // face WedgeColor ReadString(f,s); if( s == std::string("HAS_FACE_WEDGECOLOR_OCF")) { face.EnableWedgeColor(); fread((void*)&face.WCV[0],sizeof(face::vector_ocf::WedgeColorTypePack),face.size(),f); } // face WedgeNormal ReadString(f,s); if( s == std::string("HAS_FACE_WEDGENORMAL_OCF")) { face.EnableWedgeNormal(); fread((void*)&face.WNV[0],sizeof(face::vector_ocf::WedgeNormalTypePack),face.size(),f); } } }; template class ImporterVMI: public AttrAll { public: static FILE *& F(){static FILE * f; return f;} static void * Malloc(unsigned int n){ return (n)?malloc(n):0;} static void Free(void * ptr){ if(ptr) free (ptr);} typedef typename OpenMeshType::FaceType FaceType; typedef typename OpenMeshType::FaceContainer FaceContainer; typedef typename OpenMeshType::FaceIterator FaceIterator; typedef typename OpenMeshType::VertContainer VertContainer; typedef typename OpenMeshType::VertexIterator VertexIterator; typedef typename OpenMeshType::VertexType VertexType; static bool GetHeader(std::vector& fnameV, std::vector& fnameF, unsigned int & vertSize, unsigned int &faceSize){ std::string name; unsigned int nameFsize,nameVsize,i; ReadString(F(),name); ReadInt(F(),nameFsize); for(i=0; i < nameFsize; ++i) {ReadString(F(), name);fnameF.push_back( name );} ReadString(F(),name); ReadInt(F() , faceSize); ReadString(F(), name); ReadInt(F(),nameVsize); for(i=0; i < nameVsize; ++i) {ReadString(F(), name) ;fnameV.push_back( name);} ReadString(F(),name); ReadInt(F(),vertSize); ReadString(F(),name); assert(strstr( name.c_str(),"end_header")!=NULL); return true; } static bool GetHeader(char * filename,std::vector& nameV, std::vector& nameF, int & vertSize, int &faceSize){ F() = fopen(filename,"rb"); return GetHeader(F(),nameV, nameF, vertSize, faceSize); fclose(F()); } static bool Open(OpenMeshType &m,char * filename){ typedef typename OpenMeshType::VertexType VertexType; typedef typename OpenMeshType::FaceType FaceType; typename OpenMeshType::FaceIterator fi; typename OpenMeshType::VertexIterator vi; F() = fopen(filename,"rb"); std::vector nameF,nameV,fnameF,fnameV; unsigned int vertSize,faceSize; /* read the header */ GetHeader(fnameV, fnameF, vertSize, faceSize); /* read the mesh type */ OpenMeshType::FaceType::Name(nameF); OpenMeshType::VertexType::Name(nameV); /* check if the type is the very same, otherwise return */ if(fnameV != nameV) return false; if(fnameF != nameF) return false; int offsetV,offsetF; if(vertSize!=0) /* read the address of the first vertex */ fread(&offsetV,sizeof( int),1,F()); if(faceSize!=0) /* read the address of the first face */ fread(&offsetF,sizeof( int),1,F()); /* read the object mesh */ fread(&m.shot,sizeof(Shot),1,F()); fread(&m.vn,sizeof(int),1,F()); fread(&m.fn,sizeof(int),1,F()); fread(&m.imark,sizeof(int),1,F()); fread(&m.bbox,sizeof(Box3),1,F()); fread(&m.C(),sizeof(Color4b),1,F()); /* resize the vector of vertices */ m.vert.resize(vertSize); int read = 0; /* load the vertices */ if(vertSize>0){ read=fread((void*)& m.vert[0],sizeof(VertexType),vertSize,F()); assert(ferror(F())==0); assert(read==vertSize); LoadVertexOcf(F(),m.vert); } read = 0; m.face.resize(faceSize); if(faceSize>0){ /* load the faces */ read = fread((void*)& m.face[0],sizeof(FaceType),faceSize,F()); assert(ferror(F())==0); assert(!feof(F())); assert(read==faceSize); LoadFaceOcf(F(),m.face); } /* load the per vertex attributes */ std::string _string,_trash; unsigned int n,sz; ReadString(F(),_trash); ReadInt(F(),n); for(int ia = 0 ; ia < n; ++ia){ ReadString(F(),_trash); ReadString(F(),_string); ReadString(F(),_trash); ReadInt(F(),sz); void * data = Malloc(sz*m.vert.size()); fread(data,sz,m.vert.size(),F()); AttrAll::template AddAttrib<0>(m,_string.c_str(),sz,data); Free(data); } /* load the per face attributes */ ReadString(F(),_trash); ReadInt(F(),n); for(int ia = 0 ; ia < n; ++ia){ ReadString(F(),_trash); ReadString(F(),_string); ReadString(F(),_trash); ReadInt(F(),sz); void * data = Malloc(sz*m.face.size()); fread(data,sz,m.face.size(),F()); AttrAll::template AddAttrib<1>(m,_string.c_str(),sz,data); Free(data); } /* load the per mesh attributes */ ReadString(F(),_trash); ReadInt(F(),n); for(unsigned int ia = 0 ; ia < n; ++ia){ ReadString(F(),_trash); ReadString(F(),_string); ReadString(F(),_trash); ReadInt(F(),sz); void * data = Malloc(sz); fread(data,1,sz,F()); AttrAll::template AddAttrib<2>(m,_string.c_str(),sz,data); Free(data); } if(FaceType::HasVFAdjacency()) for(vi = m.vert.begin(); vi != m.vert.end(); ++vi){ (*vi).VFp() = (*vi).VFp()-(FaceType*)offsetF+ &m.face[0]; (*vi).VFp() = (*vi).VFp()-(FaceType*)offsetF+ &m.face[0]; (*vi).VFp() = (*vi).VFp()-(FaceType*)offsetF+ &m.face[0]; } if(FaceType::HasVertexRef()) for(fi = m.face.begin(); fi != m.face.end(); ++fi){ (*fi).V(0) = (*fi).V(0)-(VertexType*)offsetV+ &m.vert[0]; (*fi).V(1) = (*fi).V(1)-(VertexType*)offsetV+ &m.vert[0]; (*fi).V(2) = (*fi).V(2)-(VertexType*)offsetV+ &m.vert[0]; } if(FaceType::HasFFAdjacency()) for(fi = m.face.begin(); fi != m.face.end(); ++fi){ (*fi).FFp(0) = (*fi).FFp(0)-(FaceType*)offsetF+ &m.face[0]; (*fi).FFp(1) = (*fi).FFp(1)-(FaceType*)offsetF+ &m.face[0]; (*fi).FFp(2) = (*fi).FFp(2)-(FaceType*)offsetF+ &m.face[0]; } if(FaceType::HasVFAdjacency()) for(fi = m.face.begin(); fi != m.face.end(); ++fi){ (*fi).VFp(0) = (*fi).VFp(0)-(FaceType*)offsetF+ &m.face[0]; (*fi).VFp(1) = (*fi).VFp(1)-(FaceType*)offsetF+ &m.face[0]; (*fi).VFp(2) = (*fi).VFp(2)-(FaceType*)offsetF+ &m.face[0]; } fclose(F()); return true; } }; // end class } // end Namespace tri } // end Namespace io } // end Namespace vcg #endif