From 62a28c7baf6807fc3b14c069957806e355432553 Mon Sep 17 00:00:00 2001 From: enricoturri1966 Date: Wed, 24 Aug 2022 12:00:04 +0200 Subject: [PATCH] Measuring: refactoring related to plane models cache --- src/slic3r/GUI/Gizmos/GLGizmoMeasure.cpp | 41 +++++++++++++++--------- src/slic3r/GUI/Gizmos/GLGizmoMeasure.hpp | 2 +- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/slic3r/GUI/Gizmos/GLGizmoMeasure.cpp b/src/slic3r/GUI/Gizmos/GLGizmoMeasure.cpp index 4e7cd3db1..68fbd2aaa 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoMeasure.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoMeasure.cpp @@ -233,23 +233,28 @@ void GLGizmoMeasure::on_render() case Measure::SurfaceFeatureType::Plane: { const auto& [idx, normal, pt] = feature.get_plane(); - assert(idx < m_plane_models.size()); + assert(idx < m_plane_models_cache.size()); const Transform3d view_model_matrix = view_matrix * model_matrix; shader->set_uniform("view_model_matrix", view_model_matrix); const Matrix3d view_normal_matrix = view_matrix.matrix().block(0, 0, 3, 3) * model_matrix.matrix().block(0, 0, 3, 3).inverse().transpose(); shader->set_uniform("view_normal_matrix", view_normal_matrix); - m_plane_models[idx]->set_color(HOVER_COLOR); - m_plane_models[idx]->render(); + m_plane_models_cache[idx].set_color(HOVER_COLOR); + m_plane_models_cache[idx].render(); break; } } } #if ENABLE_DEBUG_DIALOG - if (m_show_planes) - for (const auto& glmodel : m_plane_models) { - glmodel->set_color(HOVER_COLOR); - glmodel->render(); + if (m_show_planes) { + const Transform3d view_model_matrix = view_matrix * model_matrix; + shader->set_uniform("view_model_matrix", view_model_matrix); + const Matrix3d view_normal_matrix = view_matrix.matrix().block(0, 0, 3, 3) * model_matrix.matrix().block(0, 0, 3, 3).inverse().transpose(); + shader->set_uniform("view_normal_matrix", view_normal_matrix); + for (GLModel& glmodel : m_plane_models_cache) { + glmodel.set_color(HOVER_COLOR); + glmodel.render(); } + } #endif // ENABLE_DEBUG_DIALOG } @@ -279,17 +284,14 @@ void GLGizmoMeasure::on_render_for_picking() void GLGizmoMeasure::update_if_needed() { - auto do_update = [this](const ModelObject* object, const ModelVolume* volume) { - const indexed_triangle_set& its = (volume != nullptr) ? volume->mesh().its : object->volumes.front()->mesh().its; - m_measuring.reset(new Measure::Measuring(its)); - m_plane_models.clear(); + auto update_plane_models_cache = [this](const indexed_triangle_set& its) { + m_plane_models_cache.clear(); const std::vector> planes_triangles = m_measuring->get_planes_triangle_indices(); for (const std::vector& triangle_indices : planes_triangles) { - m_plane_models.emplace_back(std::unique_ptr(new GLModel())); + m_plane_models_cache.emplace_back(GLModel()); GLModel::Geometry init_data; init_data.format = { GUI::GLModel::Geometry::EPrimitiveType::Triangles, GLModel::Geometry::EVertexLayout::P3N3 }; - init_data.color = ColorRGBA(0.9f, 0.9f, 0.9f, 0.5f); - int i = 0; + unsigned int i = 0; for (int idx : triangle_indices) { const Vec3f& v0 = its.vertices[its.indices[idx][0]]; const Vec3f& v1 = its.vertices[its.indices[idx][1]]; @@ -302,8 +304,15 @@ void GLGizmoMeasure::update_if_needed() init_data.add_triangle(i, i + 1, i + 2); i += 3; } - m_plane_models.back()->init_from(std::move(init_data)); + m_plane_models_cache.back().init_from(std::move(init_data)); } + }; + + auto do_update = [this, update_plane_models_cache](const ModelObject* object, const ModelVolume* volume) { + const indexed_triangle_set& its = (volume != nullptr) ? volume->mesh().its : object->volumes.front()->mesh().its; + m_measuring.reset(new Measure::Measuring(its)); + + update_plane_models_cache(its); // Let's save what we calculated it from: m_volumes_matrices.clear(); @@ -315,7 +324,7 @@ void GLGizmoMeasure::update_if_needed() m_volumes_matrices.push_back(vol->get_matrix()); m_volumes_types.push_back(vol->type()); } - m_first_instance_scale = object->instances.front()->get_scaling_factor(); + m_first_instance_scale = object->instances.front()->get_scaling_factor(); m_first_instance_mirror = object->instances.front()->get_mirror(); } m_old_model_object = object; diff --git a/src/slic3r/GUI/Gizmos/GLGizmoMeasure.hpp b/src/slic3r/GUI/Gizmos/GLGizmoMeasure.hpp index 23abdb314..2cbdd1506 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoMeasure.hpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoMeasure.hpp @@ -35,6 +35,7 @@ private: GLModel m_sphere; GLModel m_cylinder; GLModel m_circle; + std::vector m_plane_models_cache; // This holds information to decide whether recalculation is necessary: std::vector m_volumes_matrices; @@ -54,7 +55,6 @@ private: bool m_show_all = false; bool m_show_planes = false; #endif // ENABLE_DEBUG_DIALOG - std::vector> m_plane_models; void update_if_needed();