53#include <OpenMesh/Core/Geometry/VectorT.hh>
54#include <OpenMesh/Core/IO/BinaryHelper.hh>
55#include <OpenMesh/Core/IO/IOManager.hh>
56#include <OpenMesh/Core/IO/writer/STLWriter.hh>
68_STLWriter_ __STLWriterInstance;
69_STLWriter_& STLWriter() {
return __STLWriterInstance; }
83write(
const std::string& _filename,
BaseExporter& _be,
const Options& _writeOptions, std::streamsize _precision)
const
85 Options tmpOptions = _writeOptions;
88 if (_filename.rfind(
".stla") != std::string::npos)
92 else if (_filename.rfind(
".stlb") != std::string::npos)
98 std::fstream out(_filename.c_str(), (tmpOptions.check(
Options::Binary) ? std::ios_base::binary | std::ios_base::out
99 : std::ios_base::out) );
101 bool result =
write(out, _be, tmpOptions, _precision);
116 if (!check(_be, _writeOptions))
return false;
125 _os.precision(_precision);
128 return write_stlb(_os, _be, _writeOptions);
130 return write_stla(_os, _be, _writeOptions);
144 omlog() <<
"[STLWriter] : write ascii file\n";
148 FILE* out = fopen(_filename.c_str(),
"w");
151 omerr() <<
"[STLWriter] : cannot open file " << _filename << std::endl;
158 int i, nF(
int(_be.n_faces()));
160 std::vector<VertexHandle> vhandles;
165 fprintf(out,
"solid \n");
172 const int nV = _be.get_vhandles(fh, vhandles);
176 a = _be.point(vhandles[0]);
177 b = _be.point(vhandles[1]);
178 c = _be.point(vhandles[2]);
179 n = (_be.has_face_normals() ?
181 ((c-b) % (a-b)).normalize());
183 fprintf(out,
"facet normal %f %f %f\nouter loop\n", n[0], n[1], n[2]);
184 fprintf(out,
"vertex %.10f %.10f %.10f\n", a[0], a[1], a[2]);
185 fprintf(out,
"vertex %.10f %.10f %.10f\n", b[0], b[1], b[2]);
186 fprintf(out,
"vertex %.10f %.10f %.10f", c[0], c[1], c[2]);
189 omerr() <<
"[STLWriter] : Warning non-triangle data!\n";
191 fprintf(out,
"\nendloop\nendfacet\n");
194 fprintf(out,
"endsolid\n");
207write_stla(std::ostream& _out,
const BaseExporter& _be, Options , std::streamsize _precision)
const
209 omlog() <<
"[STLWriter] : write ascii file\n";
211 int i, nF(
int(_be.n_faces()));
213 std::vector<VertexHandle> vhandles;
215 _out.precision(_precision);
226 const int nV = _be.get_vhandles(fh, vhandles);
230 a = _be.point(vhandles[0]);
231 b = _be.point(vhandles[1]);
232 c = _be.point(vhandles[2]);
233 n = (_be.has_face_normals() ?
235 ((c-b) % (a-b)).normalize());
237 _out <<
"facet normal " << n[0] <<
" " << n[1] <<
" " << n[2] <<
"\nouter loop\n";
239 _out <<
"vertex " << a[0] <<
" " << a[1] <<
" " << a[2] <<
"\n";
240 _out <<
"vertex " << b[0] <<
" " << b[1] <<
" " << b[2] <<
"\n";
241 _out <<
"vertex " << c[0] <<
" " << c[1] <<
" " << c[2] <<
"\n";
243 omerr() <<
"[STLWriter] : Warning non-triangle data!\n";
246 _out <<
"\nendloop\nendfacet\n";
249 _out <<
"endsolid\n";
259write_stlb(
const std::string& _filename,
const BaseExporter& _be, Options )
const
261 omlog() <<
"[STLWriter] : write binary file\n";
265 FILE* out = fopen(_filename.c_str(),
"wb");
268 omerr() <<
"[STLWriter] : cannot open file " << _filename << std::endl;
273 int i, nF(
int(_be.n_faces()));
275 std::vector<VertexHandle> vhandles;
280 const char header[80] =
283 fwrite(header, 1, 80, out);
294 const int nV = _be.get_vhandles(fh, vhandles);
298 a = _be.point(vhandles[0]);
299 b = _be.point(vhandles[1]);
300 c = _be.point(vhandles[2]);
301 n = (_be.has_face_normals() ?
303 ((c-b) % (a-b)).normalize());
327 omerr() <<
"[STLWriter] : Warning: Skipped non-triangle data!\n";
339write_stlb(std::ostream& _out,
const BaseExporter& _be, Options , std::streamsize _precision)
const
341 omlog() <<
"[STLWriter] : write binary file\n";
344 int i, nF(
int(_be.n_faces()));
346 std::vector<VertexHandle> vhandles;
348 _out.precision(_precision);
352 const char header[80] =
355 _out.write(header, 80);
366 const int nV = _be.get_vhandles(fh, vhandles);
370 a = _be.point(vhandles[0]);
371 b = _be.point(vhandles[1]);
372 c = _be.point(vhandles[2]);
373 n = (_be.has_face_normals() ?
375 ((c-b) % (a-b)).normalize());
399 omerr() <<
"[STLWriter] : Warning: Skipped non-triangle data!\n";
415 size_t _12floats(12*
sizeof(
float));
421 int i, nF(
int(_be.n_faces()));
422 std::vector<VertexHandle> vhandles;
425 if (_be.get_vhandles(
FaceHandle(i), vhandles) == 3)
426 bytes += _12floats +
sizeof(short);
428 omerr() <<
"[STLWriter] : Warning: Skipped non-triangle data!\n";
Set options for reader/writer modules.
@ FaceColor
Has (r) / store (w) face colors.
@ Binary
Set binary mode for r/w.
@ VertexNormal
Has (r) / store (w) vertex normals.
@ VertexTexCoord
Has (r) / store (w) texture coordinates.
bool register_module(BaseReader *_bl)
size_t binary_size(BaseExporter &, const Options &) const override
Returns expected size of file if binary format is supported else 0.
bool write(const std::string &, BaseExporter &, const Options &_writeOptions, std::streamsize _precision=6) const override
void write_int(int _i, FILE *_out, bool _swap=false)
void write_short(short int _i, FILE *_out, bool _swap=false)
_IOManager_ & IOManager()
void write_float(float _f, FILE *_out, bool _swap=false)
Handle for a face entity.