| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#ifndef __VCGLIB_IMPORT_ASC |
| 26 |
#define __VCGLIB_IMPORT_ASC |
| 27 |
|
| 28 |
#include <stdio.h> |
| 29 |
#include <fstream> |
| 30 |
#include <iostream> |
| 31 |
#include <vcg/complex/algorithms/create/platonic.h> |
| 32 |
#include <vcg/complex/algorithms/clean.h> |
| 33 |
|
| 34 |
namespace vcg { |
| 35 |
namespace tri { |
| 36 |
namespace io { |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
template <class MESH_TYPE> |
| 44 |
class ImporterASC |
| 45 |
{ |
| 46 |
public: |
| 47 |
|
| 48 |
typedef typename MESH_TYPE::VertexPointer VertexPointer; |
| 49 |
typedef typename MESH_TYPE::ScalarType ScalarType; |
| 50 |
typedef typename MESH_TYPE::VertexType VertexType; |
| 51 |
typedef typename MESH_TYPE::FaceType FaceType; |
| 52 |
typedef typename MESH_TYPE::VertexIterator VertexIterator; |
| 53 |
typedef typename MESH_TYPE::FaceIterator FaceIterator; |
| 54 |
|
| 55 |
enum RAWError { |
| 56 |
E_NOERROR, |
| 57 |
|
| 58 |
E_CANTOPEN, |
| 59 |
E_UNESPECTEDEOF, |
| 60 |
E_NO_POINTS, |
| 61 |
}; |
| 62 |
|
| 63 |
static const char *ErrorMsg(int error) |
| 64 |
{ |
| 65 |
static const char * raw_error_msg[] = |
| 66 |
{ |
| 67 |
"No errors", |
| 68 |
"Can't open file", |
| 69 |
"Premature End of file", |
| 70 |
"Failed to import any point. Use simple ascii files with just x y z coords." |
| 71 |
}; |
| 72 |
|
| 73 |
if(error>3 || error<0) return "Unknown error"; |
| 74 |
else return raw_error_msg[error]; |
| 75 |
}; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
static int Open( MESH_TYPE &m, const char * filename, CallBackPos *cb=0, bool triangulate=false, int lineskip=0) |
| 86 |
{ |
| 87 |
FILE *fp; |
| 88 |
fp = fopen(filename, "r"); |
| 89 |
if(fp == NULL) |
| 90 |
{ |
| 91 |
qDebug("Failed opening of %s",filename); |
| 92 |
return E_CANTOPEN; |
| 93 |
} |
| 94 |
long currentPos = ftell(fp); |
| 95 |
fseek(fp,0L,SEEK_END); |
| 96 |
long fileLen = ftell(fp); |
| 97 |
fseek(fp,currentPos,SEEK_SET); |
| 98 |
|
| 99 |
m.Clear(); |
| 100 |
|
| 101 |
Point3f pp; |
| 102 |
float q; |
| 103 |
size_t cnt=0; |
| 104 |
int ret; |
| 105 |
char buf[1024]; |
| 106 |
|
| 107 |
|
| 108 |
for(int i=0;i<lineskip;++i) |
| 109 |
fgets(buf,1024,fp); |
| 110 |
|
| 111 |
|
| 112 |
while(!feof(fp)) |
| 113 |
{ |
| 114 |
if(cb && (++cnt)%1000) cb( (ftell(fp)*100)/fileLen, "ASC Mesh Loading"); |
| 115 |
if(feof(fp)) break; |
| 116 |
bool fgetOut=fgets(buf,1024,fp); |
| 117 |
if( fgetOut == 0 ) continue; |
| 118 |
ret=sscanf(buf, "%f, %f, %f, %f\n", &pp.X(), &pp.Y(), &pp.Z(),&q); |
| 119 |
if(ret==1) |
| 120 |
ret=sscanf(buf, "%f %f %f %f\n", &pp.X(), &pp.Y(), &pp.Z(),&q); |
| 121 |
|
| 122 |
if(ret>=3) |
| 123 |
{ |
| 124 |
VertexIterator vi=Allocator<MESH_TYPE>::AddVertices(m,1); |
| 125 |
(*vi).P().Import(pp); |
| 126 |
if(ret==4) (*vi).Q()=q; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
fclose(fp); |
| 131 |
if(m.vn==0) return E_NO_POINTS; |
| 132 |
if(!triangulate) return E_NOERROR; |
| 133 |
|
| 134 |
|
| 135 |
float baseY = m.vert[0].P().Y(); |
| 136 |
unsigned int i; |
| 137 |
for(i=1;i<m.vert.size();++i) |
| 138 |
{ |
| 139 |
if(m.vert[i].P().Y()!= baseY) break; |
| 140 |
} |
| 141 |
cnt=m.vert.size(); |
| 142 |
qDebug("Grid is %i x %li = %li (%zu) ",i,cnt/i,i* (cnt/i),cnt); |
| 143 |
tri::FaceGrid(m,i,int(cnt/i)); |
| 144 |
tri::Clean<MESH_TYPE>::FlipMesh(m); |
| 145 |
return E_NOERROR; |
| 146 |
} |
| 147 |
|
| 148 |
}; |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
#endif |