Debian Codegraph @

src/vcglib/wrap/io_trimesh/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-2016 \/)\/ *
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 @name Load and Save in Ply format
26 */
27 //@{
28
29 #ifndef __VCGLIB_EXPORT_PLY
30 #define __VCGLIB_EXPORT_PLY
31
32 #include<wrap/callback.h>
33 #include<wrap/ply/plylib.h>
34 #include<wrap/io_trimesh/io_mask.h>
35 #include<wrap/io_trimesh/io_ply.h>
36 #include<wrap/io_trimesh/precision.h>
37 #include<vcg/container/simple_temporary_data.h>
38 #include <vcg/complex/base.h>
39
40
41 #include <stdio.h>
42
43 namespace vcg {
44 namespace tri {
45 namespace io {
46
47
48
49 template <class SaveMeshType>
50 class ExporterPLY
51 {
52 // It takes care of converting from one type to another.
53 // It is used in saveply to match types between stotype and memtype (e.g. the type in the file and the type in memory).
54 // For example if there is an int in memory and I want to save a float
55 // src will actually be a pointer to int whose value must
56 // be converted to the desired return type (stotype)
57
58 template <class StoType>
59 static void PlyConv(int mem_type, void *src, StoType &dest)
60 {
61 switch (mem_type){
62 case ply::T_FLOAT : dest = (StoType) (* ((float *) src)); break;
63 case ply::T_DOUBLE : dest = (StoType) (* ((double *) src)); break;
64 case ply::T_INT : dest = (StoType) (* ((int *) src)); break;
65 case ply::T_SHORT : dest = (StoType) (* ((short *) src)); break;
66 case ply::T_CHAR : dest = (StoType) (* ((char *) src)); break;
67 case ply::T_UCHAR : dest = (StoType) (* ((unsigned char *)src)); break;
68 default : assert(0);
69 }
70 }
71
72 public:
73 typedef ::vcg::ply::PropDescriptor PropDescriptor ;
74 typedef typename SaveMeshType::ConstVertexPointer VertexPointer;
75 typedef typename SaveMeshType::ScalarType ScalarType;
76 typedef typename SaveMeshType::VertexType VertexType;
77 typedef typename SaveMeshType::FaceType FaceType;
78 typedef typename SaveMeshType::ConstFacePointer FacePointer;
79 typedef typename SaveMeshType::ConstVertexIterator VertexIterator;
80 typedef typename SaveMeshType::ConstFaceIterator FaceIterator;
81 typedef typename SaveMeshType::ConstEdgeIterator EdgeIterator;
82 typedef typename vcg::Shot<ScalarType>::ScalarType ShotScalarType;
83
84 static int Save(const SaveMeshType &m, const char * filename, bool binary=true)
85 {
86 PlyInfo pi;
87 return Save(m,filename,binary,pi);
88 }
89
90 static int Save(const SaveMeshType &m, const char * filename, int savemask, bool binary = true, CallBackPos *cb=0 )
91 {
92 PlyInfo pi;
93 pi.mask=savemask;
94 return Save(m,filename,binary,pi,cb);
95 }
96
97 static int Save(const SaveMeshType &m, const char * filename, bool binary, const PlyInfo &pi, CallBackPos *cb=0) // V1.0
98 {
99 FILE * fpout;
100 const char * hbin = "binary_little_endian";
101 const char * hasc = "ascii";
102 const char * h;
103 //Coord ScalarType
104 const int DGT = vcg::tri::io::Precision<ScalarType>::digits();
105 const int DGTS = vcg::tri::io::Precision<ShotScalarType>::digits();
106 const int DGTVQ = vcg::tri::io::Precision<typename VertexType::QualityType>::digits();
107 const int DGTVR = vcg::tri::io::Precision<typename VertexType::RadiusType>::digits();
108 const int DGTVT = vcg::tri::io::Precision<typename VertexType::TexCoordType::ScalarType>::digits();
109 const int DGTFQ = vcg::tri::io::Precision<typename FaceType::QualityType>::digits();
110 const int DGTFT = vcg::tri::io::Precision<typename FaceType::TexCoordType::ScalarType>::digits();
111 bool saveTexIndexFlag = false;
112
113 if(binary) h=hbin;
114 else h=hasc;
115
116 fpout = fopen(filename,"wb");
117 if(fpout==NULL) {
118 //pi.status=::vcg::ply::E_CANTOPEN;
119 return ::vcg::ply::E_CANTOPEN;
120 }
121 fprintf(fpout,
122 "ply\n"
123 "format %s 1.0\n"
124 "comment VCGLIB generated\n" ,
125 h);
126
127 if (((pi.mask & Mask::IOM_WEDGTEXCOORD) != 0) || ((pi.mask & Mask::IOM_VERTTEXCOORD) != 0))
128 {
129 const char * TFILE = "TextureFile";
130
131 for(size_t i=0; i < m.textures.size(); ++i)
132 fprintf(fpout,"comment %s %s\n", TFILE, (const char *)(m.textures[i].c_str()) );
133
134 if(m.textures.size()>1 && (HasPerWedgeTexCoord(m) || HasPerVertexTexCoord(m))) saveTexIndexFlag = true;
135 }
136
137 if((pi.mask & Mask::IOM_CAMERA))
138 {
139 const char* cmtp = vcg::tri::io::Precision<ShotScalarType>::typeName();
140 fprintf(fpout,"element camera 1\n");
141 fprintf(fpout,"property %s view_px\n",cmtp);
142 fprintf(fpout,"property %s view_py\n",cmtp);
143 fprintf(fpout,"property %s view_pz\n",cmtp);
144 fprintf(fpout,"property %s x_axisx\n",cmtp);
145 fprintf(fpout,"property %s x_axisy\n",cmtp);
146 fprintf(fpout,"property %s x_axisz\n",cmtp);
147 fprintf(fpout,"property %s y_axisx\n",cmtp);
148 fprintf(fpout,"property %s y_axisy\n",cmtp);
149 fprintf(fpout,"property %s y_axisz\n",cmtp);
150 fprintf(fpout,"property %s z_axisx\n",cmtp);
151 fprintf(fpout,"property %s z_axisy\n",cmtp);
152 fprintf(fpout,"property %s z_axisz\n",cmtp);
153 fprintf(fpout,"property %s focal\n",cmtp);
154 fprintf(fpout,"property %s scalex\n",cmtp);
155 fprintf(fpout,"property %s scaley\n",cmtp);
156 fprintf(fpout,"property %s centerx\n",cmtp);
157 fprintf(fpout,"property %s centery\n",cmtp);
158 fprintf(fpout,"property int viewportx\n");
159 fprintf(fpout,"property int viewporty\n");
160 fprintf(fpout,"property %s k1\n",cmtp);
161 fprintf(fpout,"property %s k2\n",cmtp);
162 fprintf(fpout,"property %s k3\n",cmtp);
163 fprintf(fpout,"property %s k4\n",cmtp);
164 }
165
166 const char* vttp = vcg::tri::io::Precision<ScalarType>::typeName();
167 fprintf(fpout,"element vertex %d\n",m.vn);
168 fprintf(fpout,"property %s x\n",vttp);
169 fprintf(fpout,"property %s y\n",vttp);
170 fprintf(fpout,"property %s z\n",vttp);
171
172 if( HasPerVertexNormal(m) &&( pi.mask & Mask::IOM_VERTNORMAL) )
173 {
174 fprintf(fpout,"property %s nx\n",vttp);
175 fprintf(fpout,"property %s ny\n",vttp);
176 fprintf(fpout,"property %s nz\n",vttp);
177 }
178
179
180 if( HasPerVertexFlags(m) &&( pi.mask & Mask::IOM_VERTFLAGS) )
181 {
182 fprintf(fpout,
183 "property int flags\n");
184 }
185
186 if( HasPerVertexColor(m) && (pi.mask & Mask::IOM_VERTCOLOR) )
187 {
188 fprintf(fpout,
189 "property uchar red\n"
190 "property uchar green\n"
191 "property uchar blue\n"
192 "property uchar alpha\n");
193 }
194
195 if( HasPerVertexQuality(m) && (pi.mask & Mask::IOM_VERTQUALITY) )
196 {
197 const char* vqtp = vcg::tri::io::Precision<typename VertexType::ScalarType>::typeName();
198 fprintf(fpout,"property %s quality\n",vqtp);
199 }
200
201 if( tri::HasPerVertexRadius(m) && (pi.mask & Mask::IOM_VERTRADIUS) )
202 {
203 const char* rdtp = vcg::tri::io::Precision<typename VertexType::RadiusType>::typeName();
204 fprintf(fpout,"property %s radius\n",rdtp);
205 }
206 if( ( HasPerVertexTexCoord(m) && pi.mask & Mask::IOM_VERTTEXCOORD ) )
207 {
208 const char* rdtp = vcg::tri::io::Precision<typename VertexType::TexCoordType::ScalarType>::typeName();
209 fprintf(fpout,
210 "property %s texture_u\n"
211 "property %s texture_v\n",
212 rdtp, rdtp);
213 }
214 for(size_t i=0;i<pi.VertDescriptorVec.size();i++){
215 if (!pi.VertDescriptorVec[i].islist) {
216 fprintf(
217 fpout,
218 "property %s %s\n",
219 pi.VertDescriptorVec[i].stotypename(),
220 pi.VertDescriptorVec[i].propname.c_str());
221 }
222 else {
223 fprintf(
224 fpout,
225 "property list %s %s %s\n",
226 pi.VertDescriptorVec[i].stotype2name(),
227 pi.VertDescriptorVec[i].stotypename(),
228 pi.VertDescriptorVec[i].propname.c_str());
229 }
230 }
231
232 fprintf(fpout,
233 "element face %d\n"
234 "property list uchar int vertex_indices\n",
235 m.fn );
236
237 if(HasPerFaceFlags(m) && (pi.mask & Mask::IOM_FACEFLAGS) )
238 {
239 fprintf(fpout,
240 "property int flags\n");
241 }
242
243 if( (HasPerWedgeTexCoord(m) && pi.mask & Mask::IOM_WEDGTEXCOORD ) ||
244 (HasPerVertexTexCoord(m) && (!HasPerWedgeTexCoord(m)) && pi.mask & Mask::IOM_WEDGTEXCOORD ) ) // Note that you can save VT as WT if you really really want it...
245 {
246 const char* rdtp = vcg::tri::io::Precision<typename FaceType::TexCoordType::ScalarType>::typeName();
247 fprintf(fpout,
248 "property list uchar %s texcoord\n", rdtp );
249 }
250 // The texture index information has to be saved for each face (if necessary) both for PerVert and PerWedg
251 if( saveTexIndexFlag &&
252 ( ( HasPerWedgeTexCoord(m) && (pi.mask & Mask::IOM_WEDGTEXCOORD) ) ||
253 ( HasPerVertexTexCoord(m) && (pi.mask & Mask::IOM_VERTTEXCOORD) ) ||
254 ( HasPerVertexTexCoord(m) && (!HasPerWedgeTexCoord(m)) && (pi.mask & Mask::IOM_WEDGTEXCOORD) )
255 )
256 )
257 {
258 fprintf(fpout,
259 "property int texnumber\n");
260 }
261
262 if( HasPerFaceColor(m) && (pi.mask & Mask::IOM_FACECOLOR) )
263 {
264 fprintf(
265 fpout,
266 "property uchar red\n"
267 "property uchar green\n"
268 "property uchar blue\n"
269 "property uchar alpha\n");
270 }
271
272 if ( HasPerWedgeColor(m) && (pi.mask & Mask::IOM_WEDGCOLOR) )
273 {
274 fprintf(
275 fpout,
276 "property list uchar float color\n");
277 }
278
279 if (HasPerFaceNormal(m) && (pi.mask & Mask::IOM_FACENORMAL))
280 {
281 const char* fntp = vcg::tri::io::Precision<typename SaveMeshType::ScalarType>::typeName();
282 fprintf(fpout, "property %s nx\n", fntp);
283 fprintf(fpout, "property %s ny\n", fntp);
284 fprintf(fpout, "property %s nz\n", fntp);
285 }
286
287 if( HasPerFaceQuality(m) && (pi.mask & Mask::IOM_FACEQUALITY) )
288 {
289 const char* fqtp = vcg::tri::io::Precision<typename SaveMeshType::FaceType::QualityType>::typeName();
290 fprintf(fpout,"property %s quality\n",fqtp);
291 }
292
293 for(size_t i=0;i<pi.FaceDescriptorVec.size();i++) {
294 if (!pi.FaceDescriptorVec[i].islist){
295 fprintf(
296 fpout,
297 "property %s %s\n",
298 pi.FaceDescriptorVec[i].stotypename(),
299 pi.FaceDescriptorVec[i].propname.c_str());
300 }
301 else {
302 fprintf(
303 fpout,
304 "property list %s %s %s\n",
305 pi.FaceDescriptorVec[i].stotype2name(),
306 pi.FaceDescriptorVec[i].stotypename(),
307 pi.FaceDescriptorVec[i].propname.c_str());
308 }
309 }
310
311 // Saving of edges is enabled if requested
312 if( m.en>0 && (pi.mask & Mask::IOM_EDGEINDEX) )
313 fprintf(
314 fpout,
315 "element edge %d\n" "property int vertex1\n""property int vertex2\n",m.en);
316 fprintf(fpout, "end_header\n" );
317
318 // Salvataggio camera
319 if((pi.mask & Mask::IOM_CAMERA))
320 {
321 if(binary)
322 {
323 ShotScalarType t[17];
324
325 t[ 0] = (ShotScalarType)m.shot.Extrinsics.Tra()[0];
326 t[ 1] = (ShotScalarType)m.shot.Extrinsics.Tra()[1];
327 t[ 2] = (ShotScalarType)m.shot.Extrinsics.Tra()[2];
328 t[ 3] = (ShotScalarType)m.shot.Extrinsics.Rot()[0][0];
329 t[ 4] = (ShotScalarType)m.shot.Extrinsics.Rot()[0][1];
330 t[ 5] = (ShotScalarType)m.shot.Extrinsics.Rot()[0][2];
331 t[ 6] = (ShotScalarType)m.shot.Extrinsics.Rot()[1][0];
332 t[ 7] = (ShotScalarType)m.shot.Extrinsics.Rot()[1][1];
333 t[ 8] = (ShotScalarType)m.shot.Extrinsics.Rot()[1][2];
334 t[ 9] = (ShotScalarType)m.shot.Extrinsics.Rot()[2][0];
335 t[10] = (ShotScalarType)m.shot.Extrinsics.Rot()[2][1];
336 t[11] = (ShotScalarType)m.shot.Extrinsics.Rot()[2][2];
337 t[12] = (ShotScalarType)m.shot.Intrinsics.FocalMm;
338 t[13] = (ShotScalarType)m.shot.Intrinsics.PixelSizeMm[0];
339 t[14] = (ShotScalarType)m.shot.Intrinsics.PixelSizeMm[1];
340 t[15] = (ShotScalarType)m.shot.Intrinsics.CenterPx[0];
341 t[16] = (ShotScalarType)m.shot.Intrinsics.CenterPx[1];
342 fwrite(t,sizeof(ShotScalarType),17,fpout);
343
344 fwrite( &m.shot.Intrinsics.ViewportPx[0],sizeof(int),2,fpout );
345
346 t[ 0] = (ShotScalarType)m.shot.Intrinsics.k[0];
347 t[ 1] = (ShotScalarType)m.shot.Intrinsics.k[1];
348 t[ 2] = (ShotScalarType)m.shot.Intrinsics.k[2];
349 t[ 3] = (ShotScalarType)m.shot.Intrinsics.k[3];
350 fwrite(t,sizeof(ShotScalarType),4,fpout);
351 }
352 else
353 {
354 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"
355 ,DGTS,-m.shot.Extrinsics.Tra()[0]
356 ,DGTS,-m.shot.Extrinsics.Tra()[1]
357 ,DGTS,-m.shot.Extrinsics.Tra()[2]
358 ,DGTS,m.shot.Extrinsics.Rot()[0][0]
359 ,DGTS,m.shot.Extrinsics.Rot()[0][1]
360 ,DGTS,m.shot.Extrinsics.Rot()[0][2]
361 ,DGTS,m.shot.Extrinsics.Rot()[1][0]
362 ,DGTS,m.shot.Extrinsics.Rot()[1][1]
363 ,DGTS,m.shot.Extrinsics.Rot()[1][2]
364 ,DGTS,m.shot.Extrinsics.Rot()[2][0]
365 ,DGTS,m.shot.Extrinsics.Rot()[2][1]
366 ,DGTS,m.shot.Extrinsics.Rot()[2][2]
367 ,DGTS,m.shot.Intrinsics.FocalMm
368 ,DGTS,m.shot.Intrinsics.PixelSizeMm[0]
369 ,DGTS,m.shot.Intrinsics.PixelSizeMm[1]
370 ,DGTS,m.shot.Intrinsics.CenterPx[0]
371 ,DGTS,m.shot.Intrinsics.CenterPx[1]
372 ,m.shot.Intrinsics.ViewportPx[0]
373 ,m.shot.Intrinsics.ViewportPx[1]
374 ,DGTS,m.shot.Intrinsics.k[0]
375 ,DGTS,m.shot.Intrinsics.k[1]
376 ,DGTS,m.shot.Intrinsics.k[2]
377 ,DGTS,m.shot.Intrinsics.k[3]
378 );
379 }
380 }
381
382
383 int j;
384 std::vector<int> FlagV;
385 VertexPointer vp;
386 VertexIterator vi;
387 SimpleTempData<typename SaveMeshType::VertContainer,int> indices(m.vert);
388
389 PlyAttributeHelper<SaveMeshType> PAH(pi,m);
390
391 for(j=0,vi=m.vert.begin();vi!=m.vert.end();++vi){
392 vp=&(*vi);
393 indices[vi] = j;
394 //((m.vn+m.fn) != 0) all vertices and faces have been marked as deleted but the are still in the vert/face vectors
395 if(cb && ((j%1000)==0) && ((m.vn+m.fn) != 0) )(*cb)( (100*j)/(m.vn+m.fn), "Saving Vertices");
396
397 if( !HasPerVertexFlags(m) || !vp->IsD() )
398 {
399 if(binary)
400 {
401 ScalarType t;
402
403 t = ScalarType(vp->P()[0]); fwrite(&t,sizeof(ScalarType),1,fpout);
404 t = ScalarType(vp->P()[1]); fwrite(&t,sizeof(ScalarType),1,fpout);
405 t = ScalarType(vp->P()[2]); fwrite(&t,sizeof(ScalarType),1,fpout);
406
407 if( HasPerVertexNormal(m) && (pi.mask & Mask::IOM_VERTNORMAL) )
408 {
409 t = ScalarType(vp->N()[0]); fwrite(&t,sizeof(ScalarType),1,fpout);
410 t = ScalarType(vp->N()[1]); fwrite(&t,sizeof(ScalarType),1,fpout);
411 t = ScalarType(vp->N()[2]); fwrite(&t,sizeof(ScalarType),1,fpout);
412 }
413 if( HasPerVertexFlags(m) && (pi.mask & Mask::IOM_VERTFLAGS) )
414 fwrite(&(vp->Flags()),sizeof(int),1,fpout);
415
416 if( HasPerVertexColor(m) && (pi.mask & Mask::IOM_VERTCOLOR) ){
417 auto c = vp->C();
418 fwrite(&c,sizeof(char),4,fpout);
419 }
420
421 if( HasPerVertexQuality(m) && (pi.mask & Mask::IOM_VERTQUALITY) ){
422 auto q = vp->Q();
423 fwrite(&q, sizeof(typename VertexType::QualityType),1,fpout);
424 }
425
426 if( HasPerVertexRadius(m) && (pi.mask & Mask::IOM_VERTRADIUS) ){
427 auto r = vp->R();
428 fwrite(&r,sizeof(typename VertexType::RadiusType),1,fpout);
429 }
430
431 if( HasPerVertexTexCoord(m) && (pi.mask & Mask::IOM_VERTTEXCOORD) ){
432 typename VertexType::TexCoordType::ScalarType t;
433 t = ScalarType(vp->T().u()); fwrite(&t,sizeof(typename VertexType::TexCoordType::ScalarType),1,fpout);
434 t = ScalarType(vp->T().v()); fwrite(&t,sizeof(typename VertexType::TexCoordType::ScalarType),1,fpout);
435 }
436
437 for(size_t i=0;i<pi.VertDescriptorVec.size();i++)
438 {
439 double td(0); float tf(0);int ti;short ts; char tc; unsigned char tu;
440 if(!pi.VertAttrNameVec.empty() && !pi.VertAttrNameVec[i].empty())
441 { // trying to use named attribute to retrieve the value to store
442 assert(vcg::tri::HasPerVertexAttribute(m,pi.VertAttrNameVec[i]));
443 if (!pi.VertDescriptorVec[i].islist){
444 switch (pi.VertDescriptorVec[i].stotype1)
445 {
446 case ply::T_FLOAT : tf=PAH.tchfv[i][vp]; fwrite(&tf, sizeof(float),1,fpout); break;
447 case ply::T_DOUBLE : td=PAH.tchdv[i][vp]; fwrite(&td, sizeof(double),1,fpout); break;
448 case ply::T_INT : ti=PAH.tchiv[i][vp]; fwrite(&ti, sizeof(int),1,fpout); break;
449 case ply::T_SHORT : ts=PAH.tchsv[i][vp]; fwrite(&ts, sizeof(short),1,fpout); break;
450 case ply::T_CHAR : tc=PAH.tchcv[i][vp]; fwrite(&tc, sizeof(char),1,fpout); break;
451 case ply::T_UCHAR : tu=PAH.tchuv[i][vp]; fwrite(&tu,sizeof(unsigned char),1,fpout); break;
452 default : assert(0);
453 }
454 }
455 else { //it is a Poin3f or a Point3d attribute. Saving it as a list
456 static const unsigned char psize = 3;
457 switch (pi.VertDescriptorVec[i].stotype1)
458 {
459 case ply::T_FLOAT :
460 fwrite(&psize, sizeof(unsigned char), 1,fpout);
461 fwrite(&PAH.tchp3fv[i][vp][0], sizeof(float), 1,fpout);
462 fwrite(&PAH.tchp3fv[i][vp][1], sizeof(float), 1,fpout);
463 fwrite(&PAH.tchp3fv[i][vp][2], sizeof(float), 1,fpout);
464 break;
465 //fprintf(fpout,"%d %f %f %f", 3, thp3fv[i][vp][0], thp3fv[i][vp][1], thp3fv[i][vp][2]); break;
466 case ply::T_DOUBLE :
467 fwrite(&psize, sizeof(unsigned char), 1,fpout);
468 fwrite(&PAH.tchp3dv[i][vp][0], sizeof(double), 1,fpout);
469 fwrite(&PAH.tchp3dv[i][vp][1], sizeof(double), 1,fpout);
470 fwrite(&PAH.tchp3dv[i][vp][2], sizeof(double), 1,fpout);
471 break;
472 //fprintf(fpout,"%d %lf %lf %lf", 3, thp3dv[i][vp][0], thp3dv[i][vp][1], thp3dv[i][vp][2]); break;
473 default : assert(0);
474 }
475 }
476 }
477 else
478 {
479 switch (pi.VertDescriptorVec[i].stotype1)
480 {
481 case ply::T_FLOAT : PlyConv(pi.VertDescriptorVec[i].memtype1, ((char *)vp)+pi.VertDescriptorVec[i].offset1, tf ); fwrite(&tf, sizeof(float),1,fpout); break;
482 case ply::T_DOUBLE : PlyConv(pi.VertDescriptorVec[i].memtype1, ((char *)vp)+pi.VertDescriptorVec[i].offset1, td ); fwrite(&td, sizeof(double),1,fpout); break;
483 case ply::T_INT : PlyConv(pi.VertDescriptorVec[i].memtype1, ((char *)vp)+pi.VertDescriptorVec[i].offset1, ti ); fwrite(&ti, sizeof(int),1,fpout); break;
484 case ply::T_SHORT : PlyConv(pi.VertDescriptorVec[i].memtype1, ((char *)vp)+pi.VertDescriptorVec[i].offset1, ts ); fwrite(&ts, sizeof(short),1,fpout); break;
485 case ply::T_CHAR : PlyConv(pi.VertDescriptorVec[i].memtype1, ((char *)vp)+pi.VertDescriptorVec[i].offset1, tc ); fwrite(&tc, sizeof(char),1,fpout); break;
486 case ply::T_UCHAR : PlyConv(pi.VertDescriptorVec[i].memtype1, ((char *)vp)+pi.VertDescriptorVec[i].offset1, tu ); fwrite(&tu,sizeof(unsigned char),1,fpout); break;
487 default : assert(0);
488 }
489 }
490 }
491 }
492 else // ***** ASCII *****
493 {
494 fprintf(fpout,"%.*g %.*g %.*g " ,DGT,vp->P()[0],DGT,vp->P()[1],DGT,vp->P()[2]);
495
496 if( HasPerVertexNormal(m) && (pi.mask & Mask::IOM_VERTNORMAL) )
497 fprintf(fpout,"%.*g %.*g %.*g " ,DGT,ScalarType(vp->N()[0]),DGT,ScalarType(vp->N()[1]),DGT,ScalarType(vp->N()[2]));
498
499 if( HasPerVertexFlags(m) && (pi.mask & Mask::IOM_VERTFLAGS))
500 fprintf(fpout,"%d ",vp->Flags());
501
502 if( HasPerVertexColor(m) && (pi.mask & Mask::IOM_VERTCOLOR) )
503 fprintf(fpout,"%d %d %d %d ",vp->C()[0],vp->C()[1],vp->C()[2],vp->C()[3] );
504
505 if( HasPerVertexQuality(m) && (pi.mask & Mask::IOM_VERTQUALITY) )
506 fprintf(fpout,"%.*g ",DGTVQ,vp->Q());
507
508 if( HasPerVertexRadius(m) && (pi.mask & Mask::IOM_VERTRADIUS) )
509 fprintf(fpout,"%.*g ",DGTVR,vp->R());
510
511 if( HasPerVertexTexCoord(m) && (pi.mask & Mask::IOM_VERTTEXCOORD) )
512 fprintf(fpout,"%.*g %.*g",DGTVT,vp->T().u(),DGTVT,vp->T().v());
513
514 for(size_t i=0;i<pi.VertDescriptorVec.size();i++)
515 {
516 float tf(0); double td(0); int ti;
517 if(!pi.VertAttrNameVec.empty() && !pi.VertAttrNameVec[i].empty())
518 { // trying to use named attribute to retrieve the value to store
519 assert(vcg::tri::HasPerVertexAttribute(m,pi.VertAttrNameVec[i]));
520 if (!pi.VertDescriptorVec[i].islist){
521 switch (pi.VertDescriptorVec[i].stotype1)
522 {
523 case ply::T_FLOAT : tf=PAH.tchfv[i][vp]; fprintf(fpout,"%f ",tf); break;
524 case ply::T_DOUBLE : td=PAH.tchdv[i][vp]; fprintf(fpout,"%lf ",td); break;
525 case ply::T_INT : ti=PAH.tchiv[i][vp]; fprintf(fpout,"%i ",ti); break;
526 case ply::T_SHORT : ti=PAH.tchsv[i][vp]; fprintf(fpout,"%i ",ti); break;
527 case ply::T_CHAR : ti=PAH.tchcv[i][vp]; fprintf(fpout,"%i ",ti); break;
528 case ply::T_UCHAR : ti=PAH.tchuv[i][vp]; fprintf(fpout,"%i ",ti); break;
529 default : assert(0);
530 }
531 }
532 else { //it is a Poin3f or a Point3d attribute. Saving it as a list
533 switch (pi.VertDescriptorVec[i].stotype1)
534 {
535 case ply::T_FLOAT : fprintf(fpout,"%d %f %f %f", 3, PAH.tchp3fv[i][vp][0], PAH.tchp3fv[i][vp][1], PAH.tchp3fv[i][vp][2]); break;
536 case ply::T_DOUBLE : fprintf(fpout,"%d %lf %lf %lf", 3, PAH.tchp3dv[i][vp][0], PAH.tchp3dv[i][vp][1], PAH.tchp3dv[i][vp][2]); break;
537 default : assert(0);
538 }
539 }
540 }
541 else
542 {
543 switch (pi.VertDescriptorVec[i].memtype1)
544 {
545 case ply::T_FLOAT : tf=*( (float *) (((char *)vp)+pi.VertDescriptorVec[i].offset1)); fprintf(fpout,"%f ",tf); break;
546 case ply::T_DOUBLE : td=*( (double *) (((char *)vp)+pi.VertDescriptorVec[i].offset1)); fprintf(fpout,"%lf ",tf); break;
547 case ply::T_INT : ti=*( (int *) (((char *)vp)+pi.VertDescriptorVec[i].offset1)); fprintf(fpout,"%i ",ti); break;
548 case ply::T_SHORT : ti=*( (short *) (((char *)vp)+pi.VertDescriptorVec[i].offset1)); fprintf(fpout,"%i ",ti); break;
549 case ply::T_CHAR : ti=*( (char *) (((char *)vp)+pi.VertDescriptorVec[i].offset1)); fprintf(fpout,"%i ",ti); break;
550 case ply::T_UCHAR : ti=*( (unsigned char *) (((char *)vp)+pi.VertDescriptorVec[i].offset1)); fprintf(fpout,"%i ",ti); break;
551 default : assert(0);
552 }
553 }
554 }
555
556 fprintf(fpout,"\n");
557 }
558 j++;
559 }
560 }
561 /*vcg::tri::*/
562 // this assert triggers when the vn != number of vertexes in vert that are not deleted.
563 assert(j==m.vn);
564
565 unsigned char b3char = 3;
566 unsigned char b9char = 9;
567 unsigned char b6char = 6;
568 FacePointer fp;
569 int vv[3];
570 FaceIterator fi;
571 int fcnt=0;
572 for(j=0,fi=m.face.begin();fi!=m.face.end();++fi)
573 {
574 //((m.vn+m.fn) != 0) all vertices and faces have been marked as deleted but the are still in the vert/face vectors
575 if(cb && ((j%1000)==0) && ((m.vn+m.fn) != 0))
576 (*cb)( 100*(m.vn+j)/(m.vn+m.fn), "Saving Vertices");
577
578 fp=&(*fi);
579 if( ! fp->IsD() )
580 { fcnt++;
581 if(binary)
582 {
583 vv[0]=indices[fp->cV(0)];
584 vv[1]=indices[fp->cV(1)];
585 vv[2]=indices[fp->cV(2)];
586 fwrite(&b3char,sizeof(char),1,fpout);
587 fwrite(vv,sizeof(int),3,fpout);
588
589 if(HasPerFaceFlags(m)&&( pi.mask & Mask::IOM_FACEFLAGS) ){
590 auto fl = fp->Flags();
591 fwrite(&fl,sizeof(int),1,fpout);
592 }
593
594 if( HasPerVertexTexCoord(m) && (!HasPerWedgeTexCoord(m)) && (pi.mask & Mask::IOM_WEDGTEXCOORD) ) // Note that you can save VT as WT if you really want it...
595 {
596 fwrite(&b6char,sizeof(char),1,fpout);
597 typename FaceType::TexCoordType::ScalarType t[6];
598 for(int k=0;k<3;++k)
599 {
600 t[k*2+0] = fp->V(k)->T().u();
601 t[k*2+1] = fp->V(k)->T().v();
602 }
603 fwrite(t,sizeof(typename FaceType::TexCoordType::ScalarType),6,fpout);
604 }
605 else if( HasPerWedgeTexCoord(m) && (pi.mask & Mask::IOM_WEDGTEXCOORD) )
606 {
607 fwrite(&b6char,sizeof(char),1,fpout);
608 typename FaceType::TexCoordType::ScalarType t[6];
609 for(int k=0;k<3;++k)
610 {
611 t[k*2+0] = fp->WT(k).u();
612 t[k*2+1] = fp->WT(k).v();
613 }
614 fwrite(t,sizeof(typename FaceType::TexCoordType::ScalarType),6,fpout);
615 }
616
617 if(saveTexIndexFlag)
618 {
619 int t = fp->WT(0).n();
620 fwrite(&t,sizeof(int),1,fpout);
621 }
622
623 if( HasPerFaceColor(m) && (pi.mask & Mask::IOM_FACECOLOR) )
624 fwrite(&( fp->C() ),sizeof(char),4,fpout);
625
626
627 if( HasPerWedgeColor(m) && (pi.mask & Mask::IOM_WEDGCOLOR) )
628 {
629 fwrite(&b9char,sizeof(char),1,fpout);
630 float t[3];
631 for(int z=0;z<3;++z)
632 {
633 t[0] = float(fp->WC(z)[0])/255;
634 t[1] = float(fp->WC(z)[1])/255;
635 t[2] = float(fp->WC(z)[2])/255;
636 fwrite( t,sizeof(float),3,fpout);
637 }
638 }
639
640 if( HasPerFaceNormal(m) && (pi.mask & Mask::IOM_FACENORMAL) )
641 {
642 ScalarType t;
643 t = ScalarType(fp->N()[0]); fwrite(&t,sizeof(ScalarType),1,fpout);
644 t = ScalarType(fp->N()[1]); fwrite(&t,sizeof(ScalarType),1,fpout);
645 t = ScalarType(fp->N()[2]); fwrite(&t,sizeof(ScalarType),1,fpout);
646 }
647
648 if( HasPerFaceQuality(m) && (pi.mask & Mask::IOM_FACEQUALITY) )
649 fwrite( &(fp->Q()),sizeof(typename FaceType::ScalarType),1,fpout);
650
651
652 for(size_t i=0;i<pi.FaceDescriptorVec.size();i++)
653 {
654 double td(0); float tf(0);int ti;short ts; char tc; unsigned char tu;
655 if(!pi.FaceAttrNameVec.empty() && !pi.FaceAttrNameVec[i].empty())
656 { // trying to use named attribute to retrieve the value to store
657 assert(vcg::tri::HasPerFaceAttribute(m,pi.FaceAttrNameVec[i]));
658 if (!pi.FaceDescriptorVec[i].islist){
659 switch (pi.FaceDescriptorVec[i].stotype1)
660 {
661 case ply::T_FLOAT : tf=PAH.tchff[i][fp]; fwrite(&tf, sizeof(float),1,fpout); break;
662 case ply::T_DOUBLE : td=PAH.tchdf[i][fp]; fwrite(&td, sizeof(double),1,fpout); break;
663 case ply::T_INT : ti=PAH.tchif[i][fp]; fwrite(&ti, sizeof(int),1,fpout); break;
664 case ply::T_SHORT : ts=PAH.tchsf[i][fp]; fwrite(&ts, sizeof(short),1,fpout); break;
665 case ply::T_CHAR : tc=PAH.tchcf[i][fp]; fwrite(&tc, sizeof(char),1,fpout); break;
666 case ply::T_UCHAR : tu=PAH.tchuf[i][fp]; fwrite(&tu,sizeof(unsigned char),1,fpout); break;
667 default : assert(0);
668 }
669 }
670 else {
671 static const unsigned char psize = 3;
672 switch (pi.FaceDescriptorVec[i].stotype1)
673 {
674 case ply::T_FLOAT :
675 fwrite(&psize, sizeof(unsigned char), 1,fpout);
676 fwrite(&PAH.tchp3ff[i][fp][0], sizeof(float), 1,fpout);
677 fwrite(&PAH.tchp3ff[i][fp][1], sizeof(float), 1,fpout);
678 fwrite(&PAH.tchp3ff[i][fp][2], sizeof(float), 1,fpout);
679 break;
680 case ply::T_DOUBLE :
681 fwrite(&psize, sizeof(unsigned char), 1,fpout);
682 fwrite(&PAH.tchp3df[i][fp][0], sizeof(double), 1,fpout);
683 fwrite(&PAH.tchp3df[i][fp][1], sizeof(double), 1,fpout);
684 fwrite(&PAH.tchp3df[i][fp][2], sizeof(double), 1,fpout);
685 break;
686 default : assert(0);
687 }
688 }
689 }
690 else
691 {
692 switch (pi.FaceDescriptorVec[i].stotype1){
693 case ply::T_FLOAT : PlyConv(pi.FaceDescriptorVec[i].memtype1, ((char *)fp)+pi.FaceDescriptorVec[i].offset1, tf ); fwrite(&tf, sizeof(float),1,fpout); break;
694 case ply::T_DOUBLE : PlyConv(pi.FaceDescriptorVec[i].memtype1, ((char *)fp)+pi.FaceDescriptorVec[i].offset1, td ); fwrite(&td, sizeof(double),1,fpout); break;
695 case ply::T_INT : PlyConv(pi.FaceDescriptorVec[i].memtype1, ((char *)fp)+pi.FaceDescriptorVec[i].offset1, ti ); fwrite(&ti, sizeof(int),1,fpout); break;
696 case ply::T_SHORT : PlyConv(pi.FaceDescriptorVec[i].memtype1, ((char *)fp)+pi.FaceDescriptorVec[i].offset1, ts ); fwrite(&ts, sizeof(short),1,fpout); break;
697 case ply::T_CHAR : PlyConv(pi.FaceDescriptorVec[i].memtype1, ((char *)fp)+pi.FaceDescriptorVec[i].offset1, tc ); fwrite(&tc, sizeof(char),1,fpout); break;
698 case ply::T_UCHAR : PlyConv(pi.FaceDescriptorVec[i].memtype1, ((char *)fp)+pi.FaceDescriptorVec[i].offset1, tu ); fwrite(&tu, sizeof(unsigned char),1,fpout); break;
699 default : assert(0);
700 }
701 }
702 }
703 }
704 else // ***** ASCII *****
705 {
706 fprintf(fpout,"%d " ,fp->VN());
707 for(int k=0;k<fp->VN();++k)
708 fprintf(fpout,"%d ",indices[fp->cV(k)]);
709
710 if(HasPerFaceFlags(m)&&( pi.mask & Mask::IOM_FACEFLAGS ))
711 fprintf(fpout,"%d ",fp->Flags());
712
713 if( HasPerVertexTexCoord(m) && (pi.mask & Mask::IOM_WEDGTEXCOORD) ) // you can save VT as WT if you really want it...
714 {
715 fprintf(fpout,"%d ",fp->VN()*2);
716 for(int k=0;k<fp->VN();++k)
717 fprintf(fpout,"%.*g %.*g "
718 ,DGTFT,fp->V(k)->T().u()
719 ,DGTFT,fp->V(k)->T().v()
720 );
721 }
722 else if( HasPerWedgeTexCoord(m) && (pi.mask & Mask::IOM_WEDGTEXCOORD) )
723 {
724 fprintf(fpout,"%d ",fp->VN()*2);
725 for(int k=0;k<fp->VN();++k)
726 fprintf(fpout,"%f %f "
727 ,fp->WT(k).u()
728 ,fp->WT(k).v()
729 );
730 }
731
732 if(saveTexIndexFlag)
733 {
734 fprintf(fpout,"%d ",fp->WT(0).n());
735 }
736
737 if( HasPerFaceColor(m) && (pi.mask & Mask::IOM_FACECOLOR) )
738 {
739 fprintf(fpout, "%u %u %u %u ", fp->C()[0], fp->C()[1], fp->C()[2], fp->C()[3]);
740 }
741 else if( HasPerWedgeColor(m) && (pi.mask & Mask::IOM_WEDGCOLOR) )
742 {
743 fprintf(fpout,"9 ");
744 for(int z=0;z<3;++z)
745 fprintf(fpout,"%g %g %g "
746 ,double(fp->WC(z)[0])/255
747 ,double(fp->WC(z)[1])/255
748 ,double(fp->WC(z)[2])/255
749 );
750 }
751
752 if (HasPerFaceNormal(m) && (pi.mask & Mask::IOM_FACENORMAL))
753 fprintf(fpout,"%.*g %.*g %.*g " ,DGT, ScalarType(fp->N()[0]),DGT,ScalarType(fp->N()[1]),DGT,ScalarType(fp->N()[2]));
754
755 if( HasPerFaceQuality(m) && (pi.mask & Mask::IOM_FACEQUALITY) )
756 fprintf(fpout,"%.*g ",DGTFQ,fp->Q());
757
758 for(size_t i=0;i<pi.FaceDescriptorVec.size();i++)
759 {
760 float tf(0); double td(0); int ti;
761 if(!pi.FaceAttrNameVec.empty() && !pi.FaceAttrNameVec[i].empty())
762 { // trying to use named attribute to retrieve the value to store
763 assert(vcg::tri::HasPerFaceAttribute(m,pi.FaceAttrNameVec[i]));
764 if(!pi.FaceDescriptorVec[i].islist) {
765 switch (pi.FaceDescriptorVec[i].stotype1)
766 {
767 case ply::T_FLOAT : tf=PAH.tchff[i][fp]; fprintf(fpout,"%f ",tf); break;
768 case ply::T_DOUBLE : td=PAH.tchdf[i][fp]; fprintf(fpout,"%g ",td); break;
769 case ply::T_INT : ti=PAH.tchif[i][fp]; fprintf(fpout,"%i ",ti); break;
770 case ply::T_SHORT : ti=PAH.tchsf[i][fp]; fprintf(fpout,"%i ",ti); break;
771 case ply::T_CHAR : ti=PAH.tchcf[i][fp]; fprintf(fpout,"%i ",ti); break;
772 case ply::T_UCHAR : ti=PAH.tchuf[i][fp]; fprintf(fpout,"%i ",ti); break;
773 default : assert(0);
774 }
775 }
776 else {
777 switch (pi.FaceDescriptorVec[i].stotype1)
778 {
779 case ply::T_FLOAT : fprintf(fpout,"%d %f %f %f", 3, PAH.tchp3ff[i][fp][0], PAH.tchp3ff[i][fp][1], PAH.tchp3ff[i][fp][2]); break;
780 case ply::T_DOUBLE : fprintf(fpout,"%d %lf %lf %lf", 3, PAH.tchp3df[i][fp][0], PAH.tchp3df[i][fp][1], PAH.tchp3df[i][fp][2]); break;
781 default : assert(0);
782 }
783 }
784 }
785 else
786 {
787 switch (pi.FaceDescriptorVec[i].memtype1)
788 {
789 case ply::T_FLOAT : tf=*( (float *) (((char *)fp)+pi.FaceDescriptorVec[i].offset1)); fprintf(fpout,"%g ",tf); break;
790 case ply::T_DOUBLE : td=*( (double *) (((char *)fp)+pi.FaceDescriptorVec[i].offset1)); fprintf(fpout,"%g ",tf); break;
791 case ply::T_INT : ti=*( (int *) (((char *)fp)+pi.FaceDescriptorVec[i].offset1)); fprintf(fpout,"%i ",ti); break;
792 case ply::T_SHORT : ti=*( (short *) (((char *)fp)+pi.FaceDescriptorVec[i].offset1)); fprintf(fpout,"%i ",ti); break;
793 case ply::T_CHAR : ti=*( (char *) (((char *)fp)+pi.FaceDescriptorVec[i].offset1)); fprintf(fpout,"%i ",ti); break;
794 case ply::T_UCHAR : ti=*( (unsigned char *) (((char *)fp)+pi.FaceDescriptorVec[i].offset1)); fprintf(fpout,"%i ",ti); break;
795 default : assert(0);
796 }
797 }
798 }
799
800 fprintf(fpout,"\n");
801 }
802 }
803 }
804 assert(fcnt==m.fn);
805 (void)fcnt;
806 int eauxvv[2];
807 if( pi.mask & Mask::IOM_EDGEINDEX )
808 {
809 int ecnt=0;
810 for(EdgeIterator ei=m.edge.begin();ei!=m.edge.end();++ei)
811 {
812 if( ! ei->IsD() )
813 {
814 ++ecnt;
815 if(binary)
816 {
817 eauxvv[0]=indices[ei->cV(0)];
818 eauxvv[1]=indices[ei->cV(1)];
819 fwrite(eauxvv,sizeof(int),2,fpout);
820 }
821 else // ***** ASCII *****
822 fprintf(fpout,"%d %d \n", indices[ei->cV(0)], indices[ei->cV(1)]);
823 }
824 }
825 assert(ecnt==m.en);
826 (void)ecnt;
827 }
828 int result = 0;
829 if (ferror(fpout)) result = ply::E_STREAMERROR;
830 fclose(fpout);
831 return result;
832 }
833
834 static const char *ErrorMsg(int error)
835 {
836 static std::vector<std::string> ply_error_msg;
837 if(ply_error_msg.empty())
838 {
839 ply_error_msg.resize(PlyInfo::E_MAXPLYINFOERRORS );
840 ply_error_msg[ply::E_NOERROR ]="No errors";
841 ply_error_msg[ply::E_CANTOPEN ]="Can't open file";
842 ply_error_msg[ply::E_NOTHEADER ]="Header not found";
843 ply_error_msg[ply::E_UNESPECTEDEOF ]="Eof in header";
844 ply_error_msg[ply::E_NOFORMAT ]="Format not found";
845 ply_error_msg[ply::E_SYNTAX ]="Syntax error on header";
846 ply_error_msg[ply::E_PROPOUTOFELEMENT ]="Property without element";
847 ply_error_msg[ply::E_BADTYPENAME ]="Bad type name";
848 ply_error_msg[ply::E_ELEMNOTFOUND ]="Element not found";
849 ply_error_msg[ply::E_PROPNOTFOUND ]="Property not found";
850 ply_error_msg[ply::E_BADTYPE ]="Bad type on addtoread";
851 ply_error_msg[ply::E_INCOMPATIBLETYPE ]="Incompatible type";
852 ply_error_msg[ply::E_BADCAST ]="Bad cast";
853
854 ply_error_msg[ply::E_STREAMERROR ] = "Output Stream Error";
855
856 ply_error_msg[PlyInfo::E_NO_VERTEX ]="No vertex field found";
857 ply_error_msg[PlyInfo::E_NO_FACE ]="No face field found";
858 ply_error_msg[PlyInfo::E_SHORTFILE ]="Unexpected EOF";
859 ply_error_msg[PlyInfo::E_NO_3VERTINFACE ]="Face with more than 3 vertices";
860 ply_error_msg[PlyInfo::E_BAD_VERT_INDEX ]="Bad vertex index in face";
861 ply_error_msg[PlyInfo::E_NO_6TCOORD ]="Face with no 6 texture coordinates";
862 ply_error_msg[PlyInfo::E_DIFFER_COLORS ]="Number of color differ from vertices";
863 }
864
865 if(error>PlyInfo::E_MAXPLYINFOERRORS || error<0) return "Unknown error";
866 else return ply_error_msg[error].c_str();
867 };
868
869 static int GetExportMaskCapability()
870 {
871 int capability = 0;
872 capability |= vcg::tri::io::Mask::IOM_VERTCOORD ;
873 capability |= vcg::tri::io::Mask::IOM_VERTFLAGS ;
874 capability |= vcg::tri::io::Mask::IOM_VERTCOLOR ;
875 capability |= vcg::tri::io::Mask::IOM_VERTQUALITY ;
876 capability |= vcg::tri::io::Mask::IOM_VERTNORMAL ;
877 capability |= vcg::tri::io::Mask::IOM_VERTRADIUS ;
878 capability |= vcg::tri::io::Mask::IOM_VERTTEXCOORD ;
879 capability |= vcg::tri::io::Mask::IOM_FACEINDEX ;
880 capability |= vcg::tri::io::Mask::IOM_FACEFLAGS ;
881 capability |= vcg::tri::io::Mask::IOM_FACECOLOR ;
882 capability |= vcg::tri::io::Mask::IOM_FACEQUALITY ;
883 // capability |= vcg::tri::io::Mask::IOM_FACENORMAL ;
884 capability |= vcg::tri::io::Mask::IOM_WEDGCOLOR ;
885 capability |= vcg::tri::io::Mask::IOM_WEDGTEXCOORD ;
886 capability |= vcg::tri::io::Mask::IOM_WEDGTEXMULTI ;
887 capability |= vcg::tri::io::Mask::IOM_WEDGNORMAL ;
888 capability |= vcg::tri::io::Mask::IOM_CAMERA ;
889 //capability |= vcg::tri::io::Mask::IOM_BITPOLYGONAL;
890 return capability;
891 }
892
893
894 }; // end class
895
896
897
898 } // end namespace tri
899 } // end namespace io
900 } // end namespace vcg
901 //@}
902 #endif