diff --git a/src/libslic3r/Brim.cpp b/src/libslic3r/Brim.cpp index 9cee5a0e4b..9fb1ccf519 100644 --- a/src/libslic3r/Brim.cpp +++ b/src/libslic3r/Brim.cpp @@ -1,5 +1,6 @@ #include "ClipperUtils.hpp" #include "EdgeGrid.hpp" +#include "ExclusionVolumeGeometry.hpp" #include "Layer.hpp" #include "Print.hpp" #include "ShortestPath.hpp" @@ -10,6 +11,7 @@ #include #include #include +#include #include #include @@ -24,6 +26,51 @@ namespace Slic3r { +static std::vector brim_physical_extruders( + const Print &print, + const size_t filament_id, + const size_t physical_extruder_count) +{ + if (physical_extruder_count == 0) + return {}; + + if (const auto grouping = print.get_layered_nozzle_group_result()) { + if (const auto nozzle = grouping->get_nozzle_for_filament(int(filament_id), 0); + nozzle.has_value() && nozzle->extruder_id >= 0 && + size_t(nozzle->extruder_id) < physical_extruder_count) + return { size_t(nozzle->extruder_id) }; + } + + const bool automatic_mapping_resolved = + !is_auto_filament_map_mode(print.get_filament_map_mode()) || !print.is_BBL_printer() || + print.get_nozzle_group_result() != nullptr; + const int resolved = bed_exclusion_extruder_for_filament( + filament_id, print.get_filament_maps(), print.get_filament_map_mode(), print.is_BBL_printer(), + automatic_mapping_resolved, physical_extruder_count); + if (resolved >= 0) + return { size_t(resolved) }; + + // Generated geometry must remain safe if a late automatic assignment has + // not selected a physical nozzle yet. + std::vector candidates(physical_extruder_count); + std::iota(candidates.begin(), candidates.end(), size_t(0)); + return candidates; +} + +static coord_t brim_exclusion_clearance(const Flow &flow) +{ + const coord_t width_spacing_delta = std::max(0, flow.scaled_width() - flow.scaled_spacing()); + return (width_spacing_delta + 1) / 2 + coord_t(SCALED_EPSILON); +} + +static bool combine_object_brims(const Print &print) +{ + const bool has_per_object_skirt_or_shield = print.config().skirt_type == stPerObject && + (print.has_skirt() || print.has_infinite_skirt()); + return print.config().combine_brims.value && !has_per_object_skirt_or_shield && + print.config().print_sequence != PrintSequence::ByObject; +} + static void append_and_translate(ExPolygons &dst, const ExPolygons &src, const PrintInstance &instance) { size_t dst_idx = dst.size(); expolygons_append(dst, src); @@ -419,6 +466,14 @@ static ExPolygons outer_inner_brim_area(const Print& print, { unsigned int support_material_extruder = printExtruders.front() + 1; Flow flow = print.brim_flow(); + const std::vector> exclusion_regions_by_extruder = + get_bed_excluded_regions_by_extruder(print.config()); + const double first_layer_top_z = std::max(0.0, print.skirt_first_layer_height()); + const coord_t exclusion_clearance = brim_exclusion_clearance(flow); + std::vector combined_brim_physical_extruders; + if (combine_object_brims(print) && !printExtruders.empty()) + combined_brim_physical_extruders = brim_physical_extruders( + print, printExtruders.front(), exclusion_regions_by_extruder.size()); ExPolygons brim_area; ExPolygons no_brim_area; @@ -617,8 +672,6 @@ static ExPolygons outer_inner_brim_area(const Print& print, if (extruder_unprintable_area.empty()) { extruder_unprintable_area.resize(extruder_nums, Polygons{Model::getBedPolygon()}); } - std::vector filament_map = print.get_filament_maps(); - if (print.has_wipe_tower() && !print.get_fake_wipe_tower().outer_wall.empty()) { ExPolygons expolyFromLines{}; for (auto polyline : print.get_fake_wipe_tower().outer_wall.begin()->second) { @@ -634,22 +687,39 @@ static ExPolygons outer_inner_brim_area(const Print& print, auto iter = std::find_if(objPrintVec.begin(), objPrintVec.end(), [object](const std::pair& item) { return item.first == object->id(); }); + std::vector object_physical_extruders; - if (iter != objPrintVec.end()) { - int extruder_id = filament_map[iter->second - 1] - 1; - auto bedPoly = extruder_unprintable_area[extruder_id]; - auto bedExPoly = diff_ex((offset(bedPoly, scale_(30.), jtRound, SCALED_RESOLUTION)), {bedPoly}); - if (!bedExPoly.empty()) { - extruder_no_brim_area.push_back(bedExPoly.front()); + if (iter != objPrintVec.end() && iter->second > 0) { + object_physical_extruders = brim_physical_extruders( + print, size_t(iter->second - 1), exclusion_regions_by_extruder.size()); + object_physical_extruders.insert( + object_physical_extruders.end(), + combined_brim_physical_extruders.begin(), combined_brim_physical_extruders.end()); + for (const size_t extruder_id : object_physical_extruders) { + if (extruder_id >= extruder_unprintable_area.size()) + continue; + const Polygons &bed_poly = extruder_unprintable_area[extruder_id]; + ExPolygons bed_exterior = diff_ex( + offset(bed_poly, scale_(30.), jtRound, SCALED_RESOLUTION), bed_poly); + expolygons_append(extruder_no_brim_area, std::move(bed_exterior)); } //extruder_no_brim_area = offset2_ex(extruder_no_brim_area, scaled_flow_width, -scaled_flow_width); // connect scattered small areas to prevent generating very small brims - } - for (auto& [key, areas] : brimAreaMap) - if (key.object_id == object->id()) - areas = diff_ex(areas, extruder_no_brim_area); - + if (object_physical_extruders.empty()) { + object_physical_extruders.resize(exclusion_regions_by_extruder.size()); + std::iota(object_physical_extruders.begin(), object_physical_extruders.end(), size_t(0)); + } + const ExPolygons exclusions = active_bed_exclusion_footprints( + exclusion_regions_by_extruder, object_physical_extruders, + 0.0, first_layer_top_z, Point(0, 0), exclusion_clearance); + for (auto& [key, areas] : brimAreaMap) { + if (key.object_id != object->id()) + continue; + areas = diff_ex(areas, extruder_no_brim_area); + if (!exclusions.empty()) + areas = diff_ex(areas, exclusions); + } } brim_area.clear(); diff --git a/src/libslic3r/CMakeLists.txt b/src/libslic3r/CMakeLists.txt index ce3fd8476c..dbf0d0469f 100644 --- a/src/libslic3r/CMakeLists.txt +++ b/src/libslic3r/CMakeLists.txt @@ -114,6 +114,8 @@ set(lisbslic3r_sources Emboss.hpp EmbossShape.hpp enum_bitmask.hpp + ExclusionVolumeGeometry.cpp + ExclusionVolumeGeometry.hpp Execution/Execution.hpp Execution/ExecutionSeq.hpp Execution/ExecutionTBB.hpp diff --git a/src/libslic3r/ExclusionVolumeGeometry.cpp b/src/libslic3r/ExclusionVolumeGeometry.cpp new file mode 100644 index 0000000000..32365b8d41 --- /dev/null +++ b/src/libslic3r/ExclusionVolumeGeometry.cpp @@ -0,0 +1,92 @@ +#include "ExclusionVolumeGeometry.hpp" + +#include "ClipperUtils.hpp" + +#include +#include + +namespace Slic3r { + +namespace { + +constexpr double Z_EPSILON_MM = 1e-6; + +void append_active_footprints( + Polygons &out, + const std::vector ®ions, + const double z_min, + const double z_max, + const Point &translation) +{ + for (const BedExcludeRegion ®ion : regions) { + if (region.polygon.points.size() < 3 || region.z_max < region.z_min || + !bed_exclusion_z_ranges_overlap(z_min, z_max, region.z_min, region.z_max)) + continue; + + Polygon footprint = region.polygon; + footprint.translate(translation); + out.emplace_back(std::move(footprint)); + } +} + +ExPolygons union_active_footprints(Polygons footprints, const coord_t clearance) +{ + if (footprints.empty()) + return {}; + + ExPolygons result = union_ex(footprints); + if (clearance > 0) + result = offset_ex(result, float(clearance)); + return result; +} + +} // namespace + +bool bed_exclusion_z_ranges_overlap( + double first_min, + double first_max, + double second_min, + double second_max) +{ + if (first_min > first_max) + std::swap(first_min, first_max); + if (second_min > second_max) + std::swap(second_min, second_max); + return first_max >= second_min - Z_EPSILON_MM && first_min <= second_max + Z_EPSILON_MM; +} + +ExPolygons active_bed_exclusion_footprints( + const std::vector ®ions, + const double z_min, + const double z_max, + const Point &translation, + const coord_t clearance) +{ + Polygons footprints; + footprints.reserve(regions.size()); + append_active_footprints(footprints, regions, z_min, z_max, translation); + return union_active_footprints(std::move(footprints), clearance); +} + +ExPolygons active_bed_exclusion_footprints( + const std::vector> ®ions_by_extruder, + const std::vector &physical_extruders, + const double z_min, + const double z_max, + const Point &translation, + const coord_t clearance) +{ + Polygons footprints; + std::vector visited(regions_by_extruder.size(), false); + for (const size_t extruder_id : physical_extruders) { + if (extruder_id >= regions_by_extruder.size() || visited[extruder_id]) + continue; + visited[extruder_id] = true; + const std::vector ®ions = regions_by_extruder[extruder_id]; + footprints.reserve(footprints.size() + regions.size()); + append_active_footprints(footprints, regions, z_min, z_max, translation); + } + return union_active_footprints(std::move(footprints), clearance); +} + +} // namespace Slic3r diff --git a/src/libslic3r/ExclusionVolumeGeometry.hpp b/src/libslic3r/ExclusionVolumeGeometry.hpp new file mode 100644 index 0000000000..b72868d76b --- /dev/null +++ b/src/libslic3r/ExclusionVolumeGeometry.hpp @@ -0,0 +1,33 @@ +#pragma once + +#include "ExPolygon.hpp" +#include "Point.hpp" +#include "PrintConfig.hpp" + +#include +#include + +namespace Slic3r { + +bool bed_exclusion_z_ranges_overlap(double first_min, double first_max, double second_min, double second_max); + +// Return the union of exclusion footprints intersecting the requested extrusion +// slab. Regions are already resolved into physical-nozzle/model coordinates by +// PrintConfig; callers only provide the physical nozzles that may emit the +// generated geometry and the translation into the caller's coordinate space. +ExPolygons active_bed_exclusion_footprints( + const std::vector ®ions, + double z_min, + double z_max, + const Point &translation = Point(0, 0), + coord_t clearance = 0); + +ExPolygons active_bed_exclusion_footprints( + const std::vector> ®ions_by_extruder, + const std::vector &physical_extruders, + double z_min, + double z_max, + const Point &translation = Point(0, 0), + coord_t clearance = 0); + +} // namespace Slic3r diff --git a/src/libslic3r/GCode/ExclusionVolumeTravelAvoidance.cpp b/src/libslic3r/GCode/ExclusionVolumeTravelAvoidance.cpp index 3a7f308f49..5fb7ca6b8b 100644 --- a/src/libslic3r/GCode/ExclusionVolumeTravelAvoidance.cpp +++ b/src/libslic3r/GCode/ExclusionVolumeTravelAvoidance.cpp @@ -1,6 +1,7 @@ #include "ExclusionVolumeTravelAvoidance.hpp" #include "libslic3r/ClipperUtils.hpp" +#include "libslic3r/ExclusionVolumeGeometry.hpp" #include "libslic3r/Line.hpp" #include "libslic3r/Polygon.hpp" @@ -13,7 +14,6 @@ namespace Slic3r { namespace { -constexpr double Z_EPSILON_MM = 1e-6; constexpr double T_EPSILON = 1e-9; constexpr size_t MAX_REROUTE_ITERATIONS = 16; @@ -29,11 +29,6 @@ double boundary_epsilon() return std::max(SCALED_EPSILON, scale_(0.005)); } -bool z_ranges_overlap(double a_min, double a_max, double b_min, double b_max) -{ - return a_max >= b_min - Z_EPSILON_MM && a_min <= b_max + Z_EPSILON_MM; -} - bool line_intersection_t( const Point &a, const Point &b, @@ -462,17 +457,11 @@ ExclusionVolumeTravelAvoidance::active_obstacles( double z_min, double z_max) const { - Polygons active_polygons; - active_polygons.reserve(space.regions.size()); - for (const BedExcludeRegion ®ion : space.regions) - if (z_ranges_overlap(z_min, z_max, region.z_min, region.z_max)) - active_polygons.emplace_back(region.polygon); - - if (active_polygons.empty()) - return std::nullopt; - ActiveObstacles active; - active.obstacles = union_ex(expand(active_polygons, float(m_clearance))); + active.obstacles = active_bed_exclusion_footprints( + space.regions, z_min, z_max, Point(0, 0), m_clearance); + if (active.obstacles.empty()) + return std::nullopt; if (space.bed_shape.points.size() >= 3) { active.valid_bed = offset_ex(space.bed_shape, -float(m_clearance)); if (active.valid_bed.empty())