Debian Codegraph @

src/vcglib/wrap/io_trimesh/import_gts.h

raw symbols SCIP
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004-2016 \/)\/ *
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 /* This code has been adapted from the OFF importer and Gael's GTS loader from Expe */
25
26 #ifndef __VCGLIB_IMPORT_GTS
27 #define __VCGLIB_IMPORT_GTS
28
29 #include <fstream>
30 #include <string>
31 #include <vector>
32 #include <assert.h>
33 #include <vcg/space/color4.h>
34 #include <wrap/callback.h>
35 #include <wrap/io_trimesh/io_mask.h>
36
37 namespace vcg
38 {
39 namespace tri
40 {
41 namespace io
42 {
43 // /** \addtogroup */
44 // /* @{ */
45 /**
46 This class encapsulate a filter for importing GTS meshes.
47 The GTS file format of the GNU Triangulated Surface library http://gts.sourceforge.net/
48 */
49 template<class MESH_TYPE>
50 class ImporterGTS
51 {
52 public:
53
54 typedef typename MESH_TYPE::VertexType VertexType;
55 typedef typename MESH_TYPE::VertexIterator VertexIterator;
56 typedef typename MESH_TYPE::VertexPointer VertexPointer;
57 typedef typename MESH_TYPE::FaceType FaceType;
58 typedef typename MESH_TYPE::FaceIterator FaceIterator;
59 typedef typename MESH_TYPE::FacePointer FacePointer;
60 typedef typename MESH_TYPE::CoordType CoordType;
61 typedef typename MESH_TYPE::ScalarType ScalarType;
62
63 enum GTSCodes {NoError=0, CantOpen, InvalidFile,
64 UnsupportedFormat, ErrorNotTriangularFace};
65
66 struct Options
67 {
68 Options()
69 : onlyMaskFlag(false), onlyPoints(false), flipFaces(false)
70 {}
71 bool onlyMaskFlag;
72 bool onlyPoints;
73 bool flipFaces;
74 };
75
76 /*!
77 * Standard call for knowing the meaning of an error code
78 * \param message_code The code returned by <CODE>Open</CODE>
79 * \return The string describing the error code
80 */
81 static const char* ErrorMsg(int message_code)
82 {
83 static const char* error_msg[] =
84 {
85 "No errors", "Can't open file", "Invalid file",
86 "Unsupported format", "Face with more than 3 vertices"
87 };
88
89 if(message_code>4 || message_code<0)
90 return "Unknown error";
91 else
92 return error_msg[message_code];
93 };
94
95 /**
96 * Load only the properties of the 3D objects.
97 *
98 * \param filename the name of the file to read from
99 * \param loadmask the mask which encodes the properties
100 * \return the operation result
101 */
102 static bool LoadMask(const char *filename, int &loadmask)
103 {
104 // To obtain the loading mask all the file must be parsed
105 // to distinguish between per-vertex and per-face color attribute.
106 loadmask=0;
107 MESH_TYPE dummyMesh;
108 return (Open(dummyMesh, filename, loadmask,0,true)==NoError);
109 }
110
111 static int Open(MESH_TYPE &mesh, const char *filename, CallBackPos *cb=0)
112 {
113 int loadmask;
114 return Open(mesh,filename,loadmask,cb);
115 }
116
117 /*!
118 * Standard call for reading a mesh.
119 *
120 * \param mesh the destination mesh
121 * \param filename the name of the file to read from
122 * \return the operation result
123 */
124 static int Open(MESH_TYPE &mesh, const char *filename, int &loadmask,
125 CallBackPos *cb=0, bool onlyMaskFlag=false)
126 {
127 Options opt;
128 opt.onlyMaskFlag = onlyMaskFlag;
129 return Open(mesh, filename, loadmask, opt, cb);
130 }
131
132 static int Open(MESH_TYPE &mesh, const char *filename, int &loadmask,
133 const Options& options, CallBackPos *cb=0)
134 {
135 QFile device(filename);
136 if ( (!device.open(QFile::ReadOnly)) )
137 return CantOpen;
138
139 QTextStream stream(&device);
140
141 loadmask = Mask::IOM_VERTCOORD | Mask::IOM_FACEINDEX;
142
143 int nofVertices, nofFaces, nofEdges;
144 int id0, id1, id2;
145 int eid0, eid1, eid2;
146 typename MESH_TYPE::CoordType v;
147
148 QString line;
149 QStringList sa;
150 bool done = false;
151 do
152 {
153 line = stream.readLine();
154 line = line.trimmed();
155 sa = line.split(' ');
156 if ((!line.startsWith("#")) && (sa.size()>=3))
157 {
158 nofVertices = sa[0].toInt();
159 nofEdges = sa[1].toInt();
160 nofFaces = sa[2].toInt();
161 done = true;
162 }
163 } while (!done);
164
165 if(options.onlyMaskFlag) return NoError;
166
167 int total = nofVertices + nofEdges + nofFaces;
168
169 std::cerr << "GtsMeshReader::nofVertices = " << nofVertices << std::endl;
170 std::cerr << "GtsMeshReader::nofEdges = " << nofEdges << std::endl;
171 std::cerr << "GtsMeshReader::nofFaces = " << nofFaces << std::endl;
172
173 mesh.Clear();
174 VertexIterator v_iter = Allocator<MESH_TYPE>::AddVertices(mesh, nofVertices);
175 for(int i=0 ; i<nofVertices ;)
176 {
177 if (cb && (i%1000)==0)
178 cb(i/total, "Vertex Loading");
179
180 line = stream.readLine().trimmed();
181
182 if (line.startsWith("#"))
183 {
184 continue;
185 }
186
187 sa = line.split(' ');
188 if (!(sa.size()>=3))
189 {
190 std::cerr << "Error parsing vertex " << line.toLocal8Bit().data() << "\n";
191 return InvalidFile;
192 }
193 v.X() = sa[0].toDouble();
194 v.Y() = sa[1].toDouble();
195 v.Z() = sa[2].toDouble();
196
197 (*v_iter).P() = v;
198 ++v_iter;
199 i++;
200 }
201
202 // load edges
203 std::vector< std::pair<int,int> > edges;
204 for(int i=0 ; i<nofEdges ; ++i)
205 {
206 if (cb && (i%1000)==0)
207 cb((i+nofVertices)/total, "Edge Loading");
208
209 line = stream.readLine();
210 if (line.startsWith("#"))
211 {
212 continue;
213 }
214
215 sa = line.split(' ');
216 if (!(sa.size()>=2))
217 {
218 std::cerr << "Error parsing edge " << line.toLocal8Bit().data() << "\n";
219 return InvalidFile;
220 }
221 id0 = sa[0].toInt();
222 id1 = sa[1].toInt();
223
224 edges.push_back(std::pair<int,int>(id0-1,id1-1));
225 }
226
227 // load faces
228 Allocator<MESH_TYPE>::AddFaces(mesh, nofFaces);
229 for(int i=0 ; i<nofFaces ; )
230 {
231 if (cb && (i%1000)==0)
232 cb((nofVertices+nofEdges+i)/total, "Face Loading");
233
234 line = stream.readLine();
235 if (line.startsWith("#"))
236 {
237 continue;
238 }
239
240 sa = line.split(' ');
241 if (!(sa.size()>=3))
242 {
243 std::cerr << "Error parsing face " << line.toLocal8Bit().data() << "\n";
244 return InvalidFile;
245 }
246 eid0 = sa[0].toInt();
247 eid1 = sa[1].toInt();
248 eid2 = sa[2].toInt();
249
250 int e11 = edges[eid0-1].first;
251 int e12 = edges[eid0-1].second;
252 int e21 = edges[eid1-1].first;
253 int e22 = edges[eid1-1].second;
254 if (e12 == e21) {
255 id0 = e11;
256 id1 = e12;
257 id2 = e22;
258 }
259 else if (e12 == e22) {
260 id0 = e11;
261 id1 = e12;
262 id2 = e21;
263 }
264 else if (e11 == e21) {
265 id0 = e12;
266 id1 = e11;
267 id2 = e22;
268 }
269 else {
270 id0 = e12;
271 id1 = e11;
272 id2 = e21;
273 }
274
275 if (options.flipFaces)
276 {
277 mesh.face[i].V(0) = &(mesh.vert[id0]);
278 mesh.face[i].V(1) = &(mesh.vert[id2]);
279 mesh.face[i].V(2) = &(mesh.vert[id1]);
280 }
281 else
282 {
283 mesh.face[i].V(0) = &(mesh.vert[id0]);
284 mesh.face[i].V(1) = &(mesh.vert[id1]);
285 mesh.face[i].V(2) = &(mesh.vert[id2]);
286 }
287
288 ++i;
289 }
290 return NoError;
291 } // end Open
292
293 protected:
294
295 };
296 // /*! @} */
297 } //namespace io
298 }//namespace tri
299 } // namespace vcg
300
301 #endif //__VCGLIB_IMPORT_GTS