From 8b04ecb7b1f3d54953ebd7d57e0c84cf9f5ae9a2 Mon Sep 17 00:00:00 2001 From: enricoturri1966 Date: Tue, 26 Mar 2024 20:29:50 +0800 Subject: [PATCH] FIX: layer Times in preview showed wrong color Jira: STUDIO-6612 code is from PrusaSlicer,thanks for PrusaSlicer and enricoturri1966 commit 3ce2d3a700ef215b37faef273f54be5619b9d642 Author: enricoturri1966 Date: Wed Apr 13 15:27:46 2022 +0200 #8176 - Tech ENABLE_USED_FILAMENT_POST_PROCESS - Fixes used filament data exported to gcode file not taking in account custom gcode Change-Id: Iafceb6c88f2a8b7ce1f2a34d2b392bf7a390d52f --- src/slic3r/GUI/GCodeViewer.cpp | 45 ++++++++++++++++++++++------------ src/slic3r/GUI/GCodeViewer.hpp | 8 ++++-- src/slic3r/GUI/GUI_App.hpp | 1 + 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/slic3r/GUI/GCodeViewer.cpp b/src/slic3r/GUI/GCodeViewer.cpp index 6f93a1389..da919eaff 100644 --- a/src/slic3r/GUI/GCodeViewer.cpp +++ b/src/slic3r/GUI/GCodeViewer.cpp @@ -262,31 +262,46 @@ void GCodeViewer::TBuffer::add_path(const GCodeProcessorResult::MoveVertex& move move.volumetric_rate(), move.layer_duration, move.extruder_id, move.cp_color_id, { { endpoint, endpoint } } }); } -GCodeViewer::Color GCodeViewer::Extrusions::Range::get_color_at(float value) const +GCodeViewer::Color GCodeViewer::Extrusions::Range::get_color_at(float value, EType type) const { - // Input value scaled to the colors range - const float step = step_size(); - const float global_t = (step != 0.0f) ? std::max(0.0f, value - min) / step : 0.0f; // lower limit of 0.0f - + float global_t = 0.0f; + const float step = step_size(type); + if (step > 0.0f) { + switch (type) { + default: + case EType::Linear: { + global_t = (value > min) ? (value - min) / step : 0.0f; + break; + } + case EType::Logarithmic: { + global_t = (value > min && min > 0.0f) ? ::log(value / min) / step : 0.0f; + break; + } + } + } const size_t color_max_idx = Range_Colors.size() - 1; // Compute the two colors just below (low) and above (high) the input value - const size_t color_low_idx = std::clamp(static_cast(global_t), 0, color_max_idx); + const size_t color_low_idx = std::clamp(static_cast(global_t), 0, color_max_idx); const size_t color_high_idx = std::clamp(color_low_idx + 1, 0, color_max_idx); // Compute how far the value is between the low and high colors so that they can be interpolated const float local_t = std::clamp(global_t - static_cast(color_low_idx), 0.0f, 1.0f); - // Interpolate between the low and high colors to find exactly which color the input value should get - Color ret = { 0.0f, 0.0f, 0.0f, 1.0f }; - for (unsigned int i = 0; i < 3; ++i) { - ret[i] = lerp(Range_Colors[color_low_idx][i], Range_Colors[color_high_idx][i], local_t); - } - return ret; + auto color = lerp(ColorRGBA(Range_Colors[color_low_idx]), ColorRGBA(Range_Colors[color_high_idx]), local_t); + return color.get_data(); } -float GCodeViewer::Extrusions::Range::step_size() const { - return (max - min) / (static_cast(Range_Colors.size()) - 1.0f); +float GCodeViewer::Extrusions::Range::step_size(EType type) const { + switch (type) { + default: + case EType::Linear: { + return (max > min) ? (max - min) / (static_cast(Range_Colors.size()) - 1.0f) : 0.0f; + } + case EType::Logarithmic: { + return (max > min && min > 0.0f) ? ::log(max / min) / (static_cast(Range_Colors.size()) - 1.0f) : 0.0f; + } + } } float GCodeViewer::Extrusions::Range::get_value_at_step(int step) const { @@ -3260,7 +3275,7 @@ void GCodeViewer::refresh_render_paths(bool keep_sequential_current_first, bool case EViewType::Feedrate: { color = m_extrusions.ranges.feedrate.get_color_at(path.feedrate); break; } case EViewType::FanSpeed: { color = m_extrusions.ranges.fan_speed.get_color_at(path.fan_speed); break; } case EViewType::Temperature: { color = m_extrusions.ranges.temperature.get_color_at(path.temperature); break; } - case EViewType::LayerTime: { color = m_extrusions.ranges.layer_duration.get_color_at(path.layer_time); break; } + case EViewType::LayerTime: { color = m_extrusions.ranges.layer_duration.get_color_at(path.layer_time, Extrusions::Range::EType::Logarithmic); break; } case EViewType::VolumetricRate: { color = m_extrusions.ranges.volumetric_rate.get_color_at(path.volumetric_rate); break; } case EViewType::Tool: { color = m_tools.m_tool_colors[path.extruder_id]; break; } case EViewType::ColorPrint: { diff --git a/src/slic3r/GUI/GCodeViewer.hpp b/src/slic3r/GUI/GCodeViewer.hpp index 7343f0c71..2a1f6aba4 100644 --- a/src/slic3r/GUI/GCodeViewer.hpp +++ b/src/slic3r/GUI/GCodeViewer.hpp @@ -396,6 +396,10 @@ class GCodeViewer { struct Range { + enum class EType : unsigned char { + Linear, + Logarithmic + }; float min; float max; unsigned int count; @@ -410,8 +414,8 @@ class GCodeViewer } void reset(bool log = false) { min = FLT_MAX; max = -FLT_MAX; count = 0; log_scale = log; } - float step_size() const; - Color get_color_at(float value) const; + float step_size(EType type = EType::Linear) const; + Color get_color_at(float value, EType type = EType::Linear) const; float get_value_at_step(int step) const; }; diff --git a/src/slic3r/GUI/GUI_App.hpp b/src/slic3r/GUI/GUI_App.hpp index aa99cae9f..4f1ec9c04 100644 --- a/src/slic3r/GUI/GUI_App.hpp +++ b/src/slic3r/GUI/GUI_App.hpp @@ -8,6 +8,7 @@ #include "OpenGLManager.hpp" #include "libslic3r/Preset.hpp" #include "libslic3r/PresetBundle.hpp" +#include "libslic3r/Color.hpp" #include "slic3r/GUI/DeviceManager.hpp" #include "slic3r/Utils/NetworkAgent.hpp" #include "slic3r/GUI/WebViewDialog.hpp"