From 2ed57d1ba5091bade713e7924d9cd5d5b4f5b867 Mon Sep 17 00:00:00 2001 From: Vojtech Bubnik Date: Mon, 6 Dec 2021 13:21:34 +0100 Subject: [PATCH] Further optimization of G-code viewer: Replaced std::set with std::vector. --- src/slic3r/GUI/GCodeViewer.cpp | 29 +++++++++++++---------------- src/slic3r/GUI/GCodeViewer.hpp | 12 +----------- 2 files changed, 14 insertions(+), 27 deletions(-) diff --git a/src/slic3r/GUI/GCodeViewer.cpp b/src/slic3r/GUI/GCodeViewer.cpp index 347f781e76..f2bd748822 100644 --- a/src/slic3r/GUI/GCodeViewer.cpp +++ b/src/slic3r/GUI/GCodeViewer.cpp @@ -977,8 +977,7 @@ void GCodeViewer::export_toolpaths_to_obj(const char* filename) const for (const RenderPath& path : t_buffer.render_paths) { colors.push_back(path.color); } - std::sort(colors.begin(), colors.end()); - colors.erase(std::unique(colors.begin(), colors.end()), colors.end()); + sort_remove_duplicates(colors); // save materials file boost::filesystem::path mat_filename(filename); @@ -2020,13 +2019,11 @@ void GCodeViewer::load_toolpaths(const GCodeProcessorResult& gcode_result) } // roles -> remove duplicates - std::sort(m_roles.begin(), m_roles.end()); - m_roles.erase(std::unique(m_roles.begin(), m_roles.end()), m_roles.end()); + sort_remove_duplicates(m_roles); m_roles.shrink_to_fit(); // extruder ids -> remove duplicates - std::sort(m_extruder_ids.begin(), m_extruder_ids.end()); - m_extruder_ids.erase(std::unique(m_extruder_ids.begin(), m_extruder_ids.end()), m_extruder_ids.end()); + sort_remove_duplicates(m_extruder_ids); m_extruder_ids.shrink_to_fit(); // set layers z range @@ -2374,8 +2371,10 @@ void GCodeViewer::refresh_render_paths(bool keep_sequential_current_first, bool } RenderPath key{ tbuffer_id, color, static_cast(ibuffer_id), path_id }; - if (render_path == nullptr || !RenderPathPropertyEqual()(*render_path, key)) - render_path = const_cast(&(*buffer.render_paths.emplace(key).first)); + if (render_path == nullptr || !RenderPathPropertyEqual()(*render_path, key)) { + buffer.render_paths.emplace_back(key); + render_path = const_cast(&buffer.render_paths.back()); + } unsigned int delta_1st = 0; if (sub_path.first.s_id < m_sequential_view.current.first && m_sequential_view.current.first <= sub_path.last.s_id) @@ -2433,16 +2432,14 @@ void GCodeViewer::refresh_render_paths(bool keep_sequential_current_first, bool #endif } - // removes empty render paths + // Removes empty render paths and sort. for (size_t b = 0; b < m_buffers.size(); ++b) { TBuffer* buffer = const_cast(&m_buffers[b]); - std::set::iterator it = buffer->render_paths.begin(); - while (it != buffer->render_paths.end()) { - if (it->sizes.empty() || it->offsets.empty()) - it = buffer->render_paths.erase(it); - else - ++it; - } + buffer->render_paths.erase(std::remove_if(buffer->render_paths.begin(), buffer->render_paths.end(), + [](const auto &path){ return path.sizes.empty() || path.offsets.empty(); }), + buffer->render_paths.end()); + //FIXME is this sorting needed at all? + std::sort(buffer->render_paths.begin(), buffer->render_paths.end(), RenderPathPropertyLower{}); } // second pass: for buffers using instanced and batched models, update the instances render ranges diff --git a/src/slic3r/GUI/GCodeViewer.hpp b/src/slic3r/GUI/GCodeViewer.hpp index 9147eec84d..c56e88c880 100644 --- a/src/slic3r/GUI/GCodeViewer.hpp +++ b/src/slic3r/GUI/GCodeViewer.hpp @@ -259,14 +259,6 @@ class GCodeViewer return false; } }; -// // for unordered_set implementation of render_paths -// struct RenderPathPropertyHash { -// size_t operator() (const RenderPath &p) const { -// // Convert the RGB value to an integer hash. -//// return (size_t(int(p.color[0] * 255) + 255 * int(p.color[1] * 255) + (255 * 255) * int(p.color[2] * 255)) * 7919) ^ size_t(p.ibuffer_id); -// return size_t(int(p.color[0] * 255) + 255 * int(p.color[1] * 255) + (255 * 255) * int(p.color[2] * 255)) ^ size_t(p.ibuffer_id); -// } -// }; struct RenderPathPropertyLower { bool operator() (const RenderPath &l, const RenderPath &r) const { if (l.tbuffer_id < r.tbuffer_id) @@ -319,9 +311,7 @@ class GCodeViewer std::string shader; std::vector paths; - // std::set seems to perform significantly better, at least on Windows. -// std::unordered_set render_paths; - std::set render_paths; + std::vector render_paths; bool visible{ false }; void reset();