Commit 10b271fb authored by Isaak Lim's avatar Isaak Lim
Browse files

- added functionaliy for the STLWriter to also write to ostreams

git-svn-id: http://www.openmesh.org/svnrepo/OpenMesh/trunk@705 fdac6126-5c0c-442c-9429-916003d36597
parent 7d50c6de
......@@ -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
//=============================================================================
......
......@@ -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);
//@}
......
......@@ -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 <http://www.gnu.org/licenses/>. *
* *
\*===========================================================================*/
\*===========================================================================*/
/*===========================================================================*\
* *
* *
* $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<VertexHandle> vhandles;
FaceHandle fh;
std::streamsize prec = _out.precision();
// header
_out << "solid\n";
// write face set
for (i=0; i<nF; ++i)
{
fh = FaceHandle(i);
nV = _be.get_vhandles(fh, vhandles);
if (nV == 3)
{
a = _be.point(vhandles[0]);
b = _be.point(vhandles[1]);
c = _be.point(vhandles[2]);
n = (_be.has_face_normals() ?
_be.normal(fh) :
((c-b) % (a-b)).normalize());
_out.precision(prec);
_out << "facet normal " << n[0] << " " << n[1] << " " << n[2] << "\nouter loop\n";
_out.precision(10);
_out << "vertex " << a[0] << " " << a[1] << " " << a[2] << "\n";
_out << "vertex " << b[0] << " " << b[1] << " " << b[2] << "\n";
_out << "vertex " << c[0] << " " << c[1] << " " << c[2] << "\n";
} else {
omerr() << "[STLWriter] : Warning non-triangle data!\n";
}
_out << "\nendloop\nendfacet\n";
}
return true;
}
//-----------------------------------------------------------------------------
bool
_STLWriter_::
write_stlb(const std::string& _filename, BaseExporter& _be, Options /* _opt */) const
......@@ -272,6 +336,76 @@ write_stlb(const std::string& _filename, BaseExporter& _be, Options /* _opt */)
return true;
}
//-----------------------------------------------------------------------------
bool
_STLWriter_::
write_stlb(std::ostream& _out, BaseExporter& _be, Options /* _opt */) const
{
omlog() << "[STLWriter] : write binary file\n";
unsigned int i, nF(_be.n_faces()), nV;
Vec3f a, b, c, n;
std::vector<VertexHandle> 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<nF; ++i)
{
fh = FaceHandle(i);
nV = _be.get_vhandles(fh, vhandles);
if (nV == 3)
{
a = _be.point(vhandles[0]);
b = _be.point(vhandles[1]);
c = _be.point(vhandles[2]);
n = (_be.has_face_normals() ?
_be.normal(fh) :
((c-b) % (a-b)).normalize());
// face normal
write_float(n[0], _out);
write_float(n[1], _out);
write_float(n[2], _out);
// face vertices
write_float(a[0], _out);
write_float(a[1], _out);
write_float(a[2], _out);
write_float(b[0], _out);
write_float(b[1], _out);
write_float(b[2], _out);
write_float(c[0], _out);
write_float(c[1], _out);
write_float(c[2], _out);
// space filler
write_short(0, _out);
}
else
omerr() << "[STLWriter] : Warning: Skipped non-triangle data!\n";
}
return true;
}
//-----------------------------------------------------------------------------
......
......@@ -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 <http://www.gnu.org/licenses/>. *
* *
\*===========================================================================*/
\*===========================================================================*/
/*===========================================================================*\
* *
* *
* $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;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment