From 6ddd88e596af04018ce199faf2542a0f9041fc59 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Hejl?= <hejl.lukas@gmail.com>
Date: Tue, 15 Jun 2021 16:12:26 +0200
Subject: [PATCH] Fixed an issue where the color of the first extruder always
 replaced the default color after painting in a multi-material gizmo.

multi_material_segmentation_by_painting is now returning only the painted region. Regions with default colors that aren't painted by multi-material gizmo aren't returned.
---
 src/libslic3r/MultiMaterialSegmentation.cpp | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/src/libslic3r/MultiMaterialSegmentation.cpp b/src/libslic3r/MultiMaterialSegmentation.cpp
index 211c23883..f943934b5 100644
--- a/src/libslic3r/MultiMaterialSegmentation.cpp
+++ b/src/libslic3r/MultiMaterialSegmentation.cpp
@@ -1385,16 +1385,19 @@ static std::vector<std::vector<std::pair<ExPolygon, size_t>>> merge_segmented_la
         for (size_t layer_idx = range.begin(); layer_idx < range.end(); ++layer_idx) {
             for (const std::pair<ExPolygon, size_t> &colored_expoly : segmented_regions[layer_idx]) {
                 throw_on_cancel_callback();
+                // Zero is the default color of the volume.
+                if(colored_expoly.second == 0)
+                    continue;
                 ExPolygons cut_colored_expoly = {colored_expoly.first};
                 for (const std::vector<ExPolygons> &top_and_bottom_layer : top_and_bottom_layers)
                     cut_colored_expoly = diff_ex(cut_colored_expoly, top_and_bottom_layer[layer_idx]);
                 for (ExPolygon &ex_poly : cut_colored_expoly)
-                    segmented_regions_merged[layer_idx].emplace_back(std::move(ex_poly), colored_expoly.second);
+                    segmented_regions_merged[layer_idx].emplace_back(std::move(ex_poly), colored_expoly.second - 1);
             }
 
-            for (size_t color_idx = 0; color_idx < top_and_bottom_layers.size(); ++color_idx)
+            for (size_t color_idx = 1; color_idx < top_and_bottom_layers.size(); ++color_idx)
                 for (ExPolygon &expoly : top_and_bottom_layers[color_idx][layer_idx])
-                    segmented_regions_merged[layer_idx].emplace_back(std::move(expoly), color_idx);
+                    segmented_regions_merged[layer_idx].emplace_back(std::move(expoly), color_idx - 1);
         }
     }); // end of parallel_for
     BOOST_LOG_TRIVIAL(debug) << "MMU segmentation - merging segmented layers in parallel - end";