Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
Implement M13 narrow box-box boolean support
Yuechen Li2a4d3e119dd1
diff --git a/Aetheris.Kernel.Core.Tests/Brep/Boolean/BrepBooleanTests.cs b/Aetheris.Kernel.Core.Tests/Brep/Boolean/BrepBooleanTests.csindex 7205b9b2..59a9c3ff 100644--- a/Aetheris.Kernel.Core.Tests/Brep/Boolean/BrepBooleanTests.cs+++ b/Aetheris.Kernel.Core.Tests/Brep/Boolean/BrepBooleanTests.cs@@ -1,96 +1,203 @@using Aetheris.Kernel.Core.Brep;using Aetheris.Kernel.Core.Brep.Boolean;using Aetheris.Kernel.Core.Diagnostics;+using Aetheris.Kernel.Core.Numerics;namespace Aetheris.Kernel.Core.Tests.Brep.Boolean;public sealed class BrepBooleanTests{[Fact]- public void Execute_NullRequest_FailsWithInvalidArgumentDiagnostic()+ public void BoxRecognition_CreateBox_IsRecognizedAndExtentsExtracted(){- var result = BrepBoolean.Execute(request: null);+ var box = BrepPrimitives.CreateBox(4d, 6d, 8d).Value;++ var recognized = BrepBooleanBoxRecognition.TryRecognizeAxisAlignedBox(box, ToleranceContext.Default, out var extents, out var reason);++ Assert.True(recognized, reason);+ Assert.Equal(-2d, extents.MinX);+ Assert.Equal(2d, extents.MaxX);+ Assert.Equal(-3d, extents.MinY);+ Assert.Equal(3d, extents.MaxY);+ Assert.Equal(-4d, extents.MinZ);+ Assert.Equal(4d, extents.MaxZ);+ }++ [Fact]+ public void BoxRecognition_NonBoxBody_IsRejected()+ {+ var sphere = BrepPrimitives.CreateSphere(2d).Value;++ var recognized = BrepBooleanBoxRecognition.TryRecognizeAxisAlignedBox(sphere, ToleranceContext.Default, out _, out var reason);++ Assert.False(recognized);+ Assert.NotEmpty(reason);+ }++ [Fact]+ public void Intersect_OverlappingBoxes_ReturnsPositiveVolumeBox()+ {+ var left = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(0d, 4d, 0d, 4d, 0d, 4d)).Value;+ var right = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(2d, 6d, 1d, 3d, -1d, 2d)).Value;++ var result = BrepBoolean.Intersect(left, right);++ Assert.True(result.IsSuccess);+ Assert.True(BrepBooleanBoxRecognition.TryRecognizeAxisAlignedBox(result.Value, ToleranceContext.Default, out var extents, out _));+ Assert.Equal(new AxisAlignedBoxExtents(2d, 4d, 1d, 3d, 0d, 2d), extents);+ Assert.True(BrepBindingValidator.Validate(result.Value, true).IsSuccess);+ }++ [Fact]+ public void Intersect_DisjointBoxes_ReturnsDeterministicNotImplemented()+ {+ var left = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(0d, 1d, 0d, 1d, 0d, 1d)).Value;+ var right = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(2d, 3d, 2d, 3d, 2d, 3d)).Value;++ var result = BrepBoolean.Intersect(left, right);Assert.False(result.IsSuccess);var diagnostic = Assert.Single(result.Diagnostics);- Assert.Equal(KernelDiagnosticCode.InvalidArgument, diagnostic.Code);- Assert.Equal("Boolean request must be provided.", diagnostic.Message);- Assert.Equal("BrepBoolean.ValidateInputs", diagnostic.Source);+ Assert.Equal(KernelDiagnosticCode.NotImplemented, diagnostic.Code);+ Assert.Equal("Boolean Intersect: empty intersection result is not representable in M13.", diagnostic.Message);}[Fact]- public void Execute_NullBodyInRequest_FailsPredictably()+ public void Intersect_TouchingFaceOnly_ReturnsDeterministicNotImplemented(){- var right = BrepPrimitives.CreateBox(1d, 1d, 1d).Value;- var request = new BooleanRequest(null!, right, BooleanOperation.Union);+ var left = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(0d, 1d, 0d, 1d, 0d, 1d)).Value;+ var right = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(1d, 2d, 0d, 1d, 0d, 1d)).Value;- var result = BrepBoolean.Execute(request);+ var result = BrepBoolean.Intersect(left, right);Assert.False(result.IsSuccess);var diagnostic = Assert.Single(result.Diagnostics);- Assert.Equal(KernelDiagnosticCode.InvalidArgument, diagnostic.Code);- Assert.Equal("Boolean Union: left body must be provided.", diagnostic.Message);- Assert.Equal("BrepBoolean.ValidateInputs", diagnostic.Source);+ Assert.Equal(KernelDiagnosticCode.NotImplemented, diagnostic.Code);+ Assert.Equal("Boolean Intersect: touching-only intersection is non-solid and empty results are not representable in M13.", diagnostic.Message);}[Fact]- public void Execute_DifferentBodies_ReturnsNotImplementedWithoutThrowing()+ public void Union_ContainingCase_ReturnsContainingBox(){- var left = BrepPrimitives.CreateBox(2d, 2d, 2d).Value;- var right = BrepPrimitives.CreateBox(3d, 3d, 3d).Value;+ var left = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(-3d, 3d, -3d, 3d, -3d, 3d)).Value;+ var right = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(-1d, 1d, -1d, 1d, -1d, 1d)).Value;- var exception = Record.Exception(() => BrepBoolean.Execute(new BooleanRequest(left, right, BooleanOperation.Union)));+ var result = BrepBoolean.Union(left, right);- Assert.Null(exception);+ Assert.True(result.IsSuccess);+ Assert.True(BrepBooleanBoxRecognition.TryRecognizeAxisAlignedBox(result.Value, ToleranceContext.Default, out var extents, out _));+ Assert.Equal(new AxisAlignedBoxExtents(-3d, 3d, -3d, 3d, -3d, 3d), extents);+ }++ [Fact]+ public void Union_ExactSingleBoxCase_ReturnsCombinedBox()+ {+ var left = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(0d, 2d, 0d, 2d, 0d, 2d)).Value;+ var right = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(2d, 5d, 0d, 2d, 0d, 2d)).Value;++ var result = BrepBoolean.Union(left, right);++ Assert.True(result.IsSuccess);+ Assert.True(BrepBooleanBoxRecognition.TryRecognizeAxisAlignedBox(result.Value, ToleranceContext.Default, out var extents, out _));+ Assert.Equal(new AxisAlignedBoxExtents(0d, 5d, 0d, 2d, 0d, 2d), extents);+ }++ [Fact]+ public void Union_LShapedOverlap_DoesNotReturnBoundingBox()+ {+ var left = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(0d, 2d, 0d, 4d, 0d, 2d)).Value;+ var right = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(2d, 4d, 0d, 2d, 0d, 2d)).Value;++ var result = BrepBoolean.Union(left, right);- var result = BrepBoolean.Execute(new BooleanRequest(left, right, BooleanOperation.Union));Assert.False(result.IsSuccess);+ var diagnostic = Assert.Single(result.Diagnostics);+ Assert.Equal(KernelDiagnosticCode.NotImplemented, diagnostic.Code);+ Assert.Contains("not a single box", diagnostic.Message, StringComparison.Ordinal);+ }++ [Fact]+ public void Union_DisjointBoxes_ReturnsNotImplemented()+ {+ var left = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(0d, 1d, 0d, 1d, 0d, 1d)).Value;+ var right = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(3d, 4d, 3d, 4d, 3d, 4d)).Value;+ var result = BrepBoolean.Union(left, right);++ Assert.False(result.IsSuccess);var diagnostic = Assert.Single(result.Diagnostics);Assert.Equal(KernelDiagnosticCode.NotImplemented, diagnostic.Code);- Assert.Equal("Boolean Union: general B-rep boolean intersection/rebuild is not implemented in M12.", diagnostic.Message);- Assert.Equal("BrepBoolean.RebuildResult", diagnostic.Source);+ Assert.Equal("Boolean Union: disjoint box union is multi-body and not supported in M13.", diagnostic.Message);}[Fact]- public void Intersect_SameBodyInstance_SucceedsAndReturnsSameInstance()+ public void Subtract_NoOverlap_ReturnsLeftBox(){- var body = BrepPrimitives.CreateBox(2d, 3d, 4d).Value;+ var left = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(-2d, 2d, -2d, 2d, -2d, 2d)).Value;+ var right = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(3d, 4d, 3d, 4d, 3d, 4d)).Value;- var result = BrepBoolean.Intersect(body, body);+ var result = BrepBoolean.Subtract(left, right);Assert.True(result.IsSuccess);- Assert.Same(body, result.Value);+ Assert.True(BrepBooleanBoxRecognition.TryRecognizeAxisAlignedBox(result.Value, ToleranceContext.Default, out var extents, out _));+ Assert.Equal(new AxisAlignedBoxExtents(-2d, 2d, -2d, 2d, -2d, 2d), extents);+ }++ [Fact]+ public void Subtract_FullContainment_ReturnsEmptyNotRepresentableDiagnostic()+ {+ var left = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(-1d, 1d, -1d, 1d, -1d, 1d)).Value;+ var right = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(-3d, 3d, -3d, 3d, -3d, 3d)).Value;- var validation = BrepBindingValidator.Validate(result.Value, requireAllEdgeAndFaceBindings: true);- Assert.True(validation.IsSuccess);+ var result = BrepBoolean.Subtract(left, right);++ Assert.False(result.IsSuccess);+ var diagnostic = Assert.Single(result.Diagnostics);+ Assert.Equal(KernelDiagnosticCode.NotImplemented, diagnostic.Code);+ Assert.Equal("Boolean Subtract: subtraction fully removes the left box and empty results are not representable in M13.", diagnostic.Message);}[Fact]- public void Union_SameBodyInstance_SucceedsAndReturnsSameInstance()+ public void Subtract_NonSingleBoxResult_ReturnsNotImplemented(){- var body = BrepPrimitives.CreateCylinder(1d, 2d).Value;+ var left = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(0d, 4d, 0d, 4d, 0d, 4d)).Value;+ var right = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(1d, 3d, 1d, 3d, 0d, 4d)).Value;- var result = BrepBoolean.Union(body, body);+ var result = BrepBoolean.Subtract(left, right);- Assert.True(result.IsSuccess);- Assert.Same(body, result.Value);+ Assert.False(result.IsSuccess);+ var diagnostic = Assert.Single(result.Diagnostics);+ Assert.Equal(KernelDiagnosticCode.NotImplemented, diagnostic.Code);+ Assert.Equal("Boolean Subtract: box subtraction result is not representable as a single box in M13.", diagnostic.Message);+ }++ [Fact]+ public void Subtract_SingleBoxClipCase_ReturnsSingleBox()+ {+ var left = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(0d, 6d, 0d, 2d, 0d, 2d)).Value;+ var right = BrepBooleanBoxRecognition.CreateBoxFromExtents(new AxisAlignedBoxExtents(3d, 6d, 0d, 2d, 0d, 2d)).Value;- var validation = BrepBindingValidator.Validate(result.Value, requireAllEdgeAndFaceBindings: true);- Assert.True(validation.IsSuccess);+ var result = BrepBoolean.Subtract(left, right);++ Assert.True(result.IsSuccess);+ Assert.True(BrepBooleanBoxRecognition.TryRecognizeAxisAlignedBox(result.Value, ToleranceContext.Default, out var extents, out _));+ Assert.Equal(new AxisAlignedBoxExtents(0d, 3d, 0d, 2d, 0d, 2d), extents);+ Assert.True(BrepBindingValidator.Validate(result.Value, true).IsSuccess);}[Fact]- public void Subtract_SameBodyInstance_ReturnsNotImplemented()+ public void Execute_NonBoxInput_ReturnsDeterministicNotImplementedWithoutThrowing(){- var body = BrepPrimitives.CreateSphere(1d).Value;+ var left = BrepPrimitives.CreateCylinder(1d, 2d).Value;+ var right = BrepPrimitives.CreateBox(2d, 2d, 2d).Value;- var result = BrepBoolean.Subtract(body, body);+ var exception = Record.Exception(() => BrepBoolean.Union(left, right));+ Assert.Null(exception);+ var result = BrepBoolean.Union(left, right);Assert.False(result.IsSuccess);var diagnostic = Assert.Single(result.Diagnostics);Assert.Equal(KernelDiagnosticCode.NotImplemented, diagnostic.Code);- Assert.Equal("Boolean Subtract: same-body subtraction requires an empty-body representation that is not available in M12.", diagnostic.Message);- Assert.Equal("BrepBoolean.RebuildResult", diagnostic.Source);+ Assert.Equal("Boolean Union: M13 only supports recognized axis-aligned boxes from BrepPrimitives.CreateBox(...).", diagnostic.Message);}}diff --git a/Aetheris.Kernel.Core/Brep/Boolean/BrepBoolean.cs b/Aetheris.Kernel.Core/Brep/Boolean/BrepBoolean.csindex 9f682301..ed6da8ee 100644--- a/Aetheris.Kernel.Core/Brep/Boolean/BrepBoolean.cs+++ b/Aetheris.Kernel.Core/Brep/Boolean/BrepBoolean.cs@@ -1,4 +1,5 @@using Aetheris.Kernel.Core.Diagnostics;+using Aetheris.Kernel.Core.Math;using Aetheris.Kernel.Core.Numerics;using Aetheris.Kernel.Core.Results;@@ -20,17 +21,24 @@ public sealed record BooleanRequest(public sealed record BooleanAnalysis(BooleanOperation Operation,bool IsSameBodyInstance,- string? ShortcutReason);+ string? ShortcutReason,+ AxisAlignedBoxExtents? LeftBox,+ AxisAlignedBoxExtents? RightBox,+ string? UnsupportedReason);public sealed record BooleanIntersectionData(BooleanAnalysis Analysis,bool IsComputed,- int CandidatePairCount);+ int CandidatePairCount,+ AxisAlignedBoxExtents? OverlapBox,+ bool IsTouchingOnly);public sealed record BooleanClassificationData(BooleanIntersectionData Intersections,bool IsComputed,- int FragmentCount);+ int FragmentCount,+ AxisAlignedBoxExtents? SingleBoxResult,+ string? UnsupportedReason);public sealed record BooleanRebuildData(BooleanClassificationData Classification,@@ -38,9 +46,8 @@ public sealed record BooleanRebuildData(IReadOnlyList<KernelDiagnostic> Diagnostics);/// <summary>-/// M12 boolean pipeline scaffold.-/// Only same-instance Union/Intersect shortcuts are supported in this milestone.-/// All other requests return deterministic NotImplemented diagnostics.+/// M13 boolean pipeline with narrow real support for axis-aligned box/box cases that resolve to a single box.+/// Unsupported and non-solid cases return deterministic NotImplemented diagnostics./// </summary>public static class BrepBoolean{@@ -62,8 +69,8 @@ public static class BrepBoolean}var analysis = AnalyzeInputs(validation.Value);- var intersections = ComputeIntersections(analysis);- var classification = ClassifyFragments(intersections);+ var intersections = ComputeIntersections(validation.Value, analysis);+ var classification = ClassifyFragments(validation.Value, intersections);var rebuild = RebuildResult(validation.Value, classification);if (rebuild.Diagnostics.Any(d => d.Severity == KernelDiagnosticSeverity.Error)){@@ -112,60 +119,200 @@ public static class BrepBoolean? $"Boolean {request.Operation}: same-body shortcut candidate.": null;- return new BooleanAnalysis(request.Operation, isSameBodyInstance, shortcutReason);+ var tolerance = request.Tolerance ?? ToleranceContext.Default;+ var leftRecognized = BrepBooleanBoxRecognition.TryRecognizeAxisAlignedBox(request.Left, tolerance, out var leftBox, out _);+ var rightRecognized = BrepBooleanBoxRecognition.TryRecognizeAxisAlignedBox(request.Right, tolerance, out var rightBox, out _);+ var unsupportedReason = leftRecognized && rightRecognized+ ? null+ : $"Boolean {request.Operation}: M13 only supports recognized axis-aligned boxes from BrepPrimitives.CreateBox(...).";++ return new BooleanAnalysis(request.Operation, isSameBodyInstance, shortcutReason, leftBox, rightBox, unsupportedReason);}- private static BooleanIntersectionData ComputeIntersections(BooleanAnalysis analysis)+ private static BooleanIntersectionData ComputeIntersections(BooleanRequest request, BooleanAnalysis analysis){- var candidatePairCount = analysis.IsSameBodyInstance ? 1 : 0;- return new BooleanIntersectionData(analysis, IsComputed: true, candidatePairCount);+ if (analysis.LeftBox is null || analysis.RightBox is null)+ {+ return new BooleanIntersectionData(analysis, IsComputed: true, CandidatePairCount: 0, OverlapBox: null, IsTouchingOnly: false);+ }++ var overlap = AxisAlignedBoxExtents.Intersection(analysis.LeftBox.Value, analysis.RightBox.Value);+ var tolerance = request.Tolerance ?? ToleranceContext.Default;+ var isTouchingOnly = overlap is not null && !overlap.Value.HasPositiveVolume(tolerance);+ var overlapBox = overlap is not null && overlap.Value.HasPositiveVolume(tolerance)+ ? overlap+ : null;++ return new BooleanIntersectionData(analysis, IsComputed: true, CandidatePairCount: 1, overlapBox, isTouchingOnly);}- private static BooleanClassificationData ClassifyFragments(BooleanIntersectionData intersections)+ private static BooleanClassificationData ClassifyFragments(BooleanRequest request, BooleanIntersectionData intersections){- var fragmentCount = intersections.Analysis.IsSameBodyInstance ? 1 : 0;- return new BooleanClassificationData(intersections, IsComputed: true, fragmentCount);+ if (intersections.Analysis.UnsupportedReason is not null)+ {+ return new BooleanClassificationData(intersections, IsComputed: true, FragmentCount: 0, SingleBoxResult: null, UnsupportedReason: intersections.Analysis.UnsupportedReason);+ }++ var left = intersections.Analysis.LeftBox!.Value;+ var right = intersections.Analysis.RightBox!.Value;+ var tolerance = request.Tolerance ?? ToleranceContext.Default;+ var operation = request.Operation;++ if (operation == BooleanOperation.Intersect)+ {+ if (intersections.IsTouchingOnly)+ {+ return new BooleanClassificationData(intersections, IsComputed: true, FragmentCount: 0, SingleBoxResult: null, UnsupportedReason: $"Boolean {operation}: touching-only intersection is non-solid and empty results are not representable in M13.");+ }++ if (intersections.OverlapBox is null)+ {+ return new BooleanClassificationData(intersections, IsComputed: true, FragmentCount: 0, SingleBoxResult: null, UnsupportedReason: $"Boolean {operation}: empty intersection result is not representable in M13.");+ }++ return new BooleanClassificationData(intersections, IsComputed: true, FragmentCount: 1, SingleBoxResult: intersections.OverlapBox, UnsupportedReason: null);+ }++ if (operation == BooleanOperation.Union)+ {+ if (left.ApproximatelyEquals(right, tolerance))+ {+ return new BooleanClassificationData(intersections, IsComputed: true, FragmentCount: 1, SingleBoxResult: left, UnsupportedReason: null);+ }++ if (left.Contains(right, tolerance))+ {+ return new BooleanClassificationData(intersections, IsComputed: true, FragmentCount: 1, SingleBoxResult: left, UnsupportedReason: null);+ }++ if (right.Contains(left, tolerance))+ {+ return new BooleanClassificationData(intersections, IsComputed: true, FragmentCount: 1, SingleBoxResult: right, UnsupportedReason: null);+ }++ if (intersections.OverlapBox is null)+ {+ return new BooleanClassificationData(intersections, IsComputed: true, FragmentCount: 0, SingleBoxResult: null, UnsupportedReason: $"Boolean {operation}: disjoint box union is multi-body and not supported in M13.");+ }++ var bounds = AxisAlignedBoxExtents.Bounding(left, right);+ if (AxisAlignedBoxExtents.UnionIsSingleBox(left, right, bounds, tolerance))+ {+ return new BooleanClassificationData(intersections, IsComputed: true, FragmentCount: 1, SingleBoxResult: bounds, UnsupportedReason: null);+ }++ return new BooleanClassificationData(intersections, IsComputed: true, FragmentCount: 0, SingleBoxResult: null, UnsupportedReason: $"Boolean {operation}: overlapping box union is not a single box in M13 (for example L-shaped unions)." );+ }++ if (!left.OverlapsWithPositiveVolume(right, tolerance))+ {+ return new BooleanClassificationData(intersections, IsComputed: true, FragmentCount: 1, SingleBoxResult: left, UnsupportedReason: null);+ }++ if (right.Contains(left, tolerance))+ {+ return new BooleanClassificationData(intersections, IsComputed: true, FragmentCount: 0, SingleBoxResult: null, UnsupportedReason: $"Boolean {operation}: subtraction fully removes the left box and empty results are not representable in M13.");+ }++ if (TrySubtractToSingleBox(left, right, tolerance, out var singleBox))+ {+ return new BooleanClassificationData(intersections, IsComputed: true, FragmentCount: 1, SingleBoxResult: singleBox, UnsupportedReason: null);+ }++ return new BooleanClassificationData(intersections, IsComputed: true, FragmentCount: 0, SingleBoxResult: null, UnsupportedReason: $"Boolean {operation}: box subtraction result is not representable as a single box in M13.");}private static BooleanRebuildData RebuildResult(BooleanRequest request, BooleanClassificationData classification){var operation = request.Operation;- if (classification.Intersections.Analysis.IsSameBodyInstance)+ if (classification.UnsupportedReason is not null){- return operation switch- {- BooleanOperation.Union or BooleanOperation.Intersect => new BooleanRebuildData(- classification,- RebuiltBody: request.Left,- Diagnostics: Array.Empty<KernelDiagnostic>()),- BooleanOperation.Subtract => new BooleanRebuildData(- classification,- RebuiltBody: null,- Diagnostics:- [- CreateNotImplemented(- $"Boolean {operation}: same-body subtraction requires an empty-body representation that is not available in M12.",- source: "BrepBoolean.RebuildResult"),- ]),- _ => new BooleanRebuildData(- classification,- RebuiltBody: null,- Diagnostics:- [- CreateInternalError($"Boolean {operation}: unexpected operation.", source: "BrepBoolean.RebuildResult"),- ]),- };- }-- return new BooleanRebuildData(- classification,- RebuiltBody: null,- Diagnostics:- [- CreateNotImplemented(- $"Boolean {operation}: general B-rep boolean intersection/rebuild is not implemented in M12.",- source: "BrepBoolean.RebuildResult"),- ]);+ return new BooleanRebuildData(+ classification,+ RebuiltBody: null,+ Diagnostics:+ [+ CreateNotImplemented(classification.UnsupportedReason, source: "BrepBoolean.RebuildResult"),+ ]);+ }++ if (classification.SingleBoxResult is null)+ {+ return new BooleanRebuildData(+ classification,+ RebuiltBody: null,+ Diagnostics:+ [+ CreateInternalError($"Boolean {operation}: classification omitted both result and unsupported reason.", source: "BrepBoolean.RebuildResult"),+ ]);+ }++ var rebuilt = BrepBooleanBoxRecognition.CreateBoxFromExtents(classification.SingleBoxResult.Value);+ return rebuilt.IsSuccess+ ? new BooleanRebuildData(classification, rebuilt.Value, Array.Empty<KernelDiagnostic>())+ : new BooleanRebuildData(classification, RebuiltBody: null, Diagnostics: rebuilt.Diagnostics);+ }++ private static bool TrySubtractToSingleBox(AxisAlignedBoxExtents left, AxisAlignedBoxExtents right, ToleranceContext tolerance, out AxisAlignedBoxExtents singleBox)+ {+ singleBox = default;+ var overlap = AxisAlignedBoxExtents.Intersection(left, right);+ if (overlap is null || !overlap.Value.HasPositiveVolume(tolerance))+ {+ return false;+ }++ var o = overlap.Value;++ if (ToleranceMath.GreaterThanOrAlmostEqual(o.MinX, left.MinX, tolerance) && ToleranceMath.AlmostEqual(o.MaxX, left.MaxX, tolerance) &&+ ToleranceMath.AlmostEqual(o.MinY, left.MinY, tolerance) && ToleranceMath.AlmostEqual(o.MaxY, left.MaxY, tolerance) &&+ ToleranceMath.AlmostEqual(o.MinZ, left.MinZ, tolerance) && ToleranceMath.AlmostEqual(o.MaxZ, left.MaxZ, tolerance))+ {+ singleBox = new AxisAlignedBoxExtents(left.MinX, o.MinX, left.MinY, left.MaxY, left.MinZ, left.MaxZ);+ return singleBox.HasPositiveVolume(tolerance);+ }++ if (ToleranceMath.LessThanOrAlmostEqual(o.MaxX, left.MaxX, tolerance) && ToleranceMath.AlmostEqual(o.MinX, left.MinX, tolerance) &&+ ToleranceMath.AlmostEqual(o.MinY, left.MinY, tolerance) && ToleranceMath.AlmostEqual(o.MaxY, left.MaxY, tolerance) &&+ ToleranceMath.AlmostEqual(o.MinZ, left.MinZ, tolerance) && ToleranceMath.AlmostEqual(o.MaxZ, left.MaxZ, tolerance))+ {+ singleBox = new AxisAlignedBoxExtents(o.MaxX, left.MaxX, left.MinY, left.MaxY, left.MinZ, left.MaxZ);+ return singleBox.HasPositiveVolume(tolerance);+ }++ if (ToleranceMath.GreaterThanOrAlmostEqual(o.MinY, left.MinY, tolerance) && ToleranceMath.AlmostEqual(o.MaxY, left.MaxY, tolerance) &&+ ToleranceMath.AlmostEqual(o.MinX, left.MinX, tolerance) && ToleranceMath.AlmostEqual(o.MaxX, left.MaxX, tolerance) &&+ ToleranceMath.AlmostEqual(o.MinZ, left.MinZ, tolerance) && ToleranceMath.AlmostEqual(o.MaxZ, left.MaxZ, tolerance))+ {+ singleBox = new AxisAlignedBoxExtents(left.MinX, left.MaxX, left.MinY, o.MinY, left.MinZ, left.MaxZ);+ return singleBox.HasPositiveVolume(tolerance);+ }++ if (ToleranceMath.LessThanOrAlmostEqual(o.MaxY, left.MaxY, tolerance) && ToleranceMath.AlmostEqual(o.MinY, left.MinY, tolerance) &&+ ToleranceMath.AlmostEqual(o.MinX, left.MinX, tolerance) && ToleranceMath.AlmostEqual(o.MaxX, left.MaxX, tolerance) &&+ ToleranceMath.AlmostEqual(o.MinZ, left.MinZ, tolerance) && ToleranceMath.AlmostEqual(o.MaxZ, left.MaxZ, tolerance))+ {+ singleBox = new AxisAlignedBoxExtents(left.MinX, left.MaxX, o.MaxY, left.MaxY, left.MinZ, left.MaxZ);+ return singleBox.HasPositiveVolume(tolerance);+ }++ if (ToleranceMath.GreaterThanOrAlmostEqual(o.MinZ, left.MinZ, tolerance) && ToleranceMath.AlmostEqual(o.MaxZ, left.MaxZ, tolerance) &&+ ToleranceMath.AlmostEqual(o.MinX, left.MinX, tolerance) && ToleranceMath.AlmostEqual(o.MaxX, left.MaxX, tolerance) &&+ ToleranceMath.AlmostEqual(o.MinY, left.MinY, tolerance) && ToleranceMath.AlmostEqual(o.MaxY, left.MaxY, tolerance))+ {+ singleBox = new AxisAlignedBoxExtents(left.MinX, left.MaxX, left.MinY, left.MaxY, left.MinZ, o.MinZ);+ return singleBox.HasPositiveVolume(tolerance);+ }++ if (ToleranceMath.LessThanOrAlmostEqual(o.MaxZ, left.MaxZ, tolerance) && ToleranceMath.AlmostEqual(o.MinZ, left.MinZ, tolerance) &&+ ToleranceMath.AlmostEqual(o.MinX, left.MinX, tolerance) && ToleranceMath.AlmostEqual(o.MaxX, left.MaxX, tolerance) &&+ ToleranceMath.AlmostEqual(o.MinY, left.MinY, tolerance) && ToleranceMath.AlmostEqual(o.MaxY, left.MaxY, tolerance))+ {+ singleBox = new AxisAlignedBoxExtents(left.MinX, left.MaxX, left.MinY, left.MaxY, o.MaxZ, left.MaxZ);+ return singleBox.HasPositiveVolume(tolerance);+ }++ return false;}private static KernelResult<BrepBody> ValidateOutput(BrepBody body, IReadOnlyList<KernelDiagnostic> diagnostics)diff --git a/Aetheris.Kernel.Core/Brep/Boolean/BrepBooleanBoxRecognition.cs b/Aetheris.Kernel.Core/Brep/Boolean/BrepBooleanBoxRecognition.csnew file mode 100644index 00000000..c9f4122c--- /dev/null+++ b/Aetheris.Kernel.Core/Brep/Boolean/BrepBooleanBoxRecognition.cs@@ -0,0 +1,214 @@+using Aetheris.Kernel.Core.Diagnostics;+using Aetheris.Kernel.Core.Geometry;+using Aetheris.Kernel.Core.Geometry.Curves;+using Aetheris.Kernel.Core.Geometry.Surfaces;+using Aetheris.Kernel.Core.Math;+using Aetheris.Kernel.Core.Numerics;+using Aetheris.Kernel.Core.Results;++namespace Aetheris.Kernel.Core.Brep.Boolean;++public readonly record struct AxisAlignedBoxExtents(double MinX, double MaxX, double MinY, double MaxY, double MinZ, double MaxZ)+{+ public bool HasPositiveVolume(ToleranceContext tolerance)+ => (MaxX - MinX) > tolerance.Linear && (MaxY - MinY) > tolerance.Linear && (MaxZ - MinZ) > tolerance.Linear;++ public bool ApproximatelyEquals(AxisAlignedBoxExtents other, ToleranceContext tolerance)+ => ToleranceMath.AlmostEqual(MinX, other.MinX, tolerance)+ && ToleranceMath.AlmostEqual(MaxX, other.MaxX, tolerance)+ && ToleranceMath.AlmostEqual(MinY, other.MinY, tolerance)+ && ToleranceMath.AlmostEqual(MaxY, other.MaxY, tolerance)+ && ToleranceMath.AlmostEqual(MinZ, other.MinZ, tolerance)+ && ToleranceMath.AlmostEqual(MaxZ, other.MaxZ, tolerance);++ public bool Contains(AxisAlignedBoxExtents other, ToleranceContext tolerance)+ => ToleranceMath.LessThanOrAlmostEqual(MinX, other.MinX, tolerance)+ && ToleranceMath.GreaterThanOrAlmostEqual(MaxX, other.MaxX, tolerance)+ && ToleranceMath.LessThanOrAlmostEqual(MinY, other.MinY, tolerance)+ && ToleranceMath.GreaterThanOrAlmostEqual(MaxY, other.MaxY, tolerance)+ && ToleranceMath.LessThanOrAlmostEqual(MinZ, other.MinZ, tolerance)+ && ToleranceMath.GreaterThanOrAlmostEqual(MaxZ, other.MaxZ, tolerance);++ public bool OverlapsWithPositiveVolume(AxisAlignedBoxExtents other, ToleranceContext tolerance)+ => Intersection(this, other) is { } intersection && intersection.HasPositiveVolume(tolerance);++ public static AxisAlignedBoxExtents? Intersection(AxisAlignedBoxExtents a, AxisAlignedBoxExtents b)+ {+ var minX = System.Math.Max(a.MinX, b.MinX);+ var maxX = System.Math.Min(a.MaxX, b.MaxX);+ var minY = System.Math.Max(a.MinY, b.MinY);+ var maxY = System.Math.Min(a.MaxY, b.MaxY);+ var minZ = System.Math.Max(a.MinZ, b.MinZ);+ var maxZ = System.Math.Min(a.MaxZ, b.MaxZ);++ return minX <= maxX && minY <= maxY && minZ <= maxZ+ ? new AxisAlignedBoxExtents(minX, maxX, minY, maxY, minZ, maxZ)+ : null;+ }++ public static AxisAlignedBoxExtents Bounding(AxisAlignedBoxExtents a, AxisAlignedBoxExtents b)+ => new(System.Math.Min(a.MinX, b.MinX), System.Math.Max(a.MaxX, b.MaxX), System.Math.Min(a.MinY, b.MinY), System.Math.Max(a.MaxY, b.MaxY), System.Math.Min(a.MinZ, b.MinZ), System.Math.Max(a.MaxZ, b.MaxZ));++ public static bool UnionIsSingleBox(AxisAlignedBoxExtents a, AxisAlignedBoxExtents b, AxisAlignedBoxExtents bounds, ToleranceContext tolerance)+ {+ var xAligned = (ToleranceMath.AlmostEqual(a.MinX, bounds.MinX, tolerance) && ToleranceMath.AlmostEqual(a.MaxX, bounds.MaxX, tolerance)+ && ToleranceMath.AlmostEqual(b.MinX, bounds.MinX, tolerance) && ToleranceMath.AlmostEqual(b.MaxX, bounds.MaxX, tolerance));+ var yAligned = (ToleranceMath.AlmostEqual(a.MinY, bounds.MinY, tolerance) && ToleranceMath.AlmostEqual(a.MaxY, bounds.MaxY, tolerance)+ && ToleranceMath.AlmostEqual(b.MinY, bounds.MinY, tolerance) && ToleranceMath.AlmostEqual(b.MaxY, bounds.MaxY, tolerance));+ var zAligned = (ToleranceMath.AlmostEqual(a.MinZ, bounds.MinZ, tolerance) && ToleranceMath.AlmostEqual(a.MaxZ, bounds.MaxZ, tolerance)+ && ToleranceMath.AlmostEqual(b.MinZ, bounds.MinZ, tolerance) && ToleranceMath.AlmostEqual(b.MaxZ, bounds.MaxZ, tolerance));++ var xTouchOrOverlap = System.Math.Min(a.MaxX, b.MaxX) >= System.Math.Max(a.MinX, b.MinX) - tolerance.Linear;+ var yTouchOrOverlap = System.Math.Min(a.MaxY, b.MaxY) >= System.Math.Max(a.MinY, b.MinY) - tolerance.Linear;+ var zTouchOrOverlap = System.Math.Min(a.MaxZ, b.MaxZ) >= System.Math.Max(a.MinZ, b.MinZ) - tolerance.Linear;++ return (xAligned && yTouchOrOverlap && zTouchOrOverlap)+ || (yAligned && xTouchOrOverlap && zTouchOrOverlap)+ || (zAligned && xTouchOrOverlap && yTouchOrOverlap);+ }+}++public static class BrepBooleanBoxRecognition+{+ public static bool TryRecognizeAxisAlignedBox(BrepBody body, ToleranceContext tolerance, out AxisAlignedBoxExtents extents, out string reason)+ {+ extents = default;+ reason = string.Empty;++ if (body.Topology.Vertices.Count() != 8 || body.Topology.Edges.Count() != 12 || body.Topology.Faces.Count() != 6)+ {+ reason = "topology does not match M08 box counts.";+ return false;+ }++ var faceBindings = body.Bindings.FaceBindings.ToArray();+ if (faceBindings.Length != 6)+ {+ reason = "box recognition requires exactly six face bindings.";+ return false;+ }++ if (body.Bindings.EdgeBindings.Count() != 12)+ {+ reason = "box recognition requires exactly twelve edge bindings.";+ return false;+ }++ foreach (var edgeBinding in body.Bindings.EdgeBindings)+ {+ var edgeCurve = body.Geometry.GetCurve(edgeBinding.CurveGeometryId);+ if (edgeCurve.Kind != CurveGeometryKind.Line3)+ {+ reason = "box recognition requires line curves on all edges.";+ return false;+ }+ }++ var hasXMin = false;+ var hasXMax = false;+ var hasYMin = false;+ var hasYMax = false;+ var hasZMin = false;+ var hasZMax = false;++ var minX = 0d;+ var maxX = 0d;+ var minY = 0d;+ var maxY = 0d;+ var minZ = 0d;+ var maxZ = 0d;++ foreach (var binding in faceBindings)+ {+ var surface = body.Geometry.GetSurface(binding.SurfaceGeometryId);+ if (surface.Kind != SurfaceGeometryKind.Plane || surface.Plane is null)+ {+ reason = "box recognition requires planar surfaces for all faces.";+ return false;+ }++ var normal = surface.Plane.Value.Normal;+ if (ToleranceMath.AlmostEqual(normal.X, -1d, tolerance) && ToleranceMath.AlmostZero(normal.Y, tolerance) && ToleranceMath.AlmostZero(normal.Z, tolerance))+ {+ minX = surface.Plane.Value.Origin.X;+ hasXMin = true;+ }+ else if (ToleranceMath.AlmostEqual(normal.X, 1d, tolerance) && ToleranceMath.AlmostZero(normal.Y, tolerance) && ToleranceMath.AlmostZero(normal.Z, tolerance))+ {+ maxX = surface.Plane.Value.Origin.X;+ hasXMax = true;+ }+ else if (ToleranceMath.AlmostEqual(normal.Y, -1d, tolerance) && ToleranceMath.AlmostZero(normal.X, tolerance) && ToleranceMath.AlmostZero(normal.Z, tolerance))+ {+ minY = surface.Plane.Value.Origin.Y;+ hasYMin = true;+ }+ else if (ToleranceMath.AlmostEqual(normal.Y, 1d, tolerance) && ToleranceMath.AlmostZero(normal.X, tolerance) && ToleranceMath.AlmostZero(normal.Z, tolerance))+ {+ maxY = surface.Plane.Value.Origin.Y;+ hasYMax = true;+ }+ else if (ToleranceMath.AlmostEqual(normal.Z, -1d, tolerance) && ToleranceMath.AlmostZero(normal.X, tolerance) && ToleranceMath.AlmostZero(normal.Y, tolerance))+ {+ minZ = surface.Plane.Value.Origin.Z;+ hasZMin = true;+ }+ else if (ToleranceMath.AlmostEqual(normal.Z, 1d, tolerance) && ToleranceMath.AlmostZero(normal.X, tolerance) && ToleranceMath.AlmostZero(normal.Y, tolerance))+ {+ maxZ = surface.Plane.Value.Origin.Z;+ hasZMax = true;+ }+ else+ {+ reason = "face normals are not axis-aligned as required for M13 boxes.";+ return false;+ }+ }++ if (!(hasXMin && hasXMax && hasYMin && hasYMax && hasZMin && hasZMax))+ {+ reason = "box recognition requires ±X/±Y/±Z face normals.";+ return false;+ }++ extents = new AxisAlignedBoxExtents(minX, maxX, minY, maxY, minZ, maxZ);+ if (!extents.HasPositiveVolume(tolerance))+ {+ reason = "recognized extents do not have positive volume.";+ return false;+ }++ return true;+ }++ public static KernelResult<BrepBody> CreateBoxFromExtents(AxisAlignedBoxExtents extents)+ {+ var width = extents.MaxX - extents.MinX;+ var height = extents.MaxY - extents.MinY;+ var depth = extents.MaxZ - extents.MinZ;+ var create = BrepPrimitives.CreateBox(width, height, depth);+ if (!create.IsSuccess)+ {+ return create;+ }++ var center = new Vector3D((extents.MinX + extents.MaxX) * 0.5d, (extents.MinY + extents.MaxY) * 0.5d, (extents.MinZ + extents.MaxZ) * 0.5d);+ var translatedGeometry = new BrepGeometryStore();++ foreach (var curve in create.Value.Geometry.Curves)+ {+ translatedGeometry.AddCurve(curve.Key, curve.Value.Kind == CurveGeometryKind.Line3 && curve.Value.Line3 is Line3Curve line+ ? CurveGeometry.FromLine(new Line3Curve(line.Origin + center, line.Direction))+ : curve.Value);+ }++ foreach (var surface in create.Value.Geometry.Surfaces)+ {+ translatedGeometry.AddSurface(surface.Key, surface.Value.Kind == SurfaceGeometryKind.Plane && surface.Value.Plane is PlaneSurface plane+ ? SurfaceGeometry.FromPlane(new PlaneSurface(plane.Origin + center, plane.Normal, plane.UAxis))+ : surface.Value);+ }++ return KernelResult<BrepBody>.Success(new BrepBody(create.Value.Topology, translatedGeometry, create.Value.Bindings), create.Diagnostics);+ }+}diff --git a/docs/milestones.md b/docs/milestones.mdindex 31c47fb8..dee042ef 100644--- a/docs/milestones.md+++ b/docs/milestones.md@@ -105,4 +105,14 @@- [x] Support tiny end-to-end identity shortcuts for same-instance `Union` and `Intersect`; outputs are validated in strict mode via `BrepBindingValidator` before success is returned.- [x] Return deterministic structured `NotImplemented` diagnostics for all non-supported M12 boolean requests, including same-instance `Subtract` (empty-body representation deferred).- [x] Add focused unit tests locking supported shortcuts, unsupported behavior, deterministic diagnostics, and no-throw behavior for routine unsupported cases.-- [x] Explicitly defer general B-rep/B-rep intersection, fragment splitting/classification, and robust rebuild/healing to M13.+- [x] Explicitly defer general B-rep/B-rep intersection, fragment splitting/classification, and robust rebuild/healing to later milestones.+++## M13 — Boolean Ops v1 (Real but Narrow, Axis-Aligned Box/Box Subset)++- [x] Extend the staged boolean pipeline with real support for recognized axis-aligned box vs axis-aligned box inputs (from `BrepPrimitives.CreateBox(...)`).+- [x] Support `Intersect` only for positive-volume overlap (single-box result); disjoint and touching-only contact return deterministic `NotImplemented` diagnostics because empty/non-solid results are not representable in M13.+- [x] Support `Union` for containing cases and exact single-box outcomes only; disjoint unions and non-single-box unions (for example L-shaped overlap unions) return deterministic `NotImplemented`.+- [x] Support `Subtract` for no-overlap passthrough, full-removal empty-result diagnostics, and exact single-box clip outcomes; non-single-box subtraction remains `NotImplemented`.+- [x] Rebuild supported outputs via `BrepPrimitives.CreateBox(...)` plus placement translation and validate results with strict `BrepBindingValidator`.+- [x] Explicitly defer rotated boxes, non-box primitives, multi-body/disjoint boolean outputs, arbitrary B-rep intersection/splitting, and general boolean robustness.