Files
OrcaSlicer/tests/fff_print/test_tree_support.cpp
T
Ian Bassi 3b9face0f6 Grow every tree support raft layer by the first layer expansion once
Slim, Strong and Hybrid tree supports size their raft by the first
layer expansion, and the GUI greys out the raft expansion for them. The
fixed 3 mm grip from BambuStudio broke that: the field no longer did
what its text says, 0 no longer gave a flush raft, and a one layer raft,
which is an interface layer only, ignored the setting. Grow every raft
layer by the setting again, and drop only the second expansion of the
first layer that made it twice as wide.
2026-11-30 14:11:14 -03:00

454 lines
21 KiB
C++

#include <catch2/catch_all.hpp>
#include <algorithm>
#include <initializer_list>
#include <utility>
#include <vector>
#include "libslic3r/Config.hpp"
#include "libslic3r/PrintConfig.hpp"
#include "libslic3r/BoundingBox.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/matchers/catch_matchers.hpp>
#include <catch2/matchers/catch_matchers_floating_point.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/Model.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);
}
// A plate on a column overhangs a slab that covers the whole bed, so support could only land on the slab.
TEST_CASE("Tree support on the build plate only leaves out branches that land on the first layer of the model", "[TreeSupport][Regression]")
{
const double slab_height = GENERATE(0.2, 0.6);
CAPTURE(slab_height);
TriangleMesh mesh = make_cube(80, 80, slab_height);
TriangleMesh column = make_cube(6, 6, 5.1 - slab_height);
TriangleMesh plate = make_cube(30, 30, 1);
// Each part overlaps the one below rather than resting on it; a coplanar join slices ambiguously.
column.translate(37.f, 37.f, float(slab_height) - 0.05f);
plate.translate(25.f, 25.f, 5.f);
mesh.merge(column);
mesh.merge(plate);
Slic3r::Print print;
slice_with_tree_support(mesh, print, "tree_slim", 30, 1);
CHECK(support_points(print).empty());
}
TEST_CASE("A one layer raft under tree support is one first layer below the object", "[TreeSupport][Regression]")
{
Slic3r::Print print;
slice_with_tree_support(make_cube(20, 20, 5), print, "tree_hybrid", 30, 0, 1);
const PrintObject &object = *print.objects().front();
const Layer &first = *object.layers().front();
const auto &layers = object.support_layers();
REQUIRE_FALSE(layers.empty());
CHECK_THAT(layers.front()->print_z, Catch::Matchers::WithinAbs(print.config().initial_layer_print_height.value, EPSILON));
CHECK_FALSE(layers.front()->support_fills.entities.empty());
// The raft and the gap above it end where the object's first layer starts.
for (const SupportLayer *layer : layers)
if (layer->print_z < first.print_z + EPSILON)
CHECK(layer->print_z < first.print_z - first.height + EPSILON);
}
// Slim, Strong and Hybrid size their raft by the first layer expansion instead of the raft expansion.
TEST_CASE("The first layer expansion widens every layer of a tree support raft once", "[TreeSupport][Regression]")
{
// With 4 raft layers, the first two are raft base layers. A one layer raft is an interface layer only.
const int raft_layers = GENERATE(1, 4);
CAPTURE(raft_layers);
auto raft_widths = [raft_layers](double expansion) {
Slic3r::Print print;
slice_with_tree_support(make_cube(20, 20, 5), print, "tree_hybrid", 30, 0, raft_layers, { { "raft_first_layer_expansion", expansion } });
const auto &layers = print.objects().front()->support_layers();
REQUIRE(layers.size() > 1);
// Along its fill lines, which end at the raft's edge. Across them, sparse lines stop short of it.
auto width = [&layers](size_t idx) {
Points points;
layers[idx]->support_fills.collect_points(points);
const Point size = BoundingBox(points).size();
return unscaled(std::max(size.x(), size.y()));
};
return std::make_pair(width(0), raft_layers > 1 ? width(1) : 0.);
};
const auto [first_narrow, second_narrow] = raft_widths(2.);
const auto [first_wide, second_wide] = raft_widths(6.);
// 4 mm more on each side.
CHECK_THAT(first_wide - first_narrow, Catch::Matchers::WithinAbs(8., 0.5));
if (raft_layers > 1)
CHECK_THAT(second_wide - second_narrow, Catch::Matchers::WithinAbs(8., 0.5));
}
// 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);
}
// Hybrid tree support grows its first layer by raft_first_layer_expansion after the support was clipped to
// the bed, so the grown footprint must be clipped again.
TEST_CASE("Hybrid tree support first layer expansion stays on the bed", "[TreeSupport][Regression]")
{
const double bed_max_x = 56.;
DynamicPrintConfig config = DynamicPrintConfig::full_print_config();
config.set_deserialize_strict({
{ "printable_area", "0x0,56x0,56x100,0x100" },
{ "enable_support", 1 },
{ "support_type", "tree(auto)" },
{ "support_style", "tree_hybrid" },
{ "raft_layers", 0 },
{ "raft_first_layer_expansion", 5 },
{ "layer_height", 0.2 },
{ "initial_layer_print_height", 0.2 },
});
// A block with a ledge that overhangs to 2 mm from the bed's right edge, closer than the expansion.
TriangleMesh block = make_cube(20, 40, 8);
TriangleMesh ledge = make_cube(9.1, 40, 1.9);
block.translate(25.f, 30.f, 0.f);
// The ledge overlaps the block rather than touching it; a coplanar join slices ambiguously.
ledge.translate(44.9f, 30.f, 6.f);
block.merge(ledge);
Slic3r::Print print;
Slic3r::Model model;
Slic3r::Test::init_print(std::vector<TriangleMesh>{ std::move(block) }, print, model, config, nullptr, false);
print.process();
const PrintObject *object = print.objects().front();
REQUIRE(!object->support_layers().empty());
Points first_layer;
object->support_layers().front()->support_fills.collect_points(first_layer);
REQUIRE(!first_layer.empty());
const Point shift = object->instances().front().shift;
double max_x = 0.;
for (const Point &pt : first_layer)
max_x = std::max(max_x, unscale<double>(pt.x() + shift.x()));
// Without the expansion the support stops near the ledge's edge at 54 mm, so this shows the expansion ran.
REQUIRE(max_x > 55.);
CHECK(max_x <= bed_max_x);
}