~ubuntu-branches/ubuntu/utopic/slic3r/utopic

« back to all changes in this revision

Viewing changes to t/arcs.t

  • Committer: Package Import Robot
  • Author(s): Chow Loong Jin
  • Date: 2014-06-17 01:27:26 UTC
  • Revision ID: package-import@ubuntu.com-20140617012726-2wrs4zdo251nr4vg
Tags: upstream-1.1.4+dfsg
ImportĀ upstreamĀ versionĀ 1.1.4+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
use Test::More;
 
2
use strict;
 
3
use warnings;
 
4
 
 
5
plan tests => 24;
 
6
 
 
7
BEGIN {
 
8
    use FindBin;
 
9
    use lib "$FindBin::Bin/../lib";
 
10
}
 
11
 
 
12
use Slic3r;
 
13
use Slic3r::ExtrusionPath ':roles';
 
14
use Slic3r::Geometry qw(scaled_epsilon epsilon scale unscale X Y deg2rad);
 
15
 
 
16
{
 
17
    my $angle = deg2rad(4);
 
18
    foreach my $ccw (1, 0) {
 
19
        my $polyline = Slic3r::Polyline->new_scale([0,0], [0,10]);
 
20
        {
 
21
            my $p3 = Slic3r::Point->new_scale(0, 20);
 
22
            $p3->rotate($angle * ($ccw ? 1 : -1), $polyline->[-1]);
 
23
            is $ccw, ($p3->[X] < $polyline->[-1][X]) ? 1 : 0, 'third point is rotated correctly';
 
24
            $polyline->append($p3);
 
25
        }
 
26
        ok abs($polyline->length - scale(20)) < scaled_epsilon, 'curved polyline length';
 
27
        is $ccw, ($polyline->[2]->ccw(@$polyline[0,1]) > 0) ? 1 : 0, 'curved polyline has wanted orientation';
 
28
    
 
29
        ok my $arc = Slic3r::GCode::ArcFitting::polyline_to_arc($polyline), 'arc is detected';
 
30
        is $ccw, $arc->is_ccw, 'arc orientation is correct';
 
31
    
 
32
        ok abs($arc->angle - $angle) < epsilon, 'arc relative angle is correct';
 
33
        
 
34
        ok $arc->start->coincides_with($polyline->[0]), 'arc start point is correct';
 
35
        ok $arc->end->coincides_with($polyline->[-1]), 'arc end point is correct';
 
36
        
 
37
        # since first polyline segment is vertical we expect arc center to have same Y as its first point
 
38
        is $arc->center->[Y], 0, 'arc center has correct Y';
 
39
    
 
40
        my $s1 = Slic3r::Line->new(@$polyline[0,1]);
 
41
        my $s2 = Slic3r::Line->new(@$polyline[1,2]);
 
42
        ok abs($arc->center->distance_to($s1->midpoint) - $arc->center->distance_to($s2->midpoint)) < scaled_epsilon,
 
43
            'arc center is equidistant from both segments\' midpoints';
 
44
    }
 
45
}
 
46
 
 
47
#==========================================================
 
48
 
 
49
{
 
50
    my $path = Slic3r::Polyline->new_scale(
 
51
        [13.532242,2.665496], [18.702911,9.954623], [22.251514,9.238193], [25.800116,9.954623], 
 
52
        [28.697942,11.908391], [30.65171,14.806217], [31.36814,18.35482],
 
53
        [30.65171,21.903423], [28.697942,24.801249], [25.800116,26.755017], [22.251514,27.471447], 
 
54
        [18.702911,26.755017], [15.805085,24.801249], [13.851317,21.903423], [13.134887,18.35482], 
 
55
        [86948.77,175149.09], [119825.35,100585],
 
56
    );
 
57
    
 
58
    if (0) {
 
59
        require "Slic3r::SVG";
 
60
        Slic3r::SVG::output(
 
61
            "arc.svg",
 
62
            polylines => [$path],
 
63
        );
 
64
    }
 
65
    
 
66
    my $af = Slic3r::GCode::ArcFitting->new(max_relative_angle => deg2rad(30));
 
67
    my @chunks = $af->detect_arcs($path);
 
68
    
 
69
    is scalar(@chunks), 3, 'path collection now contains three paths';
 
70
    isa_ok $chunks[0], 'Slic3r::Polyline', 'first one is polyline';
 
71
    isa_ok $chunks[1], 'Slic3r::GCode::ArcFitting::Arc', 'second one is arc';
 
72
    isa_ok $chunks[2], 'Slic3r::Polyline', 'third one is polyline';
 
73
}
 
74
 
 
75
exit;
 
76
 
 
77
#==========================================================
 
78
 
 
79
{
 
80
    my @points = map [ scale $_->[0], scale $_->[1] ], (
 
81
        [10,20], [10.7845909572784,19.9691733373313], [11.5643446504023,19.8768834059514], 
 
82
        [12.3344536385591,19.7236992039768], [13.0901699437495,19.5105651629515], 
 
83
        [13.8268343236509,19.2387953251129], [14.5399049973955,18.9100652418837], 
 
84
        [15.2249856471595,18.5264016435409], [15.8778525229247,18.0901699437495], 
 
85
        [16.4944804833018,17.6040596560003]
 
86
    );
 
87
    my $path1 = Slic3r::ExtrusionPath->new(
 
88
        polyline    => Slic3r::Polyline->new(@points),
 
89
        role        => EXTR_ROLE_FILL,
 
90
        mm3_per_mm  => 0.5,
 
91
    );
 
92
    my $path2 = Slic3r::ExtrusionPath->new(
 
93
        polyline    => Slic3r::Polyline->new(reverse @points),
 
94
        role        => EXTR_ROLE_FILL,
 
95
        mm3_per_mm  => 0.5,
 
96
    );
 
97
    
 
98
    my @paths1 = $path1->detect_arcs(10, scale 1);
 
99
    my @paths2 = $path2->detect_arcs(10, scale 1);
 
100
    
 
101
    is scalar(@paths1), 1, 'path collection now contains one path';
 
102
    is scalar(@paths2), 1, 'path collection now contains one path';
 
103
    
 
104
    isa_ok $paths1[0], 'Slic3r::ExtrusionPath::Arc', 'path';
 
105
    isa_ok $paths2[0], 'Slic3r::ExtrusionPath::Arc', 'path';
 
106
    
 
107
    my $expected_length = scale 7.06858347057701;
 
108
    ok abs($paths1[0]->length - $expected_length) < scaled_epsilon, 'cw oriented arc has correct length';
 
109
    ok abs($paths2[0]->length - $expected_length) < scaled_epsilon, 'ccw oriented arc has correct length';
 
110
 
 
111
    is $paths1[0]->orientation, 'cw', 'cw orientation was correctly detected';
 
112
    is $paths2[0]->orientation, 'ccw', 'ccw orientation was correctly detected';
 
113
    is $paths1[0]->flow_spacing, $path1->flow_spacing, 'flow spacing was correctly preserved';
 
114
    
 
115
    my $center1 = [ map sprintf('%.0f', $_), @{ $paths1[0]->center } ];
 
116
    ok abs($center1->[X] - scale 10) < scaled_epsilon && abs($center1->[Y] - scale 10) < scaled_epsilon, 'center was correctly detected';
 
117
    
 
118
    my $center2 = [ map sprintf('%.0f', $_), @{ $paths2[0]->center } ];
 
119
    ok abs($center2->[X] - scale 10) < scaled_epsilon && abs($center1->[Y] - scale 10) < scaled_epsilon, 'center was correctly detected';
 
120
}
 
121
 
 
122
#==========================================================