From 9ad1360e445cd3043c4dc30ccad64b82b914f952 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Mon, 20 Mar 2017 22:22:38 +0100 Subject: [PATCH] Bugfix: when the Voronoi diagram contained very large coordinates we need to check whether they are greater than our allowed range and consider the Voronoi edges infinite in those cases, in order to prevent overflows. #3776 --- xs/src/libslic3r/ClipperUtils.hpp | 3 ++- xs/src/libslic3r/ExPolygon.cpp | 8 ++++++++ xs/src/libslic3r/ExPolygon.hpp | 3 +++ xs/src/libslic3r/Geometry.cpp | 10 +++++++++- 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/xs/src/libslic3r/ClipperUtils.hpp b/xs/src/libslic3r/ClipperUtils.hpp index 9e4e5e89a..f76661215 100644 --- a/xs/src/libslic3r/ClipperUtils.hpp +++ b/xs/src/libslic3r/ClipperUtils.hpp @@ -19,7 +19,8 @@ namespace Slic3r { // How about 2^17=131072? // By the way, is the scalling needed at all? Cura runs all the computation with a fixed point precision of 1um, while Slic3r scales to 1nm, // further scaling by 10e5 brings us to -#define CLIPPER_OFFSET_SCALE 100000.0 +static const float CLIPPER_OFFSET_SCALE = 100000.0; +static const coord_t MAX_COORD = ClipperLib::hiRange / CLIPPER_OFFSET_SCALE; //----------------------------------------------------------- // legacy code from Clipper documentation diff --git a/xs/src/libslic3r/ExPolygon.cpp b/xs/src/libslic3r/ExPolygon.cpp index ec02acc35..93fbba467 100644 --- a/xs/src/libslic3r/ExPolygon.cpp +++ b/xs/src/libslic3r/ExPolygon.cpp @@ -514,4 +514,12 @@ ExPolygon::dump_perl() const return ret.str(); } +std::ostream& +operator <<(std::ostream &s, const ExPolygons &expolygons) +{ + for (const ExPolygon &e : expolygons) + s << e.dump_perl() << std::endl; + return s; +} + } diff --git a/xs/src/libslic3r/ExPolygon.hpp b/xs/src/libslic3r/ExPolygon.hpp index 63cc560cd..404a4385e 100644 --- a/xs/src/libslic3r/ExPolygon.hpp +++ b/xs/src/libslic3r/ExPolygon.hpp @@ -4,6 +4,7 @@ #include "libslic3r.h" #include "Polygon.hpp" #include "Polyline.hpp" +#include #include namespace Slic3r { @@ -62,6 +63,8 @@ operator+(ExPolygons src1, const ExPolygons &src2) { return src1; }; +std::ostream& operator <<(std::ostream &s, const ExPolygons &expolygons); + } // start Boost diff --git a/xs/src/libslic3r/Geometry.cpp b/xs/src/libslic3r/Geometry.cpp index 0cc9a575c..ccd645e2f 100644 --- a/xs/src/libslic3r/Geometry.cpp +++ b/xs/src/libslic3r/Geometry.cpp @@ -7,13 +7,14 @@ #include #include #include +#include #include #include #include #include #include #include - +#define SLIC3R_DEBUG #ifdef SLIC3R_DEBUG #include "SVG.hpp" #endif @@ -609,6 +610,13 @@ MedialAxis::process_edge_neighbors(const VD::edge_type* edge, ThickPolyline* pol bool MedialAxis::validate_edge(const VD::edge_type* edge) { + // prevent overflows and detect almost-infinite edges + if (std::abs(edge->vertex0()->x()) > (double)MAX_COORD + || std::abs(edge->vertex0()->y()) > (double)MAX_COORD + || std::abs(edge->vertex1()->x()) > (double)MAX_COORD + || std::abs(edge->vertex1()->y()) > (double)MAX_COORD) + return false; + // construct the line representing this edge of the Voronoi diagram const Line line( Point( edge->vertex0()->x(), edge->vertex0()->y() ),