11_clipper.t 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use List::Util qw(sum);
  5. use Slic3r::XS;
  6. use Test::More tests => 16;
  7. my $square = Slic3r::Polygon->new( # ccw
  8. [200, 100],
  9. [200, 200],
  10. [100, 200],
  11. [100, 100],
  12. );
  13. my $hole_in_square = Slic3r::Polygon->new( # cw
  14. [160, 140],
  15. [140, 140],
  16. [140, 160],
  17. [160, 160],
  18. );
  19. my $expolygon = Slic3r::ExPolygon->new($square, $hole_in_square);
  20. {
  21. my $result = Slic3r::Geometry::Clipper::offset([ $square, $hole_in_square ], 5);
  22. is_deeply [ map $_->pp, @$result ], [ [
  23. [205, 205],
  24. [95, 205],
  25. [95, 95],
  26. [205, 95],
  27. ], [
  28. [145, 145],
  29. [145, 155],
  30. [155, 155],
  31. [155, 145],
  32. ] ], 'offset';
  33. }
  34. {
  35. my $result = Slic3r::Geometry::Clipper::offset_ex([ @$expolygon ], 5);
  36. is_deeply $result->[0]->pp, [ [
  37. [205, 205],
  38. [95, 205],
  39. [95, 95],
  40. [205, 95],
  41. ], [
  42. [145, 145],
  43. [145, 155],
  44. [155, 155],
  45. [155, 145],
  46. ] ], 'offset_ex';
  47. }
  48. {
  49. my $result = Slic3r::Geometry::Clipper::offset2_ex([ @$expolygon ], 5, -2);
  50. is_deeply $result->[0]->pp, [ [
  51. [203, 203],
  52. [97, 203],
  53. [97, 97],
  54. [203, 97],
  55. ], [
  56. [143, 143],
  57. [143, 157],
  58. [157, 157],
  59. [157, 143],
  60. ] ], 'offset2_ex';
  61. }
  62. {
  63. my $expolygon2 = Slic3r::ExPolygon->new([
  64. [20000000, 20000000],
  65. [0, 20000000],
  66. [0, 0],
  67. [20000000, 0],
  68. ], [
  69. [5000000, 15000000],
  70. [15000000, 15000000],
  71. [15000000, 5000000],
  72. [5000000, 5000000],
  73. ]);
  74. my $result = Slic3r::Geometry::Clipper::offset2_ex([ @$expolygon2 ], -1, +1);
  75. is $result->[0]->area, $expolygon2->area, 'offset2_ex';
  76. }
  77. {
  78. my $polygon1 = Slic3r::Polygon->new(@$square);
  79. my $polygon2 = Slic3r::Polygon->new(reverse @$hole_in_square);
  80. my $result = Slic3r::Geometry::Clipper::diff_ex([$polygon1], [$polygon2]);
  81. is $result->[0]->area, $expolygon->area, 'diff_ex';
  82. }
  83. {
  84. my $polyline = Slic3r::Polyline->new([50,150], [300,150]);
  85. {
  86. my $result = Slic3r::Geometry::Clipper::intersection_pl([$polyline], [$square, $hole_in_square]);
  87. is scalar(@$result), 2, 'intersection_pl - correct number of result lines';
  88. # results are in no particular order
  89. is scalar(grep $_->length == 40, @$result), 2, 'intersection_pl - result lines have correct length';
  90. }
  91. {
  92. my $result = Slic3r::Geometry::Clipper::diff_pl([$polyline], [$square, $hole_in_square]);
  93. is scalar(@$result), 3, 'diff_pl - correct number of result lines';
  94. # results are in no particular order
  95. is scalar(grep $_->length == 50, @$result), 1, 'diff_pl - the left result line has correct length';
  96. is scalar(grep $_->length == 100, @$result), 1, 'diff_pl - two right result line has correct length';
  97. is scalar(grep $_->length == 20, @$result), 1, 'diff_pl - the central result line has correct length';
  98. }
  99. }
  100. if (0) { # Clipper does not preserve polyline orientation
  101. my $polyline = Slic3r::Polyline->new([50,150], [300,150]);
  102. my $result = Slic3r::Geometry::Clipper::intersection_pl([$polyline], [$square]);
  103. is scalar(@$result), 1, 'intersection_pl - correct number of result lines';
  104. is_deeply $result->[0]->pp, [[100,150], [200,150]], 'clipped line orientation is preserved';
  105. }
  106. if (0) { # Clipper does not preserve polyline orientation
  107. my $polyline = Slic3r::Polyline->new([300,150], [50,150]);
  108. my $result = Slic3r::Geometry::Clipper::intersection_pl([$polyline], [$square]);
  109. is scalar(@$result), 1, 'intersection_pl - correct number of result lines';
  110. is_deeply $result->[0]->pp, [[200,150], [100,150]], 'clipped line orientation is preserved';
  111. }
  112. {
  113. # Clipper bug #96 (our issue #2028)
  114. my $subject = Slic3r::Polyline->new(
  115. [44735000,31936670],[55270000,31936670],[55270000,25270000],[74730000,25270000],[74730000,44730000],[68063296,44730000],[68063296,55270000],[74730000,55270000],[74730000,74730000],[55270000,74730000],[55270000,68063296],[44730000,68063296],[44730000,74730000],[25270000,74730000],[25270000,55270000],[31936670,55270000],[31936670,44730000],[25270000,44730000],[25270000,25270000],[44730000,25270000],[44730000,31936670]
  116. );
  117. my $clip = [
  118. Slic3r::Polygon->new([75200000,45200000],[54800000,45200000],[54800000,24800000],[75200000,24800000]),
  119. ];
  120. my $result = Slic3r::Geometry::Clipper::intersection_pl([$subject], $clip);
  121. is scalar(@$result), 1, 'intersection_pl - result is not empty';
  122. }
  123. {
  124. # Clipper bug #122
  125. my $subject = [
  126. Slic3r::Polyline->new([1975,1975],[25,1975],[25,25],[1975,25],[1975,1975]),
  127. ];
  128. my $clip = [
  129. Slic3r::Polygon->new([2025,2025],[-25,2025],[-25,-25],[2025,-25]),
  130. Slic3r::Polygon->new([525,525],[525,1475],[1475,1475],[1475,525]),
  131. ];
  132. my $result = Slic3r::Geometry::Clipper::intersection_pl($subject, $clip);
  133. is scalar(@$result), 1, 'intersection_pl - result is not empty';
  134. is scalar(@{$result->[0]}), 5, 'intersection_pl - result is not empty';
  135. }
  136. {
  137. # Clipper bug #126
  138. my $subject = Slic3r::Polyline->new(
  139. [200000,19799999],[200000,200000],[24304692,200000],[15102879,17506106],[13883200,19799999],[200000,19799999],
  140. );
  141. my $clip = [
  142. Slic3r::Polygon->new([15257205,18493894],[14350057,20200000],[-200000,20200000],[-200000,-200000],[25196917,-200000]),
  143. ];
  144. my $result = Slic3r::Geometry::Clipper::intersection_pl([$subject], $clip);
  145. is scalar(@$result), 1, 'intersection_pl - result is not empty';
  146. is $result->[0]->length, $subject->length, 'intersection_pl - result has same length as subject polyline';
  147. }
  148. if (0) {
  149. # Disabled until Clipper bug #127 is fixed
  150. my $subject = [
  151. Slic3r::Polyline->new([-90000000,-100000000],[-90000000,100000000]), # vertical
  152. Slic3r::Polyline->new([-100000000,-10000000],[100000000,-10000000]), # horizontal
  153. Slic3r::Polyline->new([-100000000,0],[100000000,0]), # horizontal
  154. Slic3r::Polyline->new([-100000000,10000000],[100000000,10000000]), # horizontal
  155. ];
  156. my $clip = Slic3r::Polygon->new( # a circular, convex, polygon
  157. [99452190,10452846],[97814760,20791169],[95105652,30901699],[91354546,40673664],[86602540,50000000],
  158. [80901699,58778525],[74314483,66913061],[66913061,74314483],[58778525,80901699],[50000000,86602540],
  159. [40673664,91354546],[30901699,95105652],[20791169,97814760],[10452846,99452190],[0,100000000],
  160. [-10452846,99452190],[-20791169,97814760],[-30901699,95105652],[-40673664,91354546],
  161. [-50000000,86602540],[-58778525,80901699],[-66913061,74314483],[-74314483,66913061],
  162. [-80901699,58778525],[-86602540,50000000],[-91354546,40673664],[-95105652,30901699],
  163. [-97814760,20791169],[-99452190,10452846],[-100000000,0],[-99452190,-10452846],
  164. [-97814760,-20791169],[-95105652,-30901699],[-91354546,-40673664],[-86602540,-50000000],
  165. [-80901699,-58778525],[-74314483,-66913061],[-66913061,-74314483],[-58778525,-80901699],
  166. [-50000000,-86602540],[-40673664,-91354546],[-30901699,-95105652],[-20791169,-97814760],
  167. [-10452846,-99452190],[0,-100000000],[10452846,-99452190],[20791169,-97814760],
  168. [30901699,-95105652],[40673664,-91354546],[50000000,-86602540],[58778525,-80901699],
  169. [66913061,-74314483],[74314483,-66913061],[80901699,-58778525],[86602540,-50000000],
  170. [91354546,-40673664],[95105652,-30901699],[97814760,-20791169],[99452190,-10452846],[100000000,0]
  171. );
  172. my $result = Slic3r::Geometry::Clipper::intersection_pl($subject, [$clip]);
  173. is scalar(@$result), scalar(@$subject), 'intersection_pl - expected number of polylines';
  174. is sum(map scalar(@$_), @$result), scalar(@$subject)*2,
  175. 'intersection_pl - expected number of points in polylines';
  176. }
  177. __END__