Rectilinear.pm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. package Slic3r::Fill::Rectilinear;
  2. use Moo;
  3. use constant PI => 4 * atan2(1, 1);
  4. use constant X1 => 0;
  5. use constant Y1 => 1;
  6. use constant X2 => 2;
  7. use constant Y2 => 3;
  8. use constant A => 0;
  9. use constant B => 1;
  10. use constant X => 0;
  11. use constant Y => 1;
  12. use Math::Geometry::Planar;
  13. use POSIX qw(ceil);
  14. use XXX;
  15. sub make_fill {
  16. my $self = shift;
  17. my ($print, $layer) = @_;
  18. printf "Filling layer %d:\n", $layer->id;
  19. my $max_print_dimension = $print->max_length * sqrt(2);
  20. my $n = 1;
  21. foreach my $surface_collection (@{ $layer->fill_surfaces }) {
  22. my @path_collection = ();
  23. SURFACE: foreach my $surface (@{ $surface_collection->surfaces }) {
  24. Slic3r::debugf " Processing surface %s:\n", $surface->id;
  25. my $polygon = $surface->mgp_polygon;
  26. # set infill angle
  27. my (@rotate, @shift);
  28. $rotate[0] = Slic3r::Geometry::deg2rad($Slic3r::fill_angle);
  29. $rotate[1] = [ $max_print_dimension / 2, $max_print_dimension / 2 ];
  30. @shift = @{$rotate[1]};
  31. # alternate fill direction
  32. if ($layer->id % 2) {
  33. $rotate[0] = Slic3r::Geometry::deg2rad($Slic3r::fill_angle) + PI/2;
  34. }
  35. # TODO: here we should implement an "infill in direction of bridges" option
  36. # rotate surface as needed
  37. @shift = @{ +(Slic3r::Geometry::rotate_points(@rotate, \@shift))[0] };
  38. $polygon = $polygon->rotate(@rotate)->move(@shift) if $rotate[0];
  39. # force 100% density for external surfaces
  40. my $density = $surface->surface_type eq 'internal' ? $Slic3r::fill_density : 1;
  41. next SURFACE unless $density > 0;
  42. my $distance_between_lines = $Slic3r::flow_width / $Slic3r::resolution / $density;
  43. my $number_of_lines = ceil($max_print_dimension / $distance_between_lines);
  44. #printf "distance = %f\n", $distance_between_lines;
  45. #printf "number_of_lines = %d\n", $number_of_lines;
  46. # this arrayref will hold intersection points of the fill grid with surface segments
  47. my $points = [ map [], 0..$number_of_lines-1 ];
  48. foreach my $line (map $self->_lines_from_mgp_points($_), @{ $polygon->polygons }) {
  49. # find out the coordinates
  50. my @coordinates = map @$_, @$line;
  51. # get the extents of the segment along the primary axis
  52. my @line_c = sort { $a <=> $b } @coordinates[X1, X2];
  53. Slic3r::debugf "Segment %d,%d - %d,%d (extents: %f, %f)\n", @coordinates, @line_c;
  54. for (my $c = int($line_c[0] / $distance_between_lines) * $distance_between_lines;
  55. $c <= $line_c[1]; $c += $distance_between_lines) {
  56. next if $c < $line_c[0] || $c > $line_c[1];
  57. my $i = sprintf('%.0f', $c / $distance_between_lines) - 1;
  58. #printf "CURRENT \$i = %d, \$c = %f\n", $i, $c;
  59. # if the segment is parallel to our ray, there will be two intersection points
  60. if ($line_c[0] == $line_c[1]) {
  61. Slic3r::debugf " Segment is parallel!\n";
  62. push @{ $points->[$i] }, $coordinates[Y1], $coordinates[Y2];
  63. Slic3r::debugf " intersections at %f (%d) = %f, %f\n", $c, $i, $points->[$i][-2], $points->[$i][-1];
  64. } else {
  65. Slic3r::debugf " Segment NOT parallel!\n";
  66. # one point of intersection
  67. push @{ $points->[$i] }, $coordinates[Y1] + ($coordinates[Y2] - $coordinates[Y1])
  68. * ($c - $coordinates[X1]) / ($coordinates[X2] - $coordinates[X1]);
  69. Slic3r::debugf " intersection at %f (%d) = %f\n", $c, $i, $points->[$i][-1];
  70. }
  71. }
  72. }
  73. # sort and remove duplicates
  74. for (my $i = 0; $i <= $#$points; $i++) {
  75. my %h = map { sprintf("%.9f", $_) => 1 } @{ $points->[$i] };
  76. $points->[$i] = [ sort { $a <=> $b } keys %h ];
  77. }
  78. # generate extrusion paths
  79. my (@paths, @path_points) = ();
  80. my $direction = 0;
  81. my $stop_path = sub {
  82. # defensive programming
  83. if (@path_points == 1) {
  84. #warn "There shouldn't be only one point in the current path";
  85. }
  86. # if we were constructing a path, stop it
  87. push @paths, [ @path_points ] if @path_points > 1;
  88. @path_points = ();
  89. };
  90. # loop until we have spare points
  91. CYCLE: while (scalar map(@$_, @$points) > 1) {
  92. # loop through rows
  93. ROW: for (my $i = 0; $i <= $#$points; $i++) {
  94. my $row = $points->[$i] or next ROW;
  95. Slic3r::debugf "\nProcessing row %d (direction: %d)...\n", $i, $direction;
  96. if (!@$row) {
  97. Slic3r::debugf " no points\n";
  98. $stop_path->();
  99. next ROW;
  100. }
  101. Slic3r::debugf " points = %s\n", join ', ', @$row if $Slic3r::debug;
  102. # coordinate of current row
  103. my $c = ($i + 1) * $distance_between_lines;
  104. # need to start a path?
  105. if (!@path_points) {
  106. Slic3r::debugf " path starts at %d\n", $row->[0];
  107. push @path_points, [ $c, shift @$row ];
  108. }
  109. my @search_points = @$row;
  110. @search_points = reverse @search_points if $direction == 1;
  111. my @connectable_points = $self->find_connectable_points($polygon, $path_points[-1], $c, [@search_points]);
  112. Slic3r::debugf " ==> found %d connectable points = %s\n", scalar(@connectable_points),
  113. join ', ', @connectable_points if $Slic3r::debug;
  114. if (!@connectable_points && @path_points && $path_points[-1][0] != $c) {
  115. # no connectable in this row
  116. $stop_path->();
  117. }
  118. if (@connectable_points == 1 && $path_points[0][0] != $c
  119. && (($connectable_points[0] == $row->[-1] && $direction == 0)
  120. || ($connectable_points[0] == $row->[0] && $direction == 1))) {
  121. $i--; # keep searching on current row in the opposite direction
  122. }
  123. foreach my $p (@connectable_points) {
  124. push @path_points, [ $c, $p ];
  125. @$row = grep $_ != $p, @$row; # remove point from row
  126. }
  127. # invert direction
  128. $direction = $direction ? 0 : 1;
  129. }
  130. $stop_path->() if @path_points;
  131. }
  132. # paths must be rotated back
  133. if ($rotate[0]) {
  134. @paths = map [ Slic3r::Geometry::rotate_points(-$rotate[0], $rotate[1], @$_) ],
  135. map [ Slic3r::Geometry::move_points([map -$_, @shift], @$_) ], @paths;
  136. }
  137. push @path_collection, @paths;
  138. }
  139. # save into layer
  140. FINISH: push @{ $layer->fills }, Slic3r::ExtrusionPath::Collection->new(
  141. paths => [ map Slic3r::ExtrusionPath->cast([ @$_ ]), @path_collection ],
  142. );
  143. }
  144. }
  145. # this function will select the first contiguous block of
  146. # points connectable to a given one
  147. sub find_connectable_points {
  148. my $self = shift;
  149. my ($polygon, $point, $c, $points) = @_;
  150. my @connectable_points = ();
  151. foreach my $p (@$points) {
  152. if (!$self->can_connect($polygon, $point, [ $c, $p ])) {
  153. @connectable_points ? last : next;
  154. }
  155. push @connectable_points, $p;
  156. $point = [ $c, $p ] if $point->[0] != $c;
  157. }
  158. return @connectable_points;
  159. }
  160. # this subroutine tries to determine whether two points in a surface
  161. # are connectable without crossing contour or holes
  162. sub can_connect {
  163. my $self = shift;
  164. my ($polygon, $p1, $p2) = @_;
  165. #printf " Checking connectability of point %d\n", $p2->[1];
  166. # there's room for optimization here
  167. # this is not needed since we assume that $p1 and $p2 belong to $polygon
  168. for ($p1, $p2) {
  169. #return 0 unless $polygon->isinside($_);
  170. # TODO: re-enable this one after testing point_in_polygon() which
  171. # doesn't detect well points on the contour of polygon
  172. #return 0 unless Slic3r::Geometry::point_in_polygon($_, $polygon->points);
  173. }
  174. # check whether the $p1-$p2 segment doesn't intersect any segment
  175. # of the contour or of holes
  176. my ($contour_p, @holes_p) = $polygon->get_polygons;
  177. foreach my $points ($contour_p, @holes_p) {
  178. foreach my $line ($self->_lines_from_mgp_points($points)) {
  179. # theoretically speaking, SegmentIntersection() would be the right tool for the
  180. # job; however floating point math often makes it not return any intersection
  181. # point between our hypothetical extrusion segment and any other one, even
  182. # if, of course, the final point of the extrusion segment is taken from
  183. # $point and thus it's a point that belongs for sure to a segment.
  184. # then, let's calculate intersection considering extrusion segment as a ray
  185. # instead of a segment, and then check whether the intersection point
  186. # belongs to the segment
  187. my $point = SegmentRayIntersection([@$line, $p1, $p2]);
  188. #printf " intersecting ray %f,%f - %f,%f and segment %f,%f - %f,%f\n",
  189. # @$p1, @$p2, map @$_, @$line;
  190. if ($point && Slic3r::Geometry::line_point_belongs_to_segment($point, [$p1, $p2])) {
  191. #printf " ...point intersects!\n";
  192. #YYY [ $point, $p1, $p2 ];
  193. # our $p1-$p2 line intersects $line
  194. # if the intersection point is an intermediate point of $p1-$p2
  195. # it means that $p1-$p2 crosses $line, thus we're sure that
  196. # $p1 and $p2 are not connectible (one is inside polygon and one
  197. # is outside), unless $p1-$p2 and $line coincide but we've got
  198. # an intersection due to floating point math
  199. my @points_not_belonging_to_line = grep !Slic3r::Geometry::points_coincide($point, $_), $p1, $p2;
  200. if (@points_not_belonging_to_line == 2) {
  201. # make sure $p1-$p2 and $line are two distinct lines; we do this
  202. # by checking their slopes
  203. if (!Slic3r::Geometry::lines_parallel([$p1, $p2], $line)) {
  204. #printf " ...lines cross!\n";
  205. #Slic3r::SVG::output_lines($main::print, "lines" . $n++ . ".svg", [ @lines, [$p1, $p2] ]);
  206. return 0;
  207. }
  208. }
  209. # defensive programming, this shouldn't happen
  210. if (@points_not_belonging_to_line == 0) {
  211. die "SegmentIntersection is not expected to return an intersection point "
  212. . "if \$line coincides with \$p1-\$p2";
  213. }
  214. # if we're here, then either $p1 or $p2 belong to $line
  215. # so we have to check whether the other point falls inside
  216. # the polygon or not
  217. # we rely on Math::Geometry::Planar returning contour points
  218. # in counter-clockwise order and hole points in clockwise
  219. # order, so that if the point falls on the left of $line
  220. # it's inside the polygon and viceversa
  221. my $C = $points_not_belonging_to_line[0];
  222. my $isInside = (($line->[B][X] - $line->[A][X])*($C->[Y] - $line->[A][Y])
  223. - ($line->[B][Y] - $line->[A][Y])*($C->[X] - $line->[A][X])) > 0;
  224. #printf " \$line is inside polygon: %d\n", $isInside;
  225. # if the line is outside the polygon then points are not connectable
  226. return 0 if !$isInside;
  227. #Slic3r::SVG::output_lines($main::print, "lines" . $n++ . ".svg", [ @lines, [$p1, $p2] ])
  228. # if !$isInside;
  229. }
  230. }
  231. }
  232. # even if no intersection is found, we should check whether both $p1 and $p2 are
  233. # inside a hole; this may happen due to floating point path
  234. #foreach my $hole_p (map $self->_mgp_from_points_ref($_), @holes_p) {
  235. # if ($hole_p->isinside($p1) || $hole_p->isinside($p2)) {
  236. # return 0;
  237. # }
  238. #}
  239. #use Slic3r::SVG;
  240. #Slic3r::SVG::output_lines($main::print, "lines" . $n++ . ".svg", [ @lines, [$p1, $p2] ]);
  241. return 1;
  242. }
  243. sub _lines_from_mgp_points {
  244. my $self = shift;
  245. my ($points) = @_;
  246. my @lines = ();
  247. my $last_point = $points->[-1];
  248. foreach my $point (@$points) {
  249. push @lines, [ $last_point, $point ];
  250. $last_point = $point;
  251. }
  252. return @lines;
  253. }
  254. sub _mgp_from_points_ref {
  255. my $self = shift;
  256. my ($points) = @_;
  257. my $p = Math::Geometry::Planar->new;
  258. $p->points($points);
  259. return $p;
  260. }
  261. 1;