From 0e173e3f825f7d7fc0fdedeae265f17c857319b0 Mon Sep 17 00:00:00 2001 From: enricoturri1966 Date: Mon, 24 Jan 2022 08:51:19 +0100 Subject: [PATCH] Refactoring of lambda extract_move_id() to improve performance of method GCodeViewer::load_toolpaths() --- src/slic3r/GUI/GCodeViewer.cpp | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/slic3r/GUI/GCodeViewer.cpp b/src/slic3r/GUI/GCodeViewer.cpp index 151e66d4d1..cca87d2045 100644 --- a/src/slic3r/GUI/GCodeViewer.cpp +++ b/src/slic3r/GUI/GCodeViewer.cpp @@ -1652,15 +1652,26 @@ void GCodeViewer::load_toolpaths(const GCodeProcessorResult& gcode_result) } }; - auto extract_move_id = [&seams_ids](size_t id) { - for (int i = seams_ids.size() - 1; i >= 0; --i) { - if (seams_ids[i] < id + i + 1) - return id + (size_t)i + 1; + std::vector biased_seams_ids(seams_ids.size()); + for (size_t i = 0; i < seams_ids.size(); ++i) { + biased_seams_ids[i] = seams_ids[i] - i - 1; + } + + auto extract_move_id = [&biased_seams_ids](size_t id) { + size_t new_id = -1; + auto it = std::lower_bound(biased_seams_ids.begin(), biased_seams_ids.end(), id); + if (it == biased_seams_ids.end()) + new_id = id + biased_seams_ids.size(); + else { + if (it == biased_seams_ids.begin() && *it < id) + new_id = id; + else if (it != biased_seams_ids.begin()) + new_id = id + std::distance(biased_seams_ids.begin(), it); } - return id; + return (new_id == -1) ? id : new_id; }; - size_t vertex_size_floats = t_buffer.vertices.vertex_size_floats(); + const size_t vertex_size_floats = t_buffer.vertices.vertex_size_floats(); for (const Path& path : t_buffer.paths) { // the two segments of the path sharing the current vertex may belong // to two different vertex buffers @@ -1669,8 +1680,8 @@ void GCodeViewer::load_toolpaths(const GCodeProcessorResult& gcode_result) const size_t path_vertices_count = path.vertices_count(); const float half_width = 0.5f * path.width; for (size_t j = 1; j < path_vertices_count - 1; ++j) { - size_t curr_s_id = path.sub_paths.front().first.s_id + j; - size_t move_id = extract_move_id(curr_s_id); + const size_t curr_s_id = path.sub_paths.front().first.s_id + j; + const size_t move_id = extract_move_id(curr_s_id); const Vec3f& prev = gcode_result.moves[move_id - 1].position; const Vec3f& curr = gcode_result.moves[move_id].position; const Vec3f& next = gcode_result.moves[move_id + 1].position; @@ -1697,7 +1708,7 @@ void GCodeViewer::load_toolpaths(const GCodeProcessorResult& gcode_result) float displacement = 0.0f; if (cos_dir > -0.9998477f) { // if the angle between adjacent segments is smaller than 179 degrees - Vec3f med_dir = (prev_dir + next_dir).normalized(); + const Vec3f med_dir = (prev_dir + next_dir).normalized(); displacement = half_width * ::tan(::acos(std::clamp(next_dir.dot(med_dir), -1.0f, 1.0f))); } @@ -1708,7 +1719,7 @@ void GCodeViewer::load_toolpaths(const GCodeProcessorResult& gcode_result) if (can_displace) { // displacement to apply to the vertices to match - Vec3f displacement_vec = displacement * prev_dir; + const Vec3f displacement_vec = displacement * prev_dir; // matches inner corner vertices if (is_right_turn) match_right_vertices(prev_sub_path, next_sub_path, curr_s_id, vertex_size_floats, -displacement_vec); @@ -1735,9 +1746,8 @@ void GCodeViewer::load_toolpaths(const GCodeProcessorResult& gcode_result) // smooth toolpaths corners for TBuffers using triangles for (size_t i = 0; i < m_buffers.size(); ++i) { const TBuffer& t_buffer = m_buffers[i]; - if (t_buffer.render_primitive_type == TBuffer::ERenderPrimitiveType::Triangle) { + if (t_buffer.render_primitive_type == TBuffer::ERenderPrimitiveType::Triangle) smooth_triangle_toolpaths_corners(t_buffer, vertices[i]); - } } // dismiss, no more needed