From f0de57cbe41489836e194a82ce5e1bea5482fc96 Mon Sep 17 00:00:00 2001
From: Alessandro Ranellucci <aar@cpan.org>
Date: Tue, 6 Jan 2015 15:04:09 +0100
Subject: [PATCH] Minor cleanup of the init_external_mp() call

---
 lib/Slic3r/Print/GCode.pm | 31 +++++++++++++++++++------------
 1 file changed, 19 insertions(+), 12 deletions(-)

diff --git a/lib/Slic3r/Print/GCode.pm b/lib/Slic3r/Print/GCode.pm
index 517c453cd..da4d51416 100644
--- a/lib/Slic3r/Print/GCode.pm
+++ b/lib/Slic3r/Print/GCode.pm
@@ -121,23 +121,30 @@ sub export {
     
     # initialize a motion planner for object-to-object travel moves
     if ($self->config->avoid_crossing_perimeters) {
-        my $distance_from_objects = 1;
+        my $distance_from_objects = scale 1;
+        
         # compute the offsetted convex hull for each object and repeat it for each copy.
-        my @islands = ();
-        foreach my $obj_idx (0 .. ($self->print->object_count - 1)) {
+        my @islands_p = ();
+        foreach my $object (@{$self->objects}) {
+            # compute the convex hull of the entire object
             my $convex_hull = convex_hull([
-                map @{$_->contour}, map @{$_->slices}, @{$self->objects->[$obj_idx]->layers},
+                map @{$_->contour}, map @{$_->slices}, @{$object->layers},
             ]);
-            # discard layers only containing thin walls (offset would fail on an empty polygon)
-            if (@$convex_hull) {
-                my $expolygon = Slic3r::ExPolygon->new($convex_hull);
-                my @island = @{$expolygon->offset_ex(scale $distance_from_objects, 1, JT_SQUARE)};
-                foreach my $copy (@{ $self->objects->[$obj_idx]->_shifted_copies }) {
-                    push @islands, map { my $c = $_->clone; $c->translate(@$copy); $c } @island;
-                }
+            
+            # discard objects only containing thin walls (offset would fail on an empty polygon)
+            next if !@$convex_hull;
+            
+            # grow convex hull by the wanted clearance
+            my @obj_islands_p = @{offset([$convex_hull], $distance_from_objects, 1, JT_SQUARE)};
+            
+            # translate convex hull for each object copy and append it to the islands array
+            foreach my $copy (@{ $object->_shifted_copies }) {
+                my @copy_islands_p = map $_->clone, @obj_islands_p;
+                $_->translate(@$copy) for @copy_islands_p;
+                push @islands_p, @copy_islands_p;
             }
         }
-        $gcodegen->avoid_crossing_perimeters->init_external_mp(union_ex([ map @$_, @islands ]));
+        $gcodegen->avoid_crossing_perimeters->init_external_mp(union_ex(\@islands_p));
     }
     
     # calculate wiping points if needed