From d50e0af548ec6f193221320aa57f7bfee204ed1a Mon Sep 17 00:00:00 2001
From: Alessandro Ranellucci <aar@cpan.org>
Date: Sat, 9 Mar 2013 17:15:45 +0100
Subject: [PATCH] Make sure the inwards move after last perimeter loop doesn't
 exceed object boundary

---
 lib/Slic3r/GCode.pm | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/lib/Slic3r/GCode.pm b/lib/Slic3r/GCode.pm
index 4c40ee329..08da0d09b 100644
--- a/lib/Slic3r/GCode.pm
+++ b/lib/Slic3r/GCode.pm
@@ -1,7 +1,7 @@
 package Slic3r::GCode;
 use Moo;
 
-use List::Util qw(max first);
+use List::Util qw(min max first);
 use Slic3r::ExtrusionPath ':roles';
 use Slic3r::Geometry qw(scale unscale scaled_epsilon points_coincide PI X Y B);
 use Slic3r::Geometry::Clipper qw(union_ex);
@@ -159,7 +159,11 @@ sub extrude_loop {
         $angle *= -1 if $was_clockwise;
         
         # create the destination point along the first segment and rotate it
-        my $point = Slic3r::Geometry::point_along_segment(@{$extrusion_path->polyline}[0,1], scale $extrusion_path->flow_spacing);
+        # we make sure we don't exceed the segment length because we don't know
+        # the rotation of the second segment so we might cross the object boundary
+        my $first_segment = Slic3r::Line->new(@{$extrusion_path->polyline}[0,1]);
+        my $distance = min(scale $extrusion_path->flow_spacing, $first_segment->length);
+        my $point = Slic3r::Geometry::point_along_segment(@$first_segment, $distance);
         bless $point, 'Slic3r::Point';
         $point->rotate($angle, $extrusion_path->polyline->[0]);