From fe59958ea84539c2252bc1a009247f997f4686b6 Mon Sep 17 00:00:00 2001
From: Enrico Turri <enricoturri@seznam.cz>
Date: Wed, 7 Mar 2018 09:17:59 +0100
Subject: [PATCH] GCode Preview - Unified preview data ranges to ensure proper
 paths colors

---
 xs/src/libslic3r/GCode/Analyzer.cpp    | 12 +++---
 xs/src/libslic3r/GCode/PreviewData.cpp | 59 +++++++++-----------------
 xs/src/libslic3r/GCode/PreviewData.hpp |  9 ++--
 xs/src/slic3r/GUI/3DScene.cpp          |  8 ++--
 4 files changed, 34 insertions(+), 54 deletions(-)

diff --git a/xs/src/libslic3r/GCode/Analyzer.cpp b/xs/src/libslic3r/GCode/Analyzer.cpp
index d4041ac19..ee1e55beb 100644
--- a/xs/src/libslic3r/GCode/Analyzer.cpp
+++ b/xs/src/libslic3r/GCode/Analyzer.cpp
@@ -688,9 +688,9 @@ void GCodeAnalyzer::_calc_gcode_preview_extrusion_layers(GCodePreviewData& previ
     Helper::store_polyline(polyline, data, z, preview_data);
 
     // updates preview ranges data
-    preview_data.extrusion.ranges.height.set_from(height_range);
-    preview_data.extrusion.ranges.width.set_from(width_range);
-    preview_data.extrusion.ranges.feedrate.set_from(feedrate_range);
+    preview_data.ranges.height.set_from(height_range);
+    preview_data.ranges.width.set_from(width_range);
+    preview_data.ranges.feedrate.set_from(feedrate_range);
 }
 
 void GCodeAnalyzer::_calc_gcode_preview_travel(GCodePreviewData& preview_data)
@@ -759,9 +759,9 @@ void GCodeAnalyzer::_calc_gcode_preview_travel(GCodePreviewData& preview_data)
     Helper::store_polyline(polyline, type, direction, feedrate, extruder_id, preview_data);
 
     // updates preview ranges data
-    preview_data.travel.ranges.height.set_from(height_range);
-    preview_data.travel.ranges.width.set_from(width_range);
-    preview_data.travel.ranges.feedrate.set_from(feedrate_range);
+    preview_data.ranges.height.set_from(height_range);
+    preview_data.ranges.width.set_from(width_range);
+    preview_data.ranges.feedrate.set_from(feedrate_range);
 }
 
 void GCodeAnalyzer::_calc_gcode_preview_retractions(GCodePreviewData& preview_data)
diff --git a/xs/src/libslic3r/GCode/PreviewData.cpp b/xs/src/libslic3r/GCode/PreviewData.cpp
index 73ec30eef..b9be6643b 100644
--- a/xs/src/libslic3r/GCode/PreviewData.cpp
+++ b/xs/src/libslic3r/GCode/PreviewData.cpp
@@ -164,9 +164,6 @@ void GCodePreviewData::Extrusion::set_default()
     view_type = Default_View_Type;
 
     ::memcpy((void*)role_colors, (const void*)Default_Extrusion_Role_Colors, Num_Extrusion_Roles * sizeof(Color));
-    ::memcpy((void*)ranges.height.colors, (const void*)Range::Default_Colors, Range::Colors_Count * sizeof(Color));
-    ::memcpy((void*)ranges.width.colors, (const void*)Range::Default_Colors, Range::Colors_Count * sizeof(Color));
-    ::memcpy((void*)ranges.feedrate.colors, (const void*)Range::Default_Colors, Range::Colors_Count * sizeof(Color));
 
     for (unsigned int i = 0; i < Num_Extrusion_Roles; ++i)
     {
@@ -205,10 +202,6 @@ void GCodePreviewData::Travel::set_default()
     height = Default_Height;
     ::memcpy((void*)type_colors, (const void*)Default_Type_Colors, Num_Types * sizeof(Color));
 
-    ::memcpy((void*)ranges.height.colors, (const void*)Range::Default_Colors, Range::Colors_Count * sizeof(Color));
-    ::memcpy((void*)ranges.width.colors, (const void*)Range::Default_Colors, Range::Colors_Count * sizeof(Color));
-    ::memcpy((void*)ranges.feedrate.colors, (const void*)Range::Default_Colors, Range::Colors_Count * sizeof(Color));
-
     is_visible = false;
 }
 
@@ -239,6 +232,10 @@ GCodePreviewData::GCodePreviewData()
 
 void GCodePreviewData::set_default()
 {
+    ::memcpy((void*)ranges.height.colors, (const void*)Range::Default_Colors, Range::Colors_Count * sizeof(Color));
+    ::memcpy((void*)ranges.width.colors, (const void*)Range::Default_Colors, Range::Colors_Count * sizeof(Color));
+    ::memcpy((void*)ranges.feedrate.colors, (const void*)Range::Default_Colors, Range::Colors_Count * sizeof(Color));
+
     extrusion.set_default();
     travel.set_default();
     retraction.set_default();
@@ -248,6 +245,9 @@ void GCodePreviewData::set_default()
 
 void GCodePreviewData::reset()
 {
+    ranges.width.reset();
+    ranges.height.reset();
+    ranges.feedrate.reset();
     extrusion.layers.clear();
     travel.polylines.clear();
     retraction.positions.clear();
@@ -264,19 +264,19 @@ const GCodePreviewData::Color& GCodePreviewData::get_extrusion_role_color(Extrus
     return extrusion.role_colors[role];
 }
 
-const GCodePreviewData::Color& GCodePreviewData::get_extrusion_height_color(float height) const
+const GCodePreviewData::Color& GCodePreviewData::get_height_color(float height) const
 {
-    return extrusion.ranges.height.get_color_at(height);
+    return ranges.height.get_color_at(height);
 }
 
-const GCodePreviewData::Color& GCodePreviewData::get_extrusion_width_color(float width) const
+const GCodePreviewData::Color& GCodePreviewData::get_width_color(float width) const
 {
-    return extrusion.ranges.width.get_color_at(width);
+    return ranges.width.get_color_at(width);
 }
 
-const GCodePreviewData::Color& GCodePreviewData::get_extrusion_feedrate_color(float feedrate) const
+const GCodePreviewData::Color& GCodePreviewData::get_feedrate_color(float feedrate) const
 {
-    return extrusion.ranges.feedrate.get_color_at(feedrate);
+    return ranges.feedrate.get_color_at(feedrate);
 }
 
 void GCodePreviewData::set_extrusion_role_color(const std::string& role_name, float red, float green, float blue, float alpha)
@@ -356,26 +356,16 @@ GCodePreviewData::LegendItemsList GCodePreviewData::get_legend_items(const std::
 {
     struct Helper
     {
-        static void FillListFromRange(LegendItemsList& list, const std::vector<const Range*>& ranges, unsigned int decimals, float scale_factor)
+        static void FillListFromRange(LegendItemsList& list, const Range& range, unsigned int decimals, float scale_factor)
         {
-            if (ranges.empty())
-                return;
-
             list.reserve(Range::Colors_Count);
 
-            Range total_range;
-            for (const Range* range : ranges)
-            {
-                if (range != nullptr)
-                    total_range.update_from(*range);
-            }
-
-            float step = total_range.step_size();
+            float step = range.step_size();
             for (unsigned int i = 0; i < Range::Colors_Count; ++i)
             {
                 char buf[1024];
-                sprintf(buf, "%.*f/%.*f", decimals, scale_factor * (total_range.min + (float)i * step), decimals, scale_factor * (total_range.min + (float)(i + 1) * step));
-                list.emplace_back(buf, ranges[0]->colors[i]);
+                sprintf(buf, "%.*f/%.*f", decimals, scale_factor * (range.min + (float)i * step), decimals, scale_factor * (range.min + (float)(i + 1) * step));
+                list.emplace_back(buf, range.colors[i]);
             }
         }
     };
@@ -399,26 +389,17 @@ GCodePreviewData::LegendItemsList GCodePreviewData::get_legend_items(const std::
         }
     case Extrusion::Height:
         {
-            std::vector<const Range*> ranges;
-            ranges.push_back(&extrusion.ranges.height);
-            ranges.push_back(&travel.ranges.height);
-            Helper::FillListFromRange(items, ranges, 3, 1.0f);
+            Helper::FillListFromRange(items, ranges.height, 3, 1.0f);
             break;
         }
     case Extrusion::Width:
         {
-            std::vector<const Range*> ranges;
-            ranges.push_back(&extrusion.ranges.width);
-            ranges.push_back(&travel.ranges.width);
-            Helper::FillListFromRange(items, ranges, 3, 1.0f);
+            Helper::FillListFromRange(items, ranges.width, 3, 1.0f);
             break;
         }
     case Extrusion::Feedrate:
         {
-            std::vector<const Range*> ranges;
-            ranges.push_back(&extrusion.ranges.feedrate);
-            ranges.push_back(&travel.ranges.feedrate);
-            Helper::FillListFromRange(items, ranges, 0, 1.0f);
+            Helper::FillListFromRange(items, ranges.feedrate, 0, 1.0f);
             break;
         }
     case Extrusion::Tool:
diff --git a/xs/src/libslic3r/GCode/PreviewData.hpp b/xs/src/libslic3r/GCode/PreviewData.hpp
index 9579bf900..6375af5f0 100644
--- a/xs/src/libslic3r/GCode/PreviewData.hpp
+++ b/xs/src/libslic3r/GCode/PreviewData.hpp
@@ -92,7 +92,6 @@ public:
         EViewType view_type;
         Color role_colors[Num_Extrusion_Roles];
         std::string role_names[Num_Extrusion_Roles];
-        Ranges ranges;
         LayersList layers;
         unsigned int role_flags;
 
@@ -141,7 +140,6 @@ public:
         float height;
         Color type_colors[Num_Types];
         bool is_visible;
-        Ranges ranges;
 
         void set_default();
     };
@@ -180,6 +178,7 @@ public:
     Retraction retraction;
     Retraction unretraction;
     Shell shell;
+    Ranges ranges;
 
     GCodePreviewData();
 
@@ -188,9 +187,9 @@ public:
     bool empty() const;
 
     const Color& get_extrusion_role_color(ExtrusionRole role) const;
-    const Color& get_extrusion_height_color(float height) const;
-    const Color& get_extrusion_width_color(float width) const;
-    const Color& get_extrusion_feedrate_color(float feedrate) const;
+    const Color& get_height_color(float height) const;
+    const Color& get_width_color(float width) const;
+    const Color& get_feedrate_color(float feedrate) const;
 
     void set_extrusion_role_color(const std::string& role_name, float red, float green, float blue, float alpha);
     void set_extrusion_paths_colors(const std::vector<std::string>& colors);
diff --git a/xs/src/slic3r/GUI/3DScene.cpp b/xs/src/slic3r/GUI/3DScene.cpp
index 04e7f7dc6..8a1b19c51 100644
--- a/xs/src/slic3r/GUI/3DScene.cpp
+++ b/xs/src/slic3r/GUI/3DScene.cpp
@@ -1777,11 +1777,11 @@ void _3DScene::_load_gcode_extrusion_paths(const GCodePreviewData& preview_data,
             case GCodePreviewData::Extrusion::FeatureType:
                 return data.get_extrusion_role_color((ExtrusionRole)(int)value);
             case GCodePreviewData::Extrusion::Height:
-                return data.get_extrusion_height_color(value);
+                return data.get_height_color(value);
             case GCodePreviewData::Extrusion::Width:
-                return data.get_extrusion_width_color(value);
+                return data.get_width_color(value);
             case GCodePreviewData::Extrusion::Feedrate:
-                return data.get_extrusion_feedrate_color(value);
+                return data.get_feedrate_color(value);
             case GCodePreviewData::Extrusion::Tool:
                 {
                     static GCodePreviewData::Color color;
@@ -2061,7 +2061,7 @@ bool _3DScene::_travel_paths_by_feedrate(const GCodePreviewData& preview_data, G
     // creates a new volume for each feedrate
     for (Feedrate& feedrate : feedrates)
     {
-        GLVolume* volume = new GLVolume(preview_data.get_extrusion_feedrate_color(feedrate.value).rgba);
+        GLVolume* volume = new GLVolume(preview_data.get_feedrate_color(feedrate.value).rgba);
         if (volume == nullptr)
             return false;
         else