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 #ifndef __VCGLIB_IMPORT_GTS
00027 #define __VCGLIB_IMPORT_GTS
00028
00029 #include <fstream>
00030 #include <string>
00031 #include <vector>
00032 #include <assert.h>
00033 #include <vcg/space/color4.h>
00034 #include <wrap/callback.h>
00035 #include <wrap/io_trimesh/io_mask.h>
00036
00037 namespace vcg
00038 {
00039 namespace tri
00040 {
00041 namespace io
00042 {
00043
00044
00049 template<class MESH_TYPE>
00050 class ImporterGTS
00051 {
00052 public:
00053
00054 typedef typename MESH_TYPE::VertexType VertexType;
00055 typedef typename MESH_TYPE::VertexIterator VertexIterator;
00056 typedef typename MESH_TYPE::VertexPointer VertexPointer;
00057 typedef typename MESH_TYPE::FaceType FaceType;
00058 typedef typename MESH_TYPE::FaceIterator FaceIterator;
00059 typedef typename MESH_TYPE::FacePointer FacePointer;
00060 typedef typename MESH_TYPE::CoordType CoordType;
00061 typedef typename MESH_TYPE::ScalarType ScalarType;
00062
00063 enum GTSCodes {NoError=0, CantOpen, InvalidFile,
00064 UnsupportedFormat, ErrorNotTriangularFace};
00065
00066 struct Options
00067 {
00068 Options()
00069 : onlyMaskFlag(false), onlyPoints(false), flipFaces(false)
00070 {}
00071 bool onlyMaskFlag;
00072 bool onlyPoints;
00073 bool flipFaces;
00074 };
00075
00081 static const char* ErrorMsg(int message_code)
00082 {
00083 static const char* error_msg[] =
00084 {
00085 "No errors", "Can't open file", "Invalid file",
00086 "Unsupported format", "Face with more than 3 vertices"
00087 };
00088
00089 if(message_code>4 || message_code<0)
00090 return "Unknown error";
00091 else
00092 return error_msg[message_code];
00093 };
00094
00102 static bool LoadMask(const char *filename, int &loadmask)
00103 {
00104
00105
00106 loadmask=0;
00107 MESH_TYPE dummyMesh;
00108 return (Open(dummyMesh, filename, loadmask,0,true)==NoError);
00109 }
00110
00111 static int Open(MESH_TYPE &mesh, const char *filename, CallBackPos *cb=0)
00112 {
00113 int loadmask;
00114 return Open(mesh,filename,loadmask,cb);
00115 }
00116
00124 static int Open(MESH_TYPE &mesh, const char *filename, int &loadmask,
00125 CallBackPos *cb=0, bool onlyMaskFlag=false)
00126 {
00127 Options opt;
00128 opt.onlyMaskFlag = onlyMaskFlag;
00129 return Open(mesh, filename, loadmask, opt, cb);
00130 }
00131
00132 static int Open(MESH_TYPE &mesh, const char *filename, int &loadmask,
00133 const Options& options, CallBackPos *cb=0)
00134 {
00135 QFile device(filename);
00136 if ( (!device.open(QFile::ReadOnly)) )
00137 return CantOpen;
00138
00139 QTextStream stream(&device);
00140
00141 loadmask = Mask::IOM_VERTCOORD | Mask::IOM_FACEINDEX;
00142
00143 int nofVertices, nofFaces, nofEdges;
00144 int id0, id1, id2;
00145 int eid0, eid1, eid2;
00146 typename MESH_TYPE::CoordType v;
00147
00148 QString line;
00149 QStringList sa;
00150 bool done = false;
00151 do
00152 {
00153 line = stream.readLine();
00154 line = line.trimmed();
00155 sa = line.split(' ');
00156 if ((!line.startsWith("#")) && (sa.size()>=3))
00157 {
00158 nofVertices = sa[0].toInt();
00159 nofEdges = sa[1].toInt();
00160 nofFaces = sa[2].toInt();
00161 done = true;
00162 }
00163 } while (!done);
00164
00165 if(options.onlyMaskFlag) return NoError;
00166
00167 int total = nofVertices + nofEdges + nofFaces;
00168
00169 std::cerr << "GtsMeshReader::nofVertices = " << nofVertices << std::endl;
00170 std::cerr << "GtsMeshReader::nofEdges = " << nofEdges << std::endl;
00171 std::cerr << "GtsMeshReader::nofFaces = " << nofFaces << std::endl;
00172
00173 mesh.Clear();
00174 VertexIterator v_iter = Allocator<MESH_TYPE>::AddVertices(mesh, nofVertices);
00175 for(int i=0 ; i<nofVertices ;)
00176 {
00177 if (cb && (i%1000)==0)
00178 cb(i/total, "Vertex Loading");
00179
00180 line = stream.readLine().trimmed();
00181
00182 if (line.startsWith("#"))
00183 {
00184 continue;
00185 }
00186
00187 sa = line.split(' ');
00188 if (!sa.size()>=3)
00189 {
00190 std::cerr << "Error parsing vertex " << line.toLocal8Bit().data() << "\n";
00191 return InvalidFile;
00192 }
00193 v.X() = sa[0].toDouble();
00194 v.Y() = sa[1].toDouble();
00195 v.Z() = sa[2].toDouble();
00196
00197 (*v_iter).P() = v;
00198 ++v_iter;
00199 i++;
00200 }
00201
00202
00203 std::vector< std::pair<int,int> > edges;
00204 for(int i=0 ; i<nofEdges ; ++i)
00205 {
00206 if (cb && (i%1000)==0)
00207 cb((i+nofVertices)/total, "Edge Loading");
00208
00209 line = stream.readLine();
00210 if (line.startsWith("#"))
00211 {
00212 continue;
00213 }
00214
00215 sa = line.split(' ');
00216 if (!sa.size()>=2)
00217 {
00218 std::cerr << "Error parsing edge " << line.toLocal8Bit().data() << "\n";
00219 return InvalidFile;
00220 }
00221 id0 = sa[0].toInt();
00222 id1 = sa[1].toInt();
00223
00224 edges.push_back(std::pair<int,int>(id0-1,id1-1));
00225 }
00226
00227
00228 Allocator<MESH_TYPE>::AddFaces(mesh, nofFaces);
00229 for(int i=0 ; i<nofFaces ; )
00230 {
00231 if (cb && (i%1000)==0)
00232 cb((nofVertices+nofEdges+i)/total, "Face Loading");
00233
00234 line = stream.readLine();
00235 if (line.startsWith("#"))
00236 {
00237 continue;
00238 }
00239
00240 sa = line.split(' ');
00241 if (!sa.size()>=3)
00242 {
00243 std::cerr << "Error parsing face " << line.toLocal8Bit().data() << "\n";
00244 return InvalidFile;
00245 }
00246 eid0 = sa[0].toInt();
00247 eid1 = sa[1].toInt();
00248 eid2 = sa[2].toInt();
00249
00250 int e11 = edges[eid0-1].first;
00251 int e12 = edges[eid0-1].second;
00252 int e21 = edges[eid1-1].first;
00253 int e22 = edges[eid1-1].second;
00254 if (e12 == e21) {
00255 id0 = e11;
00256 id1 = e12;
00257 id2 = e22;
00258 }
00259 else if (e12 == e22) {
00260 id0 = e11;
00261 id1 = e12;
00262 id2 = e21;
00263 }
00264 else if (e11 == e21) {
00265 id0 = e12;
00266 id1 = e11;
00267 id2 = e22;
00268 }
00269 else {
00270 id0 = e12;
00271 id1 = e11;
00272 id2 = e21;
00273 }
00274
00275 if (options.flipFaces)
00276 {
00277 mesh.face[i].V(0) = &(mesh.vert[id0]);
00278 mesh.face[i].V(1) = &(mesh.vert[id2]);
00279 mesh.face[i].V(2) = &(mesh.vert[id1]);
00280 }
00281 else
00282 {
00283 mesh.face[i].V(0) = &(mesh.vert[id0]);
00284 mesh.face[i].V(1) = &(mesh.vert[id1]);
00285 mesh.face[i].V(2) = &(mesh.vert[id2]);
00286 }
00287
00288 ++i;
00289 }
00290 return NoError;
00291 }
00292
00293 protected:
00294
00295 };
00296
00297 }
00298 }
00299 }
00300
00301 #endif //__VCGLIB_IMPORT_GTS