diff options
author | Alessandro Ranellucci <aar@cpan.org> | 2011-10-06 13:55:26 +0400 |
---|---|---|
committer | Alessandro Ranellucci <aar@cpan.org> | 2011-10-06 13:55:26 +0400 |
commit | 5812804d6b8a38b023b76249da11b3a5b3cfa068 (patch) | |
tree | b5c78a3ed2e34a734ecaaf1175d914321efdd62d /lib/Slic3r/SVG.pm | |
parent | 5a07137def14d0728bf48cce21eb86054a9603d2 (diff) |
Refactored the can_connect() logic (includes a refactoring of the SVG library)
Diffstat (limited to 'lib/Slic3r/SVG.pm')
-rw-r--r-- | lib/Slic3r/SVG.pm | 157 |
1 files changed, 74 insertions, 83 deletions
diff --git a/lib/Slic3r/SVG.pm b/lib/Slic3r/SVG.pm index a8e91da93..6ce81efaa 100644 --- a/lib/Slic3r/SVG.pm +++ b/lib/Slic3r/SVG.pm @@ -17,106 +17,97 @@ sub svg { return SVG->new(width => $print->max_length * factor(), height => $print->max_length * factor()); } -sub output_points { - my ($print, $filename, $points, $red_points) = @_; - $red_points ||= []; +sub output { + my ($print, $filename, %things) = @_; 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, - ); + + foreach my $type (qw(polygons polylines)) { + if ($things{$type}) { + my $method = $type eq 'polygons' ? 'polygon' : 'polyline'; + my $g = $svg->group( + style => { + 'stroke-width' => 2, + 'stroke' => 'black', + 'fill' => 'none', + }, + ); + foreach my $polygon (@{$things{$type}}) { + my $path = $svg->get_path( + 'x' => [ map($_->[X] * factor(), @$polygon) ], + 'y' => [ map($_->[Y] * factor(), @$polygon) ], + -type => 'polygon', + ); + $g->$method( + %$path, + ); + } + } } - 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, - ); + foreach my $type (qw(points red_points)) { + if ($things{$type}) { + my ($colour, $r) = $type eq 'points' ? ('black', 2) : ('red', 3); + my $g = $svg->group( + style => { + 'stroke-width' => 2, + 'stroke' => 'black', + 'fill' => $colour, + }, + ); + foreach my $point (@{$things{$type}}) { + $g->circle( + cx => $point->[X] * factor(), + cy => $point->[Y] * factor(), + r => $r, + ); + } + } + } + + foreach my $type (qw(lines red_lines)) { + if ($things{$type}) { + my ($colour) = $type eq 'lines' ? ('black') : ('red'); + my $g = $svg->group( + style => { + 'stroke-width' => 2, + }, + ); + foreach my $line (@{$things{$type}}) { + $g->line( + x1 => $line->[0][X] * factor(), + y1 => $line->[0][Y] * factor(), + x2 => $line->[1][X] * factor(), + y2 => $line->[1][Y] * factor(), + style => { + 'stroke' => $colour, + }, + ); + } + } } write_svg($svg, $filename); } +sub output_points { + my ($print, $filename, $points, $red_points) = @_; + return output($print, $filename, points => $points, red_points => $red_points); +} + sub output_polygons { - my ($print, $filename, $polygons, $type) = @_; - $type ||= 'polygon'; - - my $svg = svg($print); - my $g = $svg->group( - style => { - 'stroke-width' => 2, - 'stroke' => 'black', - 'fill' => 'none', - }, - ); - foreach my $polygon (@$polygons) { - my $path = $svg->get_path( - 'x' => [ map($_->[X] * factor(), @$polygon) ], - 'y' => [ map($_->[Y] * factor(), @$polygon) ], - -type => 'polygon', - ); - $g->$type( - %$path, - ); - } - - write_svg($svg, $filename); + my ($print, $filename, $polygons) = @_; + return output($print, $filename, polygons => $polygons); } sub output_polylines { - return output_polygons(@_, 'polyline'); + my ($print, $filename, $polylines) = @_; + return output($print, $filename, polylines => $polylines); } sub output_lines { my ($print, $filename, $lines) = @_; - - my $svg = svg($print); - my $g = $svg->group( - style => { - 'stroke-width' => 2, - }, - ); - - my $color = 'red'; - my $draw_line = sub { - my ($line) = @_; - $g->line( - x1 => $line->[0][X] * factor(), - y1 => $line->[0][Y] * factor(), - x2 => $line->[1][X] * factor(), - y2 => $line->[1][Y] * factor(), - style => { - 'stroke' => $color, - }, - ); - }; - - my $last = pop @$lines; - foreach my $line (@$lines) { - $draw_line->($line); - } - $color = 'black'; - $draw_line->($last); - - write_svg($svg, $filename); + return output($print, $filename, lines => $lines); } sub write_svg { |