export_off.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 * VCGLib                                                            o o     *
00003 * Visual and Computer Graphics Library                            o     o   *
00004 *                                                                _   O  _   *
00005 * Copyright(C) 2004                                                \/)\/    *
00006 * Visual Computing Lab                                            /\/|      *
00007 * ISTI - Italian National Research Council                           |      *
00008 *                                                                    \      *
00009 * All rights reserved.                                                      *
00010 *                                                                           *
00011 * This program is free software; you can redistribute it and/or modify      *
00012 * it under the terms of the GNU General Public License as published by      *
00013 * the Free Software Foundation; either version 2 of the License, or         *
00014 * (at your option) any later version.                                       *
00015 *                                                                           *
00016 * This program is distributed in the hope that it will be useful,           *
00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
00019 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
00020 * for more details.                                                         *
00021 *                                                                           *
00022 ****************************************************************************/
00023 
00028 
00029 #ifndef __VCGLIB_EXPORT_OFF
00030 #define __VCGLIB_EXPORT_OFF
00031 
00032 #include <stdio.h>
00033 #include <wrap/io_trimesh/io_mask.h>
00034 #include<wrap/io_trimesh/precision.h>
00035 #include <vcg/complex/algorithms/clean.h>
00036 #include <vcg/complex/algorithms/polygon_support.h>
00037 
00038 
00039 namespace vcg {
00040 namespace tri {
00041 namespace io {
00042 template <class SaveMeshType>
00043 class ExporterOFF
00044 {
00045 
00046 public:
00047   typedef typename SaveMeshType::VertexPointer VertexPointer;
00048   typedef typename SaveMeshType::ScalarType ScalarType;
00049   typedef typename SaveMeshType::VertexType VertexType;
00050   typedef typename SaveMeshType::FaceType FaceType;
00051   typedef typename SaveMeshType::FacePointer FacePointer;
00052   typedef typename SaveMeshType::VertexIterator VertexIterator;
00053   typedef typename SaveMeshType::FaceIterator FaceIterator;
00054 
00055   static int Save(SaveMeshType &m, const char * filename, int mask=0 )
00056   {
00057     vcg::face::Pos<FaceType> he;
00058     vcg::face::Pos<FaceType> hei;
00059     FILE * fpout = fopen(filename,"w");
00060     if(fpout==NULL)     return 1; // 1 is the error code for cant'open, see the ErrorMsg function
00061 
00062 
00063     if( tri::HasPerVertexColor(m)  && (mask & io::Mask::IOM_VERTNORMAL))
00064       fprintf(fpout,"N");
00065     if( tri::HasPerVertexColor(m)   && (mask & io::Mask::IOM_VERTCOLOR))
00066       fprintf(fpout,"C");
00067     if( tri::HasPerVertexTexCoord(m) && (mask & io::Mask::IOM_VERTTEXCOORD))
00068       fprintf(fpout,"ST");
00069     fprintf(fpout,"OFF\n");
00070 
00071     int polynumber;
00072     if (mask &io::Mask::IOM_BITPOLYGONAL)
00073       polynumber = tri::Clean<SaveMeshType>::CountBitLargePolygons(m);
00074     else
00075       polynumber = m.fn;
00076 
00077     fprintf(fpout,"%d %d 0\n", m.vn, polynumber); // note that as edge number we simply write zero
00078 
00079     //vertices
00080     int j;
00081     std::vector<int> FlagV;
00082     VertexPointer  vp;
00083     VertexIterator vi;
00084     const int DGT = vcg::tri::io::Precision<ScalarType>::digits();
00085 
00086     for(j=0,vi=m.vert.begin();vi!=m.vert.end();++vi)
00087     {
00088       vp=&(*vi);
00089       if (vcg::tri::HasPerVertexFlags(m))
00090         FlagV.push_back(vp->Flags()); // Save vertex flags
00091       if( ! vp->IsD() )
00092       { // ***** ASCII *****
00093 
00094         fprintf(fpout,"%.*g %.*g %.*g " ,DGT,vp->P()[0],DGT,vp->P()[1],DGT,vp->P()[2]);
00095         if( tri::HasPerVertexColor(m)  && (mask & io::Mask::IOM_VERTCOLOR) )
00096           fprintf(fpout,"%d %d %d %d ",vp->C()[0],vp->C()[1],vp->C()[2],vp->C()[3] );
00097 
00098         if( tri::HasPerVertexNormal(m)  && (mask & io::Mask::IOM_VERTNORMAL) )
00099           fprintf(fpout,"%g %g %g ", double(vp->N()[0]),double(vp->N()[1]),double(vp->N()[2]));
00100 
00101         if( tri::HasPerVertexTexCoord(m)  && (mask & io::Mask::IOM_VERTTEXCOORD) )
00102           fprintf(fpout,"%g %g ",vp->T().u(),vp->T().v());
00103 
00104         fprintf(fpout,"\n");
00105 
00106 
00107         vp->Flags()=j; // Trucco! Nascondi nei flags l'indice del vertice non deletato!
00108         j++;
00109       }
00110     }
00111 
00112     assert(j==m.vn);
00113 
00114 
00115     if (mask &io::Mask::IOM_BITPOLYGONAL) {
00116       tri::RequireFFAdjacency(m);
00117       std::vector<VertexPointer> polygon;
00118       tri::UpdateFlags<SaveMeshType>::FaceClearV(m);
00119       for(FaceIterator fi=m.face.begin();fi!=m.face.end();++fi) if (!fi->IsD()) if (!fi->IsV()) {
00120         vcg::tri::PolygonSupport<SaveMeshType,SaveMeshType>::ExtractPolygon(&*fi,polygon);
00121         if(!polygon.empty())
00122         {
00123           fprintf(fpout,"%d ", int(polygon.size()) );
00124           for (size_t i=0; i<polygon.size(); i++) fprintf(fpout,"%d ", polygon[i]->Flags() );
00125           fprintf(fpout,"\n");
00126         }
00127       }
00128     }
00129     else {
00130       for(FaceIterator fi=m.face.begin();fi!=m.face.end();++fi)
00131       {
00132         if( ! fi->IsD() )
00133         {
00134           if( tri::HasPerFaceColor(m)  && (mask & io::Mask::IOM_FACECOLOR) )
00135             fprintf(fpout,"3 %d %d %d %i %i %i\n", fi->cV(0)->Flags(),  fi->cV(1)->Flags(), fi->cV(2)->Flags(), fi->C()[0],fi->C()[1],fi->C()[2] );
00136             else
00137           fprintf(fpout,"3 %d %d %d\n", fi->cV(0)->Flags(),     fi->cV(1)->Flags(), fi->cV(2)->Flags() );
00138         }
00139       }
00140     }
00141 
00142 
00143     fclose(fpout);
00144     // Recupera i flag originali
00145     j=0;
00146     for(vi=m.vert.begin();vi!=m.vert.end();++vi)
00147       (*vi).Flags()=FlagV[j++];
00148 
00149     return 0;
00150   }
00151 
00152   static const char *ErrorMsg(int error)
00153   {
00154     static std::vector<std::string> off_error_msg;
00155     if(off_error_msg.empty())
00156     {
00157       off_error_msg.resize(2 );
00158       off_error_msg[0]="No errors";
00159       off_error_msg[1]="Can't open file";
00160     }
00161 
00162     if(error>1 || error<0) return "Unknown error";
00163     else return off_error_msg[error].c_str();
00164   }
00165   /*
00166             returns mask of capability one define with what are the saveable information of the format.
00167         */
00168   static int GetExportMaskCapability()
00169   {
00170     int capability = 0;
00171     capability |= vcg::tri::io::Mask::IOM_VERTCOORD;
00172     capability |= vcg::tri::io::Mask::IOM_VERTCOLOR;
00173     capability |= vcg::tri::io::Mask::IOM_VERTTEXCOORD;
00174     capability |= vcg::tri::io::Mask::IOM_FACEINDEX;
00175     capability |= vcg::tri::io::Mask::IOM_FACECOLOR;
00176     capability |= vcg::tri::io::Mask::IOM_BITPOLYGONAL;
00177     return capability;
00178   }
00179 
00180 }; // end class
00181 } // end namespace tri
00182 } // end namespace io
00183 } // end namespace vcg
00185 #endif


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:30:52