From 80ccb77b00d39752301692196bdc2b908b6ffbba Mon Sep 17 00:00:00 2001 From: Filip Sykala Date: Mon, 18 Oct 2021 16:01:32 +0200 Subject: [PATCH] live preview in simplification --- src/slic3r/GUI/Gizmos/GLGizmoSimplify.cpp | 35 ++++++++++++++++++++--- src/slic3r/GUI/Gizmos/GLGizmoSimplify.hpp | 3 ++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/slic3r/GUI/Gizmos/GLGizmoSimplify.cpp b/src/slic3r/GUI/Gizmos/GLGizmoSimplify.cpp index c09d67317..88ff8dc9e 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoSimplify.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoSimplify.cpp @@ -143,8 +143,8 @@ void GLGizmoSimplify::on_render_input_window(float x, float y, float bottom_limi ImGui::Separator(); if(ImGui::RadioButton("##use_error", !m_configuration.use_count)) { - m_is_valid_result = false; m_configuration.use_count = !m_configuration.use_count; + live_preview(); } ImGui::SameLine(); m_imgui->disabled_begin(m_configuration.use_count); @@ -160,7 +160,6 @@ void GLGizmoSimplify::on_render_input_window(float x, float y, float bottom_limi ImGui::SetNextItemWidth(m_gui_cfg->input_width); static int reduction = 2; if(ImGui::SliderInt("##ReductionLevel", &reduction, 0, 4, reduce_captions[reduction].c_str())) { - m_is_valid_result = false; if (reduction < 0) reduction = 0; if (reduction > 4) reduction = 4; switch (reduction) { @@ -170,12 +169,13 @@ void GLGizmoSimplify::on_render_input_window(float x, float y, float bottom_limi case 3: m_configuration.max_error = 0.5f; break; case 4: m_configuration.max_error = 1.f; break; } + live_preview(); } m_imgui->disabled_end(); // !use_count if (ImGui::RadioButton("##use_count", m_configuration.use_count)) { - m_is_valid_result = false; m_configuration.use_count = !m_configuration.use_count; + live_preview(); } ImGui::SameLine(); @@ -192,13 +192,14 @@ void GLGizmoSimplify::on_render_input_window(float x, float y, float bottom_limi ImGui::SetNextItemWidth(m_gui_cfg->input_width); const char * format = (m_configuration.decimate_ratio > 10)? "%.0f %%": ((m_configuration.decimate_ratio > 1)? "%.1f %%":"%.2f %%"); + if (ImGui::SliderFloat("##decimate_ratio", &m_configuration.decimate_ratio, 0.f, 100.f, format)) { - m_is_valid_result = false; if (m_configuration.decimate_ratio < 0.f) m_configuration.decimate_ratio = 0.01f; if (m_configuration.decimate_ratio > 100.f) m_configuration.decimate_ratio = 100.f; m_configuration.fix_count_by_ratio(triangle_count); + live_preview(); } ImGui::NewLine(); @@ -221,12 +222,16 @@ void GLGizmoSimplify::on_render_input_window(float x, float y, float bottom_limi } } ImGui::SameLine(m_gui_cfg->bottom_left_width); + + m_imgui->disabled_begin(m_configuration.live_preview || m_is_valid_result); if (m_imgui->button(_L("Preview"))) { m_state = State::preview; // simplify but not apply on mesh process(); } + m_imgui->disabled_end(); ImGui::SameLine(); + if (m_imgui->button(_L("Apply"))) { if (!m_is_valid_result) { m_state = State::close_on_end; @@ -238,6 +243,11 @@ void GLGizmoSimplify::on_render_input_window(float x, float y, float bottom_limi close(); } } + ImGui::SameLine(); + if(ImGui::Checkbox(_L("Live").c_str(), &m_configuration.live_preview)) { + if (m_configuration.live_preview && !m_is_valid_result) + live_preview(); + } } else { m_imgui->disabled_begin(m_state == State::canceling); if (m_imgui->button(_L("Cancel"))) m_state = State::canceling; @@ -280,6 +290,23 @@ void GLGizmoSimplify::close() { gizmos_mgr.open_gizmo(GLGizmosManager::EType::Simplify); } +void GLGizmoSimplify::live_preview() { + m_is_valid_result = false; + if (!m_configuration.live_preview) return; + + if (m_state != State::settings) { + if (m_state == State::canceling) return; + + // wait until cancel + if (m_worker.joinable()) { + m_state = State::canceling; + m_worker.join(); + } + } + + m_state = State::preview; + process(); +} void GLGizmoSimplify::process() { diff --git a/src/slic3r/GUI/Gizmos/GLGizmoSimplify.hpp b/src/slic3r/GUI/Gizmos/GLGizmoSimplify.hpp index d624e3351..3ee99a4d4 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoSimplify.hpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoSimplify.hpp @@ -39,6 +39,7 @@ protected: private: void after_apply(); void close(); + void live_preview(); void process(); void set_its(indexed_triangle_set &its); void create_gui_cfg(); @@ -73,6 +74,8 @@ private: struct Configuration { + bool live_preview = false; + bool use_count = false; // minimal triangle count float decimate_ratio = 50.f; // in percent