#include #include #include #include #include #include #include "libslic3r/Config.hpp" #include "libslic3r/ExPolygon.hpp" #include "libslic3r/Model.hpp" #include "libslic3r/Point.hpp" #include "libslic3r/Polygon.hpp" #include "libslic3r/Polyline.hpp" #include "libslic3r/TriangleMesh.hpp" #include "libslic3r/libslic3r.h" #include #include #include #include #include #include "libslic3r/GCode/ExclusionVolumePathCheck.hpp" #include "libslic3r/GCode/ExclusionVolumeTravelAvoidance.hpp" #include "libslic3r/GCode/GCodeProcessor.hpp" #include "libslic3r/ClipperUtils.hpp" #include "libslic3r/ExclusionVolumeGeometry.hpp" #include "libslic3r/Print.hpp" #include "libslic3r/PrintBase.hpp" #include "libslic3r/PrintConfig.hpp" #include "libslic3r/Support/SupportCommon.hpp" #include "libslic3r/Support/SupportLayer.hpp" #include "test_helpers.hpp" #include "test_utils.hpp" #include #include #include #include #include using namespace Slic3r; using Catch::Matchers::WithinAbs; namespace { DynamicPrintConfig exclusion_config( BedExcludeVolumeMode mode = BedExcludeVolumeMode::Shared, const std::string &shared = "0..10;40x40,60x40,60x60,40x60", const std::vector &per_extruder = {}) { DynamicPrintConfig config = DynamicPrintConfig::full_print_config(); const size_t extruder_count = std::max(1, per_extruder.size()); config.set_num_extruders(extruder_count); config.set_key_value("nozzle_diameter", new ConfigOptionFloats(extruder_count, 0.4)); config.set_key_value("extruder_offset", new ConfigOptionPoints(extruder_count, Vec2d::Zero())); config.set_key_value("extruder_printable_height", new ConfigOptionFloatsNullable(extruder_count, 100.0)); config.set_key_value("printable_height", new ConfigOptionFloat(100.0)); config.set_key_value("printable_area", new ConfigOptionPoints{ Vec2d(0.0, 0.0), Vec2d(100.0, 0.0), Vec2d(100.0, 100.0), Vec2d(0.0, 100.0), }); config.set_key_value("bed_exclude_volume_mode", new ConfigOptionEnum(mode)); config.set_key_value("bed_exclude_volumes", new ConfigOptionString(shared)); if (!per_extruder.empty()) config.set_key_value("extruder_bed_exclude_volumes", new ConfigOptionStrings(per_extruder)); return config; } DynamicPrintConfig legacy_exclusion_config() { DynamicPrintConfig config = exclusion_config(); config.set_key_value("bed_exclude_volumes", new ConfigOptionString()); config.set_deserialize_strict("bed_exclude_area", "40x40,60x40,60x60,40x60"); return config; } FullPrintConfig static_config(const DynamicPrintConfig &dynamic) { FullPrintConfig config; // full_print_config() also contains GUI/profile-only keys which are not // members of the static slicing config used by these lower-level classes. config.apply(dynamic, true); return config; } Polyline travel(std::initializer_list points) { Points scaled_points; scaled_points.reserve(points.size()); for (const Vec2d &point : points) scaled_points.emplace_back(Point::new_scale(point)); return Polyline(std::move(scaled_points)); } Vec2d to_mm(const Point &point) { return point.cast() * SCALING_FACTOR; } struct Rect { double min_x; double min_y; double max_x; double max_y; }; bool point_inside_open_rect(const Vec2d &point, const Rect &rect) { return point.x() > rect.min_x && point.x() < rect.max_x && point.y() > rect.min_y && point.y() < rect.max_y; } // Independent Liang-Barsky-style test oracle for the simple rectangular // obstacles used here. This deliberately does not call the production path // checker, so matching bugs in the checker and router cannot hide each other. bool segment_enters_open_rect(const Vec2d &from, const Vec2d &to, const Rect &rect) { if (point_inside_open_rect(from, rect) || point_inside_open_rect(to, rect)) return true; double t_min = 0.0; double t_max = 1.0; const Vec2d delta = to - from; const auto clip_axis = [&](double origin, double direction, double min_value, double max_value, double &lower, double &upper) { if (std::abs(direction) < 1e-12) return origin > min_value && origin < max_value; double first = (min_value - origin) / direction; double second = (max_value - origin) / direction; if (first > second) std::swap(first, second); lower = std::max(lower, first); upper = std::min(upper, second); return lower < upper; }; if (!clip_axis(from.x(), delta.x(), rect.min_x, rect.max_x, t_min, t_max)) return false; if (!clip_axis(from.y(), delta.y(), rect.min_y, rect.max_y, t_min, t_max)) return false; if (t_min >= t_max) return false; const double midpoint_t = 0.5 * (std::max(0.0, t_min) + std::min(1.0, t_max)); return midpoint_t >= 0.0 && midpoint_t <= 1.0 && point_inside_open_rect(from + midpoint_t * delta, rect); } void check_path_avoids_rect(const Polyline &path, const Rect &rect) { REQUIRE(path.points.size() >= 2); for (size_t idx = 1; idx < path.points.size(); ++idx) CHECK_FALSE(segment_enters_open_rect(to_mm(path.points[idx - 1]), to_mm(path.points[idx]), rect)); } ExclusionVolumePathChecker configured_checker(const FullPrintConfig &config) { ExclusionVolumePathChecker checker; checker.configure(config); return checker; } void check_motion( ExclusionVolumePathChecker &checker, const Vec3d &from, const Vec3d &to, int extruder_id = 0, ExclusionVolumeMotionType type = ExclusionVolumeMotionType::Travel, bool start_xy_known = true, bool end_xy_known = true, bool start_z_known = true, bool end_z_known = true, unsigned int gcode_id = 17, size_t move_id = 23) { checker.check_motion( from, to, start_xy_known, end_xy_known, start_z_known, end_z_known, extruder_id, type, 7, gcode_id, move_id); } struct ProcessorExclusionResult { bool checked {false}; bool conflict {false}; bool travel_conflict {false}; bool extrusion_conflict {false}; int conflict_extruder_id {-1}; }; class ScopedBblPrinterMode { public: explicit ScopedBblPrinterMode(bool enabled) : m_previous(GCodeProcessor::s_IsBBLPrinter) { GCodeProcessor::s_IsBBLPrinter = enabled; } ~ScopedBblPrinterMode() { GCodeProcessor::s_IsBBLPrinter = m_previous; } private: bool m_previous; }; ProcessorExclusionResult run_processor(const FullPrintConfig &config, const std::string &gcode) { ScopedTemporaryFile file(".gcode"); { std::ofstream output(file.string()); output << gcode; } // The processor exposes this mode as shared state. Restore it after each // invocation so these tests remain independent of Catch2 execution order. ScopedBblPrinterMode printer_mode(false); GCodeProcessor processor; processor.apply_config(config); processor.configure_exclusion_volume_path_check(config); processor.process_file(file.string()); const GCodeProcessorResult &result = processor.get_result(); return { result.exclusion_volume_path_checked, result.exclusion_volume_path_conflict, result.exclusion_volume_travel_conflict, result.exclusion_volume_extrusion_conflict, result.exclusion_volume_conflict_extruder_id, }; } Polygon rectangle(double min_x, double min_y, double max_x, double max_y) { Polygon polygon({ Point::new_scale(min_x, min_y), Point::new_scale(max_x, min_y), Point::new_scale(max_x, max_y), Point::new_scale(min_x, max_y), }); polygon.make_counter_clockwise(); return polygon; } bool polygons_cover(const Polygons &polygons, double x, double y) { const Point point = Point::new_scale(x, y); const ExPolygons expolygons = union_ex(polygons); return std::any_of(expolygons.begin(), expolygons.end(), [&point](const ExPolygon &polygon) { return polygon.contains(point); }); } struct BrimRun { double total_length {0.0}; bool intersects_test_region {false}; bool leaves_bed {false}; }; BrimRun generate_brim(const std::string &exclusion_definition, const Vec2d &object_position = Vec2d(40.0, 40.0)) { DynamicPrintConfig config = exclusion_config(BedExcludeVolumeMode::Shared, exclusion_definition); // Brim clipping normally receives the cached bed polygon initialized by // the application. Give this headless fixture an explicit nozzle-reachable // area so it exercises the same boundary path without mutating global state. config.set_key_value("extruder_printable_area", new ConfigOptionPointsGroups{{ Vec2d(0.0, 0.0), Vec2d(100.0, 0.0), Vec2d(100.0, 100.0), Vec2d(0.0, 100.0), }}); config.set_deserialize_strict({ {"skirt_loops", "0"}, {"brim_type", "outer_only"}, {"brim_width", "10"}, {"initial_layer_line_width", "0.5"}, }); TriangleMesh mesh = make_cube(20.0, 20.0, 20.0); mesh.translate(float(object_position.x()), float(object_position.y()), 0.0f); Print print; Model model; Test::init_print(std::vector{std::move(mesh)}, print, model, config, nullptr, false); print.process(); Polylines paths; for (const Print::SkirtBrimGroup &group : print.skirt_brim_groups()) for (const Print::SkirtBrimGroup::Brim &brim : group.brims) { Polylines current = brim.brim.as_polylines(); paths.insert(paths.end(), std::make_move_iterator(current.begin()), std::make_move_iterator(current.end())); } BrimRun result; for (const Polyline &path : paths) result.total_length += path.length(); result.intersects_test_region = !intersection_pl(paths, Polygons{rectangle(32.0, 35.0, 38.0, 65.0)}).empty(); result.leaves_bed = std::any_of(paths.begin(), paths.end(), [](const Polyline &path) { return std::any_of(path.points.begin(), path.points.end(), [](const Point &point) { const Vec2d position = to_mm(point); return position.x() < -EPSILON || position.x() > 100.0 + EPSILON || position.y() < -EPSILON || position.y() > 100.0 + EPSILON; }); }); return result; } DynamicPrintConfig support_config(int support_filament, int interface_filament) { DynamicPrintConfig config = exclusion_config( BedExcludeVolumeMode::PerExtruder, "", {"0..10;4x4,12x4,12x12,4x12", "0..10;22x4,30x4,30x12,22x12"}); // PrintObject clamps support role ids against the number of logical // filaments, represented by filament_diameter in the static config. config.set_key_value("filament_diameter", new ConfigOptionFloats{1.75, 1.75}); config.set_key_value("filament_map_mode", new ConfigOptionEnum(fmmManual)); config.set_key_value("filament_map", new ConfigOptionInts{1, 2}); config.set_key_value("support_filament", new ConfigOptionInt(support_filament)); config.set_key_value("support_interface_filament", new ConfigOptionInt(interface_filament)); return config; } struct SupportPrintFixture { Model model; Print print; explicit SupportPrintFixture(const DynamicPrintConfig &config) { ModelObject *object = model.add_object(); ModelVolume *first = object->add_volume(make_cube(20.0, 20.0, 20.0)); ModelVolume *second = object->add_volume(make_cube(20.0, 20.0, 20.0)); first->config.set("extruder", 1); second->config.set("extruder", 2); // Pin the role assignments on the object as well as the process // config. This fixture intentionally exercises support exclusion // selection, not the separate preset-remapping subsystem. object->config.set("support_filament", config.opt_int("support_filament")); object->config.set("support_interface_filament", config.opt_int("support_interface_filament")); object->add_instance(); object->ensure_on_bed(); print.apply(model, config); } PrintObject &object() { return *print.objects_mutable().front(); } }; // Three nozzles and filaments, plus filament 4 mixed from filaments 2 and 3. The mixed // filament's own map entry names extruder 1, which prints neither component. A nonzero // `colliding_extruder` gets an exclusion volume covering the whole bed. DynamicPrintConfig mixed_filament_config(FilamentMapMode map_mode, int colliding_extruder = 0) { std::vector per_extruder(3); if (colliding_extruder > 0) per_extruder[colliding_extruder - 1] = "0..256;-100x-100,300x-100,300x300,-100x300"; DynamicPrintConfig config = Test::multifilament_config(4, { {"filament_is_mixed", "0,0,0,1"}, {"filament_mixed_components", ";;;2,3"}, {"filament_mixed_sublayer_ratios", ";;;0.5,0.5"}, {"filament_mixed_gradient", "0,0,0,0"}, {"filament_mixed_gradient_range", ";;;"}, {"filament_mixed_gradient_curve", ";;;"}, {"filament_mixed_gradient_per_part", "0,0,0,0"}, {"layer_change_gcode", "G92 E0\n"}, // validate() relative-E reset }); config.set_num_extruders(3); config.set_key_value("nozzle_diameter", new ConfigOptionFloats(3, 0.4)); config.set_key_value("extruder_offset", new ConfigOptionPoints(3, Vec2d::Zero())); config.set_key_value("extruder_printable_height", new ConfigOptionFloatsNullable(3, 256.0)); config.set_key_value("bed_exclude_volume_mode", new ConfigOptionEnum(BedExcludeVolumeMode::PerExtruder)); config.set_key_value("extruder_bed_exclude_volumes", new ConfigOptionStrings(per_extruder)); config.set_key_value("filament_map_mode", new ConfigOptionEnum(map_mode)); config.set_key_value("filament_map", new ConfigOptionInts{1, 2, 3, 1}); return config; } // Validate a cube that prints with the mixed filament 4 only. StringObjectException validate_mixed_filament_cube(FilamentMapMode map_mode, int colliding_extruder) { const std::vector> object_overrides { { { "extruder", 4 } } }; Print print; Model model; Test::init_print({ Test::cube(20.) }, print, model, mixed_filament_config(map_mode, colliding_extruder), &object_overrides); return print.validate(); } } // namespace TEST_CASE("Path checker ignores clear motions and motions outside the active Z slab", "[ExclusionVolume][GCode]") { const FullPrintConfig config = static_config(exclusion_config()); SECTION("clear XY") { auto checker = configured_checker(config); check_motion(checker, Vec3d(10.0, 10.0, 5.0), Vec3d(20.0, 20.0, 5.0)); CHECK_FALSE(checker.result().has_any_conflict); } SECTION("above volume") { auto checker = configured_checker(config); check_motion(checker, Vec3d(20.0, 50.0, 20.0), Vec3d(80.0, 50.0, 20.0)); CHECK_FALSE(checker.result().has_any_conflict); } SECTION("below volume") { auto checker = configured_checker(config); check_motion(checker, Vec3d(20.0, 50.0, -5.0), Vec3d(80.0, 50.0, -5.0)); CHECK_FALSE(checker.result().has_any_conflict); } } TEST_CASE("Legacy excluded areas do not constrain machine motion", "[ExclusionVolume][GCode][Compatibility]") { const FullPrintConfig config = static_config(legacy_exclusion_config()); const Polyline direct = travel({Vec2d(20.0, 50.0), Vec2d(80.0, 50.0)}); auto checker = configured_checker(config); check_motion(checker, Vec3d(20.0, 50.0, 5.0), Vec3d(80.0, 50.0, 5.0)); CHECK_FALSE(checker.result().has_any_conflict); ExclusionVolumeTravelAvoidance router; router.init(config, Vec3d::Zero()); const auto route = router.route(direct, 5.0, 5.0, 0); CHECK(route.status == ExclusionVolumeTravelAvoidance::Status::Unchanged); CHECK(route.detail == ExclusionVolumeTravelAvoidance::Detail::NoActiveObstacles); CHECK(route.path.points == direct.points); } TEST_CASE("G-code preview retains the bed area alongside collision volumes", "[ExclusionVolume][GCode][Compatibility][Regression]") { const auto mode = GENERATE(BedExcludeVolumeMode::Shared, BedExcludeVolumeMode::ToolheadOffset, BedExcludeVolumeMode::PerExtruder); DynamicPrintConfig config = exclusion_config(mode, "0..10;70x70,80x70,80x80,70x80", {"0..10;70x70,80x70,80x80,70x80"}); config.set_deserialize_strict("bed_exclude_area", "0x0,18x0,18x28,0x28"); ScopedTemporaryFile file(".gcode"); { std::ofstream output(file.string()); REQUIRE(output.is_open()); output << "; generated by OrcaSlicer\nG90\nM83\nG1 X30 Y30 Z0.2 F3000\n" << "; CONFIG_BLOCK_START\n"; for (const std::string &key : config.keys()) output << "; " << key << " = " << config.opt_serialize(key) << '\n'; output << "; CONFIG_BLOCK_END\n"; REQUIRE(output.good()); } ScopedBblPrinterMode printer_mode(false); GCodeProcessor processor; // Standalone preview loading uses the embedded DynamicPrintConfig, not // the streaming writer's PrintConfig overload. processor.process_file(file.string()); CHECK(processor.get_result().bed_exclude_area == config.option("bed_exclude_area")->values); } TEST_CASE("Collision volumes preserve the configured cutter travel points", "[ExclusionVolume][GCode][Compatibility][Regression]") { DynamicPrintConfig config = Test::multifilament_config(2, { {"enable_prime_tower", "0"}, {"gcode_flavor", "klipper"}, {"start_end_points", "20x-3,60x90"}, {"bed_exclude_area", "0x0,18x0,18x28,0x28"}, {"change_filament_gcode", ";CUTTER_PATH [travel_point_1_x],[travel_point_1_y];[travel_point_2_x],[travel_point_2_y];[travel_point_3_x],[travel_point_3_y]"}, }); const auto cutter_path = [](const DynamicPrintConfig &settings) { Model model; Print print; const std::vector> overrides{ {{"extruder", "1"}}, {{"extruder", "2"}}}; std::vector meshes; for (const double x : {30.0, 45.0}) { TriangleMesh mesh = make_cube(5.0, 5.0, 0.6); mesh.translate(Vec3f(float(x), 40.0f, 0.0f)); meshes.push_back(std::move(mesh)); } Test::init_print(std::move(meshes), print, model, settings, &overrides, false); const std::string gcode = Test::gcode(print); const size_t marker = gcode.find(";CUTTER_PATH "); REQUIRE(marker != std::string::npos); return gcode.substr(marker, gcode.find('\n', marker) - marker); }; const std::string expected = cutter_path(config); // The configured endpoint differs from the fallback (54, 245), so this // comparison cannot pass by silently disabling the cutter-area calculation. CHECK_THAT(expected, Catch::Matchers::ContainsSubstring("60")); CHECK_THAT(expected, Catch::Matchers::ContainsSubstring("90")); config.set_key_value("bed_exclude_volumes", new ConfigOptionString("0..10;80x80,90x80,90x90,80x90")); CHECK(cutter_path(config) == expected); } TEST_CASE("Path checker detects crossing inside and boundary motions", "[ExclusionVolume][GCode]") { const FullPrintConfig config = static_config(exclusion_config()); const auto [from, to] = GENERATE(table({ {Vec3d(20.0, 50.0, 5.0), Vec3d(80.0, 50.0, 5.0)}, {Vec3d(45.0, 45.0, 5.0), Vec3d(55.0, 55.0, 5.0)}, {Vec3d(20.0, 40.0, 5.0), Vec3d(80.0, 40.0, 5.0)}, {Vec3d(50.0, 20.0, -5.0), Vec3d(50.0, 80.0, 15.0)}, })); auto checker = configured_checker(config); check_motion(checker, from, to); CHECK(checker.result().has_any_conflict); REQUIRE(checker.result().first_hit.has_value()); CHECK(checker.result().first_hit->extruder_id == 0); } TEST_CASE("Path checker records motion classification and stable first-hit metadata", "[ExclusionVolume][GCode]") { const auto [type, travel_hit, extrusion_hit, other_hit] = GENERATE(table({ {ExclusionVolumeMotionType::Travel, true, false, false}, {ExclusionVolumeMotionType::Extrude, false, true, false}, {ExclusionVolumeMotionType::Other, false, false, true }, })); auto checker = configured_checker(static_config(exclusion_config())); check_motion(checker, Vec3d(20.0, 50.0, 5.0), Vec3d(80.0, 50.0, 5.0), 0, type, true, true, true, true, 101, 202); check_motion(checker, Vec3d(20.0, 50.0, 5.0), Vec3d(80.0, 50.0, 5.0), 0, type, true, true, true, true, 303, 404); const ExclusionVolumePathCheckResult &result = checker.result(); CHECK(result.has_travel_conflict == travel_hit); CHECK(result.has_extrusion_conflict == extrusion_hit); CHECK(result.has_other_motion_conflict == other_hit); REQUIRE(result.first_hit.has_value()); CHECK(result.first_hit->gcode_id == 101); CHECK(result.first_hit->move_id == 202); CHECK(result.first_hit->source_move_type == 7); } TEST_CASE("Path checker handles unknown coordinates conservatively without inventing XY", "[ExclusionVolume][GCode]") { const FullPrintConfig config = static_config(exclusion_config()); SECTION("unknown XY start is ignored") { auto checker = configured_checker(config); check_motion(checker, Vec3d(0.0, 0.0, 5.0), Vec3d(80.0, 50.0, 5.0), 0, ExclusionVolumeMotionType::Travel, false, true); CHECK_FALSE(checker.result().has_any_conflict); } SECTION("unknown Z checks every height and marks the result") { auto checker = configured_checker(config); check_motion(checker, Vec3d(20.0, 50.0, 100.0), Vec3d(80.0, 50.0, 100.0), 0, ExclusionVolumeMotionType::Travel, true, true, false, false); REQUIRE(checker.result().first_hit.has_value()); CHECK(checker.result().first_hit->used_unknown_z); } } TEST_CASE("Path checker selects the active nozzle and applies its XY offset", "[ExclusionVolume][GCode][MultiNozzle]") { DynamicPrintConfig dynamic = exclusion_config( BedExcludeVolumeMode::PerExtruder, "", {"", "0..10;40x40,60x40,60x60,40x60"}); dynamic.set_key_value("extruder_offset", new ConfigOptionPoints{Vec2d::Zero(), Vec2d(20.0, 0.0)}); const FullPrintConfig config = static_config(dynamic); SECTION("unrelated nozzle remains clear") { auto checker = configured_checker(config); check_motion(checker, Vec3d(20.0, 50.0, 5.0), Vec3d(80.0, 50.0, 5.0), 0); CHECK_FALSE(checker.result().has_any_conflict); } SECTION("active nozzle offset converts emitted coordinates into model space") { auto checker = configured_checker(config); check_motion(checker, Vec3d(0.0, 50.0, 5.0), Vec3d(50.0, 50.0, 5.0), 1); REQUIRE(checker.result().first_hit.has_value()); CHECK(checker.result().first_hit->extruder_id == 1); CHECK_THAT(checker.result().first_hit->from.x(), WithinAbs(20.0, 1e-9)); } SECTION("unknown active nozzle checks all physical nozzles") { auto checker = configured_checker(config); check_motion(checker, Vec3d(0.0, 50.0, 5.0), Vec3d(50.0, 50.0, 5.0), -1); REQUIRE(checker.result().first_hit.has_value()); CHECK(checker.result().first_hit->extruder_id == 1); } } TEST_CASE("Travel router leaves clear and vertically separated paths unchanged", "[ExclusionVolume][TravelRouting]") { ExclusionVolumeTravelAvoidance router; router.init(static_config(exclusion_config()), Vec3d::Zero()); const Polyline clear = travel({Vec2d(10.0, 10.0), Vec2d(90.0, 10.0)}); auto result = router.route(clear, 5.0, 5.0, 0); CHECK(result.status == ExclusionVolumeTravelAvoidance::Status::Unchanged); CHECK(result.detail == ExclusionVolumeTravelAvoidance::Detail::NoIntersection); CHECK(result.path.points == clear.points); const Polyline crossing = travel({Vec2d(20.0, 50.0), Vec2d(80.0, 50.0)}); result = router.route(crossing, 20.0, 20.0, 0); CHECK(result.status == ExclusionVolumeTravelAvoidance::Status::Unchanged); CHECK(result.detail == ExclusionVolumeTravelAvoidance::Detail::NoActiveObstacles); } TEST_CASE("Travel router produces a safe in-bed detour around an active volume", "[ExclusionVolume][TravelRouting]") { ExclusionVolumeTravelAvoidance router; router.init(static_config(exclusion_config()), Vec3d::Zero()); const Polyline direct = travel({Vec2d(20.0, 50.0), Vec2d(80.0, 50.0)}); const auto result = router.route(direct, 5.0, 5.0, 0); REQUIRE(result.status == ExclusionVolumeTravelAvoidance::Status::Rerouted); REQUIRE(result.path.points.size() > 2); CHECK(result.path.points.front() == direct.points.front()); CHECK(result.path.points.back() == direct.points.back()); check_path_avoids_rect(result.path, Rect{40.0, 40.0, 60.0, 60.0}); for (const Point &point : result.path.points) { const Vec2d position = to_mm(point); CHECK(position.x() >= 0.0); CHECK(position.x() <= 100.0); CHECK(position.y() >= 0.0); CHECK(position.y() <= 100.0); } } TEST_CASE("Travel router activates volumes touched by a sloped Z move", "[ExclusionVolume][TravelRouting]") { ExclusionVolumeTravelAvoidance router; router.init(static_config(exclusion_config()), Vec3d::Zero()); const Polyline direct = travel({Vec2d(20.0, 50.0), Vec2d(80.0, 50.0)}); const auto result = router.route(direct, -5.0, 15.0, 0); REQUIRE(result.status == ExclusionVolumeTravelAvoidance::Status::Rerouted); check_path_avoids_rect(result.path, Rect{40.0, 40.0, 60.0, 60.0}); } TEST_CASE("Travel router reports unsafe endpoints unknown tools and blocked beds", "[ExclusionVolume][TravelRouting]") { SECTION("endpoint inside") { ExclusionVolumeTravelAvoidance router; router.init(static_config(exclusion_config()), Vec3d::Zero()); const auto result = router.route(travel({Vec2d(50.0, 50.0), Vec2d(80.0, 50.0)}), 5.0, 5.0, 0); CHECK(result.status == ExclusionVolumeTravelAvoidance::Status::EndpointInside); CHECK(result.detail == ExclusionVolumeTravelAvoidance::Detail::EndpointInside); } SECTION("unknown tool") { ExclusionVolumeTravelAvoidance router; router.init(static_config(exclusion_config()), Vec3d::Zero()); const auto result = router.route(travel({Vec2d(20.0, 50.0), Vec2d(80.0, 50.0)}), 5.0, 5.0, -1); CHECK(result.status == ExclusionVolumeTravelAvoidance::Status::Failed); CHECK(result.detail == ExclusionVolumeTravelAvoidance::Detail::UnknownExtruder); } SECTION("obstacle separates the bed") { ExclusionVolumeTravelAvoidance router; router.init(static_config(exclusion_config( BedExcludeVolumeMode::Shared, "0..10;45x0,55x0,55x100,45x100")), Vec3d::Zero()); const auto result = router.route(travel({Vec2d(20.0, 50.0), Vec2d(80.0, 50.0)}), 5.0, 5.0, 0); CHECK(result.status == ExclusionVolumeTravelAvoidance::Status::Failed); } } TEST_CASE("Travel router uses only the active nozzle exclusion set", "[ExclusionVolume][TravelRouting][MultiNozzle]") { const FullPrintConfig config = static_config(exclusion_config( BedExcludeVolumeMode::PerExtruder, "", {"0..10;40x40,60x40,60x60,40x60", ""})); ExclusionVolumeTravelAvoidance router; router.init(config, Vec3d::Zero()); const Polyline direct = travel({Vec2d(20.0, 50.0), Vec2d(80.0, 50.0)}); CHECK(router.route(direct, 5.0, 5.0, 0).status == ExclusionVolumeTravelAvoidance::Status::Rerouted); const auto clear = router.route(direct, 5.0, 5.0, 1); CHECK(clear.status == ExclusionVolumeTravelAvoidance::Status::Unchanged); CHECK(clear.detail == ExclusionVolumeTravelAvoidance::Detail::NoActiveObstacles); } TEST_CASE("Travel router accepts firmware positions outside the printable boundary", "[ExclusionVolume][TravelRouting]") { ExclusionVolumeTravelAvoidance router; router.init(static_config(exclusion_config()), Vec3d::Zero()); SECTION("start outside") { const auto result = router.route( travel({Vec2d(-5.0, 50.0), Vec2d(80.0, 50.0)}), 5.0, 5.0, 0); REQUIRE(result.status == ExclusionVolumeTravelAvoidance::Status::Rerouted); check_path_avoids_rect(result.path, Rect{40.0, 40.0, 60.0, 60.0}); } SECTION("end outside") { const auto result = router.route( travel({Vec2d(20.0, 50.0), Vec2d(105.0, 50.0)}), 5.0, 5.0, 0); REQUIRE(result.status == ExclusionVolumeTravelAvoidance::Status::Rerouted); check_path_avoids_rect(result.path, Rect{40.0, 40.0, 60.0, 60.0}); } } TEST_CASE("G-code processor ignores the artificial first XY origin and checks subsequent travel", "[ExclusionVolume][GCodeProcessor]") { const FullPrintConfig config = static_config(exclusion_config()); SECTION("first explicit position has no invented start") { const ProcessorExclusionResult result = run_processor(config, "G90\nG1 X80 Y50 Z5 F6000\n"); CHECK(result.checked); CHECK_FALSE(result.conflict); } SECTION("next motion starts at the established position") { const ProcessorExclusionResult result = run_processor(config, "G90\nG1 X80 Y50 Z5 F6000\nG1 X20 Y50\n"); CHECK(result.conflict); CHECK(result.travel_conflict); } } TEST_CASE("G-code processor tracks absolute relative and reset coordinate state", "[ExclusionVolume][GCodeProcessor]") { const FullPrintConfig config = static_config(exclusion_config()); SECTION("relative move crosses the volume") { const ProcessorExclusionResult result = run_processor( config, "G90\nG1 X20 Y50 Z5\nG91\nG1 X60 Y0\n"); CHECK(result.conflict); } SECTION("G92 preserves a known transformed coordinate frame") { const ProcessorExclusionResult result = run_processor( config, "G90\nG1 X20 Y50 Z5\nG92 X0 Y0\nG1 X60 Y0\n"); CHECK(result.conflict); } SECTION("homing does not create a synthetic checked move") { const ProcessorExclusionResult result = run_processor(config, "G28\nG1 X80 Y50 Z5\n"); CHECK_FALSE(result.conflict); } SECTION("full or XY homing makes the XY start position unknown") { const std::string homing = GENERATE( std::string("G28"), std::string("G28 X"), std::string("G28 X Y"), std::string("G28 XY"), std::string("G28 X0 Y0"), std::string("G28 XYZ"), std::string("G28 W")); const ProcessorExclusionResult result = run_processor( config, "G90\nG1 X20 Y50 Z5\n" + homing + "\nG1 X80 Y50 Z5\n"); CHECK_FALSE(result.conflict); } SECTION("Z-only homing preserves the known XY position") { const std::string homing = GENERATE( std::string("G28 Z P0"), std::string("G28 Z Z_OFFSET -0.07")); const ProcessorExclusionResult result = run_processor( config, "G90\nG1 X20 Y50 Z5\n" + homing + "\nG1 X80 Y50 Z5\n"); CHECK(result.conflict); } } TEST_CASE("G-code processor classifies extrusion conflicts", "[ExclusionVolume][GCodeProcessor]") { const FullPrintConfig config = static_config(exclusion_config()); const ProcessorExclusionResult result = run_processor( config, "G90\nM83\nG1 X20 Y50 Z5\nG1 X80 Y50 E1\n"); CHECK(result.conflict); CHECK(result.extrusion_conflict); CHECK_FALSE(result.travel_conflict); } TEST_CASE("G-code processor changes the active physical nozzle at tool selection", "[ExclusionVolume][GCodeProcessor][MultiNozzle]") { const FullPrintConfig config = static_config(exclusion_config( BedExcludeVolumeMode::PerExtruder, "", {"", "0..10;40x40,60x40,60x60,40x60"})); const ProcessorExclusionResult first_tool = run_processor(config, "G90\nT0\nG1 X20 Y50 Z5\nG1 X80 Y50\n"); CHECK_FALSE(first_tool.conflict); const ProcessorExclusionResult second_tool = run_processor(config, "G90\nT1\nG1 X20 Y50 Z5\nG1 X80 Y50\n"); CHECK(second_tool.conflict); CHECK(second_tool.conflict_extruder_id == 1); } TEST_CASE("Brim paths are clipped only by exclusions active on the first layer", "[ExclusionVolume][Brim]") { const BrimRun active = generate_brim("0..1;32x35,38x35,38x65,32x65"); const BrimRun raised = generate_brim("10..20;32x35,38x35,38x65,32x65"); REQUIRE(active.total_length > 0.0); REQUIRE(raised.total_length > 0.0); CHECK_FALSE(active.intersects_test_region); CHECK(raised.intersects_test_region); CHECK(active.total_length < raised.total_length); } TEST_CASE("Brim paths remain clipped to the bed without collision volumes", "[ExclusionVolume][Brim][Regression]") { const BrimRun brim = generate_brim("", Vec2d(21.0, 5.0)); REQUIRE(brim.total_length > 0.0); CHECK_FALSE(brim.leaves_bed); } TEST_CASE("Support exclusion masks follow explicit and current-filament roles", "[ExclusionVolume][Support][MultiNozzle]") { SECTION("explicit support roles select one physical nozzle each") { SupportPrintFixture fixture(support_config(1, 2)); CHECK(support_exclusion_filaments(fixture.object(), false) == std::vector{0}); CHECK(support_exclusion_filaments(fixture.object(), true) == std::vector{1}); const auto base_masks = support_exclusion_areas_for_layers(fixture.object(), {{0.0, 1.0}}, false); const auto interface_masks = support_exclusion_areas_for_layers(fixture.object(), {{0.0, 1.0}}, true); REQUIRE(base_masks.size() == 1); REQUIRE(interface_masks.size() == 1); // PrintObject support geometry is centred around the object's local // origin; the plate-space regions above are shifted by (10, 10). CHECK(polygons_cover(base_masks.front(), -4.0, -4.0)); CHECK_FALSE(polygons_cover(base_masks.front(), 14.0, -4.0)); CHECK_FALSE(polygons_cover(interface_masks.front(), -4.0, -4.0)); CHECK(polygons_cover(interface_masks.front(), 14.0, -4.0)); } SECTION("current filament remains conservative across every used nozzle") { SupportPrintFixture fixture(support_config(0, 2)); std::vector filaments = support_exclusion_filaments(fixture.object(), false); std::sort(filaments.begin(), filaments.end()); CHECK(filaments == std::vector{0, 1}); const auto masks = support_exclusion_areas_for_layers(fixture.object(), {{0.0, 1.0}, {11.0, 12.0}}, false); REQUIRE(masks.size() == 2); CHECK(polygons_cover(masks[0], -4.0, -4.0)); CHECK(polygons_cover(masks[0], 14.0, -4.0)); CHECK(masks[1].empty()); } } TEST_CASE("Support trimming applies the correct nozzle mask to every polygon category", "[ExclusionVolume][Support][MultiNozzle]") { SupportPrintFixture fixture(support_config(1, 2)); const Polygon source = rectangle(-20.0, -20.0, 40.0, 20.0); SupportGeneratorLayer base; base.layer_type = SupporLayerType::Base; base.print_z = 1.0; base.height = 0.2; base.polygons = {source}; base.contact_polygons = std::make_unique(Polygons{source}); base.overhang_polygons = std::make_unique(Polygons{source}); base.enforcer_polygons = std::make_unique(Polygons{source}); SupportGeneratorLayer interface; interface.layer_type = SupporLayerType::TopInterface; interface.print_z = 1.0; interface.height = 0.2; interface.polygons = {source}; const SupportGeneratorLayersPtr none; const SupportGeneratorLayersPtr base_layers{&base}; const SupportGeneratorLayersPtr interface_layers{&interface}; trim_support_layers_by_exclusion_volumes( fixture.object(), none, none, none, base_layers, interface_layers, none); // Base support uses filament 1 and interface support uses filament 2. CHECK_FALSE(polygons_cover(base.polygons, -4.0, -4.0)); CHECK(polygons_cover(base.polygons, 14.0, -4.0)); CHECK(polygons_cover(interface.polygons, -4.0, -4.0)); CHECK_FALSE(polygons_cover(interface.polygons, 14.0, -4.0)); // The same sanitization is mandatory for the auxiliary support categories. REQUIRE(base.contact_polygons != nullptr); REQUIRE(base.overhang_polygons != nullptr); REQUIRE(base.enforcer_polygons != nullptr); CHECK_FALSE(polygons_cover(*base.contact_polygons, -4.0, -4.0)); CHECK_FALSE(polygons_cover(*base.overhang_polygons, -4.0, -4.0)); CHECK_FALSE(polygons_cover(*base.enforcer_polygons, -4.0, -4.0)); } TEST_CASE("A mixed filament's exclusion extruders are its component filaments' extruders", "[ExclusionVolume][MultiNozzle][MixedFilament]") { const FilamentMapMode map_mode = GENERATE(fmmAutoForFlush, fmmManual); CAPTURE(map_mode); Print print; Model model; Test::init_print({ Test::cube(20.) }, print, model, mixed_filament_config(map_mode)); // Filament 4 (id 3) mixes filaments 2 and 3, which print on extruders 2 and 3 (ids 1 and 2). CHECK(bed_exclusion_physical_extruders(print, {3}, 3, 0) == std::vector{1, 2}); } TEST_CASE("Print validation accepts a mixed filament when only an extruder printing neither component collides", "[ExclusionVolume][Print][MultiNozzle][MixedFilament]") { const FilamentMapMode map_mode = GENERATE(fmmAutoForFlush, fmmManual); CAPTURE(map_mode); // Extruder 1 prints neither filament 2 nor filament 3. CHECK(validate_mixed_filament_cube(map_mode, 1).string.empty()); } TEST_CASE("Print validation rejects a mixed filament when the extruder of one of its components collides", "[ExclusionVolume][Print][MultiNozzle][MixedFilament]") { const FilamentMapMode map_mode = GENERATE(fmmAutoForFlush, fmmManual); const int colliding_extruder = GENERATE(2, 3); CAPTURE(map_mode, colliding_extruder); // Filaments 2 and 3, the components of filament 4, print on extruders 2 and 3. CHECK_THAT(validate_mixed_filament_cube(map_mode, colliding_extruder).string, Catch::Matchers::ContainsSubstring("extruder " + std::to_string(colliding_extruder))); }