Implement nearest point index and split at index to avoid search for point.

This commit is contained in:
Mark Hindess 2012-07-02 20:26:56 +01:00
parent 35fda7de23
commit 35ecd65e77
4 changed files with 31 additions and 16 deletions

View File

@ -5,7 +5,7 @@ use Moo;
has 'polygon' => ( has 'polygon' => (
is => 'rw', is => 'rw',
required => 1, required => 1,
handles => [qw(is_printable nearest_point_to reverse)], handles => [qw(is_printable nearest_point_index_to reverse)],
); );
has 'flow_spacing' => (is => 'rw'); has 'flow_spacing' => (is => 'rw');
@ -29,6 +29,23 @@ sub shortest_path {
return $self; return $self;
} }
sub split_at_index {
my $self = shift;
my ($index) = @_;
$self->deserialize;
my @new_points = ();
push @new_points, @{$self->polygon}[$index .. $#{$self->polygon}];
push @new_points, @{$self->polygon}[0 .. $index];
return Slic3r::ExtrusionPath->new(
polyline => Slic3r::Polyline->new(\@new_points),
role => $self->role,
flow_spacing => $self->flow_spacing,
);
}
sub split_at { sub split_at {
my $self = shift; my $self = shift;
my ($point) = @_; my ($point) = @_;
@ -47,21 +64,12 @@ sub split_at {
} }
die "Point not found" if $i == -1; die "Point not found" if $i == -1;
my @new_points = (); return $self->split_at_index($i);
push @new_points, @{$self->polygon}[$i .. $#{$self->polygon}];
push @new_points, @{$self->polygon}[0 .. $i];
return Slic3r::ExtrusionPath->new(
polyline => Slic3r::Polyline->new(\@new_points),
role => $self->role,
flow_spacing => $self->flow_spacing,
);
} }
sub split_at_first_point { sub split_at_first_point {
my $self = shift; my $self = shift;
$self->deserialize; return $self->split_at_index(0);
return $self->split_at($self->polygon->[0]);
} }
# although a loop doesn't have endpoints, this method is provided to allow # although a loop doesn't have endpoints, this method is provided to allow

View File

@ -57,10 +57,11 @@ sub fill_surface {
$loop->polygon->make_counter_clockwise; $loop->polygon->make_counter_clockwise;
# find the point of the loop that is closest to the current extruder position # find the point of the loop that is closest to the current extruder position
$cur_pos = $loop->nearest_point_to($cur_pos); my $index = $loop->nearest_point_index_to($cur_pos);
$cur_pos = $loop->polygon->[0];
# split the loop at the starting point and make a path # split the loop at the starting point and make a path
my $path = $loop->split_at($cur_pos); my $path = $loop->split_at_index($index);
$path->deserialize; $path->deserialize;
# clip the path to avoid the extruder to get exactly on the first point of the loop # clip the path to avoid the extruder to get exactly on the first point of the loop

View File

@ -103,10 +103,10 @@ sub extrude_loop {
$last_pos = Slic3r::Point->new(scale $Slic3r::print_center->[X], scale $Slic3r::bed_size->[Y]); $last_pos = Slic3r::Point->new(scale $Slic3r::print_center->[X], scale $Slic3r::bed_size->[Y]);
$last_pos->rotate(rand(2*PI), $Slic3r::print_center); $last_pos->rotate(rand(2*PI), $Slic3r::print_center);
} }
my $start_at = $loop->nearest_point_to($last_pos); my $start_index = $loop->nearest_point_index_to($last_pos);
# split the loop at the starting point and make a path # split the loop at the starting point and make a path
my $extrusion_path = $loop->split_at($start_at); my $extrusion_path = $loop->split_at_index($start_index);
$extrusion_path->deserialize; $extrusion_path->deserialize;
# clip the path to avoid the extruder to get exactly on the first point of the loop; # clip the path to avoid the extruder to get exactly on the first point of the loop;

View File

@ -97,6 +97,12 @@ sub nearest_point_to {
return Slic3r::Point->new($point); return Slic3r::Point->new($point);
} }
sub nearest_point_index_to {
my $self = shift;
my ($point) = @_;
return Slic3r::Geometry::nearest_point_index($point, $self);
}
sub has_segment { sub has_segment {
my $self = shift; my $self = shift;
my ($line) = @_; my ($line) = @_;