diff --git a/wrap/io_trimesh/export_ply.h b/wrap/io_trimesh/export_ply.h index ca097e96..b5be20ca 100644 --- a/wrap/io_trimesh/export_ply.h +++ b/wrap/io_trimesh/export_ply.h @@ -34,827 +34,873 @@ #include #include #include - +#include #include namespace vcg { - namespace tri { - namespace io { +namespace tri { +namespace io { - template - class ExporterPLY - { - // Si occupa di convertire da un tipo all'altro. - // usata nella saveply per matchare i tipi tra stotype e memtype. - // Ad es se in memoria c'e' un int e voglio salvare un float - // src sara in effetti un puntatore a int il cui valore deve - // essere convertito al tipo di ritorno desiderato (stotype) +template +class ExporterPLY +{ + // Si occupa di convertire da un tipo all'altro. + // usata nella saveply per matchare i tipi tra stotype e memtype. + // Ad es se in memoria c'e' un int e voglio salvare un float + // src sara in effetti un puntatore a int il cui valore deve + // essere convertito al tipo di ritorno desiderato (stotype) - template - static void PlyConv(int mem_type, void *src, StoType &dest) - { - switch (mem_type){ - case ply::T_FLOAT : dest = (StoType) (* ((float *) src)); break; - case ply::T_DOUBLE: dest = (StoType) (* ((double *) src)); break; - case ply::T_INT : dest = (StoType) (* ((int *) src)); break; - case ply::T_SHORT : dest = (StoType) (* ((short *) src)); break; - case ply::T_CHAR : dest = (StoType) (* ((char *) src)); break; - case ply::T_UCHAR : dest = (StoType) (* ((unsigned char *)src)); break; - default : assert(0); - } - } + template + static void PlyConv(int mem_type, void *src, StoType &dest) + { + switch (mem_type){ + case ply::T_FLOAT : dest = (StoType) (* ((float *) src)); break; + case ply::T_DOUBLE: dest = (StoType) (* ((double *) src)); break; + case ply::T_INT : dest = (StoType) (* ((int *) src)); break; + case ply::T_SHORT : dest = (StoType) (* ((short *) src)); break; + case ply::T_CHAR : dest = (StoType) (* ((char *) src)); break; + case ply::T_UCHAR : dest = (StoType) (* ((unsigned char *)src)); break; + default : assert(0); + } + } - public: - typedef ::vcg::ply::PropDescriptor PropDescriptor ; - typedef typename SaveMeshType::ConstVertexPointer VertexPointer; - typedef typename SaveMeshType::ScalarType ScalarType; - typedef typename SaveMeshType::VertexType VertexType; - typedef typename SaveMeshType::FaceType FaceType; - typedef typename SaveMeshType::ConstFacePointer FacePointer; - typedef typename SaveMeshType::ConstVertexIterator VertexIterator; - typedef typename SaveMeshType::ConstFaceIterator FaceIterator; - typedef typename SaveMeshType::ConstEdgeIterator EdgeIterator; - typedef typename vcg::Shot::ScalarType ShotScalarType; +public: + typedef ::vcg::ply::PropDescriptor PropDescriptor ; + typedef typename SaveMeshType::ConstVertexPointer VertexPointer; + typedef typename SaveMeshType::ScalarType ScalarType; + typedef typename SaveMeshType::VertexType VertexType; + typedef typename SaveMeshType::FaceType FaceType; + typedef typename SaveMeshType::ConstFacePointer FacePointer; + typedef typename SaveMeshType::ConstVertexIterator VertexIterator; + typedef typename SaveMeshType::ConstFaceIterator FaceIterator; + typedef typename SaveMeshType::ConstEdgeIterator EdgeIterator; + typedef typename vcg::Shot::ScalarType ShotScalarType; - static int Save(const SaveMeshType &m, const char * filename, bool binary=true) - { - PlyInfo pi; - return Save(m,filename,binary,pi); - } + static int Save(const SaveMeshType &m, const char * filename, bool binary=true) + { + PlyInfo pi; + return Save(m,filename,binary,pi); + } - static int Save(const SaveMeshType &m, const char * filename, int savemask, bool binary = true, CallBackPos *cb=0 ) - { - PlyInfo pi; - pi.mask=savemask; - return Save(m,filename,binary,pi,cb); - } + static int Save(const SaveMeshType &m, const char * filename, int savemask, bool binary = true, CallBackPos *cb=0 ) + { + PlyInfo pi; + pi.mask=savemask; + return Save(m,filename,binary,pi,cb); + } - static int Save(const SaveMeshType &m, const char * filename, bool binary, const PlyInfo &pi, CallBackPos *cb=0) // V1.0 - { - FILE * fpout; - const char * hbin = "binary_little_endian"; - const char * hasc = "ascii"; - const char * h; - //Coord ScalarType - const int DGT = vcg::tri::io::Precision::digits(); - const int DGTS = vcg::tri::io::Precision::digits(); - const int DGTVQ = vcg::tri::io::Precision::digits(); - const int DGTVR = vcg::tri::io::Precision::digits(); - const int DGTFQ = vcg::tri::io::Precision::digits(); - bool saveTexIndexFlag = false; + static int Save(const SaveMeshType &m, const char * filename, bool binary, const PlyInfo &pi, CallBackPos *cb=0) // V1.0 + { + FILE * fpout; + const char * hbin = "binary_little_endian"; + const char * hasc = "ascii"; + const char * h; + //Coord ScalarType + const int DGT = vcg::tri::io::Precision::digits(); + const int DGTS = vcg::tri::io::Precision::digits(); + const int DGTVQ = vcg::tri::io::Precision::digits(); + const int DGTVR = vcg::tri::io::Precision::digits(); + const int DGTFQ = vcg::tri::io::Precision::digits(); + bool saveTexIndexFlag = false; - if(binary) h=hbin; - else h=hasc; + if(binary) h=hbin; + else h=hasc; - fpout = fopen(filename,"wb"); - if(fpout==NULL) { - //pi.status=::vcg::ply::E_CANTOPEN; - return ::vcg::ply::E_CANTOPEN; - } - fprintf(fpout, - "ply\n" - "format %s 1.0\n" - "comment VCGLIB generated\n" - ,h - ); + fpout = fopen(filename,"wb"); + if(fpout==NULL) { + //pi.status=::vcg::ply::E_CANTOPEN; + return ::vcg::ply::E_CANTOPEN; + } + fprintf(fpout, + "ply\n" + "format %s 1.0\n" + "comment VCGLIB generated\n" , + h); - if (((pi.mask & Mask::IOM_WEDGTEXCOORD) != 0) || ((pi.mask & Mask::IOM_VERTTEXCOORD) != 0)) - { - const char * TFILE = "TextureFile"; + if (((pi.mask & Mask::IOM_WEDGTEXCOORD) != 0) || ((pi.mask & Mask::IOM_VERTTEXCOORD) != 0)) + { + const char * TFILE = "TextureFile"; - for(size_t i=0; i < m.textures.size(); ++i) - fprintf(fpout,"comment %s %s\n", TFILE, (const char *)(m.textures[i].c_str()) ); + for(size_t i=0; i < m.textures.size(); ++i) + fprintf(fpout,"comment %s %s\n", TFILE, (const char *)(m.textures[i].c_str()) ); - if(m.textures.size()>1 && (HasPerWedgeTexCoord(m) || HasPerVertexTexCoord(m))) saveTexIndexFlag = true; - } + if(m.textures.size()>1 && (HasPerWedgeTexCoord(m) || HasPerVertexTexCoord(m))) saveTexIndexFlag = true; + } - if((pi.mask & Mask::IOM_CAMERA)) - { - const char* cmtp = vcg::tri::io::Precision::typeName(); - fprintf(fpout,"element camera 1\n"); - fprintf(fpout,"property %s view_px\n",cmtp); - fprintf(fpout,"property %s view_py\n",cmtp); - fprintf(fpout,"property %s view_pz\n",cmtp); - fprintf(fpout,"property %s x_axisx\n",cmtp); - fprintf(fpout,"property %s x_axisy\n",cmtp); - fprintf(fpout,"property %s x_axisz\n",cmtp); - fprintf(fpout,"property %s y_axisx\n",cmtp); - fprintf(fpout,"property %s y_axisy\n",cmtp); - fprintf(fpout,"property %s y_axisz\n",cmtp); - fprintf(fpout,"property %s z_axisx\n",cmtp); - fprintf(fpout,"property %s z_axisy\n",cmtp); - fprintf(fpout,"property %s z_axisz\n",cmtp); - fprintf(fpout,"property %s focal\n",cmtp); - fprintf(fpout,"property %s scalex\n",cmtp); - fprintf(fpout,"property %s scaley\n",cmtp); - fprintf(fpout,"property %s centerx\n",cmtp); - fprintf(fpout,"property %s centery\n",cmtp); - fprintf(fpout,"property int viewportx\n"); - fprintf(fpout,"property int viewporty\n"); - fprintf(fpout,"property %s k1\n",cmtp); - fprintf(fpout,"property %s k2\n",cmtp); - fprintf(fpout,"property %s k3\n",cmtp); - fprintf(fpout,"property %s k4\n",cmtp); - } + if((pi.mask & Mask::IOM_CAMERA)) + { + const char* cmtp = vcg::tri::io::Precision::typeName(); + fprintf(fpout,"element camera 1\n"); + fprintf(fpout,"property %s view_px\n",cmtp); + fprintf(fpout,"property %s view_py\n",cmtp); + fprintf(fpout,"property %s view_pz\n",cmtp); + fprintf(fpout,"property %s x_axisx\n",cmtp); + fprintf(fpout,"property %s x_axisy\n",cmtp); + fprintf(fpout,"property %s x_axisz\n",cmtp); + fprintf(fpout,"property %s y_axisx\n",cmtp); + fprintf(fpout,"property %s y_axisy\n",cmtp); + fprintf(fpout,"property %s y_axisz\n",cmtp); + fprintf(fpout,"property %s z_axisx\n",cmtp); + fprintf(fpout,"property %s z_axisy\n",cmtp); + fprintf(fpout,"property %s z_axisz\n",cmtp); + fprintf(fpout,"property %s focal\n",cmtp); + fprintf(fpout,"property %s scalex\n",cmtp); + fprintf(fpout,"property %s scaley\n",cmtp); + fprintf(fpout,"property %s centerx\n",cmtp); + fprintf(fpout,"property %s centery\n",cmtp); + fprintf(fpout,"property int viewportx\n"); + fprintf(fpout,"property int viewporty\n"); + fprintf(fpout,"property %s k1\n",cmtp); + fprintf(fpout,"property %s k2\n",cmtp); + fprintf(fpout,"property %s k3\n",cmtp); + fprintf(fpout,"property %s k4\n",cmtp); + } - const char* vttp = vcg::tri::io::Precision::typeName(); - fprintf(fpout,"element vertex %d\n",m.vn); - fprintf(fpout,"property %s x\n",vttp); - fprintf(fpout,"property %s y\n",vttp); - fprintf(fpout,"property %s z\n",vttp); + const char* vttp = vcg::tri::io::Precision::typeName(); + fprintf(fpout,"element vertex %d\n",m.vn); + fprintf(fpout,"property %s x\n",vttp); + fprintf(fpout,"property %s y\n",vttp); + fprintf(fpout,"property %s z\n",vttp); - if( HasPerVertexNormal(m) &&( pi.mask & Mask::IOM_VERTNORMAL) ) - { - fprintf(fpout,"property %s nx\n",vttp); - fprintf(fpout,"property %s ny\n",vttp); - fprintf(fpout,"property %s nz\n",vttp); - } + if( HasPerVertexNormal(m) &&( pi.mask & Mask::IOM_VERTNORMAL) ) + { + fprintf(fpout,"property %s nx\n",vttp); + fprintf(fpout,"property %s ny\n",vttp); + fprintf(fpout,"property %s nz\n",vttp); + } - if( HasPerVertexFlags(m) &&( pi.mask & Mask::IOM_VERTFLAGS) ) - { - fprintf(fpout, - "property int flags\n" - ); - } + if( HasPerVertexFlags(m) &&( pi.mask & Mask::IOM_VERTFLAGS) ) + { + fprintf(fpout, + "property int flags\n"); + } - if( HasPerVertexColor(m) && (pi.mask & Mask::IOM_VERTCOLOR) ) - { - fprintf(fpout, - "property uchar red\n" - "property uchar green\n" - "property uchar blue\n" - "property uchar alpha\n" - ); - } + if( HasPerVertexColor(m) && (pi.mask & Mask::IOM_VERTCOLOR) ) + { + fprintf(fpout, + "property uchar red\n" + "property uchar green\n" + "property uchar blue\n" + "property uchar alpha\n"); + } - if( HasPerVertexQuality(m) && (pi.mask & Mask::IOM_VERTQUALITY) ) - { - const char* vqtp = vcg::tri::io::Precision::typeName(); - fprintf(fpout,"property %s quality\n",vqtp); - } + if( HasPerVertexQuality(m) && (pi.mask & Mask::IOM_VERTQUALITY) ) + { + const char* vqtp = vcg::tri::io::Precision::typeName(); + fprintf(fpout,"property %s quality\n",vqtp); + } - if( tri::HasPerVertexRadius(m) && (pi.mask & Mask::IOM_VERTRADIUS) ) - { - const char* rdtp = vcg::tri::io::Precision::typeName(); - fprintf(fpout,"property %s radius\n",rdtp); - } - if( ( HasPerVertexTexCoord(m) && pi.mask & Mask::IOM_VERTTEXCOORD ) ) - { - fprintf(fpout, - "property float texture_u\n" - "property float texture_v\n" - ); - } - for(size_t i=0;i::typeName(); + fprintf(fpout,"property %s radius\n",rdtp); + } + if( ( HasPerVertexTexCoord(m) && pi.mask & Mask::IOM_VERTTEXCOORD ) ) + { + fprintf(fpout, + "property float texture_u\n" + "property float texture_v\n"); + } + for(size_t i=0;i::typeName(); + fprintf(fpout, "property %s nx\n", fntp); + fprintf(fpout, "property %s ny\n", fntp); + fprintf(fpout, "property %s nz\n", fntp); + } + + if( HasPerFaceQuality(m) && (pi.mask & Mask::IOM_FACEQUALITY) ) + { + const char* fqtp = vcg::tri::io::Precision::typeName(); + fprintf(fpout,"property %s quality\n",fqtp); + } + + for(size_t i=0;i0 && (pi.mask & Mask::IOM_EDGEINDEX) ) + fprintf( + fpout, + "element edge %d\n" "property int vertex1\n""property int vertex2\n",m.en); + fprintf(fpout, "end_header\n" ); + + // Salvataggio camera + if((pi.mask & Mask::IOM_CAMERA)) + { + if(binary) + { + ShotScalarType t[17]; + + t[ 0] = (ShotScalarType)m.shot.Extrinsics.Tra()[0]; + t[ 1] = (ShotScalarType)m.shot.Extrinsics.Tra()[1]; + t[ 2] = (ShotScalarType)m.shot.Extrinsics.Tra()[2]; + t[ 3] = (ShotScalarType)m.shot.Extrinsics.Rot()[0][0]; + t[ 4] = (ShotScalarType)m.shot.Extrinsics.Rot()[0][1]; + t[ 5] = (ShotScalarType)m.shot.Extrinsics.Rot()[0][2]; + t[ 6] = (ShotScalarType)m.shot.Extrinsics.Rot()[1][0]; + t[ 7] = (ShotScalarType)m.shot.Extrinsics.Rot()[1][1]; + t[ 8] = (ShotScalarType)m.shot.Extrinsics.Rot()[1][2]; + t[ 9] = (ShotScalarType)m.shot.Extrinsics.Rot()[2][0]; + t[10] = (ShotScalarType)m.shot.Extrinsics.Rot()[2][1]; + t[11] = (ShotScalarType)m.shot.Extrinsics.Rot()[2][2]; + t[12] = (ShotScalarType)m.shot.Intrinsics.FocalMm; + t[13] = (ShotScalarType)m.shot.Intrinsics.PixelSizeMm[0]; + t[14] = (ShotScalarType)m.shot.Intrinsics.PixelSizeMm[1]; + t[15] = (ShotScalarType)m.shot.Intrinsics.CenterPx[0]; + t[16] = (ShotScalarType)m.shot.Intrinsics.CenterPx[1]; + fwrite(t,sizeof(ShotScalarType),17,fpout); + + fwrite( &m.shot.Intrinsics.ViewportPx[0],sizeof(int),2,fpout ); + + t[ 0] = (ShotScalarType)m.shot.Intrinsics.k[0]; + t[ 1] = (ShotScalarType)m.shot.Intrinsics.k[1]; + t[ 2] = (ShotScalarType)m.shot.Intrinsics.k[2]; + t[ 3] = (ShotScalarType)m.shot.Intrinsics.k[3]; + fwrite(t,sizeof(ShotScalarType),4,fpout); + } + else + { + 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" + ,DGTS,-m.shot.Extrinsics.Tra()[0] + ,DGTS,-m.shot.Extrinsics.Tra()[1] + ,DGTS,-m.shot.Extrinsics.Tra()[2] + ,DGTS,m.shot.Extrinsics.Rot()[0][0] + ,DGTS,m.shot.Extrinsics.Rot()[0][1] + ,DGTS,m.shot.Extrinsics.Rot()[0][2] + ,DGTS,m.shot.Extrinsics.Rot()[1][0] + ,DGTS,m.shot.Extrinsics.Rot()[1][1] + ,DGTS,m.shot.Extrinsics.Rot()[1][2] + ,DGTS,m.shot.Extrinsics.Rot()[2][0] + ,DGTS,m.shot.Extrinsics.Rot()[2][1] + ,DGTS,m.shot.Extrinsics.Rot()[2][2] + ,DGTS,m.shot.Intrinsics.FocalMm + ,DGTS,m.shot.Intrinsics.PixelSizeMm[0] + ,DGTS,m.shot.Intrinsics.PixelSizeMm[1] + ,DGTS,m.shot.Intrinsics.CenterPx[0] + ,DGTS,m.shot.Intrinsics.CenterPx[1] + ,m.shot.Intrinsics.ViewportPx[0] + ,m.shot.Intrinsics.ViewportPx[1] + ,DGTS,m.shot.Intrinsics.k[0] + ,DGTS,m.shot.Intrinsics.k[1] + ,DGTS,m.shot.Intrinsics.k[2] + ,DGTS,m.shot.Intrinsics.k[3] + ); + } + } + + + int j; + std::vector FlagV; + VertexPointer vp; + VertexIterator vi; + SimpleTempData indices(m.vert); + + std::vector > thfv(pi.VertDescriptorVec.size()); + std::vector > thdv(pi.VertDescriptorVec.size()); + std::vector > thiv(pi.VertDescriptorVec.size()); + std::vector > thsv(pi.VertDescriptorVec.size()); + std::vector > thcv(pi.VertDescriptorVec.size()); + std::vector > thuv(pi.VertDescriptorVec.size()); + std::vector > thp3fv(pi.VertDescriptorVec.size()); + std::vector > thp3dv(pi.VertDescriptorVec.size()); + + for(size_t i=0;i::typeName(); - fprintf(fpout, "property %s nx\n", fntp); - fprintf(fpout, "property %s ny\n", fntp); - fprintf(fpout, "property %s nz\n", fntp); + case ply::T_FLOAT : thfv[i] = vcg::tri::Allocator::template FindPerVertexAttribute(m,pi.VertAttrNameVec[i]); break; + case ply::T_DOUBLE : thdv[i] = vcg::tri::Allocator::template FindPerVertexAttribute(m,pi.VertAttrNameVec[i]); break; + case ply::T_INT : thiv[i] = vcg::tri::Allocator::template FindPerVertexAttribute(m,pi.VertAttrNameVec[i]); break; + case ply::T_SHORT : thsv[i] = vcg::tri::Allocator::template FindPerVertexAttribute(m,pi.VertAttrNameVec[i]); break; + case ply::T_CHAR : thcv[i] = vcg::tri::Allocator::template FindPerVertexAttribute(m,pi.VertAttrNameVec[i]); break; + case ply::T_UCHAR : thuv[i] = vcg::tri::Allocator::template FindPerVertexAttribute(m,pi.VertAttrNameVec[i]); break; + default : assert(0); + } + } + else { + switch (pi.VertDescriptorVec[i].stotype1) + { + case ply::T_FLOAT : thp3fv[i] = vcg::tri::Allocator::template FindPerVertexAttribute(m,pi.VertAttrNameVec[i]); break; + case ply::T_DOUBLE : thp3dv[i] = vcg::tri::Allocator::template FindPerVertexAttribute(m,pi.VertAttrNameVec[i]); break; + default : assert(0); + } + } + } + } + std::vector > thff(pi.FaceDescriptorVec.size()); + std::vector > thdf(pi.FaceDescriptorVec.size()); + std::vector > thif(pi.FaceDescriptorVec.size()); + std::vector > thsf(pi.FaceDescriptorVec.size()); + std::vector > thcf(pi.FaceDescriptorVec.size()); + std::vector > thuf(pi.FaceDescriptorVec.size()); + + for(size_t i=0;i::template FindPerFaceAttribute(m,pi.FaceAttrNameVec[i]); break; + case ply::T_DOUBLE : thdf[i] = vcg::tri::Allocator::template FindPerFaceAttribute(m,pi.FaceAttrNameVec[i]); break; + case ply::T_INT : thif[i] = vcg::tri::Allocator::template FindPerFaceAttribute(m,pi.FaceAttrNameVec[i]); break; + case ply::T_SHORT : thsf[i] = vcg::tri::Allocator::template FindPerFaceAttribute(m,pi.FaceAttrNameVec[i]); break; + case ply::T_CHAR : thcf[i] = vcg::tri::Allocator::template FindPerFaceAttribute(m,pi.FaceAttrNameVec[i]); break; + case ply::T_UCHAR : thuf[i] = vcg::tri::Allocator::template FindPerFaceAttribute(m,pi.FaceAttrNameVec[i]); break; + default : assert(0); + } + } + } + + for(j=0,vi=m.vert.begin();vi!=m.vert.end();++vi){ + vp=&(*vi); + indices[vi] = j; + //((m.vn+m.fn) != 0) all vertices and faces have been marked as deleted but the are still in the vert/face vectors + if(cb && ((j%1000)==0) && ((m.vn+m.fn) != 0) )(*cb)( (100*j)/(m.vn+m.fn), "Saving Vertices"); + + if( !HasPerVertexFlags(m) || !vp->IsD() ) + { + if(binary) + { + ScalarType t; + + t = ScalarType(vp->P()[0]); fwrite(&t,sizeof(ScalarType),1,fpout); + t = ScalarType(vp->P()[1]); fwrite(&t,sizeof(ScalarType),1,fpout); + t = ScalarType(vp->P()[2]); fwrite(&t,sizeof(ScalarType),1,fpout); + + if( HasPerVertexNormal(m) && (pi.mask & Mask::IOM_VERTNORMAL) ) + { + t = ScalarType(vp->N()[0]); fwrite(&t,sizeof(ScalarType),1,fpout); + t = ScalarType(vp->N()[1]); fwrite(&t,sizeof(ScalarType),1,fpout); + t = ScalarType(vp->N()[2]); fwrite(&t,sizeof(ScalarType),1,fpout); + } + if( HasPerVertexFlags(m) && (pi.mask & Mask::IOM_VERTFLAGS) ) + fwrite(&(vp->Flags()),sizeof(int),1,fpout); + + if( HasPerVertexColor(m) && (pi.mask & Mask::IOM_VERTCOLOR) ){ + auto c = vp->C(); + fwrite(&c,sizeof(char),4,fpout); } - if( HasPerFaceQuality(m) && (pi.mask & Mask::IOM_FACEQUALITY) ) - { - const char* fqtp = vcg::tri::io::Precision::typeName(); - fprintf(fpout,"property %s quality\n",fqtp); - } + if( HasPerVertexQuality(m) && (pi.mask & Mask::IOM_VERTQUALITY) ){ + auto q = vp->Q(); + fwrite(&q, sizeof(typename VertexType::QualityType),1,fpout); + } - for(size_t i=0;i0 && (pi.mask & Mask::IOM_EDGEINDEX) ) - fprintf(fpout, - "element edge %d\n" - "property int vertex1\n" - "property int vertex2\n" - ,m.en - ); - fprintf(fpout, "end_header\n" ); + if( HasPerVertexRadius(m) && (pi.mask & Mask::IOM_VERTRADIUS) ){ + auto r = vp->R(); + fwrite(&r,sizeof(typename VertexType::RadiusType),1,fpout); + } - // Salvataggio camera - if((pi.mask & Mask::IOM_CAMERA)) - { - if(binary) - { - ShotScalarType t[17]; + if( HasPerVertexTexCoord(m) && (pi.mask & Mask::IOM_VERTTEXCOORD) ) + { + t = ScalarType(vp->T().u()); fwrite(&t,sizeof(ScalarType),1,fpout); + t = ScalarType(vp->T().v()); fwrite(&t,sizeof(ScalarType),1,fpout); + } - t[ 0] = (ShotScalarType)m.shot.Extrinsics.Tra()[0]; - t[ 1] = (ShotScalarType)m.shot.Extrinsics.Tra()[1]; - t[ 2] = (ShotScalarType)m.shot.Extrinsics.Tra()[2]; - t[ 3] = (ShotScalarType)m.shot.Extrinsics.Rot()[0][0]; - t[ 4] = (ShotScalarType)m.shot.Extrinsics.Rot()[0][1]; - t[ 5] = (ShotScalarType)m.shot.Extrinsics.Rot()[0][2]; - t[ 6] = (ShotScalarType)m.shot.Extrinsics.Rot()[1][0]; - t[ 7] = (ShotScalarType)m.shot.Extrinsics.Rot()[1][1]; - t[ 8] = (ShotScalarType)m.shot.Extrinsics.Rot()[1][2]; - t[ 9] = (ShotScalarType)m.shot.Extrinsics.Rot()[2][0]; - t[10] = (ShotScalarType)m.shot.Extrinsics.Rot()[2][1]; - t[11] = (ShotScalarType)m.shot.Extrinsics.Rot()[2][2]; - t[12] = (ShotScalarType)m.shot.Intrinsics.FocalMm; - t[13] = (ShotScalarType)m.shot.Intrinsics.PixelSizeMm[0]; - t[14] = (ShotScalarType)m.shot.Intrinsics.PixelSizeMm[1]; - t[15] = (ShotScalarType)m.shot.Intrinsics.CenterPx[0]; - t[16] = (ShotScalarType)m.shot.Intrinsics.CenterPx[1]; - fwrite(t,sizeof(ShotScalarType),17,fpout); - - fwrite( &m.shot.Intrinsics.ViewportPx[0],sizeof(int),2,fpout ); - - t[ 0] = (ShotScalarType)m.shot.Intrinsics.k[0]; - t[ 1] = (ShotScalarType)m.shot.Intrinsics.k[1]; - t[ 2] = (ShotScalarType)m.shot.Intrinsics.k[2]; - t[ 3] = (ShotScalarType)m.shot.Intrinsics.k[3]; - fwrite(t,sizeof(ShotScalarType),4,fpout); - } - else - { - 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" - ,DGTS,-m.shot.Extrinsics.Tra()[0] - ,DGTS,-m.shot.Extrinsics.Tra()[1] - ,DGTS,-m.shot.Extrinsics.Tra()[2] - ,DGTS,m.shot.Extrinsics.Rot()[0][0] - ,DGTS,m.shot.Extrinsics.Rot()[0][1] - ,DGTS,m.shot.Extrinsics.Rot()[0][2] - ,DGTS,m.shot.Extrinsics.Rot()[1][0] - ,DGTS,m.shot.Extrinsics.Rot()[1][1] - ,DGTS,m.shot.Extrinsics.Rot()[1][2] - ,DGTS,m.shot.Extrinsics.Rot()[2][0] - ,DGTS,m.shot.Extrinsics.Rot()[2][1] - ,DGTS,m.shot.Extrinsics.Rot()[2][2] - ,DGTS,m.shot.Intrinsics.FocalMm - ,DGTS,m.shot.Intrinsics.PixelSizeMm[0] - ,DGTS,m.shot.Intrinsics.PixelSizeMm[1] - ,DGTS,m.shot.Intrinsics.CenterPx[0] - ,DGTS,m.shot.Intrinsics.CenterPx[1] - ,m.shot.Intrinsics.ViewportPx[0] - ,m.shot.Intrinsics.ViewportPx[1] - ,DGTS,m.shot.Intrinsics.k[0] - ,DGTS,m.shot.Intrinsics.k[1] - ,DGTS,m.shot.Intrinsics.k[2] - ,DGTS,m.shot.Intrinsics.k[3] - ); - } - } - - - int j; - std::vector FlagV; - VertexPointer vp; - VertexIterator vi; - SimpleTempData indices(m.vert); - - std::vector > thfv(pi.VertDescriptorVec.size()); - std::vector > thdv(pi.VertDescriptorVec.size()); - std::vector > thiv(pi.VertDescriptorVec.size()); - std::vector > thsv(pi.VertDescriptorVec.size()); - std::vector > thcv(pi.VertDescriptorVec.size()); - std::vector > thuv(pi.VertDescriptorVec.size()); - - for(size_t i=0;i::template FindPerVertexAttribute(m,pi.VertAttrNameVec[i]); break; - case ply::T_DOUBLE : thdv[i] = vcg::tri::Allocator::template FindPerVertexAttribute(m,pi.VertAttrNameVec[i]); break; - case ply::T_INT : thiv[i] = vcg::tri::Allocator::template FindPerVertexAttribute(m,pi.VertAttrNameVec[i]); break; - case ply::T_SHORT : thsv[i] = vcg::tri::Allocator::template FindPerVertexAttribute(m,pi.VertAttrNameVec[i]); break; - case ply::T_CHAR : thcv[i] = vcg::tri::Allocator::template FindPerVertexAttribute(m,pi.VertAttrNameVec[i]); break; - case ply::T_UCHAR : thuv[i] = vcg::tri::Allocator::template FindPerVertexAttribute(m,pi.VertAttrNameVec[i]); break; - default : assert(0); - } - } - } - std::vector > thff(pi.FaceDescriptorVec.size()); - std::vector > thdf(pi.FaceDescriptorVec.size()); - std::vector > thif(pi.FaceDescriptorVec.size()); - std::vector > thsf(pi.FaceDescriptorVec.size()); - std::vector > thcf(pi.FaceDescriptorVec.size()); - std::vector > thuf(pi.FaceDescriptorVec.size()); - - for(size_t i=0;i::template FindPerFaceAttribute(m,pi.FaceAttrNameVec[i]); break; - case ply::T_DOUBLE : thdf[i] = vcg::tri::Allocator::template FindPerFaceAttribute(m,pi.FaceAttrNameVec[i]); break; - case ply::T_INT : thif[i] = vcg::tri::Allocator::template FindPerFaceAttribute(m,pi.FaceAttrNameVec[i]); break; - case ply::T_SHORT : thsf[i] = vcg::tri::Allocator::template FindPerFaceAttribute(m,pi.FaceAttrNameVec[i]); break; - case ply::T_CHAR : thcf[i] = vcg::tri::Allocator::template FindPerFaceAttribute(m,pi.FaceAttrNameVec[i]); break; - case ply::T_UCHAR : thuf[i] = vcg::tri::Allocator::template FindPerFaceAttribute(m,pi.FaceAttrNameVec[i]); break; - default : assert(0); - } - } - } - - - - - for(j=0,vi=m.vert.begin();vi!=m.vert.end();++vi){ - vp=&(*vi); - indices[vi] = j; - //((m.vn+m.fn) != 0) all vertices and faces have been marked as deleted but the are still in the vert/face vectors - if(cb && ((j%1000)==0) && ((m.vn+m.fn) != 0) )(*cb)( (100*j)/(m.vn+m.fn), "Saving Vertices"); - - if( !HasPerVertexFlags(m) || !vp->IsD() ) - { - if(binary) - { - ScalarType t; - - t = ScalarType(vp->P()[0]); fwrite(&t,sizeof(ScalarType),1,fpout); - t = ScalarType(vp->P()[1]); fwrite(&t,sizeof(ScalarType),1,fpout); - t = ScalarType(vp->P()[2]); fwrite(&t,sizeof(ScalarType),1,fpout); - - if( HasPerVertexNormal(m) && (pi.mask & Mask::IOM_VERTNORMAL) ) - { - t = ScalarType(vp->N()[0]); fwrite(&t,sizeof(ScalarType),1,fpout); - t = ScalarType(vp->N()[1]); fwrite(&t,sizeof(ScalarType),1,fpout); - t = ScalarType(vp->N()[2]); fwrite(&t,sizeof(ScalarType),1,fpout); - } - if( HasPerVertexFlags(m) && (pi.mask & Mask::IOM_VERTFLAGS) ) - fwrite(&(vp->Flags()),sizeof(int),1,fpout); - - if( HasPerVertexColor(m) && (pi.mask & Mask::IOM_VERTCOLOR) ){ - auto c = vp->C(); - fwrite(&c,sizeof(char),4,fpout); - } - - if( HasPerVertexQuality(m) && (pi.mask & Mask::IOM_VERTQUALITY) ){ - auto q = vp->Q(); - fwrite(&q, sizeof(typename VertexType::QualityType),1,fpout); - } - - if( HasPerVertexRadius(m) && (pi.mask & Mask::IOM_VERTRADIUS) ){ - auto r = vp->R(); - fwrite(&r,sizeof(typename VertexType::RadiusType),1,fpout); - } - - if( HasPerVertexTexCoord(m) && (pi.mask & Mask::IOM_VERTTEXCOORD) ) - { - t = ScalarType(vp->T().u()); fwrite(&t,sizeof(ScalarType),1,fpout); - t = ScalarType(vp->T().v()); fwrite(&t,sizeof(ScalarType),1,fpout); - } - - for(size_t i=0;iP()[0],DGT,vp->P()[1],DGT,vp->P()[2]); - - if( HasPerVertexNormal(m) && (pi.mask & Mask::IOM_VERTNORMAL) ) - fprintf(fpout,"%.*g %.*g %.*g " ,DGT,ScalarType(vp->N()[0]),DGT,ScalarType(vp->N()[1]),DGT,ScalarType(vp->N()[2])); - - if( HasPerVertexFlags(m) && (pi.mask & Mask::IOM_VERTFLAGS)) - fprintf(fpout,"%d ",vp->Flags()); - - if( HasPerVertexColor(m) && (pi.mask & Mask::IOM_VERTCOLOR) ) - fprintf(fpout,"%d %d %d %d ",vp->C()[0],vp->C()[1],vp->C()[2],vp->C()[3] ); - - if( HasPerVertexQuality(m) && (pi.mask & Mask::IOM_VERTQUALITY) ) - fprintf(fpout,"%.*g ",DGTVQ,vp->Q()); - - if( HasPerVertexRadius(m) && (pi.mask & Mask::IOM_VERTRADIUS) ) - fprintf(fpout,"%.*g ",DGTVR,vp->R()); - - if( HasPerVertexTexCoord(m) && (pi.mask & Mask::IOM_VERTTEXCOORD) ) - fprintf(fpout,"%f %f",vp->T().u(),vp->T().v()); - - for(size_t i=0;iIsD() ) - { fcnt++; - if(binary) - { - vv[0]=indices[fp->cV(0)]; - vv[1]=indices[fp->cV(1)]; - vv[2]=indices[fp->cV(2)]; - fwrite(&b3char,sizeof(char),1,fpout); - fwrite(vv,sizeof(int),3,fpout); - - if(HasPerFaceFlags(m)&&( pi.mask & Mask::IOM_FACEFLAGS) ){ - auto fl = fp->Flags(); - fwrite(&fl,sizeof(int),1,fpout); - } - - if( HasPerVertexTexCoord(m) && (!HasPerWedgeTexCoord(m)) && (pi.mask & Mask::IOM_WEDGTEXCOORD) ) // Note that you can save VT as WT if you really want it... - { - fwrite(&b6char,sizeof(char),1,fpout); - float t[6]; - for(int k=0;k<3;++k) - { - t[k*2+0] = fp->V(k)->T().u(); - t[k*2+1] = fp->V(k)->T().v(); - } - fwrite(t,sizeof(float),6,fpout); - } - else if( HasPerWedgeTexCoord(m) && (pi.mask & Mask::IOM_WEDGTEXCOORD) ) - { - fwrite(&b6char,sizeof(char),1,fpout); - float t[6]; - for(int k=0;k<3;++k) - { - t[k*2+0] = fp->WT(k).u(); - t[k*2+1] = fp->WT(k).v(); - } - fwrite(t,sizeof(float),6,fpout); - } - - if(saveTexIndexFlag) - { - int t = fp->WT(0).n(); - fwrite(&t,sizeof(int),1,fpout); - } - - if( HasPerFaceColor(m) && (pi.mask & Mask::IOM_FACECOLOR) ) - fwrite(&( fp->C() ),sizeof(char),4,fpout); - - - if( HasPerWedgeColor(m) && (pi.mask & Mask::IOM_WEDGCOLOR) ) - { - fwrite(&b9char,sizeof(char),1,fpout); - float t[3]; - for(int z=0;z<3;++z) - { - t[0] = float(fp->WC(z)[0])/255; - t[1] = float(fp->WC(z)[1])/255; - t[2] = float(fp->WC(z)[2])/255; - fwrite( t,sizeof(float),3,fpout); - } - } - - if( HasPerFaceNormal(m) && (pi.mask & Mask::IOM_FACENORMAL) ) - { - ScalarType t; - t = ScalarType(fp->N()[0]); fwrite(&t,sizeof(ScalarType),1,fpout); - t = ScalarType(fp->N()[1]); fwrite(&t,sizeof(ScalarType),1,fpout); - t = ScalarType(fp->N()[2]); fwrite(&t,sizeof(ScalarType),1,fpout); + for(size_t i=0;iP()[0],DGT,vp->P()[1],DGT,vp->P()[2]); - if( HasPerFaceQuality(m) && (pi.mask & Mask::IOM_FACEQUALITY) ) - fwrite( &(fp->Q()),sizeof(typename FaceType::ScalarType),1,fpout); + if( HasPerVertexNormal(m) && (pi.mask & Mask::IOM_VERTNORMAL) ) + fprintf(fpout,"%.*g %.*g %.*g " ,DGT,ScalarType(vp->N()[0]),DGT,ScalarType(vp->N()[1]),DGT,ScalarType(vp->N()[2])); + + if( HasPerVertexFlags(m) && (pi.mask & Mask::IOM_VERTFLAGS)) + fprintf(fpout,"%d ",vp->Flags()); + + if( HasPerVertexColor(m) && (pi.mask & Mask::IOM_VERTCOLOR) ) + fprintf(fpout,"%d %d %d %d ",vp->C()[0],vp->C()[1],vp->C()[2],vp->C()[3] ); + + if( HasPerVertexQuality(m) && (pi.mask & Mask::IOM_VERTQUALITY) ) + fprintf(fpout,"%.*g ",DGTVQ,vp->Q()); + + if( HasPerVertexRadius(m) && (pi.mask & Mask::IOM_VERTRADIUS) ) + fprintf(fpout,"%.*g ",DGTVR,vp->R()); + + if( HasPerVertexTexCoord(m) && (pi.mask & Mask::IOM_VERTTEXCOORD) ) + fprintf(fpout,"%f %f",vp->T().u(),vp->T().v()); + + for(size_t i=0;iIsD() ) + { fcnt++; + if(binary) + { + vv[0]=indices[fp->cV(0)]; + vv[1]=indices[fp->cV(1)]; + vv[2]=indices[fp->cV(2)]; + fwrite(&b3char,sizeof(char),1,fpout); + fwrite(vv,sizeof(int),3,fpout); + + if(HasPerFaceFlags(m)&&( pi.mask & Mask::IOM_FACEFLAGS) ){ + auto fl = fp->Flags(); + fwrite(&fl,sizeof(int),1,fpout); + } + + if( HasPerVertexTexCoord(m) && (!HasPerWedgeTexCoord(m)) && (pi.mask & Mask::IOM_WEDGTEXCOORD) ) // Note that you can save VT as WT if you really want it... + { + fwrite(&b6char,sizeof(char),1,fpout); + float t[6]; + for(int k=0;k<3;++k) + { + t[k*2+0] = fp->V(k)->T().u(); + t[k*2+1] = fp->V(k)->T().v(); + } + fwrite(t,sizeof(float),6,fpout); + } + else if( HasPerWedgeTexCoord(m) && (pi.mask & Mask::IOM_WEDGTEXCOORD) ) + { + fwrite(&b6char,sizeof(char),1,fpout); + float t[6]; + for(int k=0;k<3;++k) + { + t[k*2+0] = fp->WT(k).u(); + t[k*2+1] = fp->WT(k).v(); + } + fwrite(t,sizeof(float),6,fpout); + } + + if(saveTexIndexFlag) + { + int t = fp->WT(0).n(); + fwrite(&t,sizeof(int),1,fpout); + } + + if( HasPerFaceColor(m) && (pi.mask & Mask::IOM_FACECOLOR) ) + fwrite(&( fp->C() ),sizeof(char),4,fpout); - for(size_t i=0;iVN()); - for(int k=0;kVN();++k) - fprintf(fpout,"%d ",indices[fp->cV(k)]); + if( HasPerWedgeColor(m) && (pi.mask & Mask::IOM_WEDGCOLOR) ) + { + fwrite(&b9char,sizeof(char),1,fpout); + float t[3]; + for(int z=0;z<3;++z) + { + t[0] = float(fp->WC(z)[0])/255; + t[1] = float(fp->WC(z)[1])/255; + t[2] = float(fp->WC(z)[2])/255; + fwrite( t,sizeof(float),3,fpout); + } + } - if(HasPerFaceFlags(m)&&( pi.mask & Mask::IOM_FACEFLAGS )) - fprintf(fpout,"%d ",fp->Flags()); + if( HasPerFaceNormal(m) && (pi.mask & Mask::IOM_FACENORMAL) ) + { + ScalarType t; + t = ScalarType(fp->N()[0]); fwrite(&t,sizeof(ScalarType),1,fpout); + t = ScalarType(fp->N()[1]); fwrite(&t,sizeof(ScalarType),1,fpout); + t = ScalarType(fp->N()[2]); fwrite(&t,sizeof(ScalarType),1,fpout); + } - if( HasPerVertexTexCoord(m) && (pi.mask & Mask::IOM_WEDGTEXCOORD) ) // you can save VT as WT if you really want it... - { - fprintf(fpout,"%d ",fp->VN()*2); - for(int k=0;kVN();++k) - fprintf(fpout,"%f %f " - ,fp->V(k)->T().u() - ,fp->V(k)->T().v() - ); - } - else if( HasPerWedgeTexCoord(m) && (pi.mask & Mask::IOM_WEDGTEXCOORD) ) - { - fprintf(fpout,"%d ",fp->VN()*2); - for(int k=0;kVN();++k) - fprintf(fpout,"%f %f " - ,fp->WT(k).u() - ,fp->WT(k).v() - ); - } - - if(saveTexIndexFlag) - { - fprintf(fpout,"%d ",fp->WT(0).n()); - } - - if( HasPerFaceColor(m) && (pi.mask & Mask::IOM_FACECOLOR) ) - { - fprintf(fpout, "%u %u %u %u ", fp->C()[0], fp->C()[1], fp->C()[2], fp->C()[3]); - } - else if( HasPerWedgeColor(m) && (pi.mask & Mask::IOM_WEDGCOLOR) ) - { - fprintf(fpout,"9 "); - for(int z=0;z<3;++z) - fprintf(fpout,"%g %g %g " - ,double(fp->WC(z)[0])/255 - ,double(fp->WC(z)[1])/255 - ,double(fp->WC(z)[2])/255 - ); - } - - if (HasPerFaceNormal(m) && (pi.mask & Mask::IOM_FACENORMAL)) - fprintf(fpout,"%.*g %.*g %.*g " ,DGT, ScalarType(fp->N()[0]),DGT,ScalarType(fp->N()[1]),DGT,ScalarType(fp->N()[2])); - - if( HasPerFaceQuality(m) && (pi.mask & Mask::IOM_FACEQUALITY) ) - fprintf(fpout,"%.*g ",DGTFQ,fp->Q()); - - for(size_t i=0;iIsD() ) - { - ++ecnt; - if(binary) - { - eauxvv[0]=indices[ei->cV(0)]; - eauxvv[1]=indices[ei->cV(1)]; - fwrite(eauxvv,sizeof(int),2,fpout); - } - else // ***** ASCII ***** - fprintf(fpout,"%d %d \n", indices[ei->cV(0)], indices[ei->cV(1)]); - } - } - assert(ecnt==m.en); - } - int result = 0; - if (ferror(fpout)) result = ply::E_STREAMERROR; - fclose(fpout); - return result; - } - - static const char *ErrorMsg(int error) - { - static std::vector ply_error_msg; - if(ply_error_msg.empty()) - { - ply_error_msg.resize(PlyInfo::E_MAXPLYINFOERRORS ); - ply_error_msg[ply::E_NOERROR ]="No errors"; - ply_error_msg[ply::E_CANTOPEN ]="Can't open file"; - ply_error_msg[ply::E_NOTHEADER ]="Header not found"; - ply_error_msg[ply::E_UNESPECTEDEOF ]="Eof in header"; - ply_error_msg[ply::E_NOFORMAT ]="Format not found"; - ply_error_msg[ply::E_SYNTAX ]="Syntax error on header"; - ply_error_msg[ply::E_PROPOUTOFELEMENT ]="Property without element"; - ply_error_msg[ply::E_BADTYPENAME ]="Bad type name"; - ply_error_msg[ply::E_ELEMNOTFOUND ]="Element not found"; - ply_error_msg[ply::E_PROPNOTFOUND ]="Property not found"; - ply_error_msg[ply::E_BADTYPE ]="Bad type on addtoread"; - ply_error_msg[ply::E_INCOMPATIBLETYPE ]="Incompatible type"; - ply_error_msg[ply::E_BADCAST ]="Bad cast"; - - ply_error_msg[ply::E_STREAMERROR ] = "Output Stream Error"; - - ply_error_msg[PlyInfo::E_NO_VERTEX ]="No vertex field found"; - ply_error_msg[PlyInfo::E_NO_FACE ]="No face field found"; - ply_error_msg[PlyInfo::E_SHORTFILE ]="Unespected eof"; - ply_error_msg[PlyInfo::E_NO_3VERTINFACE ]="Face with more than 3 vertices"; - ply_error_msg[PlyInfo::E_BAD_VERT_INDEX ]="Bad vertex index in face"; - ply_error_msg[PlyInfo::E_NO_6TCOORD ]="Face with no 6 texture coordinates"; - ply_error_msg[PlyInfo::E_DIFFER_COLORS ]="Number of color differ from vertices"; - } - - if(error>PlyInfo::E_MAXPLYINFOERRORS || error<0) return "Unknown error"; - else return ply_error_msg[error].c_str(); - }; - - static int GetExportMaskCapability() - { - int capability = 0; - capability |= vcg::tri::io::Mask::IOM_VERTCOORD ; - capability |= vcg::tri::io::Mask::IOM_VERTFLAGS ; - capability |= vcg::tri::io::Mask::IOM_VERTCOLOR ; - capability |= vcg::tri::io::Mask::IOM_VERTQUALITY ; - capability |= vcg::tri::io::Mask::IOM_VERTNORMAL ; - capability |= vcg::tri::io::Mask::IOM_VERTRADIUS ; - capability |= vcg::tri::io::Mask::IOM_VERTTEXCOORD ; - capability |= vcg::tri::io::Mask::IOM_FACEINDEX ; - capability |= vcg::tri::io::Mask::IOM_FACEFLAGS ; - capability |= vcg::tri::io::Mask::IOM_FACECOLOR ; - capability |= vcg::tri::io::Mask::IOM_FACEQUALITY ; - // capability |= vcg::tri::io::Mask::IOM_FACENORMAL ; - capability |= vcg::tri::io::Mask::IOM_WEDGCOLOR ; - capability |= vcg::tri::io::Mask::IOM_WEDGTEXCOORD ; - capability |= vcg::tri::io::Mask::IOM_WEDGTEXMULTI ; - capability |= vcg::tri::io::Mask::IOM_WEDGNORMAL ; - capability |= vcg::tri::io::Mask::IOM_CAMERA ; - //capability |= vcg::tri::io::Mask::IOM_BITPOLYGONAL; - return capability; - } + if( HasPerFaceQuality(m) && (pi.mask & Mask::IOM_FACEQUALITY) ) + fwrite( &(fp->Q()),sizeof(typename FaceType::ScalarType),1,fpout); - }; // end class + for(size_t i=0;iVN()); + for(int k=0;kVN();++k) + fprintf(fpout,"%d ",indices[fp->cV(k)]); + + if(HasPerFaceFlags(m)&&( pi.mask & Mask::IOM_FACEFLAGS )) + fprintf(fpout,"%d ",fp->Flags()); + + if( HasPerVertexTexCoord(m) && (pi.mask & Mask::IOM_WEDGTEXCOORD) ) // you can save VT as WT if you really want it... + { + fprintf(fpout,"%d ",fp->VN()*2); + for(int k=0;kVN();++k) + fprintf(fpout,"%f %f " + ,fp->V(k)->T().u() + ,fp->V(k)->T().v() + ); + } + else if( HasPerWedgeTexCoord(m) && (pi.mask & Mask::IOM_WEDGTEXCOORD) ) + { + fprintf(fpout,"%d ",fp->VN()*2); + for(int k=0;kVN();++k) + fprintf(fpout,"%f %f " + ,fp->WT(k).u() + ,fp->WT(k).v() + ); + } + + if(saveTexIndexFlag) + { + fprintf(fpout,"%d ",fp->WT(0).n()); + } + + if( HasPerFaceColor(m) && (pi.mask & Mask::IOM_FACECOLOR) ) + { + fprintf(fpout, "%u %u %u %u ", fp->C()[0], fp->C()[1], fp->C()[2], fp->C()[3]); + } + else if( HasPerWedgeColor(m) && (pi.mask & Mask::IOM_WEDGCOLOR) ) + { + fprintf(fpout,"9 "); + for(int z=0;z<3;++z) + fprintf(fpout,"%g %g %g " + ,double(fp->WC(z)[0])/255 + ,double(fp->WC(z)[1])/255 + ,double(fp->WC(z)[2])/255 + ); + } + + if (HasPerFaceNormal(m) && (pi.mask & Mask::IOM_FACENORMAL)) + fprintf(fpout,"%.*g %.*g %.*g " ,DGT, ScalarType(fp->N()[0]),DGT,ScalarType(fp->N()[1]),DGT,ScalarType(fp->N()[2])); + + if( HasPerFaceQuality(m) && (pi.mask & Mask::IOM_FACEQUALITY) ) + fprintf(fpout,"%.*g ",DGTFQ,fp->Q()); + + for(size_t i=0;iIsD() ) + { + ++ecnt; + if(binary) + { + eauxvv[0]=indices[ei->cV(0)]; + eauxvv[1]=indices[ei->cV(1)]; + fwrite(eauxvv,sizeof(int),2,fpout); + } + else // ***** ASCII ***** + fprintf(fpout,"%d %d \n", indices[ei->cV(0)], indices[ei->cV(1)]); + } + } + assert(ecnt==m.en); + } + int result = 0; + if (ferror(fpout)) result = ply::E_STREAMERROR; + fclose(fpout); + return result; + } + + static const char *ErrorMsg(int error) + { + static std::vector ply_error_msg; + if(ply_error_msg.empty()) + { + ply_error_msg.resize(PlyInfo::E_MAXPLYINFOERRORS ); + ply_error_msg[ply::E_NOERROR ]="No errors"; + ply_error_msg[ply::E_CANTOPEN ]="Can't open file"; + ply_error_msg[ply::E_NOTHEADER ]="Header not found"; + ply_error_msg[ply::E_UNESPECTEDEOF ]="Eof in header"; + ply_error_msg[ply::E_NOFORMAT ]="Format not found"; + ply_error_msg[ply::E_SYNTAX ]="Syntax error on header"; + ply_error_msg[ply::E_PROPOUTOFELEMENT ]="Property without element"; + ply_error_msg[ply::E_BADTYPENAME ]="Bad type name"; + ply_error_msg[ply::E_ELEMNOTFOUND ]="Element not found"; + ply_error_msg[ply::E_PROPNOTFOUND ]="Property not found"; + ply_error_msg[ply::E_BADTYPE ]="Bad type on addtoread"; + ply_error_msg[ply::E_INCOMPATIBLETYPE ]="Incompatible type"; + ply_error_msg[ply::E_BADCAST ]="Bad cast"; + + ply_error_msg[ply::E_STREAMERROR ] = "Output Stream Error"; + + ply_error_msg[PlyInfo::E_NO_VERTEX ]="No vertex field found"; + ply_error_msg[PlyInfo::E_NO_FACE ]="No face field found"; + ply_error_msg[PlyInfo::E_SHORTFILE ]="Unespected eof"; + ply_error_msg[PlyInfo::E_NO_3VERTINFACE ]="Face with more than 3 vertices"; + ply_error_msg[PlyInfo::E_BAD_VERT_INDEX ]="Bad vertex index in face"; + ply_error_msg[PlyInfo::E_NO_6TCOORD ]="Face with no 6 texture coordinates"; + ply_error_msg[PlyInfo::E_DIFFER_COLORS ]="Number of color differ from vertices"; + } + + if(error>PlyInfo::E_MAXPLYINFOERRORS || error<0) return "Unknown error"; + else return ply_error_msg[error].c_str(); + }; + + static int GetExportMaskCapability() + { + int capability = 0; + capability |= vcg::tri::io::Mask::IOM_VERTCOORD ; + capability |= vcg::tri::io::Mask::IOM_VERTFLAGS ; + capability |= vcg::tri::io::Mask::IOM_VERTCOLOR ; + capability |= vcg::tri::io::Mask::IOM_VERTQUALITY ; + capability |= vcg::tri::io::Mask::IOM_VERTNORMAL ; + capability |= vcg::tri::io::Mask::IOM_VERTRADIUS ; + capability |= vcg::tri::io::Mask::IOM_VERTTEXCOORD ; + capability |= vcg::tri::io::Mask::IOM_FACEINDEX ; + capability |= vcg::tri::io::Mask::IOM_FACEFLAGS ; + capability |= vcg::tri::io::Mask::IOM_FACECOLOR ; + capability |= vcg::tri::io::Mask::IOM_FACEQUALITY ; + // capability |= vcg::tri::io::Mask::IOM_FACENORMAL ; + capability |= vcg::tri::io::Mask::IOM_WEDGCOLOR ; + capability |= vcg::tri::io::Mask::IOM_WEDGTEXCOORD ; + capability |= vcg::tri::io::Mask::IOM_WEDGTEXMULTI ; + capability |= vcg::tri::io::Mask::IOM_WEDGNORMAL ; + capability |= vcg::tri::io::Mask::IOM_CAMERA ; + //capability |= vcg::tri::io::Mask::IOM_BITPOLYGONAL; + return capability; + } + + +}; // end class - } // end namespace tri - } // end namespace io +} // end namespace tri +} // end namespace io } // end namespace vcg //@} #endif diff --git a/wrap/io_trimesh/io_ply.h b/wrap/io_trimesh/io_ply.h index 39144305..47b8754c 100644 --- a/wrap/io_trimesh/io_ply.h +++ b/wrap/io_trimesh/io_ply.h @@ -100,22 +100,66 @@ public: addPerElemScalarAttribute(elemType, vcg::ply::T_DOUBLE, attrName, propName); } - void addPerVertexScalarAttribute(const std::string& attrName, vcg::ply::PlyTypes attrType, std::string propName="") { + void addPerVertexScalarAttribute(const std::string& attrName, vcg::ply::PlyTypes attrType, std::string propName="") + { addPerElemScalarAttribute(0,attrType, attrName,propName); } - void AddPerVertexFloatAttribute(const std::string& attrName, std::string propName="") { + void AddPerVertexFloatAttribute(const std::string& attrName, std::string propName="") + { AddPerElemFloatAttribute(0,attrName,propName); } - void addPerFaceScalarAttribute(const std::string& attrName, vcg::ply::PlyTypes attrType, std::string propName="") { + void addPerFaceScalarAttribute(const std::string& attrName, vcg::ply::PlyTypes attrType, std::string propName="") + { addPerElemScalarAttribute(1,attrType, attrName,propName); } - void AddPerFaceFloatAttribute(const std::string& attrName, std::string propName="") { + void AddPerFaceFloatAttribute(const std::string& attrName, std::string propName="") + { AddPerElemFloatAttribute(1,attrName,propName); } + void addPerElemPointAttribute(int elemType, vcg::ply::PlyTypes propertyType, const std::string& attrName, std::string propName="") + { + if(propName=="") + propName=attrName; + + PropDescriptor p; + p.propname=propName; + p.stotype1 = propertyType; + p.memtype1 = propertyType; + p.islist = true; + p.stotype2 = vcg::ply::PlyTypes::T_UINT; + p.stotype2 = vcg::ply::PlyTypes::T_UINT; + + if (elemType == 0){ //vertex + VertAttrNameVec.push_back(attrName); + p.elemname="vertex"; + VertDescriptorVec.push_back(p); + } + else if (elemType == 1){ //face + FaceAttrNameVec.push_back(attrName); + p.elemname="face"; + FaceDescriptorVec.push_back(p); + } + } + + void addPerVertexPoint3mAttribute(const std::string& attrName, vcg::ply::PlyTypes attrType, std::string propName="") + { + addPerElemPointAttribute(0,attrType, attrName,propName); + } + + void addPerVertexPoint3fAttribute(const std::string& attrName, std::string propName="") + { + addPerElemPointAttribute(0,vcg::ply::PlyTypes::T_FLOAT, attrName,propName); + } + + void addPerVertexPoint3dAttribute(const std::string& attrName, std::string propName="") + { + addPerElemPointAttribute(0,vcg::ply::PlyTypes::T_DOUBLE, attrName,propName); + } + /* Note that saving a per vertex point3 attribute is a mess. * Actually require to allocate 3 float attribute and save them. And they are never deallocated... */ template diff --git a/wrap/ply/plylib.cpp b/wrap/ply/plylib.cpp index 8530349f..c4657fd6 100644 --- a/wrap/ply/plylib.cpp +++ b/wrap/ply/plylib.cpp @@ -143,8 +143,12 @@ static int TypeSize[] = { size_t PropDescriptor::memtypesize() const {return TypeSize[memtype1];} size_t PropDescriptor::stotypesize() const {return TypeSize[stotype1];} +size_t PropDescriptor::memtype2size() const {return TypeSize[memtype2];} +size_t PropDescriptor::stotype2size() const {return TypeSize[stotype2];} const char *PropDescriptor::memtypename() const {return PlyFile::typenames[memtype1];} const char *PropDescriptor::stotypename() const {return PlyFile::typenames[stotype1];} +const char *PropDescriptor::memtype2name() const {return PlyFile::typenames[memtype2];} +const char *PropDescriptor::stotype2name() const {return PlyFile::typenames[stotype2];} static char CrossType[9][9]= { diff --git a/wrap/ply/plylib.h b/wrap/ply/plylib.h index 88470b20..36fa757e 100644 --- a/wrap/ply/plylib.h +++ b/wrap/ply/plylib.h @@ -133,6 +133,10 @@ public: size_t memtypesize() const; // per sapere quanto e'grande un dato descrittore in memoria const char* memtypename() const; const char* stotypename() const; + size_t stotype2size() const; // per sapere quanto e'grande un dato descrittore sul file + size_t memtype2size() const; // per sapere quanto e'grande un dato descrittore in memoria + const char* memtype2name() const; + const char* stotype2name() const; }; // Reading Callback (used to copy a data prop)