test_geometry.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. #define CATCH_CONFIG_DISABLE
  2. #include <catch2/catch.hpp>
  3. #include <libslic3r/Point.hpp>
  4. #include <libslic3r/BoundingBox.hpp>
  5. #include <libslic3r/Polygon.hpp>
  6. #include <libslic3r/Polyline.hpp>
  7. #include <libslic3r/Line.hpp>
  8. #include <libslic3r/Geometry.hpp>
  9. #include <libslic3r/ClipperUtils.hpp>
  10. using namespace Slic3r;
  11. TEST_CASE("Polygon::contains works properly", ""){
  12. // this test was failing on Windows (GH #1950)
  13. Polygon polygon{ Points{
  14. Point{207802834,-57084522},
  15. Point{196528149,-37556190},
  16. Point{173626821,-25420928},
  17. Point{171285751,-21366123},
  18. Point{118673592,-21366123},
  19. Point{116332562,-25420928},
  20. Point{93431208,-37556191},
  21. Point{82156517,-57084523},
  22. Point{129714478,-84542120},
  23. Point{160244873,-84542120}
  24. } };
  25. Point point{ 95706562, -57294774 };
  26. REQUIRE(polygon.contains(point));
  27. }
  28. SCENARIO("Intersections of line segments"){
  29. GIVEN("Integer coordinates"){
  30. Line line1{ Point{5,15},Point{30,15} };
  31. Line line2{ Point{10,20}, Point{10,10} };
  32. THEN("The intersection is valid"){
  33. Point point;
  34. line1.intersection(line2,&point);
  35. REQUIRE(Point{ 10,15 } == point);
  36. }
  37. }
  38. GIVEN("Scaled coordinates"){
  39. Line line1{ Point{73.6310778185108 / 0.0000001, 371.74239268924 / 0.0000001}, Point{73.6310778185108 / 0.0000001, 501.74239268924 / 0.0000001} };
  40. Line line2{ Point{75 / 0.0000001, 437.9853 / 0.0000001}, Point{62.7484 / 0.0000001, 440.4223 / 0.0000001} };
  41. THEN("There is still an intersection"){
  42. Point point;
  43. REQUIRE(line1.intersection(line2,&point));
  44. }
  45. }
  46. }
  47. /*
  48. Tests for unused methods still written in perl
  49. {
  50. my $polygon = Slic3r::Polygon->new(
  51. [45919000, 515273900], [14726100, 461246400], [14726100, 348753500], [33988700, 315389800],
  52. [43749700, 343843000], [45422300, 352251500], [52362100, 362637800], [62748400, 369577600],
  53. [75000000, 372014700], [87251500, 369577600], [97637800, 362637800], [104577600, 352251500],
  54. [107014700, 340000000], [104577600, 327748400], [97637800, 317362100], [87251500, 310422300],
  55. [82789200, 309534700], [69846100, 294726100], [254081000, 294726100], [285273900, 348753500],
  56. [285273900, 461246400], [254081000, 515273900],
  57. );
  58. # this points belongs to $polyline
  59. # note: it's actually a vertex, while we should better check an intermediate point
  60. my $point = Slic3r::Point->new(104577600, 327748400);
  61. local $Slic3r::Geometry::epsilon = 1E-5;
  62. is_deeply Slic3r::Geometry::polygon_segment_having_point($polygon, $point)->pp,
  63. [ [107014700, 340000000], [104577600, 327748400] ],
  64. 'polygon_segment_having_point';
  65. }
  66. {
  67. auto point = Point{736310778.185108, 5017423926.8924};
  68. auto line = Line(Point{(long int} 627484000, (long int) 3695776000), Point{(long int} 750000000, (long int)3720147000));
  69. //is Slic3r::Geometry::point_in_segment($point, $line), 0, 'point_in_segment';
  70. }
  71. // Possible to delete
  72. {
  73. //my $p1 = [10, 10];
  74. //my $p2 = [10, 20];
  75. //my $p3 = [10, 30];
  76. //my $p4 = [20, 20];
  77. //my $p5 = [0, 20];
  78. THEN("Points in a line give the correct angles"){
  79. //is Slic3r::Geometry::angle3points($p2, $p3, $p1), PI(), 'angle3points';
  80. //is Slic3r::Geometry::angle3points($p2, $p1, $p3), PI(), 'angle3points';
  81. }
  82. THEN("Left turns give the correct angle"){
  83. //is Slic3r::Geometry::angle3points($p2, $p4, $p3), PI()/2, 'angle3points';
  84. //is Slic3r::Geometry::angle3points($p2, $p1, $p4), PI()/2, 'angle3points';
  85. }
  86. THEN("Right turns give the correct angle"){
  87. //is Slic3r::Geometry::angle3points($p2, $p3, $p4), PI()/2*3, 'angle3points';
  88. //is Slic3r::Geometry::angle3points($p2, $p1, $p5), PI()/2*3, 'angle3points';
  89. }
  90. //my $p1 = [30, 30];
  91. //my $p2 = [20, 20];
  92. //my $p3 = [10, 10];
  93. //my $p4 = [30, 10];
  94. //is Slic3r::Geometry::angle3points($p2, $p1, $p3), PI(), 'angle3points';
  95. //is Slic3r::Geometry::angle3points($p2, $p1, $p4), PI()/2*3, 'angle3points';
  96. //is Slic3r::Geometry::angle3points($p2, $p1, $p1), 2*PI(), 'angle3points';
  97. }
  98. SCENARIO("polygon_is_convex works"){
  99. GIVEN("A square of dimension 10"){
  100. //my $cw_square = [ [0,0], [0,10], [10,10], [10,0] ];
  101. THEN("It is not convex clockwise"){
  102. //is polygon_is_convex($cw_square), 0, 'cw square is not convex';
  103. }
  104. THEN("It is convex counter-clockwise"){
  105. //is polygon_is_convex([ reverse @$cw_square ]), 1, 'ccw square is convex';
  106. }
  107. }
  108. GIVEN("A concave polygon"){
  109. //my $convex1 = [ [0,0], [10,0], [10,10], [0,10], [0,6], [4,6], [4,4], [0,4] ];
  110. THEN("It is concave"){
  111. //is polygon_is_convex($convex1), 0, 'concave polygon';
  112. }
  113. }
  114. }*/
  115. TEST_CASE("Creating a polyline generates the obvious lines"){
  116. auto polyline = Polyline();
  117. polyline.points = std::vector<Point>({Point{0, 0}, Point{10, 0}, Point{20, 0}});
  118. REQUIRE(polyline.lines().at(0).a == Point{0,0});
  119. REQUIRE(polyline.lines().at(0).b == Point{10,0});
  120. REQUIRE(polyline.lines().at(1).a == Point{10,0});
  121. REQUIRE(polyline.lines().at(1).b == Point{20,0});
  122. }
  123. TEST_CASE("Splitting a Polygon generates a polyline correctly"){
  124. auto polygon = Polygon(std::vector<Point>({Point{0, 0}, Point{10, 0}, Point{5, 5}}));
  125. auto split = polygon.split_at_index(1);
  126. REQUIRE(split.points[0]==Point{10,0});
  127. REQUIRE(split.points[1]==Point{5,5});
  128. REQUIRE(split.points[2]==Point{0,0});
  129. REQUIRE(split.points[3]==Point{10,0});
  130. }
  131. TEST_CASE("Bounding boxes are scaled appropriately"){
  132. auto bb = BoundingBox(std::vector<Point>({Point{0, 1}, Point{10, 2}, Point{20, 2}}));
  133. bb.scale(2);
  134. REQUIRE(bb.min == Point{0,2});
  135. REQUIRE(bb.max == Point{40,4});
  136. }
  137. TEST_CASE("Offseting a line generates a polygon correctly"){
  138. Polyline tmp({ Point{10,10}, Point{20,10} });
  139. Polygon area = offset(tmp,5).at(0);
  140. REQUIRE(area.area() == Polygon(std::vector<Point>({Point{10,5},Point{20,5},Point{20,15},Point{10,15}})).area());
  141. }
  142. SCENARIO("Circle Fit, TaubinFit with Newton's method") {
  143. GIVEN("A vector of Pointfs arranged in a half-circle with approximately the same distance R from some point") {
  144. Vec2d expected_center(-6, 0);
  145. Pointfs 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}};
  146. std::transform(sample.begin(), sample.end(), sample.begin(), [expected_center] (const Vec2d& a) { return a + expected_center;});
  147. WHEN("Circle fit is called on the entire array") {
  148. Vec2d result_center(0,0);
  149. result_center = Geometry::circle_taubin_newton(sample);
  150. THEN("A center point of -6,0 is returned.") {
  151. REQUIRE(result_center == expected_center);
  152. }
  153. }
  154. WHEN("Circle fit is called on the first four points") {
  155. Vec2d result_center(0,0);
  156. result_center = Geometry::circle_taubin_newton(sample.cbegin(), sample.cbegin()+4);
  157. THEN("A center point of -6,0 is returned.") {
  158. REQUIRE(result_center == expected_center);
  159. }
  160. }
  161. WHEN("Circle fit is called on the middle four points") {
  162. Vec2d result_center(0,0);
  163. result_center = Geometry::circle_taubin_newton(sample.cbegin()+2, sample.cbegin()+6);
  164. THEN("A center point of -6,0 is returned.") {
  165. REQUIRE(result_center == expected_center);
  166. }
  167. }
  168. }
  169. GIVEN("A vector of Pointfs arranged in a half-circle with approximately the same distance R from some point") {
  170. Vec2d expected_center(-3, 9);
  171. Pointfs sample {Vec2d{6.0, 0}, Vec2d{5.1961524, 3}, Vec2d{3 ,5.1961524},
  172. Vec2d{0, 6.0},
  173. Vec2d{3, 5.1961524}, Vec2d{-5.1961524, 3}, Vec2d{-6.0, 0}};
  174. std::transform(sample.begin(), sample.end(), sample.begin(), [expected_center] (const Vec2d& a) { return a + expected_center;});
  175. WHEN("Circle fit is called on the entire array") {
  176. Vec2d result_center(0,0);
  177. result_center = Geometry::circle_taubin_newton(sample);
  178. THEN("A center point of 3,9 is returned.") {
  179. REQUIRE(result_center == expected_center);
  180. }
  181. }
  182. WHEN("Circle fit is called on the first four points") {
  183. Vec2d result_center(0,0);
  184. result_center = Geometry::circle_taubin_newton(sample.cbegin(), sample.cbegin()+4);
  185. THEN("A center point of 3,9 is returned.") {
  186. REQUIRE(result_center == expected_center);
  187. }
  188. }
  189. WHEN("Circle fit is called on the middle four points") {
  190. Vec2d result_center(0,0);
  191. result_center = Geometry::circle_taubin_newton(sample.cbegin()+2, sample.cbegin()+6);
  192. THEN("A center point of 3,9 is returned.") {
  193. REQUIRE(result_center == expected_center);
  194. }
  195. }
  196. }
  197. GIVEN("A vector of Points arranged in a half-circle with approximately the same distance R from some point") {
  198. Point expected_center { Point::new_scale(-3, 9)};
  199. Points sample {Point::new_scale(6.0, 0), Point::new_scale(5.1961524, 3), Point::new_scale(3 ,5.1961524),
  200. Point::new_scale(0, 6.0),
  201. Point::new_scale(3, 5.1961524), Point::new_scale(-5.1961524, 3), Point::new_scale(-6.0, 0)};
  202. std::transform(sample.begin(), sample.end(), sample.begin(), [expected_center] (const Point& a) { return a + expected_center;});
  203. WHEN("Circle fit is called on the entire array") {
  204. Point result_center(0,0);
  205. result_center = Geometry::circle_taubin_newton(sample);
  206. THEN("A center point of scaled 3,9 is returned.") {
  207. REQUIRE(result_center.coincides_with_epsilon(expected_center));
  208. }
  209. }
  210. WHEN("Circle fit is called on the first four points") {
  211. Point result_center(0,0);
  212. result_center = Geometry::circle_taubin_newton(sample.cbegin(), sample.cbegin()+4);
  213. THEN("A center point of scaled 3,9 is returned.") {
  214. REQUIRE(result_center.coincides_with_epsilon(expected_center));
  215. }
  216. }
  217. WHEN("Circle fit is called on the middle four points") {
  218. Point result_center(0,0);
  219. result_center = Geometry::circle_taubin_newton(sample.cbegin()+2, sample.cbegin()+6);
  220. THEN("A center point of scaled 3,9 is returned.") {
  221. REQUIRE(result_center.coincides_with_epsilon(expected_center));
  222. }
  223. }
  224. }
  225. }
  226. // A PU
  227. //TEST_CASE("Chained path working correctly"){
  228. // // if chained_path() works correctly, these points should be joined with no diagonal paths
  229. // // (thus 26 units long)
  230. // 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}};
  231. // std::vector<Points::size_type> indices;
  232. // Geometry::chained_path(points,indices);
  233. // for(Points::size_type i = 0; i < indices.size()-1;i++){
  234. // double dist = points.at(indices.at(i)).distance_to(points.at(indices.at(i+1)));
  235. // REQUIRE(abs(dist-26) <= EPSILON);
  236. // }
  237. //}
  238. SCENARIO("Line distances"){
  239. GIVEN("A line"){
  240. Line line{ Point{0, 0}, Point{20, 0} };
  241. THEN("Points on the line segment have 0 distance"){
  242. REQUIRE(Point{0, 0}.distance_to(line) == 0);
  243. REQUIRE(Point{20, 0}.distance_to(line) == 0);
  244. REQUIRE(Point{10, 0}.distance_to(line) == 0);
  245. }
  246. THEN("Points off the line have the appropriate distance"){
  247. REQUIRE(Point{10, 10}.distance_to(line) == 10);
  248. REQUIRE(Point{50, 0}.distance_to(line) == 30);
  249. }
  250. }
  251. }
  252. SCENARIO("Polygon convex/concave detection"){
  253. GIVEN(("A Square with dimension 100")){
  254. Polygon square/*new_scale*/{ std::vector<Point>{
  255. Point{100,100},
  256. Point{200,100},
  257. Point{200,200},
  258. Point{100,200}}};
  259. THEN("It has 4 convex points counterclockwise"){
  260. REQUIRE(square.concave_points(PI*4/3).size() == 0);
  261. REQUIRE(square.convex_points(PI*2/3).size() == 4);
  262. }
  263. THEN("It has 4 concave points clockwise"){
  264. square.make_clockwise();
  265. REQUIRE(square.concave_points(PI*4/3).size() == 4);
  266. REQUIRE(square.convex_points(PI*2/3).size() == 0);
  267. }
  268. }
  269. GIVEN("A Square with an extra colinearvertex"){
  270. Polygon square /*new_scale*/{ std::vector<Point>{
  271. Point{150,100},
  272. Point{200,100},
  273. Point{200,200},
  274. Point{100,200},
  275. Point{100,100}} };
  276. THEN("It has 4 convex points counterclockwise"){
  277. REQUIRE(square.concave_points(PI*4/3).size() == 0);
  278. REQUIRE(square.convex_points(PI*2/3).size() == 4);
  279. }
  280. }
  281. GIVEN("A Square with an extra collinear vertex in different order"){
  282. Polygon square = Polygon /*new_scale*/{ std::vector<Point>{
  283. Point{200,200},
  284. Point{100,200},
  285. Point{100,100},
  286. Point{150,100},
  287. Point{200,100}} };
  288. THEN("It has 4 convex points counterclockwise"){
  289. REQUIRE(square.concave_points(PI*4/3).size() == 0);
  290. REQUIRE(square.convex_points(PI*2/3).size() == 4);
  291. }
  292. }
  293. GIVEN("A triangle"){
  294. Polygon triangle{ std::vector<Point>{
  295. Point{16000170,26257364},
  296. Point{714223,461012},
  297. Point{31286371,461008}
  298. } };
  299. THEN("it has three convex vertices"){
  300. REQUIRE(triangle.concave_points(PI*4/3).size() == 0);
  301. REQUIRE(triangle.convex_points(PI*2/3).size() == 3);
  302. }
  303. }
  304. GIVEN("A triangle with an extra collinear point"){
  305. Polygon triangle{ std::vector<Point>{
  306. Point{16000170,26257364},
  307. Point{714223,461012},
  308. Point{20000000,461012},
  309. Point{31286371,461012}
  310. } };
  311. THEN("it has three convex vertices"){
  312. REQUIRE(triangle.concave_points(PI*4/3).size() == 0);
  313. REQUIRE(triangle.convex_points(PI*2/3).size() == 3);
  314. }
  315. }
  316. GIVEN("A polygon with concave vertices with angles of specifically 4/3pi"){
  317. // Two concave vertices of this polygon have angle = PI*4/3, so this test fails
  318. // if epsilon is not used.
  319. Polygon polygon{ std::vector<Point>{
  320. Point{60246458,14802768},Point{64477191,12360001},
  321. Point{63727343,11060995},Point{64086449,10853608},
  322. Point{66393722,14850069},Point{66034704,15057334},
  323. Point{65284646,13758387},Point{61053864,16200839},
  324. Point{69200258,30310849},Point{62172547,42483120},
  325. Point{61137680,41850279},Point{67799985,30310848},
  326. Point{51399866,1905506},Point{38092663,1905506},
  327. Point{38092663,692699},Point{52100125,692699}
  328. } };
  329. THEN("the correct number of points are detected"){
  330. REQUIRE(polygon.concave_points(PI*4/3).size() == 6);
  331. REQUIRE(polygon.convex_points(PI*2/3).size() == 10);
  332. }
  333. }
  334. }
  335. TEST_CASE("Triangle Simplification does not result in less than 3 points"){
  336. Polygon triangle{ std::vector<Point>{
  337. Point{16000170,26257364}, Point{714223,461012}, Point{31286371,461008}
  338. } };
  339. REQUIRE(triangle.simplify(250000).at(0).points.size() == 3);
  340. }