Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
Add adaptive planar loop-role circular sampling diagnostics
Yuechen Li650fa4d8cf53
diff --git a/Aetheris.Kernel.Core.Tests/Step242/Step242ConicalSurfaceRegressionTests.cs b/Aetheris.Kernel.Core.Tests/Step242/Step242ConicalSurfaceRegressionTests.csindex 43e2b990..0fc06bcc 100644--- a/Aetheris.Kernel.Core.Tests/Step242/Step242ConicalSurfaceRegressionTests.cs+++ b/Aetheris.Kernel.Core.Tests/Step242/Step242ConicalSurfaceRegressionTests.cs@@ -72,7 +72,7 @@ public sealed class Step242ConicalSurfaceRegressionTests[InlineData("testdata/step242/nist/STC/nist_stc_10_asme1_ap242-e2.stp","importer-topology",- "Importer.LoopRole.InnerBoundaryIntersectionWithOutsideVerticesAfterNormalization",+ "Importer.LoopRole.InnerDisjointAfterNormalization","Inner loop could not be normalized")]public void Step242_NistTargets_AdvancePastCircleTrim_AndKeepDeterministicNextBlocker(string relativePath,diff --git a/Aetheris.Kernel.Core.Tests/Step242/Step242EdgeCurveSenseRegressionTests.cs b/Aetheris.Kernel.Core.Tests/Step242/Step242EdgeCurveSenseRegressionTests.csindex 59bfb14f..d6e5bf48 100644--- a/Aetheris.Kernel.Core.Tests/Step242/Step242EdgeCurveSenseRegressionTests.cs+++ b/Aetheris.Kernel.Core.Tests/Step242/Step242EdgeCurveSenseRegressionTests.cs@@ -6,7 +6,7 @@ public sealed class Step242EdgeCurveSenseRegressionTests{[Theory][InlineData("testdata/step242/nist/FTC/nist_ftc_08_asme1_ap242-e2.stp", "Importer.LoopRole.CylinderNonNormalizableDegenerateProjection", "Cylinder loop normalization failed")]- [InlineData("testdata/step242/nist/FTC/nist_ftc_11_asme1_ap242-e2.stp", "Importer.LoopRole.InnerBoundaryIntersectionWithOutsideVerticesAfterNormalization", "Inner loop could not be normalized")]+ [InlineData("testdata/step242/nist/FTC/nist_ftc_11_asme1_ap242-e2.stp", "Importer.LoopRole.CylinderNonNormalizableDegenerateProjection", "Cylinder loop normalization failed")]public void Step242_NistEdgeCurveSenseTargets_AdvanceToNextBlocker_Deterministically(string relativePath,string expectedSource,diff --git a/Aetheris.Kernel.Core.Tests/Step242/Step242LoopRoleNormalizationRegressionTests.cs b/Aetheris.Kernel.Core.Tests/Step242/Step242LoopRoleNormalizationRegressionTests.csindex 7c59193d..3f8c0db5 100644--- a/Aetheris.Kernel.Core.Tests/Step242/Step242LoopRoleNormalizationRegressionTests.cs+++ b/Aetheris.Kernel.Core.Tests/Step242/Step242LoopRoleNormalizationRegressionTests.cs@@ -6,10 +6,10 @@ public sealed class Step242LoopRoleNormalizationRegressionTests{[Theory][InlineData("testdata/step242/nist/CTC/nist_ctc_04_asme1_ap242-e1.stp", "Importer.LoopRole.InnerBoundaryIntersectionWithOutsideVerticesAfterNormalization", "Inner loop could not be normalized")]- [InlineData("testdata/step242/nist/CTC/nist_ctc_05_asme1_ap242-e1.stp", "Importer.LoopRole.InnerBoundaryIntersectionWithOutsideVerticesAfterNormalization", "Inner loop could not be normalized")]+ [InlineData("testdata/step242/nist/CTC/nist_ctc_05_asme1_ap242-e1.stp", "Entity:1234", "FACE_BOUND loop type 'VERTEX_LOOP' is unsupported")][InlineData("testdata/step242/nist/FTC/nist_ftc_07_asme1_ap242-e2.stp", "Importer.LoopRole.InnerBoundaryIntersectionWithOutsideVerticesAfterNormalization", "Inner loop could not be normalized")][InlineData("testdata/step242/nist/FTC/nist_ftc_10_asme1_ap242-e2.stp", "Importer.LoopRole.InnerBoundaryIntersectionWithOutsideVerticesAfterNormalization", "Inner loop could not be normalized")]- [InlineData("testdata/step242/nist/FTC/nist_ftc_11_asme1_ap242-e2.stp", "Importer.LoopRole.InnerBoundaryIntersectionWithOutsideVerticesAfterNormalization", "Inner loop could not be normalized")]+ [InlineData("testdata/step242/nist/FTC/nist_ftc_11_asme1_ap242-e2.stp", "Importer.LoopRole.CylinderNonNormalizableDegenerateProjection", "Cylinder loop normalization failed")][InlineData("testdata/step242/nist/STC/nist_stc_06_asme1_ap242-e3.stp", null, null)][InlineData("testdata/step242/nist/STC/nist_stc_09_asme1_ap242-e3.stp", null, null)]public void Step242_NistLoopRoleTargets_ProgressDeterministically_WithExplicitNormalizedBoundaries(@@ -47,4 +47,39 @@ public sealed class Step242LoopRoleNormalizationRegressionTestsAssert.Equal(first.FirstDiagnostic.Source, second.FirstDiagnostic.Source);Assert.Equal(first.FirstDiagnostic.MessagePrefix, second.FirstDiagnostic.MessagePrefix);}++ [Theory]+ [InlineData("testdata/step242/nist/CTC/nist_ctc_04_asme1_ap242-e1.stp")]+ [InlineData("testdata/step242/nist/CTC/nist_ctc_05_asme1_ap242-e1.stp")]+ [InlineData("testdata/step242/nist/FTC/nist_ftc_07_asme1_ap242-e2.stp")]+ [InlineData("testdata/step242/nist/FTC/nist_ftc_10_asme1_ap242-e2.stp")]+ [InlineData("testdata/step242/nist/FTC/nist_ftc_11_asme1_ap242-e2.stp")]+ public void Step242_PlanarLoopRole_CircularSampling_IsAdaptiveAndDeterministic(string relativePath)+ {+ var first = CaptureCircularSampling(relativePath);+ var second = CaptureCircularSampling(relativePath);++ Assert.NotEmpty(first);+ Assert.Equal(first, second);++ Assert.All(first, s =>+ {+ Assert.True(s.AdaptivePointCount >= s.LegacyPointCount);+ var expected = System.Math.Max(2, (int)System.Math.Ceiling(System.Math.Abs(s.TrimSpan) / (System.Math.PI / 4d))) + 1;+ Assert.Equal(expected, s.AdaptivePointCount);+ });++ Assert.Contains(first, s => s.AdaptivePointCount > s.LegacyPointCount);+ }++ private static IReadOnlyList<Step242Importer.LoopRoleCircularSamplingDiagnostic> CaptureCircularSampling(string relativePath)+ {+ var absolutePath = Path.Combine(Step242CorpusManifestRunner.RepoRoot(), relativePath.Replace('/', Path.DirectorySeparatorChar));+ var text = File.ReadAllText(absolutePath);+ var diagnostics = new List<Step242Importer.LoopRoleCircularSamplingDiagnostic>();++ using var captureScope = Step242Importer.CaptureLoopRoleCircularSamplingDiagnostics(diagnostics);+ Step242Importer.ImportBody(text);+ return diagnostics;+ }}diff --git a/Aetheris.Kernel.Core/Step242/Step242Importer.cs b/Aetheris.Kernel.Core/Step242/Step242Importer.csindex 5aea01a9..91383abd 100644--- a/Aetheris.Kernel.Core/Step242/Step242Importer.cs+++ b/Aetheris.Kernel.Core/Step242/Step242Importer.cs@@ -6,6 +6,7 @@ using Aetheris.Kernel.Core.Geometry.Surfaces;using Aetheris.Kernel.Core.Math;using Aetheris.Kernel.Core.Results;using Aetheris.Kernel.Core.Topology;+using System.Threading;namespace Aetheris.Kernel.Core.Step242;@@ -15,6 +16,14 @@ public static class Step242Importerprivate const double PointOnSurfaceEps = 1e-5d;private const double AngleUnwrapEps = 1e-8d;private const double ContainmentEps = 1e-8d;+ private static readonly AsyncLocal<ICollection<LoopRoleCircularSamplingDiagnostic>?> CircularSamplingDiagnosticsSink = new();++ public static IDisposable CaptureLoopRoleCircularSamplingDiagnostics(ICollection<LoopRoleCircularSamplingDiagnostic> sink)+ {+ var previous = CircularSamplingDiagnosticsSink.Value;+ CircularSamplingDiagnosticsSink.Value = sink;+ return new CircularSamplingDiagnosticsScope(previous);+ }public static KernelResult<BrepBody> ImportBody(string stepText){@@ -304,7 +313,14 @@ public static class Step242ImporterIsReversed: isReversed);loopCoedges.Add(coedge);- var sampleResult = SampleCoedgePoints(bindings.GetEdgeBinding(edgeIdResult.Value), geometry, coedge.IsReversed);+ var sampleResult = SampleCoedgePoints(+ bindings.GetEdgeBinding(edgeIdResult.Value),+ geometry,+ coedge.IsReversed,+ faceEntity.Id,+ loopId.Value,+ coedge.Id.Value,+ edgeIdResult.Value.Value);if (!sampleResult.IsSuccess){return KernelResult<BrepBody>.Failure(sampleResult.Diagnostics);@@ -1692,7 +1708,14 @@ public static class Step242Importer}}- private static KernelResult<IReadOnlyList<Point3D>> SampleCoedgePoints(EdgeGeometryBinding edgeBinding, BrepGeometryStore geometry, bool isReversed)+ private static KernelResult<IReadOnlyList<Point3D>> SampleCoedgePoints(+ EdgeGeometryBinding edgeBinding,+ BrepGeometryStore geometry,+ bool isReversed,+ int faceEntityId,+ int loopId,+ int coedgeId,+ int edgeId){if (!geometry.TryGetCurve(edgeBinding.CurveGeometryId, out var curve) || curve is null){@@ -1711,24 +1734,21 @@ public static class Step242Importervar circle = curve.Circle3!.Value;var trim = edgeBinding.TrimInterval ?? new ParameterInterval(0d, 2d * double.Pi);var span = trim.End - trim.Start;- if (double.Abs(span - (2d * double.Pi)) <= AngleUnwrapEps)- {- var quarter = trim.Start + (span * 0.25d);- var mid = trim.Start + (span * 0.5d);- var threeQuarter = trim.Start + (span * 0.75d);- points = [- circle.Evaluate(trim.Start),- circle.Evaluate(quarter),- circle.Evaluate(mid),- circle.Evaluate(threeQuarter),- circle.Evaluate(trim.End)- ];- }- else- {- var mid = trim.Start + (span * 0.5d);- points = [circle.Evaluate(trim.Start), circle.Evaluate(mid), circle.Evaluate(trim.End)];- }+ var isFullCircle = double.Abs(span - (2d * double.Pi)) <= AngleUnwrapEps;+ var legacyPointCount = isFullCircle ? 5 : 3;+ var segmentCount = ComputeAdaptiveCircleSegmentCount(span);+ points = SampleCircularTrim(circle, trim, segmentCount);+ ReportCircularSamplingDiagnostic(new LoopRoleCircularSamplingDiagnostic(+ FaceEntityId: faceEntityId,+ LoopId: loopId,+ CoedgeId: coedgeId,+ EdgeId: edgeId,+ TrimStart: trim.Start,+ TrimEnd: trim.End,+ TrimSpan: span,+ IsFullCircle: isFullCircle,+ LegacyPointCount: legacyPointCount,+ AdaptivePointCount: points.Count));break;case CurveGeometryKind.BSpline3:var spline = curve.BSpline3!.Value;@@ -1749,6 +1769,52 @@ public static class Step242Importerreturn KernelResult<IReadOnlyList<Point3D>>.Success(points);}+ private static int ComputeAdaptiveCircleSegmentCount(double span)+ {+ const double maxSegmentAngle = double.Pi / 4d;+ var normalizedSpan = double.Abs(span);+ var segmentCount = (int)double.Ceiling(normalizedSpan / maxSegmentAngle);+ return System.Math.Max(2, segmentCount);+ }++ private static List<Point3D> SampleCircularTrim(Circle3Curve circle, ParameterInterval trim, int segmentCount)+ {+ var points = new List<Point3D>(segmentCount + 1);+ var step = (trim.End - trim.Start) / segmentCount;+ for (var i = 0; i <= segmentCount; i++)+ {+ points.Add(circle.Evaluate(trim.Start + (step * i)));+ }++ return points;+ }++ private static void ReportCircularSamplingDiagnostic(LoopRoleCircularSamplingDiagnostic diagnostic)+ {+ var sink = CircularSamplingDiagnosticsSink.Value;+ sink?.Add(diagnostic);+ }++ public sealed record LoopRoleCircularSamplingDiagnostic(+ int FaceEntityId,+ int LoopId,+ int CoedgeId,+ int EdgeId,+ double TrimStart,+ double TrimEnd,+ double TrimSpan,+ bool IsFullCircle,+ int LegacyPointCount,+ int AdaptivePointCount);++ private sealed class CircularSamplingDiagnosticsScope(ICollection<LoopRoleCircularSamplingDiagnostic>? previous) : IDisposable+ {+ public void Dispose()+ {+ CircularSamplingDiagnosticsSink.Value = previous;+ }+ }+private static KernelResult<T> LoopRoleFailure<T>(string message, string source) =>KernelResult<T>.Failure([new KernelDiagnostic(KernelDiagnosticCode.ValidationFailed, KernelDiagnosticSeverity.Error, message, source)]);diff --git a/testdata/step242/manifests/nist.v0.report.json b/testdata/step242/manifests/nist.v0.report.jsonindex 76c18174..bd92679a 100644--- a/testdata/step242/manifests/nist.v0.report.json+++ b/testdata/step242/manifests/nist.v0.report.json@@ -102,9 +102,9 @@"status": "importFail","firstFailureLayer": "importer-topology","firstDiagnostic": {- "code": "ValidationFailed",- "source": "Importer.LoopRole.InnerBoundaryIntersectionWithOutsideVerticesAfterNormalization",- "messagePrefix": "Inner loop could not be normalized"+ "code": "NotImplemented",+ "source": "Entity:1234",+ "messagePrefix": "FACE_BOUND loop type \u0027VERTEX_LOOP\u0027 is unsupported for non-spherical faces in M23 import subset."},"diagnosticCount": 1,"exceptionEscaped": false,@@ -271,8 +271,8 @@"firstFailureLayer": "importer-topology","firstDiagnostic": {"code": "ValidationFailed",- "source": "Importer.LoopRole.InnerBoundaryIntersectionWithOutsideVerticesAfterNormalization",- "messagePrefix": "Inner loop could not be normalized"+ "source": "Importer.LoopRole.CylinderNonNormalizableDegenerateProjection",+ "messagePrefix": "Cylinder loop normalization failed"},"diagnosticCount": 1,"exceptionEscaped": false,@@ -391,7 +391,7 @@"firstFailureLayer": "importer-topology","firstDiagnostic": {"code": "ValidationFailed",- "source": "Importer.LoopRole.InnerBoundaryIntersectionWithOutsideVerticesAfterNormalization",+ "source": "Importer.LoopRole.InnerDisjointAfterNormalization","messagePrefix": "Inner loop could not be normalized"},"diagnosticCount": 1,