Polygon.pm 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. sub concave_points {
  34. my ($self, $angle) = @_;
  35. $angle //= PI;
  36. # input angle threshold is checked on the internal side of the polygon
  37. # but angle3points measures CCW angle, so we calculate the complementary angle
  38. my $ccw_angle = 2*PI-$angle;
  39. my @concave = ();
  40. my @points = @$self;
  41. my @points_pp = @{$self->pp};
  42. for my $i (-1 .. ($#points-1)) {
  43. # angle is measured in ccw orientation
  44. my $vertex_angle = Slic3r::Geometry::angle3points(@points_pp[$i, $i-1, $i+1]);
  45. if ($vertex_angle <= $ccw_angle) {
  46. push @concave, $points[$i];
  47. }
  48. }
  49. return [@concave];
  50. }
  51. sub convex_points {
  52. my ($self, $angle) = @_;
  53. $angle //= PI;
  54. # input angle threshold is checked on the internal side of the polygon
  55. # but angle3points measures CCW angle, so we calculate the complementary angle
  56. my $ccw_angle = 2*PI-$angle;
  57. my @convex = ();
  58. my @points = @$self;
  59. my @points_pp = @{$self->pp};
  60. for my $i (-1 .. ($#points-1)) {
  61. # angle is measured in ccw orientation
  62. my $vertex_angle = Slic3r::Geometry::angle3points(@points_pp[$i, $i-1, $i+1]);
  63. if ($vertex_angle >= $ccw_angle) {
  64. push @convex, $points[$i];
  65. }
  66. }
  67. return [@convex];
  68. }
  69. 1;