Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
G6: broaden bounded additive root recognition for subtract re-entry
Yuechen Li1ae1a3d1dfc6
diff --git a/Aetheris.Kernel.Core.Tests/Brep/Boolean/BrepBooleanTests.cs b/Aetheris.Kernel.Core.Tests/Brep/Boolean/BrepBooleanTests.csindex 58a91b66..61f26366 100644--- a/Aetheris.Kernel.Core.Tests/Brep/Boolean/BrepBooleanTests.cs+++ b/Aetheris.Kernel.Core.Tests/Brep/Boolean/BrepBooleanTests.cs@@ -136,6 +136,21 @@ public sealed class BrepBooleanTestsAssert.True(validation.IsSuccess);}+ [Fact]+ public void Union_ChainedOnRecognizedOrthogonalAdditiveRoot_RemainsSupported()+ {+ var basePlate = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(-20d, 20d, -5d, 5d, -3d, 3d)).Value;+ var upright = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(12d, 20d, -5d, 5d, 3d, 27d)).Value;+ var flange = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(-20d, -12d, -5d, 5d, 3d, 15d)).Value;++ var first = BrepBoolean.Union(basePlate, upright);+ Assert.True(first.IsSuccess);++ var second = BrepBoolean.Union(first.Value, flange);+ Assert.True(second.IsSuccess);+ Assert.True(BrepBindingValidator.Validate(second.Value, requireAllEdgeAndFaceBindings: true).IsSuccess);+ }+[Fact]public void Union_DisjointBoxes_ReturnsNotImplemented(){diff --git a/Aetheris.Kernel.Core/Brep/Boolean/BrepBoolean.cs b/Aetheris.Kernel.Core/Brep/Boolean/BrepBoolean.csindex 9f9ef412..eedcba2e 100644--- a/Aetheris.Kernel.Core/Brep/Boolean/BrepBoolean.cs+++ b/Aetheris.Kernel.Core/Brep/Boolean/BrepBoolean.cs@@ -2,6 +2,7 @@ using Aetheris.Kernel.Core.Diagnostics;using Aetheris.Kernel.Core.Math;using Aetheris.Kernel.Core.Numerics;using Aetheris.Kernel.Core.Results;+using Aetheris.Kernel.Core.Topology;namespace Aetheris.Kernel.Core.Brep.Boolean;@@ -168,6 +169,22 @@ public static class BrepBooleanvar rightRecognized = BrepBooleanBoxRecognition.TryRecognizeAxisAlignedBox(rightBody, resolvedTolerance, out var rightBox, out _);var rightAnalyticRecognized = BrepBooleanAnalyticSurfaceRecognition.TryRecognizeAnalyticSurface(rightBody, resolvedTolerance, out var analyticSurface, out _);+ if (operation == BooleanOperation.Union+ && !leftRecognized+ && leftSafeCompositionRecognized+ && rightRecognized+ && leftSafeComposition is not null+ && TryClassifyBoundedOrthogonalUnionWithExistingRoot(leftSafeComposition, rightBox, resolvedTolerance, out var additiveComposition, out var additiveUnsupportedReason))+ {+ return new BooleanCaseClassification(+ BooleanExecutionClass.PlanarOnly,+ additiveComposition,+ additiveComposition.OuterBox,+ rightBox,+ null,+ null);+ }+if (leftRecognized && rightRecognized){return new BooleanCaseClassification(BooleanExecutionClass.PlanarOnly, leftSafeComposition, leftBox, rightBox, null, null);@@ -337,12 +354,17 @@ public static class BrepBooleanif (TryClassifyBoundedOrthogonalUnion(left, right, tolerance, out var unionCells, out var unsupportedReason)){+ var unionBounds = AxisAlignedBoxExtents.Bounding(left, right);return new BooleanClassificationData(intersections,IsComputed: true,FragmentCount: 1,SingleBoxResult: null,- SafeCompositionResult: null,+ SafeCompositionResult: new SafeBooleanComposition(+ unionBounds,+ [],+ SafeBooleanRootDescriptor.FromBox(unionBounds),+ unionCells),UnsupportedReason: null,OrthogonalUnionCells: unionCells);}@@ -383,6 +405,20 @@ public static class BrepBoolean]);}+ if (classification.OrthogonalUnionCells is not null)+ {+ var rebuiltOrthogonalUnion = BrepBooleanOrthogonalUnionBuilder.BuildFromCells(classification.OrthogonalUnionCells);+ if (!rebuiltOrthogonalUnion.IsSuccess)+ {+ return new BooleanRebuildData(classification, RebuiltBody: null, Diagnostics: rebuiltOrthogonalUnion.Diagnostics);+ }++ var rebuiltBody = classification.SafeCompositionResult is null+ ? rebuiltOrthogonalUnion.Value+ : CopyWithSafeComposition(rebuiltOrthogonalUnion.Value, classification.SafeCompositionResult);+ return new BooleanRebuildData(classification, rebuiltBody, rebuiltOrthogonalUnion.Diagnostics);+ }+if (classification.SafeCompositionResult is not null){var tolerance = request.Tolerance ?? ToleranceContext.Default;@@ -392,14 +428,6 @@ public static class BrepBoolean: new BooleanRebuildData(classification, RebuiltBody: null, Diagnostics: rebuiltThroughHole.Diagnostics);}- if (classification.OrthogonalUnionCells is not null)- {- var rebuiltOrthogonalUnion = BrepBooleanOrthogonalUnionBuilder.BuildFromCells(classification.OrthogonalUnionCells);- return rebuiltOrthogonalUnion.IsSuccess- ? new BooleanRebuildData(classification, rebuiltOrthogonalUnion.Value, rebuiltOrthogonalUnion.Diagnostics)- : new BooleanRebuildData(classification, RebuiltBody: null, Diagnostics: rebuiltOrthogonalUnion.Diagnostics);- }-if (classification.UnsupportedReason is not null){return new BooleanRebuildData(@@ -571,6 +599,125 @@ public static class BrepBooleanreturn true;}+ private static bool TryClassifyBoundedOrthogonalUnionWithExistingRoot(+ SafeBooleanComposition leftComposition,+ AxisAlignedBoxExtents right,+ ToleranceContext tolerance,+ out SafeBooleanComposition additiveComposition,+ out string? unsupportedReason)+ {+ additiveComposition = leftComposition;+ unsupportedReason = null;++ var leftCells = leftComposition.OccupiedCells;+ if (leftCells is null || leftCells.Count == 0)+ {+ unsupportedReason = "Boolean Union: additive root does not carry bounded orthogonal occupancy cells for chained union recognition.";+ return false;+ }++ var combined = new List<AxisAlignedBoxExtents>(leftCells.Count + 1);+ combined.AddRange(leftCells);+ combined.Add(right);++ var distinctX = combined.SelectMany(cell => new[] { cell.MinX, cell.MaxX }).Distinct().OrderBy(v => v).ToArray();+ var distinctY = combined.SelectMany(cell => new[] { cell.MinY, cell.MaxY }).Distinct().OrderBy(v => v).ToArray();+ var distinctZ = combined.SelectMany(cell => new[] { cell.MinZ, cell.MaxZ }).Distinct().OrderBy(v => v).ToArray();+ var refinedCells = new List<AxisAlignedBoxExtents>();++ for (var ix = 0; ix < distinctX.Length - 1; ix++)+ {+ for (var iy = 0; iy < distinctY.Length - 1; iy++)+ {+ for (var iz = 0; iz < distinctZ.Length - 1; iz++)+ {+ var candidate = new AxisAlignedBoxExtents(+ distinctX[ix],+ distinctX[ix + 1],+ distinctY[iy],+ distinctY[iy + 1],+ distinctZ[iz],+ distinctZ[iz + 1]);+ if (!candidate.HasPositiveVolume(tolerance))+ {+ continue;+ }++ if (combined.Any(cell => cell.Contains(candidate, tolerance)))+ {+ refinedCells.Add(candidate);+ }+ }+ }+ }++ if (refinedCells.Count == 0)+ {+ unsupportedReason = "Boolean Union: additive-root refinement produced no occupied orthogonal cells.";+ return false;+ }++ var bounds = AxisAlignedBoxExtents.Bounding(refinedCells[0], refinedCells[0]);+ for (var i = 1; i < refinedCells.Count; i++)+ {+ bounds = AxisAlignedBoxExtents.Bounding(bounds, refinedCells[i]);+ }++ if (!TryValidateOrthogonalCellConnectivity(refinedCells, tolerance))+ {+ unsupportedReason = "Boolean Union: additive-root chained union is disjoint or only edge-connected after bounded orthogonal refinement.";+ return false;+ }++ additiveComposition = new SafeBooleanComposition(+ bounds,+ leftComposition.Holes,+ SafeBooleanRootDescriptor.FromBox(bounds),+ refinedCells);+ return true;+ }++ private static bool TryValidateOrthogonalCellConnectivity(IReadOnlyList<AxisAlignedBoxExtents> cells, ToleranceContext tolerance)+ {+ if (cells.Count == 0)+ {+ return false;+ }++ var visited = new HashSet<int>();+ var queue = new Queue<int>();+ visited.Add(0);+ queue.Enqueue(0);++ while (queue.Count > 0)+ {+ var current = queue.Dequeue();+ for (var i = 0; i < cells.Count; i++)+ {+ if (visited.Contains(i) || !CellsShareVolumeOrFace(cells[current], cells[i], tolerance))+ {+ continue;+ }++ visited.Add(i);+ queue.Enqueue(i);+ }+ }++ return visited.Count == cells.Count;+ }++ private static bool CellsShareVolumeOrFace(AxisAlignedBoxExtents a, AxisAlignedBoxExtents b, ToleranceContext tolerance)+ {+ var overlap = AxisAlignedBoxExtents.Intersection(a, b);+ if (overlap is not null && overlap.Value.HasPositiveVolume(tolerance))+ {+ return true;+ }++ return IsPositiveAreaFaceContact(a, b, tolerance);+ }+private static bool IsPositiveAreaFaceContact(AxisAlignedBoxExtents left, AxisAlignedBoxExtents right, ToleranceContext tolerance){var xFaceTouch = (ToleranceMath.AlmostEqual(left.MaxX, right.MinX, tolerance) || ToleranceMath.AlmostEqual(right.MaxX, left.MinX, tolerance))@@ -611,4 +758,21 @@ public static class BrepBooleanprivate static KernelDiagnostic CreateInternalError(string message, string source)=> new(KernelDiagnosticCode.InternalError, KernelDiagnosticSeverity.Error, message, source);++ private static BrepBody CopyWithSafeComposition(BrepBody source, SafeBooleanComposition composition)+ => new(source.Topology, source.Geometry, source.Bindings, GetVertexPoints(source), composition, source.ShellRepresentation);++ private static IReadOnlyDictionary<VertexId, Point3D> GetVertexPoints(BrepBody source)+ {+ var points = new Dictionary<VertexId, Point3D>();+ foreach (var vertex in source.Topology.Vertices)+ {+ if (source.TryGetVertexPoint(vertex.Id, out var point))+ {+ points[vertex.Id] = point;+ }+ }++ return points;+ }}diff --git a/Aetheris.Kernel.Core/Brep/Boolean/BrepBooleanSafeComposition.cs b/Aetheris.Kernel.Core/Brep/Boolean/BrepBooleanSafeComposition.csindex abdec195..f9a3fe83 100644--- a/Aetheris.Kernel.Core/Brep/Boolean/BrepBooleanSafeComposition.cs+++ b/Aetheris.Kernel.Core/Brep/Boolean/BrepBooleanSafeComposition.cs@@ -7,7 +7,8 @@ namespace Aetheris.Kernel.Core.Brep.Boolean;public sealed record SafeBooleanComposition(AxisAlignedBoxExtents OuterBox,IReadOnlyList<SupportedBooleanHole> Holes,- SafeBooleanRootDescriptor? Root = null)+ SafeBooleanRootDescriptor? Root = null,+ IReadOnlyList<AxisAlignedBoxExtents>? OccupiedCells = null){public SafeBooleanRootDescriptor RootDescriptor => Root ?? SafeBooleanRootDescriptor.FromBox(OuterBox);@@ -24,7 +25,14 @@ public sealed record SafeBooleanComposition(return new SafeBooleanComposition(translatedOuter,Holes.Select(hole => hole.Translate(translation)).ToArray(),- RootDescriptor.Translate(translation));+ RootDescriptor.Translate(translation),+ OccupiedCells?.Select(cell => new AxisAlignedBoxExtents(+ cell.MinX + translation.X,+ cell.MaxX + translation.X,+ cell.MinY + translation.Y,+ cell.MaxY + translation.Y,+ cell.MinZ + translation.Z,+ cell.MaxZ + translation.Z)).ToArray());}}diff --git a/Aetheris.Kernel.Core/Brep/Boolean/BrepBooleanSafeCompositionGraphValidator.cs b/Aetheris.Kernel.Core/Brep/Boolean/BrepBooleanSafeCompositionGraphValidator.csindex 777b89fb..8f377e33 100644--- a/Aetheris.Kernel.Core/Brep/Boolean/BrepBooleanSafeCompositionGraphValidator.cs+++ b/Aetheris.Kernel.Core/Brep/Boolean/BrepBooleanSafeCompositionGraphValidator.cs@@ -30,7 +30,7 @@ public static class BrepBooleanSafeCompositionGraphValidatornextFeatureId);}- if (!TryCreateSupportedHole(composition.OuterBox, surface, tolerance, composition.Holes.Count > 0, out var nextHole, out diagnostic, nextFeatureId))+ if (!TryCreateSupportedHole(composition, surface, tolerance, composition.Holes.Count > 0, out var nextHole, out diagnostic, nextFeatureId)){return false;}@@ -249,7 +249,7 @@ public static class BrepBooleanSafeCompositionGraphValidator}private static bool TryCreateSupportedHole(- AxisAlignedBoxExtents outerBox,+ SafeBooleanComposition composition,AnalyticSurface surface,ToleranceContext tolerance,bool hasExistingHoles,@@ -257,6 +257,7 @@ public static class BrepBooleanSafeCompositionGraphValidatorout BooleanDiagnostic? diagnostic,string? featureId){+ var outerBox = composition.OuterBox;switch (surface.Kind){case AnalyticSurfaceKind.Cylinder when surface.Cylinder is RecognizedCylinder cylinder:@@ -354,6 +355,7 @@ public static class BrepBooleanSafeCompositionGraphValidator}}+private static bool ShouldTryContainedCylinderSegment(AxisAlignedBoxExtents outerBox, in RecognizedCylinder cylinder, ToleranceContext tolerance){if (!BrepBooleanCylinderRecognition.ValidateAxisAlignedZ(cylinder, tolerance))diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentBuildAndExportTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentBuildAndExportTests.csindex f7ef2367..870a42cd 100644--- a/Aetheris.Kernel.Firmament.Tests/FirmamentBuildAndExportTests.cs+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentBuildAndExportTests.cs@@ -365,6 +365,34 @@ public sealed class FirmamentBuildAndExportTests"subtract");}+ [Fact]+ public void Run_FrictionLabMountingBracketBasic_ProgressesPastUnrecognizedAdditiveRoot_ToBlindHoleCompositionBlocker()+ {+ var fullSourcePath = Path.Combine(FirmamentCorpusHarness.RepoRoot(), "Aetheris.Firmament.FrictionLab/Cases/mounting-bracket-basic/part.firmament");+ var result = FirmamentBuildAndExport.Run(fullSourcePath);++ Assert.False(result.IsSuccess);+ Assert.Contains(result.Diagnostics, diagnostic =>+ diagnostic.Message.Contains("Requested boolean feature 'upright_hole' (subtract) could not be executed.", StringComparison.Ordinal));+ Assert.Contains(result.Diagnostics, diagnostic =>+ diagnostic.Message.Contains("cannot append a blind analytic hole to an existing safe subtract composition", StringComparison.Ordinal));+ Assert.DoesNotContain(result.Diagnostics, diagnostic =>+ string.Equals(diagnostic.Source, "firmament.feature-graph.unrecognized-additive-root", StringComparison.Ordinal));+ }++ [Fact]+ public void Run_FrictionLabSimpleBracket_NowBuildsAndExports()+ {+ var fullSourcePath = Path.Combine(FirmamentCorpusHarness.RepoRoot(), "Aetheris.Firmament.FrictionLab/Cases/simple-bracket/part.firmament");+ var result = FirmamentBuildAndExport.Run(fullSourcePath);++ Assert.True(result.IsSuccess, string.Join(Environment.NewLine, result.Diagnostics.Select(d => d.Message)));+ Assert.Equal("bracket", result.Value.Export.ExportedFeatureId);+ Assert.Equal("boolean", result.Value.Export.ExportedBodyCategory);+ Assert.Equal("add", result.Value.Export.ExportedFeatureKind);+ Assert.Contains("MANIFOLD_SOLID_BREP", result.Value.Export.StepText, StringComparison.Ordinal);+ }+[Theory][InlineData("testdata/firmament/fixtures/m10j-unsupported-box-add-cylinder.firmament", "m10j-unsupported-box-add-cylinder.step", "joined", "add")][InlineData("testdata/firmament/fixtures/m10j-unsupported-box-intersect-cylinder.firmament", "m10j-unsupported-box-intersect-cylinder.step", "overlap", "intersect")]@@ -388,6 +416,20 @@ public sealed class FirmamentBuildAndExportTestsAssertUnsupportedBuildAndExport(sourcePath, expectedFileName, expectedFeatureId, expectedKind);}+ [Fact]+ public void Run_FrictionLabShaftKeyway_RemainsExplicitlyUnsupported()+ {+ var fullSourcePath = Path.Combine(FirmamentCorpusHarness.RepoRoot(), "Aetheris.Firmament.FrictionLab/Cases/shaft-keyway/part.firmament");+ var result = FirmamentBuildAndExport.Run(fullSourcePath);++ Assert.False(result.IsSuccess);+ Assert.Contains(result.Diagnostics, diagnostic =>+ diagnostic.Message.Contains("Requested boolean feature 'keyway' (subtract) could not be executed.", StringComparison.Ordinal));+ Assert.Contains(result.Diagnostics, diagnostic =>+ diagnostic.Source == "BrepBoolean.RebuildResult"+ && diagnostic.Message.Contains("sequential safe composition only supports subtracting supported analytic holes", StringComparison.Ordinal));+ }+private static void AssertExampleBuildAndExport(string sourcePath, string expectedFileName, string expectedFeatureId, int expectedOpIndex, string expectedBodyCategory, string expectedFeatureKind){var fullSourcePath = FirmamentCorpusHarness.ResolveFixtureFullPath(sourcePath);