Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
Run .NET 8 tests and refine cylindrical degenerate loop handling
Yuechen Licee8ce1d3e32
diff --git a/Aetheris.Kernel.Core.Tests/Step242/Step242EdgeCurveSenseRegressionTests.cs b/Aetheris.Kernel.Core.Tests/Step242/Step242EdgeCurveSenseRegressionTests.csindex d6e5bf48..4f2fa26b 100644--- a/Aetheris.Kernel.Core.Tests/Step242/Step242EdgeCurveSenseRegressionTests.cs+++ b/Aetheris.Kernel.Core.Tests/Step242/Step242EdgeCurveSenseRegressionTests.cs@@ -5,12 +5,11 @@ namespace Aetheris.Kernel.Core.Tests.Step242;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.CylinderNonNormalizableDegenerateProjection", "Cylinder loop normalization failed")]+ [InlineData("testdata/step242/nist/FTC/nist_ftc_08_asme1_ap242-e2.stp")]+ [InlineData("testdata/step242/nist/FTC/nist_ftc_11_asme1_ap242-e2.stp")]+ [InlineData("testdata/step242/nist/STC/nist_stc_06_asme1_ap242-e3.stp")]public void Step242_NistEdgeCurveSenseTargets_AdvanceToNextBlocker_Deterministically(- string relativePath,- string expectedSource,- string expectedMessagePrefix)+ string relativePath){var entry = new Step242CorpusManifestEntry(Id: Path.GetFileNameWithoutExtension(relativePath),@@ -26,8 +25,8 @@ public sealed class Step242EdgeCurveSenseRegressionTestsvar second = Step242CorpusManifestRunner.RunOne(entry);Assert.NotEqual("Importer.Orientation.EdgeCurveSense", first.FirstDiagnostic.Source);- Assert.Equal(expectedSource, first.FirstDiagnostic.Source);- Assert.StartsWith(expectedMessagePrefix, first.FirstDiagnostic.MessagePrefix, StringComparison.Ordinal);+ Assert.NotEqual("Importer.LoopRole.CylinderNonNormalizableDegenerateProjection", first.FirstDiagnostic.Source);+ Assert.False(string.IsNullOrWhiteSpace(first.FirstDiagnostic.MessagePrefix));Assert.Equal(first.FirstFailureLayer, second.FirstFailureLayer);Assert.Equal(first.FirstDiagnostic.Source, second.FirstDiagnostic.Source);diff --git a/Aetheris.Kernel.Core.Tests/Step242/Step242LoopRoleNormalizationRegressionTests.cs b/Aetheris.Kernel.Core.Tests/Step242/Step242LoopRoleNormalizationRegressionTests.csindex ecd6520b..907185df 100644--- a/Aetheris.Kernel.Core.Tests/Step242/Step242LoopRoleNormalizationRegressionTests.cs+++ b/Aetheris.Kernel.Core.Tests/Step242/Step242LoopRoleNormalizationRegressionTests.cs@@ -9,7 +9,7 @@ public sealed class Step242LoopRoleNormalizationRegressionTests[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", null, null)][InlineData("testdata/step242/nist/FTC/nist_ftc_10_asme1_ap242-e2.stp", null, null)]- [InlineData("testdata/step242/nist/FTC/nist_ftc_11_asme1_ap242-e2.stp", "Importer.LoopRole.CylinderNonNormalizableDegenerateProjection", "Cylinder loop normalization failed")]+ [InlineData("testdata/step242/nist/FTC/nist_ftc_11_asme1_ap242-e2.stp", null, null)][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(@@ -36,6 +36,7 @@ public sealed class Step242LoopRoleNormalizationRegressionTestsAssert.DoesNotContain("Inner loop is not contained by outer loop.", first.FirstDiagnostic.MessagePrefix, StringComparison.Ordinal);Assert.DoesNotContain("Importer.LoopRole.DegenerateLoop", first.FirstDiagnostic.Source, StringComparison.Ordinal);Assert.DoesNotContain("Importer.LoopRole.InnerNotContained", first.FirstDiagnostic.Source, StringComparison.Ordinal);+ Assert.DoesNotContain("Importer.LoopRole.CylinderNonNormalizableDegenerateProjection", first.FirstDiagnostic.Source, StringComparison.Ordinal);}else{@@ -51,6 +52,19 @@ public sealed class Step242LoopRoleNormalizationRegressionTestsAssert.Equal(first.FirstDiagnostic.MessagePrefix, second.FirstDiagnostic.MessagePrefix);}+ [Theory]+ [InlineData("testdata/step242/nist/FTC/nist_ftc_08_asme1_ap242-e2.stp")]+ [InlineData("testdata/step242/nist/FTC/nist_ftc_11_asme1_ap242-e2.stp")]+ [InlineData("testdata/step242/nist/STC/nist_stc_06_asme1_ap242-e3.stp")]+ public void Step242_CylinderLoopRole_ProjectionDiagnostics_AreDeterministic_AndExplicit(string relativePath)+ {+ var first = CaptureCylinderProjectionDiagnostics(relativePath);+ var second = CaptureCylinderProjectionDiagnostics(relativePath);++ Assert.NotEmpty(first);+ Assert.Equal(first, second);+ Assert.Contains(first, d => !string.Equals(d.Degeneracy, "None", StringComparison.Ordinal));+ }[Theory][InlineData("testdata/step242/nist/CTC/nist_ctc_04_asme1_ap242-e1.stp")]@@ -131,4 +145,15 @@ public sealed class Step242LoopRoleNormalizationRegressionTestsStep242Importer.ImportBody(text);return diagnostics;}++ private static IReadOnlyList<Step242Importer.LoopRoleCylinderProjectionDiagnostic> CaptureCylinderProjectionDiagnostics(string relativePath)+ {+ var absolutePath = Path.Combine(Step242CorpusManifestRunner.RepoRoot(), relativePath.Replace('/', Path.DirectorySeparatorChar));+ var text = File.ReadAllText(absolutePath);+ var diagnostics = new List<Step242Importer.LoopRoleCylinderProjectionDiagnostic>();++ using var captureScope = Step242Importer.CaptureLoopRoleCylinderProjectionDiagnostics(diagnostics);+ Step242Importer.ImportBody(text);+ return diagnostics;+ }}diff --git a/Aetheris.Kernel.Core/Step242/Step242Importer.cs b/Aetheris.Kernel.Core/Step242/Step242Importer.csindex 9bfd0fda..819fbf3a 100644--- a/Aetheris.Kernel.Core/Step242/Step242Importer.cs+++ b/Aetheris.Kernel.Core/Step242/Step242Importer.cs@@ -19,6 +19,7 @@ public static class Step242Importerprivate const double ContainmentEps = 1e-8d;private static readonly AsyncLocal<ICollection<LoopRoleCircularSamplingDiagnostic>?> CircularSamplingDiagnosticsSink = new();private static readonly AsyncLocal<ICollection<LoopRoleCoedgeGapDiagnostic>?> CoedgeGapDiagnosticsSink = new();+ private static readonly AsyncLocal<ICollection<LoopRoleCylinderProjectionDiagnostic>?> CylinderProjectionDiagnosticsSink = new();public static IDisposable CaptureLoopRoleCircularSamplingDiagnostics(ICollection<LoopRoleCircularSamplingDiagnostic> sink){@@ -34,6 +35,13 @@ public static class Step242Importerreturn new CoedgeGapDiagnosticsScope(previous);}+ public static IDisposable CaptureLoopRoleCylinderProjectionDiagnostics(ICollection<LoopRoleCylinderProjectionDiagnostic> sink)+ {+ var previous = CylinderProjectionDiagnosticsSink.Value;+ CylinderProjectionDiagnosticsSink.Value = sink;+ return new CylinderProjectionDiagnosticsScope(previous);+ }+public static KernelResult<BrepBody> ImportBody(string stepText){var parseResult = Step242SubsetParser.Parse(stepText);@@ -374,7 +382,7 @@ public static class Step242ImporterloopData.Add(new LoopBuildData(loopId, loopCoedges, loopSamples, hasDisconnectedCoedgeGap));}- var classifyResult = ClassifyAndNormalizeFaceLoops(loopData, bindSurfaceResult.Value.SurfaceGeometry);+ var classifyResult = ClassifyAndNormalizeFaceLoops(faceEntity.Id, loopData, bindSurfaceResult.Value.SurfaceGeometry);if (!classifyResult.IsSuccess){return KernelResult<BrepBody>.Failure(classifyResult.Diagnostics);@@ -1195,6 +1203,7 @@ public static class Step242Importer}private static KernelResult<IReadOnlyList<LoopBuildData>> ClassifyAndNormalizeFaceLoops(+ int faceEntityId,IReadOnlyList<LoopBuildData> loops,SurfaceGeometry surface){@@ -1206,7 +1215,7 @@ public static class Step242Importerreturn surface.Kind switch{SurfaceGeometryKind.Plane => ClassifyAndNormalizePlanarLoops(loops, surface.Plane!.Value),- SurfaceGeometryKind.Cylinder => ClassifyAndNormalizeCylindricalLoops(loops, surface.Cylinder!.Value),+ SurfaceGeometryKind.Cylinder => ClassifyAndNormalizeCylindricalLoops(faceEntityId, loops, surface.Cylinder!.Value),SurfaceGeometryKind.Cone or SurfaceGeometryKind.Sphere => LoopRoleFailure<IReadOnlyList<LoopBuildData>>("Multi-loop hole classification is unsupported for this surface type.","Importer.LoopRole.UnsupportedSurfaceForHoles"),@@ -1325,6 +1334,7 @@ public static class Step242Importer}private static KernelResult<IReadOnlyList<LoopBuildData>> ClassifyAndNormalizeCylindricalLoops(+ int faceEntityId,IReadOnlyList<LoopBuildData> loops,CylinderSurface cylinder){@@ -1340,8 +1350,22 @@ public static class Step242Importervar projected = ProjectLoopToCylinder(loop.Samples, cylinder);var uniqueCount = CountUniquePoints(projected, ContainmentEps);var area = ComputeSignedArea(projected);+ var analysis = AnalyzeCylindricalProjection(uniqueCount, area, projected);maxUniqueCount = System.Math.Max(maxUniqueCount, uniqueCount);maxAbsArea = System.Math.Max(maxAbsArea, double.Abs(area));++ ReportCylinderProjectionDiagnostic(new LoopRoleCylinderProjectionDiagnostic(+ FaceEntityId: faceEntityId,+ LoopId: loop.LoopId.Value,+ PointCount: projected.Count,+ UniquePointCount: uniqueCount,+ SignedArea: area,+ AngularSpan: analysis.AngularSpan,+ AxialSpan: analysis.AxialSpan,+ SeamCrossings: analysis.SeamCrossings,+ RepeatedSeamPointCount: analysis.RepeatedSeamPointCount,+ Degeneracy: analysis.Degeneracy.ToString()));+if (uniqueCount < 3 || double.Abs(area) <= AreaEps){continue;@@ -1352,7 +1376,40 @@ public static class Step242Importerif (infos.Count == 0){- return LoopRoleFailure<IReadOnlyList<LoopBuildData>>($"Cylinder loop normalization failed: all {loops.Count} loop(s) projected degenerate (maxUnique={maxUniqueCount}, maxAbsArea={maxAbsArea:E6}).", nonNormalizableSource);+ var degenerate = loops+ .Select(loop =>+ {+ var projected = ProjectLoopToCylinder(loop.Samples, cylinder);+ var uniqueCount = CountUniquePoints(projected, ContainmentEps);+ var area = ComputeSignedArea(projected);+ return (Loop: loop, Analysis: AnalyzeCylindricalProjection(uniqueCount, area, projected));+ })+ .OrderBy(d => d.Loop.LoopId.Value)+ .ToArray();++ if (degenerate.All(d => d.Analysis.Degeneracy == CylinderProjectionDegeneracy.FullRevolutionConstantAxial))+ {+ var canonical = degenerate+ .OrderBy(d => d.Loop.LoopId.Value)+ .Select(d => d.Loop)+ .First();+ return KernelResult<IReadOnlyList<LoopBuildData>>.Success([canonical]);+ }++ var primary = degenerate+ .OrderByDescending(d => (int)d.Analysis.Degeneracy)+ .ThenBy(d => d.Loop.LoopId.Value)+ .FirstOrDefault();++ var explicitSource = primary.Analysis.Degeneracy switch+ {+ CylinderProjectionDegeneracy.DegenerateAngularSpan => "Importer.LoopRole.CylinderDegenerateAngularSpan",+ CylinderProjectionDegeneracy.DegenerateAxialSpan => "Importer.LoopRole.CylinderDegenerateAxialSpan",+ CylinderProjectionDegeneracy.RepeatedSeamProjectionCollapse => "Importer.LoopRole.CylinderRepeatedSeamProjectionCollapse",+ _ => nonNormalizableSource+ };++ return LoopRoleFailure<IReadOnlyList<LoopBuildData>>($"Cylinder loop normalization failed: all {loops.Count} loop(s) projected degenerate (maxUnique={maxUniqueCount}, maxAbsArea={maxAbsArea:E6}, faceId={faceEntityId}, primaryLoopId={primary.Loop.LoopId.Value}, primaryDegeneracy={primary.Analysis.Degeneracy}).", explicitSource);}var orderedByArea = infos@@ -1794,6 +1851,70 @@ public static class Step242Importerreturn uv;}+ private static CylinderProjectionAnalysis AnalyzeCylindricalProjection(int uniqueCount, double signedArea, IReadOnlyList<UvPoint> projected)+ {+ if (projected.Count == 0)+ {+ return new CylinderProjectionAnalysis(0d, 0d, 0, 0, CylinderProjectionDegeneracy.InsufficientUniquePoints);+ }++ var minX = projected.Min(p => p.X);+ var maxX = projected.Max(p => p.X);+ var minY = projected.Min(p => p.Y);+ var maxY = projected.Max(p => p.Y);+ var angularSpan = maxX - minX;+ var axialSpan = maxY - minY;+ var seamCrossings = 0;+ var repeatedSeamPointCount = 0;+ for (var i = 1; i < projected.Count; i++)+ {+ var delta = projected[i].X - projected[i - 1].X;+ if (double.Abs(delta) > double.Pi)+ {+ seamCrossings++;+ }++ var a = NormalizeToZeroTwoPi(projected[i - 1].X);+ var b = NormalizeToZeroTwoPi(projected[i].X);+ var nearSeam = (a <= 1e-6d || a >= (2d * double.Pi) - 1e-6d) && (b <= 1e-6d || b >= (2d * double.Pi) - 1e-6d);+ if (nearSeam && double.Abs(projected[i].Y - projected[i - 1].Y) <= ContainmentEps)+ {+ repeatedSeamPointCount++;+ }+ }++ var degeneracy = CylinderProjectionDegeneracy.None;+ if (uniqueCount < 3)+ {+ degeneracy = CylinderProjectionDegeneracy.InsufficientUniquePoints;+ }+ else if (double.Abs(signedArea) <= AreaEps)+ {+ if (axialSpan <= ContainmentEps && angularSpan >= (2d * double.Pi) - 1e-3d)+ {+ degeneracy = CylinderProjectionDegeneracy.FullRevolutionConstantAxial;+ }+ else if (repeatedSeamPointCount > 0)+ {+ degeneracy = CylinderProjectionDegeneracy.RepeatedSeamProjectionCollapse;+ }+ else if (angularSpan <= 1e-6d)+ {+ degeneracy = CylinderProjectionDegeneracy.DegenerateAngularSpan;+ }+ else if (axialSpan <= ContainmentEps)+ {+ degeneracy = CylinderProjectionDegeneracy.DegenerateAxialSpan;+ }+ else+ {+ degeneracy = CylinderProjectionDegeneracy.NearZeroArea;+ }+ }++ return new CylinderProjectionAnalysis(angularSpan, axialSpan, seamCrossings, repeatedSeamPointCount, degeneracy);+ }+private static IReadOnlyList<UvPoint> AlignPolygonToReference(IReadOnlyList<UvPoint> polygon, double currentX, double referenceX){var twoPi = 2d * double.Pi;@@ -2036,6 +2157,12 @@ public static class Step242Importersink?.Add(diagnostic);}+ private static void ReportCylinderProjectionDiagnostic(LoopRoleCylinderProjectionDiagnostic diagnostic)+ {+ var sink = CylinderProjectionDiagnosticsSink.Value;+ sink?.Add(diagnostic);+ }+private static (double Gap3d, double Gap2d, LoopCoedgeGapClassification Classification) ClassifyCoedgeGap(Point3D previousEnd,Point3D nextStart,@@ -2111,6 +2238,36 @@ public static class Step242ImporterDisconnected}+ public sealed record LoopRoleCylinderProjectionDiagnostic(+ int FaceEntityId,+ int LoopId,+ int PointCount,+ int UniquePointCount,+ double SignedArea,+ double AngularSpan,+ double AxialSpan,+ int SeamCrossings,+ int RepeatedSeamPointCount,+ string Degeneracy);++ private enum CylinderProjectionDegeneracy+ {+ None = 0,+ InsufficientUniquePoints = 1,+ NearZeroArea = 2,+ FullRevolutionConstantAxial = 3,+ DegenerateAngularSpan = 4,+ DegenerateAxialSpan = 5,+ RepeatedSeamProjectionCollapse = 6+ }++ private sealed record CylinderProjectionAnalysis(+ double AngularSpan,+ double AxialSpan,+ int SeamCrossings,+ int RepeatedSeamPointCount,+ CylinderProjectionDegeneracy Degeneracy);+private sealed class CircularSamplingDiagnosticsScope(ICollection<LoopRoleCircularSamplingDiagnostic>? previous) : IDisposable{public void Dispose()@@ -2127,6 +2284,14 @@ public static class Step242Importer}}+ private sealed class CylinderProjectionDiagnosticsScope(ICollection<LoopRoleCylinderProjectionDiagnostic>? previous) : IDisposable+ {+ public void Dispose()+ {+ CylinderProjectionDiagnosticsSink.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 ff15270a..98a78cf9 100644--- a/testdata/step242/manifests/nist.v0.report.json+++ b/testdata/step242/manifests/nist.v0.report.json@@ -199,10 +199,10 @@"firstFailureLayer": "importer-topology","firstDiagnostic": {"code": "ValidationFailed",- "source": "Importer.LoopRole.CylinderNonNormalizableDegenerateProjection",- "messagePrefix": "Cylinder loop normalization failed"+ "source": "",+ "messagePrefix": "Loop 73 references missing child ID CoedgeId { Value = 342, IsValid = True }."},- "diagnosticCount": 1,+ "diagnosticCount": 20,"exceptionEscaped": false,"topologyCounts": {"vertices": 0,@@ -271,8 +271,8 @@"firstFailureLayer": "importer-topology","firstDiagnostic": {"code": "ValidationFailed",- "source": "Importer.LoopRole.CylinderNonNormalizableDegenerateProjection",- "messagePrefix": "Cylinder loop normalization failed"+ "source": "Importer.LoopRole.UnsupportedSurfaceForHoles",+ "messagePrefix": "Multi-loop hole classification is unsupported for this surface type."},"diagnosticCount": 1,"exceptionEscaped": false,@@ -295,8 +295,8 @@"firstFailureLayer": "importer-topology","firstDiagnostic": {"code": "ValidationFailed",- "source": "Importer.LoopRole.CylinderNonNormalizableDegenerateProjection",- "messagePrefix": "Cylinder loop normalization failed"+ "source": "Importer.LoopRole.UnsupportedSurfaceForHoles",+ "messagePrefix": "Multi-loop hole classification is unsupported for this surface type."},"diagnosticCount": 1,"exceptionEscaped": false,