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 #ifndef __VCGLIB_IMPORT_ASC
00026 #define __VCGLIB_IMPORT_ASC
00027
00028 #include <stdio.h>
00029 #include <fstream>
00030 #include <iostream>
00031 #include <vcg/complex/algorithms/create/platonic.h>
00032 #include <vcg/complex/algorithms/clean.h>
00033
00034 namespace vcg {
00035 namespace tri {
00036 namespace io {
00037
00043 template <class MESH_TYPE>
00044 class ImporterASC
00045 {
00046 public:
00047
00048 typedef typename MESH_TYPE::VertexPointer VertexPointer;
00049 typedef typename MESH_TYPE::ScalarType ScalarType;
00050 typedef typename MESH_TYPE::VertexType VertexType;
00051 typedef typename MESH_TYPE::FaceType FaceType;
00052 typedef typename MESH_TYPE::VertexIterator VertexIterator;
00053 typedef typename MESH_TYPE::FaceIterator FaceIterator;
00054
00055 enum RAWError {
00056 E_NOERROR,
00057
00058 E_CANTOPEN,
00059 E_UNESPECTEDEOF,
00060 E_NO_POINTS,
00061 };
00062
00063 static const char *ErrorMsg(int error)
00064 {
00065 static const char * raw_error_msg[] =
00066 {
00067 "No errors",
00068 "Can't open file",
00069 "Premature End of file",
00070 "Failed to import any point. Use simple ascii files with just x y z coords."
00071 };
00072
00073 if(error>3 || error<0) return "Unknown error";
00074 else return raw_error_msg[error];
00075 };
00076
00085 static int Open( MESH_TYPE &m, const char * filename, CallBackPos *cb=0, bool triangulate=false, int lineskip=0)
00086 {
00087 FILE *fp;
00088 fp = fopen(filename, "r");
00089 if(fp == NULL)
00090 {
00091 qDebug("Failed opening of %s",filename);
00092 return E_CANTOPEN;
00093 }
00094 long currentPos = ftell(fp);
00095 fseek(fp,0L,SEEK_END);
00096 long fileLen = ftell(fp);
00097 fseek(fp,currentPos,SEEK_SET);
00098
00099 m.Clear();
00100
00101 Point3f pp;
00102 float q;
00103 size_t cnt=0;
00104 int ret;
00105 char buf[1024];
00106
00107
00108 for(int i=0;i<lineskip;++i)
00109 fgets(buf,1024,fp);
00110
00111
00112 while(!feof(fp))
00113 {
00114 if(cb && (++cnt)%1000) cb( (ftell(fp)*100)/fileLen, "ASC Mesh Loading");
00115 if(feof(fp)) break;
00116 bool fgetOut=fgets(buf,1024,fp);
00117 if( fgetOut == 0 ) continue;
00118 ret=sscanf(buf, "%f, %f, %f, %f\n", &pp.X(), &pp.Y(), &pp.Z(),&q);
00119 if(ret==1)
00120 ret=sscanf(buf, "%f %f %f %f\n", &pp.X(), &pp.Y(), &pp.Z(),&q);
00121
00122 if(ret>=3)
00123 {
00124 VertexIterator vi=Allocator<MESH_TYPE>::AddVertices(m,1);
00125 (*vi).P().Import(pp);
00126 if(ret==4) (*vi).Q()=q;
00127 }
00128 }
00129
00130 fclose(fp);
00131 if(m.vn==0) return E_NO_POINTS;
00132 if(!triangulate) return E_NOERROR;
00133
00134
00135 float baseY = m.vert[0].P().Y();
00136 int i;
00137 for(i=1;i<m.vert.size();++i)
00138 {
00139 if(m.vert[i].P().Y()!= baseY) break;
00140 }
00141 cnt=m.vert.size();
00142 qDebug("Grid is %i x %i = %i (%i) ",i,cnt/i,i* (cnt/i),cnt);
00143 tri::FaceGrid(m,i,int(cnt/i));
00144 tri::Clean<MESH_TYPE>::FlipMesh(m);
00145 return E_NOERROR;
00146 }
00147
00148 };
00149 }
00150 }
00151 }
00152
00153 #endif