11_clipper.t 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 => 23;
  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. if (0) { # Clipper does not preserve polyline orientation
  113. my $result = Slic3r::Geometry::Clipper::intersection_ppl([$hole_in_square], [$square]);
  114. is_deeply $result->[0]->pp, $hole_in_square->split_at_first_point->pp,
  115. 'intersection_ppl - clipping cw polygon as polyline preserves winding order';
  116. }
  117. {
  118. my $square2 = $square->clone;
  119. $square2->translate(50,50);
  120. {
  121. my $result = Slic3r::Geometry::Clipper::intersection_ppl([$square2], [$square]);
  122. is scalar(@$result), 1, 'intersection_ppl - result contains a single line';
  123. is scalar(@{$result->[0]}), 3, 'intersection_ppl - result contains expected number of points';
  124. # Clipper does not preserve polyline orientation so we only check the middle point
  125. ###ok $result->[0][0]->coincides_with(Slic3r::Point->new(150,200)), 'intersection_ppl - expected point order';
  126. ok $result->[0][1]->coincides_with(Slic3r::Point->new(150,150)), 'intersection_ppl - expected point order';
  127. ###ok $result->[0][2]->coincides_with(Slic3r::Point->new(200,150)), 'intersection_ppl - expected point order';
  128. }
  129. }
  130. {
  131. my $square2 = $square->clone;
  132. $square2->reverse;
  133. $square2->translate(50,50);
  134. {
  135. my $result = Slic3r::Geometry::Clipper::intersection_ppl([$square2], [$square]);
  136. is scalar(@$result), 1, 'intersection_ppl - result contains a single line';
  137. is scalar(@{$result->[0]}), 3, 'intersection_ppl - result contains expected number of points';
  138. # Clipper does not preserve polyline orientation so we only check the middle point
  139. ###ok $result->[0][0]->coincides_with(Slic3r::Point->new(200,150)), 'intersection_ppl - expected point order';
  140. ok $result->[0][1]->coincides_with(Slic3r::Point->new(150,150)), 'intersection_ppl - expected point order';
  141. ###ok $result->[0][2]->coincides_with(Slic3r::Point->new(150,200)), 'intersection_ppl - expected point order';
  142. }
  143. }
  144. {
  145. # Clipper bug #96 (our issue #2028)
  146. my $subject = Slic3r::Polyline->new(
  147. [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]
  148. );
  149. my $clip = [
  150. Slic3r::Polygon->new([75200000,45200000],[54800000,45200000],[54800000,24800000],[75200000,24800000]),
  151. ];
  152. my $result = Slic3r::Geometry::Clipper::intersection_pl([$subject], $clip);
  153. is scalar(@$result), 1, 'intersection_pl - result is not empty';
  154. }
  155. {
  156. my $subject = Slic3r::Polygon->new(
  157. [44730000,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]
  158. );
  159. my $clip = [
  160. Slic3r::Polygon->new([75200000,45200000],[54800000,45200000],[54800000,24800000],[75200000,24800000]),
  161. ];
  162. my $result = Slic3r::Geometry::Clipper::intersection_ppl([$subject], $clip);
  163. is scalar(@$result), 1, 'intersection_ppl - result is not empty';
  164. }
  165. {
  166. # Clipper bug #122
  167. my $subject = [
  168. Slic3r::Polyline->new([1975,1975],[25,1975],[25,25],[1975,25],[1975,1975]),
  169. ];
  170. my $clip = [
  171. Slic3r::Polygon->new([2025,2025],[-25,2025],[-25,-25],[2025,-25]),
  172. Slic3r::Polygon->new([525,525],[525,1475],[1475,1475],[1475,525]),
  173. ];
  174. my $result = Slic3r::Geometry::Clipper::intersection_pl($subject, $clip);
  175. is scalar(@$result), 1, 'intersection_pl - result is not empty';
  176. is scalar(@{$result->[0]}), 5, 'intersection_pl - result is not empty';
  177. }
  178. {
  179. # Clipper bug #126
  180. my $subject = Slic3r::Polyline->new(
  181. [200000,19799999],[200000,200000],[24304692,200000],[15102879,17506106],[13883200,19799999],[200000,19799999],
  182. );
  183. my $clip = [
  184. Slic3r::Polygon->new([15257205,18493894],[14350057,20200000],[-200000,20200000],[-200000,-200000],[25196917,-200000]),
  185. ];
  186. my $result = Slic3r::Geometry::Clipper::intersection_pl([$subject], $clip);
  187. is scalar(@$result), 1, 'intersection_pl - result is not empty';
  188. is $result->[0]->length, $subject->length, 'intersection_pl - result has same length as subject polyline';
  189. }
  190. if (0) {
  191. # Disabled until Clipper bug #127 is fixed
  192. my $subject = [
  193. Slic3r::Polyline->new([-90000000,-100000000],[-90000000,100000000]), # vertical
  194. Slic3r::Polyline->new([-100000000,-10000000],[100000000,-10000000]), # horizontal
  195. Slic3r::Polyline->new([-100000000,0],[100000000,0]), # horizontal
  196. Slic3r::Polyline->new([-100000000,10000000],[100000000,10000000]), # horizontal
  197. ];
  198. my $clip = Slic3r::Polygon->new( # a circular, convex, polygon
  199. [99452190,10452846],[97814760,20791169],[95105652,30901699],[91354546,40673664],[86602540,50000000],
  200. [80901699,58778525],[74314483,66913061],[66913061,74314483],[58778525,80901699],[50000000,86602540],
  201. [40673664,91354546],[30901699,95105652],[20791169,97814760],[10452846,99452190],[0,100000000],
  202. [-10452846,99452190],[-20791169,97814760],[-30901699,95105652],[-40673664,91354546],
  203. [-50000000,86602540],[-58778525,80901699],[-66913061,74314483],[-74314483,66913061],
  204. [-80901699,58778525],[-86602540,50000000],[-91354546,40673664],[-95105652,30901699],
  205. [-97814760,20791169],[-99452190,10452846],[-100000000,0],[-99452190,-10452846],
  206. [-97814760,-20791169],[-95105652,-30901699],[-91354546,-40673664],[-86602540,-50000000],
  207. [-80901699,-58778525],[-74314483,-66913061],[-66913061,-74314483],[-58778525,-80901699],
  208. [-50000000,-86602540],[-40673664,-91354546],[-30901699,-95105652],[-20791169,-97814760],
  209. [-10452846,-99452190],[0,-100000000],[10452846,-99452190],[20791169,-97814760],
  210. [30901699,-95105652],[40673664,-91354546],[50000000,-86602540],[58778525,-80901699],
  211. [66913061,-74314483],[74314483,-66913061],[80901699,-58778525],[86602540,-50000000],
  212. [91354546,-40673664],[95105652,-30901699],[97814760,-20791169],[99452190,-10452846],[100000000,0]
  213. );
  214. my $result = Slic3r::Geometry::Clipper::intersection_pl($subject, [$clip]);
  215. is scalar(@$result), scalar(@$subject), 'intersection_pl - expected number of polylines';
  216. is sum(map scalar(@$_), @$result), scalar(@$subject)*2,
  217. 'intersection_pl - expected number of points in polylines';
  218. }
  219. __END__