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

« back to all changes in this revision

Viewing changes to xs/t/06_polygon.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);
 
7
use Slic3r::XS;
 
8
use Test::More tests => 20;
 
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
 
 
19
my $polygon = Slic3r::Polygon->new(@$square);
 
20
my $cw_polygon = $polygon->clone;
 
21
$cw_polygon->reverse;
 
22
 
 
23
ok $polygon->is_valid, 'is_valid';
 
24
is_deeply $polygon->pp, $square, 'polygon roundtrip';
 
25
 
 
26
is ref($polygon->arrayref), 'ARRAY', 'polygon arrayref is unblessed';
 
27
isa_ok $polygon->[0], 'Slic3r::Point::Ref', 'polygon point is blessed';
 
28
 
 
29
my $lines = $polygon->lines;
 
30
is_deeply [ map $_->pp, @$lines ], [
 
31
    [ [100, 100], [200, 100] ],
 
32
    [ [200, 100], [200, 200] ],
 
33
    [ [200, 200], [100, 200] ],
 
34
    [ [100, 200], [100, 100] ],
 
35
], 'polygon lines';
 
36
 
 
37
is_deeply $polygon->split_at_first_point->pp, [ @$square[0,1,2,3,0] ], 'split_at_first_point';
 
38
is_deeply $polygon->split_at_index(2)->pp, [ @$square[2,3,0,1,2] ], 'split_at_index';
 
39
is_deeply $polygon->split_at_vertex(Slic3r::Point->new(@{$square->[2]}))->pp, [ @$square[2,3,0,1,2] ], 'split_at';
 
40
is $polygon->area, 100*100, 'area';
 
41
 
 
42
ok $polygon->is_counter_clockwise, 'is_counter_clockwise';
 
43
ok !$cw_polygon->is_counter_clockwise, 'is_counter_clockwise';
 
44
{
 
45
    my $clone = $polygon->clone;
 
46
    $clone->reverse;
 
47
    ok !$clone->is_counter_clockwise, 'is_counter_clockwise';
 
48
    $clone->make_counter_clockwise;
 
49
    ok $clone->is_counter_clockwise, 'make_counter_clockwise';
 
50
    $clone->make_counter_clockwise;
 
51
    ok $clone->is_counter_clockwise, 'make_counter_clockwise';
 
52
}
 
53
 
 
54
ok ref($polygon->first_point) eq 'Slic3r::Point', 'first_point';
 
55
 
 
56
ok $polygon->contains_point(Slic3r::Point->new(150,150)), 'ccw contains_point';
 
57
ok $cw_polygon->contains_point(Slic3r::Point->new(150,150)), 'cw contains_point';
 
58
 
 
59
{
 
60
    my @points = (Slic3r::Point->new(100,0));
 
61
    foreach my $i (1..5) {
 
62
        my $point = $points[0]->clone;
 
63
        $point->rotate(PI/3*$i, [0,0]);
 
64
        push @points, $point;
 
65
    }
 
66
    my $hexagon = Slic3r::Polygon->new(@points);
 
67
    my $triangles = $hexagon->triangulate_convex;
 
68
    is scalar(@$triangles), 4, 'right number of triangles';
 
69
    ok !(defined first { $_->is_clockwise } @$triangles), 'all triangles are ccw';
 
70
}
 
71
 
 
72
{
 
73
    is_deeply $polygon->centroid->pp, [150,150], 'centroid';
 
74
}
 
75
 
 
76
# this is not a test: this just demonstrates bad usage, where $polygon->clone gets
 
77
# DESTROY'ed before the derived object ($point), causing bad memory access
 
78
if (0) {
 
79
    my $point;
 
80
    {
 
81
        $point = $polygon->clone->[0];
 
82
    }
 
83
    $point->scale(2);
 
84
}
 
85
 
 
86
__END__