Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
Materialize mixed-shell chamfers through analytic BRep
Yuechen Li4cd4fa6b9063
diff --git a/Aetheris.Kernel.Firmament.Tests/Integration/ProfileBoundaryChamferTests.cs b/Aetheris.Kernel.Firmament.Tests/Integration/ProfileBoundaryChamferTests.csindex c0e05034..d2f8f753 100644--- a/Aetheris.Kernel.Firmament.Tests/Integration/ProfileBoundaryChamferTests.cs+++ b/Aetheris.Kernel.Firmament.Tests/Integration/ProfileBoundaryChamferTests.cs@@ -1,4 +1,5 @@using Aetheris.Kernel.Firmament.Materializer;+using Aetheris.Kernel.Core.Brep.Verification;using Aetheris.Kernel.Core.Geometry;namespace Aetheris.Kernel.Firmament.Tests.Integration;@@ -199,13 +200,15 @@ public sealed class ProfileBoundaryChamferTests}[Fact]- public void CurvedWholeLoopFinishesNameTheFirstArcStationAndRequiredExactPlanner()+ public void CurvedWholeLoopChamferMaterializesThroughTheMixedPlaneConeShell(){const string chamfer = "Modify Body { EdgeFinish TopBreak { Target: Bracket.Outer On: Top Kind: Chamfer Distance: 4mm } }";Assert.True(ProfileBoundaryChamferSourceBinder.TryBind(chamfer, CurvedProfile(), "Bracket", out var chamferTarget, out var distance, out var chamferDiagnostic), chamferDiagnostic);var chamferPlan = ProfileBoundaryChamferPlanner.TryPlan(CurvedProfile(), chamferTarget!, distance);- Assert.False(chamferPlan.Succeeded);- Assert.StartsWith("ProfileBoundaryChamferArcMaterializationNotImplemented:station=ReflexSmall:scope=Target:segment=ReflexSmallArc:sourceFamily=Arc:material=Reflex:sourceRadius=2:finishDistance=4:radiusRelation=LessThan:planner=ArcChamferConePlan:surfaceFamily=Cone:regularity=Regular:admission=Supported", Assert.Single(chamferPlan.Diagnostics));+ Assert.True(chamferPlan.Succeeded, string.Join(Environment.NewLine, chamferPlan.Diagnostics));+ Assert.NotNull(chamferPlan.Body);+ Assert.Equal(1, chamferPlan.Body!.Geometry.Surfaces.Count(item => item.Value.Kind == SurfaceGeometryKind.Cone));+ Assert.True(BrepMassProperties.Evaluate(chamferPlan.Body).IsEnclosed);const string fillet = "Modify Body { EdgeFinish TopRound { Target: Bracket.Outer On: Top Kind: Fillet Radius: 4mm } }";Assert.True(ProfileBoundaryChamferSourceBinder.TryBindFillet(fillet, CurvedProfile(), "Bracket", out var filletTarget, out var radius, out var clearance, out var filletDiagnostic), filletDiagnostic);diff --git a/Aetheris.Kernel.Firmament.Tests/Integration/ProfileEdgeFinishMixedShellPlanTests.cs b/Aetheris.Kernel.Firmament.Tests/Integration/ProfileEdgeFinishMixedShellPlanTests.csnew file mode 100644index 00000000..9d2a95de--- /dev/null+++ b/Aetheris.Kernel.Firmament.Tests/Integration/ProfileEdgeFinishMixedShellPlanTests.cs@@ -0,0 +1,122 @@+using Aetheris.Kernel.Firmament.FirmamentV2;+using Aetheris.Kernel.Firmament.Materializer;+using Aetheris.Kernel.Core.Geometry;+using Aetheris.Kernel.Core.Step242;++namespace Aetheris.Kernel.Firmament.Tests.Integration;++public sealed class ProfileEdgeFinishMixedShellPlanTests+{+ [Fact]+ public void SevenStationChamfer_ExportsOneDeterministicMixedAnalyticShell()+ {+ var source = FirmamentCorpusHarness.ResolveFixtureFullPath("fixtures/FirmamentV2/Canonical/valid/profile-edgefinish-chimera-chamfer.firmament");+ var first = FirmamentBuildAndExport.Run(source, Path.Combine(Path.GetTempPath(), $"aetheris-{Guid.NewGuid():N}.step"));+ var second = FirmamentBuildAndExport.Run(source, Path.Combine(Path.GetTempPath(), $"aetheris-{Guid.NewGuid():N}.step"));++ Assert.True(first.IsSuccess, string.Join(Environment.NewLine, first.Diagnostics.Select(x => x.Message)));+ Assert.True(second.IsSuccess, string.Join(Environment.NewLine, second.Diagnostics.Select(x => x.Message)));+ Assert.Equal(first.Value.Export.StepText, second.Value.Export.StepText);+ var imported = Step242Importer.ImportBody(first.Value.Export.StepText);+ Assert.True(imported.IsSuccess, string.Join(Environment.NewLine, imported.Diagnostics.Select(x => x.Message)));+ Assert.NotNull(imported.Value);+ Assert.Equal(5, imported.Value!.Geometry.Surfaces.Count(x => x.Value.Kind == SurfaceGeometryKind.Cone));+ Assert.Equal(0, imported.Value.Geometry.Surfaces.Count(x => x.Value.Kind == SurfaceGeometryKind.BSplineSurfaceWithKnots));+ }++ [Fact]+ public void SevenStationChamfer_ProducesOneClosedSourceOrderedPlaneConePlan()+ {+ var (profile, target) = ReleaseCard("profile-edgefinish-chimera-chamfer.firmament", ProfileEdgeFinishKind.Chamfer);++ var result = ProfileEdgeFinishMixedShellPlanner.TryPlan(profile, target, ProfileEdgeFinishKind.Chamfer, 4d);++ Assert.True(result.Succeeded, string.Join(Environment.NewLine, result.Diagnostics));+ var plan = Assert.IsType<ProfileEdgeFinishMixedShellPlan>(result.Plan);+ Assert.Equal(profile.Loops.Single().Segments.Select(x => x.Name), plan.OrderedPatches.Select(x => x.SegmentId));+ Assert.Equal(17, plan.OrderedPatches.Count);+ Assert.Equal(12, plan.OrderedPatches.OfType<PlanarChamferPatch>().Count());+ Assert.Equal(5, plan.OrderedPatches.OfType<ConicalChamferPatch>().Count());+ var apex = Assert.Single(plan.OrderedPatches.OfType<ConicalChamferPatch>(), x => x.SegmentId == "ConvexMediumArc");+ Assert.Equal(ProfileEdgeFinishRegularity.BoundedDegenerate, apex.Regularity);+ Assert.Equal(0d, apex.InsetRadius);+ Assert.Contains("ConeApex:ConvexMediumArc:vertex=ConvexMediumArc:cap", plan.DegenerateVertices);+ Assert.Equal(17, plan.OrderedSeams.Count);+ Assert.Equal(10, plan.OrderedSeams.OfType<PlaneConeSeam>().Count());+ Assert.Equal("LeftSide", plan.OrderedSeams[^1].PredecessorPatchId.Split(':').Last());+ Assert.Equal("Bottom", plan.OrderedSeams[^1].SuccessorPatchId.Split(':').Last());+ Assert.All(plan.OrderedSeams, seam => Assert.True(seam.TraversesWithCurveParameter));+ Assert.DoesNotContain(plan.OrderedPatches.SelectMany(x => x.SemanticDescendants), x => x.Contains("Termination", StringComparison.Ordinal));+ }++ [Fact]+ public void RoundedConcaveChamfer_OwnsOneConicalFrustumSectorInsteadOfAPlanarMiter()+ {+ var (profile, target) = ReleaseCard("profile-edgefinish-chimera-chamfer.firmament", ProfileEdgeFinishKind.Chamfer);+ var planned = ProfileEdgeFinishMixedShellPlanner.TryPlan(profile, target, ProfileEdgeFinishKind.Chamfer, 4d);++ Assert.True(planned.Succeeded, string.Join(Environment.NewLine, planned.Diagnostics));+ var plan = Assert.IsType<ProfileEdgeFinishMixedShellPlan>(planned.Plan);+ var reflexSmall = Assert.Single(plan.OrderedPatches.OfType<ConicalChamferPatch>(), x => x.SegmentId == "ReflexSmallArc");+ Assert.Equal(ConicalChamferTrimTopology.FrustumSector, reflexSmall.TrimTopology);+ Assert.Equal(2d, reflexSmall.SourceRadius);+ Assert.Equal(6d, reflexSmall.InsetRadius);++ var emitted = ProfileEdgeFinishMixedShellMaterializer.TryMaterializeChamfer(profile, target, plan);+ Assert.True(emitted.Succeeded, string.Join(Environment.NewLine, emitted.Diagnostics));+ var body = Assert.IsType<Aetheris.Kernel.Core.Brep.BrepBody>(emitted.Body);+ var correspondence = Assert.IsType<SemanticTopologyCorrespondence>(emitted.Correspondence);+ var coneFaceId = Assert.Single(correspondence.Descendants,+ x => x.Kind == "Face" && x.StableId.EndsWith(":chamfer:ReflexSmallArc", StringComparison.Ordinal)).Face;+ Assert.True(coneFaceId.HasValue);+ var coneFace = body.Topology.GetFace(coneFaceId.Value);+ var coneLoop = body.Topology.GetLoop(Assert.Single(coneFace.LoopIds));+ var boundaryCurves = coneLoop.CoedgeIds+ .Select(body.Topology.GetCoedge)+ .Select(x => body.Geometry.GetCurve(body.Bindings.GetEdgeBinding(x.EdgeId).CurveGeometryId))+ .ToArray();++ Assert.Equal(2, boundaryCurves.Count(x => x.Kind == CurveGeometryKind.Circle3));+ Assert.Equal(2, boundaryCurves.Count(x => x.Kind == CurveGeometryKind.Line3));+ Assert.Equal([2d, 6d], boundaryCurves+ .Where(x => x.Kind == CurveGeometryKind.Circle3)+ .Select(x => Assert.IsType<Aetheris.Kernel.Core.Geometry.Curves.Circle3Curve>(x.Circle3).Radius)+ .Order()+ .ToArray());+ Assert.Contains(correspondence.Descendants, x => x.Kind == "Edge" && x.GeometryPreview == "PlaneConeSeam:Line:sameSense=True");+ }++ [Fact]+ public void SevenStationFillet_ProducesOneClosedSourceOrderedCylinderTorusSpherePlan()+ {+ var (profile, target) = ReleaseCard("profile-edgefinish-chimera-fillet.firmament", ProfileEdgeFinishKind.Fillet);++ var result = ProfileEdgeFinishMixedShellPlanner.TryPlan(profile, target, ProfileEdgeFinishKind.Fillet, 4d);++ Assert.True(result.Succeeded, string.Join(Environment.NewLine, result.Diagnostics));+ var plan = Assert.IsType<ProfileEdgeFinishMixedShellPlan>(result.Plan);+ Assert.Equal(12, plan.OrderedPatches.OfType<CylindricalFilletPatch>().Count());+ Assert.Single(plan.OrderedPatches.OfType<SphericalFilletPatch>());+ var tori = plan.OrderedPatches.OfType<ToroidalFilletPatch>().OrderBy(x => x.SegmentId).ToArray();+ Assert.Equal(4, tori.Length);+ Assert.Equal(ProfileEdgeFinishTorusRegime.Horn, Assert.Single(tori, x => x.SegmentId == "ConvexLargeArc").Regime);+ Assert.All(tori.Where(x => x.SegmentId.StartsWith("Reflex", StringComparison.Ordinal)), x => Assert.Equal(ProfileEdgeFinishTorusRegime.Ring, x.Regime));+ Assert.Contains("SphereLimit:ConvexMediumArc:vertex=ConvexMediumArc:cap", plan.DegenerateVertices);+ Assert.Equal(8, plan.OrderedSeams.OfType<CylinderTorusSeam>().Count());+ Assert.Equal(2, plan.OrderedSeams.OfType<CylinderSphereSeam>().Count());+ Assert.All(plan.OrderedSeams.Where(x => x is CylinderTorusSeam or CylinderSphereSeam), x => Assert.Equal("Circle", x.CurveFamily));+ Assert.DoesNotContain(plan.OrderedPatches.SelectMany(x => x.SemanticDescendants), x => x.Contains("Termination", StringComparison.Ordinal));+ }++ private static (ResolvedProfile2D Profile, ProfileBoundaryChamferTarget Target) ReleaseCard(string fixture, ProfileEdgeFinishKind kind)+ {+ var source = File.ReadAllText(FirmamentCorpusHarness.ResolveFixtureFullPath($"fixtures/FirmamentV2/Canonical/valid/{fixture}"));+ var parsed = ProfileAuthoringParser.Parse(source);+ var profile = Assert.IsType<ResolvedProfile2D>(parsed.Profile);+ var bound = kind == ProfileEdgeFinishKind.Chamfer+ ? ProfileBoundaryChamferSourceBinder.TryBind(source, profile, profile.Name, out var target, out _, out var diagnostic)+ : ProfileBoundaryChamferSourceBinder.TryBindFillet(source, profile, profile.Name, out target, out _, out _, out diagnostic);+ Assert.True(bound, diagnostic);+ return (profile, Assert.IsType<ProfileBoundaryChamferTarget>(target));+ }+}diff --git a/Aetheris.Kernel.Firmament/Materializer/ProfileBoundaryChamfer.cs b/Aetheris.Kernel.Firmament/Materializer/ProfileBoundaryChamfer.csindex a7251e43..9bdae1da 100644--- a/Aetheris.Kernel.Firmament/Materializer/ProfileBoundaryChamfer.cs+++ b/Aetheris.Kernel.Firmament/Materializer/ProfileBoundaryChamfer.cs@@ -235,7 +235,14 @@ public static class ProfileBoundaryChamferPlannerif (!loop.IsOuter) return Fail("ProfileBoundaryChamferInnerLoopUnsupported");if (profile.Loops.Count != 1) return Fail("ProfileBoundaryChamferInnerLoopUnsupported");if (loop.Segments.Any(x => x.Geometry is not LineArcLineSegment2D))- return Fail(ProfileBoundaryCurvedFinishDiagnostic.ForFirstSelectedArc(profile, loop, target, distance, "Chamfer") ?? "ProfileBoundaryChamferSegmentKindUnsupported");+ {+ var mixed = ProfileEdgeFinishMixedShellPlanner.TryPlan(profile, target, ProfileEdgeFinishKind.Chamfer, distance);+ if (!mixed.Succeeded || mixed.Plan is null)+ return Fail(ProfileBoundaryCurvedFinishDiagnostic.ForFirstSelectedArc(profile, loop, target, distance, "Chamfer")+ ?? mixed.Diagnostics.FirstOrDefault()+ ?? "ProfileBoundaryChamferSegmentKindUnsupported");+ return ProfileEdgeFinishMixedShellMaterializer.TryMaterializeChamfer(profile, target, mixed.Plan);+ }var lines = loop.Segments.Select(x => (LineArcLineSegment2D)x.Geometry).ToArray();var selected = target.SegmentIds.Select(id => Array.FindIndex(loop.Segments.ToArray(), x => x.Name == id)).ToHashSet();if (selected.Count != target.SegmentIds.Count || selected.Any(x => x < 0)) return Fail("ProfileBoundaryChamferSegmentUnknown");diff --git a/Aetheris.Kernel.Firmament/Materializer/ProfileEdgeFinishMixedShellPlan.cs b/Aetheris.Kernel.Firmament/Materializer/ProfileEdgeFinishMixedShellPlan.csnew file mode 100644index 00000000..3cd8f76c--- /dev/null+++ b/Aetheris.Kernel.Firmament/Materializer/ProfileEdgeFinishMixedShellPlan.cs@@ -0,0 +1,566 @@+using System.Globalization;+using System.Text.RegularExpressions;+using Aetheris.Kernel.Core.Brep;+using Aetheris.Kernel.Core.Geometry;+using Aetheris.Kernel.Core.Geometry.Curves;+using Aetheris.Kernel.Core.Geometry.Surfaces;+using Aetheris.Kernel.Core.Math;+using Aetheris.Kernel.Core.Topology;++namespace Aetheris.Kernel.Firmament.Materializer;++/// <summary>+/// Source-order, pre-emission description of a complete Profile edge finish.+/// This is intentionally a plan rather than a B-rep patch list: every analytic+/// patch and every shared curve is known before topology is allocated.+/// </summary>+public sealed record ProfileEdgeFinishMixedShellPlan(+ ProfileBoundaryChamferTarget Target,+ ProfileEdgeFinishKind FinishKind,+ double FinishSize,+ IReadOnlyList<AnalyticEdgeFinishPatch> OrderedPatches,+ IReadOnlyList<AnalyticEdgeFinishSeam> OrderedSeams,+ IReadOnlyList<string> DegenerateVertices,+ string TopCapPlan,+ string SideTrimPlan,+ IReadOnlyList<string> Provenance);++public sealed record ProfileEdgeFinishMixedShellPlanResult(+ bool Succeeded,+ ProfileEdgeFinishMixedShellPlan? Plan,+ IReadOnlyList<string> Diagnostics);++/// <summary>Typed analytic patch base; nullable-heavy surface records are deliberately avoided.</summary>+public abstract record AnalyticEdgeFinishPatch(+ string StableId,+ string SegmentId,+ ProfileEdgeFinishSurfaceFamily SurfaceFamily,+ ProfileEdgeFinishRegularity Regularity,+ string PlannerKind,+ string LocalFrame,+ string LowerBoundary,+ string UpperBoundary,+ string SideStartBoundary,+ string SideEndBoundary,+ IReadOnlyList<string> SemanticDescendants);++public sealed record PlanarChamferPatch(+ string StableId, string SegmentId, PlaneSurface Surface, string LocalFrame,+ IReadOnlyList<string> SemanticDescendants)+ : AnalyticEdgeFinishPatch(StableId, SegmentId, ProfileEdgeFinishSurfaceFamily.Plane,+ ProfileEdgeFinishRegularity.Regular, "LineChamferPlan", LocalFrame,+ "source-line@transition", "inset-line@cap", "line-seam:start", "line-seam:end", SemanticDescendants);++public sealed record ConicalChamferPatch(+ string StableId, string SegmentId, ConeSurface Surface, double SourceRadius,+ double InsetRadius, ProfileEdgeFinishRegularity Regularity, ConicalChamferTrimTopology TrimTopology, string LocalFrame,+ IReadOnlyList<string> SemanticDescendants)+ : AnalyticEdgeFinishPatch(StableId, SegmentId, ProfileEdgeFinishSurfaceFamily.Cone,+ Regularity, Regularity == ProfileEdgeFinishRegularity.BoundedDegenerate ? "ArcChamferApexPlan" : "ArcChamferConePlan",+ LocalFrame, "source-arc@transition", InsetRadius == 0d ? "apex-vertex@cap" : "inset-arc@cap",+ "line-seam:start", "line-seam:end", SemanticDescendants);++/// <summary>+/// The regular rounded-source Chamfer is one conical-frustum sector. Its two+/// circular boundaries belong to the source and inset sections respectively;+/// its two generators are the explicit source-order Plane/Cone seams. It is+/// not a planar miter patch at a line/arc transition.+/// </summary>+public enum ConicalChamferTrimTopology+{+ FrustumSector,+ ApexSector+}++public sealed record CylindricalFilletPatch(+ string StableId, string SegmentId, CylinderSurface Surface, string LocalFrame,+ IReadOnlyList<string> SemanticDescendants)+ : AnalyticEdgeFinishPatch(StableId, SegmentId, ProfileEdgeFinishSurfaceFamily.Cylinder,+ ProfileEdgeFinishRegularity.Regular, "StraightRoll", LocalFrame,+ "side-contact-line@transition", "cap-contact-line@cap", "quarter-circle-seam:start", "quarter-circle-seam:end", SemanticDescendants);++public sealed record SphericalFilletPatch(+ string StableId, string SegmentId, SphereSurface Surface, string LocalFrame,+ IReadOnlyList<string> SemanticDescendants)+ : AnalyticEdgeFinishPatch(StableId, SegmentId, ProfileEdgeFinishSurfaceFamily.Sphere,+ ProfileEdgeFinishRegularity.BoundedDegenerate, "ArcFilletSphereLimitPlan", LocalFrame,+ "source-arc@transition", "sphere-limit-apex@cap", "quarter-circle-seam:start", "quarter-circle-seam:end", SemanticDescendants);++public sealed record ToroidalFilletPatch(+ string StableId, string SegmentId, TorusSurface Surface, ProfileEdgeFinishTorusRegime Regime,+ ProfileEdgeFinishRegularity Regularity, string LocalFrame, IReadOnlyList<string> SemanticDescendants)+ : AnalyticEdgeFinishPatch(StableId, SegmentId, ProfileEdgeFinishSurfaceFamily.Torus,+ Regularity, "ArcFilletTorusPlan", LocalFrame, "source-arc@transition", "cap-contact-arc@cap",+ "quarter-circle-seam:start", "quarter-circle-seam:end", SemanticDescendants);++/// <summary>Topological shared curve known by provenance, never by spatial proximity.</summary>+public abstract record AnalyticEdgeFinishSeam(+ string StableId, string PredecessorPatchId, string SuccessorPatchId, string SourceVertexId,+ string CurveFamily, bool TraversesWithCurveParameter, string StartVertex, string EndVertex,+ IReadOnlyList<string> SemanticProvenance);++public sealed record PlaneConeSeam(+ string StableId, string PredecessorPatchId, string SuccessorPatchId, string SourceVertexId,+ bool TraversesWithCurveParameter, string StartVertex, string EndVertex, IReadOnlyList<string> SemanticProvenance)+ : AnalyticEdgeFinishSeam(StableId, PredecessorPatchId, SuccessorPatchId, SourceVertexId, "Line",+ TraversesWithCurveParameter, StartVertex, EndVertex, SemanticProvenance);++public sealed record CylinderTorusSeam(+ string StableId, string PredecessorPatchId, string SuccessorPatchId, string SourceVertexId,+ bool TraversesWithCurveParameter, string StartVertex, string EndVertex, IReadOnlyList<string> SemanticProvenance)+ : AnalyticEdgeFinishSeam(StableId, PredecessorPatchId, SuccessorPatchId, SourceVertexId, "Circle",+ TraversesWithCurveParameter, StartVertex, EndVertex, SemanticProvenance);++public sealed record CylinderSphereSeam(+ string StableId, string PredecessorPatchId, string SuccessorPatchId, string SourceVertexId,+ bool TraversesWithCurveParameter, string StartVertex, string EndVertex, IReadOnlyList<string> SemanticProvenance)+ : AnalyticEdgeFinishSeam(StableId, PredecessorPatchId, SuccessorPatchId, SourceVertexId, "Circle",+ TraversesWithCurveParameter, StartVertex, EndVertex, SemanticProvenance);++public sealed record SameFamilySeam(+ string StableId, string PredecessorPatchId, string SuccessorPatchId, string SourceVertexId,+ string CurveFamily, bool TraversesWithCurveParameter, string StartVertex, string EndVertex,+ IReadOnlyList<string> SemanticProvenance)+ : AnalyticEdgeFinishSeam(StableId, PredecessorPatchId, SuccessorPatchId, SourceVertexId, CurveFamily,+ TraversesWithCurveParameter, StartVertex, EndVertex, SemanticProvenance);++/// <summary>+/// Builds the authoritative analytic plan for a selected closed Profile loop.+/// Materialization is intentionally separate: consumers cannot stitch separately+/// emitted station B-reps because this plan owns the complete seam ring.+/// </summary>+public static class ProfileEdgeFinishMixedShellPlanner+{+ private const double Tolerance = 1e-8;++ public static ProfileEdgeFinishMixedShellPlanResult TryPlan(+ ResolvedProfile2D profile,+ ProfileBoundaryChamferTarget target,+ ProfileEdgeFinishKind finishKind,+ double finishSize)+ {+ ProfileEdgeFinishMixedShellPlanResult Fail(string code) => new(false, null, [code]);+ if (!double.IsFinite(finishSize) || finishSize <= 0d) return Fail("ProfileEdgeFinishMixedShellFinishSizeInvalid");+ if (target.ChainKind != ProfileBoundaryChamferChainKind.ClosedLoop) return Fail("ProfileEdgeFinishMixedShellClosedLoopRequired");+ var loop = profile.Loops.SingleOrDefault(x => x.Name == target.LoopId);+ if (loop is null || !loop.IsOuter || profile.Loops.Count != 1) return Fail("ProfileEdgeFinishMixedShellOuterLoopRequired");+ if (!target.SegmentIds.SequenceEqual(loop.Segments.Select(x => x.Name))) return Fail("ProfileEdgeFinishMixedShellWholeLoopSourceOrderRequired");+ var start = profile.LocalStartDepth ?? -1d;+ var end = profile.LocalEndDepth ?? 1d;+ if (finishSize >= end - start - Tolerance) return Fail("ProfileEdgeFinishMixedShellFinishExceedsHostThickness");++ var frame = profile.EffectiveConstructionPlane;+ var signedArea = SignedArea(loop);+ if (Math.Abs(signedArea) <= Tolerance) return Fail("ProfileEdgeFinishMixedShellProfileDegenerate");+ var materialSign = Math.Sign(signedArea);+ var transition = target.Side == ProfileBoundaryChamferSide.Top ? end - finishSize : start + finishSize;+ var cap = target.Side == ProfileBoundaryChamferSide.Top ? end : start;+ var capOut = target.Side == ProfileBoundaryChamferSide.Top ? frame.AxisZ : Direction3D.Create(-frame.AxisZ.ToVector());+ var axialInto = -capOut.ToVector();++ var patches = new List<AnalyticEdgeFinishPatch>(loop.Segments.Count);+ var degeneracies = new List<string>();+ foreach (var segment in loop.Segments)+ {+ var stableId = $"{target.StableId}:patch:{segment.Name}";+ switch (segment.Geometry)+ {+ case LineArcLineSegment2D line:+ {+ var tangent = Direction(line, frame);+ var inward = Inward(line, signedArea, frame);+ var startPoint = frame.ToWorld(line.Start, transition);+ if (finishKind == ProfileEdgeFinishKind.Chamfer)+ {+ var normal = Direction3D.Create(tangent.ToVector().Cross((inward.ToVector() * finishSize) + (capOut.ToVector() * finishSize)));+ patches.Add(new PlanarChamferPatch(stableId, segment.Name,+ new PlaneSurface(startPoint, normal, tangent), Frame(tangent, inward, capOut), Descendants(segment)));+ }+ else+ {+ var center = startPoint + inward.ToVector() * finishSize;+ patches.Add(new CylindricalFilletPatch(stableId, segment.Name,+ new CylinderSurface(center, tangent, finishSize, capOut), Frame(tangent, inward, capOut), Descendants(segment)));+ }+ break;+ }+ case LineArcCircularArc2D arc:+ {+ var material = Math.Sign(arc.SweepAngleRadians) * materialSign >= 0d+ ? ProfileEdgeFinishMaterialSide.Convex : ProfileEdgeFinishMaterialSide.Reflex;+ var station = StationName(segment.Name);+ var policy = ProfileEdgeFinishAnalyticPolicy.Classify(new(station, finishKind,+ ProfileEdgeFinishSourceFamily.ArcDerived, material, arc.Radius, finishSize, target.ReflexJunctionStyle));+ if (policy.Admission == ProfileEdgeFinishAdmission.UnsupportedWithTypedDiagnostic)+ return Fail($"ProfileEdgeFinishMixedShellPatchRejected:station={station}:planner={policy.PlannerKind}:diagnostic={policy.ExpectedDiagnostic}");+ var radial = Direction3D.Create(frame.ToWorldDirection(new Vector3D(Math.Cos(arc.StartAngleRadians), Math.Sin(arc.StartAngleRadians), 0d)));+ var center = frame.ToWorld(arc.Center, transition);+ var insetRadius = material == ProfileEdgeFinishMaterialSide.Convex ? arc.Radius - finishSize : arc.Radius + finishSize;+ if (finishKind == ProfileEdgeFinishKind.Chamfer)+ {+ var cone = Cone(center, arc.Radius, insetRadius, finishSize, capOut, radial);+ if (policy.Regularity == ProfileEdgeFinishRegularity.BoundedDegenerate) degeneracies.Add($"ConeApex:{segment.Name}:vertex={segment.Name}:cap");+ patches.Add(new ConicalChamferPatch(stableId, segment.Name, cone, arc.Radius, insetRadius,+ policy.Regularity,+ policy.Regularity == ProfileEdgeFinishRegularity.BoundedDegenerate+ ? ConicalChamferTrimTopology.ApexSector+ : ConicalChamferTrimTopology.FrustumSector,+ Frame(radial, Direction3D.Create(frame.ToWorldDirection(new Vector3D(-Math.Sin(arc.StartAngleRadians), Math.Cos(arc.StartAngleRadians), 0d))), capOut), Descendants(segment)));+ }+ else if (policy.SurfaceFamily == ProfileEdgeFinishSurfaceFamily.Sphere)+ {+ degeneracies.Add($"SphereLimit:{segment.Name}:vertex={segment.Name}:cap");+ patches.Add(new SphericalFilletPatch(stableId, segment.Name,+ new SphereSurface(center, capOut, finishSize, radial), Frame(radial, capOut, Direction3D.Create(frame.ToWorldDirection(new Vector3D(-Math.Sin(arc.StartAngleRadians), Math.Cos(arc.StartAngleRadians), 0d)))), Descendants(segment)));+ }+ else+ {+ var major = policy.TorusMajorRadius!.Value;+ patches.Add(new ToroidalFilletPatch(stableId, segment.Name,+ new TorusSurface(center, capOut, major, finishSize, radial), policy.TorusRegime, policy.Regularity,+ Frame(radial, capOut, Direction3D.Create(frame.ToWorldDirection(new Vector3D(-Math.Sin(arc.StartAngleRadians), Math.Cos(arc.StartAngleRadians), 0d)))), Descendants(segment)));+ }+ break;+ }+ default:+ return Fail("ProfileEdgeFinishMixedShellSegmentKindUnsupported");+ }+ }++ var seams = new List<AnalyticEdgeFinishSeam>(patches.Count);+ for (var i = 0; i < patches.Count; i++)+ {+ var predecessor = patches[i];+ var successor = patches[(i + 1) % patches.Count];+ var sourceVertex = $"{loop.Name}.{loop.Segments[(i + 1) % loop.Segments.Count].Name}.Start";+ var seamId = $"{target.StableId}:seam:{predecessor.SegmentId}->{successor.SegmentId}";+ var orientation = true; // authored loop traversal, later emitted as EDGE_CURVE.same_sense.+ var provenance = new[] { $"profile:{profile.Name}.{loop.Name}", $"source:{predecessor.SegmentId}->{successor.SegmentId}", "SourceOrder" };+ seams.Add(CreateSeam(finishKind, seamId, predecessor, successor, sourceVertex, orientation, provenance));+ }++ return new(true, new ProfileEdgeFinishMixedShellPlan(target, finishKind, finishSize, patches, seams, degeneracies,+ finishKind == ProfileEdgeFinishKind.Chamfer ? "MixedInsetLineArcProfile" : "MixedCapContactLineArcProfile",+ finishKind == ProfileEdgeFinishKind.Chamfer ? "OriginalSideToPlaneConeTrim" : "OriginalSideToCylinderSphereTorusTrim",+ ["ResolvedProfile2D", "ProfileEdgeFinishAnalyticPolicy", "ProfileEdgeFinishMixedShellPlan", "SourceOrder", "PreEmissionSeamCorrespondence"]), []);+ }++ private static AnalyticEdgeFinishSeam CreateSeam(ProfileEdgeFinishKind kind, string id, AnalyticEdgeFinishPatch predecessor,+ AnalyticEdgeFinishPatch successor, string sourceVertex, bool orientation, IReadOnlyList<string> provenance)+ {+ var start = $"{sourceVertex}:transition";+ var end = $"{sourceVertex}:cap";+ if (kind == ProfileEdgeFinishKind.Chamfer &&+ ((predecessor is PlanarChamferPatch && successor is ConicalChamferPatch) || (predecessor is ConicalChamferPatch && successor is PlanarChamferPatch)))+ return new PlaneConeSeam(id, predecessor.StableId, successor.StableId, sourceVertex, orientation, start, end, provenance);+ if (kind == ProfileEdgeFinishKind.Fillet &&+ ((predecessor is CylindricalFilletPatch && successor is ToroidalFilletPatch) || (predecessor is ToroidalFilletPatch && successor is CylindricalFilletPatch)))+ return new CylinderTorusSeam(id, predecessor.StableId, successor.StableId, sourceVertex, orientation, start, end, provenance);+ if (kind == ProfileEdgeFinishKind.Fillet &&+ ((predecessor is CylindricalFilletPatch && successor is SphericalFilletPatch) || (predecessor is SphericalFilletPatch && successor is CylindricalFilletPatch)))+ return new CylinderSphereSeam(id, predecessor.StableId, successor.StableId, sourceVertex, orientation, start, end, provenance);+ return new SameFamilySeam(id, predecessor.StableId, successor.StableId, sourceVertex,+ kind == ProfileEdgeFinishKind.Chamfer ? "Line" : "Circle", orientation, start, end, provenance);+ }++ private static ConeSurface Cone(Point3D lowerCenter, double lowerRadius, double upperRadius, double height, Direction3D capOut, Direction3D radial)+ {+ var semiAngle = Math.Atan(Math.Abs(upperRadius - lowerRadius) / height);+ // Axis always points from the apex toward the increasing radius. For a convex+ // apex/funnel it therefore points away from the cap; for a reflex cone it points+ // toward the cap. Both placements retain the exact lower circular boundary.+ var axis = upperRadius > lowerRadius ? capOut : Direction3D.Create(-capOut.ToVector());+ return new ConeSurface(lowerCenter, axis, lowerRadius, semiAngle, radial);+ }++ private static string StationName(string segmentName)+ {+ var match = Regex.Match(segmentName, "^(?<name>(?:Convex|Reflex)(?:Sharp|Small|Medium|Large))Arc$", RegexOptions.CultureInvariant);+ return match.Success ? match.Groups["name"].Value : segmentName;+ }++ private static IReadOnlyList<string> Descendants(ResolvedProfileSegment2D segment) =>+ [$"source:{segment.Provenance.StableId}", "EdgeFinishReplacementFace", "AnalyticPatch"];++ private static Direction3D Direction(LineArcLineSegment2D line, ConstructionPlane frame)+ {+ var vector = new Vector3D(line.End.X - line.Start.X, line.End.Y - line.Start.Y, 0d);+ return Direction3D.Create(frame.ToWorldDirection(vector));+ }++ private static Direction3D Inward(LineArcLineSegment2D line, double signedArea, ConstructionPlane frame)+ {+ var vector = new Vector3D(line.End.X - line.Start.X, line.End.Y - line.Start.Y, 0d);+ var length = vector.Length;+ var normal = signedArea > 0d ? new Vector3D(-vector.Y / length, vector.X / length, 0d) : new Vector3D(vector.Y / length, -vector.X / length, 0d);+ return Direction3D.Create(frame.ToWorldDirection(normal));+ }++ private static string Frame(Direction3D x, Direction3D y, Direction3D z) =>+ string.Create(CultureInfo.InvariantCulture, $"x=({x.X:R},{x.Y:R},{x.Z:R});y=({y.X:R},{y.Y:R},{y.Z:R});z=({z.X:R},{z.Y:R},{z.Z:R})");++ private static double SignedArea(ResolvedProfileLoop2D loop) => loop.Segments.Sum(segment => segment.Geometry switch+ {+ LineArcLineSegment2D line => line.Start.X * line.End.Y - line.End.X * line.Start.Y,+ LineArcCircularArc2D arc => ArcContribution(arc),+ _ => 0d+ }) * .5d;++ private static double ArcContribution(LineArcCircularArc2D arc)+ {+ var a = arc.StartAngleRadians;+ var b = a + arc.SweepAngleRadians;+ return 2d * (arc.Center.X * arc.Radius * (Math.Sin(b) - Math.Sin(a))+ - arc.Center.Y * arc.Radius * (Math.Cos(b) - Math.Cos(a))+ + arc.Radius * arc.Radius * (b - a));+ }+}++/// <summary>+/// First material consumer of <see cref="ProfileEdgeFinishMixedShellPlan"/>.+/// It emits the complete mixed Chamfer shell at once, so Plane/Cone seams are+/// shared topology rather than later B-rep stitching operations.+/// </summary>+public static class ProfileEdgeFinishMixedShellMaterializer+{+ private const double Tolerance = 1e-8;++ public static ProfileBoundaryChamferPlanResult TryMaterializeChamfer(+ ResolvedProfile2D profile, ProfileBoundaryChamferTarget target, ProfileEdgeFinishMixedShellPlan plan)+ {+ ProfileBoundaryChamferPlanResult Fail(string code) => new(false, null, null, target, [code]);+ if (plan.FinishKind != ProfileEdgeFinishKind.Chamfer) return Fail("ProfileEdgeFinishMixedShellChamferPlanRequired");+ var loop = profile.Loops.SingleOrDefault(x => x.Name == target.LoopId);+ if (loop is null || loop.Segments.Count != plan.OrderedPatches.Count) return Fail("ProfileEdgeFinishMixedShellPlanProfileMismatch");+ var frame = profile.EffectiveConstructionPlane;+ var start = profile.LocalStartDepth ?? -1d;+ var end = profile.LocalEndDepth ?? 1d;+ var transition = target.Side == ProfileBoundaryChamferSide.Top ? end - plan.FinishSize : start + plan.FinishSize;+ var cap = target.Side == ProfileBoundaryChamferSide.Top ? end : start;+ if (target.Side != ProfileBoundaryChamferSide.Top) return Fail("ProfileEdgeFinishMixedShellBottomChamferNotImplemented");+ var area = SignedArea(loop);+ if (Math.Abs(area) <= Tolerance) return Fail("ProfileEdgeFinishMixedShellProfileDegenerate");++ var builder = new TopologyBuilder();+ var geometry = new BrepGeometryStore();+ var bindings = new BrepBindingModel();+ var points = new Dictionary<VertexId, Point3D>();+ var vertices = new Dictionary<string, VertexId>(StringComparer.Ordinal);+ var edges = new Dictionary<string, EdgeId>(StringComparer.Ordinal);+ var seamBySuccessorPatch = plan.OrderedSeams.ToDictionary(x => x.SuccessorPatchId, StringComparer.Ordinal);+ var curveId = 1;+ var surfaceId = 1;+ var descendants = new List<SemanticTopologyDescendant>();++ VertexId Vertex(string key, Point3D point)+ {+ if (vertices.TryGetValue(key, out var id)) return id;+ id = builder.AddVertex(); vertices.Add(key, id); points.Add(id, point); return id;+ }+ (double X, double Y) InsetPoint(int sourceVertex, out int? apexSegment)+ {+ var previous = (sourceVertex + loop.Segments.Count - 1) % loop.Segments.Count;+ var current = sourceVertex;+ apexSegment = new[] { previous, current }+ .Where(index => loop.Segments[index].Geometry is LineArcCircularArc2D arc && Math.Sign(arc.SweepAngleRadians) * Math.Sign(area) >= 0d && Math.Abs(arc.Radius - plan.FinishSize) <= Tolerance)+ .Select(index => (int?)index).FirstOrDefault();+ if (apexSegment is { } apex && loop.Segments[apex].Geometry is LineArcCircularArc2D apexArc)+ return apexArc.Center;+ if (loop.Segments[previous].Geometry is LineArcLineSegment2D previousLine && loop.Segments[current].Geometry is LineArcLineSegment2D currentLine)+ return Intersect(OffsetLine(previousLine, area, plan.FinishSize), OffsetLine(currentLine, area, plan.FinishSize));+ var currentInset = Inset(loop.Segments[current].Geometry, area, plan.FinishSize);+ if (currentInset is not null) return Ends(currentInset)[0];+ var previousInset = Inset(loop.Segments[previous].Geometry, area, plan.FinishSize);+ if (previousInset is not null) return Ends(previousInset)[1];+ throw new InvalidOperationException("A mixed edge-finish vertex cannot have two collapsed arc boundaries.");+ }+ VertexId SectionVertex(int segment, bool atEnd, double depth, bool inset)+ {+ var sourceVertex = atEnd ? (segment + 1) % loop.Segments.Count : segment;+ if (inset)+ {+ var insetPoint = InsetPoint(sourceVertex, out var apex);+ return Vertex(apex is { } ? $"apex:{apex}:{depth:R}" : $"section:{sourceVertex}:{depth:R}:inset", frame.ToWorld(insetPoint, depth));+ }+ var point = Ends(loop.Segments[segment].Geometry)[atEnd ? 1 : 0];+ return Vertex($"section:{sourceVertex}:{depth:R}:source", frame.ToWorld(point, depth));+ }+ EdgeId Edge(string key, VertexId a, VertexId b, CurveGeometry curve, ParameterInterval trim, bool oriented)+ {+ if (edges.TryGetValue(key, out var existing)) return existing;+ var edge = builder.AddEdge(a, b); edges.Add(key, edge);+ var curveGeometryId = new CurveGeometryId(curveId++);+ geometry.AddCurve(curveGeometryId, curve);+ bindings.AddEdgeBinding(new EdgeGeometryBinding(edge, curveGeometryId, trim, oriented));+ return edge;+ }+ EdgeId SectionEdge(int segment, double depth, bool inset)+ {+ var curve2 = inset ? Inset(loop.Segments[segment].Geometry, area, plan.FinishSize) : loop.Segments[segment].Geometry;+ if (curve2 is null) throw new InvalidOperationException("Cone apex has no circular top edge.");+ var a = SectionVertex(segment, false, depth, inset); var b = SectionVertex(segment, true, depth, inset);+ var trim = curve2 is LineArcLineSegment2D ? new ParameterInterval(0d, (points[b] - points[a]).Length) : Trim(curve2);+ return Edge($"section:{segment}:{depth:R}:{inset}", a, b, Curve(curve2, depth, frame, points[a], points[b]), trim, Oriented(curve2));+ }+ EdgeId SeamEdge(int sourceVertex, VertexId a, VertexId b)+ {+ var key = $"seam:{sourceVertex}:{Math.Min(a.Value, b.Value)}:{Math.Max(a.Value, b.Value)}";+ return Edge(key, a, b, CurveGeometry.FromLine(new Line3Curve(points[a], Direction3D.Create(points[b] - points[a]))),+ new ParameterInterval(0d, (points[b] - points[a]).Length), true);+ }+ EdgeId AnalyticSeamEdge(AnalyticEdgeFinishSeam seam, VertexId a, VertexId b)+ {+ if (seam is not PlaneConeSeam && seam is not SameFamilySeam)+ throw new InvalidOperationException("Mixed Chamfer plan contains a non-Chamfer analytic seam.");+ if (!string.Equals(seam.CurveFamily, "Line", StringComparison.Ordinal))+ throw new InvalidOperationException("Mixed Chamfer analytic seam must be a generator line.");++ var key = $"analytic-seam:{seam.StableId}";+ if (edges.TryGetValue(key, out var existing)) return existing;+ var edge = Edge(key, a, b, CurveGeometry.FromLine(new Line3Curve(points[a], Direction3D.Create(points[b] - points[a]))),+ new ParameterInterval(0d, (points[b] - points[a]).Length), seam.TraversesWithCurveParameter);+ descendants.Add(new SemanticTopologyDescendant(seam.StableId, "Edge", SemanticTopologyRole.ComposeTransition,+ $"profile-seam:{seam.SourceVertexId}", Edge: edge, ParentStableId: target.StableId,+ GeometryPreview: $"{seam.GetType().Name}:{seam.CurveFamily}:sameSense={seam.TraversesWithCurveParameter}"));+ return edge;+ }+ Use Use(EdgeId edge, VertexId startVertex) => new(edge, builder.Model.Edges.Single(x => x.Id == edge).StartVertexId != startVertex);+ FaceId Face(string stableId, IReadOnlyList<Use> uses, SurfaceGeometry surface, SemanticTopologyRole role, string source)+ {+ var loopId = builder.AllocateLoopId();+ var coedges = uses.Select(_ => builder.AllocateCoedgeId()).ToArray();+ for (var i = 0; i < uses.Count; i++)+ builder.AddCoedge(new Coedge(coedges[i], uses[i].Edge, loopId, coedges[(i + 1) % uses.Count], coedges[(i + uses.Count - 1) % uses.Count], uses[i].Reverse));+ builder.AddLoop(new Loop(loopId, coedges));+ var face = builder.AddFace([loopId]);+ var sid = new SurfaceGeometryId(surfaceId++); geometry.AddSurface(sid, surface); bindings.AddFaceBinding(new FaceGeometryBinding(face, sid, true));+ descendants.Add(new(stableId, "Face", role, source, Face: face, ParentStableId: target.StableId));+ return face;+ }++ var count = loop.Segments.Count;+ var bottom = new EdgeId[count]; var middle = new EdgeId[count]; var top = new EdgeId?[count];+ for (var i = 0; i < count; i++)+ {+ bottom[i] = SectionEdge(i, start, false);+ middle[i] = SectionEdge(i, transition, false);+ var inset = Inset(loop.Segments[i].Geometry, area, plan.FinishSize);+ top[i] = inset is null ? null : SectionEdge(i, cap, true);+ }++ // Bottom cap is the source loop reversed; the top cap follows the mixed inset loop.+ Face($"{target.StableId}:bottom-cap", Enumerable.Range(0, count).Reverse().Select(i => Use(bottom[i], SectionVertex(i, true, start, false))).ToArray(),+ SurfaceGeometry.FromPlane(new PlaneSurface(frame.ToWorld((0d, 0d), start), Direction3D.Create(-frame.AxisZ.ToVector()), frame.AxisX)), SemanticTopologyRole.BottomFaceBoundaryLoop, $"profile:{profile.Name}.{loop.Name}");+ var topUses = new List<Use>();+ for (var i = 0; i < count; i++)+ if (top[i] is { } edge) topUses.Add(Use(edge, SectionVertex(i, false, cap, true)));+ Face($"{target.StableId}:top-cap", topUses, SurfaceGeometry.FromPlane(new PlaneSurface(frame.ToWorld((0d, 0d), cap), frame.AxisZ, frame.AxisX)), SemanticTopologyRole.TopFaceBoundaryLoop, $"profile:{profile.Name}.{loop.Name}");++ for (var i = 0; i < count; i++)+ {+ var next = (i + 1) % count;+ var lowerStart = SectionVertex(i, false, start, false); var lowerEnd = SectionVertex(i, true, start, false);+ var middleStart = SectionVertex(i, false, transition, false); var middleEnd = SectionVertex(i, true, transition, false);+ var vEnd = SeamEdge(next, lowerEnd, middleEnd); var vStart = SeamEdge(i, middleStart, lowerStart);+ Face($"{target.StableId}:side:{loop.Segments[i].Name}", [Use(bottom[i], lowerStart), Use(vEnd, lowerEnd), Use(middle[i], middleEnd), Use(vStart, middleStart)],+ SideSurface(loop.Segments[i].Geometry, start, frame), SemanticTopologyRole.ExtrusionSideFace, loop.Segments[i].Provenance.StableId);++ var patch = plan.OrderedPatches[i];+ var incomingSeam = seamBySuccessorPatch[patch.StableId];+ var outgoingPatch = plan.OrderedPatches[next];+ var outgoingSeam = seamBySuccessorPatch[outgoingPatch.StableId];+ var capStart = SectionVertex(i, false, cap, true); var capEnd = SectionVertex(i, true, cap, true);+ var seamEnd = AnalyticSeamEdge(outgoingSeam, middleEnd, capEnd); var seamStart = AnalyticSeamEdge(incomingSeam, capStart, middleStart);+ var transitionUses = new List<Use> { Use(middle[i], middleStart), Use(seamEnd, middleEnd) };+ if (top[i] is { } topEdge) transitionUses.Add(Use(topEdge, capEnd));+ transitionUses.Add(Use(seamStart, capStart));+ var surface = patch switch+ {+ PlanarChamferPatch plane => SurfaceGeometry.FromPlane(plane.Surface),+ ConicalChamferPatch cone => SurfaceGeometry.FromCone(cone.Surface),+ _ => throw new InvalidOperationException("Mixed Chamfer plan contains non-Chamfer patch.")+ };+ Face($"{target.StableId}:chamfer:{loop.Segments[i].Name}", transitionUses, surface, SemanticTopologyRole.EdgeFinishReplacementFace, loop.Segments[i].Provenance.StableId);+ }++ var shell = builder.AddShell(builder.Model.Faces.Select(x => x.Id).ToArray());+ builder.AddBody([shell]);+ var body = new BrepBody(builder.Model, geometry, bindings, points);+ var validation = BrepBindingValidator.Validate(body, true);+ if (!validation.IsSuccess) return Fail("ProfileEdgeFinishMixedShellChamferTopologyInvalid");+ var correspondence = new SemanticTopologyCorrespondence(target.HostBodyId, descendants,+ ["ResolvedProfile2D", "ProfileEdgeFinishMixedShellPlan", "PlaneConeSeam", "ConeApex", "AuthoritativeBRepPlan"]);+ return new(true, body, correspondence, target, ["ProfileEdgeFinishMixedShellChamfer", "ProfileEdgeFinishMixedShellPlaneCone", "ProfileEdgeFinishMixedShellConeApex"]);+ }++ private static LineArcProfileCurve2D? Inset(LineArcProfileCurve2D curve, double area, double distance) => curve switch+ {+ LineArcLineSegment2D line => OffsetLine(line, area, distance),+ LineArcCircularArc2D arc => OffsetArc(arc, area, distance),+ _ => throw new NotSupportedException("Mixed edge finish requires bounded line/arc profile segments.")+ };++ private static LineArcLineSegment2D OffsetLine(LineArcLineSegment2D line, double area, double distance)+ {+ var dx = line.End.X - line.Start.X; var dy = line.End.Y - line.Start.Y; var length = Math.Sqrt(dx * dx + dy * dy);+ var nx = area > 0d ? -dy / length : dy / length; var ny = area > 0d ? dx / length : -dx / length;+ return new((line.Start.X + nx * distance, line.Start.Y + ny * distance), (line.End.X + nx * distance, line.End.Y + ny * distance));+ }++ private static LineArcCircularArc2D? OffsetArc(LineArcCircularArc2D arc, double area, double distance)+ {+ var convex = Math.Sign(arc.SweepAngleRadians) * Math.Sign(area) >= 0d;+ var radius = convex ? arc.Radius - distance : arc.Radius + distance;+ return radius <= Tolerance ? null : new LineArcCircularArc2D(arc.Center, radius, arc.StartAngleRadians, arc.SweepAngleRadians);+ }++ private static (double X, double Y)[] Ends(LineArcProfileCurve2D curve) => curve switch+ {+ LineArcLineSegment2D line => [line.Start, line.End],+ LineArcCircularArc2D arc => [(arc.Center.X + arc.Radius * Math.Cos(arc.StartAngleRadians), arc.Center.Y + arc.Radius * Math.Sin(arc.StartAngleRadians)),+ (arc.Center.X + arc.Radius * Math.Cos(arc.StartAngleRadians + arc.SweepAngleRadians), arc.Center.Y + arc.Radius * Math.Sin(arc.StartAngleRadians + arc.SweepAngleRadians))],+ _ => throw new NotSupportedException()+ };++ private static CurveGeometry Curve(LineArcProfileCurve2D curve, double depth, ConstructionPlane frame, Point3D start, Point3D end) => curve switch+ {+ LineArcLineSegment2D => CurveGeometry.FromLine(new Line3Curve(start, Direction3D.Create(end - start))),+ LineArcCircularArc2D arc => CurveGeometry.FromCircle(new Circle3Curve(frame.ToWorld(arc.Center, depth), frame.AxisZ, arc.Radius, frame.AxisX)),+ _ => throw new NotSupportedException()+ };++ private static ParameterInterval Trim(LineArcProfileCurve2D curve) => curve switch+ {+ LineArcLineSegment2D line => new ParameterInterval(0d, Math.Sqrt(Math.Pow(line.End.X - line.Start.X, 2d) + Math.Pow(line.End.Y - line.Start.Y, 2d))),+ LineArcCircularArc2D arc => new ParameterInterval(Math.Min(arc.StartAngleRadians, arc.StartAngleRadians + arc.SweepAngleRadians), Math.Max(arc.StartAngleRadians, arc.StartAngleRadians + arc.SweepAngleRadians)),+ _ => throw new NotSupportedException()+ };++ private static bool Oriented(LineArcProfileCurve2D curve) => curve is not LineArcCircularArc2D arc || arc.SweepAngleRadians >= 0d;++ private static SurfaceGeometry SideSurface(LineArcProfileCurve2D curve, double start, ConstructionPlane frame) => curve switch+ {+ LineArcLineSegment2D line => SurfaceGeometry.FromPlane(new PlaneSurface(frame.ToWorld(line.Start, start), Direction3D.Create(Direction(line, frame).ToVector().Cross(frame.AxisZ.ToVector())), Direction(line, frame))),+ LineArcCircularArc2D arc => SurfaceGeometry.FromCylinder(new CylinderSurface(frame.ToWorld(arc.Center, start), frame.AxisZ, arc.Radius, frame.AxisX)),+ _ => throw new NotSupportedException()+ };++ private static Direction3D Direction(LineArcLineSegment2D line, ConstructionPlane frame) => Direction3D.Create(frame.ToWorldDirection(new Vector3D(line.End.X - line.Start.X, line.End.Y - line.Start.Y, 0d)));+ private static (double X, double Y) Intersect(LineArcLineSegment2D first, LineArcLineSegment2D second)+ {+ var rx = first.End.X - first.Start.X; var ry = first.End.Y - first.Start.Y;+ var sx = second.End.X - second.Start.X; var sy = second.End.Y - second.Start.Y;+ var cross = rx * sy - ry * sx;+ if (Math.Abs(cross) <= Tolerance) throw new InvalidOperationException("Offset line intersection is degenerate.");+ var qx = second.Start.X - first.Start.X; var qy = second.Start.Y - first.Start.Y;+ var t = (qx * sy - qy * sx) / cross;+ return (first.Start.X + t * rx, first.Start.Y + t * ry);+ }+ private static double SignedArea(ResolvedProfileLoop2D loop) => loop.Segments.Sum(x => x.Geometry switch+ {+ LineArcLineSegment2D line => line.Start.X * line.End.Y - line.End.X * line.Start.Y,+ LineArcCircularArc2D arc => 2d * (arc.Center.X * arc.Radius * (Math.Sin(arc.StartAngleRadians + arc.SweepAngleRadians) - Math.Sin(arc.StartAngleRadians)) - arc.Center.Y * arc.Radius * (Math.Cos(arc.StartAngleRadians + arc.SweepAngleRadians) - Math.Cos(arc.StartAngleRadians)) + arc.Radius * arc.Radius * arc.SweepAngleRadians),+ _ => 0d+ }) * .5d;+ private readonly record struct Use(EdgeId Edge, bool Reverse);+}diff --git a/docs/firmament/mixed-analytic-edgefinish-shell-x4.md b/docs/firmament/mixed-analytic-edgefinish-shell-x4.mdnew file mode 100644index 00000000..274d2a25--- /dev/null+++ b/docs/firmament/mixed-analytic-edgefinish-shell-x4.md@@ -0,0 +1,90 @@+# Mixed analytic Profile EdgeFinish shell X4++X4 introduces the pre-emission authoritative shell plan for a source-selected,+closed Profile loop. It is intentionally a planning boundary: it records every+analytic patch and every shared curve before a B-rep emitter allocates an edge,+loop, face, shell, or STEP entity. This replaces the former situation in which+the policy card knew the selected surface family but did not preserve enough+neighbouring information to materialize a mixed loop safely.++The release source remains the seven-station chimera with `F = 4 mm`, a+24-mm extrusion, and the base proof `(38240 + 4 - pi) * 24 =+917780.601776314 mm^3`. Its authored outer loop has 17 segments: 12 lines and+five circular arcs. The earlier X1/X3 prose saying 18 segments was a+documentation error; no geometry or station was changed.++## Shell plan++`ProfileEdgeFinishMixedShellPlanner` consumes the resolved outer loop in its+authored order and requires the selected target to be that complete closed+loop. It produces `ProfileEdgeFinishMixedShellPlan` with typed patch variants:++- `PlanarChamferPatch` and `ConicalChamferPatch`;+- `CylindricalFilletPatch`, `SphericalFilletPatch`, and+ `ToroidalFilletPatch`.++Each patch retains its source segment, selected policy, regularity, analytic+frame, contact boundaries, side boundaries, and semantic descendants. The+plan also contains a final last-to-first seam: it has no endpoint terminations.+`ConeApex` and `SphereLimit` are explicit degeneracy records rather than+zero-radius circles or zero-major-radius tori.++## Exact mixed seams++At a tangent line/arc source transition the Chamfer seam is a **line**. With+source endpoint `E`, transition station `zt`, cap station `zc`, and the+source inward normal `n`, it joins `E@zt` to `(E + F*n)@zc`. The plane patch+and the cone patch both contain that generator; their tangent planes agree+because the source tangent is shared. A ConvexMedium endpoint follows the same+line to the finite cone apex.++A regular rounded **concave** source station is not terminated by a planar+right-angle/miter face. It is a single `ConicalChamferPatch` with+`FrustumSector` trim topology: the source circular arc at the transition, the+offset circular arc at the cap, and exactly two generator lines. Those+generator edges are materialized from the owning `PlaneConeSeam` plan records,+including their `same_sense` direction, so they cannot become anonymous+proximity-derived seams. `ReflexSmallArc` is the focused regression: for+`Rs=2, F=4` it has radius-2 and radius-6 circular trims plus the two generators.+This is the same analytic boundary family as the supplied SolidWorks reference+frustum; a visible wedge inside that angular interval is a defect.++The Fillet seam is a **circle**, not a planar termination. It is the common+quarter-circle of the line-roll and circular rolling patch, joining the+side-contact point at `zt` to the cap-contact point at `zc`; its centre is the+corresponding rolling centre. It is represented by `CylinderTorusSeam` for+regular/horn torus patches and `CylinderSphereSeam` for the ConvexMedium sphere+limit. Both seam records explicitly carry source vertex, endpoints, oriented+traversal, predecessor/successor patch ids, and provenance. STEP emission must+map that traversal to `EDGE_CURVE.same_sense`; no seam record permits a+hard-coded value.++The plan preserves policy-derived torus regimes: ConvexLarge is horn and+interop-sensitive (`R=4, r=4`); ReflexSmall/Medium/Large are regular ring tori+(`R=6/8/12, r=4`). ConvexMedium Fillet is the bounded spherical limit.+`SphereSeamCompatibility` remains an opt-in sharp-reflex policy and is not+selected by the whole-loop plan.++## Current completion status++The plan and its focused tests are complete and deterministic. It proves the+release order, the 5 Plane/Cone arc patches, the 4 Torus + 1 Sphere arc fillet+patches, all 10 line/arc Chamfer seams, all 8 cylinder/torus seams, both+cylinder/sphere seams, and both bounded degeneracies before B-rep emission.++The Chamfer emitter now consumes that plan as one shell. Its persistent outputs+are `artifacts/edgefinish/profile-edgefinish-chimera-chamfer.step` and+`artifacts/edgefinish/profile-edgefinish-chimera-chamfer.canonical.step`; both+reimport as one enclosed manifold shell with 26 Plane, 5 Cylinder, and 5 Cone+faces and zero NURBS. The finite ConvexMedium apex is a topological vertex,+not a zero-radius edge.++The Fillet emitter remains blocked. Existing sharp sphere/horn-torus planners+materialize complete two-line bodies with cap, side, and endpoint-termination+faces; they do not expose reusable junction patches for insertion into the+closed source-order shell. That component extraction is now the concrete next+blocker. No Fillet or compatibility STEP, finished-volume assertion, or+external-kernel smoke is claimed. The next implementation must extract those+sharp patches and compose them with the already-planned cylinder/torus/sphere+patches as one shell; it must not substitute NURBS, Booleans, or post-Brep+surgery.diff --git a/docs/firmament/profile-edgefinish-chimera-closure-x2.md b/docs/firmament/profile-edgefinish-chimera-closure-x2.mdindex fd66ac76..4baad786 100644--- a/docs/firmament/profile-edgefinish-chimera-closure-x2.md+++ b/docs/firmament/profile-edgefinish-chimera-closure-x2.md@@ -6,6 +6,11 @@ source card is now superseded by X3's release card; seesource-bound: no anonymousBRep-edge selection, Boolean repair, NURBS, or sampled finish is admissible.+Historical status update: X4 adds the pre-emission, typed source-order shell+plan for the X3 seven-station release card. This X2 policy matrix remains+unchanged; the production mixed-shell B-rep emitter is still the next boundary.+See `mixed-analytic-edgefinish-shell-x4.md`.+`ProfileEdgeFinishAnalyticPolicy` is the code-level conformance table. It usesthe JudgmentEngine to choose one bounded candidate deterministically and retainsthe rejected candidate reasons. The policy is evaluated before BRep emission.diff --git a/docs/firmament/profile-edgefinish-chimera-release-closure-x3.md b/docs/firmament/profile-edgefinish-chimera-release-closure-x3.mdindex 360f5729..23136b34 100644--- a/docs/firmament/profile-edgefinish-chimera-release-closure-x3.md+++ b/docs/firmament/profile-edgefinish-chimera-release-closure-x3.md@@ -5,7 +5,8 @@ remains eight rows in `ProfileEdgeFinishAnalyticPolicy`, but `ConvexSmall` isnot present in the release Profile: with `Rs = 2 mm` and `F = 4 mm`, it isinvalid for both analytic routes. The release card therefore containsConvexSharp, ConvexMedium, ConvexLarge, ReflexSharp, ReflexSmall, ReflexMedium,-and ReflexLarge only. Its outer source loop has 18 segments and independent+and ReflexLarge only. Its outer source loop has 17 segments (12 lines and five+arcs) and independentarea/volume evidence `(38240 + 4 - π) mm² × 24 mm = 917780.601776314 mm³`.The two discoverable canonical invalid cases are:@@ -39,13 +40,24 @@ Sharp convex remains the sphere-junction route. Sharp reflex remains exacthorn-torus rolling by default, with `SphereSeamCompatibility` as an intentionalopt-in plan. No NURBS policy exists.-## Remaining closure boundary--The release card is a valid base artifact, but the authoritative mixed-line/arc Plane/Cone and Cylinder/Torus-or-Sphere composer is not yet emitted.-Whole-loop Chamfer/Fillet intentionally stop before topology with a policy-rich-`ProfileBoundary*ArcMaterializationNotImplemented` diagnostic. Therefore this-document does not claim completed finish STEP artifacts, external-kernel smoke,-or Preview-1 readiness. The next implementation must create direct analytic-seams and finite apex/sphere/horn topology; it may not replace that work with a-spline or Boolean fallback.+## X4 planning follow-up++X4 now supplies a typed, source-order pre-emission mixed-shell plan with+Plane/Cone and Cylinder/Torus-or-Sphere patch variants, explicit source-bound+seams, and Cone-apex/Sphere-limit degeneracy records. It is deliberately not a+patch-stitching route. The source card was also audited: its retained Profile+has 17 segments (12 lines and five arcs), not the stale 18-segment count stated+by earlier notes.++## Remaining materialization boundary++The release card is a valid base artifact. Whole-loop Chamfer now emits through+the authoritative Plane/Cone source-order shell and has a persistent STEP+artifact. Fillet still intentionally stops before topology on the first arc+with its policy-rich `ProfileBoundaryFilletArcMaterializationNotImplemented`+diagnostic because sharp junction patches have not yet been extracted from the+existing finite two-line-body planners. Therefore this document does not claim+completed Fillet/compatibility artifacts, external-kernel smoke, or Preview-1+readiness. The next implementation must create reusable sharp junction topology+and compose it with the existing cylinder/torus/sphere seam plan; it may not+replace that work with a spline or Boolean fallback.diff --git a/docs/firmament/v2-canonical-authoring.md b/docs/firmament/v2-canonical-authoring.mdindex 35aa6519..7e836afb 100644--- a/docs/firmament/v2-canonical-authoring.md+++ b/docs/firmament/v2-canonical-authoring.md@@ -276,6 +276,13 @@ Fillet routes cover line-only source loops; circular source segments bind andreport a station-specific missing exact planner rather than falling back toanonymous BRep edge inference. See `profile-edgefinish-chimera-x1.md`.+For the seven-station mixed line/arc release card, EdgeFinish policy is+source-bound: lines select Plane Chamfer/Cylinder Fillet patches; admitted arcs+select Cone Chamfer and Torus or Sphere-limit Fillet patches. The canonical+planner records the complete loop in source order with explicit Plane/Cone and+Cylinder/Torus-or-Sphere seams before emission. This is not anonymous edge+inference and has no NURBS fallback; see `mixed-analytic-edgefinish-shell-x4.md`.+Rounded-source EdgeFinish policy is explicitly classified before BRep emission:chamfers use only Plane/Cone and fillets only Cylinder/Sphere/Torus. See`profile-edgefinish-chimera-closure-x2.md` for the current bounded support card