From 9ac60dca1a452aed7769ff48bb3f6f940c8fe8dc Mon Sep 17 00:00:00 2001
From: Alessandro Ranellucci <aar@cpan.org>
Date: Sun, 21 Dec 2014 12:39:19 +0100
Subject: [PATCH] Fixed vibration limit.

---
 lib/Slic3r/GCode/Reader.pm         | 2 +-
 lib/Slic3r/GCode/VibrationLimit.pm | 7 +++----
 t/vibrationlimit.t                 | 2 +-
 3 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/lib/Slic3r/GCode/Reader.pm b/lib/Slic3r/GCode/Reader.pm
index cdcaeeeb1..bd82fc5b1 100644
--- a/lib/Slic3r/GCode/Reader.pm
+++ b/lib/Slic3r/GCode/Reader.pm
@@ -58,7 +58,7 @@ sub parse {
                     $info{"new_$axis"}  = $self->$axis;
                 }
             }
-            $info{dist_XY} = Slic3r::Geometry::unscale(Slic3r::Line->new_scale([0,0], [@info{qw(dist_X dist_Y)}])->length);
+            $info{dist_XY} = sqrt(($info{dist_X}**2) + ($info{dist_Y}**2));
             if (exists $args{E}) {
                 if ($info{dist_E} > 0) {
                     $info{extruding} = 1;
diff --git a/lib/Slic3r/GCode/VibrationLimit.pm b/lib/Slic3r/GCode/VibrationLimit.pm
index 0680a3b82..496d1e73a 100644
--- a/lib/Slic3r/GCode/VibrationLimit.pm
+++ b/lib/Slic3r/GCode/VibrationLimit.pm
@@ -3,7 +3,6 @@ use Moo;
 
 extends 'Slic3r::GCode::Reader';
 
-has 'config'    => (is => 'ro', required => 1);
 has '_min_time' => (is => 'lazy');
 has '_last_dir' => (is => 'ro', default => sub { [0,0] });
 has '_dir_time' => (is => 'ro', default => sub { [0,0] });
@@ -11,7 +10,6 @@ has '_dir_time' => (is => 'ro', default => sub { [0,0] });
 # inspired by http://hydraraptor.blogspot.it/2010/12/frequency-limit.html
 
 use List::Util qw(max);
-use Slic3r::Geometry qw(X Y);
 
 sub _build__min_time {
     my ($self) = @_;
@@ -27,15 +25,16 @@ sub process {
         my ($reader, $cmd, $args, $info) = @_;
         
         if ($cmd eq 'G1' && $info->{dist_XY} > 0) {
-            my $point = Slic3r::Point->new($args->{X} // $reader->X, $args->{Y} // $reader->Y);
+            my $point = Slic3r::Pointf->new($args->{X} // $reader->X, $args->{Y} // $reader->Y);
             my @dir = (
                 ($point->x <=> $reader->X),
                 ($point->y <=> $reader->Y),   #$
             );
             my $time = $info->{dist_XY} / ($args->{F} // $reader->F);  # in minutes
+            
             if ($time > 0) {
                 my @pause = ();
-                foreach my $axis (X,Y) {
+                foreach my $axis (0..$#dir) {
                     if ($dir[$axis] != 0 && $self->_last_dir->[$axis] != $dir[$axis]) {
                         if ($self->_last_dir->[$axis] != 0) {
                             # this axis is changing direction: check whether we need to pause
diff --git a/t/vibrationlimit.t b/t/vibrationlimit.t
index 2553e2260..882769c02 100644
--- a/t/vibrationlimit.t
+++ b/t/vibrationlimit.t
@@ -15,7 +15,7 @@ my $config = Slic3r::Config->new_from_defaults;
 
 # tolerance, in minutes
 # (our time estimation differs from the internal one because of decimals truncation)
-my $epsilon = 0.002;
+my $epsilon = 0.003;
 
 my $test = sub {
     my ($conf) = @_;