From 2005882ac5db693b70f7105e96d85307ed1f8503 Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Tue, 28 Mar 2023 11:22:19 +0200 Subject: [PATCH] Cut: always toggle the closest part --- src/slic3r/GUI/Gizmos/GLGizmoCut.cpp | 34 +++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/src/slic3r/GUI/Gizmos/GLGizmoCut.cpp b/src/slic3r/GUI/Gizmos/GLGizmoCut.cpp index 2f48f4392d..4aaa7ea8b0 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoCut.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoCut.cpp @@ -1428,18 +1428,25 @@ void GLGizmoCut3D::PartSelection::render(const Vec3d* normal) void GLGizmoCut3D::PartSelection::toggle_selection(const Vec2d& mouse_pos) { // FIXME: Cache the transforms. - const Camera& camera = wxGetApp().plater()->get_camera(); + const Camera& camera = wxGetApp().plater()->get_camera(); + const Vec3d& camera_pos = camera.get_position(); Vec3f pos; Vec3f normal; - + + std::vector> hits_id_and_sqdist; + for (size_t id=0; idvolumes[id]->get_offset(); Transform3d tr = model_object->instances[instance_idx]->get_matrix() * model_object->volumes[id]->get_matrix(); if (parts[id].raycaster.unproject_on_mesh(mouse_pos, tr, camera, pos, normal)) { - parts[id].selected = ! parts[id].selected; - return; - } + hits_id_and_sqdist.emplace_back(id, (camera_pos - tr*(pos.cast())).squaredNorm()); + } + } + if (! hits_id_and_sqdist.empty()) { + size_t id = std::min_element(hits_id_and_sqdist.begin(), hits_id_and_sqdist.end(), + [](const std::pair& a, const std::pair& b) { return a.second < b.second; })->first; + parts[id].selected = ! parts[id].selected; } } @@ -2470,7 +2477,22 @@ bool GLGizmoCut3D::unproject_on_cut_plane(const Vec2d& mouse_position, Vec3d& po hit = (point + t * direction); } else return false; - + + // Now check if the hit is not obscured by a selected part on this side of the plane. + // FIXME: This would be better solved by remembering which contours are active. We will + // probably need that anyway because there is not other way to find out which contours + // to render. If you want to uncomment it, fix it first. It does not work yet. + /*for (size_t id = 0; id < m_part_selection.parts.size(); ++id) { + if (! m_part_selection.parts[id].selected) { + Vec3f pos, normal; + const ModelObject* model_object = m_part_selection.model_object; + const Vec3d volume_offset = m_part_selection.model_object->volumes[id]->get_offset(); + Transform3d tr = model_object->instances[m_part_selection.instance_idx]->get_matrix() * model_object->volumes[id]->get_matrix(); + if (m_part_selection.parts[id].raycaster.unproject_on_mesh(mouse_position, tr, camera, pos, normal)) + return false; + } + }*/ + if (m_c->object_clipper()->is_projection_inside_cut(hit) == -1) return false;