Optimizations for better usage of XS code

This commit is contained in:
Alessandro Ranellucci 2013-08-29 01:36:42 +02:00
parent 9254ff9704
commit 5d6fd7f4d9
22 changed files with 68 additions and 42 deletions

View File

@ -210,7 +210,7 @@ sub medial_axis {
# cleanup # cleanup
$polyline = Slic3r::Geometry::douglas_peucker($polyline, $width / 7); $polyline = Slic3r::Geometry::douglas_peucker($polyline, $width / 7);
if (Slic3r::Geometry::same_point($polyline->[0], $polyline->[-1])) { if (Slic3r::Geometry::same_point($polyline->first_point, $polyline->last_point)) {
next if @$polyline == 2; next if @$polyline == 2;
push @result, Slic3r::Polygon->new(@$polyline[0..$#$polyline-1]); push @result, Slic3r::Polygon->new(@$polyline[0..$#$polyline-1]);
} else { } else {

View File

@ -13,11 +13,6 @@ sub split_at {
); );
} }
sub first_point {
my $self = shift;
return $self->polygon->[0];
}
sub make_counter_clockwise { sub make_counter_clockwise {
my $self = shift; my $self = shift;
if (!$self->polygon->is_counter_clockwise) { if (!$self->polygon->is_counter_clockwise) {

View File

@ -65,11 +65,6 @@ sub points {
return $self->polyline; return $self->polyline;
} }
sub last_point {
my $self = shift;
return $self->polyline->[-1];
}
sub is_perimeter { sub is_perimeter {
my $self = shift; my $self = shift;
return $self->role == EXTR_ROLE_PERIMETER return $self->role == EXTR_ROLE_PERIMETER

View File

@ -33,7 +33,7 @@ sub chained_path {
} }
push @paths, splice @my_paths, $path_index, 1; push @paths, splice @my_paths, $path_index, 1;
splice @$endpoints, $path_index*2, 2; splice @$endpoints, $path_index*2, 2;
$start_near = $paths[-1][-1]; $start_near = $paths[-1]->last_point;
} }
return @paths; return @paths;
} }

View File

@ -194,7 +194,7 @@ sub make_fill {
flow_spacing => $params->{flow_spacing} || (warn "Warning: no flow_spacing was returned by the infill engine, please report this to the developer\n"), flow_spacing => $params->{flow_spacing} || (warn "Warning: no flow_spacing was returned by the infill engine, please report this to the developer\n"),
), @polylines, ), @polylines,
); );
push @fills_ordering_points, $polylines[0][0]; push @fills_ordering_points, $polylines[0]->first_point;
} }
# add thin fill regions # add thin fill regions

View File

@ -101,10 +101,10 @@ sub fill_surface {
foreach my $path ($collection->chained_path) { foreach my $path ($collection->chained_path) {
if (@paths) { if (@paths) {
# distance between first point of this path and last point of last path # distance between first point of this path and last point of last path
my $distance = $paths[-1][-1]->distance_to($path->[0]); my $distance = $paths[-1]->last_point->distance_to($path->first_point);
if ($distance <= $m->{hex_width}) { if ($distance <= $m->{hex_width}) {
$paths[-1]->append(@$path); $paths[-1]->append_polyline($path);
next; next;
} }
} }

View File

@ -42,7 +42,7 @@ sub fill_surface {
my $x_max = $bounding_box->x_max + scaled_epsilon; my $x_max = $bounding_box->x_max + scaled_epsilon;
my @vertical_lines = (); my @vertical_lines = ();
while ($x <= $x_max) { while ($x <= $x_max) {
my $vertical_line = Slic3r::Line->new([$x, $bounding_box->y_max], [$x, $bounding_box->y_min]); my $vertical_line = [ [$x, $bounding_box->y_max], [$x, $bounding_box->y_min] ];
if ($is_line_pattern && $i % 2) { if ($is_line_pattern && $i % 2) {
$vertical_line->[A][X] += $line_oscillation; $vertical_line->[A][X] += $line_oscillation;
$vertical_line->[B][X] -= $line_oscillation; $vertical_line->[B][X] -= $line_oscillation;
@ -60,7 +60,7 @@ sub fill_surface {
my @polylines = map Slic3r::Polyline->new(@$_), my @polylines = map Slic3r::Polyline->new(@$_),
@{ Boost::Geometry::Utils::multi_polygon_multi_linestring_intersection( @{ Boost::Geometry::Utils::multi_polygon_multi_linestring_intersection(
[ map $_->pp, @{$expolygon->offset_ex($line_spacing*0.05)} ], [ map $_->pp, @{$expolygon->offset_ex($line_spacing*0.05)} ],
[ map $_->pp, @vertical_lines ], [ @vertical_lines ],
) }; ) };
# connect lines # connect lines
@ -82,13 +82,14 @@ sub fill_surface {
foreach my $polyline ($collection->chained_path) { foreach my $polyline ($collection->chained_path) {
if (@polylines) { if (@polylines) {
my $last_point = $polylines[-1][-1]->pp; my $first_point = $polyline->first_point;
my @distance = map abs($polyline->[0][$_] - $last_point->[$_]), (X,Y); my $last_point = $polylines[-1]->last_point;
my @distance = map abs($first_point->$_ - $last_point->$_), qw(x y);
# TODO: we should also check that both points are on a fill_boundary to avoid # TODO: we should also check that both points are on a fill_boundary to avoid
# connecting paths on the boundaries of internal regions # connecting paths on the boundaries of internal regions
if ($can_connect->(@distance) && $expolygon_off->encloses_line(Slic3r::Line->new($last_point, $polyline->[0]), $tolerance)) { if ($can_connect->(@distance) && $expolygon_off->encloses_line(Slic3r::Line->new($last_point, $first_point), $tolerance)) {
$polylines[-1]->append(@$polyline); $polylines[-1]->append_polyline($polyline);
next; next;
} }
} }

View File

@ -274,9 +274,11 @@ sub extrude_path {
# go to first point of extrusion path # go to first point of extrusion path
my $gcode = ""; my $gcode = "";
{
my $first_point = $path->first_point; my $first_point = $path->first_point;
$gcode .= $self->travel_to($first_point, $path->role, "move to first $description point") $gcode .= $self->travel_to($first_point, $path->role, "move to first $description point")
if !defined $self->last_pos || !$self->last_pos->coincides_with($first_point); if !defined $self->last_pos || !$self->last_pos->coincides_with($first_point);
}
# compensate retraction # compensate retraction
$gcode .= $self->unretract; $gcode .= $self->unretract;
@ -330,9 +332,10 @@ sub extrude_path {
$E = $self->extruder->extrude($e * $line_length) if $e; $E = $self->extruder->extrude($e * $line_length) if $e;
# compose G-code line # compose G-code line
my $point = $line->b;
$gcode .= sprintf "G1 X%.3f Y%.3f", $gcode .= sprintf "G1 X%.3f Y%.3f",
($line->[B]->x * &Slic3r::SCALING_FACTOR) + $self->shift_x - $self->extruder->extruder_offset->[X], ($point->x * &Slic3r::SCALING_FACTOR) + $self->shift_x - $self->extruder->extruder_offset->[X],
($line->[B]->y * &Slic3r::SCALING_FACTOR) + $self->shift_y - $self->extruder->extruder_offset->[Y]; #** ($point->y * &Slic3r::SCALING_FACTOR) + $self->shift_y - $self->extruder->extruder_offset->[Y]; #**
$gcode .= sprintf(" %s%.5f", $self->config->extrusion_axis, $E) $gcode .= sprintf(" %s%.5f", $self->config->extrusion_axis, $E)
if $E; if $E;
$gcode .= " F$local_F" $gcode .= " F$local_F"
@ -344,8 +347,10 @@ sub extrude_path {
# only include F in the first line # only include F in the first line
$local_F = 0; $local_F = 0;
} }
$self->wipe_path(Slic3r::Polyline->new(reverse @{$path->points})) if ($self->enable_wipe) {
if $self->enable_wipe; $self->wipe_path($path->polyline);
$self->wipe_path->reverse;
}
} }
$gcode .= ";_BRIDGE_FAN_END\n" if $path->is_bridge; $gcode .= ";_BRIDGE_FAN_END\n" if $path->is_bridge;
$self->last_pos($path->last_point); $self->last_pos($path->last_point);

View File

@ -80,8 +80,8 @@ sub BUILD {
for my $i (0 .. $#outer) { for my $i (0 .. $#outer) {
foreach my $line ($outer[$i]->lines) { foreach my $line ($outer[$i]->lines) {
my $dist = $line->length; my $dist = $line->length;
$edges->{$line->[A]}{$line->[B]} = $dist; $edges->{$line->a}{$line->b} = $dist;
$edges->{$line->[B]}{$line->[A]} = $dist; $edges->{$line->b}{$line->a} = $dist;
} }
} }

View File

@ -589,8 +589,8 @@ sub _detect_bridge_direction {
# remove any line not having both endpoints within anchors # remove any line not having both endpoints within anchors
@clipped_lines = grep { @clipped_lines = grep {
my $line = $_; my $line = $_;
!(first { $_->encloses_point_quick($line->[A]) } @$anchors) !(first { $_->encloses_point_quick($line->a) } @$anchors)
&& !(first { $_->encloses_point_quick($line->[B]) } @$anchors); && !(first { $_->encloses_point_quick($line->b) } @$anchors);
} @clipped_lines; } @clipped_lines;
# sum length of bridged lines # sum length of bridged lines

View File

@ -37,14 +37,6 @@ sub point_on_left {
return Slic3r::Geometry::point_is_on_left_of_segment($point, $self); return Slic3r::Geometry::point_is_on_left_of_segment($point, $self);
} }
sub midpoint {
my $self = shift;
return Slic3r::Point->new(
($self->[A][X] + $self->[B][X]) / 2,
($self->[A][Y] + $self->[B][Y]) / 2,
);
}
sub grow { sub grow {
my $self = shift; my $self = shift;
return Slic3r::Polyline->new(@$self[0,1,0])->grow(@_); return Slic3r::Polyline->new(@$self[0,1,0])->grow(@_);

View File

@ -192,7 +192,7 @@ sub chained_path {
} }
push @paths, splice @my_paths, $path_index, 1; push @paths, splice @my_paths, $path_index, 1;
splice @$endpoints, $path_index*2, 2; splice @$endpoints, $path_index*2, 2;
$start_near = $paths[-1][-1]; $start_near = $paths[-1]->last_point;
} }
return @paths; return @paths;
} }

View File

@ -14,6 +14,12 @@ ExtrusionPath::first_point() const
return &(this->polyline.points.front()); return &(this->polyline.points.front());
} }
const Point*
ExtrusionPath::last_point() const
{
return &(this->polyline.points.back());
}
ExtrusionPath* ExtrusionPath*
ExtrusionLoop::split_at_index(int index) ExtrusionLoop::split_at_index(int index)
{ {

View File

@ -39,6 +39,7 @@ class ExtrusionPath : public ExtrusionEntity
Polyline polyline; Polyline polyline;
void reverse(); void reverse();
const Point* first_point() const; const Point* first_point() const;
const Point* last_point() const;
}; };
class ExtrusionLoop : public ExtrusionEntity class ExtrusionLoop : public ExtrusionEntity

View File

@ -36,6 +36,12 @@ Line::length() const
return this->a.distance_to(&(this->b)); return this->a.distance_to(&(this->b));
} }
Point*
Line::midpoint() const
{
return new Point ((this->a.x + this->b.x) / 2.0, (this->a.y + this->b.y) / 2.0);
}
void void
Line::from_SV(SV* line_sv) Line::from_SV(SV* line_sv)
{ {

View File

@ -23,6 +23,7 @@ class Line
void rotate(double angle, Point* center); void rotate(double angle, Point* center);
void reverse(); void reverse();
double length() const; double length() const;
Point* midpoint() const;
}; };
typedef std::vector<Line> Lines; typedef std::vector<Line> Lines;

View File

@ -38,6 +38,12 @@ MultiPoint::first_point() const
return &(this->points.front()); return &(this->points.front());
} }
const Point*
MultiPoint::last_point() const
{
return &(this->points.back());
}
void void
MultiPoint::from_SV(SV* poly_sv) MultiPoint::from_SV(SV* poly_sv)
{ {

View File

@ -20,6 +20,7 @@ class MultiPoint
void rotate(double angle, Point* center); void rotate(double angle, Point* center);
void reverse(); void reverse();
const Point* first_point() const; const Point* first_point() const;
const Point* last_point() const;
}; };
} }

View File

@ -4,7 +4,7 @@ use strict;
use warnings; use warnings;
use Slic3r::XS; use Slic3r::XS;
use Test::More tests => 4; use Test::More tests => 5;
my $points = [ my $points = [
[100, 100], [100, 100],
@ -24,4 +24,7 @@ is_deeply [ map $_->pp, @$lines ], [
[ [200, 100], [200, 200] ], [ [200, 100], [200, 200] ],
], 'polyline lines'; ], 'polyline lines';
$polyline->append_polyline($polyline->clone);
is_deeply $polyline->pp, [ @$points, @$points ], 'append_polyline';
__END__ __END__

View File

@ -18,6 +18,8 @@
%code{% RETVAL = THIS->polyline.lines(); %}; %code{% RETVAL = THIS->polyline.lines(); %};
Point* first_point() Point* first_point()
%code{% const char* CLASS = "Slic3r::Point"; RETVAL = new Point(*(THIS->first_point())); %}; %code{% const char* CLASS = "Slic3r::Point"; RETVAL = new Point(*(THIS->first_point())); %};
Point* last_point()
%code{% const char* CLASS = "Slic3r::Point"; RETVAL = new Point(*(THIS->last_point())); %};
%{ %{
ExtrusionPath* ExtrusionPath*

View File

@ -21,6 +21,8 @@
void scale(double factor); void scale(double factor);
void translate(double x, double y); void translate(double x, double y);
double length(); double length();
Point* midpoint()
%code{% const char* CLASS = "Slic3r::Point"; RETVAL = THIS->midpoint(); %};
%{ %{
Line* Line*

View File

@ -21,6 +21,8 @@
Lines lines(); Lines lines();
Point* first_point() Point* first_point()
%code{% const char* CLASS = "Slic3r::Point"; RETVAL = new Point(*(THIS->first_point())); %}; %code{% const char* CLASS = "Slic3r::Point"; RETVAL = new Point(*(THIS->first_point())); %};
Point* last_point()
%code{% const char* CLASS = "Slic3r::Point"; RETVAL = new Point(*(THIS->last_point())); %};
%{ %{
Polyline* Polyline*
@ -44,6 +46,14 @@ Polyline::append(...)
THIS->points.push_back(p); THIS->points.push_back(p);
} }
void
Polyline::append_polyline(polyline)
Polyline* polyline;
CODE:
for (Points::const_iterator it = polyline->points.begin(); it != polyline->points.end(); ++it) {
THIS->points.push_back((*it));
}
void void
Polyline::rotate(angle, center_sv) Polyline::rotate(angle, center_sv)
double angle; double angle;