test_clipper_utils.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #include <catch2/catch.hpp>
  2. #include <numeric>
  3. #include <iostream>
  4. #include <boost/filesystem.hpp>
  5. #include "libslic3r/ClipperUtils.hpp"
  6. #include "libslic3r/ExPolygon.hpp"
  7. #include "libslic3r/SVG.hpp"
  8. using namespace Slic3r;
  9. SCENARIO("test clipper limits", "[ClipperUtils]") {
  10. GIVEN("100mm square") {
  11. WHEN("offset") {
  12. Slic3r::Polygon square{ Point::new_scale(200, 100), Point::new_scale(200, 200), Point::new_scale(100, 200), Point::new_scale(100, 100) };
  13. THEN("offset 100") {
  14. REQUIRE(offset(square, scale_(100)).size() == 1);
  15. }
  16. THEN("offset 1000") {
  17. REQUIRE(offset(square, scale_(1000)).size() == 1);
  18. }
  19. THEN("offset 10000") {
  20. REQUIRE(offset(square, scale_(10000)).size() == 1);
  21. }
  22. THEN("offset 100000") {
  23. REQUIRE(offset(square, scale_(100000)).size() == 1);
  24. }
  25. THEN("offset 1000000") {
  26. REQUIRE(offset(square, scale_(1000000)).size() == 1);
  27. }
  28. }
  29. }
  30. }
  31. SCENARIO("Various Clipper operations - xs/t/11_clipper.t", "[ClipperUtils]") {
  32. // CCW oriented contour
  33. Slic3r::Polygon square{ { 200, 100 }, {200, 200}, {100, 200}, {100, 100} };
  34. // CW oriented contour
  35. Slic3r::Polygon hole_in_square{ { 160, 140 }, { 140, 140 }, { 140, 160 }, { 160, 160 } };
  36. Slic3r::ExPolygon square_with_hole(square, hole_in_square);
  37. GIVEN("square_with_hole") {
  38. WHEN("offset") {
  39. Polygons result = Slic3r::offset(square_with_hole, 5.f);
  40. THEN("offset matches") {
  41. REQUIRE(result == Polygons {
  42. { { 205, 205 }, { 95, 205 }, { 95, 95 }, { 205, 95 }, },
  43. { { 145, 145 }, { 145, 155 }, { 155, 155 }, { 155, 145 } } });
  44. }
  45. }
  46. WHEN("offset_ex") {
  47. ExPolygons result = Slic3r::offset_ex(square_with_hole, 5.f);
  48. THEN("offset matches") {
  49. REQUIRE(result == ExPolygons { {
  50. { { 205, 205 }, { 95, 205 }, { 95, 95 }, { 205, 95 }, },
  51. { { 145, 145 }, { 145, 155 }, { 155, 155 }, { 155, 145 } } } } );
  52. }
  53. }
  54. WHEN("offset2_ex") {
  55. ExPolygons result = Slic3r::offset2_ex(square_with_hole, 5.f, -2.f);
  56. THEN("offset matches") {
  57. REQUIRE(result == ExPolygons { {
  58. { { 203, 203 }, { 97, 203 }, { 97, 97 }, { 203, 97 } },
  59. { { 143, 143 }, { 143, 157 }, { 157, 157 }, { 157, 143 } } } } );
  60. }
  61. }
  62. }
  63. GIVEN("square_with_hole 2") {
  64. Slic3r::ExPolygon square_with_hole(
  65. { { 20000000, 20000000 }, { 0, 20000000 }, { 0, 0 }, { 20000000, 0 } },
  66. { { 5000000, 15000000 }, { 15000000, 15000000 }, { 15000000, 5000000 }, { 5000000, 5000000 } });
  67. WHEN("offset2_ex") {
  68. Slic3r::ExPolygons result = Slic3r::offset2_ex(ExPolygons { square_with_hole }, -1.f, 1.f);
  69. THEN("offset matches") {
  70. REQUIRE(result.size() == 1);
  71. REQUIRE(square_with_hole.area() == result.front().area());
  72. }
  73. }
  74. }
  75. GIVEN("square and hole") {
  76. WHEN("diff_ex") {
  77. ExPolygons result = Slic3r::diff_ex({ square }, { hole_in_square });
  78. THEN("hole is created") {
  79. REQUIRE(result.size() == 1);
  80. REQUIRE(square_with_hole.area() == result.front().area());
  81. }
  82. }
  83. }
  84. GIVEN("polyline") {
  85. Polyline polyline { { 50, 150 }, { 300, 150 } };
  86. WHEN("intersection_pl") {
  87. Polylines result = Slic3r::intersection_pl({ polyline }, { square, hole_in_square });
  88. THEN("correct number of result lines") {
  89. REQUIRE(result.size() == 2);
  90. }
  91. THEN("result lines have correct length") {
  92. // results are in no particular order
  93. REQUIRE(result[0].length() == 40);
  94. REQUIRE(result[1].length() == 40);
  95. }
  96. }
  97. WHEN("diff_pl") {
  98. Polylines result = Slic3r::diff_pl({ polyline }, { square, hole_in_square });
  99. THEN("correct number of result lines") {
  100. REQUIRE(result.size() == 3);
  101. }
  102. // results are in no particular order
  103. THEN("the left result line has correct length") {
  104. REQUIRE(std::count_if(result.begin(), result.end(), [](const Polyline &pl) { return pl.length() == 50; }) == 1);
  105. }
  106. THEN("the right result line has correct length") {
  107. REQUIRE(std::count_if(result.begin(), result.end(), [](const Polyline &pl) { return pl.length() == 100; }) == 1);
  108. }
  109. THEN("the central result line has correct length") {
  110. REQUIRE(std::count_if(result.begin(), result.end(), [](const Polyline &pl) { return pl.length() == 20; }) == 1);
  111. }
  112. }
  113. }
  114. GIVEN("Clipper bug #96 / Slic3r issue #2028") {
  115. Slic3r::Polyline subject{
  116. { 44735000, 31936670 }, { 55270000, 31936670 }, { 55270000, 25270000 }, { 74730000, 25270000 }, { 74730000, 44730000 }, { 68063296, 44730000 }, { 68063296, 55270000 }, { 74730000, 55270000 },
  117. { 74730000, 74730000 }, { 55270000, 74730000 }, { 55270000, 68063296 }, { 44730000, 68063296 }, { 44730000, 74730000 }, { 25270000, 74730000 }, { 25270000, 55270000 }, { 31936670, 55270000 },
  118. { 31936670, 44730000 }, { 25270000, 44730000 }, { 25270000, 25270000 }, { 44730000, 25270000 }, { 44730000, 31936670 } };
  119. Slic3r::Polygon clip { {75200000, 45200000}, {54800000, 45200000}, {54800000, 24800000}, {75200000, 24800000} };
  120. Slic3r::Polylines result = Slic3r::intersection_pl({ subject }, { clip });
  121. THEN("intersection_pl - result is not empty") {
  122. REQUIRE(result.size() == 1);
  123. }
  124. }
  125. GIVEN("Clipper bug #122") {
  126. Slic3r::Polyline subject { { 1975, 1975 }, { 25, 1975 }, { 25, 25 }, { 1975, 25 }, { 1975, 1975 } };
  127. Slic3r::Polygons clip { { { 2025, 2025 }, { -25, 2025 } , { -25, -25 }, { 2025, -25 } },
  128. { { 525, 525 }, { 525, 1475 }, { 1475, 1475 }, { 1475, 525 } } };
  129. Slic3r::Polylines result = Slic3r::intersection_pl({ subject }, clip);
  130. THEN("intersection_pl - result is not empty") {
  131. REQUIRE(result.size() == 1);
  132. REQUIRE(result.front().points.size() == 5);
  133. }
  134. }
  135. GIVEN("Clipper bug #126") {
  136. Slic3r::Polyline subject { { 200000, 19799999 }, { 200000, 200000 }, { 24304692, 200000 }, { 15102879, 17506106 }, { 13883200, 19799999 }, { 200000, 19799999 } };
  137. Slic3r::Polygon clip { { 15257205, 18493894 }, { 14350057, 20200000 }, { -200000, 20200000 }, { -200000, -200000 }, { 25196917, -200000 } };
  138. Slic3r::Polylines result = Slic3r::intersection_pl({ subject }, { clip });
  139. THEN("intersection_pl - result is not empty") {
  140. REQUIRE(result.size() == 1);
  141. }
  142. THEN("intersection_pl - result has same length as subject polyline") {
  143. REQUIRE(result.front().length() == Approx(subject.length()));
  144. }
  145. }
  146. #if 0
  147. {
  148. # Clipper does not preserve polyline orientation
  149. my $polyline = Slic3r::Polyline->new([50, 150], [300, 150]);
  150. my $result = Slic3r::Geometry::Clipper::intersection_pl([$polyline], [$square]);
  151. is scalar(@$result), 1, 'intersection_pl - correct number of result lines';
  152. is_deeply $result->[0]->pp, [[100, 150], [200, 150]], 'clipped line orientation is preserved';
  153. }
  154. {
  155. # Clipper does not preserve polyline orientation
  156. my $polyline = Slic3r::Polyline->new([300, 150], [50, 150]);
  157. my $result = Slic3r::Geometry::Clipper::intersection_pl([$polyline], [$square]);
  158. is scalar(@$result), 1, 'intersection_pl - correct number of result lines';
  159. is_deeply $result->[0]->pp, [[200, 150], [100, 150]], 'clipped line orientation is preserved';
  160. }
  161. {
  162. # Disabled until Clipper bug #127 is fixed
  163. my $subject = [
  164. Slic3r::Polyline->new([-90000000, -100000000], [-90000000, 100000000]), # vertical
  165. Slic3r::Polyline->new([-100000000, -10000000], [100000000, -10000000]), # horizontal
  166. Slic3r::Polyline->new([-100000000, 0], [100000000, 0]), # horizontal
  167. Slic3r::Polyline->new([-100000000, 10000000], [100000000, 10000000]), # horizontal
  168. ];
  169. my $clip = Slic3r::Polygon->new(# a circular, convex, polygon
  170. [99452190, 10452846], [97814760, 20791169], [95105652, 30901699], [91354546, 40673664], [86602540, 50000000],
  171. [80901699, 58778525], [74314483, 66913061], [66913061, 74314483], [58778525, 80901699], [50000000, 86602540],
  172. [40673664, 91354546], [30901699, 95105652], [20791169, 97814760], [10452846, 99452190], [0, 100000000],
  173. [-10452846, 99452190], [-20791169, 97814760], [-30901699, 95105652], [-40673664, 91354546],
  174. [-50000000, 86602540], [-58778525, 80901699], [-66913061, 74314483], [-74314483, 66913061],
  175. [-80901699, 58778525], [-86602540, 50000000], [-91354546, 40673664], [-95105652, 30901699],
  176. [-97814760, 20791169], [-99452190, 10452846], [-100000000, 0], [-99452190, -10452846],
  177. [-97814760, -20791169], [-95105652, -30901699], [-91354546, -40673664], [-86602540, -50000000],
  178. [-80901699, -58778525], [-74314483, -66913061], [-66913061, -74314483], [-58778525, -80901699],
  179. [-50000000, -86602540], [-40673664, -91354546], [-30901699, -95105652], [-20791169, -97814760],
  180. [-10452846, -99452190], [0, -100000000], [10452846, -99452190], [20791169, -97814760],
  181. [30901699, -95105652], [40673664, -91354546], [50000000, -86602540], [58778525, -80901699],
  182. [66913061, -74314483], [74314483, -66913061], [80901699, -58778525], [86602540, -50000000],
  183. [91354546, -40673664], [95105652, -30901699], [97814760, -20791169], [99452190, -10452846], [100000000, 0]
  184. );
  185. my $result = Slic3r::Geometry::Clipper::intersection_pl($subject, [$clip]);
  186. is scalar(@$result), scalar(@$subject), 'intersection_pl - expected number of polylines';
  187. is sum(map scalar(@$_), @$result), scalar(@$subject) * 2, 'intersection_pl - expected number of points in polylines';
  188. }
  189. #endif
  190. }
  191. SCENARIO("Various Clipper operations - t/clipper.t", "[ClipperUtils]") {
  192. GIVEN("square with hole") {
  193. // CCW oriented contour
  194. Slic3r::Polygon square { { 10, 10 }, { 20, 10 }, { 20, 20 }, { 10, 20 } };
  195. Slic3r::Polygon square2 { { 5, 12 }, { 25, 12 }, { 25, 18 }, { 5, 18 } };
  196. // CW oriented contour
  197. Slic3r::Polygon hole_in_square { { 14, 14 }, { 14, 16 }, { 16, 16 }, { 16, 14 } };
  198. WHEN("intersection_ex with another square") {
  199. ExPolygons intersection = Slic3r::intersection_ex({ square, hole_in_square }, { square2 });
  200. THEN("intersection area matches (hole is preserved)") {
  201. ExPolygon match({ { 20, 18 }, { 10, 18 }, { 10, 12 }, { 20, 12 } },
  202. { { 14, 16 }, { 16, 16 }, { 16, 14 }, { 14, 14 } });
  203. REQUIRE(intersection.size() == 1);
  204. REQUIRE(intersection.front().area() == Approx(match.area()));
  205. }
  206. }
  207. }
  208. GIVEN("square with hole 2") {
  209. // CCW oriented contour
  210. Slic3r::Polygon square { { 0, 0 }, { 40, 0 }, { 40, 40 }, { 0, 40 } };
  211. Slic3r::Polygon square2 { { 10, 10 }, { 30, 10 }, { 30, 30 }, { 10, 30 } };
  212. // CW oriented contour
  213. Slic3r::Polygon hole { { 15, 15 }, { 15, 25 }, { 25, 25 }, {25, 15 } };
  214. WHEN("union_ex with another square") {
  215. ExPolygons union_ = Slic3r::union_ex({ square, square2, hole });
  216. THEN("union of two ccw and one cw is a contour with no holes") {
  217. REQUIRE(union_.size() == 1);
  218. REQUIRE(union_.front() == ExPolygon { { 40, 40 }, { 0, 40 }, { 0, 0 }, { 40, 0 } } );
  219. }
  220. }
  221. WHEN("diff_ex with another square") {
  222. ExPolygons diff = Slic3r::diff_ex({ square, square2 }, { hole });
  223. THEN("difference of a cw from two ccw is a contour with one hole") {
  224. REQUIRE(diff.size() == 1);
  225. REQUIRE(diff.front().area() == Approx(ExPolygon({ {40, 40}, {0, 40}, {0, 0}, {40, 0} }, { {15, 25}, {25, 25}, {25, 15}, {15, 15} }).area()));
  226. }
  227. }
  228. }
  229. GIVEN("yet another square") {
  230. Slic3r::Polygon square { { 10, 10 }, { 20, 10 }, { 20, 20 }, { 10, 20 } };
  231. Slic3r::Polyline square_pl = square.split_at_first_point();
  232. WHEN("no-op diff_pl") {
  233. Slic3r::Polylines res = Slic3r::diff_pl({ square_pl }, {});
  234. THEN("returns the right number of polylines") {
  235. REQUIRE(res.size() == 1);
  236. }
  237. THEN("returns the unmodified input polyline") {
  238. REQUIRE(res.front().points.size() == square_pl.points.size());
  239. }
  240. }
  241. }
  242. }
  243. template<e_ordering o = e_ordering::OFF, class P, class Tree>
  244. double polytree_area(const Tree &tree, std::vector<P> *out)
  245. {
  246. traverse_pt<o>(tree, out);
  247. return std::accumulate(out->begin(), out->end(), 0.0,
  248. [](double a, const P &p) { return a + p.area(); });
  249. }
  250. size_t count_polys(const ExPolygons& expolys)
  251. {
  252. size_t c = 0;
  253. for (auto &ep : expolys) c += ep.holes.size() + 1;
  254. return c;
  255. }
  256. TEST_CASE("Traversing Clipper PolyTree", "[ClipperUtils]") {
  257. // Create a polygon representing unit box
  258. Polygon unitbox;
  259. const coord_t UNIT = coord_t(1. / SCALING_FACTOR);
  260. unitbox.points = Points{Point{0, 0}, Point{UNIT, coord_t(0)}, Point{UNIT, UNIT}, Point{coord_t(0), UNIT}};
  261. Polygon box_frame = unitbox;
  262. box_frame.scale(20, 10);
  263. Polygon hole_left = unitbox;
  264. hole_left.scale(8);
  265. hole_left.translate(UNIT, UNIT);
  266. hole_left.reverse();
  267. Polygon hole_right = hole_left;
  268. hole_right.translate(UNIT * 10, 0);
  269. Polygon inner_left = unitbox;
  270. inner_left.scale(4);
  271. inner_left.translate(UNIT * 3, UNIT * 3);
  272. Polygon inner_right = inner_left;
  273. inner_right.translate(UNIT * 10, 0);
  274. Polygons reference = union_({box_frame, hole_left, hole_right, inner_left, inner_right});
  275. ClipperLib::PolyTree tree = union_pt(reference);
  276. double area_sum = box_frame.area() + hole_left.area() +
  277. hole_right.area() + inner_left.area() +
  278. inner_right.area();
  279. REQUIRE(area_sum > 0);
  280. SECTION("Traverse into Polygons WITHOUT spatial ordering") {
  281. Polygons output;
  282. REQUIRE(area_sum == Approx(polytree_area(tree.GetFirst(), &output)));
  283. REQUIRE(output.size() == reference.size());
  284. }
  285. SECTION("Traverse into ExPolygons WITHOUT spatial ordering") {
  286. ExPolygons output;
  287. REQUIRE(area_sum == Approx(polytree_area(tree.GetFirst(), &output)));
  288. REQUIRE(count_polys(output) == reference.size());
  289. }
  290. SECTION("Traverse into Polygons WITH spatial ordering") {
  291. Polygons output;
  292. REQUIRE(area_sum == Approx(polytree_area<e_ordering::ON>(tree.GetFirst(), &output)));
  293. REQUIRE(output.size() == reference.size());
  294. }
  295. SECTION("Traverse into ExPolygons WITH spatial ordering") {
  296. ExPolygons output;
  297. REQUIRE(area_sum == Approx(polytree_area<e_ordering::ON>(tree.GetFirst(), &output)));
  298. REQUIRE(count_polys(output) == reference.size());
  299. }
  300. }