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