diff --git a/src/libslic3r/GCode/ToolOrdering.cpp b/src/libslic3r/GCode/ToolOrdering.cpp index dc272e9464..e5c3260084 100644 --- a/src/libslic3r/GCode/ToolOrdering.cpp +++ b/src/libslic3r/GCode/ToolOrdering.cpp @@ -25,14 +25,20 @@ const static bool g_wipe_into_objects = false; // Shortest hamilton path problem -static std::vector solve_extruder_order(const std::vector>& wipe_volumes, std::vector all_extruders, unsigned int start_extruder_id) +static std::vector solve_extruder_order(const std::vector>& wipe_volumes, std::vector all_extruders, std::optional start_extruder_id) { - auto start_iter = std::find(all_extruders.begin(), all_extruders.end(), start_extruder_id); bool add_start_extruder_flag = false; - if (start_iter == all_extruders.end()) - all_extruders.insert(all_extruders.begin(), start_extruder_id), add_start_extruder_flag = true; - else - std::swap(*all_extruders.begin(), *start_iter); + + if (start_extruder_id) { + auto start_iter = std::find(all_extruders.begin(), all_extruders.end(), start_extruder_id); + if (start_iter == all_extruders.end()) + all_extruders.insert(all_extruders.begin(), *start_extruder_id), add_start_extruder_flag = true; + else + std::swap(*all_extruders.begin(), *start_iter); + } + else { + *start_extruder_id = all_extruders.front(); + } unsigned int iterations = (1 << all_extruders.size()); unsigned int final_state = iterations - 1; @@ -84,7 +90,7 @@ static std::vector solve_extruder_order(const std::vector get_extruders_order(const std::vector> &wipe_volumes, std::vector all_extruders, unsigned int start_extruder_id) +std::vector get_extruders_order(const std::vector> &wipe_volumes, std::vector all_extruders, std::optionalstart_extruder_id) { #define USE_DP_OPTIMIZE #ifdef USE_DP_OPTIMIZE @@ -123,7 +129,6 @@ if (all_extruders.size() > 1) { #endif // OPTIMIZE } - // Returns true in case that extruder a comes before b (b does not have to be present). False otherwise. bool LayerTools::is_extruder_order(unsigned int a, unsigned int b) const { @@ -837,11 +842,13 @@ void ToolOrdering::reorder_extruders_for_minimum_flush_volume() for (unsigned int i = 0; i < number_of_extruders; ++i) wipe_volumes.push_back(std::vector(number_of_extruders, print_config->prime_volume)); } - - auto extruders_to_hash_key = [](const std::vector& extruders, unsigned int initial_extruder_id)->uint32_t { + + auto extruders_to_hash_key = [](const std::vector& extruders, + std::optional initial_extruder_id) -> uint32_t { uint32_t hash_key = 0; // high 16 bit define initial extruder ,low 16 bit define extruder set - hash_key |= (1 << (16 + initial_extruder_id)); + if (initial_extruder_id) + hash_key |= (1 << (16 + *initial_extruder_id)); for (auto item : extruders) hash_key |= (1 << item); return hash_key; @@ -868,7 +875,7 @@ void ToolOrdering::reorder_extruders_for_minimum_flush_volume() return false; }; - unsigned int current_extruder_id = -1; + std::optionalcurrent_extruder_id; for (int i = 0; i < m_layer_tools.size(); ++i) { LayerTools& lt = m_layer_tools[i]; if (lt.extruders.empty())