M8c: interference detection

check_interference(min_volume) reports every pair of solid bodies whose
intersection encloses more than min_volume, as {body_a, body_b, volume}. It
reports only — no geometry is mutated, so calling it cannot disturb mates or
placements. Read-only at the MCP surface too: no checkpoint, no recompute.

Sheet bodies are skipped up front: an intersection involving one encloses no
volume, so the boolean would be wasted work. Bodies that merely touch share a
face and enclose nothing, so face-to-face contact is not an interference.

A boolean that fails on one pair must not lose the report for every other pair,
so each pair is guarded — and OCCT raises Standard_Failure, which is not a
std::exception and would otherwise escape.

No new serialized fields, no recipe bump: this reads `bodies`, which is
recompute output and was never serialized.

No separate MCP listing for instances and mates: describe_scene already emits
the feature tree, and Mate has rendered there correctly since M8a fixed
feature_type_name.

Tests assert the exact overlap volume (20*20*4 = 1600 mm^3), both negative cases
(clearly apart, and exact face contact), that sheets are skipped, that the
min_volume gate silences a real overlap, and that a clash created by a Fastened
mate is detected — which ties the M8b placement work to this report.

Suite 139 cases / 1960 assertions green. McpControl.cpp is reviewed but not
compiled by kernel-test.sh, which builds only libslic3r_tests.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Tommaso Bianchi
2026-07-25 12:15:30 +02:00
co-authored by Claude Opus 5
parent 6a0031c9e5
commit 30d54f0074
4 changed files with 178 additions and 0 deletions
+34
View File
@@ -3359,6 +3359,40 @@ int CadDocument::add_surface_fill(int sketch_ref, const std::string& name)
return int(features.size()) - 1;
}
std::vector<CadDocument::Interference> CadDocument::check_interference(double min_volume) const
{
std::vector<Interference> out;
const int n = int(bodies.size());
for (int i = 0; i < n; ++i) {
if (bodies[i].shape.IsNull() || is_sheet_shape(bodies[i].shape)) continue;
for (int j = i + 1; j < n; ++j) {
if (bodies[j].shape.IsNull() || is_sheet_shape(bodies[j].shape)) continue;
double v = 0;
// A boolean that blows up on one pair must not lose the report for the others,
// and OCCT signals those as Standard_Failure, which is NOT a std::exception.
try {
BRepAlgoAPI_Common common(bodies[i].shape, bodies[j].shape);
common.Build();
if (!common.IsDone()) continue;
const TopoDS_Shape s = common.Shape();
if (s.IsNull()) continue;
GProp_GProps props;
BRepGProp::VolumeProperties(s, props);
v = std::abs(props.Mass());
} catch (const Standard_Failure&) {
continue;
}
// Bodies that merely touch share a face and enclose no volume, so the
// threshold is what separates contact from interference.
if (v > min_volume) out.push_back({i, j, v});
}
}
return out;
}
// ponytail: derived from the OCCT shape type; no stored flag, bodies aren't serialized anyway.
bool CadDocument::is_sheet_shape(const TopoDS_Shape& s)
{
+7
View File
@@ -575,6 +575,13 @@ public:
GeometryEngine::MassProps body_mass_properties(int body_index) const;
// One overlapping pair of solid bodies. Indices are into `bodies`, a_ < b_.
struct Interference { int body_a{-1}; int body_b{-1}; double volume{0}; };
// Every pair of solid bodies whose intersection encloses more than min_volume (mm^3).
// Reports only — mutates nothing, so mates and placements are unaffected by calling it.
// Sheet bodies are skipped: an intersection involving one encloses no volume.
std::vector<Interference> check_interference(double min_volume = 1e-6) const;
// ponytail: derived from the OCCT shape type; no stored flag, bodies aren't serialized anyway.
static bool is_sheet_shape(const TopoDS_Shape& s); // true if TopExp finds no TopAbs_SOLID
+18
View File
@@ -339,6 +339,10 @@ json describe_tools()
json{{"name", "angle"}, {"type", "number"}, {"unit", "deg"}, {"default", 0}},
json{{"name", "flip"}, {"type", "boolean"}, {"default", false}},
})}},
json{{"name", "check_interference"}, {"summary", "Solid bodies that overlap, as {body_a, body_b, volume}. Read-only; bodies that merely touch enclose no volume and are not reported."},
{"params", json::array({
json{{"name", "min_volume"}, {"type", "number"}, {"unit", "mm^3"}, {"default", 1e-6}, {"description", "overlap volume above which a pair counts as interfering"}},
})}},
json{{"name", "query_topology"}, {"summary", "Measured faces (centroid/normal/cylinder) and edges (length/circle) of a body."},
{"params", json::array({
json{{"name", "body"}, {"type", "integer"}, {"default", 0}},
@@ -1344,6 +1348,19 @@ json action_mate(DesignPanel* panel, const json& params)
return json{{"ok", ok}, {"mate_index", idx}, {"bodies", int(doc.bodies.size())}, {"error", doc.error}};
}
json action_check_interference(DesignPanel* panel, const json& params)
{
const double min_volume = params.value("min_volume", 1e-6);
CadDocument& doc = panel->mcp_doc();
// Read-only: no checkpoint(), no recompute(), no mcp_after_change().
const auto hits = doc.check_interference(min_volume);
json arr = json::array();
for (const auto& h : hits) {
arr.push_back(json{{"body_a", h.body_a}, {"body_b", h.body_b}, {"volume", h.volume}});
}
return json{{"ok", true}, {"count", int(hits.size())}, {"interferences", arr}};
}
json action_set_variable(DesignPanel* panel, const json& params)
{
if (!params.contains("name")) throw std::runtime_error("set_variable needs 'name'");
@@ -1429,6 +1446,7 @@ std::string handle_on_main(const std::string& method, const json& params, const
if (method == "surface_loft") return rpc_result(id, action_surface_loft(panel, params));
if (method == "surface_fill") return rpc_result(id, action_surface_fill(panel, params));
if (method == "mate") return rpc_result(id, action_mate(panel, params));
if (method == "check_interference") return rpc_result(id, action_check_interference(panel, params));
return rpc_error(id, -32601, "Unknown method: " + method);
} catch (const Standard_Failure& ex) { // OCCT errors are NOT std::exception
return rpc_error(id, -32000, std::string("OCCT: ") + (ex.GetMessageString() ? ex.GetMessageString() : "failure"));