fix: slice the same model to the same lightning infill every time (#15311)

This commit is contained in:
Kris Austin
2026-08-21 20:24:39 -05:00
committed by GitHub
parent 90f76fa28c
commit 4b397fc2cc
3 changed files with 45 additions and 6 deletions

View File

@@ -755,6 +755,9 @@ struct SparseInfillShape {
size_t sharp_turns { 0 };
size_t path_count { 0 };
double length { 0. };
// Digest of every point in the order it is printed. The counts above all survive the same
// extrusions being joined into different polylines, so only this tells two such fills apart.
uint64_t sequence { 14695981039346656037ull };
};
static SparseInfillShape sparse_infill_shape(const Print &print)
@@ -767,6 +770,9 @@ static SparseInfillShape sparse_infill_shape(const Print &print)
const Points3 &pts = path.polyline.points;
++shape.path_count;
shape.point_count += pts.size();
for (const auto &pt : pts)
for (const coord_t coordinate : {pt.x(), pt.y(), pt.z()})
shape.sequence = (shape.sequence ^ uint64_t(coordinate)) * 1099511628211ull;
for (size_t i = 1; i < pts.size(); ++i)
shape.length += (pts[i] - pts[i - 1]).head<2>().cast<double>().norm();
for (size_t i = 1; i + 1 < pts.size(); ++i) {
@@ -793,6 +799,33 @@ static SparseInfillShape sparse_infill_shape(const Print &print)
return shape;
}
TEST_CASE("Lightning infill slices the same model the same way twice", "[Fill][Regression]")
{
// Slicing twice in one process catches a generator that carries state from one slice to the
// next, or whose result depends on how the parallel layer fill interleaves.
auto shape = [] {
Print print;
Slic3r::Test::init_and_process_print({Slic3r::Test::cube(20)}, print,
{{"sparse_infill_pattern", "lightning"},
{"sparse_infill_density", "50%"},
{"layer_height", 0.2}});
return sparse_infill_shape(print);
};
const SparseInfillShape first = shape();
const SparseInfillShape second = shape();
REQUIRE(first.path_count > 0);
REQUIRE(second.path_count == first.path_count);
REQUIRE(second.point_count == first.point_count);
REQUIRE(second.sharp_turns == first.sharp_turns);
// No tolerance: the same extrusions in the same order add up to the very same number.
REQUIRE_THAT(second.length, Catch::Matchers::WithinAbs(first.length, 0.));
// All of the above agree when the same branches are joined into different polylines, so the
// point sequence is what actually decides whether the two slices produced the same infill.
REQUIRE(second.sequence == first.sequence);
}
TEST_CASE("Lightning infill rounds the turns of its branches with the smooth factor", "[Fill]")
{
auto shape_for = [](const std::string &smooth_factor) {