Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
E5b: add bounded corner-first incident edge selector for chamfer
Yuechen Lic6b4a39ba392
diff --git a/Aetheris.Kernel.Core.Tests/Brep/EdgeFinishing/BrepBoundedEdgeFinishingToolParserTests.cs b/Aetheris.Kernel.Core.Tests/Brep/EdgeFinishing/BrepBoundedEdgeFinishingToolParserTests.csindex 9714fde6..99f363eb 100644--- a/Aetheris.Kernel.Core.Tests/Brep/EdgeFinishing/BrepBoundedEdgeFinishingToolParserTests.cs+++ b/Aetheris.Kernel.Core.Tests/Brep/EdgeFinishing/BrepBoundedEdgeFinishingToolParserTests.cs@@ -5,18 +5,34 @@ namespace Aetheris.Kernel.Core.Tests.Brep.EdgeFinishing;public sealed class BrepBoundedEdgeFinishingToolParserTests{[Fact]- public void TryParseChamferSelection_Allows_TwoEdgeCornerPair()+ public void TryParseChamferSelection_Allows_CornerFirst_IncidentEdgePair(){var fields = new Dictionary<string, string>{- ["edges"] = "[\"x_max_y_max\", \"x_max_y_min\"]"+ ["corners"] = "[\"x_max_y_max_z_max\"]",+ ["corner_edges"] = "[\"x_neg\", \"z_neg\"]"};- var ok = BrepBoundedEdgeFinishingToolParser.TryParseChamferSelection(fields, out var edge, out var edgePair, out var corner, out var error);+ var ok = BrepBoundedEdgeFinishingToolParser.TryParseChamferSelection(fields, out var edge, out var incidentEdgePair, out var corner, out var error);Assert.True(ok, error);Assert.Null(edge);- Assert.True(edgePair.HasValue);- Assert.Null(corner);+ Assert.True(incidentEdgePair.HasValue);+ Assert.Equal(BrepBoundedChamferCorner.XMaxYMaxZMax, corner);+ }++ [Fact]+ public void TryParseChamferSelection_Rejects_Duplicate_CornerIncidentEdgePair()+ {+ var fields = new Dictionary<string, string>+ {+ ["corners"] = "[\"x_max_y_max_z_max\"]",+ ["corner_edges"] = "[\"x_neg\", \"x_neg\"]"+ };++ var ok = BrepBoundedEdgeFinishingToolParser.TryParseChamferSelection(fields, out _, out _, out _, out var error);++ Assert.False(ok);+ Assert.Equal("corner-edge selector requires two distinct incident edge tokens", error);}}diff --git a/Aetheris.Kernel.Core/Brep/EdgeFinishing/BrepBoundedChamfer.cs b/Aetheris.Kernel.Core/Brep/EdgeFinishing/BrepBoundedChamfer.csindex d2cd522d..78a85e3a 100644--- a/Aetheris.Kernel.Core/Brep/EdgeFinishing/BrepBoundedChamfer.cs+++ b/Aetheris.Kernel.Core/Brep/EdgeFinishing/BrepBoundedChamfer.cs@@ -913,6 +913,102 @@ public readonly record struct BrepBoundedChamferCornerContext(var triple = edgeVectors[0].Cross(edgeVectors[1]).Dot(edgeVectors[2]);return double.Abs(triple) > 1e-9d;}++ public static KernelResult<bool> ValidateIncidentEdgePairSelector(+ BrepBody sourceBody,+ BrepBoundedChamferIncidentEdgePairSelector selector)+ {+ if (selector.First == selector.Second)+ {+ return KernelResult<bool>.Failure([new KernelDiagnostic(KernelDiagnosticCode.ValidationFailed, KernelDiagnosticSeverity.Error, "Bounded chamfer corner-edge selector requires two distinct incident edge tokens.", Source: "firmament.chamfer-bounded")]);+ }++ if (selector.Corner is not BrepBoundedChamferCorner.XMaxYMaxZMax)+ {+ return KernelResult<bool>.Failure([new KernelDiagnostic(KernelDiagnosticCode.ValidationFailed, KernelDiagnosticSeverity.Error, "Bounded chamfer corner-edge selector supports only corner token x_max_y_max_z_max.", Source: "firmament.chamfer-bounded")]);+ }++ if (!BrepBoundedChamferCornerContext.TryResolveMaxCornerVertex(sourceBody, out var cornerVertex, out var cornerPoint, out var reason))+ {+ return KernelResult<bool>.Failure([new KernelDiagnostic(KernelDiagnosticCode.ValidationFailed, KernelDiagnosticSeverity.Error, $"Bounded chamfer corner-edge selector rejected: {reason}.", Source: "firmament.chamfer-bounded")]);+ }++ var byToken = new Dictionary<BrepBoundedChamferCornerIncidentEdge, Edge>(3);+ foreach (var edge in sourceBody.Topology.Edges.Where(edge => edge.StartVertexId == cornerVertex || edge.EndVertexId == cornerVertex))+ {+ var otherVertex = edge.StartVertexId == cornerVertex ? edge.EndVertexId : edge.StartVertexId;+ if (!BrepBoundedChamferCornerContext.TryResolveVertexPoint(sourceBody, otherVertex, out var otherPoint))+ {+ return KernelResult<bool>.Failure([new KernelDiagnostic(KernelDiagnosticCode.ValidationFailed, KernelDiagnosticSeverity.Error, "Bounded chamfer corner-edge selector requires explicit resolvable points for incident edge vertices.", Source: "firmament.chamfer-bounded")]);+ }++ var vector = otherPoint - cornerPoint;+ if (!TryClassifyCornerIncidentEdge(vector, out var token))+ {+ continue;+ }++ byToken[token] = edge;+ }++ if (!byToken.TryGetValue(selector.First, out var firstEdge))+ {+ return KernelResult<bool>.Failure([new KernelDiagnostic(KernelDiagnosticCode.ValidationFailed, KernelDiagnosticSeverity.Error, $"Bounded chamfer corner-edge selector could not resolve selected incident edge '{selector.First}'.", Source: "firmament.chamfer-bounded")]);+ }++ if (!byToken.TryGetValue(selector.Second, out var secondEdge))+ {+ return KernelResult<bool>.Failure([new KernelDiagnostic(KernelDiagnosticCode.ValidationFailed, KernelDiagnosticSeverity.Error, $"Bounded chamfer corner-edge selector could not resolve selected incident edge '{selector.Second}'.", Source: "firmament.chamfer-bounded")]);+ }++ var firstIncident = firstEdge.StartVertexId == cornerVertex || firstEdge.EndVertexId == cornerVertex;+ var secondIncident = secondEdge.StartVertexId == cornerVertex || secondEdge.EndVertexId == cornerVertex;+ if (!firstIncident || !secondIncident)+ {+ return KernelResult<bool>.Failure([new KernelDiagnostic(KernelDiagnosticCode.ValidationFailed, KernelDiagnosticSeverity.Error, "Bounded chamfer corner-edge selector rejected: selected edges do not share the selected corner vertex.", Source: "firmament.chamfer-bounded")]);+ }++ return KernelResult<bool>.Success(true);+ }++ private static bool TryClassifyCornerIncidentEdge(Vector3D vector, out BrepBoundedChamferCornerIncidentEdge edge)+ {+ edge = default;+ var eps = 1e-8d;+ if (vector.Length <= eps)+ {+ return false;+ }++ var absX = double.Abs(vector.X);+ var absY = double.Abs(vector.Y);+ var absZ = double.Abs(vector.Z);+ var dominant = double.Max(absX, double.Max(absY, absZ));+ if (dominant <= eps)+ {+ return false;+ }++ if (absX >= absY && absX >= absZ && vector.X < 0d)+ {+ edge = BrepBoundedChamferCornerIncidentEdge.XNegative;+ return true;+ }++ if (absY >= absX && absY >= absZ && vector.Y < 0d)+ {+ edge = BrepBoundedChamferCornerIncidentEdge.YNegative;+ return true;+ }++ if (absZ >= absX && absZ >= absY && vector.Z < 0d)+ {+ edge = BrepBoundedChamferCornerIncidentEdge.ZNegative;+ return true;+ }++ return false;+ }}public enum BrepBoundedChamferCorner@@ -927,3 +1023,15 @@ public enum BrepBoundedChamferEdgeXMaxYMin,XMaxYMax}++public enum BrepBoundedChamferCornerIncidentEdge+{+ XNegative,+ YNegative,+ ZNegative+}++public readonly record struct BrepBoundedChamferIncidentEdgePairSelector(+ BrepBoundedChamferCorner Corner,+ BrepBoundedChamferCornerIncidentEdge First,+ BrepBoundedChamferCornerIncidentEdge Second);diff --git a/Aetheris.Kernel.Core/Brep/EdgeFinishing/BrepBoundedEdgeFinishingToolParser.cs b/Aetheris.Kernel.Core/Brep/EdgeFinishing/BrepBoundedEdgeFinishingToolParser.csindex 7f152bb6..2444ce68 100644--- a/Aetheris.Kernel.Core/Brep/EdgeFinishing/BrepBoundedEdgeFinishingToolParser.cs+++ b/Aetheris.Kernel.Core/Brep/EdgeFinishing/BrepBoundedEdgeFinishingToolParser.cs@@ -9,37 +9,95 @@ public static class BrepBoundedEdgeFinishingToolParserpublic static bool TryParseChamferSelection(IReadOnlyDictionary<string, string> fields,out BrepBoundedChamferEdge? edge,- out (BrepBoundedChamferEdge First, BrepBoundedChamferEdge Second)? edgePair,+ out BrepBoundedChamferIncidentEdgePairSelector? incidentEdgePair,out BrepBoundedChamferCorner? corner,out string error){edge = null;- edgePair = null;+ incidentEdgePair = null;corner = null;error = string.Empty;var hasEdges = fields.TryGetValue("edges", out var edgesRaw) && !string.IsNullOrWhiteSpace(edgesRaw);var hasCorners = fields.TryGetValue("corners", out var cornersRaw) && !string.IsNullOrWhiteSpace(cornersRaw);+ var hasCornerEdges = fields.TryGetValue("corner_edges", out var cornerEdgesRaw) && !string.IsNullOrWhiteSpace(cornerEdgesRaw);++ if (hasCornerEdges)+ {+ if (hasEdges)+ {+ error = "corner_edges selector mode does not allow edges";+ return false;+ }++ if (!hasCorners)+ {+ error = "corner_edges selector mode requires exactly one corner token";+ return false;+ }++ var cornerTokens = ParseSingleTokenArray(cornersRaw!, out error);+ if (cornerTokens is null)+ {+ return false;+ }++ if (cornerTokens.Length != 1)+ {+ error = "bounded E5b corner-edge selector supports exactly one corner token";+ return false;+ }++ if (cornerTokens[0] is not "x_max_y_max_z_max")+ {+ error = "supported corner tokens are x_max_y_max_z_max";+ return false;+ }++ var edgeTokens = ParseSingleTokenArray(cornerEdgesRaw!, out error);+ if (edgeTokens is null)+ {+ return false;+ }++ if (edgeTokens.Length != 2)+ {+ error = "bounded E5b corner-edge selector requires exactly two incident corner edge tokens";+ return false;+ }++ if (!TryParseIncidentCornerEdgeToken(edgeTokens[0], out var first)+ || !TryParseIncidentCornerEdgeToken(edgeTokens[1], out var second))+ {+ error = "supported corner edge tokens are x_neg, y_neg, z_neg";+ return false;+ }++ if (first == second)+ {+ error = "corner-edge selector requires two distinct incident edge tokens";+ return false;+ }++ corner = BrepBoundedChamferCorner.XMaxYMaxZMax;+ incidentEdgePair = new BrepBoundedChamferIncidentEdgePairSelector(corner.Value, first, second);+ return true;+ }+if (hasEdges == hasCorners){- error = "provide exactly one of edges or corners";+ error = "provide exactly one selector family: edges, corners, or corners+corner_edges";return false;}if (hasEdges){- if (!TryParseChamferEdges(fields, out var parsedEdges, out error))+ if (!TryParseChamferEdge(fields, out var parsedEdge, out error)){return false;}- if (parsedEdges.Length == 1)- {- edge = parsedEdges[0];- return true;- }-- edgePair = (parsedEdges[0], parsedEdges[1]);+ edge = parsedEdge;return true;}@@ -106,9 +164,9 @@ public static class BrepBoundedEdgeFinishingToolParserreturn false;}- if (tokens.Length is < 1 or > 2)+ if (tokens.Length != 1){- error = "bounded M5a supports either one edge token or a two-edge corner pair";+ error = "bounded M5a supports exactly one explicit edge token";return false;}@@ -131,12 +189,6 @@ public static class BrepBoundedEdgeFinishingToolParser}}- if (parsed.Length == 2 && parsed[0] == parsed[1])- {- error = "two-edge corner pair requires two distinct edge tokens";- return false;- }-edges = parsed;return true;}@@ -185,6 +237,19 @@ public static class BrepBoundedEdgeFinishingToolParserreturn true;}+ private static bool TryParseIncidentCornerEdgeToken(string token, out BrepBoundedChamferCornerIncidentEdge edge)+ {+ edge = token switch+ {+ "x_neg" => BrepBoundedChamferCornerIncidentEdge.XNegative,+ "y_neg" => BrepBoundedChamferCornerIncidentEdge.YNegative,+ "z_neg" => BrepBoundedChamferCornerIncidentEdge.ZNegative,+ _ => default+ };++ return token is "x_neg" or "y_neg" or "z_neg";+ }+private static string[]? ParseSingleTokenArray(string raw, out string error){error = string.Empty;diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentBooleanRequiredFieldValidationTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentBooleanRequiredFieldValidationTests.csindex 218de2d6..ffc1b2ae 100644--- a/Aetheris.Kernel.Firmament.Tests/FirmamentBooleanRequiredFieldValidationTests.cs+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentBooleanRequiredFieldValidationTests.cs@@ -227,6 +227,70 @@ public sealed class FirmamentBooleanRequiredFieldValidationTestsFirmamentDiagnosticCodes.BooleanInvalidFieldValue,"Boolean op 'chamfer' at index 1 has invalid field 'edges'; supported bounded M5a edge tokens are x_min_y_min, x_min_y_max, x_max_y_min, x_max_y_max.");+ [Fact]+ public void Compiler_Rejects_ChamferCornerIncidentEdgeSelector_DuplicateEdgeToken() =>+ AssertBooleanFailure(+ """+ firmament:+ version: 1++ model:+ name: demo+ units: mm++ ops[2]:+ -+ op: box+ id: base+ size[3]:+ 20+ 20+ 20+ -+ op: chamfer+ id: corner_break+ from: base+ corners[1]:+ x_max_y_max_z_max+ corner_edges[2]:+ x_neg+ x_neg+ distance: 1+ """,+ FirmamentDiagnosticCodes.BooleanInvalidFieldValue,+ "Boolean op 'chamfer' at index 1 has invalid field 'corner_edges'; bounded E5b corner-edge selector requires two distinct incident edge tokens.");++ [Fact]+ public void Compiler_Rejects_Chamfer_TwoEdge_NonCornerFirst_Selector_Form() =>+ AssertBooleanFailure(+ """+ firmament:+ version: 1++ model:+ name: demo+ units: mm++ ops[2]:+ -+ op: box+ id: base+ size[3]:+ 20+ 20+ 20+ -+ op: chamfer+ id: edge_break+ from: base+ edges[2]:+ x_max_y_max+ x_max_y_min+ distance: 1+ """,+ FirmamentDiagnosticCodes.BooleanInvalidFieldTypeOrShape,+ "Boolean op 'chamfer' at index 1 has invalid field 'edges'; expected a single-item string array with one explicit edge token.");+[Fact]public void Compiler_Accepts_ValidBoundedChamferCornerE2Shape() =>AssertValidBooleanOp("""diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentPrimitiveExecutionTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentPrimitiveExecutionTests.csindex bcabda57..fa8b83e9 100644--- a/Aetheris.Kernel.Firmament.Tests/FirmamentPrimitiveExecutionTests.cs+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentPrimitiveExecutionTests.cs@@ -303,13 +303,13 @@ public sealed class FirmamentPrimitiveExecutionTests}[Fact]- public void Compile_Rejects_BoundedChamfer_TwoEdgeCornerPair_When_Tokens_Do_Not_Name_Incident_VertexEdges()+ public void Compile_BoundedChamfer_CornerIncidentEdgeSelector_Reaches_Explicit_2EdgeGeometryBlocker(){- var result = CompileFixture("testdata/firmament/fixtures/m5a-invalid-chamfer-two-edge-corner-selector.firmament");+ var result = CompileFixture("testdata/firmament/fixtures/m5a-valid-chamfer-corner-incident-edge-selector.firmament");Assert.False(result.Compilation.IsSuccess);Assert.Contains(result.Compilation.Diagnostics, diagnostic =>- diagnostic.Message.Contains("requires a selector family that names two incident edges at one vertex", StringComparison.Ordinal));+ diagnostic.Message.Contains("selector resolved to a valid local incident-edge pair", StringComparison.Ordinal));}[Fact]diff --git a/Aetheris.Kernel.Firmament/Execution/FirmamentPrimitiveExecutor.cs b/Aetheris.Kernel.Firmament/Execution/FirmamentPrimitiveExecutor.csindex 1180173b..51a10dc2 100644--- a/Aetheris.Kernel.Firmament/Execution/FirmamentPrimitiveExecutor.cs+++ b/Aetheris.Kernel.Firmament/Execution/FirmamentPrimitiveExecutor.cs@@ -738,7 +738,7 @@ internal static class FirmamentPrimitiveExecutor]);}- if (!BrepBoundedEdgeFinishingToolParser.TryParseChamferSelection(boolean.Tool.RawFields, out var edge, out var edgePair, out var corner, out var edgeError))+ if (!BrepBoundedEdgeFinishingToolParser.TryParseChamferSelection(boolean.Tool.RawFields, out var edge, out var incidentEdgePair, out var corner, out var edgeError)){return KernelResult<BrepBody>.Failure([@@ -779,14 +779,14 @@ internal static class FirmamentPrimitiveExecutorreturn BrepBoundedChamfer.ChamferAxisAlignedBoxVerticalEdge(box, edge.Value, distance);}- if (edgePair.HasValue)+ if (incidentEdgePair.HasValue){return KernelResult<BrepBody>.Failure([new KernelDiagnostic(KernelDiagnosticCode.ValidationFailed,KernelDiagnosticSeverity.Error,- "Bounded chamfer two-edge corner resolution requires a selector family that names two incident edges at one vertex; current bounded edge tokens identify disjoint vertical edges only.",+ "Bounded chamfer two-edge corner selector resolved to a valid local incident-edge pair; geometric 2-edge corner constructor is the next isolated blocker.",Source: "firmament.chamfer-bounded")]);}diff --git a/Aetheris.Kernel.Firmament/Validation/FirmamentBooleanRequiredFieldValidator.cs b/Aetheris.Kernel.Firmament/Validation/FirmamentBooleanRequiredFieldValidator.csindex 88e87330..13f6a3a1 100644--- a/Aetheris.Kernel.Firmament/Validation/FirmamentBooleanRequiredFieldValidator.cs+++ b/Aetheris.Kernel.Firmament/Validation/FirmamentBooleanRequiredFieldValidator.cs@@ -163,27 +163,64 @@ internal static class FirmamentBooleanRequiredFieldValidatorvar hasEdges = entry.RawFields.TryGetValue("edges", out var edgesRaw) && !string.IsNullOrWhiteSpace(edgesRaw);var hasCorners = entry.RawFields.TryGetValue("corners", out var cornersRaw) && !string.IsNullOrWhiteSpace(cornersRaw);+ var hasCornerEdges = entry.RawFields.TryGetValue("corner_edges", out var cornerEdgesRaw) && !string.IsNullOrWhiteSpace(cornerEdgesRaw);++ if (hasCornerEdges)+ {+ if (hasEdges)+ {+ return InvalidFieldValue("corner_edges", opIndex, entry.OpName, "bounded E5b corner-edge selector does not allow edges; use corners[1] with corner_edges[2]");+ }++ if (!hasCorners)+ {+ return MissingField("corners", opIndex, entry.OpName);+ }++ if (!TryParseFaceTokens(cornersRaw!, out var incidentCornerTokens) || incidentCornerTokens.Count != 1)+ {+ return InvalidFieldTypeOrShape("corners", opIndex, entry.OpName, "expected a single-item string array with one explicit corner token");+ }++ if (incidentCornerTokens[0] is not "x_max_y_max_z_max")+ {+ return InvalidFieldValue("corners", opIndex, entry.OpName, "supported bounded E5b corner token is x_max_y_max_z_max");+ }++ if (!TryParseFaceTokens(cornerEdgesRaw!, out var incidentEdgeTokens) || incidentEdgeTokens.Count != 2)+ {+ return InvalidFieldTypeOrShape("corner_edges", opIndex, entry.OpName, "expected a two-item string array with two explicit incident corner-edge tokens");+ }++ if (incidentEdgeTokens.Any(token => token is not ("x_neg" or "y_neg" or "z_neg")))+ {+ return InvalidFieldValue("corner_edges", opIndex, entry.OpName, "supported bounded E5b corner-edge tokens are x_neg, y_neg, z_neg");+ }++ if (string.Equals(incidentEdgeTokens[0], incidentEdgeTokens[1], StringComparison.Ordinal))+ {+ return InvalidFieldValue("corner_edges", opIndex, entry.OpName, "bounded E5b corner-edge selector requires two distinct incident edge tokens");+ }++ return KernelResult<bool>.Success(true);+ }+if (hasEdges == hasCorners){- return InvalidFieldValue("edges", opIndex, entry.OpName, "bounded E2 chamfer requires exactly one selector family: either edges[1] or corners[1]");+ return InvalidFieldValue("edges", opIndex, entry.OpName, "bounded E2 chamfer requires exactly one selector family: edges[1], corners[1], or corners[1]+corner_edges[2]");}if (hasEdges){- if (!TryParseFaceTokens(edgesRaw!, out var edgeTokens) || edgeTokens.Count is < 1 or > 2)+ if (!TryParseFaceTokens(edgesRaw!, out var edgeTokens) || edgeTokens.Count != 1){- return InvalidFieldTypeOrShape("edges", opIndex, entry.OpName, "expected a string array with one explicit edge token or one two-edge corner pair");+ return InvalidFieldTypeOrShape("edges", opIndex, entry.OpName, "expected a single-item string array with one explicit edge token");}if (edgeTokens.Any(token => token is not ("x_min_y_min" or "x_min_y_max" or "x_max_y_min" or "x_max_y_max"))){return InvalidFieldValue("edges", opIndex, entry.OpName, "supported bounded M5a edge tokens are x_min_y_min, x_min_y_max, x_max_y_min, x_max_y_max");}-- if (edgeTokens.Count == 2 && string.Equals(edgeTokens[0], edgeTokens[1], StringComparison.Ordinal))- {- return InvalidFieldValue("edges", opIndex, entry.OpName, "bounded M5a two-edge corner pair requires two distinct edge tokens");- }}else{diff --git a/testdata/firmament/fixtures/m5a-valid-chamfer-corner-incident-edge-selector.firmament b/testdata/firmament/fixtures/m5a-valid-chamfer-corner-incident-edge-selector.firmamentnew file mode 100644index 00000000..e0b5386b--- /dev/null+++ b/testdata/firmament/fixtures/m5a-valid-chamfer-corner-incident-edge-selector.firmament@@ -0,0 +1,25 @@+firmament:+ version: 1++model:+ name: m5a_valid_chamfer_corner_incident_edge_selector+ units: mm++ops[2]:+ -+ op: box+ id: base+ size[3]:+ 20+ 20+ 20+ -+ 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