Skip to content

Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel

Blend recognition by the rolling-ball certificate (Core)

Claude7494a0bd6456
diff --git a/Aetheris.Kernel.Core.Tests/Brep/Blends/BlendRecognitionTests.cs b/Aetheris.Kernel.Core.Tests/Brep/Blends/BlendRecognitionTests.cs
new file mode 100644
index 00000000..07b261ab
--- /dev/null
+++ b/Aetheris.Kernel.Core.Tests/Brep/Blends/BlendRecognitionTests.cs
@@ -0,0 +1,87 @@
+using Aetheris.Kernel.Core.Brep;
+using Aetheris.Kernel.Core.Brep.Blends;
+using Aetheris.Kernel.Core.Geometry;
+using Aetheris.Kernel.Core.Tests.Step242;
+
+namespace Aetheris.Kernel.Core.Tests.Brep.Blends;
+
+/// <summary>The rolling-ball certificate: a face is a round when a ball centred a radius behind each of its
+/// points touches both anchors; which anchors is a judgment over measured readings.</summary>
+public sealed class BlendRecognitionTests
+{
+ [Theory]
+ [InlineData(4, 6, 4)]
+ [InlineData(6, 12, 8)]
+ [InlineData(8, 12, 6)]
+ [InlineData(12, 30, 20)]
+ [InlineData(20, 30, 12)]
+ public void RoundedPlatonicSolid_EveryEdgeIsAConvexStripBetweenItsTwoFaces_EveryCornerAJoiner(int faces, int edges, int vertices)
+ {
+ const double radius = 2;
+ var body = ConvexStockPrimitives.CreateRegular(faces, 30, radius).Value;
+ var plain = ConvexStockPrimitives.CreateRegular(faces, 30).Value;
+
+ var recognized = BlendRecognition.Recognize(body);
+ var strips = recognized.Where(f => f.Role == BlendFaceRole.Strip).ToArray();
+ Assert.Equal(edges, strips.Length);
+ Assert.Equal(vertices, recognized.Count(f => f.Role == BlendFaceRole.Joiner));
+ Assert.Equal(faces, recognized.Count(f => f.Role == BlendFaceRole.Primary));
+ foreach (var strip in strips)
+ {
+ var blend = strip.Blend!;
+ Assert.Equal(BlendSection.Round, blend.Hypothesis.Section);
+ Assert.True(blend.Hypothesis.Convex);
+ Assert.Equal(radius, blend.Size, 9);
+ Assert.True(blend.Residual < 1e-9, $"face {strip.Face.Value}: residual {blend.Residual}");
+ Assert.All(new[] { blend.Hypothesis.AnchorA, blend.Hypothesis.AnchorB }, a => Assert.Equal(BlendFaceRole.Primary, recognized.Single(r => r.Face == a).Role));
+ }
+ Assert.DoesNotContain(BlendRecognition.Recognize(plain), f => f.Role != BlendFaceRole.Primary);
+ }
+
+ [Fact]
+ public void RoundedBox_WrongReadingsAreRejectedWithTheirMeasurement()
+ {
+ var body = ConvexStockPrimitives.CreateRoundedBox(40, 30, 20, 3).Value;
+ var strip = BlendRecognition.Recognize(body).First(f => f.Role == BlendFaceRole.Strip);
+ var trace = strip.Judgment!;
+ Assert.Equal(BlendRecognition.PolicyVersion, trace.PolicyVersion);
+ // The same anchors with the ball on the wrong side: refused by the certificate, which says by how much.
+ var concave = trace.Candidates.Single(c => c.Id == strip.Blend!.Hypothesis.Id.Replace("convex", "concave"));
+ Assert.False(concave.Admissible);
+ var residual = concave.Rules.Single(r => r.Id == "residual").Result;
+ Assert.False(residual.Passed);
+ Assert.True(residual.Measured > 1, $"measured {residual.Measured}");
+ }
+
+ [Fact]
+ public void SizeLimitIsPolicy_ARoundOverItRefusesNamingItsRadius()
+ {
+ var body = ConvexStockPrimitives.CreateRoundedBox(40, 30, 20, 3).Value;
+ var recognized = BlendRecognition.Recognize(body, new BlendRecognitionOptions { MaxSize = 2 });
+ Assert.DoesNotContain(recognized, f => f.Role == BlendFaceRole.Strip);
+ var cylinder = recognized.First(f => body.TryGetFaceSurfaceGeometry(f.Face, out var s) && s!.Kind == SurfaceGeometryKind.Cylinder);
+ // The reading that certifies (residual passes) is the one refused, and only by the size rule.
+ var certified = cylinder.Judgment!.Candidates.Single(c => c.Rules.Single(r => r.Id == "residual").Result.Passed);
+ Assert.Equal(["size"], certified.Rules.Where(r => !r.Result.Passed).Select(r => r.Id));
+ var size = certified.Rules.Single(r => r.Id == "size").Result;
+ Assert.Equal(3, size.Measured, 9);
+ Assert.Equal(2, size.Limit);
+ }
+
+ /// <summary>Fusion's AP214 export: tori and cylinders certify exactly, and some of its B-spline faces certify as
+ /// strips or read as corner patches, which shape-by-kind recognition could not do.</summary>
+ [Fact]
+ public void So101_CertifiesAnalyticAndFreeformBlends()
+ {
+ var body = Step242Corpus.Body("testdata/step242/community/so101/Under_arm_SO101.step");
+ var recognized = BlendRecognition.Recognize(body);
+ var strips = recognized.Where(f => f.Role == BlendFaceRole.Strip).ToArray();
+ Assert.True(strips.Length >= 65, $"{strips.Length} strips");
+ // Anchors are faces that stay: no strip is read against another blend.
+ var roles = recognized.ToDictionary(r => r.Face, r => r.Role);
+ Assert.All(strips, s => Assert.True(roles[s.Blend!.Hypothesis.AnchorA] == BlendFaceRole.Primary && roles[s.Blend.Hypothesis.AnchorB] == BlendFaceRole.Primary, $"face {s.Face.Value}: {s.Reason}"));
+ Assert.All(strips, s => Assert.True(s.Blend!.Residual <= new BlendRecognitionOptions().Limit(s.Blend.Size)));
+ var freeform = recognized.Where(f => body.TryGetFaceSurfaceGeometry(f.Face, out var s) && s!.Kind == SurfaceGeometryKind.BSplineSurfaceWithKnots).ToArray();
+ Assert.True(freeform.Count(f => f.Role != BlendFaceRole.Primary) >= 6, string.Join(", ", freeform.Select(f => $"{f.Face.Value}:{f.Role}")));
+ }
+}
diff --git a/Aetheris.Kernel.Core/Brep/Blends/BlendRecognition.cs b/Aetheris.Kernel.Core/Brep/Blends/BlendRecognition.cs
new file mode 100644
index 00000000..4912a8a7
--- /dev/null
+++ b/Aetheris.Kernel.Core/Brep/Blends/BlendRecognition.cs
@@ -0,0 +1,426 @@
+using Aetheris.Kernel.Core.Brep.Tessellation;
+using Aetheris.Kernel.Core.Geometry;
+using Aetheris.Kernel.Core.Judgment;
+using Aetheris.Kernel.Core.Math;
+using Aetheris.Kernel.Core.Topology;
+
+namespace Aetheris.Kernel.Core.Brep.Blends;
+
+public enum BlendSection { Round, Chamfer }
+
+public enum BlendFaceRole { Primary, Strip, Joiner }
+
+/// <summary>One reading of a face as a blend: the two faces it spans, its section, and whether the edge it rounds
+/// is convex (a round: the ball centre is inside the material; a chamfer: measured from its rails).</summary>
+public sealed record BlendHypothesis(FaceId Face, FaceId AnchorA, FaceId AnchorB, BlendSection Section, bool Convex)
+{
+ public string Id => $"{Section.ToString().ToLowerInvariant()}{(Section == BlendSection.Round ? Convex ? "-convex" : "-concave" : "")}:{AnchorA.Value}+{AnchorB.Value}";
+}
+
+/// <summary>
+/// What was measured for a hypothesis, before any judgment. A round: the radius whose ball, centred a radius
+/// behind each sample of the face along its normal, touches both anchors; the residual is the worst miss.
+/// A chamfer: each rail's constant distance to the other anchor; the residual is the worst spread.
+/// </summary>
+public sealed record BlendMeasurement(
+ BlendHypothesis Hypothesis,
+ bool AnchorsAnalytic,
+ double Size,
+ double SecondSize,
+ double Residual,
+ double Coverage,
+ double CollapseMargin,
+ double SpineExtent,
+ double AnchorTurnDegrees,
+ bool BothRailsAlongEdges,
+ int Samples,
+ double Lean = 1);
+
+public sealed record BlendRecognitionOptions
+{
+ /// <summary>Largest round radius or chamfer setback read as a blend (a policy, not geometry).</summary>
+ public double MaxSize { get; init; } = 5;
+ /// <summary>Certificate tolerance: an approximating net (another kernel's B-spline fillet) misses by this.</summary>
+ public double Tolerance { get; init; } = 1e-3;
+ public double TangentDegrees { get; init; } = 3;
+ public int MaxSamples { get; init; } = 64;
+
+ public double Limit(double size) => double.Max(Tolerance, 1e-4 * size);
+}
+
+public sealed record FaceBlendRecognition(FaceId Face, BlendFaceRole Role, BlendMeasurement? Blend, JudgmentModelResult<BlendMeasurement>? Judgment, string Reason);
+
+/// <summary>
+/// Recognizes blend faces by the rolling-ball certificate, independent of how the face is represented: an
+/// analytic cylinder or torus, or another kernel's B-spline net, certifies the same way. Which neighbours a
+/// face spans is a judgment: every pair is measured first, then a <see cref="JudgmentModel{TCandidate,TContext}"/>
+/// admits the readings whose measurements pass and ranks them. Nothing is built.
+/// </summary>
+public static class BlendRecognition
+{
+ public const string PolicyVersion = "blend-recognition/1";
+
+ private const string JoinerReason = "smooth all round and meets a strip";
+
+ /// <summary>"Not bounded" as a finite number: the judgment rejects non-finite evidence by design.</summary>
+ private const double Unbounded = 1e300;
+
+ public static IReadOnlyList<FaceBlendRecognition> Recognize(BrepBody body, BlendRecognitionOptions? options = null)
+ {
+ options ??= new();
+ var tess = BrepDisplayTessellator.Tessellate(body);
+ if (!tess.IsSuccess) throw new InvalidOperationException("blend-recognition: tessellation failed: " + string.Join("; ", tess.Diagnostics.Select(d => d.Message)));
+ var context = new Context(body, tess.Value, options);
+ var results = body.Topology.Faces.OrderBy(f => f.Id.Value).Select(f => RecognizeStrip(context, f.Id, new HashSet<FaceId>())).ToDictionary(r => r.Face);
+ // An anchor is a face that stays. Joiners (corner patches: smooth all round, meeting a strip, spanning no
+ // two faces of their own) are decided from the strips; a strip read against a joiner or another strip (a
+ // transition patch rolling on a fillet of its own radius) is judged again without them, until nothing changes.
+ for (var pass = 0; ; pass++)
+ {
+ foreach (var r in results.Values.Where(r => r.Role == BlendFaceRole.Joiner).ToArray())
+ results[r.Face] = r with { Role = BlendFaceRole.Primary, Reason = r.Reason.Replace(JoinerReason, "").TrimStart(';', ' ') };
+ // A face some strip spans stays: it is an anchor, never a corner patch, however smooth it is.
+ var spanned = results.Values.Where(r => r.Blend is not null).SelectMany(r => new[] { r.Blend!.Hypothesis.AnchorA, r.Blend.Hypothesis.AnchorB }).ToHashSet();
+ foreach (var face in results.Values.Where(r => r.Role == BlendFaceRole.Primary).ToArray())
+ {
+ if (context.Kind(face.Face) == SurfaceGeometryKind.Plane || spanned.Contains(face.Face)) continue;
+ var edges = context.Edges(face.Face);
+ if (edges.Length == 0 || !edges.All(e => context.Tangent(e, face.Face))) continue;
+ if (!edges.Any(e => results[context.Other(e, face.Face)].Role == BlendFaceRole.Strip)) continue;
+ results[face.Face] = face with { Role = BlendFaceRole.Joiner, Reason = JoinerReason + (face.Reason.Length > 0 ? $"; {face.Reason}" : "") };
+ }
+ var blends = results.Values.Where(r => r.Role != BlendFaceRole.Primary).Select(r => r.Face).ToHashSet();
+ var stale = results.Values.Where(r => r.Blend is { } b && (blends.Contains(b.Hypothesis.AnchorA) || blends.Contains(b.Hypothesis.AnchorB))).ToArray();
+ if (stale.Length == 0) break;
+ if (pass == 8) throw new InvalidOperationException($"blend-recognition: anchors did not settle after {pass} passes (faces {string.Join(',', stale.Select(r => r.Face.Value))})");
+ foreach (var r in stale) results[r.Face] = RecognizeStrip(context, r.Face, blends);
+ }
+ return results.Values.OrderBy(r => r.Face.Value).ToArray();
+ }
+
+ private static readonly JudgmentModel<BlendMeasurement, BlendRecognitionOptions> Model = new(PolicyVersion,
+ [
+ new("anchors-analytic", (m, _) => new(m.AnchorsAnalytic, "blend-anchor-not-analytic", m.AnchorsAnalytic ? 1 : 0, 1, "both anchors have a distance function")),
+ new("coverage", (m, _) => new(m.Coverage >= 0.8, "blend-certificate-coverage", m.Coverage, 0.8, "fraction of samples whose ball touches an anchor")),
+ new("size", (m, o) => new(m.Size > 0 && double.Max(m.Size, m.SecondSize) <= o.MaxSize + o.Limit(o.MaxSize), "blend-size-over-limit", double.Max(m.Size, m.SecondSize), o.MaxSize, "radius or setback within the policy limit (a measured size is known to the certificate's tolerance)")),
+ new("residual", (m, o) => new(m.Residual <= o.Limit(m.Size), "blend-certificate-residual", m.Residual, o.Limit(m.Size), "worst miss of the certificate, mm")),
+ new("offset-not-collapsed", (m, o) => new(m.CollapseMargin > CollapseLimit(m, o), "blend-anchor-offset-collapsed", m.CollapseMargin, CollapseLimit(m, o), "an anchor of the ball's own radius (within 1%: approximating nets differ) is a sibling blend, not a face the ball rolls on")),
+ new("spine-is-a-curve", (m, o) => new(m.Hypothesis.Section == BlendSection.Chamfer || m.SpineExtent > 10 * o.Limit(m.Size), "blend-spine-is-a-point", m.SpineExtent, 10 * o.Limit(m.Size), "a ball that does not move is a corner, not a strip")),
+ new("chamfer-leans", (m, _) => new(m.Lean > 0.05, "blend-chamfer-does-not-lean", m.Lean, 0.05, "a chamfer's outward normal leans towards both anchors' (cosine); a wall square to them is not one")),
+ new("anchors-turn-at-all", (m, _) => new(m.AnchorTurnDegrees > 1, "blend-anchors-do-not-turn", m.AnchorTurnDegrees, 1, "degrees between the anchors: tangent anchors have no edge to blend")),
+ new("anchors-turn", (m, _) => new(m.AnchorTurnDegrees < 179, "blend-full-round", m.AnchorTurnDegrees, 179, "section sweep (round) or anchor turn (chamfer), degrees: a full round between facing walls has no sharp edge")),
+ ],
+ [
+ new("certificate", 3, (m, o) => new(Utility.Clamp01(1 - m.Residual / o.Limit(m.Size)), m.Residual, "mm", "residual against its limit")),
+ new("rails-along-edges", 2, (m, _) => new(m.BothRailsAlongEdges ? 1 : 0.5, m.BothRailsAlongEdges ? 1 : 0, "", "both rails are edges (else a point contact)")),
+ new("coverage", 1, (m, _) => new(m.Coverage, m.Coverage, "", "samples certified")),
+ ]);
+
+ private static double CollapseLimit(BlendMeasurement m, BlendRecognitionOptions o) => double.Max(o.Limit(m.Size), 0.01 * m.Size);
+
+ private static FaceBlendRecognition RecognizeStrip(Context c, FaceId face, IReadOnlySet<FaceId> notAnchors)
+ {
+ SurfaceGeometryKind? kind = c.Kind(face);
+ var edges = c.Edges(face);
+ var edgeNeighbours = edges.Select(e => c.Other(e, face)).Where(n => n != face).Distinct().ToArray();
+ var hypotheses = new List<BlendHypothesis>();
+ if (kind is { } k && k != SurfaceGeometryKind.Plane)
+ {
+ // A round's first anchor is a face it is tangent to along an edge; the second may touch it only at a
+ // vertex (a ball whose rail on that side has shrunk to a point).
+ var tangent = edges.Where(e => c.Tangent(e, face)).Select(e => c.Other(e, face)).Where(n => n != face).Distinct().ToArray();
+ var others = edgeNeighbours.Concat(c.VertexNeighbours(face)).Distinct().ToArray();
+ foreach (var a in tangent)
+ foreach (var b in others.Where(b => b != a && (!tangent.Contains(b) || a.Value < b.Value)))
+ foreach (var convex in new[] { true, false })
+ hypotheses.Add(new(face, a, b, BlendSection.Round, convex));
+ }
+ if (kind is SurfaceGeometryKind.Plane or SurfaceGeometryKind.Cone && edgeNeighbours.Length <= 12)
+ foreach (var a in edgeNeighbours)
+ foreach (var b in edgeNeighbours.Where(b => b.Value > a.Value))
+ hypotheses.Add(new(face, a, b, BlendSection.Chamfer, true));
+ hypotheses.RemoveAll(h => notAnchors.Contains(h.AnchorA) || notAnchors.Contains(h.AnchorB));
+ if (hypotheses.Count == 0)
+ return new(face, BlendFaceRole.Primary, null, null, kind == SurfaceGeometryKind.Plane || edges.Length == 0 ? ""
+ : $"no tangent neighbour (closest {edges.Min(e => c.Angle(e, face)):F1}° over a {c.Options.TangentDegrees:F1}° limit)");
+ var measured = hypotheses.Select(h => Measure(c, h)).ToArray();
+ var judgment = Model.Evaluate(c.Options, measured.Select(m => new JudgmentOption<BlendMeasurement>(m.Hypothesis.Id, m)).ToArray());
+ return judgment.Winner is { } winner
+ ? new(face, BlendFaceRole.Strip, winner.Value, judgment, winner.Value.Hypothesis.Id)
+ : new(face, BlendFaceRole.Primary, null, judgment, "no reading admitted");
+ }
+
+ /// <summary>Measure one hypothesis. Rounds use the face's own samples (points and normals); chamfers its rails.</summary>
+ public static BlendMeasurement Measure(BrepBody body, BlendHypothesis hypothesis, BlendRecognitionOptions? options = null)
+ {
+ var tess = BrepDisplayTessellator.Tessellate(body);
+ if (!tess.IsSuccess) throw new InvalidOperationException("blend-recognition: tessellation failed");
+ return Measure(new Context(body, tess.Value, options ?? new()), hypothesis);
+ }
+
+ private static BlendMeasurement Measure(Context c, BlendHypothesis h)
+ {
+ var a = BlendSupport.Of(c.Body, h.AnchorA);
+ var b = BlendSupport.Of(c.Body, h.AnchorB);
+ var railsOnEdges = c.SharesEdge(h.Face, h.AnchorA) && c.SharesEdge(h.Face, h.AnchorB);
+ if (a is null || b is null) return new(h, false, 0, 0, Unbounded, 0, Unbounded, 0, 0, railsOnEdges, 0);
+ return h.Section == BlendSection.Round ? MeasureRound(c, h, a, b, railsOnEdges) : MeasureChamfer(c, h, a, b);
+ }
+
+ private static BlendMeasurement MeasureRound(Context c, BlendHypothesis h, BlendSupport a, BlendSupport b, bool railsOnEdges)
+ {
+ var samples = c.Samples(h.Face);
+ var side = h.Convex ? 1d : -1d;
+ var reach = 2 * c.Options.MaxSize;
+ // A coarse look first: most pairs are nowhere near a ball, and need not be measured in full.
+ var coarse = samples.Where((_, i) => i % double.Max(1, samples.Length / 8) == 0).ToArray();
+ var (r0, cov0) = Radius(coarse);
+ if (r0 is { } guess && Residual(coarse, guess) > 50 * c.Options.Limit(guess))
+ return Result(guess, Residual(coarse, guess), cov0, coarse);
+ var (r, coverage) = Radius(samples);
+ if (r is not { } radius) return new(h, true, 0, 0, Unbounded, coverage, Unbounded, 0, 0, railsOnEdges, samples.Length);
+ return Result(radius, Residual(samples, radius), coverage, samples);
+
+ BlendMeasurement Result(double radius, double residual, double coverage, (Point3D P, Vector3D N)[] used)
+ {
+ var collapse = new[] { a.ConstantRadius, b.ConstantRadius }.Where(x => x is not null).Select(x => double.Abs(x!.Value - radius)).DefaultIfEmpty(Unbounded).Min();
+ var centres = used.Select(s => s.P - s.N * (side * radius)).ToArray();
+ var extent = centres.Length < 2 ? 0 : centres.Max(p => (p - centres[0]).Length);
+ // The section's sweep: the angle between the ball's two feet, 90° at a box edge, 180° for a full round
+ // between facing walls. Support normals are unsigned, so each foot is on the side that reaches its face.
+ var middle = centres.Length > 0 ? centres[centres.Length / 2] : Point3D.Origin;
+ Vector3D Foot(BlendSupport s) { var n = s.Normal(middle); return s.Distance(middle + n * radius) <= s.Distance(middle - n * radius) ? n : n * -1; }
+ var (footA, footB) = (Foot(a), Foot(b));
+ var sweep = double.Acos(double.Clamp(footA.Dot(footB), -1, 1)) * 180 / double.Pi;
+ return new(h, true, radius, 0, residual, coverage, collapse, extent, sweep, railsOnEdges, used.Length);
+ }
+
+ double Residual((Point3D P, Vector3D N)[] used, double radius) => used.Length == 0 ? Unbounded : used.Max(s =>
+ {
+ var centre = s.P - s.N * (side * radius);
+ return double.Max(double.Abs(a.Distance(centre) - radius), double.Abs(b.Distance(centre) - radius));
+ });
+
+ (double? Radius, double Coverage) Radius((Point3D P, Vector3D N)[] used)
+ {
+ // Per sample, from the better-conditioned anchor: the one whose normal is furthest from the face's.
+ var radii = new List<double>();
+ foreach (var (p, n) in used)
+ {
+ var rateA = 1 - double.Abs(a.Normal(p).Dot(n));
+ var rateB = 1 - double.Abs(b.Normal(p).Dot(n));
+ var r = Root(rateA >= rateB ? a : b, p, n) ?? Root(rateA >= rateB ? b : a, p, n);
+ if (r is { } value) radii.Add(value);
+ }
+ if (radii.Count == 0) return (null, 0);
+ radii.Sort();
+ return (radii[radii.Count / 2], radii.Count / (double)used.Length);
+ }
+
+ double? Root(BlendSupport support, Point3D p, Vector3D n)
+ {
+ double G(double r) => support.Distance(p - n * (side * r)) - r;
+ var start = G(0);
+ if (start < 1e-9) return null; // on the anchor: its own rail, no information
+ const int steps = 96;
+ var previous = 0d;
+ for (var k = 1; k <= steps; k++)
+ {
+ var r = reach * k / steps;
+ if (G(r) < 0)
+ {
+ var (lo, hi) = (previous, r);
+ for (var i = 0; i < 60; i++) { var mid = 0.5 * (lo + hi); if (G(mid) < 0) hi = mid; else lo = mid; }
+ return 0.5 * (lo + hi);
+ }
+ previous = r;
+ }
+ return null;
+ }
+ }
+
+ private static BlendMeasurement MeasureChamfer(Context c, BlendHypothesis h, BlendSupport a, BlendSupport b)
+ {
+ // A constant chamfer's rail on one anchor keeps a constant distance to the other.
+ var railA = c.Rail(h.Face, h.AnchorA).Select(b.Distance).ToArray();
+ var railB = c.Rail(h.Face, h.AnchorB).Select(a.Distance).ToArray();
+ if (railA.Length == 0 || railB.Length == 0) return new(h, true, 0, 0, Unbounded, 0, Unbounded, 0, 0, false, 0);
+ var (sa, sb) = (railA.Average(), railB.Average());
+ var spread = double.Max(railA.Max(x => double.Abs(x - sa)), railB.Max(x => double.Abs(x - sb)));
+ // The face itself must run from rail to rail: every sample lies between the anchors' sides, nearer than the sizes.
+ var samples = c.Samples(h.Face);
+ var inside = samples.Count(s => a.Distance(s.P) <= sb + c.Options.Limit(sb) && b.Distance(s.P) <= sa + c.Options.Limit(sa)) / (double)double.Max(1, samples.Length);
+ // Outward normals where the rails are: the face against each anchor, and the anchors against each other.
+ var pa = c.Rail(h.Face, h.AnchorA).ElementAt(c.Rail(h.Face, h.AnchorA).Count() / 2);
+ var pb = c.Rail(h.Face, h.AnchorB).ElementAt(c.Rail(h.Face, h.AnchorB).Count() / 2);
+ if (c.Outward(h.Face, pa) is not { } fa || c.Outward(h.Face, pb) is not { } fb || c.Outward(h.AnchorA, pa) is not { } na || c.Outward(h.AnchorB, pb) is not { } nb)
+ return new(h, true, sa, sb, spread, inside, Unbounded, Unbounded, 0, true, railA.Length + railB.Length, 0);
+ var turn = double.Acos(double.Clamp(na.Dot(nb), -1, 1)) * 180 / double.Pi;
+ var lean = double.Min(fa.Dot(na), fb.Dot(nb));
+ // Across a convex edge the other anchor's rail lies behind this anchor's outward normal.
+ var convex = (pb - pa).Dot(na) < 0;
+ return new(h with { Convex = convex }, true, sa, sb, spread, inside, Unbounded, Unbounded, turn, true, railA.Length + railB.Length, lean);
+ }
+
+ private sealed class Context
+ {
+ private readonly Dictionary<FaceId, DisplayFaceMeshPatch[]> _patches;
+ private readonly Dictionary<EdgeId, IReadOnlyList<Point3D>> _polylines;
+ private readonly Dictionary<EdgeId, FaceId[]> _edgeFaces = new();
+ private readonly Dictionary<FaceId, EdgeId[]> _faceEdges = new();
+ private readonly Dictionary<VertexId, HashSet<FaceId>> _vertexFaces = new();
+ private readonly Dictionary<(EdgeId, FaceId), double> _angle = new();
+ private readonly Dictionary<FaceId, (Point3D P, Vector3D N)[]> _samples = new();
+ private readonly Dictionary<FaceId, BlendSupport?> _supports = new();
+ private readonly Dictionary<(EdgeId, FaceId), (Point3D P, Vector3D N)[]?> _traces = new();
+
+ public Context(BrepBody body, DisplayTessellationResult mesh, BlendRecognitionOptions options)
+ {
+ Body = body; Options = options;
+ _patches = mesh.FacePatches.GroupBy(p => p.FaceId).ToDictionary(g => g.Key, g => g.ToArray());
+ _polylines = mesh.EdgePolylines.ToDictionary(p => p.EdgeId, p => p.Points);
+ var t = body.Topology;
+ foreach (var face in t.Faces)
+ {
+ var edges = face.LoopIds.SelectMany(l => t.GetLoop(l).CoedgeIds).Select(c => t.GetCoedge(c).EdgeId).Distinct().ToArray();
+ _faceEdges[face.Id] = edges;
+ foreach (var e in edges)
+ {
+ _edgeFaces[e] = _edgeFaces.TryGetValue(e, out var list) ? [.. list, face.Id] : [face.Id];
+ var edge = t.GetEdge(e);
+ foreach (var v in new[] { edge.StartVertexId, edge.EndVertexId })
+ {
+ if (!_vertexFaces.TryGetValue(v, out var set)) _vertexFaces[v] = set = [];
+ set.Add(face.Id);
+ }
+ }
+ }
+ }
+
+ public BrepBody Body { get; }
+ public BlendRecognitionOptions Options { get; }
+
+ public SurfaceGeometryKind? Kind(FaceId f) => Body.TryGetFaceSurfaceGeometry(f, out var s) && s is not null ? s.Kind : null;
+
+ public EdgeId[] Edges(FaceId f) => _faceEdges[f].Where(e => _edgeFaces[e].Length == 2).ToArray();
+
+ public FaceId Other(EdgeId e, FaceId f) => _edgeFaces[e][0] == f ? _edgeFaces[e][1] : _edgeFaces[e][0];
+
+ public bool SharesEdge(FaceId f, FaceId g) => Edges(f).Any(e => Other(e, f) == g);
+
+ public FaceId[] VertexNeighbours(FaceId f) => _faceEdges[f].Select(Body.Topology.GetEdge).SelectMany(e => new[] { e.StartVertexId, e.EndVertexId })
+ .SelectMany(v => _vertexFaces[v]).Where(g => g != f).Distinct().ToArray();
+
+ /// <summary>G1 across the edge: the faces' mesh normals agree at interior points of the edge, up to sign (a
+ /// freeform face's display normals need not be oriented, and a valid solid never folds back on itself).</summary>
+ public bool Tangent(EdgeId e, FaceId f) => Angle(e, f) <= Options.TangentDegrees;
+
+ /// <summary>Worst angle between the two faces' normals along the edge, degrees.</summary>
+ public double Angle(EdgeId e, FaceId f)
+ {
+ if (_angle.TryGetValue((e, f), out var known)) return known;
+ var g = Other(e, f);
+ // A freeform side is measured on the edge itself, through its trim curve, when it has one: its nearest
+ // display vertex can be a grid step away, which on a small fillet is several degrees.
+ var trace = Traced(e, f) ?? Traced(e, g);
+ var points = trace?.Select(x => x.P).ToArray() ?? Interior(e);
+ var worst = points.Length == 0 ? 180d : points.Max(p =>
+ {
+ var (na, nb) = (NormalOn(f, e, p), NormalOn(g, e, p));
+ return na is { } x && nb is { } y && x.Length > 0 && y.Length > 0
+ ? double.Acos(double.Clamp(double.Abs(x.Dot(y)) / (x.Length * y.Length), 0, 1)) * 180 / double.Pi : 180;
+ });
+ return _angle[(e, f)] = _angle[(e, g)] = worst;
+ }
+
+ /// <summary>The face's normal at a point of the edge: exact where the face is analytic, through the edge's trim
+ /// curve where it is freeform and has one (nearest traced sample), else its display mesh.</summary>
+ private Vector3D? NormalOn(FaceId f, EdgeId e, Point3D p)
+ {
+ if (Traced(e, f) is { } trace) return trace.MinBy(x => (x.P - p).LengthSquared).N;
+ return SurfaceNormal(f, p);
+ }
+
+ /// <summary>Points and normals of a freeform face along an edge, from the coedge's trim curve; null for an
+ /// analytic face or one without a trim curve on that edge.</summary>
+ private (Point3D P, Vector3D N)[]? Traced(EdgeId e, FaceId f)
+ {
+ if (_traces.TryGetValue((e, f), out var known)) return known;
+ (Point3D P, Vector3D N)[]? result = null;
+ if (Body.TryGetFaceSurfaceGeometry(f, out var surface) && surface?.BSplineSurfaceWithKnots is { } spline)
+ {
+ var t = Body.Topology;
+ var coedge = t.GetFace(f).LoopIds.SelectMany(l => t.GetLoop(l).CoedgeIds).Select(t.GetCoedge).FirstOrDefault(c => c.EdgeId == e);
+ if (coedge is not null && Body.Bindings.TryGetPcurveBinding(coedge.Id, out var binding))
+ {
+ var d = binding.Pcurve.Domain;
+ result = Enumerable.Range(1, 7).Select(i =>
+ {
+ var uv = binding.Pcurve.Evaluate(d.Start + (d.End - d.Start) * i / 8d);
+ return (spline.Evaluate(uv.U, uv.V), SplineNormal(spline, uv.U, uv.V));
+ }).ToArray();
+ }
+ }
+ return _traces[(e, f)] = result;
+ }
+
+ private static Vector3D SplineNormal(Geometry.Surfaces.BSplineSurfaceWithKnots s, double u, double v)
+ {
+ var du = double.Max((s.DomainEndU - s.DomainStartU) * 1e-5, 1e-9);
+ var dv = double.Max((s.DomainEndV - s.DomainStartV) * 1e-5, 1e-9);
+ double U(double x) => double.Clamp(x, s.DomainStartU, s.DomainEndU);
+ double V(double x) => double.Clamp(x, s.DomainStartV, s.DomainEndV);
+ var tu = s.Evaluate(U(u + du), v) - s.Evaluate(U(u - du), v);
+ var tv = s.Evaluate(u, V(v + dv)) - s.Evaluate(u, V(v - dv));
+ // Normalize by hand: the tangents of a small step are tiny, and TryNormalize's guard reads tiny as zero.
+ static Vector3D Unit(Vector3D x) { var l = double.Sqrt(x.X * x.X + x.Y * x.Y + x.Z * x.Z); return l > 0 && double.IsFinite(l) ? new(x.X / l, x.Y / l, x.Z / l) : Vector3D.Zero; }
+ return Unit(Unit(tu).Cross(Unit(tv)));
+ }
+
+ /// <summary>Points of the rail between the face and an anchor: interior points of their shared edges.</summary>
+ public IEnumerable<Point3D> Rail(FaceId f, FaceId anchor) => Edges(f).Where(e => Other(e, f) == anchor).SelectMany(Interior);
+
+ /// <summary>Mesh points with their surface normals, evenly thinned to the sample budget.</summary>
+ public (Point3D P, Vector3D N)[] Samples(FaceId f)
+ {
+ if (_samples.TryGetValue(f, out var known)) return known;
+ var all = (_patches.GetValueOrDefault(f) ?? []).SelectMany(p => p.Positions.Zip(p.Normals)).Where(x => x.Second.Length > 1e-12)
+ .Select(x => (x.First, x.Second * (1 / x.Second.Length))).ToArray();
+ var step = double.Max(1, all.Length / (double)Options.MaxSamples);
+ var thinned = Enumerable.Range(0, (int)double.Min(all.Length, Options.MaxSamples)).Select(i => all[(int)(i * step)]).ToArray();
+ return _samples[f] = thinned;
+ }
+
+ private Point3D[] Interior(EdgeId e)
+ {
+ if (!_polylines.TryGetValue(e, out var line) || line.Count < 2) return [];
+ return line.Count > 2 ? line.Skip(1).Take(line.Count - 2).ToArray() : [line[0] + (line[1] - line[0]) * 0.5];
+ }
+
+ public Vector3D? Outward(FaceId f, Point3D p) => SurfaceNormal(f, p) is { } n && n.Length > 1e-12 ? n * (1 / n.Length) : null;
+
+ /// <summary>The exact support normal for an analytic face, oriented by its display mesh; the mesh normal
+ /// itself for a freeform face (whose nearest mesh vertex may be a grid step away from the point).</summary>
+ private Vector3D? SurfaceNormal(FaceId f, Point3D p)
+ {
+ var mesh = NearestNormal(f, p);
+ if (!_supports.TryGetValue(f, out var support)) _supports[f] = support = BlendSupport.Of(Body, f);
+ if (support is null) return mesh;
+ var exact = support.Normal(p);
+ if (exact.Length < 1e-12) return mesh;
+ return mesh is { } m && exact.Dot(m) < 0 ? exact * -1 : exact;
+ }
+
+ private Vector3D? NearestNormal(FaceId f, Point3D p)
+ {
+ Vector3D? best = null; var distance = Unbounded;
+ foreach (var patch in _patches.GetValueOrDefault(f) ?? [])
+ for (var i = 0; i < patch.Positions.Count; i++)
+ {
+ var d = (patch.Positions[i] - p).LengthSquared;
+ if (d < distance) { distance = d; best = patch.Normals[i]; }
+ }
+ return best;
+ }
+ }
+}
diff --git a/Aetheris.Kernel.Core/Brep/Blends/BlendSupport.cs b/Aetheris.Kernel.Core/Brep/Blends/BlendSupport.cs
new file mode 100644
index 00000000..405638f2
--- /dev/null
+++ b/Aetheris.Kernel.Core/Brep/Blends/BlendSupport.cs
@@ -0,0 +1,107 @@
+using Aetheris.Kernel.Core.Geometry;
+using Aetheris.Kernel.Core.Math;
+using Aetheris.Kernel.Core.Topology;
+
+namespace Aetheris.Kernel.Core.Brep.Blends;
+
+/// <summary>
+/// An analytic face support as a distance function: exact distance to the plane, cylinder, sphere and
+/// torus, and to the cone's generators. A blend's anchors must have one: the rolling-ball certificate
+/// measures how far a ball centre is from them. Freeform supports have none (no projection yet).
+/// </summary>
+public sealed class BlendSupport
+{
+ private readonly SurfaceGeometry _surface;
+
+ private BlendSupport(FaceId face, SurfaceGeometry surface) { Face = face; _surface = surface; }
+
+ public FaceId Face { get; }
+
+ public SurfaceGeometryKind Kind => _surface.Kind;
+
+ /// <summary>The support's radius of curvature where it is constant (cylinder, sphere, torus tube); a ball of
+ /// that radius on that side has a collapsed offset and does not roll on the face.</summary>
+ public double? ConstantRadius => _surface.Kind switch
+ {
+ SurfaceGeometryKind.Cylinder => _surface.Cylinder!.Value.Radius,
+ SurfaceGeometryKind.Sphere => _surface.Sphere!.Value.Radius,
+ SurfaceGeometryKind.Torus => _surface.Torus!.Value.MinorRadius,
+ _ => null,
+ };
+
+ public static BlendSupport? Of(BrepBody body, FaceId face) =>
+ body.TryGetFaceSurfaceGeometry(face, out var surface) && surface is { Kind: SurfaceGeometryKind.Plane or SurfaceGeometryKind.Cylinder
+ or SurfaceGeometryKind.Cone or SurfaceGeometryKind.Sphere or SurfaceGeometryKind.Torus }
+ ? new BlendSupport(face, surface) : null;
+
+ /// <summary>Unsigned distance from the point to the support surface.</summary>
+ public double Distance(Point3D x)
+ {
+ switch (_surface.Kind)
+ {
+ case SurfaceGeometryKind.Plane:
+ {
+ var p = _surface.Plane!.Value;
+ return double.Abs(p.Normal.ToVector().Dot(x - p.Origin));
+ }
+ case SurfaceGeometryKind.Sphere:
+ {
+ var s = _surface.Sphere!.Value;
+ return double.Abs((x - s.Center).Length - s.Radius);
+ }
+ case SurfaceGeometryKind.Cylinder:
+ {
+ var c = _surface.Cylinder!.Value;
+ return double.Abs(Radial(x - c.Origin, c.Axis.ToVector()).Length - c.Radius);
+ }
+ case SurfaceGeometryKind.Cone:
+ {
+ var k = _surface.Cone!.Value;
+ var v = x - k.Apex; var axis = k.Axis.ToVector();
+ var h = v.Dot(axis); var rho = Radial(v, axis).Length;
+ return double.Abs(rho * double.Cos(k.SemiAngleRadians) - double.Abs(h) * double.Sin(k.SemiAngleRadians));
+ }
+ default:
+ {
+ var t = _surface.Torus!.Value;
+ var axis = t.Axis.ToVector();
+ var radial = Radial(x - t.Center, axis);
+ var tube = radial.Length > 1e-12 ? t.Center + radial * (t.MajorRadius / radial.Length) : t.Center;
+ return double.Abs((x - tube).Length - t.MinorRadius);
+ }
+ }
+ }
+
+ /// <summary>Unit normal of the support at (or nearest) the point, unsigned: only its line matters here.</summary>
+ public Vector3D Normal(Point3D x)
+ {
+ Vector3D n = _surface.Kind switch
+ {
+ SurfaceGeometryKind.Plane => _surface.Plane!.Value.Normal.ToVector(),
+ SurfaceGeometryKind.Sphere => x - _surface.Sphere!.Value.Center,
+ SurfaceGeometryKind.Cylinder => Radial(x - _surface.Cylinder!.Value.Origin, _surface.Cylinder!.Value.Axis.ToVector()),
+ SurfaceGeometryKind.Cone => ConeNormal(x),
+ _ => TorusNormal(x),
+ };
+ return n.TryNormalize(out var unit) ? unit : Vector3D.Zero;
+ }
+
+ private Vector3D ConeNormal(Point3D x)
+ {
+ var k = _surface.Cone!.Value; var axis = k.Axis.ToVector();
+ var v = x - k.Apex; var h = v.Dot(axis);
+ var radial = Radial(v, axis);
+ if (!radial.TryNormalize(out var unit)) return axis;
+ return unit * double.Cos(k.SemiAngleRadians) - axis * (double.Sin(k.SemiAngleRadians) * double.Sign(h));
+ }
+
+ private Vector3D TorusNormal(Point3D x)
+ {
+ var t = _surface.Torus!.Value; var axis = t.Axis.ToVector();
+ var radial = Radial(x - t.Center, axis);
+ var tube = radial.Length > 1e-12 ? t.Center + radial * (t.MajorRadius / radial.Length) : t.Center;
+ return x - tube;
+ }
+
+ private static Vector3D Radial(Vector3D v, Vector3D axis) => v - axis * v.Dot(axis);
+}
diff --git a/Aetheris.Kernel.Firmament.Tests/Experiments/BlendNetworkRemovalTests.cs b/Aetheris.Kernel.Firmament.Tests/Experiments/BlendNetworkRemovalTests.cs
index a2bdceb1..ce589de4 100644
--- a/Aetheris.Kernel.Firmament.Tests/Experiments/BlendNetworkRemovalTests.cs
+++ b/Aetheris.Kernel.Firmament.Tests/Experiments/BlendNetworkRemovalTests.cs
@@ -225,6 +225,9 @@ public sealed class BlendNetworkRemovalReach
var body = import.Value;
BrepMassProperties.TryEvaluateExactVolume(body, out var before);
var probe = BlendRegionProbe.Run(Path.GetFileName(path), body);
+ var recognition = Aetheris.Kernel.Core.Brep.Blends.BlendRecognition.Recognize(body).ToDictionary(r => r.Face);
+ var railLength = Aetheris.Kernel.Core.Brep.Tessellation.BrepDisplayTessellator.Tessellate(body).Value.EdgePolylines
+ .ToDictionary(p => p.EdgeId, p => p.Points.Zip(p.Points.Skip(1)).Sum(q => (q.Second - q.First).Length));
var baseline = Step242Exporter.ExportBody(body);
var baselineBack = baseline.IsSuccess ? Step242Importer.ImportBody(baseline.Value) : null;
lines.Add($" {Path.GetFileNameWithoutExtension(path)} baseline round trip: {(baselineBack is { IsSuccess: true } ? "ok" : baseline.IsSuccess ? "reimport fails: " + baselineBack!.Diagnostics.FirstOrDefault()?.Message : "export fails: " + baseline.Diagnostics.FirstOrDefault()?.Message)}");
@@ -255,12 +258,36 @@ public sealed class BlendNetworkRemovalReach
// A convex blend's removal adds material and a concave one's removes it; either way the
// change is bounded by the blend's area times the largest admitted radius.
var stripArea = region.Faces.Sum(f => probe.FaceReports.Single(r => r.Face == f).Area);
+ // Expected change from the certificate alone: each strip's section area times its length
+ // (mean of its rails). Corner joiners are not modelled, so those regions report separately.
+ var hasCorners = region.Faces.Any(f => recognition[new FaceId(f)].Role != Aetheris.Kernel.Core.Brep.Blends.BlendFaceRole.Strip);
+ var expected = region.Faces.Sum(f =>
+ {
+ var b = recognition[new FaceId(f)].Blend;
+ if (b is null) return 0d;
+ var t = body.Topology;
+ double Rail(FaceId anchor) => t.GetFace(new FaceId(f)).LoopIds.SelectMany(l => t.GetLoop(l).CoedgeIds).Select(c => t.GetCoedge(c).EdgeId).Distinct()
+ .Where(e => t.Faces.Any(o => o.Id == anchor && o.LoopIds.SelectMany(l => t.GetLoop(l).CoedgeIds).Any(c => t.GetCoedge(c).EdgeId == e))).Sum(e => railLength.GetValueOrDefault(e));
+ var length = (Rail(b.Hypothesis.AnchorA) + Rail(b.Hypothesis.AnchorB)) / 2;
+ var theta = b.AnchorTurnDegrees * Math.PI / 180;
+ var area = b.Hypothesis.Section == Aetheris.Kernel.Core.Brep.Blends.BlendSection.Round
+ ? b.Size * b.Size * (Math.Tan(theta / 2) - theta / 2)
+ : b.Size * b.SecondSize / (2 * Math.Sin(theta));
+ return (b.Hypothesis.Convex ? 1 : -1) * area * length;
+ });
if (false)
{
var before0 = BrepPcurveValidator.Validate(body, 1e-5);
var after0 = BrepPcurveValidator.Validate(removed.Body, 1e-5);
lines.Add($" {Path.GetFileNameWithoutExtension(path)} R{region.Id} pcurves before: {string.Join(" | ", before0.Diagnostics.Take(2))} after: {string.Join(" | ", after0.Diagnostics.Take(3))}");
}
+ if (back is { IsSuccess: true } && Math.Abs(expected) > 1e-9)
+ {
+ var error = Math.Abs(grew - expected) / Math.Abs(expected);
+ var bucket = (hasCorners ? " volume vs certificate (with corners) " : " volume vs certificate (strips only) ") + (error <= 0.02 ? "within 2%" : error <= 0.1 ? "within 10%" : "off >10%");
+ total[bucket] = total.GetValueOrDefault(bucket) + region.Faces.Length;
+ if (!hasCorners && error > 0.1) lines.Add($" {Path.GetFileNameWithoutExtension(path)} R{region.Id} volume {grew:G5} vs certificate {expected:G5}");
+ }
if (back is { IsSuccess: false }) lines.Add($" {Path.GetFileNameWithoutExtension(path)} R{region.Id} reimport: {back.Diagnostics.FirstOrDefault()?.Message}");
if (step is { IsSuccess: false }) lines.Add($" {Path.GetFileNameWithoutExtension(path)} R{region.Id} export: {step.Diagnostics.FirstOrDefault()?.Message}");
verdict = !valid ? "removed-invalid" : back is not { IsSuccess: true } ? "removed-reimport-failed"
diff --git a/Aetheris.Kernel.Firmament.Tests/Experiments/BlendRegionProbe.cs b/Aetheris.Kernel.Firmament.Tests/Experiments/BlendRegionProbe.cs
index c6026043..c2f603db 100644
--- a/Aetheris.Kernel.Firmament.Tests/Experiments/BlendRegionProbe.cs
+++ b/Aetheris.Kernel.Firmament.Tests/Experiments/BlendRegionProbe.cs
@@ -1,4 +1,5 @@
using Aetheris.Kernel.Core.Brep;
+using Aetheris.Kernel.Core.Brep.Blends;
using Aetheris.Kernel.Core.Brep.Tessellation;
using Aetheris.Kernel.Core.Geometry;
using Aetheris.Kernel.Core.Math;
@@ -14,7 +15,7 @@ namespace Aetheris.Kernel.Firmament.Tests.Experiments;
/// </summary>
internal static class BlendRegionProbe
{
- internal sealed record Options(double MaxBlendRadius = 5, double TangentDegrees = 3)
+ internal sealed record Options(double MaxBlendRadius = 5, double TangentDegrees = 3, bool Certificate = true)
{
// A 45° chamfer of size c is c·√2 wide; admit chamfers up to the blend-radius limit.
public double MaxChamferWidth => MaxBlendRadius * Math.Sqrt(2) + 1e-6;
@@ -60,8 +61,26 @@ internal static class BlendRegionProbe
.Where(edges.ContainsKey).Select(e => edges[e]).Distinct().ToArray());
var reports = new Dictionary<FaceId, FaceReport>();
+ // The rolling-ball certificate (Core BlendRecognition) decides strips, joiners and anchors; the shape
+ // heuristics below remain for comparison (Options.Certificate = false).
+ var recognized = options.Certificate
+ ? BlendRecognition.Recognize(body, new BlendRecognitionOptions { MaxSize = options.MaxBlendRadius, TangentDegrees = options.TangentDegrees }).ToDictionary(r => r.Face)
+ : null;
foreach (var face in body.Topology.Faces)
{
+ if (recognized is not null)
+ {
+ body.TryGetFaceSurfaceGeometry(face.Id, out var g);
+ var mineEdges = faceEdges[face.Id];
+ var r = recognized[face.Id];
+ var tangentCount = mineEdges.Count(e => e.Tangent);
+ var innerLoops = face.LoopIds.Count(l => body.Bindings.FaceBoundaryRoleBindings.Any(b => b.FaceId == face.Id && b.LoopId == l && b.Role == FaceBoundaryRole.Inner));
+ var holeLike = g?.Cylinder is { } cyl && IsConcave(cyl, patches.GetValueOrDefault(face.Id) ?? []);
+ reports[face.Id] = new(face.Id.Value, (g?.Kind ?? SurfaceGeometryKind.Plane).ToString(), r.Role == BlendFaceRole.Primary ? "primary" : "secondary",
+ r.Role switch { BlendFaceRole.Strip when r.Blend!.Hypothesis.Section == BlendSection.Chamfer => "chamfer-strip", BlendFaceRole.Strip => "round-certified", BlendFaceRole.Joiner => "corner-patch-candidate", _ => holeLike && tangentCount == 0 ? "hole-or-cut-wall" : r.Reason },
+ r.Blend?.Size, Area(patches.GetValueOrDefault(face.Id) ?? []), tangentCount, mineEdges.Length - tangentCount, innerLoops, holeLike);
+ continue;
+ }
body.TryGetFaceSurfaceGeometry(face.Id, out var surface);
var kind = surface?.Kind ?? SurfaceGeometryKind.Plane;
var mine = faceEdges[face.Id];
@@ -154,7 +173,9 @@ internal static class BlendRegionProbe
// Per member: distinct primaries it is tangent to (2 = strip, 3 = corner, 0-1 = blend meeting blends).
// Anchors: the primaries a blend face actually spans. A fillet's are its tangent neighbours;
// a chamfer's are the neighbours it leans 20-70° against (its end caps meet it near 90°).
- var faceAnchors = members.OrderBy(f => f.Value).Select(f => faceEdges[f]
+ var faceAnchors = recognized is not null
+ ? members.OrderBy(f => f.Value).Select(f => recognized[f].Blend is { } b ? new[] { b.Hypothesis.AnchorA.Value, b.Hypothesis.AnchorB.Value }.Order().ToArray() : []).ToArray()
+ : members.OrderBy(f => f.Value).Select(f => faceEdges[f]
.Where(e => reports[f].Reason == "chamfer-strip" ? e.MaxAngleDegrees is > 20 and < 70 : e.Tangent)
.Select(e => e.Faces[0] == f ? e.Faces[1] : e.Faces[0]).Where(n => reports[n].Role == "primary").Distinct().Select(n => n.Value).Order().ToArray()).ToArray();
var bridges = faceAnchors.Select(a => a.Length).ToArray();
diff --git a/docs/development/reports/2026-11/BLEND-RECOGNITION-CERTIFICATE.md b/docs/development/reports/2026-11/BLEND-RECOGNITION-CERTIFICATE.md
new file mode 100644
index 00000000..bc974f78
--- /dev/null
+++ b/docs/development/reports/2026-11/BLEND-RECOGNITION-CERTIFICATE.md
@@ -0,0 +1,62 @@
+# Blend recognition by the rolling-ball certificate
+
+**Goal.** Recognize blend faces by what they are, not by surface kind: another kernel's B-spline
+fillet (Fusion today, SolidCraft's certified nets tomorrow) must read the same as an analytic
+torus. Choose which neighbours a face spans as an explicit judgment. Graduated into Core
+(`Aetheris.Kernel.Core.Brep.Blends`), additive: nothing existing calls it except the blend
+clean-up probe.
+
+Idea taken from solidcore's generic-blend design (rails are where the ball touches each face;
+see `claude/solidcore-blend-takeaways.md` in the project); the certificate, the judgment and
+the anchor rules are ours.
+
+## Method
+
+- **Certificate (round).** For sample points p of the face with normal n, the ball centre
+ p ∓ r·n is at distance r from both anchors. r is solved per sample from the better-conditioned
+ anchor, the median taken, then every sample is checked at that r. The residual is the worst
+ miss. Representation-free: samples come from the display mesh; anchors need a distance
+ function (plane, cylinder, cone, sphere, torus; `BlendSupport`).
+- **Certificate (chamfer).** Each rail keeps a constant distance to the other anchor; the face
+ leans towards both anchors; convexity from the rails.
+- **Judgment.** Every reading (anchor pair × round/chamfer × convex/concave) is measured first,
+ then a `JudgmentModel` (policy `blend-recognition/1`) admits and ranks them. Hard rules carry
+ measured vs limit: anchors analytic, coverage, size (policy limit), residual, offset not
+ collapsed (an anchor of the ball's own radius, within 1%), centre path is a curve (a ball that
+ does not move is a corner), anchors turn (1°..179°: no full rounds), chamfer leans.
+- **Anchors stay.** A strip read against another blend (a transition patch rolling on a fillet of
+ its own radius) is judged again without the blends, to a fixed point. A face some strip spans is
+ never a corner patch. Joiners: smooth all round, meeting a strip, spanning nothing.
+- **Normals.** Exact support normals for analytic faces (the nearest display vertex can be a row
+ away: 18.7° on a coarse cylinder); freeform faces are measured on the edge through its trim
+ curve.
+
+## Results
+
+Core tests (8): all five rounded Platonic solids read every edge as a convex strip at the exact
+radius (residual < 1e-9) and every corner as a joiner; plain solids read nothing; a wrong-side
+reading is refused with its measured miss; a 3 mm round under a 2 mm limit refuses naming 3;
+SO-101 certifies analytic and freeform blends with no strip read against a blend.
+
+Blend clean-up reach (imported corpus, probe anchors from the certificate instead of shape
+heuristics):
+
+| | heuristic (E2b) | certificate |
+|---|---|---|
+| removed, valid, round-trips, volume plausible | 754 | **802** |
+| of which full pcurve round trip | 578 | **693** |
+
+STC-07's 20 B-spline corner patches and SO-101's 10 now read as joiners; FTC-10 +12, SO-101 +8.
+
+**Volume against the certificate** (new verifier): each removed strip predicts its volume change
+as section area (r²(tan(θ/2) − θ/2), or s_a·s_b/(2 sin θ)) times its length. Strip-only regions:
+61 faces within 2%, 125 within 10%, 45 off. It flags FTC-06 R16 (−3,344 mm³ measured, −237
+predicted), the source-tessellation error found by hand before. Most of the 45 are ring strips
+(FTC-08, constant ratio 1.34): the length is the rails' mean, not the section centroid's
+(Pappus); a limitation of the prediction.
+
+## Open
+
+- Freeform anchors (no distance function yet): `freeform-neighbour-not-extendable` 186 faces.
+- Point-contact strips (the chimera sphere; item 2 of the plan) read as joiners for now.
+- Pappus length for curved strips in the volume prediction.