Skip to content

Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel

Refine planar loop intersection diagnostics for multi-hole faces

Yuechen Li26023183593d
diff --git a/Aetheris.Kernel.Core.Tests/Step242/Step242ConicalSurfaceRegressionTests.cs b/Aetheris.Kernel.Core.Tests/Step242/Step242ConicalSurfaceRegressionTests.cs
index abbf1a0f..e3039f67 100644
--- a/Aetheris.Kernel.Core.Tests/Step242/Step242ConicalSurfaceRegressionTests.cs
+++ b/Aetheris.Kernel.Core.Tests/Step242/Step242ConicalSurfaceRegressionTests.cs
@@ -33,7 +33,7 @@ public sealed class Step242ConicalSurfaceRegressionTests
[Theory]
[InlineData("testdata/step242/nist/CTC/nist_ctc_01_asme1_ap242-e1.stp", "tessellator", "Viewer.Tessellation.CurvedTopologyUnsupported", "Face 57 curved tessellation expected mirrored line uses for this cone/revolved topology. Observed")]
[InlineData("testdata/step242/nist/CTC/nist_ctc_02_asme1_ap242-e2.stp", "importer-geometry", "Importer.Geometry.CircleTrim", "Unable to compute circle trim from supplied vertices.")]
- [InlineData("testdata/step242/nist/CTC/nist_ctc_04_asme1_ap242-e1.stp", "importer-topology", "Importer.LoopRole.InnerCrossesOuterAfterNormalization", "Inner loop could not be normalized")]
+ [InlineData("testdata/step242/nist/CTC/nist_ctc_04_asme1_ap242-e1.stp", "importer-topology", "Importer.LoopRole.InnerBoundaryIntersectionWithOutsideVerticesAfterNormalization", "Inner loop could not be normalized")]
[InlineData("testdata/step242/nist/FTC/nist_ftc_06_asme1_ap242-e2.stp", "tessellator", "", "Face 2 curved tessellation does not support this torus/revolved boundary topology yet. Observed")]
public void Step242_NistConicalTargets_AdvancePastConicalRadius_AndReportDeterministicNextBlocker(
string relativePath,
diff --git a/Aetheris.Kernel.Core.Tests/Step242/Step242EdgeCurveSenseRegressionTests.cs b/Aetheris.Kernel.Core.Tests/Step242/Step242EdgeCurveSenseRegressionTests.cs
index a261cb93..59bfb14f 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.InnerCrossesOuterAfterNormalization", "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")]
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.cs
index 39bb19bc..7c59193d 100644
--- a/Aetheris.Kernel.Core.Tests/Step242/Step242LoopRoleNormalizationRegressionTests.cs
+++ b/Aetheris.Kernel.Core.Tests/Step242/Step242LoopRoleNormalizationRegressionTests.cs
@@ -5,11 +5,11 @@ namespace Aetheris.Kernel.Core.Tests.Step242;
public sealed class Step242LoopRoleNormalizationRegressionTests
{
[Theory]
- [InlineData("testdata/step242/nist/CTC/nist_ctc_04_asme1_ap242-e1.stp", "Importer.LoopRole.InnerCrossesOuterAfterNormalization", "Inner loop could not be normalized")]
- [InlineData("testdata/step242/nist/CTC/nist_ctc_05_asme1_ap242-e1.stp", "Importer.LoopRole.InnerCrossesOuterAfterNormalization", "Inner loop could not be normalized")]
- [InlineData("testdata/step242/nist/FTC/nist_ftc_07_asme1_ap242-e2.stp", "Importer.LoopRole.InnerCrossesOuterAfterNormalization", "Inner loop could not be normalized")]
- [InlineData("testdata/step242/nist/FTC/nist_ftc_10_asme1_ap242-e2.stp", "Importer.LoopRole.InnerCrossesOuterAfterNormalization", "Inner loop could not be normalized")]
- [InlineData("testdata/step242/nist/FTC/nist_ftc_11_asme1_ap242-e2.stp", "Importer.LoopRole.InnerCrossesOuterAfterNormalization", "Inner loop could not be normalized")]
+ [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/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/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(
diff --git a/Aetheris.Kernel.Core/Step242/Step242Importer.cs b/Aetheris.Kernel.Core/Step242/Step242Importer.cs
index 3662ce94..ef059ea1 100644
--- a/Aetheris.Kernel.Core/Step242/Step242Importer.cs
+++ b/Aetheris.Kernel.Core/Step242/Step242Importer.cs
@@ -963,7 +963,7 @@ public static class Step242Importer
var infos = new List<PlanarLoopInfo>(loops.Count);
foreach (var loop in loops)
{
- var projected = ProjectLoopToPlane(loop.Samples, plane);
+ var projected = SimplifyClosedPolygon(ProjectLoopToPlane(loop.Samples, plane), ContainmentEps);
var uniqueCount = CountUniquePoints(projected, ContainmentEps);
var area = ComputeSignedArea(projected);
if (uniqueCount < 3 || double.Abs(area) <= AreaEps)
@@ -1019,8 +1019,12 @@ public static class Step242Importer
foreach (var candidate in infosWithSamples.Where(i => i.Info.Loop.LoopId != outer.Info.Loop.LoopId))
{
var containment = EvaluateContainment(candidate.Info.ProjectedPoints, outer.Info.ProjectedPoints, containmentTolerance);
- var intersectsOuter = PolygonsIntersect(candidate.Info.ProjectedPoints, outer.Info.ProjectedPoints, containmentTolerance);
- if (!intersectsOuter && containment.OutsideCount == 0)
+ var intersectionCount = CountPolygonIntersections(candidate.Info.ProjectedPoints, outer.Info.ProjectedPoints, containmentTolerance);
+ var intersectsOuter = intersectionCount > 0;
+ if ((!intersectsOuter && containment.OutsideCount == 0)
+ || (intersectsOuter
+ && containment.OutsideCount == 0
+ && containment.MinDistanceToOuter > (containmentTolerance * 8d)))
{
containedInners.Add(candidate.Info);
continue;
@@ -1032,7 +1036,7 @@ public static class Step242Importer
continue;
}
- var failure = BuildInnerContainmentFailure(candidate.Info, outer.Info, containmentTolerance, areaTolerance);
+ var failure = BuildInnerContainmentFailure(candidate.Info, outer.Info, containmentTolerance, areaTolerance, intersectionCount);
return LoopRoleFailure<IReadOnlyList<LoopBuildData>>(failure.Message, failure.Source);
}
@@ -1167,27 +1171,29 @@ public static class Step242Importer
PlanarLoopInfo inner,
PlanarLoopInfo outer,
double containmentTolerance,
- double areaTolerance)
+ double areaTolerance,
+ int intersectionCount)
{
var containment = EvaluateContainment(inner.ProjectedPoints, outer.ProjectedPoints, containmentTolerance);
- var crossingOuter = PolygonsIntersect(inner.ProjectedPoints, outer.ProjectedPoints, containmentTolerance);
+ var crossingOuter = intersectionCount > 0;
var areaRatio = double.Abs(outer.SignedArea) <= areaTolerance
? 0d
: double.Abs(inner.SignedArea) / double.Abs(outer.SignedArea);
var reason = crossingOuter
- ? "crosses_outer_boundary"
+ ? (containment.OutsideCount == 0 ? "crosses_outer_boundary_with_all_vertices_inside" : "crosses_outer_boundary_with_outside_vertices")
: (containment.OutsideCount == containment.VertexCount ? "disjoint" : "partially_outside");
var source = reason switch
{
- "crosses_outer_boundary" => "Importer.LoopRole.InnerCrossesOuterAfterNormalization",
+ "crosses_outer_boundary_with_all_vertices_inside" => "Importer.LoopRole.InnerBoundaryIntersectionWithContainedVerticesAfterNormalization",
+ "crosses_outer_boundary_with_outside_vertices" => "Importer.LoopRole.InnerBoundaryIntersectionWithOutsideVerticesAfterNormalization",
"disjoint" => "Importer.LoopRole.InnerDisjointAfterNormalization",
_ => "Importer.LoopRole.InnerPartiallyOutsideAfterNormalization"
};
- var message = $"Inner loop could not be normalized: {reason}. innerLoopId={inner.Loop.LoopId.Value}, outerLoopId={outer.Loop.LoopId.Value}, outsideVertices={containment.OutsideCount}/{containment.VertexCount}, nearestOuterDistance={containment.MinDistanceToOuter:E6}, areaRatio={areaRatio:E6}.";
+ var message = $"Inner loop could not be normalized: {reason}. innerLoopId={inner.Loop.LoopId.Value}, outerLoopId={outer.Loop.LoopId.Value}, outsideVertices={containment.OutsideCount}/{containment.VertexCount}, nearestOuterDistance={containment.MinDistanceToOuter:E6}, areaRatio={areaRatio:E6}, intersections={intersectionCount}.";
return new ContainmentFailure(message, source);
}
@@ -1224,7 +1230,7 @@ public static class Step242Importer
IReadOnlyList<UvPoint> outer,
double containmentTolerance)
{
- if (PolygonsIntersect(inner, outer, containmentTolerance))
+ if (CountPolygonIntersections(inner, outer, containmentTolerance) > 0)
{
return false;
}
@@ -1244,36 +1250,67 @@ public static class Step242Importer
return minDistance;
}
- private static bool PolygonsIntersect(IReadOnlyList<UvPoint> a, IReadOnlyList<UvPoint> b, double tolerance)
+ private static int CountPolygonIntersections(IReadOnlyList<UvPoint> a, IReadOnlyList<UvPoint> b, double tolerance)
{
+ var intersections = 0;
for (var i = 0; i < a.Count - 1; i++)
{
+ if (SegmentLengthSquared(a[i], a[i + 1]) <= (tolerance * tolerance))
+ {
+ continue;
+ }
+
for (var j = 0; j < b.Count - 1; j++)
{
+ if (SegmentLengthSquared(b[j], b[j + 1]) <= (tolerance * tolerance))
+ {
+ continue;
+ }
+
if (SegmentsIntersect(a[i], a[i + 1], b[j], b[j + 1], tolerance))
{
- return true;
+ intersections++;
}
}
}
- return false;
+ return intersections;
}
private static bool SegmentsIntersect(UvPoint a0, UvPoint a1, UvPoint b0, UvPoint b1, double tolerance)
{
- if (DistancePointToSegment(a0, b0, b1) <= tolerance || DistancePointToSegment(a1, b0, b1) <= tolerance
- || DistancePointToSegment(b0, a0, a1) <= tolerance || DistancePointToSegment(b1, a0, a1) <= tolerance)
- {
- return true;
- }
-
var o1 = Orientation(a0, a1, b0);
var o2 = Orientation(a0, a1, b1);
var o3 = Orientation(b0, b1, a0);
var o4 = Orientation(b0, b1, a1);
- return (o1 * o2) < 0d && (o3 * o4) < 0d;
+ if (IsProperStraddle(o1, o2, tolerance) && IsProperStraddle(o3, o4, tolerance))
+ {
+ return true;
+ }
+
+ return (double.Abs(o1) <= tolerance && IsPointOnSegment(b0, a0, a1, tolerance))
+ || (double.Abs(o2) <= tolerance && IsPointOnSegment(b1, a0, a1, tolerance))
+ || (double.Abs(o3) <= tolerance && IsPointOnSegment(a0, b0, b1, tolerance))
+ || (double.Abs(o4) <= tolerance && IsPointOnSegment(a1, b0, b1, tolerance));
+ }
+
+ private static bool IsProperStraddle(double a, double b, double tolerance)
+ => (a > tolerance && b < -tolerance) || (a < -tolerance && b > tolerance);
+
+ private static bool IsPointOnSegment(UvPoint p, UvPoint a, UvPoint b, double tolerance)
+ {
+ var minX = double.Min(a.X, b.X) - tolerance;
+ var maxX = double.Max(a.X, b.X) + tolerance;
+ var minY = double.Min(a.Y, b.Y) - tolerance;
+ var maxY = double.Max(a.Y, b.Y) + tolerance;
+ return p.X >= minX && p.X <= maxX && p.Y >= minY && p.Y <= maxY;
+ }
+
+ private static double SegmentLengthSquared(UvPoint a, UvPoint b)
+ {
+ var delta = b - a;
+ return delta.Dot(delta);
}
private static double Orientation(UvPoint a, UvPoint b, UvPoint c)
@@ -1405,6 +1442,37 @@ public static class Step242Importer
return unique.Count;
}
+
+ private static List<UvPoint> SimplifyClosedPolygon(IReadOnlyList<UvPoint> polygon, double tolerance)
+ {
+ var simplified = new List<UvPoint>(polygon.Count + 1);
+ foreach (var point in polygon)
+ {
+ if (simplified.Count > 0 && (simplified[^1] - point).Length <= tolerance)
+ {
+ continue;
+ }
+
+ simplified.Add(point);
+ }
+
+ if (simplified.Count == 0)
+ {
+ return simplified;
+ }
+
+ if ((simplified[0] - simplified[^1]).Length > tolerance)
+ {
+ simplified.Add(simplified[0]);
+ }
+ else
+ {
+ simplified[^1] = simplified[0];
+ }
+
+ return simplified;
+ }
+
private static List<UvPoint> ProjectLoopToPlane(IReadOnlyList<Point3D> samples, PlaneSurface plane)
{
var uv = new List<UvPoint>(samples.Count + 1);
diff --git a/testdata/step242/manifests/nist.v0.report.json b/testdata/step242/manifests/nist.v0.report.json
index e57c1f80..70de17ac 100644
--- a/testdata/step242/manifests/nist.v0.report.json
+++ b/testdata/step242/manifests/nist.v0.report.json
@@ -79,7 +79,7 @@
"firstFailureLayer": "importer-topology",
"firstDiagnostic": {
"code": "ValidationFailed",
- "source": "Importer.LoopRole.InnerCrossesOuterAfterNormalization",
+ "source": "Importer.LoopRole.InnerBoundaryIntersectionWithOutsideVerticesAfterNormalization",
"messagePrefix": "Inner loop could not be normalized"
},
"diagnosticCount": 1,
@@ -103,7 +103,7 @@
"firstFailureLayer": "importer-topology",
"firstDiagnostic": {
"code": "ValidationFailed",
- "source": "Importer.LoopRole.InnerCrossesOuterAfterNormalization",
+ "source": "Importer.LoopRole.InnerBoundaryIntersectionWithOutsideVerticesAfterNormalization",
"messagePrefix": "Inner loop could not be normalized"
},
"diagnosticCount": 1,
@@ -151,7 +151,7 @@
"firstFailureLayer": "importer-topology",
"firstDiagnostic": {
"code": "ValidationFailed",
- "source": "Importer.LoopRole.InnerCrossesOuterAfterNormalization",
+ "source": "Importer.LoopRole.InnerBoundaryIntersectionWithOutsideVerticesAfterNormalization",
"messagePrefix": "Inner loop could not be normalized"
},
"diagnosticCount": 1,
@@ -247,7 +247,7 @@
"firstFailureLayer": "importer-topology",
"firstDiagnostic": {
"code": "ValidationFailed",
- "source": "Importer.LoopRole.InnerCrossesOuterAfterNormalization",
+ "source": "Importer.LoopRole.InnerBoundaryIntersectionWithOutsideVerticesAfterNormalization",
"messagePrefix": "Inner loop could not be normalized"
},
"diagnosticCount": 1,
@@ -271,7 +271,7 @@
"firstFailureLayer": "importer-topology",
"firstDiagnostic": {
"code": "ValidationFailed",
- "source": "Importer.LoopRole.InnerCrossesOuterAfterNormalization",
+ "source": "Importer.LoopRole.InnerBoundaryIntersectionWithOutsideVerticesAfterNormalization",
"messagePrefix": "Inner loop could not be normalized"
},
"diagnosticCount": 1,