test_clipper_utils.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. #define CATCH_CONFIG_DISABLE
  2. #include <catch2/catch.hpp>
  3. #include <numeric>
  4. #include <iostream>
  5. #include <boost/filesystem.hpp>
  6. #include "libslic3r/ClipperUtils.hpp"
  7. #include "libslic3r/ExPolygon.hpp"
  8. #include "libslic3r/SVG.hpp"
  9. using namespace Slic3r;
  10. // 32 bit = 10^9
  11. // 64 bits: 10^18
  12. // scale : 1 000 000 (10^6) ~ 2^20
  13. // clipper scale : 2^17
  14. // => clipper useful range: 2^26 ~ 10 000 000m => 10 000 km
  15. SCENARIO("test clipper limits", "[ClipperUtils]") {
  16. GIVEN("100mm square") {
  17. WHEN("offset") {
  18. Slic3r::Polygon square{ Point::new_scale(200, 100), Point::new_scale(200, 200), Point::new_scale(100, 200), Point::new_scale(100, 100) };
  19. THEN("offset 100") {
  20. REQUIRE(offset(square, scale_(100)).size() == 1);
  21. }
  22. THEN("offset 1000") {
  23. REQUIRE(offset(square, scale_(1000)).size() == 1);
  24. }
  25. THEN("offset 10000") {
  26. REQUIRE(offset(square, scale_(10000)).size() == 1);
  27. }
  28. // every segment shorter than 0.5% of the offset will be cut.
  29. // that means 500 for an offset of 100000
  30. // so from now, offsetting it will destroy evrything
  31. // (since 2017)
  32. THEN("offset 100000") {
  33. REQUIRE(offset(square, scale_(100000)).size() == 0);
  34. }
  35. THEN("offset 1000000") {
  36. REQUIRE(offset(square, scale_(1000000)).size() == 0);
  37. }
  38. }
  39. }
  40. }
  41. SCENARIO("Various Clipper operations - xs/t/11_clipper.t", "[ClipperUtils]") {
  42. // CCW oriented contour
  43. Slic3r::Polygon square{ { 200, 100 }, {200, 200}, {100, 200}, {100, 100} };
  44. // CW oriented contour
  45. Slic3r::Polygon hole_in_square{ { 160, 140 }, { 140, 140 }, { 140, 160 }, { 160, 160 } };
  46. Slic3r::ExPolygon square_with_hole(square, hole_in_square);
  47. GIVEN("square_with_hole") {
  48. WHEN("offset") {
  49. Polygons result = Slic3r::offset(square_with_hole, 5.f);
  50. THEN("offset matches") {
  51. REQUIRE(result == Polygons {
  52. { { 205, 205 }, { 95, 205 }, { 95, 95 }, { 205, 95 }, },
  53. { { 155, 145 }, { 145, 145 }, { 145, 155 }, { 155, 155 } } });
  54. }
  55. }
  56. WHEN("offset_ex") {
  57. ExPolygons result = Slic3r::offset_ex(square_with_hole, 5.f);
  58. THEN("offset matches") {
  59. REQUIRE(result == ExPolygons { {
  60. { { 205, 205 }, { 95, 205 }, { 95, 95 }, { 205, 95 }, },
  61. { { 145, 145 }, { 145, 155 }, { 155, 155 }, { 155, 145 } } } } );
  62. }
  63. }
  64. WHEN("offset2_ex") {
  65. ExPolygons result = Slic3r::offset2_ex({ square_with_hole }, 5.f, -2.f);
  66. THEN("offset matches") {
  67. REQUIRE(result == ExPolygons { {
  68. { { 203, 203 }, { 97, 203 }, { 97, 97 }, { 203, 97 } },
  69. { { 143, 143 }, { 143, 157 }, { 157, 157 }, { 157, 143 } } } } );
  70. }
  71. }
  72. }
  73. GIVEN("square_with_hole 2") {
  74. Slic3r::ExPolygon square_with_hole(
  75. { { 20000000, 20000000 }, { 0, 20000000 }, { 0, 0 }, { 20000000, 0 } },
  76. { { 5000000, 15000000 }, { 15000000, 15000000 }, { 15000000, 5000000 }, { 5000000, 5000000 } });
  77. WHEN("offset2_ex") {
  78. Slic3r::ExPolygons result = Slic3r::offset2_ex(ExPolygons { square_with_hole }, -1.f, 1.f);
  79. THEN("offset matches") {
  80. REQUIRE(result.size() == 1);
  81. REQUIRE(square_with_hole.area() == result.front().area());
  82. }
  83. }
  84. }
  85. GIVEN("square and hole") {
  86. WHEN("diff_ex") {
  87. ExPolygons result = Slic3r::diff_ex(Polygons{ square }, Polygons{ hole_in_square });
  88. THEN("hole is created") {
  89. REQUIRE(result.size() == 1);
  90. REQUIRE(square_with_hole.area() == result.front().area());
  91. }
  92. }
  93. }
  94. GIVEN("polyline") {
  95. Polyline polyline { { 50, 150 }, { 300, 150 } };
  96. WHEN("intersection_pl") {
  97. Polylines result = Slic3r::intersection_pl(polyline, ExPolygon{ square, hole_in_square });
  98. THEN("correct number of result lines") {
  99. REQUIRE(result.size() == 2);
  100. }
  101. THEN("result lines have correct length") {
  102. // results are in no particular order
  103. REQUIRE(result[0].length() == 40);
  104. REQUIRE(result[1].length() == 40);
  105. }
  106. }
  107. WHEN("diff_pl") {
  108. Polylines result = Slic3r::diff_pl({ polyline }, Polygons{ square, hole_in_square });
  109. THEN("correct number of result lines") {
  110. REQUIRE(result.size() == 3);
  111. }
  112. // results are in no particular order
  113. THEN("the left result line has correct length") {
  114. REQUIRE(std::count_if(result.begin(), result.end(), [](const Polyline &pl) { return pl.length() == 50; }) == 1);
  115. }
  116. THEN("the right result line has correct length") {
  117. REQUIRE(std::count_if(result.begin(), result.end(), [](const Polyline &pl) { return pl.length() == 100; }) == 1);
  118. }
  119. THEN("the central result line has correct length") {
  120. REQUIRE(std::count_if(result.begin(), result.end(), [](const Polyline &pl) { return pl.length() == 20; }) == 1);
  121. }
  122. }
  123. }
  124. GIVEN("Clipper bug #96 / Slic3r issue #2028") {
  125. Slic3r::Polyline subject{
  126. { 44735000, 31936670 }, { 55270000, 31936670 }, { 55270000, 25270000 }, { 74730000, 25270000 }, { 74730000, 44730000 }, { 68063296, 44730000 }, { 68063296, 55270000 }, { 74730000, 55270000 },
  127. { 74730000, 74730000 }, { 55270000, 74730000 }, { 55270000, 68063296 }, { 44730000, 68063296 }, { 44730000, 74730000 }, { 25270000, 74730000 }, { 25270000, 55270000 }, { 31936670, 55270000 },
  128. { 31936670, 44730000 }, { 25270000, 44730000 }, { 25270000, 25270000 }, { 44730000, 25270000 }, { 44730000, 31936670 } };
  129. Slic3r::Polygon clip { {75200000, 45200000}, {54800000, 45200000}, {54800000, 24800000}, {75200000, 24800000} };
  130. Slic3r::Polylines result = Slic3r::intersection_pl(subject, ExPolygon{ clip });
  131. THEN("intersection_pl - result is not empty") {
  132. REQUIRE(result.size() == 1);
  133. }
  134. }
  135. GIVEN("Clipper bug #122") {
  136. Slic3r::Polyline subject { { 1975, 1975 }, { 25, 1975 }, { 25, 25 }, { 1975, 25 }, { 1975, 1975 } };
  137. Slic3r::Polygons clip { { { 2025, 2025 }, { -25, 2025 } , { -25, -25 }, { 2025, -25 } },
  138. { { 525, 525 }, { 525, 1475 }, { 1475, 1475 }, { 1475, 525 } } };
  139. Slic3r::Polylines result = Slic3r::intersection_pl({ subject }, clip);
  140. THEN("intersection_pl - result is not empty") {
  141. REQUIRE(result.size() == 1);
  142. REQUIRE(result.front().points.size() == 5);
  143. }
  144. }
  145. GIVEN("Clipper bug #126") {
  146. Slic3r::Polyline subject { { 200000, 19799999 }, { 200000, 200000 }, { 24304692, 200000 }, { 15102879, 17506106 }, { 13883200, 19799999 }, { 200000, 19799999 } };
  147. Slic3r::Polygon clip { { 15257205, 18493894 }, { 14350057, 20200000 }, { -200000, 20200000 }, { -200000, -200000 }, { 25196917, -200000 } };
  148. Slic3r::Polylines result = Slic3r::intersection_pl(subject, ExPolygon{ clip });
  149. THEN("intersection_pl - result is not empty") {
  150. REQUIRE(result.size() == 1);
  151. }
  152. THEN("intersection_pl - result has same length as subject polyline") {
  153. REQUIRE(result.front().length() == Approx(subject.length()));
  154. }
  155. }
  156. #if 0
  157. {
  158. # Clipper does not preserve polyline orientation
  159. my $polyline = Slic3r::Polyline->new([50, 150], [300, 150]);
  160. my $result = Slic3r::Geometry::Clipper::intersection_pl([$polyline], [$square]);
  161. is scalar(@$result), 1, 'intersection_pl - correct number of result lines';
  162. is_deeply $result->[0]->pp, [[100, 150], [200, 150]], 'clipped line orientation is preserved';
  163. }
  164. {
  165. # Clipper does not preserve polyline orientation
  166. my $polyline = Slic3r::Polyline->new([300, 150], [50, 150]);
  167. my $result = Slic3r::Geometry::Clipper::intersection_pl([$polyline], [$square]);
  168. is scalar(@$result), 1, 'intersection_pl - correct number of result lines';
  169. is_deeply $result->[0]->pp, [[200, 150], [100, 150]], 'clipped line orientation is preserved';
  170. }
  171. {
  172. # Disabled until Clipper bug #127 is fixed
  173. my $subject = [
  174. Slic3r::Polyline->new([-90000000, -100000000], [-90000000, 100000000]), # vertical
  175. Slic3r::Polyline->new([-100000000, -10000000], [100000000, -10000000]), # horizontal
  176. Slic3r::Polyline->new([-100000000, 0], [100000000, 0]), # horizontal
  177. Slic3r::Polyline->new([-100000000, 10000000], [100000000, 10000000]), # horizontal
  178. ];
  179. my $clip = Slic3r::Polygon->new(# a circular, convex, polygon
  180. [99452190, 10452846], [97814760, 20791169], [95105652, 30901699], [91354546, 40673664], [86602540, 50000000],
  181. [80901699, 58778525], [74314483, 66913061], [66913061, 74314483], [58778525, 80901699], [50000000, 86602540],
  182. [40673664, 91354546], [30901699, 95105652], [20791169, 97814760], [10452846, 99452190], [0, 100000000],
  183. [-10452846, 99452190], [-20791169, 97814760], [-30901699, 95105652], [-40673664, 91354546],
  184. [-50000000, 86602540], [-58778525, 80901699], [-66913061, 74314483], [-74314483, 66913061],
  185. [-80901699, 58778525], [-86602540, 50000000], [-91354546, 40673664], [-95105652, 30901699],
  186. [-97814760, 20791169], [-99452190, 10452846], [-100000000, 0], [-99452190, -10452846],
  187. [-97814760, -20791169], [-95105652, -30901699], [-91354546, -40673664], [-86602540, -50000000],
  188. [-80901699, -58778525], [-74314483, -66913061], [-66913061, -74314483], [-58778525, -80901699],
  189. [-50000000, -86602540], [-40673664, -91354546], [-30901699, -95105652], [-20791169, -97814760],
  190. [-10452846, -99452190], [0, -100000000], [10452846, -99452190], [20791169, -97814760],
  191. [30901699, -95105652], [40673664, -91354546], [50000000, -86602540], [58778525, -80901699],
  192. [66913061, -74314483], [74314483, -66913061], [80901699, -58778525], [86602540, -50000000],
  193. [91354546, -40673664], [95105652, -30901699], [97814760, -20791169], [99452190, -10452846], [100000000, 0]
  194. );
  195. my $result = Slic3r::Geometry::Clipper::intersection_pl($subject, [$clip]);
  196. is scalar(@$result), scalar(@$subject), 'intersection_pl - expected number of polylines';
  197. is sum(map scalar(@$_), @$result), scalar(@$subject) * 2, 'intersection_pl - expected number of points in polylines';
  198. }
  199. #endif
  200. }
  201. SCENARIO("Various Clipper operations - t/clipper.t", "[ClipperUtils]") {
  202. GIVEN("square with hole") {
  203. // CCW oriented contour
  204. Slic3r::Polygon square { { 10, 10 }, { 20, 10 }, { 20, 20 }, { 10, 20 } };
  205. Slic3r::Polygon square2 { { 5, 12 }, { 25, 12 }, { 25, 18 }, { 5, 18 } };
  206. // CW oriented contour
  207. Slic3r::Polygon hole_in_square { { 14, 14 }, { 14, 16 }, { 16, 16 }, { 16, 14 } };
  208. WHEN("intersection_ex with another square") {
  209. ExPolygons intersection = Slic3r::intersection_ex(Polygons{ square, hole_in_square }, Polygons{ square2 });
  210. THEN("intersection area matches (hole is preserved)") {
  211. ExPolygon match({ { 20, 18 }, { 10, 18 }, { 10, 12 }, { 20, 12 } },
  212. { { 14, 16 }, { 16, 16 }, { 16, 14 }, { 14, 14 } });
  213. REQUIRE(intersection.size() == 1);
  214. REQUIRE(intersection.front().area() == Approx(match.area()));
  215. }
  216. }
  217. ExPolygons expolygons { ExPolygon { square, hole_in_square } };
  218. WHEN("Clipping line 1") {
  219. Polylines intersection = intersection_pl({ Polyline { { 15, 18 }, { 15, 15 } } }, expolygons);
  220. THEN("line is clipped to square with hole") {
  221. REQUIRE((Vec2f(15, 18) - Vec2f(15, 16)).norm() == Approx(intersection.front().length()));
  222. }
  223. }
  224. WHEN("Clipping line 2") {
  225. Polylines intersection = intersection_pl({ Polyline { { 15, 15 }, { 15, 12 } } }, expolygons);
  226. THEN("line is clipped to square with hole") {
  227. REQUIRE((Vec2f(15, 14) - Vec2f(15, 12)).norm() == Approx(intersection.front().length()));
  228. }
  229. }
  230. WHEN("Clipping line 3") {
  231. Polylines intersection = intersection_pl({ Polyline { { 12, 18 }, { 18, 18 } } }, expolygons);
  232. THEN("line is clipped to square with hole") {
  233. REQUIRE((Vec2f(18, 18) - Vec2f(12, 18)).norm() == Approx(intersection.front().length()));
  234. }
  235. }
  236. WHEN("Clipping line 4") {
  237. Polylines intersection = intersection_pl({ Polyline { { 5, 15 }, { 30, 15 } } }, expolygons);
  238. THEN("line is clipped to square with hole") {
  239. REQUIRE((Vec2f(14, 15) - Vec2f(10, 15)).norm() == Approx(intersection.front().length()));
  240. REQUIRE((Vec2f(20, 15) - Vec2f(16, 15)).norm() == Approx(intersection[1].length()));
  241. }
  242. }
  243. WHEN("Clipping line 5") {
  244. Polylines intersection = intersection_pl({ Polyline { { 30, 15 }, { 5, 15 } } }, expolygons);
  245. THEN("reverse line is clipped to square with hole") {
  246. REQUIRE((Vec2f(20, 15) - Vec2f(16, 15)).norm() == Approx(intersection.front().length()));
  247. REQUIRE((Vec2f(14, 15) - Vec2f(10, 15)).norm() == Approx(intersection[1].length()));
  248. }
  249. }
  250. WHEN("Clipping line 6") {
  251. Polylines intersection = intersection_pl({ Polyline { { 10, 18 }, { 20, 18 } } }, expolygons);
  252. THEN("tangent line is clipped to square with hole") {
  253. REQUIRE((Vec2f(20, 18) - Vec2f(10, 18)).norm() == Approx(intersection.front().length()));
  254. }
  255. }
  256. }
  257. GIVEN("square with hole 2") {
  258. // CCW oriented contour
  259. Slic3r::Polygon square { { 0, 0 }, { 40, 0 }, { 40, 40 }, { 0, 40 } };
  260. Slic3r::Polygon square2 { { 10, 10 }, { 30, 10 }, { 30, 30 }, { 10, 30 } };
  261. // CW oriented contour
  262. Slic3r::Polygon hole { { 15, 15 }, { 15, 25 }, { 25, 25 }, {25, 15 } };
  263. WHEN("union_ex with another square") {
  264. ExPolygons union_ = Slic3r::union_ex({ square, square2, hole });
  265. THEN("union of two ccw and one cw is a contour with no holes") {
  266. REQUIRE(union_.size() == 1);
  267. REQUIRE(union_.front() == ExPolygon { { 40, 40 }, { 0, 40 }, { 0, 0 }, { 40, 0 } } );
  268. }
  269. }
  270. WHEN("diff_ex with another square") {
  271. ExPolygons diff = Slic3r::diff_ex(Polygons{ square, square2 }, Polygons{ hole });
  272. THEN("difference of a cw from two ccw is a contour with one hole") {
  273. REQUIRE(diff.size() == 1);
  274. REQUIRE(diff.front().area() == Approx(ExPolygon({ {40, 40}, {0, 40}, {0, 0}, {40, 0} }, { {15, 25}, {25, 25}, {25, 15}, {15, 15} }).area()));
  275. }
  276. }
  277. }
  278. GIVEN("yet another square") {
  279. Slic3r::Polygon square { { 10, 10 }, { 20, 10 }, { 20, 20 }, { 10, 20 } };
  280. Slic3r::Polyline square_pl = square.split_at_first_point();
  281. WHEN("no-op diff_pl") {
  282. Slic3r::Polylines res = Slic3r::diff_pl({ square_pl }, Polygons{});
  283. THEN("returns the right number of polylines") {
  284. REQUIRE(res.size() == 1);
  285. }
  286. THEN("returns the unmodified input polyline") {
  287. REQUIRE(res.front().points.size() == square_pl.points.size());
  288. }
  289. }
  290. }
  291. GIVEN("circle") {
  292. Slic3r::ExPolygon circle_with_hole { Polygon::new_scale({
  293. { 151.8639,288.1192 }, {133.2778,284.6011}, { 115.0091,279.6997 }, { 98.2859,270.8606 }, { 82.2734,260.7933 },
  294. { 68.8974,247.4181 }, { 56.5622,233.0777 }, { 47.7228,216.3558 }, { 40.1617,199.0172 }, { 36.6431,180.4328 },
  295. { 34.932,165.2312 }, { 37.5567,165.1101 }, { 41.0547,142.9903 }, { 36.9056,141.4295 }, { 40.199,124.1277 },
  296. { 47.7776,106.7972 }, { 56.6335,90.084 }, { 68.9831,75.7557 }, { 82.3712,62.3948 }, { 98.395,52.3429 },
  297. { 115.1281,43.5199 }, { 133.4004,38.6374 }, { 151.9884,35.1378 }, { 170.8905,35.8571 }, { 189.6847,37.991 },
  298. { 207.5349,44.2488 }, { 224.8662,51.8273 }, { 240.0786,63.067 }, { 254.407,75.4169 }, { 265.6311,90.6406 },
  299. { 275.6832,106.6636 }, { 281.9225,124.52 }, { 286.8064,142.795 }, { 287.5061,161.696 }, { 286.7874,180.5972 },
  300. { 281.8856,198.8664 }, { 275.6283,216.7169 }, { 265.5604,232.7294 }, { 254.3211,247.942 }, { 239.9802,260.2776 },
  301. { 224.757,271.5022 }, { 207.4179,279.0635 }, { 189.5605,285.3035 }, { 170.7649,287.4188 }
  302. }) };
  303. circle_with_hole.holes = { Polygon::new_scale({
  304. { 158.227,215.9007 }, { 164.5136,215.9007 }, { 175.15,214.5007 }, { 184.5576,210.6044 }, { 190.2268,207.8743 },
  305. { 199.1462,201.0306 }, { 209.0146,188.346 }, { 213.5135,177.4829 }, { 214.6979,168.4866 }, { 216.1025,162.3325 },
  306. { 214.6463,151.2703 }, { 213.2471,145.1399 }, { 209.0146,134.9203 }, { 199.1462,122.2357 }, { 189.8944,115.1366 },
  307. { 181.2504,111.5567 }, { 175.5684,108.8205 }, { 164.5136,107.3655 }, { 158.2269,107.3655 }, { 147.5907,108.7656 },
  308. { 138.183,112.6616 }, { 132.5135,115.3919 }, { 123.5943,122.2357 }, { 113.7259,134.92 }, { 109.2269,145.7834 },
  309. { 108.0426,154.7799 }, { 106.638,160.9339 }, { 108.0941,171.9957 }, { 109.4933,178.1264 }, { 113.7259,188.3463 },
  310. { 123.5943,201.0306 }, { 132.8461,208.1296 }, { 141.4901,211.7094 }, { 147.172,214.4458 }
  311. }) };
  312. THEN("contour is counter-clockwise") {
  313. REQUIRE(circle_with_hole.contour.is_counter_clockwise());
  314. }
  315. THEN("hole is counter-clockwise") {
  316. REQUIRE(circle_with_hole.holes.size() == 1);
  317. REQUIRE(circle_with_hole.holes.front().is_clockwise());
  318. }
  319. WHEN("clipping a line") {
  320. auto line = Polyline::new_scale({ { 152.742,288.086671142818 }, { 152.742,34.166466971035 } });
  321. Polylines intersection = intersection_pl(line, to_polygons(circle_with_hole));
  322. THEN("clipped to two pieces") {
  323. REQUIRE(intersection.front().length() == Approx((Vec2d(152742000, 215178843) - Vec2d(152742000, 288086661)).norm()));
  324. REQUIRE(intersection[1].length() == Approx((Vec2d(152742000, 35166477) - Vec2d(152742000, 108087507)).norm()));
  325. }
  326. }
  327. }
  328. GIVEN("line") {
  329. THEN("expand by 5") {
  330. REQUIRE(offset(Polyline({10,10}, {20,10}), 5).front().area() == Polygon({ {10,5}, {20,5}, {20,15}, {10,15} }).area());
  331. }
  332. }
  333. }
  334. template<e_ordering o = e_ordering::OFF, class P, class P_Alloc, class Tree>
  335. double polytree_area(const Tree &tree, std::vector<P, P_Alloc> *out)
  336. {
  337. traverse_pt<o>(tree, out);
  338. return std::accumulate(out->begin(), out->end(), 0.0,
  339. [](double a, const P &p) { return a + p.area(); });
  340. }
  341. size_t count_polys(const ExPolygons& expolys)
  342. {
  343. size_t c = 0;
  344. for (auto &ep : expolys) c += ep.holes.size() + 1;
  345. return c;
  346. }
  347. TEST_CASE("Traversing Clipper PolyTree", "[ClipperUtils]") {
  348. // Create a polygon representing unit box
  349. Polygon unitbox;
  350. const coord_t UNIT = coord_t(1. / SCALING_FACTOR);
  351. unitbox.points = Points{Point{0, 0}, Point{UNIT, coord_t(0)}, Point{UNIT, UNIT}, Point{coord_t(0), UNIT}};
  352. Polygon box_frame = unitbox;
  353. box_frame.scale(20, 10);
  354. Polygon hole_left = unitbox;
  355. hole_left.scale(8);
  356. hole_left.translate(UNIT, UNIT);
  357. hole_left.reverse();
  358. Polygon hole_right = hole_left;
  359. hole_right.translate(UNIT * 10, 0);
  360. Polygon inner_left = unitbox;
  361. inner_left.scale(4);
  362. inner_left.translate(UNIT * 3, UNIT * 3);
  363. Polygon inner_right = inner_left;
  364. inner_right.translate(UNIT * 10, 0);
  365. Polygons reference = union_({box_frame, hole_left, hole_right, inner_left, inner_right});
  366. ClipperLib::PolyTree tree = union_pt(reference);
  367. double area_sum = box_frame.area() + hole_left.area() +
  368. hole_right.area() + inner_left.area() +
  369. inner_right.area();
  370. REQUIRE(area_sum > 0);
  371. SECTION("Traverse into Polygons WITHOUT spatial ordering") {
  372. Polygons output;
  373. REQUIRE(area_sum == Approx(polytree_area(tree.GetFirst(), &output)));
  374. REQUIRE(output.size() == reference.size());
  375. }
  376. SECTION("Traverse into ExPolygons WITHOUT spatial ordering") {
  377. ExPolygons output;
  378. REQUIRE(area_sum == Approx(polytree_area(tree.GetFirst(), &output)));
  379. REQUIRE(count_polys(output) == reference.size());
  380. }
  381. SECTION("Traverse into Polygons WITH spatial ordering") {
  382. Polygons output;
  383. REQUIRE(area_sum == Approx(polytree_area<e_ordering::ON>(tree.GetFirst(), &output)));
  384. REQUIRE(output.size() == reference.size());
  385. }
  386. SECTION("Traverse into ExPolygons WITH spatial ordering") {
  387. ExPolygons output;
  388. REQUIRE(area_sum == Approx(polytree_area<e_ordering::ON>(tree.GetFirst(), &output)));
  389. REQUIRE(count_polys(output) == reference.size());
  390. }
  391. }