Polygon.pm 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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(polygon_remove_parallel_continuous_edges
  7. polygon_remove_acute_vertices polygon_segment_having_point
  8. PI X1 X2 Y1 Y2 epsilon);
  9. sub wkt {
  10. my $self = shift;
  11. return sprintf "POLYGON((%s))", join ',', map "$_->[0] $_->[1]", @$self;
  12. }
  13. sub merge_continuous_lines {
  14. my $self = shift;
  15. my $p = $self->pp;
  16. polygon_remove_parallel_continuous_edges($p);
  17. return __PACKAGE__->new(@$p);
  18. }
  19. sub remove_acute_vertices {
  20. my $self = shift;
  21. polygon_remove_acute_vertices($self);
  22. }
  23. sub encloses_point {
  24. my $self = shift;
  25. my ($point) = @_;
  26. return Boost::Geometry::Utils::point_covered_by_polygon($point->pp, [$self->pp]);
  27. }
  28. sub grow {
  29. my $self = shift;
  30. return $self->split_at_first_point->grow(@_);
  31. }
  32. # NOTE that this will turn the polygon to ccw regardless of its
  33. # original orientation
  34. sub simplify {
  35. my $self = shift;
  36. return @{Slic3r::Geometry::Clipper::simplify_polygons([ $self->SUPER::simplify(@_) ])};
  37. }
  38. # this method subdivides the polygon segments to that no one of them
  39. # is longer than the length provided
  40. sub subdivide {
  41. my $self = shift;
  42. my ($max_length) = @_;
  43. my @points = @$self;
  44. push @points, $points[0]; # append first point as this is a polygon
  45. my @new_points = shift @points;
  46. while (@points) {
  47. while ($new_points[-1]->distance_to($points[0]) > $max_length) {
  48. push @new_points, map Slic3r::Point->new(@$_),
  49. Slic3r::Geometry::point_along_segment($new_points[-1], $points[0], $max_length);
  50. }
  51. push @new_points, shift @points;
  52. }
  53. pop @new_points; # remove last point as it coincides with first one
  54. return Slic3r::Polygon->new(@new_points);
  55. }
  56. # for cw polygons this will return convex points!
  57. sub concave_points {
  58. my $self = shift;
  59. my @points = @$self;
  60. my @points_pp = @{$self->pp};
  61. return map $points[$_],
  62. grep Slic3r::Geometry::angle3points(@points_pp[$_, $_-1, $_+1]) < PI - epsilon,
  63. -1 .. ($#points-1);
  64. }
  65. 1;