From 5bf3f5f5e495db46c59fe9e9ac1c680f2823f3ff Mon Sep 17 00:00:00 2001
From: Alessandro Ranellucci <aar@cpan.org>
Date: Fri, 10 May 2013 13:47:40 +0200
Subject: [PATCH] Make sure brim paths don't overlap

---
 lib/Slic3r/Geometry/Clipper.pm | 12 +++++++++++-
 lib/Slic3r/Print.pm            |  5 +++--
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/lib/Slic3r/Geometry/Clipper.pm b/lib/Slic3r/Geometry/Clipper.pm
index fb0fa242e..d454b3b1c 100644
--- a/lib/Slic3r/Geometry/Clipper.pm
+++ b/lib/Slic3r/Geometry/Clipper.pm
@@ -6,7 +6,7 @@ require Exporter;
 our @ISA = qw(Exporter);
 our @EXPORT_OK = qw(safety_offset safety_offset_ex offset offset_ex collapse_ex
     diff_ex diff union_ex intersection_ex xor_ex PFT_EVENODD JT_MITER JT_ROUND
-    JT_SQUARE is_counter_clockwise union_pt);
+    JT_SQUARE is_counter_clockwise union_pt ex_int_offset2 offset2);
 
 use Math::Clipper 1.17 qw(:cliptypes :polyfilltypes :jointypes is_counter_clockwise area);
 use Slic3r::Geometry qw(scale);
@@ -33,6 +33,16 @@ sub offset {
     return @$offsets;
 }
 
+sub offset2 {
+    my ($polygons, $distance1, $distance2, $scale, $joinType, $miterLimit) = @_;
+    $scale      ||= 100000;
+    $joinType   //= JT_MITER;
+    $miterLimit //= 3;
+    
+    my $offsets = Math::Clipper::int_offset2($polygons, $distance1, $distance2, $scale, $joinType, $miterLimit);
+    return @$offsets;
+}
+
 sub offset_ex {
     my ($polygons, $distance, $scale, $joinType, $miterLimit) = @_;
     $scale      ||= 100000;
diff --git a/lib/Slic3r/Print.pm b/lib/Slic3r/Print.pm
index c04e3b28b..69d84475c 100644
--- a/lib/Slic3r/Print.pm
+++ b/lib/Slic3r/Print.pm
@@ -7,7 +7,8 @@ use List::Util qw(max first);
 use Math::ConvexHull::MonotoneChain qw(convex_hull);
 use Slic3r::ExtrusionPath ':roles';
 use Slic3r::Geometry qw(X Y Z X1 Y1 X2 Y2 MIN PI scale unscale move_points nearest_point chained_path_items);
-use Slic3r::Geometry::Clipper qw(diff_ex union_ex union_pt intersection_ex offset JT_ROUND JT_SQUARE PFT_EVENODD);
+use Slic3r::Geometry::Clipper qw(diff_ex union_ex union_pt intersection_ex offset
+    ex_int_offset2 offset2 JT_ROUND JT_SQUARE PFT_EVENODD);
 use Time::HiRes qw(gettimeofday tv_interval);
 
 has 'config'                 => (is => 'rw', default => sub { Slic3r::Config->new_from_defaults }, trigger => 1);
@@ -679,7 +680,7 @@ sub make_brim {
         # JT_SQUARE ensures no vertex is outside the given offset distance
         # -0.5 because islands are not represented by their centerlines
         # TODO: we need the offset inwards/offset outwards logic to avoid overlapping extrusions
-        push @loops, offset(\@islands, ($i - 0.5) * $flow->scaled_spacing, undef, JT_SQUARE);
+        push @loops, offset2(\@islands, ($i - 2) * $flow->scaled_spacing, ($i + 1.5) * $flow->scaled_spacing, undef, JT_SQUARE);
     }
     
     # prepare a subroutine to traverse the tree and return inner perimeters first