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

« back to all changes in this revision

Viewing changes to xs/t/04_expolygon.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
#!/usr/bin/perl
 
2
 
 
3
use strict;
 
4
use warnings;
 
5
 
 
6
use List::Util qw(first sum);
 
7
use Slic3r::XS;
 
8
use Test::More tests => 33;
 
9
 
 
10
use constant PI => 4 * atan2(1, 1);
 
11
 
 
12
my $square = [  # ccw
 
13
    [100, 100],
 
14
    [200, 100],
 
15
    [200, 200],
 
16
    [100, 200],
 
17
];
 
18
my $hole_in_square = [  # cw
 
19
    [140, 140],
 
20
    [140, 160],
 
21
    [160, 160],
 
22
    [160, 140],
 
23
];
 
24
 
 
25
my $expolygon = Slic3r::ExPolygon->new($square, $hole_in_square);
 
26
 
 
27
ok $expolygon->is_valid, 'is_valid';
 
28
is ref($expolygon->pp), 'ARRAY', 'expolygon pp is unblessed';
 
29
is_deeply $expolygon->pp, [$square, $hole_in_square], 'expolygon roundtrip';
 
30
 
 
31
is ref($expolygon->arrayref), 'ARRAY', 'expolygon arrayref is unblessed';
 
32
isa_ok $expolygon->[0], 'Slic3r::Polygon::Ref', 'expolygon polygon is blessed';
 
33
isa_ok $expolygon->contour, 'Slic3r::Polygon::Ref', 'expolygon contour is blessed';
 
34
isa_ok $expolygon->holes->[0], 'Slic3r::Polygon::Ref', 'expolygon hole is blessed';
 
35
isa_ok $expolygon->[0][0], 'Slic3r::Point::Ref', 'expolygon point is blessed';
 
36
 
 
37
{
 
38
    my $expolygon2 = $expolygon->clone;
 
39
    my $polygon = $expolygon2->[0];
 
40
    $polygon->scale(2);
 
41
    is $expolygon2->[0][0][0], $polygon->[0][0], 'polygons are returned by reference';
 
42
}
 
43
 
 
44
is_deeply $expolygon->clone->pp, [$square, $hole_in_square], 'clone';
 
45
 
 
46
is $expolygon->area, 100*100-20*20, 'area';
 
47
 
 
48
{
 
49
    my $expolygon2 = $expolygon->clone;
 
50
    $expolygon2->scale(2.5);
 
51
    is_deeply $expolygon2->pp, [
 
52
        [map [ 2.5*$_->[0], 2.5*$_->[1] ], @$square],
 
53
        [map [ 2.5*$_->[0], 2.5*$_->[1] ], @$hole_in_square]
 
54
        ], 'scale';
 
55
}
 
56
 
 
57
{
 
58
    my $expolygon2 = $expolygon->clone;
 
59
    $expolygon2->translate(10, -5);
 
60
    is_deeply $expolygon2->pp, [
 
61
        [map [ $_->[0]+10, $_->[1]-5 ], @$square],
 
62
        [map [ $_->[0]+10, $_->[1]-5 ], @$hole_in_square]
 
63
        ], 'translate';
 
64
}
 
65
 
 
66
{
 
67
    my $expolygon2 = $expolygon->clone;
 
68
    $expolygon2->rotate(PI/2, Slic3r::Point->new(150,150));
 
69
    is_deeply $expolygon2->pp, [
 
70
        [ @$square[1,2,3,0] ],
 
71
        [ @$hole_in_square[3,0,1,2] ]
 
72
        ], 'rotate around Point';
 
73
}
 
74
 
 
75
{
 
76
    my $expolygon2 = $expolygon->clone;
 
77
    $expolygon2->rotate(PI/2, [150,150]);
 
78
    is_deeply $expolygon2->pp, [
 
79
        [ @$square[1,2,3,0] ],
 
80
        [ @$hole_in_square[3,0,1,2] ]
 
81
        ], 'rotate around pure-Perl Point';
 
82
}
 
83
 
 
84
{
 
85
    my $expolygon2 = $expolygon->clone;
 
86
    $expolygon2->scale(2);
 
87
    my $collection = Slic3r::ExPolygon::Collection->new($expolygon->pp, $expolygon2->pp);
 
88
    is_deeply $collection->pp, [ $expolygon->pp, $expolygon2->pp ],
 
89
        'expolygon collection (pure Perl) roundtrip';
 
90
    
 
91
    my $collection2 = Slic3r::ExPolygon::Collection->new($expolygon, $expolygon2);
 
92
    is_deeply $collection->pp, $collection2->pp,
 
93
        'expolygon collection (XS) roundtrip';
 
94
    
 
95
    $collection->clear;
 
96
    is scalar(@$collection), 0, 'clear collection';
 
97
    
 
98
    $collection->append($expolygon);
 
99
    is scalar(@$collection), 1, 'append to collection';
 
100
    
 
101
    my $exp = $collection->[0];
 
102
    $exp->scale(3);
 
103
    is $collection->[0][0][0][0], $exp->[0][0][0], 'collection items are returned by reference';
 
104
    
 
105
    is_deeply $collection->[0]->clone->pp, $collection->[0]->pp, 'clone collection item';
 
106
}
 
107
 
 
108
{
 
109
    my $expolygon = Slic3r::ExPolygon->new($square);
 
110
    my $polygons = $expolygon->get_trapezoids2(PI/2);
 
111
    is scalar(@$polygons), 1, 'correct number of trapezoids returned';
 
112
    is scalar(@{$polygons->[0]}), 4, 'trapezoid has 4 points';
 
113
    is $polygons->[0]->area, $expolygon->area, 'trapezoid has correct area';
 
114
}
 
115
 
 
116
{
 
117
    my $polygons = $expolygon->get_trapezoids2(PI/2);
 
118
    is scalar(@$polygons), 4, 'correct number of trapezoids returned';
 
119
    
 
120
    # trapezoid polygons might have more than 4 points in case of collinear segments
 
121
    $polygons = [ map @{$_->simplify(1)}, @$polygons ];
 
122
    ok !defined(first { @$_ != 4 } @$polygons), 'all trapezoids have 4 points';
 
123
    
 
124
    is scalar(grep { $_->area == 40*100 } @$polygons), 2, 'trapezoids have expected area';
 
125
    is scalar(grep { $_->area == 20*40 } @$polygons), 2, 'trapezoids have expected area';
 
126
}
 
127
 
 
128
{
 
129
    my $expolygon = Slic3r::ExPolygon->new([ [0,100],[100,0],[200,0],[300,100],[200,200],[100,200] ]);
 
130
    my $polygons = $expolygon->get_trapezoids2(PI/2);
 
131
    is scalar(@$polygons), 3, 'correct number of trapezoids returned';
 
132
    is scalar(grep { $_->area == 100*200/2 } @$polygons), 2, 'trapezoids have expected area';
 
133
    is scalar(grep { $_->area == 100*200 } @$polygons), 1, 'trapezoids have expected area';
 
134
}
 
135
 
 
136
{
 
137
    my $triangles = $expolygon->triangulate_pp;
 
138
    is scalar(@$triangles), 8, 'expected number of triangles';
 
139
    is sum(map $_->area, @$triangles), $expolygon->area, 'sum of triangles area equals original expolygon area';
 
140
}
 
141
 
 
142
__END__