ENH: Select group that best fit filaments in AMS

1.Only consider groups with a distance within the threshold

jira:NONE

Signed-off-by: xun.zhang <xun.zhang@bambulab.com>
Change-Id: I91526a796a0f7f1ed3e77c41076c1f85620dd944
This commit is contained in:
xun.zhang 2024-10-14 19:38:39 +08:00 committed by lane.wei
parent b17c5e7e01
commit 1379b83846
2 changed files with 26 additions and 15 deletions

View File

@ -1,5 +1,6 @@
#include "FilamentGroup.hpp"
#include "GCode/ToolOrderUtils.hpp"
#include "FlushVolPredictor.hpp"
#include <queue>
#include <random>
#include <cassert>
@ -53,16 +54,6 @@ namespace Slic3r
return true;
}
static int calc_color_distance(const Color &src, const Color &dst)
{
double rmean = (src.r + dst.r) / 2.f;
double dr = src.r - dst.r;
double dg = src.g - dst.g;
double db = src.b - dst.b;
return sqrt((512 + rmean) / 256.f * dr * dr + 4 * dg * dg + (767 - rmean) / 256 * db * db);
}
// 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)
{
@ -177,14 +168,30 @@ namespace Slic3r
return false;
}
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)
/**
* @brief Select the group that best fit the filaments in AMS
*
* Calculate the total color distance between the grouping results and the AMS filaments through
* minimum cost maximum flow. Only those with a distance difference within the threshold are
* considered valid.
*
* @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 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)
{
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));
const double ams_color_dist_threshold = used_filaments.size() * color_threshold;
for (size_t idx = 0; idx < ams_filament_colors_str.size(); ++idx) {
std::vector<Color> tmp;
@ -212,8 +219,12 @@ namespace Slic3r
// calculate color distance matrix
for (size_t src = 0; src < group_colors[i].size(); ++src) {
for (size_t dst = 0; dst < ams_filament_colors[i].size(); ++dst)
distance_matrix[src][dst] = calc_color_distance(group_colors[i][src], ams_filament_colors[i][dst]);
for (size_t dst = 0; dst < ams_filament_colors[i].size(); ++dst) {
distance_matrix[src][dst] = calc_color_distance(
RGBColor(group_colors[i][src].r, group_colors[i][src].g, group_colors[i][src].b),
RGBColor(ams_filament_colors[i][dst].r, ams_filament_colors[i][dst].g, ams_filament_colors[i][dst].b)
);
}
}
// get min cost by min cost max flow
@ -230,7 +241,7 @@ namespace Slic3r
}
}
if (tmp_cost < best_cost) {
if (tmp_cost < ams_color_dist_threshold && tmp_cost < best_cost) {
best_cost = tmp_cost;
best_map = map;
}

View File

@ -80,7 +80,7 @@ namespace Slic3r
int master_extruder_id;
};
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);
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);
bool optimize_group_for_master_extruder(const std::vector<unsigned int>& used_filaments, const FilamentGroupContext& ctx, std::vector<int>& filament_map);