From 9cac904f971d28599b5cf01356f91164b88b34d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Hejl?= Date: Tue, 16 Nov 2021 15:13:12 +0100 Subject: [PATCH] Fix of #7299 (Crash in the multi-material segmentation when some projected triangle is outside the bounding box of the current layer. --- src/libslic3r/MultiMaterialSegmentation.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/libslic3r/MultiMaterialSegmentation.cpp b/src/libslic3r/MultiMaterialSegmentation.cpp index 7041f7b825..737514901e 100644 --- a/src/libslic3r/MultiMaterialSegmentation.cpp +++ b/src/libslic3r/MultiMaterialSegmentation.cpp @@ -1723,10 +1723,21 @@ std::vector> multi_material_segmentation_by_painting(con }); // end of parallel_for BOOST_LOG_TRIVIAL(debug) << "MMU segmentation - slices preparation in parallel - end"; + std::vector layer_bboxes(num_layers); for (size_t layer_idx = 0; layer_idx < num_layers; ++layer_idx) { throw_on_cancel_callback(); - BoundingBox bbox(get_extents(layers[layer_idx]->regions())); - bbox.merge(get_extents(input_expolygons[layer_idx])); + layer_bboxes[layer_idx] = get_extents(layers[layer_idx]->regions()); + layer_bboxes[layer_idx].merge(get_extents(input_expolygons[layer_idx])); + } + + for (size_t layer_idx = 0; layer_idx < num_layers; ++layer_idx) { + throw_on_cancel_callback(); + BoundingBox bbox = layer_bboxes[layer_idx]; + // Projected triangles could, in rare cases (as in GH issue #7299), belongs to polygons printed in the previous or the next layer. + // Let's merge the bounding box of the current layer with bounding boxes of the previous and the next layer to ensure that + // every projected triangle will be inside the resulting bounding box. + if (layer_idx > 1) bbox.merge(layer_bboxes[layer_idx - 1]); + if (layer_idx < num_layers - 1) bbox.merge(layer_bboxes[layer_idx + 1]); // Projected triangles may slightly exceed the input polygons. bbox.offset(20 * SCALED_EPSILON); edge_grids[layer_idx].set_bbox(bbox);