From ab260d005e5ea50d224595617c2d0e8eb4b72ae6 Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Fri, 29 Oct 2021 12:45:30 +0200 Subject: [PATCH] More adjustments, still working with Model directly --- src/slic3r/GUI/Gizmos/GLGizmoSimplify.cpp | 49 +++++++++-------------- src/slic3r/GUI/Gizmos/GLGizmoSimplify.hpp | 7 +--- 2 files changed, 21 insertions(+), 35 deletions(-) diff --git a/src/slic3r/GUI/Gizmos/GLGizmoSimplify.cpp b/src/slic3r/GUI/Gizmos/GLGizmoSimplify.cpp index d15048300..6c4cce9ee 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoSimplify.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoSimplify.cpp @@ -27,7 +27,6 @@ GLGizmoSimplify::GLGizmoSimplify(GLCanvas3D & parent, // translation for GUI size , tr_mesh_name(_u8L("Mesh name")) , tr_triangles(_u8L("Triangles")) - , tr_preview(_u8L("Preview")) , tr_detail_level(_u8L("Detail level")) , tr_decimate_ratio(_u8L("Decimate ratio")) {} @@ -139,7 +138,7 @@ void GLGizmoSimplify::on_render_input_window(float x, float y, float bottom_limi m_is_valid_result = false; m_exist_preview = false; init_model(); - live_preview(); + process(); // set window position if (m_move_to_center && change_window_position) { @@ -185,20 +184,13 @@ void GLGizmoSimplify::on_render_input_window(float x, float y, float bottom_limi m_imgui->text_colored(ImGuiWrapper::COL_ORANGE_LIGHT, tr_triangles + ":"); ImGui::SameLine(m_gui_cfg->top_left_width); m_imgui->text(std::to_string(triangle_count)); - /* - m_imgui->text_colored(ImGuiWrapper::COL_ORANGE_LIGHT, tr_preview + ":"); - ImGui::SameLine(m_gui_cfg->top_left_width); - if (m_exist_preview) { - m_imgui->text(std::to_string(m_volume->mesh().its.indices.size())); - } else { - m_imgui->text("---"); - }*/ + ImGui::Separator(); if(ImGui::RadioButton("##use_error", !m_configuration.use_count)) { m_configuration.use_count = !m_configuration.use_count; - live_preview(); + process(); } ImGui::SameLine(); m_imgui->disabled_begin(m_configuration.use_count); @@ -223,13 +215,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(); + process(); } m_imgui->disabled_end(); // !use_count if (ImGui::RadioButton("##use_count", m_configuration.use_count)) { m_configuration.use_count = !m_configuration.use_count; - live_preview(); + process(); } ImGui::SameLine(); @@ -253,7 +245,7 @@ void GLGizmoSimplify::on_render_input_window(float x, float y, float bottom_limi if (m_configuration.decimate_ratio > 100.f) m_configuration.decimate_ratio = 100.f; m_configuration.fix_count_by_ratio(triangle_count); - live_preview(); + process(); } ImGui::NewLine(); @@ -338,16 +330,6 @@ void GLGizmoSimplify::close() { gizmos_mgr.open_gizmo(GLGizmosManager::EType::Simplify); } -void GLGizmoSimplify::live_preview() { - m_is_valid_result = false; - if (m_state.status != State::settings) { - // already canceling process - if (m_state.status == State::canceling) return; - } - - m_state.status = State::preview; - process(); -} void GLGizmoSimplify::process() { @@ -383,28 +365,35 @@ void GLGizmoSimplify::process() } m_state.progress = 0; - if (m_worker.joinable()) m_worker.join(); + if (m_worker.joinable()) + m_worker.join(); - m_worker = std::thread([this]() { + // Create a copy of current mesh to pass to the worker thread. + // Using unique_ptr instead of pass-by-value to avoid an extra + // copy (which would happen when passing to std::thread). + auto its = std::make_unique(*m_original_its); + + m_worker = std::thread([this](std::unique_ptr its) { // Checks that the UI thread did not request cancellation, throw if so. std::function throw_on_cancel = [this]() { + std::lock_guard lk(m_state_mutex); if (m_state.status == State::canceling) throw SimplifyCanceledException(); }; // Called by worker thread, std::function statusfn = [this](int percent) { + std::lock_guard lk(m_state_mutex); m_state.progress = percent; }; - indexed_triangle_set collapsed = *m_original_its; // copy uint32_t triangle_count = (m_configuration.use_count) ? m_configuration.wanted_count : 0; float max_error = (!m_configuration.use_count) ? m_configuration.max_error : std::numeric_limits::max(); try { - its_quadric_edge_collapse(collapsed, triangle_count, &max_error, throw_on_cancel, statusfn); - set_its(collapsed); + its_quadric_edge_collapse(*its, triangle_count, &max_error, throw_on_cancel, statusfn); + set_its(*its); m_is_valid_result = true; m_exist_preview = true; } catch (SimplifyCanceledException &) { @@ -413,7 +402,7 @@ void GLGizmoSimplify::process() } // need to render last status fn to change bar graph to buttons request_rerender(); - }); + }, std::move(its)); } void GLGizmoSimplify::set_its(const indexed_triangle_set &its) { diff --git a/src/slic3r/GUI/Gizmos/GLGizmoSimplify.hpp b/src/slic3r/GUI/Gizmos/GLGizmoSimplify.hpp index 3373ca0b2..3e0ea6706 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoSimplify.hpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoSimplify.hpp @@ -51,7 +51,6 @@ protected: private: void after_apply(); void close(); - void live_preview(); void process(); void set_its(const indexed_triangle_set &its); void create_gui_cfg(); @@ -80,9 +79,6 @@ private: std::atomic m_need_reload; // after simplify, glReload must be on main thread - std::thread m_worker; - std::mutex m_state_mutex; - struct State { enum Status { settings, @@ -96,6 +92,8 @@ private: indexed_triangle_set result; }; + std::thread m_worker; + std::mutex m_state_mutex; // guards m_state State m_state; struct Configuration @@ -144,7 +142,6 @@ private: // translations used for calc window size const std::string tr_mesh_name; const std::string tr_triangles; - const std::string tr_preview; const std::string tr_detail_level; const std::string tr_decimate_ratio;