ExPolygon.pm 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 Math::Geometry::Voronoi;
  7. use Slic3r::Geometry qw(X Y A B point_in_polygon same_line);
  8. use Slic3r::Geometry::Clipper qw(union_ex JT_MITER);
  9. # the constructor accepts an array of polygons
  10. # or a Math::Clipper ExPolygon (hashref)
  11. sub new {
  12. my $class = shift;
  13. my $self;
  14. if (@_ == 1 && ref $_[0] eq 'HASH') {
  15. $self = [
  16. Slic3r::Polygon->new($_[0]{outer}),
  17. map Slic3r::Polygon->new($_), @{$_[0]{holes}},
  18. ];
  19. } else {
  20. $self = [ map Slic3r::Polygon->new($_), @_ ];
  21. }
  22. bless $self, $class;
  23. $self;
  24. }
  25. sub clone {
  26. my $self = shift;
  27. return (ref $self)->new(map $_->clone, @$self);
  28. }
  29. sub contour {
  30. my $self = shift;
  31. return $self->[0];
  32. }
  33. sub holes {
  34. my $self = shift;
  35. return @$self[1..$#$self];
  36. }
  37. sub lines {
  38. my $self = shift;
  39. return map $_->lines, @$self;
  40. }
  41. sub clipper_expolygon {
  42. my $self = shift;
  43. return {
  44. outer => $self->contour,
  45. holes => [ $self->holes ],
  46. };
  47. }
  48. sub boost_polygon {
  49. my $self = shift;
  50. return Boost::Geometry::Utils::polygon(@$self);
  51. }
  52. sub offset {
  53. my $self = shift;
  54. my ($distance, $scale, $joinType, $miterLimit) = @_;
  55. $scale ||= $Slic3r::scaling_factor * 1000000;
  56. $joinType = JT_MITER if !defined $joinType;
  57. $miterLimit ||= 2;
  58. my $offsets = Math::Clipper::offset($self, $distance, $scale, $joinType, $miterLimit);
  59. return @$offsets;
  60. }
  61. sub safety_offset {
  62. my $self = shift;
  63. # we're offsetting contour and holes separately
  64. # because Clipper doesn't return polygons in the same order as
  65. # we feed them to it
  66. return (ref $self)->new(
  67. $self->contour->safety_offset,
  68. @{ Slic3r::Geometry::Clipper::safety_offset([$self->holes]) },
  69. );
  70. }
  71. sub offset_ex {
  72. my $self = shift;
  73. my @offsets = $self->offset(@_);
  74. # apply holes to the right contours
  75. return @{ union_ex(\@offsets) };
  76. }
  77. sub encloses_point {
  78. my $self = shift;
  79. my ($point) = @_;
  80. return $self->contour->encloses_point($point)
  81. && (!grep($_->encloses_point($point), $self->holes)
  82. || grep($_->point_on_segment($point), $self->holes));
  83. }
  84. # A version of encloses_point for use when hole borders do not matter.
  85. # Useful because point_on_segment is slow
  86. sub encloses_point_quick {
  87. my $self = shift;
  88. my ($point) = @_;
  89. return $self->contour->encloses_point($point)
  90. && !grep($_->encloses_point($point), $self->holes);
  91. }
  92. sub encloses_line {
  93. my $self = shift;
  94. my ($line) = @_;
  95. my $clip = $self->clip_line($line);
  96. return @$clip == 1 && same_line($clip->[0], $line);
  97. }
  98. sub point_on_segment {
  99. my $self = shift;
  100. my ($point) = @_;
  101. for (@$self) {
  102. my $line = $_->point_on_segment($point);
  103. return $line if $line;
  104. }
  105. return undef;
  106. }
  107. sub bounding_box {
  108. my $self = shift;
  109. return Slic3r::Geometry::bounding_box($self->contour);
  110. }
  111. sub bounding_box_polygon {
  112. my $self = shift;
  113. my @bb = $self->bounding_box;
  114. return Slic3r::Polygon->new([
  115. [ $bb[0], $bb[1] ],
  116. [ $bb[2], $bb[1] ],
  117. [ $bb[2], $bb[3] ],
  118. [ $bb[0], $bb[3] ],
  119. ]);
  120. }
  121. sub clip_line {
  122. my $self = shift;
  123. my ($line) = @_; # line must be a Slic3r::Line object
  124. return Boost::Geometry::Utils::polygon_linestring_intersection(
  125. $self->boost_polygon,
  126. $line->boost_linestring,
  127. );
  128. }
  129. sub simplify {
  130. my $self = shift;
  131. $_->simplify(@_) for @$self;
  132. }
  133. sub translate {
  134. my $self = shift;
  135. $_->translate(@_) for @$self;
  136. }
  137. sub rotate {
  138. my $self = shift;
  139. $_->rotate(@_) for @$self;
  140. }
  141. sub area {
  142. my $self = shift;
  143. my $area = $self->contour->area;
  144. $area -= $_->area for $self->holes;
  145. return $area;
  146. }
  147. # this method only works for expolygons having only a contour or
  148. # a contour and a hole, and not being thicker than the supplied
  149. # width. it returns a polyline or a polygon
  150. sub medial_axis {
  151. my $self = shift;
  152. my ($width) = @_;
  153. my @self_lines = map $_->lines, @$self;
  154. my $expolygon = $self->clone;
  155. my @points = ();
  156. foreach my $polygon (@$expolygon) {
  157. Slic3r::Geometry::polyline_remove_short_segments($polygon, $width / 2);
  158. # subdivide polygon segments so that we don't have anyone of them
  159. # being longer than $width / 2
  160. $polygon->subdivide($width/2);
  161. push @points, @$polygon;
  162. }
  163. my $voronoi = Math::Geometry::Voronoi->new(points => \@points);
  164. $voronoi->compute;
  165. my @skeleton_lines = ();
  166. my $vertices = $voronoi->vertices;
  167. my $edges = $voronoi->edges;
  168. foreach my $edge (@$edges) {
  169. # ignore lines going to infinite
  170. next if $edge->[1] == -1 || $edge->[2] == -1;
  171. my ($a, $b);
  172. $a = $vertices->[$edge->[1]];
  173. $b = $vertices->[$edge->[2]];
  174. next if !$self->encloses_point_quick($a) || !$self->encloses_point_quick($b);
  175. push @skeleton_lines, [$edge->[1], $edge->[2]];
  176. }
  177. # remove leafs (lines not connected to other lines at one of their endpoints)
  178. {
  179. my %pointmap = ();
  180. $pointmap{$_}++ for map @$_, @skeleton_lines;
  181. @skeleton_lines = grep {
  182. $pointmap{$_->[A]} >= 2 && $pointmap{$_->[B]} >= 2
  183. } @skeleton_lines;
  184. }
  185. return () if !@skeleton_lines;
  186. # now walk along the medial axis and build continuos polylines or polygons
  187. my @polylines = ();
  188. {
  189. # build a map of line endpoints
  190. my %pointmap = (); # point_idx => [line_idx, line_idx ...]
  191. for my $line_idx (0 .. $#skeleton_lines) {
  192. for my $point_idx (@{$skeleton_lines[$line_idx]}) {
  193. $pointmap{$point_idx} ||= [];
  194. push @{$pointmap{$point_idx}}, $line_idx;
  195. }
  196. }
  197. # build the list of available lines
  198. my %spare_lines = map {$_ => 1} (0 .. $#skeleton_lines);
  199. CYCLE: while (%spare_lines) {
  200. push @polylines, [];
  201. my $polyline = $polylines[-1];
  202. # start from a random line
  203. my $first_line_idx = +(keys %spare_lines)[0];
  204. delete $spare_lines{$first_line_idx};
  205. push @$polyline, @{ $skeleton_lines[$first_line_idx] };
  206. while (1) {
  207. my $last_point_id = $polyline->[-1];
  208. my $lines_starting_here = $pointmap{$last_point_id};
  209. # remove all the visited lines from the array
  210. shift @$lines_starting_here
  211. while @$lines_starting_here && !$spare_lines{$lines_starting_here->[0]};
  212. # do we have a line starting here?
  213. my $next_line_idx = shift @$lines_starting_here;
  214. if (!defined $next_line_idx) {
  215. delete $pointmap{$last_point_id};
  216. next CYCLE;
  217. }
  218. # line is not available anymore
  219. delete $spare_lines{$next_line_idx};
  220. # add the other point to our polyline and continue walking
  221. push @$polyline, grep $_ ne $last_point_id, @{$skeleton_lines[$next_line_idx]};
  222. }
  223. }
  224. }
  225. my @result = ();
  226. foreach my $polyline (@polylines) {
  227. next unless @$polyline >= 2;
  228. # now replace point indexes with coordinates
  229. @$polyline = map $vertices->[$_], @$polyline;
  230. # cleanup
  231. $polyline = Slic3r::Geometry::douglas_peucker($polyline, $width / 7);
  232. if (Slic3r::Geometry::same_point($polyline->[0], $polyline->[-1])) {
  233. next if @$polyline == 2;
  234. push @result, Slic3r::Polygon->new(@$polyline[0..$#$polyline-1]);
  235. } else {
  236. push @result, Slic3r::Polyline->new($polyline);
  237. }
  238. }
  239. return @result;
  240. }
  241. 1;