From 942443429e471f6066c9a1350d425619558e1128 Mon Sep 17 00:00:00 2001 From: Filip Sykala Date: Wed, 24 Mar 2021 09:39:17 +0100 Subject: [PATCH] =?UTF-8?q?=EF=BB=BFSeparate=20draw=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SLA/SupportIslands/LineUtils.cpp | 39 +++++++++++++++++++ .../SLA/SupportIslands/LineUtils.hpp | 19 +++++++++ .../SLA/SupportIslands/PolygonUtils.cpp | 22 ++++++++++- .../SLA/SupportIslands/PolygonUtils.hpp | 8 ++++ .../SupportIslands/SampleConfigFactory.hpp | 1 + .../SLA/SupportIslands/SampleIslandUtils.cpp | 11 +++++- .../SLA/SupportIslands/SampleIslandUtils.hpp | 4 ++ .../SLA/SupportIslands/VoronoiGraphUtils.cpp | 30 +++++++------- .../SLA/SupportIslands/VoronoiGraphUtils.hpp | 5 +++ 9 files changed, 123 insertions(+), 16 deletions(-) diff --git a/src/libslic3r/SLA/SupportIslands/LineUtils.cpp b/src/libslic3r/SLA/SupportIslands/LineUtils.cpp index 99081b4865..4a79906205 100644 --- a/src/libslic3r/SLA/SupportIslands/LineUtils.cpp +++ b/src/libslic3r/SLA/SupportIslands/LineUtils.cpp @@ -222,4 +222,43 @@ std::tuple LineUtils::get_param(const Linef &line) double b = normal.y(); double c = -a * line.a.x() - b * line.a.y(); return {a, b, c}; +} + +void LineUtils::draw(SVG & svg, + const Line &line, + const char *color, + coordf_t stroke_width, + const char *name, + bool side_points, + const char *color_a, + const char *color_b) +{ + svg.draw(line, color, stroke_width); + bool use_name = name != nullptr; + if (use_name) { + Point middle = line.a/2 + line.b/2; + svg.draw_text(middle, name, color); + } + if (side_points) { + std::string name_a = (use_name) ? "A" : (std::string("A_") + name); + std::string name_b = (use_name) ? "B" : (std::string("B_") + name); + svg.draw_text(line.a, name_a.c_str(), color_a); + svg.draw_text(line.b, name_b.c_str(), color_b); + } +} + +void LineUtils::draw(SVG & svg, + const Lines &lines, + const char * color, + coordf_t stroke_width, + bool ord, + bool side_points, + const char * color_a, + const char * color_b) +{ + for (const auto &line : lines) { + draw(svg, line, color, stroke_width, + (ord) ? std::to_string(&line - &lines.front()).c_str() : nullptr, + side_points, color_a, color_b); + } } \ No newline at end of file diff --git a/src/libslic3r/SLA/SupportIslands/LineUtils.hpp b/src/libslic3r/SLA/SupportIslands/LineUtils.hpp index 0f7338158a..3e3a1102e7 100644 --- a/src/libslic3r/SLA/SupportIslands/LineUtils.hpp +++ b/src/libslic3r/SLA/SupportIslands/LineUtils.hpp @@ -3,7 +3,9 @@ #include #include +#include #include +#include namespace Slic3r::sla { @@ -81,6 +83,23 @@ public: /// a, b, c static std::tuple get_param(const Line &line); static std::tuple get_param(const Linef &line); + + static void draw(SVG & svg, + const Line &line, + const char *color = "gray", + coordf_t stroke_width = 0, + const char *name = nullptr, + bool side_points = false, + const char *color_a = "lightgreen", + const char *color_b = "lightblue"); + static void draw(SVG & svg, + const Lines &lines, + const char *color = "gray", + coordf_t stroke_width = 0, + bool ord = false, // write order as text + bool side_points = false, + const char *color_a = "lightgreen", + const char *color_b = "lightblue"); }; } // namespace Slic3r::sla diff --git a/src/libslic3r/SLA/SupportIslands/PolygonUtils.cpp b/src/libslic3r/SLA/SupportIslands/PolygonUtils.cpp index 91e1fc032c..ef664d02b2 100644 --- a/src/libslic3r/SLA/SupportIslands/PolygonUtils.cpp +++ b/src/libslic3r/SLA/SupportIslands/PolygonUtils.cpp @@ -63,4 +63,24 @@ bool PolygonUtils::is_ccw(const Polygon &polygon, const Point ¢er) { prev = &point; } return true; -} \ No newline at end of file +} + +bool PolygonUtils::is_not_self_intersect(const Polygon &polygon, + const Point & center) +{ + auto get_angle = [¢er](const Point &point) { + Point diff_point = point - center; + return atan2(diff_point.y(), diff_point.x()); + }; + bool found_circle_end = false; // only one can be on polygon + double prev_angle = get_angle(polygon.points.back()); + for (const Point &point : polygon.points) { + double angle = get_angle(point); + if (angle < prev_angle) { + if (found_circle_end) return false; + found_circle_end = true; + } + prev_angle = angle; + } + return true; +} diff --git a/src/libslic3r/SLA/SupportIslands/PolygonUtils.hpp b/src/libslic3r/SLA/SupportIslands/PolygonUtils.hpp index 3f4495e177..314e7257d3 100644 --- a/src/libslic3r/SLA/SupportIslands/PolygonUtils.hpp +++ b/src/libslic3r/SLA/SupportIslands/PolygonUtils.hpp @@ -71,6 +71,14 @@ public: /// center point inside polygon /// True when all points in polygon are CCW with center static bool is_ccw(const Polygon &polygon, const Point ¢er); + + /// + /// ! Only for polygon around point, like Voronoi diagram cell + /// + /// Polygon to check + /// Center inside polygon, points create circle around center + /// True when valid without self intersection otherwise FALSE + static bool is_not_self_intersect(const Polygon &polygon, const Point ¢er); }; } // namespace Slic3r::sla #endif // slic3r_SLA_SuppotstIslands_PolygonUtils_hpp_ diff --git a/src/libslic3r/SLA/SupportIslands/SampleConfigFactory.hpp b/src/libslic3r/SLA/SupportIslands/SampleConfigFactory.hpp index 64dce6e76d..63ed0e4c6c 100644 --- a/src/libslic3r/SLA/SupportIslands/SampleConfigFactory.hpp +++ b/src/libslic3r/SLA/SupportIslands/SampleConfigFactory.hpp @@ -16,6 +16,7 @@ public: // factory method to iniciate config static SampleConfig create(const SupportPointGenerator::Config &config) { + // TODO: find valid params !!!! SampleConfig result; result.max_distance = 100. * config.head_diameter; result.head_radius = config.head_diameter / 2; diff --git a/src/libslic3r/SLA/SupportIslands/SampleIslandUtils.cpp b/src/libslic3r/SLA/SupportIslands/SampleIslandUtils.cpp index 8afe0614b6..06f2ce9399 100644 --- a/src/libslic3r/SLA/SupportIslands/SampleIslandUtils.cpp +++ b/src/libslic3r/SLA/SupportIslands/SampleIslandUtils.cpp @@ -13,7 +13,7 @@ #include // allign -// comment to enable assert() +// comment definition of NDEBUG to enable assert() // #define NDEBUG #include @@ -191,6 +191,13 @@ Slic3r::Points SampleIslandUtils::to_points(const SupportIslandPoints &support_p return VectorUtils::transform(support_points, transform_func); } +std::vector SampleIslandUtils::to_points_f(const SupportIslandPoints &support_points) +{ + std::function transform_func = + [](const SupportIslandPoint &p) { return p.point.cast(); }; + return VectorUtils::transform(support_points, transform_func); +} + void SampleIslandUtils::align_samples(SupportIslandPoints &samples, const ExPolygon & island, const SampleConfig & config) @@ -250,8 +257,8 @@ coord_t SampleIslandUtils::align_once(SupportIslandPoints &samples, break; } } - Point center = island_cell->centroid(); assert(island_cell != nullptr); + Point center = island_cell->centroid(); assert(is_points_in_distance(center, island_cell->points, config.max_distance)); #ifdef VISUALIZE_SAMPLE_ISLAND_UTILS_ALIGN_ONCE svg.draw(polygon, "lightgray"); diff --git a/src/libslic3r/SLA/SupportIslands/SampleIslandUtils.hpp b/src/libslic3r/SLA/SupportIslands/SampleIslandUtils.hpp index 63adfcfd0b..edd51bc8f5 100644 --- a/src/libslic3r/SLA/SupportIslands/SampleIslandUtils.hpp +++ b/src/libslic3r/SLA/SupportIslands/SampleIslandUtils.hpp @@ -129,7 +129,11 @@ public: const SampleConfig & config, VoronoiGraph::ExPath &longest_path); + /// + /// Transform support point to slicer points + /// static Slic3r::Points to_points(const SupportIslandPoints &support_points); + static std::vector to_points_f(const SupportIslandPoints &support_points); /// /// keep same distances between support points diff --git a/src/libslic3r/SLA/SupportIslands/VoronoiGraphUtils.cpp b/src/libslic3r/SLA/SupportIslands/VoronoiGraphUtils.cpp index 7ec0fd9129..e41d7a1314 100644 --- a/src/libslic3r/SLA/SupportIslands/VoronoiGraphUtils.cpp +++ b/src/libslic3r/SLA/SupportIslands/VoronoiGraphUtils.cpp @@ -12,6 +12,10 @@ #include +// comment definition of NDEBUG to enable assert() +// #define NDEBUG +#include + //#define SLA_CELL_2_POLYGON_DEBUG using namespace Slic3r::sla; @@ -144,6 +148,7 @@ std::optional VoronoiGraphUtils::to_line( return Line(segment->a.cast(), segment->b.cast()); } + Slic3r::Polygon VoronoiGraphUtils::to_polygon(const Lines &lines, const Point ¢er, double maximal_distance, @@ -196,21 +201,10 @@ Slic3r::Polygon VoronoiGraphUtils::to_polygon(const Lines &lines, points.push_back(p2); } Polygon polygon(points); + //if (!polygon.contains(center)) draw(polygon, lines, center); assert(polygon.is_valid()); - if (!polygon.contains(center)) { - SVG svg("bad_polygon.svg", {polygon.points}); - svg.draw(polygon, "orange"); - svg.draw(lines, "red"); - int counter = 0; - for (auto &line : lines) { - ++counter; - svg.draw_text(line.a, ("A"+std::to_string(counter)).c_str(), "lightgreen"); - svg.draw_text(line.b, ("B" + std::to_string(counter)).c_str(), "lightblue"); - } - svg.draw(center); - } assert(polygon.contains(center)); - assert(PolygonUtils::is_ccw(polygon, center)); + assert(PolygonUtils::is_not_self_intersect(polygon, center)); return polygon; } @@ -1063,3 +1057,13 @@ void VoronoiGraphUtils::draw(SVG & svg, draw(svg, path.nodes, width, mainPathColor); } + +void VoronoiGraphUtils::draw(const Polygon &polygon, + const Lines & lines, + const Point & center) +{ + SVG svg("Bad_polygon.svg", {polygon.points}); + svg.draw(polygon, "orange"); + LineUtils::draw(svg, lines, "red", 0., true, true); + svg.draw(center); +} \ No newline at end of file diff --git a/src/libslic3r/SLA/SupportIslands/VoronoiGraphUtils.hpp b/src/libslic3r/SLA/SupportIslands/VoronoiGraphUtils.hpp index f130455abc..c56020564e 100644 --- a/src/libslic3r/SLA/SupportIslands/VoronoiGraphUtils.hpp +++ b/src/libslic3r/SLA/SupportIslands/VoronoiGraphUtils.hpp @@ -349,6 +349,11 @@ public: // draw function for debug static void draw(SVG & svg, const VoronoiGraph::ExPath &path, coord_t width); + + // draw polygon when convert from cell + static void draw(const Slic3r::Polygon &polygon, + const Slic3r::Lines & lines, + const Slic3r::Point & center); }; } // namespace Slic3r::sla