Debian Codegraph @

vcg/wrap/io_trimesh/import_ptx.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 History
25 $Log: import_ptx.h,v $
26 Revision 1.7 2007/10/16 16:46:53 cignoni
27 Added Allocator::DeleteFace and Allocator::DeleteVertex; Now the use of SetD() should be deprecated.
28
29 Revision 1.6 2006/11/28 11:36:25 cignoni
30 Removed nonstandard include io.h
31
32 Revision 1.5 2006/11/21 22:34:58 cignoni
33 small gcc compiling issues
34
35 Revision 1.4 2006/11/21 19:23:50 e_cerisoli
36 Added comments for documentation
37
38 ****************************************************************************/
39
40 #ifndef __VCGLIB_IMPORT_PTX
41 #define __VCGLIB_IMPORT_PTX
42
43 #include <stdio.h>
44 #include <wrap/callback.h>
45 #include <vcg/complex/trimesh/allocate.h>
46 #include <vcg/complex/trimesh/clean.h>
47 #include <vcg/complex/trimesh/update/normal.h>
48 #include <vcg/complex/trimesh/update/bounding.h>
49
50 namespace vcg {
51 namespace tri {
52 namespace io {
53 /**
54 This class encapsulate a filter for importing ptx meshes.
55 */
56 template <class OpenMeshType>
57 class ImporterPTX
58 {
59 public:
60 enum PTX_OPEN_MASK_ENUM
61 {
62 PTX_ONLY_POINTS = 0x08000000, //BIT_27 no add faces (PTX_FLIPFACES and PTX_SWITCHSIDE are ignored!)
63 PTX_COLOR = 0x10000000, //BIT_28 must be VertexType::HasColor();
64 PTX_COMPUTE_AABBOX = 0x20000000, //BIT_29 compute axis aligned bbox
65 PTX_FLIPFACES = 0x40000000, //BIT_30 flip all faces ( PTX_ONLY_POINTS must be false )
66 PTX_SWITCHSIDE = 0x80000000 //BIT_31 inverse triangulation order (swaping row->cols) ( PTX_ONLY_POINTS must be false )
67 };
68 typedef typename OpenMeshType::VertexPointer VertexPointer;
69 typedef typename OpenMeshType::ScalarType ScalarType;
70 typedef typename OpenMeshType::VertexType VertexType;
71 typedef typename OpenMeshType::FaceType FaceType;
72 typedef typename OpenMeshType::VertexIterator VertexIterator;
73 typedef typename OpenMeshType::FaceIterator FaceIterator;
74
75 struct RANGEMAP_INFO
76 {
77 fpos_t pos;
78 int vn;
79 int fn;
80 };
81
82 typedef typename std::vector< RANGEMAP_INFO > RANGEMAP_INFO_TABLE;
83
84 struct PTX_HEAD_INFO
85 {
86 int vn;
87 int fn;
88 RANGEMAP_INFO_TABLE rmapInfo;
89 };
90
91 /// Standard call for knowing the meaning of an error code
92 static const char *ErrorMsg(int error)
93 {
94 static const char * ptx_error_msg[] =
95 {
96 "No errors",
97 "Can't open file",
98 "Header not found",
99 "Eof in header",
100 "Format not found",
101 "Syntax error on header",
102 };
103 if(error>6 || error<0) return "Unknown error";
104 else return ptx_error_msg[error];
105 };
106
107 static bool skipmesh(FILE* fp, CallBackPos *cb=NULL)
108 {
109 PTX_HEAD_INFO tab;
110 return skipmesh(fp, cb);
111 }
112 static bool skipmesh(FILE* fp, PTX_HEAD_INFO & tab, CallBackPos *cb=NULL)
113 {
114 int colnum;
115 int rownum;
116 int skiplines;
117 char linebuf;
118
119 if(feof(fp)) return false;
120 RANGEMAP_INFO ptxInfo;
121 fgetpos(fp, &ptxInfo.pos );
122
123 // getting mesh size;
124 fscanf(fp,"%i\n",&colnum);
125 fscanf(fp,"%i\n",&rownum);
126
127 ptxInfo.vn = rownum*colnum;
128 ptxInfo.fn = (rownum-1) * (colnum-1) * 2;
129
130 char tmp[255];
131 sprintf(tmp, "PTX Mesh analysis... mesh %i vert %i face %i", (int)tab.rmapInfo.size(), ptxInfo.vn, ptxInfo.fn);
132
133 if ( ( colnum <=0 ) || ( rownum <=0 ) ) return false;
134
135 if(feof(fp)) return false;
136 if(cb) cb( rand()%100, tmp);
137 skiplines = (colnum * rownum) + 8; // have to skip (col * row) lines plus 8 lines for the header
138 for(int ii=0; ii<skiplines; ii++)
139 {
140 fread(&linebuf,1,1,fp);
141 while(linebuf != '\n') fread(&linebuf,1,1,fp);
142 }
143
144 if(cb) cb( 100, tmp);
145 tab.vn += ptxInfo.vn;
146 tab.fn += ptxInfo.fn;
147 tab.rmapInfo.push_back( ptxInfo );
148 return true;
149 }
150
151
152 static bool Analysis(const char * filename, PTX_HEAD_INFO &info, CallBackPos *cb=NULL)
153 {
154 info.fn = 0;
155 info.vn = 0;
156 info.rmapInfo.clear();
157 FILE *fp;
158 fp = fopen(filename, "rb");
159 if(fp == NULL) return false;
160 while ( skipmesh( fp, info, cb ) ) {}
161 return true;
162 };
163 static int Open( OpenMeshType &m, const char * filename, int meshNumber, int mask = PTX_ONLY_POINTS, CallBackPos *cb=NULL)
164 {
165 FILE *fp;
166 fp = fopen(filename, "rb");
167 if(fp == NULL) return false;
168 m.Clear();
169 m.vn=0;
170 m.fn=0;
171
172 PTX_HEAD_INFO ptxHead;
173
174 if ( meshNumber>0 ) for (int i=0; i!=meshNumber; ++i) skipmesh(fp, ptxHead, cb);
175 if (!readPTX( m, fp, mask, meshNumber, cb))
176 {
177 m.Clear();
178 return 1;
179 }
180 clearBadVertex(m, mask, cb);
181 return 0;
182 }
183
184 static void clearBadVertex(OpenMeshType &m, int mask, CallBackPos *cb=NULL)
185 {
186 if(cb) cb(40,"PTX Mesh Loading - remove bad vertex!");
187 for(VertexIterator vi = m.vert.begin(); vi != m.vert.end(); vi++)
188 {
189 if((*vi).P() == Point3f(0.0, 0.0, 0.0))
190 Allocator<OpenMeshType>::DeleteVertex(m,*vi);
191 }
192
193 if(cb) cb(60,"PTX Mesh Loading - remove bad face!");
194 bool onlypoints = ((mask & PTX_ONLY_POINTS) != 0);
195 if(! onlypoints)
196 {
197 for(typename OpenMeshType::FaceIterator fi = m.face.begin(); fi != m.face.end(); fi++)
198 {
199 if( ((*fi).V(0)->IsD()) || ((*fi).V(1)->IsD()) || ((*fi).V(2)->IsD()) )
200 Allocator<OpenMeshType>::DeleteFace(m,*fi);
201 }
202
203 // eliminate high angle triangles
204 int angle = 88;
205
206 //printf(" culling by angle \n");
207 float limit = cos( angle*3.14159265358979323846/180.0 );
208 Point3f raggio;
209
210 if(cb) cb(85,"PTX Mesh Loading - remove bad face!");
211 vcg::tri::UpdateNormals<OpenMeshType>::PerFaceNormalized(m);
212 for(typename OpenMeshType::FaceIterator fi = m.face.begin(); fi != m.face.end(); fi++)
213 if(!(*fi).IsD())
214 {
215 raggio = -((*fi).V(0)->P() + (*fi).V(1)->P() + (*fi).V(2)->P()) / 3.0;
216 raggio.Normalize();
217 if((raggio * (*fi).N()) < limit)
218 Allocator<OpenMeshType>::DeleteFace(m,*fi);
219
220 }
221
222 }
223 /*if(cb) cb(60,"PTX Mesh Loading RemoveDuplicateVertex");
224 tri::Clean<OpenMeshType>::RemoveDuplicateVertex(m);
225
226 if (!onlypoints)
227 {
228 if(cb) cb(60,"PTX Mesh Loading RemoveUnreferencedVertex");
229 tri::Clean<OpenMeshType>::RemoveUnreferencedVertex(m);
230 }*/
231 if(cb) cb(100,"PTX Mesh Loading finish!");
232
233
234 }
235
236 //if numMesh == -1 load all mesh
237 static int Open_( OpenMeshType &m, const char * filename, int mask = PTX_ONLY_POINTS, CallBackPos *cb=NULL)
238 {
239 FILE *fp;
240 fp = fopen(filename, "rb");
241 if(fp == NULL) return 1;
242
243 m.Clear();
244 m.vn=0;
245 m.fn=0;
246 int vn=0;
247 int fn=0;
248 //PTX_HEAD_INFO tab;
249 //tab.clear();
250 //while ( skipmesh( fp, tab, cb ) ) {}
251 /*if ( (vn<=0) && (fn<=0) ) return false;
252 //VertexIterator vi = Allocator<OpenMeshType>::AddVertices(m,vn);
253 //OpenMeshType::FaceIterator fi= Allocator<OpenMeshType>::AddFaces(m,fn);
254
255 VertexIterator vi = Allocator<OpenMeshType>::AddVertices(m, tab[20].vn);
256 FaceIterator fi = Allocator<OpenMeshType>::AddFaces(m, tab[20].fn);
257 readPTX( m, fp, vi, fi, tab[20], mask, 20, cb);
258 fclose(fp);
259 /* return true;
260
261 if ( numMesh>0 )
262 for (int i=0; i!=numMesh; ++i) if (!skipmesh(fp, vn, fn, tab)) return false;
263
264 int mn=0;
265 if ( numMesh == -1 )
266 {
267 bool next = true;
268 while ( next )
269 {
270 bool r = readPTX(m, fp, mask, mn, cb);
271 mn++;
272 if ((r==false) && (m.vn==0) ) { fclose(fp); return false; }
273 else if (r==false) next = false;
274 }
275 } else
276 {
277 bool r = readPTX(m, fp, mask, numMesh, cb);
278 if ((r==false) && (m.vn==0) ) { fclose(fp); return false; }
279 }
280
281 fclose(fp);
282 */
283 // now i delete all points in (0,0,0) that are unsampled points
284 for(VertexIterator vi = m.vert.begin(); vi != m.vert.end(); vi++)
285 {
286 if((*vi).P() == Point3f(0.0, 0.0, 0.0))
287 Allocator<OpenMeshType>::DeleteVertex(m,*vi);
288 }
289
290 bool onlypoints = ((mask & PTX_ONLY_POINTS) != 0);
291 if(! onlypoints)
292 {
293
294
295 for(typename OpenMeshType::FaceIterator fi = m.face.begin(); fi != m.face.end(); fi++)
296 {
297 if( ((*fi).V(0)->IsD()) || ((*fi).V(1)->IsD()) || ((*fi).V(2)->IsD()) )
298 Allocator<OpenMeshType>::DeleteFace(m,*fi);
299 }
300
301 // eliminate high angle triangles
302 int angle = 88;
303
304 printf(" culling by angle \n");
305 float limit = cos( angle*3.14159265358979323846/180.0 );
306 Point3f raggio;
307
308 vcg::tri::UpdateNormals<OpenMeshType>::PerFaceNormalized(m);
309 for(typename OpenMeshType::FaceIterator fi = m.face.begin(); fi != m.face.end(); fi++)
310 if(!(*fi).IsD())
311 {
312 raggio = -((*fi).V(0)->P() + (*fi).V(1)->P() + (*fi).V(2)->P()) / 3.0;
313 raggio.Normalize();
314 if((raggio * (*fi).N()) < limit)
315 Allocator<OpenMeshType>::DeleteFace(m,*fi);
316 }
317
318 }
319 /*if(cb) cb(60,"PTX Mesh Loading RemoveDuplicateVertex");
320 tri::Clean<OpenMeshType>::RemoveDuplicateVertex(m);
321
322 if (!onlypoints)
323 {
324 if(cb) cb(60,"PTX Mesh Loading RemoveUnreferencedVertex");
325 tri::Clean<OpenMeshType>::RemoveUnreferencedVertex(m);
326 }*/
327 if(cb) cb(100,"PTX Mesh Loading finish!");
328 return 0;
329 }
330 static bool readPTX( OpenMeshType &m, FILE *fp, VertexIterator &vi, FaceIterator &fi, const RANGEMAP_INFO &ptxInfo, int mask, int mn, CallBackPos *cb=NULL)
331 {
332 int colnum;
333 int rownum;
334 int numtokens;
335 char linebuf[256];
336 int ii;
337 float xx,yy,zz; // position
338 float rr,gg,bb; // color
339 float rf; // reflectance
340 Matrix44f currtrasf;
341
342 bool hascolor;
343
344 bool savecolor = ((mask & PTX_COLOR) != 0) && VertexType::HasColor();
345 bool computeBbox = ((mask & PTX_COMPUTE_AABBOX) != 0);
346 bool onlypoints = ((mask & PTX_ONLY_POINTS) != 0);
347 bool switchside = ((mask & PTX_SWITCHSIDE) != 0);
348 bool flipfaces = ((mask & PTX_FLIPFACES) != 0);
349 int total = 50;
350 if ( onlypoints ) total = 100;
351
352 if (fsetpos(fp, &ptxInfo.pos)!=0) return false;
353
354
355 // getting mesh size;
356 fscanf(fp,"%i\n",&colnum);
357 fscanf(fp,"%i\n",&rownum);
358
359 if ( ( colnum <=0 ) || ( rownum <=0 ) ) return false;
360
361 // initial 4 lines [still don't know what is this :) :)]
362 if ( !fscanf(fp,"%f %f %f\n", &xx, &yy, &zz) ) return false;
363 if ( !fscanf(fp,"%f %f %f\n", &xx, &yy, &zz) ) return false;
364 if ( !fscanf(fp,"%f %f %f\n", &xx, &yy, &zz) ) return false;
365 if ( !fscanf(fp,"%f %f %f\n", &xx, &yy, &zz) ) return false;
366
367 // now the transformation matrix
368 if ( !fscanf(fp,"%f %f %f %f\n", &(currtrasf.ElementAt(0,0)), &(currtrasf.ElementAt(0,1)), &(currtrasf.ElementAt(0,2)), &(currtrasf.ElementAt(0,3))) )return false;
369 if ( !fscanf(fp,"%f %f %f %f\n", &(currtrasf.ElementAt(1,0)), &(currtrasf.ElementAt(1,1)), &(currtrasf.ElementAt(1,2)), &(currtrasf.ElementAt(1,3))) )return false;
370 if ( !fscanf(fp,"%f %f %f %f\n", &(currtrasf.ElementAt(2,0)), &(currtrasf.ElementAt(2,1)), &(currtrasf.ElementAt(2,2)), &(currtrasf.ElementAt(2,3))) )return false;
371 if ( !fscanf(fp,"%f %f %f %f\n", &(currtrasf.ElementAt(3,0)), &(currtrasf.ElementAt(3,1)), &(currtrasf.ElementAt(3,2)), &(currtrasf.ElementAt(3,3))) )return false;
372
373 // now the real data begins
374
375 // first line, we should know if the format is
376 // XX YY ZZ RF
377 // or it is
378 // XX YY ZZ RF RR GG BB
379
380 // read the entire first line and then count the spaces. it's rude but it works :)
381 ii=0;
382 fread(&(linebuf[ii++]),1,1,fp);
383 while(linebuf[ii-1] != '\n') if ( fread(&(linebuf[ii++]),1,1,fp)==0 ) return false;
384 linebuf[ii-1] = '\0'; // terminate the string
385 numtokens=1;
386 for(ii=0; ii<(int)strlen(linebuf); ii++) if(linebuf[ii] == ' ') numtokens++;
387 if(numtokens == 4) hascolor = false;
388 else if(numtokens == 7) hascolor = true;
389 else return false;
390
391 Transpose(currtrasf);
392 int vn = rownum*colnum;
393
394 //VertexIterator vi = Allocator<OpenMeshType>::AddVertices(m,vn);
395 //m.vn += vn;
396 // parse the first line....
397 if(hascolor)
398 {
399 printf("\n hascolor ");
400 sscanf(linebuf,"%f %f %f %f %f %f %f", &xx, &yy, &zz, &rf, &rr, &gg, &bb);
401 }
402 else
403 {
404 printf("\n no color ");
405 sscanf(linebuf,"%f %f %f %f", &xx, &yy, &zz, &rf);
406 }
407
408 //if (computeBbox) m.bbox.SetNull();
409
410
411 //addthefirstpoint
412 (*vi).P()[0]=xx;
413 (*vi).P()[1]=yy;
414 (*vi).P()[2]=zz;
415 (*vi).P() = currtrasf * (*vi).P();
416 if (computeBbox) m.bbox.Add( (*vi).P() );
417 if(hascolor && savecolor)
418 {
419 (*vi).C()[0]=rr;
420 (*vi).C()[1]=gg;
421 (*vi).C()[2]=bb;
422 }
423 vi++;
424
425
426
427 // now for each line until end of mesh (row*col)-1
428 for(ii=0; ii<((rownum*colnum)-1); ii++)
429 {
430
431 char tmp[255];
432 sprintf(tmp, "PTX Mesh Loading... mesh %i", mn);
433 if(cb) cb((ii*total)/vn, tmp);
434
435 // read the stream
436 if(hascolor) fscanf(fp,"%f %f %f %f %f %f %f", &xx, &yy, &zz, &rf, &rr, &gg, &bb);
437 else fscanf(fp,"%f %f %f %f", &xx, &yy, &zz, &rf);
438
439
440 // add the point
441 (*vi).P()[0]=xx;
442 (*vi).P()[1]=yy;
443 (*vi).P()[2]=zz;
444 (*vi).P() = currtrasf * (*vi).P();
445 if (computeBbox) m.bbox.Add( (*vi).P() );
446 if(hascolor && savecolor)
447 {
448 (*vi).C()[0]=rr;
449 (*vi).C()[1]=gg;
450 (*vi).C()[2]=bb;
451 }
452 vi++;
453
454
455
456 }
457
458 if(! onlypoints)
459 {
460
461 // now i can triangulate
462 int trinum = (rownum-1) * (colnum-1) * 2;
463
464
465 //OpenMeshType::FaceIterator fi= Allocator<OpenMeshType>::AddFaces(m,trinum);
466
467 // m.fn += trinum;
468
469
470 int v0i,v1i,v2i, t;
471 for(int rit=0; rit<rownum-1; rit++)
472 for(int cit=0; cit<colnum-1; cit++)
473 {
474
475 if(cb) cb(50 + (t*50)/(rownum*colnum),"PTX Mesh Loading");
476
477 if(!switchside)
478 {
479 v0i = (rit ) + ((cit ) * rownum);
480 v1i = (rit+1) + ((cit ) * rownum);
481 v2i = (rit ) + ((cit+1) * rownum);
482 }
483 else
484 {
485 v0i = (cit ) + ((rit ) * colnum);
486 v1i = (cit+1) + ((rit ) * colnum);
487 v2i = (cit ) + ((rit+1) * colnum);
488 }
489
490
491 // upper tri
492 (*fi).V(2) = &(m.vert[v0i]);
493 (*fi).V(1) = &(m.vert[v1i]);
494 (*fi).V(0) = &(m.vert[v2i]);
495
496 if(flipfaces)
497 {
498 (*fi).V(2) = &(m.vert[v1i]);
499 (*fi).V(1) = &(m.vert[v0i]);
500 }
501
502 //m.fn++;
503 fi++;
504
505 if(!switchside)
506 {
507 v0i = (rit+1) + ((cit ) * rownum);
508 v1i = (rit+1) + ((cit+1) * rownum);
509 v2i = (rit ) + ((cit+1) * rownum);
510 }
511 else
512 {
513 v0i = (cit+1) + ((rit ) * colnum);
514 v1i = (cit+1) + ((rit+1) * colnum);
515 v2i = (cit ) + ((rit+1) * colnum);
516 }
517
518 // lower tri
519 (*fi).V(2) = &(m.vert[v0i]);
520 (*fi).V(1) = &(m.vert[v1i]);
521 (*fi).V(0) = &(m.vert[v2i]);
522
523 if(flipfaces)
524 {
525 (*fi).V(2) = &(m.vert[v1i]);
526 (*fi).V(1) = &(m.vert[v0i]);
527 }
528
529 // m.fn++;
530 fi++;
531 }
532
533
534
535 }
536
537 return true;
538
539
540
541
542 }
543 ///Call that load a mesh
544 static bool readPTX( OpenMeshType &m, FILE *fp, int mask, CallBackPos *cb=NULL)
545 {
546 int numtokens;
547 int colnum;
548 int rownum;
549 float xx,yy,zz; // position
550 float rr,gg,bb; // color
551 float rf; // reflectance
552 Matrix44f currtrasf;
553
554 bool hascolor;
555 bool savecolor = ((mask & PTX_COLOR) != 0) && VertexType::HasColor();
556 bool computeBbox = ((mask & PTX_COMPUTE_AABBOX) != 0);
557 bool onlypoints = ((mask & PTX_ONLY_POINTS) != 0);
558 bool switchside = ((mask & PTX_SWITCHSIDE) != 0);
559 bool flipfaces = ((mask & PTX_FLIPFACES) != 0);
560 int total = 50;
561 if ( onlypoints ) total = 100;
562 char linebuf[256];
563
564 fscanf(fp,"%i\n",&colnum);
565 fscanf(fp,"%i\n",&rownum);
566
567 if ( ( colnum <=0 ) || ( rownum <=0 ) ) return false;
568 // initial 4 lines [still don't know what is this :) :)]
569 if ( !fscanf(fp,"%f %f %f\n", &xx, &yy, &zz) ) return false;
570 if ( !fscanf(fp,"%f %f %f\n", &xx, &yy, &zz) ) return false;
571 if ( !fscanf(fp,"%f %f %f\n", &xx, &yy, &zz) ) return false;
572 if ( !fscanf(fp,"%f %f %f\n", &xx, &yy, &zz) ) return false;
573 // now the transformation matrix
574 if ( !fscanf(fp,"%f %f %f %f\n", &(currtrasf.ElementAt(0,0)), &(currtrasf.ElementAt(0,1)), &(currtrasf.ElementAt(0,2)), &(currtrasf.ElementAt(0,3))) )return false;
575 if ( !fscanf(fp,"%f %f %f %f\n", &(currtrasf.ElementAt(1,0)), &(currtrasf.ElementAt(1,1)), &(currtrasf.ElementAt(1,2)), &(currtrasf.ElementAt(1,3))) )return false;
576 if ( !fscanf(fp,"%f %f %f %f\n", &(currtrasf.ElementAt(2,0)), &(currtrasf.ElementAt(2,1)), &(currtrasf.ElementAt(2,2)), &(currtrasf.ElementAt(2,3))) )return false;
577 if ( !fscanf(fp,"%f %f %f %f\n", &(currtrasf.ElementAt(3,0)), &(currtrasf.ElementAt(3,1)), &(currtrasf.ElementAt(3,2)), &(currtrasf.ElementAt(3,3))) )return false;
578
579 //now the real data begins
580 // first line, we should know if the format is
581 // XX YY ZZ RF
582 // or it is
583 // XX YY ZZ RF RR GG BB
584 // read the entire first line and then count the spaces. it's rude but it works :)
585 int ii=0;
586 fread(&(linebuf[ii++]),1,1,fp);
587 while(linebuf[ii-1] != '\n') if ( fread(&(linebuf[ii++]),1,1,fp)==0 ) return false;
588 linebuf[ii-1] = '\0'; // terminate the string
589 numtokens=1;
590 for(ii=0; ii<(int)strlen(linebuf); ii++) if(linebuf[ii] == ' ') numtokens++;
591 if(numtokens == 4) hascolor = false;
592 else if(numtokens == 7) hascolor = true;
593 else return false;
594 Transpose(currtrasf);
595 int vn = rownum*colnum;
596 VertexIterator vi = Allocator<OpenMeshType>::AddVertices(m,vn);
597 m.vn = vn;
598 // parse the first line....
599 if(hascolor)
600 {
601 printf("\n hascolor ");
602 sscanf(linebuf,"%f %f %f %f %f %f %f", &xx, &yy, &zz, &rf, &rr, &gg, &bb);
603 }
604 else
605 {
606 printf("\n no color ");
607 sscanf(linebuf,"%f %f %f %f", &xx, &yy, &zz, &rf);
608 }
609 if (computeBbox) m.bbox.SetNull();
610
611 //addthefirstpoint
612 (*vi).P()[0]=xx;
613 (*vi).P()[1]=yy;
614 (*vi).P()[2]=zz;
615
616 if (computeBbox) m.bbox.Add( (*vi).P() );
617 if(hascolor && savecolor)
618 {
619 (*vi).C()[0]=rr;
620 (*vi).C()[1]=gg;
621 (*vi).C()[2]=bb;
622 }
623 vi++;
624 // now for each line until end of mesh (row*col)-1
625 for(ii=0; ii<((rownum*colnum)-1); ii++)
626 {
627 char tmp[255];
628 sprintf(tmp, "PTX Mesh Loading...");
629 if(cb) cb((ii*total)/vn, tmp);
630 // read the stream
631 if(hascolor) fscanf(fp,"%f %f %f %f %f %f %f", &xx, &yy, &zz, &rf, &rr, &gg, &bb);
632 else fscanf(fp,"%f %f %f %f", &xx, &yy, &zz, &rf);
633 // add the point
634 (*vi).P()[0]=xx;
635 (*vi).P()[1]=yy;
636 (*vi).P()[2]=zz;
637
638 if (computeBbox) m.bbox.Add( (*vi).P() );
639 if(hascolor && savecolor)
640 {
641 (*vi).C()[0]=rr;
642 (*vi).C()[1]=gg;
643 (*vi).C()[2]=bb;
644 }
645 vi++;
646 }
647 if(! onlypoints)
648 {
649 // now i can triangulate
650 int trinum = (rownum-1) * (colnum-1) * 2;
651 typename OpenMeshType::FaceIterator fi= Allocator<OpenMeshType>::AddFaces(m,trinum);
652 m.fn = trinum;
653 int v0i,v1i,v2i, t;
654 t=0;
655 for(int rit=0; rit<rownum-1; rit++)
656 for(int cit=0; cit<colnum-1; cit++)
657 {
658 t++;
659 if(cb) cb(50 + (t*50)/(rownum*colnum),"PTX Mesh Loading");
660
661 if(!switchside)
662 {
663 v0i = (rit ) + ((cit ) * rownum);
664 v1i = (rit+1) + ((cit ) * rownum);
665 v2i = (rit ) + ((cit+1) * rownum);
666 }
667 else
668 {
669 v0i = (cit ) + ((rit ) * colnum);
670 v1i = (cit+1) + ((rit ) * colnum);
671 v2i = (cit ) + ((rit+1) * colnum);
672 }
673
674 // upper tri
675 (*fi).V(2) = &(m.vert[v0i]);
676 (*fi).V(1) = &(m.vert[v1i]);
677 (*fi).V(0) = &(m.vert[v2i]);
678 if(flipfaces)
679 {
680 (*fi).V(2) = &(m.vert[v1i]);
681 (*fi).V(1) = &(m.vert[v0i]);
682 }
683
684 fi++;
685 if(!switchside)
686 {
687 v0i = (rit+1) + ((cit ) * rownum);
688 v1i = (rit+1) + ((cit+1) * rownum);
689 v2i = (rit ) + ((cit+1) * rownum);
690 }
691 else
692 {
693 v0i = (cit+1) + ((rit ) * colnum);
694 v1i = (cit+1) + ((rit+1) * colnum);
695 v2i = (cit ) + ((rit+1) * colnum);
696 }
697 // lower tri
698 (*fi).V(2) = &(m.vert[v0i]);
699 (*fi).V(1) = &(m.vert[v1i]);
700 (*fi).V(0) = &(m.vert[v2i]);
701 if(flipfaces)
702 {
703 (*fi).V(2) = &(m.vert[v1i]);
704 (*fi).V(1) = &(m.vert[v0i]);
705 }
706
707 fi++;
708 }
709 }
710 if(cb) cb(40,"PTX Mesh Loading - remove bad vertex!");
711 for(typename OpenMeshType::VertexIterator vi = m.vert.begin(); vi != m.vert.end(); vi++)
712 {
713 if((*vi).P() == Point3f(0.0, 0.0, 0.0))
714 Allocator<OpenMeshType>::DeleteVertex(m,*vi);
715 }
716 if(cb) cb(60,"PTX Mesh Loading - remove bad face!");
717 onlypoints = ((mask & PTX_ONLY_POINTS) != 0);
718 if(! onlypoints)
719 {
720 for(typename OpenMeshType::FaceIterator fi = m.face.begin(); fi != m.face.end(); fi++)
721 {
722 if( ((*fi).V(0)->IsD()) || ((*fi).V(1)->IsD()) || ((*fi).V(2)->IsD()) )
723 Allocator<OpenMeshType>::DeleteFace(m,*fi);
724 }
725 // eliminate high angle triangles
726 int angle = 88;
727 printf(" culling by angle \n");
728 float limit = cos( angle*3.14159265358979323846/180.0 );
729 Point3f raggio;
730
731 vcg::tri::UpdateNormals<OpenMeshType>::PerFaceNormalized(m);
732 for(typename OpenMeshType::FaceIterator fi = m.face.begin(); fi != m.face.end(); fi++)
733 if(!(*fi).IsD())
734 {
735 raggio = -((*fi).V(0)->P() + (*fi).V(1)->P() + (*fi).V(2)->P()) / 3.0;
736 raggio.Normalize();
737 if((raggio * (*fi).N()) < limit)
738 Allocator<OpenMeshType>::DeleteFace(m,*fi);
739 }
740 }
741 for(typename OpenMeshType::VertexIterator vi = m.vert.begin(); vi != m.vert.end(); vi++)
742 {
743 if(!(*vi).IsD())
744 (*vi).P() = currtrasf * (*vi).P();
745 }
746 vcg::tri::UpdateNormals<OpenMeshType>::PerFaceNormalized(m);
747 vcg::tri::UpdateBounding<CMeshO>::Box(m);
748 if(cb) cb(100,"PTX Mesh Loading finish!");
749 return true;
750 }
751
752 ///Standard call that reading a mesh
753 static int Open( OpenMeshType &m, const char * filename, int mask = PTX_ONLY_POINTS, CallBackPos *cb=NULL)
754 {
755 FILE *fp;
756 fp = fopen(filename, "rb");
757 if(fp == NULL) return 1;
758 m.Clear();
759 m.vn=0;
760 m.fn=0;
761 if (!readPTX( m, fp, mask,cb))
762 {
763 m.Clear();
764 return 1;
765 }
766 int endfile,end = 0;
767 fscanf(fp,"%i%i",&endfile,&end);
768 if(end != 0)
769 return 2;
770 return 0;
771 }
772 }; // end class
773
774 } // end Namespace tri
775 } // end Namespace io
776 } // end Namespace vcg
777 #endif