From 78167f56ee2d982bb16f5a565d9bbd180cc614b3 Mon Sep 17 00:00:00 2001
From: Alessandro Ranellucci <aar@cpan.org>
Date: Mon, 16 Sep 2013 19:15:30 +0200
Subject: [PATCH] Some cleanup for the Voronoi code

---
 lib/Slic3r/ExPolygon.pm | 35 ++++++++++++++++++-----------------
 1 file changed, 18 insertions(+), 17 deletions(-)

diff --git a/lib/Slic3r/ExPolygon.pm b/lib/Slic3r/ExPolygon.pm
index 1cd8222a9..a4d774406 100644
--- a/lib/Slic3r/ExPolygon.pm
+++ b/lib/Slic3r/ExPolygon.pm
@@ -105,29 +105,30 @@ sub medial_axis {
     my $self = shift;
     my ($width) = @_;
     
-    my @points = ();
-    foreach my $polygon (@$self) {
-        {
-            my $p = $polygon->pp;
-            Slic3r::Geometry::polyline_remove_short_segments($p, $width / 2);
-            $polygon = Slic3r::Polygon->new(@$p);
+    my $voronoi;
+    {
+        my @points = ();
+        foreach my $polygon (@$self) {
+            {
+                my $p = $polygon->pp;
+                Slic3r::Geometry::polyline_remove_short_segments($p, $width / 2);
+                $polygon = Slic3r::Polygon->new(@$p);
+            }
+            
+            # subdivide polygon segments so that we don't have anyone of them
+            # being longer than $width / 2
+            $polygon = $polygon->subdivide($width/2);
+            
+            push @points, map $_->pp, @$polygon;
         }
-        
-        # subdivide polygon segments so that we don't have anyone of them
-        # being longer than $width / 2
-        $polygon = $polygon->subdivide($width/2);
-        
-        push @points, map $_->clone, @$polygon;
+        $voronoi = Math::Geometry::Voronoi->new(points => \@points);
     }
     
-    my $voronoi = Math::Geometry::Voronoi->new(points => [ map $_->pp, @points ]);
     $voronoi->compute;
+    my $vertices = $voronoi->vertices;
     
     my @skeleton_lines = ();
-    
-    my $vertices = $voronoi->vertices;
-    my $edges = $voronoi->edges;
-    foreach my $edge (@$edges) {
+    foreach my $edge (@{ $voronoi->edges }) {
         # ignore lines going to infinite
         next if $edge->[1] == -1 || $edge->[2] == -1;