| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 165 |
|
| 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 |
|
| 173 |
if( ! GetDirFromPath(fname,d,n) ) return false; |
| 174 |
|
| 175 |
|
| 176 |
if(d[0]!=0) |
| 177 |
strcat(d,DIR_SEP); |
| 178 |
strcat(d,cachedir); |
| 179 |
if( !CheckCacheDirectory(d) ) return false; |
| 180 |
|
| 181 |
|
| 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 |
|
| 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 |
|
| 217 |
if( ! GetDirFromPath(fname,cname,n) ) return false; |
| 218 |
|
| 219 |
|
| 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 |
|
| 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 |
|
| 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}, |
| 278 |
{"vertex","y",T_FLOAT,T_DOUBLE,offsetof(PlyPoint3d,y),0,0,0,0,0 ,0}, |
| 279 |
{"vertex","z",T_FLOAT,T_DOUBLE,offsetof(PlyPoint3d,z),0,0,0,0,0, 0}, |
| 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]; |
| 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 |
|
| 317 |
pf.Read( dummyspace ); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
if(use_cache) |
| 322 |
{ |
| 323 |
SaveBBoxCache(fname,box); |
| 324 |
} |
| 325 |
|
| 326 |
return true; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 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}, |
| 343 |
{"vertex","y",T_FLOAT,T_DOUBLE,offsetof(PlyPoint3d,y),0,0,0,0,0, 0}, |
| 344 |
{"vertex","z",T_FLOAT,T_DOUBLE,offsetof(PlyPoint3d,z),0,0,0,0,0, 0}, |
| 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]; |
| 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 |
|
| 382 |
pf.Read( dummyspace ); |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
if(use_cache) |
| 387 |
{ |
| 388 |
SaveBBoxCache(fname,box); |
| 389 |
} |
| 390 |
|
| 391 |
return true; |
| 392 |
} |
| 393 |
|
| 394 |
} |
| 395 |
} |
| 396 |
#endif |
| 397 |
|