diff --git a/docs/HLSD/only-one-wall-top.md b/docs/HLSD/only-one-wall-top.md new file mode 100644 index 0000000000..7aa8b7ab8a --- /dev/null +++ b/docs/HLSD/only-one-wall-top.md @@ -0,0 +1,96 @@ +# Only one wall on top surfaces — High Level Design + +## Purpose and scope + +`only_one_wall_top` leaves a single wall, the outer one, where a layer has a top surface, so the top fill runs up +to the outer wall instead of stopping at a band of inner walls. The topmost layer of an object, which is all top +surface, is printed with one wall. Below it, the perimeter generators find the top surface of each island and deal +with the inner walls that run over it. + +The option needs a top shell to act on. With `top_shell_layers` at 0 the top surfaces are retyped as internal (see +`LayerRegion::prepare_fill_surfaces()`), so `has_top_shell_layers()` switches the option off and the GUI hides it. +`only_one_wall_first_layer` is its bottom counterpart: it thins the first layer to one wall, and needs bottom shell +layers in the same way. + +A top surface is the part of an island that the next layer leaves uncovered. Bridges keep their walls, and a top +surface narrower than `min_width_top_surface` next to the geometry above is not treated as top, so thin features +such as lettering on the next layer do not strip the walls under them. + +## Two strategies + +What happens to the inner walls depends on whether the top fill can take their place +(`top_fill_replaces_inner_walls()`). + +- **Rerouting.** With `top_surface_expansion` at 0 or `top_surface_density` at 0%, the inner walls are generated + on the part of the island that is not top surface only. They stop at the top surface and run along its edge, so + every feature rising from a top surface is ringed by inner walls. Projects that do not use the expansion get + this result. +- **Removal.** With both set, the walls are generated on the real geometry, as with the option off, and the inner + walls that run over the top surface are then removed. `detect_surfaces_type()` grows the top surface by + `top_surface_expansion` under the features rising from it, inside the island less one wall, so the top fill runs + on under those features instead of being cut up by the walls that rerouting would put around them. + +Crossing zero with either setting switches between the strategies, so it regenerates the walls; other changes to +them only redo the fill steps. + +## Which walls run over the top + +The walls are generated before the surfaces are typed, so the perimeter generator works from the real top surface, +never from the expanded one. + +A wall runs over the top by the length of its centerline that lies inside the top region (`over_top_length()`). +Testing its vertices is not enough: a straight wall along a ledge crosses the top between two vertices that both +sit under the geometry rising at either end of the ledge. A wall whose bounding box misses the region is skipped +without any clipping. + +The classic generator's region is the area the top fill takes (`top_fill_area()`): the top fills grown by half the +outer wall spacing, to meet the outer wall, which is exactly what becomes the top fill later. The not-top +complement that `split_top_surfaces()` returns does not do: when gap fill is on it counts as not top the band +within one outer wall spacing of the outer wall, which the rerouting strategy leaves to gap fill. With the precise +outer wall off, or a wall order other than inner/outer, and equal wall spacings, the first inner wall lies exactly +on the inner edge of that band. Measured against the complement it never runs over the top, while the top fill +covers it along a whole exposed edge. + +## Classic walls + +A classic wall is a closed loop, printed as an `ExtrusionLoop` with its seam, place in the wall order, overhang +detection and fuzzy skin, so it is kept or dropped whole: + +- a wall with nothing over the top is kept; +- a wall that only grazes the top, by less than two wall widths, is kept whole, and the band it covers is withheld + from the top fill so the fill is routed around it; +- any other wall is dropped. + +The band a dropped wall covered outside the top region goes to the fill surfaces, so nothing is left void, and the +gaps inside the region are removed from the gap fill. Those fill surfaces are typed later like any other: top fill +where the expansion reaches, internal solid infill with Ensure vertical shell thickness at All, sparse infill with +the other settings. + +A dropped loop loses its parts outside the top surface as well. When a loop runs both over a ledge and under the +geometry rising from it, the walls under that geometry go for that layer, and a small top patch crossed by long +loops drops them all. + +## Arachne walls + +Arachne walls can be open lines, so `clip_inner_walls_over_top()` cuts them instead of dropping them, and the walls +under geometry that keeps rising stay. Each line is measured the same way against Arachne's top region. A line with +nothing over the top is kept and one wholly over it is dropped. Any other line is clipped against the region grown +by half a wall width, because the rounded end of a bead reaches half a width past its endpoint. Clipper treats the +line as open, so it also cuts a closed loop at its start vertex and may reverse pieces; pieces sharing an endpoint +are stitched back together, and pieces shorter than a wall width are dropped. A clip that would remove less than +two wall widths, about the pull-back of one crossing, keeps the line whole and withholds its footprint from the top +fill instead. + +The inner walls are numbered after the single outer wall. Arachne's single-wall pass may widen two beads to fill a +wall narrower than three outer walls, which leaves no room for the inner walls outside the top surface. When its +outer walls run more than a tenth of an outer wall width away from the full pass's, for more than a wall width +outside the top surface, the full pass's outer walls and the area inside them are used instead. + +## Constraints + +- A kept inner wall is never under the top fill; a wall that grazes the top has the fill routed around it. +- With the option off nothing changes, and without a top fill to take their place the inner walls are rerouted. +- Classic walls are dropped whole, Arachne walls are cut. Cutting classic walls would need open inner walls through + the loop pipeline, with a start and an end at every cut. +- The cost is one clip per inner wall whose bounding box meets the top region, on islands with a top surface below + the topmost layer. diff --git a/src/libslic3r/PerimeterGenerator.cpp b/src/libslic3r/PerimeterGenerator.cpp index bac82a92fd..fae9019ca3 100644 --- a/src/libslic3r/PerimeterGenerator.cpp +++ b/src/libslic3r/PerimeterGenerator.cpp @@ -639,36 +639,20 @@ static bool top_fill_replaces_inner_walls(const PrintRegionConfig &config) return has_top_shell_layers(config) && config.top_surface_density.value > 0 && config.top_surface_expansion.value > 0; } -// ORCA: only_one_wall_top - cheap per-vertex classification of a wall against the top surface. Only Partial -// needs the geometry clipped or measured; a segment crossing the top with no vertex inside is rare enough to ignore. -enum class TopOverlap { None, Partial, Full }; - -static bool point_over_top(const Point &p, const ExPolygons &top_region, const BoundingBox &top_region_bbox) +// ORCA: only_one_wall_top - how much of a wall runs over the top surface, measured along its centerline. The +// vertices alone do not tell: a wall can cross the top between two vertices that both sit next to a feature +// that keeps rising, which is what a long straight wall along a wide ledge looks like. +static double over_top_length(const Polyline ¢erline, const ExPolygons &top_region, const BoundingBox &top_region_bbox) { - if (! top_region_bbox.contains(p)) - return false; - for (const ExPolygon &ex : top_region) - if (ex.contains(p, false)) - return true; - return false; + if (! get_extents(centerline).overlap(top_region_bbox)) + return 0.; + return total_length(intersection_pl(Polylines{ centerline }, top_region)); } -static TopOverlap classify_over_top(const Points &pts, const ExPolygons &top_region, const BoundingBox &top_region_bbox) +// ORCA: the area the top fill takes, the top fills grown by half the outer wall spacing to meet the outer wall. +static ExPolygons top_fill_area(const ExPolygons &top_fills, const ExPolygons &fill_clip, coord_t ext_perimeter_spacing) { - size_t inside = 0; - for (const Point &p : pts) - if (point_over_top(p, top_region, top_region_bbox)) - ++ inside; - return inside == 0 ? TopOverlap::None : inside == pts.size() ? TopOverlap::Full : TopOverlap::Partial; -} - -static TopOverlap classify_over_top(const Arachne::ExtrusionLine &el, const ExPolygons &top_region, const BoundingBox &top_region_bbox) -{ - size_t inside = 0; - for (const Arachne::ExtrusionJunction &j : el.junctions) - if (point_over_top(j.p, top_region, top_region_bbox)) - ++ inside; - return inside == 0 ? TopOverlap::None : inside == el.junctions.size() ? TopOverlap::Full : TopOverlap::Partial; + return intersection_ex(fill_clip, offset_ex(top_fills, double(ext_perimeter_spacing / 2))); } // ORCA: only_one_wall_top for Arachne - cut out of the already generated inner walls the parts running over the top @@ -677,15 +661,18 @@ static TopOverlap classify_over_top(const Arachne::ExtrusionLine &el, const ExPo static void clip_inner_walls_over_top(std::vector &inner_perimeters, const ExPolygons &top_region, coord_t perimeter_width, Polygons &kept_over_top) { const BoundingBox top_region_bbox = get_extents(top_region).inflated(SCALED_EPSILON); - auto covered_by = [](const Arachne::ExtrusionLine &el) { + auto centerline_of = [](const Arachne::ExtrusionLine &el) { Polyline centerline; centerline.points.reserve(el.junctions.size()); - coord_t width = 0; - for (const Arachne::ExtrusionJunction &j : el.junctions) { + for (const Arachne::ExtrusionJunction &j : el.junctions) centerline.points.emplace_back(j.p); + return centerline; + }; + auto covered_by = [¢erline_of](const Arachne::ExtrusionLine &el) { + coord_t width = 0; + for (const Arachne::ExtrusionJunction &j : el.junctions) width = std::max(width, j.w); - } - return offset(centerline, float(width) / 2.f); + return offset(centerline_of(el), float(width) / 2.f); }; // Pull the cut back by half a wall width: the clip severs the centerline, but the bead's rounded end // extends half a width past its endpoint and would otherwise overlap the top fill. @@ -703,17 +690,18 @@ static void clip_inner_walls_over_top(std::vector & for (Arachne::ExtrusionLine &el : inner_perimeter) { if (el.empty()) continue; - const TopOverlap overlap = classify_over_top(el, top_region, top_region_bbox); - if (overlap == TopOverlap::None) { + const Polyline centerline = centerline_of(el); + const double over = over_top_length(centerline, top_region, top_region_bbox); + if (over <= 0.) { kept.emplace_back(std::move(el)); continue; } - if (overlap == TopOverlap::Full) - continue; // the clip below would return nothing anyway ClipperZUtils::ZPath subject; subject.reserve(el.size()); for (const Arachne::ExtrusionJunction &j : el.junctions) subject.emplace_back(j.p.x(), j.p.y(), j.w); + if (over >= clipper_z_path_length(subject) - double(SCALED_EPSILON)) + continue; // the clip below would return nothing anyway ClipperZUtils::ZPaths pieces = clip_extrusion(subject, top_paths_z, ctDifference); // Clipper treats the subject as an open polyline, so it also cuts a closed loop at its (arbitrary) @@ -1728,8 +1716,9 @@ void PerimeterGenerator::process_classic() ExPolygons non_top_polygons; this->split_top_surfaces(last, top_fills, non_top_polygons, fill_clip); apply_one_wall_top = !top_fills.empty(); + // All the top fill covers, see docs/HLSD/only-one-wall-top.md. if (apply_one_wall_top) - one_wall_top_region = diff_ex(last, non_top_polygons); + one_wall_top_region = top_fill_area(top_fills, fill_clip, ext_perimeter_spacing); } else { // Onion the not-top region only, so the remaining walls stop at the top boundary. this->split_top_surfaces(last, top_fills, last, fill_clip); @@ -1760,12 +1749,15 @@ void PerimeterGenerator::process_classic() Polygons dropped_wall_bands; auto reduce_over_top = [&](PerimeterGeneratorLoops &loops) { loops.erase(std::remove_if(loops.begin(), loops.end(), [&](const PerimeterGeneratorLoop &loop) { - const TopOverlap overlap = classify_over_top(loop.polygon.points, one_wall_top_region, top_region_bbox); - if (overlap == TopOverlap::None) - return false; - // Only a wall straddling the boundary is worth measuring; a wall wholly over the top goes. - if (overlap == TopOverlap::Partial && - total_length(intersection_pl(Polylines{ loop.polygon.split_at_first_point() }, one_wall_top_region)) < grazing_tolerance) { + if (! get_extents(loop.polygon).overlap(top_region_bbox)) + return false; // too far from the top to run over it + const Polyline centerline = loop.polygon.split_at_first_point(); + const double over = over_top_length(centerline, one_wall_top_region, top_region_bbox); + if (over <= 0.) + return false; // no part of the wall runs over the top + // A wall wholly over the top goes. One that merely grazes it is kept whole, its footprint + // withheld from the top fill instead of slitting the loop open. + if (over < loop.polygon.length() - double(SCALED_EPSILON) && over < grazing_tolerance) { append(one_wall_top_kept_bands, wall_band(loop.polygon)); return false; } @@ -2065,7 +2057,7 @@ void PerimeterGenerator::process_classic() float(min_perimeter_infill_spacing / 2.)); // append infill areas to fill_surfaces //if any top_fills, grow them by ext_perimeter_spacing/2 to have the real un-anchored fill - ExPolygons top_infill_exp = intersection_ex(fill_clip, offset_ex(top_fills, double(ext_perimeter_spacing / 2))); + ExPolygons top_infill_exp = top_fill_area(top_fills, fill_clip, ext_perimeter_spacing); // ORCA: only_one_wall_top - route the top fill around the walls kept despite grazing the top. if (!one_wall_top_kept_bands.empty()) top_infill_exp = diff_ex(top_infill_exp, one_wall_top_kept_bands); diff --git a/tests/fff_print/test_perimeters.cpp b/tests/fff_print/test_perimeters.cpp index a97e3f5656..a703bf9c09 100644 --- a/tests/fff_print/test_perimeters.cpp +++ b/tests/fff_print/test_perimeters.cpp @@ -278,6 +278,167 @@ TEST_CASE("Only one wall on the first layer needs a bottom shell", "[Perimeters] namespace { +// A 40x40x5 base with a 10x10x10 post in each corner. The inner walls of the base's top layer run along the +// ledge between two posts, so every vertex of a loop sits inside the posts, which keep rising, while the side +// between two posts runs over the top surface. +Print &base_with_corner_posts(Print &print, Model &model, const DynamicPrintConfig &config) +{ + ModelObject *object = model.add_object(); + object->name = "base_with_corner_posts.stl"; + object->add_volume(make_cube(40., 40., 5.), ModelVolumeType::MODEL_PART, false); + for (const double x : { 0., 30. }) + for (const double y : { 0., 30. }) { + TriangleMesh post = make_cube(10., 10., 10.); + post.translate(float(x), float(y), 0.f); + object->add_volume(std::move(post), ModelVolumeType::MODEL_PART, false); + } + object->add_instance(); + object->ensure_on_bed(); + + print.auto_assign_extruders(object); + print.apply(model, config); + print.validate(); + print.set_status_silent(); + print.process(); + return print; +} + +const Layer *layer_at(const Print &print, double print_z) +{ + for (const Layer *layer : print.objects().front()->layers()) + if (std::abs(layer->print_z - print_z) < 1e-4) + return layer; + return nullptr; +} + +// Length of the top surface fill on a layer. +double top_fill_length(const Layer &layer) +{ + double len = 0.; + for (const LayerRegion *region : layer.regions()) + for (const ExtrusionEntity *entity : region->fills.flatten().entities) + if (is_top_surface(entity->role())) + len += entity->length(); + return unscale(len); +} + +// Length of the inner-wall centerline lying inside the top surface fill on a layer. +double inner_wall_over_top_fill(const Layer &layer) +{ + double over = 0.; + for (const LayerRegion *region : layer.regions()) { + Polygons top_fill; + for (const ExtrusionEntity *entity : region->fills.flatten().entities) + if (is_top_surface(entity->role())) + append(top_fill, entity->polygons_covered_by_width()); + if (top_fill.empty()) + continue; + for (const ExtrusionEntity *entity : region->perimeters.flatten().entities) + if (is_internal_perimeter(entity->role())) + over += total_length(intersection_pl(entity->as_polylines(), top_fill)); + } + return unscale(over); +} + +// Length of the inner walls on a layer. +double inner_wall_length(const Layer &layer) +{ + double len = 0.; + for (const LayerRegion *region : layer.regions()) + for (const ExtrusionEntity *entity : region->perimeters.flatten().entities) + if (is_internal_perimeter(entity->role())) + len += entity->length(); + return unscale(len); +} + +// Length of the inner-wall centerline inside the given area on a layer. +double inner_wall_length_inside(const Layer &layer, const ExPolygons &area) +{ + double len = 0.; + for (const LayerRegion *region : layer.regions()) + for (const ExtrusionEntity *entity : region->perimeters.flatten().entities) + if (is_internal_perimeter(entity->role())) + len += total_length(intersection_pl(entity->as_polylines(), area)); + return unscale(len); +} + +} // namespace + +// A wall runs over the top surface wherever any part of it is over that surface, which a per-vertex test does +// not see when the wall crosses the top between two vertices that both sit over the geometry that keeps +// rising. The inner walls of a base between its corner posts are exactly that: the top fill covers them, so +// the perimeter generator has to give them up rather than print the fill over them. +TEST_CASE("Only one wall on top surfaces keeps the top fill off inner walls that cross it between their vertices", "[Perimeters]") +{ + const char *wall_generator = GENERATE("classic", "arachne"); + CAPTURE(wall_generator); + + DynamicPrintConfig config = base_config(wall_generator); + config.set_deserialize_strict({ + { "only_one_wall_top", true }, + { "top_surface_expansion", 2.0 }, + }); + Print print; + Model model; + base_with_corner_posts(print, model, config); + + const Layer *ledge = layer_at(print, ledge_z); + REQUIRE(ledge != nullptr); + // The ledge layer has a top fill to keep clear, and the layer below still has the inner walls, so the + // check below is about which walls survive the ledge and not about the shape lacking either. + REQUIRE(top_fill_length(*ledge) > 0.); + const Layer *below = layer_at(print, ledge_z - 0.2); + REQUIRE(below != nullptr); + REQUIRE(inner_wall_length(*below) > 0.); + + CHECK(inner_wall_over_top_fill(*ledge) < 1.0); +} + +// Arachne cuts its inner walls at the top surface instead of dropping them, so the walls under the posts stay. Each +// post sits on two outer sides of the base, each carrying both inner walls of wall_loops 3: 4 x 2 x 2 x 10mm, of +// which the cut at the inner sides of the posts may take some, but not half. +TEST_CASE("Only one wall on top surfaces keeps the Arachne inner walls under the geometry that keeps rising", "[Perimeters]") +{ + DynamicPrintConfig config = base_config("arachne"); + config.set_deserialize_strict({ + { "only_one_wall_top", true }, + { "top_surface_expansion", 2.0 }, + }); + Print print; + Model model; + base_with_corner_posts(print, model, config); + + const Layer *ledge = layer_at(print, ledge_z); + REQUIRE(ledge != nullptr); + REQUIRE(ledge->upper_layer != nullptr); + CHECK(inner_wall_length_inside(*ledge, ledge->upper_layer->lslices) > 0.5 * 4 * 2 * 2 * 10.); +} + +// With the precise outer wall off and equal wall spacings, the first inner wall lies exactly on the edge of the band +// next to the outer wall that split_top_surfaces() counts as not top, while the top fill covers it. A ledge along the +// whole outer wall gives that wall no other crossing to be caught by. +TEST_CASE("Only one wall on top surfaces keeps the top fill off the inner wall beside the outer wall", "[Perimeters]") +{ + const char *wall_generator = GENERATE("classic", "arachne"); + CAPTURE(wall_generator); + + DynamicPrintConfig config = base_config(wall_generator); + config.set_deserialize_strict({ + { "only_one_wall_top", true }, + { "top_surface_expansion", 2.0 }, + { "precise_outer_wall", false }, + }); + Print print; + init_and_process_print({ step_with_ledge() }, print, config); + + const Layer *ledge = layer_at(print, ledge_z); + REQUIRE(ledge != nullptr); + REQUIRE(top_fill_length(*ledge) > 0.); + CHECK(inner_wall_over_top_fill(*ledge) < 1.0); +} + +namespace { + // The last layer of the tab, whose top surface shares an island with the tube walls rising past it. const double tab_top_z = 5.0;