From b71d61f0f7b895e7f63c117b2cffd90712cbbf1d Mon Sep 17 00:00:00 2001 From: enricoturri1966 Date: Tue, 27 Sep 2022 10:21:57 +0200 Subject: [PATCH] Measuring - Refactoring in GLGizmoMeasure related to scene raycasters state cache --- src/slic3r/GUI/GLCanvas3D.cpp | 6 +++--- src/slic3r/GUI/Gizmos/GLGizmoMeasure.cpp | 25 +++++++++++------------- src/slic3r/GUI/Gizmos/GLGizmoMeasure.hpp | 9 +++++++-- src/slic3r/GUI/SceneRaycaster.cpp | 25 ++++++++++++++++++++++++ src/slic3r/GUI/SceneRaycaster.hpp | 4 ++++ 5 files changed, 50 insertions(+), 19 deletions(-) diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index b5fea57be..ed82a3a8d 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -5517,11 +5517,11 @@ void GLCanvas3D::_picking_pass() ImGui::Separator(); imgui.text("Registered for picking:"); - sprintf(buf, "Beds: %d", (int)m_scene_raycaster.beds_count()); + sprintf(buf, "Beds: %d (%d)", (int)m_scene_raycaster.beds_count(), (int)m_scene_raycaster.active_beds_count()); imgui.text(std::string(buf)); - sprintf(buf, "Volumes: %d", (int)m_scene_raycaster.volumes_count()); + sprintf(buf, "Volumes: %d (%d)", (int)m_scene_raycaster.volumes_count(), (int)m_scene_raycaster.active_volumes_count()); imgui.text(std::string(buf)); - sprintf(buf, "Gizmo elements: %d", (int)m_scene_raycaster.gizmos_count()); + sprintf(buf, "Gizmo elements: %d (%d)", (int)m_scene_raycaster.gizmos_count(), (int)m_scene_raycaster.active_gizmos_count()); imgui.text(std::string(buf)); imgui.end(); #endif // ENABLE_RAYCAST_PICKING_DEBUG diff --git a/src/slic3r/GUI/Gizmos/GLGizmoMeasure.cpp b/src/slic3r/GUI/Gizmos/GLGizmoMeasure.cpp index fc49e9883..c511f7e50 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoMeasure.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoMeasure.cpp @@ -372,11 +372,13 @@ void GLGizmoMeasure::on_set_state() else { m_mode = EMode::BasicSelection; // store current state of scene raycaster for later use - m_scene_raycaster_state.clear(); - m_scene_raycasters = m_parent.get_raycasters_for_picking(SceneRaycaster::EType::Volume); - if (m_scene_raycasters != nullptr) { - for (const auto& r : *m_scene_raycasters) { - m_scene_raycaster_state.emplace_back(r->is_active()); + m_scene_raycasters.clear(); + auto scene_raycasters = m_parent.get_raycasters_for_picking(SceneRaycaster::EType::Volume); + if (scene_raycasters != nullptr) { + m_scene_raycasters.reserve(scene_raycasters->size()); + for (auto r : *scene_raycasters) { + SceneRaycasterState state = { r, r->is_active() }; + m_scene_raycasters.emplace_back(state); } } } @@ -772,20 +774,15 @@ void GLGizmoMeasure::update_if_needed() void GLGizmoMeasure::disable_scene_raycasters() { - if (m_scene_raycasters != nullptr) { - for (auto r : *m_scene_raycasters) { - r->set_active(false); - } + for (auto r : m_scene_raycasters) { + r.raycaster->set_active(false); } } void GLGizmoMeasure::restore_scene_raycasters_state() { - if (m_scene_raycasters != nullptr) { - assert(m_scene_raycasters->size() == m_scene_raycaster_state.size()); - for (size_t i = 0; i < m_scene_raycasters->size(); ++i) { - (*m_scene_raycasters)[i]->set_active(m_scene_raycaster_state[i]); - } + for (auto r : m_scene_raycasters) { + r.raycaster->set_active(r.state); } } diff --git a/src/slic3r/GUI/Gizmos/GLGizmoMeasure.hpp b/src/slic3r/GUI/Gizmos/GLGizmoMeasure.hpp index 11e861cc7..0e967472a 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoMeasure.hpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoMeasure.hpp @@ -89,8 +89,13 @@ class GLGizmoMeasure : public GLGizmoBase std::map> m_raycasters; std::optional m_curr_feature; std::optional m_curr_point_on_feature_position; - std::vector>* m_scene_raycasters{ nullptr }; - std::vector m_scene_raycaster_state; + struct SceneRaycasterState + { + std::shared_ptr raycaster{ nullptr }; + bool state{true}; + + }; + std::vector m_scene_raycasters; // These hold information to decide whether recalculation is necessary: std::vector m_volumes_matrices; diff --git a/src/slic3r/GUI/SceneRaycaster.cpp b/src/slic3r/GUI/SceneRaycaster.cpp index 49a59789e..78cb59c1b 100644 --- a/src/slic3r/GUI/SceneRaycaster.cpp +++ b/src/slic3r/GUI/SceneRaycaster.cpp @@ -173,6 +173,31 @@ void SceneRaycaster::render_hit(const Camera& camera) shader->stop_using(); } + +size_t SceneRaycaster::active_beds_count() const { + size_t count = 0; + for (const auto& b : m_bed) { + if (b->is_active()) + ++count; + } + return count; +} +size_t SceneRaycaster::active_volumes_count() const { + size_t count = 0; + for (const auto& v : m_volumes) { + if (v->is_active()) + ++count; + } + return count; +} +size_t SceneRaycaster::active_gizmos_count() const { + size_t count = 0; + for (const auto& g : m_gizmos) { + if (g->is_active()) + ++count; + } + return count; +} #endif // ENABLE_RAYCAST_PICKING_DEBUG std::vector>* SceneRaycaster::get_raycasters(EType type) diff --git a/src/slic3r/GUI/SceneRaycaster.hpp b/src/slic3r/GUI/SceneRaycaster.hpp index 8993c51a9..7acf82fe1 100644 --- a/src/slic3r/GUI/SceneRaycaster.hpp +++ b/src/slic3r/GUI/SceneRaycaster.hpp @@ -100,6 +100,10 @@ public: size_t beds_count() const { return m_bed.size(); } size_t volumes_count() const { return m_volumes.size(); } size_t gizmos_count() const { return m_gizmos.size(); } + + size_t active_beds_count() const; + size_t active_volumes_count() const; + size_t active_gizmos_count() const; #endif // ENABLE_RAYCAST_PICKING_DEBUG private: