From e0756969cc5af4e1c7c154f28577ee6f2ea63e97 Mon Sep 17 00:00:00 2001 From: eyal0 <109809+eyal0@users.noreply.github.com> Date: Mon, 29 Jun 2020 20:59:18 -0600 Subject: [PATCH] Put svg_writer in a different file --- Makefile.am | 2 ++ surface_vectorial.cpp | 71 ------------------------------------- svg_writer.cpp | 82 +++++++++++++++++++++++++++++++++++++++++++ svg_writer.hpp | 23 ++++++++++++ 4 files changed, 107 insertions(+), 71 deletions(-) create mode 100644 svg_writer.cpp create mode 100644 svg_writer.hpp diff --git a/Makefile.am b/Makefile.am index 60d0f98cb..9f1667103 100644 --- a/Makefile.am +++ b/Makefile.am @@ -45,6 +45,8 @@ pcb2gcode_SOURCES = \ options.cpp \ outline_bridges.hpp \ outline_bridges.cpp \ + svg_writer.hpp \ + svg_writer.cpp \ units.hpp \ unique_codes.hpp \ voronoi.hpp \ diff --git a/surface_vectorial.cpp b/surface_vectorial.cpp index 858b8c1c2..e5745e025 100644 --- a/surface_vectorial.cpp +++ b/surface_vectorial.cpp @@ -1013,74 +1013,3 @@ vector Surface_vectorial::offset_polygon( return polygons; } - -svg_writer::svg_writer(string filename, box_type_fp bounding_box) : - output_file(filename), - bounding_box(bounding_box) -{ - const coordinate_type_fp width = - (bounding_box.max_corner().x() - bounding_box.min_corner().x()) * SVG_PIX_PER_IN; - const coordinate_type_fp height = - (bounding_box.max_corner().y() - bounding_box.min_corner().y()) * SVG_PIX_PER_IN; - const coordinate_type_fp viewBox_width = - (bounding_box.max_corner().x() - bounding_box.min_corner().x()) * SVG_DOTS_PER_IN; - const coordinate_type_fp viewBox_height = - (bounding_box.max_corner().y() - bounding_box.min_corner().y()) * SVG_DOTS_PER_IN; - - //Some SVG readers does not behave well when viewBox is not specified - const string svg_dimensions = - str(boost::format("width=\"%1%\" height=\"%2%\" viewBox=\"0 0 %3% %4%\"") % width % height % viewBox_width % viewBox_height); - - mapper = unique_ptr> - (new bg::svg_mapper(output_file, viewBox_width, viewBox_height, svg_dimensions)); - mapper->add(bounding_box); -} - -template -void svg_writer::add(const multi_polygon_type_t& geometry, double opacity, bool stroke) -{ - string stroke_str = stroke ? "stroke:rgb(0,0,0);stroke-width:2" : ""; - - for (const auto& poly : geometry) - { - const unsigned int r = rand() % 256; - const unsigned int g = rand() % 256; - const unsigned int b = rand() % 256; - - multi_polygon_type_t new_bounding_box; - bg::convert(bounding_box, new_bounding_box); - - mapper->map(poly & new_bounding_box, - str(boost::format("fill-opacity:%f;fill:rgb(%u,%u,%u);" + stroke_str) % - opacity % r % g % b)); - } -} - -void svg_writer::add(const multi_linestring_type_fp& mls, coordinate_type_fp width, bool stroke) { - string stroke_str = stroke ? "stroke:rgb(0,0,0);stroke-width:2" : ""; - - for (const auto& ls : mls) { - const unsigned int r = rand() % 256; - const unsigned int g = rand() % 256; - const unsigned int b = rand() % 256; - - add(ls, width, r, g, b); - } -} - -void svg_writer::add(const linestring_type_fp& path, coordinate_type_fp width, unsigned int r, unsigned int g, unsigned int b) { - // Stroke the width of the path. - mapper->map(path, - str(boost::format("stroke:rgb(%u,%u,%u);stroke-width:%f;fill:none;" - "stroke-opacity:0.5;stroke-linecap:round;stroke-linejoin:round;") % r % g % b % (width * SVG_DOTS_PER_IN))); - // Stroke the center of the path. - mapper->map(path, - "stroke:rgb(0,0,0);stroke-width:1px;fill:none;" - "stroke-opacity:1;stroke-linecap:round;stroke-linejoin:round;"); -} - -void svg_writer::add(const multi_linestring_type_fp& path, coordinate_type_fp width, unsigned int r, unsigned int g, unsigned int b) { - for (const auto& p : path) { - add(p, width, r, g, b); - } -} diff --git a/svg_writer.cpp b/svg_writer.cpp new file mode 100644 index 000000000..2dbf87a5f --- /dev/null +++ b/svg_writer.cpp @@ -0,0 +1,82 @@ +#include +#include +#include +#include "geometry.hpp" +#include "bg_helpers.hpp" +#include "svg_writer.hpp" + +using std::string; +using std::unique_ptr; + +svg_writer::svg_writer(string filename, box_type_fp bounding_box) : + output_file(filename), + bounding_box(bounding_box) +{ + const coordinate_type_fp width = + (bounding_box.max_corner().x() - bounding_box.min_corner().x()) * SVG_PIX_PER_IN; + const coordinate_type_fp height = + (bounding_box.max_corner().y() - bounding_box.min_corner().y()) * SVG_PIX_PER_IN; + const coordinate_type_fp viewBox_width = + (bounding_box.max_corner().x() - bounding_box.min_corner().x()) * SVG_DOTS_PER_IN; + const coordinate_type_fp viewBox_height = + (bounding_box.max_corner().y() - bounding_box.min_corner().y()) * SVG_DOTS_PER_IN; + + //Some SVG readers does not behave well when viewBox is not specified + const string svg_dimensions = + str(boost::format("width=\"%1%\" height=\"%2%\" viewBox=\"0 0 %3% %4%\"") % width % height % viewBox_width % viewBox_height); + + mapper = unique_ptr> + (new bg::svg_mapper(output_file, viewBox_width, viewBox_height, svg_dimensions)); + mapper->add(bounding_box); +} + +template +void svg_writer::add(const multi_polygon_type_t& geometry, double opacity, bool stroke) +{ + string stroke_str = stroke ? "stroke:rgb(0,0,0);stroke-width:2" : ""; + + for (const auto& poly : geometry) + { + const unsigned int r = rand() % 256; + const unsigned int g = rand() % 256; + const unsigned int b = rand() % 256; + + multi_polygon_type_t new_bounding_box; + bg::convert(bounding_box, new_bounding_box); + + mapper->map(poly & new_bounding_box, + str(boost::format("fill-opacity:%f;fill:rgb(%u,%u,%u);" + stroke_str) % + opacity % r % g % b)); + } +} + +template void svg_writer::add(const multi_polygon_type_fp&, double, bool); + +void svg_writer::add(const multi_linestring_type_fp& mls, coordinate_type_fp width, bool stroke) { + string stroke_str = stroke ? "stroke:rgb(0,0,0);stroke-width:2" : ""; + + for (const auto& ls : mls) { + const unsigned int r = rand() % 256; + const unsigned int g = rand() % 256; + const unsigned int b = rand() % 256; + + add(ls, width, r, g, b); + } +} + +void svg_writer::add(const linestring_type_fp& path, coordinate_type_fp width, unsigned int r, unsigned int g, unsigned int b) { + // Stroke the width of the path. + mapper->map(path, + str(boost::format("stroke:rgb(%u,%u,%u);stroke-width:%f;fill:none;" + "stroke-opacity:0.5;stroke-linecap:round;stroke-linejoin:round;") % r % g % b % (width * SVG_DOTS_PER_IN))); + // Stroke the center of the path. + mapper->map(path, + "stroke:rgb(0,0,0);stroke-width:1px;fill:none;" + "stroke-opacity:1;stroke-linecap:round;stroke-linejoin:round;"); +} + +void svg_writer::add(const multi_linestring_type_fp& path, coordinate_type_fp width, unsigned int r, unsigned int g, unsigned int b) { + for (const auto& p : path) { + add(p, width, r, g, b); + } +} diff --git a/svg_writer.hpp b/svg_writer.hpp new file mode 100644 index 000000000..fb8b19a6c --- /dev/null +++ b/svg_writer.hpp @@ -0,0 +1,23 @@ +#ifndef SVG_WRITER_HPP +#define SVG_WRITER_HPP + +#include + +class svg_writer { + public: + svg_writer(std::string filename, box_type_fp bounding_box); + template + void add(const multi_polygon_type_t& geometry, double opacity, bool stroke); + void add(const multi_linestring_type_fp& mls, coordinate_type_fp width, bool stroke); + void add(const std::vector& geometries, double opacity, + int r = -1, int g = -1, int b = -1); + void add(const linestring_type_fp& paths, coordinate_type_fp width, unsigned int r, unsigned int g, unsigned int b); + void add(const multi_linestring_type_fp& paths, coordinate_type_fp width, unsigned int r, unsigned int g, unsigned int b); + + protected: + std::ofstream output_file; + const box_type_fp bounding_box; + std::unique_ptr > mapper; +}; + +#endif //SVG_WRITER_HPP