Go to the documentation of this file.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
00030
00031 #ifndef __VCGLIB_EXPORT_GTS
00032 #define __VCGLIB_EXPORT_GTS
00033
00034 #include <QFile>
00035 #include <QTextStream>
00036 #include <map>
00037 #include <wrap/io_trimesh/io_mask.h>
00038
00039
00040 namespace vcg {
00041 namespace tri {
00042 namespace io {
00043 template <class SaveMeshType>
00044 class ExporterGTS
00045 {
00046
00047 public:
00048 typedef typename SaveMeshType::VertexPointer VertexPointer;
00049 typedef typename SaveMeshType::ScalarType ScalarType;
00050 typedef typename SaveMeshType::VertexType VertexType;
00051 typedef typename SaveMeshType::FaceType FaceType;
00052 typedef typename SaveMeshType::FacePointer FacePointer;
00053 typedef typename SaveMeshType::VertexIterator VertexIterator;
00054 typedef typename SaveMeshType::FaceIterator FaceIterator;
00055
00056 static int Save(SaveMeshType &m, const char * filename, int mask=0 )
00057 {
00058 QFile device(filename);
00059 if (!device.open(QFile::WriteOnly))
00060 return 1;
00061
00062 QTextStream stream(&device);
00063
00064
00065 std::vector<int> FlagV;
00066 VertexPointer vp;
00067 VertexIterator vi;
00068 int j;
00069 for(j=0,vi=m.vert.begin(); vi!=m.vert.end(); ++vi)
00070 {
00071 vp = &(*vi);
00072 FlagV.push_back(vp->Flags());
00073 if (!vp->IsD())
00074 {
00075 vp->Flags() = j;
00076 j++;
00077 }
00078 }
00079 assert(j==m.vn);
00080
00081
00082 typedef std::pair<int,int> Edge;
00083 typedef std::map<Edge,int> Edge2Index;
00084 Edge2Index edge2index;
00085
00086
00087 FacePointer fp;
00088 int edgeCount = 0;
00089 FaceIterator fi;
00090 for (j=0,fi=m.face.begin(); fi!=m.face.end(); ++fi)
00091 {
00092 fp = &(*fi);
00093 if (!fp->IsD() )
00094 {
00095 for (int k=0; k<3; ++k)
00096 {
00097 int a = fp->cV(k)->Flags();
00098 int b = fp->cV((k+1)%3)->Flags();
00099 if (a>b)
00100 std::swap(a,b);
00101 Edge e(a,b);
00102 Edge2Index::iterator it = edge2index.find(e);
00103 if (it==edge2index.end())
00104 {
00105 edge2index[e] = edgeCount;
00106 edgeCount++;
00107 }
00108 }
00109 }
00110 }
00111
00112 stream << m.vn << " "
00113 << edgeCount << " "
00114 << m.fn << " "
00115 << "GtsSurface GtsFace GtsEdge GtsVertex\n";
00116
00117
00118 for (vi=m.vert.begin();vi!=m.vert.end();++vi)
00119 {
00120 vp=&(*vi);
00121 if (!vp->IsD())
00122 {
00123 stream << vp->P()[0] << " " << vp->P()[1] << " " << vp->P()[2] << "\n";
00124 }
00125 }
00126
00127
00128 for (j=0,fi=m.face.begin(); fi!=m.face.end(); ++fi)
00129 {
00130 fp = &(*fi);
00131 if (!fp->IsD() )
00132 {
00133 for (int k=0; k<3; ++k)
00134 {
00135 int a = fp->cV(k)->Flags();
00136 int b = fp->cV((k+1)%3)->Flags();
00137 if (a>b)
00138 std::swap(a,b);
00139 Edge e(a,b);
00140 Edge2Index::iterator it = edge2index.find(e);
00141 if (it!=edge2index.end())
00142 {
00143 stream << a+1 << " " << b+1 << "\n";
00144 }
00145 }
00146 }
00147 }
00148
00149
00150 for (j=0,fi=m.face.begin(); fi!=m.face.end(); ++fi)
00151 {
00152 fp = &(*fi);
00153 if (!fp->IsD() )
00154 {
00155 for (int k=0; k<3; ++k)
00156 {
00157 int a = fp->cV(k)->Flags();
00158 int b = fp->cV((k+1)%3)->Flags();
00159 if (a>b)
00160 std::swap(a,b);
00161 Edge e(a,b);
00162 Edge2Index::iterator it = edge2index.find(e);
00163 if (it!=edge2index.end())
00164 stream << it->second+1 << " ";
00165 else
00166 return 2;
00167 }
00168 stream << "\n";
00169 }
00170 }
00171
00172
00173 for(j=0,vi=m.vert.begin();vi!=m.vert.end();++vi)
00174 (*vi).Flags()=FlagV[j++];
00175
00176 return 0;
00177 }
00178
00179 static const char *ErrorMsg(int error)
00180 {
00181 static std::vector<std::string> off_error_msg;
00182 if(off_error_msg.empty())
00183 {
00184 off_error_msg.resize(2 );
00185 off_error_msg[0]="No errors";
00186 off_error_msg[1]="Can't open file";
00187 off_error_msg[2]="Internal error";
00188 }
00189
00190 if(error>2 || error<0) return "Unknown error";
00191 else return off_error_msg[error].c_str();
00192 }
00193
00194
00195
00196 static int GetExportMaskCapability()
00197 {
00198 int capability = 0;
00199 capability |= vcg::tri::io::Mask::IOM_VERTCOORD;
00200 capability |= vcg::tri::io::Mask::IOM_FACEINDEX;
00201 return capability;
00202 }
00203
00204 };
00205 }
00206 }
00207 }
00209 #endif