00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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 <vcg/complex/trimesh/clean.h>
00035 #include <vcg/complex/trimesh/polygon_support.h>
00036
00037
00038 namespace vcg {
00039 namespace tri {
00040 namespace io {
00041 template <class SaveMeshType>
00042 class ExporterOFF
00043 {
00044
00045 public:
00046 typedef typename SaveMeshType::VertexPointer VertexPointer;
00047 typedef typename SaveMeshType::ScalarType ScalarType;
00048 typedef typename SaveMeshType::VertexType VertexType;
00049 typedef typename SaveMeshType::FaceType FaceType;
00050 typedef typename SaveMeshType::FacePointer FacePointer;
00051 typedef typename SaveMeshType::VertexIterator VertexIterator;
00052 typedef typename SaveMeshType::FaceIterator FaceIterator;
00053
00054 static int Save(SaveMeshType &m, const char * filename, int mask=0 )
00055 {
00056 vcg::face::Pos<FaceType> he;
00057 vcg::face::Pos<FaceType> hei;
00058 FILE * fpout = fopen(filename,"w");
00059 if(fpout==NULL) return 1;
00060
00061
00062
00063 if( m.HasPerVertexNormal() && (mask & io::Mask::IOM_VERTNORMAL)) fprintf(fpout,"N");
00064 if( tri::HasPerVertexColor(m) && (mask & io::Mask::IOM_VERTCOLOR)) fprintf(fpout,"C");
00065 if( tri::HasPerVertexTexCoord(m) && (mask & io::Mask::IOM_VERTTEXCOORD)) fprintf(fpout,"ST");
00066 fprintf(fpout,"OFF\n");
00067
00068 int polynumber;
00069 if (mask &io::Mask::IOM_BITPOLYGONAL)
00070 polynumber = tri::Clean<SaveMeshType>::CountBitLargePolygons(m);
00071 else polynumber = m.fn;
00072
00073 fprintf(fpout,"%d %d 0\n", m.vn, polynumber);
00074 typename SaveMeshType::FaceIterator fi;
00075
00076
00077 int j;
00078 std::vector<int> FlagV;
00079 VertexPointer vp;
00080 VertexIterator vi;
00081 for(j=0,vi=m.vert.begin();vi!=m.vert.end();++vi)
00082 {
00083 vp=&(*vi);
00084 FlagV.push_back(vp->UberFlags());
00085 if( ! vp->IsD() )
00086 {
00087
00088 fprintf(fpout,"%g %g %g " ,vp->P()[0],vp->P()[1],vp->P()[2]);
00089 if( tri::HasPerVertexColor(m) && (mask & io::Mask::IOM_VERTCOLOR) )
00090 fprintf(fpout,"%d %d %d %d ",vp->C()[0],vp->C()[1],vp->C()[2],vp->C()[3] );
00091
00092 if( m.HasPerVertexNormal() && (mask & io::Mask::IOM_VERTNORMAL) )
00093 fprintf(fpout,"%g %g %g ", vp->N()[0],vp->N()[1],vp->N()[2]);
00094
00095 if( m.HasPerVertexTexCoord() && (mask & io::Mask::IOM_VERTTEXCOORD) )
00096 fprintf(fpout,"%g %g ",vp->T().u(),vp->T().v());
00097
00098 fprintf(fpout,"\n");
00099
00100
00101 vp->UberFlags()=j;
00102 j++;
00103 }
00104 }
00105
00106 assert(j==m.vn);
00107
00108
00109 if (mask &io::Mask::IOM_BITPOLYGONAL) {
00110
00111 std::vector<VertexPointer> polygon;
00112 for(fi=m.face.begin();fi!=m.face.end();++fi) if (!fi->IsD()) fi->ClearV();
00113 for(fi=m.face.begin();fi!=m.face.end();++fi) if (!fi->IsD()) if (!fi->IsV()) {
00114 assert(tri::HasFFAdjacency(m));
00115 vcg::tri::PolygonSupport<SaveMeshType,SaveMeshType>::ExtractPolygon(&*fi,polygon);
00116 fprintf(fpout,"%d ", int(polygon.size()) );
00117 for (size_t i=0; i<polygon.size(); i++) fprintf(fpout,"%d ", polygon[i]->UberFlags() );
00118 fprintf(fpout,"\n");
00119 }
00120 }
00121 else {
00122 for(fi=m.face.begin();fi!=m.face.end();++fi)
00123 {
00124 FacePointer fp=&(*fi);
00125 if( ! fp->IsD() )
00126 { fprintf(fpout,"3 %d %d %d\n",
00127 fp->cV(0)->UberFlags(), fp->cV(1)->UberFlags(), fp->cV(2)->UberFlags() );
00128 }
00129 }
00130 }
00131
00132
00133 fclose(fpout);
00134
00135 j=0;
00136 for(vi=m.vert.begin();vi!=m.vert.end();++vi)
00137 (*vi).UberFlags()=FlagV[j++];
00138
00139 return 0;
00140 }
00141
00142 static const char *ErrorMsg(int error)
00143 {
00144 static std::vector<std::string> off_error_msg;
00145 if(off_error_msg.empty())
00146 {
00147 off_error_msg.resize(2 );
00148 off_error_msg[0]="No errors";
00149 off_error_msg[1]="Can't open file";
00150 }
00151
00152 if(error>1 || error<0) return "Unknown error";
00153 else return off_error_msg[error].c_str();
00154 }
00155
00156
00157
00158 static int GetExportMaskCapability()
00159 {
00160 int capability = 0;
00161 capability |= vcg::tri::io::Mask::IOM_VERTCOORD;
00162 capability |= vcg::tri::io::Mask::IOM_VERTCOLOR;
00163 capability |= vcg::tri::io::Mask::IOM_VERTTEXCOORD;
00164 capability |= vcg::tri::io::Mask::IOM_FACEINDEX;
00165 capability |= vcg::tri::io::Mask::IOM_BITPOLYGONAL;
00166 return capability;
00167 }
00168
00169 };
00170 }
00171 }
00172 }
00174 #endif