test_polyline.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include <catch2/catch.hpp>
  2. #include "libslic3r/Point.hpp"
  3. #include "libslic3r/Polyline.hpp"
  4. using namespace Slic3r;
  5. SCENARIO("Simplify polyne, template", "[Polyline]")
  6. {
  7. Points polyline{ {0,0}, {1000,0}, {2000,0}, {2000,1000}, {2000,2000}, {1000,2000}, {0,2000}, {0,1000}, {0,0} };
  8. WHEN("simplified with Douglas-Peucker with back inserter") {
  9. Points out;
  10. douglas_peucker<int64_t>(polyline.begin(), polyline.end(), std::back_inserter(out), 10, [](const Point &p) { return p; });
  11. THEN("simplified correctly") {
  12. REQUIRE(out == Points{ {0,0}, {2000,0}, {2000,2000}, {0,2000}, {0,0} });
  13. }
  14. }
  15. WHEN("simplified with Douglas-Peucker in place") {
  16. Points out{ polyline };
  17. out.erase(douglas_peucker<int64_t>(out.begin(), out.end(), out.begin(), 10, [](const Point &p) { return p; }), out.end());
  18. THEN("simplified correctly") {
  19. REQUIRE(out == Points{ {0,0}, {2000,0}, {2000,2000}, {0,2000}, {0,0} });
  20. }
  21. }
  22. }
  23. SCENARIO("Simplify polyline", "[Polyline]")
  24. {
  25. GIVEN("polyline 1") {
  26. auto polyline = Polyline{ {0,0},{1,0},{2,0},{2,1},{2,2},{1,2},{0,2},{0,1},{0,0} };
  27. WHEN("simplified with Douglas-Peucker") {
  28. polyline.simplify(1.);
  29. THEN("simplified correctly") {
  30. REQUIRE(polyline == Polyline{ {0,0}, {2,0}, {2,2}, {0,2}, {0,0} });
  31. }
  32. }
  33. }
  34. GIVEN("polyline 2") {
  35. auto polyline = Polyline{ {0,0}, {50,50}, {100,0}, {125,-25}, {150,50} };
  36. WHEN("simplified with Douglas-Peucker") {
  37. polyline.simplify(25.);
  38. THEN("not simplified") {
  39. REQUIRE(polyline == Polyline{ {0,0}, {50,50}, {125,-25}, {150,50} });
  40. }
  41. }
  42. }
  43. }