Welcome to mirror list, hosted at ThFree Co, Russian Federation.

04_expolygon.t « t « xs - github.com/prusa3d/PrusaSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5c10651f828ef667b64c29ec2fe97b2c78e957fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/perl

use strict;
use warnings;

use List::Util qw(first sum);
use Slic3r::XS;
use Test::More tests => 33;

use constant PI => 4 * atan2(1, 1);

my $square = [  # ccw
    [100, 100],
    [200, 100],
    [200, 200],
    [100, 200],
];
my $hole_in_square = [  # cw
    [140, 140],
    [140, 160],
    [160, 160],
    [160, 140],
];

my $expolygon = Slic3r::ExPolygon->new($square, $hole_in_square);

ok $expolygon->is_valid, 'is_valid';
is ref($expolygon->pp), 'ARRAY', 'expolygon pp is unblessed';
is_deeply $expolygon->pp, [$square, $hole_in_square], 'expolygon roundtrip';

is ref($expolygon->arrayref), 'ARRAY', 'expolygon arrayref is unblessed';
isa_ok $expolygon->[0], 'Slic3r::Polygon::Ref', 'expolygon polygon is blessed';
isa_ok $expolygon->contour, 'Slic3r::Polygon::Ref', 'expolygon contour is blessed';
isa_ok $expolygon->holes->[0], 'Slic3r::Polygon::Ref', 'expolygon hole is blessed';
isa_ok $expolygon->[0][0], 'Slic3r::Point::Ref', 'expolygon point is blessed';

{
    my $expolygon2 = $expolygon->clone;
    my $polygon = $expolygon2->[0];
    $polygon->scale(2);
    is $expolygon2->[0][0][0], $polygon->[0][0], 'polygons are returned by reference';
}

is_deeply $expolygon->clone->pp, [$square, $hole_in_square], 'clone';

is $expolygon->area, 100*100-20*20, 'area';

{
    my $expolygon2 = $expolygon->clone;
    $expolygon2->scale(2.5);
    is_deeply $expolygon2->pp, [
        [map [ 2.5*$_->[0], 2.5*$_->[1] ], @$square],
        [map [ 2.5*$_->[0], 2.5*$_->[1] ], @$hole_in_square]
        ], 'scale';
}

{
    my $expolygon2 = $expolygon->clone;
    $expolygon2->translate(10, -5);
    is_deeply $expolygon2->pp, [
        [map [ $_->[0]+10, $_->[1]-5 ], @$square],
        [map [ $_->[0]+10, $_->[1]-5 ], @$hole_in_square]
        ], 'translate';
}

{
    my $expolygon2 = $expolygon->clone;
    $expolygon2->rotate(PI/2, Slic3r::Point->new(150,150));
    is_deeply $expolygon2->pp, [
        [ @$square[1,2,3,0] ],
        [ @$hole_in_square[3,0,1,2] ]
        ], 'rotate around Point';
}

{
    my $expolygon2 = $expolygon->clone;
    $expolygon2->rotate(PI/2, [150,150]);
    is_deeply $expolygon2->pp, [
        [ @$square[1,2,3,0] ],
        [ @$hole_in_square[3,0,1,2] ]
        ], 'rotate around pure-Perl Point';
}

{
    my $expolygon2 = $expolygon->clone;
    $expolygon2->scale(2);
    my $collection = Slic3r::ExPolygon::Collection->new($expolygon->pp, $expolygon2->pp);
    is_deeply $collection->pp, [ $expolygon->pp, $expolygon2->pp ],
        'expolygon collection (pure Perl) roundtrip';
    
    my $collection2 = Slic3r::ExPolygon::Collection->new($expolygon, $expolygon2);
    is_deeply $collection->pp, $collection2->pp,
        'expolygon collection (XS) roundtrip';
    
    $collection->clear;
    is scalar(@$collection), 0, 'clear collection';
    
    $collection->append($expolygon);
    is scalar(@$collection), 1, 'append to collection';
    
    my $exp = $collection->[0];
    $exp->scale(3);
    is $collection->[0][0][0][0], $exp->[0][0][0], 'collection items are returned by reference';
    
    is_deeply $collection->[0]->clone->pp, $collection->[0]->pp, 'clone collection item';
}

{
    my $expolygon = Slic3r::ExPolygon->new($square);
    my $polygons = $expolygon->get_trapezoids2(PI/2);
    is scalar(@$polygons), 1, 'correct number of trapezoids returned';
    is scalar(@{$polygons->[0]}), 4, 'trapezoid has 4 points';
    is $polygons->[0]->area, $expolygon->area, 'trapezoid has correct area';
}

{
    my $polygons = $expolygon->get_trapezoids2(PI/2);
    is scalar(@$polygons), 4, 'correct number of trapezoids returned';
    
    # trapezoid polygons might have more than 4 points in case of collinear segments
    $polygons = [ map @{$_->simplify(1)}, @$polygons ];
    ok !defined(first { @$_ != 4 } @$polygons), 'all trapezoids have 4 points';
    
    is scalar(grep { $_->area == 40*100 } @$polygons), 2, 'trapezoids have expected area';
    is scalar(grep { $_->area == 20*40 } @$polygons), 2, 'trapezoids have expected area';
}

{
    my $expolygon = Slic3r::ExPolygon->new([ [0,100],[100,0],[200,0],[300,100],[200,200],[100,200] ]);
    my $polygons = $expolygon->get_trapezoids2(PI/2);
    is scalar(@$polygons), 3, 'correct number of trapezoids returned';
    is scalar(grep { $_->area == 100*200/2 } @$polygons), 2, 'trapezoids have expected area';
    is scalar(grep { $_->area == 100*200 } @$polygons), 1, 'trapezoids have expected area';
}

{
    my $triangles = $expolygon->triangulate_pp;
    is scalar(@$triangles), 8, 'expected number of triangles';
    is sum(map $_->area, @$triangles), $expolygon->area, 'sum of triangles area equals original expolygon area';
}

__END__