diff --git a/src/cgalutils-polyhedron.cc b/src/cgalutils-polyhedron.cc index 59a2ec71..c821f541 100644 --- a/src/cgalutils-polyhedron.cc +++ b/src/cgalutils-polyhedron.cc @@ -296,6 +296,63 @@ namespace CGALUtils { template bool createPolySetFromPolyhedron(const CGAL::Polyhedron_3 &p, PolySet &ps); template bool createPolySetFromPolyhedron(const CGAL::Polyhedron_3 > &p, PolySet &ps); + class Polyhedron_writer { + std::ostream *out; + bool firstv; + std::vector indices; + public: + Polyhedron_writer() {} + void write_header(std::ostream &stream, + std::size_t vertices, + std::size_t halfedges, + std::size_t facets, + bool normals = false) { + this->out = &stream; + *out << "polyhedron(points=["; + firstv = true; + } + void write_footer() { + *out << "]);" << std::endl; + } + void write_vertex( const double& x, const double& y, const double& z) { + *out << (firstv ? "" : ",") << '[' << x << ',' << y << ',' << z << ']'; + firstv = false; + } + void write_facet_header() { + *out << "], faces=["; + firstv = true; + } + void write_facet_begin( std::size_t no) { + *out << (firstv ? "" : ",") << '['; + indices.clear(); + firstv = false; + } + void write_facet_vertex_index( std::size_t index) { + indices.push_back(index); + } + void write_facet_end() { + bool firsti = true; + BOOST_REVERSE_FOREACH(int i, indices) { + *out << (firsti ? "" : ",") << i; + firsti = false; + } + *out << ']'; + } + }; + + template + std::string printPolyhedron(const Polyhedron &p) { + std::stringstream sstream; + sstream.precision(20); + + Polyhedron_writer writer; + generic_print_polyhedron(sstream, p, writer); + + return sstream.str(); + } + + template std::string printPolyhedron(const CGAL_Polyhedron &p); + }; // namespace CGALUtils #endif /* ENABLE_CGAL */ diff --git a/src/cgalutils.h b/src/cgalutils.h index c8eb69db..92c909dd 100644 --- a/src/cgalutils.h +++ b/src/cgalutils.h @@ -21,6 +21,7 @@ namespace CGALUtils { bool is_approximately_convex(const PolySet &ps); Geometry const* applyMinkowski(const Geometry::ChildList &children); + template std::string printPolyhedron(const Polyhedron &p); template bool createPolySetFromPolyhedron(const Polyhedron &p, PolySet &ps); template bool createPolyhedronFromPolySet(const PolySet &ps, Polyhedron &p); template