test_geometry.cpp 34 KB

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