Polygon.pm 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package Slic3r::Polygon;
  2. use strict;
  3. use warnings;
  4. # a polygon is a closed polyline.
  5. use parent 'Slic3r::Polyline';
  6. use Slic3r::Geometry qw(PI);
  7. sub dump_perl {
  8. my $self = shift;
  9. return sprintf "[%s]", join ',', map "[$_->[0],$_->[1]]", @$self;
  10. }
  11. sub grow {
  12. my $self = shift;
  13. return $self->split_at_first_point->grow(@_);
  14. }
  15. # this method subdivides the polygon segments to that no one of them
  16. # is longer than the length provided
  17. sub subdivide {
  18. my $self = shift;
  19. my ($max_length) = @_;
  20. my @points = @$self;
  21. push @points, $points[0]; # append first point as this is a polygon
  22. my @new_points = shift @points;
  23. while (@points) {
  24. while ($new_points[-1]->distance_to($points[0]) > $max_length) {
  25. push @new_points, map Slic3r::Point->new(@$_),
  26. Slic3r::Geometry::point_along_segment($new_points[-1], $points[0], $max_length);
  27. }
  28. push @new_points, shift @points;
  29. }
  30. pop @new_points; # remove last point as it coincides with first one
  31. return Slic3r::Polygon->new(@new_points);
  32. }
  33. 1;