| 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 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
#ifndef __VCGLIB_EXPORT_STL |
| 62 |
#define __VCGLIB_EXPORT_STL |
| 63 |
|
| 64 |
#include <stdio.h> |
| 65 |
|
| 66 |
namespace vcg { |
| 67 |
namespace tri { |
| 68 |
namespace io { |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
template <class SaveMeshType> |
| 75 |
class ExporterSTL |
| 76 |
{ |
| 77 |
public: |
| 78 |
static int Save(SaveMeshType &m, const char * filename, const int &mask, CallBackPos *cb=0) |
| 79 |
{ |
| 80 |
return Save(m,filename,true); |
| 81 |
} |
| 82 |
|
| 83 |
static int Save(SaveMeshType &m, const char * filename , bool binary =true, const char *objectname=0) |
| 84 |
{ |
| 85 |
typedef typename SaveMeshType::FaceIterator FaceIterator; |
| 86 |
FILE *fp; |
| 87 |
|
| 88 |
fp = fopen(filename,"wb"); |
| 89 |
if(fp==0) |
| 90 |
return 1; |
| 91 |
|
| 92 |
if(binary) |
| 93 |
{ |
| 94 |
|
| 95 |
char *header="VCG "; |
| 96 |
if(objectname) strncpy(header,objectname,80); |
| 97 |
fwrite(header,80,1,fp); |
| 98 |
|
| 99 |
fwrite(&m.fn,1,sizeof(int),fp); |
| 100 |
Point3f p; |
| 101 |
unsigned short attributes=0; |
| 102 |
|
| 103 |
FaceIterator fi; |
| 104 |
for(fi=m.face.begin(); fi!=m.face.end(); ++fi) if( !(*fi).IsD() ) |
| 105 |
{ |
| 106 |
|
| 107 |
p.Import(vcg::NormalizedNormal(*fi)); |
| 108 |
fwrite(p.V(),3,sizeof(float),fp); |
| 109 |
|
| 110 |
for(int k=0;k<3;++k){ |
| 111 |
p.Import((*fi).V(k)->P()); |
| 112 |
fwrite(p.V(),3,sizeof(float),fp); |
| 113 |
} |
| 114 |
fwrite(&attributes,1,sizeof(short),fp); |
| 115 |
} |
| 116 |
} |
| 117 |
else |
| 118 |
{ |
| 119 |
if(objectname) fprintf(fp,"solid %s\n",objectname); |
| 120 |
else fprintf(fp,"solid vcg\n"); |
| 121 |
|
| 122 |
Point3f p; |
| 123 |
FaceIterator fi; |
| 124 |
for(fi=m.face.begin(); fi!=m.face.end(); ++fi) if( !(*fi).IsD() ) |
| 125 |
{ |
| 126 |
|
| 127 |
p.Import(vcg::NormalizedNormal(*fi)); |
| 128 |
fprintf(fp," facet normal %13e %13e %13e\n",p[0],p[1],p[2]); |
| 129 |
fprintf(fp," outer loop\n"); |
| 130 |
for(int k=0;k<3;++k){ |
| 131 |
p.Import((*fi).V(k)->P()); |
| 132 |
fprintf(fp," vertex %13e %13e %13e\n",p[0],p[1],p[2]); |
| 133 |
} |
| 134 |
fprintf(fp," endloop\n"); |
| 135 |
fprintf(fp," endfacet\n"); |
| 136 |
} |
| 137 |
fprintf(fp,"endsolid vcg\n"); |
| 138 |
} |
| 139 |
fclose(fp); |
| 140 |
return 0; |
| 141 |
} |
| 142 |
static const char *ErrorMsg(int error) |
| 143 |
{ |
| 144 |
static std::vector<std::string> stl_error_msg; |
| 145 |
if(stl_error_msg.empty()) |
| 146 |
{ |
| 147 |
stl_error_msg.resize(2 ); |
| 148 |
stl_error_msg[0]="No errors"; |
| 149 |
stl_error_msg[1]="Can't open file"; |
| 150 |
} |
| 151 |
|
| 152 |
if(error>1 || error<0) return "Unknown error"; |
| 153 |
else return stl_error_msg[error].c_str(); |
| 154 |
}; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
static int GetExportMaskCapability() |
| 160 |
{ |
| 161 |
int capability = 0; |
| 162 |
capability |= vcg::tri::io::Mask::IOM_VERTCOORD; |
| 163 |
capability |= vcg::tri::io::Mask::IOM_FACEINDEX; |
| 164 |
return capability; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
}; |
| 169 |
|
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
#endif |