From 3fea03f8850bb7fd8af22cb34fedad74ac475362 Mon Sep 17 00:00:00 2001
From: Enrico Turri <enricoturri@seznam.cz>
Date: Thu, 18 Apr 2019 15:59:12 +0200
Subject: [PATCH] Picking using rectangle selection WIP

---
 src/slic3r/GUI/GLCanvas3D.cpp           | 84 +++++++++++++++++++++----
 src/slic3r/GUI/GLSelectionRectangle.hpp | 11 +++-
 2 files changed, 81 insertions(+), 14 deletions(-)

diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp
index 1fa3b2bdd..3edfabb58 100644
--- a/src/slic3r/GUI/GLCanvas3D.cpp
+++ b/src/slic3r/GUI/GLCanvas3D.cpp
@@ -3648,14 +3648,7 @@ void GLCanvas3D::_refresh_if_shown_on_screen()
 
 void GLCanvas3D::_picking_pass() const
 {
-//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
-//    const Vec2d& pos = m_mouse.position;
-//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
-
-//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
     if (m_picking_enabled && !m_mouse.dragging && (m_mouse.position != Vec2d(DBL_MAX, DBL_MAX)))
-//    if (m_picking_enabled && !m_mouse.dragging && (pos != Vec2d(DBL_MAX, DBL_MAX)))
-//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
     {
         // Render the object for picking.
         // FIXME This cannot possibly work in a multi - sampled context as the color gets mangled by the anti - aliasing.
@@ -3687,16 +3680,10 @@ void GLCanvas3D::_picking_pass() const
 
         GLubyte color[4] = { 0, 0, 0, 0 };
         const Size& cnv_size = get_canvas_size();
-//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
         bool inside = (0 <= m_mouse.position(0)) && (m_mouse.position(0) < cnv_size.get_width()) && (0 <= m_mouse.position(1)) && (m_mouse.position(1) < cnv_size.get_height());
-//        bool inside = (0 <= pos(0)) && (pos(0) < cnv_size.get_width()) && (0 <= pos(1)) && (pos(1) < cnv_size.get_height());
-//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
         if (inside)
         {
-//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
             glsafe(::glReadPixels(m_mouse.position(0), cnv_size.get_height() - m_mouse.position(1) - 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, (void*)color));
-//            glsafe(::glReadPixels(pos(0), cnv_size.get_height() - pos(1) - 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, (void*)color));
-//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
             volume_id = color[0] + color[1] * 256 + color[2] * 256 * 256;
         }
         if ((0 <= volume_id) && (volume_id < (int)m_volumes.volumes.size()))
@@ -3719,6 +3706,77 @@ void GLCanvas3D::_rectangular_selection_picking_pass() const
 {
     if (m_picking_enabled)
     {
+        if (m_multisample_allowed)
+            glsafe(::glDisable(GL_MULTISAMPLE));
+
+        glsafe(::glDisable(GL_BLEND));
+        glsafe(::glEnable(GL_DEPTH_TEST));
+
+        glsafe(::glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
+
+        _render_volumes_for_picking();
+
+        if (m_multisample_allowed)
+            glsafe(::glEnable(GL_MULTISAMPLE));
+
+        int width = (int)m_rectangle_selection.get_width();
+        int height = (int)m_rectangle_selection.get_height();
+
+        if ((width > 0) && (height > 0))
+        {
+            int left = (int)m_rectangle_selection.get_left();
+            int top = get_canvas_size().get_height() - (int)m_rectangle_selection.get_top();
+            if ((left >= 0) && (top >= 0))
+            {
+                std::vector<std::array<unsigned char, 4>> frame(width * height);
+//                std::vector<GLubyte> frame(4 * width * height);
+                glsafe(::glReadPixels(left, top, width, height, GL_RGBA, GL_UNSIGNED_BYTE, (void*)frame.data()));
+
+#if 1
+                std::sort(frame.begin(), frame.end());
+                frame.resize(std::distance(frame.begin(), std::unique(frame.begin(), frame.end())));
+                if (frame.size() > 1)
+                {
+                    std::cout << frame.size() - 1 << std::endl;
+                }
+
+/*
+                std::set<int> idxs;
+                for (int r = 0; r < height; ++r)
+                {
+                    for (int c = 0; c < width; ++c)
+                    {
+                        int id_px = 4 * (r * width + c);
+                        idxs.insert(frame[id_px] + frame[id_px + 1] * 256 + frame[id_px + 2] * 256 * 256);
+                    }
+                }
+
+                if (idxs.size() > 1)
+                {
+                    std::cout << idxs.size() - 1 << std::endl;
+                }
+*/
+#else
+                wxImage frame_image(width, height);
+                unsigned char* image_data = frame_image.GetData();
+                for (int r = 0; r < height; ++r)
+                {
+                    for (int c = 0; c < width; ++c)
+                    {
+                        int id_px = r * width + c;
+                        ::memcpy((void*)(image_data + 3 * id_px), (const void*)&frame[4 * id_px], 3);
+                    }
+                }
+
+                wxImageHistogram frame_histogram;
+                unsigned long colors_count = frame_image.ComputeHistogram(frame_histogram);
+                if (colors_count > 1)
+                {
+                    std::cout << colors_count - 1 << std::endl;
+                }
+#endif
+            }
+        }
     }
 }
 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
diff --git a/src/slic3r/GUI/GLSelectionRectangle.hpp b/src/slic3r/GUI/GLSelectionRectangle.hpp
index 21e53a172..7cab96f41 100644
--- a/src/slic3r/GUI/GLSelectionRectangle.hpp
+++ b/src/slic3r/GUI/GLSelectionRectangle.hpp
@@ -55,7 +55,16 @@ public:
 
     bool is_active() const { return m_status != Off; }
     EState get_status() const { return m_status; }
-    
+
+//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+    float get_width() const { return std::abs(m_start_corner(0) - m_end_corner(0)); }
+    float get_height() const { return std::abs(m_start_corner(1) - m_end_corner(1)); }
+    float get_left() const { return std::min(m_start_corner(0), m_end_corner(0)); }
+    float get_right() const { return std::max(m_start_corner(0), m_end_corner(0)); }
+    float get_top() const { return std::max(m_start_corner(1), m_end_corner(1)); }
+    float get_bottom() const { return std::min(m_start_corner(1), m_end_corner(1)); }
+//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+
 private:
     EState m_status = Off;
     Vec2d m_start_corner;