diff --git a/src/libslic3r/Arrange.cpp b/src/libslic3r/Arrange.cpp index 997ae00a56..4aee1520fe 100644 --- a/src/libslic3r/Arrange.cpp +++ b/src/libslic3r/Arrange.cpp @@ -1,6 +1,8 @@ #include "Arrange.hpp" +#include "ClipperUtils.hpp" #include "ExPolygon.hpp" #include "Point.hpp" +#include "Polygon.hpp" #include "Print.hpp" #include "BoundingBox.hpp" #include "PrintConfig.hpp" @@ -153,11 +155,6 @@ void update_arrange_params(ArrangeParams& params, const DynamicPrintConfig* prin params.brim_skirt_distance = skirt_distance; params.bed_shrink_x += params.brim_skirt_distance; params.bed_shrink_y += params.brim_skirt_distance; - // for sequential print, we need to inflate the bed because clearance_radius is so large - if (params.is_seq_print) { - params.bed_shrink_x -= params.clearance_radius / 2; - params.bed_shrink_y -= params.clearance_radius / 2; - } } void update_selected_items_inflation(ArrangePolygons& selected, const DynamicPrintConfig* print_cfg, ArrangeParams& params) { @@ -168,7 +165,8 @@ void update_selected_items_inflation(ArrangePolygons& selected, const DynamicPri if (params.is_seq_print) { bool all_objects_are_short = std::all_of(selected.begin(), selected.end(), [&](ArrangePolygon& ap) { return ap.height < params.nozzle_height; }); if (all_objects_are_short) { - params.min_obj_distance = std::max(params.min_obj_distance, scaled(std::max(MAX_OUTER_NOZZLE_DIAMETER/2.f, params.object_skirt_offset*2)+0.001)); + // The gap Print::sequential_print_clearance_valid() requires between short objects. + params.min_obj_distance = std::max(params.min_obj_distance, scaled(std::max(float(MAX_OUTER_NOZZLE_DIAMETER), params.object_skirt_offset*2)+0.001)); } else params.min_obj_distance = std::max(params.min_obj_distance, scaled(params.clearance_radius + 0.001)); // +0.001mm to avoid clearance check fail due to rounding error @@ -200,25 +198,42 @@ void update_selected_items_inflation(ArrangePolygons& selected, const DynamicPri if (!selected.empty()) { coord_t max_width = 0, max_height = 0; for (const ArrangePolygon &ap : selected) { - BoundingBox apbb = ap.poly.contour.bounding_box(); - max_width = std::max(max_width, apbb.size().x() + 2 * ap.inflation); - max_height = std::max(max_height, apbb.size().y() + 2 * ap.inflation); + BoundingBox apbb = ap.poly.contour.bounding_box(); + coord_t inflation = ap.inflation; + if (params.is_seq_print) { + // Printing by object, the inflation only keeps objects apart and may reach past the edge, + // so measure the object itself, leaving EPSILON for rounding when it fills the bed. Measure + // it as the nester packs it, inflated with square joins and then turned, since the corners + // of that outline reach past the inflation. + const ExPolygons inflated = offset_ex(ap.poly, float(ap.inflation), jtSquare); + Polygon outline = inflated.empty() ? ap.poly.contour : inflated.front().contour; + outline.rotate(ap.rotation); + apbb = outline.bounding_box(); + inflation = scaled(EPSILON) - (inflated.empty() ? 0 : ap.inflation); + } + max_width = std::max(max_width, apbb.size().x() + 2 * inflation); + max_height = std::max(max_height, apbb.size().y() + 2 * inflation); } auto margin = [](coord_t bed, coord_t item) { return std::max(0., unscale(bed - item) / 2.); }; params.bed_shrink_x = std::min(params.bed_shrink_x, std::max(params.brim_skirt_distance, margin(bedbb.size().x(), max_width))); params.bed_shrink_y = std::min(params.bed_shrink_y, std::max(params.brim_skirt_distance, margin(bedbb.size().y(), max_height))); } + // Printing by object, grow the bed by the objects' inflation, so the objects themselves keep the bed margin. + if (params.is_seq_print) { + params.bed_shrink_x -= unscale(params.min_obj_distance) / 2; + params.bed_shrink_y -= unscale(params.min_obj_distance) / 2; + } } void update_unselected_items_inflation(ArrangePolygons& unselected, const DynamicPrintConfig* print_cfg, const ArrangeParams& params) { float exclusion_gap = 1.f; if (params.is_seq_print) { - // bed_shrink_x is typically (-params.clearance_radius / 2+5) for seq_print - exclusion_gap = std::max(exclusion_gap, params.clearance_radius / 2 + params.bed_shrink_x + 1.f); // +1mm gap so the exclusion region is not too close - // dont forget to move the excluded region + // bed_shrink_x includes the bed's growth by min_obj_distance / 2; add it back so the gap is the bed margin. + exclusion_gap = std::max(exclusion_gap, unscale(params.min_obj_distance) / 2 + params.bed_shrink_x + 1.f); // +1mm gap so the exclusion region is not too close + // dont forget to move the excluded region when the bed grows for (auto& region : unselected) { - if (region.is_virt_object) region.poly.translate(scaled(params.bed_shrink_x), scaled(params.bed_shrink_y)); + if (region.is_virt_object) region.poly.translate(scaled(std::min(0.f, params.bed_shrink_x)), scaled(std::min(0.f, params.bed_shrink_y))); } } // For occulusion regions, inflation should be larger to prevent genrating brim on them. @@ -1089,13 +1104,19 @@ std::function AutoArranger::g }; } -template void remove_large_items(std::vector &items, Bin &&bin) +// Drop the fixed objects lying wholly outside the bin, such as those on other plates. One that reaches +// into the bin still takes the room it covers there, like an object at the bed edge whose inflation +// crosses the edge of a shrunk bed. +template void remove_items_outside_bin(std::vector &items, const Bin &bin) { + const auto binbb = sl::boundingBox(bin); auto it = items.begin(); while (it != items.end()) { + const auto bb = it->boundingBox(); //BBS: skip virtual object - if (!it->is_virt_object && !sl::isInside(it->transformedShape(), bin)) + if (!it->is_virt_object && (bb.maxCorner().x() < binbb.minCorner().x() || bb.minCorner().x() > binbb.maxCorner().x() || + bb.maxCorner().y() < binbb.minCorner().y() || bb.minCorner().y() > binbb.maxCorner().y())) it = items.erase(it); else it++; @@ -1141,7 +1162,7 @@ void _arrange( AutoArranger arranger{corrected_bin, mod_params, progressfn, stopfn}; - remove_large_items(excludes, corrected_bin); + remove_items_outside_bin(excludes, corrected_bin); // If there is something on the plate if (!excludes.empty()) arranger.preload(excludes); diff --git a/tests/libslic3r/test_arrange.cpp b/tests/libslic3r/test_arrange.cpp index 68669949a6..69d9e87c41 100644 --- a/tests/libslic3r/test_arrange.cpp +++ b/tests/libslic3r/test_arrange.cpp @@ -52,6 +52,21 @@ ArrangePolygons squares(int n, double side_mm, double height_mm = 0.) return items; } +// 64-gon disc of the given diameter, centred on the origin. +ArrangePolygon make_disc(double diameter_mm, double height_mm) +{ + Polygon p; + for (int i = 0; i < 64; ++i) { + const double a = 2. * PI * i / 64; + p.points.emplace_back(scaled(diameter_mm / 2 * std::cos(a)), scaled(diameter_mm / 2 * std::sin(a))); + } + ArrangePolygon ap; + ap.poly = ExPolygon(p); + ap.bed_idx = 0; + ap.height = height_mm; + return ap; +} + // Bed [0,0]..[w,h] in scaled coordinates. BoundingBox bed(double w_mm, double h_mm) { @@ -106,7 +121,7 @@ void require_no_overlap(const ArrangePolygons &items) // so the two are defined together and every expectation is derived from them. constexpr double NOZZLE_HEIGHT_MM = 2.5; constexpr double CLEARANCE_MM = 30.; -constexpr double NOZZLE_FLOOR_MM = MAX_OUTER_NOZZLE_DIAMETER / 2.; +constexpr double NOZZLE_FLOOR_MM = MAX_OUTER_NOZZLE_DIAMETER; ArrangeParams seq_print_params(coord_t min_dist) { @@ -134,6 +149,25 @@ ArrangePolygons squares_of_heights(const std::vector &heights_mm) return items; } +// Arranges as the arrange job does for a sequential print, on the bed_config() bed. +void arrange_by_object(ArrangePolygons &items, ArrangePolygons &fixed) +{ + DynamicPrintConfig cfg = bed_config(); + ArrangeParams p = seq_print_params(0); + p.bed_shrink_x = BED_SHRINK_SEQ_PRINT; + p.bed_shrink_y = BED_SHRINK_SEQ_PRINT; + // Rod and lid above the objects, so they need not each go to a bed of their own. + p.clearance_height_to_rod = float(NOZZLE_HEIGHT_MM * 10); + p.clearance_height_to_lid = float(NOZZLE_HEIGHT_MM * 40); + for (ArrangePolygon &ap : items) + ap.extrude_ids = {1}; // the sequential sort reads extrude_ids.front() + + update_arrange_params(p, &cfg, items); + update_selected_items_inflation(items, &cfg, p); + update_unselected_items_inflation(fixed, &cfg, p); + arrange(items, fixed, get_shrink_bedpts(&cfg, p), p); +} + } // namespace // Prove the overlap check the other tests rely on actually detects overlap. @@ -506,6 +540,112 @@ TEST_CASE("Layered printing does not floor the object distance", "[Arrange]") CHECK(p.min_obj_distance == 0); } +// Short objects only need a nozzle's width between them, far less than the clearance +// radius, so the bed may only grow by half that width. A row of four 50 mm objects and +// their gaps is wider than the 200 mm bed but would fit one grown by half the clearance +// radius. Twenty of them overfill the bed, so the pile reaches its edges. +TEST_CASE("Sequential print keeps short objects on the bed and a nozzle apart", "[Arrange]") +{ + ArrangePolygons items = squares(20, 50., NOZZLE_HEIGHT_MM / 2); + ArrangePolygons fixed; + arrange_by_object(items, fixed); + + // Grown as Print::sequential_print_clearance_valid() grows them. + ExPolygons grown; + for (const ArrangePolygon &ap : items) { + if (ap.bed_idx != 0) + continue; + CHECK(bed(200, 200).contains(ap.transformed_poly().contour.bounding_box())); + append(grown, offset_ex(ap.transformed_poly(), float(scale_(0.5 * MAX_OUTER_NOZZLE_DIAMETER - 0.1)))); + } + REQUIRE(grown.size() > 1); + REQUIRE(disjoint(grown)); +} + +// Only the disc has to fit the bed; the half gap it is inflated by may reach past the +// edge. Within the bed margin of filling the bed, the arrange bed is cut to the disc's +// size, and rounding must not leave it a hair too small. +TEST_CASE("Sequential print arranges a short object filling the printable area", "[Arrange]") +{ + ArrangePolygons items{make_disc(196., NOZZLE_HEIGHT_MM / 2)}; + ArrangePolygons fixed; + arrange_by_object(items, fixed); + + REQUIRE(items.front().bed_idx == 0); + REQUIRE(bed(200, 200).contains(items.front().transformed_poly().contour.bounding_box())); +} + +// The largest object and its clearance are wider than the bed less its margins, though +// the object itself fits within them, so every object keeps the margin. +TEST_CASE("Sequential print keeps the bed margin beside a large tall object", "[Arrange]") +{ + ArrangePolygons items = squares(3, 10., NOZZLE_HEIGHT_MM * 2); + items.push_back(make_disc(170., NOZZLE_HEIGHT_MM * 2)); + ArrangePolygons fixed; + arrange_by_object(items, fixed); + + const coord_t margin = scaled(BED_SHRINK_SEQ_PRINT - 0.01); // 0.01 mm for rounding + const BoundingBox inner(Point(margin, margin), Point(scaled(200.) - margin, scaled(200.) - margin)); + for (const ArrangePolygon &ap : items) + if (ap.bed_idx == 0) + CHECK(inner.contains(ap.transformed_poly().contour.bounding_box())); +} + +// Short objects keep off an extrusion calibration region. One along the back edge catches +// the region being moved back by a positive bed shrink, which lets objects onto it. +TEST_CASE("Sequential print keeps short objects off a calibration region", "[Arrange]") +{ + const BoundingBox region_bb(Point(0, scaled(185.)), Point(scaled(200.), scaled(200.))); + ArrangePolygon region; + region.poly = ExPolygon(region_bb.polygon()); + region.bed_idx = 0; + region.is_virt_object = true; + region.is_extrusion_cali_object = true; + ArrangePolygons items = squares(20, 40., NOZZLE_HEIGHT_MM / 2); + ArrangePolygons fixed{region}; + arrange_by_object(items, fixed); + + for (const ArrangePolygon &ap : items) + if (ap.bed_idx == 0) + CHECK(ap.transformed_poly().contour.bounding_box().max.y() <= region_bb.min.y()); +} + +// The nester packs an object turned and inflated, and the inflated corners of a turned square +// reach past its inflation, so cutting the arrange bed to the object's size must measure that +// outline. The square is 196 mm wide turned 30 degrees. +TEST_CASE("Sequential print arranges a turned short object filling the printable area", "[Arrange]") +{ + ArrangePolygon item = make_square(scaled(196. / (std::cos(PI / 6) + std::sin(PI / 6)))); + item.height = NOZZLE_HEIGHT_MM / 2; + item.rotation = PI / 6; + ArrangePolygons items{item}; + ArrangePolygons fixed; + arrange_by_object(items, fixed); + + REQUIRE(items.front().bed_idx == 0); + REQUIRE(bed(200, 200).contains(items.front().transformed_poly().contour.bounding_box())); +} + +// An object closer to the bed edge than the bed margin, as one placed by hand, still takes its +// room when Fill bed adds objects around it, though its inflation crosses the arrange bed's edge. +TEST_CASE("Sequential print keeps short objects off one at the bed edge", "[Arrange]") +{ + ArrangePolygon obstacle = make_square(scaled(40.)); + obstacle.translation = {scaled(1.), scaled(80.)}; + obstacle.height = NOZZLE_HEIGHT_MM / 2; + obstacle.extrude_ids = {1}; + ArrangePolygons fixed{obstacle}; + ArrangePolygons items = squares(20, 40., NOZZLE_HEIGHT_MM / 2); + arrange_by_object(items, fixed); + + ExPolygons placed{obstacle.transformed_poly()}; + for (const ArrangePolygon &ap : items) + if (ap.bed_idx == 0) + placed.emplace_back(ap.transformed_poly()); + REQUIRE(placed.size() > 1); + REQUIRE(disjoint(placed)); +} + TEST_CASE("Bed exclusions apply by physical nozzle and overlapping Z range", "[Arrange][ExclusionVolume][MultiNozzle]") { ArrangePolygon object = make_square(scaled(20.));