added support for vertex attributes

experimental. next: test / factorize / extend to faces and edges
This commit is contained in:
ganovelli 2009-07-29 13:44:00 +00:00
parent 286ac9162a
commit 8484f4522e
2 changed files with 462 additions and 304 deletions

View File

@ -1,118 +1,161 @@
/**************************************************************************** /****************************************************************************
* VCGLib o o * * VCGLib o o *
* Visual and Computer Graphics Library o o * * Visual and Computer Graphics Library o o *
* _ O _ * * _ O _ *
* Copyright(C) 2004 \/)\/ * * Copyright(C) 2004 \/)\/ *
* Visual Computing Lab /\/| * * Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | * * ISTI - Italian National Research Council | *
* \ * * \ *
* All rights reserved. * * All rights reserved. *
* * * *
* This program is free software; you can redistribute it and/or modify * * 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 * * it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or * * the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. * * (at your option) any later version. *
* * * *
* This program is distributed in the hope that it will be useful, * * This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of * * but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. * * for more details. *
* * * *
****************************************************************************/ ****************************************************************************/
/**************************************************************************** /****************************************************************************
History History
$Log: not supported by cvs2svn $ $Log: not supported by cvs2svn $
Revision 1.1 2007/02/14 01:20:37 ganovelli 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) 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_EXPORT_VMI #ifndef __VCGLIB_EXPORT_VMI
#define __VCGLIB_EXPORT_VMI #define __VCGLIB_EXPORT_VMI
/* /*
VMI VCG Mesh Image. VMI VCG Mesh Image.
The vmi image file consists of a header containing the description of the vertex and face type, 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 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). 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. 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 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. 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 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 import_vmi must be updated to reflect changes in vcg/complex/trimesh/base.h
*/ */
namespace vcg { namespace vcg {
namespace tri { namespace tri {
namespace io { namespace io {
template <class SaveMeshType> template <int N> struct PlaceHolderType{ char A[N];};
class ExporterVMI
{ template <class SaveMeshType>
public: class ExporterVMI
typedef typename SaveMeshType::FaceIterator FaceIterator; {
typedef typename SaveMeshType::VertexIterator VertexIterator; public:
typedef typename SaveMeshType::VertexType VertexType; typedef typename SaveMeshType::FaceIterator FaceIterator;
typedef typename SaveMeshType::VertexIterator VertexIterator;
static void Save(const SaveMeshType &m,char * filename){ typedef typename SaveMeshType::VertexType VertexType;
unsigned int i; typedef SimpleTempDataBase<typename SaveMeshType::VertContainer> STDB;
int vertSize,faceSize;
FILE * f = fopen(filename,"wb"); static void Save(const SaveMeshType &m,char * filename){
std::vector<std::string> nameF,nameV; unsigned int i;
SaveMeshType::FaceType::Name(nameF); int vertSize,faceSize;
SaveMeshType::VertexType::Name(nameV); FILE * f = fopen(filename,"wb");
vertSize = m.vert.size(); std::vector<std::string> nameF,nameV;
faceSize = m.face.size(); SaveMeshType::FaceType::Name(nameF);
SaveMeshType::VertexType::Name(nameV);
/* write header */ vertSize = m.vert.size();
fprintf(f,"FACE_TYPE %d\n",nameF.size()); faceSize = m.face.size();
for(i=0; i < nameF.size(); ++i) fprintf(f,"%s\n",nameF[i].c_str());
fprintf(f,"SIZE_VECTOR_FACES %d\n",faceSize); /* write header */
fprintf(f,"FACE_TYPE %d\n",nameF.size());
fprintf(f,"VERTEX_TYPE %d\n",nameV.size()); for(i=0; i < nameF.size(); ++i) fprintf(f,"%s\n",nameF[i].c_str());
for(i=0; i < nameV.size(); ++i) fprintf(f,"%s\n",nameV[i].c_str()); fprintf(f,"SIZE_VECTOR_FACES %d\n",faceSize);
fprintf(f,"SIZE_VECTOR_VERTS %d\n",vertSize);
fprintf(f,"end_header\n"); fprintf(f,"VERTEX_TYPE %d\n",nameV.size());
for(i=0; i < nameV.size(); ++i) fprintf(f,"%s\n",nameV[i].c_str());
fprintf(f,"SIZE_VECTOR_VERTS %d\n",vertSize);
unsigned int offsetV = (unsigned int) &m.vert[0]; fprintf(f,"end_header\n");
/* write the address of the first vertex */
fwrite(&offsetV,sizeof(unsigned int),1,f);
if(vertSize!=0){
int offsetF= ( int) &m.face[0]; unsigned int offsetV = (unsigned int) &m.vert[0];
/* write the address of the first face */ /* write the address of the first vertex */
fwrite(&offsetF,sizeof( int),1,f); fwrite(&offsetV,sizeof(unsigned int),1,f);
}
/* save the object mesh */
fwrite(&m.shot,sizeof(Shot<typename SaveMeshType::ScalarType>),1,f); if(faceSize!=0){
fwrite(&m.vn,sizeof(int),1,f); int offsetF= ( int) &m.face[0];
fwrite(&m.fn,sizeof(int),1,f); /* write the address of the first face */
fwrite(&m.imark,sizeof(int),1,f); fwrite(&offsetF,sizeof( int),1,f);
fwrite(&m.bbox,sizeof(Box3<typename SaveMeshType::ScalarType>),1,f); }
fwrite(&m.C(),sizeof(Color4b),1,f); /* save the object mesh */
fwrite(&m.shot,sizeof(Shot<typename SaveMeshType::ScalarType>),1,f);
int written; fwrite(&m.vn,sizeof(int),1,f);
/* save the vertices */ fwrite(&m.fn,sizeof(int),1,f);
written = fwrite((void*)&m.vert[0],sizeof(typename SaveMeshType::VertexType),m.vert.size(),f); fwrite(&m.imark,sizeof(int),1,f);
assert(written==m.vert.size()); fwrite(&m.bbox,sizeof(Box3<typename SaveMeshType::ScalarType>),1,f);
fwrite(&m.C(),sizeof(Color4b),1,f);
/* save the faces */
written = fwrite((void*)&m.face[0],sizeof(typename SaveMeshType::FaceType),m.face.size(),f); int written;
assert(written==m.face.size());
if(vertSize!=0){
// fflush(f); /* save the vertices */
fclose(f); written = fwrite((void*)&m.vert[0],sizeof(typename SaveMeshType::VertexType),m.vert.size(),f);
} assert(written==m.vert.size());
}
}; // end class
if(faceSize!=0){
} // end Namespace tri /* save the faces */
} // end Namespace io written = fwrite((void*)&m.face[0],sizeof(typename SaveMeshType::FaceType),m.face.size(),f);
} // end Namespace vcg assert(written==m.face.size());
}
#endif
/* save the attribtues */
typename std::set< SaveMeshType::PointerToAttribute>::const_iterator ai;
/* save the per vertex attributes */
fprintf(f,"N_PER_VERTEX_ATTRIBUTES %d \n",m.vert_attr.size());
for(ai = m.vert_attr.begin(); ai != m.vert_attr.end(); ++ai){
STDB * stdb = (STDB *) (*ai)._handle;
fprintf(f,"PER_VERTEX_ATTR_NAME %s \n",(*ai)._name.c_str());
fprintf(f,"PER_VERTEX_ATTR_SIZE %d \n",stdb->SizeOf() );
fwrite(stdb->DataBegin(),m.vert.size(),stdb->SizeOf(),f);
}
///* save the per face attributes */
//fprintf(f,"N_PER_FACE_ATTRIBUTES %d\n",m.face_attr.size());
//for(ai = m.face_attr.begin(); ai != m.face_attr.end(); ++ai){
// SimpleTempDataBase<SaveMeshType::FaceContainer>* handle;
// fprintf(f,"PER_FACE_ATTR_NAME %s\n",(*ai)._name.c_str());
// fprintf(f,"PER_FACE_ATTR_SIZE %d\n",(*ai)._handle->SizeOf() );
// fwrite((*ai)._handle->DataBegin(),m.face.size(),(*ai)._handle->SizeOf(),f);
// }
///* save the per mesh attributes */
//fprintf(f,"N_PER_MESH_ATTRIBUTES %d\n",m.mesh_attr.size());
//for(ai = m.mesh_attr.begin(); ai != m.mesh_attr.end(); ++ai){
// AttributeBase * handle = (AttributeBase *) (*ai)._handle ;
// fprintf(f,"PER_MESH_ATTR_NAME %s\n",(*ai)._name.c_str());
// fprintf(f,"PER_MESH_ATTR_SIZE %d\n",(*ai)._handle->SizeOf() );
// fwrite((*ai)._handle->DataBegin(),1,(*ai)._handle->SizeOf(),f);
// }
// fflush(f);
fclose(f);
}
}; // end class
} // end Namespace tri
} // end Namespace io
} // end Namespace vcg
#endif

View File

@ -1,186 +1,301 @@
/**************************************************************************** /****************************************************************************
* VCGLib o o * * VCGLib o o *
* Visual and Computer Graphics Library o o * * Visual and Computer Graphics Library o o *
* _ O _ * * _ O _ *
* Copyright(C) 2004 \/)\/ * * Copyright(C) 2004 \/)\/ *
* Visual Computing Lab /\/| * * Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | * * ISTI - Italian National Research Council | *
* \ * * \ *
* All rights reserved. * * All rights reserved. *
* * * *
* This program is free software; you can redistribute it and/or modify * * 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 * * it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or * * the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. * * (at your option) any later version. *
* * * *
* This program is distributed in the hope that it will be useful, * * This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of * * but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. * * for more details. *
* * * *
****************************************************************************/ ****************************************************************************/
/**************************************************************************** /****************************************************************************
History History
$Log: not supported by cvs2svn $ $Log: not supported by cvs2svn $
Revision 1.1 2007/02/14 01:20:37 ganovelli 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) 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 #ifndef __VCGLIB_IMPORT_VMI
#define __VCGLIB_IMPORT_VMI #define __VCGLIB_IMPORT_VMI
/* /*
VMI VCG Mesh Image. VMI VCG Mesh Image.
The vmi image file consists of a header containing the description of the vertex and face type, 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 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) 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. 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 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. 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 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 import_vmi must be updated to reflect changes in vcg/complex/trimesh/base.h
*/ */
namespace vcg { namespace vcg {
namespace tri { namespace tri {
namespace io { namespace io {
template <class OpenMeshType> /* derivation chain */
class ImporterVMI template <int N> struct DummyType{ char placeholder[N]; };
{
public: template <class MeshType, class A, class T>
typedef typename OpenMeshType::FaceIterator FaceIterator; struct Der:public T{
typedef typename OpenMeshType::VertexIterator VertexIterator; typedef typename std::set<typename MeshType::PointerToAttribute >::iterator HWIte;
typedef typename OpenMeshType::VertexType VertexType; static void AddAttrib(MeshType &m, char * name, int s, void * data){
if(s == sizeof(A)){
static bool GetHeader(FILE * f,std::vector<std::string>& fnameV, std::vector<std::string>& fnameF, int & vertSize, int &faceSize){ typename MeshType::template PerVertexAttributeHandle<A> h = vcg::tri::Allocator<MeshType>:: template AddPerVertexAttribute<A>(m,name);
char name[100]; for(int i = 0; i < m.vert.size(); ++i)
char buf[4096]; memcpy(&h[i], (void*) &((A*)data)[i],sizeof(A)); // we don't want the type conversion
int nameFsize,nameVsize,i; }
fgets(buf,4096,f); else
sscanf(buf,"%s %d",&name[0],&nameFsize); T::AddAttrib(m,name,s,data);
for(i=0; i < nameFsize; ++i) { }
fgets(buf,4096,f); };
sscanf(buf,"%s ",&name[0]);fnameF.push_back(std::string(name)); template <class MeshType, class A, class T>
} struct DerK:public T{
fgets(buf,4096,f); typedef typename std::set<typename MeshType::PointerToAttribute >::iterator HWIte;
sscanf(buf,"%s %d",&name[0],&faceSize); static void AddAttrib(MeshType &m, char * name, int s, void * data){
if(s == sizeof(A)){
fgets(buf,4096,f); typename MeshType::template PerVertexAttributeHandle<A> h = vcg::tri::Allocator<MeshType>::template AddPerVertexAttribute<A>(m,name);
sscanf(buf,"%s %d",&name[0],&nameVsize); for(unsigned int i = 0; i < m.vert.size(); ++i)
for(i=0; i < nameVsize; ++i) { memcpy((void*) &(h[i]), (void*) &((A*)data)[i],sizeof(A)); // we don't want the type conversion
fgets(buf,4096,f); }
sscanf(buf,"%s ",&name[0]);fnameV.push_back(std::string(name));} else
fgets(buf,4096,f); if(s < sizeof(A)){
sscanf(buf,"%s %d",&name[0],&vertSize); // padding
fgets(buf,4096,f); int padd = sizeof(A) - s;
assert(strstr(buf,"end_header")!=NULL); typename MeshType::template PerVertexAttributeHandle<A> h = vcg::tri::Allocator<MeshType>::template AddPerVertexAttribute<A>(m,name);
return true; for(unsigned int i = 0; i < m.vert.size(); ++i){
} char * dest = &((char*)(&h[i]))[padd];
memcpy( (void *)dest , (void*) &((A*)data)[i],s); // we don't want the type conversion
static bool GetHeader(char * filename,std::vector<std::string>& nameV, std::vector<std::string>& nameF, int & vertSize, int &faceSize){ }
FILE * f = fopen(filename,"rb"); typename MeshType::PointerToAttribute pa;
return GetHeader(f,nameV, nameF, vertSize, faceSize); pa._name = std::string(name);
fclose(f); HWIte res = m.vert_attr.find(pa);
} pa = *res;
static bool Open(OpenMeshType &m,char * filename){ m.vert_attr.erase(res);
int i; pa._padding = padd;
typedef typename OpenMeshType::VertexType VertexType; std::pair<HWIte,bool > new_pa = m.vert_attr.insert(pa);
typedef typename OpenMeshType::FaceType FaceType; assert(new_pa.second);
typename OpenMeshType::FaceIterator fi; }
typename OpenMeshType::VertexIterator vi; else
FILE * f = fopen(filename,"rb"); T::AddAttrib(m,name,s,data);
std::vector<std::string> nameF,nameV,fnameF,fnameV; }
int vertSize,faceSize; };
/* read the header */
GetHeader(f,fnameV, fnameF, vertSize, faceSize); template <class MeshType> struct K {
static void AddAttrib(MeshType &m, char * name, int s, void * data){
/* read the mesh type */
OpenMeshType::FaceType::Name(nameF); assert(0);
OpenMeshType::VertexType::Name(nameV); }
};
/* check if the type is the very same, otherwise return */
if(fnameV != nameV) return false; template <class MeshType, class B0 > struct K0 : public DerK< MeshType, B0, K<MeshType> > {};
if(fnameF != nameF) return false; template <class MeshType, class B0, class B1 > struct K1 : public DerK< MeshType, B1, K0<MeshType, B0> > {};
template <class MeshType, class B0, class B1, class B2 > struct K2 : public DerK< MeshType, B2, K1<MeshType, B0, B1> > {};
int offsetV,offsetF; template <class MeshType, class B0, class B1, class B2,class B3> struct K3 : public DerK< MeshType, B3, K2<MeshType, B0, B1, B2> > {};
/* read the address of the first vertex */ template <class MeshType, class B0, class B1, class B2,class B3,class B4> struct K4 : public DerK< MeshType, B4, K3<MeshType, B0, B1, B2, B3> > {};
fread(&offsetV,sizeof( int),1,f); template <class MeshType, class B0, class B1, class B2,class B3,class B4,class B5> struct K5 : public DerK< MeshType, B5, K4<MeshType, B0, B1, B2, B3, B4> > {};
template <class MeshType, class B0, class B1, class B2,class B3,class B4,class B5,class B6> struct K6 : public DerK< MeshType, B6, K5<MeshType, B0, B1, B2, B3, B4, B5> > {};
/* read the address of the first face */ template <class MeshType, class B0, class B1, class B2,class B3,class B4,class B5,class B6,class B7> struct K7 : public DerK< MeshType, B7, K6<MeshType, B0, B1, B2, B3, B4, B5, B6> > {};
fread(&offsetF,sizeof( int),1,f); template <class MeshType, class B0, class B1, class B2,class B3,class B4,class B5,class B6,class B7,class B8> struct K8 : public DerK< MeshType, B8, K7<MeshType, B0, B1, B2, B3, B4, B5, B6, B7> > {};
template <class MeshType, class B0, class B1, class B2,class B3,class B4,class B5,class B6,class B7,class B8,class B9> struct K9 : public DerK< MeshType, B9, K8<MeshType, B0, B1, B2, B3, B4, B5, B6, B7, B8> > {};
/* read the object mesh */ template <class MeshType, class B0, class B1, class B2,class B3,class B4,class B5,class B6,class B7,class B8,class B9,class B10> struct K10 : public DerK< MeshType, B10, K9<MeshType, B0, B1, B2, B3, B4, B5, B6, B7, B8, B9> > {};
fread(&m.shot,sizeof(Shot<typename OpenMeshType::ScalarType>),1,f); template <class MeshType, class B0, class B1, class B2,class B3,class B4,class B5,class B6,class B7,class B8,class B9,class B10,class B11> struct K11 : public DerK< MeshType, B11, K10<MeshType, B0, B1, B2, B3, B4, B5, B6, B7, B8, B9, B11 > > {};
fread(&m.vn,sizeof(int),1,f); template <class MeshType, class B0, class B1, class B2,class B3,class B4,class B5,class B6,class B7,class B8,class B9,class B10,class B11,class B12>struct K12 : public DerK< MeshType, B12, K11<MeshType, B0, B1, B2, B3, B4, B5, B6, B7, B8, B9, B11, B12 > > {};
fread(&m.fn,sizeof(int),1,f);
fread(&m.imark,sizeof(int),1,f); template <class MeshType, class A0,
fread(&m.bbox,sizeof(Box3<typename OpenMeshType::ScalarType>),1,f); class B0 = DummyType<1048576>,
fread(&m.C(),sizeof(Color4b),1,f); class B1 = DummyType<2048>,
class B2 = DummyType<1024>,
/* resize the vector of vertices */ class B3 = DummyType<512>,
m.vert.resize(vertSize); class B4 = DummyType<256>,
class B5 = DummyType<128>,
int read = 0; class B6 = DummyType<64>,
/* load the vertices */ class B7 = DummyType<32>,
if(vertSize>0) class B8 = DummyType<16>,
read=fread((void*)& m.vert[0],sizeof(VertexType),vertSize,f); class B9 = DummyType<8>,
assert(ferror(f)==0); class B10 = DummyType<4>,
assert(read==vertSize); class B11 = DummyType<2>,
class B12 = DummyType<1>
read = 0; > struct C0 : public DerK< MeshType, A0, K12<MeshType, B0, B1, B2, B3, B4,B5,B6,B7,B8,B9,B10,B11,B12> > {};
m.face.resize(faceSize);
if(faceSize>0) template <class MeshType, class A0, class A1> struct C1 : public Der< MeshType, A1, C0<MeshType, A0> > {};
/* load the faces */ template <class MeshType, class A0, class A1, class A2> struct C2 : public Der< MeshType, A2, C1<MeshType, A0, A1> > {};
read = fread((void*)& m.face[0],sizeof(FaceType),faceSize,f); template <class MeshType, class A0, class A1, class A2,class A3> struct C3 : public Der< MeshType, A3, C2<MeshType, A0, A1, A2> > {};
assert(ferror(f)==0); template <class MeshType, class A0, class A1, class A2,class A3,class A4> struct AttrAll : public Der< MeshType, A4, C3<MeshType, A0, A1, A2, A3> > {};
assert(!feof(f));
assert(read==faceSize);
/* end derivation chain */
if(FaceType::HasVFAdjacency())
for(vi = m.vert.begin(); vi != m.vert.end(); ++vi){ template <class OpenMeshType,class A0 = long, class A1 = double, class A2 = int,class A3 = short, class A4 = char >
(*vi).VFp() = (*vi).VFp()-(FaceType*)offsetF+ &m.face[0]; class ImporterVMI: public AttrAll<OpenMeshType,A0,A1,A2,A3,A4>
(*vi).VFp() = (*vi).VFp()-(FaceType*)offsetF+ &m.face[0]; {
(*vi).VFp() = (*vi).VFp()-(FaceType*)offsetF+ &m.face[0]; public:
} typedef typename OpenMeshType::FaceIterator FaceIterator;
typedef typename OpenMeshType::VertexIterator VertexIterator;
if(FaceType::HasVertexRef()) typedef typename OpenMeshType::VertexType VertexType;
for(fi = m.face.begin(); fi != m.face.end(); ++fi){
(*fi).V(0) = (*fi).V(0)-(VertexType*)offsetV+ &m.vert[0]; static bool GetHeader(FILE * f,std::vector<std::string>& fnameV, std::vector<std::string>& fnameF, int & vertSize, int &faceSize){
(*fi).V(1) = (*fi).V(1)-(VertexType*)offsetV+ &m.vert[0]; char name[100];
(*fi).V(2) = (*fi).V(2)-(VertexType*)offsetV+ &m.vert[0]; char buf[4096];
} int nameFsize,nameVsize,i;
fgets(buf,4096,f);
if(FaceType::HasFFAdjacency()) sscanf(buf,"%s %d",&name[0],&nameFsize);
for(fi = m.face.begin(); fi != m.face.end(); ++fi){ for(i=0; i < nameFsize; ++i) {
(*fi).FFp(0) = (*fi).FFp(0)-(FaceType*)offsetF+ &m.face[0]; fgets(buf,4096,f);
(*fi).FFp(1) = (*fi).FFp(1)-(FaceType*)offsetF+ &m.face[0]; sscanf(buf,"%s ",&name[0]);fnameF.push_back(std::string(name));
(*fi).FFp(2) = (*fi).FFp(2)-(FaceType*)offsetF+ &m.face[0]; }
} fgets(buf,4096,f);
sscanf(buf,"%s %d",&name[0],&faceSize);
if(FaceType::HasVFAdjacency())
for(fi = m.face.begin(); fi != m.face.end(); ++fi){ fgets(buf,4096,f);
(*fi).VFp(0) = (*fi).VFp(0)-(FaceType*)offsetF+ &m.face[0]; sscanf(buf,"%s %d",&name[0],&nameVsize);
(*fi).VFp(1) = (*fi).VFp(1)-(FaceType*)offsetF+ &m.face[0]; for(i=0; i < nameVsize; ++i) {
(*fi).VFp(2) = (*fi).VFp(2)-(FaceType*)offsetF+ &m.face[0]; fgets(buf,4096,f);
} sscanf(buf,"%s ",&name[0]);fnameV.push_back(std::string(name));}
fgets(buf,4096,f);
fclose(f); sscanf(buf,"%s %d",&name[0],&vertSize);
return true; fgets(buf,4096,f);
} assert(strstr(buf,"end_header")!=NULL);
return true;
}; // end class }
} // end Namespace tri static bool GetHeader(char * filename,std::vector<std::string>& nameV, std::vector<std::string>& nameF, int & vertSize, int &faceSize){
} // end Namespace io FILE * f = fopen(filename,"rb");
} // end Namespace vcg return GetHeader(f,nameV, nameF, vertSize, faceSize);
fclose(f);
#endif }
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;
FILE * f = fopen(filename,"rb");
std::vector<std::string> nameF,nameV,fnameF,fnameV;
int vertSize,faceSize;
/* read the header */
GetHeader(f,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<typename OpenMeshType::ScalarType>),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<typename OpenMeshType::ScalarType>),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);
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);
/* load the per vertex attributes */
char _string[65536],_trash[65536];
int n,sz;
fscanf(f,"%s %d",&_trash[0],&n);
for(int ia = 0 ; ia < n; ++ia){
fscanf(f,"%s %s",&_trash[0],&_string[0]);
fscanf(f,"%s %d",&_trash[0],&sz);
void * data = malloc(sz*m.vert.size());
fread(data,sz,m.vert.size(),f);
AddAttrib(m,_string,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