Skip to content

Commit

Permalink
DoubleSlider: Code refactoring for auto color change
Browse files Browse the repository at this point in the history
  • Loading branch information
YuSanka committed Oct 12, 2021
1 parent 94843bb commit cedfc5e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
29 changes: 21 additions & 8 deletions src/slic3r/GUI/DoubleSlider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ using GUI::format_wxstr;

namespace DoubleSlider {

bool possible_threshold(const double& bottom_area, const double& top_area)
{
// Check percent of the area decrease.
// This value have to be more than 25 mm2
return (bottom_area - top_area > min_delta_area()) &&
// and more 10%
(top_area / bottom_area < 0.9);
}

bool equivalent_areas(const double& bottom_area, const double& top_area)
{
return fabs(bottom_area - top_area <= min_threshold());
}

bool overhang(const double& bottom_area, const double& top_area)
{
return top_area > bottom_area && !equivalent_areas(bottom_area, top_area);
}

wxDEFINE_EVENT(wxCUSTOMEVT_TICKSCHANGED, wxEvent);

static std::string gcode(Type type)
Expand Down Expand Up @@ -2049,8 +2068,6 @@ void Control::auto_color_change()
int extruder = 2;

const Print& print = GUI::wxGetApp().plater()->fff_print();
double delta_area = scale_(scale_(25)); // equal to 25 mm2

for (auto object : print.objects()) {
if (object->layer_count() == 0)
continue;
Expand All @@ -2060,14 +2077,10 @@ void Control::auto_color_change()
Layer* layer = object->get_layer(i);
double cur_area = area(layer->lslices);

if (cur_area > prev_area && prev_area - cur_area > scale_(scale_(1)))
if (overhang(prev_area, cur_area))
break;

if (prev_area - cur_area > delta_area) {
// Check percent of the area decrease.
// Ignore it, if this value is less than 10%
if (cur_area / prev_area > 0.9)
continue;
if (possible_threshold(prev_area, cur_area)) {
int tick = get_tick_from_value(layer->print_z);
if (tick >= 0 && !m_ticks.has_tick(tick)) {
if (m_mode == SingleExtruder) {
Expand Down
7 changes: 7 additions & 0 deletions src/slic3r/GUI/DoubleSlider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ namespace DoubleSlider {
*/
constexpr double epsilon() { return 0.0011; }

constexpr double min_delta_area() { return scale_(scale_(25)); } // equal to 25 mm2
constexpr double min_threshold() { return scale_(scale_(1)); } // equal to 1 mm2

bool possible_threshold(const double& bottom_area, const double& top_area);
bool equivalent_areas(const double& bottom_area, const double& top_area);
bool overhang(const double& bottom_area, const double& top_area);

// custom message the slider sends to its parent to notify a tick-change:
wxDECLARE_EVENT(wxCUSTOMEVT_TICKSCHANGED, wxEvent);

Expand Down
7 changes: 3 additions & 4 deletions src/slic3r/GUI/GUI_Preview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,6 @@ void Preview::update_layers_slider(const std::vector<double>& layers_z, bool kee
if (m_layers_slider->IsNewPrint())
{
const Print& print = wxGetApp().plater()->fff_print();
double delta_area = scale_(scale_(25)); // equal to 25 mm2

//bool is_possible_auto_color_change = false;
for (auto object : print.objects()) {
Expand All @@ -708,7 +707,7 @@ void Preview::update_layers_slider(const std::vector<double>& layers_z, bool kee
int i, min_solid_height = int(0.25 * num_layers);
for (i = 1; i <= min_solid_height; ++ i) {
double cur_area = area(object->get_layer(i)->lslices);
if (cur_area != bottom_area && fabs(cur_area - bottom_area) > scale_(scale_(1))) {
if (!DoubleSlider::equivalent_areas(bottom_area, cur_area)) {
// but due to the elephant foot compensation, the first layer may be slightly smaller than the others
if (i == 1 && fabs(cur_area - bottom_area) / bottom_area < 0.1) {
// So, let process this case and use second layer as a bottom
Expand All @@ -725,15 +724,15 @@ void Preview::update_layers_slider(const std::vector<double>& layers_z, bool kee
double prev_area = area(object->get_layer(i)->lslices);
for ( i++; i < num_layers; i++) {
double cur_area = area(object->get_layer(i)->lslices);
if (cur_area > prev_area && prev_area - cur_area > scale_(scale_(1)))
if (DoubleSlider::overhang(prev_area, cur_area))
break;
prev_area = cur_area;
}
if (i < num_layers)
continue;

double top_area = area(object->get_layer(int(object->layers().size()) - 1)->lslices);
if( bottom_area - top_area > delta_area) {
if (DoubleSlider::possible_threshold(bottom_area, top_area)) {
NotificationManager *notif_mngr = wxGetApp().plater()->get_notification_manager();
notif_mngr->push_notification(
NotificationType::SignDetected, NotificationManager::NotificationLevel::PrintInfoNotificationLevel,
Expand Down

0 comments on commit cedfc5e

Please sign in to comment.