00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00033
00034 #ifndef _JFR_VCGLIB_EXPORT_PLY
00035 #define _JFR_VCGLIB_EXPORT_PLY
00036
00037 #include "modeler/jfr_io_mask.h"
00038 #include "fdetect/InterestFeature.hpp"
00039 #include "fdetect/RealDescriptor.hpp"
00040 #include<wrap/io_trimesh/io_ply.h>
00041 #include<vcg/container/simple_temporary_data.h>
00042 #include<vcg/complex/trimesh/allocate.h>
00043
00044 #include <stdio.h>
00045
00046 using namespace jafar;
00047 using namespace fdetect;
00048
00049 namespace vcg {
00050 namespace tri {
00051 namespace io {
00052
00053 template <class SaveMeshType>
00054 class JfrExporterPLY
00055 {
00056
00057
00058
00059
00060
00061
00062 template <class StoType>
00063 static void PlyConv(int mem_type, void *src, StoType &dest)
00064 {
00065 switch (mem_type){
00066 case ply::T_FLOAT : dest = (StoType) (* ((float *) src)); break;
00067 case ply::T_DOUBLE: dest = (StoType) (* ((double *) src)); break;
00068 case ply::T_INT : dest = (StoType) (* ((int *) src)); break;
00069 case ply::T_SHORT : dest = (StoType) (* ((short *) src)); break;
00070 case ply::T_CHAR : dest = (StoType) (* ((char *) src)); break;
00071 case ply::T_UCHAR : dest = (StoType) (* ((unsigned char *)src)); break;
00072 case ply::T_UINT : dest = (StoType) (* ((unsigned int *)src)); break;
00073 default : assert(0);
00074 }
00075 }
00076
00077 public:
00078 typedef ::vcg::ply::PropDescriptor PropDescriptor ;
00079 typedef typename SaveMeshType::VertexPointer VertexPointer;
00080 typedef typename SaveMeshType::ScalarType ScalarType;
00081 typedef typename SaveMeshType::VertexType VertexType;
00082 typedef typename SaveMeshType::FaceType FaceType;
00083 typedef typename SaveMeshType::FacePointer FacePointer;
00084 typedef typename SaveMeshType::VertexIterator VertexIterator;
00085 typedef typename SaveMeshType::FaceIterator FaceIterator;
00086
00087 static int Save(SaveMeshType &m, const char * filename, bool binary=true)
00088 {
00089 PlyInfo pi;
00090 return Save(m,filename,binary,pi);
00091 }
00092
00093 static int Save(SaveMeshType &m, const char * filename, int savemask, bool binary = true, CallBackPos *cb=0 )
00094 {
00095 PlyInfo pi;
00096 pi.mask=savemask;
00097 return Save(m,filename,binary,pi,cb);
00098 }
00099
00100 static int Save(SaveMeshType &m, const char * filename, bool binary, PlyInfo &pi, CallBackPos *cb=0)
00101 {
00102 FILE * fpout;
00103 int i;
00104
00105 const char * hbin = "binary_little_endian";
00106 const char * hasc = "ascii";
00107 const char * h;
00108
00109 bool multit = false;
00110
00111 if(binary) h=hbin;
00112 else h=hasc;
00113
00114 fpout = fopen(filename,"wb");
00115 if(fpout==NULL) {
00116 pi.status=::vcg::ply::E_CANTOPEN;
00117 return ::vcg::ply::E_CANTOPEN;
00118 }
00119 fprintf(fpout,
00120 "ply\n"
00121 "format %s 1.0\n"
00122 "comment VCGLIB generated\n"
00123 ,h
00124 );
00125
00126 if (((pi.mask & JfrMask::IOM_WEDGTEXCOORD) != 0) || ((pi.mask & JfrMask::IOM_VERTTEXCOORD) != 0))
00127 {
00128 const char * TFILE = "TextureFile";
00129
00130 for(i=0; i < static_cast<int>(m.textures.size()); ++i)
00131 fprintf(fpout,"comment %s %s\n", TFILE, (const char *)(m.textures[i].c_str()) );
00132
00133 if(m.textures.size()>1 && (HasPerWedgeTexCoord(m) || HasPerVertexTexCoord(m))) multit = true;
00134 }
00135
00136 if((pi.mask & JfrMask::IOM_CAMERA))
00137 {
00138 fprintf(fpout,
00139 "element camera %d\n"
00140 "property uint id\n"
00141 "property float view_px\n"
00142 "property float view_py\n"
00143 "property float view_pz\n"
00144 "property float x_axisx\n"
00145 "property float x_axisy\n"
00146 "property float x_axisz\n"
00147 "property float y_axisx\n"
00148 "property float y_axisy\n"
00149 "property float y_axisz\n"
00150 "property float z_axisx\n"
00151 "property float z_axisy\n"
00152 "property float z_axisz\n"
00153 "property float focal\n"
00154 "property float scalex\n"
00155 "property float scaley\n"
00156 "property float centerx\n"
00157 "property float centery\n"
00158 "property int viewportx\n"
00159 "property int viewporty\n"
00160 "property float k1\n"
00161 "property float k2\n"
00162 "property float k3\n"
00163 "property float k4\n"
00164 ,m.sn);
00165 }
00166 if( vcg::tri::HasPerVertexAttribute(m, "Origines") && (pi.mask & JfrMask::IOM_VERTORIGINES) )
00167 {
00168 fprintf(fpout,
00169 "element feature %d\n"
00170 "property double u\n"
00171 "property double v\n"
00172 "property uint id\n"
00173 "property uint index\n"
00174 "property double quality\n"
00175 "property uint parent_id\n"
00176 "property float coef\n"
00177 "property list uint float desc\n"
00178 ,m.ftn);
00179 }
00180 fprintf(fpout,
00181 "element vertex %d\n"
00182 "property float x\n"
00183 "property float y\n"
00184 "property float z\n"
00185 ,m.vn
00186 );
00187
00188 if( HasPerVertexNormal(m) &&( pi.mask & JfrMask::IOM_VERTNORMAL) )
00189 {
00190 fprintf(fpout,
00191 "property float nx\n"
00192 "property float ny\n"
00193 "property float nz\n"
00194 );
00195 }
00196
00197
00198 if( HasPerVertexFlags(m) &&( pi.mask & JfrMask::IOM_VERTFLAGS) )
00199 {
00200 fprintf(fpout,
00201 "property int flags\n"
00202 );
00203 }
00204
00205 if( HasPerVertexColor(m) && (pi.mask & JfrMask::IOM_VERTCOLOR) )
00206 {
00207 fprintf(fpout,
00208 "property uchar red\n"
00209 "property uchar green\n"
00210 "property uchar blue\n"
00211 "property uchar alpha\n"
00212 );
00213 }
00214
00215 if( HasPerVertexQuality(m) && (pi.mask & JfrMask::IOM_VERTQUALITY) )
00216 {
00217 fprintf(fpout,
00218 "property float quality\n"
00219 );
00220 }
00221
00222 if( tri::HasPerVertexRadius(m) && (pi.mask & JfrMask::IOM_VERTRADIUS) )
00223 {
00224 fprintf(fpout,
00225 "property float radius\n"
00226 );
00227 }
00228 if( ( HasPerVertexTexCoord(m) && pi.mask & JfrMask::IOM_VERTTEXCOORD ) )
00229 {
00230 fprintf(fpout,
00231 "property float texture_u\n"
00232 "property float texture_v\n"
00233 );
00234 }
00235 if( vcg::tri::HasPerVertexAttribute(m, "Id") && (pi.mask & JfrMask::IOM_VERTID) )
00236 {
00237 fprintf(fpout,
00238 "property uint vertex_id\n");
00239 }
00240 if( vcg::tri::HasPerVertexAttribute(m, "Origines") && (pi.mask & JfrMask::IOM_VERTORIGINES) )
00241 {
00242 fprintf(fpout,
00243 "property list uint uint feature_indices\n");
00244 }
00245 for(i=0;i<pi.vdn;i++)
00246 fprintf(fpout,"property %s %s\n",pi.VertexData[i].stotypename(),pi.VertexData[i].propname);
00247
00248 fprintf(fpout,
00249 "element face %d\n"
00250 "property list uchar int vertex_indices\n"
00251 ,m.fn
00252 );
00253
00254 if(HasPerFaceFlags(m) && (pi.mask & JfrMask::IOM_FACEFLAGS) )
00255 {
00256 fprintf(fpout,
00257 "property int flags\n"
00258 );
00259 }
00260
00261 if( (HasPerWedgeTexCoord(m) || HasPerVertexTexCoord(m) ) && pi.mask & JfrMask::IOM_WEDGTEXCOORD )
00262 {
00263 fprintf(fpout,
00264 "property list uchar float texcoord\n"
00265 );
00266
00267 if(multit)
00268 fprintf(fpout,
00269 "property int texnumber\n"
00270 );
00271 }
00272
00273 if( HasPerFaceColor(m) && (pi.mask & JfrMask::IOM_FACECOLOR) )
00274 {
00275 fprintf(fpout,
00276 "property uchar red\n"
00277 "property uchar green\n"
00278 "property uchar blue\n"
00279 "property uchar alpha\n"
00280 );
00281 }
00282
00283 if ( HasPerWedgeColor(m) && (pi.mask & JfrMask::IOM_WEDGCOLOR) )
00284 {
00285 fprintf(fpout,
00286 "property list uchar float color\n"
00287 );
00288 }
00289
00290 if( HasPerFaceQuality(m) && (pi.mask & JfrMask::IOM_FACEQUALITY) )
00291 {
00292 fprintf(fpout,
00293 "property float quality\n"
00294 );
00295 }
00296
00297 for(i=0;i<pi.fdn;i++)
00298 fprintf(fpout,"property %s %s\n",pi.FaceData[i].stotypename(),pi.FaceData[i].propname);
00299 fprintf(fpout, "end_header\n" );
00300
00301
00302 if((pi.mask & JfrMask::IOM_CAMERA))
00303 {
00304 unsigned int scnt = 0;
00305 if(binary)
00306 {
00307 for(typename std::map<unsigned int, vcg::Shot<ScalarType> >::const_iterator shots_it = m.shots.begin();
00308 shots_it != m.shots.end();
00309 ++shots_it)
00310 {
00311 unsigned int tui = shots_it->first;
00312 fwrite(&tui,sizeof(unsigned int),1,fpout);
00313 float t[17];
00314
00315 t[ 0] = (float)shots_it->second.Extrinsics.Tra()[0];
00316 t[ 1] = (float)shots_it->second.Extrinsics.Tra()[1];
00317 t[ 2] = (float)shots_it->second.Extrinsics.Tra()[2];
00318 t[ 3] = (float)shots_it->second.Extrinsics.Rot()[0][0];
00319 t[ 4] = (float)shots_it->second.Extrinsics.Rot()[0][1];
00320 t[ 5] = (float)shots_it->second.Extrinsics.Rot()[0][2];
00321 t[ 6] = (float)shots_it->second.Extrinsics.Rot()[1][0];
00322 t[ 7] = (float)shots_it->second.Extrinsics.Rot()[1][1];
00323 t[ 8] = (float)shots_it->second.Extrinsics.Rot()[1][2];
00324 t[ 9] = (float)shots_it->second.Extrinsics.Rot()[2][0];
00325 t[10] = (float)shots_it->second.Extrinsics.Rot()[2][1];
00326 t[11] = (float)shots_it->second.Extrinsics.Rot()[2][2];
00327 t[12] = (float)shots_it->second.Intrinsics.FocalMm;
00328 t[13] = (float)shots_it->second.Intrinsics.PixelSizeMm[0];
00329 t[14] = (float)shots_it->second.Intrinsics.PixelSizeMm[1];
00330 t[15] = (float)shots_it->second.Intrinsics.CenterPx[0];
00331 t[16] = (float)shots_it->second.Intrinsics.CenterPx[1];
00332 fwrite(t,sizeof(float),17,fpout);
00333
00334 fwrite( &shots_it->second.Intrinsics.ViewportPx[0],sizeof(int),2,fpout );
00335
00336 t[ 0] = (float)shots_it->second.Intrinsics.k[0];
00337 t[ 1] = (float)shots_it->second.Intrinsics.k[1];
00338 t[ 2] = (float)shots_it->second.Intrinsics.k[2];
00339 t[ 3] = (float)shots_it->second.Intrinsics.k[3];
00340 fwrite(t,sizeof(float),4,fpout);
00341 scnt++;
00342 }
00343 }
00344 else
00345 {
00346 for(typename std::map<unsigned int, vcg::Shot<ScalarType> >::const_iterator shots_it = m.shots.begin();
00347 shots_it != m.shots.end();
00348 ++shots_it)
00349 {
00350 fprintf(fpout,"%u %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %d %d %g %g %g %g\n"
00351 ,shots_it->first
00352 ,-shots_it->second.Extrinsics.Tra()[0]
00353 ,-shots_it->second.Extrinsics.Tra()[1]
00354 ,-shots_it->second.Extrinsics.Tra()[2]
00355 ,shots_it->second.Extrinsics.Rot()[0][0]
00356 ,shots_it->second.Extrinsics.Rot()[0][1]
00357 ,shots_it->second.Extrinsics.Rot()[0][2]
00358 ,shots_it->second.Extrinsics.Rot()[1][0]
00359 ,shots_it->second.Extrinsics.Rot()[1][1]
00360 ,shots_it->second.Extrinsics.Rot()[1][2]
00361 ,shots_it->second.Extrinsics.Rot()[2][0]
00362 ,shots_it->second.Extrinsics.Rot()[2][1]
00363 ,shots_it->second.Extrinsics.Rot()[2][2]
00364 ,shots_it->second.Intrinsics.FocalMm
00365 ,shots_it->second.Intrinsics.PixelSizeMm[0]
00366 ,shots_it->second.Intrinsics.PixelSizeMm[1]
00367 ,shots_it->second.Intrinsics.CenterPx[0]
00368 ,shots_it->second.Intrinsics.CenterPx[1]
00369 ,shots_it->second.Intrinsics.ViewportPx[0]
00370 ,shots_it->second.Intrinsics.ViewportPx[1]
00371 ,shots_it->second.Intrinsics.k[0]
00372 ,shots_it->second.Intrinsics.k[1]
00373 ,shots_it->second.Intrinsics.k[2]
00374 ,shots_it->second.Intrinsics.k[3]
00375 );
00376 scnt++;
00377 }
00378 }
00379 assert(m.sn == scnt);
00380 }
00381
00382
00383 if( vcg::tri::HasPerVertexAttribute(m, "Origines") && (pi.mask & JfrMask::IOM_VERTORIGINES) )
00384 {
00385 unsigned int ftcnt = 0;
00386 if(binary)
00387 {
00388 for(std::vector<fdetect_v2::InterestFeature<fdetect_v2::FloatDescriptor>* >::const_iterator it = m.features.begin();
00389 it != m.features.end();
00390 ++it)
00391 {
00392 double td(0); float tf(0); unsigned int tui;
00393 td = double((*it)->u()); fwrite(&td,sizeof(double),1,fpout);
00394 td = double((*it)->v()); fwrite(&td,sizeof(double),1,fpout);
00395 tui = (*it)->id(); fwrite(&tui,sizeof(unsigned int),1,fpout);
00396 tui = (*it)->index(); fwrite(&tui,sizeof(unsigned int),1,fpout);
00397 td = double((*it)->quality()); fwrite(&td,sizeof(double),1,fpout);
00398 tui = (*it)->parent_id; fwrite(&tui,sizeof(unsigned int),1,fpout);
00399 fdetect_v2::FloatDescriptor fd = (*it)->descriptor();
00400 tf = float(fd.m_normalizeCoef); fwrite(&tf,sizeof(float),1,fpout);
00401 tui = fd.m_descr.size(); fwrite(&tui,sizeof(unsigned int),1,fpout);
00402 for(unsigned int counter=0; counter < tui; counter++)
00403 {
00404 tf = float(fd.m_descr[counter]);
00405 fwrite(&tf,sizeof(float),1,fpout);
00406 }
00407 ++ftcnt;
00408 }
00409 }
00410 else
00411 {
00412 for(std::vector<fdetect_v2::InterestFeature<fdetect_v2::FloatDescriptor>* >::const_iterator it = m.features.begin();
00413 it != m.features.end();
00414 ++it)
00415 {
00416 fdetect_v2::FloatDescriptor fdes = (*it)->descriptor();
00417 fprintf(fpout,"%f %f %u %u %f %u %f %zu",(*it)->u(), (*it)->v(), (*it)->id(), (*it)->index(), (*it)->quality(), (*it)->parent_id, fdes.m_normalizeCoef, fdes.m_descr.size());
00418 for(unsigned int counter = 0; counter < fdes.m_descr.size(); ++counter)
00419 {
00420 fprintf(fpout, " %f", fdes.m_descr[counter]);
00421 }
00422 fprintf(fpout,"\n");
00423 ++ftcnt;
00424 }
00425 }
00426 assert(ftcnt==m.ftn);
00427 }
00428
00429 int j;
00430 std::vector<int> FlagV;
00431 VertexPointer vp;
00432 VertexIterator vi;
00433 SimpleTempData<typename SaveMeshType::VertContainer,int> indices(m.vert);
00434
00435 for(j=0,vi=m.vert.begin();vi!=m.vert.end();++vi){
00436 vp=&(*vi);
00437 indices[vi] = j;
00438
00439 if(cb && ((j%1000)==0))(*cb)( (100*j)/(m.vn+m.fn), "Saving Vertices");
00440
00441 if( !HasPerVertexFlags(m) || !vp->IsD() )
00442 {
00443 if(binary)
00444 {
00445 float t; unsigned int tui;
00446 typedef unsigned int uint;
00447 t = float(vp->P()[0]); fwrite(&t,sizeof(float),1,fpout);
00448 t = float(vp->P()[1]); fwrite(&t,sizeof(float),1,fpout);
00449 t = float(vp->P()[2]); fwrite(&t,sizeof(float),1,fpout);
00450
00451 if( HasPerVertexNormal(m) && (pi.mask & JfrMask::IOM_VERTNORMAL) )
00452 {
00453 t = float(vp->N()[0]); fwrite(&t,sizeof(float),1,fpout);
00454 t = float(vp->N()[1]); fwrite(&t,sizeof(float),1,fpout);
00455 t = float(vp->N()[2]); fwrite(&t,sizeof(float),1,fpout);
00456 }
00457 if( HasPerVertexFlags(m) && (pi.mask & JfrMask::IOM_VERTFLAGS) )
00458 fwrite(&(vp->UberFlags()),sizeof(int),1,fpout);
00459
00460 if( HasPerVertexColor(m) && (pi.mask & JfrMask::IOM_VERTCOLOR) )
00461 fwrite(&( vp->C() ),sizeof(char),4,fpout);
00462
00463 if( HasPerVertexQuality(m) && (pi.mask & JfrMask::IOM_VERTQUALITY) )
00464 fwrite(&( vp->Q() ),sizeof(float),1,fpout);
00465
00466 if( HasPerVertexRadius(m) && (pi.mask & JfrMask::IOM_VERTRADIUS) )
00467 fwrite(&( vp->R() ),sizeof(float),1,fpout);
00468
00469 if( HasPerVertexTexCoord(m) && (pi.mask & JfrMask::IOM_VERTTEXCOORD) )
00470 {
00471 t = float(vp->T().u()); fwrite(&t,sizeof(float),1,fpout);
00472 t = float(vp->T().v()); fwrite(&t,sizeof(float),1,fpout);
00473 }
00474
00475 if( vcg::tri::HasPerVertexAttribute(m, "Id") && (pi.mask & JfrMask::IOM_VERTID) )
00476 {
00477 tui = m.id_getter[vp]; fwrite(&tui,sizeof(unsigned int),1,fpout);
00478 }
00479
00480 if( vcg::tri::HasPerVertexAttribute(m, "Origines") && (pi.mask & JfrMask::IOM_VERTORIGINES) )
00481 {
00482 tui = m.origines_getter[vp].size(); fwrite(&tui, sizeof(unsigned int), 1, fpout);
00483 for (std::vector<unsigned int>::const_iterator it = m.origines_getter[vp].begin();
00484 it != m.origines_getter[vp].end();
00485 ++it)
00486 {
00487 tui = *it; fwrite(&tui, sizeof(unsigned int), 1, fpout);
00488 }
00489 }
00490
00491 for(i=0;i<pi.vdn;i++)
00492 {
00493 double td(0); float tf(0);int ti;short ts; char tc; unsigned char tuc; unsigned int tui;
00494 switch (pi.VertexData[i].stotype1)
00495 {
00496 case ply::T_FLOAT : PlyConv(pi.VertexData[i].memtype1, ((char *)vp)+pi.VertexData[i].offset1, tf ); fwrite(&tf, sizeof(float),1,fpout); break;
00497 case ply::T_DOUBLE : PlyConv(pi.VertexData[i].memtype1, ((char *)vp)+pi.VertexData[i].offset1, td ); fwrite(&td, sizeof(double),1,fpout); break;
00498 case ply::T_INT : PlyConv(pi.VertexData[i].memtype1, ((char *)vp)+pi.VertexData[i].offset1, ti ); fwrite(&ti, sizeof(int),1,fpout); break;
00499 case ply::T_SHORT : PlyConv(pi.VertexData[i].memtype1, ((char *)vp)+pi.VertexData[i].offset1, ts ); fwrite(&ts, sizeof(short),1,fpout); break;
00500 case ply::T_CHAR : PlyConv(pi.VertexData[i].memtype1, ((char *)vp)+pi.VertexData[i].offset1, tc ); fwrite(&tc, sizeof(char),1,fpout); break;
00501 case ply::T_UCHAR : PlyConv(pi.VertexData[i].memtype1, ((char *)vp)+pi.VertexData[i].offset1, tuc); fwrite(&tuc,sizeof(unsigned char),1,fpout); break;
00502 case ply::T_UINT : PlyConv(pi.VertexData[i].memtype1, ((char *)vp)+pi.VertexData[i].offset1, tui); fwrite(&tui,sizeof(unsigned int),1,fpout); break;
00503 default : assert(0);
00504 }
00505 }
00506 }
00507 else
00508 {
00509 fprintf(fpout,"%g %g %g " ,vp->P()[0],vp->P()[1],vp->P()[2]);
00510
00511 if( HasPerVertexNormal(m) && (pi.mask & JfrMask::IOM_VERTNORMAL) )
00512 fprintf(fpout,"%g %g %g " ,double(vp->N()[0]),double(vp->N()[1]),double(vp->N()[2]));
00513
00514 if( HasPerVertexFlags(m) && (pi.mask & JfrMask::IOM_VERTFLAGS))
00515 fprintf(fpout,"%d ",vp->UberFlags());
00516
00517 if( HasPerVertexColor(m) && (pi.mask & JfrMask::IOM_VERTCOLOR) )
00518 fprintf(fpout,"%d %d %d %d ",vp->C()[0],vp->C()[1],vp->C()[2],vp->C()[3] );
00519
00520 if( HasPerVertexQuality(m) && (pi.mask & JfrMask::IOM_VERTQUALITY) )
00521 fprintf(fpout,"%g ",vp->Q());
00522
00523 if( HasPerVertexRadius(m) && (pi.mask & JfrMask::IOM_VERTRADIUS) )
00524 fprintf(fpout,"%g ",vp->R());
00525
00526 if( HasPerVertexTexCoord(m) && (pi.mask & JfrMask::IOM_VERTTEXCOORD) )
00527 fprintf(fpout,"%g %g ",vp->T().u(),vp->T().v());
00528
00529 if( vcg::tri::HasPerVertexAttribute(m, "Id") && (pi.mask & JfrMask::IOM_VERTID) )
00530 {
00531 fprintf(fpout,"%u ",m.id_getter[vp]);
00532 }
00533 if( vcg::tri::HasPerVertexAttribute(m, "Origines") && (pi.mask & JfrMask::IOM_VERTORIGINES) )
00534 {
00535 fprintf(fpout,"%zu ",m.origines_getter[vp].size());
00536 for (std::vector<unsigned int>::const_iterator it = m.origines_getter[vp].begin();
00537 it != m.origines_getter[vp].end();
00538 ++it)
00539 {
00540 fprintf(fpout,"%u ",*it);
00541 }
00542 }
00543
00544 for(i=0;i<pi.vdn;i++)
00545 {
00546 float tf(0); double td(0);
00547 int ti; unsigned int tui;
00548 switch (pi.VertexData[i].memtype1)
00549 {
00550 case ply::T_FLOAT : tf=*( (float *) (((char *)vp)+pi.VertexData[i].offset1)); fprintf(fpout,"%g ",tf); break;
00551 case ply::T_DOUBLE : td=*( (double *) (((char *)vp)+pi.VertexData[i].offset1)); fprintf(fpout,"%g ",tf); break;
00552 case ply::T_INT : ti=*( (int *) (((char *)vp)+pi.VertexData[i].offset1)); fprintf(fpout,"%i ",ti); break;
00553 case ply::T_SHORT : ti=*( (short *) (((char *)vp)+pi.VertexData[i].offset1)); fprintf(fpout,"%i ",ti); break;
00554 case ply::T_CHAR : ti=*( (char *) (((char *)vp)+pi.VertexData[i].offset1)); fprintf(fpout,"%i ",ti); break;
00555 case ply::T_UCHAR : ti=*( (unsigned char *) (((char *)vp)+pi.VertexData[i].offset1)); fprintf(fpout,"%i ",ti); break;
00556 case ply::T_UINT : tui=*( (unsigned int *) (((char *)vp)+pi.VertexData[i].offset1)); fprintf(fpout,"%u ",tui); break;
00557 default : assert(0);
00558 }
00559 }
00560
00561 fprintf(fpout,"\n");
00562 }
00563 j++;
00564 }
00565 }
00566
00567 assert(j==m.vn);
00568
00569 char c = 3;
00570 unsigned char b9 = 9;
00571 unsigned char b6 = 6;
00572 FacePointer fp;
00573 int vv[3];
00574 FaceIterator fi;
00575 int fcnt=0;
00576 for(j=0,fi=m.face.begin();fi!=m.face.end();++fi)
00577 {
00578 if(cb && ((j%1000)==0) ) (*cb)( 100*(m.vn+j)/(m.vn+m.fn), "Saving Vertices");
00579
00580 fp=&(*fi);
00581 if( ! fp->IsD() )
00582 { fcnt++;
00583 if(binary)
00584 {
00585 vv[0]=indices[fp->cV(0)];
00586 vv[1]=indices[fp->cV(1)];
00587 vv[2]=indices[fp->cV(2)];
00588 fwrite(&c,1,1,fpout);
00589 fwrite(vv,sizeof(int),3,fpout);
00590
00591 if(HasPerFaceFlags(m)&&( pi.mask & JfrMask::IOM_FACEFLAGS) )
00592 fwrite(&(fp->Flags()),sizeof(int),1,fpout);
00593
00594 if( HasPerVertexTexCoord(m) && (pi.mask & JfrMask::IOM_WEDGTEXCOORD) )
00595 {
00596 fwrite(&b6,sizeof(char),1,fpout);
00597 float t[6];
00598 for(int k=0;k<3;++k)
00599 {
00600 t[k*2+0] = fp->V(k)->T().u();
00601 t[k*2+1] = fp->V(k)->T().v();
00602 }
00603 fwrite(t,sizeof(float),6,fpout);
00604 }
00605 else if( HasPerWedgeTexCoord(m) && (pi.mask & JfrMask::IOM_WEDGTEXCOORD) )
00606 {
00607 fwrite(&b6,sizeof(char),1,fpout);
00608 float t[6];
00609 for(int k=0;k<3;++k)
00610 {
00611 t[k*2+0] = fp->WT(k).u();
00612 t[k*2+1] = fp->WT(k).v();
00613 }
00614 fwrite(t,sizeof(float),6,fpout);
00615 }
00616
00617 if(multit)
00618 {
00619 int t = fp->WT(0).n();
00620 fwrite(&t,sizeof(int),1,fpout);
00621 }
00622
00623 if( HasPerFaceColor(m) && (pi.mask & JfrMask::IOM_FACECOLOR) )
00624 fwrite(&( fp->C() ),sizeof(char),4,fpout);
00625
00626
00627 if( HasPerWedgeColor(m) && (pi.mask & JfrMask::IOM_WEDGCOLOR) )
00628 {
00629 fwrite(&b9,sizeof(char),1,fpout);
00630 float t[3];
00631 for(int z=0;z<3;++z)
00632 {
00633 t[0] = float(fp->WC(z)[0])/255;
00634 t[1] = float(fp->WC(z)[1])/255;
00635 t[2] = float(fp->WC(z)[2])/255;
00636 fwrite( t,sizeof(float),3,fpout);
00637 }
00638 }
00639
00640 if( HasPerFaceQuality(m) && (pi.mask & JfrMask::IOM_FACEQUALITY) )
00641 fwrite( &(fp->Q()),sizeof(float),1,fpout);
00642
00643
00644 for(i=0;i<pi.fdn;i++)
00645 {
00646 double td(0); float tf(0);int ti;short ts; char tc; unsigned char tuc;
00647 switch (pi.FaceData[i].stotype1){
00648 case ply::T_FLOAT : PlyConv(pi.FaceData[i].memtype1, ((char *)fp)+pi.FaceData[i].offset1, tf ); fwrite(&tf, sizeof(float),1,fpout); break;
00649 case ply::T_DOUBLE : PlyConv(pi.FaceData[i].memtype1, ((char *)fp)+pi.FaceData[i].offset1, td ); fwrite(&td, sizeof(double),1,fpout); break;
00650 case ply::T_INT : PlyConv(pi.FaceData[i].memtype1, ((char *)fp)+pi.FaceData[i].offset1, ti ); fwrite(&ti, sizeof(int),1,fpout); break;
00651 case ply::T_SHORT : PlyConv(pi.FaceData[i].memtype1, ((char *)fp)+pi.FaceData[i].offset1, ts ); fwrite(&ts, sizeof(short),1,fpout); break;
00652 case ply::T_CHAR : PlyConv(pi.FaceData[i].memtype1, ((char *)fp)+pi.FaceData[i].offset1, tc ); fwrite(&tc, sizeof(char),1,fpout); break;
00653 case ply::T_UCHAR : PlyConv(pi.FaceData[i].memtype1, ((char *)fp)+pi.FaceData[i].offset1, tuc); fwrite(&tuc,sizeof(unsigned char),1,fpout); break;
00654 default : assert(0);
00655 }
00656 }
00657 }
00658 else
00659 {
00660 fprintf(fpout,"3 %d %d %d ",
00661 indices[fp->cV(0)], indices[fp->cV(1)], indices[fp->cV(2)] );
00662
00663 if(HasPerFaceFlags(m)&&( pi.mask & JfrMask::IOM_FACEFLAGS ))
00664 fprintf(fpout,"%d ",fp->Flags());
00665
00666 if( HasPerVertexTexCoord(m) && (pi.mask & JfrMask::IOM_WEDGTEXCOORD) )
00667 {
00668 fprintf(fpout,"6 ");
00669 for(int k=0;k<3;++k)
00670 fprintf(fpout,"%g %g "
00671 ,fp->V(k)->T().u()
00672 ,fp->V(k)->T().v()
00673 );
00674 }
00675 else if( HasPerWedgeTexCoord(m) && (pi.mask & JfrMask::IOM_WEDGTEXCOORD) )
00676 {
00677 fprintf(fpout,"6 ");
00678 for(int k=0;k<3;++k)
00679 fprintf(fpout,"%g %g "
00680 ,fp->WT(k).u()
00681 ,fp->WT(k).v()
00682 );
00683 }
00684
00685 if(multit)
00686 {
00687 fprintf(fpout,"%d ",fp->WT(0).n());
00688 }
00689
00690 if( HasPerFaceColor(m) && (pi.mask & JfrMask::IOM_FACECOLOR) )
00691 {
00692 float t[3];
00693 t[0] = float(fp->C()[0])/255;
00694 t[1] = float(fp->C()[1])/255;
00695 t[2] = float(fp->C()[2])/255;
00696 fprintf(fpout,"9 ");
00697 fprintf(fpout,"%g %g %g ",t[0],t[1],t[2]);
00698 fprintf(fpout,"%g %g %g ",t[0],t[1],t[2]);
00699 fprintf(fpout,"%g %g %g ",t[0],t[1],t[2]);
00700 }
00701 else if( HasPerWedgeColor(m) && (pi.mask & JfrMask::IOM_WEDGCOLOR) )
00702 {
00703 fprintf(fpout,"9 ");
00704 for(int z=0;z<3;++z)
00705 fprintf(fpout,"%g %g %g "
00706 ,double(fp->WC(z)[0])/255
00707 ,double(fp->WC(z)[1])/255
00708 ,double(fp->WC(z)[2])/255
00709 );
00710 }
00711
00712 if( HasPerFaceQuality(m) && (pi.mask & JfrMask::IOM_FACEQUALITY) )
00713 fprintf(fpout,"%g ",fp->Q());
00714
00715 for(i=0;i<pi.fdn;i++)
00716 {
00717 float tf(0); double td(0);
00718 int ti;
00719 switch (pi.FaceData[i].memtype1)
00720 {
00721 case ply::T_FLOAT : tf=*( (float *) (((char *)fp)+pi.FaceData[i].offset1)); fprintf(fpout,"%g ",tf); break;
00722 case ply::T_DOUBLE : td=*( (double *) (((char *)fp)+pi.FaceData[i].offset1)); fprintf(fpout,"%g ",tf); break;
00723 case ply::T_INT : ti=*( (int *) (((char *)fp)+pi.FaceData[i].offset1)); fprintf(fpout,"%i ",ti); break;
00724 case ply::T_SHORT : ti=*( (short *) (((char *)fp)+pi.FaceData[i].offset1)); fprintf(fpout,"%i ",ti); break;
00725 case ply::T_CHAR : ti=*( (char *) (((char *)fp)+pi.FaceData[i].offset1)); fprintf(fpout,"%i ",ti); break;
00726 case ply::T_UCHAR : ti=*( (unsigned char *) (((char *)fp)+pi.FaceData[i].offset1)); fprintf(fpout,"%i ",ti); break;
00727 default : assert(0);
00728 }
00729 }
00730
00731 fprintf(fpout,"\n");
00732 }
00733 }
00734 }
00735 assert(fcnt==m.fn);
00736 fclose(fpout);
00737 return 0;
00738 }
00739
00740 static const char *ErrorMsg(int error)
00741 {
00742 static std::vector<std::string> ply_error_msg;
00743 if(ply_error_msg.empty())
00744 {
00745 ply_error_msg.resize(PlyInfo::E_MAXPLYINFOERRORS );
00746 ply_error_msg[ply::E_NOERROR ]="No errors";
00747 ply_error_msg[ply::E_CANTOPEN ]="Can't open file";
00748 ply_error_msg[ply::E_NOTHEADER ]="Header not found";
00749 ply_error_msg[ply::E_UNESPECTEDEOF ]="Eof in header";
00750 ply_error_msg[ply::E_NOFORMAT ]="Format not found";
00751 ply_error_msg[ply::E_SYNTAX ]="Syntax error on header";
00752 ply_error_msg[ply::E_PROPOUTOFELEMENT]="Property without element";
00753 ply_error_msg[ply::E_BADTYPENAME ]="Bad type name";
00754 ply_error_msg[ply::E_ELEMNOTFOUND ]="Element not found";
00755 ply_error_msg[ply::E_PROPNOTFOUND ]="Property not found";
00756 ply_error_msg[ply::E_BADTYPE ]="Bad type on addtoread";
00757 ply_error_msg[ply::E_INCOMPATIBLETYPE]="Incompatible type";
00758 ply_error_msg[ply::E_BADCAST ]="Bad cast";
00759
00760 ply_error_msg[PlyInfo::E_NO_VERTEX ]="No vertex field found";
00761 ply_error_msg[PlyInfo::E_NO_FACE ]="No face field found";
00762 ply_error_msg[PlyInfo::E_SHORTFILE ]="Unespected eof";
00763 ply_error_msg[PlyInfo::E_NO_3VERTINFACE ]="Face with more than 3 vertices";
00764 ply_error_msg[PlyInfo::E_BAD_VERT_INDEX ]="Bad vertex index in face";
00765 ply_error_msg[PlyInfo::E_NO_6TCOORD ]="Face with no 6 texture coordinates";
00766 ply_error_msg[PlyInfo::E_DIFFER_COLORS ]="Number of color differ from vertices";
00767 }
00768
00769 if(error>PlyInfo::E_MAXPLYINFOERRORS || error<0) return "Unknown error";
00770 else return ply_error_msg[error].c_str();
00771 };
00772
00773 static int GetExportJfrMaskCapability()
00774 {
00775 int capability = 0;
00776 capability |= vcg::tri::io::JfrMask::IOM_VERTCOORD ;
00777 capability |= vcg::tri::io::JfrMask::IOM_VERTFLAGS ;
00778 capability |= vcg::tri::io::JfrMask::IOM_VERTCOLOR ;
00779 capability |= vcg::tri::io::JfrMask::IOM_VERTQUALITY ;
00780 capability |= vcg::tri::io::JfrMask::IOM_VERTNORMAL ;
00781 capability |= vcg::tri::io::JfrMask::IOM_VERTRADIUS ;
00782 capability |= vcg::tri::io::JfrMask::IOM_VERTTEXCOORD ;
00783 capability |= vcg::tri::io::JfrMask::IOM_VERTID ;
00784 capability |= vcg::tri::io::JfrMask::IOM_VERTORIGINES ;
00785 capability |= vcg::tri::io::JfrMask::IOM_FACEINDEX ;
00786 capability |= vcg::tri::io::JfrMask::IOM_FACEFLAGS ;
00787 capability |= vcg::tri::io::JfrMask::IOM_FACECOLOR ;
00788 capability |= vcg::tri::io::JfrMask::IOM_FACEQUALITY ;
00789
00790 capability |= vcg::tri::io::JfrMask::IOM_WEDGCOLOR ;
00791 capability |= vcg::tri::io::JfrMask::IOM_WEDGTEXCOORD ;
00792 capability |= vcg::tri::io::JfrMask::IOM_WEDGTEXMULTI ;
00793 capability |= vcg::tri::io::JfrMask::IOM_WEDGNORMAL ;
00794 capability |= vcg::tri::io::JfrMask::IOM_CAMERA ;
00795 return capability;
00796 }
00797
00798
00799 };
00800
00801
00802
00803 }
00804 }
00805 }
00807 #endif