ENH: refactor filament group
1.Seperate min flush max flow solver 2.Add "best match" mode for filament map 3.Refine code strucuture jira:NONE Signed-off-by: xun.zhang <xun.zhang@bambulab.com> Change-Id: If4ba09a0320366b862cec59f8ed1f22c392c53b9
This commit is contained in:
parent
e45f8c6dc2
commit
414a2105c9
|
@ -434,6 +434,8 @@ set(lisbslic3r_sources
|
|||
FlushVolPredictor.cpp
|
||||
FilamentGroup.hpp
|
||||
FilamentGroup.cpp
|
||||
FilamentGroupUtils.hpp
|
||||
FilamentGroupUtils.cpp
|
||||
GCode/ToolOrderUtils.hpp
|
||||
GCode/ToolOrderUtils.cpp
|
||||
FlushVolPredictor.hpp
|
||||
|
|
|
@ -8,54 +8,9 @@
|
|||
|
||||
namespace Slic3r
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
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 (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 (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;
|
||||
}
|
||||
|
||||
static bool check_printable(const std::vector<std::set<int>>& groups, const std::map<int,int>& unprintable)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
using namespace FilamentGroupUtils;
|
||||
// clear the array and heap,save the groups in heap to the array
|
||||
static void change_memoryed_heaps_to_arrays(FilamentGroupUtils::MemoryedGroupHeap& heap,const int total_filament_num,const std::vector<unsigned int>& used_filaments, std::vector<std::vector<int>>& arrs)
|
||||
static void change_memoryed_heaps_to_arrays(MemoryedGroupHeap& heap,const int total_filament_num,const std::vector<unsigned int>& used_filaments, std::vector<std::vector<int>>& arrs)
|
||||
{
|
||||
// switch the label idx
|
||||
arrs.clear();
|
||||
|
@ -69,49 +24,33 @@ namespace Slic3r
|
|||
}
|
||||
}
|
||||
|
||||
Color::Color(const std::string& hexstr) {
|
||||
if (hexstr.empty() || (hexstr.length() != 9 && hexstr.length() != 7) || hexstr[0] != '#')
|
||||
{
|
||||
assert(false);
|
||||
r = 0, g = 0, b = 0, a = 255;
|
||||
return;
|
||||
std::vector<int> calc_filament_group_for_tpu(const std::set<int>& tpu_filaments, const int filament_nums, const int master_extruder_id)
|
||||
{
|
||||
std::vector<int> ret(filament_nums);
|
||||
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;
|
||||
}
|
||||
|
||||
auto hexToByte = [](const std::string& hex)->unsigned char
|
||||
{
|
||||
unsigned int byte;
|
||||
std::istringstream(hex) >> std::hex >> byte;
|
||||
return static_cast<unsigned char>(byte);
|
||||
};
|
||||
r = hexToByte(hexstr.substr(1, 2));
|
||||
g = hexToByte(hexstr.substr(3, 2));
|
||||
b = hexToByte(hexstr.substr(5, 2));
|
||||
if (hexstr.size() == 9)
|
||||
a = hexToByte(hexstr.substr(7, 2));
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool can_swap_groups(const int extruder_id_0, const std::set<int>& group_0, const int extruder_id_1, const std::set<int>& group_1, const FilamentGroupContext& ctx)
|
||||
{
|
||||
std::vector<std::set<int>>extruder_unprintables(2);
|
||||
{
|
||||
std::vector<std::set<int>> physical_unprintables = ctx.physical_unprintables;
|
||||
std::vector<std::set<int>> geometric_unprintables = ctx.geometric_unprintables;
|
||||
remove_intersection(physical_unprintables[0], physical_unprintables[1]);
|
||||
remove_intersection(geometric_unprintables[0], geometric_unprintables[1]);
|
||||
std::map<int, std::vector<int>>unplaceable_limts;
|
||||
for (auto& unprintables : { physical_unprintables,geometric_unprintables }) {
|
||||
for (auto& group_id : { extruder_id_0,extruder_id_1 }) {
|
||||
for (auto f : unprintables[group_id]) {
|
||||
// TODO: xcr: check whether group_id has been inside the vector ?
|
||||
if (unplaceable_limts.count(f) == 0)
|
||||
unplaceable_limts[f].emplace_back(group_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
std::vector<std::set<int>> unprintable_filaments = ctx.model_info.unprintable_filaments;
|
||||
if (unprintable_filaments.size() > 1)
|
||||
remove_intersection(unprintable_filaments[0], unprintable_filaments[1]);
|
||||
|
||||
for (auto& elem : unplaceable_limts) {
|
||||
std::map<int, std::vector<int>>unplaceable_limts;
|
||||
for (auto& group_id : { extruder_id_0,extruder_id_1 })
|
||||
for (auto f : unprintable_filaments[group_id])
|
||||
unplaceable_limts[f].emplace_back(group_id);
|
||||
|
||||
for (auto& elem : unplaceable_limts)
|
||||
sort_remove_duplicates(elem.second);
|
||||
}
|
||||
|
||||
for (auto& elem : unplaceable_limts) {
|
||||
for (auto& eid : elem.second) {
|
||||
|
@ -137,7 +76,7 @@ namespace Slic3r
|
|||
}
|
||||
|
||||
// check extruder capacity ,if result before exchange meets the constraints and the result after exchange does not meet the constraints, return false
|
||||
if (ctx.max_group_size[extruder_id_0] >= group_0.size() && ctx.max_group_size[extruder_id_1] >= group_1.size() && (ctx.max_group_size[extruder_id_0] < group_1.size() || ctx.max_group_size[extruder_id_1] < group_0.size()))
|
||||
if (ctx.machine_info.max_group_size[extruder_id_0] >= group_0.size() && ctx.machine_info.max_group_size[extruder_id_1] >= group_1.size() && (ctx.machine_info.max_group_size[extruder_id_0] < group_1.size() || ctx.machine_info.max_group_size[extruder_id_1] < group_0.size()))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
@ -154,14 +93,14 @@ namespace Slic3r
|
|||
groups[group_id].insert(filament_id);
|
||||
}
|
||||
|
||||
int none_master_extruder_id = 1 - ctx.master_extruder_id;
|
||||
int none_master_extruder_id = 1 - ctx.machine_info.master_extruder_id;
|
||||
assert(0 <= none_master_extruder_id && none_master_extruder_id <= 1);
|
||||
|
||||
if (can_swap_groups(none_master_extruder_id, groups[none_master_extruder_id], ctx.master_extruder_id, groups[ctx.master_extruder_id], ctx)
|
||||
&& groups[none_master_extruder_id].size()>groups[ctx.master_extruder_id].size()) {
|
||||
if (can_swap_groups(none_master_extruder_id, groups[none_master_extruder_id], ctx.machine_info.master_extruder_id, groups[ctx.machine_info.master_extruder_id], ctx)
|
||||
&& groups[none_master_extruder_id].size()>groups[ctx.machine_info.master_extruder_id].size()) {
|
||||
for (auto fid : groups[none_master_extruder_id])
|
||||
filament_map[fid] = ctx.master_extruder_id;
|
||||
for (auto fid : groups[ctx.master_extruder_id])
|
||||
filament_map[fid] = ctx.machine_info.master_extruder_id;
|
||||
for (auto fid : groups[ctx.machine_info.master_extruder_id])
|
||||
filament_map[fid] = none_master_extruder_id;
|
||||
return true;
|
||||
}
|
||||
|
@ -177,28 +116,25 @@ namespace Slic3r
|
|||
*
|
||||
* @param map_lists Group list with similar flush count
|
||||
* @param used_filaments Idx of used filaments
|
||||
* @param used_filament_colors_str Colors of used filaments
|
||||
* @param ams_filament_colors_str Colors of filaments in AMS,should have same size with extruder
|
||||
* @param used_filament_colors_ Colors of used filaments
|
||||
* @param ams_filament_colors_ colors of filaments in AMS,should have same size with extruder
|
||||
* @param color_threshold Threshold for considering colors to be similar
|
||||
* @return The group that best fits the filament distribution in AMS
|
||||
*/
|
||||
std::vector<int> select_best_group_for_ams(const std::vector<std::vector<int>>& map_lists, const std::vector<unsigned int>& used_filaments, const std::vector<std::string>& used_filament_colors_str, const std::vector<std::vector<std::string>>& ams_filament_colors_str,const double color_threshold)
|
||||
std::vector<int> select_best_group_for_ams(const std::vector<std::vector<int>>& map_lists, const std::vector<unsigned int>& used_filaments, const std::vector<Color>& used_filament_colors_, const std::vector<std::vector<Color>>& ams_filament_colors_,const double color_threshold)
|
||||
{
|
||||
using namespace FlushPredict;
|
||||
// change the color str to real colors
|
||||
std::vector<Color>used_filament_colors;
|
||||
std::vector<std::vector<Color>>ams_filament_colors(2);
|
||||
for (auto& item : used_filament_colors_str)
|
||||
used_filament_colors.emplace_back(Color(item));
|
||||
for (auto& item : used_filament_colors_)
|
||||
used_filament_colors.emplace_back(item);
|
||||
|
||||
const double ams_color_dist_threshold = used_filaments.size() * color_threshold;
|
||||
|
||||
for (size_t idx = 0; idx < ams_filament_colors_str.size(); ++idx) {
|
||||
for (size_t idx = 0; idx < ams_filament_colors_.size(); ++idx) {
|
||||
std::vector<Color> tmp;
|
||||
for (auto& item : ams_filament_colors_str[idx]) {
|
||||
if (!item.empty())
|
||||
tmp.emplace_back(Color(item));
|
||||
}
|
||||
for (auto& item : ams_filament_colors_[idx])
|
||||
tmp.emplace_back(item);
|
||||
ams_filament_colors[idx] = std::move(tmp);
|
||||
}
|
||||
|
||||
|
@ -233,11 +169,11 @@ namespace Slic3r
|
|||
std::vector<int>l_nodes(group_colors[i].size()), r_nodes(ams_filament_colors[i].size());
|
||||
std::iota(l_nodes.begin(), l_nodes.end(), 0);
|
||||
std::iota(r_nodes.begin(), r_nodes.end(), 0);
|
||||
MinCostMaxFlow mcmf(distance_matrix, l_nodes, r_nodes);
|
||||
GeneralMinCostSolver mcmf(distance_matrix, l_nodes, r_nodes);
|
||||
auto ams_map = mcmf.solve();
|
||||
|
||||
for (size_t idx = 0; idx < ams_map.size(); ++idx) {
|
||||
if (ams_map[idx] == -1)
|
||||
if (ams_map[idx] == MaxFlowGraph::INVALID_ID)
|
||||
continue;
|
||||
tmp_cost += distance_matrix[idx][ams_map[idx]];
|
||||
}
|
||||
|
@ -463,7 +399,7 @@ namespace Slic3r
|
|||
|
||||
void KMediods2::do_clustering(const FGStrategy& g_strategy, int timeout_ms)
|
||||
{
|
||||
FilamentGroupUtils::FlushTimeMachine T;
|
||||
FlushTimeMachine T;
|
||||
T.time_machine_start();
|
||||
|
||||
if (m_elem_count < m_k) {
|
||||
|
@ -515,30 +451,186 @@ namespace Slic3r
|
|||
this->m_cluster_labels = best_labels;
|
||||
}
|
||||
|
||||
FilamentGroup::FilamentGroup(const FilamentGroupContext& context)
|
||||
std::vector<int> FilamentGroup::calc_min_flush_group(int* cost)
|
||||
{
|
||||
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);
|
||||
|
||||
auto used_filaments = collect_sorted_used_filaments(ctx.model_info.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);
|
||||
return calc_min_flush_group_by_enum(used_filaments, cost);
|
||||
else
|
||||
return calc_filament_group_by_pam2(layer_filaments, used_filaments, g_strategy, cost, 500);
|
||||
return calc_min_flush_group_by_pam2(used_filaments, cost, 500);
|
||||
}
|
||||
|
||||
|
||||
std::vector<int> FilamentGroup::calc_filament_group(int* cost)
|
||||
{
|
||||
try {
|
||||
if (FGMode::MatchMode == ctx.group_info.mode)
|
||||
return calc_filament_group_for_match(cost);
|
||||
}
|
||||
catch (const FilamentGroupException& e) {
|
||||
}
|
||||
return calc_filament_group_for_flush(cost);
|
||||
}
|
||||
|
||||
std::vector<int> FilamentGroup::calc_filament_group_for_match(int* cost)
|
||||
{
|
||||
using namespace FlushPredict;
|
||||
|
||||
auto used_filaments = collect_sorted_used_filaments(ctx.model_info.layer_filaments);
|
||||
std::vector<Color> used_colors;
|
||||
std::vector<std::string> used_types;
|
||||
|
||||
for (auto& f : used_filaments) {
|
||||
used_colors.emplace_back(Color(ctx.model_info.filament_colors[f]));
|
||||
used_types.emplace_back(ctx.model_info.filament_types[f]);
|
||||
}
|
||||
|
||||
std::vector<FilamentInfo> machine_filaments;
|
||||
|
||||
for (size_t eid = 0; eid < ctx.machine_info.machine_filament_info.size(); ++eid) {
|
||||
for (auto& filament : ctx.machine_info.machine_filament_info[eid]) {
|
||||
if (!ctx.group_info.ignore_ext_filament || !filament.is_extended) {
|
||||
machine_filaments.emplace_back(filament);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (machine_filaments.empty())
|
||||
throw FilamentGroupException(FilamentGroupException::EmptyAmsFilaments,"Empty ams filament in For-Match mode.");
|
||||
|
||||
std::map<int, int> unprintable_limits; // key stores filament idx in used_filament, value stores unprintable extruder
|
||||
extract_unprintable_limit_indices(ctx.model_info.unprintable_filaments, used_filaments, unprintable_limits);
|
||||
|
||||
auto is_extruder_filament_compatible = [&unprintable_limits](int filament_idx, int extruder_id) {
|
||||
auto iter = unprintable_limits.find(filament_idx);
|
||||
if (iter != unprintable_limits.end() && iter->second == extruder_id)
|
||||
return false;
|
||||
return true;
|
||||
};
|
||||
|
||||
|
||||
auto build_unlink_limits = [](const std::vector<int>& l_nodes, const std::vector<int>& r_nodes, const std::function<bool(int, int)>& can_link) {
|
||||
std::unordered_map<int, std::vector<int>> unlink_limits;
|
||||
for (size_t i = 0; i < l_nodes.size(); ++i) {
|
||||
std::vector<int> unlink_filaments;
|
||||
for (size_t j = 0; j < r_nodes.size(); ++j) {
|
||||
if (!can_link(i, j))
|
||||
unlink_filaments.emplace_back(j);
|
||||
}
|
||||
if (!unlink_filaments.empty())
|
||||
unlink_limits.emplace(i, std::move(unlink_filaments));
|
||||
}
|
||||
return unlink_limits;
|
||||
};
|
||||
|
||||
|
||||
std::vector<std::vector<float>> color_dist_matrix(used_colors.size(), std::vector<float>(machine_filaments.size()));
|
||||
for (size_t i = 0; i < used_colors.size(); ++i) {
|
||||
for (size_t j = 0; j < machine_filaments.size(); ++j) {
|
||||
color_dist_matrix[i][j] = calc_color_distance(
|
||||
RGBColor(used_colors[i].r, used_colors[i].g, used_colors[i].b),
|
||||
RGBColor(machine_filaments[j].color.r, machine_filaments[j].color.g, machine_filaments[j].color.b)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<int>l_nodes(used_filaments.size());
|
||||
std::vector<int>r_nodes(machine_filaments.size());
|
||||
std::iota(r_nodes.begin(), r_nodes.end(), 0);
|
||||
std::vector<int>r_node_capacity(machine_filaments.size(),l_nodes.size());
|
||||
|
||||
std::vector<int> group(ctx.group_info.total_filament_num, ctx.machine_info.master_extruder_id);
|
||||
std::vector<int> ungrouped_filaments;
|
||||
|
||||
{
|
||||
std::iota(l_nodes.begin(), l_nodes.end(), 0);
|
||||
auto unlink_limits = build_unlink_limits(l_nodes, r_nodes, [&](int lidx, int ridx) {
|
||||
return used_types[l_nodes[lidx]] == machine_filaments[r_nodes[ridx]].type &&
|
||||
is_extruder_filament_compatible(l_nodes[lidx], machine_filaments[ridx].extruder_id);
|
||||
});
|
||||
|
||||
MatchModeGroupSolver s(color_dist_matrix, l_nodes, r_nodes, r_node_capacity, unlink_limits);
|
||||
auto ret = s.solve();
|
||||
for (size_t idx = 0; idx < ret.size(); ++idx)
|
||||
if (ret[idx] == MaxFlowGraph::INVALID_ID)
|
||||
ungrouped_filaments.emplace_back(l_nodes[idx]);
|
||||
else
|
||||
group[used_filaments[l_nodes[idx]]] = machine_filaments[r_nodes[ret[idx]]].extruder_id;
|
||||
|
||||
for (size_t idx = 0; idx < std::min(ret.size(), l_nodes.size()); ++idx)
|
||||
l_nodes[idx] = ret[idx];
|
||||
}
|
||||
if (ungrouped_filaments.empty())
|
||||
return group;
|
||||
|
||||
{
|
||||
l_nodes = ungrouped_filaments;
|
||||
ungrouped_filaments.clear();
|
||||
|
||||
auto unlink_limits = build_unlink_limits(l_nodes, r_nodes, [&](int lidx, int ridx) {
|
||||
return is_extruder_filament_compatible(l_nodes[lidx], machine_filaments[ridx].extruder_id);
|
||||
});
|
||||
|
||||
MatchModeGroupSolver s(color_dist_matrix, l_nodes, r_nodes, r_node_capacity, unlink_limits);
|
||||
auto ret = s.solve();
|
||||
for (size_t idx = 0; idx < ret.size(); ++idx) {
|
||||
if (ret[idx] == MaxFlowGraph::INVALID_ID)
|
||||
ungrouped_filaments.emplace_back(l_nodes[idx]);
|
||||
else
|
||||
group[used_filaments[l_nodes[idx]]] = machine_filaments[r_nodes[ret[idx]]].extruder_id;
|
||||
}
|
||||
}
|
||||
if (ungrouped_filaments.empty())
|
||||
return group;
|
||||
|
||||
{
|
||||
l_nodes = ungrouped_filaments;
|
||||
ungrouped_filaments.clear();
|
||||
MatchModeGroupSolver s(color_dist_matrix, l_nodes, r_nodes, r_node_capacity, {});
|
||||
auto ret = s.solve();
|
||||
for (size_t idx = 0; idx < ret.size(); ++idx) {
|
||||
if (ret[idx] == MaxFlowGraph::INVALID_ID)
|
||||
assert(false);
|
||||
else
|
||||
group[used_filaments[l_nodes[idx]]] = machine_filaments[r_nodes[ret[idx]]].extruder_id;
|
||||
}
|
||||
}
|
||||
|
||||
return group;
|
||||
}
|
||||
|
||||
std::vector<int> FilamentGroup::calc_filament_group_for_flush(int* cost)
|
||||
{
|
||||
auto used_filaments = collect_sorted_used_filaments(ctx.model_info.layer_filaments);
|
||||
|
||||
std::vector<int> ret = calc_min_flush_group(cost);
|
||||
|
||||
optimize_group_for_master_extruder(used_filaments, ctx, ret); // ignore the return value
|
||||
|
||||
std::vector<std::vector<int>> memoryed_maps = this->m_memoryed_groups;
|
||||
memoryed_maps.insert(memoryed_maps.begin(), ret);
|
||||
|
||||
std::vector<Color> used_colors;
|
||||
for (const auto& f : used_filaments)
|
||||
used_colors.push_back(Color(ctx.model_info.filament_colors[f]));
|
||||
|
||||
std::vector<std::vector<Color>> ams_colors;
|
||||
for (const auto& filament_info : ctx.machine_info.machine_filament_info) {
|
||||
ams_colors.emplace_back();
|
||||
for (const auto& info : filament_info)
|
||||
if (!ctx.group_info.ignore_ext_filament || !info.is_extended)
|
||||
ams_colors.back().push_back(info.color);
|
||||
}
|
||||
|
||||
ret = select_best_group_for_ams(memoryed_maps, used_filaments, used_colors, ams_colors);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
// 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)
|
||||
std::vector<int> FilamentGroup::calc_min_flush_group_by_enum(const std::vector<unsigned int>& used_filaments, 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
|
||||
|
@ -558,25 +650,7 @@ namespace Slic3r
|
|||
};
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
extract_unprintable_limit_indices(ctx.model_info.unprintable_filaments, used_filaments, unplaceable_limits);
|
||||
|
||||
int used_filament_num = used_filaments.size();
|
||||
uint64_t max_group_num = (static_cast<uint64_t>(1) << used_filament_num);
|
||||
|
@ -598,9 +672,9 @@ namespace Slic3r
|
|||
|
||||
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])
|
||||
if (groups[0].size() <= ctx.machine_info.max_group_size[0] && groups[1].size() <= ctx.machine_info.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])
|
||||
if (FGStrategy::BestFit == ctx.group_info.strategy && groups[0].size() >= ctx.machine_info.max_group_size[0] && groups[1].size() >= ctx.machine_info.max_group_size[1])
|
||||
prefer_level += BEST_FIT_LIMIT_REWARD;
|
||||
|
||||
std::vector<int>filament_maps(used_filament_num);
|
||||
|
@ -614,8 +688,8 @@ namespace Slic3r
|
|||
int total_cost = reorder_filaments_for_minimum_flush_volume(
|
||||
used_filaments,
|
||||
filament_maps,
|
||||
layer_filaments,
|
||||
m_context.flush_matrix,
|
||||
ctx.model_info.layer_filaments,
|
||||
ctx.model_info.flush_matrix,
|
||||
get_custom_seq,
|
||||
nullptr
|
||||
);
|
||||
|
@ -627,62 +701,48 @@ namespace Slic3r
|
|||
}
|
||||
|
||||
{
|
||||
MemoryedGroup mg(filament_maps,total_cost,prefer_level);
|
||||
update_memoryed_groups(mg, memory_threshold, memoryed_groups);
|
||||
MemoryedGroup mg(filament_maps, total_cost, prefer_level);
|
||||
update_memoryed_groups(mg, ctx.group_info.max_gap_threshold, memoryed_groups);
|
||||
}
|
||||
}
|
||||
|
||||
if (cost)
|
||||
*cost = best_cost;
|
||||
|
||||
std::vector<int> filament_labels(m_context.total_filament_num, 0);
|
||||
std::vector<int> filament_labels(ctx.group_info.total_filament_num, 0);
|
||||
for (size_t i = 0; i < best_label.size(); ++i)
|
||||
filament_labels[used_filaments[i]] = best_label[i];
|
||||
|
||||
|
||||
change_memoryed_heaps_to_arrays(memoryed_groups, m_context.total_filament_num, used_filaments, m_memoryed_groups);
|
||||
change_memoryed_heaps_to_arrays(memoryed_groups, ctx.group_info.total_filament_num, used_filaments, m_memoryed_groups);
|
||||
|
||||
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> FilamentGroup::calc_min_flush_group_by_pam2(const std::vector<unsigned int>& used_filaments, int* cost, int timeout_ms)
|
||||
{
|
||||
std::vector<int>filament_labels_ret(m_context.total_filament_num, m_context.master_extruder_id);
|
||||
std::vector<int>filament_labels_ret(ctx.group_info.total_filament_num, ctx.machine_info.master_extruder_id);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
extract_unprintable_limit_indices(ctx.model_info.unprintable_filaments, used_filaments, unplaceable_limits);
|
||||
|
||||
auto distance_evaluator = std::make_shared<FlushDistanceEvaluator>(m_context.flush_matrix[0], used_filaments, layer_filaments);
|
||||
KMediods2 PAM((int)used_filaments.size(),distance_evaluator,m_context.master_extruder_id);
|
||||
PAM.set_max_cluster_size(m_context.max_group_size);
|
||||
auto distance_evaluator = std::make_shared<FlushDistanceEvaluator>(ctx.model_info.flush_matrix[0], used_filaments, ctx.model_info.layer_filaments);
|
||||
KMediods2 PAM((int)used_filaments.size(), distance_evaluator, ctx.machine_info.master_extruder_id);
|
||||
PAM.set_max_cluster_size(ctx.machine_info.max_group_size);
|
||||
PAM.set_unplaceable_limits(unplaceable_limits);
|
||||
PAM.set_memory_threshold(memory_threshold);
|
||||
PAM.do_clustering(g_strategy, timeout_ms);
|
||||
PAM.set_memory_threshold(ctx.group_info.max_gap_threshold);
|
||||
PAM.do_clustering(ctx.group_info.strategy, timeout_ms);
|
||||
|
||||
std::vector<int>filament_labels = PAM.get_cluster_labels();
|
||||
|
||||
{
|
||||
auto memoryed_groups = PAM.get_memoryed_groups();
|
||||
change_memoryed_heaps_to_arrays(memoryed_groups, m_context.total_filament_num, used_filaments, m_memoryed_groups);
|
||||
change_memoryed_heaps_to_arrays(memoryed_groups, ctx.group_info.total_filament_num, used_filaments, m_memoryed_groups);
|
||||
}
|
||||
|
||||
if(cost)
|
||||
*cost=reorder_filaments_for_minimum_flush_volume(used_filaments,filament_labels,layer_filaments,m_context.flush_matrix,std::nullopt,nullptr);
|
||||
if (cost)
|
||||
*cost = reorder_filaments_for_minimum_flush_volume(used_filaments, filament_labels, ctx.model_info.layer_filaments, ctx.model_info.flush_matrix, std::nullopt, nullptr);
|
||||
|
||||
for (int i = 0; i < filament_labels.size(); ++i)
|
||||
filament_labels_ret[used_filaments[i]] = filament_labels[i];
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#include <vector>
|
||||
#include <queue>
|
||||
#include "GCode/ToolOrderUtils.hpp"
|
||||
#include "FilamentGroupUtils.hpp"
|
||||
|
||||
const static int DEFAULT_CLUSTER_SIZE = 16;
|
||||
|
||||
|
@ -23,14 +24,9 @@ namespace Slic3r
|
|||
BestFit
|
||||
};
|
||||
|
||||
struct Color
|
||||
{
|
||||
unsigned char r = 0;
|
||||
unsigned char g = 0;
|
||||
unsigned char b = 0;
|
||||
unsigned char a = 255;
|
||||
Color(unsigned char r_ = 0, unsigned char g_ = 0, unsigned char b_ = 0, unsigned a_ = 255) :r(r_), g(g_), b(b_), a(a_) {}
|
||||
Color(const std::string& hexstr);
|
||||
enum FGMode {
|
||||
FlushMode,
|
||||
MatchMode
|
||||
};
|
||||
|
||||
namespace FilamentGroupUtils
|
||||
|
@ -65,6 +61,7 @@ namespace Slic3r
|
|||
int prefer_level{ 0 };
|
||||
std::vector<int>group;
|
||||
};
|
||||
|
||||
using MemoryedGroupHeap = std::priority_queue<MemoryedGroup, std::vector<MemoryedGroup>, std::greater<MemoryedGroup>>;
|
||||
|
||||
void update_memoryed_groups(const MemoryedGroup& item,const double gap_threshold, MemoryedGroupHeap& groups);
|
||||
|
@ -72,20 +69,38 @@ namespace Slic3r
|
|||
|
||||
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;
|
||||
int master_extruder_id;
|
||||
struct ModelInfo {
|
||||
std::vector<FlushMatrix> flush_matrix;
|
||||
std::vector<std::vector<unsigned int>> layer_filaments;
|
||||
std::vector<std::string> filament_colors;
|
||||
std::vector<std::string> filament_types;
|
||||
std::vector<std::set<int>> unprintable_filaments;
|
||||
} model_info;
|
||||
|
||||
struct GroupInfo {
|
||||
int total_filament_num;
|
||||
double max_gap_threshold;
|
||||
FGMode mode;
|
||||
FGStrategy strategy;
|
||||
bool ignore_ext_filament; //wai gua filament
|
||||
} group_info;
|
||||
|
||||
struct MachineInfo {
|
||||
std::vector<int> max_group_size;
|
||||
std::vector<std::vector<FilamentGroupUtils::FilamentInfo>> machine_filament_info;
|
||||
std::vector<std::pair<std::set<int>, int>> extruder_group_size;
|
||||
int master_extruder_id;
|
||||
} machine_info;
|
||||
};
|
||||
|
||||
std::vector<int> select_best_group_for_ams(const std::vector<std::vector<int>>& map_lists, const std::vector<unsigned int>& used_filaments, const std::vector<std::string>& used_filament_colors, const std::vector<std::vector<std::string>>& ams_filament_colros,const double color_delta_threshold = 20);
|
||||
std::vector<int> select_best_group_for_ams(const std::vector<std::vector<int>>& map_lists, const std::vector<unsigned int>& used_filaments, const std::vector<FilamentGroupUtils::Color>& used_filament_colors_, const std::vector<std::vector<FilamentGroupUtils::Color>>& ams_filament_colros_,const double color_delta_threshold = 20);
|
||||
|
||||
bool optimize_group_for_master_extruder(const std::vector<unsigned int>& used_filaments, const FilamentGroupContext& ctx, std::vector<int>& filament_map);
|
||||
|
||||
bool can_swap_groups(const int extruder_id_0, const std::set<int>& group_0, const int extruder_id_1, const std::set<int>& group_1, const FilamentGroupContext& ctx);
|
||||
|
||||
std::vector<int> calc_filament_group_for_tpu(const std::set<int>& tpu_filaments, const int filament_nums, const int master_extruder_id);
|
||||
|
||||
class FlushDistanceEvaluator
|
||||
{
|
||||
public:
|
||||
|
@ -99,19 +114,24 @@ namespace Slic3r
|
|||
|
||||
class FilamentGroup
|
||||
{
|
||||
using MemoryedGroupHeap = FilamentGroupUtils::MemoryedGroupHeap;
|
||||
using MemoryedGroup = FilamentGroupUtils::MemoryedGroup;
|
||||
using MemoryedGroupHeap = FilamentGroupUtils::MemoryedGroupHeap;
|
||||
public:
|
||||
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);
|
||||
explicit FilamentGroup(const FilamentGroupContext& ctx_) :ctx(ctx_) {}
|
||||
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_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);
|
||||
void set_memory_threshold(double threshold) { memory_threshold = threshold; }
|
||||
std::vector<int> calc_filament_group(int * cost = nullptr);
|
||||
std::vector<std::vector<int>> get_memoryed_groups()const { return m_memoryed_groups; }
|
||||
|
||||
public:
|
||||
std::vector<int> calc_filament_group_for_match(int* cost = nullptr);
|
||||
std::vector<int> calc_filament_group_for_flush(int* cost = nullptr);
|
||||
|
||||
private:
|
||||
FilamentGroupContext m_context;
|
||||
double memory_threshold{ 0 };
|
||||
std::vector<int> calc_min_flush_group(int* cost = nullptr);
|
||||
std::vector<int> calc_min_flush_group_by_enum(const std::vector<unsigned int>& used_filaments, int* cost = nullptr);
|
||||
std::vector<int> calc_min_flush_group_by_pam2(const std::vector<unsigned int>& used_filaments, int* cost = nullptr, int timeout_ms = 300);
|
||||
private:
|
||||
FilamentGroupContext ctx;
|
||||
std::vector<std::vector<int>> m_memoryed_groups;
|
||||
|
||||
public:
|
||||
|
|
|
@ -0,0 +1,167 @@
|
|||
#include "FilamentGroupUtils.hpp"
|
||||
|
||||
|
||||
namespace Slic3r
|
||||
{
|
||||
namespace FilamentGroupUtils
|
||||
{
|
||||
Color::Color(const std::string& hexstr) {
|
||||
if (hexstr.empty() || (hexstr.length() != 9 && hexstr.length() != 7) || hexstr[0] != '#')
|
||||
{
|
||||
assert(false);
|
||||
r = 0, g = 0, b = 0, a = 255;
|
||||
return;
|
||||
}
|
||||
|
||||
auto hexToByte = [](const std::string& hex)->unsigned char
|
||||
{
|
||||
unsigned int byte;
|
||||
std::istringstream(hex) >> std::hex >> byte;
|
||||
return static_cast<unsigned char>(byte);
|
||||
};
|
||||
r = hexToByte(hexstr.substr(1, 2));
|
||||
g = hexToByte(hexstr.substr(3, 2));
|
||||
b = hexToByte(hexstr.substr(5, 2));
|
||||
if (hexstr.size() == 9)
|
||||
a = hexToByte(hexstr.substr(7, 2));
|
||||
}
|
||||
|
||||
// TODO: add explanation
|
||||
std::vector<int> calc_max_group_size(const std::vector<std::map<int, int>>& ams_counts, bool ignore_ext_filament) {
|
||||
// add default value to 2
|
||||
std::vector<int>group_size(2, 0);
|
||||
for (size_t idx = 0; idx < ams_counts.size(); ++idx) {
|
||||
const auto& ams_count = ams_counts[idx];
|
||||
for (auto iter = ams_count.begin(); iter != ams_count.end(); ++iter) {
|
||||
group_size[idx] += iter->first * iter->second;
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t idx = 0; idx < group_size.size(); ++idx) {
|
||||
if (!ignore_ext_filament && group_size[idx] == 0)
|
||||
group_size[idx] = 1;
|
||||
}
|
||||
return group_size;
|
||||
}
|
||||
|
||||
|
||||
std::vector<std::vector<FilamentInfo>> build_machine_filaments(const std::vector<std::vector<DynamicPrintConfig>>& filament_configs)
|
||||
{
|
||||
// defualt size set to 2
|
||||
std::vector<std::vector<FilamentInfo>> machine_filaments(2);
|
||||
for (size_t idx = 0; idx < filament_configs.size(); ++idx) {
|
||||
auto& arr = filament_configs[idx];
|
||||
for (auto& item : arr) {
|
||||
FilamentInfo temp;
|
||||
temp.color = Color(item.option<ConfigOptionStrings>("filament_colour")->get_at(0));
|
||||
temp.type = item.option<ConfigOptionStrings>("filament_type")->get_at(0);
|
||||
temp.extruder_id = idx;
|
||||
temp.is_extended = item.option<ConfigOptionStrings>("tray_name")->get_at(0) == "Ext"; // hard-coded ext flag
|
||||
machine_filaments[idx].emplace_back(std::move(temp));
|
||||
}
|
||||
}
|
||||
return machine_filaments;
|
||||
}
|
||||
|
||||
bool collect_unprintable_limits(const std::vector<std::set<int>>& physical_unprintables, const std::vector<std::set<int>>& geometric_unprintables, std::vector<std::set<int>>& unprintable_limits)
|
||||
{
|
||||
unprintable_limits.clear();
|
||||
unprintable_limits.resize(2);
|
||||
// resize unprintables to 2
|
||||
auto resized_physical_unprintables = physical_unprintables;
|
||||
resized_physical_unprintables.resize(2);
|
||||
auto resized_geometric_unprintables = geometric_unprintables;
|
||||
resized_geometric_unprintables.resize(2);
|
||||
|
||||
bool conflict = false;
|
||||
conflict |= remove_intersection(resized_physical_unprintables[0], resized_physical_unprintables[1]);
|
||||
conflict |= remove_intersection(resized_geometric_unprintables[0], resized_geometric_unprintables[1]);
|
||||
|
||||
std::map<int, int>filament_unprintable_exts;
|
||||
for (auto& ext_unprintables : { resized_physical_unprintables,resized_geometric_unprintables }) {
|
||||
for (int eid = 0; eid < ext_unprintables.size(); ++eid) {
|
||||
for (int fid : ext_unprintables[eid]) {
|
||||
if (auto iter = filament_unprintable_exts.find(fid); iter != filament_unprintable_exts.end() && iter->second != eid)
|
||||
conflict = true;
|
||||
else
|
||||
filament_unprintable_exts[fid] = eid;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (auto& elem : filament_unprintable_exts)
|
||||
unprintable_limits[elem.second].insert(elem.first);
|
||||
|
||||
return !conflict;
|
||||
}
|
||||
|
||||
bool 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));
|
||||
bool have_intersection = !intersection.empty();
|
||||
for (auto& item : intersection) {
|
||||
a.erase(item);
|
||||
b.erase(item);
|
||||
}
|
||||
return have_intersection;
|
||||
}
|
||||
|
||||
void extract_indices(const std::vector<unsigned int>& used_filaments, const std::vector<std::set<int>>& unprintable_elems, std::vector<std::set<int>>& unprintable_idxs)
|
||||
{
|
||||
std::vector<std::set<int>>(unprintable_elems.size()).swap(unprintable_idxs);
|
||||
for (size_t gid = 0; gid < unprintable_elems.size(); ++gid) {
|
||||
for (auto& f : unprintable_elems[gid]) {
|
||||
auto iter = std::find(used_filaments.begin(), used_filaments.end(), (unsigned)f);
|
||||
if (iter != used_filaments.end())
|
||||
unprintable_idxs[gid].insert(iter - used_filaments.begin());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void extract_unprintable_limit_indices(const std::vector<std::set<int>>& unprintable_elems, const std::vector<unsigned int>& used_filaments, std::map<int, int>& unplaceable_limits)
|
||||
{
|
||||
unplaceable_limits.clear();
|
||||
// map the unprintable filaments to idx of used filaments , if not used ,just ignore
|
||||
std::vector<std::set<int>> unprintable_idxs;
|
||||
extract_indices(used_filaments, unprintable_elems, unprintable_idxs);
|
||||
if (unprintable_idxs.size() > 1)
|
||||
remove_intersection(unprintable_idxs[0], unprintable_idxs[1]);
|
||||
|
||||
for (size_t idx = 0; idx < unprintable_idxs.size(); ++idx) {
|
||||
for (auto f : unprintable_idxs[idx])
|
||||
if (unplaceable_limits.count(f) == 0)
|
||||
unplaceable_limits[f] = idx;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void extract_unprintable_limit_indices(const std::vector<std::set<int>>& unprintable_elems, const std::vector<unsigned int>& used_filaments, std::unordered_map<int, std::vector<int>>& unplaceable_limits)
|
||||
{
|
||||
unplaceable_limits.clear();
|
||||
std::vector<std::set<int>>unprintable_idxs;
|
||||
// map the unprintable filaments to idx of used filaments , if not used ,just ignore
|
||||
extract_indices(used_filaments, unprintable_elems, unprintable_idxs);
|
||||
// remove elems that cannot be printed in both extruder
|
||||
if (unprintable_idxs.size() > 1)
|
||||
remove_intersection(unprintable_idxs[0], unprintable_idxs[1]);
|
||||
|
||||
for (size_t group_id = 0; group_id < unprintable_idxs.size(); ++group_id)
|
||||
for (auto f : unprintable_idxs[group_id])
|
||||
unplaceable_limits[f].emplace_back(group_id);
|
||||
|
||||
for (auto& elem : unplaceable_limits)
|
||||
sort_remove_duplicates(elem.second);
|
||||
}
|
||||
|
||||
bool check_printable(const std::vector<std::set<int>>& groups, const std::map<int,int>& unprintable)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,80 @@
|
|||
#ifndef FILAMENT_GROUP_UTILS_HPP
|
||||
#define FILAMENT_GROUP_UTILS_HPP
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <exception>
|
||||
|
||||
#include "PrintConfig.hpp"
|
||||
|
||||
namespace Slic3r
|
||||
{
|
||||
namespace FilamentGroupUtils
|
||||
{
|
||||
struct Color
|
||||
{
|
||||
unsigned char r = 0;
|
||||
unsigned char g = 0;
|
||||
unsigned char b = 0;
|
||||
unsigned char a = 255;
|
||||
Color(unsigned char r_ = 0, unsigned char g_ = 0, unsigned char b_ = 0, unsigned a_ = 255) :r(r_), g(g_), b(b_), a(a_) {}
|
||||
Color(const std::string& hexstr);
|
||||
};
|
||||
|
||||
|
||||
struct FilamentInfo {
|
||||
Color color;
|
||||
std::string type;
|
||||
int extruder_id;
|
||||
bool is_extended; // TODO: rename
|
||||
};
|
||||
|
||||
|
||||
class FilamentGroupException: public std::exception {
|
||||
public:
|
||||
enum ErrorCode {
|
||||
EmptyAmsFilaments,
|
||||
ConflictLimits,
|
||||
Unknown
|
||||
};
|
||||
|
||||
private:
|
||||
ErrorCode code_;
|
||||
std::string message_;
|
||||
|
||||
public:
|
||||
FilamentGroupException(ErrorCode code, const std::string& message)
|
||||
: code_(code), message_(message) {}
|
||||
|
||||
ErrorCode code() const noexcept {
|
||||
return code_;
|
||||
}
|
||||
|
||||
const char* what() const noexcept override {
|
||||
return message_.c_str();
|
||||
}
|
||||
};
|
||||
|
||||
std::vector<int> calc_max_group_size(const std::vector<std::map<int, int>>& ams_counts,bool ignore_ext_filament);
|
||||
|
||||
std::vector<std::vector<FilamentInfo>> build_machine_filaments(const std::vector<std::vector<DynamicPrintConfig>>& filament_configs);
|
||||
|
||||
bool collect_unprintable_limits(const std::vector<std::set<int>>& physical_unprintables, const std::vector<std::set<int>>& geometric_unprintables, std::vector<std::set<int>>& unprintable_limits);
|
||||
|
||||
bool remove_intersection(std::set<int>& a, std::set<int>& b);
|
||||
|
||||
void extract_indices(const std::vector<unsigned int>& used_filaments, const std::vector<std::set<int>>& unprintable_elems, std::vector<std::set<int>>& unprintable_idxs);
|
||||
|
||||
void extract_unprintable_limit_indices(const std::vector<std::set<int>>& unprintable_elems, const std::vector<unsigned int>& used_filaments, std::map<int, int>& unplaceable_limits);
|
||||
|
||||
void extract_unprintable_limit_indices(const std::vector<std::set<int>>& unprintable_elems, const std::vector<unsigned int>& used_filaments, std::unordered_map<int, std::vector<int>>& unplaceable_limits);
|
||||
|
||||
bool check_printable(const std::vector<std::set<int>>& groups, const std::map<int, int>& unprintable);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
|
@ -7,8 +7,119 @@
|
|||
|
||||
namespace Slic3r
|
||||
{
|
||||
struct MinCostMaxFlow {
|
||||
public:
|
||||
struct Edge {
|
||||
int from, to, capacity, cost, flow;
|
||||
Edge(int u, int v, int cap, int cst) : from(u), to(v), capacity(cap), cost(cst), flow(0) {}
|
||||
};
|
||||
|
||||
MaxFlow::MaxFlow(const std::vector<int>& u_nodes, const std::vector<int>& v_nodes,
|
||||
std::vector<int> solve();
|
||||
void add_edge(int from, int to, int capacity, int cost);
|
||||
bool spfa(int source, int sink);
|
||||
int get_distance(int idx_in_left, int idx_in_right);
|
||||
|
||||
std::vector<std::vector<float>> matrix;
|
||||
std::vector<int> l_nodes;
|
||||
std::vector<int> r_nodes;
|
||||
std::vector<Edge> edges;
|
||||
std::vector<std::vector<int>> adj;
|
||||
|
||||
int total_nodes{ -1 };
|
||||
int source_id{ -1 };
|
||||
int sink_id{ -1 };
|
||||
};
|
||||
|
||||
std::vector<int> MinCostMaxFlow::solve()
|
||||
{
|
||||
while (spfa(source_id, sink_id));
|
||||
|
||||
std::vector<int>matching(l_nodes.size(), MaxFlowGraph::INVALID_ID);
|
||||
// to get the match info, just traverse the left nodes and
|
||||
// check the edges with flow > 0 and linked to right nodes
|
||||
for (int u = 0; u < l_nodes.size(); ++u) {
|
||||
for (int eid : adj[u]) {
|
||||
Edge& e = edges[eid];
|
||||
if (e.flow > 0 && e.to >= l_nodes.size() && e.to < l_nodes.size() + r_nodes.size())
|
||||
matching[e.from] = r_nodes[e.to - l_nodes.size()];
|
||||
}
|
||||
}
|
||||
|
||||
return matching;
|
||||
}
|
||||
|
||||
void MinCostMaxFlow::add_edge(int from, int to, int capacity, int cost)
|
||||
{
|
||||
adj[from].emplace_back(edges.size());
|
||||
edges.emplace_back(from, to, capacity, cost);
|
||||
//also add reverse edge ,set capacity to zero,cost to negative
|
||||
adj[to].emplace_back(edges.size());
|
||||
edges.emplace_back(to, from, 0, -cost);
|
||||
}
|
||||
|
||||
bool MinCostMaxFlow::spfa(int source, int sink)
|
||||
{
|
||||
std::vector<int>dist(total_nodes, MaxFlowGraph::INF);
|
||||
std::vector<bool>in_queue(total_nodes, false);
|
||||
std::vector<int>flow(total_nodes, MaxFlowGraph::INF);
|
||||
std::vector<int>prev(total_nodes, 0);
|
||||
|
||||
std::queue<int>q;
|
||||
q.push(source);
|
||||
in_queue[source] = true;
|
||||
dist[source] = 0;
|
||||
|
||||
while (!q.empty()) {
|
||||
int now_at = q.front();
|
||||
q.pop();
|
||||
in_queue[now_at] = false;
|
||||
|
||||
for (auto eid : adj[now_at]) //traverse all linked edges
|
||||
{
|
||||
Edge& e = edges[eid];
|
||||
if (e.flow<e.capacity && dist[e.to]>dist[now_at] + e.cost) {
|
||||
dist[e.to] = dist[now_at] + e.cost;
|
||||
prev[e.to] = eid;
|
||||
flow[e.to] = std::min(flow[now_at], e.capacity - e.flow);
|
||||
if (!in_queue[e.to]) {
|
||||
q.push(e.to);
|
||||
in_queue[e.to] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (dist[sink] == MaxFlowGraph::INF)
|
||||
return false;
|
||||
|
||||
int now_at = sink;
|
||||
while (now_at != source) {
|
||||
int prev_edge = prev[now_at];
|
||||
edges[prev_edge].flow += flow[sink];
|
||||
edges[prev_edge ^ 1].flow -= flow[sink];
|
||||
now_at = edges[prev_edge].from;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int MinCostMaxFlow::get_distance(int idx_in_left, int idx_in_right)
|
||||
{
|
||||
if (l_nodes[idx_in_left] == -1) {
|
||||
return 0;
|
||||
//TODO: test more here
|
||||
int sum = 0;
|
||||
for (int i = 0; i < matrix.size(); ++i)
|
||||
sum += matrix[i][idx_in_right];
|
||||
sum /= matrix.size();
|
||||
return -sum;
|
||||
}
|
||||
|
||||
return matrix[l_nodes[idx_in_left]][r_nodes[idx_in_right]];
|
||||
}
|
||||
|
||||
|
||||
MaxFlowSolver::MaxFlowSolver(const std::vector<int>& u_nodes, const std::vector<int>& v_nodes,
|
||||
const std::unordered_map<int, std::vector<int>>& uv_link_limits,
|
||||
const std::unordered_map<int, std::vector<int>>& uv_unlink_limits,
|
||||
const std::vector<int>& u_capacity,
|
||||
|
@ -58,7 +169,7 @@ namespace Slic3r
|
|||
}
|
||||
}
|
||||
|
||||
void MaxFlow::add_edge(int from, int to, int capacity)
|
||||
void MaxFlowSolver::add_edge(int from, int to, int capacity)
|
||||
{
|
||||
adj[from].emplace_back(edges.size());
|
||||
edges.emplace_back(from, to, capacity);
|
||||
|
@ -67,14 +178,14 @@ namespace Slic3r
|
|||
edges.emplace_back(to, from, 0);
|
||||
}
|
||||
|
||||
std::vector<int> MaxFlow::solve() {
|
||||
std::vector<int> MaxFlowSolver::solve() {
|
||||
std::vector<int> augment;
|
||||
std::vector<int> previous(total_nodes, 0);
|
||||
while (1) {
|
||||
std::vector<int>(total_nodes, 0).swap(augment);
|
||||
std::queue<int> travel;
|
||||
travel.push(source_id);
|
||||
augment[source_id] = INF;
|
||||
augment[source_id] = MaxFlowGraph::INF;
|
||||
while (!travel.empty()) {
|
||||
int from = travel.front();
|
||||
travel.pop();
|
||||
|
@ -104,7 +215,7 @@ namespace Slic3r
|
|||
}
|
||||
}
|
||||
|
||||
std::vector<int> matching(l_nodes.size(), -1);
|
||||
std::vector<int> matching(l_nodes.size(), MaxFlowGraph::INVALID_ID);
|
||||
// to get the match info, just traverse the left nodes and
|
||||
// check the edge with flow > 0 and linked to right nodes
|
||||
for (int u = 0; u < l_nodes.size(); ++u) {
|
||||
|
@ -117,7 +228,52 @@ namespace Slic3r
|
|||
return matching;
|
||||
}
|
||||
|
||||
MinCostMaxFlow::MinCostMaxFlow(const std::vector<std::vector<float>>& matrix_, const std::vector<int>& u_nodes, const std::vector<int>& v_nodes,
|
||||
GeneralMinCostSolver::~GeneralMinCostSolver()
|
||||
{
|
||||
}
|
||||
|
||||
GeneralMinCostSolver::GeneralMinCostSolver(const std::vector<std::vector<float>>& matrix_, const std::vector<int>& u_nodes, const std::vector<int>& v_nodes)
|
||||
{
|
||||
m_solver = std::make_unique<MinCostMaxFlow>();
|
||||
m_solver->matrix = matrix_;;
|
||||
m_solver->l_nodes = u_nodes;
|
||||
m_solver->r_nodes = v_nodes;
|
||||
|
||||
m_solver->total_nodes = u_nodes.size() + v_nodes.size() + 2;
|
||||
|
||||
m_solver->source_id =m_solver->total_nodes - 2;
|
||||
m_solver->sink_id = m_solver->total_nodes - 1;
|
||||
|
||||
m_solver->adj.resize(m_solver->total_nodes);
|
||||
|
||||
|
||||
// add edge from source to left nodes,cost to 0
|
||||
for (int i = 0; i < m_solver->l_nodes.size(); ++i)
|
||||
m_solver->add_edge(m_solver->source_id, i, 1, 0);
|
||||
|
||||
// add edge from right nodes to sink,cost to 0
|
||||
for (int i = 0; i < m_solver->r_nodes.size(); ++i)
|
||||
m_solver->add_edge(m_solver->l_nodes.size() + i, m_solver->sink_id, 1, 0);
|
||||
|
||||
// add edge from left node to right nodes
|
||||
for (int i = 0; i < m_solver->l_nodes.size(); ++i) {
|
||||
int from_idx = i;
|
||||
for (int j = 0; j < m_solver->r_nodes.size(); ++j) {
|
||||
int to_idx = m_solver->l_nodes.size() + j;
|
||||
m_solver->add_edge(from_idx, to_idx, 1, m_solver->get_distance(i, j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<int> GeneralMinCostSolver::solve() {
|
||||
return m_solver->solve();
|
||||
}
|
||||
|
||||
MinFlushFlowSolver::~MinFlushFlowSolver()
|
||||
{
|
||||
}
|
||||
|
||||
MinFlushFlowSolver::MinFlushFlowSolver(const std::vector<std::vector<float>>& matrix_, const std::vector<int>& u_nodes, const std::vector<int>& v_nodes,
|
||||
const std::unordered_map<int, std::vector<int>>& uv_link_limits,
|
||||
const std::unordered_map<int, std::vector<int>>& uv_unlink_limits,
|
||||
const std::vector<int>& u_capacity,
|
||||
|
@ -125,34 +281,35 @@ namespace Slic3r
|
|||
{
|
||||
assert(u_capacity.empty() || u_capacity.size() == u_nodes.size());
|
||||
assert(v_capacity.empty() || v_capacity.size() == v_nodes.size());
|
||||
matrix = matrix_;
|
||||
l_nodes = u_nodes;
|
||||
r_nodes = v_nodes;
|
||||
m_solver = std::make_unique<MinCostMaxFlow>();
|
||||
m_solver->matrix = matrix_;;
|
||||
m_solver->l_nodes = u_nodes;
|
||||
m_solver->r_nodes = v_nodes;
|
||||
|
||||
total_nodes = u_nodes.size() + v_nodes.size() + 2;
|
||||
m_solver->total_nodes = u_nodes.size() + v_nodes.size() + 2;
|
||||
|
||||
source_id = total_nodes - 2;
|
||||
sink_id = total_nodes - 1;
|
||||
m_solver->source_id =m_solver->total_nodes - 2;
|
||||
m_solver->sink_id = m_solver->total_nodes - 1;
|
||||
|
||||
adj.resize(total_nodes);
|
||||
m_solver->adj.resize(m_solver->total_nodes);
|
||||
|
||||
// add edge from source to left nodes,cost to 0
|
||||
for (int i = 0; i < l_nodes.size(); ++i) {
|
||||
for (int i = 0; i < m_solver->l_nodes.size(); ++i) {
|
||||
int capacity = u_capacity.empty() ? 1 : u_capacity[i];
|
||||
add_edge(source_id, i, capacity, 0);
|
||||
m_solver->add_edge(m_solver->source_id, i, capacity, 0);
|
||||
}
|
||||
// add edge from right nodes to sink,cost to 0
|
||||
for (int i = 0; i < r_nodes.size(); ++i) {
|
||||
for (int i = 0; i < m_solver->r_nodes.size(); ++i) {
|
||||
int capacity = v_capacity.empty() ? 1 : v_capacity[i];
|
||||
add_edge(l_nodes.size() + i, sink_id, capacity, 0);
|
||||
m_solver->add_edge(m_solver->l_nodes.size() + i, m_solver->sink_id, capacity, 0);
|
||||
}
|
||||
// add edge from left node to right nodes
|
||||
for (int i = 0; i < l_nodes.size(); ++i) {
|
||||
for (int i = 0; i < m_solver->l_nodes.size(); ++i) {
|
||||
int from_idx = i;
|
||||
// process link limits, i can only link to link_limits
|
||||
if (auto iter = uv_link_limits.find(i); iter != uv_link_limits.end()) {
|
||||
for (auto r_id : iter->second)
|
||||
add_edge(from_idx, l_nodes.size() + r_id, 1, get_distance(i, r_id));
|
||||
m_solver->add_edge(from_idx, m_solver->l_nodes.size() + r_id, 1, m_solver->get_distance(i, r_id));
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -160,100 +317,64 @@ namespace Slic3r
|
|||
std::optional<std::vector<int>> unlink_limits;
|
||||
if (auto iter = uv_unlink_limits.find(i); iter != uv_unlink_limits.end())
|
||||
unlink_limits = iter->second;
|
||||
for (int j = 0; j < r_nodes.size(); ++j) {
|
||||
for (int j = 0; j < m_solver->r_nodes.size(); ++j) {
|
||||
if (unlink_limits.has_value() && std::find(unlink_limits->begin(), unlink_limits->end(), j) != unlink_limits->end())
|
||||
continue;
|
||||
add_edge(from_idx, l_nodes.size() + j, 1, get_distance(i, j));
|
||||
m_solver->add_edge(from_idx, m_solver->l_nodes.size() + j, 1, m_solver->get_distance(i, j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<int> MinCostMaxFlow::solve()
|
||||
{
|
||||
while (spfa(source_id, sink_id));
|
||||
std::vector<int> MinFlushFlowSolver::solve() {
|
||||
return m_solver->solve();
|
||||
}
|
||||
|
||||
std::vector<int>matching(l_nodes.size(), -1);
|
||||
// to get the match info, just traverse the left nodes and
|
||||
// check the edges with flow > 0 and linked to right nodes
|
||||
for (int u = 0; u < l_nodes.size(); ++u) {
|
||||
for (int eid : adj[u]) {
|
||||
Edge& e = edges[eid];
|
||||
if (e.flow > 0 && e.to >= l_nodes.size() && e.to < l_nodes.size() + r_nodes.size())
|
||||
matching[e.from] = r_nodes[e.to - l_nodes.size()];
|
||||
MatchModeGroupSolver::~MatchModeGroupSolver()
|
||||
{
|
||||
}
|
||||
|
||||
MatchModeGroupSolver::MatchModeGroupSolver(const std::vector<std::vector<float>>& matrix_, const std::vector<int>& u_nodes, const std::vector<int>& v_nodes, const std::vector<int>& v_capacity, const std::unordered_map<int, std::vector<int>>& uv_unlink_limits)
|
||||
{
|
||||
assert(v_nodes.size() == v_capacity.size());
|
||||
m_solver = std::make_unique<MinCostMaxFlow>();
|
||||
m_solver->matrix = matrix_;;
|
||||
m_solver->l_nodes = u_nodes;
|
||||
m_solver->r_nodes = v_nodes;
|
||||
|
||||
m_solver->total_nodes = u_nodes.size() + v_nodes.size() + 2;
|
||||
|
||||
m_solver->source_id = m_solver->total_nodes - 2;
|
||||
m_solver->sink_id = m_solver->total_nodes - 1;
|
||||
|
||||
m_solver->adj.resize(m_solver->total_nodes);
|
||||
|
||||
|
||||
// add edge from source to left nodes,cost to 0
|
||||
for (int i = 0; i < m_solver->l_nodes.size(); ++i)
|
||||
m_solver->add_edge(m_solver->source_id, i, 1, 0);
|
||||
|
||||
// add edge from right nodes to sink,cost to 0
|
||||
for (int i = 0; i < m_solver->r_nodes.size(); ++i)
|
||||
m_solver->add_edge(m_solver->l_nodes.size() + i, m_solver->sink_id, v_capacity[i], 0);
|
||||
|
||||
// add edge from left node to right nodes
|
||||
for (int i = 0; i < m_solver->l_nodes.size(); ++i) {
|
||||
int from_idx = i;
|
||||
|
||||
// process unlink limits, check whether i can link to j
|
||||
std::optional<std::vector<int>> unlink_limits;
|
||||
if (auto iter = uv_unlink_limits.find(i); iter != uv_unlink_limits.end())
|
||||
unlink_limits = iter->second;
|
||||
for (int j = 0; j < m_solver->r_nodes.size(); ++j) {
|
||||
if (unlink_limits.has_value() && std::find(unlink_limits->begin(), unlink_limits->end(), j) != unlink_limits->end())
|
||||
continue;
|
||||
m_solver->add_edge(from_idx, m_solver->l_nodes.size() + j, 1, m_solver->get_distance(i, j));
|
||||
}
|
||||
}
|
||||
|
||||
return matching;
|
||||
}
|
||||
|
||||
void MinCostMaxFlow::add_edge(int from, int to, int capacity, int cost)
|
||||
{
|
||||
adj[from].emplace_back(edges.size());
|
||||
edges.emplace_back(from, to, capacity, cost);
|
||||
//also add reverse edge ,set capacity to zero,cost to negative
|
||||
adj[to].emplace_back(edges.size());
|
||||
edges.emplace_back(to, from, 0, -cost);
|
||||
}
|
||||
|
||||
bool MinCostMaxFlow::spfa(int source, int sink)
|
||||
{
|
||||
std::vector<int>dist(total_nodes, INF);
|
||||
std::vector<bool>in_queue(total_nodes, false);
|
||||
std::vector<int>flow(total_nodes, INF);
|
||||
std::vector<int>prev(total_nodes, 0);
|
||||
|
||||
std::queue<int>q;
|
||||
q.push(source);
|
||||
in_queue[source] = true;
|
||||
dist[source] = 0;
|
||||
|
||||
while (!q.empty()) {
|
||||
int now_at = q.front();
|
||||
q.pop();
|
||||
in_queue[now_at] = false;
|
||||
|
||||
for (auto eid : adj[now_at]) //traverse all linked edges
|
||||
{
|
||||
Edge& e = edges[eid];
|
||||
if (e.flow<e.capacity && dist[e.to]>dist[now_at] + e.cost) {
|
||||
dist[e.to] = dist[now_at] + e.cost;
|
||||
prev[e.to] = eid;
|
||||
flow[e.to] = std::min(flow[now_at], e.capacity - e.flow);
|
||||
if (!in_queue[e.to]) {
|
||||
q.push(e.to);
|
||||
in_queue[e.to] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (dist[sink] == INF)
|
||||
return false;
|
||||
|
||||
int now_at = sink;
|
||||
while (now_at != source) {
|
||||
int prev_edge = prev[now_at];
|
||||
edges[prev_edge].flow += flow[sink];
|
||||
edges[prev_edge ^ 1].flow -= flow[sink];
|
||||
now_at = edges[prev_edge].from;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int MinCostMaxFlow::get_distance(int idx_in_left, int idx_in_right)
|
||||
{
|
||||
if (l_nodes[idx_in_left] == -1) {
|
||||
return 0;
|
||||
//TODO: test more here
|
||||
int sum = 0;
|
||||
for (int i = 0; i < matrix.size(); ++i)
|
||||
sum += matrix[i][idx_in_right];
|
||||
sum /= matrix.size();
|
||||
return -sum;
|
||||
}
|
||||
|
||||
return matrix[l_nodes[idx_in_left]][r_nodes[idx_in_right]];
|
||||
std::vector<int> MatchModeGroupSolver::solve() {
|
||||
return m_solver->solve();
|
||||
}
|
||||
|
||||
//solve the problem by searching the least flush of current filament
|
||||
|
|
|
@ -1,24 +1,29 @@
|
|||
#ifndef TOOL_ORDER_UTILS_HPP
|
||||
#define TOOL_ORDER_UTILS_HPP
|
||||
|
||||
#include<vector>
|
||||
#include<optional>
|
||||
#include<functional>
|
||||
#include <vector>
|
||||
#include <optional>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
namespace Slic3r {
|
||||
|
||||
using FlushMatrix = std::vector<std::vector<float>>;
|
||||
|
||||
class MaxFlow
|
||||
namespace MaxFlowGraph {
|
||||
const int INF = std::numeric_limits<int>::max();
|
||||
const int INVALID_ID = -1;
|
||||
}
|
||||
|
||||
class MaxFlowSolver
|
||||
{
|
||||
private:
|
||||
const int INF = std::numeric_limits<int>::max();
|
||||
struct Edge {
|
||||
int from, to, capacity, flow;
|
||||
Edge(int u, int v, int cap) :from(u), to(v), capacity(cap), flow(0) {}
|
||||
};
|
||||
public:
|
||||
MaxFlow(const std::vector<int>& u_nodes, const std::vector<int>& v_nodes,
|
||||
MaxFlowSolver(const std::vector<int>& u_nodes, const std::vector<int>& v_nodes,
|
||||
const std::unordered_map<int, std::vector<int>>& uv_link_limits = {},
|
||||
const std::unordered_map<int, std::vector<int>>& uv_unlink_limits = {},
|
||||
const std::vector<int>& u_capacity = {},
|
||||
|
@ -29,7 +34,6 @@ public:
|
|||
private:
|
||||
void add_edge(int from, int to, int capacity);
|
||||
|
||||
|
||||
int total_nodes;
|
||||
int source_id;
|
||||
int sink_id;
|
||||
|
@ -39,41 +43,57 @@ private:
|
|||
std::vector<std::vector<int>>adj;
|
||||
};
|
||||
|
||||
class MinCostMaxFlow
|
||||
{
|
||||
const int INF = std::numeric_limits<int>::max();
|
||||
struct Edge
|
||||
{
|
||||
int from, to, capacity, cost, flow;
|
||||
Edge(int u, int v, int cap, int cst) : from(u), to(v), capacity(cap), cost(cst), flow(0) {}
|
||||
};
|
||||
|
||||
struct MinCostMaxFlow;
|
||||
|
||||
class GeneralMinCostSolver
|
||||
{
|
||||
public:
|
||||
MinCostMaxFlow(const std::vector<std::vector<float>>& matrix_, const std::vector<int>& u_nodes, const std::vector<int>& v_nodes,
|
||||
GeneralMinCostSolver(const std::vector<std::vector<float>>& matrix_,
|
||||
const std::vector<int>& u_nodes,
|
||||
const std::vector<int>& v_nodes);
|
||||
|
||||
std::vector<int> solve();
|
||||
~GeneralMinCostSolver();
|
||||
private:
|
||||
std::unique_ptr<MinCostMaxFlow> m_solver;
|
||||
};
|
||||
|
||||
|
||||
class MinFlushFlowSolver
|
||||
{
|
||||
public:
|
||||
MinFlushFlowSolver(const std::vector<std::vector<float>>& matrix_,
|
||||
const std::vector<int>& u_nodes,
|
||||
const std::vector<int>& v_nodes,
|
||||
const std::unordered_map<int, std::vector<int>>& uv_link_limits = {},
|
||||
const std::unordered_map<int, std::vector<int>>& uv_unlink_limits = {},
|
||||
const std::vector<int>& u_capacity = {},
|
||||
const std::vector<int>& v_capacity = {}
|
||||
);
|
||||
std::vector<int> solve();
|
||||
|
||||
~MinFlushFlowSolver();
|
||||
private:
|
||||
void add_edge(int from, int to, int capacity, int cost);
|
||||
bool spfa(int source, int sink);
|
||||
int get_distance(int idx_in_left, int idx_in_right);
|
||||
|
||||
private:
|
||||
std::vector<std::vector<float>> matrix;
|
||||
std::vector<int> l_nodes;
|
||||
std::vector<int> r_nodes;
|
||||
std::vector<Edge> edges;
|
||||
std::vector<std::vector<int>> adj;
|
||||
|
||||
int total_nodes;
|
||||
int source_id;
|
||||
int sink_id;
|
||||
std::unique_ptr<MinCostMaxFlow> m_solver;
|
||||
};
|
||||
|
||||
|
||||
class MatchModeGroupSolver
|
||||
{
|
||||
public:
|
||||
MatchModeGroupSolver(const std::vector<std::vector<float>>& matrix_,
|
||||
const std::vector<int>& u_nodes,
|
||||
const std::vector<int>& v_nodes,
|
||||
const std::vector<int>& v_capacity,
|
||||
const std::unordered_map<int, std::vector<int>>& uv_unlink_limits = {});
|
||||
|
||||
std::vector<int> solve();
|
||||
~MatchModeGroupSolver();
|
||||
private:
|
||||
std::unique_ptr<MinCostMaxFlow> m_solver;
|
||||
};
|
||||
|
||||
|
||||
std::vector<unsigned int> get_extruders_order(const std::vector<std::vector<float>> &wipe_volumes,
|
||||
const std::vector<unsigned int> &curr_layer_extruders,
|
||||
const std::vector<unsigned int> &next_layer_extruders,
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include "ClipperUtils.hpp"
|
||||
#include "ParameterUtils.hpp"
|
||||
#include "GCode/ToolOrderUtils.hpp"
|
||||
#include "FilamentGroupUtils.hpp"
|
||||
// #define SLIC3R_DEBUG
|
||||
|
||||
// Make assert active if SLIC3R_DEBUG
|
||||
|
@ -1010,6 +1011,7 @@ float get_flush_volume(const std::vector<int> &filament_maps, const std::vector<
|
|||
|
||||
std::vector<int> ToolOrdering::get_recommended_filament_maps(const std::vector<std::vector<unsigned int>>& layer_filaments, const PrintConfig* print_config, const Print* print, const std::vector<std::set<int>>&physical_unprintables,const std::vector<std::set<int>>&geometric_unprintables)
|
||||
{
|
||||
using namespace FilamentGroupUtils;
|
||||
if (!print_config || layer_filaments.empty())
|
||||
return std::vector<int>();
|
||||
|
||||
|
@ -1052,73 +1054,48 @@ std::vector<int> ToolOrdering::get_recommended_filament_maps(const std::vector<s
|
|||
// 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;
|
||||
auto extruder_ams_counts = get_extruder_ams_count(extruder_ams_count_str);
|
||||
std::vector<int> group_size = {16, 16};
|
||||
if (extruder_ams_counts.size() > 0) {
|
||||
assert(extruder_ams_counts.size() == 2);
|
||||
for (int i = 0; i < extruder_ams_counts.size(); ++i) {
|
||||
group_size[i] = 0;
|
||||
const auto &ams_count = extruder_ams_counts[i];
|
||||
for (auto iter = ams_count.begin(); iter != ams_count.end(); ++iter) { group_size[i] += iter->first * iter->second; }
|
||||
}
|
||||
// When the AMS count is 0, only external filament can be used, so set the capacity to 1.
|
||||
for(auto& size: group_size)
|
||||
if(size == 0)
|
||||
size = 1;
|
||||
}
|
||||
auto extruder_ams_counts = get_extruder_ams_count(extruder_ams_count_str);
|
||||
std::vector<int> group_size = calc_max_group_size(extruder_ams_counts, false);
|
||||
|
||||
auto machine_filament_info = build_machine_filaments(print->get_extruder_filament_info());
|
||||
std::vector<std::string> filament_types = print_config->filament_type.values;
|
||||
std::vector<std::string> filament_colours = print_config->filament_colour.values;
|
||||
|
||||
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;
|
||||
context.master_extruder_id = print_config->master_extruder_id.value - 1; // transfer to 0 based idx
|
||||
}
|
||||
// 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");
|
||||
FGMode fg_mode = print_config->filament_map_mode.value == FilamentMapMode::fmmAutoForMatch ? FGMode::MatchMode: FGMode::FlushMode;
|
||||
|
||||
std::vector<std::set<int>> ext_unprintable_filaments;
|
||||
collect_unprintable_limits(physical_unprintables, geometric_unprintables, ext_unprintable_filaments); // TODO: throw exception if fail or set it to status
|
||||
|
||||
FilamentGroupContext context;
|
||||
{
|
||||
context.model_info.flush_matrix = std::move(nozzle_flush_mtx);
|
||||
context.model_info.unprintable_filaments = ext_unprintable_filaments; // TODO:
|
||||
context.model_info.layer_filaments = layer_filaments;
|
||||
context.model_info.filament_colors = filament_colours;
|
||||
context.model_info.filament_types = filament_types;
|
||||
|
||||
context.machine_info.machine_filament_info = machine_filament_info;
|
||||
context.machine_info.max_group_size = std::move(group_size);
|
||||
context.machine_info.master_extruder_id = print_config->master_extruder_id.value - 1; // switch to 0 based idx
|
||||
|
||||
context.group_info.total_filament_num = (int)(filament_nums);
|
||||
context.group_info.max_gap_threshold = 0.01;
|
||||
context.group_info.strategy = FGStrategy::BestCost;
|
||||
context.group_info.mode = fg_mode;
|
||||
context.group_info.ignore_ext_filament = false; // TODO:
|
||||
}
|
||||
|
||||
|
||||
if (!tpu_filaments.empty()) {
|
||||
for (size_t fidx = 0; fidx < filament_nums; ++fidx) {
|
||||
if (tpu_filaments.count(fidx))
|
||||
ret[fidx] = context.master_extruder_id;
|
||||
else
|
||||
ret[fidx] = 1 - context.master_extruder_id;
|
||||
}
|
||||
ret = calc_filament_group_for_tpu(tpu_filaments, context.group_info.total_filament_num, context.machine_info.master_extruder_id);
|
||||
}
|
||||
else {
|
||||
FilamentGroup fg(context);
|
||||
fg.set_memory_threshold(0.02);
|
||||
fg.get_custom_seq = get_custom_seq;
|
||||
|
||||
ret = fg.calc_filament_group(layer_filaments, FGStrategy::BestCost);
|
||||
|
||||
// optimize for master extruder id
|
||||
optimize_group_for_master_extruder(used_filaments, context, ret);
|
||||
|
||||
// optimize according to AMS filaments
|
||||
std::vector<std::vector<int>>memoryed_maps{ ret };
|
||||
{
|
||||
auto tmp_maps = fg.get_memoryed_groups();
|
||||
memoryed_maps.insert(memoryed_maps.end(), std::make_move_iterator(tmp_maps.begin()), std::make_move_iterator(tmp_maps.end()));
|
||||
}
|
||||
|
||||
std::vector<std::string>used_colors;
|
||||
for (size_t idx = 0; idx < used_filaments.size(); ++idx)
|
||||
used_colors.emplace_back(print_config->filament_colour.get_at(used_filaments[idx]));
|
||||
|
||||
auto ams_filament_info = print->get_extruder_filament_info();
|
||||
std::vector<std::vector<std::string>> ams_colors(extruder_nums);
|
||||
for (size_t i = 0; i < ams_filament_info.size(); ++i) {
|
||||
auto& arr = ams_filament_info[i];
|
||||
std::vector<std::string>colors;
|
||||
for (auto& item : arr)
|
||||
colors.emplace_back(item.option<ConfigOptionStrings>("filament_colour")->get_at(0));
|
||||
ams_colors[i] = std::move(colors);
|
||||
}
|
||||
ret = select_best_group_for_ams(memoryed_maps, used_filaments, used_colors, ams_colors, similar_color_threshold_de2000);
|
||||
ret = fg.calc_filament_group();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue