PlanePath.pm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package Slic3r::Fill::PlanePath;
  2. use Moo;
  3. extends 'Slic3r::Fill::Base';
  4. with qw(Slic3r::Fill::WithDirection);
  5. use Slic3r::Geometry qw(scale X1 Y1 X2 Y2);
  6. use Slic3r::Geometry::Clipper qw(intersection_pl);
  7. sub angles () { [0] }
  8. sub multiplier () { 1 }
  9. sub process_polyline {}
  10. sub fill_surface {
  11. my $self = shift;
  12. my ($surface, %params) = @_;
  13. # rotate polygons
  14. my $expolygon = $surface->expolygon->clone;
  15. my $rotate_vector = $self->infill_direction($surface);
  16. $self->rotate_points($expolygon, $rotate_vector);
  17. my $distance_between_lines = scale($self->spacing) / $params{density} * $self->multiplier;
  18. # align infill across layers using the object's bounding box
  19. my $bb_polygon = $self->bounding_box->polygon;
  20. $self->rotate_points($bb_polygon, $rotate_vector);
  21. my $bounding_box = $bb_polygon->bounding_box;
  22. (ref $self) =~ /::([^:]+)$/;
  23. my $path = "Math::PlanePath::$1"->new;
  24. my $translate = Slic3r::Point->new(0,0); # vector
  25. if ($path->x_negative || $path->y_negative) {
  26. # if the curve extends on both positive and negative coordinate space,
  27. # center our expolygon around origin
  28. $translate = $bounding_box->center->negative;
  29. } else {
  30. # if the curve does not extend in negative coordinate space,
  31. # move expolygon entirely in positive coordinate space
  32. $translate = $bounding_box->min_point->negative;
  33. }
  34. $expolygon->translate(@$translate);
  35. $bounding_box->translate(@$translate);
  36. my ($n_lo, $n_hi) = $path->rect_to_n_range(
  37. map { $_ / $distance_between_lines }
  38. @{$bounding_box->min_point},
  39. @{$bounding_box->max_point},
  40. );
  41. my $polyline = Slic3r::Polyline->new(
  42. map [ map { $_ * $distance_between_lines } $path->n_to_xy($_) ], ($n_lo..$n_hi)
  43. );
  44. return {} if @$polyline <= 1;
  45. $self->process_polyline($polyline, $bounding_box);
  46. my @paths = @{intersection_pl([$polyline], \@$expolygon)};
  47. if (0) {
  48. require "Slic3r/SVG.pm";
  49. Slic3r::SVG::output("fill.svg",
  50. no_arrows => 1,
  51. polygons => \@$expolygon,
  52. green_polygons => [ $bounding_box->polygon ],
  53. polylines => [ $polyline ],
  54. red_polylines => \@paths,
  55. );
  56. }
  57. # paths must be repositioned and rotated back
  58. $_->translate(@{$translate->negative}) for @paths;
  59. $self->rotate_points_back(\@paths, $rotate_vector);
  60. return @paths;
  61. }
  62. package Slic3r::Fill::ArchimedeanChords;
  63. use Moo;
  64. extends 'Slic3r::Fill::PlanePath';
  65. use Math::PlanePath::ArchimedeanChords;
  66. package Slic3r::Fill::Flowsnake;
  67. use Moo;
  68. extends 'Slic3r::Fill::PlanePath';
  69. use Math::PlanePath::Flowsnake;
  70. use Slic3r::Geometry qw(X);
  71. # Sorry, this fill is currently broken.
  72. sub process_polyline {
  73. my $self = shift;
  74. my ($polyline, $bounding_box) = @_;
  75. $_->[X] += $bounding_box->center->[X] for @$polyline;
  76. }
  77. package Slic3r::Fill::HilbertCurve;
  78. use Moo;
  79. extends 'Slic3r::Fill::PlanePath';
  80. use Math::PlanePath::HilbertCurve;
  81. package Slic3r::Fill::OctagramSpiral;
  82. use Moo;
  83. extends 'Slic3r::Fill::PlanePath';
  84. use Math::PlanePath::OctagramSpiral;
  85. sub multiplier () { sqrt(2) }
  86. 1;