Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
G2: allow bounded subtract continuation after safe add roots
Yuechen Lic499fae03e47
diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentBooleanCanonicalSuccessTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentBooleanCanonicalSuccessTests.csindex 944355dd..9e1a0f68 100644--- a/Aetheris.Kernel.Firmament.Tests/FirmamentBooleanCanonicalSuccessTests.cs+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentBooleanCanonicalSuccessTests.cs@@ -14,7 +14,8 @@ public sealed class FirmamentBooleanCanonicalSuccessTests{ "testdata/firmament/examples/boolean_box_cylinder_hole.firmament", "hole", FirmamentLoweredBooleanKind.Subtract },{ "testdata/firmament/examples/boolean_box_cone_throughhole_basic.firmament", "cut", FirmamentLoweredBooleanKind.Subtract },{ "testdata/firmament/examples/boolean_two_cylinder_holes_basic.firmament", "hole_b", FirmamentLoweredBooleanKind.Subtract },- { "testdata/firmament/examples/boolean_cylinder_cone_holes_basic.firmament", "cut_b", FirmamentLoweredBooleanKind.Subtract }+ { "testdata/firmament/examples/boolean_cylinder_cone_holes_basic.firmament", "cut_b", FirmamentLoweredBooleanKind.Subtract },+ { "testdata/firmament/fixtures/m13b-invalid-composed-reenter-safe-family.firmament", "hole", FirmamentLoweredBooleanKind.Subtract }};[Theory]@@ -42,6 +43,7 @@ public sealed class FirmamentBooleanCanonicalSuccessTests[InlineData("testdata/firmament/examples/boolean_box_cone_throughhole_basic.firmament", "cut", 1, "subtract")][InlineData("testdata/firmament/examples/boolean_two_cylinder_holes_basic.firmament", "hole_b", 2, "subtract")][InlineData("testdata/firmament/examples/boolean_cylinder_cone_holes_basic.firmament", "cut_b", 2, "subtract")]+ [InlineData("testdata/firmament/fixtures/m13b-invalid-composed-reenter-safe-family.firmament", "hole", 2, "subtract")]public void CanonicalBooleanExamples_Export_Deterministically_WithExpectedMarkers(string fixturePath, string expectedFeatureId, int expectedOpIndex, string expectedKind){var first = ExportFixture(fixturePath);@@ -189,7 +191,6 @@ public sealed class FirmamentBooleanCanonicalSuccessTests[InlineData("testdata/firmament/fixtures/m13a-unsupported-composed-add-ordering.firmament", "joined", "cannot continue the safe subtract chain rooted at 'hole_a'")][InlineData("testdata/firmament/fixtures/m13a-unsupported-composed-subtract-sphere.firmament", "cavity", "unsupported follow-on tool kind 'sphere'")][InlineData("testdata/firmament/fixtures/m13a-unsupported-composed-subtract-box.firmament", "notch", "unsupported follow-on tool kind 'box'")]- [InlineData("testdata/firmament/fixtures/m13b-invalid-composed-reenter-safe-family.firmament", "hole", "cannot re-enter the safe subtract family from 'joined'")]public void SequentialCompositionOutsideSafeSubset_RemainsRejectedWithoutFallback(string fixturePath, string expectedFeatureId, string expectedMessageFragment){var result = FirmamentCorpusHarness.Compile(FirmamentCorpusHarness.ReadFixtureText(fixturePath));diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentBuildAndExportTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentBuildAndExportTests.csindex 94ccee78..f7ef2367 100644--- a/Aetheris.Kernel.Firmament.Tests/FirmamentBuildAndExportTests.cs+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentBuildAndExportTests.cs@@ -353,6 +353,18 @@ public sealed class FirmamentBuildAndExportTests"subtract");}+ [Fact]+ public void Run_AddThenSubtract_ReentrySafeRootFixture_Builds_And_Writes_Export()+ {+ AssertExampleBuildAndExport(+ "testdata/firmament/fixtures/m13b-invalid-composed-reenter-safe-family.firmament",+ "m13b-invalid-composed-reenter-safe-family.step",+ "hole",+ 2,+ "boolean",+ "subtract");+ }+[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")]@@ -366,7 +378,6 @@ public sealed class FirmamentBuildAndExportTests[InlineData("testdata/firmament/fixtures/m13a-unsupported-composed-add-ordering.firmament", "m13a-unsupported-composed-add-ordering.step", "joined", "add")][InlineData("testdata/firmament/fixtures/m13a-unsupported-composed-subtract-sphere.firmament", "m13a-unsupported-composed-subtract-sphere.step", "cavity", "subtract")][InlineData("testdata/firmament/fixtures/m13a-unsupported-composed-subtract-box.firmament", "m13a-unsupported-composed-subtract-box.step", "notch", "subtract")]- [InlineData("testdata/firmament/fixtures/m13b-invalid-composed-reenter-safe-family.firmament", "m13b-invalid-composed-reenter-safe-family.step", "hole", "subtract")][InlineData("testdata/firmament/fixtures/m13c-unsupported-cylinder-root-follow-on-hole.firmament", "m13c-unsupported-cylinder-root-follow-on-hole.step", "bolt_hole", "subtract")]public void Run_Unsupported_MixedPrimitive_Fixtures_Fail_And_Do_Not_Write_Fallback_Export(string sourcePath,diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentSafeSubtractFeatureGraphValidationTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentSafeSubtractFeatureGraphValidationTests.csindex 4effb798..96e9032d 100644--- a/Aetheris.Kernel.Firmament.Tests/FirmamentSafeSubtractFeatureGraphValidationTests.cs+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentSafeSubtractFeatureGraphValidationTests.cs@@ -1,4 +1,6 @@using Aetheris.Kernel.Core.Diagnostics;+using Aetheris.Kernel.Firmament.Lowering;+using Aetheris.Kernel.Firmament.Validation;namespace Aetheris.Kernel.Firmament.Tests;@@ -22,7 +24,6 @@ public sealed class FirmamentSafeSubtractFeatureGraphValidationTests[InlineData("testdata/firmament/fixtures/m13a-unsupported-tangent-composed-holes.firmament", KernelDiagnosticCode.NotImplemented, "Boolean feature 'hole_b' (subtract) would be tangent to previously accepted hole <unknown>", "BrepBoolean.AnalyticHole.TangentContact")][InlineData("testdata/firmament/fixtures/m13a-unsupported-composed-subtract-sphere.firmament", KernelDiagnosticCode.ValidationFailed, "Boolean feature 'cavity' (subtract) uses unsupported follow-on tool kind 'sphere'", "firmament.feature-graph.unsupported-follow-on-kind")][InlineData("testdata/firmament/fixtures/m13a-unsupported-composed-subtract-box.firmament", KernelDiagnosticCode.ValidationFailed, "Boolean feature 'notch' (subtract) uses unsupported follow-on tool kind 'box'", "firmament.feature-graph.unsupported-follow-on-kind")]- [InlineData("testdata/firmament/fixtures/m13b-invalid-composed-reenter-safe-family.firmament", KernelDiagnosticCode.ValidationFailed, "Boolean feature 'hole' (subtract) cannot re-enter the safe subtract family from 'joined'", "firmament.feature-graph.invalid-composition-order")]public void FeatureGraphValidator_FailsDeterministically_ForUnsupportedSequentialComposition(string fixturePath,KernelDiagnosticCode expectedCode,@@ -39,4 +40,38 @@ public sealed class FirmamentSafeSubtractFeatureGraphValidationTestsAssert.DoesNotContain(result.Compilation.Diagnostics, diagnostic =>diagnostic.Message.Contains("requires at least one executed primitive or boolean body", StringComparison.Ordinal));}++ [Fact]+ public void FeatureGraphValidator_Rejects_AdditiveRootOutsideRecognizedSafeFamily_WithSpecificDiagnostic()+ {+ var boolean = new FirmamentLoweredBoolean(+ OpIndex: 2,+ FeatureId: "hole",+ Kind: FirmamentLoweredBooleanKind.Subtract,+ PrimaryReferenceField: "from",+ PrimaryReferenceFeatureId: "joined",+ Tool: new FirmamentLoweredToolOp(+ "cylinder",+ new Dictionary<string, string>(StringComparer.Ordinal)+ {+ ["radius"] = "2",+ ["height"] = "20"+ },+ RawValue: string.Empty),+ Placement: null);++ var result = FirmamentSafeSubtractFeatureGraphValidator.ValidateNextBoolean(+ boolean,+ new Dictionary<string, FirmamentSafeSubtractFeatureGraphState>(StringComparer.Ordinal)+ {+ ["joined"] = FirmamentSafeSubtractFeatureGraphState.BoundedOrthogonalAdditiveOutsideSafeRoot+ },+ new Dictionary<string, Aetheris.Kernel.Core.Brep.BrepBody>());++ Assert.False(result.IsSuccess);+ Assert.Contains(result.Diagnostics, diagnostic =>+ diagnostic.Code == KernelDiagnosticCode.ValidationFailed+ && diagnostic.Source == "firmament.feature-graph.unrecognized-additive-root"+ && diagnostic.Message.Contains("prior add result is not a recognized bounded orthogonal safe subtract root", StringComparison.Ordinal));+ }}diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentStepExporterTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentStepExporterTests.csindex c1b01803..70f78913 100644--- a/Aetheris.Kernel.Firmament.Tests/FirmamentStepExporterTests.cs+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentStepExporterTests.cs@@ -225,6 +225,18 @@ public sealed class FirmamentStepExporterTestsAssert.Contains("BREP_WITH_VOIDS", first.Value.StepText, StringComparison.Ordinal);}+ [Fact]+ public void Export_AddThenSubtract_ReentrySafeRootFixture_Succeeds_Deterministically_Without_Fallback()+ {+ var first = ExportFixture("testdata/firmament/fixtures/m13b-invalid-composed-reenter-safe-family.firmament");+ var second = ExportFixture("testdata/firmament/fixtures/m13b-invalid-composed-reenter-safe-family.firmament");++ Assert.True(first.IsSuccess);+ Assert.True(second.IsSuccess);+ Assert.Equal(first.Value.StepText, second.Value.StepText);+ Assert.Contains("CYLINDRICAL_SURFACE", first.Value.StepText, StringComparison.Ordinal);+ }+[Theory][InlineData("testdata/firmament/fixtures/m10l-unsupported-box-subtract-sphere-touching-boundary.firmament", "tangent_cavity")]@@ -242,7 +254,6 @@ public sealed class FirmamentStepExporterTests[InlineData("testdata/firmament/fixtures/m13a-unsupported-composed-add-ordering.firmament", "joined")][InlineData("testdata/firmament/fixtures/m13a-unsupported-composed-subtract-sphere.firmament", "cavity")][InlineData("testdata/firmament/fixtures/m13a-unsupported-composed-subtract-box.firmament", "notch")]- [InlineData("testdata/firmament/fixtures/m13b-invalid-composed-reenter-safe-family.firmament", "hole")]public void Export_UnsupportedMixedPrimitiveBooleanFixtures_Fail_Loudly_Without_Fallback(string fixturePath, string expectedFeatureId){var first = ExportFixture(fixturePath);diff --git a/Aetheris.Kernel.Firmament/Execution/FirmamentPrimitiveExecutor.cs b/Aetheris.Kernel.Firmament/Execution/FirmamentPrimitiveExecutor.csindex 282d270b..0989b103 100644--- a/Aetheris.Kernel.Firmament/Execution/FirmamentPrimitiveExecutor.cs+++ b/Aetheris.Kernel.Firmament/Execution/FirmamentPrimitiveExecutor.cs@@ -26,6 +26,8 @@ internal static class FirmamentPrimitiveExecutorvar booleanExecutionBodiesByFeatureId = new Dictionary<string, BrepBody>(StringComparer.Ordinal);var featureGraphStates = new Dictionary<string, FirmamentSafeSubtractFeatureGraphState>(StringComparer.Ordinal);+ var tolerance = ToleranceContext.Default;+foreach (var primitive in loweringPlan.Primitives.OrderBy(p => p.OpIndex)){var bodyResult = ExecutePrimitive(primitive, publishedBodiesByFeatureId);@@ -95,7 +97,11 @@ internal static class FirmamentPrimitiveExecutorexecutedBooleans.Add(new FirmamentExecutedBoolean(boolean.OpIndex, boolean.FeatureId, boolean.Kind, patternBooleanBody));publishedBodiesByFeatureId[boolean.FeatureId] = patternBooleanBody;booleanExecutionBodiesByFeatureId[boolean.FeatureId] = patternBooleanBody;- featureGraphStates[boolean.FeatureId] = patternGraphValidation.Value.ResultState;+ featureGraphStates[boolean.FeatureId] = DetermineBooleanFeatureGraphState(+ boolean,+ patternGraphValidation.Value.ResultState,+ patternBooleanBody,+ tolerance);continue;}@@ -161,7 +167,11 @@ internal static class FirmamentPrimitiveExecutorexecutedBooleans.Add(new FirmamentExecutedBoolean(boolean.OpIndex, boolean.FeatureId, boolean.Kind, placedBooleanBody));publishedBodiesByFeatureId[boolean.FeatureId] = placedBooleanBody;booleanExecutionBodiesByFeatureId[boolean.FeatureId] = placedBooleanBody;- featureGraphStates[boolean.FeatureId] = featureGraphValidation.Value.ResultState;+ featureGraphStates[boolean.FeatureId] = DetermineBooleanFeatureGraphState(+ boolean,+ featureGraphValidation.Value.ResultState,+ placedBooleanBody,+ tolerance);}return KernelResult<FirmamentPrimitiveExecutionResult>.Success(new FirmamentPrimitiveExecutionResult(executedPrimitives, executedBooleans));@@ -269,6 +279,27 @@ internal static class FirmamentPrimitiveExecutor_ => KernelResult<BrepBody>.Failure([new KernelDiagnostic(KernelDiagnosticCode.NotImplemented, KernelDiagnosticSeverity.Error, $"Boolean execution for kind '{kind}' is not implemented.")])};+ private static FirmamentSafeSubtractFeatureGraphState DetermineBooleanFeatureGraphState(+ FirmamentLoweredBoolean boolean,+ FirmamentSafeSubtractFeatureGraphState validatedResultState,+ BrepBody resultBody,+ ToleranceContext tolerance)+ {+ if (validatedResultState == FirmamentSafeSubtractFeatureGraphState.SafeSubtractComposition)+ {+ return validatedResultState;+ }++ if (boolean.Kind == FirmamentLoweredBooleanKind.Add)+ {+ return BrepBooleanSafeComposition.TryRecognize(resultBody, tolerance, out _, out _)+ ? FirmamentSafeSubtractFeatureGraphState.BoundedOrthogonalAdditiveSafeRoot+ : FirmamentSafeSubtractFeatureGraphState.BoundedOrthogonalAdditiveOutsideSafeRoot;+ }++ return validatedResultState;+ }+private static BrepBody ApplyDefaultToolLocalFrame(FirmamentLoweredToolOp tool, BrepBody body){if (string.Equals(tool.OpName, "box", StringComparison.Ordinal)diff --git a/Aetheris.Kernel.Firmament/Validation/FirmamentSafeSubtractFeatureGraphValidator.cs b/Aetheris.Kernel.Firmament/Validation/FirmamentSafeSubtractFeatureGraphValidator.csindex 319a3497..caa486ed 100644--- a/Aetheris.Kernel.Firmament/Validation/FirmamentSafeSubtractFeatureGraphValidator.cs+++ b/Aetheris.Kernel.Firmament/Validation/FirmamentSafeSubtractFeatureGraphValidator.cs@@ -48,7 +48,9 @@ internal static class FirmamentSafeSubtractFeatureGraphValidatorreturn ValidateSupportedSafeSubtract(boolean, executionBodiesByFeatureId, tolerance, resolvedToolBody);}- if (sourceState is FirmamentSafeSubtractFeatureGraphState.BoxRoot or FirmamentSafeSubtractFeatureGraphState.CylinderRoot)+ if (sourceState is FirmamentSafeSubtractFeatureGraphState.BoxRoot+ or FirmamentSafeSubtractFeatureGraphState.CylinderRoot+ or FirmamentSafeSubtractFeatureGraphState.BoundedOrthogonalAdditiveSafeRoot){if (boolean.Kind == FirmamentLoweredBooleanKind.Subtract && usesSupportedSafeHoleTool){@@ -61,6 +63,16 @@ internal static class FirmamentSafeSubtractFeatureGraphValidatorResultState: FirmamentSafeSubtractFeatureGraphState.Other));}+ if (sourceState == FirmamentSafeSubtractFeatureGraphState.BoundedOrthogonalAdditiveOutsideSafeRoot+ && boolean.Kind == FirmamentLoweredBooleanKind.Subtract+ && usesSupportedSafeHoleTool)+ {+ return Failure(+ KernelDiagnosticCode.ValidationFailed,+ $"Boolean feature '{boolean.FeatureId}' (subtract) cannot continue from '{boolean.PrimaryReferenceFeatureId}' because that prior add result is not a recognized bounded orthogonal safe subtract root.",+ "firmament.feature-graph.unrecognized-additive-root");+ }+if (boolean.Kind == FirmamentLoweredBooleanKind.Subtract && usesSupportedSafeHoleTool){return Failure(@@ -159,6 +171,8 @@ internal enum FirmamentSafeSubtractFeatureGraphState{BoxRoot,CylinderRoot,+ BoundedOrthogonalAdditiveSafeRoot,+ BoundedOrthogonalAdditiveOutsideSafeRoot,SafeSubtractComposition,Other}