ENH: add filament cluster algorithm
1.Add new KMediods algorithm 2.Consider physical and geometric printables 3.Refine code structure jira:NONE Signed-off-by: xun.zhang <xun.zhang@bambulab.com> Change-Id: I1412835c3c6380f9cedb44ff6914004365bba889
This commit is contained in:
parent
ed61d1d31b
commit
c53a35856d
|
@ -1,280 +1,72 @@
|
|||
#include "FilamentGroup.hpp"
|
||||
#include "GCode/ToolOrderUtils.hpp"
|
||||
#include <queue>
|
||||
#include <random>
|
||||
#include <cassert>
|
||||
|
||||
namespace Slic3r
|
||||
{
|
||||
void KMediods::fit(const FGStrategy&g_strategy , int timeout_ms)
|
||||
{
|
||||
std::vector<int>best_medoids;
|
||||
std::vector<int>best_labels;
|
||||
int best_cost = std::numeric_limits<int>::max();
|
||||
|
||||
FlushTimeMachine T;
|
||||
T.time_machine_start();
|
||||
|
||||
int count = 0;
|
||||
while (true)
|
||||
{
|
||||
std::vector<int>medoids;
|
||||
std::vector<int>labels;
|
||||
if (count == 0)
|
||||
medoids = initialize(INIT_TYPE::Farthest);
|
||||
else
|
||||
medoids = initialize(INIT_TYPE::Random);
|
||||
|
||||
labels = assign_label(medoids,g_strategy);
|
||||
int cost = calc_cost(labels, medoids);
|
||||
|
||||
for (int i = 0; i < m_filament_num; ++i) {
|
||||
if (std::find(medoids.begin(), medoids.end(), i) != medoids.end())
|
||||
continue;
|
||||
|
||||
for (int j = 0; j < 2; ++j) {
|
||||
std::vector<int> new_medoids = medoids;
|
||||
new_medoids[j] = i;
|
||||
std::vector<int> new_labels = assign_label(new_medoids,g_strategy);
|
||||
int new_cost = calc_cost(new_labels, new_medoids);
|
||||
|
||||
if (new_cost < cost)
|
||||
{
|
||||
labels = new_labels;
|
||||
cost = new_cost;
|
||||
medoids = new_medoids;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (cost < best_cost)
|
||||
{
|
||||
best_cost = cost;
|
||||
best_labels = labels;
|
||||
best_medoids = medoids;
|
||||
}
|
||||
count += 1;
|
||||
|
||||
if (T.time_machine_end() > timeout_ms || m_medoids_set.size() == (m_filament_num * (m_filament_num - 1) / 2))
|
||||
break;
|
||||
static void remove_intersection(std::set<int>& a, std::set<int>& b) {
|
||||
std::vector<int>intersection;
|
||||
std::set_intersection(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(intersection));
|
||||
for (auto& item : intersection) {
|
||||
a.erase(item);
|
||||
b.erase(item);
|
||||
}
|
||||
|
||||
this->m_filament_labels = best_labels;
|
||||
}
|
||||
|
||||
std::vector<int> KMediods::assign_label(const std::vector<int>& medoids,const FGStrategy&g_strategy)
|
||||
static bool extract_indices(const std::vector<unsigned int>& used_filaments, const std::vector<std::set<int>>& physical_unprintable_elems, const std::vector<std::set<int>>& geometric_unprintable_elems,
|
||||
std::vector<std::set<int>>& physical_unprintable_idxs, std::vector<std::set<int>>& geometric_unprintable_idxs)
|
||||
{
|
||||
std::vector<int>labels(m_filament_num);
|
||||
struct Comp {
|
||||
bool operator()(const std::pair<int, int>& a, const std::pair<int, int>& b) {
|
||||
return a.second > b.second;
|
||||
}
|
||||
};
|
||||
std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>,Comp>min_heap;
|
||||
assert(physical_unprintable_elems.size() == geometric_unprintable_elems.size());
|
||||
std::vector<std::set<int>>(physical_unprintable_elems.size()).swap(physical_unprintable_idxs);
|
||||
std::vector<std::set<int>>(geometric_unprintable_elems.size()).swap(geometric_unprintable_idxs);
|
||||
|
||||
for (int i = 0; i < m_filament_num; ++i) {
|
||||
int distancec_to_0 = m_distance_matrix[i][medoids[0]];
|
||||
int distancec_to_1 = m_distance_matrix[i][medoids[1]];
|
||||
min_heap.push({ i,distancec_to_0 - distancec_to_1 });
|
||||
}
|
||||
std::set<int> group_0, group_1;
|
||||
bool have_enough_size = (m_filament_num <= (m_max_group_size[0] + m_max_group_size[1]));
|
||||
if (have_enough_size || g_strategy == FGStrategy::BestFit) {
|
||||
while (!min_heap.empty()) {
|
||||
auto top = min_heap.top();
|
||||
min_heap.pop();
|
||||
if (group_0.size() < m_max_group_size[0] && (top.second <= 0 || group_1.size() >= m_max_group_size[1]))
|
||||
group_0.insert(top.first);
|
||||
else if (group_1.size() < m_max_group_size[1] && (top.second > 0 || group_0.size() >= m_max_group_size[0]))
|
||||
group_1.insert(top.first);
|
||||
else {
|
||||
if (top.second <= 0)
|
||||
group_0.insert(top.first);
|
||||
else
|
||||
group_1.insert(top.first);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (g_strategy == FGStrategy::BestCost) {
|
||||
while (!min_heap.empty()) {
|
||||
auto top = min_heap.top();
|
||||
min_heap.pop();
|
||||
if (top.second <= 0)
|
||||
group_0.insert(top.first);
|
||||
else
|
||||
group_1.insert(top.first);
|
||||
for (size_t gid = 0; gid < physical_unprintable_elems.size(); ++gid) {
|
||||
for (auto& f : physical_unprintable_elems[gid]) {
|
||||
auto iter = std::find(used_filaments.begin(), used_filaments.end(), (unsigned)f);
|
||||
if (iter != used_filaments.end())
|
||||
physical_unprintable_idxs[gid].insert(iter - used_filaments.begin());
|
||||
}
|
||||
}
|
||||
|
||||
for (auto& item : group_0)
|
||||
labels[item] = 0;
|
||||
for (auto& item : group_1)
|
||||
labels[item] = 1;
|
||||
|
||||
return labels;
|
||||
for (size_t gid = 0; gid < geometric_unprintable_elems.size(); ++gid) {
|
||||
for (auto& f : geometric_unprintable_elems[gid]) {
|
||||
auto iter = std::find(used_filaments.begin(), used_filaments.end(), (unsigned)f);
|
||||
if (iter != used_filaments.end())
|
||||
geometric_unprintable_idxs[gid].insert(iter - used_filaments.begin());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int KMediods::calc_cost(const std::vector<int>& labels, const std::vector<int>& medoids)
|
||||
static bool check_printable(const std::vector<std::set<int>>& groups, const std::map<int,int>& unprintable)
|
||||
{
|
||||
int total_cost = 0;
|
||||
for (int i = 0; i < m_filament_num; ++i)
|
||||
total_cost += m_distance_matrix[i][medoids[labels[i]]];
|
||||
return total_cost;
|
||||
}
|
||||
|
||||
std::vector<int> KMediods::initialize(INIT_TYPE type)
|
||||
{
|
||||
auto hash_func = [](int n1, int n2) {
|
||||
return n1 * 100 + n2;
|
||||
};
|
||||
srand(time(nullptr));
|
||||
std::vector<int>ret;
|
||||
if (type == INIT_TYPE::Farthest) {
|
||||
//get the farthest items
|
||||
int target_i = 0, target_j = 0, target_val = std::numeric_limits<int>::min();
|
||||
for (int i = 0; i < m_distance_matrix.size(); ++i) {
|
||||
for (int j = 0; j < m_distance_matrix[0].size(); ++j) {
|
||||
if (i != j && m_distance_matrix[i][j] > target_val) {
|
||||
target_val = m_distance_matrix[i][j];
|
||||
target_i = i;
|
||||
target_j = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
ret.emplace_back(std::min(target_i, target_j));
|
||||
ret.emplace_back(std::max(target_i, target_j));
|
||||
}
|
||||
else if (type == INIT_TYPE::Random) {
|
||||
while (true) {
|
||||
std::vector<int>medoids;
|
||||
while (medoids.size() < k)
|
||||
{
|
||||
int candidate = rand() % m_filament_num;
|
||||
if (std::find(medoids.begin(), medoids.end(), candidate) == medoids.end())
|
||||
medoids.push_back(candidate);
|
||||
}
|
||||
std::sort(medoids.begin(), medoids.end());
|
||||
|
||||
if (m_medoids_set.find(hash_func(medoids[0], medoids[1])) != m_medoids_set.end() && m_medoids_set.size() != (m_filament_num * (m_filament_num - 1) / 2))
|
||||
continue;
|
||||
else {
|
||||
ret = medoids;
|
||||
break;
|
||||
}
|
||||
for (size_t i = 0; i < groups.size(); ++i) {
|
||||
auto& group = groups[i];
|
||||
for (auto& filament : group) {
|
||||
if (auto iter = unprintable.find(filament); iter != unprintable.end() && i == iter->second)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
m_medoids_set.insert(hash_func(ret[0],ret[1]));
|
||||
return ret;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
std::vector<int> FilamentGroup::calc_filament_group(const std::vector<std::vector<unsigned int>>& layer_filaments, const FGStrategy& g_strategy,int* cost)
|
||||
std::vector<unsigned int> collect_sorted_used_filaments(const std::vector<std::vector<unsigned int>>& layer_filaments)
|
||||
{
|
||||
std::set<unsigned int>used_filaments_set;
|
||||
for (const auto& lf : layer_filaments)
|
||||
for (const auto& extruder : lf)
|
||||
used_filaments_set.insert(extruder);
|
||||
|
||||
std::vector<unsigned int>used_filaments = std::vector<unsigned int>(used_filaments_set.begin(), used_filaments_set.end());
|
||||
for (const auto& f : lf)
|
||||
used_filaments_set.insert(f);
|
||||
std::vector<unsigned int>used_filaments(used_filaments_set.begin(), used_filaments_set.end());
|
||||
std::sort(used_filaments.begin(), used_filaments.end());
|
||||
|
||||
int used_filament_num = used_filaments.size();
|
||||
|
||||
std::vector<int> filament_labels(m_total_filament_num, 0);
|
||||
|
||||
if (used_filament_num <= 1) {
|
||||
if (cost)
|
||||
*cost = 0;
|
||||
return filament_labels;
|
||||
}
|
||||
if (used_filament_num < 10)
|
||||
return calc_filament_group_by_enum(layer_filaments, used_filaments, g_strategy, cost);
|
||||
else
|
||||
return calc_filament_group_by_pam(layer_filaments, used_filaments, g_strategy, cost, 100);
|
||||
|
||||
return used_filaments;
|
||||
}
|
||||
|
||||
std::vector<int> FilamentGroup::calc_filament_group_by_enum(const std::vector<std::vector<unsigned int>>& layer_filaments, const std::vector<unsigned int>& used_filaments, const FGStrategy& g_strategy,int*cost)
|
||||
FlushDistanceEvaluator::FlushDistanceEvaluator(const FlushMatrix& flush_matrix, const std::vector<unsigned int>& used_filaments, const std::vector<std::vector<unsigned int>>& layer_filaments, double p)
|
||||
{
|
||||
auto bit_count_one = [](uint64_t n)
|
||||
{
|
||||
int count = 0;
|
||||
while (n != 0)
|
||||
{
|
||||
n &= n - 1;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
};
|
||||
|
||||
int used_filament_num = used_filaments.size();
|
||||
bool have_enough_size = (used_filament_num <= (m_max_group_size[0] + m_max_group_size[1]));
|
||||
|
||||
uint64_t max_group_num = (static_cast<uint64_t>(1) << used_filament_num);
|
||||
int best_cost = std::numeric_limits<int>::max();
|
||||
std::vector<int>best_label;
|
||||
|
||||
for (uint64_t i = 0; i < max_group_num; ++i) {
|
||||
int num_to_group_1 = bit_count_one(i);
|
||||
int num_to_group_0 = used_filament_num - num_to_group_1;
|
||||
bool should_accept = false;
|
||||
if (have_enough_size)
|
||||
should_accept = (num_to_group_0 <= m_max_group_size[0] && num_to_group_1 <= m_max_group_size[1]);
|
||||
else if (g_strategy == FGStrategy::BestCost)
|
||||
should_accept = true;
|
||||
else if (g_strategy == FGStrategy::BestFit)
|
||||
should_accept = (num_to_group_0 >= m_max_group_size[0] && num_to_group_1 >= m_max_group_size[1]);
|
||||
|
||||
if (!should_accept)
|
||||
continue;
|
||||
|
||||
std::set<int>group_0, group_1;
|
||||
for (int j = 0; j < used_filament_num; ++j) {
|
||||
if (i & (static_cast<uint64_t>(1) << j))
|
||||
group_1.insert(used_filaments[j]);
|
||||
else
|
||||
group_0.insert(used_filaments[j]);
|
||||
}
|
||||
|
||||
std::vector<int>filament_maps(used_filament_num);
|
||||
for (int i = 0; i < used_filament_num; ++i) {
|
||||
if (group_0.find(used_filaments[i]) != group_0.end())
|
||||
filament_maps[i] = 0;
|
||||
if (group_1.find(used_filaments[i]) != group_1.end())
|
||||
filament_maps[i] = 1;
|
||||
}
|
||||
|
||||
int total_cost = reorder_filaments_for_minimum_flush_volume(
|
||||
used_filaments,
|
||||
filament_maps,
|
||||
layer_filaments,
|
||||
m_flush_matrix,
|
||||
get_custom_seq,
|
||||
nullptr
|
||||
);
|
||||
|
||||
if (total_cost < best_cost) {
|
||||
best_cost = total_cost;
|
||||
best_label = filament_maps;
|
||||
}
|
||||
}
|
||||
|
||||
if (cost)
|
||||
*cost = best_cost;
|
||||
|
||||
std::vector<int> filament_labels(m_total_filament_num, 0);
|
||||
for (int i = 0; i < best_label.size(); ++i)
|
||||
filament_labels[used_filaments[i]] = best_label[i];
|
||||
|
||||
return filament_labels;
|
||||
}
|
||||
|
||||
std::vector<int> FilamentGroup::calc_filament_group_by_pam(const std::vector<std::vector<unsigned int>>& layer_filaments, const std::vector<unsigned int>& used_filaments, const FGStrategy& g_strategy, int*cost,int timeout_ms)
|
||||
{
|
||||
std::vector<int>filament_labels_ret(m_total_filament_num, 0);
|
||||
int used_filament_num = used_filaments.size();
|
||||
if (used_filaments.size() == 1)
|
||||
return filament_labels_ret;
|
||||
//calc pair counts
|
||||
std::vector<std::vector<int>>count_matrix(used_filament_num, std::vector<int>(used_filament_num));
|
||||
std::vector<std::vector<int>>count_matrix(used_filaments.size(), std::vector<int>(used_filaments.size()));
|
||||
for (const auto& lf : layer_filaments) {
|
||||
for (auto iter = lf.begin(); iter != lf.end(); ++iter) {
|
||||
auto id_iter1 = std::find(used_filaments.begin(), used_filaments.end(), *iter);
|
||||
|
@ -292,29 +84,327 @@ namespace Slic3r
|
|||
}
|
||||
}
|
||||
|
||||
//calc distance matrix
|
||||
std::vector<std::vector<float>>distance_matrix(used_filament_num, std::vector<float>(used_filament_num));
|
||||
m_distance_matrix.resize(used_filaments.size(), std::vector<float>(used_filaments.size()));
|
||||
|
||||
for (size_t i = 0; i < used_filaments.size(); ++i) {
|
||||
for (size_t j = 0; j < used_filaments.size(); ++j) {
|
||||
if (i == j)
|
||||
distance_matrix[i][j] = 0;
|
||||
m_distance_matrix[i][j] = 0;
|
||||
else {
|
||||
//TODO: check m_flush_matrix
|
||||
float max_val = std::max(m_flush_matrix[0][used_filaments[i]][used_filaments[j]], m_flush_matrix[0][used_filaments[j]][used_filaments[i]]);
|
||||
float min_val = std::min(m_flush_matrix[0][used_filaments[i]][used_filaments[j]], m_flush_matrix[0][used_filaments[j]][used_filaments[i]]);
|
||||
float max_val = std::max(flush_matrix[used_filaments[i]][used_filaments[j]], flush_matrix[used_filaments[j]][used_filaments[i]]);
|
||||
float min_val = std::min(flush_matrix[used_filaments[i]][used_filaments[j]], flush_matrix[used_filaments[j]][used_filaments[i]]);
|
||||
m_distance_matrix[i][j] = (max_val * p + min_val * (1 - p)) * count_matrix[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
double p = 0.65;
|
||||
distance_matrix[i][j] = (max_val * p + min_val * (1 - p)) * count_matrix[i][j];
|
||||
double FlushDistanceEvaluator::get_distance(int idx_a, int idx_b) const
|
||||
{
|
||||
assert(0 <= idx_a && idx_a < m_distance_matrix.size());
|
||||
assert(0 <= idx_b && idx_b < m_distance_matrix.size());
|
||||
|
||||
return m_distance_matrix[idx_a][idx_b];
|
||||
}
|
||||
|
||||
std::vector<int> KMediods2::cluster_small_data(const std::map<int, int>& unplaceable_limits, const std::vector<int>& group_size)
|
||||
{
|
||||
std::vector<int>labels(m_elem_count, -1);
|
||||
std::vector<int>new_group_size = group_size;
|
||||
|
||||
for (auto& [elem, center] : unplaceable_limits) {
|
||||
if (labels[elem] == -1) {
|
||||
int gid = 1 - center;
|
||||
labels[elem] = gid;
|
||||
new_group_size[gid] -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto& label : labels) {
|
||||
if (label == -1) {
|
||||
int gid = -1;
|
||||
for (size_t idx = 0; idx < new_group_size.size(); ++idx) {
|
||||
if (new_group_size[idx] > 0) {
|
||||
gid = idx;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (gid != -1) {
|
||||
label = gid;
|
||||
new_group_size[gid] -= 1;
|
||||
}
|
||||
else {
|
||||
label = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
KMediods PAM(distance_matrix, used_filament_num, m_max_group_size);
|
||||
PAM.fit(g_strategy, timeout_ms);
|
||||
std::vector<int>filament_labels = PAM.get_filament_labels();
|
||||
return labels;
|
||||
}
|
||||
|
||||
std::vector<int> KMediods2::assign_cluster_label(const std::vector<int>& center, const std::map<int, int>& unplaceable_limtis, const std::vector<int>& group_size, const FGStrategy& strategy)
|
||||
{
|
||||
struct Comp {
|
||||
bool operator()(const std::pair<int, int>& a, const std::pair<int, int>& b) {
|
||||
return a.second > b.second;
|
||||
}
|
||||
};
|
||||
|
||||
std::vector<std::set<int>>groups(2);
|
||||
std::vector<int>new_max_group_size = group_size;
|
||||
// store filament idx and distance gap between center 0 and center 1
|
||||
std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, Comp>min_heap;
|
||||
|
||||
for (int i = 0; i < m_elem_count; ++i) {
|
||||
if (auto it = unplaceable_limtis.find(i); it != unplaceable_limtis.end()) {
|
||||
int gid = it->second;
|
||||
assert(gid == 0 || gid == 1);
|
||||
groups[1 - gid].insert(i); // insert to group
|
||||
new_max_group_size[1 - gid] = std::max(new_max_group_size[1 - gid] - 1, 0); // decrease group_size
|
||||
continue;
|
||||
}
|
||||
int distance_to_0 = m_evaluator->get_distance(i, center[0]);
|
||||
int distance_to_1 = m_evaluator->get_distance(i, center[1]);
|
||||
min_heap.push({ i,distance_to_0 - distance_to_1 });
|
||||
}
|
||||
|
||||
bool have_enough_size = (min_heap.size() <= (new_max_group_size[0] + new_max_group_size[1]));
|
||||
|
||||
if (have_enough_size || strategy == FGStrategy::BestFit) {
|
||||
while (!min_heap.empty()) {
|
||||
auto top = min_heap.top();
|
||||
min_heap.pop();
|
||||
if (groups[0].size() < new_max_group_size[0] && (top.second <= 0 || groups[1].size() >= new_max_group_size[1]))
|
||||
groups[0].insert(top.first);
|
||||
else if (groups[1].size() < new_max_group_size[1] && (top.second > 0 || groups[0].size() >= new_max_group_size[0]))
|
||||
groups[1].insert(top.first);
|
||||
else {
|
||||
if (top.second <= 0)
|
||||
groups[0].insert(top.first);
|
||||
else
|
||||
groups[1].insert(top.first);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
while (!min_heap.empty()) {
|
||||
auto top = min_heap.top();
|
||||
min_heap.pop();
|
||||
if (top.second <= 0)
|
||||
groups[0].insert(top.first);
|
||||
else
|
||||
groups[1].insert(top.first);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<int>labels(m_elem_count);
|
||||
for (auto& f : groups[0])
|
||||
labels[f] = 0;
|
||||
for (auto& f : groups[1])
|
||||
labels[f] = 1;
|
||||
|
||||
return labels;
|
||||
}
|
||||
|
||||
int KMediods2::calc_cost(const std::vector<int>& labels, const std::vector<int>& medoids)
|
||||
{
|
||||
int total_cost = 0;
|
||||
for (int i = 0; i < m_elem_count; ++i)
|
||||
total_cost += m_evaluator->get_distance(i, medoids[labels[i]]);
|
||||
return total_cost;
|
||||
}
|
||||
|
||||
void KMediods2::do_clustering(const FGStrategy& g_strategy, int timeout_ms)
|
||||
{
|
||||
FlushTimeMachine T;
|
||||
T.time_machine_start();
|
||||
|
||||
if (m_elem_count < m_k) {
|
||||
m_cluster_labels = cluster_small_data(m_unplaceable_limits, m_max_cluster_size);
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<int>best_labels;
|
||||
int best_cost = std::numeric_limits<int>::max();
|
||||
|
||||
for (int center_0 = 0; center_0 < m_elem_count; ++center_0) {
|
||||
if (auto iter = m_unplaceable_limits.find(center_0); iter != m_unplaceable_limits.end() && iter->second == 0)
|
||||
continue;
|
||||
for (int center_1 = 0; center_1 < m_elem_count; ++center_1) {
|
||||
if (center_0 == center_1)
|
||||
continue;
|
||||
if (auto iter = m_unplaceable_limits.find(center_1); iter != m_unplaceable_limits.end() && iter->second == 1)
|
||||
continue;
|
||||
|
||||
std::vector<int>new_centers = { center_0,center_1 };
|
||||
std::vector<int>new_labels = assign_cluster_label(new_centers, m_unplaceable_limits, m_max_cluster_size, g_strategy);
|
||||
|
||||
int new_cost = calc_cost(new_labels, new_centers);
|
||||
if (new_cost < best_cost) {
|
||||
best_cost = new_cost;
|
||||
best_labels = new_labels;
|
||||
}
|
||||
if (T.time_machine_end() > timeout_ms)
|
||||
break;
|
||||
}
|
||||
if (T.time_machine_end() > timeout_ms)
|
||||
break;
|
||||
}
|
||||
this->m_cluster_labels = best_labels;
|
||||
}
|
||||
|
||||
FilamentGroup::FilamentGroup(const FilamentGroupContext& context)
|
||||
{
|
||||
assert(context.flush_matrix.size() == 2);
|
||||
assert(context.flush_matrix.size() == context.max_group_size.size());
|
||||
assert(context.max_group_size.size() == context.physical_unprintables.size());
|
||||
assert(context.physical_unprintables.size() == context.geometric_unprintables.size());
|
||||
|
||||
m_context = context;
|
||||
}
|
||||
|
||||
std::vector<int> FilamentGroup::calc_filament_group(const std::vector<std::vector<unsigned int>>& layer_filaments, const FGStrategy& g_strategy, int* cost)
|
||||
{
|
||||
std::vector<unsigned int> used_filaments = collect_sorted_used_filaments(layer_filaments);
|
||||
|
||||
int used_filament_num = used_filaments.size();
|
||||
if (used_filament_num < 10)
|
||||
return calc_filament_group_by_enum(layer_filaments, used_filaments, g_strategy, cost);
|
||||
else
|
||||
return calc_filament_group_by_pam2(layer_filaments, used_filaments, g_strategy, cost, 100);
|
||||
}
|
||||
|
||||
// sorted used_filaments
|
||||
std::vector<int> FilamentGroup::calc_filament_group_by_enum(const std::vector<std::vector<unsigned int>>& layer_filaments, const std::vector<unsigned int>& used_filaments, const FGStrategy& g_strategy,int*cost)
|
||||
{
|
||||
static constexpr int UNPLACEABLE_LIMIT_REWARD = 100; // reward value if the group result follows the unprintable limit
|
||||
static constexpr int MAX_SIZE_LIMIT_REWARD = 10; // reward value if the group result follows the max size per extruder
|
||||
static constexpr int BEST_FIT_LIMIT_REWARD = 1; // reward value if the group result try to fill the max size per extruder
|
||||
auto bit_count_one = [](uint64_t n)
|
||||
{
|
||||
int count = 0;
|
||||
while (n != 0)
|
||||
{
|
||||
n &= n - 1;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
};
|
||||
|
||||
std::map<int, int>unplaceable_limits;
|
||||
{
|
||||
// if the filament cannot be placed in both extruder, we just ignore it
|
||||
std::vector<std::set<int>>physical_unprintables = m_context.physical_unprintables;
|
||||
std::vector<std::set<int>>geometric_unprintables = m_context.geometric_unprintables;
|
||||
// TODO: should we instantly fail here later?
|
||||
remove_intersection(physical_unprintables[0], physical_unprintables[1]);
|
||||
remove_intersection(geometric_unprintables[0], geometric_unprintables[1]);
|
||||
|
||||
for (auto& unprintables : { physical_unprintables, geometric_unprintables }) {
|
||||
for (size_t group_id = 0; group_id < 2; ++group_id) {
|
||||
for (size_t elem = 0; elem < used_filaments.size(); ++elem) {
|
||||
for (auto f : unprintables[group_id]) {
|
||||
if (unplaceable_limits.count(f) == 0)
|
||||
unplaceable_limits[f] = group_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int used_filament_num = used_filaments.size();
|
||||
uint64_t max_group_num = (static_cast<uint64_t>(1) << used_filament_num);
|
||||
|
||||
int best_cost = std::numeric_limits<int>::max();
|
||||
std::vector<int>best_label;
|
||||
int best_prefer_level = 0;
|
||||
|
||||
for (uint64_t i = 0; i < max_group_num; ++i) {
|
||||
std::vector<std::set<int>>groups(2);
|
||||
for (int j = 0; j < used_filament_num; ++j) {
|
||||
if (i & (static_cast<uint64_t>(1) << j))
|
||||
groups[1].insert(used_filaments[j]);
|
||||
else
|
||||
groups[0].insert(used_filaments[j]);
|
||||
}
|
||||
|
||||
int prefer_level = 0;
|
||||
|
||||
if (check_printable(groups, unplaceable_limits))
|
||||
prefer_level += UNPLACEABLE_LIMIT_REWARD;
|
||||
if (groups[0].size() <= m_context.max_group_size[0] && groups[1].size() <= m_context.max_group_size[1])
|
||||
prefer_level += MAX_SIZE_LIMIT_REWARD;
|
||||
if (FGStrategy::BestFit == g_strategy && groups[0].size() >= m_context.max_group_size[0] && groups[1].size() >= m_context.max_group_size[1])
|
||||
prefer_level += BEST_FIT_LIMIT_REWARD;
|
||||
|
||||
std::vector<int>filament_maps(used_filament_num);
|
||||
for (int i = 0; i < used_filament_num; ++i) {
|
||||
if (groups[0].find(used_filaments[i]) != groups[0].end())
|
||||
filament_maps[i] = 0;
|
||||
if (groups[1].find(used_filaments[i]) != groups[1].end())
|
||||
filament_maps[i] = 1;
|
||||
}
|
||||
|
||||
int total_cost = reorder_filaments_for_minimum_flush_volume(
|
||||
used_filaments,
|
||||
filament_maps,
|
||||
layer_filaments,
|
||||
m_context.flush_matrix,
|
||||
get_custom_seq,
|
||||
nullptr
|
||||
);
|
||||
|
||||
if (prefer_level > best_prefer_level || (prefer_level == best_prefer_level && total_cost < best_cost)) {
|
||||
best_prefer_level = prefer_level;
|
||||
best_cost = total_cost;
|
||||
best_label = filament_maps;
|
||||
}
|
||||
}
|
||||
|
||||
if (cost)
|
||||
*cost = best_cost;
|
||||
|
||||
std::vector<int> filament_labels(m_context.total_filament_num, 0);
|
||||
for (int i = 0; i < best_label.size(); ++i)
|
||||
filament_labels[used_filaments[i]] = best_label[i];
|
||||
|
||||
return filament_labels;
|
||||
}
|
||||
|
||||
// sorted used_filaments
|
||||
std::vector<int> FilamentGroup::calc_filament_group_by_pam2(const std::vector<std::vector<unsigned int>>& layer_filaments, const std::vector<unsigned int>& used_filaments, const FGStrategy& g_strategy, int*cost,int timeout_ms)
|
||||
{
|
||||
std::vector<int>filament_labels_ret(m_context.total_filament_num, 0);
|
||||
if (used_filaments.size() == 1)
|
||||
return filament_labels_ret;
|
||||
|
||||
std::map<int, int>unplaceable_limits;
|
||||
{
|
||||
// map the unprintable filaments to idx of used filaments , if not used ,just ignore
|
||||
std::vector<std::set<int>> physical_unprintable_idxs, geometric_unprintable_idxs;
|
||||
extract_indices(used_filaments, m_context.physical_unprintables, m_context.geometric_unprintables, physical_unprintable_idxs, geometric_unprintable_idxs);
|
||||
remove_intersection(physical_unprintable_idxs[0], physical_unprintable_idxs[1]);
|
||||
remove_intersection(geometric_unprintable_idxs[0], geometric_unprintable_idxs[1]);
|
||||
for (auto& unprintables : { physical_unprintable_idxs, geometric_unprintable_idxs }) {
|
||||
for (size_t group_id = 0; group_id < 2; ++group_id) {
|
||||
for(auto f:unprintables[group_id]){
|
||||
if(unplaceable_limits.count(f)==0)
|
||||
unplaceable_limits[f]=group_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto distance_evaluator = std::make_shared<FlushDistanceEvaluator>(m_context.flush_matrix[0], used_filaments, layer_filaments);
|
||||
KMediods2 PAM((int)used_filaments.size(),distance_evaluator);
|
||||
PAM.set_max_cluster_size(m_context.max_group_size);
|
||||
PAM.set_unplaceable_limits(unplaceable_limits);
|
||||
PAM.do_clustering(g_strategy, timeout_ms);
|
||||
std::vector<int>filament_labels = PAM.get_cluster_labels();
|
||||
|
||||
if(cost)
|
||||
*cost=reorder_filaments_for_minimum_flush_volume(used_filaments,filament_labels,layer_filaments,m_flush_matrix,std::nullopt,nullptr);
|
||||
*cost=reorder_filaments_for_minimum_flush_volume(used_filaments,filament_labels,layer_filaments,m_context.flush_matrix,std::nullopt,nullptr);
|
||||
|
||||
for (int i = 0; i < filament_labels.size(); ++i)
|
||||
filament_labels_ret[used_filaments[i]] = filament_labels[i];
|
||||
|
|
|
@ -1,12 +1,20 @@
|
|||
#ifndef FILAMENT_GROUP_HPP
|
||||
#define FILAMENT_GROUP_HPP
|
||||
|
||||
#include<chrono>
|
||||
#include<numeric>
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
#include <numeric>
|
||||
#include <set>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include "GCode/ToolOrderUtils.hpp"
|
||||
|
||||
const static int DEFAULT_CLUSTER_SIZE = 16;
|
||||
|
||||
namespace Slic3r
|
||||
{
|
||||
std::vector<unsigned int>collect_sorted_used_filaments(const std::vector<std::vector<unsigned int>>& layer_filaments);
|
||||
|
||||
struct FlushTimeMachine
|
||||
{
|
||||
private:
|
||||
|
@ -31,29 +39,43 @@ namespace Slic3r
|
|||
BestFit
|
||||
};
|
||||
|
||||
struct FilamentGroupContext
|
||||
{
|
||||
std::vector<FlushMatrix> flush_matrix;
|
||||
std::vector<std::set<int>>physical_unprintables;
|
||||
std::vector<std::set<int>>geometric_unprintables;
|
||||
std::vector<int>max_group_size;
|
||||
int total_filament_num;
|
||||
};
|
||||
|
||||
|
||||
class FlushDistanceEvaluator
|
||||
{
|
||||
public:
|
||||
FlushDistanceEvaluator(const FlushMatrix& flush_matrix,const std::vector<unsigned int>&used_filaments,const std::vector<std::vector<unsigned int>>& layer_filaments, double p = 0.65);
|
||||
~FlushDistanceEvaluator() = default;
|
||||
double get_distance(int idx_a, int idx_b) const;
|
||||
private:
|
||||
std::vector<std::vector<float>>m_distance_matrix;
|
||||
|
||||
};
|
||||
|
||||
class FilamentGroup
|
||||
{
|
||||
public:
|
||||
FilamentGroup(const std::vector<FlushMatrix>& flush_matrix, const int total_filament_num, const std::vector<int>& max_group_size) :
|
||||
m_flush_matrix{ flush_matrix },
|
||||
m_total_filament_num{ total_filament_num },
|
||||
m_max_group_size{ max_group_size }
|
||||
{}
|
||||
|
||||
FilamentGroup(const FilamentGroupContext& context);
|
||||
std::vector<int> calc_filament_group(const std::vector<std::vector<unsigned int>>& layer_filaments, const FGStrategy& g_strategy = FGStrategy::BestFit, int* cost = nullptr);
|
||||
private:
|
||||
public:
|
||||
std::vector<int> calc_filament_group_by_enum(const std::vector<std::vector<unsigned int>>& layer_filaments, const std::vector<unsigned int>& used_filaments, const FGStrategy& g_strategy, int* cost = nullptr);
|
||||
std::vector<int> calc_filament_group_by_pam(const std::vector<std::vector<unsigned int>>& layer_filaments, const std::vector<unsigned int>& used_filaments, const FGStrategy& g_strategy, int* cost = nullptr, int timeout_ms = 300);
|
||||
std::vector<int> calc_filament_group_by_pam2(const std::vector<std::vector<unsigned int>>& layer_filaments, const std::vector<unsigned int>& used_filaments, const FGStrategy& g_strategy, int* cost = nullptr, int timeout_ms = 300);
|
||||
private:
|
||||
std::vector<FlushMatrix>m_flush_matrix;
|
||||
std::vector<int>m_max_group_size;
|
||||
int m_total_filament_num;
|
||||
FilamentGroupContext m_context;
|
||||
public:
|
||||
std::optional<std::function<bool(int, std::vector<int>&)>> get_custom_seq;
|
||||
};
|
||||
|
||||
|
||||
class KMediods
|
||||
class KMediods2
|
||||
{
|
||||
enum INIT_TYPE
|
||||
{
|
||||
|
@ -61,29 +83,34 @@ namespace Slic3r
|
|||
Farthest
|
||||
};
|
||||
public:
|
||||
KMediods(const std::vector<std::vector<float>>& distance_matrix, const int filament_num,const std::vector<int>& max_group_size) :
|
||||
m_distance_matrix{ distance_matrix },
|
||||
m_filament_num{ filament_num },
|
||||
m_max_group_size{ max_group_size }{}
|
||||
|
||||
void fit(const FGStrategy& g_strategy,int timeout_ms = 300);
|
||||
std::vector<int>get_filament_labels()const {
|
||||
return m_filament_labels;
|
||||
KMediods2(const int elem_count, const std::shared_ptr<FlushDistanceEvaluator>& evaluator) :
|
||||
m_evaluator{ evaluator },
|
||||
m_elem_count{ elem_count }
|
||||
{
|
||||
m_max_cluster_size = std::vector<int>(m_k, DEFAULT_CLUSTER_SIZE);
|
||||
}
|
||||
|
||||
// set max group size
|
||||
void set_max_cluster_size(const std::vector<int>& group_size) { m_max_cluster_size = group_size; }
|
||||
|
||||
// key stores elem idx, value stores the cluster id that elem cnanot be placed
|
||||
void set_unplaceable_limits(const std::map<int, int>& placeable_limits) { m_unplaceable_limits = placeable_limits; }
|
||||
|
||||
void do_clustering(const FGStrategy& g_strategy,int timeout_ms = 100);
|
||||
std::vector<int>get_cluster_labels()const { return m_cluster_labels; }
|
||||
|
||||
private:
|
||||
std::vector<int>initialize(INIT_TYPE type);
|
||||
std::vector<int>assign_label(const std::vector<int>& medoids,const FGStrategy&g_strategy);
|
||||
std::vector<int>cluster_small_data(const std::map<int, int>& unplaceable_limits, const std::vector<int>& group_size);
|
||||
std::vector<int>assign_cluster_label(const std::vector<int>& center, const std::map<int, int>& unplaceable_limits, const std::vector<int>& group_size, const FGStrategy& strategy);
|
||||
int calc_cost(const std::vector<int>& labels, const std::vector<int>& medoids);
|
||||
private:
|
||||
std::vector<std::vector<float>>m_distance_matrix;
|
||||
int m_filament_num;
|
||||
std::vector<int>m_max_group_size;
|
||||
std::set<int>m_medoids_set;
|
||||
const int k = 2;
|
||||
private:
|
||||
std::vector<int>m_filament_labels;
|
||||
};
|
||||
std::shared_ptr<FlushDistanceEvaluator> m_evaluator;
|
||||
std::map<int, int>m_unplaceable_limits;
|
||||
std::vector<int>m_max_cluster_size;
|
||||
int m_elem_count;
|
||||
const int m_k = 2;
|
||||
|
||||
std::vector<int>m_cluster_labels;
|
||||
};
|
||||
}
|
||||
#endif // !FILAMENT_GROUP_HPP
|
||||
|
|
|
@ -24,6 +24,60 @@ namespace Slic3r {
|
|||
|
||||
const static bool g_wipe_into_objects = false;
|
||||
|
||||
static std::set<int>get_filament_by_type(const std::vector<unsigned int>& used_filaments, const PrintConfig* print_config, const std::string& type)
|
||||
{
|
||||
std::set<int> target_filaments;
|
||||
for (unsigned int filament_id : used_filaments) {
|
||||
std::string filament_type = print_config->filament_type.get_at(filament_id);
|
||||
if (filament_type == type)
|
||||
target_filaments.insert(filament_id);
|
||||
}
|
||||
return target_filaments;
|
||||
}
|
||||
|
||||
std::vector<std::set<int>> ToolOrdering::get_physical_unprintables(const std::vector<unsigned int>& used_filaments, const PrintConfig* config, int master_extruder_id)
|
||||
{
|
||||
auto tpu_filaments = get_filament_by_type(used_filaments, config, "TPU");
|
||||
if (tpu_filaments.size() > 1) {
|
||||
throw Slic3r::RuntimeError(std::string("Only supports up to one TPU filament."));
|
||||
}
|
||||
|
||||
// consider tpu, only place tpu in extruder with ams
|
||||
std::vector<std::set<int>>physical_unprintables(config->nozzle_diameter.size());
|
||||
int extruder_without_tpu = 1 - master_extruder_id;
|
||||
for (auto& f : tpu_filaments)
|
||||
physical_unprintables[extruder_without_tpu].insert(f);
|
||||
|
||||
// consider nozzle hrc, nozzle hrc should larger than filament hrc
|
||||
for (size_t eid = 0; eid < physical_unprintables.size(); ++eid) {
|
||||
auto nozzle_type = config->nozzle_type.get_at(eid);
|
||||
int nozzle_hrc = Print::get_hrc_by_nozzle_type(NozzleType(nozzle_type));
|
||||
for (auto& f : used_filaments) {
|
||||
int filament_hrc = config->required_nozzle_HRC.get_at(f);
|
||||
if(filament_hrc>nozzle_hrc){
|
||||
physical_unprintables[eid].insert(f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return physical_unprintables;
|
||||
}
|
||||
|
||||
std::vector<std::set<int>> ToolOrdering::get_geometrical_unprintables(const std::vector<std::vector<int>>& unprintable_arrs, const PrintConfig* config)
|
||||
{
|
||||
auto arrs_idx_switched = unprintable_arrs;
|
||||
int extruder_nums = config->nozzle_diameter.size();
|
||||
std::vector<std::set<int>> unprintables(extruder_nums);
|
||||
for (auto& arr : arrs_idx_switched)
|
||||
for (auto& item : arr)
|
||||
item -= 1;
|
||||
|
||||
for (size_t idx = 0; idx < arrs_idx_switched.size(); ++idx)
|
||||
unprintables[idx] = std::set<int>(arrs_idx_switched[idx].begin(), arrs_idx_switched[idx].end());
|
||||
|
||||
return unprintables;
|
||||
}
|
||||
|
||||
// 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
|
||||
{
|
||||
|
@ -474,25 +528,6 @@ void ToolOrdering::collect_extruders(const PrintObject &object, const std::vecto
|
|||
}
|
||||
}
|
||||
|
||||
std::set<int> ToolOrdering::get_tpu_filaments() const
|
||||
{
|
||||
std::vector<unsigned int> all_filaments;
|
||||
for (const auto < : m_layer_tools) {
|
||||
append(all_filaments, lt.extruders);
|
||||
sort_remove_duplicates(all_filaments);
|
||||
}
|
||||
|
||||
std::set<int> tpu_filaments;
|
||||
for (unsigned int filament_id : all_filaments) {
|
||||
std::string filament_name = m_print->config().filament_type.get_at(filament_id);
|
||||
if (filament_name == "TPU") {
|
||||
tpu_filaments.insert(filament_id);
|
||||
}
|
||||
}
|
||||
|
||||
return tpu_filaments;
|
||||
}
|
||||
|
||||
bool ToolOrdering::check_tpu_group(std::vector<int> filament_maps) const
|
||||
{
|
||||
std::vector<unsigned int> all_filaments;
|
||||
|
@ -855,12 +890,12 @@ float get_flush_volume(const std::vector<int> &filament_maps, const std::vector<
|
|||
return flush_volume;
|
||||
}
|
||||
|
||||
std::vector<int> ToolOrdering::get_recommended_filament_maps(const std::vector<std::vector<unsigned int>>& layer_filaments, const PrintConfig *print_config)
|
||||
std::vector<int> ToolOrdering::get_recommended_filament_maps(const std::vector<std::vector<unsigned int>>& layer_filaments, const PrintConfig* print_config, const std::vector<std::set<int>>&physical_unprintables,const std::vector<std::set<int>>&geometric_unprintables)
|
||||
{
|
||||
if (!print_config || layer_filaments.empty())
|
||||
return std::vector<int>();
|
||||
|
||||
const unsigned int filament_nums = (unsigned int) (print_config->filament_colour.values.size() + EPSILON);
|
||||
const unsigned int filament_nums = (unsigned int)(print_config->filament_colour.values.size() + EPSILON);
|
||||
|
||||
// get flush matrix
|
||||
std::vector<FlushMatrix> nozzle_flush_mtx;
|
||||
|
@ -875,27 +910,27 @@ std::vector<int> ToolOrdering::get_recommended_filament_maps(const std::vector<s
|
|||
}
|
||||
|
||||
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");
|
||||
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);
|
||||
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 {
|
||||
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];
|
||||
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::vector<int>ret(filament_nums,0);
|
||||
std::vector<int>ret(filament_nums, 0);
|
||||
// if mutli_extruder, calc group,otherwise set to 0
|
||||
if (extruder_nums == 2) {
|
||||
std::vector<std::string> extruder_ams_count_str = print_config->extruder_ams_count.values;
|
||||
|
@ -910,9 +945,32 @@ std::vector<int> ToolOrdering::get_recommended_filament_maps(const std::vector<s
|
|||
}
|
||||
}
|
||||
|
||||
FilamentGroup fg(nozzle_flush_mtx, (int) filament_nums, group_size);
|
||||
fg.get_custom_seq = get_custom_seq;
|
||||
ret = fg.calc_filament_group(layer_filaments, FGStrategy::BestFit);
|
||||
FilamentGroupContext context;
|
||||
context.flush_matrix = std::move(nozzle_flush_mtx);
|
||||
context.geometric_unprintables = geometric_unprintables;
|
||||
context.physical_unprintables = physical_unprintables;
|
||||
context.max_group_size = std::move(group_size);
|
||||
context.total_filament_num = (int)filament_nums;
|
||||
|
||||
// TODO: load master extruder id from config
|
||||
int master_extruder_id = 1;
|
||||
// speacially handle tpu filaments
|
||||
auto used_filaments = collect_sorted_used_filaments(layer_filaments);
|
||||
auto tpu_filaments = get_filament_by_type(used_filaments, print_config, "TPU");
|
||||
|
||||
if (!tpu_filaments.empty()) {
|
||||
for (size_t fidx = 0; fidx < filament_nums; ++fidx) {
|
||||
if (tpu_filaments.count(fidx))
|
||||
ret[fidx] = master_extruder_id;
|
||||
else
|
||||
ret[fidx] = 1 - master_extruder_id;
|
||||
}
|
||||
}
|
||||
else {
|
||||
FilamentGroup fg(context);
|
||||
fg.get_custom_seq = get_custom_seq;
|
||||
ret = fg.calc_filament_group(layer_filaments, FGStrategy::BestFit);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
@ -949,17 +1007,6 @@ void ToolOrdering::reorder_extruders_for_minimum_flush_volume(bool reorder_first
|
|||
using FlushMatrix = std::vector<std::vector<float>>;
|
||||
size_t nozzle_nums = print_config->nozzle_diameter.values.size();
|
||||
|
||||
std::vector<std::set<int>> extruder_tpu_status(2, std::set<int>());
|
||||
if (nozzle_nums > 1) {
|
||||
std::set<int> tpu_filaments = get_tpu_filaments();
|
||||
if (tpu_filaments.size() > 1) {
|
||||
throw Slic3r::RuntimeError(std::string("Only supports up to one TPU filament."));
|
||||
}
|
||||
|
||||
// todo multi_exturder: Need to determine whether the TPU can be placed on the left or right head according to the print model.
|
||||
extruder_tpu_status[0] = tpu_filaments;
|
||||
}
|
||||
|
||||
std::vector<FlushMatrix> nozzle_flush_mtx;
|
||||
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)));
|
||||
|
@ -972,6 +1019,17 @@ void ToolOrdering::reorder_extruders_for_minimum_flush_volume(bool reorder_first
|
|||
|
||||
std::vector<int>filament_maps(number_of_extruders, 0);
|
||||
FilamentMapMode map_mode = FilamentMapMode::fmmAuto;
|
||||
|
||||
std::vector<std::vector<unsigned int>> layer_filaments;
|
||||
for (auto& lt : m_layer_tools) {
|
||||
layer_filaments.emplace_back(lt.extruders);
|
||||
}
|
||||
|
||||
std::vector<unsigned int> used_filaments = collect_sorted_used_filaments(layer_filaments);
|
||||
|
||||
std::vector<std::set<int>>geometric_unprintables = get_geometrical_unprintables(m_print->get_unprintable_filament_ids(), print_config);
|
||||
std::vector<std::set<int>>physical_unprintables = get_physical_unprintables(used_filaments, print_config);
|
||||
|
||||
if (nozzle_nums > 1) {
|
||||
filament_maps = m_print->get_filament_maps();
|
||||
map_mode = m_print->get_filament_map_mode();
|
||||
|
@ -982,12 +1040,7 @@ void ToolOrdering::reorder_extruders_for_minimum_flush_volume(bool reorder_first
|
|||
print_config = &(m_print_object_ptr->print()->config());
|
||||
}
|
||||
|
||||
std::vector<std::vector<unsigned int>> layer_filaments;
|
||||
for (auto& lt : m_layer_tools) {
|
||||
layer_filaments.emplace_back(lt.extruders);
|
||||
}
|
||||
|
||||
filament_maps = ToolOrdering::get_recommended_filament_maps(layer_filaments, print_config);
|
||||
filament_maps = ToolOrdering::get_recommended_filament_maps(layer_filaments, print_config, physical_unprintables, geometric_unprintables);
|
||||
|
||||
if (filament_maps.empty())
|
||||
return;
|
||||
|
@ -1009,10 +1062,6 @@ void ToolOrdering::reorder_extruders_for_minimum_flush_volume(bool reorder_first
|
|||
std::vector<std::vector<unsigned int>>filament_sequences;
|
||||
std::vector<unsigned int>filament_lists(number_of_extruders);
|
||||
std::iota(filament_lists.begin(), filament_lists.end(), 0);
|
||||
std::vector<std::vector<unsigned int>>layer_filaments;
|
||||
for (auto& lt : m_layer_tools) {
|
||||
layer_filaments.emplace_back(lt.extruders);
|
||||
}
|
||||
|
||||
std::vector<LayerPrintSequence> other_layers_seqs;
|
||||
const ConfigOptionInts* other_layers_print_sequence_op = print_config->option<ConfigOptionInts>("other_layers_print_sequence");
|
||||
|
@ -1083,7 +1132,7 @@ void ToolOrdering::reorder_extruders_for_minimum_flush_volume(bool reorder_first
|
|||
if (map_mode == fmmManual)
|
||||
{
|
||||
std::vector<std::vector<unsigned int>>filament_sequences_one_extruder;
|
||||
std::vector<int>filament_maps_auto = get_recommended_filament_maps(layer_filaments, print_config);
|
||||
std::vector<int>filament_maps_auto = get_recommended_filament_maps(layer_filaments, print_config, physical_unprintables, geometric_unprintables);
|
||||
reorder_filaments_for_minimum_flush_volume(
|
||||
filament_lists,
|
||||
filament_maps_auto,
|
||||
|
|
|
@ -229,7 +229,11 @@ public:
|
|||
std::vector<LayerTools>& layer_tools() { return m_layer_tools; }
|
||||
bool has_wipe_tower() const { return ! m_layer_tools.empty() && m_first_printing_extruder != (unsigned int)-1 && m_layer_tools.front().has_wipe_tower; }
|
||||
|
||||
static std::vector<int> get_recommended_filament_maps(const std::vector<std::vector<unsigned int>>& layer_filaments, const PrintConfig *print_config);
|
||||
|
||||
static std::vector<int> get_recommended_filament_maps(const std::vector<std::vector<unsigned int>>& layer_filaments, const PrintConfig* print_config, const std::vector<std::set<int>>& physical_unprintables, const std::vector<std::set<int>>& geometric_unprintables);
|
||||
|
||||
static std::vector<std::set<int>> get_physical_unprintables(const std::vector<unsigned int>& layer_filaments, const PrintConfig* config, int master_extruder_id = 1);
|
||||
static std::vector<std::set<int>> get_geometrical_unprintables(const std::vector<std::vector<int>>& unprintable_arrs, const PrintConfig* config);
|
||||
|
||||
// should be called after doing reorder
|
||||
FilamentChangeStats get_filament_change_stats(FilamentChangeMode mode);
|
||||
|
@ -238,7 +242,6 @@ private:
|
|||
void initialize_layers(std::vector<coordf_t> &zs);
|
||||
void collect_extruders(const PrintObject &object, const std::vector<std::pair<double, unsigned int>> &per_layer_extruder_switches);
|
||||
void reorder_extruders(unsigned int last_extruder_id);
|
||||
std::set<int> get_tpu_filaments() const;
|
||||
bool check_tpu_group(std::vector<int> filament_maps) const;
|
||||
|
||||
// BBS
|
||||
|
|
|
@ -1861,19 +1861,21 @@ void Print::process(std::unordered_map<std::string, long long>* slice_time, bool
|
|||
if (this->config().print_sequence == PrintSequence::ByObject) {
|
||||
// Order object instances for sequential print.
|
||||
print_object_instances_ordering = sort_object_instances_by_model_order(*this);
|
||||
std::vector<std::vector<unsigned int>> all_filaments;
|
||||
for (print_object_instance_sequential_active = print_object_instances_ordering.begin(); print_object_instance_sequential_active != print_object_instances_ordering.end(); ++print_object_instance_sequential_active) {
|
||||
tool_ordering = ToolOrdering(*(*print_object_instance_sequential_active)->print_object, initial_extruder_id);
|
||||
for (const auto& layer_tool : tool_ordering.layer_tools()) {
|
||||
all_filaments.emplace_back(layer_tool.extruders);
|
||||
}
|
||||
}
|
||||
|
||||
auto used_filaments = collect_sorted_used_filaments(all_filaments);
|
||||
auto physical_unprintables = ToolOrdering::get_physical_unprintables(used_filaments, &m_config);
|
||||
auto geometric_unprintables = ToolOrdering::get_geometrical_unprintables(get_unprintable_filament_ids(), &m_config);
|
||||
|
||||
// get recommended filament map
|
||||
if (get_filament_map_mode() == FilamentMapMode::fmmAuto) {
|
||||
std::vector<std::vector<unsigned int>> all_filaments;
|
||||
print_object_instance_sequential_active = print_object_instances_ordering.begin();
|
||||
for (; print_object_instance_sequential_active != print_object_instances_ordering.end(); ++print_object_instance_sequential_active) {
|
||||
tool_ordering = ToolOrdering(*(*print_object_instance_sequential_active)->print_object, initial_extruder_id);
|
||||
for (const auto &layer_tool : tool_ordering.layer_tools()) {
|
||||
all_filaments.emplace_back(layer_tool.extruders);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<int> recomended_maps = ToolOrdering::get_recommended_filament_maps(all_filaments, &config());
|
||||
std::vector<int> recomended_maps = ToolOrdering::get_recommended_filament_maps(all_filaments, &config(), physical_unprintables, geometric_unprintables);
|
||||
std::transform(recomended_maps.begin(), recomended_maps.end(), recomended_maps.begin(), [](int value) { return value + 1; });
|
||||
update_filament_maps_to_config(recomended_maps);
|
||||
}
|
||||
|
|
|
@ -827,6 +827,13 @@ public:
|
|||
// get the group label of filament
|
||||
size_t get_extruder_id(unsigned int filament_id) const;
|
||||
|
||||
// 1 based ids
|
||||
const std::vector<std::vector<int>>& get_unprintable_filament_ids() const { return m_unprintable_filament_ids; }
|
||||
void set_unprintable_filament_ids(const std::vector<std::vector<int>> &filament_ids) { m_unprintable_filament_ids = filament_ids; }
|
||||
|
||||
std::vector<Vec2d> get_printable_area();
|
||||
std::vector<std::vector<Vec2d>> get_extruder_printable_area();
|
||||
|
||||
bool enable_timelapse_print() const;
|
||||
|
||||
std::string output_filename(const std::string &filename_base = std::string()) const override;
|
||||
|
|
Loading…
Reference in New Issue