From c1246f86eb7cd1c92f8a3c57ba924bc2f9b1ce9a Mon Sep 17 00:00:00 2001 From: enricoturri1966 Date: Mon, 27 Apr 2020 12:43:51 +0200 Subject: [PATCH] GCodeViewer -> Refactoring --- src/slic3r/GUI/GCodeViewer.cpp | 53 ++++++++++++++-------------------- src/slic3r/GUI/GCodeViewer.hpp | 22 +++++++------- src/slic3r/GUI/GLCanvas3D.cpp | 2 ++ 3 files changed, 35 insertions(+), 42 deletions(-) diff --git a/src/slic3r/GUI/GCodeViewer.cpp b/src/slic3r/GUI/GCodeViewer.cpp index 045f6ed57..268b998fa 100644 --- a/src/slic3r/GUI/GCodeViewer.cpp +++ b/src/slic3r/GUI/GCodeViewer.cpp @@ -65,19 +65,6 @@ void GCodeViewer::VBuffer::reset() vertices_count = 0; } -bool GCodeViewer::Path::is_path_visible(const Path& path, unsigned int flags) { - return Extrusions::is_role_visible(flags, path.role); -}; - -bool GCodeViewer::Path::is_path_in_z_range(const Path& path, const std::array& z_range) -{ - auto in_z_range = [z_range](double z) { - return z > z_range[0] - EPSILON && z < z_range[1] + EPSILON; - }; - - return in_z_range(path.first_z) || in_z_range(path.last_z); -} - void GCodeViewer::IBuffer::reset() { // release gpu memory @@ -186,6 +173,8 @@ void GCodeViewer::load(const GCodeProcessor::Result& gcode_result, const Print& void GCodeViewer::refresh(const GCodeProcessor::Result& gcode_result, const std::vector& str_tool_colors) { + auto start_time = std::chrono::high_resolution_clock::now(); + if (m_vertices.vertices_count == 0) return; @@ -222,6 +211,9 @@ void GCodeViewer::refresh(const GCodeProcessor::Result& gcode_result, const std: default: { break; } } } + + auto end_time = std::chrono::high_resolution_clock::now(); + std::cout << "refresh: " << std::chrono::duration_cast(end_time - start_time).count() << "ms \n"; } void GCodeViewer::reset() @@ -560,7 +552,7 @@ void GCodeViewer::render_toolpaths() const std::array color = { 1.0f, 1.0f, 1.0f }; set_color(current_program_id, color); for (const Path& path : buffer.paths) { - if (!Path::is_path_in_z_range(path, m_layers_z_range)) + if (!is_in_z_range(path)) continue; glsafe(::glEnable(GL_PROGRAM_POINT_SIZE)); @@ -574,7 +566,7 @@ void GCodeViewer::render_toolpaths() const std::array color = { 1.0f, 0.0f, 0.0f }; set_color(current_program_id, color); for (const Path& path : buffer.paths) { - if (!Path::is_path_in_z_range(path, m_layers_z_range)) + if (!is_in_z_range(path)) continue; glsafe(::glEnable(GL_PROGRAM_POINT_SIZE)); @@ -588,7 +580,7 @@ void GCodeViewer::render_toolpaths() const std::array color = { 0.0f, 1.0f, 0.0f }; set_color(current_program_id, color); for (const Path& path : buffer.paths) { - if (!Path::is_path_in_z_range(path, m_layers_z_range)) + if (!is_in_z_range(path)) continue; glsafe(::glEnable(GL_PROGRAM_POINT_SIZE)); @@ -602,7 +594,7 @@ void GCodeViewer::render_toolpaths() const std::array color = { 0.0f, 0.0f, 1.0f }; set_color(current_program_id, color); for (const Path& path : buffer.paths) { - if (!Path::is_path_in_z_range(path, m_layers_z_range)) + if (!is_in_z_range(path)) continue; glsafe(::glEnable(GL_PROGRAM_POINT_SIZE)); @@ -616,7 +608,7 @@ void GCodeViewer::render_toolpaths() const std::array color = { 1.0f, 0.0f, 1.0f }; set_color(current_program_id, color); for (const Path& path : buffer.paths) { - if (!Path::is_path_in_z_range(path, m_layers_z_range)) + if (!is_in_z_range(path)) continue; glsafe(::glEnable(GL_PROGRAM_POINT_SIZE)); @@ -630,7 +622,7 @@ void GCodeViewer::render_toolpaths() const std::array color = { 0.0f, 1.0f, 1.0f }; set_color(current_program_id, color); for (const Path& path : buffer.paths) { - if (!Path::is_path_in_z_range(path, m_layers_z_range)) + if (!is_in_z_range(path)) continue; glsafe(::glEnable(GL_PROGRAM_POINT_SIZE)); @@ -642,7 +634,7 @@ void GCodeViewer::render_toolpaths() const case GCodeProcessor::EMoveType::Extrude: { for (const Path& path : buffer.paths) { - if (!Path::is_path_visible(path, m_extrusions.role_visibility_flags) || !Path::is_path_in_z_range(path, m_layers_z_range)) + if (!is_visible(path) || !is_in_z_range(path)) continue; set_color(current_program_id, extrusion_color(path)); @@ -653,7 +645,7 @@ void GCodeViewer::render_toolpaths() const case GCodeProcessor::EMoveType::Travel: { for (const Path& path : buffer.paths) { - if (!Path::is_path_in_z_range(path, m_layers_z_range)) + if (!is_in_z_range(path)) continue; set_color(current_program_id, (m_view_type == EViewType::Feedrate || m_view_type == EViewType::Tool || m_view_type == EViewType::ColorPrint) ? extrusion_color(path) : travel_color(path)); @@ -689,9 +681,7 @@ void GCodeViewer::render_shells() const void GCodeViewer::render_overlay() const { static const ImVec4 ORANGE(1.0f, 0.49f, 0.22f, 1.0f); - static const float ICON_BORDER_SIZE = 25.0f; static const ImU32 ICON_BORDER_COLOR = ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, 1.0f)); - static const float GAP_ICON_TEXT = 7.5f; if (!m_legend_enabled || m_roles.empty()) return; @@ -709,14 +699,15 @@ void GCodeViewer::render_overlay() const ImDrawList* draw_list = ImGui::GetWindowDrawList(); auto add_item = [draw_list, &imgui](const std::array& color, const std::string& label) { - // draw icon - ImVec2 pos(ImGui::GetCursorPosX() + 2.0f, ImGui::GetCursorPosY() + 2.0f); - draw_list->AddRect({ pos.x, pos.y }, { pos.x + ICON_BORDER_SIZE, pos.y + ICON_BORDER_SIZE }, ICON_BORDER_COLOR, 0.0f, 0); - draw_list->AddRectFilled({ pos.x + 1.0f, pos.y + 1.0f }, - { pos.x + ICON_BORDER_SIZE - 1.0f, pos.y + ICON_BORDER_SIZE - 1.0f }, - ImGui::GetColorU32({ color[0], color[1], color[2], 1.0f })); + float icon_size = ImGui::GetTextLineHeight(); + ImVec2 pos = ImGui::GetCursorPos(); + draw_list->AddRect({ pos.x, pos.y }, { pos.x + icon_size, pos.y + icon_size }, ICON_BORDER_COLOR, 0.0f, 0); + draw_list->AddRectFilled({ pos.x + 1.0f, pos.y + 1.0f }, { pos.x + icon_size - 1.0f, pos.y + icon_size - 1.0f }, + ImGui::GetColorU32({ color[0], color[1], color[2], 1.0f })); + // draw text - ImGui::SetCursorPos({ pos.x + ICON_BORDER_SIZE + GAP_ICON_TEXT, pos.y + 0.5f * (ICON_BORDER_SIZE - ImGui::GetTextLineHeight()) }); + ImGui::Dummy({ icon_size, icon_size }); + ImGui::SameLine(); imgui.text(label); }; @@ -762,7 +753,7 @@ void GCodeViewer::render_overlay() const case EViewType::FeatureType: { for (ExtrusionRole role : m_roles) { - bool visible = m_extrusions.is_role_visible(role); + bool visible = is_visible(role); if (!visible) ImGui::PushStyleVar(ImGuiStyleVar_Alpha, 0.3333f); diff --git a/src/slic3r/GUI/GCodeViewer.hpp b/src/slic3r/GUI/GCodeViewer.hpp index 3c73cee5a..d2eb103f6 100644 --- a/src/slic3r/GUI/GCodeViewer.hpp +++ b/src/slic3r/GUI/GCodeViewer.hpp @@ -56,9 +56,6 @@ class GCodeViewer feedrate == move.feedrate && fan_speed == move.fan_speed && volumetric_rate == move.volumetric_rate() && extruder_id == move.extruder_id && cp_color_id == move.cp_color_id; } - - static bool is_path_visible(const Path& path, unsigned int flags); - static bool is_path_in_z_range(const Path& path, const std::array& z_range); }; // buffer containing indices data and shader for a specific toolpath type @@ -134,14 +131,6 @@ class GCodeViewer } void reset_ranges() { ranges.reset(); } - - bool is_role_visible(ExtrusionRole role) const { - return role < erCount && (role_visibility_flags & (1 << role)) != 0; - } - - static bool is_role_visible(unsigned int flags, ExtrusionRole role) { - return role < erCount && (flags & (1 << role)) != 0; - } }; public: @@ -217,6 +206,17 @@ private: void render_toolpaths() const; void render_shells() const; void render_overlay() const; + bool is_visible(ExtrusionRole role) const { + return role < erCount && (m_extrusions.role_visibility_flags & (1 << role)) != 0; + } + bool is_visible(const Path& path) const { return is_visible(path.role); } + bool is_in_z_range(const Path& path) const { + auto in_z_range = [this](double z) { + return z > m_layers_z_range[0] - EPSILON && z < m_layers_z_range[1] + EPSILON; + }; + + return in_z_range(path.first_z) || in_z_range(path.last_z); + } }; } // namespace GUI diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index 6e7b53f68..fa38aca47 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -2858,6 +2858,8 @@ void GLCanvas3D::load_gcode_preview(const GCodeProcessor::Result& gcode_result) void GLCanvas3D::refresh_gcode_preview(const GCodeProcessor::Result& gcode_result, const std::vector& str_tool_colors) { m_gcode_viewer.refresh(gcode_result, str_tool_colors); + set_as_dirty(); + request_extra_frame(); } #endif // ENABLE_GCODE_VIEWER