| 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 |
|
| 31 |
#ifndef __VCGLIB_IMPORTER_SMF |
| 32 |
#define __VCGLIB_IMPORTER_SMF |
| 33 |
|
| 34 |
#include <stdio.h> |
| 35 |
#include <vcg/space/point3.h> |
| 36 |
|
| 37 |
namespace vcg { |
| 38 |
namespace tri { |
| 39 |
namespace io { |
| 40 |
template<class OpenMeshType> |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
class ImporterSMF |
| 45 |
{ |
| 46 |
public: |
| 47 |
typedef typename OpenMeshType::VertexPointer VertexPointer; |
| 48 |
typedef typename OpenMeshType::ScalarType ScalarType; |
| 49 |
typedef typename OpenMeshType::VertexType VertexType; |
| 50 |
typedef typename OpenMeshType::FaceType FaceType; |
| 51 |
typedef typename OpenMeshType::VertexIterator VertexIterator; |
| 52 |
typedef typename OpenMeshType::FaceIterator FaceIterator; |
| 53 |
|
| 54 |
|
| 55 |
enum {SMF_LABEL_SIZE=80}; |
| 56 |
|
| 57 |
class SMFFacet |
| 58 |
{ |
| 59 |
public: |
| 60 |
vcg::Point3f n; |
| 61 |
vcg::Point3f v[3]; |
| 62 |
}; |
| 63 |
|
| 64 |
enum SMFError { |
| 65 |
E_NOERROR, |
| 66 |
|
| 67 |
E_CANTOPEN, |
| 68 |
E_UNESPECTEDEOF |
| 69 |
}; |
| 70 |
|
| 71 |
|
| 72 |
static const char *ErrorMsg(int error) |
| 73 |
{ |
| 74 |
static const char * smf_error_msg[] = |
| 75 |
{ |
| 76 |
"No errors", |
| 77 |
"Can't open file", |
| 78 |
"Premature End of file", |
| 79 |
}; |
| 80 |
if(error>2 || error<0) return "Unknown error"; |
| 81 |
else return smf_error_msg[error]; |
| 82 |
}; |
| 83 |
|
| 84 |
static int Open(OpenMeshType &m, const char * filename ) |
| 85 |
{ |
| 86 |
VertexType v; |
| 87 |
FaceType f; |
| 88 |
FILE *fp; |
| 89 |
float x,y,z; |
| 90 |
bool one = true; |
| 91 |
std::map<int,VertexPointer> mv; |
| 92 |
fp = fopen(filename,"r"); |
| 93 |
if(!fp) return -1; |
| 94 |
char buf[1024]; |
| 95 |
while( fgets(buf,1024,fp) ) |
| 96 |
{ |
| 97 |
char *vf, *comm_pt; |
| 98 |
if((comm_pt = strstr(buf,"#")) != NULL) |
| 99 |
*comm_pt = '\0'; |
| 100 |
if( (vf = strstr(buf,"v")) != NULL ) |
| 101 |
{ |
| 102 |
sscanf(vf+1,"%f %f %f", &x, &y, &z); |
| 103 |
v.P()[0] = x; |
| 104 |
v.P()[1] = y; |
| 105 |
v.P()[2] = z; |
| 106 |
m.vert.push_back(v); |
| 107 |
} |
| 108 |
else if( (vf = strstr(buf,"f")) != NULL) |
| 109 |
{ |
| 110 |
if(one) |
| 111 |
{ |
| 112 |
VertexIterator vi; |
| 113 |
int ind; |
| 114 |
for(ind=1,vi=m.vert.begin(); vi!=m.vert.end(); ++vi,++ind) |
| 115 |
mv[ind]=&*vi; |
| 116 |
one = false; |
| 117 |
} |
| 118 |
int v1,v2,v3; |
| 119 |
sscanf(vf+1,"%d %d %d", &v1, &v2, &v3); |
| 120 |
f.V(0) = mv[v1]; |
| 121 |
f.V(1) = mv[v2]; |
| 122 |
f.V(2) = mv[v3]; |
| 123 |
m.face.push_back(f); |
| 124 |
} |
| 125 |
} |
| 126 |
m.vn = m.vert.size(); |
| 127 |
m.fn = m.face.size(); |
| 128 |
return 0; |
| 129 |
} |
| 130 |
}; |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
#endif |
| 136 |
|