Debian Codegraph @

vcg/wrap/io_trimesh/import_vmi.h

raw symbols SCIP
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23
24 /****************************************************************************
25 History
26
27 $Log: import_vmi.h,v $
28 Revision 1.2 2007/02/21 10:16:49 ganovelli
29 added GetHeader
30
31 Revision 1.1 2007/02/14 01:20:37 ganovelli
32 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)
33
34
35
36 ****************************************************************************/
37
38 #ifndef __VCGLIB_IMPORT_VMI
39 #define __VCGLIB_IMPORT_VMI
40
41 /*
42 VMI VCG Mesh Image.
43 The vmi image file consists of a header containing the description of the vertex and face type,
44 the length of vectors containing vertices of faces and the memory image of the object mesh as it is when
45 passed to the function Save(SaveMeshType m)
46 */
47
48 namespace vcg {
49 namespace tri {
50 namespace io {
51
52 template <class OpenMeshType>
53 class ImporterVMI
54 {
55 public:
56 typedef typename OpenMeshType::FaceIterator FaceIterator;
57 typedef typename OpenMeshType::VertexIterator VertexIterator;
58 typedef typename OpenMeshType::VertexType VertexType;
59
60 static bool GetHeader(FILE * f,std::vector<std::string>& fnameV, std::vector<std::string>& fnameF, int & vertSize, int &faceSize){
61 char name[100];
62 char buf[4096];
63 int nameFsize,nameVsize,i;
64 fgets(buf,4096,f);
65 sscanf(buf,"%s %d",&name[0],&nameFsize);
66 for(i=0; i < nameFsize; ++i) {
67 fgets(buf,4096,f);
68 sscanf(buf,"%s ",&name[0]);fnameF.push_back(std::string(name));
69 }
70 fgets(buf,4096,f);
71 sscanf(buf,"%s %d",&name[0],&faceSize);
72
73 fgets(buf,4096,f);
74 sscanf(buf,"%s %d",&name[0],&nameVsize);
75 for(i=0; i < nameVsize; ++i) {
76 fgets(buf,4096,f);
77 sscanf(buf,"%s ",&name[0]);fnameV.push_back(std::string(name));}
78 fgets(buf,4096,f);
79 sscanf(buf,"%s %d",&name[0],&vertSize);
80 fgets(buf,4096,f);
81 assert(strstr(buf,"end_header")!=NULL);
82 return true;
83 }
84
85 static bool GetHeader(char * filename,std::vector<std::string>& nameV, std::vector<std::string>& nameF, int & vertSize, int &faceSize){
86 FILE * f = fopen(filename,"rb");
87 return GetHeader(f,nameV, nameF, vertSize, faceSize);
88 fclose(f);
89 }
90 static bool Open(OpenMeshType &m,char * filename){
91 int i;
92 OpenMeshType::FaceIterator fi;
93 OpenMeshType::VertexIterator vi;
94 FILE * f = fopen(filename,"rb");
95 std::vector<string> nameF,nameV,fnameF,fnameV;
96 int vertSize,faceSize;
97
98 /* read the header */
99 GetHeader(f,fnameV, fnameF, vertSize, faceSize);
100
101 /* read the mesh type */
102 OpenMeshType::FaceType::Name(nameF);
103 OpenMeshType::VertexType::Name(nameV);
104
105 /* check if the type is the very same, otherwise return */
106 if(fnameV != nameV) return false;
107 if(fnameF != nameF) return false;
108
109 int offsetV,offsetF;
110 /* read the address of the first vertex */
111 fread(&offsetV,sizeof( int),1,f);
112
113 /* read the address of the first face */
114 fread(&offsetF,sizeof( int),1,f);
115
116 /* read the object mesh */
117 fread(&m,sizeof(OpenMeshType),1,f);
118
119 /* overwrite che container because they could be inconsistent */
120 OpenMeshType::VertContainer tvc;
121 OpenMeshType::FaceContainer tfc;
122 memcpy(&m.vert,&tvc,sizeof(OpenMeshType::VertContainer));
123 memcpy(&m.face,&tfc,sizeof(OpenMeshType::FaceContainer));
124
125 m.vert.resize(vertSize);
126
127 int read;
128 /* load the vertices */
129 read=fread((void*)& m.vert[0],sizeof(OpenMeshType::VertexType),vertSize,f);
130 assert(ferror(f)==0);
131 assert(read==vertSize);
132
133 m.face.resize(faceSize);
134 /* load the faces */
135 read = fread((void*)& m.face[0],sizeof(OpenMeshType::FaceType),faceSize,f);
136 assert(ferror(f)==0);
137 assert(!feof(f));
138 assert(read==faceSize);
139
140 if(OpenMeshType::FaceType::HasVFAdjacency())
141 for(vi = m.vert.begin(); vi != m.vert.end(); ++vi){
142 (*vi).VFp() = (*vi).VFp()-(OpenMeshType::FaceType*)offsetF+ &m.face[0];
143 (*vi).VFp() = (*vi).VFp()-(OpenMeshType::FaceType*)offsetF+ &m.face[0];
144 (*vi).VFp() = (*vi).VFp()-(OpenMeshType::FaceType*)offsetF+ &m.face[0];
145 }
146
147 if(OpenMeshType::FaceType::HasVertexRef())
148 for(fi = m.face.begin(); fi != m.face.end(); ++fi){
149 (*fi).V(0) = (*fi).V(0)-(OpenMeshType::VertexType*)offsetV+ &m.vert[0];
150 (*fi).V(1) = (*fi).V(1)-(OpenMeshType::VertexType*)offsetV+ &m.vert[0];
151 (*fi).V(2) = (*fi).V(2)-(OpenMeshType::VertexType*)offsetV+ &m.vert[0];
152 }
153
154 if(OpenMeshType::FaceType::HasFFAdjacency())
155 for(fi = m.face.begin(); fi != m.face.end(); ++fi){
156 (*fi).FFp(0) = (*fi).FFp(0)-(OpenMeshType::FaceType*)offsetF+ &m.face[0];
157 (*fi).FFp(1) = (*fi).FFp(1)-(OpenMeshType::FaceType*)offsetF+ &m.face[0];
158 (*fi).FFp(2) = (*fi).FFp(2)-(OpenMeshType::FaceType*)offsetF+ &m.face[0];
159 }
160
161 if(OpenMeshType::FaceType::HasVFAdjacency())
162 for(fi = m.face.begin(); fi != m.face.end(); ++fi){
163 (*fi).VFp(0) = (*fi).VFp(0)-(OpenMeshType::FaceType*)offsetF+ &m.face[0];
164 (*fi).VFp(1) = (*fi).VFp(1)-(OpenMeshType::FaceType*)offsetF+ &m.face[0];
165 (*fi).VFp(2) = (*fi).VFp(2)-(OpenMeshType::FaceType*)offsetF+ &m.face[0];
166 }
167
168 fclose(f);
169 }
170
171 }; // end class
172
173 } // end Namespace tri
174 } // end Namespace io
175 } // end Namespace vcg
176
177 #endif