Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
Add bounded mixed through-void reconstruction for box roots
Yuechen Li2deb39a8bedf
diff --git a/Aetheris.Kernel.Core.Tests/Brep/Boolean/BrepBooleanTests.cs b/Aetheris.Kernel.Core.Tests/Brep/Boolean/BrepBooleanTests.csindex f9b2fda7..d354b5a6 100644--- a/Aetheris.Kernel.Core.Tests/Brep/Boolean/BrepBooleanTests.cs+++ b/Aetheris.Kernel.Core.Tests/Brep/Boolean/BrepBooleanTests.cs@@ -1665,7 +1665,7 @@ public sealed class BrepBooleanTests}[Fact]- public void Unsupported_MixedSubtract_Reveals_RebuildBoundary()+ public void MixedThroughVoid_AnalyticPlusSlot_Succeeds(){var root = BrepPrimitives.CreateBox(20d, 20d, 20d).Value;var holeTool = BrepPrimitives.CreateCylinder(3d, 24d).Value;@@ -1675,9 +1675,26 @@ public sealed class BrepBooleanTestsvar slotTool = TransformBody(StandardLibraryPrimitives.CreateSlotCut(16d, 8d, 10d, 4d).Value, Transform3D.CreateTranslation(new Vector3D(0d, 0d, -5d)));var result = BrepBoolean.Subtract(withHole.Value, slotTool);+ Assert.True(result.IsSuccess);+ Assert.NotNull(result.Value.SafeBooleanComposition?.ThroughVoids);+ Assert.Single(result.Value.SafeBooleanComposition!.ThroughVoids!.PrismaticVoids);+ Assert.Empty(result.Value.SafeBooleanComposition.ThroughVoids.AnalyticVoids);+ }++ [Fact]+ public void MixedThroughVoid_UnsupportedInteraction_Rejects()+ {+ var root = BrepPrimitives.CreateBox(20d, 20d, 20d).Value;+ var holeTool = TransformBody(BrepPrimitives.CreateCylinder(3d, 24d).Value, Transform3D.CreateTranslation(new Vector3D(6d, 0d, 0d)));+ var withHole = BrepBoolean.Subtract(root, holeTool);+ Assert.True(withHole.IsSuccess);++ var slotTool = TransformBody(StandardLibraryPrimitives.CreateSlotCut(16d, 8d, 10d, 4d).Value, Transform3D.CreateTranslation(new Vector3D(0d, 0d, -5d)));+ var result = BrepBoolean.Subtract(withHole.Value, slotTool);+Assert.False(result.IsSuccess);var diagnostic = Assert.Single(result.Diagnostics);- Assert.Contains("bounded mixed analytic+prismatic subtract continuation is recognized but bounded reconstruction for this mixed family is not implemented yet", diagnostic.Message, StringComparison.Ordinal);+ Assert.Contains("bounded mixed through-void builder", diagnostic.Message, StringComparison.Ordinal);}[Fact]diff --git a/Aetheris.Kernel.Core/Brep/Boolean/BrepBoolean.cs b/Aetheris.Kernel.Core/Brep/Boolean/BrepBoolean.csindex 9e73cea9..f4c6c604 100644--- a/Aetheris.Kernel.Core/Brep/Boolean/BrepBoolean.cs+++ b/Aetheris.Kernel.Core/Brep/Boolean/BrepBoolean.cs@@ -760,12 +760,16 @@ public static class BrepBoolean&& intersections.Analysis.RightPrismaticTool is { } prismaticTool&& TryClassifyBoundedPrismaticThroughCutSubtract(leftCompositionForPrismatic, prismaticTool, tolerance, out _)){+ var throughVoids = BuildThroughVoidSetForPrismaticContinuation(leftCompositionForPrismatic, prismaticTool);return new BooleanClassificationData(intersections,IsComputed: true,FragmentCount: 1,SingleBoxResult: null,- SafeCompositionResult: null,+ SafeCompositionResult: leftCompositionForPrismatic with+ {+ ThroughVoids = throughVoids,+ },UnsupportedReason: null,PrismaticThroughCutTool: prismaticTool);}@@ -990,10 +994,24 @@ public static class BrepBooleanif (classification.PrismaticThroughCutTool is { } prismaticTool&& classification.Intersections.Analysis.LeftSafeComposition is { } leftComposition){+ if (leftComposition.Holes.Count == 1)+ {+ var rebuiltMixed = BrepBooleanBoxMixedThroughVoidBuilder.Build(leftComposition, prismaticTool, request.Tolerance ?? ToleranceContext.Default);+ return rebuiltMixed.IsSuccess+ ? new BooleanRebuildData(classification, rebuiltMixed.Value, rebuiltMixed.Diagnostics)+ : new BooleanRebuildData(classification, RebuiltBody: null, Diagnostics: rebuiltMixed.Diagnostics);+ }+var rebuiltPrismSubtract = BrepBooleanBoxPrismThroughCutBuilder.Build(leftComposition.RootDescriptor.Box, prismaticTool.Footprint, request.Tolerance ?? ToleranceContext.Default);- return rebuiltPrismSubtract.IsSuccess- ? new BooleanRebuildData(classification, rebuiltPrismSubtract.Value, rebuiltPrismSubtract.Diagnostics)- : new BooleanRebuildData(classification, RebuiltBody: null, Diagnostics: rebuiltPrismSubtract.Diagnostics);+ if (!rebuiltPrismSubtract.IsSuccess)+ {+ return new BooleanRebuildData(classification, RebuiltBody: null, Diagnostics: rebuiltPrismSubtract.Diagnostics);+ }++ var rebuiltBody = classification.SafeCompositionResult is null+ ? rebuiltPrismSubtract.Value+ : CopyWithSafeComposition(rebuiltPrismSubtract.Value, classification.SafeCompositionResult);+ return new BooleanRebuildData(classification, rebuiltBody, rebuiltPrismSubtract.Diagnostics);}if (classification.SafeCompositionResult is not null)@@ -1769,20 +1787,19 @@ public static class BrepBooleanreturn false;}- if (leftComposition.Holes.Count > 0 || (leftComposition.OpenSlots?.Count ?? 0) > 0)+ var hasPriorSubtractHistory = leftComposition.Holes.Count > 0 || (leftComposition.OpenSlots?.Count ?? 0) > 0;+ if (hasPriorSubtractHistory){if (!SupportsBoundedAnalyticHistoryForPrismaticContinuation(leftComposition, tolerance, out var historyReason)){unsupportedReason = historyReason;return false;}-- unsupportedReason = "Boolean Subtract: bounded mixed analytic+prismatic subtract continuation is recognized but bounded reconstruction for this mixed family is not implemented yet.";- return false;}var root = leftComposition.RootDescriptor.Box;- if (prismTool.Bounds.MinZ > root.MinZ + tolerance.Linear || prismTool.Bounds.MaxZ < root.MaxZ - tolerance.Linear)+ if (!hasPriorSubtractHistory+ && (prismTool.Bounds.MinZ > root.MinZ + tolerance.Linear || prismTool.Bounds.MaxZ < root.MaxZ - tolerance.Linear)){unsupportedReason = "Boolean Subtract: bounded prismatic subtract family requires a through-cut tool that spans the full box root height.";return false;@@ -1802,6 +1819,21 @@ public static class BrepBooleanreturn true;}+ private static SupportedThroughVoidSet BuildThroughVoidSetForPrismaticContinuation(+ SafeBooleanComposition composition,+ SupportedPrismaticSubtractTool prismaticTool)+ {+ var analyticVoids = composition.Holes+ .Where(hole => hole.SpanKind == SupportedBooleanHoleSpanKind.Through+ && hole.Surface.Kind is (AnalyticSurfaceKind.Cylinder or AnalyticSurfaceKind.Cone))+ .ToArray();+ var prismaticVoids = new[]+ {+ new SupportedPrismaticThroughVoid(prismaticTool.Bounds, prismaticTool.Footprint),+ };+ return new SupportedThroughVoidSet(analyticVoids, prismaticVoids);+ }+private static bool SupportsBoundedAnalyticHistoryForPrismaticContinuation(SafeBooleanComposition composition,ToleranceContext tolerance,diff --git a/Aetheris.Kernel.Core/Brep/Boolean/BrepBooleanBoxMixedThroughVoidBuilder.cs b/Aetheris.Kernel.Core/Brep/Boolean/BrepBooleanBoxMixedThroughVoidBuilder.csnew file mode 100644index 00000000..db49c946--- /dev/null+++ b/Aetheris.Kernel.Core/Brep/Boolean/BrepBooleanBoxMixedThroughVoidBuilder.cs@@ -0,0 +1,196 @@+using Aetheris.Kernel.Core.Diagnostics;+using Aetheris.Kernel.Core.Geometry;+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;++public static class BrepBooleanBoxMixedThroughVoidBuilder+{+ public static KernelResult<BrepBody> Build(+ SafeBooleanComposition composition,+ SupportedPrismaticSubtractTool prismaticTool,+ ToleranceContext tolerance)+ {+ if (composition.RootDescriptor.Kind != SafeBooleanRootKind.Box)+ {+ return Failure("Boolean Subtract: bounded mixed through-void builder requires a recognized safe box root.");+ }++ if (composition.OpenSlots is { Count: > 0 })+ {+ return Failure("Boolean Subtract: bounded mixed through-void builder does not support prior open-slot history.");+ }++ if (composition.Holes.Count != 1)+ {+ return Failure("Boolean Subtract: bounded mixed through-void builder currently supports exactly one analytic through-void history entry.");+ }++ var hole = composition.Holes[0];+ if (hole.SpanKind != SupportedBooleanHoleSpanKind.Through)+ {+ return Failure("Boolean Subtract: bounded mixed through-void builder currently supports only through analytic history.");+ }++ if (hole.Surface.Kind is not (AnalyticSurfaceKind.Cylinder or AnalyticSurfaceKind.Cone))+ {+ return Failure("Boolean Subtract: bounded mixed through-void builder currently supports cylinder/cone analytic through-void history.");+ }++ var axis = hole.Axis.ToVector();+ if (!ToleranceMath.AlmostZero(axis.X, tolerance)+ || !ToleranceMath.AlmostZero(axis.Y, tolerance)+ || !ToleranceMath.AlmostEqual(System.Math.Abs(axis.Z), 1d, tolerance))+ {+ return Failure("Boolean Subtract: bounded mixed through-void builder requires world-Z aligned analytic through-void history.");+ }++ if (!TryClassifyContainedAnalyticInsidePrismaticFootprint(hole, prismaticTool.Footprint, tolerance, out var classReason))+ {+ return Failure(classReason);+ }++ var rebuilt = BrepBooleanBoxPrismThroughCutBuilder.Build(composition.RootDescriptor.Box, prismaticTool.Footprint, tolerance);+ if (!rebuilt.IsSuccess)+ {+ return rebuilt;+ }++ var resultingComposition = composition with+ {+ Holes = [],+ ThroughVoids = new SupportedThroughVoidSet(+ AnalyticVoids: [],+ PrismaticVoids: [new SupportedPrismaticThroughVoid(prismaticTool.Bounds, prismaticTool.Footprint)])+ };+ var rebuiltBody = new BrepBody(+ rebuilt.Value.Topology,+ rebuilt.Value.Geometry,+ rebuilt.Value.Bindings,+ GetVertexPoints(rebuilt.Value),+ resultingComposition,+ rebuilt.Value.ShellRepresentation);+ return KernelResult<BrepBody>.Success(rebuiltBody, rebuilt.Diagnostics);+ }++ private static bool TryClassifyContainedAnalyticInsidePrismaticFootprint(+ in SupportedBooleanHole hole,+ IReadOnlyList<(double X, double Y)> footprint,+ ToleranceContext tolerance,+ out string unsupportedReason)+ {+ unsupportedReason = "Boolean Subtract: bounded mixed through-void builder requires a valid prismatic footprint.";+ if (footprint.Count < 3)+ {+ return false;+ }++ var radius = hole.MaxBoundaryRadius;+ var center = (X: hole.CenterX, Y: hole.CenterY);+ if (!IsPointInsidePolygon(center, footprint, tolerance))+ {+ unsupportedReason = "Boolean Subtract: bounded mixed through-void builder currently supports only the containment interaction class where the analytic through-void lies inside the incoming prismatic footprint.";+ return false;+ }++ var minEdgeDistance = ComputeMinimumDistanceToPolygonEdges(center, footprint);+ if (minEdgeDistance <= radius + (2d * tolerance.Linear))+ {+ unsupportedReason = "Boolean Subtract: bounded mixed through-void builder rejects tangent/edge-grazing analytic-prismatic interactions; containment requires strict positive radial margin.";+ return false;+ }++ unsupportedReason = string.Empty;+ return true;+ }++ private static bool IsPointInsidePolygon(+ (double X, double Y) point,+ IReadOnlyList<(double X, double Y)> polygon,+ ToleranceContext tolerance)+ {+ var winding = false;+ for (var i = 0; i < polygon.Count; i++)+ {+ var a = polygon[i];+ var b = polygon[(i + 1) % polygon.Count];+ if (DistancePointToSegment(point, a, b) <= tolerance.Linear)+ {+ return true;+ }++ var intersects = ((a.Y > point.Y) != (b.Y > point.Y))+ && (point.X < (((b.X - a.X) * (point.Y - a.Y)) / (b.Y - a.Y + double.Epsilon)) + a.X);+ if (intersects)+ {+ winding = !winding;+ }+ }++ return winding;+ }++ private static double ComputeMinimumDistanceToPolygonEdges((double X, double Y) point, IReadOnlyList<(double X, double Y)> polygon)+ {+ var minDistance = double.PositiveInfinity;+ for (var i = 0; i < polygon.Count; i++)+ {+ var a = polygon[i];+ var b = polygon[(i + 1) % polygon.Count];+ var distance = DistancePointToSegment(point, a, b);+ if (distance < minDistance)+ {+ minDistance = distance;+ }+ }++ return minDistance;+ }++ private static double DistancePointToSegment((double X, double Y) point, (double X, double Y) a, (double X, double Y) b)+ {+ var abX = b.X - a.X;+ var abY = b.Y - a.Y;+ var abLengthSquared = (abX * abX) + (abY * abY);+ if (abLengthSquared <= double.Epsilon)+ {+ var dx = point.X - a.X;+ var dy = point.Y - a.Y;+ return System.Math.Sqrt((dx * dx) + (dy * dy));+ }++ var t = (((point.X - a.X) * abX) + ((point.Y - a.Y) * abY)) / abLengthSquared;+ t = System.Math.Max(0d, System.Math.Min(1d, t));+ var closestX = a.X + (t * abX);+ var closestY = a.Y + (t * abY);+ var ddx = point.X - closestX;+ var ddy = point.Y - closestY;+ return System.Math.Sqrt((ddx * ddx) + (ddy * ddy));+ }++ 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;+ }++ private static KernelResult<BrepBody> Failure(string message)+ => KernelResult<BrepBody>.Failure([+ new KernelDiagnostic(+ KernelDiagnosticCode.NotImplemented,+ KernelDiagnosticSeverity.Error,+ message,+ "BrepBooleanBoxMixedThroughVoidBuilder.Build"),+ ]);+}diff --git a/Aetheris.Kernel.Core/Brep/Boolean/BrepBooleanSafeComposition.cs b/Aetheris.Kernel.Core/Brep/Boolean/BrepBooleanSafeComposition.csindex 5daf3521..dc80c695 100644--- a/Aetheris.Kernel.Core/Brep/Boolean/BrepBooleanSafeComposition.cs+++ b/Aetheris.Kernel.Core/Brep/Boolean/BrepBooleanSafeComposition.cs@@ -9,7 +9,8 @@ public sealed record SafeBooleanComposition(IReadOnlyList<SupportedBooleanHole> Holes,SafeBooleanRootDescriptor? Root = null,IReadOnlyList<AxisAlignedBoxExtents>? OccupiedCells = null,- IReadOnlyList<SupportedCylinderOpenSlot>? OpenSlots = null)+ IReadOnlyList<SupportedCylinderOpenSlot>? OpenSlots = null,+ SupportedThroughVoidSet? ThroughVoids = null){public SafeBooleanRootDescriptor RootDescriptor => Root ?? SafeBooleanRootDescriptor.FromBox(OuterBox);@@ -34,10 +35,37 @@ public sealed record SafeBooleanComposition(cell.MaxY + translation.Y,cell.MinZ + translation.Z,cell.MaxZ + translation.Z)).ToArray(),- OpenSlots?.Select(slot => slot.Translate(translation)).ToArray());+ OpenSlots?.Select(slot => slot.Translate(translation)).ToArray(),+ ThroughVoids?.Translate(translation));}}+public sealed record SupportedThroughVoidSet(+ IReadOnlyList<SupportedBooleanHole> AnalyticVoids,+ IReadOnlyList<SupportedPrismaticThroughVoid> PrismaticVoids)+{+ public SupportedThroughVoidSet Translate(Vector3D translation)+ => new(+ AnalyticVoids.Select(hole => hole.Translate(translation)).ToArray(),+ PrismaticVoids.Select(voidFeature => voidFeature.Translate(translation)).ToArray());+}++public readonly record struct SupportedPrismaticThroughVoid(+ AxisAlignedBoxExtents Bounds,+ IReadOnlyList<(double X, double Y)> Footprint)+{+ public SupportedPrismaticThroughVoid Translate(Vector3D translation)+ => new(+ new AxisAlignedBoxExtents(+ Bounds.MinX + translation.X,+ Bounds.MaxX + translation.X,+ Bounds.MinY + translation.Y,+ Bounds.MaxY + translation.Y,+ Bounds.MinZ + translation.Z,+ Bounds.MaxZ + translation.Z),+ Footprint.Select(point => (point.X + translation.X, point.Y + translation.Y)).ToArray());+}+public enum SafeBooleanRootKind{Box,diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentConnectorLibraryPartTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentConnectorLibraryPartTests.csindex 1cc84979..08732e8f 100644--- a/Aetheris.Kernel.Firmament.Tests/FirmamentConnectorLibraryPartTests.cs+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentConnectorLibraryPartTests.cs@@ -46,32 +46,28 @@ public sealed class FirmamentConnectorLibraryPartTests}[Fact]- public void ConnectorPart_With_SlotCut_Composition_Reveals_Narrower_MixedRebuild_Blocker()+ public void ConnectorPart_With_SlotCut_Composition_Succeeds(){var source = FirmamentCorpusHarness.ReadFixtureText("testdata/firmament/examples/composed_part_with_slot.firmament");var compile = FirmamentCorpusHarness.Compile(source);- Assert.False(compile.Compilation.IsSuccess);- Assert.Contains(- compile.Compilation.Diagnostics,- diagnostic => diagnostic.Message.Contains("Requested boolean feature 'slot_carve' (subtract) could not be executed.", StringComparison.Ordinal));- Assert.Contains(+ Assert.True(compile.Compilation.IsSuccess);+ Assert.DoesNotContain(compile.Compilation.Diagnostics,- diagnostic => diagnostic.Source == "BrepBoolean.RebuildResult"- && diagnostic.Message.Contains("bounded mixed analytic+prismatic subtract continuation is recognized but bounded reconstruction for this mixed family is not implemented yet", StringComparison.Ordinal));+ diagnostic => diagnostic.Message.Contains("slot_carve", StringComparison.Ordinal));}[Fact]- public void Firmament_Composed_Model_Export_Reveals_Same_Blocker()+ public void Firmament_Composed_Model_Export_Succeeds(){var source = FirmamentCorpusHarness.ReadFixtureText("testdata/firmament/examples/composed_part_with_slot.firmament");var compile = FirmamentCorpusHarness.Compile(source);- Assert.False(compile.Compilation.IsSuccess);+ Assert.True(compile.Compilation.IsSuccess);var export = FirmamentStepExporter.Export(new FirmamentCompileRequest(new FirmamentSourceDocument(source)));- Assert.False(export.IsSuccess);+ Assert.True(export.IsSuccess);}[Fact]