From 6bcafd7c830ddfe5e8a6974f455d26279b0f0603 Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Tue, 17 Sep 2019 14:56:46 +0200 Subject: [PATCH] SLA gizmo no more uses IGL code directly, all was moved to the new MeshRaycaster class --- src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.cpp | 36 +++----------------- src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.hpp | 7 ---- src/slic3r/GUI/MeshUtils.cpp | 18 +++++++++- src/slic3r/GUI/MeshUtils.hpp | 2 ++ 4 files changed, 24 insertions(+), 39 deletions(-) diff --git a/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.cpp b/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.cpp index f15bc806b..992f4912a 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.cpp @@ -296,8 +296,9 @@ void GLGizmoSlaSupports::render_points(const Selection& selection, bool picking) // Matrices set, we can render the point mark now. // If in editing mode, we'll also render a cone pointing to the sphere. if (m_editing_mode) { + // in case the normal is not yet cached, find and cache it if (m_editing_cache[i].normal == Vec3f::Zero()) - update_cache_entry_normal(i); // in case the normal is not yet cached, find and cache it + m_mesh_raycaster->get_closest_point(m_editing_cache[i].support_point.pos, &m_editing_cache[i].normal); Eigen::Quaterniond q; q.setFromTwoVectors(Vec3d{0., 0., 1.}, instance_scaling_matrix_inverse * m_editing_cache[i].normal.cast()); @@ -366,17 +367,8 @@ void GLGizmoSlaSupports::update_mesh() m_its = &m_mesh->its; // If this is different mesh than last time or if the AABB tree is uninitialized, recalculate it. - if (m_model_object_id != m_model_object->id() || (m_AABB.m_left == NULL && m_AABB.m_right == NULL)) - { - //############################šš - m_AABB.deinit(); - m_AABB.init( - MapMatrixXfUnaligned(m_its->vertices.front().data(), m_its->vertices.size(), 3), - MapMatrixXiUnaligned(m_its->indices.front().data(), m_its->indices.size(), 3)); - //############################šš - + if (m_model_object_id != m_model_object->id() || ! m_mesh_raycaster) m_mesh_raycaster.reset(new MeshRaycaster(*m_mesh)); - } m_model_object_id = m_model_object->id(); disable_editing_mode(); @@ -389,7 +381,7 @@ void GLGizmoSlaSupports::update_mesh() bool GLGizmoSlaSupports::unproject_on_mesh(const Vec2d& mouse_pos, std::pair& pos_and_normal) { // if the gizmo doesn't have the V, F structures for igl, calculate them first: - if (m_its == nullptr) + if (! m_mesh_raycaster) update_mesh(); const Camera& camera = m_parent.get_camera(); @@ -658,23 +650,6 @@ std::vector GLGizmoSlaSupports::get_config_options(const st } -void GLGizmoSlaSupports::update_cache_entry_normal(size_t i) const -{ - int idx = 0; - Eigen::Matrix pp = m_editing_cache[i].support_point.pos; - Eigen::Matrix cc; - m_AABB.squared_distance( - MapMatrixXfUnaligned(m_its->vertices.front().data(), m_its->vertices.size(), 3), - MapMatrixXiUnaligned(m_its->indices.front().data(), m_its->indices.size(), 3), - pp, idx, cc); - Vec3f a = (m_its->vertices[m_its->indices[idx](1)] - m_its->vertices[m_its->indices[idx](0)]); - Vec3f b = (m_its->vertices[m_its->indices[idx](2)] - m_its->vertices[m_its->indices[idx](0)]); - m_editing_cache[i].normal = a.cross(b); -} - - - - ClippingPlane GLGizmoSlaSupports::get_sla_clipping_plane() const { if (!m_model_object || m_state == Off || m_clipping_plane_distance == 0.f) @@ -1027,8 +1002,7 @@ void GLGizmoSlaSupports::on_set_state() m_parent.toggle_model_objects_visibility(true); m_normal_cache.clear(); m_clipping_plane_distance = 0.f; - // Release triangle mesh slicer and the AABB spatial search structure. - m_AABB.deinit(); + // Release clippers and the AABB raycaster. m_its = nullptr; m_object_clipper.reset(); m_supports_clipper.reset(); diff --git a/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.hpp b/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.hpp index 8a55f65d1..cf5245f19 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.hpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.hpp @@ -4,11 +4,6 @@ #include "GLGizmoBase.hpp" #include "slic3r/GUI/GLSelectionRectangle.hpp" -// There is an L function in igl that would be overridden by our localization macro - let's undefine it... -#undef L -#include -#include "slic3r/GUI/I18N.hpp" // ...and redefine again when we are done with the igl code - #include "libslic3r/SLA/SLACommon.hpp" #include @@ -38,7 +33,6 @@ private: GLUquadricObj* m_quadric; typedef Eigen::Map> MapMatrixXfUnaligned; typedef Eigen::Map> MapMatrixXiUnaligned; - igl::AABB m_AABB; std::unique_ptr m_mesh_raycaster; const TriangleMesh* m_mesh; @@ -101,7 +95,6 @@ private: void render_clipping_plane(const Selection& selection) const; bool is_mesh_update_necessary() const; void update_mesh(); - void update_cache_entry_normal(size_t i) const; bool unsaved_changes() const; bool m_lock_unique_islands = false; diff --git a/src/slic3r/GUI/MeshUtils.cpp b/src/slic3r/GUI/MeshUtils.cpp index 2c0089053..e559020e9 100644 --- a/src/slic3r/GUI/MeshUtils.cpp +++ b/src/slic3r/GUI/MeshUtils.cpp @@ -240,11 +240,27 @@ std::vector MeshRaycaster::get_unobscured_idxs(const Geometry::Transfo if (! is_obscured) out.push_back(i); } - return out; } +Vec3f MeshRaycaster::get_closest_point(const Vec3f& point, Vec3f* normal) const +{ + int idx = 0; + Eigen::Matrix closest_point; + m_AABB_wrapper->m_AABB.squared_distance( + AABBWrapper::MapMatrixXfUnaligned(m_mesh->its.vertices.front().data(), m_mesh->its.vertices.size(), 3), + AABBWrapper::MapMatrixXiUnaligned(m_mesh->its.indices.front().data(), m_mesh->its.indices.size(), 3), + point, idx, closest_point); + if (normal) { + igl::Hit imag_hit; + imag_hit.id = idx; + *normal = m_AABB_wrapper->get_hit_normal(imag_hit); + } + return closest_point; +} + + } // namespace GUI } // namespace Slic3r diff --git a/src/slic3r/GUI/MeshUtils.hpp b/src/slic3r/GUI/MeshUtils.hpp index 2573b4b89..21c375e81 100644 --- a/src/slic3r/GUI/MeshUtils.hpp +++ b/src/slic3r/GUI/MeshUtils.hpp @@ -103,6 +103,8 @@ public: std::vector get_unobscured_idxs(const Geometry::Transformation& trafo, const Camera& camera, const std::vector& points, std::function fn_ignore_hit) const; + Vec3f get_closest_point(const Vec3f& point, Vec3f* normal = nullptr) const; + private: // PIMPL wrapper around igl::AABB so I don't have to include the header-only IGL here class AABBWrapper;