Skip to content

Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel

step242: add executed regression for unsupported-surface progression

Yuechen Lia9a19d4f9e42
diff --git a/Aetheris.Kernel.Core.Tests/Step242/Step242EdgeCurveSenseRegressionTests.cs b/Aetheris.Kernel.Core.Tests/Step242/Step242EdgeCurveSenseRegressionTests.cs
index 4f2fa26b..6d7888cf 100644
--- a/Aetheris.Kernel.Core.Tests/Step242/Step242EdgeCurveSenseRegressionTests.cs
+++ b/Aetheris.Kernel.Core.Tests/Step242/Step242EdgeCurveSenseRegressionTests.cs
@@ -26,8 +26,14 @@ public sealed class Step242EdgeCurveSenseRegressionTests
Assert.NotEqual("Importer.Orientation.EdgeCurveSense", first.FirstDiagnostic.Source);
Assert.NotEqual("Importer.LoopRole.CylinderNonNormalizableDegenerateProjection", first.FirstDiagnostic.Source);
+ Assert.NotEqual("Importer.LoopRole.UnsupportedSurfaceForHoles", first.FirstDiagnostic.Source);
Assert.False(string.IsNullOrWhiteSpace(first.FirstDiagnostic.MessagePrefix));
+ if (string.Equals(relativePath, "testdata/step242/nist/STC/nist_stc_06_asme1_ap242-e3.stp", StringComparison.Ordinal))
+ {
+ Assert.StartsWith("Importer.LoopRole.UnsupportedSurfaceForHoles.", first.FirstDiagnostic.Source, StringComparison.Ordinal);
+ }
+
Assert.Equal(first.FirstFailureLayer, second.FirstFailureLayer);
Assert.Equal(first.FirstDiagnostic.Source, second.FirstDiagnostic.Source);
Assert.Equal(first.FirstDiagnostic.MessagePrefix, second.FirstDiagnostic.MessagePrefix);
diff --git a/Aetheris.Kernel.Core.Tests/Step242/Step242EllipseEdgeRegressionTests.cs b/Aetheris.Kernel.Core.Tests/Step242/Step242EllipseEdgeRegressionTests.cs
index 5b2bec76..4c30fe5c 100644
--- a/Aetheris.Kernel.Core.Tests/Step242/Step242EllipseEdgeRegressionTests.cs
+++ b/Aetheris.Kernel.Core.Tests/Step242/Step242EllipseEdgeRegressionTests.cs
@@ -6,7 +6,6 @@ public sealed class Step242EllipseEdgeRegressionTests
{
[Theory]
[InlineData("testdata/step242/nist/CTC/nist_ctc_02_asme1_ap242-e2.stp", "exporter", "Face:47", "Unsupported surface kind 'Sphere'.")]
- [InlineData("testdata/step242/nist/STC/nist_stc_06_asme1_ap242-e3.stp", "importer-topology", "Importer.LoopRole.UnsupportedSurfaceForHoles", "Multi-loop hole classification is unsupported for this surface type.")]
public void Step242_NistEllipseTargets_AdvancePastUnsupportedEllipse_AndRemainDeterministic(string relativePath, string expectedLayer, string expectedSource, string expectedMessagePrefix)
{
var entry = new Step242CorpusManifestEntry(
diff --git a/Aetheris.Kernel.Core.Tests/Step242/Step242HoleSemanticsTests.cs b/Aetheris.Kernel.Core.Tests/Step242/Step242HoleSemanticsTests.cs
index a7e4d2f6..3551df6c 100644
--- a/Aetheris.Kernel.Core.Tests/Step242/Step242HoleSemanticsTests.cs
+++ b/Aetheris.Kernel.Core.Tests/Step242/Step242HoleSemanticsTests.cs
@@ -76,7 +76,7 @@ public sealed class Step242HoleSemanticsTests
Assert.False(import.IsSuccess);
var diagnostic = Assert.Single(import.Diagnostics);
Assert.Equal(KernelDiagnosticCode.ValidationFailed, diagnostic.Code);
- Assert.Equal("Importer.LoopRole.UnsupportedSurfaceForHoles", diagnostic.Source);
+ Assert.Equal("Importer.LoopRole.UnsupportedSurfaceForHoles.Sphere", diagnostic.Source);
}
[Fact]
diff --git a/Aetheris.Kernel.Core.Tests/Step242/Step242UnsupportedSurfaceForHolesRegressionTests.cs b/Aetheris.Kernel.Core.Tests/Step242/Step242UnsupportedSurfaceForHolesRegressionTests.cs
new file mode 100644
index 00000000..63cd2161
--- /dev/null
+++ b/Aetheris.Kernel.Core.Tests/Step242/Step242UnsupportedSurfaceForHolesRegressionTests.cs
@@ -0,0 +1,46 @@
+using Aetheris.Kernel.Core.Step242;
+
+namespace Aetheris.Kernel.Core.Tests.Step242;
+
+public sealed class Step242UnsupportedSurfaceForHolesRegressionTests
+{
+ [Theory]
+ [InlineData(
+ "testdata/step242/nist/FTC/nist_ftc_11_asme1_ap242-e2.stp",
+ "importer-topology",
+ "Importer.LoopRole.TorusDegenerateProjection",
+ "Toroidal loop normalization failed")]
+ [InlineData(
+ "testdata/step242/nist/STC/nist_stc_06_asme1_ap242-e3.stp",
+ "importer-topology",
+ "Importer.LoopRole.UnsupportedSurfaceForHoles.Cone",
+ "Multi-loop hole classification is unsupported for surface type 'Cone'.")]
+ public void Step242_NistTargets_AdvancePastGenericUnsupportedSurfaceForHoles_Deterministically(
+ string relativePath,
+ string expectedLayer,
+ string expectedSource,
+ string expectedMessagePrefix)
+ {
+ var entry = new Step242CorpusManifestEntry(
+ Id: Path.GetFileNameWithoutExtension(relativePath),
+ Path: relativePath,
+ Group: "nist-loop-role-regression",
+ Notes: "regression",
+ ExpectedFirstDiagnostic: null,
+ ExpectHashStableAfterCanonicalization: null,
+ ExpectTopologyCounts: null,
+ ExpectGeometryKinds: null);
+
+ var first = Step242CorpusManifestRunner.RunOne(entry);
+ var second = Step242CorpusManifestRunner.RunOne(entry);
+
+ Assert.Equal(expectedLayer, first.FirstFailureLayer);
+ Assert.Equal(expectedSource, first.FirstDiagnostic.Source);
+ Assert.StartsWith(expectedMessagePrefix, first.FirstDiagnostic.MessagePrefix, StringComparison.Ordinal);
+ Assert.NotEqual("Importer.LoopRole.UnsupportedSurfaceForHoles", first.FirstDiagnostic.Source);
+
+ Assert.Equal(first.FirstFailureLayer, second.FirstFailureLayer);
+ Assert.Equal(first.FirstDiagnostic.Source, second.FirstDiagnostic.Source);
+ Assert.Equal(first.FirstDiagnostic.MessagePrefix, second.FirstDiagnostic.MessagePrefix);
+ }
+}
diff --git a/Aetheris.Kernel.Core/Step242/Step242Importer.cs b/Aetheris.Kernel.Core/Step242/Step242Importer.cs
index 819fbf3a..26bbddb2 100644
--- a/Aetheris.Kernel.Core/Step242/Step242Importer.cs
+++ b/Aetheris.Kernel.Core/Step242/Step242Importer.cs
@@ -1216,12 +1216,16 @@ public static class Step242Importer
{
SurfaceGeometryKind.Plane => ClassifyAndNormalizePlanarLoops(loops, surface.Plane!.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"),
+ SurfaceGeometryKind.Torus => ClassifyAndNormalizeToroidalLoops(loops, surface.Torus!.Value),
+ SurfaceGeometryKind.Cone => LoopRoleFailure<IReadOnlyList<LoopBuildData>>(
+ "Multi-loop hole classification is unsupported for surface type 'Cone'.",
+ "Importer.LoopRole.UnsupportedSurfaceForHoles.Cone"),
+ SurfaceGeometryKind.Sphere => LoopRoleFailure<IReadOnlyList<LoopBuildData>>(
+ "Multi-loop hole classification is unsupported for surface type 'Sphere'.",
+ "Importer.LoopRole.UnsupportedSurfaceForHoles.Sphere"),
_ => LoopRoleFailure<IReadOnlyList<LoopBuildData>>(
- "Multi-loop hole classification is unsupported for this surface type.",
- "Importer.LoopRole.UnsupportedSurfaceForHoles")
+ $"Multi-loop hole classification is unsupported for surface type '{surface.Kind}'.",
+ $"Importer.LoopRole.UnsupportedSurfaceForHoles.{surface.Kind}")
};
}
@@ -1458,6 +1462,80 @@ public static class Step242Importer
return KernelResult<IReadOnlyList<LoopBuildData>>.Success(ordered);
}
+ private static KernelResult<IReadOnlyList<LoopBuildData>> ClassifyAndNormalizeToroidalLoops(
+ IReadOnlyList<LoopBuildData> loops,
+ TorusSurface torus)
+ {
+ const string degenerateSource = "Importer.LoopRole.TorusDegenerateProjection";
+ const string ambiguousOuterSource = "Importer.LoopRole.TorusAmbiguousOuter";
+ const string containmentSource = "Importer.LoopRole.TorusInnerContainmentFailed";
+
+ var infos = new List<CylindricalLoopInfo>(loops.Count);
+ foreach (var loop in loops)
+ {
+ var projected = SimplifyClosedPolygon(ProjectLoopToTorus(loop.Samples, torus), ContainmentEps);
+ var uniqueCount = CountUniquePoints(projected, ContainmentEps);
+ var area = ComputeSignedArea(projected);
+ if (uniqueCount < 3 || double.Abs(area) <= AreaEps)
+ {
+ continue;
+ }
+
+ infos.Add(new CylindricalLoopInfo(loop, projected, area, ComputePolygonCentroid(projected)));
+ }
+
+ if (infos.Count == 0)
+ {
+ return LoopRoleFailure<IReadOnlyList<LoopBuildData>>("Toroidal loop normalization failed: all loop projections were degenerate.", degenerateSource);
+ }
+
+ var orderedByArea = infos
+ .OrderByDescending(i => double.Abs(i.SignedArea))
+ .ThenBy(i => i.Loop.LoopId.Value)
+ .ToArray();
+
+ var outer = orderedByArea[0];
+ if (orderedByArea.Length > 1)
+ {
+ var areaTolerance = ComputeAreaTolerance(orderedByArea.SelectMany(i => i.ProjectedPoints));
+ if (double.Abs(double.Abs(outer.SignedArea) - double.Abs(orderedByArea[1].SignedArea)) <= areaTolerance)
+ {
+ return LoopRoleFailure<IReadOnlyList<LoopBuildData>>("Unable to choose a unique toroidal outer loop.", ambiguousOuterSource);
+ }
+ }
+
+ var containmentTolerance = ComputeContainmentTolerance(outer.ProjectedPoints);
+ var containedInners = new List<CylindricalLoopInfo>();
+ foreach (var candidate in infos.Where(i => i.Loop.LoopId != outer.Loop.LoopId))
+ {
+ var alignedCandidate = AlignPolygonToReference(candidate.ProjectedPoints, candidate.Centroid.X, outer.Centroid.X);
+ alignedCandidate = AlignPolygonToReferenceY(alignedCandidate, candidate.Centroid.Y, outer.Centroid.Y);
+ var sampleResult = ChooseContainmentPoint(alignedCandidate, containmentTolerance);
+ if (!sampleResult.IsSuccess)
+ {
+ return LoopRoleFailure<IReadOnlyList<LoopBuildData>>("Unable to classify toroidal inner loop containment.", containmentSource);
+ }
+
+ if (!IsPointInPolygon(sampleResult.Value, outer.ProjectedPoints, containmentTolerance))
+ {
+ return LoopRoleFailure<IReadOnlyList<LoopBuildData>>($"Toroidal inner loop {candidate.Loop.LoopId.Value} could not be normalized inside selected outer loop {outer.Loop.LoopId.Value}.", containmentSource);
+ }
+
+ containedInners.Add(candidate with { ProjectedPoints = alignedCandidate, Centroid = ComputePolygonCentroid(alignedCandidate) });
+ }
+
+ var normalizedOuter = NormalizeLoopWinding(outer.Loop, outer.SignedArea, shouldBePositive: true);
+ var normalizedInners = containedInners
+ .OrderByDescending(i => double.Abs(i.SignedArea))
+ .ThenBy(i => i.Loop.LoopId.Value)
+ .Select(i => NormalizeLoopWinding(i.Loop, i.SignedArea, shouldBePositive: false))
+ .ToList();
+
+ var ordered = new List<LoopBuildData>(1 + normalizedInners.Count) { normalizedOuter };
+ ordered.AddRange(normalizedInners);
+ return KernelResult<IReadOnlyList<LoopBuildData>>.Success(ordered);
+ }
+
private static LoopBuildData NormalizeLoopWinding(LoopBuildData loop, double signedArea, bool shouldBePositive)
{
if (shouldBePositive ? signedArea >= 0d : signedArea <= 0d)
@@ -1851,6 +1929,65 @@ public static class Step242Importer
return uv;
}
+ private static List<UvPoint> ProjectLoopToTorus(IReadOnlyList<Point3D> samples, TorusSurface torus)
+ {
+ var axis = torus.Axis.ToVector();
+ var xAxis = torus.XAxis.ToVector();
+ var yAxis = torus.YAxis.ToVector();
+
+ var uv = new List<UvPoint>(samples.Count + 1);
+ double? previousU = null;
+ var uRevolutions = 0d;
+ double? previousV = null;
+ var vRevolutions = 0d;
+ foreach (var sample in samples)
+ {
+ var offset = sample - torus.Center;
+ var axial = offset.Dot(axis);
+ var inPlane = offset - (axis * axial);
+ var radial = inPlane.Length;
+
+ var u = NormalizeToZeroTwoPi(double.Atan2(inPlane.Dot(yAxis), inPlane.Dot(xAxis)));
+ if (previousU.HasValue)
+ {
+ var deltaU = u - previousU.Value;
+ if (deltaU > double.Pi)
+ {
+ uRevolutions -= 2d * double.Pi;
+ }
+ else if (deltaU < -double.Pi)
+ {
+ uRevolutions += 2d * double.Pi;
+ }
+ }
+
+ var v = NormalizeToZeroTwoPi(double.Atan2(axial, radial - torus.MajorRadius));
+ if (previousV.HasValue)
+ {
+ var deltaV = v - previousV.Value;
+ if (deltaV > double.Pi)
+ {
+ vRevolutions -= 2d * double.Pi;
+ }
+ else if (deltaV < -double.Pi)
+ {
+ vRevolutions += 2d * double.Pi;
+ }
+ }
+
+ uv.Add(new UvPoint(u + uRevolutions, v + vRevolutions));
+ previousU = u;
+ previousV = v;
+ }
+
+ if (uv.Count > 0 && (uv[0] - uv[^1]).Length > ContainmentEps)
+ {
+ uv.Add(uv[0]);
+ }
+
+ return uv;
+ }
+
private static CylinderProjectionAnalysis AnalyzeCylindricalProjection(int uniqueCount, double signedArea, IReadOnlyList<UvPoint> projected)
{
if (projected.Count == 0)
@@ -1929,6 +2066,20 @@ public static class Step242Importer
return polygon.Select(p => new UvPoint(p.X + shift, p.Y)).ToArray();
}
+ private static IReadOnlyList<UvPoint> AlignPolygonToReferenceY(IReadOnlyList<UvPoint> polygon, double currentY, double referenceY)
+ {
+ var twoPi = 2d * double.Pi;
+ var delta = referenceY - currentY;
+ var turns = double.Round(delta / twoPi);
+ var shift = turns * twoPi;
+ if (double.Abs(shift) <= AngleUnwrapEps)
+ {
+ return polygon;
+ }
+
+ return polygon.Select(p => new UvPoint(p.X, p.Y + shift)).ToArray();
+ }
+
private static double NormalizeToZeroTwoPi(double angle)
{
var twoPi = 2d * double.Pi;