Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
Blend clean-up E2a: strips and corners next to cylinders and cones
Claude30a28d016ca4
diff --git a/Aetheris.Kernel.Firmament.Tests/Experiments/BlendE2Census.cs b/Aetheris.Kernel.Firmament.Tests/Experiments/BlendE2Census.csnew file mode 100644index 00000000..858f5d2a--- /dev/null+++ b/Aetheris.Kernel.Firmament.Tests/Experiments/BlendE2Census.cs@@ -0,0 +1,112 @@+using Aetheris.Kernel.Core.Brep;+using Aetheris.Kernel.Core.Geometry;+using Aetheris.Kernel.Core.Math;+using Aetheris.Kernel.Core.Step242;+using Aetheris.Kernel.Core.Topology;++namespace Aetheris.Kernel.Firmament.Tests.Experiments;++/// <summary>E2 census: what the non-planar refusals of E3 are, geometrically. Gated, writes e2-census.txt.</summary>+public sealed class BlendE2Census+{+ [Fact]+ public void E2_Census()+ {+ if (Environment.GetEnvironmentVariable("AETHERIS_EXPERIMENTS") != "1") return;+ var directory = new DirectoryInfo(AppContext.BaseDirectory);+ while (directory is not null && !File.Exists(Path.Combine(directory.FullName, "Aetheris.slnx"))) directory = directory.Parent;+ var root = directory!.FullName;+ var pairs = new Dictionary<string, int>(); // strip faces by anchor relation+ var regionWorst = new Dictionary<string, int>(); // region faces by the set of relations they need+ var moved = new Dictionary<string, int>(); // kept edges that would move, by curve kind+ var lines = new List<string>();+ foreach (var path in Directory.GetFiles(Path.Combine(root, "testdata/step242/nist"), "*.stp", SearchOption.AllDirectories).Order()+ .Concat(Directory.GetFiles(Path.Combine(root, "testdata/step242/community"), "*.step", SearchOption.AllDirectories)))+ {+ if (path.Contains("-tg")) continue;+ var import = Step242Importer.ImportBody(File.ReadAllText(path));+ if (!import.IsSuccess) continue;+ var body = import.Value;+ var probe = BlendRegionProbe.Run(Path.GetFileName(path), body);+ var anchors = BlendNetworkRemovalTests.Anchors(probe);+ foreach (var region in probe.Regions)+ {+ if (region.FaceBlockers.Any(b => b is not null)) continue;+ var faces = region.Faces.Select(f => new FaceId(f)).ToHashSet();+ var removed = BlendNetworkRemoval.Remove(body, faces, anchors);+ if (removed.Body is not null || removed.Refusal?.StartsWith("blend-removal-primary-not-planar") != true) continue;+ var needs = new SortedSet<string>();+ foreach (var f in faces)+ {+ var a = anchors.GetValueOrDefault(f) ?? [];+ var key = a.Length == 2 ? $"{Kind(body, f)} between {Relation(body, a[0], a[1])}" : $"joiner {Kind(body, f)} ({a.Length} anchors)";+ pairs[key] = pairs.GetValueOrDefault(key) + 1;+ needs.Add(a.Length == 2 ? Relation(body, a[0], a[1]) : "joiner");+ }+ // Joiner corners among curved faces: which surfaces meet at each joiner's neighbourhood.+ foreach (var f in faces.Where(f => (anchors.GetValueOrDefault(f) ?? []).Length != 2))+ {+ var around = Neighbours(body, f).Where(n => !faces.Contains(n)).Select(n => Kind(body, n)).Order().ToArray();+ needs.Add("corner[" + string.Join('+', around.Distinct()) + "]");+ }+ foreach (var kind in MovedEdgeKinds(body, faces)) { moved[kind] = moved.GetValueOrDefault(kind) + 1; needs.Add("moves " + kind); }+ var signature = string.Join(" | ", needs);+ regionWorst[signature] = regionWorst.GetValueOrDefault(signature) + faces.Count;+ lines.Add($" {Path.GetFileNameWithoutExtension(path)} R{region.Id} ({faces.Count}): {signature}");+ }+ }+ lines.Insert(0, "STRIP FACES BY ANCHOR RELATION");+ lines.InsertRange(1, pairs.OrderByDescending(p => p.Value).Select(p => $"{p.Value,5} {p.Key}"));+ var at = pairs.Count + 1;+ lines.Insert(at++, "KEPT EDGES THAT MOVE, BY CURVE");+ lines.InsertRange(at, moved.OrderByDescending(p => p.Value).Select(p => $"{p.Value,5} {p.Key}"));+ at += moved.Count;+ lines.Insert(at++, "REGION FACES BY WHAT THEY NEED");+ lines.InsertRange(at, regionWorst.OrderByDescending(p => p.Value).Select(p => $"{p.Value,5} {p.Key}"));+ File.WriteAllLines(Path.Combine(root, "artifacts/local/blend-probe/e2-census.txt"), lines);+ }++ private static string Kind(BrepBody body, FaceId f) => body.TryGetFaceSurfaceGeometry(f, out var s) ? s!.Kind.ToString() : "?";++ internal static string Relation(BrepBody body, FaceId a, FaceId b)+ {+ body.TryGetFaceSurfaceGeometry(a, out var sa); body.TryGetFaceSurfaceGeometry(b, out var sb);+ if (Rank(sa!) > Rank(sb!)) (sa, sb) = (sb, sa);+ static (Point3D P, Vector3D D)? Axis(SurfaceGeometry s) => s.Cylinder is { } c ? (c.Origin, c.Axis.ToVector())+ : s.Cone is { } k ? (k.Apex, k.Axis.ToVector()) : s.Torus is { } t ? (t.Center, t.Axis.ToVector())+ : s.Sphere is { } p ? (p.Center, Vector3D.Zero) : null;+ var name = $"{sa!.Kind}-{sb!.Kind}";+ if (sa.Plane is { } plane && Axis(sb) is { } ax)+ {+ if (sb.Sphere is not null) return name;+ var c = Math.Abs(plane.Normal.ToVector().Dot(ax.D));+ return name + (c > 1 - 1e-9 ? " (plane across axis: circle)" : c < 1e-9 ? " (plane along axis: lines)" : " (oblique)");+ }+ if (Axis(sa) is { } x && Axis(sb) is { } y)+ {+ if (x.D.Length < 1e-12 || y.D.Length < 1e-12) return name;+ var parallel = Math.Abs(Math.Abs(x.D.Dot(y.D)) - 1) < 1e-9;+ var offset = (y.P - x.P) - x.D * (y.P - x.P).Dot(x.D);+ return name + (parallel && offset.Length < 1e-6 ? " (coaxial: circle)" : parallel ? " (parallel axes)" : " (skew axes)");+ }+ return name;+ }++ private static int Rank(SurfaceGeometry s) => s.Kind switch { SurfaceGeometryKind.Plane => 0, SurfaceGeometryKind.Cylinder => 1, SurfaceGeometryKind.Cone => 2, _ => 3 };++ private static IEnumerable<FaceId> Neighbours(BrepBody body, FaceId f)+ {+ var t = body.Topology;+ var edges = t.GetFace(f).LoopIds.SelectMany(l => t.GetLoop(l).CoedgeIds).Select(c => t.GetCoedge(c).EdgeId).ToHashSet();+ return t.Faces.Where(o => o.Id != f && o.LoopIds.SelectMany(l => t.GetLoop(l).CoedgeIds).Any(c => edges.Contains(t.GetCoedge(c).EdgeId))).Select(o => o.Id);+ }++ private static IEnumerable<string> MovedEdgeKinds(BrepBody body, HashSet<FaceId> faces)+ {+ var t = body.Topology;+ var touched = faces.SelectMany(f => t.GetFace(f).LoopIds.SelectMany(l => t.GetLoop(l).CoedgeIds)).Select(c => t.GetCoedge(c).EdgeId).ToHashSet();+ var vertices = touched.Select(t.GetEdge).SelectMany(e => new[] { e.StartVertexId, e.EndVertexId }).ToHashSet();+ foreach (var e in t.Edges.Where(e => !touched.Contains(e.Id) && (vertices.Contains(e.StartVertexId) || vertices.Contains(e.EndVertexId))))+ yield return body.Geometry.GetCurve(body.Bindings.GetEdgeBinding(e.Id).CurveGeometryId).Kind.ToString();+ }+}diff --git a/Aetheris.Kernel.Firmament.Tests/Experiments/BlendNetworkRemoval.cs b/Aetheris.Kernel.Firmament.Tests/Experiments/BlendNetworkRemoval.csindex a3633e55..144a98bc 100644--- a/Aetheris.Kernel.Firmament.Tests/Experiments/BlendNetworkRemoval.cs+++ b/Aetheris.Kernel.Firmament.Tests/Experiments/BlendNetworkRemoval.cs@@ -12,8 +12,9 @@ namespace Aetheris.Kernel.Firmament.Tests.Experiments;/// E1 of blend clean-up: delete a network of blend strips bounded by planes and rebuild the/// sharp geometry from the extended planes. Each strip becomes the line where its two bridged/// planes meet; every cluster of vertices joined by a strip's end or miter edges becomes the-/// point where the planes meeting there intersect. Experimental: planar primaries only;-/// anything else refuses with a reason.+/// point where the planes meeting there intersect. E2 extends this to cylinders and cones: a strip+/// between a plane and a revolved face across its axis becomes a circle, one along it a line, and+/// corners among curved faces are solved implicitly. Anything else refuses with a reason./// </summary>internal static class BlendNetworkRemoval{@@ -30,6 +31,9 @@ internal static class BlendNetworkRemovalprivate static Result Run(BrepBody body, IReadOnlySet<FaceId> strips, IReadOnlyDictionary<FaceId, FaceId[]> anchors){var topology = body.Topology;+ // An imported body is only as exact as its source says: corners and kept edges are checked to the+ // declared distance accuracy (10 um when it declares none), as tolerant edges are on import.+ var accuracy = body.ImportQualification is { } qualification ? Math.Max(1e-6, qualification.SourceDistanceAccuracyMillimetres ?? 0.01) : 1e-6;var edgeFaces = new Dictionary<EdgeId, List<FaceId>>();foreach (var face in topology.Faces)foreach (var loop in face.LoopIds)@@ -39,16 +43,17 @@ internal static class BlendNetworkRemovalif (!edgeFaces.TryGetValue(e, out var list)) edgeFaces[e] = list = [];list.Add(face.Id);}- PlaneSurface Plane(FaceId f) => body.TryGetFaceSurfaceGeometry(f, out var s) && s?.Plane is { } p ? p- : throw new InvalidOperationException($"blend-removal-primary-not-planar: face {f.Value} borders the network and is {s?.Kind}; E1 admits planes only");- var lengths = BrepDisplayTessellator.Tessellate(body).Value.EdgePolylines- .ToDictionary(p => p.EdgeId, p => p.Points.Zip(p.Points.Skip(1)).Sum(q => (q.Second - q.First).Length));+ Implicit Surface(FaceId f) => body.TryGetFaceSurfaceGeometry(f, out var s) && s is { Kind: SurfaceGeometryKind.Plane or SurfaceGeometryKind.Cylinder or SurfaceGeometryKind.Cone }+ ? new Implicit(f, s)+ : throw new InvalidOperationException($"blend-removal-primary-unsupported: face {f.Value} borders the network and is {s?.Kind}; E2 admits planes, cylinders and cones");+ var polylines = BrepDisplayTessellator.Tessellate(body).Value.EdgePolylines.ToDictionary(p => p.EdgeId, p => p.Points);FaceId Other(EdgeId e, FaceId f) => edgeFaces[e].Count == 2 && edgeFaces[e][0] != edgeFaces[e][1] ? edgeFaces[e].Single(x => x != f) : throw new InvalidOperationException($"blend-removal-seam-edge: edge {e.Value} is a seam or non-manifold");IEnumerable<EdgeId> EdgesOf(FaceId f) => topology.GetFace(f).LoopIds.SelectMany(l => topology.GetLoop(l).CoedgeIds).Select(c => topology.GetCoedge(c).EdgeId).Distinct();// 1. Each strip spans exactly two anchors (from classification: tangent for fillets, leaning for chamfers).var bridge = new Dictionary<FaceId, (FaceId A, FaceId B)>();+ var bridgeCircle = new Dictionary<FaceId, (Point3D Center, Vector3D Axis, double Radius)?>();var longitudinal = new HashSet<EdgeId>();// A face spanning other than two anchors is a joiner (corner patch, setback, a blend that only// meets blends): it has no edge of its own to leave behind, so all its edges are transverse and@@ -58,12 +63,13 @@ internal static class BlendNetworkRemovalvar anchor = anchors.GetValueOrDefault(s) ?? [];if (anchor.Length != 2) continue;var (a, b) = (anchor[0], anchor[1]);- Plane(a); Plane(b);bridge[s] = (a, b);- foreach (var e in EdgesOf(s).Where(e => edgeFaces[e].Count == 2 && (Other(e, s) == a || Other(e, s) == b))) longitudinal.Add(e);+ bridgeCircle[s] = SharpEdge(Surface(a), Surface(b), s);+ // A strip's own seam (a ring blend) has both sides on the strip: it is transverse.+ foreach (var e in EdgesOf(s).Where(e => edgeFaces[e].Count == 2 && edgeFaces[e][0] != edgeFaces[e][1] && (Other(e, s) == a || Other(e, s) == b))) longitudinal.Add(e);}- if (strips.SelectMany(EdgesOf).Any(e => edgeFaces[e].Count != 2 || edgeFaces[e][0] == edgeFaces[e][1]))- throw new InvalidOperationException("blend-removal-seam-edge: a strip has a seam or non-manifold edge");+ if (strips.SelectMany(EdgesOf).Any(e => edgeFaces[e].Count != 2 || (edgeFaces[e][0] == edgeFaces[e][1] && !strips.Contains(edgeFaces[e][0]))))+ throw new InvalidOperationException("blend-removal-seam-edge: a strip has a non-manifold edge");var touched = strips.SelectMany(EdgesOf).ToHashSet();var transverse = touched.Where(e => !longitudinal.Contains(e)).ToHashSet();@@ -87,10 +93,15 @@ internal static class BlendNetworkRemovalvar representative = new Dictionary<VertexId, VertexId>();foreach (var (root, members) in clusters){- var planes = members.SelectMany(v => vertexFaces[v]).Where(f => !strips.Contains(f)).Distinct().Select(f => (Face: f, Plane: Plane(f))).ToArray();- body.TryGetVertexPoint(members[0], out var seed);- corner[root] = Solve(planes.Select(p => p.Plane).ToArray(), seed,- $"cluster of vertices {string.Join(',', members.Select(m => m.Value))} on faces {string.Join(',', planes.Select(p => p.Face.Value))}");+ var around = members.SelectMany(v => vertexFaces[v]).Where(f => !strips.Contains(f)).Distinct().Select(Surface).ToArray();+ // Seed from a vertex a kept edge leaves from: that edge's curve already holds it, and the+ // smallest move onto the other surfaces keeps it there when the cluster is underdetermined.+ var anchorVertex = members.FirstOrDefault(v => topology.Edges.Any(e => !touched.Contains(e.Id) && (e.StartVertexId == v || e.EndVertexId == v)), members[0]);+ body.TryGetVertexPoint(anchorVertex, out var seed);+ var where = $"cluster of vertices {string.Join(',', members.Select(m => m.Value))} on faces {string.Join(',', around.Select(p => $"{p.Face.Value}{p.Surface.Kind.ToString()[..3]}"))}";+ corner[root] = around.All(p => p.Surface.Plane is not null)+ ? Solve(around.Select(p => p.Surface.Plane!.Value).ToArray(), seed, where, accuracy)+ : SolveImplicit(around, seed, where, accuracy);representative[root] = members.MinBy(v => v.Value)!;}VertexId Map(VertexId v) => clusterOf.TryGetValue(v, out var root) ? representative[root] : v;@@ -98,19 +109,52 @@ internal static class BlendNetworkRemoval// 3. One straight edge per (corner, corner, plane pair).var nextEdge = topology.Edges.Max(e => e.Id.Value) + 1;var nextCurve = body.Geometry.Curves.Max(c => c.Key.Value) + 1;- var newEdges = new Dictionary<(int, int, int, int), (EdgeId Id, VertexId Start, VertexId End, CurveGeometryId Curve, Line3Curve Line, double Length)>();- (EdgeId Id, VertexId Start, VertexId End, CurveGeometryId Curve, Line3Curve Line, double Length) NewEdge(EdgeId old, FaceId s)+ var newEdges = new Dictionary<(int, int, int, int), NewEdgeData>();+ NewEdgeData NewEdge(EdgeId old, FaceId s){var edge = topology.GetEdge(old);var (u, v) = (Map(edge.StartVertexId), Map(edge.EndVertexId));- if (u == v) throw new InvalidOperationException($"blend-removal-strip-collapses: edge {old.Value} has both ends in one corner");+ var circle = bridgeCircle[s];+ if (u == v && circle is null) throw new InvalidOperationException($"blend-removal-strip-collapses: edge {old.Value} has both ends in one corner");var (a, b) = bridge[s];var key = (Math.Min(u.Value, v.Value), Math.Max(u.Value, v.Value), Math.Min(a.Value, b.Value), Math.Max(a.Value, b.Value));if (newEdges.TryGetValue(key, out var made)) return made;var (start, end) = u.Value < v.Value ? (u, v) : (v, u);var p = corner[clusterOf[start]]; var q = corner[clusterOf[end]];- var line = new Line3Curve(p, Direction3D.Create(q - p));- return newEdges[key] = (new EdgeId(nextEdge++), start, end, new CurveGeometryId(nextCurve++), line, (q - p).Length);+ NewEdgeData result;+ if (circle is not { } c)+ {+ var line = new Line3Curve(p, Direction3D.Create(q - p));+ result = new(new EdgeId(nextEdge++), start, end, new CurveGeometryId(nextCurve++), CurveGeometry.FromLine(line), new ParameterInterval(0, (q - p).Length));+ }+ else+ {+ // The arc runs the way the old edge ran: through the old edge's midpoint, which sits a blend+ // radius away from the sharp edge but at the same angle.+ var points = polylines[old];+ var middle = points[points.Count / 2];+ var axis = c.Axis;+ var x = p - c.Center; x -= axis * x.Dot(axis);+ if (Math.Abs(x.Length - c.Radius) > accuracy) throw new InvalidOperationException($"blend-removal-corner-off-circle: corner off strip {s.Value}'s sharp edge by {Math.Abs(x.Length - c.Radius):G3} mm");+ var closed = start == end;+ if (!closed)+ {+ var trial = new Circle3Curve(c.Center, Direction3D.Create(axis), c.Radius, Direction3D.Create(x));+ double Angle(Circle3Curve k, Point3D r) { var d = r - k.Center; var t = Math.Atan2(d.Dot(k.YAxis.ToVector()), d.Dot(k.XAxis.ToVector())); return t < 0 ? t + 2 * Math.PI : t; }+ // Of the two arcs from start to end, the sharp edge is the one under the old edge.+ if (Angle(trial, middle) > Angle(trial, q)) axis = -axis;+ }+ var k = new Circle3Curve(c.Center, Direction3D.Create(axis), c.Radius, Direction3D.Create(x));+ var dq = q - k.Center;+ var sweep = closed ? 2 * Math.PI : Math.Atan2(dq.Dot(k.YAxis.ToVector()), dq.Dot(k.XAxis.ToVector()));+ if (sweep <= 0) sweep += 2 * Math.PI;+ result = new(new EdgeId(nextEdge++), start, end, new CurveGeometryId(nextCurve++), CurveGeometry.FromCircle(k), new ParameterInterval(0, sweep));+ }+ // The sharp edge lies on both surfaces it separates.+ var mid = result.Evaluate(0.5 * (result.Trim.Start + result.Trim.End));+ foreach (var f in new[] { a, b })+ if (Math.Abs(Surface(f).F(mid)) > accuracy) throw new InvalidOperationException($"blend-removal-sharp-edge-off-surface: strip {s.Value}'s new edge misses face {f.Value} by {Math.Abs(Surface(f).F(mid)):G3} mm");+ return newEdges[key] = result;}// 4. Rebuild the loops of every face outside the network.@@ -138,7 +182,17 @@ internal static class BlendNetworkRemovalvar edge = topology.GetEdge(c.EdgeId);var from = Map(c.IsReversed ? edge.EndVertexId : edge.StartVertexId);var made = NewEdge(c.EdgeId, s);- var use = (made.Id, from != made.Start);+ bool reversed;+ if (made.Start != made.End) reversed = from != made.Start;+ else+ {+ // A closed edge has one vertex: compare directions where the old edge ran.+ var pts = polylines[c.EdgeId];+ var i = pts.Count / 2;+ var oldDirection = (pts[Math.Min(i + 1, pts.Count - 1)] - pts[Math.Max(i - 1, 0)]) * (c.IsReversed ? -1 : 1);+ reversed = made.Tangent(pts[i]).Dot(oldDirection) < 0;+ }+ var use = (made.Id, reversed);if (uses.Count == 0 || uses[^1] != use) uses.Add(use);}if (changed) changedFaces.Add(face.Id);@@ -186,10 +240,32 @@ internal static class BlendNetworkRemoval// Every face owning a re-trimmed edge needs its pcurves rebuilt, not only faces whose loops changed.changedFaces.UnionWith(edgeFaces[e.Id]);var curve = body.Geometry.GetCurve(binding.CurveGeometryId);- if (curve.Line3 is not { } line) throw new InvalidOperationException($"blend-removal-moved-edge-not-straight: edge {e.Id.Value} is {curve.Kind}");- double At(Point3D p) => (p - line.Origin).Dot(line.Direction.ToVector());- var (s, t) = (At(points[Map(e.StartVertexId)]), At(points[Map(e.EndVertexId)]));- if (!binding.OrientedEdgeSense) (s, t) = (t, s);+ var (ps, pe) = (points[Map(e.StartVertexId)], points[Map(e.EndVertexId)]);+ double s, t;+ if (curve.Line3 is { } line)+ {+ double At(Point3D p) => (p - line.Origin).Dot(line.Direction.ToVector());+ foreach (var p in new[] { ps, pe })+ if ((p - line.Evaluate(At(p))).Length > accuracy) throw new InvalidOperationException($"blend-removal-corner-off-kept-edge: edge {e.Id.Value} misses its new corner by {(p - line.Evaluate(At(p))).Length:G3} mm");+ (s, t) = (At(ps), At(pe));+ if (!binding.OrientedEdgeSense) (s, t) = (t, s);+ }+ else if (curve.Circle3 is { } circle && binding.TrimInterval is { } old)+ {+ double Angle(Point3D p)+ {+ var d = p - circle.Center;+ var off = Math.Abs(d.Dot(circle.Normal.ToVector())) + Math.Abs((d - circle.Normal.ToVector() * d.Dot(circle.Normal.ToVector())).Length - circle.Radius);+ if (off > accuracy) throw new InvalidOperationException($"blend-removal-corner-off-kept-edge: circular edge {e.Id.Value} misses its new corner by {off:G3} mm");+ return Math.Atan2(d.Dot(circle.YAxis.ToVector()), d.Dot(circle.XAxis.ToVector()));+ }+ // Unwrap each end next to where it was: corners move by a blend radius, not a turn.+ static double Near(double angle, double reference) => reference + Math.IEEERemainder(angle - reference, 2 * Math.PI);+ var (first, last) = binding.OrientedEdgeSense ? (ps, pe) : (pe, ps);+ s = Near(Angle(first), old.Start);+ t = e.StartVertexId == e.EndVertexId ? s + (old.End - old.Start) : Near(Angle(last), old.End);+ }+ else throw new InvalidOperationException($"blend-removal-moved-edge-unsupported: edge {e.Id.Value} is {curve.Kind}");if (t - s <= Tol) throw new InvalidOperationException($"blend-removal-edge-inverts: edge {e.Id.Value}");binding = binding with { TrimInterval = new ParameterInterval(s, t) };}@@ -197,8 +273,8 @@ internal static class BlendNetworkRemoval}foreach (var e in newEdges.Values){- geometry.AddCurve(e.Curve, CurveGeometry.FromLine(e.Line));- bindings.AddEdgeBinding(new EdgeGeometryBinding(e.Id, e.Curve, new ParameterInterval(0, e.Length)));+ geometry.AddCurve(e.Curve, e.Geometry);+ bindings.AddEdgeBinding(new EdgeGeometryBinding(e.Id, e.Curve, e.Trim));}foreach (var b in body.Bindings.FaceBindings.Where(b => !strips.Contains(b.FaceId))) bindings.AddFaceBinding(b);foreach (var b in body.Bindings.FaceBoundaryRoleBindings.Where(b => !strips.Contains(b.FaceId))) bindings.AddFaceBoundaryRoleBinding(b);@@ -221,15 +297,98 @@ internal static class BlendNetworkRemovalvar scratch = new BrepBindingModel();foreach (var b in bindings.EdgeBindings) scratch.AddEdgeBinding(b);foreach (var b in bindings.FaceBindings) scratch.AddFaceBinding(b);- var pcurves = BrepPcurveRecovery.Populate(sub, geometry, scratch);+ var pcurves = BrepPcurveRecovery.Populate(sub, geometry, scratch, sourceTolerance: accuracy > 1e-6 ? accuracy : null);if (pcurves.Diagnostics.Count > 0) throw new InvalidOperationException("blend-removal-pcurves: " + string.Join("; ", pcurves.Diagnostics.Take(3).Select(d => d.Message)));foreach (var b in scratch.PcurveBindings) bindings.AddPcurveBinding(b);return new(new BrepBody(model, geometry, bindings, points, null, body.ShellRepresentation), null, newEdges.Count, clusters.Count);}+ private sealed record NewEdgeData(EdgeId Id, VertexId Start, VertexId End, CurveGeometryId Curve, CurveGeometry Geometry, ParameterInterval Trim)+ {+ public Point3D Evaluate(double t) => Geometry.Line3 is { } l ? l.Evaluate(t) : Geometry.Circle3!.Value.Evaluate(t);+ public Vector3D Tangent(Point3D near)+ {+ if (Geometry.Line3 is { } l) return l.Direction.ToVector();+ var c = Geometry.Circle3!.Value; var d = near - c.Center;+ return c.Tangent(Math.Atan2(d.Dot(c.YAxis.ToVector()), d.Dot(c.XAxis.ToVector())));+ }+ }++ /// <summary>A face's support as an implicit function: signed distance for planes and cylinders, distance to+ /// the generator for cones.</summary>+ private sealed record Implicit(FaceId Face, SurfaceGeometry Surface)+ {+ public double F(Point3D x) => Eval(x).F;+ public Vector3D Gradient(Point3D x) => Eval(x).G;+ private (double F, Vector3D G) Eval(Point3D x)+ {+ if (Surface.Plane is { } p) return (p.Normal.ToVector().Dot(x - p.Origin), p.Normal.ToVector());+ var (origin, axis) = Surface.Cylinder is { } c ? (c.Origin, c.Axis.ToVector()) : (Surface.Cone!.Value.Apex, Surface.Cone!.Value.Axis.ToVector());+ var v = x - origin; var h = v.Dot(axis); var radial = v - axis * h; var rho = radial.Length;+ var unit = rho > 1e-12 ? radial * (1 / rho) : Vector3D.Zero;+ if (Surface.Cylinder is { } cylinder) return (rho - cylinder.Radius, unit);+ var a = Surface.Cone!.Value.SemiAngleRadians; var side = h < 0 ? -1d : 1d;+ return (rho * Math.Cos(a) - Math.Abs(h) * Math.Sin(a), unit * Math.Cos(a) - axis * (side * Math.Sin(a)));+ }+ }++ /// <summary>The circle where a plane meets a cylinder or cone across its axis; null for two planes or a plane+ /// along a cylinder's axis, whose sharp edge is the straight line between its corners.</summary>+ private static (Point3D Center, Vector3D Axis, double Radius)? SharpEdge(Implicit a, Implicit b, FaceId strip)+ {+ if (a.Surface.Plane is null) (a, b) = (b, a);+ if (a.Surface.Plane is not { } plane) throw new InvalidOperationException($"blend-removal-anchor-pair-unsupported: strip {strip.Value} bridges {a.Surface.Kind} and {b.Surface.Kind}");+ if (b.Surface.Plane is not null) return null;+ var n = plane.Normal.ToVector();+ var (origin, axis) = b.Surface.Cylinder is { } c ? (c.Origin, c.Axis.ToVector()) : (b.Surface.Cone!.Value.Apex, b.Surface.Cone!.Value.Axis.ToVector());+ var along = n.Dot(axis);+ if (Math.Abs(along) < 1e-9 && b.Surface.Cylinder is not null) return null;+ if (Math.Abs(Math.Abs(along) - 1) > 1e-9) throw new InvalidOperationException($"blend-removal-anchor-pair-oblique: strip {strip.Value} meets a {b.Surface.Kind} obliquely");+ var center = origin + axis * ((plane.Origin - origin).Dot(n) / along);+ var radius = b.Surface.Cylinder is { } cylinder ? cylinder.Radius : Math.Abs((center - origin).Dot(axis)) * Math.Tan(b.Surface.Cone!.Value.SemiAngleRadians);+ if (radius < 1e-9) throw new InvalidOperationException($"blend-removal-sharp-edge-degenerate: strip {strip.Value}'s plane meets the cone at its apex");+ return (center, axis, radius);+ }++ /// <summary>Corner on curved faces: Gauss-Newton with the minimum-norm step from the seed, so an+ /// underdetermined cluster (two faces) lands on the nearest point of their intersection.</summary>+ private static Point3D SolveImplicit(Implicit[] surfaces, Point3D seed, string where, double accuracy)+ {+ if (surfaces.Length < 2) throw new InvalidOperationException($"blend-removal-corner-underdetermined: {where}");+ var x = seed;+ var m = surfaces.Length;+ for (var iteration = 0; iteration < 100; iteration++)+ {+ var f = surfaces.Select(s => s.F(x)).ToArray();+ if (f.Max(Math.Abs) < 1e-11) break;+ var g = surfaces.Select(s => s.Gradient(x)).ToArray();+ // delta = J^T (J J^T + lambda I)^-1 f+ var a = new double[m, m + 1];+ for (var i = 0; i < m; i++) { for (var j = 0; j < m; j++) a[i, j] = g[i].Dot(g[j]) + (i == j ? 1e-12 : 0); a[i, m] = f[i]; }+ for (var col = 0; col < m; col++)+ {+ var pivot = Enumerable.Range(col, m - col).MaxBy(r => Math.Abs(a[r, col]));+ for (var k = 0; k <= m; k++) (a[col, k], a[pivot, k]) = (a[pivot, k], a[col, k]);+ for (var r = 0; r < m; r++)+ {+ if (r == col) continue;+ var factor = a[r, col] / a[col, col];+ for (var k = col; k <= m; k++) a[r, k] -= factor * a[col, k];+ }+ }+ var step = Vector3D.Zero;+ for (var i = 0; i < m; i++) step += g[i] * (a[i, m] / a[i, i]);+ x -= step;+ }+ var residual = surfaces.Max(s => Math.Abs(s.F(x)));+ if (residual > Math.Max(1e-7, accuracy)) throw new InvalidOperationException($"blend-removal-corner-not-common: {surfaces.Length} surfaces miss one point by {residual:G3} mm at {where}");+ if ((x - seed).Length > 100) throw new InvalidOperationException($"blend-removal-corner-far: corner moved {(x - seed).Length:G3} mm at {where}");+ return x;+ }+/// <summary>Point common to the planes (least squares, then checked), or the point on their/// line nearest the seed when only two planes meet there.</summary>- private static Point3D Solve(PlaneSurface[] planes, Point3D seed, string where)+ private static Point3D Solve(PlaneSurface[] planes, Point3D seed, string where, double accuracy){if (planes.Length < 2) throw new InvalidOperationException($"blend-removal-corner-underdetermined: {where}");var n = planes.Select(p => p.Normal.ToVector()).ToArray();@@ -255,7 +414,7 @@ internal static class BlendNetworkRemovalx = Solve3([new(a[0, 0], a[0, 1], a[0, 2]), new(a[1, 0], a[1, 1], a[1, 2]), new(a[2, 0], a[2, 1], a[2, 2])], r);}var residual = n.Zip(d).Max(p => Math.Abs(p.First.Dot(x - Point3D.Origin) - p.Second));- if (residual > 1e-6) throw new InvalidOperationException($"blend-removal-corner-not-common: {planes.Length} planes miss one point by {residual:G3} mm at {where}");+ if (residual > accuracy) throw new InvalidOperationException($"blend-removal-corner-not-common: {planes.Length} planes miss one point by {residual:G3} mm at {where}");return x;}diff --git a/Aetheris.Kernel.Firmament.Tests/Experiments/BlendNetworkRemovalTests.cs b/Aetheris.Kernel.Firmament.Tests/Experiments/BlendNetworkRemovalTests.csindex 1d8a22a7..a2bdceb1 100644--- a/Aetheris.Kernel.Firmament.Tests/Experiments/BlendNetworkRemovalTests.cs+++ b/Aetheris.Kernel.Firmament.Tests/Experiments/BlendNetworkRemovalTests.cs@@ -41,8 +41,51 @@ public sealed class BlendNetworkRemovalTests(ITestOutputHelper output){ "rect-top-chamfer", Rect("Modify Body { EdgeFinish Rim { target: Section.Outer; on: Top; kind: Chamfer; distance: 2mm } }") },{ "rect-bottom-fillet", Rect("Modify Body { EdgeFinish Rim { target: Section.Outer; on: Bottom; kind: Fillet; radius: 3mm } }") },{ "polygon-rim-fillet", "fixtures/Regression/EdgeFinishRobustness/polygon-rim-fillet.firmament" },+ // E2: rims that run along cylinders (arcs in the profile) as well as planes.+ { "d-profile-fillet", "fixtures/Regression/EdgeFinishRobustness/d-profile-fillet.firmament" },+ { "rounded-rect-top-fillet", RoundedRect("Modify Body { EdgeFinish Rim { target: Section.Outer; on: Top; kind: Fillet; radius: 2mm } }") },+ { "rounded-rect-top-chamfer", RoundedRect("Modify Body { EdgeFinish Rim { target: Section.Outer; on: Top; kind: Chamfer; distance: 2mm } }") },};+ /// <summary>Corner arcs tangent to the sides: the rim strips along the arcs bridge the top and a cylinder+ /// across its axis, and meet the side strips where a plane and a cylinder are tangent.</summary>+ private static string RoundedRect(string finish) => $$"""+ Model RR {+ units: mm+ Path Outline {+ start: Point2(6mm, 0mm)+ heading: 0deg+ Line South { length: 28mm }+ Arc SE { turn: 90deg; radius: 6mm }+ Line East { length: 13mm }+ Arc NE { turn: 90deg; radius: 6mm }+ Line North { length: 28mm }+ Arc NW { turn: 90deg; radius: 6mm }+ Line West { length: 13mm }+ Arc SW { turn: 90deg; radius: 6mm }+ }+ Profile Section from Outline+ Struct Body { Extrude Stock { profile: Section; from: 0mm; to: 10mm } }+ {{finish}}+ }+ """;++ /// <summary>The chimera's convex 4 mm arc under a 4 mm fillet: the blend there is a sphere touching the top at+ /// one point, which the probe reads as a joiner with no anchors. Its corner is two corners merged.</summary>+ [Fact]+ public void FilletAsLargeAsTheArc_Refuses()+ {+ var source = File.ReadAllText(Path.Combine(Root(), "fixtures/Regression/CanonicalGeometry/profile-edgefinish-chimera-fillet.firmament"));+ var finished = Import(source);+ var probe = BlendRegionProbe.Run("chimera", finished);+ var strips = probe.Regions.SelectMany(r => r.Faces).Select(f => new FaceId(f))+ .Where(f => !(finished.TryGetFaceSurfaceGeometry(f, out var g) && g!.Cylinder is { } c && Math.Abs(Math.Abs(c.Axis.ToVector().Z) - 1) < 1e-9))+ .ToHashSet();+ var removed = BlendNetworkRemoval.Remove(finished, strips, Anchors(probe));+ Assert.Null(removed.Body);+ Assert.StartsWith("blend-removal-corner-not-common", removed.Refusal);+ }+[Theory][MemberData(nameof(Admitted))]public void RemovingTheStrips_ReturnsThePartWithoutTheFinish(string name, string sourceOrPath)@@ -50,27 +93,42 @@ public sealed class BlendNetworkRemovalTests(ITestOutputHelper output)var source = sourceOrPath.StartsWith("fixtures/", StringComparison.Ordinal) ? File.ReadAllText(Path.Combine(Root(), sourceOrPath)) : sourceOrPath;var finished = Import(source);var stripped = Regex.Replace(source, @"Modify Body \{\s*EdgeFinish[^{}]*\{[^{}]*\}\s*\}", "");- Assert.DoesNotContain("EdgeFinish", stripped);+ Assert.DoesNotMatch(@"EdgeFinish \w+ \{", stripped);var plain = Import(Regex.Replace(source, @"Modify Body \{\s*EdgeFinish[^{}]*\{[^{}]*\}\s*\}", ""));var probe = BlendRegionProbe.Run(name, finished);- var strips = probe.Regions.SelectMany(r => r.Faces).Select(f => new FaceId(f)).ToHashSet();+ // Profile arcs extruded along Z are tangent walls the probe also reads as blends; the authored part+ // without the finish keeps them, so only the finish is removed and the arcs stay as anchors.+ var strips = probe.Regions.SelectMany(r => r.Faces).Select(f => new FaceId(f))+ .Where(f => !(finished.TryGetFaceSurfaceGeometry(f, out var g) && g!.Cylinder is { } c && Math.Abs(Math.Abs(c.Axis.ToVector().Z) - 1) < 1e-9))+ .ToHashSet();Assert.NotEmpty(strips);- var removed = BlendNetworkRemoval.Remove(finished, strips, Anchors(probe));+ // A kept wall the probe read as a blend is an anchor of the strips along it.+ var kept = probe.Regions.SelectMany(r => r.Faces).Select(f => new FaceId(f)).Where(f => !strips.Contains(f)).ToHashSet();+ var anchors = Anchors(probe);+ foreach (var f in strips)+ anchors[f] = anchors[f].Concat(Neighbours(finished, f).Where(kept.Contains)).Distinct().ToArray();+ var removed = BlendNetworkRemoval.Remove(finished, strips, anchors);Assert.True(removed.Body is not null, removed.Refusal);output.WriteLine($"{name}: removed {strips.Count} faces, {removed.NewEdges} sharp edges, {removed.Clusters} corners");var body = removed.Body!;Assert.True(TopologyGraphValidator.Validate(body.Topology).IsSuccess);Assert.True(BrepBindingValidator.Validate(body, requireAllEdgeAndFaceBindings: true).IsSuccess);+ output.WriteLine("plain: " + Kinds(plain)); output.WriteLine("body: " + Kinds(body));Assert.Equal(plain.Topology.Faces.Count(), body.Topology.Faces.Count());Assert.Equal(plain.Topology.Edges.Count(), body.Topology.Edges.Count());Assert.Equal(plain.Topology.Vertices.Count(), body.Topology.Vertices.Count());Assert.Equal(Points(plain), Points(body));- Assert.True(BrepMassProperties.TryEvaluateExactVolume(plain, out var expected));- Assert.True(BrepMassProperties.TryEvaluateExactVolume(body, out var actual), actual?.Status.ToString());- Assert.Equal(expected.AbsoluteVolume, actual.AbsoluteVolume, 6);+ // Exact volume where both have one; curved faces fall back to the tessellated evaluator for both.+ if (BrepMassProperties.TryEvaluateExactVolume(plain, out var expected) && BrepMassProperties.TryEvaluateExactVolume(body, out var actual))+ Assert.Equal(expected.AbsoluteVolume, actual.AbsoluteVolume, 6);+ else+ {+ var (v0, v1) = (BrepMassProperties.Evaluate(plain).AbsoluteVolume, BrepMassProperties.Evaluate(body).AbsoluteVolume);+ Assert.True(Math.Abs(v1 - v0) <= 1e-3 * v0, $"volume {v1} vs {v0}");+ }var step = Step242Exporter.ExportBody(body);Assert.True(step.IsSuccess, string.Join("\n", step.Diagnostics.Select(d => d.Message)));@@ -120,19 +178,6 @@ public sealed class BlendNetworkRemovalTests(ITestOutputHelper output)Assert.Equal(Points(plain), Points(reimported.Value));}- [Theory]- [InlineData("fixtures/Regression/EdgeFinishRobustness/d-profile-fillet.firmament", "blend-removal-primary-not-planar")]- [InlineData("fixtures/Regression/CanonicalGeometry/profile-edgefinish-chimera-fillet.firmament", "blend-removal-")]- public void CurvedNeighbours_Refuse_WithAReason(string path, string code)- {- var finished = Import(File.ReadAllText(Path.Combine(Root(), path)));- var probe = BlendRegionProbe.Run(path, finished);- var removed = BlendNetworkRemoval.Remove(finished, probe.Regions.SelectMany(r => r.Faces).Select(f => new FaceId(f)).ToHashSet(), Anchors(probe));- Assert.Null(removed.Body);- Assert.StartsWith(code, removed.Refusal);- output.WriteLine(removed.Refusal);- }-internal static Dictionary<FaceId, FaceId[]> Anchors(BlendRegionProbe.Report probe) => probe.Regions.SelectMany(r => r.Faces.Zip(r.FaceAnchors)).ToDictionary(p => new FaceId(p.First), p => p.Second.Select(a => new FaceId(a)).ToArray());@@ -145,6 +190,15 @@ public sealed class BlendNetworkRemovalTests(ITestOutputHelper output)return body.Value;}+ private static IEnumerable<FaceId> Neighbours(BrepBody body, FaceId f)+ {+ var t = body.Topology;+ var edges = t.GetFace(f).LoopIds.SelectMany(l => t.GetLoop(l).CoedgeIds).Select(c => t.GetCoedge(c).EdgeId).ToHashSet();+ return t.Faces.Where(o => o.Id != f && o.LoopIds.SelectMany(l => t.GetLoop(l).CoedgeIds).Any(c => edges.Contains(t.GetCoedge(c).EdgeId))).Select(o => o.Id);+ }++ private static string Kinds(BrepBody body) => string.Join(",", body.Topology.Faces.Select(f => body.TryGetFaceSurfaceGeometry(f.Id, out var g) ? g!.Kind.ToString()[..3] : "?").GroupBy(k => k).Select(g => $"{g.Key}x{g.Count()}"));+private static string[] Points(BrepBody body) => body.Topology.Vertices.Select(v => { body.TryGetVertexPoint(v.Id, out var p); return $"{Math.Round(p.X, 6) + 0.0:F6},{Math.Round(p.Y, 6) + 0.0:F6},{Math.Round(p.Z, 6) + 0.0:F6}"; }).Order(StringComparer.Ordinal).ToArray();diff --git a/docs/development/reports/2026-11/BLEND-CLEANUP-E2-CURVED-NEIGHBOURS.md b/docs/development/reports/2026-11/BLEND-CLEANUP-E2-CURVED-NEIGHBOURS.mdnew file mode 100644index 00000000..2e0cef58--- /dev/null+++ b/docs/development/reports/2026-11/BLEND-CLEANUP-E2-CURVED-NEIGHBOURS.md@@ -0,0 +1,61 @@+# Blend clean-up E2a: strips and corners next to cylinders and cones++**Goal.** Remove blend networks whose neighbours are curved, which E3 refused as+"non-planar neighbour" (566 of 1,201 imported blend faces).++## Census first++`BlendE2Census` (gated) classified the refusals. Most needed little: 222 were ordinary+plane–plane strips refused only because a corner touched a cylinder. The curved cases were+plane–cylinder/cone across the axis (sharp edge is a circle: 178 faces), plane–cylinder+along the axis (a line: 65), and corners among planes and cylinders. Kept edges that move+were 597 lines and 135 circles; splines, spheres and tori as neighbours were rare.++## Method++- Neighbour faces are implicit functions (plane, cylinder, cone). Plane-only corners keep+ E1's exact solve; others use Gauss–Newton with the minimum-norm step from a vertex a kept+ edge leaves from, so an underdetermined corner (two faces) stays on that edge.+- A strip's sharp edge is the line between its corners (two planes, or a plane along a+ cylinder's axis) or the circle where a plane meets a cylinder or cone across its axis,+ taking the arc under the old edge. A ring blend (its own seam) becomes a closed circle.+- Kept circles that move are re-trimmed by angle, each end unwrapped next to where it was.+- Every new edge is checked on both its surfaces, every moved corner on its kept edges.+- Checks use the source's declared accuracy (10 µm when it declares none), as tolerant+ edges do on import. At 1e-6 mm, 178 faces refused for corners 3e-5 to 4e-4 mm off kept+ edges that are themselves that far off their surfaces in the source.++## Results++Authored ground truth (14 tests): E1/E3 cases unchanged; D-profile rim fillet and a rounded+rectangle with tangent corner arcs (fillet and chamfer) come back as the sharp part: faces,+edges, vertices within 1e-6 mm, volume, and after export and reimport.++Imported parts (1,201 blend faces):++| Outcome | E3 | E2a |+|---|---|---|+| removed, valid, round-trips, volume plausible | 308 | **750** (475 concave, 275 convex) |+| of which full pcurve round trip | 305 | 575 |+| non-planar neighbour / corner off kept edge | 566 | 0 |+| corner not common (two corners in one cluster) | 6 | 138 |+| blocked: freeform neighbour / hole crossing a blend | 208 | 142 |+| anchor pair unsupported or oblique | – | 74 |+| loop collapses | 48 | 60 |+| other (parallel planes, inverted edge, underdetermined) | 74 | 45 |++The FTC-06 "volume implausible" verdict (faces 112–113) is a measurement error in the+**source**: its tessellated face 24 (half-cylinder whose top circle is split into two arcs,+one trimmed from −6e-15) covers 302 mm² instead of 601. The removal itself is right: both+half-cylinders come back at exactly πrh. Display tessellator issue, not investigated.++## Next (E2b)++- Corners not common (138). Two patterns:+ - A strip ending on a curved face outside the network whose far end is already a sharp+ corner of the kept faces (CTC-01): that vertex must stay, and the end edge becomes the+ kept face's intersection with the anchor, instead of collapsing into one point.+ - Sphere joiners between cylinder fillets around a rounded pocket wall (FTC-10), and a+ fillet as large as its arc, where the blend touches the top at a point (chimera; the+ probe reads it as a 0-anchor joiner). Pinned by `FilletAsLargeAsTheArc_Refuses`.+- Anchor pairs not covered (74): cylinder–cylinder, cone–cylinder, oblique plane–cone.