ExPolygon.pm 7.8 KB

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