Files
OrcaSlicer/tests/fff_print/test_tree_support.cpp
Hanif Koh 87cf1429ca Keep Every Overhang That Joins a Tree Support Cluster on the Same Layer
An overhang cluster stored one overhang per layer, so a second overhang
joining the cluster on a layer it already had was dropped and got no tree
support. Store every overhang of the layer, as BambuStudio does since
adb3fbcfc.
2026-11-14 16:06:46 +08:00

342 lines
15 KiB
C++

#include <catch2/catch_all.hpp>
#include <algorithm>
#include <initializer_list>
#include "libslic3r/Config.hpp"
#include "libslic3r/PrintConfig.hpp"
#include "libslic3r/Point.hpp"
#include "libslic3r/Polygon.hpp"
#include "libslic3r/ExPolygon.hpp"
#include "libslic3r/ClipperUtils.hpp"
#include <cstddef>
#include "libslic3r/libslic3r.h"
#include <catch2/catch_test_macros.hpp>
#include <catch2/catch_message.hpp>
#include <catch2/generators/catch_generators.hpp>
#include "libslic3r/Layer.hpp"
#include "libslic3r/TriangleMesh.hpp"
#include "test_helpers.hpp"
#include "libslic3r/ExtrusionEntity.hpp"
#include "libslic3r/ExtrusionEntityCollection.hpp"
#include "libslic3r/Print.hpp"
using namespace Slic3r::Test;
using namespace Slic3r;
namespace {
// The upper plate overhangs both the lower plate and open air, so branches land on the model and on
// the bed in the same slice.
TriangleMesh two_tier_mesh()
{
TriangleMesh lower = make_cube(30, 30, 3);
TriangleMesh column = make_cube(8, 8, 15);
TriangleMesh upper = make_cube(50, 50, 3);
// Each part overlaps the one below rather than resting on it; a coplanar join slices ambiguously.
column.translate(11.f, 11.f, 2.f);
upper.translate(-10.f, -10.f, 16.f);
TriangleMesh mesh = lower;
mesh.merge(column);
mesh.merge(upper);
return mesh;
}
// A slab on a column overhangs it on one side. One layer higher, two wings leave the slab's other
// two sides, so both wings are new overhangs on the same layer, next to the slab's overhang.
TriangleMesh slab_with_two_wings_mesh()
{
TriangleMesh column = make_cube(10, 10, 5.1);
TriangleMesh slab = make_cube(14, 10, 5);
TriangleMesh front = make_cube(4, 6.1, 4.8);
TriangleMesh back = make_cube(4, 6.1, 4.8);
// Each part overlaps the one it joins rather than resting on it; a coplanar join slices ambiguously.
slab.translate(0.f, 0.f, 5.f);
front.translate(10.f, -6.f, 5.2f);
back.translate(10.f, 9.9f, 5.2f);
TriangleMesh mesh = column;
mesh.merge(slab);
mesh.merge(front);
mesh.merge(back);
return mesh;
}
TriangleMesh scaled(TestMesh id, float scale)
{
TriangleMesh mesh = Slic3r::Test::mesh(id);
mesh.scale(scale);
return mesh;
}
// `extra` is applied last, so a caller can add or override any key.
void slice_with_tree_support(const TriangleMesh &mesh, Slic3r::Print &print, const char *style,
int threshold_angle = 30, int build_plate_only = 0, int raft_layers = 0,
std::initializer_list<Slic3r::ConfigBase::SetDeserializeItem> extra = {})
{
DynamicPrintConfig config = DynamicPrintConfig::full_print_config();
config.set_deserialize_strict({
{ "enable_support", 1 },
{ "support_type", "tree(auto)" },
{ "support_style", style },
{ "support_on_build_plate_only", build_plate_only },
{ "support_threshold_angle", threshold_angle },
{ "raft_layers", raft_layers },
{ "layer_height", 0.2 },
});
config.set_deserialize_strict(extra);
Slic3r::Test::init_and_process_print({ mesh }, print, config);
}
Points support_points(const Slic3r::Print &print)
{
Points points;
for (const SupportLayer *layer : print.objects().front()->support_layers())
layer->support_fills.collect_points(points);
return points;
}
size_t support_point_count(const TriangleMesh &mesh, const char *style, int threshold_angle = 30,
int build_plate_only = 0)
{
Slic3r::Print print;
slice_with_tree_support(mesh, print, style, threshold_angle, build_plate_only);
return support_points(print).size();
}
// Index of the first differing point, or the common length when they match. An index keeps a
// failure readable; comparing the vectors themselves dumps thousands of points.
size_t first_difference(const Points &a, const Points &b)
{
const size_t common = std::min(a.size(), b.size());
for (size_t i = 0; i < common; ++i)
if (a[i] != b[i])
return i;
return common;
}
// Slice `mesh` twice and require an identical support point sequence. Point counts and total
// length are order insensitive, so the sequence is what a reordering shows up in.
void sliced_twice_matches(const TriangleMesh &mesh, int build_plate_only, const char *style = "tree_slim",
std::initializer_list<Slic3r::ConfigBase::SetDeserializeItem> extra = {})
{
Slic3r::Print first_print, second_print;
slice_with_tree_support(mesh, first_print, style, 30, build_plate_only, 0, extra);
slice_with_tree_support(mesh, second_print, style, 30, build_plate_only, 0, extra);
const Points first = support_points(first_print);
const Points second = support_points(second_print);
REQUIRE(first.size() > 1000); // without support the comparison below passes vacuously
REQUIRE(second.size() == first.size());
REQUIRE(first_difference(first, second) == first.size());
}
} // namespace
TEST_CASE("Tree support is generated for an overhang and not for a plain cube", "[TreeSupport]")
{
REQUIRE(support_point_count(scaled(TestMesh::overhang, 2.f), "tree_slim") > 1000);
REQUIRE(support_point_count(Slic3r::Test::cube(20), "tree_slim") == 0);
}
TEST_CASE("Restricting tree support to the build plate changes what is generated", "[TreeSupport]")
{
const TriangleMesh mesh = two_tier_mesh();
const size_t anywhere = support_point_count(mesh, "tree_slim", 30, 0);
const size_t plate_only = support_point_count(mesh, "tree_slim", 30, 1);
REQUIRE(anywhere > 1000);
REQUIRE(plate_only > 1000);
// The upper plate overhangs the lower one, so some branches would land on the model.
REQUIRE(plate_only != anywhere);
}
TEST_CASE("Tree support layers rise monotonically within the layer height limits", "[TreeSupport]")
{
Slic3r::Print print;
slice_with_tree_support(scaled(TestMesh::overhang, 2.f), print, "tree_slim");
const double nozzle = print.config().nozzle_diameter.values.front();
size_t checked = 0;
double previous = 0;
bool previous_was_adjacent = false;
for (const SupportLayer *layer : print.objects().front()->support_layers()) {
if (layer->print_z <= 0 || layer->height <= 0) {
// Layers with no nodes are left at zero. Skipping one leaves a hole, so the next pair
// spans more than one layer and its gap says nothing about the layer height limit.
previous_was_adjacent = false;
continue;
}
if (previous > 0) {
CAPTURE(previous, layer->print_z);
REQUIRE(layer->print_z > previous);
if (previous_was_adjacent)
REQUIRE(layer->print_z - previous <= nozzle + EPSILON);
}
previous = layer->print_z;
previous_was_adjacent = true;
++checked;
}
REQUIRE(checked > 10);
}
TEST_CASE("A raft is still generated under tree support", "[TreeSupport]")
{
// The mesh supports itself, so a layer count alone passes with no raft at all.
Slic3r::Print rafted, unrafted;
slice_with_tree_support(scaled(TestMesh::overhang, 2.f), rafted, "tree_slim", 30, 0, 3);
slice_with_tree_support(scaled(TestMesh::overhang, 2.f), unrafted, "tree_slim", 30, 0, 0);
const PrintObject *rafted_object = rafted.objects().front();
const PrintObject *unrafted_object = unrafted.objects().front();
REQUIRE(rafted_object->support_layers().size() > unrafted_object->support_layers().size());
// The raft goes under the object.
REQUIRE(rafted_object->layers().front()->print_z > unrafted_object->layers().front()->print_z);
}
// drop_nodes() decides the node merges and spawns the next layer's nodes in parallel. Every one of
// those decisions has to be applied in a fixed order, or the same model gives different branches on
// each slice.
TEST_CASE("Tree support toolpaths do not depend on thread scheduling", "[TreeSupport][Regression]")
{
// Scaled up so that a layer holds enough nodes for the parallel range to be split. At stock
// size it stays in one chunk and the order never varies.
SECTION("overhang") { sliced_twice_matches(scaled(TestMesh::overhang, 2.f), 0); }
SECTION("bridge with hole") { sliced_twice_matches(scaled(TestMesh::bridge_with_hole, 3.f), 0); }
// Dropping every branch that cannot reach the bed leaves the survivors dense enough that the
// neighbour merge fires in bulk.
SECTION("on the build plate") { sliced_twice_matches(scaled(TestMesh::overhang, 4.f), 1); }
// Branches resting on the model are what put nodes in a part group other than 0, which is the
// only way to reach the prune in the second pass. tree_hybrid additionally builds polygon
// nodes, so it is the only style that exercises the overhang merge.
SECTION("resting on the model") { sliced_twice_matches(two_tier_mesh(), 0); }
SECTION("hybrid on the model") { sliced_twice_matches(two_tier_mesh(), 0, "tree_hybrid"); }
}
// Prim breaks equal-distance ties by heap address. A 1 mm branch diameter puts neighbours close
// enough to tie, and an explicit line width pins max_move_dist, so the moved tie winner reaches
// the support toolpaths.
TEST_CASE("Tree support toolpaths do not depend on the MST tie order", "[TreeSupport][Regression]")
{
sliced_twice_matches(two_tier_mesh(), 0, "tree_hybrid", {
{ "tree_support_branch_diameter", 1.0 },
{ "tree_support_branch_distance", 5.0 },
{ "tree_support_branch_angle", 40 },
{ "support_line_width", 0.4 },
});
}
// Branches landing on the lower plate get the bottom gap and then every bottom interface layer asked for. The contact
// search stops at the gap and interface height below each layer, so it must still reach the plate from the highest
// interface layer, and the layers above must not get an interface.
TEST_CASE("Tree support resting on the model gets every bottom interface layer above the gap", "[TreeSupport]")
{
const int interface_layers = 2;
const double lower_top = 3., upper_bottom = 16.; // two_tier_mesh()
Slic3r::Print print;
slice_with_tree_support(two_tier_mesh(), print, "tree_slim", 30, 0, 0, {
{ "support_interface_top_layers", interface_layers },
{ "support_interface_bottom_layers", interface_layers },
{ "support_bottom_z_distance", 0.2 },
});
int above_plate = 0, between_plates = 0;
for (const SupportLayer *layer : print.objects().front()->support_layers()) {
bool has_interface = false;
for (const ExtrusionEntity *entity : layer->support_fills.flatten().entities)
has_interface |= entity->role() == erSupportMaterialInterface;
if (! has_interface)
continue;
if (layer->print_z > lower_top && layer->print_z < lower_top + 1.5)
++above_plate;
else if (layer->print_z < upper_bottom - 1.5)
++between_plates;
}
CHECK(above_plate == interface_layers);
CHECK(between_plates == 0);
}
// Each node under an overhang adds the whole overhang to its roof list, so the top interface is a solid sheet rather
// than the branch circles alone. Counting interface layers would not notice the sheet missing.
TEST_CASE("Tree support top interface covers the whole overhang", "[TreeSupport]")
{
const double upper_bottom = 16.; // two_tier_mesh()
Slic3r::Print print;
slice_with_tree_support(two_tier_mesh(), print, "tree_slim", 30, 0, 0, {
// The test print is centred on the origin, and tree support drops nodes off the bed.
{ "printable_area", "-100x-100,100x-100,100x100,-100x100" },
{ "support_interface_top_layers", 2 },
{ "support_interface_spacing", 0 },
});
const PrintObject &object = *print.objects().front();
// The underside of the upper plate, less the column and a margin for the xy distance kept around it.
ExPolygons underside, column;
for (const Layer *layer : object.layers())
if (layer->print_z < upper_bottom)
column = layer->lslices;
else if (underside.empty())
underside = layer->lslices;
const ExPolygons overhang = diff_ex(underside, offset_ex(column, scaled<float>(2.)));
REQUIRE(area(overhang) > 0);
const SupportLayer *top_interface = nullptr;
for (const SupportLayer *layer : object.support_layers())
if (layer->print_z < upper_bottom)
for (const ExtrusionEntity *entity : layer->support_fills.flatten().entities)
if (entity->role() == erSupportMaterialInterface)
top_interface = layer;
REQUIRE(top_interface != nullptr);
Polygons covered;
for (const ExtrusionEntity *entity : top_interface->support_fills.flatten().entities)
if (entity->role() == erSupportMaterialInterface)
entity->polygons_covered_by_width(covered, scaled<float>(0.05));
const double uncovered = area(diff_ex(overhang, covered)) / area(overhang);
INFO("uncovered fraction " << uncovered);
CHECK(uncovered < 0.02);
}
// Both wings join the cluster of the slab's overhang on the same layer, and each must still get its own support.
TEST_CASE("Tree support reaches every overhang that joins a cluster on the same layer", "[TreeSupport][Regression]")
{
const char *style = GENERATE("tree_hybrid", "organic");
CAPTURE(style);
Slic3r::Print print;
slice_with_tree_support(slab_with_two_wings_mesh(), print, style, 30, 0, 0, {
// The test print is centred on the origin, and tree support drops nodes off the bed.
{ "printable_area", "-100x-100,100x-100,100x100,-100x100" },
{ "initial_layer_print_height", 0.2 },
// Overhangs join a cluster within one line width, and the default of 0 never joins them.
{ "line_width", 0.45 },
});
const PrintObject *object = print.objects().front();
// The slab is 10 mm deep and centred between the wings, which reach 6 mm past it on each side.
const BoundingBox top = get_extents(object->layers().back()->lslices);
const coord_t wing_depth = (top.size().y() - scaled<coord_t>(10.)) / 2;
REQUIRE(wing_depth > scaled<coord_t>(5.));
// Points more than 1 mm past the slab's edge are under a wing, not under the slab's own overhang.
const coord_t front_limit = top.min.y() + wing_depth - scaled<coord_t>(1.);
const coord_t back_limit = top.max.y() - wing_depth + scaled<coord_t>(1.);
// The wings start at 5.2 mm, so their contact is in the support layers just below.
size_t under_front = 0;
size_t under_back = 0;
for (const SupportLayer *layer : object->support_layers()) {
if (layer->print_z < 4. || layer->print_z > 5.2 + EPSILON)
continue;
Points points;
layer->support_fills.collect_points(points);
for (const Point &pt : points) {
if (pt.y() < front_limit)
++under_front;
else if (pt.y() > back_limit)
++under_back;
}
}
// The wings mirror each other, so each gets a similar amount. Branches spreading from the slab
// put a little under a wing even when the wing itself has no contact.
CAPTURE(under_front, under_back);
REQUIRE(under_back > 0);
REQUIRE(2 * under_front > under_back);
REQUIRE(2 * under_back > under_front);
}