Ported ExtrusionPath::Collection->chained_path

This commit is contained in:
Alessandro Ranellucci 2013-08-29 11:47:59 +02:00
parent ea1d138c95
commit bd7b0e2aed
11 changed files with 170 additions and 57 deletions

View File

@ -2,42 +2,6 @@ package Slic3r::ExtrusionPath::Collection;
use strict; use strict;
use warnings; use warnings;
sub first_point {
my $self = shift;
return $self->[0]->[0];
}
# (Same algorithm as Polyline::Collection)
sub chained_path {
my $self = shift;
my ($start_near, $no_reverse) = @_;
my @my_paths = @$self;
return @my_paths if $self->no_sort;
my @paths = ();
my $start_at;
my $endpoints = $no_reverse
? [ map { @$_[0,0] } @my_paths ]
: [ map { @$_[0,-1] } @my_paths ];
while (@my_paths) {
# find nearest point
my $start_index = defined $start_near
? $start_near->nearest_point_index($endpoints)
: 0;
my $path_index = int($start_index/2);
if ($start_index % 2 && !$no_reverse) { # index is end so reverse to make it the start
# path is reversed in place, but we got a copy from XS
$my_paths[$path_index]->reverse;
}
push @paths, splice @my_paths, $path_index, 1;
splice @$endpoints, $path_index*2, 2;
$start_near = $paths[-1]->last_point;
}
return @paths;
}
sub cleanup { sub cleanup {
my $self = shift; my $self = shift;

View File

@ -228,7 +228,7 @@ sub extrude_loop {
$extrusion_path->intersect_expolygons($self->_layer_overhangs); $extrusion_path->intersect_expolygons($self->_layer_overhangs);
# reapply the nearest point search for starting point # reapply the nearest point search for starting point
@paths = Slic3r::ExtrusionPath::Collection->new(@paths)->chained_path($start_at, 1); @paths = @{Slic3r::ExtrusionPath::Collection->new(@paths)->chained_path_from($start_at, 1)};
} else { } else {
push @paths, $extrusion_path; push @paths, $extrusion_path;
} }

View File

@ -111,12 +111,12 @@ sub process_layer {
if ($layer->support_interface_fills) { if ($layer->support_interface_fills) {
$gcode .= $self->gcodegen->set_extruder($self->extruders->[$Slic3r::Config->support_material_interface_extruder-1]); $gcode .= $self->gcodegen->set_extruder($self->extruders->[$Slic3r::Config->support_material_interface_extruder-1]);
$gcode .= $self->gcodegen->extrude_path($_, 'support material interface') $gcode .= $self->gcodegen->extrude_path($_, 'support material interface')
for $layer->support_interface_fills->chained_path($self->gcodegen->last_pos); for @{$layer->support_interface_fills->chained_path_from($self->gcodegen->last_pos, 0)};
} }
if ($layer->support_fills) { if ($layer->support_fills) {
$gcode .= $self->gcodegen->set_extruder($self->extruders->[$Slic3r::Config->support_material_extruder-1]); $gcode .= $self->gcodegen->set_extruder($self->extruders->[$Slic3r::Config->support_material_extruder-1]);
$gcode .= $self->gcodegen->extrude_path($_, 'support material') $gcode .= $self->gcodegen->extrude_path($_, 'support material')
for $layer->support_fills->chained_path($self->gcodegen->last_pos); for @{$layer->support_fills->chained_path_from($self->gcodegen->last_pos, 0)};
} }
} }
@ -214,7 +214,7 @@ sub _extrude_infill {
for my $fill (@{ $island->{fills} }) { for my $fill (@{ $island->{fills} }) {
if ($fill->isa('Slic3r::ExtrusionPath::Collection')) { if ($fill->isa('Slic3r::ExtrusionPath::Collection')) {
$gcode .= $self->gcodegen->extrude($_, 'fill') $gcode .= $self->gcodegen->extrude($_, 'fill')
for $fill->chained_path($self->gcodegen->last_pos); for @{$fill->chained_path_from($self->gcodegen->last_pos, 0)};
} else { } else {
$gcode .= $self->gcodegen->extrude($fill, 'fill') ; $gcode .= $self->gcodegen->extrude($fill, 'fill') ;
} }

View File

@ -300,15 +300,13 @@ sub make_perimeters {
$self->perimeters->append(@loops); $self->perimeters->append(@loops);
# add thin walls as perimeters # add thin walls as perimeters
push @{ $self->perimeters }, Slic3r::ExtrusionPath::Collection->new( push @{ $self->perimeters }, @{Slic3r::ExtrusionPath::Collection->new(
map { map Slic3r::ExtrusionPath->new(
Slic3r::ExtrusionPath->new(
polyline => ($_->isa('Slic3r::Polygon') ? $_->split_at_first_point : $_), polyline => ($_->isa('Slic3r::Polygon') ? $_->split_at_first_point : $_),
role => EXTR_ROLE_EXTERNAL_PERIMETER, role => EXTR_ROLE_EXTERNAL_PERIMETER,
flow_spacing => $self->perimeter_flow->spacing, flow_spacing => $self->perimeter_flow->spacing,
); ), @{ $self->thin_walls }
} @{ $self->thin_walls } )->chained_path(0)};
)->chained_path;
} }
sub _fill_gaps { sub _fill_gaps {

View File

@ -115,7 +115,7 @@ sub scale_points (@) { map [scale $_->[X], scale $_->[Y]], @_ }
Slic3r::Polyline->new([0,10], [0,8], [0,5]), Slic3r::Polyline->new([0,10], [0,8], [0,5]),
]); ]);
is_deeply is_deeply
[ map $_->[Y], map @$_, $collection->chained_path(Slic3r::Point->new(0,30)) ], [ map $_->[Y], map @$_, $collection->chained_path(Slic3r::Point->new(0,30), 0) ],
[20, 18, 15, 10, 8, 5], [20, 18, 15, 10, 8, 5],
'chained path'; 'chained path';
} }
@ -126,7 +126,7 @@ sub scale_points (@) { map [scale $_->[X], scale $_->[Y]], @_ }
Slic3r::Polyline->new([10,5], [15,5], [20,5]), Slic3r::Polyline->new([10,5], [15,5], [20,5]),
]); ]);
is_deeply is_deeply
[ map $_->[X], map @$_, $collection->chained_path(Slic3r::Point->new(30,0)) ], [ map $_->[X], map @$_, $collection->chained_path(Slic3r::Point->new(30,0), 0) ],
[reverse 4, 10, 15, 10, 15, 20], [reverse 4, 10, 15, 10, 15, 20],
'chained path'; 'chained path';
} }
@ -138,7 +138,7 @@ sub scale_points (@) { map [scale $_->[X], scale $_->[Y]], @_ }
Slic3r::Polyline->new([0,10], [0,8], [0,5]), Slic3r::Polyline->new([0,10], [0,8], [0,5]),
); );
is_deeply is_deeply
[ map $_->[Y], map @{$_->polyline}, $collection->chained_path(Slic3r::Point->new(0,30)) ], [ map $_->[Y], map @{$_->polyline}, @{$collection->chained_path_from(Slic3r::Point->new(0,30), 0)} ],
[20, 18, 15, 10, 8, 5], [20, 18, 15, 10, 8, 5],
'chained path'; 'chained path';
} }
@ -150,7 +150,7 @@ sub scale_points (@) { map [scale $_->[X], scale $_->[Y]], @_ }
Slic3r::Polyline->new([10,5], [15,5], [20,5]), Slic3r::Polyline->new([10,5], [15,5], [20,5]),
); );
is_deeply is_deeply
[ map $_->[X], map @{$_->polyline}, $collection->chained_path(Slic3r::Point->new(30,0)) ], [ map $_->[X], map @{$_->polyline}, @{$collection->chained_path_from(Slic3r::Point->new(30,0), 0)} ],
[reverse 4, 10, 15, 10, 15, 20], [reverse 4, 10, 15, 10, 15, 20],
'chained path'; 'chained path';
} }

View File

@ -2,24 +2,36 @@
namespace Slic3r { namespace Slic3r {
ExtrusionPath*
ExtrusionPath::clone() const
{
return new ExtrusionPath (*this);
}
void void
ExtrusionPath::reverse() ExtrusionPath::reverse()
{ {
this->polyline.reverse(); this->polyline.reverse();
} }
const Point* Point*
ExtrusionPath::first_point() const ExtrusionPath::first_point()
{ {
return &(this->polyline.points.front()); return &(this->polyline.points.front());
} }
const Point* Point*
ExtrusionPath::last_point() const ExtrusionPath::last_point()
{ {
return &(this->polyline.points.back()); return &(this->polyline.points.back());
} }
ExtrusionLoop*
ExtrusionLoop::clone() const
{
return new ExtrusionLoop (*this);
}
ExtrusionPath* ExtrusionPath*
ExtrusionLoop::split_at_index(int index) ExtrusionLoop::split_at_index(int index)
{ {
@ -47,4 +59,22 @@ ExtrusionLoop::make_counter_clockwise()
return this->polygon.make_counter_clockwise(); return this->polygon.make_counter_clockwise();
} }
void
ExtrusionLoop::reverse()
{
// no-op
}
Point*
ExtrusionLoop::first_point()
{
return &(this->polygon.points.front());
}
Point*
ExtrusionLoop::last_point()
{
return &(this->polygon.points.front()); // in polygons, first == last
}
} }

View File

@ -25,10 +25,14 @@ enum ExtrusionRole {
class ExtrusionEntity class ExtrusionEntity
{ {
public: public:
virtual ExtrusionEntity* clone() const = 0;
virtual ~ExtrusionEntity() {}; virtual ~ExtrusionEntity() {};
ExtrusionRole role; ExtrusionRole role;
double height; // vertical thickness of the extrusion expressed in mm double height; // vertical thickness of the extrusion expressed in mm
double flow_spacing; double flow_spacing;
virtual void reverse() = 0;
virtual Point* first_point() = 0;
virtual Point* last_point() = 0;
}; };
typedef std::vector<ExtrusionEntity*> ExtrusionEntitiesPtr; typedef std::vector<ExtrusionEntity*> ExtrusionEntitiesPtr;
@ -36,19 +40,24 @@ typedef std::vector<ExtrusionEntity*> ExtrusionEntitiesPtr;
class ExtrusionPath : public ExtrusionEntity class ExtrusionPath : public ExtrusionEntity
{ {
public: public:
ExtrusionPath* clone() const;
Polyline polyline; Polyline polyline;
void reverse(); void reverse();
const Point* first_point() const; Point* first_point();
const Point* last_point() const; Point* last_point();
}; };
class ExtrusionLoop : public ExtrusionEntity class ExtrusionLoop : public ExtrusionEntity
{ {
public: public:
ExtrusionLoop* clone() const;
Polygon polygon; Polygon polygon;
ExtrusionPath* split_at_index(int index); ExtrusionPath* split_at_index(int index);
ExtrusionPath* split_at_first_point(); ExtrusionPath* split_at_first_point();
bool make_counter_clockwise(); bool make_counter_clockwise();
void reverse();
Point* first_point();
Point* last_point();
}; };
} }

View File

@ -0,0 +1,75 @@
#include "ExtrusionEntityCollection.hpp"
namespace Slic3r {
ExtrusionEntityCollection*
ExtrusionEntityCollection::clone() const
{
return new ExtrusionEntityCollection (*this);
}
void
ExtrusionEntityCollection::reverse()
{
for (ExtrusionEntitiesPtr::iterator it = this->entities.begin(); it != this->entities.end(); ++it) {
(*it)->reverse();
}
std::reverse(this->entities.begin(), this->entities.end());
}
Point*
ExtrusionEntityCollection::first_point()
{
return this->entities.front()->first_point();
}
Point*
ExtrusionEntityCollection::last_point()
{
return this->entities.back()->last_point();
}
ExtrusionEntityCollection*
ExtrusionEntityCollection::chained_path(bool no_reverse) const
{
if (this->entities.empty()) {
return new ExtrusionEntityCollection ();
}
return this->chained_path_from(this->entities.front()->first_point(), no_reverse);
}
ExtrusionEntityCollection*
ExtrusionEntityCollection::chained_path_from(Point* start_near, bool no_reverse) const
{
if (this->no_sort) return new ExtrusionEntityCollection(*this);
ExtrusionEntityCollection* retval = new ExtrusionEntityCollection;
ExtrusionEntitiesPtr my_paths = this->entities;
Points endpoints;
for (ExtrusionEntitiesPtr::iterator it = my_paths.begin(); it != my_paths.end(); ++it) {
endpoints.push_back(*(*it)->first_point());
if (no_reverse) {
endpoints.push_back(*(*it)->first_point());
} else {
endpoints.push_back(*(*it)->last_point());
}
}
while (!my_paths.empty()) {
// find nearest point
int start_index = start_near->nearest_point_index(endpoints);
int path_index = start_index/2;
if (start_index % 2 && !no_reverse) {
my_paths.at(path_index) = my_paths.at(path_index)->clone(); // maybe we should clone them all when building my_paths?
my_paths.at(path_index)->reverse();
}
retval->entities.push_back(my_paths.at(path_index));
my_paths.erase(my_paths.begin() + path_index);
endpoints.erase(endpoints.begin() + 2*path_index, endpoints.begin() + 2*path_index + 2);
start_near = retval->entities.back()->last_point();
}
return retval;
}
}

View File

@ -9,9 +9,15 @@ namespace Slic3r {
class ExtrusionEntityCollection : public ExtrusionEntity class ExtrusionEntityCollection : public ExtrusionEntity
{ {
public: public:
ExtrusionEntityCollection* clone() const;
ExtrusionEntitiesPtr entities; ExtrusionEntitiesPtr entities;
bool no_sort; bool no_sort;
ExtrusionEntityCollection(): no_sort(false) {}; ExtrusionEntityCollection(): no_sort(false) {};
ExtrusionEntityCollection* chained_path(bool no_reverse) const;
ExtrusionEntityCollection* chained_path_from(Point* start_near, bool no_reverse) const;
void reverse();
Point* first_point();
Point* last_point();
}; };
} }

View File

@ -4,7 +4,7 @@ use strict;
use warnings; use warnings;
use Slic3r::XS; use Slic3r::XS;
use Test::More tests => 9; use Test::More tests => 12;
my $points = [ my $points = [
[100, 100], [100, 100],
@ -41,5 +41,32 @@ isa_ok $collection->[3], 'Slic3r::ExtrusionLoop', 'correct object returned for l
is scalar(@{$collection->[1]}), 1, 'appended collection was duplicated'; is scalar(@{$collection->[1]}), 1, 'appended collection was duplicated';
{
my $collection = Slic3r::ExtrusionPath::Collection->new(
map Slic3r::ExtrusionPath->new(polyline => $_, role => 0),
Slic3r::Polyline->new([0,15], [0,18], [0,20]),
Slic3r::Polyline->new([0,10], [0,8], [0,5]),
);
is_deeply
[ map $_->y, map @{$_->polyline}, @{$collection->chained_path_from(Slic3r::Point->new(0,30), 0)} ],
[20, 18, 15, 10, 8, 5],
'chained_path_from';
is_deeply
[ map $_->y, map @{$_->polyline}, @{$collection->chained_path(0)} ],
[15, 18, 20, 10, 8, 5],
'chained_path';
}
{
my $collection = Slic3r::ExtrusionPath::Collection->new(
map Slic3r::ExtrusionPath->new(polyline => $_, role => 0),
Slic3r::Polyline->new([15,0], [10,0], [4,0]),
Slic3r::Polyline->new([10,5], [15,5], [20,5]),
);
is_deeply
[ map $_->x, map @{$_->polyline}, @{$collection->chained_path_from(Slic3r::Point->new(30,0), 0)} ],
[reverse 4, 10, 15, 10, 15, 20],
'chained_path_from';
}
__END__ __END__

View File

@ -10,6 +10,10 @@
~ExtrusionEntityCollection(); ~ExtrusionEntityCollection();
void clear() void clear()
%code{% THIS->entities.clear(); %}; %code{% THIS->entities.clear(); %};
ExtrusionEntityCollection* chained_path(bool no_reverse)
%code{% const char* CLASS = "Slic3r::ExtrusionPath::Collection"; RETVAL = THIS->chained_path(no_reverse); %};
ExtrusionEntityCollection* chained_path_from(Point* start_near, bool no_reverse)
%code{% const char* CLASS = "Slic3r::ExtrusionPath::Collection"; RETVAL = THIS->chained_path_from(start_near, no_reverse); %};
%{ %{
SV* SV*