diff --git a/src/OpenMesh/Core/IO/BinaryHelper.cc b/src/OpenMesh/Core/IO/BinaryHelper.cc index 6162b00c..eb55699c 100644 --- a/src/OpenMesh/Core/IO/BinaryHelper.cc +++ b/src/OpenMesh/Core/IO/BinaryHelper.cc @@ -240,6 +240,66 @@ void write_double(double _d, FILE* _out, bool _swap) fwrite((char*)dc.c, 1, 8, _out); } + +//----------------------------------------------------------------------------- + + +void write_short(short int _i, std::ostream& _out, bool _swap) +{ + union u1 { short int s; unsigned char c[2]; } sc; + sc.s = _i; + if (_swap) std::swap(sc.c[0], sc.c[1]); + _out.write((char*)sc.c, 2); +} + + +//----------------------------------------------------------------------------- + + +void write_int(int _i, std::ostream& _out, bool _swap) +{ + union u2 { int i; unsigned char c[4]; } ic; + ic.i = _i; + if (_swap) { + std::swap(ic.c[0], ic.c[3]); + std::swap(ic.c[1], ic.c[2]); + } + _out.write((char*)ic.c, 4); +} + + +//----------------------------------------------------------------------------- + + +void write_float(float _f, std::ostream& _out, bool _swap) +{ + union u3 { float f; unsigned char c[4]; } fc; + fc.f = _f; + if (_swap) { + std::swap(fc.c[0], fc.c[3]); + std::swap(fc.c[1], fc.c[2]); + } + _out.write((char*)fc.c, 4); +} + + +//----------------------------------------------------------------------------- + + +void write_double(double _d, std::ostream& _out, bool _swap) +{ + union u4 { double d; unsigned char c[8]; } dc; + dc.d = _d; + if (_swap) { + std::swap(dc.c[0], dc.c[7]); + std::swap(dc.c[1], dc.c[6]); + std::swap(dc.c[2], dc.c[5]); + std::swap(dc.c[3], dc.c[4]); + } + _out.write((char*)dc.c, 8); +} + + #endif //============================================================================= diff --git a/src/OpenMesh/Core/IO/BinaryHelper.hh b/src/OpenMesh/Core/IO/BinaryHelper.hh index ef862f11..d470d565 100644 --- a/src/OpenMesh/Core/IO/BinaryHelper.hh +++ b/src/OpenMesh/Core/IO/BinaryHelper.hh @@ -129,6 +129,21 @@ void write_float(float _f, FILE* _out, bool _swap=false); \c _swap is true */ void write_double(double _d, FILE* _out, bool _swap=false); +/** Binary write a \c short to \c _os and perform byte swapping if + \c _swap is true */ +void write_short(short int _i, std::ostream& _out, bool _swap=false); + +/** Binary write an \c int to \c _os and perform byte swapping if + \c _swap is true */ +void write_int(int _i, std::ostream& _out, bool _swap=false); + +/** Binary write a \c float to \c _os and perform byte swapping if + \c _swap is true */ +void write_float(float _f, std::ostream& _out, bool _swap=false); + +/** Binary write a \c double to \c _os and perform byte swapping if + \c _swap is true */ +void write_double(double _d, std::ostream& _out, bool _swap=false); //@} diff --git a/src/OpenMesh/Core/IO/writer/STLWriter.cc b/src/OpenMesh/Core/IO/writer/STLWriter.cc index 8444ea17..36fef3f7 100644 --- a/src/OpenMesh/Core/IO/writer/STLWriter.cc +++ b/src/OpenMesh/Core/IO/writer/STLWriter.cc @@ -4,10 +4,10 @@ * Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen * * www.openmesh.org * * * - *---------------------------------------------------------------------------* + *---------------------------------------------------------------------------* * This file is part of OpenMesh. * * * - * OpenMesh is free software: you can redistribute it and/or modify * + * OpenMesh is free software: you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License as * * published by the Free Software Foundation, either version 3 of * * the License, or (at your option) any later version with the * @@ -30,10 +30,10 @@ * License along with OpenMesh. If not, * * see . * * * -\*===========================================================================*/ +\*===========================================================================*/ /*===========================================================================*\ - * * + * * * $Revision$ * * $Date$ * * * @@ -105,7 +105,7 @@ write(const std::string& _filename, BaseExporter& _be, Options _opt) const } else if (_filename.rfind(".stl") != std::string::npos) { - return (_opt.check( Options::Binary ) + return (_opt.check( Options::Binary ) ? write_stlb(_filename, _be, _opt) : write_stla(_filename, _be, _opt) ); } @@ -120,7 +120,20 @@ bool _STLWriter_:: write(std::ostream& _os, BaseExporter& _be, Options _opt) const { - omerr() << "[STLWriter] : STL Streams are not supported " << std::endl; + // check exporter features + if (!check(_be, _opt)) return false; + + // check writer features + if (_opt.check(Options::VertexNormal) || + _opt.check(Options::VertexTexCoord) || + _opt.check(Options::FaceColor)) + return false; + + if (_opt & Options::Binary) + return write_stlb(_os, _be, _opt); + else + return write_stla(_os, _be, _opt); + return false; } @@ -194,6 +207,57 @@ write_stla(const std::string& _filename, BaseExporter& _be, Options /* _opt */) //----------------------------------------------------------------------------- +bool +_STLWriter_:: +write_stla(std::ostream& _out, BaseExporter& _be, Options /* _opt */) const +{ + omlog() << "[STLWriter] : write ascii file\n"; + + unsigned int i, nF(_be.n_faces()), nV; + Vec3f a, b, c, n; + std::vector vhandles; + FaceHandle fh; + std::streamsize prec = _out.precision(); + + + // header + _out << "solid\n"; + + + // write face set + for (i=0; i vhandles; + FaceHandle fh; + + + // write header + const char header[80] = + "binary stl file" + " "; + _out.write(header, 80); + + + // number of faces + write_int(_be.n_faces(), _out); + + + // write face set + for (i=0; i. * * * -\*===========================================================================*/ +\*===========================================================================*/ /*===========================================================================*\ - * * + * * * $Revision$ * * $Date$ * * * @@ -77,31 +77,33 @@ namespace IO { //=== IMPLEMENTATION ========================================================== -/** - Implementation of the STL format writer. This class is singleton'ed by +/** + Implementation of the STL format writer. This class is singleton'ed by SingletonT to STLWriter. */ class OPENMESHDLLEXPORT _STLWriter_ : public BaseWriter { public: - + _STLWriter_(); /// Destructor virtual ~_STLWriter_() {}; - + std::string get_description() const { return "Stereolithography Format"; } std::string get_extensions() const { return "stla stlb"; } - + bool write(const std::string&, BaseExporter&, Options) const; - + bool write(std::ostream&, BaseExporter&, Options) const; - + size_t binary_size(BaseExporter&, Options) const; private: bool write_stla(const std::string&, BaseExporter&, Options) const; + bool write_stla(std::ostream&, BaseExporter&, Options) const; bool write_stlb(const std::string&, BaseExporter&, Options) const; + bool write_stlb(std::ostream&, BaseExporter&, Options) const; };