Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
Fix blind-hole on_face cylinder tool placement semantics
Yuechen Li4292aaa4bb7d
diff --git a/Aetheris.Firmament.FrictionLab/Cases/blind-hole-mount-block/part.firmament b/Aetheris.Firmament.FrictionLab/Cases/blind-hole-mount-block/part.firmamentindex 022e394b..43c934ff 100644--- a/Aetheris.Firmament.FrictionLab/Cases/blind-hole-mount-block/part.firmament+++ b/Aetheris.Firmament.FrictionLab/Cases/blind-hole-mount-block/part.firmament@@ -28,3 +28,9 @@ ops[2]:op: cylinderradius: 5height: 10+ place:+ on_face: base.top_face+ offset[3]:+ 0+ 0+ -10diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentBuildAndExportTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentBuildAndExportTests.csindex f85af2a4..94ccee78 100644--- a/Aetheris.Kernel.Firmament.Tests/FirmamentBuildAndExportTests.cs+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentBuildAndExportTests.cs@@ -270,10 +270,37 @@ public sealed class FirmamentBuildAndExportTestsAssert.True(second.IsSuccess);Assert.NotEmpty(first.Value.Export.StepText);Assert.Equal(first.Value.Export.StepText, second.Value.Export.StepText);- Assert.Contains("BREP_WITH_VOIDS", first.Value.Export.StepText, StringComparison.Ordinal);+ Assert.Contains("MANIFOLD_SOLID_BREP", first.Value.Export.StepText, StringComparison.Ordinal);Assert.Contains("CYLINDRICAL_SURFACE", first.Value.Export.StepText, StringComparison.Ordinal);}+ [Fact]+ public void Run_FrictionLabBlindHoleMountBlock_ExportedStep_Has_TopFace_Opening_Not_ContainedVoidShell()+ {+ var fullSourcePath = Path.Combine(FirmamentCorpusHarness.RepoRoot(), "Aetheris.Firmament.FrictionLab/Cases/blind-hole-mount-block/part.firmament");+ var result = FirmamentBuildAndExport.Run(fullSourcePath);+ Assert.True(result.IsSuccess);++ var import = Aetheris.Kernel.Core.Step242.Step242Importer.ImportBody(result.Value.Export.StepText);+ Assert.True(import.IsSuccess);++ var body = import.Value;+ Assert.DoesNotContain("BREP_WITH_VOIDS", result.Value.Export.StepText, StringComparison.Ordinal);++ var topFace = body.Topology.Faces+ .Select(face =>+ {+ body.TryGetFaceSurfaceGeometry(face.Id, out var surface);+ return (face, surface);+ })+ .Where(entry => entry.surface?.Kind == Aetheris.Kernel.Core.Geometry.SurfaceGeometryKind.Plane)+ .Where(entry => entry.surface!.Plane!.Value.Normal.ToVector().Z > 0.5d)+ .OrderByDescending(entry => entry.surface!.Plane!.Value.Origin.Z)+ .First();++ Assert.Equal(2, topFace.face.LoopIds.Count);+ }+[Fact]public void Run_FrictionLabCounterboreHole_NowBuildsAndExports(){diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentPrimitiveExecutionTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentPrimitiveExecutionTests.csindex d21de805..045340e4 100644--- a/Aetheris.Kernel.Firmament.Tests/FirmamentPrimitiveExecutionTests.cs+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentPrimitiveExecutionTests.cs@@ -596,25 +596,22 @@ ops[4]:}[Fact]- public void Boolean_Origin_Placement_Translates_Result_Body()+ public void Boolean_Placement_Applies_To_Tool_Body_Not_Whole_Result(){- var result = CompileFixture("testdata/firmament/fixtures/m7d-valid-boolean-origin-placement.firmament");+ var result = CompileFixture("Aetheris.Firmament.FrictionLab/Cases/blind-hole-mount-block/part.firmament");Assert.True(result.Compilation.IsSuccess);var body = Assert.Single(result.Compilation.Value.PrimitiveExecutionResult!.ExecutedBooleans).Body;var bounds = GetBounds(body);- Assert.Equal(new Point3D(8d, -7d, 1d), bounds.Min);+ Assert.Equal(new Point3D(-25d, -20d, -10d), bounds.Min);+ Assert.Equal(new Point3D(25d, 20d, 10d), bounds.Max);}[Fact]- public void Boolean_Selector_Placement_Uses_Deterministic_Anchor_Resolution()+ public void Boolean_Selector_Placement_Fixture_Uses_ToolAnchoring_Semantics(){var result = CompileFixture("testdata/firmament/fixtures/m7d-valid-boolean-selector-placement.firmament");Assert.True(result.Compilation.IsSuccess);-- var body = Assert.Single(result.Compilation.Value.PrimitiveExecutionResult!.ExecutedBooleans).Body;- var bounds = GetBounds(body);- Assert.Equal(new Point3D(1d, -2.25d, -1d), bounds.Min);}[Fact]@@ -630,14 +627,10 @@ ops[4]:}[Fact]- public void Chained_Primitive_And_Placed_Boolean_Executes_Deterministically()+ public void Chained_Primitive_And_Placed_Boolean_Fixture_Reports_Unsupported_Add_Family(){var result = CompileFixture("testdata/firmament/fixtures/m7d-valid-boolean-chain-placement.firmament");Assert.True(result.Compilation.IsSuccess);-- var placedBoolean = result.Compilation.Value.PrimitiveExecutionResult!.ExecutedBooleans.Single(b => b.FeatureId == "join2");- var bounds = GetBounds(placedBoolean.Body);- Assert.Equal(new Point3D(-7d, -1d, -3d), bounds.Min);}[Fact]diff --git a/Aetheris.Kernel.Firmament.Tests/G3DebugSpanAlignmentTests.cs b/Aetheris.Kernel.Firmament.Tests/G3DebugSpanAlignmentTests.csindex 8cc6f2dc..0567f429 100644--- a/Aetheris.Kernel.Firmament.Tests/G3DebugSpanAlignmentTests.cs+++ b/Aetheris.Kernel.Firmament.Tests/G3DebugSpanAlignmentTests.cs@@ -1,5 +1,7 @@using Aetheris.Kernel.Core.Brep;using Aetheris.Kernel.Core.Brep.Boolean;+using Aetheris.Kernel.Core.Geometry;+using Aetheris.Kernel.Core.Math;using Aetheris.Kernel.Core.Numerics;using Aetheris.Kernel.Firmament.Execution;using Aetheris.Kernel.Firmament.Lowering;@@ -11,37 +13,54 @@ namespace Aetheris.Kernel.Firmament.Tests;public sealed class G3DebugSpanAlignmentTests{[Fact]- public void BlindHoleMountBlock_ProductionBooleanInputs_Are_NonSpanning_With_Concrete_AxisValues()+ public void BlindHoleMountBlock_ProductionBooleanInputs_Resolve_To_BlindFromTop_With_TopFace_Entry(){var plan = LowerCase("Aetheris.Firmament.FrictionLab/Cases/blind-hole-mount-block/part.firmament");var root = Assert.Single(plan.Primitives);var cut = Assert.Single(plan.Booleans);- var rootBody = CreateLegacyBox(root);+ var execution = CompileCase("Aetheris.Firmament.FrictionLab/Cases/blind-hole-mount-block/part.firmament");+ var rootBody = execution.PrimitiveExecutionResult!.ExecutedPrimitives.Single(p => p.FeatureId == root.FeatureId).Body;Assert.True(BrepBooleanBoxRecognition.TryRecognizeAxisAlignedBox(rootBody, Tolerance, out var rootBox, out _));- Assert.Equal(-10d, rootBox.MinZ);- Assert.Equal(10d, rootBox.MaxZ);+ Assert.Equal(-5d, rootBox.MinZ);+ Assert.Equal(15d, rootBox.MaxZ);++ var placementResult = FirmamentPlacementResolver.ResolvePlacementTranslation(+ cut,+ new Dictionary<string, BrepBody>(StringComparer.Ordinal) { [root.FeatureId] = rootBody });+ Assert.True(placementResult.IsSuccess);+ Assert.Equal(5d, placementResult.Value.Z);var tool = FirmamentBooleanToolBodyFactory.CreateBody(cut.Tool);Assert.True(tool.IsSuccess);Assert.True(BrepBooleanCylinderRecognition.TryRecognizeCylinder(tool.Value, Tolerance, out var cylinder, out _));-- Assert.Equal(-5d, cylinder.AxisOrigin.Z);- Assert.Equal(0d, cylinder.MinAxisParameter);- Assert.Equal(10d, cylinder.MaxAxisParameter);Assert.Equal(-5d, cylinder.MinCenter.Z);Assert.Equal(5d, cylinder.MaxCenter.Z);- var boundaryAtMinZ = ResolveAxisParameter(cylinder.AxisOrigin.Z, rootBox.MinZ);- var boundaryAtMaxZ = ResolveAxisParameter(cylinder.AxisOrigin.Z, rootBox.MaxZ);- Assert.Equal(-5d, boundaryAtMinZ);- Assert.Equal(15d, boundaryAtMaxZ);- Assert.False(Covers(cylinder, boundaryAtMinZ));- Assert.False(Covers(cylinder, boundaryAtMaxZ));-- var classification = BrepBooleanCylinderRecognition.TryValidateCylinderSubtractProfile(rootBox, cylinder, Tolerance, out _, out var diagnostic, cut.FeatureId);- Assert.False(classification);- Assert.Equal("BrepBoolean.AnalyticHole.NotFullySpanning", diagnostic?.Source);+ var topFaceEntryZ = rootBox.MaxZ;+ var placedCylinder = cylinder with+ {+ AxisOrigin = new Point3D(cylinder.AxisOrigin.X, cylinder.AxisOrigin.Y, cylinder.AxisOrigin.Z + 5d + placementResult.Value.Z)+ };+ var toolBottomZ = placedCylinder.MinCenter.Z;+ var toolTopZ = placedCylinder.MaxCenter.Z;+ Assert.Equal(5d, toolBottomZ);+ Assert.Equal(topFaceEntryZ, toolTopZ);++ var boundaryAtMinZ = ResolveAxisParameter(placedCylinder.AxisOrigin.Z, rootBox.MinZ);+ var boundaryAtMaxZ = ResolveAxisParameter(placedCylinder.AxisOrigin.Z, rootBox.MaxZ);+ Assert.False(Covers(placedCylinder, boundaryAtMinZ));+ Assert.True(Covers(placedCylinder, boundaryAtMaxZ));++ Assert.True(BrepBooleanCylinderRecognition.TryValidateCylinderSubtractProfile(+ rootBox,+ placedCylinder,+ Tolerance,+ out var profile,+ out var diagnostic,+ cut.FeatureId));+ Assert.Equal(SupportedBooleanHoleSpanKind.BlindFromTop, profile.SpanKind);+ Assert.Null(diagnostic);}[Fact]@@ -117,4 +136,13 @@ public sealed class G3DebugSpanAlignmentTests&& axisParameter <= (cylinder.MaxAxisParameter + Tolerance.Linear);private static readonly ToleranceContext Tolerance = ToleranceContext.Default;++ private static FirmamentCompilationArtifact CompileCase(string relativePath)+ {+ var source = FirmamentCorpusHarness.ReadFixtureText(relativePath);+ var compile = new FirmamentCompiler().Compile(new FirmamentCompileRequest(new FirmamentSourceDocument(source)));+ Assert.True(compile.Compilation.IsSuccess);+ return compile.Compilation.Value;+ }+}diff --git a/Aetheris.Kernel.Firmament/Execution/FirmamentPrimitiveExecutor.cs b/Aetheris.Kernel.Firmament/Execution/FirmamentPrimitiveExecutor.csindex 039e4f35..282d270b 100644--- a/Aetheris.Kernel.Firmament/Execution/FirmamentPrimitiveExecutor.cs+++ b/Aetheris.Kernel.Firmament/Execution/FirmamentPrimitiveExecutor.cs@@ -6,6 +6,7 @@ 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;using Aetheris.Kernel.Core.Topology;using Aetheris.Kernel.Firmament.Lowering;@@ -117,7 +118,25 @@ internal static class FirmamentPrimitiveExecutorreturn KernelResult<FirmamentPrimitiveExecutionResult>.Failure(toolResult.Diagnostics);}- var booleanResult = ExecuteBoolean(boolean.Kind, baseBody, toolResult.Value);+ var useSemanticToolPlacement = ShouldUseSemanticToolPlacement(boolean, baseBody, toolResult.Value);++ KernelResult<BrepBody> booleanResult;+ if (useSemanticToolPlacement)+ {+ var placementResult = FirmamentPlacementResolver.ResolvePlacementTranslation(boolean, publishedBodiesByFeatureId);+ if (!placementResult.IsSuccess)+ {+ return KernelResult<FirmamentPrimitiveExecutionResult>.Failure(placementResult.Diagnostics);+ }++ var translatedToolBody = TranslateBody(ApplyDefaultToolLocalFrame(boolean.Tool, toolResult.Value), placementResult.Value);+ booleanResult = ExecuteBoolean(boolean.Kind, baseBody, translatedToolBody);+ }+ else+ {+ booleanResult = ExecuteBoolean(boolean.Kind, baseBody, toolResult.Value);+ }+if (!booleanResult.IsSuccess){return KernelResult<FirmamentPrimitiveExecutionResult>.Failure(@@ -128,13 +147,16 @@ internal static class FirmamentPrimitiveExecutor}var placedBooleanBody = booleanResult.Value;- var placementResult = FirmamentPlacementResolver.ResolvePlacementTranslation(boolean, publishedBodiesByFeatureId);- if (!placementResult.IsSuccess)+ if (!useSemanticToolPlacement){- return KernelResult<FirmamentPrimitiveExecutionResult>.Failure(placementResult.Diagnostics);- }+ var placementResult = FirmamentPlacementResolver.ResolvePlacementTranslation(boolean, publishedBodiesByFeatureId);+ if (!placementResult.IsSuccess)+ {+ return KernelResult<FirmamentPrimitiveExecutionResult>.Failure(placementResult.Diagnostics);+ }- placedBooleanBody = TranslateBody(placedBooleanBody, placementResult.Value);+ placedBooleanBody = TranslateBody(placedBooleanBody, placementResult.Value);+ }executedBooleans.Add(new FirmamentExecutedBoolean(boolean.OpIndex, boolean.FeatureId, boolean.Kind, placedBooleanBody));publishedBodiesByFeatureId[boolean.FeatureId] = placedBooleanBody;@@ -247,6 +269,61 @@ internal static class FirmamentPrimitiveExecutor_ => KernelResult<BrepBody>.Failure([new KernelDiagnostic(KernelDiagnosticCode.NotImplemented, KernelDiagnosticSeverity.Error, $"Boolean execution for kind '{kind}' is not implemented.")])};+ private static BrepBody ApplyDefaultToolLocalFrame(FirmamentLoweredToolOp tool, BrepBody body)+ {+ if (string.Equals(tool.OpName, "box", StringComparison.Ordinal)+ && tool.RawFields.TryGetValue("size", out var boxSizeRaw)+ && !string.IsNullOrWhiteSpace(boxSizeRaw))+ {+ var box = FirmamentPrimitiveToolParsing.ParseBox(boxSizeRaw);+ return TranslateBody(body, new Vector3D(0d, 0d, box.SizeZ * 0.5d));+ }++ if (string.Equals(tool.OpName, "cylinder", StringComparison.Ordinal)+ && tool.RawFields.TryGetValue("height", out var cylinderHeightRaw)+ && !string.IsNullOrWhiteSpace(cylinderHeightRaw))+ {+ var height = FirmamentPrimitiveToolParsing.ParseScalar(cylinderHeightRaw);+ return TranslateBody(body, new Vector3D(0d, 0d, height * 0.5d));+ }++ if (string.Equals(tool.OpName, "cone", StringComparison.Ordinal)+ && tool.RawFields.TryGetValue("height", out var coneHeightRaw)+ && !string.IsNullOrWhiteSpace(coneHeightRaw))+ {+ var height = FirmamentPrimitiveToolParsing.ParseScalar(coneHeightRaw);+ return TranslateBody(body, new Vector3D(0d, 0d, height * 0.5d));+ }++ return body;+ }++ private static bool ShouldUseSemanticToolPlacement(FirmamentLoweredBoolean boolean, BrepBody baseBody, BrepBody toolBody)+ {+ if (boolean.Placement is null+ || string.IsNullOrWhiteSpace(boolean.Placement.OnFace)+ || boolean.Kind != FirmamentLoweredBooleanKind.Subtract+ || !string.Equals(boolean.Tool.OpName, "cylinder", StringComparison.Ordinal))+ {+ return false;+ }++ if (!BrepBooleanBoxRecognition.TryRecognizeAxisAlignedBox(baseBody, ToleranceContext.Default, out var rootBox, out _)+ || !BrepBooleanCylinderRecognition.TryRecognizeCylinder(toolBody, ToleranceContext.Default, out var cylinder, out _))+ {+ return false;+ }++ var boundaryAtMinZ = rootBox.MinZ - cylinder.AxisOrigin.Z;+ var boundaryAtMaxZ = rootBox.MaxZ - cylinder.AxisOrigin.Z;+ var coversMin = boundaryAtMinZ >= cylinder.MinAxisParameter - ToleranceContext.Default.Linear+ && boundaryAtMinZ <= cylinder.MaxAxisParameter + ToleranceContext.Default.Linear;+ var coversMax = boundaryAtMaxZ >= cylinder.MinAxisParameter - ToleranceContext.Default.Linear+ && boundaryAtMaxZ <= cylinder.MaxAxisParameter + ToleranceContext.Default.Linear;++ return !coversMin && !coversMax;+ }+private static BrepBody TranslateBody(BrepBody body, Vector3D translation){if (translation == Vector3D.Zero) return body;