From cedfc5e3fb2090e6c7a3897346abe3eb694ebd90 Mon Sep 17 00:00:00 2001 From: YuSanka Date: Tue, 12 Oct 2021 10:46:09 +0200 Subject: [PATCH] DoubleSlider: Code refactoring for auto color change --- src/slic3r/GUI/DoubleSlider.cpp | 29 +++++++++++++++++++++-------- src/slic3r/GUI/DoubleSlider.hpp | 7 +++++++ src/slic3r/GUI/GUI_Preview.cpp | 7 +++---- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/slic3r/GUI/DoubleSlider.cpp b/src/slic3r/GUI/DoubleSlider.cpp index 77679a1ad..d52d8be1c 100644 --- a/src/slic3r/GUI/DoubleSlider.cpp +++ b/src/slic3r/GUI/DoubleSlider.cpp @@ -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) @@ -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; @@ -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) { diff --git a/src/slic3r/GUI/DoubleSlider.hpp b/src/slic3r/GUI/DoubleSlider.hpp index 0f663f663..13b7f483d 100644 --- a/src/slic3r/GUI/DoubleSlider.hpp +++ b/src/slic3r/GUI/DoubleSlider.hpp @@ -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); diff --git a/src/slic3r/GUI/GUI_Preview.cpp b/src/slic3r/GUI/GUI_Preview.cpp index 57840dda2..558435019 100644 --- a/src/slic3r/GUI/GUI_Preview.cpp +++ b/src/slic3r/GUI/GUI_Preview.cpp @@ -687,7 +687,6 @@ void Preview::update_layers_slider(const std::vector& 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()) { @@ -708,7 +707,7 @@ void Preview::update_layers_slider(const std::vector& 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 @@ -725,7 +724,7 @@ void Preview::update_layers_slider(const std::vector& 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; } @@ -733,7 +732,7 @@ void Preview::update_layers_slider(const std::vector& layers_z, bool kee 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,