mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-05-16 18:12:10 +00:00
ENH: Add recommend filament maps method
Change-Id: I3945a8b9f0a57e10a1d230003f21c9877cc5f342 (cherry picked from commit 8c8c9a967b032a270a60e6cf075fe41a6f329e1c)
This commit is contained in:
@@ -26,7 +26,7 @@ const static bool g_wipe_into_objects = false;
|
||||
|
||||
|
||||
// Shortest hamilton path problem
|
||||
static std::vector<unsigned int> solve_extruder_order(const std::vector<std::vector<float>>& wipe_volumes, std::vector<unsigned int> all_extruders, std::optional<unsigned int> start_extruder_id)
|
||||
static std::vector<unsigned int> solve_extruder_order(const std::vector<std::vector<float>>& wipe_volumes, std::vector<unsigned int> all_extruders, std::optional<unsigned int> start_extruder_id, float* min_cost)
|
||||
{
|
||||
bool add_start_extruder_flag = false;
|
||||
|
||||
@@ -70,6 +70,8 @@ static std::vector<unsigned int> solve_extruder_order(const std::vector<std::vec
|
||||
for (unsigned int dst = 0; dst < all_extruders.size(); ++dst) {
|
||||
if (all_extruders[dst] != start_extruder_id && cost > cache[final_state][dst]) {
|
||||
cost = cache[final_state][dst];
|
||||
if (min_cost)
|
||||
*min_cost = cost;
|
||||
final_dst = dst;
|
||||
}
|
||||
}
|
||||
@@ -91,11 +93,17 @@ static std::vector<unsigned int> solve_extruder_order(const std::vector<std::vec
|
||||
return path;
|
||||
}
|
||||
|
||||
std::vector<unsigned int> get_extruders_order(const std::vector<std::vector<float>> &wipe_volumes, std::vector<unsigned int> all_extruders, std::optional<unsigned int>start_extruder_id)
|
||||
std::vector<unsigned int> get_extruders_order(const std::vector<std::vector<float>> &wipe_volumes, std::vector<unsigned int> all_extruders, std::optional<unsigned int>start_extruder_id, float* cost = nullptr)
|
||||
{
|
||||
if (all_extruders.size() == 1) {
|
||||
if (cost)
|
||||
*cost = 0;
|
||||
return all_extruders;
|
||||
}
|
||||
|
||||
#define USE_DP_OPTIMIZE
|
||||
#ifdef USE_DP_OPTIMIZE
|
||||
return solve_extruder_order(wipe_volumes, all_extruders, start_extruder_id);
|
||||
return solve_extruder_order(wipe_volumes, all_extruders, start_extruder_id, cost);
|
||||
#else
|
||||
if (all_extruders.size() > 1) {
|
||||
int begin_index = 0;
|
||||
@@ -827,14 +835,217 @@ void ToolOrdering::collect_extruder_statistics(bool prime_multi_material)
|
||||
}
|
||||
}
|
||||
|
||||
void ToolOrdering::reorder_extruders_for_minimum_flush_volume()
|
||||
std::set<std::pair<std::vector<unsigned int>, std::vector<unsigned int>>> generate_combinations(const std::vector<unsigned int> &extruders)
|
||||
{
|
||||
bool is_multi_extruder = false;
|
||||
if (is_multi_extruder) {
|
||||
reorder_extruders_for_minimum_flush_volume_multi_extruder();
|
||||
return;
|
||||
int n = extruders.size();
|
||||
std::vector<bool> flags(n);
|
||||
std::set<std::pair<std::vector<unsigned int>, std::vector<unsigned int>>> unique_combinations;
|
||||
|
||||
if (extruders.empty())
|
||||
return unique_combinations;
|
||||
|
||||
for (int i = 1; i <= n / 2; ++i) {
|
||||
std::fill(flags.begin(), flags.begin() + i, true);
|
||||
std::fill(flags.begin() + i, flags.end(), false);
|
||||
|
||||
do {
|
||||
std::vector<unsigned int> group1, group2;
|
||||
for (int j = 0; j < n; ++j) {
|
||||
if (flags[j]) {
|
||||
group1.push_back(extruders[j]);
|
||||
} else {
|
||||
group2.push_back(extruders[j]);
|
||||
}
|
||||
}
|
||||
|
||||
if (group1.size() > group2.size()) { std::swap(group1, group2); }
|
||||
|
||||
unique_combinations.insert({group1, group2});
|
||||
|
||||
} while (std::prev_permutation(flags.begin(), flags.end()));
|
||||
}
|
||||
|
||||
return unique_combinations;
|
||||
}
|
||||
|
||||
using FlushMatrix = std::vector<std::vector<float>>;
|
||||
|
||||
float get_flush_volume(const std::vector<int> &filament_maps, const std::vector<unsigned int> &extruders, const std::vector<FlushMatrix> &matrix, size_t nozzle_nums)
|
||||
{
|
||||
std::vector<std::vector<unsigned int>> nozzle_filaments;
|
||||
nozzle_filaments.resize(nozzle_nums);
|
||||
|
||||
for (unsigned int filament_id : extruders) {
|
||||
nozzle_filaments[filament_maps[filament_id]].emplace_back(filament_id);
|
||||
}
|
||||
|
||||
float flush_volume = 0;
|
||||
for (size_t nozzle_id = 0; nozzle_id < nozzle_nums; ++nozzle_id) {
|
||||
for (size_t i = 0; i + 1 < nozzle_filaments[nozzle_id].size(); ++i) {
|
||||
flush_volume += matrix[nozzle_id][nozzle_filaments[nozzle_id][i]][nozzle_filaments[nozzle_id][i+1]];
|
||||
}
|
||||
}
|
||||
|
||||
return flush_volume;
|
||||
}
|
||||
|
||||
std::vector<int> ToolOrdering::get_recommended_filament_maps()
|
||||
{
|
||||
const PrintConfig *print_config = m_print_config_ptr;
|
||||
if (!print_config && m_print_object_ptr) {
|
||||
print_config = &(m_print_object_ptr->print()->config());
|
||||
}
|
||||
|
||||
if (!print_config || m_layer_tools.empty())
|
||||
return std::vector<int>();
|
||||
|
||||
const unsigned int number_of_extruders = (unsigned int) (print_config->filament_colour.values.size() + EPSILON);
|
||||
|
||||
// get flush matrix
|
||||
std::vector<FlushMatrix> nozzle_flush_mtx;
|
||||
size_t nozzle_nums = print_config->nozzle_diameter.values.size();
|
||||
for (size_t nozzle_id = 0; nozzle_id < nozzle_nums; ++nozzle_id) {
|
||||
std::vector<float> flush_matrix(cast<float>(get_flush_volumes_matrix(print_config->flush_volumes_matrix.values, nozzle_id, nozzle_nums)));
|
||||
std::vector<std::vector<float>> wipe_volumes;
|
||||
for (unsigned int i = 0; i < number_of_extruders; ++i)
|
||||
wipe_volumes.push_back(std::vector<float>(flush_matrix.begin() + i * number_of_extruders, flush_matrix.begin() + (i + 1) * number_of_extruders));
|
||||
|
||||
nozzle_flush_mtx.emplace_back(wipe_volumes);
|
||||
}
|
||||
|
||||
auto extruders_to_hash_key = [](const std::vector<unsigned int> &extruders, std::optional<unsigned int> initial_extruder_id) -> uint32_t {
|
||||
uint32_t hash_key = 0;
|
||||
// high 16 bit define initial extruder ,low 16 bit define extruder set
|
||||
if (initial_extruder_id) hash_key |= (1 << (16 + *initial_extruder_id));
|
||||
for (auto item : extruders) hash_key |= (1 << item);
|
||||
return hash_key;
|
||||
};
|
||||
|
||||
std::vector<LayerPrintSequence> other_layers_seqs;
|
||||
const ConfigOptionInts * other_layers_print_sequence_op = print_config->option<ConfigOptionInts>("other_layers_print_sequence");
|
||||
const ConfigOptionInt * other_layers_print_sequence_nums_op = print_config->option<ConfigOptionInt>("other_layers_print_sequence_nums");
|
||||
if (other_layers_print_sequence_op && other_layers_print_sequence_nums_op) {
|
||||
const std::vector<int> &print_sequence = other_layers_print_sequence_op->values;
|
||||
int sequence_nums = other_layers_print_sequence_nums_op->value;
|
||||
other_layers_seqs = get_other_layers_print_sequence(sequence_nums, print_sequence);
|
||||
}
|
||||
|
||||
// other_layers_seq: the layer_idx and extruder_idx are base on 1
|
||||
auto get_custom_seq = [&other_layers_seqs](int layer_idx, std::vector<int> &out_seq) -> bool {
|
||||
for (size_t idx = other_layers_seqs.size() - 1; idx != size_t(-1); --idx) {
|
||||
const auto &other_layers_seq = other_layers_seqs[idx];
|
||||
if (layer_idx + 1 >= other_layers_seq.first.first && layer_idx + 1 <= other_layers_seq.first.second) {
|
||||
out_seq = other_layers_seq.second;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
std::set<unsigned int> extruders;
|
||||
for (int i = 0; i < m_layer_tools.size(); ++i) {
|
||||
LayerTools < = m_layer_tools[i];
|
||||
for (unsigned int extruder : lt.extruders)
|
||||
extruders.insert(extruder);
|
||||
}
|
||||
|
||||
auto extruder_group = generate_combinations(std::vector<unsigned int>(extruders.begin(), extruders.end()));
|
||||
|
||||
std::vector<int> recommended_filament_maps;
|
||||
float min_flush_volume = std::numeric_limits<float>::max();
|
||||
for (auto iter = extruder_group.begin(); iter != extruder_group.end(); ++iter) {
|
||||
std::vector<int> filament_maps;
|
||||
filament_maps.resize(number_of_extruders);
|
||||
for (unsigned int e : iter->first) {
|
||||
filament_maps[e] = 0;
|
||||
}
|
||||
for (unsigned int e : iter->second) {
|
||||
filament_maps[e] = 1;
|
||||
}
|
||||
|
||||
std::optional<unsigned int> current_extruder_id;
|
||||
std::vector<std::optional<unsigned int>> nozzle_to_cur_filaments;
|
||||
nozzle_to_cur_filaments.resize(nozzle_nums);
|
||||
|
||||
float flush_volume_cost = 0;
|
||||
for (int i = 0; i < m_layer_tools.size(); ++i) {
|
||||
LayerTools < = m_layer_tools[i];
|
||||
if (lt.extruders.empty())
|
||||
continue;
|
||||
|
||||
std::vector<int> custom_extruder_seq;
|
||||
if (get_custom_seq(i, custom_extruder_seq) && !custom_extruder_seq.empty()) {
|
||||
std::vector<unsigned int> unsign_custom_extruder_seq;
|
||||
for (int extruder : custom_extruder_seq) {
|
||||
unsigned int unsign_extruder = static_cast<unsigned int>(extruder) - 1;
|
||||
auto it = std::find(lt.extruders.begin(), lt.extruders.end(), unsign_extruder);
|
||||
if (it != lt.extruders.end()) {
|
||||
unsign_custom_extruder_seq.emplace_back(unsign_extruder);
|
||||
nozzle_to_cur_filaments[filament_maps[unsign_extruder]] = unsign_extruder;
|
||||
}
|
||||
}
|
||||
assert(lt.extruders.size() == unsign_custom_extruder_seq.size());
|
||||
lt.extruders = unsign_custom_extruder_seq;
|
||||
current_extruder_id = lt.extruders.back();
|
||||
flush_volume_cost += get_flush_volume(filament_maps, lt.extruders, nozzle_flush_mtx, nozzle_nums);
|
||||
continue;
|
||||
}
|
||||
|
||||
// The algorithm complexity is O(n2*2^n)
|
||||
if (i != 0) {
|
||||
std::vector<std::vector<unsigned int>> nozzle_filaments;
|
||||
nozzle_filaments.resize(nozzle_nums);
|
||||
|
||||
for (unsigned int filament_id : lt.extruders) {
|
||||
nozzle_filaments[filament_maps[filament_id]].emplace_back(filament_id);
|
||||
}
|
||||
|
||||
for (size_t nozzle_id = 0; nozzle_id < nozzle_nums; ++nozzle_id) {
|
||||
auto hash_key = extruders_to_hash_key(nozzle_filaments[nozzle_id], nozzle_to_cur_filaments[nozzle_id]);
|
||||
auto iter = m_tool_order_cache.find(hash_key);
|
||||
// todo : the cache with flush cost
|
||||
//if (iter == m_tool_order_cache.end()) {
|
||||
float f_cost = 0;
|
||||
nozzle_filaments[nozzle_id] = get_extruders_order(nozzle_flush_mtx[nozzle_id], nozzle_filaments[nozzle_id], nozzle_to_cur_filaments[nozzle_id], &f_cost);
|
||||
std::vector<uint8_t> hash_val;
|
||||
hash_val.reserve(nozzle_filaments[nozzle_id].size());
|
||||
for (auto item : nozzle_filaments[nozzle_id])
|
||||
hash_val.emplace_back(static_cast<uint8_t>(item));
|
||||
m_tool_order_cache[hash_key] = hash_val;
|
||||
flush_volume_cost += f_cost;
|
||||
//} else {
|
||||
// std::vector<unsigned int> extruder_order;
|
||||
// extruder_order.reserve(iter->second.size());
|
||||
// for (auto item : iter->second)
|
||||
// extruder_order.emplace_back(static_cast<unsigned int>(item));
|
||||
// nozzle_filaments[nozzle_id] = std::move(extruder_order);
|
||||
//}
|
||||
nozzle_to_cur_filaments[nozzle_id] = nozzle_filaments[nozzle_id].back();
|
||||
}
|
||||
|
||||
lt.extruders.clear();
|
||||
for (size_t nozzle_id = 0; nozzle_id < nozzle_nums; ++nozzle_id) {
|
||||
lt.extruders.insert(lt.extruders.end(), nozzle_filaments[nozzle_id].begin(), nozzle_filaments[nozzle_id].end());
|
||||
}
|
||||
}
|
||||
current_extruder_id = lt.extruders.back();
|
||||
}
|
||||
|
||||
if (flush_volume_cost == 0) {
|
||||
recommended_filament_maps = filament_maps;
|
||||
break;
|
||||
}
|
||||
|
||||
if (flush_volume_cost < min_flush_volume) {
|
||||
min_flush_volume = flush_volume_cost;
|
||||
recommended_filament_maps = filament_maps;
|
||||
}
|
||||
}
|
||||
return recommended_filament_maps;
|
||||
}
|
||||
|
||||
void ToolOrdering::reorder_extruders_for_minimum_flush_volume()
|
||||
{
|
||||
const PrintConfig *print_config = m_print_config_ptr;
|
||||
if (!print_config && m_print_object_ptr) {
|
||||
print_config = &(m_print_object_ptr->print()->config());
|
||||
@@ -843,6 +1054,13 @@ void ToolOrdering::reorder_extruders_for_minimum_flush_volume()
|
||||
if (!print_config || m_layer_tools.empty())
|
||||
return;
|
||||
|
||||
size_t nozzle_nums = print_config->nozzle_diameter.values.size();
|
||||
if (nozzle_nums > 1) {
|
||||
std::vector<int> filament_maps = get_recommended_filament_maps();
|
||||
reorder_extruders_for_minimum_flush_volume_multi_extruder(filament_maps);
|
||||
return;
|
||||
}
|
||||
|
||||
// Get wiping matrix to get number of extruders and convert vector<double> to vector<float>:
|
||||
std::vector<float> flush_matrix(cast<float>(print_config->flush_volumes_matrix.values));
|
||||
const unsigned int number_of_extruders = (unsigned int) (sqrt(flush_matrix.size()) + EPSILON);
|
||||
@@ -936,7 +1154,7 @@ void ToolOrdering::reorder_extruders_for_minimum_flush_volume()
|
||||
}
|
||||
}
|
||||
|
||||
void ToolOrdering::reorder_extruders_for_minimum_flush_volume_multi_extruder()
|
||||
void ToolOrdering::reorder_extruders_for_minimum_flush_volume_multi_extruder(const std::vector<int>& filament_maps)
|
||||
{
|
||||
const PrintConfig *print_config = m_print_config_ptr;
|
||||
if (!print_config && m_print_object_ptr) {
|
||||
@@ -947,14 +1165,6 @@ void ToolOrdering::reorder_extruders_for_minimum_flush_volume_multi_extruder()
|
||||
return;
|
||||
|
||||
const unsigned int number_of_extruders = (unsigned int) (print_config->filament_colour.values.size() + EPSILON);
|
||||
// todo multi_extruders: get filament_maps
|
||||
std::vector<int> filament_maps;
|
||||
for (int i = 0; i <= number_of_extruders / 2; ++i) {
|
||||
filament_maps.push_back(1);
|
||||
}
|
||||
for (int i = number_of_extruders / 2 + 1; i < number_of_extruders; ++i) {
|
||||
filament_maps.push_back(2);
|
||||
}
|
||||
|
||||
using FlushMatrix = std::vector<std::vector<float>>;
|
||||
size_t nozzle_nums = print_config->nozzle_diameter.values.size();
|
||||
@@ -1000,6 +1210,7 @@ void ToolOrdering::reorder_extruders_for_minimum_flush_volume_multi_extruder()
|
||||
std::optional<unsigned int> current_extruder_id;
|
||||
|
||||
std::vector<std::optional<unsigned int>> nozzle_to_cur_filaments;
|
||||
nozzle_to_cur_filaments.resize(nozzle_nums);
|
||||
|
||||
for (int i = 0; i < m_layer_tools.size(); ++i) {
|
||||
LayerTools < = m_layer_tools[i];
|
||||
@@ -1029,7 +1240,7 @@ void ToolOrdering::reorder_extruders_for_minimum_flush_volume_multi_extruder()
|
||||
nozzle_filaments.resize(nozzle_nums);
|
||||
|
||||
for (unsigned int filament_id : lt.extruders) {
|
||||
nozzle_filaments[filament_maps[filament_id] - 1].emplace_back(filament_id);
|
||||
nozzle_filaments[filament_maps[filament_id]].emplace_back(filament_id);
|
||||
}
|
||||
|
||||
for (size_t nozzle_id = 0; nozzle_id < nozzle_nums; ++nozzle_id)
|
||||
|
||||
Reference in New Issue
Block a user