From 61210025168c837d821cc1c5e686c7199562511d Mon Sep 17 00:00:00 2001 From: Wang YB <94800665+Gradbb@users.noreply.github.com> Date: Tue, 30 Apr 2024 10:38:59 +0800 Subject: [PATCH] add make_overhang_printable --- src/libslic3r/ClipperUtils.cpp | 31 +++++++++++ src/libslic3r/ClipperUtils.hpp | 35 +++++++++++++ src/libslic3r/Preset.cpp | 2 + src/libslic3r/Print.hpp | 3 +- src/libslic3r/PrintConfig.cpp | 30 +++++++++++ src/libslic3r/PrintConfig.hpp | 5 ++ src/libslic3r/PrintObject.cpp | 6 ++- src/libslic3r/PrintObjectSlice.cpp | 75 ++++++++++++++++++++++++++- src/slic3r/GUI/ConfigManipulation.cpp | 5 ++ src/slic3r/GUI/Tab.cpp | 4 ++ 10 files changed, 193 insertions(+), 3 deletions(-) diff --git a/src/libslic3r/ClipperUtils.cpp b/src/libslic3r/ClipperUtils.cpp index dfbb757..1bba138 100644 --- a/src/libslic3r/ClipperUtils.cpp +++ b/src/libslic3r/ClipperUtils.cpp @@ -758,6 +758,24 @@ Slic3r::ExPolygons diff_ex(const Slic3r::SurfacesPtr &subject, const Slic3r::Pol { return _clipper_ex(ClipperLib::ctDifference, ClipperUtils::SurfacesPtrProvider(subject), ClipperUtils::PolygonsProvider(clip), do_safety_offset); } Slic3r::ExPolygons diff_ex(const Slic3r::SurfacesPtr &subject, const Slic3r::ExPolygons &clip, ApplySafetyOffset do_safety_offset) { return _clipper_ex(ClipperLib::ctDifference, ClipperUtils::SurfacesPtrProvider(subject), ClipperUtils::ExPolygonsProvider(clip), do_safety_offset); } +//w31 +inline Slic3r::ExPolygons diff_ex(const Slic3r::Polygon& subject, const Slic3r::Polygons& clip, ApplySafetyOffset do_safety_offset) +{ + Slic3r::Polygons subject_temp; + subject_temp.push_back(subject); + + return diff_ex(subject_temp, clip, do_safety_offset); +} + +inline Slic3r::ExPolygons diff_ex(const Slic3r::Polygon& subject, const Slic3r::Polygon& clip, ApplySafetyOffset do_safety_offset) +{ + Slic3r::Polygons subject_temp; + Slic3r::Polygons clip_temp; + + subject_temp.push_back(subject); + clip_temp.push_back(clip); + return diff_ex(subject_temp, clip_temp, do_safety_offset); +} Slic3r::ExPolygons intersection_ex(const Slic3r::Polygons &subject, const Slic3r::Polygons &clip, ApplySafetyOffset do_safety_offset) { return _clipper_ex(ClipperLib::ctIntersection, ClipperUtils::PolygonsProvider(subject), ClipperUtils::PolygonsProvider(clip), do_safety_offset); } @@ -777,6 +795,14 @@ Slic3r::ExPolygons intersection_ex(const Slic3r::Surfaces &subject, const Slic3r { return _clipper_ex(ClipperLib::ctIntersection, ClipperUtils::SurfacesProvider(subject), ClipperUtils::SurfacesProvider(clip), do_safety_offset); } Slic3r::ExPolygons intersection_ex(const Slic3r::SurfacesPtr &subject, const Slic3r::ExPolygons &clip, ApplySafetyOffset do_safety_offset) { return _clipper_ex(ClipperLib::ctIntersection, ClipperUtils::SurfacesPtrProvider(subject), ClipperUtils::ExPolygonsProvider(clip), do_safety_offset); } +//w31 +Slic3r::ExPolygons intersection_ex(const Slic3r::ExPolygon& subject, const Slic3r::ExPolygon& clip, ApplySafetyOffset do_safety_offset) + { return _clipper_ex(ClipperLib::ctIntersection, ClipperUtils::ExPolygonProvider(subject), ClipperUtils::ExPolygonProvider(clip), do_safety_offset); } +Slic3r::ExPolygons intersection_ex(const Slic3r::ExPolygons& subject, const Slic3r::ExPolygon& clip, ApplySafetyOffset do_safety_offset) + { return _clipper_ex(ClipperLib::ctIntersection, ClipperUtils::ExPolygonsProvider(subject), ClipperUtils::ExPolygonProvider(clip), do_safety_offset);} +Slic3r::ExPolygons intersection_ex(const Slic3r::ExPolygon& subject, const Slic3r::ExPolygons& clip, ApplySafetyOffset do_safety_offset) + { return _clipper_ex(ClipperLib::ctIntersection, ClipperUtils::ExPolygonProvider(subject), ClipperUtils::ExPolygonsProvider(clip), do_safety_offset);} + // May be used to "heal" unusual models (3DLabPrints etc.) by providing fill_type (pftEvenOdd, pftNonZero, pftPositive, pftNegative). Slic3r::ExPolygons union_ex(const Slic3r::Polygons &subject, ClipperLib::PolyFillType fill_type) { return _clipper_ex(ClipperLib::ctUnion, ClipperUtils::PolygonsProvider(subject), ClipperUtils::EmptyPathsProvider(), ApplySafetyOffset::No, fill_type); } @@ -793,6 +819,11 @@ Slic3r::ExPolygons union_ex(const Slic3r::ExPolygons &subject, const Slic3r::Pol Slic3r::ExPolygons union_ex(const Slic3r::Surfaces &subject) { return PolyTreeToExPolygons(clipper_do_polytree(ClipperLib::ctUnion, ClipperUtils::SurfacesProvider(subject), ClipperUtils::EmptyPathsProvider(), ClipperLib::pftNonZero)); } +//w31 +Slic3r::ExPolygons xor_ex(const Slic3r::ExPolygons &subject, const Slic3r::ExPolygon &clip, ApplySafetyOffset do_safety_offset) { + return _clipper_ex(ClipperLib::ctXor, ClipperUtils::ExPolygonsProvider(subject), ClipperUtils::ExPolygonProvider(clip), do_safety_offset); +} + template Polylines _clipper_pl_open(ClipperLib::ClipType clipType, PathsProvider1 &&subject, PathsProvider2 &&clip) { diff --git a/src/libslic3r/ClipperUtils.hpp b/src/libslic3r/ClipperUtils.hpp index 7d036eb..de81008 100644 --- a/src/libslic3r/ClipperUtils.hpp +++ b/src/libslic3r/ClipperUtils.hpp @@ -447,6 +447,33 @@ Slic3r::Polylines diff_pl(const Slic3r::Polylines &subject, const Slic3r::ExPol Slic3r::Polylines diff_pl(const Slic3r::Polylines &subject, const Slic3r::ExPolygons &clip); Slic3r::Polylines diff_pl(const Slic3r::Polygons &subject, const Slic3r::Polygons &clip); +//w31 +inline Slic3r::ExPolygons diff_ex(const Slic3r::ExPolygon& subject, const Slic3r::ExPolygon& clip, ApplySafetyOffset do_safety_offset = ApplySafetyOffset::No) +{ + Slic3r::ExPolygons subject_temp; + Slic3r::ExPolygons clip_temp; + + subject_temp.push_back(subject); + clip_temp.push_back(clip); + return diff_ex(subject_temp, clip_temp); +} + +inline Slic3r::ExPolygons diff_ex(const Slic3r::ExPolygon& subject, const Slic3r::ExPolygons& clip, ApplySafetyOffset do_safety_offset = ApplySafetyOffset::No) +{ + Slic3r::ExPolygons subject_temp; + subject_temp.push_back(subject); + + return diff_ex(subject_temp, clip, do_safety_offset); +} + +inline Slic3r::ExPolygons diff_ex(const Slic3r::ExPolygons& subject, const Slic3r::ExPolygon& clip, ApplySafetyOffset do_safety_offset = ApplySafetyOffset::No) +{ + Slic3r::ExPolygons clip_temp; + clip_temp.push_back(clip); + + return diff_ex(subject, clip_temp, do_safety_offset); +} + inline Slic3r::Lines diff_ln(const Slic3r::Lines &subject, const Slic3r::Polygons &clip) { return _clipper_ln(ClipperLib::ctDifference, subject, clip); @@ -473,6 +500,11 @@ Slic3r::ExPolygons intersection_ex(const Slic3r::Surfaces &subject, const Slic3r Slic3r::ExPolygons intersection_ex(const Slic3r::Surfaces &subject, const Slic3r::ExPolygons &clip, ApplySafetyOffset do_safety_offset = ApplySafetyOffset::No); Slic3r::ExPolygons intersection_ex(const Slic3r::Surfaces &subject, const Slic3r::Surfaces &clip, ApplySafetyOffset do_safety_offset = ApplySafetyOffset::No); Slic3r::ExPolygons intersection_ex(const Slic3r::SurfacesPtr &subject, const Slic3r::ExPolygons &clip, ApplySafetyOffset do_safety_offset = ApplySafetyOffset::No); +//w31 +Slic3r::ExPolygons intersection_ex(const Slic3r::ExPolygon& subject, const Slic3r::ExPolygon& clip, ApplySafetyOffset do_safety_offset = ApplySafetyOffset::No); +Slic3r::ExPolygons intersection_ex(const Slic3r::ExPolygons& subject, const Slic3r::ExPolygon& clip, ApplySafetyOffset do_safety_offset = ApplySafetyOffset::No); +Slic3r::ExPolygons intersection_ex(const Slic3r::ExPolygon& subject, const Slic3r::ExPolygons& clip, ApplySafetyOffset do_safety_offset = ApplySafetyOffset::No); + Slic3r::Polylines intersection_pl(const Slic3r::Polylines &subject, const Slic3r::Polygon &clip); Slic3r::Polylines intersection_pl(const Slic3r::Polyline &subject, const Slic3r::ExPolygon &clip); Slic3r::Polylines intersection_pl(const Slic3r::Polylines &subject, const Slic3r::ExPolygon &clip); @@ -513,6 +545,9 @@ Slic3r::ExPolygons union_ex(const Slic3r::Surfaces &subject); ClipperLib::PolyTree union_pt(const Slic3r::Polygons &subject); ClipperLib::PolyTree union_pt(const Slic3r::ExPolygons &subject); +//w31 +Slic3r::ExPolygons xor_ex(const Slic3r::ExPolygons &subject, const Slic3r::ExPolygon &clip, ApplySafetyOffset do_safety_offset = ApplySafetyOffset::No); + Slic3r::Polygons union_pt_chained_outside_in(const Slic3r::Polygons &subject); ClipperLib::PolyNodes order_nodes(const ClipperLib::PolyNodes &nodes); diff --git a/src/libslic3r/Preset.cpp b/src/libslic3r/Preset.cpp index 3b7059c..0722f74 100644 --- a/src/libslic3r/Preset.cpp +++ b/src/libslic3r/Preset.cpp @@ -493,6 +493,8 @@ static std::vector s_Preset_print_options { ,"max_bridge_length" //w30 ,"top_solid_infill_flow_ratio", "bottom_solid_infill_flow_ratio" + //w31 + ,"make_overhang_printable", "make_overhang_printable_angle", "make_overhang_printable_hole_size" }; static std::vector s_Preset_filament_options { diff --git a/src/libslic3r/Print.hpp b/src/libslic3r/Print.hpp index 2047c43..279440e 100644 --- a/src/libslic3r/Print.hpp +++ b/src/libslic3r/Print.hpp @@ -270,7 +270,8 @@ public: && this->config().brim_width.value > 0. && ! this->has_raft(); } - + //w31 + void apply_conical_overhang(); // This is the *total* layer count (including support layers) // this value is not supposed to be compared with Layer::id // since they have different semantics. diff --git a/src/libslic3r/PrintConfig.cpp b/src/libslic3r/PrintConfig.cpp index 856ebee..3bd071d 100644 --- a/src/libslic3r/PrintConfig.cpp +++ b/src/libslic3r/PrintConfig.cpp @@ -3698,6 +3698,36 @@ void PrintConfigDef::init_fff_params() def->mode = comAdvanced; def->set_default_value(new ConfigOptionBool(false)); + //w31 + def = this->add("make_overhang_printable", coBool); + def->label = L("Make overhang printable"); + def->category = L("Advanced"); + def->tooltip = L("Modify the geometry to print overhangs without support material."); + def->mode = comAdvanced; + def->set_default_value(new ConfigOptionBool(false)); + + def = this->add("make_overhang_printable_angle", coFloat); + def->label = L("Make overhang printable maximum angle"); + def->category = L("Advanced"); + def->tooltip = L("Maximum angle of overhangs to allow after making more steep overhangs printable." + "90° will not change the model at all and allow any overhang, while 0 will " + "replace all overhangs with conical material."); + def->sidetext = L("°"); + def->mode = comAdvanced; + def->min = 0.; + def->max = 90.; + def->set_default_value(new ConfigOptionFloat(55.)); + + def = this->add("make_overhang_printable_hole_size", coFloat); + def->label = L("Make overhang printable hole area"); + def->category = L("Advanced"); + def->tooltip = L("Maximum area of a hole in the base of the model before it's filled by conical material." + "A value of 0 will fill all the holes in the model base."); + def->sidetext = L("mm²"); + def->mode = comAdvanced; + def->min = 0.; + def->set_default_value(new ConfigOptionFloat(0.)); + def = this->add("wall_transition_length", coFloatOrPercent); def->label = L("Perimeter transition length"); def->category = L("Advanced"); diff --git a/src/libslic3r/PrintConfig.hpp b/src/libslic3r/PrintConfig.hpp index 3647f64..2de5514 100644 --- a/src/libslic3r/PrintConfig.hpp +++ b/src/libslic3r/PrintConfig.hpp @@ -605,6 +605,9 @@ PRINT_CONFIG_CLASS_DEFINE( ((ConfigOptionBool, only_one_wall_first_layer)) //w27 ((ConfigOptionBool, precise_z_height)) + //w31 + ((ConfigOptionFloat, make_overhang_printable_angle)) + ((ConfigOptionFloat, make_overhang_printable_hole_size)) ) PRINT_CONFIG_CLASS_DEFINE( @@ -671,6 +674,8 @@ PRINT_CONFIG_CLASS_DEFINE( //w30 ((ConfigOptionFloat, top_solid_infill_flow_ratio)) ((ConfigOptionFloat, bottom_solid_infill_flow_ratio)) + //w31 + ((ConfigOptionBool, make_overhang_printable)) ) PRINT_CONFIG_CLASS_DEFINE( diff --git a/src/libslic3r/PrintObject.cpp b/src/libslic3r/PrintObject.cpp index 57a5940..535e51f 100644 --- a/src/libslic3r/PrintObject.cpp +++ b/src/libslic3r/PrintObject.cpp @@ -876,7 +876,11 @@ bool PrintObject::invalidate_state_by_config_options( //w17 || opt_key == "top_area_threshold" //w23 - || opt_key == "only_one_wall_first_layer") { + || opt_key == "only_one_wall_first_layer" + //w31 + || opt_key == "make_overhang_printable" + || opt_key == "make_overhang_printable_angle" + || opt_key == "make_overhang_printable_hole_size") { steps.emplace_back(posSlice); } else if ( opt_key == "seam_position" diff --git a/src/libslic3r/PrintObjectSlice.cpp b/src/libslic3r/PrintObjectSlice.cpp index f3cf7c3..027f1ef 100644 --- a/src/libslic3r/PrintObjectSlice.cpp +++ b/src/libslic3r/PrintObjectSlice.cpp @@ -760,7 +760,9 @@ void PrintObject::slice_volumes() BOOST_LOG_TRIVIAL(debug) << "Slicing volumes - MMU segmentation"; apply_mm_segmentation(*this, [print]() { print->throw_if_canceled(); }); } - + //w31 + this->apply_conical_overhang(); + m_print->throw_if_canceled(); BOOST_LOG_TRIVIAL(debug) << "Slicing volumes - make_slices in parallel - begin"; { @@ -908,6 +910,77 @@ void PrintObject::slice_volumes() m_print->throw_if_canceled(); BOOST_LOG_TRIVIAL(debug) << "Slicing volumes - make_slices in parallel - end"; } +//w31 +void PrintObject::apply_conical_overhang() +{ + BOOST_LOG_TRIVIAL(info) << "Make overhang printable..."; + + if (m_layers.empty()) { + return; + } + + const double conical_overhang_angle = this->config().make_overhang_printable_angle; + if (conical_overhang_angle == 90.0) { + return; + } + const double angle_radians = conical_overhang_angle * M_PI / 180.; + const double max_hole_area = this->config().make_overhang_printable_hole_size; + const double tan_angle = tan(angle_radians); + BOOST_LOG_TRIVIAL(info) << "angle " << angle_radians << " maxHoleArea " << max_hole_area << " tan_angle " << tan_angle; + const coordf_t layer_thickness = m_config.layer_height.value; + const coordf_t max_dist_from_lower_layer = tan_angle * layer_thickness; + BOOST_LOG_TRIVIAL(info) << "layer_thickness " << layer_thickness << " max_dist_from_lower_layer " << max_dist_from_lower_layer; + + const coordf_t scaled_max_dist_from_lower_layer = -float(scale_(max_dist_from_lower_layer)); + const coordf_t scaled_max_hole_area = float(scale_(scale_(max_hole_area))); + + for (auto i = m_layers.rbegin() + 1; i != m_layers.rend(); ++i) { + m_print->throw_if_canceled(); + Layer *layer = *i; + Layer *upper_layer = layer->upper_layer; + + if (upper_layer->empty()) { + continue; + } + + if (std::all_of(layer->m_regions.begin(), layer->m_regions.end(), + [](const LayerRegion *r) { return r->slices().empty() || !r->region().config().make_overhang_printable; })) { + continue; + } + auto upper_poly = upper_layer->merged(float(SCALED_EPSILON)); + upper_poly = union_ex(upper_poly); + + if (scaled_max_hole_area > 0.0) { + + auto current_poly = layer->merged(float(SCALED_EPSILON)); + current_poly = union_ex(current_poly); + + for (auto layer_polygon : current_poly) { + for (auto hole : layer_polygon.holes) { + if (std::abs(hole.area()) < scaled_max_hole_area) { + ExPolygon hole_poly(hole); + auto hole_with_above = intersection_ex(upper_poly, hole_poly); + if (!hole_with_above.empty()) { + auto hole_difference = xor_ex(hole_with_above, hole_poly); + if (hole_difference.empty()) { + upper_poly = diff_ex(upper_poly, hole_poly); + } + } + } + } + } + } + upper_poly = offset_ex(upper_poly, scaled_max_dist_from_lower_layer); + for (size_t region_id = 0; region_id < this->num_printing_regions(); ++region_id) { + if (!upper_layer->m_regions[region_id]->region().config().make_overhang_printable) { + continue; + } + auto p = intersection_ex(upper_layer->m_regions[region_id]->slices().surfaces, upper_poly); + ExPolygons layer_polygons = to_expolygons(layer->m_regions[region_id]->slices().surfaces); + layer->m_regions[region_id]->m_slices.set(union_ex(layer_polygons, p), stInternal); + } + } +} //w12 ExPolygons PrintObject::_shrink_contour_holes(double contour_delta, double hole_delta, const ExPolygons &polys) const { diff --git a/src/slic3r/GUI/ConfigManipulation.cpp b/src/slic3r/GUI/ConfigManipulation.cpp index 3c429b8..6325013 100644 --- a/src/slic3r/GUI/ConfigManipulation.cpp +++ b/src/slic3r/GUI/ConfigManipulation.cpp @@ -377,6 +377,11 @@ void ConfigManipulation::toggle_print_fff_options(DynamicPrintConfig* config) //w21 bool is_top_one_wall = config->opt_enum("top_one_wall_type") != TopOneWallType::Disable; toggle_field("top_area_threshold", is_top_one_wall); + + //w31 + bool have_make_overhang_printable = config->opt_bool("make_overhang_printable"); + toggle_field("make_overhang_printable_angle", have_make_overhang_printable); + toggle_field("make_overhang_printable_hole_size", have_make_overhang_printable); } void ConfigManipulation::toggle_print_sla_options(DynamicPrintConfig* config) diff --git a/src/slic3r/GUI/Tab.cpp b/src/slic3r/GUI/Tab.cpp index 7e5f7be..8d82d27 100644 --- a/src/slic3r/GUI/Tab.cpp +++ b/src/slic3r/GUI/Tab.cpp @@ -1474,6 +1474,10 @@ void TabPrint::build() optgroup->append_single_option_line("top_area_threshold"); //w23 optgroup->append_single_option_line("only_one_wall_first_layer"); + //w31 + optgroup->append_single_option_line("make_overhang_printable"); + optgroup->append_single_option_line("make_overhang_printable_angle"); + optgroup->append_single_option_line("make_overhang_printable_hole_size"); optgroup = page->new_optgroup(L("Fuzzy skin (experimental)")); category_path = "fuzzy-skin_246186/#";