SVG export: Implemented flipping in Y axis. Now flipped by default

to match the 3D print paths view when looking at the print from the top.
This commit is contained in:
Vojtech Bubnik 2020-11-16 10:18:51 +01:00
parent 4d102ac8ca
commit 32362cff0b
2 changed files with 74 additions and 98 deletions

View file

@ -3,8 +3,6 @@
#include <boost/nowide/cstdio.hpp>
#define COORD(x) (unscale<float>((x))*10)
namespace Slic3r {
bool SVG::open(const char* afilename)
@ -33,8 +31,9 @@ bool SVG::open(const char* afilename, const BoundingBox &bbox, const coord_t bbo
this->f = boost::nowide::fopen(afilename, "w");
if (f == NULL)
return false;
float w = COORD(bbox.max(0) - bbox.min(0) + 2 * bbox_offset);
float h = COORD(bbox.max(1) - bbox.min(1) + 2 * bbox_offset);
float w = to_svg_coord(bbox.max(0) - bbox.min(0) + 2 * bbox_offset);
float h = to_svg_coord(bbox.max(1) - bbox.min(1) + 2 * bbox_offset);
this->height = h;
fprintf(this->f,
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN\" \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n"
@ -47,12 +46,11 @@ bool SVG::open(const char* afilename, const BoundingBox &bbox, const coord_t bbo
return true;
}
void
SVG::draw(const Line &line, std::string stroke, coordf_t stroke_width)
void SVG::draw(const Line &line, std::string stroke, coordf_t stroke_width)
{
fprintf(this->f,
" <line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" style=\"stroke: %s; stroke-width: %f\"",
COORD(line.a(0) - origin(0)), COORD(line.a(1) - origin(1)), COORD(line.b(0) - origin(0)), COORD(line.b(1) - origin(1)), stroke.c_str(), (stroke_width == 0) ? 1.f : COORD(stroke_width));
to_svg_x(line.a(0) - origin(0)), to_svg_y(line.a(1) - origin(1)), to_svg_x(line.b(0) - origin(0)), to_svg_y(line.b(1) - origin(1)), stroke.c_str(), (stroke_width == 0) ? 1.f : to_svg_coord(stroke_width));
if (this->arrows)
fprintf(this->f, " marker-end=\"url(#endArrow)\"");
fprintf(this->f, "/>\n");
@ -67,34 +65,31 @@ void SVG::draw(const ThickLine &line, const std::string &fill, const std::string
coordf_t db = coordf_t(0.5)*line.b_width/len;
fprintf(this->f,
" <polygon points=\"%f,%f %f,%f %f,%f %f,%f\" style=\"fill:%s; stroke: %s; stroke-width: %f\"/>\n",
COORD(line.a(0)-da*perp(0)-origin(0)),
COORD(line.a(1)-da*perp(1)-origin(1)),
COORD(line.b(0)-db*perp(0)-origin(0)),
COORD(line.b(1)-db*perp(1)-origin(1)),
COORD(line.b(0)+db*perp(0)-origin(0)),
COORD(line.b(1)+db*perp(1)-origin(1)),
COORD(line.a(0)+da*perp(0)-origin(0)),
COORD(line.a(1)+da*perp(1)-origin(1)),
to_svg_x(line.a(0)-da*perp(0)-origin(0)),
to_svg_y(line.a(1)-da*perp(1)-origin(1)),
to_svg_x(line.b(0)-db*perp(0)-origin(0)),
to_svg_y(line.b(1)-db*perp(1)-origin(1)),
to_svg_x(line.b(0)+db*perp(0)-origin(0)),
to_svg_y(line.b(1)+db*perp(1)-origin(1)),
to_svg_x(line.a(0)+da*perp(0)-origin(0)),
to_svg_y(line.a(1)+da*perp(1)-origin(1)),
fill.c_str(), stroke.c_str(),
(stroke_width == 0) ? 1.f : COORD(stroke_width));
(stroke_width == 0) ? 1.f : to_svg_coord(stroke_width));
}
void
SVG::draw(const Lines &lines, std::string stroke, coordf_t stroke_width)
void SVG::draw(const Lines &lines, std::string stroke, coordf_t stroke_width)
{
for (Lines::const_iterator it = lines.begin(); it != lines.end(); ++it)
this->draw(*it, stroke, stroke_width);
for (const Line &l : lines)
this->draw(l, stroke, stroke_width);
}
void
SVG::draw(const IntersectionLines &lines, std::string stroke)
void SVG::draw(const IntersectionLines &lines, std::string stroke)
{
for (IntersectionLines::const_iterator it = lines.begin(); it != lines.end(); ++it)
this->draw((Line)*it, stroke);
for (const IntersectionLine &il : lines)
this->draw((Line)il, stroke);
}
void
SVG::draw(const ExPolygon &expolygon, std::string fill, const float fill_opacity)
void SVG::draw(const ExPolygon &expolygon, std::string fill, const float fill_opacity)
{
this->fill = fill;
@ -106,8 +101,7 @@ SVG::draw(const ExPolygon &expolygon, std::string fill, const float fill_opacity
this->path(d, true, 0, fill_opacity);
}
void
SVG::draw_outline(const ExPolygon &expolygon, std::string stroke_outer, std::string stroke_holes, coordf_t stroke_width)
void SVG::draw_outline(const ExPolygon &expolygon, std::string stroke_outer, std::string stroke_holes, coordf_t stroke_width)
{
draw_outline(expolygon.contour, stroke_outer, stroke_width);
for (Polygons::const_iterator it = expolygon.holes.begin(); it != expolygon.holes.end(); ++ it) {
@ -115,83 +109,71 @@ SVG::draw_outline(const ExPolygon &expolygon, std::string stroke_outer, std::str
}
}
void
SVG::draw(const ExPolygons &expolygons, std::string fill, const float fill_opacity)
void SVG::draw(const ExPolygons &expolygons, std::string fill, const float fill_opacity)
{
for (ExPolygons::const_iterator it = expolygons.begin(); it != expolygons.end(); ++it)
this->draw(*it, fill, fill_opacity);
}
void
SVG::draw_outline(const ExPolygons &expolygons, std::string stroke_outer, std::string stroke_holes, coordf_t stroke_width)
void SVG::draw_outline(const ExPolygons &expolygons, std::string stroke_outer, std::string stroke_holes, coordf_t stroke_width)
{
for (ExPolygons::const_iterator it = expolygons.begin(); it != expolygons.end(); ++ it)
draw_outline(*it, stroke_outer, stroke_holes, stroke_width);
}
void
SVG::draw(const Surface &surface, std::string fill, const float fill_opacity)
void SVG::draw(const Surface &surface, std::string fill, const float fill_opacity)
{
draw(surface.expolygon, fill, fill_opacity);
}
void
SVG::draw_outline(const Surface &surface, std::string stroke_outer, std::string stroke_holes, coordf_t stroke_width)
void SVG::draw_outline(const Surface &surface, std::string stroke_outer, std::string stroke_holes, coordf_t stroke_width)
{
draw_outline(surface.expolygon, stroke_outer, stroke_holes, stroke_width);
}
void
SVG::draw(const Surfaces &surfaces, std::string fill, const float fill_opacity)
void SVG::draw(const Surfaces &surfaces, std::string fill, const float fill_opacity)
{
for (Surfaces::const_iterator it = surfaces.begin(); it != surfaces.end(); ++it)
this->draw(*it, fill, fill_opacity);
}
void
SVG::draw_outline(const Surfaces &surfaces, std::string stroke_outer, std::string stroke_holes, coordf_t stroke_width)
void SVG::draw_outline(const Surfaces &surfaces, std::string stroke_outer, std::string stroke_holes, coordf_t stroke_width)
{
for (Surfaces::const_iterator it = surfaces.begin(); it != surfaces.end(); ++ it)
draw_outline(*it, stroke_outer, stroke_holes, stroke_width);
}
void
SVG::draw(const SurfacesPtr &surfaces, std::string fill, const float fill_opacity)
void SVG::draw(const SurfacesPtr &surfaces, std::string fill, const float fill_opacity)
{
for (SurfacesPtr::const_iterator it = surfaces.begin(); it != surfaces.end(); ++it)
this->draw(*(*it), fill, fill_opacity);
}
void
SVG::draw_outline(const SurfacesPtr &surfaces, std::string stroke_outer, std::string stroke_holes, coordf_t stroke_width)
void SVG::draw_outline(const SurfacesPtr &surfaces, std::string stroke_outer, std::string stroke_holes, coordf_t stroke_width)
{
for (SurfacesPtr::const_iterator it = surfaces.begin(); it != surfaces.end(); ++ it)
draw_outline(*(*it), stroke_outer, stroke_holes, stroke_width);
}
void
SVG::draw(const Polygon &polygon, std::string fill)
void SVG::draw(const Polygon &polygon, std::string fill)
{
this->fill = fill;
this->path(this->get_path_d(polygon, true), !fill.empty(), 0, 1.f);
}
void
SVG::draw(const Polygons &polygons, std::string fill)
void SVG::draw(const Polygons &polygons, std::string fill)
{
for (Polygons::const_iterator it = polygons.begin(); it != polygons.end(); ++it)
this->draw(*it, fill);
}
void
SVG::draw(const Polyline &polyline, std::string stroke, coordf_t stroke_width)
void SVG::draw(const Polyline &polyline, std::string stroke, coordf_t stroke_width)
{
this->stroke = stroke;
this->path(this->get_path_d(polyline, false), false, stroke_width, 1.f);
}
void
SVG::draw(const Polylines &polylines, std::string stroke, coordf_t stroke_width)
void SVG::draw(const Polylines &polylines, std::string stroke, coordf_t stroke_width)
{
for (Polylines::const_iterator it = polylines.begin(); it != polylines.end(); ++it)
this->draw(*it, stroke, stroke_width);
@ -203,73 +185,64 @@ void SVG::draw(const ThickLines &thicklines, const std::string &fill, const std:
this->draw(*it, fill, stroke, stroke_width);
}
void
SVG::draw(const ThickPolylines &polylines, const std::string &stroke, coordf_t stroke_width)
void SVG::draw(const ThickPolylines &polylines, const std::string &stroke, coordf_t stroke_width)
{
for (ThickPolylines::const_iterator it = polylines.begin(); it != polylines.end(); ++it)
this->draw((Polyline)*it, stroke, stroke_width);
}
void
SVG::draw(const ThickPolylines &thickpolylines, const std::string &fill, const std::string &stroke, coordf_t stroke_width)
void SVG::draw(const ThickPolylines &thickpolylines, const std::string &fill, const std::string &stroke, coordf_t stroke_width)
{
for (ThickPolylines::const_iterator it = thickpolylines.begin(); it != thickpolylines.end(); ++ it)
draw(it->thicklines(), fill, stroke, stroke_width);
}
void
SVG::draw(const Point &point, std::string fill, coord_t iradius)
void SVG::draw(const Point &point, std::string fill, coord_t iradius)
{
float radius = (iradius == 0) ? 3.f : COORD(iradius);
float radius = (iradius == 0) ? 3.f : to_svg_coord(iradius);
std::ostringstream svg;
svg << " <circle cx=\"" << COORD(point(0) - origin(0)) << "\" cy=\"" << COORD(point(1) - origin(1))
svg << " <circle cx=\"" << to_svg_x(point(0) - origin(0)) << "\" cy=\"" << to_svg_y(point(1) - origin(1))
<< "\" r=\"" << radius << "\" "
<< "style=\"stroke: none; fill: " << fill << "\" />";
fprintf(this->f, "%s\n", svg.str().c_str());
}
void
SVG::draw(const Points &points, std::string fill, coord_t radius)
void SVG::draw(const Points &points, std::string fill, coord_t radius)
{
for (Points::const_iterator it = points.begin(); it != points.end(); ++it)
this->draw(*it, fill, radius);
}
void
SVG::draw(const ClipperLib::Path &polygon, double scale, std::string stroke, coordf_t stroke_width)
void SVG::draw(const ClipperLib::Path &polygon, double scale, std::string stroke, coordf_t stroke_width)
{
this->stroke = stroke;
this->path(this->get_path_d(polygon, scale, true), false, stroke_width, 1.f);
}
void
SVG::draw(const ClipperLib::Paths &polygons, double scale, std::string stroke, coordf_t stroke_width)
void SVG::draw(const ClipperLib::Paths &polygons, double scale, std::string stroke, coordf_t stroke_width)
{
for (ClipperLib::Paths::const_iterator it = polygons.begin(); it != polygons.end(); ++ it)
draw(*it, scale, stroke, stroke_width);
}
void
SVG::draw_outline(const Polygon &polygon, std::string stroke, coordf_t stroke_width)
void SVG::draw_outline(const Polygon &polygon, std::string stroke, coordf_t stroke_width)
{
this->stroke = stroke;
this->path(this->get_path_d(polygon, true), false, stroke_width, 1.f);
}
void
SVG::draw_outline(const Polygons &polygons, std::string stroke, coordf_t stroke_width)
void SVG::draw_outline(const Polygons &polygons, std::string stroke, coordf_t stroke_width)
{
for (Polygons::const_iterator it = polygons.begin(); it != polygons.end(); ++ it)
draw_outline(*it, stroke, stroke_width);
}
void
SVG::path(const std::string &d, bool fill, coordf_t stroke_width, const float fill_opacity)
void SVG::path(const std::string &d, bool fill, coordf_t stroke_width, const float fill_opacity)
{
float lineWidth = 0.f;
if (! fill)
lineWidth = (stroke_width == 0) ? 2.f : COORD(stroke_width);
lineWidth = (stroke_width == 0) ? 2.f : to_svg_coord(stroke_width);
fprintf(
this->f,
@ -283,27 +256,25 @@ SVG::path(const std::string &d, bool fill, coordf_t stroke_width, const float fi
);
}
std::string
SVG::get_path_d(const MultiPoint &mp, bool closed) const
std::string SVG::get_path_d(const MultiPoint &mp, bool closed) const
{
std::ostringstream d;
d << "M ";
for (Points::const_iterator p = mp.points.begin(); p != mp.points.end(); ++p) {
d << COORD((*p)(0) - origin(0)) << " ";
d << COORD((*p)(1) - origin(1)) << " ";
d << to_svg_x((*p)(0) - origin(0)) << " ";
d << to_svg_y((*p)(1) - origin(1)) << " ";
}
if (closed) d << "z";
return d.str();
}
std::string
SVG::get_path_d(const ClipperLib::Path &path, double scale, bool closed) const
std::string SVG::get_path_d(const ClipperLib::Path &path, double scale, bool closed) const
{
std::ostringstream d;
d << "M ";
for (ClipperLib::Path::const_iterator p = path.begin(); p != path.end(); ++p) {
d << COORD(scale * p->X - origin(0)) << " ";
d << COORD(scale * p->Y - origin(1)) << " ";
d << to_svg_x(scale * p->X - origin(0)) << " ";
d << to_svg_y(scale * p->Y - origin(1)) << " ";
}
if (closed) d << "z";
return d.str();
@ -313,8 +284,8 @@ void SVG::draw_text(const Point &pt, const char *text, const char *color)
{
fprintf(this->f,
"<text x=\"%f\" y=\"%f\" font-family=\"sans-serif\" font-size=\"20px\" fill=\"%s\">%s</text>",
COORD(pt(0)-origin(0)),
COORD(pt(1)-origin(1)),
to_svg_x(pt(0)-origin(0)),
to_svg_y(pt(1)-origin(1)),
color, text);
}
@ -322,18 +293,17 @@ void SVG::draw_legend(const Point &pt, const char *text, const char *color)
{
fprintf(this->f,
"<circle cx=\"%f\" cy=\"%f\" r=\"10\" fill=\"%s\"/>",
COORD(pt(0)-origin(0)),
COORD(pt(1)-origin(1)),
to_svg_x(pt(0)-origin(0)),
to_svg_y(pt(1)-origin(1)),
color);
fprintf(this->f,
"<text x=\"%f\" y=\"%f\" font-family=\"sans-serif\" font-size=\"10px\" fill=\"%s\">%s</text>",
COORD(pt(0)-origin(0)) + 20.f,
COORD(pt(1)-origin(1)),
to_svg_x(pt(0)-origin(0)) + 20.f,
to_svg_y(pt(1)-origin(1)),
"black", text);
}
void
SVG::Close()
void SVG::Close()
{
fprintf(this->f, "</svg>\n");
fclose(this->f);

View file

@ -16,27 +16,28 @@ public:
bool arrows;
std::string fill, stroke;
Point origin;
bool flipY;
float height;
bool flipY;
SVG(const char* afilename) :
arrows(false), fill("grey"), stroke("black"), filename(afilename), flipY(false)
{ open(filename); }
SVG(const char* afilename, const BoundingBox &bbox, const coord_t bbox_offset = scale_(1.), bool aflipY = false) :
arrows(false), fill("grey"), stroke("black"), filename(afilename), origin(bbox.min - Point(bbox_offset, bbox_offset)), flipY(aflipY)
{ open(filename, bbox, bbox_offset, aflipY); }
SVG(const char* afilename, const BoundingBox &bbox, const coord_t bbox_offset = scale_(1.), bool flipY = true) :
arrows(false), fill("grey"), stroke("black"), filename(afilename), origin(bbox.min - Point(bbox_offset, bbox_offset)), flipY(flipY)
{ open(filename, bbox, bbox_offset, flipY); }
SVG(const std::string &filename) :
arrows(false), fill("grey"), stroke("black"), filename(filename), flipY(false)
{ open(filename); }
SVG(const std::string &filename, const BoundingBox &bbox, const coord_t bbox_offset = scale_(1.), bool aflipY = false) :
arrows(false), fill("grey"), stroke("black"), filename(filename), origin(bbox.min - Point(bbox_offset, bbox_offset)), flipY(aflipY)
{ open(filename, bbox, bbox_offset, aflipY); }
SVG(const std::string &filename, const BoundingBox &bbox, const coord_t bbox_offset = scale_(1.), bool flipY = true) :
arrows(false), fill("grey"), stroke("black"), filename(filename), origin(bbox.min - Point(bbox_offset, bbox_offset)), flipY(flipY)
{ open(filename, bbox, bbox_offset, flipY); }
~SVG() { if (f != NULL) Close(); }
bool open(const char* filename);
bool open(const char* filename, const BoundingBox &bbox, const coord_t bbox_offset = scale_(1.), bool flipY = false);
bool open(const char* filename, const BoundingBox &bbox, const coord_t bbox_offset = scale_(1.), bool flipY = true);
bool open(const std::string &filename)
{ return open(filename.c_str()); }
bool open(const std::string &filename, const BoundingBox &bbox, const coord_t bbox_offset = scale_(1.), bool flipY = false)
bool open(const std::string &filename, const BoundingBox &bbox, const coord_t bbox_offset = scale_(1.), bool flipY = true)
{ return open(filename.c_str(), bbox, bbox_offset, flipY); }
void draw(const Line &line, std::string stroke = "black", coordf_t stroke_width = 0);
@ -127,6 +128,11 @@ public:
};
static void export_expolygons(const char *path, const std::vector<std::pair<Slic3r::ExPolygons, ExPolygonAttributes>> &expolygons_with_attributes);
private:
static float to_svg_coord(float x) throw() { return unscale<float>(x) * 10.f; }
static float to_svg_x(float x) throw() { return to_svg_coord(x); }
float to_svg_y(float x) const throw() { return flipY ? this->height - to_svg_coord(x) : to_svg_coord(x); }
};
}