Polyline.pm 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package Slic3r::Polyline;
  2. use strict;
  3. use warnings;
  4. use Slic3r::Geometry qw(A B X Y X1 X2 Y1 Y2 polyline_remove_parallel_continuous_edges polyline_remove_acute_vertices);
  5. use Slic3r::Geometry::Clipper qw(JT_SQUARE);
  6. use Storable qw();
  7. sub new_scale {
  8. my $class = shift;
  9. my @points = map { ref($_) eq 'Slic3r::Point' ? $_->pp : $_ } @_;
  10. return $class->new(map [ Slic3r::Geometry::scale($_->[X]), Slic3r::Geometry::scale($_->[Y]) ], @points);
  11. }
  12. sub wkt {
  13. my $self = shift;
  14. return sprintf "LINESTRING((%s))", join ',', map "$_->[0] $_->[1]", @$self;
  15. }
  16. sub merge_continuous_lines {
  17. my $self = shift;
  18. polyline_remove_parallel_continuous_edges($self);
  19. }
  20. sub remove_acute_vertices {
  21. my $self = shift;
  22. polyline_remove_acute_vertices($self);
  23. }
  24. sub simplify {
  25. my $self = shift;
  26. my $tolerance = shift || 10;
  27. my $simplified = Boost::Geometry::Utils::linestring_simplify($self->pp, $tolerance);
  28. return __PACKAGE__->new(@$simplified);
  29. }
  30. sub grow {
  31. my $self = shift;
  32. my ($distance, $scale, $joinType, $miterLimit) = @_;
  33. $joinType //= JT_SQUARE; # we override this one
  34. $scale //= 100000; # we init these because we can't pass undef
  35. $miterLimit //= 3;
  36. my @points = @$self;
  37. return @{Slic3r::Geometry::Clipper::offset(
  38. [ Slic3r::Polygon->new(@points, CORE::reverse @points[1..($#points-1)]) ],
  39. $distance, $scale, $joinType, $miterLimit,
  40. )};
  41. }
  42. sub clip_with_polygon {
  43. my $self = shift;
  44. my ($polygon) = @_;
  45. return $self->clip_with_expolygon(Slic3r::ExPolygon->new($polygon));
  46. }
  47. sub clip_with_expolygon {
  48. my $self = shift;
  49. my ($expolygon) = @_;
  50. my $result = Boost::Geometry::Utils::polygon_multi_linestring_intersection($expolygon->pp, [$self->pp]);
  51. return map { __PACKAGE__->new(@$_) } @$result;
  52. }
  53. sub bounding_box {
  54. my $self = shift;
  55. return Slic3r::Geometry::BoundingBox->new_from_points($self);
  56. }
  57. sub size {
  58. my $self = shift;
  59. return [ Slic3r::Geometry::size_2D($self) ];
  60. }
  61. sub align_to_origin {
  62. my $self = shift;
  63. my $bb = $self->bounding_box;
  64. return $self->translate(-$bb->x_min, -$bb->y_min);
  65. }
  66. 1;