From cd8e0d002ba30cd12c107d421aaeb128407a8f1f Mon Sep 17 00:00:00 2001 From: YuSanka Date: Tue, 19 Jul 2022 16:58:27 +0200 Subject: [PATCH] Cut WIP: Added "Place o cut" --- src/libslic3r/Model.cpp | 38 +++++++++++++++------------- src/libslic3r/Model.hpp | 2 +- src/slic3r/GUI/Gizmos/GLGizmoCut.cpp | 24 +++++++++++++++--- src/slic3r/GUI/Gizmos/GLGizmoCut.hpp | 2 ++ 4 files changed, 43 insertions(+), 23 deletions(-) diff --git a/src/libslic3r/Model.cpp b/src/libslic3r/Model.cpp index 19b2f8f7d..36034d904 100644 --- a/src/libslic3r/Model.cpp +++ b/src/libslic3r/Model.cpp @@ -1494,19 +1494,9 @@ ModelObjectPtrs ModelObject::cut(size_t instance, const Vec3d& cut_center, const instances[instance]->get_mirror() ); - const auto cut_matrix = Geometry::assemble_transform( - -cut_center, - Vec3d::Zero(), - Vec3d::Ones(), - Vec3d::Ones() - ); + const auto cut_matrix = Geometry::assemble_transform(-cut_center); - const auto invert_cut_matrix = Geometry::assemble_transform( - cut_center, - cut_rotation, - Vec3d::Ones(), - Vec3d::Ones() - ); + const auto invert_cut_matrix = Geometry::assemble_transform(cut_center, cut_rotation); // Displacement (in instance coordinates) to be applied to place the upper parts Vec3d local_displace = Vec3d::Zero(); @@ -1522,11 +1512,22 @@ ModelObjectPtrs ModelObject::cut(size_t instance, const Vec3d& cut_center, const if (!volume->is_model_part()) { if (volume->source.is_connector) { if (attributes.has(ModelObjectCutAttribute::KeepUpper)) { + + Transform3d m = attributes.has(ModelObjectCutAttribute::PlaceOnCutUpper) ? + Geometry::rotation_transform(cut_rotation).inverse() * cut_matrix * instance_matrix * volume_matrix : + instance_matrix * volume_matrix; + volume->set_transformation(m); + ModelVolume* vol = upper->add_volume(*volume); // make a "hole" dipper vol->set_scaling_factor(Z, 1.1 * vol->get_scaling_factor(Z)); } if (attributes.has(ModelObjectCutAttribute::KeepLower)) { + + Transform3d m = attributes.has(ModelObjectCutAttribute::PlaceOnCutLower) ? + Geometry::rotation_transform(Geometry::deg2rad(180.0) * Vec3d::UnitX()) * Geometry::rotation_transform(cut_rotation).inverse() * cut_matrix * instance_matrix * volume_matrix : + instance_matrix * volume_matrix; + volume->set_transformation(m); ModelVolume* vol = lower->add_volume(*volume); if (attributes.has(ModelObjectCutAttribute::CreateDowels)) // make a "hole" dipper @@ -1567,10 +1568,7 @@ ModelObjectPtrs ModelObject::cut(size_t instance, const Vec3d& cut_center, const // Transform the mesh by the combined transformation matrix. // Flip the triangles in case the composite transformation is left handed. TriangleMesh mesh(volume->mesh()); - mesh.transform(cut_matrix * instance_matrix * volume_matrix, true); - mesh.rotate(-cut_rotation.z(), Z); - mesh.rotate(-cut_rotation.y(), Y); - mesh.rotate(-cut_rotation.x(), X); + mesh.transform(Geometry::rotation_transform(cut_rotation).inverse() * cut_matrix * instance_matrix * volume_matrix, true); volume->reset_mesh(); // Reset volume transformation except for offset @@ -1590,7 +1588,8 @@ ModelObjectPtrs ModelObject::cut(size_t instance, const Vec3d& cut_center, const } if (attributes.has(ModelObjectCutAttribute::KeepUpper) && !upper_mesh.empty()) { - upper_mesh.transform(invert_cut_matrix); + if (!attributes.has(ModelObjectCutAttribute::PlaceOnCutUpper)) + upper_mesh.transform(invert_cut_matrix); ModelVolume* vol = upper->add_volume(upper_mesh); vol->name = volume->name; @@ -1601,7 +1600,10 @@ ModelObjectPtrs ModelObject::cut(size_t instance, const Vec3d& cut_center, const vol->set_material(volume->material_id(), *volume->material()); } if (attributes.has(ModelObjectCutAttribute::KeepLower) && !lower_mesh.empty()) { - lower_mesh.transform(invert_cut_matrix); + if (attributes.has(ModelObjectCutAttribute::PlaceOnCutLower)) + lower_mesh.transform(Geometry::assemble_transform(Vec3d::Zero(), Geometry::deg2rad(180.0)*Vec3d::UnitX())); + else + lower_mesh.transform(invert_cut_matrix); ModelVolume* vol = lower->add_volume(lower_mesh); vol->name = volume->name; diff --git a/src/libslic3r/Model.hpp b/src/libslic3r/Model.hpp index 72d9b33c1..6afa2efe8 100644 --- a/src/libslic3r/Model.hpp +++ b/src/libslic3r/Model.hpp @@ -304,7 +304,7 @@ enum class ModelVolumeType : int { SUPPORT_ENFORCER, }; -enum class ModelObjectCutAttribute : int { KeepUpper, KeepLower, FlipUpper, FlipLower, CreateDowels }; +enum class ModelObjectCutAttribute : int { KeepUpper, KeepLower, FlipUpper, FlipLower, PlaceOnCutUpper, PlaceOnCutLower, CreateDowels }; using ModelObjectCutAttributes = enum_bitmask; ENABLE_ENUM_BITMASK_OPERATORS(ModelObjectCutAttribute); diff --git a/src/slic3r/GUI/Gizmos/GLGizmoCut.cpp b/src/slic3r/GUI/Gizmos/GLGizmoCut.cpp index 5ea85964c..0d8eae28d 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoCut.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoCut.cpp @@ -1121,10 +1121,17 @@ void GLGizmoCut3D::on_render_input_window(float x, float y, float bottom_limit) m_imgui->disabled_begin(!connectors.empty()); m_imgui->checkbox(_L("Keep") + "##upper", connectors.empty() ? m_keep_upper : keep); m_imgui->disabled_end(); - ImGui::SameLine(); + ImGui::SameLine(3 * m_label_width); + m_imgui->disabled_begin(!m_keep_upper); m_imgui->disabled_begin(is_approx(Geometry::Transformation(m_rotation_m).get_rotation().x(), 0.) && is_approx(Geometry::Transformation(m_rotation_m).get_rotation().y(), 0.)); - m_imgui->checkbox(_L("Place on cut") + "##upper", m_rotate_upper); // #ysTODO implement place on cut instead of Flip? + + if (m_imgui->checkbox(_L("Place on cut") + "##upper", m_place_on_cut_upper)) + m_rotate_upper = false; + ImGui::SameLine(); + if (m_imgui->checkbox(_L("Flip") + "##upper", m_rotate_upper)) + m_place_on_cut_upper = false; + m_imgui->disabled_end(); m_imgui->disabled_end(); @@ -1134,11 +1141,18 @@ void GLGizmoCut3D::on_render_input_window(float x, float y, float bottom_limit) ImGui::SameLine(2 * m_label_width); m_imgui->disabled_begin(!connectors.empty()); + m_imgui->checkbox(_L("Keep") + "##lower", connectors.empty() ? m_keep_lower : keep); m_imgui->disabled_end(); - ImGui::SameLine(); + ImGui::SameLine(3 * m_label_width); m_imgui->disabled_begin(!m_keep_lower); - m_imgui->checkbox(_L("Place on cut") + "##lower", m_rotate_lower); // #ysTODO implement place on cut instead of Flip? + + if (m_imgui->checkbox(_L("Place on cut") + "##lower", m_place_on_cut_lower)) + m_rotate_lower = false; + ImGui::SameLine(); + if (m_imgui->checkbox(_L("Flip") + "##lower", m_rotate_lower)) + m_place_on_cut_lower = false; + m_imgui->disabled_end(); } @@ -1451,6 +1465,8 @@ void GLGizmoCut3D::perform_cut(const Selection& selection) plater->cut(object_idx, instance_idx, cut_center_offset, rotation, only_if(has_connectors ? true : m_keep_upper, ModelObjectCutAttribute::KeepUpper) | only_if(has_connectors ? true : m_keep_lower, ModelObjectCutAttribute::KeepLower) | + only_if(m_place_on_cut_upper, ModelObjectCutAttribute::PlaceOnCutUpper) | + only_if(m_place_on_cut_lower, ModelObjectCutAttribute::PlaceOnCutLower) | only_if(m_rotate_upper, ModelObjectCutAttribute::FlipUpper) | only_if(m_rotate_lower, ModelObjectCutAttribute::FlipLower) | only_if(create_dowels_as_separate_object, ModelObjectCutAttribute::CreateDowels)); diff --git a/src/slic3r/GUI/Gizmos/GLGizmoCut.hpp b/src/slic3r/GUI/Gizmos/GLGizmoCut.hpp index ea053d99f..dbb1e6ae0 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoCut.hpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoCut.hpp @@ -61,6 +61,8 @@ class GLGizmoCut3D : public GLGizmoBase bool m_keep_upper{ true }; bool m_keep_lower{ true }; + bool m_place_on_cut_upper{ true }; + bool m_place_on_cut_lower{ true }; bool m_rotate_upper{ false }; bool m_rotate_lower{ false };