- 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
This commit is contained in:
@@ -240,6 +240,66 @@ void write_double(double _d, FILE* _out, bool _swap)
|
|||||||
fwrite((char*)dc.c, 1, 8, _out);
|
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
|
#endif
|
||||||
|
|
||||||
//=============================================================================
|
//=============================================================================
|
||||||
|
|||||||
@@ -129,6 +129,21 @@ void write_float(float _f, FILE* _out, bool _swap=false);
|
|||||||
\c _swap is true */
|
\c _swap is true */
|
||||||
void write_double(double _d, FILE* _out, bool _swap=false);
|
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);
|
||||||
|
|
||||||
//@}
|
//@}
|
||||||
|
|
||||||
|
|||||||
@@ -120,7 +120,20 @@ bool
|
|||||||
_STLWriter_::
|
_STLWriter_::
|
||||||
write(std::ostream& _os, BaseExporter& _be, Options _opt) const
|
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;
|
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
|
bool
|
||||||
_STLWriter_::
|
_STLWriter_::
|
||||||
write_stlb(const std::string& _filename, BaseExporter& _be, Options /* _opt */) const
|
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;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|||||||
@@ -101,7 +101,9 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
bool write_stla(const std::string&, BaseExporter&, Options) const;
|
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(const std::string&, BaseExporter&, Options) const;
|
||||||
|
bool write_stlb(std::ostream&, BaseExporter&, Options) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user