From 13e3a9129ce3f2cf9227f156678711a4fd684337 Mon Sep 17 00:00:00 2001
From: Alessandro Ranellucci <aar@cpan.org>
Date: Sun, 14 Jul 2013 15:06:49 +0200
Subject: [PATCH] Use Slic3r::ExPolygon::Collection for Layer->slices

---
 lib/Slic3r/Layer.pm            | 5 +++--
 lib/Slic3r/Print.pm            | 4 +++-
 xs/t/04_expolygon.t            | 7 ++++++-
 xs/xsp/ExPolygonCollection.xsp | 9 +++++++++
 4 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/lib/Slic3r/Layer.pm b/lib/Slic3r/Layer.pm
index 8fcf87c02..bd27e37ea 100644
--- a/lib/Slic3r/Layer.pm
+++ b/lib/Slic3r/Layer.pm
@@ -16,7 +16,7 @@ has 'height'            => (is => 'ro', required => 1); # layer height in unscal
 
 # collection of expolygons generated by slicing the original geometry;
 # also known as 'islands' (all regions and surface types are merged here)
-has 'slices'            => (is => 'rw');
+has 'slices'            => (is => 'rw', default => sub { Slic3r::ExPolygon::Collection->new });
 
 # ordered collection of extrusion paths to fill surfaces for support material
 has 'support_islands'           => (is => 'rw');
@@ -80,7 +80,8 @@ sub make_slices {
     my $self = shift;
     
     my $slices = union_ex([ map $_->p, map @{$_->slices}, @{$self->regions} ]);
-    $self->slices([ map Slic3r::ExPolygon::XS->new(@$_), @$slices ]);
+    $self->slices->clear;
+    $self->slices->append(map Slic3r::ExPolygon::XS->new(@$_), @$slices);
 }
 
 sub make_perimeters {
diff --git a/lib/Slic3r/Print.pm b/lib/Slic3r/Print.pm
index 67ba0c00c..b067b6ed6 100644
--- a/lib/Slic3r/Print.pm
+++ b/lib/Slic3r/Print.pm
@@ -305,7 +305,9 @@ sub _simplify_slices {
     my ($distance) = @_;
     
     foreach my $layer (map @{$_->layers}, @{$self->objects}) {
-        $layer->slices([ map $_->simplify($distance), @{$layer->slices} ]);
+        my @new = map $_->simplify($distance), @{$layer->slices};
+        $layer->slices->clear;
+        $layer->slices->append(@new);
         foreach my $layerm (@{$layer->regions}) {
             my @new = map $_->simplify($distance), @{$layerm->slices};
             $layerm->slices->clear;
diff --git a/xs/t/04_expolygon.t b/xs/t/04_expolygon.t
index a09df27b8..b8e920e40 100644
--- a/xs/t/04_expolygon.t
+++ b/xs/t/04_expolygon.t
@@ -4,7 +4,7 @@ use strict;
 use warnings;
 
 use Slic3r::XS;
-use Test::More tests => 11;
+use Test::More tests => 13;
 
 use constant PI => 4 * atan2(1, 1);
 
@@ -80,6 +80,11 @@ isa_ok $expolygon->[0][0], 'Slic3r::Point', 'Perl polygon points are blessed';
     my $collection2 = Slic3r::ExPolygon::Collection->new($expolygon, $expolygon2);
     is_deeply [ @$collection ], [ @$collection2 ],
         'expolygon collection with XS expolygons';
+    
+    $collection->clear;
+    is scalar(@$collection), 0, 'clear collection';
+    $collection->append($expolygon);
+    is scalar(@$collection), 1, 'append to collection';
 }
 
 __END__
diff --git a/xs/xsp/ExPolygonCollection.xsp b/xs/xsp/ExPolygonCollection.xsp
index 0c5fba488..2adf85276 100644
--- a/xs/xsp/ExPolygonCollection.xsp
+++ b/xs/xsp/ExPolygonCollection.xsp
@@ -9,6 +9,8 @@
     ~ExPolygonCollection();
     ExPolygonCollection* clone()
         %code{% const char* CLASS = "Slic3r::ExPolygon::Collection"; RETVAL = new ExPolygonCollection(*THIS); %};
+    void clear()
+        %code{% THIS->expolygons.clear(); %};
     void scale(double factor);
     void translate(double x, double y);
     void rotate(double angle, Point* center);
@@ -45,5 +47,12 @@ ExPolygonCollection::arrayref()
     OUTPUT:
         RETVAL
 
+void
+ExPolygonCollection::append(...)
+    CODE:
+        for (unsigned int i = 1; i < items; i++) {
+            THIS->expolygons.push_back(*(ExPolygon *)SvIV((SV*)SvRV( ST(i) )));
+        }
+
 %}
 };