From e312f3be4374f4684110b0897191f2c0339e9cc4 Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Fri, 15 May 2020 14:16:56 +0200 Subject: [PATCH] FDM supports gizmo: caching triangle normals in order to increase performance --- src/slic3r/GUI/Gizmos/GLGizmoFdmSupports.cpp | 6 ++++-- src/slic3r/GUI/MeshUtils.cpp | 15 +++++---------- src/slic3r/GUI/MeshUtils.hpp | 9 +++++++-- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/slic3r/GUI/Gizmos/GLGizmoFdmSupports.cpp b/src/slic3r/GUI/Gizmos/GLGizmoFdmSupports.cpp index 0fea328bb..6843bab46 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoFdmSupports.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoFdmSupports.cpp @@ -506,8 +506,10 @@ void GLGizmoFdmSupports::update_vertex_buffers(const ModelVolume* mv, if (status != type) continue; for (int i=0; i<3; ++i) - iva.push_geometry(mesh->its.vertices[mesh->its.indices[facet_idx](i)].cast(), - MeshRaycaster::get_triangle_normal(mesh->its, facet_idx).cast()); + iva.push_geometry( + mesh->its.vertices[mesh->its.indices[facet_idx](i)].cast(), + m_c->raycaster()->raycasters()[mesh_id]->get_triangle_normal(facet_idx).cast() + ); iva.push_triangle(3*triangle_cnt, 3*triangle_cnt+1, 3*triangle_cnt+2); ++triangle_cnt; } diff --git a/src/slic3r/GUI/MeshUtils.cpp b/src/slic3r/GUI/MeshUtils.cpp index 6c4f5e49d..18526d8ce 100644 --- a/src/slic3r/GUI/MeshUtils.cpp +++ b/src/slic3r/GUI/MeshUtils.cpp @@ -95,11 +95,9 @@ void MeshClipper::recalculate_triangles() } -Vec3f MeshRaycaster::get_triangle_normal(const indexed_triangle_set& its, size_t facet_idx) +Vec3f MeshRaycaster::get_triangle_normal(size_t facet_idx) const { - Vec3f a(its.vertices[its.indices[facet_idx](1)] - its.vertices[its.indices[facet_idx](0)]); - Vec3f b(its.vertices[its.indices[facet_idx](2)] - its.vertices[its.indices[facet_idx](0)]); - return Vec3f(a.cross(b)).normalized(); + return m_normals[facet_idx]; } void MeshRaycaster::line_from_mouse_pos(const Vec2d& mouse_pos, const Transform3d& trafo, const Camera& camera, @@ -218,12 +216,9 @@ Vec3f MeshRaycaster::get_closest_point(const Vec3f& point, Vec3f* normal) const int idx = 0; Vec3d closest_point; m_emesh.squared_distance(point.cast(), idx, closest_point); - if (normal) { - auto indices = m_emesh.F().row(idx); - Vec3d a(m_emesh.V().row(indices(1)) - m_emesh.V().row(indices(0))); - Vec3d b(m_emesh.V().row(indices(2)) - m_emesh.V().row(indices(0))); - *normal = Vec3f(a.cross(b).cast()); - } + if (normal) + *normal = m_normals[idx]; + return closest_point.cast(); } diff --git a/src/slic3r/GUI/MeshUtils.hpp b/src/slic3r/GUI/MeshUtils.hpp index 92f444f55..8e321730c 100644 --- a/src/slic3r/GUI/MeshUtils.hpp +++ b/src/slic3r/GUI/MeshUtils.hpp @@ -108,7 +108,11 @@ public: // The pointer can be invalidated after constructor returns. MeshRaycaster(const TriangleMesh& mesh) : m_emesh(mesh) - {} + { + m_normals.reserve(mesh.stl.facet_start.size()); + for (const stl_facet& facet : mesh.stl.facet_start) + m_normals.push_back(facet.normal); + } void line_from_mouse_pos(const Vec2d& mouse_pos, const Transform3d& trafo, const Camera& camera, Vec3d& point, Vec3d& direction) const; @@ -140,10 +144,11 @@ public: Vec3f get_closest_point(const Vec3f& point, Vec3f* normal = nullptr) const; - static Vec3f get_triangle_normal(const indexed_triangle_set& its, size_t facet_idx); + Vec3f get_triangle_normal(size_t facet_idx) const; private: sla::EigenMesh3D m_emesh; + std::vector m_normals; };