Validate excluded bed area (#17209)

This commit is contained in:
Kiss Lorand authored and GitHub committed 2026-11-06 22:39:38 -03:00
1 parent bac7112046
commit c01ac8ff44
5 files changed
+80 -4

No files matched your search

+33 -4
View File
@@ -13853,6 +13853,37 @@ Points get_bed_shape(const PrintConfig &cfg, bool use_share)
Points get_bed_shape(const SLAPrinterConfig &cfg) { return to_points(make_counter_clockwise(cfg.printable_area.values)); }
bool is_valid_bed_exclude_area(const Pointfs &points)
{
if (points.size() < 3)
return false;
for (const Vec2d &point : points) {
if (!std::isfinite(point.x()) || !std::isfinite(point.y()))
return false;
}
const Polygon polygon = Polygon::new_scale(points);
if (has_duplicate_points(polygon))
return false;
const Points &vertices = polygon.points;
if (polygon.area() == 0.0)
return false;
const size_t count = vertices.size();
for (size_t idx = 0; idx + 2 < count; ++idx) {
const size_t end = idx == 0 ? count - 1 : count;
for (size_t other = idx + 2; other < end; ++other) {
if (Geometry::segments_intersect(
vertices[idx], vertices[idx + 1], vertices[other], vertices[(other + 1) % count]))
return false;
}
}
return true;
}
namespace {
static bool looks_like_bed_exclusion_volume_syntax(const std::string &value)
@@ -13928,15 +13959,13 @@ static std::optional<Polygon> parse_exclude_polygon_points(const std::string &po
return std::nullopt;
points.emplace_back(x, y);
}
if (points.size() < 3)
if (!is_valid_bed_exclude_area(points))
return std::nullopt;
Polygon polygon;
polygon.points.reserve(points.size());
for (const Vec2d &pt : points)
polygon.points.emplace_back(scale_(pt.x()), scale_(pt.y()));
if (polygon.area() == 0.0)
return std::nullopt;
polygon.make_counter_clockwise();
return polygon;
}
@@ -13946,7 +13975,7 @@ static void append_legacy_bed_exclude_regions(
const Pointfs &legacy_points,
const double printable_height)
{
if (legacy_points.size() < 3)
if (!is_valid_bed_exclude_area(legacy_points))
return;
Polygon polygon;
+2
View File
@@ -2576,6 +2576,8 @@ int bed_exclusion_extruder_for_filament(size_t filament_id, const std::vector<in
FilamentMapMode map_mode, bool is_bambu, bool automatic_map_resolved,
size_t extruder_count);
bool is_bed_exclusion_volume_syntax(const std::string &value);
// Bed exclusion areas must be simple polygons with non-zero area.
bool is_valid_bed_exclude_area(const Pointfs &points);
bool is_valid_bed_exclude_volumes_string(const std::string &value, double printable_height);
// Add shared definitions without replacing existing volumes or changing the
// configured multi-nozzle mode. In individual mode the new definitions are
+9
View File
@@ -799,6 +799,15 @@ void Field::get_value_by_opt_type(wxString& str, const bool check_value/* = true
}
}
if (m_opt_id == "bed_exclude_area" && out_values.size() > 1 && !is_valid_bed_exclude_area(out_values)) {
wxString text_value;
if (const auto *previous_values = boost::any_cast<std::vector<Vec2d>>(&m_value); previous_values != nullptr)
text_value = from_u8(ConfigOptionPoints(*previous_values).serialize());
set_value(text_value, true);
show_error(m_parent, _L("The excluded bed area is invalid. It must be a simple, non-self-intersecting polygon."));
break;
}
m_value = out_values;
break; }
+11
View File
@@ -8978,6 +8978,7 @@ std::vector<size_t> Plater::priv::load_files(const std::vector<fs::path>& input_
bool dlg_cont = true;
bool is_user_cancel = false;
bool translate_old = false;
bool invalid_bed_exclude_area = false;
int current_width = 0, current_depth = 0;
double current_height = 0;
@@ -9679,6 +9680,12 @@ std::vector<size_t> Plater::priv::load_files(const std::vector<fs::path>& input_
config.apply(static_cast<const ConfigBase &>(FullPrintConfig::defaults()));
// and place the loaded config over the base.
config += std::move(config_loaded);
if (ConfigOptionPoints *bed_exclude_area = config.option<ConfigOptionPoints>("bed_exclude_area");
bed_exclude_area != nullptr && bed_exclude_area->values.size() > 1 &&
!is_valid_bed_exclude_area(bed_exclude_area->values)) {
bed_exclude_area->values.clear();
invalid_bed_exclude_area = true;
}
std::map<std::string, std::string> validity = config.validate();
if (!validity.empty()) {
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << ":" << __LINE__ << " " << boost::format("Param values in 3mf error: ");
@@ -10568,6 +10575,10 @@ std::vector<size_t> Plater::priv::load_files(const std::vector<fs::path>& input_
}
q->schedule_background_process(true);
q->mark_plate_toolbar_image_dirty();
if (invalid_bed_exclude_area) {
GUI::show_error(q, _L("The excluded bed area is invalid. It must be a simple, non-self-intersecting polygon.") +
"\n" + _L("The invalid area has been removed, so please define it again using a valid polygon."));
}
return obj_idxs;
}
@@ -102,6 +102,31 @@ TEST_CASE("Legacy exclusion polygons keep their point representation", "[Exclusi
CHECK_FALSE(is_bed_exclusion_volume_syntax(option.serialize()));
}
TEST_CASE("Excluded bed areas require simple polygons", "[ExclusionVolume][PrintConfig][Regression]")
{
ConfigOptionPoints self_intersecting;
REQUIRE(self_intersecting.deserialize("0x0,6x0,1x220,6x220"));
CHECK_FALSE(is_valid_bed_exclude_area(self_intersecting.values));
DynamicPrintConfig invalid_config = DynamicPrintConfig::full_print_config();
invalid_config.set_key_value("bed_exclude_area", self_intersecting.clone());
CHECK(get_bed_excluded_regions(invalid_config).empty());
ConfigOptionPoints zero_area;
REQUIRE(zero_area.deserialize("0x0,6x0,0x220,6x220"));
CHECK_FALSE(is_valid_bed_exclude_area(zero_area.values));
ConfigOptionPoints valid;
REQUIRE(valid.deserialize("0x0,6x0,6x220,0x220"));
CHECK(is_valid_bed_exclude_area(valid.values));
DynamicPrintConfig valid_config = DynamicPrintConfig::full_print_config();
valid_config.set_key_value("bed_exclude_area", valid.clone());
const std::vector<BedExcludeRegion> regions = get_bed_excluded_regions(valid_config);
REQUIRE(regions.size() == 1);
CHECK(regions.front().polygon.points.size() == valid.values.size());
}
TEST_CASE("Belt material keep-outs are projected once without projecting collision volumes", "[ExclusionVolume][PrintConfig][Belt][Regression]")
{
const auto [belt, axis, angle, scale_x, scale_y] = GENERATE(table<bool, BeltRotationAxis, double, double, double>({