ExPolygon.pm 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package Slic3r::ExPolygon;
  2. use strict;
  3. use warnings;
  4. # an ExPolygon is a polygon with holes
  5. use Boost::Geometry::Utils;
  6. use List::Util qw(first);
  7. use Math::Geometry::Voronoi;
  8. use Slic3r::Geometry qw(X Y A B point_in_polygon epsilon scaled_epsilon);
  9. use Slic3r::Geometry::Clipper qw(union_ex diff_pl);
  10. sub wkt {
  11. my $self = shift;
  12. return sprintf "POLYGON(%s)",
  13. join ',', map "($_)", map { join ',', map "$_->[0] $_->[1]", @$_ } @$self;
  14. }
  15. sub dump_perl {
  16. my $self = shift;
  17. return sprintf "[%s]",
  18. join ',', map "[$_]", map { join ',', map "[$_->[0],$_->[1]]", @$_ } @$self;
  19. }
  20. sub offset {
  21. my $self = shift;
  22. return Slic3r::Geometry::Clipper::offset(\@$self, @_);
  23. }
  24. sub offset_ex {
  25. my $self = shift;
  26. return Slic3r::Geometry::Clipper::offset_ex(\@$self, @_);
  27. }
  28. sub noncollapsing_offset_ex {
  29. my $self = shift;
  30. my ($distance, @params) = @_;
  31. return $self->offset_ex($distance + 1, @params);
  32. }
  33. sub bounding_box {
  34. my $self = shift;
  35. return $self->contour->bounding_box;
  36. }
  37. sub simplify_as_polygons {
  38. my $self = shift;
  39. my ($tolerance) = @_;
  40. # it would be nice to have a multilinestring_simplify method in B::G::U
  41. return @{Slic3r::Geometry::Clipper::simplify_polygons(
  42. [ map Boost::Geometry::Utils::linestring_simplify($_, $tolerance), @{$self->pp} ],
  43. )};
  44. }
  45. sub simplify {
  46. my $self = shift;
  47. my ($tolerance) = @_;
  48. return @{ Slic3r::Geometry::Clipper::union_ex([ $self->simplify_as_polygons($tolerance) ]) };
  49. }
  50. # this method only works for expolygons having only a contour or
  51. # a contour and a hole, and not being thicker than the supplied
  52. # width. it returns a polyline or a polygon
  53. sub medial_axis {
  54. my ($self, $width) = @_;
  55. return $self->_medial_axis_voronoi($width);
  56. }
  57. sub _medial_axis_clip {
  58. my ($self, $width) = @_;
  59. my $grow = sub {
  60. my ($line, $distance) = @_;
  61. my $line_clone = $line->clone;
  62. $line_clone->clip_start(scaled_epsilon);
  63. return () if !$line_clone->is_valid;
  64. $line_clone->clip_end(scaled_epsilon);
  65. return () if !$line_clone->is_valid;
  66. my ($a, $b) = @$line_clone;
  67. my $dx = $a->x - $b->x;
  68. my $dy = $a->y - $b->y; #-
  69. my $dist = sqrt($dx*$dx + $dy*$dy);
  70. $dx /= $dist;
  71. $dy /= $dist;
  72. return Slic3r::Polygon->new(
  73. Slic3r::Point->new($a->x + $distance*$dy, $a->y - $distance*$dx), #--
  74. Slic3r::Point->new($b->x + $distance*$dy, $b->y - $distance*$dx), #--
  75. Slic3r::Point->new($b->x - $distance*$dy, $b->y + $distance*$dx), #++
  76. Slic3r::Point->new($a->x - $distance*$dy, $a->y + $distance*$dx), #++
  77. );
  78. };
  79. my @result = ();
  80. my $covered = [];
  81. foreach my $polygon (@$self) {
  82. my @polylines = ();
  83. foreach my $line (@{$polygon->lines}) {
  84. # remove the areas that are already covered from this line
  85. my $clipped = diff_pl([$line->as_polyline], $covered);
  86. # skip very short segments/dots
  87. @$clipped = grep $_->length > $width/10, @$clipped;
  88. # grow the remaining lines and add them to the covered areas
  89. push @$covered, map $grow->($_, $width*1.1), @$clipped;
  90. # if the first remaining segment is connected to the last polyline, append it
  91. # to that -- FIXME: this assumes that diff_pl()
  92. # preserved the orientation of the input linestring but this is not generally true
  93. if (@polylines && @$clipped && $clipped->[0]->first_point->distance_to($polylines[-1]->last_point) <= $width/10) {
  94. $polylines[-1]->append_polyline(shift @$clipped);
  95. }
  96. push @polylines, @$clipped;
  97. }
  98. foreach my $polyline (@polylines) {
  99. # if this polyline looks like a closed loop, return it as a polygon
  100. if ($polyline->first_point->coincides_with($polyline->last_point)) {
  101. next if @$polyline == 2;
  102. $polyline->pop_back;
  103. push @result, Slic3r::Polygon->new(@$polyline);
  104. } else {
  105. push @result, $polyline;
  106. }
  107. }
  108. }
  109. return @result;
  110. }
  111. my $voronoi_lock :shared;
  112. sub _medial_axis_voronoi {
  113. my ($self, $width) = @_;
  114. lock($voronoi_lock);
  115. my $voronoi;
  116. {
  117. my @points = ();
  118. foreach my $polygon (@$self) {
  119. {
  120. my $p = $polygon->pp;
  121. Slic3r::Geometry::polyline_remove_short_segments($p, $width / 2);
  122. $polygon = Slic3r::Polygon->new(@$p);
  123. }
  124. # subdivide polygon segments so that we don't have anyone of them
  125. # being longer than $width / 2
  126. $polygon = $polygon->subdivide($width/2);
  127. push @points, @{$polygon->pp};
  128. }
  129. $voronoi = Math::Geometry::Voronoi->new(points => \@points);
  130. }
  131. $voronoi->compute;
  132. my $vertices = $voronoi->vertices;
  133. my @skeleton_lines = ();
  134. foreach my $edge (@{ $voronoi->edges }) {
  135. # ignore lines going to infinite
  136. next if $edge->[1] == -1 || $edge->[2] == -1;
  137. my $line = Slic3r::Line->new($vertices->[$edge->[1]], $vertices->[$edge->[2]]);
  138. next if !$self->contains_line($line);
  139. # contains_point() could be faster, but we need an implementation that
  140. # reliably considers points on boundary
  141. #next if !$self->contains_point(Slic3r::Point->new(@{$vertices->[$edge->[1]]}))
  142. # || !$self->contains_point(Slic3r::Point->new(@{$vertices->[$edge->[2]]}));
  143. push @skeleton_lines, [$edge->[1], $edge->[2]];
  144. }
  145. return () if !@skeleton_lines;
  146. # now walk along the medial axis and build continuos polylines or polygons
  147. my @polylines = ();
  148. {
  149. my @lines = @skeleton_lines;
  150. push @polylines, [ map @$_, shift @lines ];
  151. CYCLE: while (@lines) {
  152. for my $i (0..$#lines) {
  153. if ($lines[$i][0] == $polylines[-1][-1]) {
  154. push @{$polylines[-1]}, $lines[$i][1];
  155. } elsif ($lines[$i][1] == $polylines[-1][-1]) {
  156. push @{$polylines[-1]}, $lines[$i][0];
  157. } elsif ($lines[$i][1] == $polylines[-1][0]) {
  158. unshift @{$polylines[-1]}, $lines[$i][0];
  159. } elsif ($lines[$i][0] == $polylines[-1][0]) {
  160. unshift @{$polylines[-1]}, $lines[$i][1];
  161. } else {
  162. next;
  163. }
  164. splice @lines, $i, 1;
  165. next CYCLE;
  166. }
  167. push @polylines, [ map @$_, shift @lines ];
  168. }
  169. }
  170. my @result = ();
  171. foreach my $polyline (@polylines) {
  172. next unless @$polyline >= 2;
  173. # now replace point indexes with coordinates
  174. my @points = map Slic3r::Point->new(@{$vertices->[$_]}), @$polyline;
  175. if ($points[0]->coincides_with($points[-1])) {
  176. next if @points == 2;
  177. push @result, Slic3r::Polygon->new(@points[0..$#points-1]);
  178. } else {
  179. push @result, Slic3r::Polyline->new(@points);
  180. }
  181. $result[-1]->simplify($width / 7);
  182. }
  183. return @result;
  184. }
  185. package Slic3r::ExPolygon::Collection;
  186. use Slic3r::Geometry qw(X1 Y1);
  187. sub align_to_origin {
  188. my $self = shift;
  189. my @bb = Slic3r::Geometry::bounding_box([ map @$_, map @$_, @$self ]);
  190. $self->translate(-$bb[X1], -$bb[Y1]);
  191. $self;
  192. }
  193. sub size {
  194. my $self = shift;
  195. return [ Slic3r::Geometry::size_2D([ map @$_, map @$_, @$self ]) ];
  196. }
  197. 1;