test_gcode_travels.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include <catch2/catch.hpp>
  2. #include <libslic3r/GCode/Travels.hpp>
  3. #include <libslic3r/ExPolygon.hpp>
  4. #include <boost/math/special_functions/pow.hpp>
  5. using namespace Slic3r;
  6. using namespace Slic3r::GCode::Impl::Travels;
  7. struct ApproxEqualsPoints : public Catch::MatcherBase<Points> {
  8. ApproxEqualsPoints(const Points& expected, unsigned tolerance): expected(expected), tolerance(tolerance) {}
  9. bool match(const Points& points) const override {
  10. if (points.size() != expected.size()) {
  11. return false;
  12. }
  13. for (auto i = 0u; i < points.size(); ++i) {
  14. const Point& point = points[i];
  15. const Point& expected_point = this->expected[i];
  16. if (
  17. std::abs(point.x() - expected_point.x()) > int(this->tolerance)
  18. || std::abs(point.y() - expected_point.y()) > int(this->tolerance)
  19. ) {
  20. return false;
  21. }
  22. }
  23. return true;
  24. }
  25. std::string describe() const override {
  26. std::stringstream ss;
  27. ss << std::endl;
  28. for (const Point& point : expected) {
  29. ss << "(" << point.x() << ", " << point.y() << ")" << std::endl;
  30. }
  31. ss << "With tolerance: " << this->tolerance;
  32. return "Equals " + ss.str();
  33. }
  34. private:
  35. Points expected;
  36. unsigned tolerance;
  37. };
  38. Points get_points(const std::vector<DistancedPoint>& result) {
  39. Points result_points;
  40. std::transform(
  41. result.begin(),
  42. result.end(),
  43. std::back_inserter(result_points),
  44. [](const DistancedPoint& point){
  45. return point.point;
  46. }
  47. );
  48. return result_points;
  49. }
  50. std::vector<double> get_distances(const std::vector<DistancedPoint>& result) {
  51. std::vector<double> result_distances;
  52. std::transform(
  53. result.begin(),
  54. result.end(),
  55. std::back_inserter(result_distances),
  56. [](const DistancedPoint& point){
  57. return point.distance_from_start;
  58. }
  59. );
  60. return result_distances;
  61. }
  62. TEST_CASE("Place points at distances - expected use", "[GCode]") {
  63. std::vector<Point> line{
  64. scaled(Vec2f{0, 0}),
  65. scaled(Vec2f{1, 0}),
  66. scaled(Vec2f{2, 1}),
  67. scaled(Vec2f{2, 2})
  68. };
  69. std::vector<double> distances{0, 0.2, 0.5, 1 + std::sqrt(2)/2, 1 + std::sqrt(2) + 0.5, 100.0};
  70. std::vector<DistancedPoint> result = slice_xy_path(line, distances);
  71. REQUIRE_THAT(get_points(result), ApproxEqualsPoints(Points{
  72. scaled(Vec2f{0, 0}),
  73. scaled(Vec2f{0.2, 0}),
  74. scaled(Vec2f{0.5, 0}),
  75. scaled(Vec2f{1, 0}),
  76. scaled(Vec2f{1.5, 0.5}),
  77. scaled(Vec2f{2, 1}),
  78. scaled(Vec2f{2, 1.5}),
  79. scaled(Vec2f{2, 2})
  80. }, 5));
  81. REQUIRE_THAT(get_distances(result), Catch::Matchers::Approx(std::vector<double>{
  82. distances[0], distances[1], distances[2], 1, distances[3], 1 + std::sqrt(2), distances[4], 2 + std::sqrt(2)
  83. }));
  84. }
  85. TEST_CASE("Place points at distances - edge case", "[GCode]") {
  86. std::vector<Point> line{
  87. scaled(Vec2f{0, 0}),
  88. scaled(Vec2f{1, 0}),
  89. scaled(Vec2f{2, 0})
  90. };
  91. std::vector<double> distances{0, 1, 1.5, 2};
  92. Points result{get_points(slice_xy_path(line, distances))};
  93. CHECK(result == Points{
  94. scaled(Vec2f{0, 0}),
  95. scaled(Vec2f{1, 0}),
  96. scaled(Vec2f{1.5, 0}),
  97. scaled(Vec2f{2, 0})
  98. });
  99. }
  100. TEST_CASE("Generate elevated travel", "[GCode]") {
  101. std::vector<Point> xy_path{
  102. scaled(Vec2f{0, 0}),
  103. scaled(Vec2f{1, 0}),
  104. };
  105. std::vector<double> ensure_points_at_distances{0.2, 0.5};
  106. Points3 result{generate_elevated_travel(xy_path, ensure_points_at_distances, 2.0, [](double x){return 1 + x;})};
  107. CHECK(result == Points3{
  108. scaled(Vec3f{ 0.f, 0.f, 3.f}),
  109. scaled(Vec3f{0.2f, 0.f, 3.2f}),
  110. scaled(Vec3f{0.5f, 0.f, 3.5f}),
  111. scaled(Vec3f{ 1.f, 0.f, 4.f})
  112. });
  113. }
  114. TEST_CASE("Get first crossed line distance", "[GCode]") {
  115. // A 2x2 square at 0, 0, with 1x1 square hole in its center.
  116. ExPolygon square_with_hole{
  117. {
  118. scaled(Vec2f{-1, -1}),
  119. scaled(Vec2f{1, -1}),
  120. scaled(Vec2f{1, 1}),
  121. scaled(Vec2f{-1, 1})
  122. },
  123. {
  124. scaled(Vec2f{-0.5, -0.5}),
  125. scaled(Vec2f{0.5, -0.5}),
  126. scaled(Vec2f{0.5, 0.5}),
  127. scaled(Vec2f{-0.5, 0.5})
  128. }
  129. };
  130. // A 2x2 square above the previous square at (0, 3).
  131. ExPolygon square_above{
  132. {
  133. scaled(Vec2f{-1, 2}),
  134. scaled(Vec2f{1, 2}),
  135. scaled(Vec2f{1, 4}),
  136. scaled(Vec2f{-1, 4})
  137. }
  138. };
  139. // Bottom-up travel intersecting the squares.
  140. Lines travel{Polyline{
  141. scaled(Vec2f{0, -2}),
  142. scaled(Vec2f{0, -0.7}),
  143. scaled(Vec2f{0, 0}),
  144. scaled(Vec2f{0, 1}),
  145. scaled(Vec2f{0, 1.3}),
  146. scaled(Vec2f{0, 2.4}),
  147. scaled(Vec2f{0, 4.5}),
  148. scaled(Vec2f{0, 5}),
  149. }.lines()};
  150. std::vector<Linef> lines;
  151. for (const ExPolygon& polygon : {square_with_hole, square_above}) {
  152. for (const Line& line : polygon.lines()) {
  153. lines.emplace_back(unscale(line.a), unscale(line.b));
  154. }
  155. }
  156. // Try different cases by skipping lines in the travel.
  157. AABBTreeLines::LinesDistancer<Linef> distancer{std::move(lines)};
  158. CHECK(get_first_crossed_line_distance(travel, distancer) == Approx(1));
  159. CHECK(get_first_crossed_line_distance(tcb::span{travel}.subspan(1), distancer) == Approx(0.2));
  160. CHECK(get_first_crossed_line_distance(tcb::span{travel}.subspan(2), distancer) == Approx(0.5));
  161. CHECK(get_first_crossed_line_distance(tcb::span{travel}.subspan(3), distancer) == Approx(1.0)); //Edge case
  162. CHECK(get_first_crossed_line_distance(tcb::span{travel}.subspan(4), distancer) == Approx(0.7));
  163. CHECK(get_first_crossed_line_distance(tcb::span{travel}.subspan(5), distancer) == Approx(1.6));
  164. CHECK(get_first_crossed_line_distance(tcb::span{travel}.subspan(6), distancer) == std::numeric_limits<double>::max());
  165. }
  166. TEST_CASE("Elevated travel formula", "[GCode]") {
  167. const double lift_height{10};
  168. const double slope_end{10};
  169. const double blend_width{10};
  170. const ElevatedTravelParams params{lift_height, slope_end, blend_width};
  171. ElevatedTravelFormula f{params};
  172. const double distance = slope_end - blend_width / 2;
  173. const double slope = (f(distance) - f(0)) / distance;
  174. // At the begining it has given slope.
  175. CHECK(slope == lift_height / slope_end);
  176. // At the end it is flat.
  177. CHECK(f(slope_end + blend_width / 2) == f(slope_end + blend_width));
  178. // Should be smoothed.
  179. CHECK(f(slope_end) < lift_height);
  180. }