Debian Codegraph @

vcg/wrap/io_tetramesh/export_ply.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
26 $Log: export_ply.h,v $
27 Revision 1.1 2004/06/03 13:16:32 ganovelli
28 created
29
30
31 ****************************************************************************/
32
33 /**
34 @name Load and Save in Ply format
35 */
36 //@{
37
38 #ifndef __VCGLIB_TETRAEXPORT_PLY
39 #define __VCGLIB_TETRAEXPORT_PLY
40
41 #include<wrap/ply/io_mask.h>
42 #include<wrap/io_tetramesh/io_ply.h>
43
44 #include <stdio.h>
45
46 namespace vcg {
47 namespace tetra {
48 namespace io {
49
50
51 template <class SaveMeshType>
52 class ExporterPLY
53 {
54 // Si occupa di convertire da un tipo all'altro.
55 // usata nella saveply per matchare i tipi tra stotype e memtype.
56 // Ad es se in memoria c'e' un int e voglio salvare un float
57 // src sara in effetti un puntatore a int il cui valore deve
58 // essere convertito al tipo di ritorno desiderato (stotype)
59
60 template <class StoType>
61 static void PlyConv(int mem_type, void *src, StoType &dest)
62 {
63 switch (mem_type){
64 case ply::T_FLOAT : dest = (StoType) (* ((float *) src)); break;
65 case ply::T_DOUBLE: dest = (StoType) (* ((double *) src)); break;
66 case ply::T_INT : dest = (StoType) (* ((int *) src)); break;
67 case ply::T_SHORT : dest = (StoType) (* ((short *) src)); break;
68 case ply::T_CHAR : dest = (StoType) (* ((char *) src)); break;
69 case ply::T_UCHAR : dest = (StoType) (* ((unsigned char *)src)); break;
70 default : assert(0);
71 }
72 }
73
74 public:
75 typedef ::vcg::ply::PropDescriptor PropDescriptor ;
76 typedef typename SaveMeshType::VertexPointer VertexPointer;
77 typedef typename SaveMeshType::ScalarType ScalarType;
78 typedef typename SaveMeshType::VertexType VertexType;
79 typedef typename SaveMeshType::TetraType TetraType;
80 typedef typename SaveMeshType::TetraPointer TetraPointer;
81 typedef typename SaveMeshType::VertexIterator VertexIterator;
82 typedef typename SaveMeshType::TetraIterator TetraIterator;
83
84 static bool Save(SaveMeshType &m, const char * filename, bool binary=true)
85 {
86 PlyInfo pi;
87 return Save(m,filename,binary,pi);
88 }
89
90 static bool Save(SaveMeshType &m, const char * filename, int savemask )
91 {
92 PlyInfo pi;
93 pi.mask=savemask;
94 return Save(m,filename,true,pi);
95 }
96
97 static bool Save(SaveMeshType &m, const char * filename, bool binary, PlyInfo &pi ) // V1.0
98 {
99 FILE * fpout;
100 int i;
101
102 const char * hbin = "binary_little_endian";
103 const char * hasc = "ascii";
104 const char * h;
105
106 bool multit = false;
107
108 if(binary) h=hbin;
109 else h=hasc;
110
111 fpout = fopen(filename,"wb");
112 if(fpout==NULL) {
113 pi.status=::vcg::ply::E_CANTOPEN;
114 return false;
115 }
116 fprintf(fpout,
117 "ply\n"
118 "format %s 1.0\n"
119 "comment VCGLIB generated\n"
120 ,h
121 );
122
123 //if( pi.mask & ply::PLYMask::PM_WEDGTEXCOORD )
124 //{
125 // //const char * TFILE = "TextureFile";
126
127 // //for(i=0;i<textures.size();++i)
128 // // fprintf(fpout,"comment %s %s\n", TFILE, (const char *)(textures[i]) );
129
130 // //if(textures.size()>1 && (HasPerWedgeTexture() || HasPerVertexTexture())) multit = true;
131 //}
132
133 //if( (pi.mask & PLYMask::PM_CAMERA) && camera.IsValid() )
134 /*{
135 fprintf(fpout,
136 "element camera 1\n"
137 "property float view_px\n"
138 "property float view_py\n"
139 "property float view_pz\n"
140 "property float x_axisx\n"
141 "property float x_axisy\n"
142 "property float x_axisz\n"
143 "property float y_axisx\n"
144 "property float y_axisy\n"
145 "property float y_axisz\n"
146 "property float z_axisx\n"
147 "property float z_axisy\n"
148 "property float z_axisz\n"
149 "property float focal\n"
150 "property float scalex\n"
151 "property float scaley\n"
152 "property float centerx\n"
153 "property float centery\n"
154 "property int viewportx\n"
155 "property int viewporty\n"
156 "property float k1\n"
157 "property float k2\n"
158 "property float k3\n"
159 "property float k4\n"
160 );
161 }*/
162
163 fprintf(fpout,
164 "element vertex %d\n"
165 "property float x\n"
166 "property float y\n"
167 "property float z\n"
168 ,m.vn
169 );
170
171
172 if( pi.mask & ply::PLYMask::PM_VERTFLAGS )
173 {
174 fprintf(fpout,
175 "property int flags\n"
176 );
177 }
178
179 if( m.HasPerVertexColor() && (pi.mask & ply::PLYMask::PM_VERTCOLOR) )
180 {
181 fprintf(fpout,
182 "property uchar red\n"
183 "property uchar green\n"
184 "property uchar blue\n"
185 "property uchar alpha\n"
186 );
187 }
188
189 if( m.HasPerVertexQuality() && (pi.mask & ply::PLYMask::PM_VERTQUALITY) )
190 {
191 fprintf(fpout,
192 "property float quality\n"
193 );
194 }
195
196 for(i=0;i<pi.vdn;i++)
197 fprintf(fpout,"property %s %s\n",pi.VertexData[i].stotypename(),pi.VertexData[i].propname);
198
199 fprintf(fpout,
200 "element tetra %d\n"
201 "property list uchar int vertex_indices\n"
202 ,m.tn
203 );
204
205 if( pi.mask & ply::PLYMask::PM_TETRAFLAGS )
206 {
207 fprintf(fpout,
208 "property int flags\n"
209 );
210 }
211
212 if( VertexType::HasTexture() /*|| VertexType::HasPerWedgeTexture()*/ )
213 {
214 fprintf(fpout,
215 "property list uchar float texcoord\n"
216 );
217
218 if(multit)
219 fprintf(fpout,
220 "property int texnumber\n"
221 );
222 }
223
224 if( TetraType::HasTetraColor() && (pi.mask & ply::PLYMask::PM_TETRACOLOR) )
225 {
226 fprintf(fpout,
227 "property uchar red\n"
228 "property uchar green\n"
229 "property uchar blue\n"
230 "property uchar alpha\n"
231 );
232 }
233
234 //if ( m.HasPerWedgeColor() && (pi.mask & ply::PLYMask::PM_WEDGCOLOR) )
235 //{
236 // fprintf(fpout,
237 // "property list uchar float color\n"
238 // );
239 //}
240
241 if( m.HasPerTetraQuality() && (pi.mask & ply::PLYMask::PM_TETRAQUALITY) )
242 {
243 fprintf(fpout,
244 "property float quality\n"
245 );
246 }
247
248 for(i=0;i<pi.fdn;i++)
249 fprintf(fpout,"property %s %s\n",pi.TetraData[i].stotypename(),pi.TetraData[i].propname);
250
251 fprintf(fpout, "end_header\n" );
252
253 // Salvataggio camera
254 //if( (pi.mask & ply::PLYMask::PM_CAMERA) && camera.IsValid() )
255 //{
256 //if(binary)
257 //{
258 // float t[17];
259
260 // t[ 0] = camera.view_p[0];
261 // t[ 1] = camera.view_p[1];
262 // t[ 2] = camera.view_p[2];
263 // t[ 3] = camera.x_axis[0];
264 // t[ 4] = camera.x_axis[1];
265 // t[ 5] = camera.x_axis[2];
266 // t[ 6] = camera.y_axis[0];
267 // t[ 7] = camera.y_axis[1];
268 // t[ 8] = camera.y_axis[2];
269 // t[ 9] = camera.z_axis[0];
270 // t[10] = camera.z_axis[1];
271 // t[11] = camera.z_axis[2];
272 // t[12] = camera.f;
273 // t[13] = camera.s[0];
274 // t[14] = camera.s[1];
275 // t[15] = camera.c[0];
276 // t[16] = camera.c[1];
277 // fwrite(t,sizeof(float),17,fpout);
278
279 // fwrite( camera.viewport,sizeof(int),2,fpout );
280
281 // t[ 0] = camera.k[0];
282 // t[ 1] = camera.k[1];
283 // t[ 2] = camera.k[2];
284 // t[ 3] = camera.k[3];
285 // fwrite(t,sizeof(float),4,fpout);
286 //}
287 //else
288 //{
289 // fprintf(fpout,"%g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %d %d %g %g %g %g\n"
290 // ,camera.view_p[0]
291 // ,camera.view_p[1]
292 // ,camera.view_p[2]
293 // ,camera.x_axis[0]
294 // ,camera.x_axis[1]
295 // ,camera.x_axis[2]
296 // ,camera.y_axis[0]
297 // ,camera.y_axis[1]
298 // ,camera.y_axis[2]
299 // ,camera.z_axis[0]
300 // ,camera.z_axis[1]
301 // ,camera.z_axis[2]
302 // ,camera.f
303 // ,camera.s[0]
304 // ,camera.s[1]
305 // ,camera.c[0]
306 // ,camera.c[1]
307 // ,camera.viewport[0]
308 // ,camera.viewport[1]
309 // ,camera.k[0]
310 // ,camera.k[1]
311 // ,camera.k[2]
312 // ,camera.k[3]
313 // );
314 //}
315 //}
316
317
318 int j;
319 std::vector<int> FlagV;
320 VertexPointer vp;
321 VertexIterator vi;
322 for(j=0,vi=m.vert.begin();vi!=m.vert.end();++vi)
323 {
324 vp=&(*vi);
325 FlagV.push_back(vp->UberFlags()); // Salva in ogni caso flag del vertice
326 if( ! vp->IsD() )
327 {
328 if(binary)
329 {
330 float t;
331
332 t = float(vp->UberP()[0]); fwrite(&t,sizeof(float),1,fpout);
333 t = float(vp->UberP()[1]); fwrite(&t,sizeof(float),1,fpout);
334 t = float(vp->UberP()[2]); fwrite(&t,sizeof(float),1,fpout);
335
336 if( pi.mask & ply::PLYMask::PM_VERTFLAGS )
337 fwrite(&(vp->UberFlags()),sizeof(int),1,fpout);
338
339 if( m.HasPerVertexColor() && (pi.mask & ply::PLYMask::PM_VERTCOLOR) )
340 fwrite(&( vp->C() ),sizeof(char),4,fpout);
341
342 if( m.HasPerVertexQuality() && (pi.mask & ply::PLYMask::PM_VERTQUALITY) )
343 fwrite(&( vp->Q() ),sizeof(float),1,fpout);
344
345
346 for(i=0;i<pi.vdn;i++)
347 {
348 double td; float tf;int ti;short ts; char tc; unsigned char tuc;
349 switch (pi.VertexData[i].stotype1)
350 {
351 case ply::T_FLOAT : PlyConv(pi.VertexData[i].memtype1, ((char *)vp)+pi.VertexData[i].offset1, tf ); fwrite(&tf, sizeof(float),1,fpout); break;
352 case ply::T_DOUBLE : PlyConv(pi.VertexData[i].memtype1, ((char *)vp)+pi.VertexData[i].offset1, td ); fwrite(&td, sizeof(double),1,fpout); break;
353 case ply::T_INT : PlyConv(pi.VertexData[i].memtype1, ((char *)vp)+pi.VertexData[i].offset1, ti ); fwrite(&ti, sizeof(int),1,fpout); break;
354 case ply::T_SHORT : PlyConv(pi.VertexData[i].memtype1, ((char *)vp)+pi.VertexData[i].offset1, ts ); fwrite(&ts, sizeof(short),1,fpout); break;
355 case ply::T_CHAR : PlyConv(pi.VertexData[i].memtype1, ((char *)vp)+pi.VertexData[i].offset1, tc ); fwrite(&tc, sizeof(char),1,fpout); break;
356 case ply::T_UCHAR : PlyConv(pi.VertexData[i].memtype1, ((char *)vp)+pi.VertexData[i].offset1, tuc); fwrite(&tuc,sizeof(unsigned char),1,fpout); break;
357 default : assert(0);
358 }
359 }
360 }
361 else // ***** ASCII *****
362 {
363 fprintf(fpout,"%g %g %g " ,vp->P()[0],vp->P()[1],vp->P()[2]);
364
365 if( pi.mask & ply::PLYMask::PM_VERTFLAGS )
366 fprintf(fpout,"%d ",vp->UberFlags());
367
368 if( m.HasPerVertexColor() && (pi.mask & ply::PLYMask::PM_VERTCOLOR) )
369 fprintf(fpout,"%d %d %d %d ",vp->C()[0],vp->C()[1],vp->C()[2],vp->C()[3] );
370
371 if( m.HasPerVertexQuality() && (pi.mask & ply::PLYMask::PM_VERTQUALITY) )
372 fprintf(fpout,"%g ",vp->Q());
373
374 for(i=0;i<pi.vdn;i++)
375 {
376 float tf; double td;
377 int ti;
378 switch (pi.VertexData[i].memtype1)
379 {
380 case ply::T_FLOAT : tf=*( (float *) (((char *)vp)+pi.VertexData[i].offset1)); fprintf(fpout,"%g ",tf); break;
381 case ply::T_DOUBLE : td=*( (double *) (((char *)vp)+pi.VertexData[i].offset1)); fprintf(fpout,"%g ",tf); break;
382 case ply::T_INT : ti=*( (int *) (((char *)vp)+pi.VertexData[i].offset1)); fprintf(fpout,"%i ",ti); break;
383 case ply::T_SHORT : ti=*( (short *) (((char *)vp)+pi.VertexData[i].offset1)); fprintf(fpout,"%i ",ti); break;
384 case ply::T_CHAR : ti=*( (char *) (((char *)vp)+pi.VertexData[i].offset1)); fprintf(fpout,"%i ",ti); break;
385 case ply::T_UCHAR : ti=*( (unsigned char *) (((char *)vp)+pi.VertexData[i].offset1)); fprintf(fpout,"%i ",ti); break;
386 default : assert(0);
387 }
388 }
389
390 fprintf(fpout,"\n");
391 }
392
393 vp->UberFlags()=j; // Trucco! Nascondi nei flags l'indice del vertice non deletato!
394 j++;
395 }
396 }
397 assert(j==m.vn);
398
399 char c = 4;
400 unsigned char b9 = 9;
401 unsigned char b6 = 6;
402 TetraPointer fp;
403 int vv[4];
404 TetraIterator fi;
405 int fcnt=0;
406 for(j=0,fi=m.tetra.begin();fi!=m.tetra.end();++fi)
407 {
408 fp=&(*fi);
409 if( ! fp->IsD() )
410 { fcnt++;
411 if(binary)
412 {
413 vv[0]=fp->cV(0)->UberFlags();
414 vv[1]=fp->cV(1)->UberFlags();
415 vv[2]=fp->cV(2)->UberFlags();
416 vv[3]=fp->cV(2)->UberFlags();
417 fwrite(&c,1,1,fpout);
418 fwrite(vv,sizeof(int),4,fpout);
419
420 if( pi.mask & ply::PLYMask::PM_TETRAFLAGS )
421 fwrite(&(fp->Flags()),sizeof(int),1,fpout);
422
423 if( m.HasPerVertexTexture() && (pi.mask & ply::PLYMask::PM_VERTTEXCOORD) )
424 {
425 fwrite(&b6,sizeof(char),1,fpout);
426 float t[6];
427 for(int k=0;k<4;++k)
428 {
429 t[k*2+0] = fp->V(k)->T().u();
430 t[k*2+1] = fp->V(k)->T().v();
431 }
432 fwrite(t,sizeof(float),6,fpout);
433 }
434 //else if( m.HasPerWedgeTexture() && (pi.mask & ply::PLYMask::PM_WEDGTEXCOORD) )
435 //{
436 // fwrite(&b6,sizeof(char),1,fpout);
437 // float t[6];
438 // for(int k=0;k<3;++k)
439 // {
440 // t[k*2+0] = fp->WT(k).u();
441 // t[k*2+1] = fp->WT(k).v();
442 // }
443 // fwrite(t,sizeof(float),6,fpout);
444 //}
445
446 /* if(multit)
447 {
448 int t = fp->WT(0).n();
449 fwrite(&t,sizeof(int),1,fpout);
450 }
451
452 if( m.HasPerTetraColor() && (pi.mask & ply::PLYMask::PM_TETRACOLOR) )
453 fwrite(&( fp->C() ),sizeof(char),4,fpout);*/
454
455
456 //if( m.HasPerWedgeColor() && (pi.mask & ply::PLYMask::PM_WEDGCOLOR) )
457 //{
458 // fwrite(&b9,sizeof(char),1,fpout);
459 // float t[3];
460 // for(int z=0;z<3;++z)
461 // {
462 // t[0] = float(fp->WC(z)[0])/255;
463 // t[1] = float(fp->WC(z)[1])/255;
464 // t[2] = float(fp->WC(z)[2])/255;
465 // fwrite( t,sizeof(float),3,fpout);
466 // }
467 //}
468
469 if( TetraType::HasTetraQuality() && (pi.mask & ply::PLYMask::PM_TETRAQUALITY) )
470 fwrite( &(fp->Q()),sizeof(float),1,fpout);
471
472
473 for(i=0;i<pi.fdn;i++)
474 {
475 double td; float tf;int ti;short ts; char tc; unsigned char tuc;
476 switch (pi.TetraData[i].stotype1){
477 case ply::T_FLOAT : PlyConv(pi.TetraData[i].memtype1, ((char *)fp)+pi.TetraData[i].offset1, tf ); fwrite(&tf, sizeof(float),1,fpout); break;
478 case ply::T_DOUBLE : PlyConv(pi.TetraData[i].memtype1, ((char *)fp)+pi.TetraData[i].offset1, td ); fwrite(&td, sizeof(double),1,fpout); break;
479 case ply::T_INT : PlyConv(pi.TetraData[i].memtype1, ((char *)fp)+pi.TetraData[i].offset1, ti ); fwrite(&ti, sizeof(int),1,fpout); break;
480 case ply::T_SHORT : PlyConv(pi.TetraData[i].memtype1, ((char *)fp)+pi.TetraData[i].offset1, ts ); fwrite(&ts, sizeof(short),1,fpout); break;
481 case ply::T_CHAR : PlyConv(pi.TetraData[i].memtype1, ((char *)fp)+pi.TetraData[i].offset1, tc ); fwrite(&tc, sizeof(char),1,fpout); break;
482 case ply::T_UCHAR : PlyConv(pi.TetraData[i].memtype1, ((char *)fp)+pi.TetraData[i].offset1, tuc); fwrite(&tuc,sizeof(unsigned char),1,fpout); break;
483 default : assert(0);
484 }
485 }
486 }
487 else // ***** ASCII *****
488 {
489 fprintf(fpout,"3 %d %d %d ",
490 fp->cV(0)->UberFlags(), fp->cV(1)->UberFlags(), fp->cV(2)->UberFlags() );
491
492 if( pi.mask & ply::PLYMask::PM_TETRAFLAGS )
493 fprintf(fpout,"%d ",fp->Flags());
494
495 if( m.HasPerVertexTexture() && (pi.mask & ply::PLYMask::PM_VERTTEXCOORD) )
496 {
497 fprintf(fpout,"6 ");
498 for(int k=0;k<4;++k)
499 fprintf(fpout,"%g %g "
500 ,fp->V(k)->T().u()
501 ,fp->V(k)->T().v()
502 );
503 }
504 //else if( m.HasPerWedgeTexture() && (pi.mask & ply::PLYMask::PM_WEDGTEXCOORD) )
505 //{
506 // fprintf(fpout,"6 ");
507 // for(int k=0;k<3;++k)
508 // fprintf(fpout,"%g %g "
509 // ,fp->WT(k).u()
510 // ,fp->WT(k).v()
511 // );
512 //}
513
514 /* if(multit)
515 {
516 fprintf(fpout,"%d ",fp->WT(0).n());
517 }*/
518
519 if( TetraType::HasTetraColor() && (pi.mask & ply::PLYMask::PM_TETRACOLOR) )
520 {
521 float t[3];
522 t[0] = float(fp->C()[0])/255;
523 t[1] = float(fp->C()[1])/255;
524 t[2] = float(fp->C()[2])/255;
525 fprintf(fpout,"9 ");
526 fprintf(fpout,"%g %g %g ",t[0],t[1],t[2]);
527 fprintf(fpout,"%g %g %g ",t[0],t[1],t[2]);
528 fprintf(fpout,"%g %g %g ",t[0],t[1],t[2]);
529 }
530 //else if( m.HasPerWedgeColor() && (pi.mask & ply::PLYMask::PM_WEDGCOLOR) )
531 //{
532 // fprintf(fpout,"9 ");
533 // for(int z=0;z<3;++z)
534 // fprintf(fpout,"%g %g %g "
535 // ,double(fp->WC(z)[0])/255
536 // ,double(fp->WC(z)[1])/255
537 // ,double(fp->WC(z)[2])/255
538 // );
539 //}
540
541 if( m.HasPerTetraQuality() && (pi.mask & ply::PLYMask::PM_TETRAQUALITY) )
542 fprintf(fpout,"%g ",fp->Q());
543
544 for(i=0;i<pi.fdn;i++)
545 {
546 float tf; double td;
547 int ti;
548 switch (pi.TetraData[i].memtype1)
549 {
550 case ply::T_FLOAT : tf=*( (float *) (((char *)fp)+pi.TetraData[i].offset1)); fprintf(fpout,"%g ",tf); break;
551 case ply::T_DOUBLE : td=*( (double *) (((char *)fp)+pi.TetraData[i].offset1)); fprintf(fpout,"%g ",tf); break;
552 case ply::T_INT : ti=*( (int *) (((char *)fp)+pi.TetraData[i].offset1)); fprintf(fpout,"%i ",ti); break;
553 case ply::T_SHORT : ti=*( (short *) (((char *)fp)+pi.TetraData[i].offset1)); fprintf(fpout,"%i ",ti); break;
554 case ply::T_CHAR : ti=*( (char *) (((char *)fp)+pi.TetraData[i].offset1)); fprintf(fpout,"%i ",ti); break;
555 case ply::T_UCHAR : ti=*( (unsigned char *) (((char *)fp)+pi.TetraData[i].offset1)); fprintf(fpout,"%i ",ti); break;
556 default : assert(0);
557 }
558 }
559
560 fprintf(fpout,"\n");
561 }
562 }
563 }
564 assert(fcnt==m.tn);
565 fclose(fpout);
566
567 // Recupera i flag originali
568 for(j=0,vi=m.vert.begin();vi!=m.vert.end();++vi)
569 (*vi).UberFlags()=FlagV[j++];
570
571 return 0;
572 }
573
574
575
576
577 }; // end class
578
579
580
581 } // end namespace tri
582 } // end namespace io
583 } // end namespace vcg
584
585 #endif