diff --git a/src/libslic3r/ConnectedBodies.cpp b/src/libslic3r/ConnectedBodies.cpp index 8cfe5a3e43..e9fe96511d 100644 --- a/src/libslic3r/ConnectedBodies.cpp +++ b/src/libslic3r/ConnectedBodies.cpp @@ -89,11 +89,14 @@ std::vector> connected_bodies(const std::vector order(m_boxes.size()); for (size_t i = 0; i < order.size(); ++i) diff --git a/src/libslic3r/ConnectedBodies.hpp b/src/libslic3r/ConnectedBodies.hpp index a420401d28..d6f43a8bb5 100644 --- a/src/libslic3r/ConnectedBodies.hpp +++ b/src/libslic3r/ConnectedBodies.hpp @@ -26,7 +26,9 @@ class IslandLocator { public: // The islands must outlive the locator. Their boxes are widened by the margin, for points reaching past an outline. - IslandLocator(const ExPolygons &islands, coord_t margin); + // A caller that needs only to know that some island holds a point, not which one, need not tell them apart: then the + // box of every island vouches for it unless the call is strict. + IslandLocator(const ExPolygons &islands, coord_t margin, bool tell_apart = true); // Whether the island holds the point, or its box does where no other box reaches unless strict. bool holds(size_t island, const Point &point, bool strict = false) const; // The island holding the point as above, else the nearest one whose box holds it, with the squared distance to it; -1 diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index 6508c8453f..e32dc84920 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -2644,9 +2644,11 @@ static void set_mass_locator(GCodeProcessor &processor, const Print &print) model_instances.begin()); } bodies_total += int(count * object->instances().size()); + // Without bodies only the instance matters, which any island box of an instance no other reaches proves. The + // islands of crowded instances are tested strictly anyway. for (const Layer *layer : layers) { o.print_zs.emplace_back(layer->print_z); - o.islands.emplace_back(layer->lslices, scaled(1.)); + o.islands.emplace_back(layer->lslices, scaled(1.), o.bodies_count > 0); } } if (objects.empty()) @@ -2706,12 +2708,13 @@ static void set_mass_locator(GCodeProcessor &processor, const Print &print) return GCodeProcessor::MassLocation{ o.first_instance + int(hit.instance), o.bodies_count == 0 ? -1 : o.first_body + int(hit.instance * o.bodies_count + o.bodies[hit.layer][hit.island]) }; }; - // A point lies on the first layer at or above it, as spiral vase rises through each layer. + // A point lies on the first layer whose print_z is at least its height less the tolerance, as spiral vase rises + // through each layer. Both paths below pick it alike, as lower_bound does. + const double z = point.z() - z_tolerance; // Extrusions mostly follow each other on one island. if (last) { const Object &o = objects[last->object]; - if (point.z() <= o.print_zs[last->layer] + z_tolerance && - (last->layer == 0 || point.z() > o.print_zs[last->layer - 1] + z_tolerance) && + if (!(o.print_zs[last->layer] < z) && (last->layer == 0 || o.print_zs[last->layer - 1] < z) && o.islands[last->layer].holds(last->island, local(last->object, last->instance), o.crowded[last->instance])) return location(*last); } @@ -2720,10 +2723,10 @@ static void set_mass_locator(GCodeProcessor &processor, const Print &print) double distance = std::numeric_limits::max(); for (size_t object = 0; object < objects.size(); ++object) { const Object &o = objects[object]; - const auto z = std::lower_bound(o.print_zs.begin(), o.print_zs.end(), point.z() - z_tolerance); - if (z == o.print_zs.end()) + const auto above = std::lower_bound(o.print_zs.begin(), o.print_zs.end(), z); + if (above == o.print_zs.end()) continue; - const size_t layer = size_t(z - o.print_zs.begin()); + const size_t layer = size_t(above - o.print_zs.begin()); for (size_t instance = 0; instance < o.object->instances().size(); ++instance) { const auto [island, d] = o.islands[layer].find(local(object, instance), o.crowded[instance]); if (island < 0) diff --git a/tests/libslic3r/test_connected_bodies.cpp b/tests/libslic3r/test_connected_bodies.cpp index b4e897ffe3..56e604e46d 100644 --- a/tests/libslic3r/test_connected_bodies.cpp +++ b/tests/libslic3r/test_connected_bodies.cpp @@ -23,6 +23,21 @@ namespace { ExPolygon rectangle(double x, double width) { return ExPolygon(Polygon::new_scale({ { x, 0. }, { x + width, 0. }, { x + width, 10. }, { x, 10. } })); } +// A ring from 0 to 30 mm with a hole from 5 to 25 mm, an island from 10 to 20 mm in that hole, and one from 40 to 50 mm alone. +ExPolygons ring_inner_alone() +{ + const auto square = [](double from, double to) { + return Polygon::new_scale({ { from, from }, { to, from }, { to, to }, { from, to } }); + }; + Polygon hole = square(5., 25.); + hole.make_clockwise(); + ExPolygon ring(square(0., 30.)); + ring.holes.emplace_back(hole); + return { ring, ExPolygon(square(10., 20.)), ExPolygon(square(40., 50.)) }; +} + +Point at(double x, double y) { return Point::new_scale(x, y); } + } // namespace TEST_CASE("Islands overlapping their neighbors' make one body", "[ConnectedBodies]") @@ -48,17 +63,8 @@ TEST_CASE("Islands overlapping their neighbors' make one body", "[ConnectedBodie TEST_CASE("The island locator tests the outlines only where boxes overlap", "[ConnectedBodies]") { - const auto square = [](double from, double to) { - return Polygon::new_scale({ { from, from }, { to, from }, { to, to }, { from, to } }); - }; - Polygon hole = square(5., 25.); - hole.make_clockwise(); - ExPolygon ring(square(0., 30.)); - ring.holes.emplace_back(hole); - ExPolygon alone(square(40., 50.)); - const ExPolygons islands = { ring, ExPolygon(square(10., 20.)), alone }; + const ExPolygons islands = ring_inner_alone(); const IslandLocator locator(islands, scaled(1.)); - const auto at = [](double x, double y) { return Point::new_scale(x, y); }; CHECK(locator.find(at(2., 2.)).first == 0); CHECK(locator.find(at(15., 15.)).first == 1); @@ -77,6 +83,23 @@ TEST_CASE("The island locator tests the outlines only where boxes overlap", "[Co CHECK(locator.find(at(35., 45.)).first == -1); } +TEST_CASE("Islands not told apart are found by their boxes", "[ConnectedBodies]") +{ + const ExPolygons islands = ring_inner_alone(); + const IslandLocator locator(islands, scaled(1.), false); + + // In the ring's hole, outside the island within it, the ring's box holds the point. + CHECK(locator.holds(0, at(7., 15.))); + const auto [island, distance] = locator.find(at(7., 15.)); + CHECK(island == 0); + CHECK_THAT(distance, WithinAbs(0., 0.)); + // A strict call still tests the outline: the ring is only the nearest, 2 mm away. + CHECK_FALSE(locator.holds(0, at(7., 15.), true)); + const auto [nearest, strict_distance] = locator.find(at(7., 15.), true); + CHECK(nearest == 0); + CHECK_THAT(strict_distance, WithinRel(sqr(scaled(2.)), 1e-6)); +} + TEST_CASE("Separate solids are separate bodies", "[ConnectedBodies]") { const indexed_triangle_set cube = its_make_cube(10., 10., 10.);