From 2cf6a9630f701bb4e094cb66ef82ff80922eeb34 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Hejl?= <hejl.lukas@gmail.com>
Date: Tue, 10 May 2022 13:16:00 +0200
Subject: [PATCH] Fixed a crash when functions for preprocessing input polygons
 produced intersecting polygons. It should also fix another crash caused by a
 missing twin edge in the post-processing Voronoi diagram (probably some issue
 in Voronoi diagram post-processing, not in Boost Voronoi generator).

---
 src/libslic3r/Arachne/WallToolPaths.cpp | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/src/libslic3r/Arachne/WallToolPaths.cpp b/src/libslic3r/Arachne/WallToolPaths.cpp
index 0229d92c5..e27c12390 100644
--- a/src/libslic3r/Arachne/WallToolPaths.cpp
+++ b/src/libslic3r/Arachne/WallToolPaths.cpp
@@ -486,6 +486,12 @@ const std::vector<VariableWidthLines> &WallToolPaths::generate()
     removeDegenerateVerts(prepared_outline);
     removeSmallAreas(prepared_outline, small_area_length * small_area_length, false);
 
+    // The functions above could produce intersecting polygons that could cause a crash inside Arachne.
+    // Applying Clipper union should be enough to get rid of this issue.
+    // Clipper union also fixed an issue in Arachne that in post-processing Voronoi diagram, some edges
+    // didn't have twin edges (this probably isn't an issue in Boost Voronoi generator).
+    prepared_outline = union_(prepared_outline);
+
     if (area(prepared_outline) <= 0) {
         assert(toolpaths.empty());
         return toolpaths;