Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
Relax bounded box union classification with fallback JudgmentEngine candidate
Yuechen Licb0576510460
diff --git a/Aetheris.Kernel.Core.Tests/Brep/Boolean/BrepBooleanTests.cs b/Aetheris.Kernel.Core.Tests/Brep/Boolean/BrepBooleanTests.csindex f46f69d0..39337453 100644--- a/Aetheris.Kernel.Core.Tests/Brep/Boolean/BrepBooleanTests.cs+++ b/Aetheris.Kernel.Core.Tests/Brep/Boolean/BrepBooleanTests.cs@@ -267,10 +267,37 @@ public sealed class BrepBooleanTestsvar result = BrepBoolean.Union(left, right);+ Assert.True(result.IsSuccess);+ Assert.NotNull(result.Value.SafeBooleanComposition);+ Assert.True(result.Value.SafeBooleanComposition!.OccupiedCells is { Count: > 0 });+ Assert.False(BrepBooleanBoxRecognition.TryRecognizeAxisAlignedBox(result.Value, ToleranceContext.Default, out _, out _));+ }++ [Fact]+ public void Union_FaceContactWithoutSharedFullSpan_Rebuilds_BoundedOrthogonalUnion()+ {+ var left = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(0d, 2d, 0d, 2d, 0d, 2d)).Value;+ var right = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(2d, 4d, 0.5d, 1.5d, 0.5d, 1.5d)).Value;++ var result = BrepBoolean.Union(left, right);++ Assert.True(result.IsSuccess);+ Assert.NotNull(result.Value.SafeBooleanComposition);+ Assert.True(result.Value.SafeBooleanComposition!.OccupiedCells is { Count: > 0 });+ }++ [Fact]+ public void Union_EdgeOnlyContact_RemainsRejectedWithClearDiagnostic()+ {+ var left = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(0d, 2d, 0d, 2d, 0d, 2d)).Value;+ var right = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(2d, 4d, 2d, 4d, 0d, 2d)).Value;++ var result = BrepBoolean.Union(left, right);+Assert.False(result.IsSuccess);var diagnostic = Assert.Single(result.Diagnostics);Assert.Equal(KernelDiagnosticCode.NotImplemented, diagnostic.Code);- Assert.Contains("bounded F1 additive family", diagnostic.Message, StringComparison.Ordinal);+ Assert.Contains("edge-only/point-only unions are non-manifold", diagnostic.Message, StringComparison.Ordinal);}[Fact]@@ -1463,6 +1490,28 @@ public sealed class BrepBooleanTestsAssert.Null(classification.RightAnalyticSurface);}+ [Fact]+ public void ClassifyBoundedOrthogonalUnion_StrictSpanCase_PrefersStrictCandidate()+ {+ var left = new AxisAlignedBoxExtents(0d, 2d, 0d, 4d, 0d, 2d);+ var right = new AxisAlignedBoxExtents(2d, 4d, 0d, 2d, 0d, 2d);++ var candidate = BrepBoolean.ClassifyBoundedOrthogonalUnionCandidateForTesting(left, right, ToleranceContext.Default);++ Assert.Equal(BrepBoolean.BoxBoxUnionOrthogonalCellsStrictCandidate, candidate);+ }++ [Fact]+ public void ClassifyBoundedOrthogonalUnion_FaceContactWithoutSharedSpan_UsesFaceContactCandidate()+ {+ var left = new AxisAlignedBoxExtents(0d, 2d, 0d, 2d, 0d, 2d);+ var right = new AxisAlignedBoxExtents(2d, 4d, 0.5d, 1.5d, 0.5d, 1.5d);++ var candidate = BrepBoolean.ClassifyBoundedOrthogonalUnionCandidateForTesting(left, right, ToleranceContext.Default);++ Assert.Equal(BrepBoolean.BoxBoxUnionFaceContactCellsCandidate, candidate);+ }+[Fact]public void ClassifyBooleanCase_BoxCylinderSubtract_IsPlanarWithAnalyticHole(){diff --git a/Aetheris.Kernel.Core/Brep/Boolean/BrepBoolean.cs b/Aetheris.Kernel.Core/Brep/Boolean/BrepBoolean.csindex 0a94c316..8ec91490 100644--- a/Aetheris.Kernel.Core/Brep/Boolean/BrepBoolean.cs+++ b/Aetheris.Kernel.Core/Brep/Boolean/BrepBoolean.cs@@ -97,6 +97,16 @@ internal sealed record BooleanCaseContext(bool IsCylinderRootKeywaySupported,string? CylinderRootKeywayUnsupportedReason);+internal sealed record OrthogonalUnionClassificationContext(+ AxisAlignedBoxExtents Left,+ AxisAlignedBoxExtents Right,+ ToleranceContext Tolerance,+ int SharedAxisSpanCount,+ bool HasPositiveVolumeOverlap,+ bool HasPositiveAreaFaceContact,+ IReadOnlyList<AxisAlignedBoxExtents> CandidateCells,+ bool IsConnectedCellUnion);+/// <summary>/// bounded boolean pipeline with narrow real support for axis-aligned box/box cases that resolve to a single box/// plus the M10k box-minus-Z-aligned-through-cylinder subset that rebuilds to a single box-with-hole solid.@@ -104,6 +114,9 @@ internal sealed record BooleanCaseContext(/// </summary>public static class BrepBoolean{+ internal const string BoxBoxUnionOrthogonalCellsStrictCandidate = "box_box_union_orthogonal_cells_strict";+ internal const string BoxBoxUnionFaceContactCellsCandidate = "box_box_union_face_contact_cells";+public static KernelResult<BrepBody> Union(BrepBody left, BrepBody right)=> Execute(new BooleanRequest(left, right, BooleanOperation.Union));@@ -583,7 +596,7 @@ public static class BrepBooleanreturn new BooleanClassificationData(intersections, IsComputed: true, FragmentCount: 0, SingleBoxResult: null, SafeCompositionResult: null, UnsupportedReason: $"Boolean {operation}: disjoint box union is multi-body and not supported in the bounded boolean family.");}- if (TryClassifyBoundedOrthogonalUnion(left, right, tolerance, out var unionCells, out var unsupportedReason))+ if (TryClassifyBoundedOrthogonalUnion(left, right, tolerance, out var unionCells, out var unsupportedReason, out _)){var unionBounds = AxisAlignedBoxExtents.Bounding(left, right);return new BooleanClassificationData(@@ -780,32 +793,12 @@ public static class BrepBooleanAxisAlignedBoxExtents right,ToleranceContext tolerance,out IReadOnlyList<AxisAlignedBoxExtents> occupiedCells,- out string? unsupportedReason)+ out string? unsupportedReason,+ out string? selectedCandidateName){occupiedCells = Array.Empty<AxisAlignedBoxExtents>();unsupportedReason = null;-- var sharedAxisSpanCount = 0;- if (ToleranceMath.AlmostEqual(left.MinX, right.MinX, tolerance) && ToleranceMath.AlmostEqual(left.MaxX, right.MaxX, tolerance))- {- sharedAxisSpanCount++;- }-- if (ToleranceMath.AlmostEqual(left.MinY, right.MinY, tolerance) && ToleranceMath.AlmostEqual(left.MaxY, right.MaxY, tolerance))- {- sharedAxisSpanCount++;- }-- if (ToleranceMath.AlmostEqual(left.MinZ, right.MinZ, tolerance) && ToleranceMath.AlmostEqual(left.MaxZ, right.MaxZ, tolerance))- {- sharedAxisSpanCount++;- }-- if (sharedAxisSpanCount == 0)- {- unsupportedReason = "Boolean Union: box union is connected but outside the bounded F1 additive family because the operands do not share a full span on any primary axis.";- return false;- }+ selectedCandidateName = null;var intersection = AxisAlignedBoxExtents.Intersection(left, right);if (intersection is null)@@ -818,44 +811,53 @@ public static class BrepBooleanvar connectedByFace = IsPositiveAreaFaceContact(left, right, tolerance);if (!connectedByVolume && !connectedByFace){- unsupportedReason = "Boolean Union: edge-only or point-only contact is non-manifold and outside the bounded F1 additive family.";+ unsupportedReason = "Boolean Union: boxes do not intersect or touch with positive-area face contact; edge-only/point-only unions are non-manifold.";return false;}- var splitX = new[] { left.MinX, left.MaxX, right.MinX, right.MaxX }.Distinct().OrderBy(v => v).ToArray();- var splitY = new[] { left.MinY, left.MaxY, right.MinY, right.MaxY }.Distinct().OrderBy(v => v).ToArray();- var splitZ = new[] { left.MinZ, left.MaxZ, right.MinZ, right.MaxZ }.Distinct().OrderBy(v => v).ToArray();- var cells = new List<AxisAlignedBoxExtents>();- for (var ix = 0; ix < splitX.Length - 1; ix++)+ if (!TryBuildBoundedOrthogonalUnionCells(left, right, tolerance, out var cells)){- for (var iy = 0; iy < splitY.Length - 1; iy++)- {- for (var iz = 0; iz < splitZ.Length - 1; iz++)- {- var candidate = new AxisAlignedBoxExtents(splitX[ix], splitX[ix + 1], splitY[iy], splitY[iy + 1], splitZ[iz], splitZ[iz + 1]);- if (!candidate.HasPositiveVolume(tolerance))- {- continue;- }-- if (left.Contains(candidate, tolerance) || right.Contains(candidate, tolerance))- {- cells.Add(candidate);- }- }- }+ unsupportedReason = "Boolean Union: bounded orthogonal reconstruction produced no occupied cells.";+ return false;}- if (cells.Count == 0)- {- unsupportedReason = "Boolean Union: bounded orthogonal reconstruction produced no occupied cells.";+ var sharedAxisSpanCount = CountSharedFullAxisSpans(left, right, tolerance);+ var isConnectedCellUnion = TryValidateOrthogonalCellConnectivity(cells, tolerance);+ var candidateContext = new OrthogonalUnionClassificationContext(+ left,+ right,+ tolerance,+ sharedAxisSpanCount,+ connectedByVolume,+ connectedByFace,+ cells,+ isConnectedCellUnion);++ var classification = new JudgmentEngine<OrthogonalUnionClassificationContext>().Evaluate(candidateContext, BuildOrthogonalUnionCandidates());+ if (!classification.IsSuccess)+ {+ var closestRejection = classification.Rejections.FirstOrDefault();+ unsupportedReason = closestRejection.CandidateName is null+ ? "Boolean Union: union not reconstructible via orthogonal cell decomposition."+ : $"Boolean Union: candidate '{closestRejection.CandidateName}' rejected: {closestRejection.Reason}";return false;}+ selectedCandidateName = classification.Selection!.Value.Candidate.Name;occupiedCells = cells;return true;}+ internal static string? ClassifyBoundedOrthogonalUnionCandidateForTesting(+ AxisAlignedBoxExtents left,+ AxisAlignedBoxExtents right,+ ToleranceContext tolerance)+ {+ return TryClassifyBoundedOrthogonalUnion(left, right, tolerance, out _, out _, out var selectedCandidateName)+ ? selectedCandidateName+ : null;+ }+private static bool TryClassifyBoundedOrthogonalUnionWithExistingRoot(SafeBooleanComposition leftComposition,AxisAlignedBoxExtents right,@@ -964,6 +966,104 @@ public static class BrepBooleanreturn visited.Count == cells.Count;}+ private static IReadOnlyList<JudgmentCandidate<OrthogonalUnionClassificationContext>> BuildOrthogonalUnionCandidates()+ =>+ [+ new JudgmentCandidate<OrthogonalUnionClassificationContext>(+ Name: BoxBoxUnionOrthogonalCellsStrictCandidate,+ IsAdmissible: context => context.SharedAxisSpanCount > 0+ && (context.HasPositiveVolumeOverlap || context.HasPositiveAreaFaceContact)+ && context.CandidateCells.Count > 0+ && context.IsConnectedCellUnion,+ Score: context => 200d + context.SharedAxisSpanCount,+ RejectionReason: context => context.SharedAxisSpanCount == 0+ ? "strict candidate requires shared full axis span on at least one primary axis."+ : !context.IsConnectedCellUnion+ ? "union not reconstructible via orthogonal cell decomposition (cells are disconnected or only edge-connected)."+ : "strict orthogonal union candidate predicates were not satisfied."),+ new JudgmentCandidate<OrthogonalUnionClassificationContext>(+ Name: BoxBoxUnionFaceContactCellsCandidate,+ IsAdmissible: context => (context.HasPositiveVolumeOverlap || context.HasPositiveAreaFaceContact)+ && context.CandidateCells.Count > 0+ && context.IsConnectedCellUnion,+ Score: context => context.HasPositiveVolumeOverlap ? 125d : 100d,+ RejectionReason: context =>+ {+ if (!context.HasPositiveVolumeOverlap && !context.HasPositiveAreaFaceContact)+ {+ return "boxes do not intersect or touch with positive-area face contact.";+ }++ if (context.CandidateCells.Count == 0)+ {+ return "union not reconstructible via orthogonal cell decomposition (no occupied cells).";+ }++ if (!context.IsConnectedCellUnion)+ {+ return "ambiguous multi-region union not supported: bounded orthogonal cells are disconnected or only edge-connected.";+ }++ return "face-contact orthogonal cell candidate predicates were not satisfied.";+ }),+ ];++ private static int CountSharedFullAxisSpans(AxisAlignedBoxExtents left, AxisAlignedBoxExtents right, ToleranceContext tolerance)+ {+ var sharedAxisSpanCount = 0;+ if (ToleranceMath.AlmostEqual(left.MinX, right.MinX, tolerance) && ToleranceMath.AlmostEqual(left.MaxX, right.MaxX, tolerance))+ {+ sharedAxisSpanCount++;+ }++ if (ToleranceMath.AlmostEqual(left.MinY, right.MinY, tolerance) && ToleranceMath.AlmostEqual(left.MaxY, right.MaxY, tolerance))+ {+ sharedAxisSpanCount++;+ }++ if (ToleranceMath.AlmostEqual(left.MinZ, right.MinZ, tolerance) && ToleranceMath.AlmostEqual(left.MaxZ, right.MaxZ, tolerance))+ {+ sharedAxisSpanCount++;+ }++ return sharedAxisSpanCount;+ }++ private static bool TryBuildBoundedOrthogonalUnionCells(+ AxisAlignedBoxExtents left,+ AxisAlignedBoxExtents right,+ ToleranceContext tolerance,+ out IReadOnlyList<AxisAlignedBoxExtents> cells)+ {+ cells = Array.Empty<AxisAlignedBoxExtents>();+ var splitX = new[] { left.MinX, left.MaxX, right.MinX, right.MaxX }.Distinct().OrderBy(v => v).ToArray();+ var splitY = new[] { left.MinY, left.MaxY, right.MinY, right.MaxY }.Distinct().OrderBy(v => v).ToArray();+ var splitZ = new[] { left.MinZ, left.MaxZ, right.MinZ, right.MaxZ }.Distinct().OrderBy(v => v).ToArray();+ var reconstructedCells = new List<AxisAlignedBoxExtents>();+ for (var ix = 0; ix < splitX.Length - 1; ix++)+ {+ for (var iy = 0; iy < splitY.Length - 1; iy++)+ {+ for (var iz = 0; iz < splitZ.Length - 1; iz++)+ {+ var candidate = new AxisAlignedBoxExtents(splitX[ix], splitX[ix + 1], splitY[iy], splitY[iy + 1], splitZ[iz], splitZ[iz + 1]);+ if (!candidate.HasPositiveVolume(tolerance))+ {+ continue;+ }++ if (left.Contains(candidate, tolerance) || right.Contains(candidate, tolerance))+ {+ reconstructedCells.Add(candidate);+ }+ }+ }+ }++ cells = reconstructedCells;+ return reconstructedCells.Count > 0;+ }+private static bool TryClassifyBoundedCylinderRootKeyway(in SafeBooleanRootDescriptor rootDescriptor,AxisAlignedBoxExtents toolBox,