feat(libslic3r): multi-nozzle slicing engine for H2C/A2L

Port BambuStudio's dual-nozzle slicing core: H2C-era config keys, filament-to-nozzle grouping with per-layer dynamic regrouping, filament/nozzle/hotend gcode placeholder vocabulary, multi-nozzle wipe tower pre-heat/pre-cool, the two-pass pre-cooling injector, and corexy farthest-point timelapse.
This commit is contained in:
SoftFever
2026-07-09 00:06:26 +08:00
parent 781ecdc2c1
commit 237ef41b06
36 changed files with 7622 additions and 708 deletions

View File

@@ -13,7 +13,8 @@
const static int DEFAULT_CLUSTER_SIZE = 16;
const static int ABSOLUTE_FLUSH_GAP_TOLERANCE = 5;
const static int ABSOLUTE_FLUSH_GAP_TOLERANCE = 10;
namespace Slic3r
{
@@ -52,12 +53,12 @@ namespace Slic3r
struct MemoryedGroup {
MemoryedGroup() = default;
MemoryedGroup(const std::vector<int>& group_, const int cost_, const int prefer_level_) :group(group_), cost(cost_), prefer_level(prefer_level_) {}
MemoryedGroup(const std::vector<int>& group_, const double cost_, const int prefer_level_) :group(group_), cost(cost_), prefer_level(prefer_level_) {}
bool operator>(const MemoryedGroup& other) const {
return prefer_level < other.prefer_level || (prefer_level == other.prefer_level && cost > other.cost);
}
int cost{ 0 };
double cost{ 0 };
int prefer_level{ 0 };
std::vector<int>group;
};
@@ -75,6 +76,7 @@ namespace Slic3r
std::vector<FilamentGroupUtils::FilamentInfo> filament_info;
std::vector<std::string> filament_ids;
std::vector<std::set<int>> unprintable_filaments;
std::map<int, std::set<NozzleVolumeType>> unprintable_volumes;
} model_info;
struct GroupInfo {
@@ -82,40 +84,64 @@ namespace Slic3r
double max_gap_threshold;
FGMode mode;
FGStrategy strategy;
bool ignore_ext_filament; //wai gua filament
bool ignore_ext_filament;
bool has_filament_switcher = false;
std::vector<int> filament_volume_map;
} group_info;
struct MachineInfo {
std::vector<int> max_group_size;
std::vector<std::vector<FilamentGroupUtils::MachineFilamentInfo>> machine_filament_info;
std::vector<std::pair<std::set<int>, int>> extruder_group_size;
std::vector<bool> prefer_non_model_filament;
int master_extruder_id;
} machine_info;
struct SpeedInfo{
std::unordered_map<int,std::unordered_map<int,double>> filament_print_time;
double extruder_change_time;
double filament_change_time;
bool group_with_time;
MultiNozzleUtils::FilamentChangeTimeParams change_time_params;
std::vector<bool> ams_preload_enabled;
} speed_info;
struct NozzleInfo {
std::map<int, std::vector<int>> extruder_nozzle_list;
std::vector<MultiNozzleUtils::NozzleInfo> nozzle_list;
std::unordered_map<int, int> nozzle_status;
} nozzle_info;
};
std::vector<int> select_best_group_for_ams(const std::vector<std::vector<int>>& map_lists,
std::vector<int> select_best_group_for_ams(const std::vector<std::vector<int>> &filament_to_nozzles,
const std::vector<MultiNozzleUtils::NozzleInfo>& nozzle_list,
const std::vector<unsigned int>& used_filaments,
const std::vector<FilamentGroupUtils::FilamentInfo>& used_filament_info,
const std::vector<std::vector<FilamentGroupUtils::MachineFilamentInfo>>& machine_filament_info,
const bool has_filament_switcher = false,
const double color_delta_threshold = 20);
std::vector<int> optimize_group_for_master_extruder(const std::vector<unsigned int>& used_filaments, const FilamentGroupContext& ctx, const 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:
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(const std::vector<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;
double get_distance(int idx_a, int idx_b, int extruder_id) const;
private:
std::vector<std::vector<float>>m_distance_matrix;
std::vector<std::vector<std::vector<float>>>m_distance_matrix;
};
class TimeEvaluator
{
public:
TimeEvaluator(const FilamentGroupContext::SpeedInfo& speed_info) : m_speed_info(speed_info) {}
double get_estimated_time(const std::vector<int>& filament_map) const;
private:
FilamentGroupContext::SpeedInfo m_speed_info;
};
class FilamentGroup
{
using MemoryedGroup = FilamentGroupUtils::MemoryedGroup;
@@ -129,11 +155,17 @@ namespace Slic3r
public:
std::vector<int> calc_filament_group_for_match(int* cost = nullptr);
std::vector<int> calc_filament_group_for_flush(int* cost = nullptr);
std::vector<int> calc_filament_group_for_tpu(int* cost = nullptr);
private:
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);
std::vector<int> calc_group_by_enum(int k, const std::vector<unsigned int>& used_filaments,
const std::unordered_map<int, std::vector<int>>& unplaceable_limits, int* cost = nullptr);
std::vector<int> calc_group_by_kmedoids(int k, const std::vector<unsigned int>& used_filaments,
const std::unordered_map<int, std::vector<int>>& unplaceable_limits, int* cost = nullptr, int timeout_ms = 500);
std::map<int, int> rebuild_unprintables(const std::vector<unsigned int>& used_filaments, const std::map<int,int>& extruder_unprintables);
std::unordered_map<int, std::vector<int>> rebuild_nozzle_unprintables(const std::vector<unsigned int>& used_filaments, const std::unordered_map<int, std::vector<int>>& extruder_unprintables, const std::vector<int>& filament_volume_map);
std::unordered_map<int, std::vector<int>> try_merge_filaments();
void rebuild_context(const std::unordered_map<int, std::vector<int>>& merged_filaments);
@@ -141,57 +173,78 @@ namespace Slic3r
private:
FilamentGroupContext ctx;
MemoryedGroupHeap m_memoryed_heap;
std::vector<std::vector<int>> m_memoryed_groups;
public:
std::optional<std::function<bool(int, std::vector<int>&)>> get_custom_seq;
};
class KMediods2
std::vector<int> calc_filament_group_for_manual_multi_nozzle(const std::vector<int>& filament_map_manual,const FilamentGroupContext& ctx);
std::vector<int> calc_filament_group_for_match_multi_nozzle(const FilamentGroupContext& ctx);
struct FilamentPlanRes
{
std::vector<int> fil_order;
std::vector<int> fil_nozzle_match;
};
std::vector<FilamentPlanRes> plan_filament_nozzle_mapping_and_order(const FilamentGroupContext& ctx);
class KMediods
{
protected:
using MemoryedGroupHeap = FilamentGroupUtils::MemoryedGroupHeap;
using MemoryedGroup = FilamentGroupUtils::MemoryedGroup;
enum INIT_TYPE
{
Random = 0,
Farthest
};
public:
KMediods2(const int elem_count, const std::shared_ptr<FlushDistanceEvaluator>& evaluator, int default_group_id = 0) :
m_evaluator{ evaluator },
m_elem_count{ elem_count },
m_default_group_id{ default_group_id }
{
m_max_cluster_size = std::vector<int>(m_k, DEFAULT_CLUSTER_SIZE);
KMediods(const int k, const int elem_count, const std::shared_ptr<FlushDistanceEvaluator>& evaluator, int default_group_id = 0) {
m_k = k;
m_evaluator = evaluator;
m_max_cluster_size = std::vector<int>(k, DEFAULT_CLUSTER_SIZE);
m_elem_count = elem_count;
m_default_group_id = default_group_id;
}
// 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 set_cluster_group_size(const std::vector<std::pair<std::set<int>,int>>& cluster_group_size);
void do_clustering(const FGStrategy& g_strategy,int timeout_ms = 100);
// key stores elem, value stores the cluster id that the elem must be placed
void set_placable_limits(const std::unordered_map<int, std::vector<int>>& placable_limits) { m_placeable_limits = placable_limits; }
// key stores elem, value stores the cluster id that the elem cannot be placed
void set_unplacable_limits(const std::unordered_map<int, std::vector<int>>& unplacable_limits) { m_unplaceable_limits = unplacable_limits; }
void set_memory_threshold(double threshold) { memory_threshold = threshold; }
MemoryedGroupHeap get_memoryed_groups()const { return memoryed_groups; }
std::vector<int>get_cluster_labels()const { return m_cluster_labels; }
void do_clustering(const FilamentGroupContext& context, int timeout_ms = 100, int retry = 10);
std::vector<int> get_cluster_labels()const { return m_cluster_labels; }
private:
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);
protected:
FilamentGroupUtils::MemoryedGroupHeap memoryed_groups;
std::shared_ptr<FlushDistanceEvaluator> m_evaluator;
std::map<int, int>m_unplaceable_limits;
std::vector<int>m_cluster_labels;
std::vector<int>m_max_cluster_size;
bool have_enough_size(const std::vector<int>& cluster_size, const std::vector<std::pair<std::set<int>, int>>& cluster_group_size,int elem_count);
// calculate cluster distance
int calc_cost(const std::vector<int>& clusters, const std::vector<int>& cluster_centers, int cluster_id = -1);
const int m_k = 2;
// get initial cluster center
std::vector<int>init_cluster_center(const std::unordered_map<int, std::vector<int>>& placeable_limits, const std::unordered_map<int, std::vector<int>>& unplaceable_limits, const std::vector<int>& cluster_size, const std::vector<std::pair<std::set<int>, int>>& cluster_group_size, int seed);
// assign each elem to the cluster
std::vector<int> assign_cluster_label(const std::vector<int>& center, const std::unordered_map<int, std::vector<int>>& placeable_limits, const std::unordered_map<int, std::vector<int>>& unplaceable_limits, const std::vector<int>& group_size, const std::vector<std::pair<std::set<int>, int>>& cluster_group_size);
protected:
MemoryedGroupHeap memoryed_groups;
std::shared_ptr<FlushDistanceEvaluator>m_evaluator;
std::unordered_map<int, std::vector<int>> m_unplaceable_limits; // key: filament, value: nozzle ids it cannot be assigned to
std::unordered_map<int, std::vector<int>> m_placeable_limits; // key: filament, value: nozzle ids it must be assigned to
std::vector<int>m_max_cluster_size; // max number of filaments each nozzle can hold
std::vector<int>m_cluster_labels; // assignment result, resolved down to nozzle id
std::vector<std::pair<std::set<int>,int>> m_cluster_group_size;
std::vector<int> m_nozzle_to_extruder;
int m_k;
int m_elem_count;
int m_default_group_id{ 0 };
double memory_threshold{ 0 };