Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
Implement analytic-hole boolean diagnostics
Yuechen Li4ed5caa2e8a9
diff --git a/Aetheris.Kernel.Core.Tests/Brep/Boolean/BrepBooleanTests.cs b/Aetheris.Kernel.Core.Tests/Brep/Boolean/BrepBooleanTests.csindex 22419599..8be13653 100644--- a/Aetheris.Kernel.Core.Tests/Brep/Boolean/BrepBooleanTests.cs+++ b/Aetheris.Kernel.Core.Tests/Brep/Boolean/BrepBooleanTests.cs@@ -269,9 +269,8 @@ public sealed class BrepBooleanTestsAssert.False(result.IsSuccess);var diagnostic = Assert.Single(result.Diagnostics);Assert.Equal(KernelDiagnosticCode.NotImplemented, diagnostic.Code);- Assert.Equal("BrepBoolean.AnalyticHoleConstraintViolation", diagnostic.Source);- Assert.Contains("analytic hole candidate violates M13 constraints", diagnostic.Message, StringComparison.Ordinal);- Assert.Contains("cylinder axis is not aligned with the box Z axis", diagnostic.Message, StringComparison.Ordinal);+ Assert.Equal("BrepBoolean.AnalyticHole.AxisNotAligned", diagnostic.Source);+ Assert.Equal("Boolean Subtract: analytic hole candidate failed diagnostic AxisNotAligned (cylinder axis is not aligned with the box Z axis.).", diagnostic.Message);}[Fact]@@ -285,25 +284,38 @@ public sealed class BrepBooleanTestsAssert.False(result.IsSuccess);var diagnostic = Assert.Single(result.Diagnostics);Assert.Equal(KernelDiagnosticCode.NotImplemented, diagnostic.Code);- Assert.Equal("BrepBoolean.AnalyticHoleConstraintViolation", diagnostic.Source);- Assert.Contains("analytic hole candidate violates M13 constraints", diagnostic.Message, StringComparison.Ordinal);- Assert.Contains("does not fully span the box Z range", diagnostic.Message, StringComparison.Ordinal);+ Assert.Equal("BrepBoolean.AnalyticHole.NotFullySpanning", diagnostic.Source);+ Assert.Equal("Boolean Subtract: analytic hole candidate failed diagnostic NotFullySpanning (cylinder does not fully span the box Z range.).", diagnostic.Message);}[Fact]- public void Subtract_BoxCylinderOutsideFootprint_ReturnsDeterministicNotImplemented()+ public void Subtract_BoxCylinderOutsideFootprint_ReturnsRadiusExceedsBoundaryDiagnostic(){var left = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(-20d, 20d, -15d, 15d, 0d, 12d)).Value;- var outside = TransformBody(BrepPrimitives.CreateCylinder(4d, 20d).Value, Transform3D.CreateTranslation(new Vector3D(30d, 0d, 6d)));+ var outside = TransformBody(BrepPrimitives.CreateCylinder(4d, 20d).Value, Transform3D.CreateTranslation(new Vector3D(17d, 0d, 6d)));var result = BrepBoolean.Subtract(left, outside);Assert.False(result.IsSuccess);var diagnostic = Assert.Single(result.Diagnostics);Assert.Equal(KernelDiagnosticCode.NotImplemented, diagnostic.Code);- Assert.Equal("BrepBoolean.AnalyticHoleConstraintViolation", diagnostic.Source);- Assert.Contains("analytic hole candidate violates M13 constraints", diagnostic.Message, StringComparison.Ordinal);- Assert.Contains("strictly inside the box XY footprint", diagnostic.Message, StringComparison.Ordinal);+ Assert.Equal("BrepBoolean.AnalyticHole.RadiusExceedsBoundary", diagnostic.Source);+ Assert.Equal("Boolean Subtract: analytic hole candidate failed diagnostic RadiusExceedsBoundary (cylinder radial footprint exceeds the box XY boundary.).", diagnostic.Message);+ }++ [Fact]+ public void Subtract_BoxCylinderTangentToBoundary_ReturnsTangentContactDiagnostic()+ {+ var left = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(-20d, 20d, -15d, 15d, 0d, 12d)).Value;+ var tangent = TransformBody(BrepPrimitives.CreateCylinder(4d, 20d).Value, Transform3D.CreateTranslation(new Vector3D(16d, 0d, 6d)));++ var result = BrepBoolean.Subtract(left, tangent);++ Assert.False(result.IsSuccess);+ var diagnostic = Assert.Single(result.Diagnostics);+ Assert.Equal(KernelDiagnosticCode.NotImplemented, diagnostic.Code);+ Assert.Equal("BrepBoolean.AnalyticHole.TangentContact", diagnostic.Source);+ Assert.Equal("Boolean Subtract: analytic hole candidate failed diagnostic TangentContact (cylinder is tangent to the box XY boundary.).", diagnostic.Message);}[Fact]diff --git a/Aetheris.Kernel.Core/Brep/Boolean/BooleanDiagnostic.cs b/Aetheris.Kernel.Core/Brep/Boolean/BooleanDiagnostic.csnew file mode 100644index 00000000..24adc92b--- /dev/null+++ b/Aetheris.Kernel.Core/Brep/Boolean/BooleanDiagnostic.cs@@ -0,0 +1,26 @@+using Aetheris.Kernel.Core.Diagnostics;++namespace Aetheris.Kernel.Core.Brep.Boolean;++public enum BooleanDiagnosticCode+{+ AxisNotAligned,+ NotFullySpanning,+ RadiusExceedsBoundary,+ TangentContact,+ MultiBodyResult,+ UnsupportedAnalyticSurfaceKind+}++public sealed record BooleanDiagnostic(+ BooleanDiagnosticCode Code,+ string Message,+ string Source)+{+ public KernelDiagnostic ToKernelDiagnostic()+ => new(+ KernelDiagnosticCode.NotImplemented,+ KernelDiagnosticSeverity.Error,+ Message,+ Source);+}diff --git a/Aetheris.Kernel.Core/Brep/Boolean/BrepBoolean.cs b/Aetheris.Kernel.Core/Brep/Boolean/BrepBoolean.csindex 6bc02c54..8e18e25d 100644--- a/Aetheris.Kernel.Core/Brep/Boolean/BrepBoolean.cs+++ b/Aetheris.Kernel.Core/Brep/Boolean/BrepBoolean.cs@@ -40,7 +40,8 @@ public sealed record BooleanCaseClassification(AxisAlignedBoxExtents? LeftBox,AxisAlignedBoxExtents? RightBox,AnalyticSurface? RightAnalyticSurface,- string? UnsupportedReason);+ string? UnsupportedReason,+ BooleanDiagnostic? Diagnostic = null);public sealed record BooleanIntersectionData(BooleanAnalysis Analysis,@@ -55,7 +56,8 @@ public sealed record BooleanClassificationData(int FragmentCount,AxisAlignedBoxExtents? SingleBoxResult,AnalyticSurface? AnalyticHoleSurface,- string? UnsupportedReason);+ string? UnsupportedReason,+ BooleanDiagnostic? Diagnostic = null);public sealed record BooleanRebuildData(BooleanClassificationData Classification,@@ -165,12 +167,7 @@ public static class BrepBooleanif (operation == BooleanOperation.Subtract && leftRecognized && rightAnalyticRecognized){- if (analyticSurface.Kind != AnalyticSurfaceKind.Cone- || (analyticSurface.Cone is RecognizedCone cone- && BrepBooleanCylinderRecognition.ValidateThroughHole(leftBox, cone, resolvedTolerance, out _)))- {- return new BooleanCaseClassification(BooleanExecutionClass.PlanarWithAnalyticHole, leftBox, null, analyticSurface, null);- }+ return new BooleanCaseClassification(BooleanExecutionClass.PlanarWithAnalyticHole, leftBox, null, analyticSurface, null);}return new BooleanCaseClassification(@@ -229,7 +226,19 @@ public static class BrepBooleanif (analyticSurface.Kind == AnalyticSurfaceKind.Cylinder&& analyticSurface.Cylinder is RecognizedCylinder cylinder- && !BrepBooleanCylinderRecognition.ValidateThroughHole(left, cylinder, tolerance, out var reason))+ && !BrepBooleanCylinderRecognition.ValidateThroughHole(left, cylinder, tolerance, out var diagnostic))+ {+ return new BooleanClassificationData(+ intersections,+ IsComputed: true,+ FragmentCount: 0,+ SingleBoxResult: null,+ AnalyticHoleSurface: analyticSurface,+ UnsupportedReason: null,+ Diagnostic: diagnostic);+ }++ if (analyticSurface.Kind is AnalyticSurfaceKind.Cone or AnalyticSurfaceKind.Sphere or AnalyticSurfaceKind.Torus){return new BooleanClassificationData(intersections,@@ -237,7 +246,8 @@ public static class BrepBooleanFragmentCount: 0,SingleBoxResult: null,AnalyticHoleSurface: analyticSurface,- UnsupportedReason: $"Boolean {operation}: analytic hole candidate violates M13 constraints ({reason}).");+ UnsupportedReason: null,+ Diagnostic: BrepBooleanCylinderRecognition.CreateUnsupportedAnalyticSurfaceKindDiagnostic(operation.ToString(), analyticSurface.Kind));}return new BooleanClassificationData(@@ -329,6 +339,17 @@ public static class BrepBoolean{var operation = request.Operation;+ if (classification.Diagnostic is not null)+ {+ return new BooleanRebuildData(+ classification,+ RebuiltBody: null,+ Diagnostics:+ [+ classification.Diagnostic.ToKernelDiagnostic(),+ ]);+ }+if (classification.AnalyticHoleSurface is not null){var tolerance = request.Tolerance ?? ToleranceContext.Default;@@ -340,15 +361,12 @@ public static class BrepBooleanif (classification.UnsupportedReason is not null){- var source = classification.UnsupportedReason.Contains("analytic hole candidate violates M13 constraints", StringComparison.Ordinal)- ? "BrepBoolean.AnalyticHoleConstraintViolation"- : "BrepBoolean.RebuildResult";return new BooleanRebuildData(classification,RebuiltBody: null,Diagnostics:[- CreateNotImplemented(classification.UnsupportedReason, source: source),+ CreateNotImplemented(classification.UnsupportedReason, source: "BrepBoolean.RebuildResult"),]);}diff --git a/Aetheris.Kernel.Core/Brep/Boolean/BrepBooleanBoxCylinderHoleBuilder.cs b/Aetheris.Kernel.Core/Brep/Boolean/BrepBooleanBoxCylinderHoleBuilder.csindex bd74e251..05beef78 100644--- a/Aetheris.Kernel.Core/Brep/Boolean/BrepBooleanBoxCylinderHoleBuilder.cs+++ b/Aetheris.Kernel.Core/Brep/Boolean/BrepBooleanBoxCylinderHoleBuilder.cs@@ -16,10 +16,10 @@ public static class BrepBooleanBoxCylinderHoleBuilderswitch (surface.Kind){case AnalyticSurfaceKind.Cylinder when surface.Cylinder is RecognizedCylinder cylinder:- if (!BrepBooleanCylinderRecognition.ValidateThroughHole(outer, cylinder, tolerance, out var reason))+ if (!BrepBooleanCylinderRecognition.ValidateThroughHole(outer, cylinder, tolerance, out var diagnostic)){return KernelResult<BrepBody>.Failure([- BrepBooleanCylinderRecognition.CreateConstraintViolationDiagnostic(BooleanOperation.Subtract.ToString(), reason),+ diagnostic!.ToKernelDiagnostic(),]);}@@ -28,11 +28,11 @@ public static class BrepBooleanBoxCylinderHoleBuildercase AnalyticSurfaceKind.Sphere:case AnalyticSurfaceKind.Torus:return KernelResult<BrepBody>.Failure([- CreateUnsupportedAnalyticSurfaceKindDiagnostic(BooleanOperation.Subtract.ToString(), surface.Kind),+ BrepBooleanCylinderRecognition.CreateUnsupportedAnalyticSurfaceKindDiagnostic(BooleanOperation.Subtract.ToString(), surface.Kind).ToKernelDiagnostic(),]);default:return KernelResult<BrepBody>.Failure([- CreateUnsupportedAnalyticSurfaceKindDiagnostic(BooleanOperation.Subtract.ToString(), surface.Kind),+ BrepBooleanCylinderRecognition.CreateUnsupportedAnalyticSurfaceKindDiagnostic(BooleanOperation.Subtract.ToString(), surface.Kind).ToKernelDiagnostic(),]);}}@@ -187,13 +187,6 @@ public static class BrepBooleanBoxCylinderHoleBuilder: KernelResult<BrepBody>.Failure(validation.Diagnostics);}- private static KernelDiagnostic CreateUnsupportedAnalyticSurfaceKindDiagnostic(string operation, AnalyticSurfaceKind kind)- => new(- KernelDiagnosticCode.NotImplemented,- KernelDiagnosticSeverity.Error,- $"Boolean {operation}: analytic hole surface kind '{kind}' is recognized but not implemented for M13 reconstruction.",- Source: "BrepBoolean.UnsupportedAnalyticSurfaceKind");-private static LoopId AddLoop(TopologyBuilder builder, IReadOnlyList<EdgeUse> edgeUses){var loopId = builder.AllocateLoopId();diff --git a/Aetheris.Kernel.Core/Brep/Boolean/BrepBooleanCylinderRecognition.cs b/Aetheris.Kernel.Core/Brep/Boolean/BrepBooleanCylinderRecognition.csindex 1c60a1c5..396e8218 100644--- a/Aetheris.Kernel.Core/Brep/Boolean/BrepBooleanCylinderRecognition.cs+++ b/Aetheris.Kernel.Core/Brep/Boolean/BrepBooleanCylinderRecognition.cs@@ -152,13 +152,13 @@ public static class BrepBooleanCylinderRecognition&& ToleranceMath.AlmostEqual(double.Abs(axis.Z), 1d, tolerance);}- public static bool ValidateThroughHole(AxisAlignedBoxExtents box, in RecognizedCylinder cylinder, ToleranceContext tolerance, out string reason)+ public static bool ValidateThroughHole(AxisAlignedBoxExtents box, in RecognizedCylinder cylinder, ToleranceContext tolerance, out BooleanDiagnostic? diagnostic){- reason = string.Empty;+ diagnostic = null;if (!ValidateAxisAlignedZ(cylinder, tolerance)){- reason = "cylinder axis is not aligned with the box Z axis.";+ diagnostic = CreateAxisNotAlignedDiagnostic(BooleanOperation.Subtract.ToString(), "cylinder axis is not aligned with the box Z axis.");return false;}@@ -166,7 +166,7 @@ public static class BrepBooleanCylinderRecognitionvar maxCenter = cylinder.MaxCenter;if (!ToleranceMath.AlmostEqual(minCenter.X, maxCenter.X, tolerance) || !ToleranceMath.AlmostEqual(minCenter.Y, maxCenter.Y, tolerance)){- reason = "cylinder cap centers are not vertically aligned in XY.";+ diagnostic = CreateAxisNotAlignedDiagnostic(BooleanOperation.Subtract.ToString(), "cylinder cap centers are not vertically aligned in XY.");return false;}@@ -177,16 +177,32 @@ public static class BrepBooleanCylinderRecognitionif (minZ > (box.MinZ + tolerance.Linear) || maxZ < (box.MaxZ - tolerance.Linear)){- reason = "cylinder does not fully span the box Z range.";+ diagnostic = CreateNotFullySpanningDiagnostic(BooleanOperation.Subtract.ToString(), "cylinder does not fully span the box Z range.");+ return false;+ }++ var minFootprintX = centerX - cylinder.Radius;+ var maxFootprintX = centerX + cylinder.Radius;+ var minFootprintY = centerY - cylinder.Radius;+ var maxFootprintY = centerY + cylinder.Radius;++ var tangentContact =+ ToleranceMath.AlmostEqual(minFootprintX, box.MinX, tolerance)+ || ToleranceMath.AlmostEqual(maxFootprintX, box.MaxX, tolerance)+ || ToleranceMath.AlmostEqual(minFootprintY, box.MinY, tolerance)+ || ToleranceMath.AlmostEqual(maxFootprintY, box.MaxY, tolerance);+ if (tangentContact)+ {+ diagnostic = CreateTangentContactDiagnostic(BooleanOperation.Subtract.ToString(), "cylinder is tangent to the box XY boundary.");return false;}- if ((centerX - cylinder.Radius) <= (box.MinX + tolerance.Linear)- || (centerX + cylinder.Radius) >= (box.MaxX - tolerance.Linear)- || (centerY - cylinder.Radius) <= (box.MinY + tolerance.Linear)- || (centerY + cylinder.Radius) >= (box.MaxY - tolerance.Linear))+ if (minFootprintX < (box.MinX - tolerance.Linear)+ || maxFootprintX > (box.MaxX + tolerance.Linear)+ || minFootprintY < (box.MinY - tolerance.Linear)+ || maxFootprintY > (box.MaxY + tolerance.Linear)){- reason = "cylinder radial footprint must stay strictly inside the box XY footprint.";+ diagnostic = CreateRadiusExceedsBoundaryDiagnostic(BooleanOperation.Subtract.ToString(), "cylinder radial footprint exceeds the box XY boundary.");return false;}@@ -238,17 +254,33 @@ public static class BrepBooleanCylinderRecognitionreturn true;}- public static KernelDiagnostic CreateConstraintViolationDiagnostic(string operation, string message)+ public static BooleanDiagnostic CreateAxisNotAlignedDiagnostic(string operation, string detail)+ => new(+ BooleanDiagnosticCode.AxisNotAligned,+ $"Boolean {operation}: analytic hole candidate failed diagnostic AxisNotAligned ({detail}).",+ "BrepBoolean.AnalyticHole.AxisNotAligned");++ public static BooleanDiagnostic CreateNotFullySpanningDiagnostic(string operation, string detail)+ => new(+ BooleanDiagnosticCode.NotFullySpanning,+ $"Boolean {operation}: analytic hole candidate failed diagnostic NotFullySpanning ({detail}).",+ "BrepBoolean.AnalyticHole.NotFullySpanning");++ public static BooleanDiagnostic CreateRadiusExceedsBoundaryDiagnostic(string operation, string detail)+ => new(+ BooleanDiagnosticCode.RadiusExceedsBoundary,+ $"Boolean {operation}: analytic hole candidate failed diagnostic RadiusExceedsBoundary ({detail}).",+ "BrepBoolean.AnalyticHole.RadiusExceedsBoundary");++ public static BooleanDiagnostic CreateTangentContactDiagnostic(string operation, string detail)=> new(- KernelDiagnosticCode.NotImplemented,- KernelDiagnosticSeverity.Error,- $"Boolean {operation}: analytic hole candidate violates M13 constraints ({message}).",- Source: "BrepBoolean.AnalyticHoleConstraintViolation");+ BooleanDiagnosticCode.TangentContact,+ $"Boolean {operation}: analytic hole candidate failed diagnostic TangentContact ({detail}).",+ "BrepBoolean.AnalyticHole.TangentContact");- public static KernelDiagnostic CreateUnsupportedThroughHoleDiagnostic(string operation, string reason)+ public static BooleanDiagnostic CreateUnsupportedAnalyticSurfaceKindDiagnostic(string operation, AnalyticSurfaceKind kind)=> new(- KernelDiagnosticCode.NotImplemented,- KernelDiagnosticSeverity.Error,- $"Boolean {operation}: box-cylinder subtract only supports a strict Z-aligned through-hole subset ({reason}).",- Source: "BrepBoolean.RebuildResult");+ BooleanDiagnosticCode.UnsupportedAnalyticSurfaceKind,+ $"Boolean {operation}: analytic hole surface kind '{kind}' is recognized but not implemented for M13 reconstruction.",+ "BrepBoolean.UnsupportedAnalyticSurfaceKind");}diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentBuildAndExportTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentBuildAndExportTests.csindex 2a2331ed..20ead6f5 100644--- a/Aetheris.Kernel.Firmament.Tests/FirmamentBuildAndExportTests.cs+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentBuildAndExportTests.cs@@ -203,6 +203,6 @@ public sealed class FirmamentBuildAndExportTestsprivate static bool HasExpectedMixedPrimitiveFailure(string message)=> message.Contains("M13 only supports recognized axis-aligned boxes from BrepPrimitives.CreateBox(...).", StringComparison.Ordinal)- || message.Contains("analytic hole candidate violates M13 constraints", StringComparison.Ordinal)+ || message.Contains("analytic hole candidate failed diagnostic", StringComparison.Ordinal)|| message.Contains("analytic hole surface kind", StringComparison.Ordinal);}diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentPrimitiveExecutionTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentPrimitiveExecutionTests.csindex 09968574..c30cd372 100644--- a/Aetheris.Kernel.Firmament.Tests/FirmamentPrimitiveExecutionTests.cs+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentPrimitiveExecutionTests.cs@@ -277,7 +277,8 @@ public sealed class FirmamentPrimitiveExecutionTestsAssert.Contains(result.Compilation.Diagnostics, diagnostic =>diagnostic.Code == Aetheris.Kernel.Core.Diagnostics.KernelDiagnosticCode.NotImplemented&& (diagnostic.Message.Contains("M13 only supports recognized axis-aligned boxes from BrepPrimitives.CreateBox(...).", StringComparison.Ordinal)- || diagnostic.Message.Contains("analytic hole surface kind", StringComparison.Ordinal)));+ || diagnostic.Message.Contains("analytic hole surface kind", StringComparison.Ordinal)+ || diagnostic.Message.Contains("analytic hole candidate failed diagnostic", StringComparison.Ordinal)));}[Fact]diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentStepExporterTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentStepExporterTests.csindex b4f0e99c..5ffe93b6 100644--- a/Aetheris.Kernel.Firmament.Tests/FirmamentStepExporterTests.cs+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentStepExporterTests.cs@@ -93,7 +93,8 @@ public sealed class FirmamentStepExporterTestsAssert.False(second.IsSuccess);Assert.Equal(first.Diagnostics, second.Diagnostics);Assert.Contains(first.Diagnostics, diagnostic => diagnostic.Message.Contains("Requested boolean feature 'hole' (subtract) could not be executed.", StringComparison.Ordinal));- Assert.Contains(first.Diagnostics, diagnostic => diagnostic.Message.Contains("analytic hole candidate violates M13 constraints", StringComparison.Ordinal));+ Assert.Contains(first.Diagnostics, diagnostic => diagnostic.Source == "BrepBoolean.AnalyticHole.NotFullySpanning"+ && diagnostic.Message.Contains("analytic hole candidate failed diagnostic NotFullySpanning", StringComparison.Ordinal));Assert.DoesNotContain(first.Diagnostics, diagnostic => diagnostic.Message.Contains("requires at least one executed primitive or boolean body", StringComparison.Ordinal));}@@ -157,7 +158,7 @@ public sealed class FirmamentStepExporterTestsprivate static bool HasExpectedMixedPrimitiveFailure(string message)=> message.Contains("M13 only supports recognized axis-aligned boxes from BrepPrimitives.CreateBox(...).", StringComparison.Ordinal)- || message.Contains("analytic hole candidate violates M13 constraints", StringComparison.Ordinal)+ || message.Contains("analytic hole candidate failed diagnostic", StringComparison.Ordinal)|| message.Contains("analytic hole surface kind", StringComparison.Ordinal);[Fact]