diff --git a/docs/HLSD/multiline-infill.md b/docs/HLSD/multiline-infill.md index b3b46d0fd7..fc255d1209 100644 --- a/docs/HLSD/multiline-infill.md +++ b/docs/HLSD/multiline-infill.md @@ -9,7 +9,8 @@ spacing (so the density holds), and `multiline_fill()` then replaces each centerline by the lines of that wall: the centerline itself when N is odd, and closed outlines around it at every `spacing` out to `d1 / 2`. The outlines are clipped to the fill region contracted by half a line width, then connected like -any other infill. +any other infill. Clipping also cuts an outline at its first point, which may be +anywhere along a line, so `join_cut_outlines()` joins the pieces back first. Outlines of centerlines that cross each other overlap at every crossing, which over-extrudes the wall intersections. The line-crossing patterns Grid, diff --git a/src/libslic3r/Fill/FillAdaptive.cpp b/src/libslic3r/Fill/FillAdaptive.cpp index b693ae7cfa..ddfe54665c 100644 --- a/src/libslic3r/Fill/FillAdaptive.cpp +++ b/src/libslic3r/Fill/FillAdaptive.cpp @@ -1910,6 +1910,7 @@ void Filler::_fill_surface_single( // Crop all polylines all_polylines = intersection_pl(std::move(all_polylines), expolygon); + join_cut_outlines(all_polylines, params); #endif } diff --git a/src/libslic3r/Fill/FillBase.cpp b/src/libslic3r/Fill/FillBase.cpp index ce35b145d2..1d68f0b70e 100644 --- a/src/libslic3r/Fill/FillBase.cpp +++ b/src/libslic3r/Fill/FillBase.cpp @@ -3152,4 +3152,10 @@ void join_close_ends(Polylines &polylines, double max_gap, bool can_reverse) polylines = std::move(out); } +void join_cut_outlines(Polylines &polylines, const FillParams ¶ms) +{ + if (params.multiline > 1) + join_close_ends(polylines, SCALED_EPSILON, true); +} + } // namespace Slic3r diff --git a/src/libslic3r/Fill/FillBase.hpp b/src/libslic3r/Fill/FillBase.hpp index 4711192999..3b124d60f1 100644 --- a/src/libslic3r/Fill/FillBase.hpp +++ b/src/libslic3r/Fill/FillBase.hpp @@ -266,6 +266,8 @@ public: // Joins polylines whose ends are at most max_gap apart, shortest gaps first and without closing a loop, so each such // group prints as one path. Without can_reverse only the end of one polyline joins the start of another. void join_close_ends(Polylines &polylines, double max_gap, bool can_reverse); + // Clipping also cuts a closed outline of multiline_fill() at its first point, anywhere along a line. Joins those pieces back. + void join_cut_outlines(Polylines &polylines, const FillParams ¶ms); } // namespace Slic3r #endif // slic3r_FillBase_hpp_ diff --git a/src/libslic3r/Fill/FillCrossHatch.cpp b/src/libslic3r/Fill/FillCrossHatch.cpp index a77bbaf021..05556b1fb6 100644 --- a/src/libslic3r/Fill/FillCrossHatch.cpp +++ b/src/libslic3r/Fill/FillCrossHatch.cpp @@ -220,6 +220,7 @@ void FillCrossHatch ::_fill_surface_single( multiline_fill(polylines, params, spacing); polylines = intersection_pl(std::move(polylines), to_polygons(expolygon)); + join_cut_outlines(polylines, params); // --- remove small remains from gyroid infill if (!polylines.empty()) { diff --git a/src/libslic3r/Fill/FillHoneycomb.cpp b/src/libslic3r/Fill/FillHoneycomb.cpp index 3d2c4ae62c..f33edd21cf 100644 --- a/src/libslic3r/Fill/FillHoneycomb.cpp +++ b/src/libslic3r/Fill/FillHoneycomb.cpp @@ -89,6 +89,7 @@ void FillHoneycomb::_fill_surface_single( multiline_fill(all_polylines, params, spacing); all_polylines = intersection_pl(std::move(all_polylines), expolygon); + join_cut_outlines(all_polylines, params); chain_or_connect_infill(std::move(all_polylines), expolygon, polylines_out, this->spacing, params); } diff --git a/src/libslic3r/Fill/FillLightning.cpp b/src/libslic3r/Fill/FillLightning.cpp index 6bd5172db8..5e211dfbc9 100644 --- a/src/libslic3r/Fill/FillLightning.cpp +++ b/src/libslic3r/Fill/FillLightning.cpp @@ -42,6 +42,7 @@ void Filler::_fill_surface_single( multiline_fill(fill_lines, params, spacing); fill_lines = Slic3r::intersection_pl(std::move(fill_lines), expolygon); + join_cut_outlines(fill_lines, params); chain_or_connect_infill(std::move(fill_lines), expolygon, polylines_out, this->spacing, params); } diff --git a/src/libslic3r/Fill/FillPlanePath.cpp b/src/libslic3r/Fill/FillPlanePath.cpp index 5ae254f8e6..eb66530903 100644 --- a/src/libslic3r/Fill/FillPlanePath.cpp +++ b/src/libslic3r/Fill/FillPlanePath.cpp @@ -146,6 +146,7 @@ void FillPlanePath::_fill_surface_single( if (polyline.size() >= 2) { polylines = intersection_pl(std::move(polylines), expolygon); + join_cut_outlines(polylines, params); if (!polylines.empty()) { Polylines chained; if (params.dont_connect() || params.density > 0.5) { diff --git a/src/libslic3r/Fill/FillRectilinear.cpp b/src/libslic3r/Fill/FillRectilinear.cpp index 142afbae1a..3d020cbd48 100644 --- a/src/libslic3r/Fill/FillRectilinear.cpp +++ b/src/libslic3r/Fill/FillRectilinear.cpp @@ -3040,6 +3040,9 @@ bool FillRectilinear::fill_surface_by_multilines(const Surface *surface, FillPar if ((params.pattern == ipLateralLattice || params.pattern == ipLateralHoneycomb) && params.multiline > 1) remove_overlapped(fill_lines, line_width); + // After remove_overlapped(), which would not recognize a line joined with its duplicate. + join_cut_outlines(fill_lines, params); + if (!fill_lines.empty()) { if (params.dont_connect()) { if (fill_lines.size() > 1) diff --git a/tests/fff_print/test_fill.cpp b/tests/fff_print/test_fill.cpp index d2fb49143c..3278289b6b 100644 --- a/tests/fff_print/test_fill.cpp +++ b/tests/fff_print/test_fill.cpp @@ -2393,38 +2393,75 @@ TEST_CASE("Adaptive TPMS infill of a region matches the infill of a larger regio } } -TEST_CASE("TPMS infill lines do not end where the rings of the pattern start", "[Fill][Regression]") +TEST_CASE("Infill lines do not end where the rings of the pattern start", "[Fill][Regression]") { - // Marching squares patterns are closed rings, starting anywhere along their lines. - const auto [pattern, mode, optimized] = GENERATE(table( - {{ipGyroid, AdaptiveInfillMode::DistanceWarp, false}, - {ipTpmsD, AdaptiveInfillMode::DistanceWarp, false}, - {ipTpmsFK, AdaptiveInfillMode::DistanceWarp, false}, - {ipTpmsFK, AdaptiveInfillMode::Disabled, false}, - {ipGyroid, AdaptiveInfillMode::Disabled, true}})); - const int multiline = GENERATE(1, 2); - CAPTURE(pattern, mode, optimized, multiline); + // Marching squares patterns and multiline outlines are closed rings, starting anywhere along their lines. + const auto [pattern, mode, optimized, first_multiline] = GENERATE(table( + {{ipGyroid, AdaptiveInfillMode::DistanceWarp, false, 1}, + {ipTpmsD, AdaptiveInfillMode::DistanceWarp, false, 1}, + {ipTpmsFK, AdaptiveInfillMode::DistanceWarp, false, 1}, + {ipTpmsFK, AdaptiveInfillMode::Disabled, false, 1}, + {ipGyroid, AdaptiveInfillMode::Disabled, true, 1}, + {ipGyroid, AdaptiveInfillMode::Disabled, false, 2}, + {ipTpmsD, AdaptiveInfillMode::Disabled, false, 2}, + {ipHoneycomb, AdaptiveInfillMode::Disabled, false, 2}, + {ipQuarterCubic, AdaptiveInfillMode::Disabled, false, 2}, + {ipLateralLattice, AdaptiveInfillMode::Disabled, false, 2}, + {ipLateralHoneycomb, AdaptiveInfillMode::Disabled, false, 2}, + {ipRectilinear, AdaptiveInfillMode::Disabled, false, 2}, + {ipAlignedRectilinear, AdaptiveInfillMode::Disabled, false, 2}, + {ipGrid, AdaptiveInfillMode::Disabled, false, 2}, + {ipTriangles, AdaptiveInfillMode::Disabled, false, 2}, + {ipStars, AdaptiveInfillMode::Disabled, false, 2}, + {ipCubic, AdaptiveInfillMode::Disabled, false, 2}, + {ipCrossHatch, AdaptiveInfillMode::Disabled, false, 2}, + {ipHilbertCurve, AdaptiveInfillMode::Disabled, false, 2}})); + const int multiline = GENERATE(1, 2, 3); + const float density = GENERATE(0.1f, 0.3f); + const bool with_hole = GENERATE(false, true); + // A single line of the other patterns is not a ring, and the center line of these two may stop short of the boundary. + if (multiline < first_multiline || (multiline == 3 && (pattern == ipCrossHatch || pattern == ipHilbertCurve))) + return; + CAPTURE(pattern, mode, optimized, multiline, density, with_hole); // A 60 mm cube, filled with lines connected along the boundary of its 0.45 mm narrower section. const double spacing = 0.45; const ExPolygons box{ExPolygon(Points{Point::new_scale(0., 0.), Point::new_scale(60., 0.), Point::new_scale(60., 60.), Point::new_scale(0., 60.)})}; - std::vector slices; - for (int i = 0; i < 300; ++i) - slices.push_back({0.2 * i, 0.2 * (i + 1), &box}); - const AdaptiveInfillField field(slices, get_extents(box), AdaptiveInfillMode::DistanceWarp, [] {}); - const AABBTreeLines::LinesDistancer walls(to_lines(offset_ex(box, -float(scale_(0.5 * spacing))))); + ExPolygon region = box.front(); + if (with_hole) { + Polygon hole = make_circle_num_segments(scale_(10.), 60); + hole.translate(Point::new_scale(30., 30.)); + hole.reverse(); + region.holes.emplace_back(std::move(hole)); + } + std::unique_ptr field; + if (mode != AdaptiveInfillMode::Disabled) { + std::vector slices; + for (int i = 0; i < 300; ++i) + slices.push_back({0.2 * i, 0.2 * (i + 1), &box}); + field = std::make_unique(slices, get_extents(box), AdaptiveInfillMode::DistanceWarp, [] {}); + } + const AABBTreeLines::LinesDistancer walls(to_lines(offset_ex(region, -float(scale_(0.5 * spacing))))); auto on_wall = [&walls](const Point &point) { return walls.distance_from_lines(point) < scale_(0.01); }; + auto runs_between_walls = [&on_wall](const Polyline &path) { + for (size_t i = 1; i < path.size(); ++i) + if (path.points[i - 1].distance_to(path.points[i]) > scale_(0.1) && on_wall(path.points[i - 1]) && on_wall(path.points[i])) + return true; + return false; + }; for (const double z : {5.1, 12.3, 20.1, 25.3, 28.7, 34.7, 40.1, 47.9, 54.9}) { CAPTURE(z); std::unique_ptr filler(Fill::new_from_type(pattern)); - filler->spacing = spacing; - filler->angle = float(M_PI / 4.); - filler->z = z; - filler->adaptive_infill_field = mode == AdaptiveInfillMode::Disabled ? nullptr : &field; + filler->spacing = spacing; + filler->angle = float(M_PI / 4.); + filler->z = z; + filler->layer_id = size_t(std::lround(z / 0.2)); + filler->adaptive_infill_field = field.get(); FillParams params; - params.density = 0.3f; + params.pattern = pattern; + params.density = density; params.multiline = multiline; params.gyroid_optimized = optimized; params.adaptive_infill_mode = mode; @@ -2432,14 +2469,18 @@ TEST_CASE("TPMS infill lines do not end where the rings of the pattern start", " params.adaptive_infill_gradient = AdaptiveInfillGradient::Linear; params.layer_height = 0.2; params.dont_adjust = true; - Surface surface(stInternal, box.front()); + Surface surface(stInternal, region); const Polylines paths = filler->fill_surface(&surface, params); REQUIRE_FALSE(paths.empty()); for (const Polyline &path : paths) if (path.first_point() == path.last_point()) { - // An island of the pattern, away from the boundary. Running along it means a ring cut at its start. - CHECK(std::none_of(path.points.begin(), path.points.end(), on_wall)); + // Not a line joined with its duplicate. + CHECK(path.size() > 3); + // The anchors of a line may meet on the boundary. Away from it this is an island of the pattern, unless + // it runs along the boundary, which means a ring cut at its start. + if (!on_wall(path.first_point())) + CHECK_FALSE(runs_between_walls(path)); } else { CHECK(on_wall(path.first_point())); CHECK(on_wall(path.last_point()));