Improvements to SectionCut

This commit is contained in:
Alessandro Ranellucci 2016-12-19 14:58:05 +01:00
parent f2d8ab5b87
commit 22708fe126
2 changed files with 133 additions and 86 deletions

View File

@ -4,7 +4,7 @@
package Slic3r::Test::SectionCut; package Slic3r::Test::SectionCut;
use Moo; use Moo;
use List::Util qw(first min max); use List::Util qw(any min max);
use Slic3r::Geometry qw(unscale); use Slic3r::Geometry qw(unscale);
use Slic3r::Geometry::Clipper qw(intersection_pl); use Slic3r::Geometry::Clipper qw(intersection_pl);
use SVG; use SVG;
@ -13,20 +13,27 @@ use Slic3r::SVG;
has 'print' => (is => 'ro', required => 1); has 'print' => (is => 'ro', required => 1);
has 'scale' => (is => 'ro', default => sub { 30 }); has 'scale' => (is => 'ro', default => sub { 30 });
has 'y_percent' => (is => 'ro', default => sub { 0.5 }); # Y coord of section line expressed as factor has 'y_percent' => (is => 'ro', default => sub { 0.5 }); # Y coord of section line expressed as factor
has 'line' => (is => 'rw'); has '_line' => (is => 'lazy');
has '_height' => (is => 'rw'); has '_height' => (is => 'rw');
has '_svg' => (is => 'rw'); has '_svg' => (is => 'rw');
has '_svg_style' => (is => 'rw', default => sub { {} }); has '_svg_style' => (is => 'rw', default => sub { {} });
has '_bb' => (is => 'lazy');
sub BUILD { sub _build__line {
my $self = shift; my $self = shift;
# calculate the Y coordinate of the section line # calculate the Y coordinate of the section line
my $bb = $self->print->bounding_box; my $bb = $self->_bb;
my $y = ($bb->y_min + $bb->y_max) * $self->y_percent; my $y = ($bb->y_min + $bb->y_max) * $self->y_percent;
# store our section line # store our section line
$self->line(Slic3r::Line->new([ $bb->x_min, $y ], [ $bb->x_max, $y ])); return Slic3r::Line->new([ $bb->x_min, $y ], [ $bb->x_max, $y ]);
}
sub _build__bb {
my ($self) = @_;
return $self->print->bounding_box;
} }
sub export_svg { sub export_svg {
@ -35,8 +42,7 @@ sub export_svg {
# get bounding box of print and its height # get bounding box of print and its height
# (Print should return a BoundingBox3 object instead) # (Print should return a BoundingBox3 object instead)
my $bb = $self->print->bounding_box; my $print_size = $self->_bb->size;
my $print_size = $bb->size;
$self->_height(max(map $_->print_z, map @{$_->layers}, @{$self->print->objects})); $self->_height(max(map $_->print_z, map @{$_->layers}, @{$self->print->objects}));
# initialize the SVG canvas # initialize the SVG canvas
@ -75,58 +81,86 @@ sub _plot_group {
my $self = shift; my $self = shift;
my ($filter) = @_; my ($filter) = @_;
my $bb = $self->print->bounding_box;
my $g = $self->_svg->group(style => { %{$self->_svg_style} });
foreach my $object (@{$self->print->objects}) { foreach my $object (@{$self->print->objects}) {
foreach my $copy (@{$object->_shifted_copies}) {
foreach my $layer (@{$object->layers}, @{$object->support_layers}) { foreach my $layer (@{$object->layers}, @{$object->support_layers}) {
# get all ExtrusionPath objects my @paths = map $_->clone, map @{$_->flatten}, grep defined $_, $filter->($layer);
my @paths = map $_->clone,
map { ($_->isa('Slic3r::ExtrusionLoop') || $_->isa('Slic3r::ExtrusionPath::Collection')) ? @$_ : $_ }
grep defined $_,
$filter->($layer);
# move paths to location of copy my $name = sprintf "%s %d (z = %f)",
$_->polyline->translate(@$copy) for @paths; ($layer->isa('Slic3r::Layer::Support') ? 'Support Layer' : 'Layer'),
$layer->id,
$layer->print_z;
my $g = $self->_svg->getElementByID($name)
|| $self->_svg->group(id => $name, style => { %{$self->_svg_style} });
foreach my $copy (@{$object->_shifted_copies}) {
if (0) { if (0) {
# export plan with section line and exit # export plan with section line and exit
my @grown = map @{$_->grow}, @paths;
$_->translate(@$copy) for @paths;
require "Slic3r/SVG.pm"; require "Slic3r/SVG.pm";
Slic3r::SVG::output( Slic3r::SVG::output(
"line.svg", "line.svg",
no_arrows => 1, no_arrows => 1,
lines => [ $self->line ], polygons => \@grown,
red_polylines => [ map $_->polyline, @paths ], red_lines => [ $self->_line ],
); );
exit; exit;
} }
foreach my $path (@paths) { $self->_plot_path($_, $g, $copy, $layer) for @paths;
foreach my $line (@{$path->lines}) { }
my @intersections = @{intersection_pl( }
[ $self->line->as_polyline ], }
$line->grow(Slic3r::Geometry::scale $path->width/2), }
)};
die "Intersection has more than two points!\n" sub _plot_path {
if defined first { @$_ > 2 } @intersections; my ($self, $path, $g, $copy, $layer) = @_;
# turn intersections to lines my $grown = $path->grow;
my @lines = map Slic3r::Line->new(@$_), @intersections; $_->translate(@$copy) for @$grown;
my $intersections = intersection_pl(
[ $self->_line->as_polyline ],
$grown,
);
# align intersections to canvas if (0 && @$intersections) {
$_->translate(-$bb->x_min, 0) for @lines; # export plan with section line and exit
require "Slic3r/SVG.pm";
# we want lines oriented from left to right in order to draw Slic3r::SVG::output(
# rectangles correctly "intersections.svg",
foreach my $line (@lines) { no_arrows => 1,
$line->reverse if $line->a->x > $line->b->x; polygons => $grown,
red_lines => [ $self->_line ],
);
exit;
} }
if ($path->is_bridge) { # turn intersections to lines
die "Intersection has more than two points!\n"
if any { @$_ > 2 } @$intersections;
my @lines = map Slic3r::Line->new(@$_), @$intersections;
my $is_bridge = $path->isa('Slic3r::ExtrusionPath')
? $path->is_bridge
: any { $_->is_bridge } @$path;
foreach my $line (@lines) { foreach my $line (@lines) {
my $radius = $path->width / 2; my $this_path = $path;
if ($path->isa('Slic3r::ExtrusionLoop')) {
# FIXME: find the actual ExtrusionPath of this intersection
$this_path = $path->[0];
}
# align to canvas
$line->translate(-$self->_bb->x_min, 0);
# we want lines oriented from left to right in order to draw rectangles correctly
$line->reverse if $line->a->x > $line->b->x;
if ($is_bridge) {
my $radius = $this_path->width / 2;
my $width = unscale abs($line->b->x - $line->a->x); my $width = unscale abs($line->b->x - $line->a->x);
if ((10 * $radius) < $width) { if ((10 * $radius) < $width) {
# we're cutting the path in the longitudinal direction, so we've got a rectangle # we're cutting the path in the longitudinal direction, so we've got a rectangle
@ -145,11 +179,8 @@ sub _plot_group {
'r' => $self->scale * $radius, 'r' => $self->scale * $radius,
); );
} }
}
} else { } else {
foreach my $line (@lines) { my $height = $this_path->height != -1 ? $this_path->height : $layer->height;
my $height = $path->height;
$height = $layer->height if $height == -1;
$g->rectangle( $g->rectangle(
'x' => $self->scale * unscale($line->a->x), 'x' => $self->scale * unscale($line->a->x),
'y' => $self->scale * $self->_y($layer->print_z), 'y' => $self->scale * $self->_y($layer->print_z),
@ -160,11 +191,6 @@ sub _plot_group {
); );
} }
} }
}
}
}
}
}
} }
sub _y { sub _y {

View File

@ -3,6 +3,7 @@
#include "ExPolygonCollection.hpp" #include "ExPolygonCollection.hpp"
#include "ClipperUtils.hpp" #include "ClipperUtils.hpp"
#include "Extruder.hpp" #include "Extruder.hpp"
#include <algorithm>
#include <cmath> #include <cmath>
#include <limits> #include <limits>
#include <sstream> #include <sstream>
@ -224,10 +225,30 @@ ExtrusionLoop::has_overhang_point(const Point &point) const
Polygons Polygons
ExtrusionLoop::grow() const ExtrusionLoop::grow() const
{ {
Polygons pp; if (this->paths.empty()) return Polygons();
// collect all the path widths
std::vector<float> widths;
for (ExtrusionPaths::const_iterator path = this->paths.begin(); path != this->paths.end(); ++path)
widths.push_back(path->width);
// grow this polygon with the minimum common width
// (this ensures vertices are grown correctly, which doesn't happen if we just
// union the paths grown individually)
const float min_width = *std::min_element(widths.begin(), widths.end());
const Polygon p = this->polygon();
Polygons pp = diff(
offset(p, +scale_(min_width/2)),
offset(p, -scale_(min_width/2))
);
// if we have thicker segments, grow them
if (min_width != *std::max_element(widths.begin(), widths.end())) {
for (ExtrusionPaths::const_iterator path = this->paths.begin(); path != this->paths.end(); ++path) for (ExtrusionPaths::const_iterator path = this->paths.begin(); path != this->paths.end(); ++path)
append_to(pp, path->grow()); append_to(pp, path->grow());
return pp; }
return union_(pp);
} }
double double