clipper.t 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. use Test::More;
  2. use strict;
  3. use warnings;
  4. plan tests => 6;
  5. BEGIN {
  6. use FindBin;
  7. use lib "$FindBin::Bin/../lib";
  8. use local::lib "$FindBin::Bin/../local-lib";
  9. }
  10. use List::Util qw(sum);
  11. use Slic3r;
  12. use Slic3r::Geometry::Clipper qw(intersection_ex union_ex diff_ex diff_pl);
  13. {
  14. my $square = [ # ccw
  15. [10, 10],
  16. [20, 10],
  17. [20, 20],
  18. [10, 20],
  19. ];
  20. my $hole_in_square = [ # cw
  21. [14, 14],
  22. [14, 16],
  23. [16, 16],
  24. [16, 14],
  25. ];
  26. my $square2 = [ # ccw
  27. [5, 12],
  28. [25, 12],
  29. [25, 18],
  30. [5, 18],
  31. ];
  32. my $intersection = intersection_ex([ $square, $hole_in_square ], [ $square2 ]);
  33. is sum(map $_->area, @$intersection), Slic3r::ExPolygon->new(
  34. [
  35. [20, 18],
  36. [10, 18],
  37. [10, 12],
  38. [20, 12],
  39. ],
  40. [
  41. [14, 16],
  42. [16, 16],
  43. [16, 14],
  44. [14, 14],
  45. ],
  46. )->area, 'hole is preserved after intersection';
  47. }
  48. #==========================================================
  49. {
  50. my $contour1 = [ [0,0], [40,0], [40,40], [0,40] ]; # ccw
  51. my $contour2 = [ [10,10], [30,10], [30,30], [10,30] ]; # ccw
  52. my $hole = [ [15,15], [15,25], [25,25], [25,15] ]; # cw
  53. my $union = union_ex([ $contour1, $contour2, $hole ]);
  54. is_deeply [ map $_->pp, @$union ], [[ [ [40,40], [0,40], [0,0], [40,0] ] ]],
  55. 'union of two ccw and one cw is a contour with no holes';
  56. my $diff = diff_ex([ $contour1, $contour2 ], [ $hole ]);
  57. is sum(map $_->area, @$diff),
  58. Slic3r::ExPolygon->new([ [40,40], [0,40], [0,0], [40,0] ], [ [15,25], [25,25], [25,15], [15,15] ])->area,
  59. 'difference of a cw from two ccw is a contour with one hole';
  60. }
  61. #==========================================================
  62. {
  63. my $square = Slic3r::Polygon->new_scale( # ccw
  64. [10, 10],
  65. [20, 10],
  66. [20, 20],
  67. [10, 20],
  68. );
  69. my $square_pl = $square->split_at_first_point;
  70. my $res = diff_pl([$square_pl], []);
  71. is scalar(@$res), 1, 'no-op diff_pl returns the right number of polylines';
  72. isa_ok $res->[0], 'Slic3r::Polyline', 'no-op diff_pl result';
  73. is scalar(@{$res->[0]}), scalar(@$square_pl), 'no-op diff_pl returns the unmodified input polyline';
  74. }
  75. __END__