From 460cf820afe95951eea41ab6a0ae1a58e64c5937 Mon Sep 17 00:00:00 2001 From: bubnikv Date: Wed, 30 Jan 2019 12:10:26 +0100 Subject: [PATCH] Some optimizations of the color print preview. --- src/libslic3r/GCode/PreviewData.cpp | 11 ++++------- src/slic3r/GUI/GLCanvas3D.cpp | 22 +++------------------- src/slic3r/GUI/GUI_Preview.cpp | 14 ++++++++------ 3 files changed, 15 insertions(+), 32 deletions(-) diff --git a/src/libslic3r/GCode/PreviewData.cpp b/src/libslic3r/GCode/PreviewData.cpp index cd6be3db1..8eba6801e 100644 --- a/src/libslic3r/GCode/PreviewData.cpp +++ b/src/libslic3r/GCode/PreviewData.cpp @@ -479,16 +479,13 @@ GCodePreviewData::LegendItemsList GCodePreviewData::get_legend_items(const std:: } case Extrusion::ColorPrint: { - const size_t color_cnt = tool_colors.size()/4; + const int color_cnt = (int)tool_colors.size()/4; - const auto color_print_cnt = cp_values.size(); - for (size_t i = color_print_cnt; i >= 0 ; --i) + const auto color_print_cnt = (int)cp_values.size(); + for (int i = color_print_cnt; i >= 0 ; --i) { - size_t val = i; - while (val >= color_cnt) - val -= color_cnt; GCodePreviewData::Color color; - ::memcpy((void*)color.rgba, (const void*)(tool_colors.data() + val * 4), 4 * sizeof(float)); + ::memcpy((void*)color.rgba, (const void*)(tool_colors.data() + (i % color_cnt) * 4), 4 * sizeof(float)); if (color_print_cnt == 0) { items.emplace_back(Slic3r::I18N::translate(L("Default print color")), color); diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index 52030df48..65e8cd3fb 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -7288,31 +7288,15 @@ void GLCanvas3D::_load_print_object_toolpaths(const PrintObject& print_object, c const float* color_tool(size_t tool) const { return tool_colors->data() + tool * 4; } int volume_idx(int extruder, int feature) const { - return this->color_by_tool() ? std::min(this->number_tools() - 1, std::max(extruder - 1, 0)) : feature; + return this->color_by_color_print() ? 0 : this->color_by_tool() ? std::min(this->number_tools() - 1, std::max(extruder - 1, 0)) : feature; } // For coloring by a color_print(M600), return a parsed color. bool color_by_color_print() const { return color_print_values!=nullptr; } const float* color_print_by_layer_idx(const size_t layer_idx) const { - double z = layers[layer_idx]->print_z; - size_t layer = 0; - const size_t clr_change_cnt = color_print_values->size(); - for (size_t i = 0; i < clr_change_cnt; i++) - { - if (z < (*color_print_values)[i]-EPSILON) { - layer = i; - break; - } - if (layer == 0 && i == clr_change_cnt - 1) - layer = clr_change_cnt; - } - - const size_t clr_cnt = number_tools(); - while (layer >= clr_cnt) - layer -= clr_cnt; - - return color_tool(layer); + auto it = std::lower_bound(color_print_values->begin(), color_print_values->end(), layers[layer_idx]->print_z - EPSILON); + return color_tool((it - color_print_values->begin()) % number_tools()); } } ctxt; diff --git a/src/slic3r/GUI/GUI_Preview.cpp b/src/slic3r/GUI/GUI_Preview.cpp index eb35aa61e..c2a769586 100644 --- a/src/slic3r/GUI/GUI_Preview.cpp +++ b/src/slic3r/GUI/GUI_Preview.cpp @@ -737,9 +737,12 @@ void Preview::load_print_as_fff() if (m_gcode_preview_data->extrusion.view_type == GCodePreviewData::Extrusion::ColorPrint) { colors = GCodePreviewData::ColorPrintColors(); - - const auto& config = wxGetApp().preset_bundle->full_config(); - color_print_values = config.option("colorprint_heights")->values; + if (! gcode_preview_data_valid) { + //FIXME accessing full_config() is pretty expensive. + // Only initialize color_print_values for the initial preview, not for the full preview where the color_print_values is extracted from the G-code. + const auto& config = wxGetApp().preset_bundle->full_config(); + color_print_values = config.option("colorprint_heights")->values; + } } else if (gcode_preview_data_valid || (m_gcode_preview_data->extrusion.view_type == GCodePreviewData::Extrusion::Tool) ) { @@ -758,10 +761,9 @@ void Preview::load_print_as_fff() color = "#FFFFFF"; } - colors.push_back(color); + colors.emplace_back(color); } - if (!color_print_values.empty()) - color_print_values.clear(); + color_print_values.clear(); } if (IsShown())