diff --git a/lib/Slic3r/TriangleMesh.pm b/lib/Slic3r/TriangleMesh.pm index a3823c36..fc052502 100644 --- a/lib/Slic3r/TriangleMesh.pm +++ b/lib/Slic3r/TriangleMesh.pm @@ -183,79 +183,38 @@ sub make_loops { my @lines = map unpack_line($_), @$lines; # remove tangent edges - { - for (my $i = 0; $i <= $#lines; $i++) { - next unless defined $lines[$i] && defined $lines[$i][I_FACET_EDGE]; - # if the line is a facet edge, find another facet edge - # having the same endpoints but in reverse order - for (my $j = $i+1; $j <= $#lines; $j++) { - next unless defined $lines[$j] && defined $lines[$j][I_FACET_EDGE]; - - # are these facets adjacent? (sharing a common edge on this layer) - if ($lines[$i][I_A_ID] == $lines[$j][I_B_ID] && $lines[$i][I_B_ID] == $lines[$j][I_A_ID]) { - - # if they are both oriented upwards or downwards (like a 'V') - # then we can remove both edges from this layer since it won't - # affect the sliced shape - if ($lines[$j][I_FACET_EDGE] == $lines[$i][I_FACET_EDGE]) { - $lines[$i] = undef; - $lines[$j] = undef; - last; - } - - # if one of them is oriented upwards and the other is oriented - # downwards, let's only keep one of them (it doesn't matter which - # one since all 'top' lines were reversed at slicing) - if ($lines[$i][I_FACET_EDGE] == FE_TOP && $lines[$j][I_FACET_EDGE] == FE_BOTTOM) { - $lines[$j] = undef; - last; - } + for my $i (0 .. $#lines) { + next unless defined $lines[$i] && defined $lines[$i][I_FACET_EDGE]; + # if the line is a facet edge, find another facet edge + # having the same endpoints but in reverse order + for my $j ($i+1 .. $#lines) { + next unless defined $lines[$j] && defined $lines[$j][I_FACET_EDGE]; + + # are these facets adjacent? (sharing a common edge on this layer) + if ($lines[$i][I_A_ID] == $lines[$j][I_B_ID] && $lines[$i][I_B_ID] == $lines[$j][I_A_ID]) { + + # if they are both oriented upwards or downwards (like a 'V') + # then we can remove both edges from this layer since it won't + # affect the sliced shape + if ($lines[$j][I_FACET_EDGE] == $lines[$i][I_FACET_EDGE]) { + $lines[$i] = undef; + $lines[$j] = undef; + last; } + # if one of them is oriented upwards and the other is oriented + # downwards, let's only keep one of them (it doesn't matter which + # one since all 'top' lines were reversed at slicing) + if ($lines[$i][I_FACET_EDGE] == FE_TOP && $lines[$j][I_FACET_EDGE] == FE_BOTTOM) { + $lines[$j] = undef; + last; + } } + } } - @lines = grep $_, @lines; - # count relationships - my %a_count = (); # how many lines have the same a_id - foreach my $line (@lines) { - if (defined $line->[I_A_ID]) { - $a_count{$line->[I_A_ID]}++; - } - } - - foreach my $point_id (grep $a_count{$_} > 1, keys %a_count) { - my @lines_starting_here = grep defined $_->[I_A_ID] && $_->[I_A_ID] == $point_id, @lines; - Slic3r::debugf "%d lines start at point %d\n", scalar(@lines_starting_here), $point_id; - - # if two lines start at this point, one being a 'top' facet edge and the other being a 'bottom' one, - # then remove the top one and those following it (removing the top or the bottom one is an arbitrary - # choice) - # The "// ''" on the next line avoids uninitialized value errors mentioned in issue #357 but these - # errors occur on fixed models so the root cause still needs to be found - if (@lines_starting_here == 2 && join('', sort map $_->[I_FACET_EDGE] // '', @lines_starting_here) eq FE_TOP.FE_BOTTOM) { #/ - my @to_remove = grep $_->[I_FACET_EDGE] == FE_TOP, @lines_starting_here; - while (!grep defined $_->[I_B_ID] && $_->[I_B_ID] == $to_remove[-1]->[I_B_ID] && $_ ne $to_remove[-1], @lines) { - push @to_remove, grep defined $_->[I_A_ID] && $_->[I_A_ID] == $to_remove[-1]->[I_B_ID], @lines; - } - my %to_remove = map {$_ => 1} @to_remove; - @lines = grep !$to_remove{$_}, @lines; - } else { - Slic3r::debugf " this shouldn't happen and should be further investigated\n"; - if (0) { - require "Slic3r/SVG.pm"; - Slic3r::SVG::output("same_point.svg", - lines => [ map $_->line, grep !defined $_->[I_FACET_EDGE], @lines ], - red_lines => [ map $_->line, grep defined $_->[I_FACET_EDGE], @lines ], - #points => [ $self->vertices->[$point_id] ], - no_arrows => 0, - ); - } - } - } - my (@polygons, @failed_loops) = (); CYCLE: while (@lines) { # take first spare line and start a new loop @@ -287,7 +246,7 @@ sub make_loops { } push @loop, splice @lines, $line_idx, 1; } - }; + } # TODO: we should try to combine failed loops for (grep @$_ >= 3, @failed_loops) {