2024-07-08 13:31:28 +00:00
|
|
|
#ifndef FILAMENT_GROUP_HPP
|
|
|
|
#define FILAMENT_GROUP_HPP
|
|
|
|
|
2024-09-02 13:10:01 +00:00
|
|
|
#include <chrono>
|
|
|
|
#include <memory>
|
|
|
|
#include <numeric>
|
|
|
|
#include <set>
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
2024-09-06 09:49:03 +00:00
|
|
|
#include <queue>
|
2024-08-09 06:14:23 +00:00
|
|
|
#include "GCode/ToolOrderUtils.hpp"
|
2024-07-08 13:31:28 +00:00
|
|
|
|
2024-09-02 13:10:01 +00:00
|
|
|
const static int DEFAULT_CLUSTER_SIZE = 16;
|
|
|
|
|
2024-09-06 09:49:03 +00:00
|
|
|
const static int ABSOLUTE_FLUSH_GAP_TOLERANCE = 2000;
|
|
|
|
|
2024-07-08 13:31:28 +00:00
|
|
|
namespace Slic3r
|
|
|
|
{
|
2024-09-02 13:10:01 +00:00
|
|
|
std::vector<unsigned int>collect_sorted_used_filaments(const std::vector<std::vector<unsigned int>>& layer_filaments);
|
|
|
|
|
2024-09-06 09:49:03 +00:00
|
|
|
enum FGStrategy {
|
|
|
|
BestCost,
|
|
|
|
BestFit
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Color
|
2024-07-08 13:31:28 +00:00
|
|
|
{
|
2024-09-06 09:49:03 +00:00
|
|
|
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);
|
|
|
|
};
|
2024-07-08 13:31:28 +00:00
|
|
|
|
2024-09-06 09:49:03 +00:00
|
|
|
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);
|
2024-07-08 13:31:28 +00:00
|
|
|
|
2024-09-06 09:49:03 +00:00
|
|
|
namespace FilamentGroupUtils
|
|
|
|
{
|
|
|
|
struct FlushTimeMachine
|
2024-07-08 13:31:28 +00:00
|
|
|
{
|
2024-09-06 09:49:03 +00:00
|
|
|
private:
|
|
|
|
std::chrono::high_resolution_clock::time_point start;
|
|
|
|
|
|
|
|
public:
|
|
|
|
void time_machine_start()
|
|
|
|
{
|
|
|
|
start = std::chrono::high_resolution_clock::now();
|
|
|
|
}
|
|
|
|
|
|
|
|
int time_machine_end()
|
|
|
|
{
|
|
|
|
auto end = std::chrono::high_resolution_clock::now();
|
|
|
|
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
|
|
|
return duration.count();
|
|
|
|
}
|
|
|
|
};
|
2024-07-08 13:31:28 +00:00
|
|
|
|
2024-09-06 09:49:03 +00:00
|
|
|
struct MemoryedGroup {
|
|
|
|
int cost{ 0 };
|
|
|
|
int prefer_level{ 0 };
|
|
|
|
std::vector<int>group;
|
|
|
|
bool operator>(const MemoryedGroup& other) const {
|
|
|
|
return prefer_level < other.prefer_level || (prefer_level == other.prefer_level && cost > other.cost);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
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);
|
|
|
|
}
|
2024-07-08 13:31:28 +00:00
|
|
|
|
2024-09-02 13:10:01 +00:00
|
|
|
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
|
2024-07-08 13:31:28 +00:00
|
|
|
{
|
2024-07-12 10:04:01 +00:00
|
|
|
public:
|
2024-09-02 13:10:01 +00:00
|
|
|
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;
|
2024-07-08 13:31:28 +00:00
|
|
|
|
2024-09-02 13:10:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class FilamentGroup
|
|
|
|
{
|
2024-09-06 09:49:03 +00:00
|
|
|
using MemoryedGroupHeap = FilamentGroupUtils::MemoryedGroupHeap;
|
|
|
|
using MemoryedGroup = FilamentGroupUtils::MemoryedGroup;
|
2024-09-02 13:10:01 +00:00
|
|
|
public:
|
|
|
|
FilamentGroup(const FilamentGroupContext& context);
|
2024-08-09 06:14:23 +00:00
|
|
|
std::vector<int> calc_filament_group(const std::vector<std::vector<unsigned int>>& layer_filaments, const FGStrategy& g_strategy = FGStrategy::BestFit, int* cost = nullptr);
|
2024-09-02 13:10:01 +00:00
|
|
|
public:
|
2024-08-09 06:14:23 +00:00
|
|
|
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);
|
2024-09-02 13:10:01 +00:00
|
|
|
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);
|
2024-09-06 09:49:03 +00:00
|
|
|
void set_memory_threshold(double threshold) { memory_threshold = threshold; }
|
|
|
|
std::vector<std::vector<int>> get_memoryed_groups()const { return m_memoryed_groups; }
|
2024-07-08 13:31:28 +00:00
|
|
|
private:
|
2024-09-02 13:10:01 +00:00
|
|
|
FilamentGroupContext m_context;
|
2024-09-06 09:49:03 +00:00
|
|
|
double memory_threshold{ 0 };
|
|
|
|
std::vector<std::vector<int>> m_memoryed_groups;
|
|
|
|
|
2024-07-08 13:31:28 +00:00
|
|
|
public:
|
|
|
|
std::optional<std::function<bool(int, std::vector<int>&)>> get_custom_seq;
|
|
|
|
};
|
|
|
|
|
2024-08-08 03:23:29 +00:00
|
|
|
|
2024-09-02 13:10:01 +00:00
|
|
|
class KMediods2
|
2024-07-08 13:31:28 +00:00
|
|
|
{
|
2024-09-06 09:49:03 +00:00
|
|
|
using MemoryedGroupHeap = FilamentGroupUtils::MemoryedGroupHeap;
|
|
|
|
using MemoryedGroup = FilamentGroupUtils::MemoryedGroup;
|
|
|
|
|
2024-07-08 13:31:28 +00:00
|
|
|
enum INIT_TYPE
|
|
|
|
{
|
|
|
|
Random = 0,
|
|
|
|
Farthest
|
|
|
|
};
|
|
|
|
public:
|
2024-09-02 13:10:01 +00:00
|
|
|
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);
|
2024-07-08 13:31:28 +00:00
|
|
|
}
|
|
|
|
|
2024-09-02 13:10:01 +00:00
|
|
|
// 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);
|
2024-09-06 09:49:03 +00:00
|
|
|
|
|
|
|
void set_memory_threshold(double threshold) { memory_threshold; }
|
|
|
|
MemoryedGroupHeap get_memoryed_groups()const { return memoryed_groups; }
|
|
|
|
|
2024-09-02 13:10:01 +00:00
|
|
|
std::vector<int>get_cluster_labels()const { return m_cluster_labels; }
|
|
|
|
|
2024-07-08 13:31:28 +00:00
|
|
|
private:
|
2024-09-02 13:10:01 +00:00
|
|
|
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);
|
2024-08-09 06:14:23 +00:00
|
|
|
int calc_cost(const std::vector<int>& labels, const std::vector<int>& medoids);
|
2024-07-08 13:31:28 +00:00
|
|
|
private:
|
2024-09-02 13:10:01 +00:00
|
|
|
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;
|
2024-08-08 03:23:29 +00:00
|
|
|
|
2024-09-06 09:49:03 +00:00
|
|
|
double memory_threshold{ 0 };
|
|
|
|
FilamentGroupUtils::MemoryedGroupHeap memoryed_groups;
|
|
|
|
|
2024-09-02 13:10:01 +00:00
|
|
|
std::vector<int>m_cluster_labels;
|
|
|
|
};
|
2024-07-08 13:31:28 +00:00
|
|
|
}
|
|
|
|
#endif // !FILAMENT_GROUP_HPP
|