| 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 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#ifndef __VCGLIB_IMPORT_ASC |
| 31 |
#define __VCGLIB_IMPORT_ASC |
| 32 |
|
| 33 |
#include <stdio.h> |
| 34 |
#include <fstream> |
| 35 |
#include <iostream> |
| 36 |
#include <vcg/complex/trimesh/allocate.h> |
| 37 |
#include <vcg/complex/trimesh/create/platonic.h> |
| 38 |
#include <vcg/complex/trimesh/clean.h> |
| 39 |
|
| 40 |
namespace vcg { |
| 41 |
namespace tri { |
| 42 |
namespace io { |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
template <class MESH_TYPE> |
| 50 |
class ImporterASC |
| 51 |
{ |
| 52 |
public: |
| 53 |
|
| 54 |
typedef typename MESH_TYPE::VertexPointer VertexPointer; |
| 55 |
typedef typename MESH_TYPE::ScalarType ScalarType; |
| 56 |
typedef typename MESH_TYPE::VertexType VertexType; |
| 57 |
typedef typename MESH_TYPE::FaceType FaceType; |
| 58 |
typedef typename MESH_TYPE::VertexIterator VertexIterator; |
| 59 |
typedef typename MESH_TYPE::FaceIterator FaceIterator; |
| 60 |
|
| 61 |
enum RAWError { |
| 62 |
E_NOERROR, |
| 63 |
|
| 64 |
E_CANTOPEN, |
| 65 |
E_UNESPECTEDEOF, |
| 66 |
}; |
| 67 |
|
| 68 |
static const char *ErrorMsg(int error) |
| 69 |
{ |
| 70 |
static const char * raw_error_msg[] = |
| 71 |
{ |
| 72 |
"No errors", |
| 73 |
"Can't open file", |
| 74 |
"Premature End of file", |
| 75 |
}; |
| 76 |
|
| 77 |
if(error>2 || error<0) return "Unknown error"; |
| 78 |
else return raw_error_msg[error]; |
| 79 |
}; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
static int Open( MESH_TYPE &m, const char * filename, CallBackPos *cb=0,bool triangulate=false) |
| 90 |
{ |
| 91 |
FILE *fp; |
| 92 |
fp = fopen(filename, "r"); |
| 93 |
if(fp == NULL) |
| 94 |
{ |
| 95 |
return E_CANTOPEN; |
| 96 |
} |
| 97 |
long currentPos = ftell(fp); |
| 98 |
fseek(fp,0L,SEEK_END); |
| 99 |
long fileLen = ftell(fp); |
| 100 |
fseek(fp,currentPos,SEEK_SET); |
| 101 |
|
| 102 |
m.Clear(); |
| 103 |
|
| 104 |
Point3f pp; |
| 105 |
int cnt=0; |
| 106 |
int ret; |
| 107 |
|
| 108 |
while(!feof(fp)) |
| 109 |
{ |
| 110 |
if(cb && (++cnt)%1000) cb( (ftell(fp)*100)/fileLen, "ASC Mesh Loading"); |
| 111 |
if(feof(fp)) break; |
| 112 |
ret=fscanf(fp, "%f, %f, %f\n", &pp.X(), &pp.Y(), &pp.Z()); |
| 113 |
if(ret!=3) return E_UNESPECTEDEOF; |
| 114 |
VertexIterator vi=Allocator<MESH_TYPE>::AddVertices(m,1); |
| 115 |
(*vi).P().Import(pp); |
| 116 |
} |
| 117 |
|
| 118 |
fclose(fp); |
| 119 |
|
| 120 |
|
| 121 |
float baseY = m.vert[0].P().Y(); |
| 122 |
int i; |
| 123 |
for(i=1;i<m.vert.size();++i) |
| 124 |
{ |
| 125 |
if(m.vert[i].P().Y()!= baseY) break; |
| 126 |
} |
| 127 |
cnt=m.vert.size(); |
| 128 |
qDebug("Grid is %i x %i = %i (%i) ",i,cnt/i,i* (cnt/i),cnt); |
| 129 |
tri::FaceGrid(m,i,cnt/i); |
| 130 |
tri::Clean<CMeshO>::FlipMesh(m); |
| 131 |
return E_NOERROR; |
| 132 |
} |
| 133 |
|
| 134 |
}; |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
#endif |