Refactor chained_path(), add chained_path_items() and chained_path_points()

This commit is contained in:
Alessandro Ranellucci 2013-02-06 12:03:53 +01:00
parent e593a30fc7
commit 26a3cd5542
3 changed files with 37 additions and 19 deletions

View File

@ -182,10 +182,8 @@ sub make_fill {
push @fills, @{$layer->thin_fills}; push @fills, @{$layer->thin_fills};
push @fills_ordering_points, map $_->unpack->points->[0], @{$layer->thin_fills}; push @fills_ordering_points, map $_->unpack->points->[0], @{$layer->thin_fills};
# organize infill paths using a shortest path search # organize infill paths using a nearest-neighbor search
@fills = @{chained_path([ @fills = @fills[ chained_path(\@fills_ordering_points) ];
map [ $fills_ordering_points[$_], $fills[$_] ], 0..$#fills,
])};
return @fills; return @fills;
} }

View File

@ -7,7 +7,7 @@ our @ISA = qw(Exporter);
our @EXPORT_OK = qw( our @EXPORT_OK = qw(
PI X Y Z A B X1 Y1 X2 Y2 MIN MAX epsilon slope line_atan lines_parallel PI X Y Z A B X1 Y1 X2 Y2 MIN MAX epsilon slope line_atan lines_parallel
line_point_belongs_to_segment points_coincide distance_between_points line_point_belongs_to_segment points_coincide distance_between_points
comparable_distance_between_points comparable_distance_between_points chained_path_items chained_path_points
line_length midpoint point_in_polygon point_in_segment segment_in_segment line_length midpoint point_in_polygon point_in_segment segment_in_segment
point_is_on_left_of_segment polyline_lines polygon_lines nearest_point point_is_on_left_of_segment polyline_lines polygon_lines nearest_point
point_along_segment polygon_segment_having_point polygon_has_subsegment point_along_segment polygon_segment_having_point polygon_has_subsegment
@ -799,29 +799,43 @@ sub polyline_remove_short_segments {
} }
} }
# accepts an arrayref; each item should be an arrayref whose first # accepts an arrayref of points; it returns a list of indices
# item is the point to be used for the shortest path, and the second # according to a nearest-neighbor walk
# one is the value to be returned in output (if the second item
# is not provided, the point will be returned)
sub chained_path { sub chained_path {
my ($items, $start_near) = @_; my ($items, $start_near) = @_;
my %values = map +($_->[0] => $_->[1] || $_->[0]), @$items; my @points = @$items;
my @points = map $_->[0], @$items; my %indices = map { $points[$_] => $_ } 0 .. $#points;
my $result = []; my @result = ();
my $last_point; my $last_point;
if (!$start_near) { if (!$start_near) {
$start_near = shift @points; $start_near = shift @points;
push @$result, $values{$start_near} if $start_near; push @result, $indices{$start_near} if $start_near;
} }
while (@points) { while (@points) {
$start_near = nearest_point($start_near, [@points]); $start_near = nearest_point($start_near, [@points]);
@points = grep $_ ne $start_near, @points; @points = grep $_ ne $start_near, @points;
push @$result, $values{$start_near}; push @result, $indices{$start_near};
} }
return $result; return @result;
}
# accepts an arrayref; each item should be an arrayref whose first
# item is the point to be used for the shortest path, and the second
# one is the value to be returned in output (if the second item
# is not provided, the point will be returned)
sub chained_path_items {
my ($items, $start_near) = @_;
my @indices = chained_path([ map $_->[0], @$items ], $start_near);
return [ map $_->[1], @$items[@indices] ];
}
sub chained_path_points {
my ($points, $start_near) = @_;
return [ @$points[ chained_path($points, $start_near) ] ];
} }
sub douglas_peucker { sub douglas_peucker {

View File

@ -2,7 +2,7 @@ package Slic3r::Layer::Region;
use Moo; use Moo;
use Slic3r::ExtrusionPath ':roles'; use Slic3r::ExtrusionPath ':roles';
use Slic3r::Geometry qw(scale chained_path); use Slic3r::Geometry qw(scale chained_path_items);
use Slic3r::Geometry::Clipper qw(safety_offset union_ex diff_ex intersection_ex); use Slic3r::Geometry::Clipper qw(safety_offset union_ex diff_ex intersection_ex);
use Slic3r::Surface ':types'; use Slic3r::Surface ':types';
@ -167,7 +167,7 @@ sub make_perimeters {
my @perimeters = (); # one item per depth; each item my @perimeters = (); # one item per depth; each item
# organize islands using a shortest path search # organize islands using a shortest path search
my @surfaces = @{chained_path([ my @surfaces = @{chained_path_items([
map [ $_->contour->[0], $_ ], @{$self->slices}, map [ $_->contour->[0], $_ ], @{$self->slices},
])}; ])};
@ -319,18 +319,24 @@ sub make_perimeters {
} }
# process one island (original surface) at time # process one island (original surface) at time
# islands are already sorted with a nearest-neighbor search
foreach my $island (@perimeters) { foreach my $island (@perimeters) {
# do holes starting from innermost one # do holes starting from innermost one
my @holes = (); my @holes = ();
my %is_external = (); my %is_external = ();
# each item of @$island contains the expolygons having the same depth;
# for each depth we build an arrayref containing all the holes
my @hole_depths = map [ map $_->holes, @$_ ], @$island; my @hole_depths = map [ map $_->holes, @$_ ], @$island;
# organize the outermost hole loops using a shortest path search # organize the outermost hole loops using a nearest-neighbor search
@{$hole_depths[0]} = @{chained_path([ @{$hole_depths[0]} = @{chained_path_items([
map [ $_->[0], $_ ], @{$hole_depths[0]}, map [ $_->[0], $_ ], @{$hole_depths[0]},
])}; ])};
# loop while we have spare holes
CYCLE: while (map @$_, @hole_depths) { CYCLE: while (map @$_, @hole_depths) {
# remove first depth container if it contains no holes anymore
shift @hole_depths while !@{$hole_depths[0]}; shift @hole_depths while !@{$hole_depths[0]};
# take first available hole # take first available hole