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

« back to all changes in this revision

Viewing changes to lib/Slic3r/Fill/Rectilinear.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::Fill::Rectilinear;
 
2
use Moo;
 
3
 
 
4
extends 'Slic3r::Fill::Base';
 
5
 
 
6
has 'cache'         => (is => 'rw', default => sub {{}});
 
7
 
 
8
use Slic3r::Geometry qw(A B X Y MIN scale unscale scaled_epsilon);
 
9
use Slic3r::Geometry::Clipper qw(intersection_pl offset);
 
10
 
 
11
sub fill_surface {
 
12
    my $self = shift;
 
13
    my ($surface, %params) = @_;
 
14
    
 
15
    # rotate polygons so that we can work with vertical lines here
 
16
    my $expolygon = $surface->expolygon->clone;
 
17
    my $rotate_vector = $self->infill_direction($surface);
 
18
    $self->rotate_points($expolygon, $rotate_vector);
 
19
    
 
20
    my $flow                = $params{flow} or die "No flow supplied to fill_surface()";
 
21
    my $min_spacing         = $flow->scaled_spacing;
 
22
    my $line_spacing        = $min_spacing / $params{density};
 
23
    my $line_oscillation    = $line_spacing - $min_spacing;
 
24
    my $is_line_pattern     = $self->isa('Slic3r::Fill::Line');
 
25
    my $bounding_box        = $expolygon->bounding_box;
 
26
    
 
27
    # define flow spacing according to requested density
 
28
    if ($params{density} == 1 && !$params{dont_adjust}) {
 
29
        $line_spacing = $self->adjust_solid_spacing(
 
30
            width       => $bounding_box->size->[X],
 
31
            distance    => $line_spacing,
 
32
        );
 
33
        $flow = Slic3r::Flow->new_from_spacing(
 
34
            spacing             => unscale($line_spacing),
 
35
            nozzle_diameter     => $flow->nozzle_diameter,
 
36
            layer_height        => ($params{layer_height} or die "No layer_height supplied to fill_surface()"),
 
37
            bridge              => $flow->bridge,
 
38
        );
 
39
    } else {
 
40
        # extend bounding box so that our pattern will be aligned with other layers
 
41
        $bounding_box->merge_point(Slic3r::Point->new(
 
42
            $bounding_box->x_min - ($bounding_box->x_min % $line_spacing),
 
43
            $bounding_box->y_min - ($bounding_box->y_min % $line_spacing),
 
44
        ));
 
45
    }
 
46
    
 
47
    # generate the basic pattern
 
48
    my $i               = 0;
 
49
    my $x               = $bounding_box->x_min;
 
50
    my $x_max           = $bounding_box->x_max + scaled_epsilon;
 
51
    my @vertical_lines  = ();
 
52
    while ($x <= $x_max) {
 
53
        my $vertical_line = [ [$x, $bounding_box->y_max], [$x, $bounding_box->y_min] ];
 
54
        if ($is_line_pattern && $i % 2) {
 
55
            $vertical_line->[A][X] += $line_oscillation;
 
56
            $vertical_line->[B][X] -= $line_oscillation;
 
57
        }
 
58
        push @vertical_lines, Slic3r::Polyline->new(@$vertical_line);
 
59
        $i++;
 
60
        $x += $line_spacing;
 
61
    }
 
62
    
 
63
    # clip paths against a slightly larger expolygon, so that the first and last paths
 
64
    # are kept even if the expolygon has vertical sides
 
65
    # the minimum offset for preventing edge lines from being clipped is scaled_epsilon;
 
66
    # however we use a larger offset to support expolygons with slightly skewed sides and 
 
67
    # not perfectly straight
 
68
    my @polylines = @{intersection_pl(\@vertical_lines, $expolygon->offset(scale 0.02))};
 
69
    
 
70
    # connect lines
 
71
    unless ($params{dont_connect} || !@polylines) {  # prevent calling leftmost_point() on empty collections
 
72
        my ($expolygon_off) = @{$expolygon->offset_ex($min_spacing/2)};
 
73
        my $collection = Slic3r::Polyline::Collection->new(@polylines);
 
74
        @polylines = ();
 
75
        
 
76
        my $tolerance = 10 * scaled_epsilon;
 
77
        my $diagonal_distance = $line_spacing * 2;
 
78
        my $can_connect = $is_line_pattern
 
79
            ? sub {
 
80
                ($_[X] >= ($line_spacing - $line_oscillation) - $tolerance) && ($_[X] <= ($line_spacing + $line_oscillation) + $tolerance)
 
81
                    && $_[Y] <= $diagonal_distance
 
82
            }
 
83
            : sub { $_[X] <= $diagonal_distance && $_[Y] <= $diagonal_distance };
 
84
        
 
85
        foreach my $polyline (@{$collection->chained_path_from($collection->leftmost_point, 0)}) {
 
86
            if (@polylines) {
 
87
                my $first_point = $polyline->first_point;
 
88
                my $last_point = $polylines[-1]->last_point;
 
89
                my @distance = map abs($first_point->$_ - $last_point->$_), qw(x y);
 
90
                
 
91
                # TODO: we should also check that both points are on a fill_boundary to avoid 
 
92
                # connecting paths on the boundaries of internal regions
 
93
                if ($can_connect->(@distance) && $expolygon_off->contains_line(Slic3r::Line->new($last_point, $first_point))) {
 
94
                    $polylines[-1]->append_polyline($polyline);
 
95
                    next;
 
96
                }
 
97
            }
 
98
            
 
99
            # make a clone before $collection goes out of scope
 
100
            push @polylines, $polyline->clone;
 
101
        }
 
102
    }
 
103
    
 
104
    # paths must be rotated back
 
105
    $self->rotate_points_back(\@polylines, $rotate_vector);
 
106
    
 
107
    return { flow => $flow }, @polylines;
 
108
}
 
109
 
 
110
1;