clipper.t 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. use Test::More;
  2. use strict;
  3. use warnings;
  4. plan tests => 3;
  5. BEGIN {
  6. use FindBin;
  7. use lib "$FindBin::Bin/../lib";
  8. }
  9. use Slic3r;
  10. use Slic3r::Geometry::Clipper qw(intersection_ex union_ex diff_ex);
  11. {
  12. my $square = [ # ccw
  13. [10, 10],
  14. [20, 10],
  15. [20, 20],
  16. [10, 20],
  17. ];
  18. my $hole_in_square = [ # cw
  19. [14, 14],
  20. [14, 16],
  21. [16, 16],
  22. [16, 14],
  23. ];
  24. my $square2 = [ # ccw
  25. [5, 12],
  26. [25, 12],
  27. [25, 18],
  28. [5, 18],
  29. ];
  30. my $intersection = intersection_ex([ $square, $hole_in_square ], [ $square2 ]);
  31. is_deeply [ map $_->pp, @$intersection ], [[
  32. [
  33. [20, 18],
  34. [10, 18],
  35. [10, 12],
  36. [20, 12],
  37. ],
  38. [
  39. [14, 16],
  40. [16, 16],
  41. [16, 14],
  42. [14, 14],
  43. ],
  44. ]], 'hole is preserved after intersection';
  45. }
  46. #==========================================================
  47. {
  48. my $contour1 = [ [0,0], [40,0], [40,40], [0,40] ]; # ccw
  49. my $contour2 = [ [10,10], [30,10], [30,30], [10,30] ]; # ccw
  50. my $hole = [ [15,15], [15,25], [25,25], [25,15] ]; # cw
  51. my $union = union_ex([ $contour1, $contour2, $hole ]);
  52. is_deeply [ map $_->pp, @$union ], [[ [ [40,40], [0,40], [0,0], [40,0] ] ]],
  53. 'union of two ccw and one cw is a contour with no holes';
  54. my $diff = diff_ex([ $contour1, $contour2 ], [ $hole ]);
  55. is_deeply [ map $_->pp, @$diff ], [[ [ [40,40], [0,40], [0,0], [40,0] ], [ [15,25], [25,25], [25,15], [15,15] ] ]],
  56. 'difference of a cw from two ccw is a contour with one hole';
  57. }