From cf5adca928d246802bc47bf3d4df648538ca80c3 Mon Sep 17 00:00:00 2001
From: Alessandro Ranellucci <aar@cpan.org>
Date: Sat, 16 Mar 2013 19:58:34 +0100
Subject: [PATCH] New --resolution option to simplify input models

---
 README.markdown       |  1 +
 lib/Slic3r/Config.pm  |  9 +++++++++
 lib/Slic3r/GUI/Tab.pm |  6 +++---
 lib/Slic3r/Print.pm   | 20 ++++++++++++++++----
 slic3r.pl             |  1 +
 5 files changed, 30 insertions(+), 7 deletions(-)

diff --git a/README.markdown b/README.markdown
index df651b6a0..ffebd1481 100644
--- a/README.markdown
+++ b/README.markdown
@@ -289,6 +289,7 @@ The author of the Silk icon set is Mark James.
        
        Miscellaneous options:
         --notes             Notes to be added as comments to the output file
+        --resolution        Minimum detail resolution (mm, set zero for full resolution, default: 0)
       
        Flow options (advanced):
         --extrusion-width   Set extrusion width manually; it accepts either an absolute value in mm
diff --git a/lib/Slic3r/Config.pm b/lib/Slic3r/Config.pm
index 454afd40c..1bd9657a0 100644
--- a/lib/Slic3r/Config.pm
+++ b/lib/Slic3r/Config.pm
@@ -37,6 +37,15 @@ our $Options = {
         default => $Slic3r::have_threads ? 2 : 1,
         readonly => !$Slic3r::have_threads,
     },
+    'resolution' => {
+        label   => 'Resolution',
+        tooltip => 'Minimum detail resolution, used to simplify the input file for speeding up the slicing job and reducing memory usage. High-resolution models often carry more detail than printers can render. Set to zero to disable any simplification and use full resolution from input.',
+        sidetext => 'mm',
+        cli     => 'resolution=f',
+        type    => 'f',
+        min     => 0,
+        default => 0,
+    },
 
     # output options
     'output_filename_format' => {
diff --git a/lib/Slic3r/GUI/Tab.pm b/lib/Slic3r/GUI/Tab.pm
index 79f9af96e..fa10383e5 100644
--- a/lib/Slic3r/GUI/Tab.pm
+++ b/lib/Slic3r/GUI/Tab.pm
@@ -517,10 +517,10 @@ sub build {
             title => 'Flow',
             options => [qw(bridge_flow_ratio)],
         },
-        $Slic3r::have_threads ? {
+        {
             title => 'Other',
-            options => [qw(threads)],
-        } : (),
+            options => [($Slic3r::have_threads ? qw(threads) : ()), qw(resolution)],
+        },
     ]);
 }
 
diff --git a/lib/Slic3r/Print.pm b/lib/Slic3r/Print.pm
index e5025cc09..3be7fe87b 100644
--- a/lib/Slic3r/Print.pm
+++ b/lib/Slic3r/Print.pm
@@ -313,6 +313,16 @@ sub size {
     return [ $bb[X2] - $bb[X1], $bb[Y2] - $bb[Y1] ];
 }
 
+sub _simplify_slices {
+    my $self = shift;
+    my ($distance) = @_;
+    
+    foreach my $layer (map @{$_->layers}, @{$self->objects}) {
+        @$_ = map $_->simplify($distance), @$_
+            for $layer->slices, (map $_->slices, @{$layer->regions});
+    }
+}
+
 sub export_gcode {
     my $self = shift;
     my %params = @_;
@@ -326,6 +336,11 @@ sub export_gcode {
     $status_cb->(10, "Processing triangulated mesh");
     $_->slice for @{$self->objects};
     
+    if ($Slic3r::Config->resolution) {
+        $status_cb->(15, "Simplifying input");
+        $self->_simplify_slices(scale $Slic3r::Config->resolution);
+    }
+    
     # make perimeters
     # this will add a set of extrusion loops to each layer
     # as well as generate infill boundaries
@@ -334,10 +349,7 @@ sub export_gcode {
     
     # simplify slices (both layer and region slices),
     # we only need the max resolution for perimeters
-    foreach my $layer (map @{$_->layers}, @{$self->objects}) {
-        @$_ = map $_->simplify(&Slic3r::SCALED_RESOLUTION), @$_
-            for $layer->slices, (map $_->slices, @{$layer->regions});
-    }
+    $self->_simplify_slices(&Slic3r::SCALED_RESOLUTION);
     
     # this will assign a type (top/bottom/internal) to $layerm->slices
     # and transform $layerm->fill_surfaces from expolygon 
diff --git a/slic3r.pl b/slic3r.pl
index 4290ceb8e..c4cfd8487 100755
--- a/slic3r.pl
+++ b/slic3r.pl
@@ -342,6 +342,7 @@ $j
    
    Miscellaneous options:
     --notes             Notes to be added as comments to the output file
+    --resolution        Minimum detail resolution (mm, set zero for full resolution, default: $config->{resolution})
   
    Flow options (advanced):
     --extrusion-width   Set extrusion width manually; it accepts either an absolute value in mm