diff --git a/docs/HLSD/travel-optimization.md b/docs/HLSD/travel-optimization.md index 119133cee2..79774f7ff9 100644 --- a/docs/HLSD/travel-optimization.md +++ b/docs/HLSD/travel-optimization.md @@ -10,7 +10,9 @@ things shorten the travel between infill paths: - the fill generators join their lines into as few paths as they can, since every separate path needs a travel to it; - the G-code export refines the greedy chain with a local search that shortens - the straight-line travel between the paths. + the straight-line travel between the paths; +- a fill that keeps its order prints each piece of its gap fill where it passes + by, since the G-code export cannot reorder it. The refinement covers every chain that `chain_and_reorder_extrusion_entities()` builds from a start point: the infill and ironing of `GCode::extrude_infill()` @@ -97,6 +99,22 @@ joined at different walls and the travel between them crossed the island. Lines that cannot reverse, such as Grid's, only join the end of one to the start of the next, and fills that keep their order are left alone. +## Gap fill in a fill that keeps its order + +`Fill::_create_gap_fill()` adds the gap fill of a solid surface, where +`gap_fill_target` asks for it, after the lines of the fill. The chain mixes it +in with the lines of a sortable fill like any other path. A fill that keeps its +order, such as monotonic, would print all of its lines and then come back for +every piece of gap fill, most of which lies at the walls where the lines end. + +So each piece, in turn, goes between the two extrusions of the fill where it +adds the least travel, flipped when that is shorter. A closed piece counts from +its point nearest the nozzle, where `GCode::extrude_loop()` starts it. The two +ends of the fill cost nothing, since the travel to and from them depends on what +is printed before and after. The fill's own extrusions keep their order and +direction, so a monotonic fill stays monotonic. When the result travels no less +than the gap fill printed after the lines in its own order, that order is kept. + ## The search A chain of up to eight paths is found exactly, by dynamic programming over the diff --git a/src/libslic3r/Fill/FillBase.cpp b/src/libslic3r/Fill/FillBase.cpp index 7a2aee3f7f..d855dc1446 100644 --- a/src/libslic3r/Fill/FillBase.cpp +++ b/src/libslic3r/Fill/FillBase.cpp @@ -228,6 +228,79 @@ void Fill::fill_surface_extrusion(const Surface* surface, const FillParams& para } } +// Orca: port of Bambu Studio's gap fill interleaving for fills printed in a fixed order, see docs/HLSD/travel-optimization.md. +static void insert_gap_fill(const ThickPolylines &gaps, const Flow &flow, ExtrusionEntitiesPtr &entities) +{ + if (entities.empty()) { + variable_width(gaps, erGapFill, flow, entities); + return; + } + static constexpr size_t npos = size_t(-1); + // An extrusion of the fill, or gaps[gap], with the points where the nozzle enters and leaves it. + struct Item { Point in, out; size_t gap; bool reversed; }; + auto distance = [](const Point &a, const Point &b) { return (b - a).cast().norm(); }; + auto closed = [&gaps](size_t gap) { return gaps[gap].first_point() == gaps[gap].last_point(); }; + // GCode::extrude_loop starts a loop at its point nearest the nozzle. + auto travel = [&](const std::vector &seq) { + double sum = 0.; + Point at = seq.front().out; + for (auto it = seq.begin() + 1; it != seq.end(); ++ it) { + const Point in = it->gap != npos && closed(it->gap) ? *gaps[it->gap].closest_point(at) : it->in; + sum += distance(at, in); + at = it->gap != npos && closed(it->gap) ? in : it->out; + } + return sum; + }; + + std::vector items, appended; + items.reserve(entities.size() + gaps.size()); + for (const ExtrusionEntity *ee : entities) + items.push_back({ ee->first_point(), ee->last_point(), npos, false }); + appended = items; + for (size_t gap = 0; gap < gaps.size(); ++ gap) { + appended.push_back({ gaps[gap].first_point(), gaps[gap].last_point(), gap, false }); + double best = std::numeric_limits::max(); + size_t best_slot = 0; + Item best_item{}; + for (size_t slot = 0; slot <= items.size(); ++ slot) { + const Point *prev = slot > 0 ? &items[slot - 1].out : nullptr; + const Point *next = slot < items.size() ? &items[slot].in : nullptr; + auto consider = [&](const Point &in, const Point &out, bool reversed) { + const double added = (prev ? distance(*prev, in) : 0.) + (next ? distance(out, *next) : 0.) - (prev && next ? distance(*prev, *next) : 0.); + if (added < best) { + best = added; + best_slot = slot; + best_item = { in, out, gap, reversed }; + } + }; + if (closed(gap)) { + const Point &entry = *gaps[gap].closest_point(prev ? *prev : *next); + consider(entry, entry, false); + } else { + consider(gaps[gap].first_point(), gaps[gap].last_point(), false); + consider(gaps[gap].last_point(), gaps[gap].first_point(), true); + } + } + items.insert(items.begin() + best_slot, best_item); + } + if (travel(appended) <= travel(items)) + items = std::move(appended); + + ExtrusionEntitiesPtr out; + out.reserve(entities.size() + gaps.size()); + size_t next_entity = 0; + for (const Item &item : items) + if (item.gap == npos) + out.push_back(entities[next_entity ++]); + else { + ThickPolylines gap { gaps[item.gap] }; + if (item.reversed) + gap.front().reverse(); + variable_width(gap, erGapFill, flow, out); + } + entities = std::move(out); +} + // Orca: Dedicated function to calculate gap fill lines for the provided surface, according to the print object parameters // and append them to the out ExtrusionEntityCollection. void Fill::_create_gap_fill(const Surface* surface, const FillParams& params, ExtrusionEntityCollection* out){ @@ -277,7 +350,10 @@ void Fill::_create_gap_fill(const Surface* surface, const FillParams& params, Ex return p.length() < scale_(params.config->filter_out_gap_fill.value); }), polylines.end()); - variable_width(polylines, erGapFill, params.flow, out->entities); + if (out->no_sort) + insert_gap_fill(polylines, params.flow, out->entities); + else + variable_width(polylines, erGapFill, params.flow, out->entities); } } } diff --git a/tests/fff_print/test_fill.cpp b/tests/fff_print/test_fill.cpp index b2d0ef9abe..bd87ddc2ef 100644 --- a/tests/fff_print/test_fill.cpp +++ b/tests/fff_print/test_fill.cpp @@ -2417,3 +2417,70 @@ TEST_CASE("Infill lines that cannot reverse join only end to start", "[Fill]") CHECK(position(c) < position(d)); } } + +// Orca: the top surface fills of the topmost layer, which keep their order, each as its extrusions in print order. +static std::vector> top_surface_fills(const Print &print) +{ + std::vector> fills; + for (const LayerRegion *region : print.objects().front()->layers().back()->regions()) + for (const ExtrusionEntity *entity : region->fills.entities) + if (auto *fill = dynamic_cast(entity); fill && fill->no_sort) + if (std::any_of(fill->entities.begin(), fill->entities.end(), [](const ExtrusionEntity *ee) { return ee->role() == erTopSolidInfill; })) + fills.emplace_back(fill->entities.begin(), fill->entities.end()); + return fills; +} + +static double travel_between(const std::vector &extrusions) +{ + double travel = 0.; + for (size_t i = 1; i < extrusions.size(); ++ i) + travel += unscale((extrusions[i]->first_point() - extrusions[i - 1]->last_point()).cast().norm()); + return travel; +} + +TEST_CASE("Monotonic line top surfaces print their gap fill next to their lines and keep the lines in order", "[Fill]") +{ + // The slanted legs of the letter A leave gap fill along the walls where the lines end. + auto slice_letter = [](Print &print, Model &model, const char *gap_fill_target) { + auto config = DynamicPrintConfig::full_print_config(); + config.set_deserialize_strict({{"top_surface_pattern", "monotonicline"}, + {"gap_fill_target", gap_fill_target}, + {"filter_out_gap_fill", 0}, + {"top_shell_layers", 3}, + {"layer_height", 0.2}, + {"initial_layer_print_height", 0.2}}); + Slic3r::Test::init_print({Slic3r::Test::TestMesh::A}, print, model, config); + print.process(); + }; + Print print_without, print_with; + Model model_without, model_with; + slice_letter(print_without, model_without, "nowhere"); + slice_letter(print_with, model_with, "topbottom"); + const auto without = top_surface_fills(print_without); + const auto with = top_surface_fills(print_with); + REQUIRE(with.size() == without.size()); + + auto is_gap_fill = [](const ExtrusionEntity *ee) { return ee->role() == erGapFill; }; + size_t gap_fills = 0, gap_fills_before_a_line = 0; + for (size_t i = 0; i < with.size(); ++ i) { + std::vector lines, gaps; + std::partition_copy(with[i].begin(), with[i].end(), std::back_inserter(gaps), std::back_inserter(lines), is_gap_fill); + const auto last_line = std::find_if_not(with[i].rbegin(), with[i].rend(), is_gap_fill); + gap_fills += gaps.size(); + gap_fills_before_a_line += std::count_if(last_line, with[i].rend(), is_gap_fill); + // The lines are those of the surface without gap fill, in the same order and direction. + REQUIRE(lines.size() == without[i].size()); + for (size_t j = 0; j < lines.size(); ++ j) { + CHECK(lines[j]->first_point() == without[i][j]->first_point()); + CHECK(lines[j]->last_point() == without[i][j]->last_point()); + } + // Printed where they are, the gap fills travel less than printed after the last line. + if (! gaps.empty()) { + std::vector appended = lines; + appended.insert(appended.end(), gaps.begin(), gaps.end()); + CHECK(travel_between(with[i]) < travel_between(appended)); + } + } + REQUIRE(gap_fills > 0); + CHECK(gap_fills_before_a_line > 0); +}