test_polyline.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * Ported from xs/t/09_polyline.t
  3. */
  4. #include <catch2/catch.hpp>
  5. #include "libslic3r/Point.hpp"
  6. #include "libslic3r/Polyline.hpp"
  7. using namespace Slic3r;
  8. struct PolylineTestCase {
  9. Polyline polyline{
  10. {100, 100},
  11. {200, 100},
  12. {200, 200}
  13. };
  14. };
  15. TEST_CASE_METHOD(PolylineTestCase, "Lines can be retrieved", "[Polyline]") {
  16. CHECK(polyline.lines() == Lines{
  17. {{100, 100}, {200, 100}},
  18. {{200, 100}, {200, 200}},
  19. });
  20. }
  21. TEST_CASE_METHOD(PolylineTestCase, "Clip", "[Polyline]") {
  22. const double len = polyline.length();
  23. polyline.clip_end(len/3);
  24. CHECK(std::abs(polyline.length() - 2.0/3.0*len) < 1);
  25. }
  26. TEST_CASE_METHOD(PolylineTestCase, "Append", "[Polyline]") {
  27. Polyline tested_polyline{polyline};
  28. tested_polyline.append(tested_polyline);
  29. Points expected{polyline.points};
  30. expected.insert(expected.end(), polyline.points.begin(), polyline.points.end());
  31. CHECK(tested_polyline.points == expected);
  32. }
  33. TEST_CASE_METHOD(PolylineTestCase, "Extend end", "[Polyline]") {
  34. CHECK(polyline.length() == 100*2);
  35. polyline.extend_end(50);
  36. CHECK(polyline.length() == 100*2 + 50);
  37. }
  38. TEST_CASE_METHOD(PolylineTestCase, "Extend start", "[Polyline]") {
  39. CHECK(polyline.length() == 100*2);
  40. polyline.extend_start(50);
  41. CHECK(polyline.length() == 100*2 + 50);
  42. }
  43. TEST_CASE_METHOD(PolylineTestCase, "Split", "[Polyline]") {
  44. Polyline p1;
  45. Polyline p2;
  46. const Point point{150, 100};
  47. polyline.split_at(point, &p1, &p2);
  48. CHECK(p1.size() == 2);
  49. CHECK(p2.size() == 3);
  50. CHECK(p1.last_point() == point);
  51. CHECK(p2.first_point() == point);
  52. }
  53. TEST_CASE_METHOD(PolylineTestCase, "Split at first point", "[Polyline]") {
  54. Polyline to_split{
  55. polyline.points[0],
  56. polyline.points[1],
  57. polyline.points[2],
  58. polyline.points[0]
  59. };
  60. Polyline p1;
  61. Polyline p2;
  62. to_split.split_at(to_split.first_point(), &p1, &p2);
  63. CHECK(p1.size() == 1);
  64. CHECK(p2.size() == 4);
  65. }
  66. SCENARIO("Simplify polyne, template", "[Polyline]")
  67. {
  68. Points polyline{ {0,0}, {1000,0}, {2000,0}, {2000,1000}, {2000,2000}, {1000,2000}, {0,2000}, {0,1000}, {0,0} };
  69. WHEN("simplified with Douglas-Peucker with back inserter") {
  70. Points out;
  71. douglas_peucker<int64_t>(polyline.begin(), polyline.end(), std::back_inserter(out), 10, [](const Point &p) { return p; });
  72. THEN("simplified correctly") {
  73. REQUIRE(out == Points{ {0,0}, {2000,0}, {2000,2000}, {0,2000}, {0,0} });
  74. }
  75. }
  76. WHEN("simplified with Douglas-Peucker in place") {
  77. Points out{ polyline };
  78. out.erase(douglas_peucker<int64_t>(out.begin(), out.end(), out.begin(), 10, [](const Point &p) { return p; }), out.end());
  79. THEN("simplified correctly") {
  80. REQUIRE(out == Points{ {0,0}, {2000,0}, {2000,2000}, {0,2000}, {0,0} });
  81. }
  82. }
  83. }
  84. SCENARIO("Simplify polyline", "[Polyline]")
  85. {
  86. GIVEN("polyline 1") {
  87. auto polyline = Polyline{ {0,0},{1,0},{2,0},{2,1},{2,2},{1,2},{0,2},{0,1},{0,0} };
  88. WHEN("simplified with Douglas-Peucker") {
  89. polyline.simplify(1.);
  90. THEN("simplified correctly") {
  91. REQUIRE(polyline == Polyline{ {0,0}, {2,0}, {2,2}, {0,2}, {0,0} });
  92. }
  93. }
  94. }
  95. GIVEN("polyline 2") {
  96. auto polyline = Polyline{ {0,0}, {50,50}, {100,0}, {125,-25}, {150,50} };
  97. WHEN("simplified with Douglas-Peucker") {
  98. polyline.simplify(25.);
  99. THEN("not simplified") {
  100. REQUIRE(polyline == Polyline{ {0,0}, {50,50}, {125,-25}, {150,50} });
  101. }
  102. }
  103. }
  104. GIVEN("polyline 3") {
  105. auto polyline = Polyline{ {0,0}, {100,0}, {50,10} };
  106. WHEN("simplified with Douglas-Peucker") {
  107. polyline.simplify(25.);
  108. THEN("not simplified") {
  109. REQUIRE(polyline == Polyline{ {0,0}, {100, 0}, {50,10} });
  110. }
  111. }
  112. }
  113. GIVEN("polyline 4") {
  114. auto polyline = Polyline{ {0,0}, {20,0}, {50,0}, {80,0}, {100,0} };
  115. WHEN("simplified with Douglas-Peucker") {
  116. polyline.simplify(2.);
  117. THEN("not simplified") {
  118. REQUIRE(polyline == Polyline{ {0,0}, {100,0} });
  119. }
  120. }
  121. }
  122. }