Debian Codegraph @

vcg/wrap/io_trimesh/import_stl.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_stl.h,v $
28 Revision 1.16 2008/02/15 09:51:36 cignoni
29 corrected bug in the management of of multiple solid ascii stl files
30
31 Revision 1.15 2007/05/24 06:56:54 cignoni
32 Corrected gcc warning
33
34 Revision 1.14 2006/06/10 12:49:05 mariolatronico
35 file length is now computed using fseek and ftell
36
37 Revision 1.13 2006/06/06 14:35:32 zifnab1974
38 Changes for compilation on linux AMD64. Some remarks: Linux filenames are case-sensitive. _fileno and _filelength do not exist on linux
39
40 Revision 1.12 2006/05/03 21:19:34 cignoni
41 Added support for progress callback
42
43 Revision 1.11 2006/01/30 15:02:50 cignoni
44 Added mask filling in open
45
46 Revision 1.10 2006/01/04 16:14:43 cignoni
47 Added callback managment on loading of binary stl
48
49 Revision 1.9 2005/09/15 09:29:45 m_di_benedetto
50 #included missing <wrap/callback.h> and <vcg/complex/trimesh/allocate.h>
51
52 Revision 1.8 2004/10/28 00:52:45 cignoni
53 Better Doxygen documentation
54
55 Revision 1.7 2004/08/25 15:27:51 ponchio
56 Comma at end of enum.
57
58 Revision 1.6 2004/08/25 15:15:27 ganovelli
59 minor changes to comply gcc compiler (typename's and stuff)
60
61 Revision 1.5 2004/06/23 15:36:44 cignoni
62 Restructured management of error, now the standard open for any mesh type return the error code, the default success value is zero
63 Any import class has a method ErrorMsg that give a verbal description of an error code.
64
65 Revision 1.4 2004/03/18 15:30:57 cignoni
66 Removed float/double warning
67
68 Revision 1.3 2004/03/12 21:42:52 cignoni
69 First working version!
70
71 ****************************************************************************/
72
73 #ifndef __VCGLIB_IMPORT_STL
74 #define __VCGLIB_IMPORT_STL
75 #include <stdio.h>
76 #include <wrap/callback.h>
77 #include <vcg/complex/trimesh/allocate.h>
78
79 namespace vcg {
80 namespace tri {
81 namespace io {
82
83 /**
84 This class encapsulate a filter for importing stl (stereolitograpy) meshes.
85 The stl format is quite simple and rather un-flexible. It just stores, in ascii or binary the, unindexed, geometry of the faces.
86 Warning: this code assume little endian (PC) architecture!!!
87 */
88 template <class OpenMeshType>
89 class ImporterSTL
90 {
91 public:
92
93 typedef typename OpenMeshType::VertexPointer VertexPointer;
94 typedef typename OpenMeshType::ScalarType ScalarType;
95 typedef typename OpenMeshType::VertexType VertexType;
96 typedef typename OpenMeshType::FaceType FaceType;
97 typedef typename OpenMeshType::VertexIterator VertexIterator;
98 typedef typename OpenMeshType::FaceIterator FaceIterator;
99
100 // if it is binary there are 80 char of comment, the number fn of faces and then exactly fn*4*3 bytes.
101
102 enum {STL_LABEL_SIZE=80};
103
104 class STLFacet
105 {
106 public:
107 Point3f n;
108 Point3f v[3];
109 // short attr;
110 };
111
112 enum STLError {
113 E_NOERROR, // 0
114 // Errori di open
115 E_CANTOPEN, // 1
116 E_UNESPECTEDEOF // 2
117 };
118
119 static const char *ErrorMsg(int error)
120 {
121 static const char * stl_error_msg[] =
122 {
123 "No errors",
124 "Can't open file",
125 "Premature End of file",
126 };
127
128 if(error>2 || error<0) return "Unknown error";
129 else return stl_error_msg[error];
130 };
131
132 static int Open(OpenMeshType &mesh, const char *filename, int &loadmask, CallBackPos *cb=0)
133 {
134 loadmask = Mask::IOM_VERTCOORD | Mask::IOM_FACEINDEX;
135 return Open(mesh,filename,cb);
136 }
137
138 static int Open( OpenMeshType &m, const char * filename, CallBackPos *cb=0)
139 {
140 FILE *fp;
141 bool binary=false;
142 fp = fopen(filename, "r");
143 if(fp == NULL)
144 {
145 return E_CANTOPEN;
146 }
147
148 /* Find size of file */
149 fseek(fp, 0, SEEK_END);
150 int file_size = ftell(fp);
151 int facenum;
152 /* Check for binary or ASCII file */
153 fseek(fp, STL_LABEL_SIZE, SEEK_SET);
154 fread(&facenum, sizeof(int), 1, fp);
155 int expected_file_size=STL_LABEL_SIZE + 4 + (sizeof(short)+sizeof(STLFacet) )*facenum ;
156 if(file_size == expected_file_size) binary = true;
157 unsigned char tmpbuf[128];
158 fread(tmpbuf,sizeof(tmpbuf),1,fp);
159 for(unsigned int i = 0; i < sizeof(tmpbuf); i++)
160 {
161 if(tmpbuf[i] > 127)
162 {
163 binary=true;
164 break;
165 }
166 }
167 // Now we know if the stl file is ascii or binary.
168 fclose(fp);
169 if(binary) return OpenBinary(m,filename,cb);
170 else return OpenAscii(m,filename,cb);
171 }
172
173 static int OpenBinary( OpenMeshType &m, const char * filename, CallBackPos *cb=0)
174 {
175 FILE *fp;
176 fp = fopen(filename, "rb");
177 if(fp == NULL)
178 {
179 return E_CANTOPEN;
180 }
181
182 int facenum;
183 fseek(fp, STL_LABEL_SIZE, SEEK_SET);
184 fread(&facenum, sizeof(int), 1, fp);
185
186 m.Clear();
187 FaceIterator fi=Allocator<OpenMeshType>::AddFaces(m,facenum);
188 VertexIterator vi=Allocator<OpenMeshType>::AddVertices(m,facenum*3);
189 // For each triangle read the normal, the three coords and a short set to zero
190 for(int i=0;i<facenum;++i)
191 {
192 short attr;
193 Point3f norm;
194 Point3f tri[3];
195 fread(&norm,sizeof(Point3f),1,fp);
196 fread(&tri,sizeof(Point3f),3,fp);
197 fread(&attr,sizeof(short),1,fp);
198 for(int k=0;k<3;++k)
199 {
200 (*vi).P().Import(tri[k]);
201 (*fi).V(k)=&*vi;
202 ++vi;
203 }
204 ++fi;
205 if(cb && (i%1000)==0) cb((i*100)/facenum,"STL Mesh Loading");
206 }
207 fclose(fp);
208 return E_NOERROR;
209 }
210
211
212 static int OpenAscii( OpenMeshType &m, const char * filename, CallBackPos *cb=0)
213 {
214 FILE *fp;
215 fp = fopen(filename, "r");
216 if(fp == NULL)
217 {
218 return E_CANTOPEN;
219 }
220 long currentPos = ftell(fp);
221 fseek(fp,0L,SEEK_END);
222 long fileLen = ftell(fp);
223 fseek(fp,currentPos,SEEK_SET);
224
225 m.Clear();
226
227 /* Skip the first line of the file */
228 while(getc(fp) != '\n');
229
230 STLFacet f;
231 int cnt=0;
232 int ret;
233 /* Read a single facet from an ASCII .STL file */
234 while(!feof(fp))
235 {
236 if((++cnt)%1000) cb( (ftell(fp)*100)/fileLen, "STL Mesh Loading");
237
238 ret=fscanf(fp, "%*s %*s %f %f %f\n", &f.n.X(), &f.n.Y(), &f.n.Z());
239 if(ret!=3)
240 {
241 // we could be in the case of a multiple solid object, where after a endfaced instead of another facet we have to skip two lines:
242 // endloop
243 // endfacet
244 //endsolid <- continue on ret==0 will skip this line
245 //solid ascii <- and this one.
246 // facet normal 0.000000e+000 7.700727e-001 -6.379562e-001
247 continue;
248 }
249 ret=fscanf(fp, "%*s %*s");
250 ret=fscanf(fp, "%*s %f %f %f\n", &f.v[0].X(), &f.v[0].Y(), &f.v[0].Z());
251 if(ret!=3)
252 return E_UNESPECTEDEOF;
253 ret=fscanf(fp, "%*s %f %f %f\n", &f.v[1].X(), &f.v[1].Y(), &f.v[1].Z());
254 if(ret!=3)
255 return E_UNESPECTEDEOF;
256 ret=fscanf(fp, "%*s %f %f %f\n", &f.v[2].X(), &f.v[2].Y(), &f.v[2].Z());
257 if(ret!=3)
258 return E_UNESPECTEDEOF;
259 ret=fscanf(fp, "%*s"); // end loop
260 ret=fscanf(fp, "%*s"); // end facet
261 if(feof(fp)) break;
262 FaceIterator fi=Allocator<OpenMeshType>::AddFaces(m,1);
263 VertexIterator vi=Allocator<OpenMeshType>::AddVertices(m,3);
264 for(int k=0;k<3;++k)
265 {
266 (*vi).P().Import(f.v[k]);
267 (*fi).V(k)=&*vi;
268 ++vi;
269 }
270 }
271 fclose(fp);
272 return E_NOERROR;
273 }
274 }; // end class
275 } // end Namespace tri
276 } // end Namespace io
277 } // end Namespace vcg
278
279 #endif