M3c: 2D bridging curve — cubic-Bezier G1 connector between sketch endpoints

Adds SketchEngine::make_bridge (4-pole cubic Bezier, G1-tangent to Line/Arc
endpoints, straight-line fallback for other types) emitted as the existing
BSpline SketchEntity — no new geometry type, no serialized-field change, golden
recipe fixture untouched. CadDocument::add_bridge appends it (non-parametric,
index-validated, throws on bad refs). MCP `bridge` method mirrors action_project.
4 new [CadDocument][bridge] tests; full kernel suite green (76 cases, 1280 asserts).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BVzKmX6Y1aEteit1HTXG4Q
This commit is contained in:
Tommaso Bianchi
2026-07-25 01:59:47 +02:00
co-authored by Claude Opus 4.8
parent 62d39fba27
commit 65caa2ea6e
6 changed files with 240 additions and 0 deletions
+15
View File
@@ -800,6 +800,21 @@ int CadDocument::add_project_edges(int source_body, const std::vector<int>& edge
return int(features.size()) - 1;
}
int CadDocument::add_bridge(int sketch_ref, int ent_a, int end_a, int ent_b, int end_b,
const std::string& name)
{
(void)name;
if (sketch_ref < 0 || sketch_ref >= int(features.size())
|| features[sketch_ref].type != CadFeatureType::Sketch)
throw std::runtime_error("bridge: sketch_ref must refer to a Sketch feature");
auto& ents = features[sketch_ref].entities;
if (ent_a < 0 || ent_a >= int(ents.size()) || ent_b < 0 || ent_b >= int(ents.size()))
throw std::runtime_error("bridge: entity index out of range in sketch");
SketchEntity br = SketchEngine::make_bridge(ents[ent_a], end_a, ents[ent_b], end_b);
ents.push_back(br);
return int(ents.size()) - 1;
}
int CadDocument::add_plane(int base, double offset, double angle_tilt, int axis,
const std::string& name)
{
+6
View File
@@ -378,6 +378,12 @@ public:
// entities are (re)derived on every recompute.
int add_project_edges(int source_body, const std::vector<int>& edge_ids, int face,
const SketchPlane& plane, const std::string& name);
// Append a bridging BSpline entity connecting endpoint `end_a` of entity `ent_a` to
// endpoint `end_b` of entity `ent_b`, both within sketch feature `sketch_ref`. Returns
// the new entity's index within that sketch's entities vector. Non-parametric: computed
// once from the current endpoints (does not auto-follow later solver moves).
int add_bridge(int sketch_ref, int ent_a, int end_a, int ent_b, int end_b,
const std::string& name);
// Solve features[index]'s sketch constraints, writing solved coordinates back
// into its profile.points. No-op (returns true) if the feature has no
// constraints. Returns false if index is invalid / not a Sketch / solve fails.
+53
View File
@@ -1372,4 +1372,57 @@ bool SketchEngine::extend_entity(SketchEntity& e, const std::vector<SketchEntity
return true;
}
// Bridge: cubic Bézier with G1 continuity at both ends.
// Poles = {Pa, Pa + Ta*d/3, Pb - Tb*d/3, Pb}, where d = |Pb - Pa|.
SketchEntity SketchEngine::make_bridge(const SketchEntity& a, int a_end,
const SketchEntity& b, int b_end)
{
auto endpoint = [](const SketchEntity& e, int end) -> Vec2d {
return end == 0 ? e.p0 : e.p1;
};
auto tangent = [](const SketchEntity& e, int end, const Vec2d& fallback_dir) -> Vec2d {
switch (e.type) {
case SketchEntity::Type::Line: {
Vec2d dir = end == 1 ? e.p1 - e.p0 : e.p0 - e.p1;
double len = dir.norm();
if (len < 1e-12) return fallback_dir;
return dir / len;
}
case SketchEntity::Type::Arc: {
double theta = end == 1 ? e.end_angle : e.start_angle;
// Tangent to the circle at angle theta, CCW: (-sin θ, cos θ).
// At end=1 (end_angle), the outward direction is along the sweep direction.
// At end=0 (start_angle), outward is opposite the sweep direction.
double sweep = e.end_angle - e.start_angle;
int sign = end == 1 ? (sweep >= 0 ? 1 : -1) : (sweep >= 0 ? -1 : 1);
Vec2d t(-std::sin(theta), std::cos(theta));
return t * double(sign);
}
default:
// ponytail: straight-ish bridge for unsupported entity types.
return fallback_dir;
}
};
const Vec2d Pa = endpoint(a, a_end);
const Vec2d Pb = endpoint(b, b_end);
const double d = (Pb - Pa).norm();
// Fallback tangent direction: point toward the other endpoint.
Vec2d fallback = d < 1e-9 ? Vec2d(1, 0) : (Pb - Pa) / d;
Vec2d Ta = tangent(a, a_end, fallback);
Vec2d Tb = tangent(b, b_end, fallback * -1.0);
const double k = std::max(d, 1e-9) / 3.0;
SketchEntity e;
e.type = SketchEntity::Type::BSpline;
e.construction = false;
e.ctrl = { Pa, Pa + Ta * k, Pb - Tb * k, Pb };
e.p0 = e.ctrl.front();
e.p1 = e.ctrl.back();
return e;
}
} // namespace Slic3r
+6
View File
@@ -250,6 +250,12 @@ public:
static bool extend_entity(SketchEntity& e, const std::vector<SketchEntity>& others,
const Vec2d& pick);
// Build a cubic-Bezier G1 bridge (as a BSpline entity, 4 poles) connecting endpoint
// `a_end` of `a` to endpoint `b_end` of `b` (0 = start/p0 side, 1 = end/p1 side).
// Tangent-continuous with both entities where the endpoint tangent is defined.
static SketchEntity make_bridge(const SketchEntity& a, int a_end,
const SketchEntity& b, int b_end);
};
} // namespace Slic3r
+29
View File
@@ -256,6 +256,14 @@ json describe_tools()
json{{"name", "edges"}, {"type", "array"}, {"default", json::array()}, {"description", "global edge ids to project; empty => project the face"}},
json{{"name", "plane"}, {"type", "string"}, {"default", "XY"}, {"description", "target sketch plane (XY/XZ/YZ)"}},
})}},
json{{"name", "bridge"}, {"summary", "Build a cubic-Bezier G1 bridge (BSpline) between two sketch-entity endpoints within a sketch feature."},
{"params", json::array({
json{{"name", "sketch"}, {"type", "integer"}, {"description", "sketch feature index"}},
json{{"name", "ent_a"}, {"type", "integer"}, {"description", "first entity index within the sketch"}},
json{{"name", "end_a"}, {"type", "integer"}, {"enum", json::array({0, 1})}, {"default", 1}, {"description", "0 = start/p0 side, 1 = end/p1 side"}},
json{{"name", "ent_b"}, {"type", "integer"}, {"description", "second entity index within the sketch"}},
json{{"name", "end_b"}, {"type", "integer"}, {"enum", json::array({0, 1})}, {"default", 0}, {"description", "0 = start/p0 side, 1 = end/p1 side"}},
})}},
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}},
@@ -958,6 +966,26 @@ json action_project(DesignPanel* panel, const json& params)
return json{{"ok", ok}, {"project_index", idx}, {"bodies", int(doc.bodies.size())}, {"error", doc.error}};
}
json action_bridge(DesignPanel* panel, const json& params)
{
if (!params.contains("sketch")) throw std::runtime_error("bridge needs 'sketch' (feature index)");
if (!params.contains("ent_a")) throw std::runtime_error("bridge needs 'ent_a' (entity index)");
if (!params.contains("ent_b")) throw std::runtime_error("bridge needs 'ent_b' (entity index)");
int sketch = params["sketch"].get<int>();
int ent_a = params["ent_a"].get<int>();
int ent_b = params["ent_b"].get<int>();
int end_a = params.value("end_a", 1);
int end_b = params.value("end_b", 0);
CadDocument& doc = panel->mcp_doc();
doc.checkpoint();
int ei = doc.add_bridge(sketch, ent_a, end_a, ent_b, end_b, "Bridge");
bool ok = doc.recompute();
if (!ok) doc.undo();
panel->mcp_after_change();
return json{{"ok", ok}, {"sketch_index", sketch}, {"entity_index", ei},
{"bodies", int(doc.bodies.size())}, {"error", doc.error}};
}
json action_axis(DesignPanel* panel, const json& params)
{
CadDocument& doc = panel->mcp_doc();
@@ -1060,6 +1088,7 @@ std::string handle_on_main(const std::string& method, const json& params, const
if (method == "thicken") return rpc_result(id, action_thicken(panel, params));
if (method == "split") return rpc_result(id, action_split(panel, params));
if (method == "project") return rpc_result(id, action_project(panel, params));
if (method == "bridge") return rpc_result(id, action_bridge(panel, params));
if (method == "axis") return rpc_result(id, action_axis(panel, params));
if (method == "coordsys") return rpc_result(id, action_coordsys(panel, params));
if (method == "helix") return rpc_result(id, action_helix(panel, params));