Debian Codegraph @

vcg/wrap/ply/plystuff.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 Acknowlegments
25 Portions of this file were based on the original code of the Ply library
26 of Greg Turk and on the work of Claudio Rocchini
27
28 ****************************************************************************/
29
30 /****************************************************************************
31 History
32
33 $Log: plystuff.h,v $
34 Revision 1.7 2008/01/24 11:50:14 cignoni
35 solved slash backslash issue in vcg_cache
36
37 Revision 1.6 2007/06/25 15:24:00 cignoni
38 removed a possibly useless static kw
39
40 Revision 1.5 2006/04/11 09:48:04 zifnab1974
41 changes needed for compilation on linux 64b with gcc 3.4.5
42
43 Revision 1.4 2005/12/02 00:00:53 cignoni
44 Moved and corrected interpret_texture_name from plystuff.h to plylib.cpp
45
46 Revision 1.3 2005/03/18 00:14:40 cignoni
47 removed small gcc compiling issues
48
49 Revision 1.2 2005/03/15 11:46:52 cignoni
50 Cleaning of the automatic bbox caching support for ply files. First working version.
51
52
53 */
54
55 //****************** Gestione cache *****************
56
57 #ifndef __VCG_PLYLIB_STUFF
58 #define __VCG_PLYLIB_STUFF
59
60 #include <sys/types.h>
61 #include <sys/stat.h>
62 #include <fcntl.h>
63 #ifdef WIN32
64 #include <io.h>
65 #endif
66
67 #include <vcg/space/box3.h>
68 #include <wrap/ply/plylib.h>
69 using namespace vcg;
70
71 #ifdef WIN32
72 #include <direct.h>
73 #define pb_mkdir(n) _mkdir(n)
74 #define pb_access _access
75 #define pb_stat _stat
76 #define pb_fstat _fstat
77 #define pb_open _open
78 #define pb_close _close
79 #define DIR_SEP "\\"
80 #else
81 #define pb_mkdir(n) mkdir(n,0755)
82 #define pb_access access
83 #define pb_stat stat
84 #define pb_fstat fstat
85 #define pb_open open
86 #define pb_close close
87 #define _O_BINARY 0 // Does not exist on Unix
88 #define _O_RDONLY O_RDONLY
89 #define DIR_SEP "/"
90 #endif
91
92
93 namespace vcg {
94 namespace ply {
95
96 const int MAXBPATH = 256;
97
98 // Stringhe per la cache
99 const char * cachedir = "vcg_cache";
100 const char * bboxcacheext = ".bbox_cache";
101 const char * bboxheader = "BBOXCACH";
102
103 bool GetDirFromPath( const char * path, char * dir, char * name )
104 {
105 strcpy(dir,path);
106 char * p;
107
108 p = strrchr(dir,'\\');
109 if(p==0) p=strrchr(dir,'/');
110 if(p==0)
111 {
112 dir[0] = 0;
113 strcpy(name,path);
114 }
115 else
116 {
117 strcpy(name,p+1);
118 *p = 0;
119 }
120 return true;
121 }
122
123
124 static bool CheckCacheDirectory( const char * dir )
125 {
126 if( pb_access(dir,0)!=0 )
127 {
128 if( pb_mkdir(dir)==-1 )
129 return false;
130 }
131 return true;
132 }
133
134
135 bool CheckCacheTime( const char * fname, const char * cname )
136 {
137
138 if( pb_access(fname,4)==-1 ) return false;
139 if( pb_access(cname,4)==-1 ) return false;
140
141 int h,r;
142 struct pb_stat st;
143 time_t ft,bt;
144
145 h = pb_open(fname,_O_BINARY|_O_RDONLY);
146 if(h==0) return false;
147 r = pb_fstat(h,&st);
148 pb_close(h);
149 if(r==-1) return false;
150 ft = st.st_mtime;
151
152 h = pb_open(cname,_O_BINARY|_O_RDONLY);
153 if(h==0) return false;
154 r = pb_fstat(h,&st);
155 //_read(h,&box,sizeof(box));
156 pb_close(h);
157 if(r==-1) return false;
158 bt = st.st_mtime;
159
160 if( difftime(bt,ft)>=0 ) return true;
161 else return false;
162 }
163
164 // restituisce true se il file con la cache del bbox della mesh e' piu' recente del file ply
165 // se fname2 != 0, allora deve essere piu recente anche di fname2.
166 static bool CheckBBoxCache( const char * fname, Box3d & box, const char *fname2=0 )
167 {
168 char d[MAXBPATH];
169 char n[MAXBPATH];
170 char h[8];
171
172 // Estrazione dati
173 if( ! GetDirFromPath(fname,d,n) ) return false;
174
175 // Controllo esistenza directory delle cache
176 if(d[0]!=0)
177 strcat(d,DIR_SEP);
178 strcat(d,cachedir);
179 if( !CheckCacheDirectory(d) ) return false;
180
181 // Controllo esistenza e data file cache
182 strcat(d,DIR_SEP);
183 strcat(d,n);
184 strcat(d,bboxcacheext);
185 if( CheckCacheTime(fname,d) &&
186 (fname2==0 || CheckCacheTime(fname2,d)) )
187 {
188 // Lettura bbox e controllo
189 FILE * fp = fopen(d,"rb");
190 if(fp==0) return false;
191 if( fread(h,1,8,fp)!=8 )
192 {
193 fclose(fp);
194 return false;
195 }
196 if( fread(&box,sizeof(Box3d),1,fp)!=1 )
197 {
198 fclose(fp);
199 return false;
200 }
201 fclose(fp);
202 if( strncmp(h,bboxheader,8) )
203 return false;
204 else
205 return true;
206 }
207 else
208 return false;
209 }
210
211
212 bool GetCacheName( const char * fname, const char * ext_name, char * cname )
213 {
214 static char n[MAXBPATH];
215
216 // Estrazione dati
217 if( ! GetDirFromPath(fname,cname,n) ) return false;
218
219 // Controllo esistenza directory delle cache
220 if(cname[0]!=0)
221 strcat(cname,DIR_SEP);
222 strcat(cname,cachedir);
223 if( !CheckCacheDirectory(cname) ) return false;
224
225 strcat(cname,DIR_SEP);
226 strcat(cname,n);
227 strcat(cname,ext_name);
228 return true;
229 }
230
231
232 static bool SaveBBoxCache( const char * fname, const Box3d & box )
233 {
234 char d[MAXBPATH];
235
236 if( !GetCacheName(fname,bboxcacheext,d) )
237 return false;
238
239 // Lettura bbox e controllo
240 FILE * fp = fopen(d,"wb");
241 if(fp==0) return false;
242 if( fwrite(bboxheader,1,8,fp)!=8 )
243 {
244 fclose(fp);
245 return false;
246 }
247 if( fwrite(&box,sizeof(Box3d),1,fp)!=1 )
248 {
249 fclose(fp);
250 return false;
251 }
252 fclose(fp);
253 return true;
254 }
255
256 struct PlyPoint3d
257 {
258 double x;
259 double y;
260 double z;
261 };
262
263
264 // Calcola il bbox di un file ply
265
266 bool ScanBBox( const char * fname, Box3d & box, bool use_cache=true )
267 {
268
269 if(use_cache)
270 {
271 if( CheckBBoxCache(fname,box) )
272 return true;
273 }
274
275 static const PropDescriptor pv[3]=
276 {
277 {"vertex","x",T_FLOAT,T_DOUBLE,offsetof(PlyPoint3d,x),0,0,0,0,0 ,0}, // TO GET RID OF COMPILER WARNING I ADDED 0 TO INITIALIZE format (MV)
278 {"vertex","y",T_FLOAT,T_DOUBLE,offsetof(PlyPoint3d,y),0,0,0,0,0 ,0}, // TO GET RID OF COMPILER WARNING I ADDED 0 TO INITIALIZE format (MV)
279 {"vertex","z",T_FLOAT,T_DOUBLE,offsetof(PlyPoint3d,z),0,0,0,0,0, 0},// TO GET RID OF COMPILER WARNING I ADDED 0 TO INITIALIZE format (MV)
280 };
281
282
283 PlyFile pf;
284
285 if( pf.Open(fname,PlyFile::MODE_READ)==-1 )
286 {
287 fprintf(stderr,"Warning: File %s not found\n",fname);
288 return false;
289 }
290
291 if( pf.AddToRead(pv[0])==-1 ) { fprintf(stderr,"Warning: Read error\n"); return false; }
292 if( pf.AddToRead(pv[1])==-1 ) { fprintf(stderr,"Warning: Read error\n"); return false; }
293 if( pf.AddToRead(pv[2])==-1 ) { fprintf(stderr,"Warning: Read error\n"); return false; }
294
295 box.SetNull();
296 char dummyspace[1024]; // sperando basti...
297
298 for(int i=0;i<int(pf.elements.size());++i)
299 {
300 int n = pf.ElemNumber(i);
301 pf.SetCurElement(i);
302
303 if( !strcmp( pf.ElemName(i),"vertex" ) )
304 {
305 for(int j=0;j<n;++j)
306 {
307 PlyPoint3d t;
308
309 pf.Read( (void *)(&t) );
310 box.Add( Point3d(t.x,t.y,t.z) );
311 }
312 }
313 else
314 {
315 for(int j=0;j<n;++j)
316 //pf.Read( 0 ); // prima era cosi' e faceva un'assert e scrivema plausibilimente a caso in mem
317 pf.Read( dummyspace );
318 }
319 }
320
321 if(use_cache)
322 {
323 SaveBBoxCache(fname,box);
324 }
325
326 return true;
327 }
328
329 // Come la precedente ma applica la matrice m ai punti prima di calcolare il bbox.
330 // Visto che la matrice di solito e' tenuta in un qualche file, se si vuole usare la cache
331 // si puo' passare anche un'altro filename da controllare
332 bool ScanBBox( const char * fname, Box3d & box, const Matrix44d & m, bool use_cache, const char *matrixfname)
333 {
334
335 if(use_cache)
336 {
337 if ( CheckBBoxCache(fname,box,matrixfname) ) return true;
338 }
339
340 static const PropDescriptor pv[3]=
341 {
342 {"vertex","x",T_FLOAT,T_DOUBLE,offsetof(PlyPoint3d,x),0,0,0,0,0, 0},// TO GET RID OF COMPILER WARNING I ADDED 0 TO INITIALIZE format (MV)
343 {"vertex","y",T_FLOAT,T_DOUBLE,offsetof(PlyPoint3d,y),0,0,0,0,0, 0},// TO GET RID OF COMPILER WARNING I ADDED 0 TO INITIALIZE format (MV)
344 {"vertex","z",T_FLOAT,T_DOUBLE,offsetof(PlyPoint3d,z),0,0,0,0,0, 0},// TO GET RID OF COMPILER WARNING I ADDED 0 TO INITIALIZE format (MV)
345 };
346
347
348 PlyFile pf;
349
350 if( pf.Open(fname,PlyFile::MODE_READ)==-1 )
351 {
352 fprintf(stderr,"Warning: File %s not found\n",fname);
353 return false;
354 }
355
356 if( pf.AddToRead(pv[0])==-1 ) { fprintf(stderr,"Warning: Read error\n"); return false; }
357 if( pf.AddToRead(pv[1])==-1 ) { fprintf(stderr,"Warning: Read error\n"); return false; }
358 if( pf.AddToRead(pv[2])==-1 ) { fprintf(stderr,"Warning: Read error\n"); return false; }
359
360 box.SetNull();
361 char dummyspace[1024]; // sperando basti...
362
363 for(int i=0;i<int(pf.elements.size());++i)
364 {
365 int n = pf.ElemNumber(i);
366 pf.SetCurElement(i);
367
368 if( !strcmp( pf.ElemName(i),"vertex" ) )
369 {
370 for(int j=0;j<n;++j)
371 {
372 PlyPoint3d t;
373
374 pf.Read( (void *)(&t) );
375 box.Add( m*Point3d(t.x,t.y,t.z) );
376 }
377 }
378 else
379 {
380 for(int j=0;j<n;++j)
381 //pf.Read( 0 ); // prima era cosi' e faceva un'assert e scrivema plausibilimente a caso in mem
382 pf.Read( dummyspace );
383 }
384 }
385
386 if(use_cache)
387 {
388 SaveBBoxCache(fname,box);
389 }
390
391 return true;
392 }
393
394 } // end namespace ply
395 } // end namespace vcg
396 #endif
397