Skip to content

Commit

Permalink
Fix of prusa3d#8472: The middle perimeter was missing for some specif…
Browse files Browse the repository at this point in the history
…ic configurations due to numeric rounding errors.
  • Loading branch information
hejllukas committed Aug 2, 2022
1 parent 756eaee commit 0161a59
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ DistributedBeadingStrategy::DistributedBeadingStrategy(const coord_t optimal_wid
name = "DistributedBeadingStrategy";
}

DistributedBeadingStrategy::Beading DistributedBeadingStrategy::compute(coord_t thickness, coord_t bead_count) const
DistributedBeadingStrategy::Beading DistributedBeadingStrategy::compute(const coord_t thickness, const coord_t bead_count) const
{
Beading ret;

Expand All @@ -40,18 +40,24 @@ DistributedBeadingStrategy::Beading DistributedBeadingStrategy::compute(coord_t
for (coord_t bead_idx = 0; bead_idx < bead_count; bead_idx++)
weights[bead_idx] = getWeight(bead_idx);

const float total_weight = std::accumulate(weights.cbegin(), weights.cend(), 0.f);
const float total_weight = std::accumulate(weights.cbegin(), weights.cend(), 0.f);
coord_t accumulated_width = 0;
for (coord_t bead_idx = 0; bead_idx < bead_count; bead_idx++) {
const float weight_fraction = weights[bead_idx] / total_weight;
const float weight_fraction = weights[bead_idx] / total_weight;
const coord_t splitup_left_over_weight = to_be_divided * weight_fraction;
const coord_t width = optimal_width + splitup_left_over_weight;
const coord_t width = (bead_idx == bead_count - 1) ? thickness - accumulated_width : optimal_width + splitup_left_over_weight;

// Be aware that toolpath_locations is computed by dividing the width by 2, so toolpath_locations
// could be off by 1 because of rounding errors.
if (bead_idx == 0)
ret.toolpath_locations.emplace_back(width / 2);
else
ret.toolpath_locations.emplace_back(ret.toolpath_locations.back() + (ret.bead_widths.back() + width) / 2);
ret.bead_widths.emplace_back(width);
accumulated_width += width;
}
ret.left_over = 0;
assert((accumulated_width + ret.left_over) == thickness);
} else if (bead_count == 2) {
const coord_t outer_width = thickness / 2;
ret.bead_widths.emplace_back(outer_width);
Expand All @@ -68,6 +74,13 @@ DistributedBeadingStrategy::Beading DistributedBeadingStrategy::compute(coord_t
ret.left_over = thickness;
}

assert(([&ret = std::as_const(ret), thickness]() -> bool {
coord_t total_bead_width = 0;
for (const coord_t &bead_width : ret.bead_widths)
total_bead_width += bead_width;
return (total_bead_width + ret.left_over) == thickness;
}()));

return ret;
}

Expand Down
7 changes: 5 additions & 2 deletions src/libslic3r/Arachne/SkeletalTrapezoidation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ void SkeletalTrapezoidation::constructFromPolygons(const Polygons& polys)
#endif

#ifdef ARACHNE_DEBUG
assert(is_voronoi_diagram_planar_intersection(voronoi_diagram));
assert(Geometry::VoronoiUtilsCgal::is_voronoi_diagram_planar_intersection(voronoi_diagram));
#endif

// Try to detect cases when some Voronoi vertex is missing and when
Expand Down Expand Up @@ -1911,7 +1911,10 @@ void SkeletalTrapezoidation::generateJunctions(ptr_vector_t<BeadingPropagation>&
for (junction_idx = (std::max(size_t(1), beading->toolpath_locations.size()) - 1) / 2; junction_idx < num_junctions; junction_idx--)
{
coord_t bead_R = beading->toolpath_locations[junction_idx];
if (bead_R <= start_R)
// toolpath_locations computed inside DistributedBeadingStrategy be off by 1 because of rounding errors.
// In GH issue #8472, these roundings errors caused missing the middle extrusion.
// Adding some epsilon should help resolve those cases.
if (bead_R <= start_R + scaled<coord_t>(0.005))
{ // Junction coinciding with start node is used in this function call
break;
}
Expand Down
29 changes: 29 additions & 0 deletions tests/libslic3r/test_arachne.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,32 @@ TEST_CASE("Arachne - Closed ExtrusionLine", "[ArachneClosedExtrusionLine]") {
}
}

// This test case was distilled from GitHub issue #8472.
// Where for wall_distribution_count == 3 sometime middle perimeter was missing.
TEST_CASE("Arachne - Missing perimeter - #8472", "[ArachneMissingPerimeter8472]") {
Polygon poly = {
Point(-9000000, 8054793),
Point( 7000000, 8054793),
Point( 7000000, 10211874),
Point(-8700000, 10211874),
Point(-9000000, 9824444)
};

Polygons polygons = {poly};
coord_t spacing = 437079;
coord_t inset_count = 3;

PrintObjectConfig print_object_config = PrintObjectConfig::defaults();
print_object_config.wall_distribution_count.setInt(3);

Arachne::WallToolPaths wallToolPaths(polygons, spacing, spacing, inset_count, 0, print_object_config, PrintConfig::defaults());
wallToolPaths.generate();
std::vector<Arachne::VariableWidthLines> perimeters = wallToolPaths.getToolPaths();

#ifdef ARACHNE_DEBUG_OUT
draw_extrusion(debug_out_path("arachne-missing-perimeter-8472.svg"), polygons, perimeters, union_ex(wallToolPaths.getInnerContour()));
#endif

REQUIRE(perimeters.size() == 3);
}

0 comments on commit 0161a59

Please sign in to comment.