test_geometry.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. #include <catch2/catch.hpp>
  2. #include "libslic3r/Point.hpp"
  3. #include "libslic3r/BoundingBox.hpp"
  4. #include "libslic3r/Polygon.hpp"
  5. #include "libslic3r/Polyline.hpp"
  6. #include "libslic3r/Line.hpp"
  7. #include "libslic3r/Geometry.hpp"
  8. #include "libslic3r/Geometry/Circle.hpp"
  9. #include "libslic3r/Geometry/ConvexHull.hpp"
  10. #include "libslic3r/ClipperUtils.hpp"
  11. #include "libslic3r/ShortestPath.hpp"
  12. //#include <random>
  13. //#include "libnest2d/tools/benchmark.h"
  14. #include "libslic3r/SVG.hpp"
  15. #include "../libnest2d/printer_parts.hpp"
  16. #include <unordered_set>
  17. using namespace Slic3r;
  18. TEST_CASE("Line::parallel_to", "[Geometry]"){
  19. Line l{ { 100000, 0 }, { 0, 0 } };
  20. Line l2{ { 200000, 0 }, { 0, 0 } };
  21. REQUIRE(l.parallel_to(l));
  22. REQUIRE(l.parallel_to(l2));
  23. Line l3(l2);
  24. l3.rotate(0.9 * EPSILON, { 0, 0 });
  25. REQUIRE(l.parallel_to(l3));
  26. Line l4(l2);
  27. l4.rotate(1.1 * EPSILON, { 0, 0 });
  28. REQUIRE(! l.parallel_to(l4));
  29. // The angle epsilon is so low that vectors shorter than 100um rotated by epsilon radians are not rotated at all.
  30. Line l5{ { 20000, 0 }, { 0, 0 } };
  31. l5.rotate(1.1 * EPSILON, { 0, 0 });
  32. REQUIRE(l.parallel_to(l5));
  33. l.rotate(1., { 0, 0 });
  34. Point offset{ 342876, 97636249 };
  35. l.translate(offset);
  36. l3.rotate(1., { 0, 0 });
  37. l3.translate(offset);
  38. l4.rotate(1., { 0, 0 });
  39. l4.translate(offset);
  40. REQUIRE(l.parallel_to(l3));
  41. REQUIRE(!l.parallel_to(l4));
  42. }
  43. TEST_CASE("Line::perpendicular_to", "[Geometry]") {
  44. Line l{ { 100000, 0 }, { 0, 0 } };
  45. Line l2{ { 0, 200000 }, { 0, 0 } };
  46. REQUIRE(! l.perpendicular_to(l));
  47. REQUIRE(l.perpendicular_to(l2));
  48. Line l3(l2);
  49. l3.rotate(0.9 * EPSILON, { 0, 0 });
  50. REQUIRE(l.perpendicular_to(l3));
  51. Line l4(l2);
  52. l4.rotate(1.1 * EPSILON, { 0, 0 });
  53. REQUIRE(! l.perpendicular_to(l4));
  54. // The angle epsilon is so low that vectors shorter than 100um rotated by epsilon radians are not rotated at all.
  55. Line l5{ { 0, 20000 }, { 0, 0 } };
  56. l5.rotate(1.1 * EPSILON, { 0, 0 });
  57. REQUIRE(l.perpendicular_to(l5));
  58. l.rotate(1., { 0, 0 });
  59. Point offset{ 342876, 97636249 };
  60. l.translate(offset);
  61. l3.rotate(1., { 0, 0 });
  62. l3.translate(offset);
  63. l4.rotate(1., { 0, 0 });
  64. l4.translate(offset);
  65. REQUIRE(l.perpendicular_to(l3));
  66. REQUIRE(! l.perpendicular_to(l4));
  67. }
  68. TEST_CASE("Polygon::contains works properly", "[Geometry]"){
  69. // this test was failing on Windows (GH #1950)
  70. Slic3r::Polygon polygon(std::vector<Point>({
  71. Point(207802834,-57084522),
  72. Point(196528149,-37556190),
  73. Point(173626821,-25420928),
  74. Point(171285751,-21366123),
  75. Point(118673592,-21366123),
  76. Point(116332562,-25420928),
  77. Point(93431208,-37556191),
  78. Point(82156517,-57084523),
  79. Point(129714478,-84542120),
  80. Point(160244873,-84542120)
  81. }));
  82. Point point(95706562, -57294774);
  83. REQUIRE(polygon.contains(point));
  84. }
  85. SCENARIO("Intersections of line segments", "[Geometry]"){
  86. GIVEN("Integer coordinates"){
  87. Line line1(Point(5,15),Point(30,15));
  88. Line line2(Point(10,20), Point(10,10));
  89. THEN("The intersection is valid"){
  90. Point point;
  91. line1.intersection(line2,&point);
  92. REQUIRE(Point(10,15) == point);
  93. }
  94. }
  95. GIVEN("Scaled coordinates"){
  96. Line line1(Point(73.6310778185108 / 0.00001, 371.74239268924 / 0.00001), Point(73.6310778185108 / 0.00001, 501.74239268924 / 0.00001));
  97. Line line2(Point(75/0.00001, 437.9853/0.00001), Point(62.7484/0.00001, 440.4223/0.00001));
  98. THEN("There is still an intersection"){
  99. Point point;
  100. REQUIRE(line1.intersection(line2,&point));
  101. }
  102. }
  103. }
  104. SCENARIO("polygon_is_convex works") {
  105. GIVEN("A square of dimension 10") {
  106. WHEN("Polygon is convex clockwise") {
  107. Polygon cw_square { { {0, 0}, {0,10}, {10,10}, {10,0} } };
  108. THEN("it is not convex") {
  109. REQUIRE(! polygon_is_convex(cw_square));
  110. }
  111. }
  112. WHEN("Polygon is convex counter-clockwise") {
  113. Polygon ccw_square { { {0, 0}, {10,0}, {10,10}, {0,10} } };
  114. THEN("it is convex") {
  115. REQUIRE(polygon_is_convex(ccw_square));
  116. }
  117. }
  118. }
  119. GIVEN("A concave polygon") {
  120. Polygon concave = { {0,0}, {10,0}, {10,10}, {0,10}, {0,6}, {4,6}, {4,4}, {0,4} };
  121. THEN("It is not convex") {
  122. REQUIRE(! polygon_is_convex(concave));
  123. }
  124. }
  125. }
  126. TEST_CASE("Creating a polyline generates the obvious lines", "[Geometry]"){
  127. Slic3r::Polyline polyline;
  128. polyline.points = std::vector<Point>({Point(0, 0), Point(10, 0), Point(20, 0)});
  129. REQUIRE(polyline.lines().at(0).a == Point(0,0));
  130. REQUIRE(polyline.lines().at(0).b == Point(10,0));
  131. REQUIRE(polyline.lines().at(1).a == Point(10,0));
  132. REQUIRE(polyline.lines().at(1).b == Point(20,0));
  133. }
  134. TEST_CASE("Splitting a Polygon generates a polyline correctly", "[Geometry]"){
  135. Slic3r::Polygon polygon(std::vector<Point>({Point(0, 0), Point(10, 0), Point(5, 5)}));
  136. Slic3r::Polyline split = polygon.split_at_index(1);
  137. REQUIRE(split.points[0]==Point(10,0));
  138. REQUIRE(split.points[1]==Point(5,5));
  139. REQUIRE(split.points[2]==Point(0,0));
  140. REQUIRE(split.points[3]==Point(10,0));
  141. }
  142. TEST_CASE("Bounding boxes are scaled appropriately", "[Geometry]"){
  143. BoundingBox bb(std::vector<Point>({Point(0, 1), Point(10, 2), Point(20, 2)}));
  144. bb.scale(2);
  145. REQUIRE(bb.min == Point(0,2));
  146. REQUIRE(bb.max == Point(40,4));
  147. }
  148. TEST_CASE("Offseting a line generates a polygon correctly", "[Geometry]"){
  149. Slic3r::Polyline tmp = { Point(10,10), Point(20,10) };
  150. Slic3r::Polygon area = offset(tmp,5).at(0);
  151. REQUIRE(area.area() == Slic3r::Polygon(std::vector<Point>({Point(10,5),Point(20,5),Point(20,15),Point(10,15)})).area());
  152. }
  153. SCENARIO("Circle Fit, TaubinFit with Newton's method", "[Geometry]") {
  154. GIVEN("A vector of Vec2ds arranged in a half-circle with approximately the same distance R from some point") {
  155. Vec2d expected_center(-6, 0);
  156. Vec2ds sample {Vec2d(6.0, 0), Vec2d(5.1961524, 3), Vec2d(3 ,5.1961524), Vec2d(0, 6.0), Vec2d(3, 5.1961524), Vec2d(-5.1961524, 3), Vec2d(-6.0, 0)};
  157. std::transform(sample.begin(), sample.end(), sample.begin(), [expected_center] (const Vec2d& a) { return a + expected_center;});
  158. WHEN("Circle fit is called on the entire array") {
  159. Vec2d result_center(0,0);
  160. result_center = Geometry::circle_center_taubin_newton(sample);
  161. THEN("A center point of -6,0 is returned.") {
  162. REQUIRE(is_approx(result_center, expected_center));
  163. }
  164. }
  165. WHEN("Circle fit is called on the first four points") {
  166. Vec2d result_center(0,0);
  167. result_center = Geometry::circle_center_taubin_newton(sample.cbegin(), sample.cbegin()+4);
  168. THEN("A center point of -6,0 is returned.") {
  169. REQUIRE(is_approx(result_center, expected_center));
  170. }
  171. }
  172. WHEN("Circle fit is called on the middle four points") {
  173. Vec2d result_center(0,0);
  174. result_center = Geometry::circle_center_taubin_newton(sample.cbegin()+2, sample.cbegin()+6);
  175. THEN("A center point of -6,0 is returned.") {
  176. REQUIRE(is_approx(result_center, expected_center));
  177. }
  178. }
  179. }
  180. GIVEN("A vector of Vec2ds arranged in a half-circle with approximately the same distance R from some point") {
  181. Vec2d expected_center(-3, 9);
  182. Vec2ds sample {Vec2d(6.0, 0), Vec2d(5.1961524, 3), Vec2d(3 ,5.1961524),
  183. Vec2d(0, 6.0),
  184. Vec2d(3, 5.1961524), Vec2d(-5.1961524, 3), Vec2d(-6.0, 0)};
  185. std::transform(sample.begin(), sample.end(), sample.begin(), [expected_center] (const Vec2d& a) { return a + expected_center;});
  186. WHEN("Circle fit is called on the entire array") {
  187. Vec2d result_center(0,0);
  188. result_center = Geometry::circle_center_taubin_newton(sample);
  189. THEN("A center point of 3,9 is returned.") {
  190. REQUIRE(is_approx(result_center, expected_center));
  191. }
  192. }
  193. WHEN("Circle fit is called on the first four points") {
  194. Vec2d result_center(0,0);
  195. result_center = Geometry::circle_center_taubin_newton(sample.cbegin(), sample.cbegin()+4);
  196. THEN("A center point of 3,9 is returned.") {
  197. REQUIRE(is_approx(result_center, expected_center));
  198. }
  199. }
  200. WHEN("Circle fit is called on the middle four points") {
  201. Vec2d result_center(0,0);
  202. result_center = Geometry::circle_center_taubin_newton(sample.cbegin()+2, sample.cbegin()+6);
  203. THEN("A center point of 3,9 is returned.") {
  204. REQUIRE(is_approx(result_center, expected_center));
  205. }
  206. }
  207. }
  208. GIVEN("A vector of Points arranged in a half-circle with approximately the same distance R from some point") {
  209. Point expected_center { Point::new_scale(-3, 9)};
  210. Points sample {Point::new_scale(6.0, 0), Point::new_scale(5.1961524, 3), Point::new_scale(3 ,5.1961524),
  211. Point::new_scale(0, 6.0),
  212. Point::new_scale(3, 5.1961524), Point::new_scale(-5.1961524, 3), Point::new_scale(-6.0, 0)};
  213. std::transform(sample.begin(), sample.end(), sample.begin(), [expected_center] (const Point& a) { return a + expected_center;});
  214. WHEN("Circle fit is called on the entire array") {
  215. Point result_center(0,0);
  216. result_center = Geometry::circle_center_taubin_newton(sample);
  217. THEN("A center point of scaled 3,9 is returned.") {
  218. REQUIRE(is_approx(result_center, expected_center));
  219. }
  220. }
  221. WHEN("Circle fit is called on the first four points") {
  222. Point result_center(0,0);
  223. result_center = Geometry::circle_center_taubin_newton(sample.cbegin(), sample.cbegin()+4);
  224. THEN("A center point of scaled 3,9 is returned.") {
  225. REQUIRE(is_approx(result_center, expected_center));
  226. }
  227. }
  228. WHEN("Circle fit is called on the middle four points") {
  229. Point result_center(0,0);
  230. result_center = Geometry::circle_center_taubin_newton(sample.cbegin()+2, sample.cbegin()+6);
  231. THEN("A center point of scaled 3,9 is returned.") {
  232. REQUIRE(is_approx(result_center, expected_center));
  233. }
  234. }
  235. }
  236. }
  237. TEST_CASE("smallest_enclosing_circle_welzl", "[Geometry]") {
  238. // Some random points in plane.
  239. Points pts {
  240. { 89243, 4359 }, { 763465, 59687 }, { 3245, 734987 }, { 2459867, 987634 }, { 759866, 67843982 }, { 9754687, 9834658 }, { 87235089, 743984373 },
  241. { 65874456, 2987546 }, { 98234524, 657654873 }, { 786243598, 287934765 }, { 824356, 734265 }, { 82576449, 7864534 }, { 7826345, 3984765 }
  242. };
  243. const auto c = Slic3r::Geometry::smallest_enclosing_circle_welzl(pts);
  244. // The radius returned is inflated by SCALED_EPSILON, thus all points should be inside.
  245. bool all_inside = std::all_of(pts.begin(), pts.end(), [c](const Point &pt){ return c.contains(pt.cast<double>()); });
  246. auto c2(c);
  247. c2.radius -= SCALED_EPSILON * 2.1;
  248. auto num_on_boundary = std::count_if(pts.begin(), pts.end(), [c2](const Point& pt) { return ! c2.contains(pt.cast<double>(), SCALED_EPSILON); });
  249. REQUIRE(all_inside);
  250. REQUIRE(num_on_boundary == 3);
  251. }
  252. SCENARIO("Path chaining", "[Geometry]") {
  253. GIVEN("A path") {
  254. std::vector<Point> points = { Point(26,26),Point(52,26),Point(0,26),Point(26,52),Point(26,0),Point(0,52),Point(52,52),Point(52,0) };
  255. THEN("Chained with no diagonals (thus 26 units long)") {
  256. std::vector<Points::size_type> indices = chain_points(points);
  257. for (Points::size_type i = 0; i + 1 < indices.size(); ++ i) {
  258. double dist = (points.at(indices.at(i)).cast<double>() - points.at(indices.at(i+1)).cast<double>()).norm();
  259. REQUIRE(std::abs(dist-26) <= EPSILON);
  260. }
  261. }
  262. }
  263. GIVEN("Gyroid infill end points") {
  264. Polylines polylines = {
  265. { {28122608, 3221037}, {27919139, 56036027} },
  266. { {33642863, 3400772}, {30875220, 56450360} },
  267. { {34579315, 3599827}, {35049758, 55971572} },
  268. { {26483070, 3374004}, {23971830, 55763598} },
  269. { {38931405, 4678879}, {38740053, 55077714} },
  270. { {20311895, 5015778}, {20079051, 54551952} },
  271. { {16463068, 6773342}, {18823514, 53992958} },
  272. { {44433771, 7424951}, {42629462, 53346059} },
  273. { {15697614, 7329492}, {15350896, 52089991} },
  274. { {48085792, 10147132}, {46435427, 50792118} },
  275. { {48828819, 10972330}, {49126582, 48368374} },
  276. { {9654526, 12656711}, {10264020, 47691584} },
  277. { {5726905, 18648632}, {8070762, 45082416} },
  278. { {54818187, 39579970}, {52974912, 43271272} },
  279. { {4464342, 37371742}, {5027890, 39106220} },
  280. { {54139746, 18417661}, {55177987, 38472580} },
  281. { {56527590, 32058461}, {56316456, 34067185} },
  282. { {3303988, 29215290}, {3569863, 32985633} },
  283. { {56255666, 25025857}, {56478310, 27144087} },
  284. { {4300034, 22805361}, {3667946, 25752601} },
  285. { {8266122, 14250611}, {6244813, 17751595} },
  286. { {12177955, 9886741}, {10703348, 11491900} }
  287. };
  288. Polylines chained = chain_polylines(polylines);
  289. THEN("Chained taking the shortest path") {
  290. double connection_length = 0.;
  291. for (size_t i = 1; i < chained.size(); ++i) {
  292. const Polyline &pl1 = chained[i - 1];
  293. const Polyline &pl2 = chained[i];
  294. connection_length += (pl2.first_point() - pl1.last_point()).cast<double>().norm();
  295. }
  296. REQUIRE(connection_length < 85206000.);
  297. }
  298. }
  299. GIVEN("Loop pieces") {
  300. Point a { 2185796, 19058485 };
  301. Point b { 3957902, 18149382 };
  302. Point c { 2912841, 18790564 };
  303. Point d { 2831848, 18832390 };
  304. Point e { 3179601, 18627769 };
  305. Point f { 3137952, 18653370 };
  306. Polylines polylines = { { a, b },
  307. { c, d },
  308. { e, f },
  309. { d, a },
  310. { f, c },
  311. { b, e } };
  312. Polylines chained = chain_polylines(polylines, &a);
  313. THEN("Connected without a gap") {
  314. for (size_t i = 0; i < chained.size(); ++i) {
  315. const Polyline &pl1 = (i == 0) ? chained.back() : chained[i - 1];
  316. const Polyline &pl2 = chained[i];
  317. REQUIRE(pl1.points.back() == pl2.points.front());
  318. }
  319. }
  320. }
  321. }
  322. SCENARIO("Line distances", "[Geometry]"){
  323. GIVEN("A line"){
  324. Line line(Point(0, 0), Point(20, 0));
  325. THEN("Points on the line segment have 0 distance"){
  326. REQUIRE(line.distance_to(Point(0, 0)) == 0);
  327. REQUIRE(line.distance_to(Point(20, 0)) == 0);
  328. REQUIRE(line.distance_to(Point(10, 0)) == 0);
  329. }
  330. THEN("Points off the line have the appropriate distance"){
  331. REQUIRE(line.distance_to(Point(10, 10)) == 10);
  332. REQUIRE(line.distance_to(Point(50, 0)) == 30);
  333. }
  334. }
  335. }
  336. SCENARIO("Polygon convex/concave detection", "[Geometry]"){
  337. GIVEN(("A Square with dimension 100")){
  338. auto square = Slic3r::Polygon /*new_scale*/(std::vector<Point>({
  339. Point(100,100),
  340. Point(200,100),
  341. Point(200,200),
  342. Point(100,200)}));
  343. THEN("It has 4 convex points counterclockwise"){
  344. REQUIRE(square.concave_points(PI*4/3).size() == 0);
  345. REQUIRE(square.convex_points(PI*2/3).size() == 4);
  346. }
  347. THEN("It has 4 concave points clockwise"){
  348. square.make_clockwise();
  349. REQUIRE(square.concave_points(PI*4/3).size() == 4);
  350. REQUIRE(square.convex_points(PI*2/3).size() == 0);
  351. }
  352. }
  353. GIVEN("A Square with an extra colinearvertex"){
  354. auto square = Slic3r::Polygon /*new_scale*/(std::vector<Point>({
  355. Point(150,100),
  356. Point(200,100),
  357. Point(200,200),
  358. Point(100,200),
  359. Point(100,100)}));
  360. THEN("It has 4 convex points counterclockwise"){
  361. REQUIRE(square.concave_points(PI*4/3).size() == 0);
  362. REQUIRE(square.convex_points(PI*2/3).size() == 4);
  363. }
  364. }
  365. GIVEN("A Square with an extra collinear vertex in different order"){
  366. auto square = Slic3r::Polygon /*new_scale*/(std::vector<Point>({
  367. Point(200,200),
  368. Point(100,200),
  369. Point(100,100),
  370. Point(150,100),
  371. Point(200,100)}));
  372. THEN("It has 4 convex points counterclockwise"){
  373. REQUIRE(square.concave_points(PI*4/3).size() == 0);
  374. REQUIRE(square.convex_points(PI*2/3).size() == 4);
  375. }
  376. }
  377. GIVEN("A triangle"){
  378. auto triangle = Slic3r::Polygon(std::vector<Point>({
  379. Point(16000170,26257364),
  380. Point(714223,461012),
  381. Point(31286371,461008)
  382. }));
  383. THEN("it has three convex vertices"){
  384. REQUIRE(triangle.concave_points(PI*4/3).size() == 0);
  385. REQUIRE(triangle.convex_points(PI*2/3).size() == 3);
  386. }
  387. }
  388. GIVEN("A triangle with an extra collinear point"){
  389. auto triangle = Slic3r::Polygon(std::vector<Point>({
  390. Point(16000170,26257364),
  391. Point(714223,461012),
  392. Point(20000000,461012),
  393. Point(31286371,461012)
  394. }));
  395. THEN("it has three convex vertices"){
  396. REQUIRE(triangle.concave_points(PI*4/3).size() == 0);
  397. REQUIRE(triangle.convex_points(PI*2/3).size() == 3);
  398. }
  399. }
  400. GIVEN("A polygon with concave vertices with angles of specifically 4/3pi"){
  401. // Two concave vertices of this polygon have angle = PI*4/3, so this test fails
  402. // if epsilon is not used.
  403. auto polygon = Slic3r::Polygon(std::vector<Point>({
  404. Point(60246458,14802768),Point(64477191,12360001),
  405. Point(63727343,11060995),Point(64086449,10853608),
  406. Point(66393722,14850069),Point(66034704,15057334),
  407. Point(65284646,13758387),Point(61053864,16200839),
  408. Point(69200258,30310849),Point(62172547,42483120),
  409. Point(61137680,41850279),Point(67799985,30310848),
  410. Point(51399866,1905506),Point(38092663,1905506),
  411. Point(38092663,692699),Point(52100125,692699)
  412. }));
  413. THEN("the correct number of points are detected"){
  414. REQUIRE(polygon.concave_points(PI*4/3).size() == 6);
  415. REQUIRE(polygon.convex_points(PI*2/3).size() == 10);
  416. }
  417. }
  418. }
  419. TEST_CASE("Triangle Simplification does not result in less than 3 points", "[Geometry]"){
  420. auto triangle = Slic3r::Polygon(std::vector<Point>({
  421. Point(16000170,26257364), Point(714223,461012), Point(31286371,461008)
  422. }));
  423. REQUIRE(triangle.simplify(250000).at(0).points.size() == 3);
  424. }
  425. SCENARIO("Ported from xs/t/14_geometry.t", "[Geometry]"){
  426. GIVEN(("square")){
  427. Slic3r::Points points { { 100, 100 }, {100, 200 }, { 200, 200 }, { 200, 100 }, { 150, 150 } };
  428. Slic3r::Polygon hull = Slic3r::Geometry::convex_hull(points);
  429. SECTION("convex hull returns the correct number of points") { REQUIRE(hull.points.size() == 4); }
  430. }
  431. SECTION("arrange returns expected number of positions") {
  432. Pointfs positions;
  433. Slic3r::Geometry::arrange(4, Vec2d(20, 20), 5, nullptr, positions);
  434. REQUIRE(positions.size() == 4);
  435. }
  436. SECTION("directions_parallel") {
  437. REQUIRE(Slic3r::Geometry::directions_parallel(0, 0, 0));
  438. REQUIRE(Slic3r::Geometry::directions_parallel(0, M_PI, 0));
  439. REQUIRE(Slic3r::Geometry::directions_parallel(0, 0, M_PI / 180));
  440. REQUIRE(Slic3r::Geometry::directions_parallel(0, M_PI, M_PI / 180));
  441. REQUIRE(! Slic3r::Geometry::directions_parallel(M_PI /2, M_PI, 0));
  442. REQUIRE(! Slic3r::Geometry::directions_parallel(M_PI /2, PI, M_PI /180));
  443. }
  444. }
  445. TEST_CASE("Convex polygon intersection on two disjoint squares", "[Geometry][Rotcalip]") {
  446. Polygon A{{0, 0}, {10, 0}, {10, 10}, {0, 10}};
  447. A.scale(1. / SCALING_FACTOR);
  448. Polygon B = A;
  449. B.translate(20 / SCALING_FACTOR, 0);
  450. bool is_inters = Geometry::convex_polygons_intersect(A, B);
  451. REQUIRE(is_inters == false);
  452. }
  453. TEST_CASE("Convex polygon intersection on two intersecting squares", "[Geometry][Rotcalip]") {
  454. Polygon A{{0, 0}, {10, 0}, {10, 10}, {0, 10}};
  455. A.scale(1. / SCALING_FACTOR);
  456. Polygon B = A;
  457. B.translate(5 / SCALING_FACTOR, 5 / SCALING_FACTOR);
  458. bool is_inters = Geometry::convex_polygons_intersect(A, B);
  459. REQUIRE(is_inters == true);
  460. }
  461. TEST_CASE("Convex polygon intersection on two squares touching one edge", "[Geometry][Rotcalip]") {
  462. Polygon A{{0, 0}, {10, 0}, {10, 10}, {0, 10}};
  463. A.scale(1. / SCALING_FACTOR);
  464. Polygon B = A;
  465. B.translate(10 / SCALING_FACTOR, 0);
  466. bool is_inters = Geometry::convex_polygons_intersect(A, B);
  467. REQUIRE(is_inters == false);
  468. }
  469. TEST_CASE("Convex polygon intersection on two squares touching one vertex", "[Geometry][Rotcalip]") {
  470. Polygon A{{0, 0}, {10, 0}, {10, 10}, {0, 10}};
  471. A.scale(1. / SCALING_FACTOR);
  472. Polygon B = A;
  473. B.translate(10 / SCALING_FACTOR, 10 / SCALING_FACTOR);
  474. SVG svg{std::string("one_vertex_touch") + ".svg"};
  475. svg.draw(A, "blue");
  476. svg.draw(B, "green");
  477. svg.Close();
  478. bool is_inters = Geometry::convex_polygons_intersect(A, B);
  479. REQUIRE(is_inters == false);
  480. }
  481. TEST_CASE("Convex polygon intersection on two overlapping squares", "[Geometry][Rotcalip]") {
  482. Polygon A{{0, 0}, {10, 0}, {10, 10}, {0, 10}};
  483. A.scale(1. / SCALING_FACTOR);
  484. Polygon B = A;
  485. bool is_inters = Geometry::convex_polygons_intersect(A, B);
  486. REQUIRE(is_inters == true);
  487. }
  488. //// Only for benchmarking
  489. //static Polygon gen_convex_poly(std::mt19937_64 &rg, size_t point_cnt)
  490. //{
  491. // std::uniform_int_distribution<coord_t> dist(0, 100);
  492. // Polygon out;
  493. // out.points.reserve(point_cnt);
  494. // coord_t tr = dist(rg) * 2 / SCALING_FACTOR;
  495. // for (size_t i = 0; i < point_cnt; ++i)
  496. // out.points.emplace_back(tr + dist(rg) / SCALING_FACTOR,
  497. // tr + dist(rg) / SCALING_FACTOR);
  498. // return Geometry::convex_hull(out.points);
  499. //}
  500. //TEST_CASE("Convex polygon intersection test on random polygons", "[Geometry]") {
  501. // constexpr size_t TEST_CNT = 1000;
  502. // constexpr size_t POINT_CNT = 1000;
  503. // auto seed = std::random_device{}();
  504. //// unsigned long seed = 2525634386;
  505. // std::mt19937_64 rg{seed};
  506. // Benchmark bench;
  507. // auto tests = reserve_vector<std::pair<Polygon, Polygon>>(TEST_CNT);
  508. // auto results = reserve_vector<bool>(TEST_CNT);
  509. // auto expects = reserve_vector<bool>(TEST_CNT);
  510. // for (size_t i = 0; i < TEST_CNT; ++i) {
  511. // tests.emplace_back(gen_convex_poly(rg, POINT_CNT), gen_convex_poly(rg, POINT_CNT));
  512. // }
  513. // bench.start();
  514. // for (const auto &test : tests)
  515. // results.emplace_back(Geometry::convex_polygons_intersect(test.first, test.second));
  516. // bench.stop();
  517. // std::cout << "Test time: " << bench.getElapsedSec() << std::endl;
  518. // bench.start();
  519. // for (const auto &test : tests)
  520. // expects.emplace_back(!intersection(test.first, test.second).empty());
  521. // bench.stop();
  522. // std::cout << "Clipper time: " << bench.getElapsedSec() << std::endl;
  523. // REQUIRE(results.size() == expects.size());
  524. // auto seedstr = std::to_string(seed);
  525. // for (size_t i = 0; i < results.size(); ++i) {
  526. // // std::cout << expects[i] << " ";
  527. // if (results[i] != expects[i]) {
  528. // SVG svg{std::string("fail_seed") + seedstr + "_" + std::to_string(i) + ".svg"};
  529. // svg.draw(tests[i].first, "blue");
  530. // svg.draw(tests[i].second, "green");
  531. // svg.Close();
  532. // // std::cout << std::endl;
  533. // }
  534. // REQUIRE(results[i] == expects[i]);
  535. // }
  536. // std::cout << std::endl;
  537. //}
  538. struct Pair
  539. {
  540. size_t first, second;
  541. bool operator==(const Pair &b) const { return first == b.first && second == b.second; }
  542. };
  543. template<> struct std::hash<Pair> {
  544. size_t operator()(const Pair &c) const
  545. {
  546. return c.first * PRINTER_PART_POLYGONS.size() + c.second;
  547. }
  548. };
  549. TEST_CASE("Convex polygon intersection test prusa polygons", "[Geometry][Rotcalip]") {
  550. // Overlap of the same polygon should always be an intersection
  551. for (size_t i = 0; i < PRINTER_PART_POLYGONS.size(); ++i) {
  552. Polygon P = PRINTER_PART_POLYGONS[i];
  553. P = Geometry::convex_hull(P.points);
  554. bool res = Geometry::convex_polygons_intersect(P, P);
  555. if (!res) {
  556. SVG svg{std::string("fail_self") + std::to_string(i) + ".svg"};
  557. svg.draw(P, "green");
  558. svg.Close();
  559. }
  560. REQUIRE(res == true);
  561. }
  562. std::unordered_set<Pair> combos;
  563. for (size_t i = 0; i < PRINTER_PART_POLYGONS.size(); ++i) {
  564. for (size_t j = 0; j < PRINTER_PART_POLYGONS.size(); ++j) {
  565. if (i != j) {
  566. size_t a = std::min(i, j), b = std::max(i, j);
  567. combos.insert(Pair{a, b});
  568. }
  569. }
  570. }
  571. // All disjoint
  572. for (const auto &combo : combos) {
  573. Polygon A = PRINTER_PART_POLYGONS[combo.first], B = PRINTER_PART_POLYGONS[combo.second];
  574. A = Geometry::convex_hull(A.points);
  575. B = Geometry::convex_hull(B.points);
  576. auto bba = A.bounding_box();
  577. auto bbb = B.bounding_box();
  578. A.translate(-bba.center());
  579. B.translate(-bbb.center());
  580. B.translate(bba.size() + bbb.size());
  581. bool res = Geometry::convex_polygons_intersect(A, B);
  582. bool ref = !intersection(A, B).empty();
  583. if (res != ref) {
  584. SVG svg{std::string("fail") + std::to_string(combo.first) + "_" + std::to_string(combo.second) + ".svg"};
  585. svg.draw(A, "blue");
  586. svg.draw(B, "green");
  587. svg.Close();
  588. }
  589. REQUIRE(res == ref);
  590. }
  591. // All intersecting
  592. for (const auto &combo : combos) {
  593. Polygon A = PRINTER_PART_POLYGONS[combo.first], B = PRINTER_PART_POLYGONS[combo.second];
  594. A = Geometry::convex_hull(A.points);
  595. B = Geometry::convex_hull(B.points);
  596. auto bba = A.bounding_box();
  597. auto bbb = B.bounding_box();
  598. A.translate(-bba.center());
  599. B.translate(-bbb.center());
  600. bool res = Geometry::convex_polygons_intersect(A, B);
  601. bool ref = !intersection(A, B).empty();
  602. if (res != ref) {
  603. SVG svg{std::string("fail") + std::to_string(combo.first) + "_" + std::to_string(combo.second) + ".svg"};
  604. svg.draw(A, "blue");
  605. svg.draw(B, "green");
  606. svg.Close();
  607. }
  608. REQUIRE(res == ref);
  609. }
  610. }