2024-07-08 13:31:28 +00:00
|
|
|
#ifndef FILAMENT_GROUP_HPP
|
|
|
|
#define FILAMENT_GROUP_HPP
|
|
|
|
|
|
|
|
#include<chrono>
|
2024-08-09 06:14:23 +00:00
|
|
|
#include<numeric>
|
|
|
|
#include "GCode/ToolOrderUtils.hpp"
|
2024-07-08 13:31:28 +00:00
|
|
|
|
|
|
|
namespace Slic3r
|
|
|
|
{
|
|
|
|
struct FlushTimeMachine
|
|
|
|
{
|
|
|
|
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-12 10:04:01 +00:00
|
|
|
enum FGStrategy {
|
|
|
|
BestCost,
|
|
|
|
BestFit
|
|
|
|
};
|
2024-07-08 13:31:28 +00:00
|
|
|
|
|
|
|
class FilamentGroup
|
|
|
|
{
|
2024-07-12 10:04:01 +00:00
|
|
|
public:
|
2024-08-09 06:14:23 +00:00
|
|
|
FilamentGroup(const std::vector<FlushMatrix>& flush_matrix, const int total_filament_num, const std::vector<int>& max_group_size) :
|
2024-07-08 13:31:28 +00:00
|
|
|
m_flush_matrix{ flush_matrix },
|
2024-08-09 06:14:23 +00:00
|
|
|
m_total_filament_num{ total_filament_num },
|
2024-07-08 13:31:28 +00:00
|
|
|
m_max_group_size{ max_group_size }
|
|
|
|
{}
|
|
|
|
|
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);
|
|
|
|
private:
|
|
|
|
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);
|
2024-07-08 13:31:28 +00:00
|
|
|
private:
|
|
|
|
std::vector<FlushMatrix>m_flush_matrix;
|
|
|
|
std::vector<int>m_max_group_size;
|
2024-08-09 06:14:23 +00:00
|
|
|
int m_total_filament_num;
|
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-07-08 13:31:28 +00:00
|
|
|
class KMediods
|
|
|
|
{
|
|
|
|
enum INIT_TYPE
|
|
|
|
{
|
|
|
|
Random = 0,
|
|
|
|
Farthest
|
|
|
|
};
|
|
|
|
public:
|
2024-08-09 06:14:23 +00:00
|
|
|
KMediods(const std::vector<std::vector<float>>& distance_matrix, const int filament_num,const std::vector<int>& max_group_size) :
|
2024-07-08 13:31:28 +00:00
|
|
|
m_distance_matrix{ distance_matrix },
|
|
|
|
m_filament_num{ filament_num },
|
2024-08-09 06:14:23 +00:00
|
|
|
m_max_group_size{ max_group_size }{}
|
2024-07-08 13:31:28 +00:00
|
|
|
|
2024-07-12 10:04:01 +00:00
|
|
|
void fit(const FGStrategy& g_strategy,int timeout_ms = 300);
|
2024-07-08 13:31:28 +00:00
|
|
|
std::vector<int>get_filament_labels()const {
|
|
|
|
return m_filament_labels;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2024-08-09 06:14:23 +00:00
|
|
|
std::vector<int>initialize(INIT_TYPE type);
|
|
|
|
std::vector<int>assign_label(const std::vector<int>& medoids,const FGStrategy&g_strategy);
|
|
|
|
int calc_cost(const std::vector<int>& labels, const std::vector<int>& medoids);
|
2024-07-08 13:31:28 +00:00
|
|
|
private:
|
|
|
|
std::vector<std::vector<float>>m_distance_matrix;
|
|
|
|
int m_filament_num;
|
|
|
|
std::vector<int>m_max_group_size;
|
2024-08-09 06:14:23 +00:00
|
|
|
std::set<int>m_medoids_set;
|
|
|
|
const int k = 2;
|
2024-07-08 13:31:28 +00:00
|
|
|
private:
|
|
|
|
std::vector<int>m_filament_labels;
|
|
|
|
};
|
2024-08-08 03:23:29 +00:00
|
|
|
|
2024-07-08 13:31:28 +00:00
|
|
|
}
|
|
|
|
#endif // !FILAMENT_GROUP_HPP
|