Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
Harden E6c local 2-edge chamfer rewrite path
Yuechen Li52389c59c8a2
diff --git a/Aetheris.Kernel.Core.Tests/Brep/EdgeFinishing/BrepBoundedChamferCornerJudgmentTests.cs b/Aetheris.Kernel.Core.Tests/Brep/EdgeFinishing/BrepBoundedChamferCornerJudgmentTests.csindex e87d9ac9..2a340747 100644--- a/Aetheris.Kernel.Core.Tests/Brep/EdgeFinishing/BrepBoundedChamferCornerJudgmentTests.cs+++ b/Aetheris.Kernel.Core.Tests/Brep/EdgeFinishing/BrepBoundedChamferCornerJudgmentTests.cs@@ -52,6 +52,70 @@ public sealed class BrepBoundedChamferCornerJudgmentTestsAssert.True(result.IsSuccess, string.Join(Environment.NewLine, result.Diagnostics.Select(d => d.Message)));Assert.Equal(7, result.Value.Topology.Faces.Count());+ Assert.Equal(15, result.Value.Topology.Edges.Count());+ Assert.Equal(10, result.Value.Topology.Vertices.Count());+ }++ [Fact]+ public void ChamferTrustedPolyhedralIncidentEdgePair_Uses_SelectorDriven_Local_Rewrite_Path()+ {+ var box = BrepPrimitives.CreateBox(20d, 20d, 20d);+ Assert.True(box.IsSuccess);++ var xyResult = BrepBoundedChamfer.ChamferTrustedPolyhedralIncidentEdgePair(+ box.Value,+ new BrepBoundedChamferIncidentEdgePairSelector(+ BrepBoundedChamferCorner.XMaxYMaxZMax,+ BrepBoundedChamferCornerIncidentEdge.XNegative,+ BrepBoundedChamferCornerIncidentEdge.YNegative),+ distance: 1d);+ var xzResult = BrepBoundedChamfer.ChamferTrustedPolyhedralIncidentEdgePair(+ box.Value,+ new BrepBoundedChamferIncidentEdgePairSelector(+ BrepBoundedChamferCorner.XMaxYMaxZMax,+ BrepBoundedChamferCornerIncidentEdge.XNegative,+ BrepBoundedChamferCornerIncidentEdge.ZNegative),+ distance: 1d);++ Assert.True(xyResult.IsSuccess, string.Join(Environment.NewLine, xyResult.Diagnostics.Select(d => d.Message)));+ Assert.True(xzResult.IsSuccess, string.Join(Environment.NewLine, xzResult.Diagnostics.Select(d => d.Message)));++ var xyVertices = xyResult.Value.Topology.Vertices+ .Select(vertex =>+ {+ xyResult.Value.TryGetVertexPoint(vertex.Id, out var point);+ return $"{point.X:F6}|{point.Y:F6}|{point.Z:F6}";+ })+ .OrderBy(value => value)+ .ToArray();+ var xzVertices = xzResult.Value.Topology.Vertices+ .Select(vertex =>+ {+ xzResult.Value.TryGetVertexPoint(vertex.Id, out var point);+ return $"{point.X:F6}|{point.Y:F6}|{point.Z:F6}";+ })+ .OrderBy(value => value)+ .ToArray();+ Assert.NotEqual(string.Join(";", xyVertices), string.Join(";", xzVertices));+ }++ [Fact]+ public void ChamferTrustedPolyhedralIncidentEdgePair_Rejects_NonOrthogonalTrustedBody()+ {+ var prism = BrepPrimitives.CreateTriangularPrism(baseWidth: 8d, baseDepth: 6d, height: 10d);+ Assert.True(prism.IsSuccess);++ var result = BrepBoundedChamfer.ChamferTrustedPolyhedralIncidentEdgePair(+ prism.Value,+ new BrepBoundedChamferIncidentEdgePairSelector(+ BrepBoundedChamferCorner.XMaxYMaxZMax,+ BrepBoundedChamferCornerIncidentEdge.XNegative,+ BrepBoundedChamferCornerIncidentEdge.YNegative),+ distance: 1d);++ Assert.False(result.IsSuccess);+ Assert.Contains(result.Diagnostics, diagnostic =>+ diagnostic.Message.Contains("two-edge corner resolution rejected", StringComparison.Ordinal));}diff --git a/Aetheris.Kernel.Core/Brep/EdgeFinishing/BrepBoundedChamfer.cs b/Aetheris.Kernel.Core/Brep/EdgeFinishing/BrepBoundedChamfer.csindex ab974e85..9c8f4e4c 100644--- a/Aetheris.Kernel.Core/Brep/EdgeFinishing/BrepBoundedChamfer.cs+++ b/Aetheris.Kernel.Core/Brep/EdgeFinishing/BrepBoundedChamfer.cs@@ -247,6 +247,7 @@ public static class BrepBoundedChamfercontext => context.IsDistanceWithinLocalBounds,context => context.HasCoherentBoundedPlanarCut,context => context.PreservesManifoldTopology,+ context => context.IsTrustedOrthogonalBody,context => context.Corner == BrepBoundedChamferCorner.XMaxYMaxZMax);return@@ -476,17 +477,11 @@ public static class BrepBoundedChamferreturn KernelResult<BrepBody>.Failure([Failure($"Bounded chamfer two-edge corner construction rejected: {reason}.", "firmament.chamfer-bounded")]);}- if (!TryResolveCornerIncidentEdgesByToken(sourceBody, cornerVertex, cornerPoint, out var edgeByToken, out reason))+ if (!TryResolveCanonicalIncidentEdgePair(sourceBody, cornerVertex, cornerPoint, selector, out var firstEdge, out var secondEdge, out reason)){return KernelResult<BrepBody>.Failure([Failure($"Bounded chamfer two-edge corner construction rejected: {reason}.", "firmament.chamfer-bounded")]);}- if (!edgeByToken.TryGetValue(selector.First, out var firstEdge)- || !edgeByToken.TryGetValue(selector.Second, out var secondEdge))- {- return KernelResult<BrepBody>.Failure([Failure("Bounded chamfer two-edge corner construction rejected: selected edges were not resolved at the selected corner.", "firmament.chamfer-bounded")]);- }-var selectedEdgeIds = new HashSet<EdgeId> { firstEdge.Id, secondEdge.Id };var vertexPoints = sourceBody.Topology.Vertices@@ -595,6 +590,11 @@ public static class BrepBoundedChamfersplitVertexByIncidentEdge[secondEdge.Id],VertexToken.FromVertex(cornerVertex)};+ if (cutFace[0].Equals(cutFace[1]))+ {+ return KernelResult<BrepBody>.Failure([Failure("Bounded chamfer two-edge corner construction rejected: selected incident edge pair did not resolve to two distinct local split vertices.", "firmament.chamfer-bounded")]);+ }+var interiorPoint = new Point3D(vertexPoints.Values.Average(point => point.X),vertexPoints.Values.Average(point => point.Y),@@ -608,7 +608,92 @@ public static class BrepBoundedChamferreturn localRewriteResult;}- return CreateTrustedPolyhedralSingleCornerPlanarChamferBody(sourceBody, context);+ return KernelResult<BrepBody>.Failure(+ [+ Failure(+ "Bounded chamfer two-edge corner construction rejected: dedicated local rewrite failed to produce a valid manifold body for the supported canonical subset.",+ "firmament.chamfer-bounded"),+ .. localRewriteResult.Diagnostics+ ]);+ }++ private static bool TryResolveCanonicalIncidentEdgePair(+ BrepBody sourceBody,+ VertexId cornerVertex,+ Point3D cornerPoint,+ BrepBoundedChamferIncidentEdgePairSelector selector,+ out Edge first,+ out Edge second,+ out string reason)+ {+ const double eps = 1e-8d;+ first = default;+ second = default;+ reason = "selected edges were not resolved at the selected corner";+ var incidents = new List<(Edge Edge, Vector3D Vector)>();+ foreach (var incident in sourceBody.Topology.Edges.Where(candidate => candidate.StartVertexId == cornerVertex || candidate.EndVertexId == cornerVertex))+ {+ var otherVertex = incident.StartVertexId == cornerVertex ? incident.EndVertexId : incident.StartVertexId;+ if (!BrepBoundedChamferCornerContext.TryResolveVertexPoint(sourceBody, otherVertex, out var otherPoint))+ {+ reason = "source body is missing explicit resolvable points for incident edge vertices";+ return false;+ }++ incidents.Add((incident, otherPoint - cornerPoint));+ }++ if (incidents.Count < 2)+ {+ reason = "selected corner has fewer than two incident edges";+ return false;+ }++ static double Score(BrepBoundedChamferCornerIncidentEdge token, Vector3D vector, double eps)+ => token switch+ {+ BrepBoundedChamferCornerIncidentEdge.XNegative when vector.X < -eps => -vector.X,+ BrepBoundedChamferCornerIncidentEdge.YNegative when vector.Y < -eps => -vector.Y,+ BrepBoundedChamferCornerIncidentEdge.ZNegative when vector.Z < -eps => -vector.Z,+ _ => double.NegativeInfinity+ };++ var candidates = new List<(Edge First, Edge Second, double Score)>();+ foreach (var firstCandidate in incidents)+ {+ var firstScore = Score(selector.First, firstCandidate.Vector, eps);+ if (!double.IsFinite(firstScore))+ {+ continue;+ }++ foreach (var secondCandidate in incidents)+ {+ if (secondCandidate.Edge.Id == firstCandidate.Edge.Id)+ {+ continue;+ }++ var secondScore = Score(selector.Second, secondCandidate.Vector, eps);+ if (!double.IsFinite(secondScore))+ {+ continue;+ }++ candidates.Add((firstCandidate.Edge, secondCandidate.Edge, firstScore + secondScore));+ }+ }++ if (candidates.Count == 0)+ {+ reason = "selected incident edge pair did not resolve to two distinct token-matching edges at selected corner";+ return false;+ }++ var resolved = candidates.OrderByDescending(candidate => candidate.Score).First();+ first = resolved.First;+ second = resolved.Second;+ return true;}private static KernelResult<BrepBody> CreateTrustedPolyhedralSingleCornerPlanarChamferBody(@@ -772,7 +857,19 @@ public static class BrepBoundedChamfervar edgeIds = new Dictionary<(VertexToken, VertexToken), EdgeId>();var edgeEndpoints = new Dictionary<EdgeId, (VertexToken Start, VertexToken End)>();var faces = new List<FaceId>(faceCycles.Count);- foreach (var cycle in faceCycles)+ var normalizedFaceCycles = new List<IReadOnlyList<VertexToken>>(faceCycles.Count);+ for (var faceIndex = 0; faceIndex < faceCycles.Count; faceIndex++)+ {+ var normalizedCycle = NormalizeFaceCycle(faceCycles[faceIndex]);+ if (normalizedCycle is null)+ {+ return KernelResult<BrepBody>.Failure([Failure($"Bounded chamfer corner construction produced an invalid face loop at index {faceIndex}.", "firmament.chamfer-bounded")]);+ }++ normalizedFaceCycles.Add(normalizedCycle);+ }++ foreach (var cycle in normalizedFaceCycles){var edgeUses = new List<EdgeUse>(cycle.Count);for (var i = 0; i < cycle.Count; i++)@@ -822,9 +919,9 @@ public static class BrepBoundedChamfer}var surfaceId = 1;- for (var faceIndex = 0; faceIndex < faceCycles.Count; faceIndex++)+ for (var faceIndex = 0; faceIndex < normalizedFaceCycles.Count; faceIndex++){- var cycle = faceCycles[faceIndex];+ var cycle = normalizedFaceCycles[faceIndex];var points = cycle.Select(token => vertexPoints[token]).ToArray();if (!TryCreatePlane(points, out var origin, out var normal, out var uAxis)){@@ -844,6 +941,25 @@ public static class BrepBoundedChamfer: KernelResult<BrepBody>.Failure(validation.Diagnostics);}+ private static List<VertexToken>? NormalizeFaceCycle(IReadOnlyList<VertexToken> cycle)+ {+ var normalized = new List<VertexToken>(cycle.Count);+ foreach (var vertex in cycle)+ {+ if (normalized.Count == 0 || !normalized[^1].Equals(vertex))+ {+ normalized.Add(vertex);+ }+ }++ if (normalized.Count > 1 && normalized[0].Equals(normalized[^1]))+ {+ normalized.RemoveAt(normalized.Count - 1);+ }++ return normalized.Count >= 3 ? normalized : null;+ }+private static bool TryCreatePlane(IReadOnlyList<Point3D> points,out Point3D origin,diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentPrimitiveExecutionTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentPrimitiveExecutionTests.csindex 8c8bbe9b..544fb5f1 100644--- a/Aetheris.Kernel.Firmament.Tests/FirmamentPrimitiveExecutionTests.cs+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentPrimitiveExecutionTests.cs@@ -324,6 +324,16 @@ public sealed class FirmamentPrimitiveExecutionTestsdiagnostic.Message.Contains("two-edge corner resolution rejected", StringComparison.Ordinal));}+ [Fact]+ public void Compile_Rejects_BoundedChamfer_CornerIncidentEdgeSelector_On_NonOrthogonal_Root()+ {+ var result = CompileFixture("testdata/firmament/fixtures/m5a-invalid-chamfer-corner-incident-nonorthogonal-root.firmament");++ Assert.False(result.Compilation.IsSuccess);+ Assert.Contains(result.Compilation.Diagnostics, diagnostic =>+ diagnostic.Message.Contains("two-edge corner resolution rejected", StringComparison.Ordinal));+ }+[Fact]public void Compile_Executes_BoundedChamfer_SingleCorner_E2_On_BoxRoot_Into_Real_Body(){diff --git a/testdata/firmament/fixtures/m5a-invalid-chamfer-corner-incident-nonorthogonal-root.firmament b/testdata/firmament/fixtures/m5a-invalid-chamfer-corner-incident-nonorthogonal-root.firmamentnew file mode 100644index 00000000..7b07e9b4--- /dev/null+++ b/testdata/firmament/fixtures/m5a-invalid-chamfer-corner-incident-nonorthogonal-root.firmament@@ -0,0 +1,24 @@+firmament:+ version: 1++model:+ name: m5a_invalid_chamfer_corner_incident_nonorthogonal_root+ units: mm++ops[2]:+ -+ op: triangular_prism+ id: base+ base_width: 10+ base_depth: 6+ height: 8+ -+ op: chamfer+ id: corner_pair_break+ from: base+ corners[1]:+ x_max_y_max_z_max+ corner_edges[2]:+ x_neg+ y_neg+ distance: 1