Fixes and improvements after testing with more complex objects

This commit is contained in:
Alessandro Ranellucci 2011-09-26 14:48:22 +02:00
parent bfad101c8c
commit 478047fad3
5 changed files with 70 additions and 5 deletions

View File

@ -86,7 +86,7 @@ sub point_in_polygon {
} }
# if point is not in polygon, let's check whether it belongs to the contour # if point is not in polygon, let's check whether it belongs to the contour
if (!$side) { if (!$side && 0) {
foreach my $line (polygon_lines($polygon)) { foreach my $line (polygon_lines($polygon)) {
# calculate the Y in line at X of the point # calculate the Y in line at X of the point
if ($line->[A][X] == $line->[B][X]) { if ($line->[A][X] == $line->[B][X]) {

View File

@ -119,6 +119,28 @@ sub make_polylines {
die "No point should be endpoint of less or more than 2 lines!" die "No point should be endpoint of less or more than 2 lines!"
if grep @$_ != 2, values %pointmap; if grep @$_ != 2, values %pointmap;
if (0) {
# defensive programming
for (keys %pointmap) {
next if @{$pointmap{$_}} == 2;
#use Slic3r::SVG;
#Slic3r::SVG::output_points($main::print, "points.svg", [ map [split /,/], keys %pointmap ], [ [split /,/, $_ ] ]);
die sprintf "No point should be endpoint of less or more than 2 lines (%d)!", scalar(@{$pointmap{$_}});
}
while (my @single_line_points = grep @{$pointmap{$_}} == 1, keys %pointmap) {
for my $point_id (@single_line_points) {
foreach my $lines (values %pointmap) {
next unless $pointmap{$point_id}->[0];
@$lines = grep $_ ne $pointmap{$point_id}->[0], @$lines;
}
delete $pointmap{$point_id};
}
}
}
# make a subroutine to remove lines from pointmap # make a subroutine to remove lines from pointmap
my $remove_line = sub { my $remove_line = sub {
my $line = shift; my $line = shift;
@ -167,6 +189,9 @@ sub make_surfaces {
my $self = shift; my $self = shift;
my ($polylines) = @_; my ($polylines) = @_;
#use Slic3r::SVG;
#Slic3r::SVG::output_polygons($main::print, "polylines.svg", [ map $_->p, @$polylines ]);
# count how many other polylines enclose each polyline # count how many other polylines enclose each polyline
# even = contour; odd = hole # even = contour; odd = hole
my %enclosing_polylines = (); my %enclosing_polylines = ();
@ -178,6 +203,7 @@ sub make_surfaces {
my $point = $polyline->points->[0]; my $point = $polyline->points->[0];
my $ordered_id = $polyline->id; my $ordered_id = $polyline->id;
# find polylines contaning $point, and thus $polyline
$enclosing_polylines{$polyline} = $enclosing_polylines{$polyline} =
[ grep $_->id ne $ordered_id && $_->encloses_point($point), @$polylines ]; [ grep $_->id ne $ordered_id && $_->encloses_point($point), @$polylines ];
$enclosing_polylines_count{$polyline} = scalar @{ $enclosing_polylines{$polyline} }; $enclosing_polylines_count{$polyline} = scalar @{ $enclosing_polylines{$polyline} };

View File

@ -14,7 +14,7 @@ has 'points' => (
sub id { sub id {
my $self = shift; my $self = shift;
return join ' - ', map $_->id, @{$self->points}; return join ' - ', sort map $_->id, @{$self->points};
} }
sub cast { sub cast {

View File

@ -94,7 +94,7 @@ sub _facet {
Slic3r::debugf "z: min = %.0f, max = %.0f\n", $min_z, $max_z; Slic3r::debugf "z: min = %.0f, max = %.0f\n", $min_z, $max_z;
# calculate the layer extents # calculate the layer extents
my ($min_layer, $max_layer) = map {$_ * $Slic3r::resolution / $Slic3r::layer_height} $min_z, $max_z; my ($min_layer, $max_layer) = map { sprintf '%.0f', $_ * $Slic3r::resolution / $Slic3r::layer_height } $min_z, $max_z;
Slic3r::debugf "layers: min = %.0f, max = %.0f\n", $min_layer, $max_layer; Slic3r::debugf "layers: min = %.0f, max = %.0f\n", $min_layer, $max_layer;
# is the facet horizontal? # is the facet horizontal?
@ -159,7 +159,7 @@ sub _facet {
# check whether the two points coincide due to resolution rounding # check whether the two points coincide due to resolution rounding
if ($intersection_points[0]->coincides_with($intersection_points[1])) { if ($intersection_points[0]->coincides_with($intersection_points[1])) {
Slic3r::debugf "Points coincide; removing\n"; Slic3r::debugf "Points coincide at layer %d; removing\n", $layer_id;
next; next;
} }

View File

@ -8,7 +8,7 @@ use constant X => 0;
use constant Y => 1; use constant Y => 1;
sub factor { sub factor {
return $Slic3r::resolution * 100; return $Slic3r::resolution * 10;
} }
sub svg { sub svg {
@ -17,6 +17,44 @@ sub svg {
return SVG->new(width => $print->max_length * factor(), height => $print->max_length * factor()); return SVG->new(width => $print->max_length * factor(), height => $print->max_length * factor());
} }
sub output_points {
my ($print, $filename, $points, $red_points) = @_;
$red_points ||= [];
my $svg = svg($print);
my $g = $svg->group(
style => {
'stroke-width' => 2,
'stroke' => 'black',
'fill' => 'black',
},
);
foreach my $point (@$points) {
$g->circle(
cx => $point->[X] * factor(),
cy => $point->[Y] * factor(),
r => 2,
);
}
my $g2 = $svg->group(
style => {
'stroke-width' => 2,
'stroke' => 'red',
'fill' => 'red',
},
);
foreach my $point (@$red_points) {
$g2->circle(
cx => $point->[X] * factor(),
cy => $point->[Y] * factor(),
r => 3,
);
}
write_svg($svg, $filename);
}
sub output_polygons { sub output_polygons {
my ($print, $filename, $polygons) = @_; my ($print, $filename, $polygons) = @_;
@ -25,6 +63,7 @@ sub output_polygons {
style => { style => {
'stroke-width' => 2, 'stroke-width' => 2,
'stroke' => 'black', 'stroke' => 'black',
'fill' => 'none',
}, },
); );
foreach my $polygon (@$polygons) { foreach my $polygon (@$polygons) {