From bed64cb7c7ceac5531762c06c715d92b15556ad6 Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Mon, 5 Dec 2022 13:28:56 +0100 Subject: [PATCH] Measurement: optimize plane highlighting --- src/libslic3r/Measure.cpp | 29 ++++++++++++++++-------- src/libslic3r/Measure.hpp | 11 +++++---- src/slic3r/GUI/Gizmos/GLGizmoMeasure.cpp | 14 ++++-------- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/libslic3r/Measure.cpp b/src/libslic3r/Measure.cpp index 6368f30f5..89af1cf82 100644 --- a/src/libslic3r/Measure.cpp +++ b/src/libslic3r/Measure.cpp @@ -68,7 +68,8 @@ public: std::vector get_all_features() const; std::optional get_feature(size_t face_idx, const Vec3d& point) const; - std::vector> get_planes_triangle_indices() const; + int get_num_of_planes() const; + const std::vector& get_plane_triangle_indices(int idx) const; const std::vector& get_plane_features(unsigned int plane_id) const; const TriangleMesh& get_mesh() const; @@ -554,12 +555,17 @@ std::optional MeasuringImpl::get_feature(size_t face_idx, const -std::vector> MeasuringImpl::get_planes_triangle_indices() const +int MeasuringImpl::get_num_of_planes() const { - std::vector> out; - for (const PlaneData& plane : m_planes) - out.emplace_back(plane.facets); - return out; + return (m_planes.size()); +} + + + +const std::vector& MeasuringImpl::get_plane_triangle_indices(int idx) const +{ + assert(idx >= 0 && idx < int(m_planes.size())); + return m_planes[idx].facets; } const std::vector& MeasuringImpl::get_plane_features(unsigned int plane_id) const @@ -602,10 +608,15 @@ std::optional Measuring::get_feature(size_t face_idx, const Vec3 } - -std::vector> Measuring::get_planes_triangle_indices() const +int Measuring::get_num_of_planes() const { - return priv->get_planes_triangle_indices(); + return priv->get_num_of_planes(); +} + + +const std::vector& Measuring::get_plane_triangle_indices(int idx) const +{ + return priv->get_plane_triangle_indices(idx); } const std::vector& Measuring::get_plane_features(unsigned int plane_id) const diff --git a/src/libslic3r/Measure.hpp b/src/libslic3r/Measure.hpp index 3a1a4b89d..b1d2e611f 100644 --- a/src/libslic3r/Measure.hpp +++ b/src/libslic3r/Measure.hpp @@ -102,11 +102,12 @@ public: // should be highlighted (if any). std::optional get_feature(size_t face_idx, const Vec3d& point) const; - // Returns a list of triangle indices for each identified plane. Each - // Plane object contains an index into this vector. Expensive, do not - // call too often. - std::vector> get_planes_triangle_indices() const; - + // Return total number of planes. + int get_num_of_planes() const; + + // Returns a list of triangle indices for given plane. + const std::vector& get_plane_triangle_indices(int idx) const; + // Returns the surface features of the plane with the given index const std::vector& get_plane_features(unsigned int plane_id) const; diff --git a/src/slic3r/GUI/Gizmos/GLGizmoMeasure.cpp b/src/slic3r/GUI/Gizmos/GLGizmoMeasure.cpp index 8c4b46826..0e3f480cc 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoMeasure.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoMeasure.cpp @@ -93,11 +93,8 @@ static std::string center_on_feature_type_as_string(Measure::SurfaceFeatureType return ret; } -static GLModel::Geometry init_plane_data(const indexed_triangle_set& its, const std::vector>& planes_triangles, int idx) +static GLModel::Geometry init_plane_data(const indexed_triangle_set& its, const std::vector& triangle_indices) { - assert(0 <= idx && idx < (int)planes_triangles.size()); - const std::vector& triangle_indices = planes_triangles[idx]; - GLModel::Geometry init_data; init_data.format = { GUI::GLModel::Geometry::EPrimitiveType::Triangles, GLModel::Geometry::EVertexLayout::P3N3 }; unsigned int i = 0; @@ -653,8 +650,8 @@ void GLGizmoMeasure::on_render() if (m_last_plane_idx != idx) { m_last_plane_idx = idx; const indexed_triangle_set& its = m_measuring->get_mesh().its; - const std::vector> planes_triangles = m_measuring->get_planes_triangle_indices(); - GLModel::Geometry init_data = init_plane_data(its, planes_triangles, idx); + const std::vector& plane_triangles = m_measuring->get_plane_triangle_indices(idx); + GLModel::Geometry init_data = init_plane_data(its, plane_triangles); m_plane.reset(); m_plane.mesh_raycaster = std::make_unique(std::make_shared(init_data.get_as_indexed_triangle_set())); m_plane.model.init_from(std::move(init_data)); @@ -1041,10 +1038,9 @@ void GLGizmoMeasure::update_if_needed() { 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 (int idx = 0; idx < (int)planes_triangles.size(); ++idx) { + for (int idx = 0; idx < m_measuring->get_num_of_planes(); ++idx) { m_plane_models_cache.emplace_back(GLModel()); - GLModel::Geometry init_data = init_plane_data(its, planes_triangles, idx); + GLModel::Geometry init_data = init_plane_data(its, m_measuring->get_plane_triangle_indices(idx)); m_plane_models_cache.back().init_from(std::move(init_data)); } };