test_clipper_utils.cpp 14 KB

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