58 #include <OpenMesh/Core/Geometry/VectorT.hh>
59 #include <OpenMesh/Core/IO/BinaryHelper.hh>
60 #include <OpenMesh/Core/IO/IOManager.hh>
61 #include <OpenMesh/Core/IO/writer/STLWriter.hh>
73 _STLWriter_ __STLWriterInstance;
74 _STLWriter_& STLWriter() {
return __STLWriterInstance; }
91 if (_filename.rfind(
".stla") != std::string::npos)
95 else if (_filename.rfind(
".stlb") != std::string::npos)
101 std::fstream out(_filename.c_str(), (_opt.check(
Options::Binary) ? std::ios_base::binary | std::ios_base::out
102 : std::ios_base::out) );
104 bool result =
write(out, _be, _opt, _precision);
119 if (!check(_be, _opt))
return false;
128 _os.precision(_precision);
131 return write_stlb(_os, _be, _opt);
133 return write_stla(_os, _be, _opt);
147 omlog() <<
"[STLWriter] : write ascii file\n";
151 FILE* out = fopen(_filename.c_str(),
"w");
154 omerr() <<
"[STLWriter] : cannot open file " << _filename << std::endl;
161 int i, nF(
int(_be.n_faces())), nV;
163 std::vector<VertexHandle> vhandles;
168 fprintf(out,
"solid \n");
175 nV = _be.get_vhandles(fh, vhandles);
179 a = _be.point(vhandles[0]);
180 b = _be.point(vhandles[1]);
181 c = _be.point(vhandles[2]);
182 n = (_be.has_face_normals() ?
184 ((c-b) % (a-b)).normalize());
186 fprintf(out,
"facet normal %f %f %f\nouter loop\n", n[0], n[1], n[2]);
187 fprintf(out,
"vertex %.10f %.10f %.10f\n", a[0], a[1], a[2]);
188 fprintf(out,
"vertex %.10f %.10f %.10f\n", b[0], b[1], b[2]);
189 fprintf(out,
"vertex %.10f %.10f %.10f", c[0], c[1], c[2]);
192 omerr() <<
"[STLWriter] : Warning non-triangle data!\n";
194 fprintf(out,
"\nendloop\nendfacet\n");
197 fprintf(out,
"endsolid\n");
210 write_stla(std::ostream& _out,
BaseExporter& _be, Options , std::streamsize _precision)
const
212 omlog() <<
"[STLWriter] : write ascii file\n";
214 int i, nF(
int(_be.n_faces())), nV;
216 std::vector<VertexHandle> vhandles;
218 _out.precision(_precision);
229 nV = _be.get_vhandles(fh, vhandles);
233 a = _be.point(vhandles[0]);
234 b = _be.point(vhandles[1]);
235 c = _be.point(vhandles[2]);
236 n = (_be.has_face_normals() ?
238 ((c-b) % (a-b)).normalize());
240 _out <<
"facet normal " << n[0] <<
" " << n[1] <<
" " << n[2] <<
"\nouter loop\n";
242 _out <<
"vertex " << a[0] <<
" " << a[1] <<
" " << a[2] <<
"\n";
243 _out <<
"vertex " << b[0] <<
" " << b[1] <<
" " << b[2] <<
"\n";
244 _out <<
"vertex " << c[0] <<
" " << c[1] <<
" " << c[2] <<
"\n";
246 omerr() <<
"[STLWriter] : Warning non-triangle data!\n";
249 _out <<
"\nendloop\nendfacet\n";
252 _out <<
"endsolid\n";
262 write_stlb(
const std::string& _filename,
BaseExporter& _be, Options )
const
264 omlog() <<
"[STLWriter] : write binary file\n";
268 FILE* out = fopen(_filename.c_str(),
"wb");
271 omerr() <<
"[STLWriter] : cannot open file " << _filename << std::endl;
276 int i, nF(
int(_be.n_faces())), nV;
278 std::vector<VertexHandle> vhandles;
283 const char header[80] =
286 fwrite(header, 1, 80, out);
297 nV = _be.get_vhandles(fh, vhandles);
301 a = _be.point(vhandles[0]);
302 b = _be.point(vhandles[1]);
303 c = _be.point(vhandles[2]);
304 n = (_be.has_face_normals() ?
306 ((c-b) % (a-b)).normalize());
330 omerr() <<
"[STLWriter] : Warning: Skipped non-triangle data!\n";
342 write_stlb(std::ostream& _out,
BaseExporter& _be, Options , std::streamsize _precision)
const
344 omlog() <<
"[STLWriter] : write binary file\n";
347 int i, nF(
int(_be.n_faces())), nV;
349 std::vector<VertexHandle> vhandles;
351 _out.precision(_precision);
355 const char header[80] =
358 _out.write(header, 80);
369 nV = _be.get_vhandles(fh, vhandles);
373 a = _be.point(vhandles[0]);
374 b = _be.point(vhandles[1]);
375 c = _be.point(vhandles[2]);
376 n = (_be.has_face_normals() ?
378 ((c-b) % (a-b)).normalize());
402 omerr() <<
"[STLWriter] : Warning: Skipped non-triangle data!\n";
418 size_t _12floats(12*
sizeof(
float));
424 int i, nF(
int(_be.n_faces()));
425 std::vector<VertexHandle> vhandles;
428 if (_be.get_vhandles(
FaceHandle(i), vhandles) == 3)
429 bytes += _12floats +
sizeof(
short);
431 omerr() <<
"[STLWriter] : Warning: Skipped non-triangle data!\n";
Has (r) / store (w) vertex normals.
bool register_module(BaseReader *_bl)
void write_short(short int _i, FILE *_out, bool _swap=false)
Has (r) / store (w) face colors.
size_t binary_size(BaseExporter &, Options) const
Returns expected size of file if binary format is supported else 0.
Has (r) / store (w) texture coordinates.
Set options for reader/writer modules.
void write_int(int _i, FILE *_out, bool _swap=false)
bool write(const std::string &, BaseExporter &, Options, std::streamsize _precision=6) const
Handle for a face entity.
void write_float(float _f, FILE *_out, bool _swap=false)
_IOManager_ & IOManager()