2012-04-16 09:55:14 +00:00
|
|
|
package Slic3r::Fill::Honeycomb;
|
|
|
|
use Moo;
|
|
|
|
|
|
|
|
extends 'Slic3r::Fill::Base';
|
|
|
|
|
2012-10-30 13:34:41 +00:00
|
|
|
has 'cache' => (is => 'rw', default => sub {{}});
|
2012-04-16 09:55:14 +00:00
|
|
|
|
2013-06-16 11:26:57 +00:00
|
|
|
use Slic3r::Geometry qw(PI X Y MIN MAX scale scaled_epsilon);
|
2013-11-21 13:15:38 +00:00
|
|
|
use Slic3r::Geometry::Clipper qw(intersection intersection_pl);
|
2012-04-16 09:55:14 +00:00
|
|
|
|
|
|
|
sub angles () { [0, PI/3, PI/3*2] }
|
|
|
|
|
|
|
|
sub fill_surface {
|
|
|
|
my $self = shift;
|
|
|
|
my ($surface, %params) = @_;
|
|
|
|
|
|
|
|
my $rotate_vector = $self->infill_direction($surface);
|
|
|
|
|
2013-06-16 11:13:52 +00:00
|
|
|
# cache hexagons math
|
|
|
|
my $cache_id = sprintf "d%s_s%s", $params{density}, $params{flow_spacing};
|
|
|
|
my $m;
|
|
|
|
if (!($m = $self->cache->{$cache_id})) {
|
|
|
|
$m = $self->cache->{$cache_id} = {};
|
|
|
|
my $min_spacing = scale $params{flow_spacing};
|
|
|
|
$m->{distance} = $min_spacing / $params{density};
|
|
|
|
$m->{hex_side} = $m->{distance} / (sqrt(3)/2);
|
|
|
|
$m->{hex_width} = $m->{distance} * 2; # $m->{hex_width} == $m->{hex_side} * sqrt(3);
|
|
|
|
my $hex_height = $m->{hex_side} * 2;
|
|
|
|
$m->{pattern_height} = $hex_height + $m->{hex_side};
|
|
|
|
$m->{y_short} = $m->{distance} * sqrt(3)/3;
|
|
|
|
$m->{x_offset} = $min_spacing / 2;
|
|
|
|
$m->{y_offset} = $m->{x_offset} * sqrt(3)/3;
|
|
|
|
$m->{hex_center} = Slic3r::Point->new($m->{hex_width}/2, $m->{hex_side});
|
|
|
|
}
|
2012-04-16 09:55:14 +00:00
|
|
|
|
2013-06-16 11:13:52 +00:00
|
|
|
my @polygons = ();
|
|
|
|
{
|
2012-04-16 09:55:14 +00:00
|
|
|
# adjust actual bounding box to the nearest multiple of our hex pattern
|
|
|
|
# and align it so that it matches across layers
|
2013-05-19 09:25:18 +00:00
|
|
|
|
2013-06-16 11:13:52 +00:00
|
|
|
my $bounding_box = $surface->expolygon->bounding_box;
|
2012-04-16 09:55:14 +00:00
|
|
|
{
|
2013-06-16 11:13:52 +00:00
|
|
|
# rotate bounding box according to infill direction
|
2013-06-16 10:21:25 +00:00
|
|
|
my $bb_polygon = $bounding_box->polygon;
|
2013-06-16 11:13:52 +00:00
|
|
|
$bb_polygon->rotate($rotate_vector->[0][0], $m->{hex_center});
|
2013-06-16 10:21:25 +00:00
|
|
|
$bounding_box = $bb_polygon->bounding_box;
|
2013-06-16 11:13:52 +00:00
|
|
|
|
|
|
|
# extend bounding box so that our pattern will be aligned with other layers
|
|
|
|
# $bounding_box->[X1] and [Y1] represent the displacement between new bounding box offset and old one
|
|
|
|
$bounding_box->extents->[X][MIN] -= $bounding_box->x_min % $m->{hex_width};
|
|
|
|
$bounding_box->extents->[Y][MIN] -= $bounding_box->y_min % $m->{pattern_height};
|
2012-04-16 09:55:14 +00:00
|
|
|
}
|
|
|
|
|
2013-06-16 10:21:25 +00:00
|
|
|
my $x = $bounding_box->x_min;
|
|
|
|
while ($x <= $bounding_box->x_max) {
|
2012-04-16 09:55:14 +00:00
|
|
|
my $p = [];
|
|
|
|
|
2013-06-16 11:13:52 +00:00
|
|
|
my @x = ($x + $m->{x_offset}, $x + $m->{distance} - $m->{x_offset});
|
2012-04-16 09:55:14 +00:00
|
|
|
for (1..2) {
|
|
|
|
@$p = reverse @$p; # turn first half upside down
|
|
|
|
my @p = ();
|
2013-06-16 11:13:52 +00:00
|
|
|
for (my $y = $bounding_box->y_min; $y <= $bounding_box->y_max; $y += $m->{y_short} + $m->{hex_side} + $m->{y_short} + $m->{hex_side}) {
|
2012-04-16 09:55:14 +00:00
|
|
|
push @$p,
|
2013-06-16 11:13:52 +00:00
|
|
|
[ $x[1], $y + $m->{y_offset} ],
|
|
|
|
[ $x[0], $y + $m->{y_short} - $m->{y_offset} ],
|
|
|
|
[ $x[0], $y + $m->{y_short} + $m->{hex_side} + $m->{y_offset} ],
|
|
|
|
[ $x[1], $y + $m->{y_short} + $m->{hex_side} + $m->{y_short} - $m->{y_offset} ],
|
|
|
|
[ $x[1], $y + $m->{y_short} + $m->{hex_side} + $m->{y_short} + $m->{hex_side} + $m->{y_offset} ];
|
2012-04-16 09:55:14 +00:00
|
|
|
}
|
2013-06-16 11:13:52 +00:00
|
|
|
@x = map $_ + $m->{distance}, reverse @x; # draw symmetrical pattern
|
|
|
|
$x += $m->{distance};
|
2012-04-16 09:55:14 +00:00
|
|
|
}
|
|
|
|
|
2013-07-05 12:29:57 +00:00
|
|
|
push @polygons, Slic3r::Polygon->new(@$p);
|
2012-04-16 09:55:14 +00:00
|
|
|
}
|
|
|
|
|
2013-06-16 11:13:52 +00:00
|
|
|
$_->rotate(-$rotate_vector->[0][0], $m->{hex_center}) for @polygons;
|
2012-04-16 09:55:14 +00:00
|
|
|
}
|
|
|
|
|
2013-06-16 11:31:10 +00:00
|
|
|
my @paths;
|
|
|
|
if ($params{complete}) {
|
|
|
|
# we were requested to complete each loop;
|
|
|
|
# in this case we don't try to make more continuous paths
|
|
|
|
@paths = map $_->split_at_first_point,
|
2013-09-03 22:10:53 +00:00
|
|
|
@{intersection([ $surface->p ], \@polygons)};
|
2013-06-16 11:31:10 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
# consider polygons as polylines without re-appending the initial point:
|
|
|
|
# this cuts the last segment on purpose, so that the jump to the next
|
|
|
|
# path is more straight
|
2013-11-21 13:15:38 +00:00
|
|
|
@paths = @{intersection_pl(
|
|
|
|
[ map Slic3r::Polyline->new(@$_), @polygons ],
|
|
|
|
[ @{$surface->expolygon} ],
|
|
|
|
)};
|
2013-06-16 11:31:10 +00:00
|
|
|
|
|
|
|
# connect paths
|
2013-11-21 15:21:42 +00:00
|
|
|
if (@paths) { # prevent calling leftmost_point() on empty collections
|
2013-08-29 22:06:10 +00:00
|
|
|
my $collection = Slic3r::Polyline::Collection->new(@paths);
|
2013-06-16 11:31:10 +00:00
|
|
|
@paths = ();
|
2013-11-21 13:15:38 +00:00
|
|
|
foreach my $path (@{$collection->chained_path_from($collection->leftmost_point, 0)}) {
|
2013-06-16 11:31:10 +00:00
|
|
|
if (@paths) {
|
|
|
|
# distance between first point of this path and last point of last path
|
2013-08-28 23:36:42 +00:00
|
|
|
my $distance = $paths[-1]->last_point->distance_to($path->first_point);
|
2013-06-16 11:31:10 +00:00
|
|
|
|
|
|
|
if ($distance <= $m->{hex_width}) {
|
2013-08-28 23:36:42 +00:00
|
|
|
$paths[-1]->append_polyline($path);
|
2013-06-16 11:31:10 +00:00
|
|
|
next;
|
|
|
|
}
|
2013-06-16 11:26:57 +00:00
|
|
|
}
|
2013-09-02 18:22:20 +00:00
|
|
|
|
|
|
|
# make a clone before $collection goes out of scope
|
|
|
|
push @paths, $path->clone;
|
2013-06-16 11:26:57 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-16 11:31:10 +00:00
|
|
|
|
|
|
|
# clip paths again to prevent connection segments from crossing the expolygon boundaries
|
2013-11-21 13:15:38 +00:00
|
|
|
@paths = @{intersection_pl(
|
|
|
|
\@paths,
|
|
|
|
[ @{$surface->expolygon->offset_ex(scaled_epsilon)} ],
|
|
|
|
)};
|
2013-06-16 11:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return { flow_spacing => $params{flow_spacing} }, @paths;
|
2012-04-16 09:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|