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

« back to all changes in this revision

Viewing changes to lib/Slic3r/Polygon.pm

  • 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
package Slic3r::Polygon;
 
2
use strict;
 
3
use warnings;
 
4
 
 
5
# a polygon is a closed polyline.
 
6
use parent 'Slic3r::Polyline';
 
7
 
 
8
use Slic3r::Geometry qw(
 
9
    polygon_segment_having_point
 
10
    PI X1 X2 Y1 Y2 epsilon);
 
11
use Slic3r::Geometry::Clipper qw(intersection_pl);
 
12
 
 
13
sub wkt {
 
14
    my $self = shift;
 
15
    return sprintf "POLYGON((%s))", join ',', map "$_->[0] $_->[1]", @$self;
 
16
}
 
17
 
 
18
sub dump_perl {
 
19
    my $self = shift;
 
20
    return sprintf "[%s]", join ',', map "[$_->[0],$_->[1]]", @$self;
 
21
}
 
22
 
 
23
sub grow {
 
24
    my $self = shift;
 
25
    return $self->split_at_first_point->grow(@_);
 
26
}
 
27
 
 
28
# this method subdivides the polygon segments to that no one of them
 
29
# is longer than the length provided
 
30
sub subdivide {
 
31
    my $self = shift;
 
32
    my ($max_length) = @_;
 
33
    
 
34
    my @points = @$self;
 
35
    push @points, $points[0];  # append first point as this is a polygon
 
36
    my @new_points = shift @points;
 
37
    while (@points) {
 
38
        while ($new_points[-1]->distance_to($points[0]) > $max_length) {
 
39
            push @new_points, map Slic3r::Point->new(@$_),
 
40
                Slic3r::Geometry::point_along_segment($new_points[-1], $points[0], $max_length);
 
41
        }
 
42
        push @new_points, shift @points;
 
43
    }
 
44
    pop @new_points;  # remove last point as it coincides with first one
 
45
    return Slic3r::Polygon->new(@new_points);
 
46
}
 
47
 
 
48
sub concave_points {
 
49
    my ($self, $angle) = @_;
 
50
    
 
51
    $angle //= PI;
 
52
    
 
53
    # input angle threshold is checked on the internal side of the polygon
 
54
    # but angle3points measures CCW angle, so we calculate the complementary angle
 
55
    my $ccw_angle = 2*PI-$angle;
 
56
    
 
57
    my @points = @$self;
 
58
    my @points_pp = @{$self->pp};
 
59
    
 
60
    my @concave = ();
 
61
    for my $i (-1 .. ($#points-1)) {
 
62
        next if $points[$i-1]->coincides_with($points[$i]);
 
63
        # angle is measured in ccw orientation
 
64
        my $vertex_angle = Slic3r::Geometry::angle3points(@points_pp[$i, $i-1, $i+1]);
 
65
        if ($vertex_angle <= $ccw_angle) {
 
66
            push @concave, $points[$i];
 
67
        }
 
68
    }
 
69
    return [@concave];
 
70
}
 
71
 
 
72
sub convex_points {
 
73
    my ($self, $angle) = @_;
 
74
    
 
75
    $angle //= PI;
 
76
    
 
77
    # input angle threshold is checked on the internal side of the polygon
 
78
    # but angle3points measures CCW angle, so we calculate the complementary angle
 
79
    my $ccw_angle = 2*PI-$angle;
 
80
    
 
81
    my @points = @$self;
 
82
    my @points_pp = @{$self->pp};
 
83
    
 
84
    my @convex = ();
 
85
    for my $i (-1 .. ($#points-1)) {
 
86
        next if $points[$i-1]->coincides_with($points[$i]);
 
87
        # angle is measured in ccw orientation
 
88
        my $vertex_angle = Slic3r::Geometry::angle3points(@points_pp[$i, $i-1, $i+1]);
 
89
        if ($vertex_angle >= $ccw_angle) {
 
90
            push @convex, $points[$i];
 
91
        }
 
92
    }
 
93
    return [@convex];
 
94
}
 
95
 
 
96
1;
 
 
b'\\ No newline at end of file'