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; }
86 if (_filename.rfind(
".stla") != std::string::npos)
90 else if (_filename.rfind(
".stlb") != std::string::npos)
96 std::fstream out(_filename.c_str(), (_opt.check(
Options::Binary) ? std::ios_base::binary | std::ios_base::out
97 : std::ios_base::out) );
99 bool result =
write(out, _be, _opt, _precision);
114 if (!check(_be, _opt))
return false;
123 _os.precision(_precision);
126 return write_stlb(_os, _be, _opt);
128 return write_stla(_os, _be, _opt);
142 omlog() <<
"[STLWriter] : write ascii file\n";
146 FILE* out = fopen(_filename.c_str(),
"w");
149 omerr() <<
"[STLWriter] : cannot open file " << _filename << std::endl;
156 int i, nF(
int(_be.n_faces()));
158 std::vector<VertexHandle> vhandles;
163 fprintf(out,
"solid \n");
170 const int nV = _be.get_vhandles(fh, vhandles);
174 a = _be.point(vhandles[0]);
175 b = _be.point(vhandles[1]);
176 c = _be.point(vhandles[2]);
177 n = (_be.has_face_normals() ?
179 ((c-b) % (a-b)).normalize());
181 fprintf(out,
"facet normal %f %f %f\nouter loop\n", n[0], n[1], n[2]);
182 fprintf(out,
"vertex %.10f %.10f %.10f\n", a[0], a[1], a[2]);
183 fprintf(out,
"vertex %.10f %.10f %.10f\n", b[0], b[1], b[2]);
184 fprintf(out,
"vertex %.10f %.10f %.10f", c[0], c[1], c[2]);
187 omerr() <<
"[STLWriter] : Warning non-triangle data!\n";
189 fprintf(out,
"\nendloop\nendfacet\n");
192 fprintf(out,
"endsolid\n");
205 write_stla(std::ostream& _out,
BaseExporter& _be,
Options , std::streamsize _precision)
const 207 omlog() <<
"[STLWriter] : write ascii file\n";
209 int i, nF(
int(_be.n_faces()));
211 std::vector<VertexHandle> vhandles;
213 _out.precision(_precision);
224 const int nV = _be.get_vhandles(fh, vhandles);
228 a = _be.point(vhandles[0]);
229 b = _be.point(vhandles[1]);
230 c = _be.point(vhandles[2]);
231 n = (_be.has_face_normals() ?
233 ((c-b) % (a-b)).normalize());
235 _out <<
"facet normal " << n[0] <<
" " << n[1] <<
" " << n[2] <<
"\nouter loop\n";
237 _out <<
"vertex " << a[0] <<
" " << a[1] <<
" " << a[2] <<
"\n";
238 _out <<
"vertex " << b[0] <<
" " << b[1] <<
" " << b[2] <<
"\n";
239 _out <<
"vertex " << c[0] <<
" " << c[1] <<
" " << c[2] <<
"\n";
241 omerr() <<
"[STLWriter] : Warning non-triangle data!\n";
244 _out <<
"\nendloop\nendfacet\n";
247 _out <<
"endsolid\n";
259 omlog() <<
"[STLWriter] : write binary file\n";
263 FILE* out = fopen(_filename.c_str(),
"wb");
266 omerr() <<
"[STLWriter] : cannot open file " << _filename << std::endl;
271 int i, nF(
int(_be.n_faces()));
273 std::vector<VertexHandle> vhandles;
278 const char header[80] =
281 fwrite(header, 1, 80, out);
292 const int nV = _be.get_vhandles(fh, vhandles);
296 a = _be.point(vhandles[0]);
297 b = _be.point(vhandles[1]);
298 c = _be.point(vhandles[2]);
299 n = (_be.has_face_normals() ?
301 ((c-b) % (a-b)).normalize());
325 omerr() <<
"[STLWriter] : Warning: Skipped non-triangle data!\n";
337 write_stlb(std::ostream& _out,
BaseExporter& _be,
Options , std::streamsize _precision)
const 339 omlog() <<
"[STLWriter] : write binary file\n";
342 int i, nF(
int(_be.n_faces()));
344 std::vector<VertexHandle> vhandles;
346 _out.precision(_precision);
350 const char header[80] =
353 _out.write(header, 80);
364 const int nV = _be.get_vhandles(fh, vhandles);
368 a = _be.point(vhandles[0]);
369 b = _be.point(vhandles[1]);
370 c = _be.point(vhandles[2]);
371 n = (_be.has_face_normals() ?
373 ((c-b) % (a-b)).normalize());
397 omerr() <<
"[STLWriter] : Warning: Skipped non-triangle data!\n";
413 size_t _12floats(12*
sizeof(
float));
419 int i, nF(
int(_be.n_faces()));
420 std::vector<VertexHandle> vhandles;
423 if (_be.get_vhandles(
FaceHandle(i), vhandles) == 3)
424 bytes += _12floats +
sizeof(
short);
426 omerr() <<
"[STLWriter] : Warning: Skipped non-triangle data!\n";
Handle for a face entity.
bool write(const std::string &, BaseExporter &, Options, std::streamsize _precision=6) const override
Has (r) / store (w) face colors.
void write_int(int _i, FILE *_out, bool _swap=false)
bool register_module(BaseReader *_bl)
Set options for reader/writer modules.
Has (r) / store (w) vertex normals.
_IOManager_ & IOManager()
void write_short(short int _i, FILE *_out, bool _swap=false)
void write_float(float _f, FILE *_out, bool _swap=false)
Has (r) / store (w) texture coordinates.
size_t binary_size(BaseExporter &, Options) const override
Returns expected size of file if binary format is supported else 0.