From d2ca4563e8049cdd0e804fcc73a50f3555758812 Mon Sep 17 00:00:00 2001 From: Vojtech Bubnik Date: Mon, 22 Feb 2021 17:31:26 +0100 Subject: [PATCH] Extended SVG::export_expolygons() with legend. --- src/libslic3r/SVG.cpp | 50 +++++++++++++++++++++++++++++++++++++------ src/libslic3r/SVG.hpp | 44 ++++++++++++++++++++++++++++++++++--- 2 files changed, 84 insertions(+), 10 deletions(-) diff --git a/src/libslic3r/SVG.cpp b/src/libslic3r/SVG.cpp index da30a197f..7308d7e50 100644 --- a/src/libslic3r/SVG.cpp +++ b/src/libslic3r/SVG.cpp @@ -319,31 +319,67 @@ void SVG::export_expolygons(const char *path, const BoundingBox &bbox, const Sli svg.Close(); } +// Paint the expolygons in the order they are presented, thus the latter overwrites the former expolygon. +// 1) Paint all areas with the provided ExPolygonAttributes::color_fill and ExPolygonAttributes::fill_opacity. +// 2) Optionally paint outlines of the areas if ExPolygonAttributes::outline_width > 0. +// Paint with ExPolygonAttributes::color_contour and ExPolygonAttributes::color_holes. +// If color_contour is empty, color_fill is used. If color_hole is empty, color_contour is used. +// 3) Optionally paint points of all expolygon contours with ExPolygonAttributes::radius_points if radius_points > 0. +// 4) Paint ExPolygonAttributes::legend into legend using the ExPolygonAttributes::color_fill if legend is not empty. void SVG::export_expolygons(const char *path, const std::vector> &expolygons_with_attributes) { if (expolygons_with_attributes.empty()) return; + size_t num_legend = std::count_if(expolygons_with_attributes.begin(), expolygons_with_attributes.end(), [](const auto &v){ return ! v.second.legend.empty(); }); + // Format in num_columns. + size_t num_columns = 3; + // Width of the column. + coord_t step_x = scale_(20.); + Point legend_size(scale_(1.) + num_columns * step_x, scale_(0.4 + 1.3 * (num_legend + num_columns - 1) / num_columns)); + BoundingBox bbox = get_extents(expolygons_with_attributes.front().first); for (size_t i = 0; i < expolygons_with_attributes.size(); ++ i) bbox.merge(get_extents(expolygons_with_attributes[i].first)); + // Legend y. + coord_t pos_y = bbox.max.y() + scale_(1.5); + bbox.merge(Point(std::max(bbox.min.x() + legend_size.x(), bbox.max.x()), bbox.max.y() + legend_size.y())); SVG svg(path, bbox); for (const auto &exp_with_attr : expolygons_with_attributes) svg.draw(exp_with_attr.first, exp_with_attr.second.color_fill, exp_with_attr.second.fill_opacity); for (const auto &exp_with_attr : expolygons_with_attributes) { - std::string color_contour = exp_with_attr.second.color_contour; - if (color_contour.empty()) - color_contour = exp_with_attr.second.color_fill; - std::string color_holes = exp_with_attr.second.color_holes; - if (color_holes.empty()) - color_holes = color_contour; - svg.draw_outline(exp_with_attr.first, color_contour, color_holes, exp_with_attr.second.outline_width); + if (exp_with_attr.second.outline_width > 0) { + std::string color_contour = exp_with_attr.second.color_contour; + if (color_contour.empty()) + color_contour = exp_with_attr.second.color_fill; + std::string color_holes = exp_with_attr.second.color_holes; + if (color_holes.empty()) + color_holes = color_contour; + svg.draw_outline(exp_with_attr.first, color_contour, color_holes, exp_with_attr.second.outline_width); + } } for (const auto &exp_with_attr : expolygons_with_attributes) if (exp_with_attr.second.radius_points > 0) for (const ExPolygon &expoly : exp_with_attr.first) svg.draw((Points)expoly, exp_with_attr.second.color_points, exp_with_attr.second.radius_points); + + // Export legend. + // 1st row + coord_t pos_x0 = bbox.min.x() + scale_(1.); + coord_t pos_x = pos_x0; + size_t i_legend = 0; + for (const auto &exp_with_attr : expolygons_with_attributes) { + if (! exp_with_attr.second.legend.empty()) { + svg.draw_legend(Point(pos_x, pos_y), exp_with_attr.second.legend.c_str(), exp_with_attr.second.color_fill.c_str()); + if ((++ i_legend) % num_columns == 0) { + pos_x = pos_x0; + pos_y += scale_(1.3); + } else { + pos_x += step_x; + } + } + } svg.Close(); } diff --git a/src/libslic3r/SVG.hpp b/src/libslic3r/SVG.hpp index 1ebd8d918..da9dca093 100644 --- a/src/libslic3r/SVG.hpp +++ b/src/libslic3r/SVG.hpp @@ -102,7 +102,7 @@ public: ExPolygonAttributes(color, color, color) {} ExPolygonAttributes( - const std::string &color_fill, + const std::string &color_fill, const std::string &color_contour, const std::string &color_holes, const coord_t outline_width = scale_(0.05), @@ -118,16 +118,54 @@ public: radius_points (radius_points) {} + ExPolygonAttributes( + const std::string &legend, + const std::string &color_fill, + const std::string &color_contour, + const std::string &color_holes, + const coord_t outline_width = scale_(0.05), + const float fill_opacity = 0.5f, + const std::string &color_points = "black", + const coord_t radius_points = 0) : + legend (legend), + color_fill (color_fill), + color_contour (color_contour), + color_holes (color_holes), + outline_width (outline_width), + fill_opacity (fill_opacity), + color_points (color_points), + radius_points (radius_points) + {} + + ExPolygonAttributes( + const std::string &legend, + const std::string &color_fill, + const float fill_opacity) : + legend (legend), + color_fill (color_fill), + fill_opacity (fill_opacity) + {} + + std::string legend; std::string color_fill; std::string color_contour; std::string color_holes; std::string color_points; - coord_t outline_width; + coord_t outline_width { 0 }; float fill_opacity; - coord_t radius_points; + coord_t radius_points { 0 }; }; + // Paint the expolygons in the order they are presented, thus the latter overwrites the former expolygon. + // 1) Paint all areas with the provided ExPolygonAttributes::color_fill and ExPolygonAttributes::fill_opacity. + // 2) Optionally paint outlines of the areas if ExPolygonAttributes::outline_width > 0. + // Paint with ExPolygonAttributes::color_contour and ExPolygonAttributes::color_holes. + // If color_contour is empty, color_fill is used. If color_hole is empty, color_contour is used. + // 3) Optionally paint points of all expolygon contours with ExPolygonAttributes::radius_points if radius_points > 0. + // 4) Paint ExPolygonAttributes::legend into legend using the ExPolygonAttributes::color_fill if legend is not empty. static void export_expolygons(const char *path, const std::vector> &expolygons_with_attributes); + static void export_expolygons(const std::string &path, const std::vector> &expolygons_with_attributes) + { export_expolygons(path.c_str(), expolygons_with_attributes); } private: static float to_svg_coord(float x) throw() { return unscale(x) * 10.f; }