Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
Allow planar tessellation to handle multi-loop faces
Yuechen Liffd7359a4d59
diff --git a/Aetheris.Kernel.Core.Tests/Brep/Tessellation/BrepDisplayTessellatorTests.cs b/Aetheris.Kernel.Core.Tests/Brep/Tessellation/BrepDisplayTessellatorTests.csindex 0c240c1f..4c61046d 100644--- a/Aetheris.Kernel.Core.Tests/Brep/Tessellation/BrepDisplayTessellatorTests.cs+++ b/Aetheris.Kernel.Core.Tests/Brep/Tessellation/BrepDisplayTessellatorTests.cs@@ -160,7 +160,7 @@ public sealed class BrepDisplayTessellatorTestsAssert.False(result.IsSuccess);var diagnostic = Assert.Single(result.Diagnostics, d => d.Code == KernelDiagnosticCode.NotImplemented);- Assert.Equal("Face 1 planar tessellation requires exactly one loop.", diagnostic.Message);+ Assert.Equal("Face 1 planar tessellation requires at least one loop.", diagnostic.Message);}[Fact]diff --git a/Aetheris.Kernel.Core.Tests/Step242/Step242TessellationRobustnessTests.cs b/Aetheris.Kernel.Core.Tests/Step242/Step242TessellationRobustnessTests.csnew file mode 100644index 00000000..4c14c9b9--- /dev/null+++ b/Aetheris.Kernel.Core.Tests/Step242/Step242TessellationRobustnessTests.cs@@ -0,0 +1,38 @@+using System.Text;+using Aetheris.Kernel.Core.Brep.Tessellation;+using Aetheris.Kernel.Core.Step242;++namespace Aetheris.Kernel.Core.Tests.Step242;++public sealed class Step242TessellationRobustnessTests+{+ [Fact]+ public void Step242_Tessellate_PlanarFace_AllowsMultipleLoops_UsesOuterOnly()+ {+ var text = LoadFixture("testdata/step242/tessellation-robustness/planar-face-with-hole-outer-and-inner.step");++ var import = Step242Importer.ImportBody(text);++ Assert.True(import.IsSuccess);++ var tessellation = BrepDisplayTessellator.Tessellate(import.Value);++ Assert.True(tessellation.IsSuccess);+ Assert.DoesNotContain(tessellation.Diagnostics, d => d.Message.Contains("requires exactly one loop", StringComparison.Ordinal));++ var facePatch = Assert.Single(tessellation.Value.FacePatches);+ Assert.NotEmpty(facePatch.TriangleIndices);++ var warning = tessellation.Diagnostics.SingleOrDefault(d => string.Equals(d.Source, "Viewer.Tessellation.PlanarHolesIgnored", StringComparison.Ordinal));+ if (warning is not null)+ {+ Assert.Contains("ignored", warning.Message, StringComparison.Ordinal);+ }+ }++ private static string LoadFixture(string relativePath)+ {+ var path = Path.Combine(Step242CorpusManifestRunner.RepoRoot(), relativePath.Replace('/', Path.DirectorySeparatorChar));+ return File.ReadAllText(path, Encoding.UTF8);+ }+}diff --git a/Aetheris.Kernel.Core/Brep/Tessellation/BrepDisplayTessellator.cs b/Aetheris.Kernel.Core/Brep/Tessellation/BrepDisplayTessellator.csindex 1a4c27ae..f8ed0349 100644--- a/Aetheris.Kernel.Core/Brep/Tessellation/BrepDisplayTessellator.cs+++ b/Aetheris.Kernel.Core/Brep/Tessellation/BrepDisplayTessellator.cs@@ -73,12 +73,18 @@ public static class BrepDisplayTessellatorprivate static KernelResult<DisplayFaceMeshPatch> TessellatePlanarFace(BrepBody body, FaceId faceId, PlaneSurface plane, DisplayTessellationOptions options){var loopIds = body.GetLoopIds(faceId);- if (loopIds.Count != 1)+ if (loopIds.Count == 0){- return KernelResult<DisplayFaceMeshPatch>.Failure([CreateNotImplemented($"Face {faceId.Value} planar tessellation requires exactly one loop.")]);+ return KernelResult<DisplayFaceMeshPatch>.Failure([CreateNotImplemented($"Face {faceId.Value} planar tessellation requires at least one loop.")]);}- var coedges = body.GetCoedgeIds(loopIds[0])+ var selectedLoopId = SelectPlanarPrimaryLoop(body, faceId, plane, loopIds);+ var ignoredLoopCount = System.Math.Max(0, loopIds.Count - 1);+ var successDiagnostics = ignoredLoopCount > 0+ ? new[] { CreateValidationWarning($"Face {faceId.Value} planar tessellation ignored {ignoredLoopCount} inner loop(s).", "Viewer.Tessellation.PlanarHolesIgnored") }+ : Array.Empty<KernelDiagnostic>();++ var coedges = body.GetCoedgeIds(selectedLoopId).Select(id => body.Topology.GetCoedge(id)).ToArray();@@ -103,18 +109,78 @@ public static class BrepDisplayTessellatorreturn KernelResult<DisplayFaceMeshPatch>.Failure([CreateNotImplemented($"Face {faceId.Value} planar loop triangulation requires a convex polygon.")]);}- return KernelResult<DisplayFaceMeshPatch>.Success(CreatePlanarPatch(faceId, polygonPoints, plane.Normal.ToVector(), indices));+ return KernelResult<DisplayFaceMeshPatch>.Success(CreatePlanarPatch(faceId, polygonPoints, plane.Normal.ToVector(), indices), successDiagnostics);}if (coedges.Length == 1 && body.GetEdgeCurve(coedges[0].EdgeId).Kind == CurveGeometryKind.Circle3){var circlePatch = TessellatePlanarCircleFace(body, faceId, coedges[0], plane, options);- return circlePatch;+ return circlePatch.IsSuccess+ ? KernelResult<DisplayFaceMeshPatch>.Success(circlePatch.Value, successDiagnostics.Concat(circlePatch.Diagnostics))+ : circlePatch;}return KernelResult<DisplayFaceMeshPatch>.Failure([CreateNotImplemented($"Face {faceId.Value} planar tessellation supports only all-line loops or a single circle loop.")]);}+ private static LoopId SelectPlanarPrimaryLoop(BrepBody body, FaceId faceId, PlaneSurface plane, IReadOnlyList<LoopId> loopIds)+ {+ if (loopIds.Count <= 1)+ {+ return loopIds[0];+ }++ var bestLoop = loopIds[0];+ var bestArea = double.NegativeInfinity;++ foreach (var loopId in loopIds)+ {+ var coedges = body.GetCoedgeIds(loopId)+ .Select(id => body.Topology.GetCoedge(id))+ .ToArray();++ if (!coedges.All(c => body.GetEdgeCurve(c.EdgeId).Kind == CurveGeometryKind.Line3))+ {+ continue;+ }++ var pointsResult = BuildOrderedPlanarLoopPoints(body, coedges, faceId);+ if (!pointsResult.IsSuccess)+ {+ continue;+ }++ var area = double.Abs(ComputeSignedPlanarArea(pointsResult.Value, plane));+ if (area > bestArea)+ {+ bestArea = area;+ bestLoop = loopId;+ }+ }++ return bestLoop;+ }++ private static double ComputeSignedPlanarArea(IReadOnlyList<Point3D> polygonPoints, PlaneSurface plane)+ {+ var uAxis = plane.UAxis.ToVector();+ var vAxis = plane.VAxis.ToVector();+ var area = 0d;++ for (var i = 0; i < polygonPoints.Count; i++)+ {+ var current = polygonPoints[i] - plane.Origin;+ var next = polygonPoints[(i + 1) % polygonPoints.Count] - plane.Origin;+ var currentU = current.Dot(uAxis);+ var currentV = current.Dot(vAxis);+ var nextU = next.Dot(uAxis);+ var nextV = next.Dot(vAxis);+ area += (currentU * nextV) - (nextU * currentV);+ }++ return area * 0.5d;+ }+private static KernelResult<DisplayFaceMeshPatch> TessellatePlanarCircleFace(BrepBody body, FaceId faceId, Coedge coedge, PlaneSurface plane, DisplayTessellationOptions options){var edge = TessellateEdge(body, coedge.EdgeId, options);@@ -675,4 +741,7 @@ public static class BrepDisplayTessellatorprivate static KernelDiagnostic CreateNotImplemented(string message)=> new(KernelDiagnosticCode.NotImplemented, KernelDiagnosticSeverity.Error, message);++ private static KernelDiagnostic CreateValidationWarning(string message, string source)+ => new(KernelDiagnosticCode.ValidationFailed, KernelDiagnosticSeverity.Warning, message, source);}diff --git a/testdata/step242/tessellation-robustness/planar-face-with-hole-outer-and-inner.step b/testdata/step242/tessellation-robustness/planar-face-with-hole-outer-and-inner.stepnew file mode 100644index 00000000..9d03f396--- /dev/null+++ b/testdata/step242/tessellation-robustness/planar-face-with-hole-outer-and-inner.step@@ -0,0 +1,66 @@+ISO-10303-21;+HEADER;+ENDSEC;+DATA;+#1=MANIFOLD_SOLID_BREP('solid',#2);+#2=CLOSED_SHELL($,(#3));+#3=ADVANCED_FACE((#4,#5),#6,.T.);+#4=FACE_OUTER_BOUND($,#7,.T.);+#5=FACE_BOUND($,#8,.T.);+#6=PLANE($,#9);+#7=EDGE_LOOP($,(#10,#11,#12,#13));+#8=EDGE_LOOP($,(#14,#15,#16,#17));+#9=AXIS2_PLACEMENT_3D($,#90,#91,#92);+#10=ORIENTED_EDGE($,$,$,#20,.T.);+#11=ORIENTED_EDGE($,$,$,#21,.T.);+#12=ORIENTED_EDGE($,$,$,#22,.T.);+#13=ORIENTED_EDGE($,$,$,#23,.T.);+#14=ORIENTED_EDGE($,$,$,#24,.T.);+#15=ORIENTED_EDGE($,$,$,#25,.T.);+#16=ORIENTED_EDGE($,$,$,#26,.T.);+#17=ORIENTED_EDGE($,$,$,#27,.T.);+#20=EDGE_CURVE($,#30,#31,#40,.T.);+#21=EDGE_CURVE($,#31,#32,#41,.T.);+#22=EDGE_CURVE($,#32,#33,#42,.T.);+#23=EDGE_CURVE($,#33,#30,#43,.T.);+#24=EDGE_CURVE($,#34,#35,#44,.T.);+#25=EDGE_CURVE($,#35,#36,#45,.T.);+#26=EDGE_CURVE($,#36,#37,#46,.T.);+#27=EDGE_CURVE($,#37,#34,#47,.T.);+#30=VERTEX_POINT($,#50);+#31=VERTEX_POINT($,#51);+#32=VERTEX_POINT($,#52);+#33=VERTEX_POINT($,#53);+#34=VERTEX_POINT($,#54);+#35=VERTEX_POINT($,#55);+#36=VERTEX_POINT($,#56);+#37=VERTEX_POINT($,#57);+#40=LINE($,#50,#80);+#41=LINE($,#51,#81);+#42=LINE($,#52,#82);+#43=LINE($,#53,#83);+#44=LINE($,#54,#80);+#45=LINE($,#55,#81);+#46=LINE($,#56,#82);+#47=LINE($,#57,#83);+#50=CARTESIAN_POINT($,(0,0,0));+#51=CARTESIAN_POINT($,(10,0,0));+#52=CARTESIAN_POINT($,(10,10,0));+#53=CARTESIAN_POINT($,(0,10,0));+#54=CARTESIAN_POINT($,(3,3,0));+#55=CARTESIAN_POINT($,(7,3,0));+#56=CARTESIAN_POINT($,(7,7,0));+#57=CARTESIAN_POINT($,(3,7,0));+#80=VECTOR($,#84,1.0);+#81=VECTOR($,#85,1.0);+#82=VECTOR($,#86,1.0);+#83=VECTOR($,#87,1.0);+#84=DIRECTION($,(1,0,0));+#85=DIRECTION($,(0,1,0));+#86=DIRECTION($,(-1,0,0));+#87=DIRECTION($,(0,-1,0));+#90=CARTESIAN_POINT($,(0,0,0));+#91=DIRECTION($,(0,0,1));+#92=DIRECTION($,(1,0,0));+ENDSEC;+END-ISO-10303-21;