From c7806dd021ca523a5d0e8354450a2c0d8aee1d92 Mon Sep 17 00:00:00 2001 From: enricoturri1966 Date: Mon, 4 May 2020 09:37:06 +0200 Subject: [PATCH] GCodeViewer -> Fixed visualization of travel paths --- src/slic3r/GUI/GCodeViewer.cpp | 114 ++++++++++++++++++++++++++++----- src/slic3r/GUI/GCodeViewer.hpp | 13 ++-- src/slic3r/GUI/GUI_Preview.cpp | 2 + 3 files changed, 107 insertions(+), 22 deletions(-) diff --git a/src/slic3r/GUI/GCodeViewer.cpp b/src/slic3r/GUI/GCodeViewer.cpp index df8e81646..0c631904a 100644 --- a/src/slic3r/GUI/GCodeViewer.cpp +++ b/src/slic3r/GUI/GCodeViewer.cpp @@ -66,6 +66,30 @@ void GCodeViewer::VBuffer::reset() vertices_count = 0; } +bool GCodeViewer::Path::matches(const GCodeProcessor::MoveVertex& move) const +{ + switch (move.type) + { + case GCodeProcessor::EMoveType::Tool_change: + case GCodeProcessor::EMoveType::Color_change: + case GCodeProcessor::EMoveType::Pause_Print: + case GCodeProcessor::EMoveType::Custom_GCode: + case GCodeProcessor::EMoveType::Retract: + case GCodeProcessor::EMoveType::Unretract: + case GCodeProcessor::EMoveType::Extrude: + { + return type == move.type && role == move.extrusion_role && height == move.height && width == move.width && + 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; + } + case GCodeProcessor::EMoveType::Travel: + { + return type == move.type && feedrate == move.feedrate && extruder_id == move.extruder_id && cp_color_id == move.cp_color_id; + } + default: { return false; } + } +} + void GCodeViewer::IBuffer::reset() { // release gpu memory @@ -92,7 +116,7 @@ bool GCodeViewer::IBuffer::init_shader(const std::string& vertex_shader_src, con void GCodeViewer::IBuffer::add_path(const GCodeProcessor::MoveVertex& move) { - Path::Endpoint endpoint = { static_cast(data.size()), static_cast(move.position[2]) }; + Path::Endpoint endpoint = { static_cast(data.size()), move.position }; paths.push_back({ move.type, move.extrusion_role, endpoint, endpoint, move.delta_extruder, move.height, move.width, move.feedrate, move.fan_speed, move.volumetric_rate(), move.extruder_id, move.cp_color_id }); } @@ -410,10 +434,11 @@ void GCodeViewer::load_toolpaths(const GCodeProcessor::Result& gcode_result) { if (prev.type != curr.type || !buffer.paths.back().matches(curr)) { buffer.add_path(curr); + buffer.paths.back().first.position = prev.position; buffer.data.push_back(static_cast(i - 1)); } - buffer.paths.back().last.id = static_cast(buffer.data.size()); + buffer.paths.back().last = { static_cast(buffer.data.size()), curr.position }; buffer.data.push_back(static_cast(i)); break; } @@ -424,6 +449,13 @@ void GCodeViewer::load_toolpaths(const GCodeProcessor::Result& gcode_result) } } +#if ENABLE_GCODE_VIEWER_STATISTICS + for (IBuffer& buffer : m_buffers) + { + m_statistics.paths_size += SLIC3R_STDVEC_MEMSIZE(buffer.paths, Path); + } +#endif // ENABLE_GCODE_VIEWER_STATISTICS + // indices data -> send data to gpu for (IBuffer& buffer : m_buffers) { @@ -445,12 +477,16 @@ void GCodeViewer::load_toolpaths(const GCodeProcessor::Result& gcode_result) } // layers zs / roles / extruder ids / cp color ids -> extract from result - for (const GCodeProcessor::MoveVertex& move : gcode_result.moves) { + for (size_t i = 0; i < m_vertices.vertices_count; ++i) + { + const GCodeProcessor::MoveVertex& move = gcode_result.moves[i]; if (move.type == GCodeProcessor::EMoveType::Extrude) m_layers_zs.emplace_back(static_cast(move.position[2])); - m_roles.emplace_back(move.extrusion_role); m_extruder_ids.emplace_back(move.extruder_id); + + if (i > 0) + m_roles.emplace_back(move.extrusion_role); } // layers zs -> replace intervals of layers with similar top positions with their average value. @@ -560,9 +596,13 @@ void GCodeViewer::refresh_render_paths() const for (IBuffer& buffer : m_buffers) { buffer.render_paths = std::vector(); - for (const Path& path : buffer.paths) - { - if (!is_in_z_range(path)) + for (size_t i = 0; i < buffer.paths.size(); ++i) { + const Path& path = buffer.paths[i]; + if (path.type == GCodeProcessor::EMoveType::Travel) { + if (!is_travel_in_z_range(i)) + continue; + } + else if (!is_in_z_range(path)) continue; if (path.type == GCodeProcessor::EMoveType::Extrude && !is_visible(path)) @@ -576,8 +616,7 @@ void GCodeViewer::refresh_render_paths() const } auto it = std::find_if(buffer.render_paths.begin(), buffer.render_paths.end(), [color](const RenderPath& path) { return path.color == color; }); - if (it == buffer.render_paths.end()) - { + if (it == buffer.render_paths.end()) { it = buffer.render_paths.insert(buffer.render_paths.end(), RenderPath()); it->color = color; } @@ -1053,34 +1092,79 @@ void GCodeViewer::render_statistics() const ImGui::SameLine(offset); imgui.text(std::to_string(m_statistics.results_size) + " bytes"); + ImGui::Separator(); + ImGui::PushStyleColor(ImGuiCol_Text, ORANGE); imgui.text(std::string("Vertices CPU:")); ImGui::PopStyleColor(); ImGui::SameLine(offset); imgui.text(std::to_string(m_statistics.vertices_size) + " bytes"); - ImGui::PushStyleColor(ImGuiCol_Text, ORANGE); - imgui.text(std::string("Vertices GPU:")); - ImGui::PopStyleColor(); - ImGui::SameLine(offset); - imgui.text(std::to_string(m_statistics.vertices_gpu_size) + " bytes"); - ImGui::PushStyleColor(ImGuiCol_Text, ORANGE); imgui.text(std::string("Indices CPU:")); ImGui::PopStyleColor(); ImGui::SameLine(offset); imgui.text(std::to_string(m_statistics.indices_size) + " bytes"); + ImGui::PushStyleColor(ImGuiCol_Text, ORANGE); + imgui.text(std::string("Paths CPU:")); + ImGui::PopStyleColor(); + ImGui::SameLine(offset); + imgui.text(std::to_string(m_statistics.paths_size) + " bytes"); + + ImGui::PushStyleColor(ImGuiCol_Text, ORANGE); + imgui.text(std::string("TOTAL CPU:")); + ImGui::PopStyleColor(); + ImGui::SameLine(offset); + imgui.text(std::to_string(m_statistics.vertices_size + m_statistics.indices_size + m_statistics.paths_size) + " bytes"); + + ImGui::Separator(); + + ImGui::PushStyleColor(ImGuiCol_Text, ORANGE); + imgui.text(std::string("Vertices GPU:")); + ImGui::PopStyleColor(); + ImGui::SameLine(offset); + imgui.text(std::to_string(m_statistics.vertices_gpu_size) + " bytes"); + ImGui::PushStyleColor(ImGuiCol_Text, ORANGE); imgui.text(std::string("Indices GPU:")); ImGui::PopStyleColor(); ImGui::SameLine(offset); imgui.text(std::to_string(m_statistics.indices_gpu_size) + " bytes"); + ImGui::PushStyleColor(ImGuiCol_Text, ORANGE); + imgui.text(std::string("TOTAL GPU:")); + ImGui::PopStyleColor(); + ImGui::SameLine(offset); + imgui.text(std::to_string(m_statistics.vertices_gpu_size + m_statistics.indices_gpu_size) + " bytes"); + imgui.end(); } #endif // ENABLE_GCODE_VIEWER_STATISTICS +bool GCodeViewer::is_travel_in_z_range(size_t id) const +{ + const IBuffer& buffer = m_buffers[buffer_id(GCodeProcessor::EMoveType::Travel)]; + if (id >= buffer.paths.size()) + return false; + + Path path = buffer.paths[id]; + int first = static_cast(id); + unsigned int last = static_cast(id); + + // check adjacent paths + while (first > 0 && path.first.position.isApprox(buffer.paths[first - 1].last.position)) { + --first; + path.first = buffer.paths[first].first; + } + while (last < static_cast(buffer.paths.size() - 1) && path.last.position.isApprox(buffer.paths[last + 1].first.position)) { + ++last; + path.last = buffer.paths[last].last; + } + + return is_in_z_range(path); +} + } // namespace GUI } // namespace Slic3r diff --git a/src/slic3r/GUI/GCodeViewer.hpp b/src/slic3r/GUI/GCodeViewer.hpp index 9af7e7cc4..aa024a742 100644 --- a/src/slic3r/GUI/GCodeViewer.hpp +++ b/src/slic3r/GUI/GCodeViewer.hpp @@ -38,7 +38,7 @@ class GCodeViewer struct Endpoint { unsigned int id{ 0u }; - double z{ 0.0 }; + Vec3f position{ Vec3f::Zero() }; }; GCodeProcessor::EMoveType type{ GCodeProcessor::EMoveType::Noop }; @@ -54,11 +54,7 @@ class GCodeViewer unsigned char extruder_id{ 0 }; unsigned char cp_color_id{ 0 }; - bool matches(const GCodeProcessor::MoveVertex& move) const { - return type == move.type && role == move.extrusion_role && height == move.height && width == move.width && - 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; - } + bool matches(const GCodeProcessor::MoveVertex& move) const; }; // Used to batch the indices needed to render paths @@ -157,6 +153,7 @@ class GCodeViewer long long vertices_gpu_size{ 0 }; long long indices_size{ 0 }; long long indices_gpu_size{ 0 }; + long long paths_size{ 0 }; void reset_all() { reset_times(); @@ -180,6 +177,7 @@ class GCodeViewer vertices_gpu_size = 0; indices_size = 0; indices_gpu_size = 0; + paths_size = 0; } }; #endif // ENABLE_GCODE_VIEWER_STATISTICS @@ -279,8 +277,9 @@ private: 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); + return in_z_range(path.first.position[2]) || in_z_range(path.last.position[2]); } + bool is_travel_in_z_range(size_t id) const; }; } // namespace GUI diff --git a/src/slic3r/GUI/GUI_Preview.cpp b/src/slic3r/GUI/GUI_Preview.cpp index ea7d2dc1b..22b3d2c25 100644 --- a/src/slic3r/GUI/GUI_Preview.cpp +++ b/src/slic3r/GUI/GUI_Preview.cpp @@ -405,6 +405,7 @@ bool Preview::init(wxWindow* parent, Bed3D& bed, Camera& camera, GLToolbar& view bind_event_handlers(); +#if !ENABLE_GCODE_VIEWER // sets colors for gcode preview extrusion roles std::vector extrusion_roles_colors = { "Perimeter", "FFFF66", @@ -422,6 +423,7 @@ bool Preview::init(wxWindow* parent, Bed3D& bed, Camera& camera, GLToolbar& view "Custom", "28CC94" }; m_gcode_preview_data->set_extrusion_paths_colors(extrusion_roles_colors); +#endif // !ENABLE_GCODE_VIEWER return true; }