test_polygon.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include <catch2/catch.hpp>
  2. #include "libslic3r/Point.hpp"
  3. #include "libslic3r/Polygon.hpp"
  4. using namespace Slic3r;
  5. SCENARIO("Converted Perl tests", "[Polygon]") {
  6. GIVEN("ccw_square") {
  7. Polygon ccw_square{ { 100, 100 }, { 200, 100 }, { 200, 200 }, { 100, 200 } };
  8. Polygon cw_square(ccw_square);
  9. cw_square.reverse();
  10. THEN("ccw_square is valid") {
  11. REQUIRE(ccw_square.is_valid());
  12. }
  13. THEN("cw_square is valid") {
  14. REQUIRE(cw_square.is_valid());
  15. }
  16. THEN("ccw_square.area") {
  17. REQUIRE(ccw_square.area() == 100 * 100);
  18. }
  19. THEN("cw_square.area") {
  20. REQUIRE(cw_square.area() == - 100 * 100);
  21. }
  22. THEN("ccw_square.centroid") {
  23. REQUIRE(ccw_square.centroid() == Point { 150, 150 });
  24. }
  25. THEN("cw_square.centroid") {
  26. REQUIRE(cw_square.centroid() == Point { 150, 150 });
  27. }
  28. THEN("ccw_square.contains_point(150, 150)") {
  29. REQUIRE(ccw_square.contains({ 150, 150 }));
  30. }
  31. THEN("cw_square.contains_point(150, 150)") {
  32. REQUIRE(cw_square.contains({ 150, 150 }));
  33. }
  34. THEN("conversion to lines") {
  35. REQUIRE(ccw_square.lines() == Lines{
  36. { { 100, 100 }, { 200, 100 } },
  37. { { 200, 100 }, { 200, 200 } },
  38. { { 200, 200 }, { 100, 200 } },
  39. { { 100, 200 }, { 100, 100 } } });
  40. }
  41. THEN("split_at_first_point") {
  42. REQUIRE(ccw_square.split_at_first_point() == Polyline { ccw_square[0], ccw_square[1], ccw_square[2], ccw_square[3], ccw_square[0] });
  43. }
  44. THEN("split_at_index(2)") {
  45. REQUIRE(ccw_square.split_at_index(2) == Polyline { ccw_square[2], ccw_square[3], ccw_square[0], ccw_square[1], ccw_square[2] });
  46. }
  47. THEN("split_at_vertex(ccw_square[2])") {
  48. REQUIRE(ccw_square.split_at_vertex(ccw_square[2]) == Polyline { ccw_square[2], ccw_square[3], ccw_square[0], ccw_square[1], ccw_square[2] });
  49. }
  50. THEN("is_counter_clockwise") {
  51. REQUIRE(ccw_square.is_counter_clockwise());
  52. }
  53. THEN("! is_counter_clockwise") {
  54. REQUIRE(! cw_square.is_counter_clockwise());
  55. }
  56. THEN("make_counter_clockwise") {
  57. cw_square.make_counter_clockwise();
  58. REQUIRE(cw_square.is_counter_clockwise());
  59. }
  60. THEN("make_counter_clockwise^2") {
  61. cw_square.make_counter_clockwise();
  62. cw_square.make_counter_clockwise();
  63. REQUIRE(cw_square.is_counter_clockwise());
  64. }
  65. THEN("first_point") {
  66. REQUIRE(&ccw_square.first_point() == &ccw_square.points.front());
  67. }
  68. }
  69. GIVEN("Triangulating hexagon") {
  70. Polygon hexagon{ { 100, 0 } };
  71. for (size_t i = 1; i < 6; ++ i) {
  72. Point p = hexagon.points.front();
  73. p.rotate(PI / 3 * i);
  74. hexagon.points.emplace_back(p);
  75. }
  76. Polygons triangles;
  77. hexagon.triangulate_convex(&triangles);
  78. THEN("right number of triangles") {
  79. REQUIRE(triangles.size() == 4);
  80. }
  81. THEN("all triangles are ccw") {
  82. auto it = std::find_if(triangles.begin(), triangles.end(), [](const Polygon &tri) { return tri.is_clockwise(); });
  83. REQUIRE(it == triangles.end());
  84. }
  85. }
  86. GIVEN("General triangle") {
  87. Polygon polygon { { 50000000, 100000000 }, { 300000000, 102000000 }, { 50000000, 104000000 } };
  88. Line line { { 175992032, 102000000 }, { 47983964, 102000000 } };
  89. Point intersection;
  90. bool has_intersection = polygon.intersection(line, &intersection);
  91. THEN("Intersection with line") {
  92. REQUIRE(has_intersection);
  93. REQUIRE(intersection == Point { 50000000, 102000000 });
  94. }
  95. }
  96. }
  97. TEST_CASE("Centroid of Trapezoid must be inside", "[Polygon][Utils]")
  98. {
  99. Slic3r::Polygon trapezoid {
  100. { 4702134, 1124765853 },
  101. { -4702134, 1124765853 },
  102. { -9404268, 1049531706 },
  103. { 9404268, 1049531706 },
  104. };
  105. Point centroid = trapezoid.centroid();
  106. CHECK(trapezoid.contains(centroid));
  107. }
  108. // This test currently only covers remove_collinear_points.
  109. // All remaining tests are to be ported from xs/t/06_polygon.t
  110. Slic3r::Points collinear_circle({
  111. Slic3r::Point::new_scale(0, 0), // 3 collinear points at beginning
  112. Slic3r::Point::new_scale(10, 0),
  113. Slic3r::Point::new_scale(20, 0),
  114. Slic3r::Point::new_scale(30, 10),
  115. Slic3r::Point::new_scale(40, 20), // 2 collinear points
  116. Slic3r::Point::new_scale(40, 30),
  117. Slic3r::Point::new_scale(30, 40), // 3 collinear points
  118. Slic3r::Point::new_scale(20, 40),
  119. Slic3r::Point::new_scale(10, 40),
  120. Slic3r::Point::new_scale(-10, 20),
  121. Slic3r::Point::new_scale(-20, 10),
  122. Slic3r::Point::new_scale(-20, 0), // 3 collinear points at end
  123. Slic3r::Point::new_scale(-10, 0),
  124. Slic3r::Point::new_scale(-5, 0)
  125. });
  126. SCENARIO("Remove collinear points from Polygon", "[Polygon]") {
  127. GIVEN("Polygon with collinear points"){
  128. Slic3r::Polygon p(collinear_circle);
  129. WHEN("collinear points are removed") {
  130. remove_collinear(p);
  131. THEN("Leading collinear points are removed") {
  132. REQUIRE(p.points.front() == Slic3r::Point::new_scale(20, 0));
  133. }
  134. THEN("Trailing collinear points are removed") {
  135. REQUIRE(p.points.back() == Slic3r::Point::new_scale(-20, 0));
  136. }
  137. THEN("Number of remaining points is correct") {
  138. REQUIRE(p.points.size() == 7);
  139. }
  140. }
  141. }
  142. }