diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index 501b5d0264..01457d9344 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -1028,11 +1028,21 @@ static std::vector get_path_of_change_filament(const Print& print) double current_z = gcodegen.writer().get_position().z(); if (z == -1.) // in case no specific z was provided, print at current_z pos z = current_z; - if (!is_approx(z, current_z)) { + // Orca: wipe_tower_no_sparse_layers crash guard. With sparse layers skipped the tower is + // compacted far below the object, so descending to it is only safe once the nozzle is parked + // over the tower - which is what the is_finish_first travel above does. Otherwise the nozzle + // is still over the model and this descent would drive it into the print, so defer it to the + // re-descents below, which run after the travel to the tower. + const bool defer_compacted_descend = m_sparse_layers_skipped + && !tcr.priming && !tcr.is_finish_first && (current_z - z) > EPSILON; + if (!is_approx(z, current_z) && !defer_compacted_descend) { gcode += gcodegen.writer().retract(); gcode += gcodegen.writer().travel_to_z(z, "Travel down to the last wipe tower layer."); gcode += gcodegen.writer().unretract(); } + // Tower compacted below the object, so any extrusion emitted without an explicit z has to be + // pulled back down to it first. + const bool compacted_below_object = m_sparse_layers_skipped && z >= 0. && (tcr.print_z - z) > EPSILON; // Process the end filament gcode. bool add_change_filament_624 = false; @@ -1085,11 +1095,23 @@ static std::vector get_path_of_change_filament(const Print& print) std::string nozzle_change_gcode_trans; if (is_nozzle_change) { // move to start_pos before nozzle change + // Orca: travel_to() lifts to the object layer height to clear the print. That lift is + // needed when arriving from the model, but is a wasted full-height Z bounce when the + // nozzle already sits on the compacted tower, so travel at the compacted z instead. + const bool compact_intower_nc_travel = compacted_below_object + && (tcr.print_z - gcodegen.writer().get_position().z()) > EPSILON; std::string start_pos_str; start_pos_str = gcodegen.travel_to(wipe_tower_point_to_object_point(gcodegen, transform_wt_pt(tcr.nozzle_change_result.start_pos) + plate_origin_2d), erMixed, - "Move to nozzle change start pos"); + "Move to nozzle change start pos", compact_intower_nc_travel ? z : DBL_MAX); check_add_eol(start_pos_str); nozzle_change_gcode_trans += start_pos_str; + // The nozzle-change wipe below carries no explicit z, so it would extrude at the object + // layer height and float above the compacted tower. Descend unless the travel stayed down. + if (!compact_intower_nc_travel && compacted_below_object) { + std::string nc_z_descend = gcodegen.writer().travel_to_z(z, "Descend to compacted wipe tower z (no sparse layers)"); + check_add_eol(nc_z_descend); + nozzle_change_gcode_trans += nc_z_descend; + } nozzle_change_gcode_trans += gcodegen.unretract(); nozzle_change_gcode_trans += transform_gcode(tcr.nozzle_change_result.gcode, tcr.nozzle_change_result.start_pos, wipe_tower_offset, wipe_tower_rotation); gcodegen.set_last_pos(wipe_tower_point_to_object_point(gcodegen, transform_wt_pt(tcr.nozzle_change_result.end_pos) + plate_origin_2d)); @@ -1428,6 +1450,15 @@ static std::vector get_path_of_change_filament(const Print& print) start_filament_gcode_str = start_filament_gcode_str + wipe_next_start_point_str + toolchange_unretract_str; + // Orca: the custom change_filament_gcode lifts to the object layer height and the unretract + // de-hops back to it, so every tower extrusion emitted after it (purge moves, and the wall + // when it prints after the toolchange) would float above the compacted tower. Descend first. + if (compacted_below_object) { + std::string z_descend = gcodegen.writer().travel_to_z(z, "Descend to compacted wipe tower z (no sparse layers)"); + check_add_eol(z_descend); + start_filament_gcode_str += z_descend; + } + // Insert the end filament, toolchange, and start filament gcode into the generated gcode. DynamicConfig config; config.set_key_value("filament_end_gcode", new ConfigOptionString(end_filament_gcode_str)); @@ -1915,11 +1946,9 @@ static std::vector get_path_of_change_filament(const Print& print) // resulting in a wipe tower with sparse layers. double wipe_tower_z = -1; bool ignore_sparse = false; - if (gcodegen.config().wipe_tower_no_sparse_layers.value) { + if (m_sparse_layers_skipped) { wipe_tower_z = m_last_wipe_tower_print_z; - ignore_sparse = (m_tool_changes[m_layer_idx].size() == 1 && - m_tool_changes[m_layer_idx].front().initial_tool == m_tool_changes[m_layer_idx].front().new_tool && - m_layer_idx != 0); + ignore_sparse = wipe_tower_layer_is_sparse(m_tool_changes[m_layer_idx]) && m_layer_idx != 0; if (m_tool_change_idx == 0 && !ignore_sparse) wipe_tower_z = m_last_wipe_tower_print_z + m_tool_changes[m_layer_idx].front().layer_height; } @@ -1935,12 +1964,9 @@ static std::vector get_path_of_change_filament(const Print& print) // resulting in a wipe tower with sparse layers. double wipe_tower_z = -1; bool ignore_sparse = false; - if (gcodegen.config().wipe_tower_no_sparse_layers.value) { - wipe_tower_z = m_last_wipe_tower_print_z; - ignore_sparse = (m_tool_changes[m_layer_idx].size() == 1 && - m_tool_changes[m_layer_idx].front().initial_tool == m_tool_changes[m_layer_idx].front().new_tool); - if (m_tool_change_idx == 0 && !ignore_sparse) - wipe_tower_z = m_last_wipe_tower_print_z + m_tool_changes[m_layer_idx].front().layer_height; + if (m_sparse_layers_skipped) { + ignore_sparse = wipe_tower_layer_is_sparse(m_tool_changes[m_layer_idx]); + wipe_tower_z = m_compacted_tower_z[m_layer_idx]; } if ((m_enable_timelapse_print || m_enable_wrapping_detection) && m_is_first_print) { @@ -1953,10 +1979,8 @@ static std::vector get_path_of_change_filament(const Print& print) if (!(size_t(m_tool_change_idx) < m_tool_changes[m_layer_idx].size())) throw Slic3r::RuntimeError("Wipe tower generation failed, possibly due to empty first layer."); - if (!ignore_sparse) { + if (!ignore_sparse) gcode += append_tcr(gcodegen, m_tool_changes[m_layer_idx][m_tool_change_idx++], extruder_id, wipe_tower_z); - m_last_wipe_tower_print_z = wipe_tower_z; - } } } @@ -1970,9 +1994,8 @@ static std::vector get_path_of_change_filament(const Print& print) return true; bool ignore_sparse = false; - if (gcodegen.config().wipe_tower_no_sparse_layers.value) { - ignore_sparse = (m_tool_changes[m_layer_idx].size() == 1 && m_tool_changes[m_layer_idx].front().initial_tool == m_tool_changes[m_layer_idx].front().new_tool); - } + if (m_sparse_layers_skipped) + ignore_sparse = wipe_tower_layer_is_sparse(m_tool_changes[m_layer_idx]); if ((m_enable_timelapse_print || m_enable_wrapping_detection) && m_is_first_print) { return false; diff --git a/src/libslic3r/GCode.hpp b/src/libslic3r/GCode.hpp index 29e4638a94..3933fd4e56 100644 --- a/src/libslic3r/GCode.hpp +++ b/src/libslic3r/GCode.hpp @@ -106,8 +106,13 @@ public: m_enable_wrapping_detection(print_config.enable_wrapping_detection && (print_config.wrapping_exclude_area.values.size() > 2) && (slice_used_filaments.size() <= 1)), m_is_first_print(true), m_print_config(&print_config), - m_last_wipe_tower_print_z(print_config.z_offset.value) + m_last_wipe_tower_print_z(print_config.z_offset.value), + m_sparse_layers_skipped(wipe_tower_sparse_layers_skipped(print_config)) { + // Precomputed rather than accumulated while emitting, so that the clearance validator and + // the emitter cannot disagree about where the compacted tower sits on any given layer. + if (m_sparse_layers_skipped) + m_compacted_tower_z = compute_compacted_wipe_tower_z(tool_changes, float(print_config.z_offset.value)); // initialize with the extruder offset of master extruder id m_extruder_offsets.resize(print_config.filament_map.size(), print_config.extruder_offset.get_at(print_config.master_extruder_id.value - 1)); const auto& filament_map = print_config.filament_map.values; // 1 based idx @@ -167,6 +172,11 @@ private: float m_wipe_tower_depth; BoundingBoxf m_wipe_tower_bbx; Vec2f m_rib_offset{Vec2f(0, 0)}; + // wipe_tower_no_sparse_layers, as answered by the shared compaction rule rather than by the raw + // option: smooth timelapse and wrapping detection keep a tower on every layer regardless. + const bool m_sparse_layers_skipped; + // Print z of the compacted tower per planned layer. Empty when the tower is not compacted. + std::vector m_compacted_tower_z; }; class ColorPrintColors diff --git a/src/libslic3r/GCode/WipeTower.cpp b/src/libslic3r/GCode/WipeTower.cpp index e80433f4ae..eaf8a2eaf3 100644 --- a/src/libslic3r/GCode/WipeTower.cpp +++ b/src/libslic3r/GCode/WipeTower.cpp @@ -25,6 +25,30 @@ static constexpr int arc_fit_size = 20; enum class LimitFlow { None, LimitPrintFlow, LimitRammingFlow, LimitRammingFlowNC};//nc:nozzle change static const std::map nozzle_diameter_to_nozzle_change_width{{0.2f, 0.5f}, {0.4f, 1.0f}, {0.6f, 1.2f}, {0.8f, 1.4f}}; +bool wipe_tower_sparse_layers_skipped(const PrintConfig &config) +{ + return config.wipe_tower_no_sparse_layers.value && config.timelapse_type.value != TimelapseType::tlSmooth && + ! config.enable_wrapping_detection.value; +} + +bool wipe_tower_layer_is_sparse(const std::vector &layer_tool_changes) +{ + return layer_tool_changes.size() == 1 && layer_tool_changes.front().initial_tool == layer_tool_changes.front().new_tool; +} + +std::vector compute_compacted_wipe_tower_z(const std::vector> &tool_changes, + float base_z) +{ + std::vector tower_z(tool_changes.size(), base_z); + float last = base_z; + for (size_t i = 0; i < tool_changes.size(); ++i) { + if (! tool_changes[i].empty() && ! wipe_tower_layer_is_sparse(tool_changes[i])) + last += tool_changes[i].front().layer_height; + tower_z[i] = last; + } + return tower_z; +} + inline float align_round(float value, float base) { return std::round(value / base) * base; @@ -1879,7 +1903,7 @@ WipeTower::WipeTower(const PrintConfig& config, int plate_idx, Vec3d plate_origi m_z_pos(0.f), //m_bridging(float(config.wipe_tower_bridging)), m_bridging(10.f), - m_no_sparse_layers(config.wipe_tower_no_sparse_layers), + m_sparse_layers_skipped(wipe_tower_sparse_layers_skipped(config)), m_gcode_flavor(config.gcode_flavor), m_travel_speed(config.travel_speed.get_at(get_extruder_index(config, (unsigned int)initial_tool))), m_current_tool(initial_tool), @@ -2977,7 +3001,7 @@ WipeTower::ToolChangeResult WipeTower::finish_layer(bool extrude_perimeter, bool // Ask our writer about how much material was consumed. // Skip this in case the layer is sparse and config option to not print sparse layers is enabled. - if (! m_no_sparse_layers || toolchanges_on_layer) + if (! m_sparse_layers_skipped || toolchanges_on_layer) if (m_current_tool < m_used_filament_length.size()) m_used_filament_length[m_current_tool] += writer.get_and_reset_used_filament_length(); @@ -3021,7 +3045,7 @@ void WipeTower::plan_toolchange(float z_par, float layer_height_par, unsigned in if (m_plan.empty() || m_plan.back().z + WT_EPSILON < z_par) // if we moved to a new layer, we'll add it to m_plan first m_plan.push_back(WipeTowerInfo(z_par, layer_height_par)); - if (m_first_layer_idx == size_t(-1) && (! m_no_sparse_layers || old_tool != new_tool)) + if (m_first_layer_idx == size_t(-1) && (! m_sparse_layers_skipped || old_tool != new_tool)) m_first_layer_idx = m_plan.size() - 1; if (old_tool == new_tool) // new layer without toolchanges - we are done @@ -3874,7 +3898,7 @@ WipeTower::ToolChangeResult WipeTower::finish_layer_new(bool extrude_perimeter, // Ask our writer about how much material was consumed. // Skip this in case the layer is sparse and config option to not print sparse layers is enabled. - if (!m_no_sparse_layers || toolchanges_on_layer) + if (!m_sparse_layers_skipped || toolchanges_on_layer) if (m_current_tool < m_used_filament_length.size()) m_used_filament_length[m_current_tool] += writer.get_and_reset_used_filament_length(); @@ -3984,7 +4008,7 @@ WipeTower::ToolChangeResult WipeTower::finish_block(const WipeTowerBlock &block, // Ask our writer about how much material was consumed. // Skip this in case the layer is sparse and config option to not print sparse layers is enabled. - if (!m_no_sparse_layers || toolchanges_on_layer) + if (!m_sparse_layers_skipped || toolchanges_on_layer) if (filament_id < m_used_filament_length.size()) m_used_filament_length[filament_id] += writer.get_and_reset_used_filament_length(); @@ -4101,7 +4125,7 @@ WipeTower::ToolChangeResult WipeTower::finish_block_solid(const WipeTowerBlock & // Ask our writer about how much material was consumed. // Skip this in case the layer is sparse and config option to not print sparse layers is enabled. - if (!m_no_sparse_layers || toolchanges_on_layer) + if (!m_sparse_layers_skipped || toolchanges_on_layer) if (filament_id < m_used_filament_length.size()) m_used_filament_length[filament_id] += writer.get_and_reset_used_filament_length(); @@ -5155,7 +5179,7 @@ WipeTower::ToolChangeResult WipeTower::only_generate_out_wall(bool is_new_mode) // Ask our writer about how much material was consumed. // Skip this in case the layer is sparse and config option to not print sparse layers is enabled. - if (!m_no_sparse_layers || toolchanges_on_layer) + if (!m_sparse_layers_skipped || toolchanges_on_layer) if (m_current_tool < m_used_filament_length.size()) m_used_filament_length[m_current_tool] += writer.get_and_reset_used_filament_length(); return construct_tcr(writer, false, old_tool, true, false, 0.f, false); diff --git a/src/libslic3r/GCode/WipeTower.hpp b/src/libslic3r/GCode/WipeTower.hpp index 9303f09691..5426c9bd85 100644 --- a/src/libslic3r/GCode/WipeTower.hpp +++ b/src/libslic3r/GCode/WipeTower.hpp @@ -521,7 +521,7 @@ private: //float m_parking_pos_retraction = 0.f; //float m_extra_loading_move = 0.f; float m_bridging = 0.f; - bool m_no_sparse_layers = false; + bool m_sparse_layers_skipped = false; // BBS: remove useless config //bool m_set_extruder_trimpot = false; bool m_adhesion = true; @@ -680,6 +680,24 @@ private: }; +// Compaction rule for wipe_tower_no_sparse_layers. Shared by the G-code emitter and by the +// clearance validator so that both agree on where the compacted tower actually sits; a drift +// between the two would either let a real nozzle collision through or reject a safe plate. + +// Whether sparse layers are really skipped, i.e. whether the tower is compacted at all. Smooth +// timelapse and wrapping detection put a tower on every layer, so no layer is ever dropped and the +// tower keeps following the object even though the option is on. Tower planning, G-code emission and +// the clearance validator all ask this single question, so none of them can compact on its own. +bool wipe_tower_sparse_layers_skipped(const PrintConfig &config); + +// A planned layer prints no tower at all when its only toolchange keeps the same filament. +bool wipe_tower_layer_is_sparse(const std::vector &layer_tool_changes); + +// Print z the compacted tower reaches on every planned layer. Sparse layers carry over the +// previous value, so the tower falls one layer height behind the object for each of them. base_z is +// the z the tower starts from, which Orca offsets by z_offset. +std::vector compute_compacted_wipe_tower_z(const std::vector> &tool_changes, + float base_z = 0.f); } // namespace Slic3r diff --git a/src/libslic3r/GCode/WipeTower2.cpp b/src/libslic3r/GCode/WipeTower2.cpp index 4e752bd772..0080094270 100644 --- a/src/libslic3r/GCode/WipeTower2.cpp +++ b/src/libslic3r/GCode/WipeTower2.cpp @@ -1032,7 +1032,7 @@ WipeTower2::WipeTower2(const PrintConfig& config, const PrintRegionConfig& defau m_y_shift(0.f), m_z_pos(0.f), m_bridging(float(config.wipe_tower_bridging)), - m_no_sparse_layers(config.wipe_tower_no_sparse_layers), + m_sparse_layers_skipped(wipe_tower_sparse_layers_skipped(config)), m_gcode_flavor(config.gcode_flavor), m_travel_speed(config.travel_speed.get_at(get_extruder_index(config, (unsigned int)initial_tool))), m_infill_speed(default_region_config.sparse_infill_speed.get_at(get_extruder_index(config, (unsigned int)initial_tool))), @@ -1730,7 +1730,7 @@ void WipeTower2::toolchange_Change( } else if (m_wall_type == (int)wtwCone) { const double support_scale = get_wipe_tower_cone_base(m_wipe_tower_width, m_wipe_tower_height, m_wipe_tower_depth, m_wipe_tower_cone_angle).second; - const double z = m_no_sparse_layers ? (m_current_height + m_layer_info->height) : m_layer_info->z; + const double z = m_sparse_layers_skipped ? (m_current_height + m_layer_info->height) : m_layer_info->z; const double r = std::tan(Geometry::deg2rad(m_wipe_tower_cone_angle / 2.f)) * (m_wipe_tower_height - z); const double w = m_layer_info->depth + m_perimeter_width; if (r > 0.5 * w + 0.01) { // same guard as generate_support_cone_wall @@ -1872,7 +1872,7 @@ void WipeTower2::toolchange_Wipe( // All the calculations in all other places take the spacing into account for all the layers. // If spare layers are excluded->if 1 or less toolchange has been done, it must be sill the first layer, too.So slow down. - const float target_speed = is_first_layer() || (m_num_tool_changes <= 1 && m_no_sparse_layers) ? m_first_layer_speed * 60.f : std::min(m_wipe_tower_max_purge_speed * 60.f, m_infill_speed * 60.f); + const float target_speed = is_first_layer() || (m_num_tool_changes <= 1 && m_sparse_layers_skipped) ? m_first_layer_speed * 60.f : std::min(m_wipe_tower_max_purge_speed * 60.f, m_infill_speed * 60.f); float wipe_speed = 0.33f * target_speed; // if there is less than 2.5*line_width to the edge, advance straightaway (there is likely a blob anyway) @@ -1970,7 +1970,7 @@ WipeTower::ToolChangeResult WipeTower2::finish_layer() // Slow down on the 1st layer. // If spare layers are excluded -> if 1 or less toolchange has been done, it must be still the first layer, too. So slow down. - bool first_layer = is_first_layer() || (m_num_tool_changes <= 1 && m_no_sparse_layers); + bool first_layer = is_first_layer() || (m_num_tool_changes <= 1 && m_sparse_layers_skipped); float feedrate = first_layer ? m_first_layer_speed * 60.f : std::min(m_wipe_tower_max_purge_speed * 60.f, m_infill_speed * 60.f); if (m_enable_tower_interface_features && m_prev_layer_had_interface) feedrate = std::min(feedrate, 20.f * 60.f); @@ -2103,7 +2103,7 @@ WipeTower::ToolChangeResult WipeTower2::finish_layer() // Ask our writer about how much material was consumed. // Skip this in case the layer is sparse and config option to not print sparse layers is enabled. - if (! m_no_sparse_layers || toolchanges_on_layer || first_layer) { + if (! m_sparse_layers_skipped || toolchanges_on_layer || first_layer) { if (m_current_tool < m_used_filament_length.size()) m_used_filament_length[m_current_tool] += writer.get_and_reset_used_filament_length(); m_current_height += m_layer_info->height; @@ -2226,7 +2226,7 @@ void WipeTower2::plan_toolchange(float z_par, float layer_height_par, unsigned i if (m_plan.empty() || m_plan.back().z + WT_EPSILON < z_par) // if we moved to a new layer, we'll add it to m_plan first m_plan.push_back(WipeTowerInfo(z_par, layer_height_par)); - if (m_first_layer_idx == size_t(-1) && (! m_no_sparse_layers || old_tool != new_tool || m_plan.size() == 1)) + if (m_first_layer_idx == size_t(-1) && (! m_sparse_layers_skipped || old_tool != new_tool || m_plan.size() == 1)) m_first_layer_idx = m_plan.size() - 1; if (old_tool == new_tool) // new layer without toolchanges - we are done @@ -2652,7 +2652,7 @@ Polygon WipeTower2::generate_support_cone_wall( const auto [R, support_scale] = get_wipe_tower_cone_base(m_wipe_tower_width, m_wipe_tower_height, m_wipe_tower_depth, m_wipe_tower_cone_angle); - double z = m_no_sparse_layers ? + double z = m_sparse_layers_skipped ? (m_current_height + m_layer_info->height) : m_layer_info->z; // the former should actually work in both cases, but let's stay on the safe side (the 2.6.0 is close) diff --git a/src/libslic3r/GCode/WipeTower2.hpp b/src/libslic3r/GCode/WipeTower2.hpp index 232cad1a6b..eb3a2562fb 100644 --- a/src/libslic3r/GCode/WipeTower2.hpp +++ b/src/libslic3r/GCode/WipeTower2.hpp @@ -267,7 +267,7 @@ private: float m_parking_pos_retraction = 0.f; float m_extra_loading_move = 0.f; float m_bridging = 0.f; - bool m_no_sparse_layers = false; + bool m_sparse_layers_skipped = false; bool m_set_extruder_trimpot = false; bool m_adhesion = true; GCodeFlavor m_gcode_flavor; diff --git a/src/libslic3r/Preset.cpp b/src/libslic3r/Preset.cpp index deae560d7c..b2b5d9277b 100644 --- a/src/libslic3r/Preset.cpp +++ b/src/libslic3r/Preset.cpp @@ -1447,7 +1447,7 @@ static std::vector s_Preset_printer_options { "gcode_skip_config_block", "fan_kickstart", "part_cooling_fan_min_pwm", "fan_speedup_time", "fan_speedup_overhangs", "single_extruder_multi_material", "manual_filament_change", "file_start_gcode", "machine_start_gcode", "machine_end_gcode", "before_layer_change_gcode", "printing_by_object_gcode", "layer_change_gcode", "time_lapse_gcode", "wrapping_detection_gcode", "change_filament_gcode", "change_extrusion_role_gcode", "printer_model", "printer_variant", "printer_extruder_id", "printer_extruder_variant", "extruder_variant_list", "default_nozzle_volume_type", - "printable_height", "extruder_printable_height", "extruder_clearance_radius", "extruder_clearance_height_to_lid", "extruder_clearance_height_to_rod", + "printable_height", "extruder_printable_height", "extruder_clearance_radius", "extruder_clearance_height_to_lid", "extruder_clearance_height_to_rod", "extruder_clearance_dist_to_rod", "nozzle_height", "master_extruder_id", "default_print_profile", "inherits", "silent_mode", diff --git a/src/libslic3r/Print.cpp b/src/libslic3r/Print.cpp index 13fe7758ff..833b811535 100644 --- a/src/libslic3r/Print.cpp +++ b/src/libslic3r/Print.cpp @@ -966,6 +966,377 @@ StringObjectException Print::sequential_print_clearance_valid(const Print &print return single_object_exception; } +// --------------------------------------------------------------------------------------------- +// Clearance rule for a prime tower compacted by wipe_tower_no_sparse_layers. +// Ported from BambuStudio and adapted to Orca's printer config: Orca has no +// prime_tower_lift_height (z_hop alone bounds the spiral), spells the toolhead radius +// extruder_clearance_radius, and derives the spiral slope from the per-filament travel_slope instead +// of one global constant. +// --------------------------------------------------------------------------------------------- + +double compacted_tower_footprint_padding(const PrintConfig &config, double brim_width) +{ + // The brim is deposited material like any other and reaches past the wall on the first layer, so + // the sweeping rod has to clear it too. + // + // On top of it, two effects make a nominal outline fall short of the printed tower on its low + // corner even though it overshoots by millimetres on the high one: WipeTower re-centres the tower + // by rib_offset once its first-layer wall is known, and the precise check hulls extrusion centre + // lines, so the deposited material reaches half a line width further still. Allowing a line width + // per side covers both, which is what keeps an estimated footprint enclosing the real one and the + // pre-slice check stricter than the precise one. + return std::max(0., brim_width) + 2. * config.nozzle_diameter.get_at(0); +} + +Polygons compacted_wipe_tower_rings(const CompactedTowerZone &zone, bool any_body_tier) +{ + Polygons rings = zone.grown_nozzle; + if (any_body_tier) + append(rings, zone.grown_body); + return rings; +} + +CompactedTowerZone compacted_wipe_tower_zone(const PrintConfig &config, const Polygon &tower_footprint) +{ + CompactedTowerZone zone; + if (tower_footprint.points.empty()) + return zone; + + // Spiral Z-hop at wipe-tower entry (the G3 Z I J that GCodeWriter emits for a SpiralLift) starts on + // the tower outline at a low Z. The spiral centre sits one radius away from the start point, so the + // circle reaches 2 * radius beyond the outline. radius = lift / (2*pi*atan(travel_slope)) is the + // same formula GCodeWriter uses; both are per filament, so take the widest any filament can make. + double spiral_reach = 0.; + for (size_t i = 0; i < config.z_hop.size(); ++i) { + const double lift = std::min(double(config.z_hop.get_at(i)), 5.); + if (lift < EPSILON) + continue; + const double slope = i < config.travel_slope.size() ? double(config.travel_slope.get_at(i)) : 0.; + if (slope < EPSILON) + continue; + spiral_reach = std::max(spiral_reach, 2. * lift / (2. * PI * std::atan(slope))); + } + + // Working footprint = outline grown by the spiral envelope. All later clearance tests use this, so + // a travel that leaves the deposited wall at low Z is still treated as part of the tower. + zone.hull = tower_footprint; + if (spiral_reach > EPSILON) { + const Polygons grown = offset(tower_footprint, float(scale_(spiral_reach)), jtRound, scale_(0.1)); + if (! grown.empty()) + zone.hull = Geometry::convex_hull(grown); + } + + // The rod sweeps the whole X axis, so its keep-out band is the tower's Y span widened by half + // the nozzle-to-rod offset per side (the instance carries the other half). Orca's sequential + // check has no such margin, having had no option to read it from until now. + zone.bbox_rod = zone.hull.bounding_box(); + zone.bbox_rod.offset(scale_(config.extruder_clearance_dist_to_rod.value * 0.5)); + + // Horizontal clearance, mirroring the sequential print check down to how the distance is split: + // there each of the two object hulls grows by half of extruder_clearance_radius, so the two + // outlines touch exactly when the objects are the full radius apart. Splitting it the same way + // here (half on the tower, half on the instance in compacted_wipe_tower_clearance) states the + // same criterion, and it is what lets the plater draw both outlines: they meet at the instant the + // check trips, instead of one of them being already buried inside the other. The smaller + // MAX_OUTER_NOZZLE_DIAMETER tier is the bare nozzle cone, the only part narrow enough to sit + // beside an object rising less than nozzle_height. The 0.2 mm shaved off is the same rounding + // slack the sequential check applies, 0.1 mm per side. Both rings are built here; which one a + // given object is measured against depends on its own height and is decided in + // compacted_wipe_tower_clearance(). + zone.body_radius = config.extruder_clearance_radius.value; + zone.grown_body = offset(zone.hull, float(scale_(compacted_tower_half_clearance(zone.body_radius))), jtRound, scale_(0.1)); + zone.grown_nozzle = offset(zone.hull, float(scale_(compacted_tower_half_clearance(MAX_OUTER_NOZZLE_DIAMETER))), jtRound, scale_(0.1)); + return zone; +} + +CompactedTowerClearance compacted_wipe_tower_clearance(const PrintConfig &config, const CompactedTowerZone &zone, + const Polygon &inst_hull, double object_rise) +{ + BoundingBox inst_bbox = inst_hull.bounding_box(); + inst_bbox.offset(scale_(config.extruder_clearance_dist_to_rod.value * 0.5)); + + // Only the Y span matters for the rod: it spans the whole X axis, so an object sharing the tower's + // Y band passes under it however far apart the two are in X. + const bool overlaps_in_y = std::min(inst_bbox.max.y(), zone.bbox_rod.max.y()) - std::max(inst_bbox.min.y(), zone.bbox_rod.min.y()) > 0; + + CompactedTowerClearance result; + result.far_clearance = overlaps_in_y ? config.extruder_clearance_height_to_rod.value : config.extruder_clearance_height_to_lid.value; + + // The rod and the lid are the only obstacles once the object stands far enough away. Closer than + // the toolhead radius it is the head body itself that hits the object, and it does so as soon as + // the object rises past the nozzle cone, which is far below the rod. + // The instance carries the other half of each clearance, the tower rings already hold the first + // half; see compacted_wipe_tower_zone(). Both halves are needed for the verdict to mean + // "a full radius apart", and drawing what is tested is what keeps the plater honest. + // + // Which tier applies is a property of this object alone: the head body sits above the nozzle cone, + // so it cannot reach an object that stays below nozzle_height however close it stands, and however + // tall the rest of the plate is. + const bool object_is_short = object_rise <= double(config.nozzle_height.value) + EPSILON; + result.body_clearance = object_is_short ? double(MAX_OUTER_NOZZLE_DIAMETER) : zone.body_radius; + + const Polygons inst_near_nozzle = offset(inst_hull, float(scale_(compacted_tower_half_clearance(MAX_OUTER_NOZZLE_DIAMETER))), jtRound, scale_(0.1)); + const bool near_nozzle = ! intersection(zone.grown_nozzle, inst_near_nozzle).empty(); + result.near_body = false; + if (! object_is_short) { + const Polygons inst_near_body = offset(inst_hull, float(scale_(compacted_tower_half_clearance(zone.body_radius))), jtRound, scale_(0.1)); + result.near_body = ! intersection(zone.grown_body, inst_near_body).empty(); + } + + result.allowed_rise = result.far_clearance; + if (near_nozzle) + result.allowed_rise = 0.; + else if (result.near_body) + result.allowed_rise = std::min(result.far_clearance, double(config.nozzle_height.value)); + return result; +} + +Polygon compacted_wipe_tower_offender_outline(const Polygon &inst_hull, double body_clearance) +{ + // Exactly the half-clearance the check grew this instance by, so the halo drawn around an object is + // the very outline that was tested against the tower ring of the same tier. Passing the clearance + // the object was actually judged on keeps a short object from being drawn with the wide ring it is + // not subject to. + const Polygons grown = offset(inst_hull, float(scale_(compacted_tower_half_clearance(body_clearance))), jtRound, scale_(0.1)); + return grown.empty() ? inst_hull : grown.front(); +} + +// Shared user-facing message for every compacted-tower clearance failure. Height-limit and too-close +// are the same class of layout violation under "No sparse layers", so they share one wording. +static std::string compacted_wipe_tower_clearance_error() +{ + return L("The relative position of the model and the prime tower does not meet the requirements of the \"No sparse layers\" feature. Please adjust their relative positions, lower the model height, or turn off \"No sparse layers\"."); +} + +// Convex hull of one print instance in bed coordinates, the same outline both compacted tower checks +// compare against the tower. +static Polygon compacted_tower_print_instance_hull(const PrintObject &object, const PrintInstance &instance) +{ + Points pts; + for (const ModelVolume *v : object.model_object()->volumes) { + if (! v->is_model_part()) + continue; + Polygon hull = v->get_convex_hull_2d(Geometry::assemble_transform(Vec3d::Zero(), instance.model_instance->get_rotation(), + instance.model_instance->get_scaling_factor(), instance.model_instance->get_mirror())); + hull.translate(instance.shift - object.center_offset()); + append(pts, hull.points); + } + return pts.empty() ? Polygon() : Geometry::convex_hull(pts); +} + +// Footprint the compacted prime tower is expected to occupy on the plate, in bed coordinates. +// Before psWipeTower has run there is no tower geometry at all, so this falls back to the same +// estimate the plater builds its preview box from. Answering while the user is still arranging the +// plate is the whole point of the pre-slice check, and an estimate is all that can be had then. +static Polygon estimated_wipe_tower_footprint(const Print &print) +{ + const PrintConfig &config = print.config(); + const size_t filaments_cnt = print.extruders().size(); + if (filaments_cnt == 0) + return Polygon(); + + const WipeTowerData &wtd = print.wipe_tower_data(filaments_cnt); + + double width, depth, brim; + Vec2d local_min; + if (wtd.bbx.size().x() > EPSILON && wtd.bbx.size().y() > EPSILON) { + // The tower has already been generated once, so use its real box (brim included) instead of + // re-estimating. Same frame first_layer_wipe_tower_corners() works in. + width = wtd.bbx.size().x(); + depth = wtd.bbx.size().y(); + local_min = wtd.bbx.min + wtd.rib_offset.cast(); + brim = 0.; + } else { + depth = wtd.depth; + if (depth < EPSILON) + return Polygon(); + // PartPlate::estimate_wipe_tower_size() squares the rib tower off and the preview box the user + // drags around is built from that, so match it here rather than keeping the nominal width. + width = config.wipe_tower_wall_type.value == WipeTowerWallType::wtwRib ? depth : double(config.prime_tower_width.value); + local_min = Vec2d::Zero(); + brim = double(wtd.brim_width); + } + + const double padding = compacted_tower_footprint_padding(config, brim); + local_min -= Vec2d(padding, padding); + width += 2. * padding; + depth += 2. * padding; + + const Eigen::Rotation2Dd rot(Geometry::deg2rad(config.wipe_tower_rotation_angle.value)); + const Vec2d translate(config.wipe_tower_x.get_at(print.get_plate_index()) + print.get_plate_origin()(0), + config.wipe_tower_y.get_at(print.get_plate_index()) + print.get_plate_origin()(1)); + + Polygon footprint; + for (const Vec2d &corner : { local_min, + Vec2d(local_min.x() + width, local_min.y()), + Vec2d(local_min.x() + width, local_min.y() + depth), + Vec2d(local_min.x(), local_min.y() + depth) }) { + const Vec2d p = rot * corner + translate; + footprint.points.emplace_back(scale_(p.x()), scale_(p.y())); + } + return footprint; +} + +// Pre-slice counterpart of validate_compacted_wipe_tower_clearance(). It applies the very same +// clearance rule, but to an estimated tower footprint instead of the real tool-change extrusions, +// which is what lets it run from Print::validate() before anything has been sliced. Reporting through +// polygons / height_polygons rather than by throwing is what puts the collision area and the height +// limit plane on the plater, exactly the way sequential printing does it. +StringObjectException Print::compacted_wipe_tower_clearance_valid(const Print &print, Polygons *polygons, std::vector> *height_polygons) +{ + const PrintConfig &config = print.config(); + if (! wipe_tower_sparse_layers_skipped(config) || config.print_sequence != PrintSequence::ByLayer || ! print.has_wipe_tower()) + return {}; + + const CompactedTowerZone zone = compacted_wipe_tower_zone(config, estimated_wipe_tower_footprint(print)); + if (zone.empty()) + return {}; + + StringObjectException exception; + Polygons offenders; + bool body_tier_used = false; + for (const PrintObject *object : print.objects()) { + const double object_top = unscaled(object->max_z()); + for (const PrintInstance &instance : object->instances()) { + const Polygon inst_hull = compacted_tower_print_instance_hull(*object, instance); + if (inst_hull.points.empty()) + continue; + const CompactedTowerClearance clearance = compacted_wipe_tower_clearance(config, zone, inst_hull, object_top); + body_tier_used = body_tier_used || compacted_tower_body_tier(clearance); + // Every tier the precise check applies is applied here too, otherwise an object standing + // within the toolhead radius would pass here and then be rejected mid-slice, which is the + // one outcome this check exists to prevent. The compacted tower base is unknown before + // slicing, so the rise is measured from the plate rather than from the tower top; that + // overstates it by the tower's own height and makes this check err strict, never lax. + if (object_top <= clearance.allowed_rise + EPSILON) + continue; + + // Height-limit and too-close cases share one user-facing message: both mean the layout + // violates the "No sparse layers" clearance rule, and the remedies are the same. + const std::string msg = compacted_wipe_tower_clearance_error(); + if (exception.string.empty()) { + exception.string = msg; + exception.object = instance.model_instance; + } else { + // Same wording for every offender; keep a single copy and drop the object pointer. + exception.object = nullptr; + } + const Polygon outline = compacted_wipe_tower_offender_outline(inst_hull, clearance.body_clearance); + offenders.emplace_back(outline); + if (height_polygons) + height_polygons->emplace_back(outline, float(clearance.allowed_rise)); + } + } + + // Draw the tower's keep-out ring alongside the offending objects, so the collision area reads as + // "this object reaches into the space the toolhead needs around the tower" rather than as a lone + // highlighted object. Emitted only on a real collision; the plater discards polygons otherwise. + // Only the rings some object on this plate is actually measured against are drawn, so that a ring + // and an object outline touching always means that object is over its limit. + if (polygons && ! offenders.empty()) { + append(*polygons, compacted_wipe_tower_rings(zone, body_tier_used)); + append(*polygons, offenders); + } + return exception; +} + +// With wipe_tower_no_sparse_layers the tower only grows on layers that carry a real toolchange, +// so it ends up far below the object and the nozzle has to descend to it. While the nozzle sits +// down on the compacted tower the rod is at tower_z + extruder_clearance_height_to_rod, and it +// sweeps the tower's Y band across the whole X axis. Anything already printed above that line and +// sharing the band gets hit. Nearer than the toolhead radius the head body hits the object well before +// the rod does, which is the horizontal half of the same problem. The spiral Z-hop that opens a wipe- +// tower travel also leaves the extrusion outline at a low Z, so the footprint used here is the +// deposited hull grown by the spiral circle's maximum reach. This mirrors both clearance checks of +// sequential printing, except that the tower is revisited over and over, so every object is compared +// against it. +void Print::validate_compacted_wipe_tower_clearance() const +{ + // Nothing to check when the tower is not compacted: it then follows the object as usual and the + // regular by-layer clearance check already covers it. Asking wipe_tower_sparse_layers_skipped() + // rather than the raw option keeps this from rejecting plates whose tower is in fact full height. + if (! wipe_tower_sparse_layers_skipped(m_config) || m_config.print_sequence != PrintSequence::ByLayer) + return; + + const std::vector> &tool_changes = m_wipe_tower_data.tool_changes; + if (tool_changes.empty() || m_objects.empty()) + return; + + // Same accumulation the G-code emitter runs, so validation and output cannot disagree. + const std::vector tower_z = compute_compacted_wipe_tower_z(tool_changes, float(m_config.z_offset.value)); + + // Wipe tower footprint: build it from the ACTUAL tool-change extrusions rather than the nominal + // width x depth rectangle returned by first_layer_wipe_tower_corners(). With a rib wall the printed + // wall bulges past the nominal box and the first-layer brim reaches even further; the nominal box + // (m_wipe_tower_data.bbx) undercounts that outermost extent by several millimetres, which is + // exactly the extent that decides how close the sweeping rod comes to a neighbouring object. The + // extrusion end-points are stored in the wipe-tower local frame, so we map them to the bed frame + // with the same transform the G-code emitter applies. The two emitters differ in where rib_offset + // enters: WipeTowerIntegration::append_tcr() (type 1) rotates the point and then adds the offset, + // append_tcr2() (type 2) adds it before rotating. On a rotated rib-wall tower the two land several + // millimetres apart, which is exactly the margin this check measures, so follow the emitter in use. + const Eigen::Rotation2Dd wt_rot(Geometry::deg2rad(m_config.wipe_tower_rotation_angle.value)); + const Vec2d wt_translate(m_config.wipe_tower_x.get_at(m_plate_index) + m_origin(0), + m_config.wipe_tower_y.get_at(m_plate_index) + m_origin(1)); + const Vec2d rib_off = m_wipe_tower_data.rib_offset.cast(); + const bool rib_off_rotates = this->wipe_tower_type() == WipeTowerType::Type2; + auto to_bed = [&wt_rot, &wt_translate, &rib_off, rib_off_rotates](const Vec2d &pt) { + return rib_off_rotates ? Vec2d(wt_rot * (pt + rib_off) + wt_translate) : Vec2d(wt_rot * pt + wt_translate + rib_off); + }; + + Points tower_pts; + for (const std::vector &layer : tool_changes) { + if (layer.empty() || wipe_tower_layer_is_sparse(layer)) + continue; + for (const WipeTower::ToolChangeResult &tcr : layer) + for (size_t i = 0; i < tcr.extrusions.size(); ++i) { + // A zero width marks a travel end-point. Keep it only when it opens a real extrusion, so + // the hull covers the deposited material and nothing else; travels reach a bit further out + // than the walls do. + const WipeTower::Extrusion &e = tcr.extrusions[i]; + if (e.width == 0.f && (i + 1 == tcr.extrusions.size() || tcr.extrusions[i + 1].width == 0.f)) + continue; + const Vec2d p = to_bed(Vec2d(e.pos.x(), e.pos.y())); + tower_pts.emplace_back(scale_(p.x()), scale_(p.y())); + } + } + if (tower_pts.empty()) + return; + + const CompactedTowerZone zone = compacted_wipe_tower_zone(m_config, Geometry::convex_hull(tower_pts)); + if (zone.empty()) + return; + + for (const PrintObject *object : m_objects) { + const double object_top = unscaled(object->max_z()); + for (const PrintInstance &instance : object->instances()) { + const Polygon inst_hull = compacted_tower_print_instance_hull(*object, instance); + if (inst_hull.points.empty()) + continue; + + // Report the worst layer rather than the first offending one, it is the one that explains the + // collision best. The rise has to be known before the clearance: it is what selects the + // horizontal tier, the nozzle cone being out of the head body's reach. + double max_rise = 0.; + for (size_t i = 0; i < tool_changes.size(); ++i) { + if (tool_changes[i].empty() || wipe_tower_layer_is_sparse(tool_changes[i])) + continue; + // Nothing above the current layer exists yet, so a tall object only counts up to it. + const double rise = std::min(object_top, double(tool_changes[i].front().print_z)) - tower_z[i]; + if (rise > max_rise) + max_rise = rise; + } + + const CompactedTowerClearance clearance = compacted_wipe_tower_clearance(m_config, zone, inst_hull, max_rise); + if (max_rise <= clearance.allowed_rise + EPSILON) + continue; + // Same wording as compacted_wipe_tower_clearance_valid(): height-limit and too-close + // share one message, since both are layout violations of "No sparse layers". + throw Slic3r::SlicingError(compacted_wipe_tower_clearance_error()); + } + } +} + //BBS static StringObjectException layered_print_cleareance_valid(const Print &print, StringObjectException *warning) { @@ -1410,6 +1781,16 @@ StringObjectException Print::validate(std::vector *warnin } if (!layer_warning.string.empty()) add_warning(layer_warning); + + // Orca: a compacted prime tower drags the nozzle back down to the plate on every toolchange, so + // tall objects collide with it much like they do in sequential printing. Checking it here rather + // than only during slicing is what lets the plater show the collision area and the height limit + // while the plate is still being arranged. + ret = compacted_wipe_tower_clearance_valid(*this, collison_polygons, height_polygons); + if (!ret.string.empty()) { + ret.type = STRING_EXCEPT_OBJECT_COLLISION_IN_LAYER_PRINT; + return ret; + } } if (m_config.enable_prime_tower) { @@ -2622,6 +3003,12 @@ void Print::process(long long *time_cost_with_cache, bool use_cache) if (this->has_wipe_tower()) { m_fake_wipe_tower.set_pos({ m_config.wipe_tower_x.get_at(m_plate_index), m_config.wipe_tower_y.get_at(m_plate_index) }); + // Validated on every process() run rather than only when the wipe tower step is (re)generated. + // Moving the tower changes only wipe_tower_x/y, which invalidates psSkirtBrim but not psWipeTower, + // so a validate call living inside _make_wipe_tower would be skipped and keep using the stale + // position, missing a fresh collision. The tower geometry (tool_changes) is stored in the local + // frame and is position independent, so re-checking here with the current position is correct. + this->validate_compacted_wipe_tower_clearance(); } if (this->set_started(psSkirtBrim)) { diff --git a/src/libslic3r/Print.hpp b/src/libslic3r/Print.hpp index 9822c5520c..9c1782e047 100644 --- a/src/libslic3r/Print.hpp +++ b/src/libslic3r/Print.hpp @@ -1160,6 +1160,8 @@ public: //BBS static StringObjectException sequential_print_clearance_valid(const Print &print, Polygons *polygons = nullptr, std::vector>* height_polygons = nullptr); + // Orca: pre-slice clearance check for a prime tower compacted by "No sparse layers". + static StringObjectException compacted_wipe_tower_clearance_valid(const Print &print, Polygons *polygons = nullptr, std::vector>* height_polygons = nullptr); ConflictResultOpt get_conflict_result() const { return m_conflict_result; } // Return 4 wipe tower corners in the world coordinates (shifted and rotated), including the wipe tower brim. @@ -1174,6 +1176,8 @@ public: void set_calib_params(const Calib_Params& params); const Calib_Params& calib_params() const { return m_calib_params; } Vec2d translate_to_print_space(const Vec2d &point) const; + // Orca: precise counterpart of compacted_wipe_tower_clearance_valid(), run once the tower exists. + void validate_compacted_wipe_tower_clearance() const; float get_wipe_tower_depth() const { return m_wipe_tower_data.depth; } BoundingBoxf get_wipe_tower_bbx() const { return m_wipe_tower_data.bbx; } Vec2f get_rib_offset() const { return m_wipe_tower_data.rib_offset; } @@ -1394,6 +1398,89 @@ public: }; +// --------------------------------------------------------------------------------------------- +// Clearance rule for a prime tower compacted by wipe_tower_no_sparse_layers. Shared by the precise +// check that runs on the real extrusions, the pre-slice estimate that feeds the plater with collision +// polygons, and the plater's own live preview while the user drags the tower or an object around. +// Keeping the rule in one place is what stops those three from drifting apart and reporting different +// things for the same plate. +// --------------------------------------------------------------------------------------------- + +// Half of a clearance distance, the share each of the two outlines carries. Sequential printing splits +// extruder_clearance_radius between the two object hulls this way; the tower checks split their +// clearances between the tower ring and the instance hull for the same reason, so that the two +// outlines the plater draws touch precisely when the check trips. The 0.2 mm comes off first: it is +// the rounding slack the sequential check applies, 0.1 mm per side. +inline double compacted_tower_half_clearance(double clearance) { return 0.5 * (clearance - 0.2); } + +// Keep-out geometry a compacted tower projects onto the plate, derived from its bare footprint. +struct CompactedTowerZone +{ + // Footprint the checks work on: the raw outline grown by the spiral Z-hop envelope. + Polygon hull; + // hull grown by half the toolhead radius; an object whose own half-grown hull reaches into it is + // hit by the head body. This is also the ring the plater draws. + Polygons grown_body; + // hull grown by half the bare nozzle cone radius, the innermost tier. + Polygons grown_nozzle; + // hull bounding box, the Y band the rod sweeps. + BoundingBox bbox_rod; + // Full body clearance, of which grown_body carries half. Which of the two tiers applies is decided + // per object rather than here; see compacted_wipe_tower_clearance(). + double body_radius { 0. }; + + bool empty() const { return hull.points.empty(); } +}; + +// Per-side padding a bare wipe tower outline needs before the clearance checks may treat it as the +// tower's footprint. Callers whose outline already carries the first-layer brim pass zero for it. +// Shared by the pre-slice estimate and the plater's live preview: both start from an outline that +// falls short of the printed tower in the same two ways, and padding them by different amounts is +// exactly how the preview and the validation behind it would end up disagreeing. +double compacted_tower_footprint_padding(const PrintConfig &config, double brim_width); + +// Grow a bare tower footprint (bed frame, scaled) into its keep-out zone. +CompactedTowerZone compacted_wipe_tower_zone(const PrintConfig &config, const Polygon &tower_footprint); + +// How far an object may rise above the compacted tower base before the toolhead hits it. +struct CompactedTowerClearance +{ + // Height the object may reach above the tower base. Zero means it may not rise at all. + double allowed_rise; + // Clearance that applies once the object stands clear of the toolhead in XY, i.e. rod or lid. + double far_clearance; + // The object sits within the toolhead radius, so the head body limits it rather than the rod. + bool near_body; + // Horizontal clearance this particular object has to keep from the tower: the full toolhead + // radius once it rises past the nozzle cone, the bare cone while it stays below. It is what the + // error message quotes and what the plater grows the object outline by. + double body_clearance; +}; + +// object_rise is the height above the tower base that the caller is going to compare against +// allowed_rise. It also selects the horizontal tier, so the two cannot disagree. +CompactedTowerClearance compacted_wipe_tower_clearance(const PrintConfig &config, const CompactedTowerZone &zone, + const Polygon &inst_hull, double object_rise); + +// This object was judged on a tier reaching past the bare nozzle cone, so the wide ring is the one its +// outline has to be drawn against. +inline bool compacted_tower_body_tier(const CompactedTowerClearance &clearance) +{ + return clearance.body_clearance > double(MAX_OUTER_NOZZLE_DIAMETER); +} + +// Keep-out rings to draw around the tower. The nozzle one always applies; the wide body one is drawn +// only when some object on the plate is actually measured against it, otherwise it would show a +// keep-out zone no object can violate. +Polygons compacted_wipe_tower_rings(const CompactedTowerZone &zone, bool any_body_tier); + +// Outline to hand the plater for an offending object: the instance hull grown by the same half +// clearance the check grew it by, which is CompactedTowerClearance::body_clearance for that object. +// Sequential printing reports its hulls the same way, and it doubles as the fix for the bare hull +// being unusable on screen, where drawn flat it hides under the object and drawn at the height limit +// it ends up buried inside the mesh. +Polygon compacted_wipe_tower_offender_outline(const Polygon &inst_hull, double body_clearance); + } /* slic3r_Print_hpp_ */ #endif diff --git a/src/libslic3r/PrintConfig.cpp b/src/libslic3r/PrintConfig.cpp index 74d332ca01..f187e6ab82 100644 --- a/src/libslic3r/PrintConfig.cpp +++ b/src/libslic3r/PrintConfig.cpp @@ -2549,6 +2549,16 @@ void PrintConfigDef::init_fff_params() def->enum_labels.push_back("5"); def->mode = comAdvanced; + // Orca: already carried by the BBL/Qidi/Geeetech/Eryone machine profiles, which inherited it from + // the BambuStudio import; without a definition here it was parsed as an unknown key and dropped. + def = this->add("extruder_clearance_dist_to_rod", coFloat); + def->label = L("Distance to rod"); + def->tooltip = L("Horizontal distance of the nozzle tip to the rod's farther edge. Used for collision avoidance in by-object printing."); + def->sidetext = L("mm"); // millimeters, CIS languages need translation + def->min = 0; + def->mode = comAdvanced; + def->set_default_value(new ConfigOptionFloat(40)); + def = this->add("extruder_clearance_height_to_rod", coFloat); def->label = L("Height to rod"); def->tooltip = L("Distance from the nozzle tip to the lower rod. Used for collision avoidance in by-object printing."); @@ -6673,8 +6683,10 @@ void PrintConfigDef::init_fff_params() def = this->add("wipe_tower_no_sparse_layers", coBool); def->label = L("No sparse layers (beta)"); def->tooltip = L("If enabled, the wipe tower will not be printed on layers with no tool changes. " - "On layers with a tool change, extruder will travel downward to print the wipe tower. " - "User is responsible for ensuring there is no collision with the print."); + "On layers with a tool change, extruder will travel downward to print the wipe tower, " + "so the tower ends up below the model and the toolhead has to reach down to it. " + "Layouts where that would collide with an already printed object are rejected. " + "Has no effect with smooth timelapse or clumping detection, which need a tower on every layer."); def->mode = comAdvanced; def->set_default_value(new ConfigOptionBool(false)); diff --git a/src/libslic3r/PrintConfig.hpp b/src/libslic3r/PrintConfig.hpp index 3373fbece7..d161863a9c 100644 --- a/src/libslic3r/PrintConfig.hpp +++ b/src/libslic3r/PrintConfig.hpp @@ -1790,6 +1790,7 @@ PRINT_CONFIG_CLASS_DERIVED_DEFINE( ((ConfigOptionBools, slow_down_for_layer_cooling)) ((ConfigOptionInts, close_fan_the_first_x_layers)) ((ConfigOptionEnum, draft_shield)) + ((ConfigOptionFloat, extruder_clearance_dist_to_rod))//BBS ((ConfigOptionFloat, extruder_clearance_height_to_rod))//BBs ((ConfigOptionFloat, extruder_clearance_height_to_lid))//BBS ((ConfigOptionFloat, extruder_clearance_radius)) diff --git a/src/slic3r/GUI/ConfigManipulation.cpp b/src/slic3r/GUI/ConfigManipulation.cpp index f0f0adfa38..0244710298 100644 --- a/src/slic3r/GUI/ConfigManipulation.cpp +++ b/src/slic3r/GUI/ConfigManipulation.cpp @@ -1041,10 +1041,12 @@ void ConfigManipulation::toggle_print_fff_options(DynamicPrintConfig *config, in for (auto el : {"wipe_tower_rotation_angle", "wipe_tower_cone_angle", "wipe_tower_extra_spacing", "wipe_tower_max_purge_speed", - "wipe_tower_bridging", "wipe_tower_extra_flow", - "wipe_tower_no_sparse_layers"}) + "wipe_tower_bridging", "wipe_tower_extra_flow"}) toggle_line(el, have_prime_tower && supports_wipe_tower_2); + // Orca: both tower generators skip sparse layers, so this is not a wipe tower 2 exclusive. + toggle_line("wipe_tower_no_sparse_layers", have_prime_tower); + WipeTowerWallType wipe_tower_wall_type = config->opt_enum("wipe_tower_wall_type"); bool have_rib_wall = (wipe_tower_wall_type == WipeTowerWallType::wtwRib)&&have_prime_tower; toggle_line("wipe_tower_cone_angle", have_prime_tower && supports_wipe_tower_2 && wipe_tower_wall_type == WipeTowerWallType::wtwCone); diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index 76192491bf..83c8cba0ee 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -4316,6 +4316,9 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt) if (can_sequential_clearance_show_in_gizmo()) update_sequential_clearance(); } else { + // Orca: by-layer counterpart, for a prime tower compacted by "No sparse layers". + if (current_printer_technology() == ptFFF && can_sequential_clearance_show_in_gizmo()) + update_compacted_wipe_tower_clearance(); if (c == GLGizmosManager::EType::Move || c == GLGizmosManager::EType::Scale || c == GLGizmosManager::EType::Rotate) @@ -4549,8 +4552,12 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt) TransformationType trafo_type; trafo_type.set_relative(); m_selection.translate(cur_pos - m_mouse.drag.start_position_3D, trafo_type); - if (current_printer_technology() == ptFFF && (fff_print()->config().print_sequence == PrintSequence::ByObject)) - update_sequential_clearance(); + if (current_printer_technology() == ptFFF) { + if (fff_print()->config().print_sequence == PrintSequence::ByObject) + update_sequential_clearance(); + else + update_compacted_wipe_tower_clearance(); + } // BBS //wxGetApp().obj_manipul()->set_dirty(); m_dirty = true; @@ -5614,6 +5621,101 @@ bool GLCanvas3D::can_sequential_clearance_show_in_gizmo() { return false; } +// Live preview of the compacted prime tower clearance, the by-layer counterpart of +// update_sequential_clearance(). Called while the user drags a volume / gizmo; idle visibility +// matches sequential print (hidden when valid, filled when Print::validate reports a collision). +// Print::compacted_wipe_tower_clearance_valid() answers the same question authoritatively, but it +// reads the tower position from the config, which only catches up once do_move() writes it back on +// mouse release. Recomputing from the volumes here is what makes the keep-out zone follow the tower +// while it is still under the cursor. +void GLCanvas3D::update_compacted_wipe_tower_clearance() +{ + if (current_printer_technology() != ptFFF) + return; + const Print *print = fff_print(); + if (print == nullptr) + return; + const PrintConfig &config = print->config(); + if (config.print_sequence != PrintSequence::ByLayer || ! wipe_tower_sparse_layers_skipped(config) || ! print->has_wipe_tower()) + return; + + PartPlateList &plate_list = wxGetApp().plater()->get_partplate_list(); + PartPlate *plate = plate_list.get_curr_plate(); + if (plate == nullptr) + return; + const int plate_id = plate_list.get_curr_plate_index(); + + // Once the tower has been generated the scene shows its real mesh with the brim merged in, + // otherwise it is a bare estimated cube with no brim at all. Only the latter needs the brim added + // here, and the width comes from WipeTowerData, the same source the preview box is sized from, so + // the zone cannot be padded against a brim the preview was not built with. + const bool preview_carries_brim = print->is_step_done(psWipeTower) && print->wipe_tower_data().wipe_tower_mesh_data.has_value(); + const double brim = preview_carries_brim ? 0. : double(print->wipe_tower_data(print->extruders().size()).brim_width); + const double padding = compacted_tower_footprint_padding(config, brim); + + // Tower footprint straight from the volume the user sees, so that dragging either the tower or an + // object updates the zone on the very next frame. + Polygon tower_footprint; + for (const GLVolume *v : m_volumes.volumes) { + if (! v->is_wipe_tower || v->object_idx() - 1000 != plate_id) + continue; + const BoundingBoxf3 bbox = v->transformed_convex_hull_bounding_box(); + tower_footprint = Polygon({ Point(scale_(bbox.min.x() - padding), scale_(bbox.min.y() - padding)), + Point(scale_(bbox.max.x() + padding), scale_(bbox.min.y() - padding)), + Point(scale_(bbox.max.x() + padding), scale_(bbox.max.y() + padding)), + Point(scale_(bbox.min.x() - padding), scale_(bbox.max.y() + padding)) }); + break; + } + + const CompactedTowerZone zone = compacted_wipe_tower_zone(config, tower_footprint); + if (zone.empty()) { + reset_sequential_print_clearance(); + return; + } + + // While dragging, outline every on-plate instance next to the tower ring, the way sequential print + // outlines every object. Both carry half of the clearance, so the two outlines meeting is precisely + // the moment that object goes over its limit - which is what makes the pair worth drawing at all. + // The tier is per object, so a short object gets the narrow nozzle outline rather than the wide + // body one it is not subject to; without that, a 3 mm object parked beside the tower would be drawn + // deep inside the keep-out ring while passing the check. Only the instances that already exceed + // allowed_rise also get a height limit plane. + Polygons outlines; + std::vector> height_polygons; + bool body_tier_used = false; + const BoundingBox plate_bb = plate->get_bounding_box_crd(); + for (const ModelObject *model_object : m_model->objects) { + for (size_t i = 0; i < model_object->instances.size(); ++i) { + Geometry::Transformation trafo(model_object->instances[i]->get_transformation()); + const Vec3d offset = trafo.get_offset(); + trafo.set_offset(Vec3d(offset.x(), offset.y(), 0.0)); + const Polygon inst_hull = model_object->convex_hull_2d(trafo.get_matrix()); + if (inst_hull.points.empty() || ! plate_bb.overlap(inst_hull.bounding_box())) + continue; + + // Same tiers and the same rise measured from the plate as + // Print::compacted_wipe_tower_clearance_valid(), so that the preview and the validation + // that follows it never contradict each other. + const double object_top = model_object->get_instance_max_z(i); + const CompactedTowerClearance clearance = compacted_wipe_tower_clearance(config, zone, inst_hull, object_top); + body_tier_used = body_tier_used || compacted_tower_body_tier(clearance); + + const Polygon outline = compacted_wipe_tower_offender_outline(inst_hull, clearance.body_clearance); + outlines.emplace_back(outline); + if (object_top <= clearance.allowed_rise + EPSILON) + continue; + height_polygons.emplace_back(outline, float(clearance.allowed_rise)); + } + } + + Polygons polygons = compacted_wipe_tower_rings(zone, body_tier_used); + append(polygons, outlines); + + set_sequential_print_clearance_visible(true); + set_sequential_print_clearance_render_fill(false); + set_sequential_print_clearance_polygons(polygons, height_polygons); +} + void GLCanvas3D::update_sequential_clearance() { if (current_printer_technology() != ptFFF || (fff_print()->config().print_sequence == PrintSequence::ByLayer)) diff --git a/src/slic3r/GUI/GLCanvas3D.hpp b/src/slic3r/GUI/GLCanvas3D.hpp index c2962c3858..1ea352ac96 100644 --- a/src/slic3r/GUI/GLCanvas3D.hpp +++ b/src/slic3r/GUI/GLCanvas3D.hpp @@ -1191,6 +1191,8 @@ public: bool can_sequential_clearance_show_in_gizmo(); void update_sequential_clearance(); + // Orca: by-layer counterpart, for a prime tower compacted by "No sparse layers". + void update_compacted_wipe_tower_clearance(); const Print* fff_print() const; const SLAPrint* sla_print() const; diff --git a/src/slic3r/GUI/PartPlate.cpp b/src/slic3r/GUI/PartPlate.cpp index 893260f934..260c857f22 100644 --- a/src/slic3r/GUI/PartPlate.cpp +++ b/src/slic3r/GUI/PartPlate.cpp @@ -1053,7 +1053,13 @@ void PartPlate::render_grid(bool bottom) { void PartPlate::render_height_limit(PartPlate::HeightLimitMode mode) { - if (m_print && m_print->config().print_sequence == PrintSequence::ByObject && mode != HEIGHT_LIMIT_NONE) + // Orca: a prime tower compacted by "No sparse layers" drags the nozzle back down to the plate on + // every toolchange, so the rod and the lid limit how tall a neighbouring object may be exactly as + // they do in sequential printing. The reference lines are just as useful there. + const bool relevant_for_print_mode = m_print && (m_print->config().print_sequence == PrintSequence::ByObject || + (m_print->config().print_sequence == PrintSequence::ByLayer && + wipe_tower_sparse_layers_skipped(m_print->config()) && m_print->has_wipe_tower())); + if (relevant_for_print_mode && mode != HEIGHT_LIMIT_NONE) { // draw lower limit // ORCA: OpenGL Core Profile diff --git a/src/slic3r/GUI/Tab.cpp b/src/slic3r/GUI/Tab.cpp index 6279941e13..28d3478bf2 100644 --- a/src/slic3r/GUI/Tab.cpp +++ b/src/slic3r/GUI/Tab.cpp @@ -1992,6 +1992,20 @@ void Tab::on_value_change(const std::string& opt_key, const boost::any& value) // reload scene to update timelapse wipe tower if (opt_key == "timelapse_type") { + // Smooth timelapse parks the nozzle on the prime tower every layer, so it needs a tower on + // every layer. That is exactly what "No sparse layers" removes, and with both on the tower is + // planned full height and then dropped on emission. Drop "No sparse layers" and tell the user. + if (boost::any_cast(value) == (int) TimelapseType::tlSmooth && m_config->opt_bool("wipe_tower_no_sparse_layers")) { + MessageDialog dlg(wxGetApp().plater(), + _L("Smooth timelapse needs a prime tower on every layer, which is not compatible with \"No sparse layers\". " + "\"No sparse layers\" has been turned off."), + _L("Warning"), wxICON_WARNING | wxOK); + dlg.ShowModal(); + DynamicPrintConfig new_conf = *m_config; + new_conf.set_key_value("wipe_tower_no_sparse_layers", new ConfigOptionBool(false)); + m_config_manipulation.apply(m_config, &new_conf); + } + bool wipe_tower_enabled = m_config->option("enable_prime_tower")->value; if (!wipe_tower_enabled && boost::any_cast(value) == (int)TimelapseType::tlSmooth) { MessageDialog dlg(wxGetApp().plater(), _L("A prime tower is required for smooth timelapse mode. There may be flaws on the model without prime tower. Do you want to enable the prime tower\?"), @@ -2007,6 +2021,23 @@ void Tab::on_value_change(const std::string& opt_key, const boost::any& value) } } + // Mirror of the timelapse_type branch above: enabling "No sparse layers" while smooth timelapse + // is active would leave the tower on every layer anyway, so fall back to traditional timelapse. + if (opt_key == "wipe_tower_no_sparse_layers" && boost::any_cast(value)) { + auto timelapse_type = m_config->option>("timelapse_type"); + if (timelapse_type && timelapse_type->value == TimelapseType::tlSmooth) { + MessageDialog dlg(wxGetApp().plater(), + _L("\"No sparse layers\" is not compatible with smooth timelapse, which needs a prime tower on every layer. " + "Timelapse has been switched to traditional mode."), + _L("Warning"), wxICON_WARNING | wxOK); + dlg.ShowModal(); + DynamicPrintConfig new_conf = *m_config; + new_conf.set_key_value("timelapse_type", new ConfigOptionEnum(TimelapseType::tlTraditional)); + m_config_manipulation.apply(m_config, &new_conf); + wxGetApp().plater()->update(); + } + } + if (opt_key == "print_sequence" && m_config->opt_enum("print_sequence") == PrintSequence::ByObject) { auto printer_structure_opt = m_preset_bundle->printers.get_edited_preset().config.option>("printer_structure"); if (printer_structure_opt && printer_structure_opt->value == PrinterStructure::psI3) { @@ -5109,6 +5140,7 @@ void TabPrinter::build_fff() optgroup = page->new_optgroup(L("Extruder Clearance"), "param_extruder_clearance"); optgroup->append_single_option_line("extruder_clearance_radius", "printer_basic_information_extruder_clearance#radius"); + optgroup->append_single_option_line("extruder_clearance_dist_to_rod", "printer_basic_information_extruder_clearance"); optgroup->append_single_option_line("extruder_clearance_height_to_rod", "printer_basic_information_extruder_clearance#height-to-rod"); optgroup->append_single_option_line("extruder_clearance_height_to_lid", "printer_basic_information_extruder_clearance#height-to-lid"); diff --git a/tests/libslic3r/test_wipe_tower.cpp b/tests/libslic3r/test_wipe_tower.cpp index 2987dce9da..277e11eceb 100644 --- a/tests/libslic3r/test_wipe_tower.cpp +++ b/tests/libslic3r/test_wipe_tower.cpp @@ -6,6 +6,8 @@ #include "libslic3r/ClipperUtils.hpp" #include "libslic3r/GCode/WipeTower.hpp" #include "libslic3r/GCode/WipeTower2.hpp" +#include "libslic3r/Print.hpp" +#include "libslic3r/PrintConfig.hpp" using namespace Slic3r; using Catch::Matchers::WithinAbs; @@ -91,3 +93,197 @@ TEST_CASE("Brim width estimate matches each generator's loop quantization", "[Wi CHECK_THAT(WipeTower::estimate_brim_real_width(3.f, 0.4f, 0.2f, false), WithinAbs(7.5f * spacing, 1e-4f)); CHECK_THAT(WipeTower::estimate_brim_real_width(0.f, 0.4f, 0.2f, true), WithinAbs(0.f, 1e-6f)); } + +// --------------------------------------------------------------------------------------------- +// "No sparse layers": the compaction rule and the clearance it demands of the plate. +// --------------------------------------------------------------------------------------------- + +// A square of side mm centred on (cx, cy), in bed coordinates. +static Polygon centered_square(double cx, double cy, double side) +{ + const double h = 0.5 * side; + Polygon poly; + poly.points = {Point::new_scale(cx - h, cy - h), Point::new_scale(cx + h, cy - h), + Point::new_scale(cx + h, cy + h), Point::new_scale(cx - h, cy + h)}; + return poly; +} + +static WipeTower::ToolChangeResult make_tcr(int initial_tool, int new_tool, float layer_height) +{ + WipeTower::ToolChangeResult tcr{}; + tcr.initial_tool = initial_tool; + tcr.new_tool = new_tool; + tcr.layer_height = layer_height; + return tcr; +} + +// A 20 mm square tower at the bed origin, no spiral z-hop, so the keep-out zone is the bare +// footprint and every distance below is one the test sets. +static PrintConfig clearance_config() +{ + PrintConfig cfg; + cfg.extruder_clearance_radius.value = 40.; + cfg.extruder_clearance_dist_to_rod.value = 20.; + cfg.extruder_clearance_height_to_rod.value = 25.; + cfg.extruder_clearance_height_to_lid.value = 120.; + cfg.nozzle_height.value = 5.; + cfg.nozzle_diameter.values = {0.4}; + cfg.z_hop.values = {0.}; + cfg.travel_slope.values = {3.}; + return cfg; +} + +TEST_CASE("Sparse layers are skipped only when nothing else needs a tower on every layer", "[WipeTower][NoSparseLayers]") { + PrintConfig cfg; + cfg.timelapse_type.value = TimelapseType::tlTraditional; + cfg.enable_wrapping_detection.value = false; + + cfg.wipe_tower_no_sparse_layers.value = false; + CHECK_FALSE(wipe_tower_sparse_layers_skipped(cfg)); + cfg.wipe_tower_no_sparse_layers.value = true; + CHECK(wipe_tower_sparse_layers_skipped(cfg)); + + // Both park the nozzle on the tower every layer, so no layer is ever dropped and the option + // must read as off everywhere rather than compact in one place and not another. + cfg.timelapse_type.value = TimelapseType::tlSmooth; + CHECK_FALSE(wipe_tower_sparse_layers_skipped(cfg)); + cfg.timelapse_type.value = TimelapseType::tlTraditional; + cfg.enable_wrapping_detection.value = true; + CHECK_FALSE(wipe_tower_sparse_layers_skipped(cfg)); +} + +TEST_CASE("A planned layer is sparse only when its single tool change keeps the filament", "[WipeTower][NoSparseLayers]") { + CHECK(wipe_tower_layer_is_sparse({make_tcr(1, 1, 0.2f)})); + CHECK_FALSE(wipe_tower_layer_is_sparse({make_tcr(0, 1, 0.2f)})); + // A second entry means the layer carries real work whatever the tools are. + CHECK_FALSE(wipe_tower_layer_is_sparse({make_tcr(1, 1, 0.2f), make_tcr(1, 1, 0.2f)})); + CHECK_FALSE(wipe_tower_layer_is_sparse({})); +} + +TEST_CASE("The compacted tower falls one layer height behind the object per sparse layer", "[WipeTower][NoSparseLayers]") { + // Five 0.2 mm layers off a 0.1 mm z offset, the middle two sparse. The object reaches + // 0.1 + 5 * 0.2 = 1.1; the tower only grows on the three printed layers, so it ends at + // 0.1 + 3 * 0.2 = 0.7 and a sparse layer carries the previous value rather than its own. + const std::vector> tool_changes{ + {make_tcr(0, 1, 0.2f)}, {make_tcr(1, 1, 0.2f)}, {make_tcr(1, 1, 0.2f)}, + {make_tcr(1, 0, 0.2f)}, {make_tcr(0, 1, 0.2f)}}; + + const std::vector tower_z = compute_compacted_wipe_tower_z(tool_changes, 0.1f); + REQUIRE(tower_z.size() == tool_changes.size()); + CHECK_THAT(tower_z[0], WithinAbs(0.3f, 1e-5f)); + CHECK_THAT(tower_z[1], WithinAbs(0.3f, 1e-5f)); + CHECK_THAT(tower_z[2], WithinAbs(0.3f, 1e-5f)); + CHECK_THAT(tower_z[3], WithinAbs(0.5f, 1e-5f)); + CHECK_THAT(tower_z[4], WithinAbs(0.7f, 1e-5f)); + CHECK_THAT(1.1f - tower_z.back(), WithinAbs(2 * 0.2f, 1e-5f)); + + // Without a base the tower starts at the bed, and an empty layer carries over like a sparse one. + const std::vector no_offset = compute_compacted_wipe_tower_z({{make_tcr(0, 1, 0.2f)}, {}}, 0.f); + CHECK_THAT(no_offset[0], WithinAbs(0.2f, 1e-5f)); + CHECK_THAT(no_offset[1], WithinAbs(0.2f, 1e-5f)); +} + +TEST_CASE("The tower keep-out zone grows by the spiral z-hop envelope", "[WipeTower][NoSparseLayers]") { + PrintConfig cfg = clearance_config(); + const Polygon footprint = centered_square(0., 0., 20.); + + // No lift, no envelope: the zone works on the bare footprint. + CHECK_THAT(unscaled(compacted_wipe_tower_zone(cfg, footprint).hull.bounding_box().max.x()), WithinAbs(10., 1e-6)); + + // A spiral lift leaves the outline at low z, so it counts as tower. The circle reaches + // 2 * lift / (2*pi*atan(slope)) past the outline, matching GCodeWriter: 2*2/(2*pi*atan(3)) = 0.51 mm. + cfg.z_hop.values = {2.}; + const CompactedTowerZone lifted = compacted_wipe_tower_zone(cfg, footprint); + CHECK_THAT(unscaled(lifted.hull.bounding_box().max.x()), WithinAbs(10.51, 0.02)); + CHECK_THAT(unscaled(lifted.hull.bounding_box().min.y()), WithinAbs(-10.51, 0.02)); + CHECK(diff(Polygons{footprint}, Polygons{lifted.hull}).empty()); + + // z_hop is capped at 5 mm by the option, so a taller lift cannot widen the zone further. + cfg.z_hop.values = {10.}; + const double capped = unscaled(compacted_wipe_tower_zone(cfg, footprint).hull.bounding_box().max.x()); + CHECK_THAT(capped, WithinAbs(10. + 2. * 5. / (2. * M_PI * std::atan(3.)), 0.02)); + + // The rod sweeps the whole X axis, so its band is the tower's y span plus half the rod offset. + CHECK_THAT(unscaled(lifted.bbox_rod.max.y()), WithinAbs(10.51 + 10., 0.02)); +} + +TEST_CASE("An object beside a compacted tower is limited by the nearest part of the toolhead", "[WipeTower][NoSparseLayers]") { + const PrintConfig cfg = clearance_config(); + const CompactedTowerZone zone = compacted_wipe_tower_zone(cfg, centered_square(0., 0., 20.)); + + // Each side carries half its clearance less 0.1 mm slack, so the two outlines meet when the + // objects are a full clearance apart: 2 * (4 - 0.2) / 2 = 3.8 mm for the bare nozzle cone, + // 2 * (40 - 0.2) / 2 = 39.8 mm for the head body. A 10 mm object at x leaves a gap of x - 15. + const double tall = 50., shortish = 3.; + + // Gap 1 mm, inside the nozzle cone: the object may not rise above the tower at all. + const CompactedTowerClearance touching = compacted_wipe_tower_clearance(cfg, zone, centered_square(16., 0., 10.), tall); + CHECK_THAT(touching.allowed_rise, WithinAbs(0., 1e-9)); + + // Gap 10 mm: clear of the cone but inside the head body, which starts at nozzle_height. + const CompactedTowerClearance near_body = compacted_wipe_tower_clearance(cfg, zone, centered_square(25., 0., 10.), tall); + CHECK(near_body.near_body); + CHECK_THAT(near_body.allowed_rise, WithinAbs(5., 1e-9)); + CHECK_THAT(near_body.body_clearance, WithinAbs(40., 1e-9)); + + // The same spot, but an object that never rises past the cone. The body sits above the cone, so + // it cannot reach this object however close it stands, and only the narrow tier applies. + const CompactedTowerClearance low = compacted_wipe_tower_clearance(cfg, zone, centered_square(25., 0., 10.), shortish); + CHECK_FALSE(low.near_body); + CHECK_THAT(low.body_clearance, WithinAbs(4., 1e-9)); + CHECK_THAT(low.allowed_rise, WithinAbs(25., 1e-9)); + + // Gap 55 mm, clear of the head entirely: the rod is the obstacle, since the object shares the + // tower's y band and the rod spans the whole x axis however far apart the two stand. + const CompactedTowerClearance far_in_band = compacted_wipe_tower_clearance(cfg, zone, centered_square(70., 0., 10.), tall); + CHECK_FALSE(far_in_band.near_body); + CHECK_THAT(far_in_band.far_clearance, WithinAbs(25., 1e-9)); + CHECK_THAT(far_in_band.allowed_rise, WithinAbs(25., 1e-9)); + + // Out of the band the rod passes over it and only the lid is left. + const CompactedTowerClearance out_of_band = compacted_wipe_tower_clearance(cfg, zone, centered_square(70., 60., 10.), tall); + CHECK_THAT(out_of_band.allowed_rise, WithinAbs(120., 1e-9)); +} + +TEST_CASE("The ring drawn around the tower meets the outline drawn around an offender", "[WipeTower][NoSparseLayers]") { + const PrintConfig cfg = clearance_config(); + const CompactedTowerZone zone = compacted_wipe_tower_zone(cfg, centered_square(0., 0., 20.)); + + // What the plater draws has to be what the check tested, otherwise a user moves an object until + // the outlines part and slicing still refuses the plate. Both halves of the 3.8 mm nozzle + // clearance: at a 3 mm gap the rings overlap and the rise limit is zero, at 5 mm neither holds. + for (const auto &c : {std::make_pair(18., true), std::make_pair(20., false)}) { + DYNAMIC_SECTION("object at x = " << c.first) { + const Polygon hull = centered_square(c.first, 0., 10.); + const CompactedTowerClearance clearance = compacted_wipe_tower_clearance(cfg, zone, hull, 3.); + const Polygons rings = compacted_wipe_tower_rings(zone, compacted_tower_body_tier(clearance)); + const Polygon outline = compacted_wipe_tower_offender_outline(hull, clearance.body_clearance); + const bool outlines_meet = ! intersection(rings, Polygons{outline}).empty(); + const bool rise_denied = clearance.allowed_rise < EPSILON; + CHECK(outlines_meet == c.second); + CHECK(rise_denied == c.second); + } + } +} + +TEST_CASE("Only the keep-out ring an object is measured against is drawn", "[WipeTower][NoSparseLayers]") { + const PrintConfig cfg = clearance_config(); + const CompactedTowerZone zone = compacted_wipe_tower_zone(cfg, centered_square(0., 0., 20.)); + + // Drawing the wide ring when no object is judged on it would show a keep-out zone the check can + // never trip, so it is added only once some object reaches past the nozzle cone. + CHECK(compacted_wipe_tower_rings(zone, false).size() == zone.grown_nozzle.size()); + CHECK(compacted_wipe_tower_rings(zone, true).size() == zone.grown_nozzle.size() + zone.grown_body.size()); + CHECK_THAT(unscaled(get_extents(zone.grown_nozzle).max.x()), WithinAbs(10. + 0.5 * (4. - 0.2), 0.02)); + CHECK_THAT(unscaled(get_extents(zone.grown_body).max.x()), WithinAbs(10. + 0.5 * (40. - 0.2), 0.02)); +} + +TEST_CASE("Footprint padding covers the brim and the extrusion half width on each side", "[WipeTower][NoSparseLayers]") { + // A nominal outline hulls extrusion centre lines and is re-centred once the real wall is known, + // so a line width per side on top of the brim is what keeps an estimate enclosing the real tower. + const PrintConfig cfg = clearance_config(); + CHECK_THAT(compacted_tower_footprint_padding(cfg, 2.), WithinAbs(2. + 2. * 0.4, 1e-9)); + CHECK_THAT(compacted_tower_footprint_padding(cfg, 0.), WithinAbs(2. * 0.4, 1e-9)); + // Callers whose outline already carries the brim pass zero, and a negative one cannot shrink it. + CHECK_THAT(compacted_tower_footprint_padding(cfg, -5.), WithinAbs(2. * 0.4, 1e-9)); +}