Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
Add deterministic STEP multi-loop hole validation rules
Yuechen Libda9c1e618ae
diff --git a/Aetheris.Kernel.Core.Tests/Step242/Step242FixtureCorpus.cs b/Aetheris.Kernel.Core.Tests/Step242/Step242FixtureCorpus.csindex 26108dbf..19350b7e 100644--- a/Aetheris.Kernel.Core.Tests/Step242/Step242FixtureCorpus.cs+++ b/Aetheris.Kernel.Core.Tests/Step242/Step242FixtureCorpus.cs@@ -12,6 +12,16 @@ internal static class Step242FixtureCorpuspublic const string MalformedMissingParen = "ISO-10303-21; DATA; #1=MANIFOLD_SOLID_BREP('bad',#2 ENDSEC; END-ISO-10303-21;";+ public const string PlanarFaceWithRectangularHole = "ISO-10303-21;\nHEADER;\nENDSEC;\nDATA;\n#1=MANIFOLD_SOLID_BREP('solid',#2);\n#2=CLOSED_SHELL($,(#3));\n#3=ADVANCED_FACE((#4,#5),#6,.T.);\n#4=FACE_OUTER_BOUND($,#7,.T.);\n#5=FACE_BOUND($,#8,.T.);\n#6=PLANE($,#9);\n#7=EDGE_LOOP($,(#10,#11,#12,#13));\n#8=EDGE_LOOP($,(#14,#15,#16,#17));\n#9=AXIS2_PLACEMENT_3D($,#90,#91,#92);\n#10=ORIENTED_EDGE($,$,$,#20,.T.);\n#11=ORIENTED_EDGE($,$,$,#21,.T.);\n#12=ORIENTED_EDGE($,$,$,#22,.T.);\n#13=ORIENTED_EDGE($,$,$,#23,.T.);\n#14=ORIENTED_EDGE($,$,$,#24,.T.);\n#15=ORIENTED_EDGE($,$,$,#25,.T.);\n#16=ORIENTED_EDGE($,$,$,#26,.T.);\n#17=ORIENTED_EDGE($,$,$,#27,.T.);\n#20=EDGE_CURVE($,#30,#31,#40,.T.);\n#21=EDGE_CURVE($,#31,#32,#41,.T.);\n#22=EDGE_CURVE($,#32,#33,#42,.T.);\n#23=EDGE_CURVE($,#33,#30,#43,.T.);\n#24=EDGE_CURVE($,#34,#35,#44,.T.);\n#25=EDGE_CURVE($,#35,#36,#45,.T.);\n#26=EDGE_CURVE($,#36,#37,#46,.T.);\n#27=EDGE_CURVE($,#37,#34,#47,.T.);\n#30=VERTEX_POINT($,#50);\n#31=VERTEX_POINT($,#51);\n#32=VERTEX_POINT($,#52);\n#33=VERTEX_POINT($,#53);\n#34=VERTEX_POINT($,#54);\n#35=VERTEX_POINT($,#55);\n#36=VERTEX_POINT($,#56);\n#37=VERTEX_POINT($,#57);\n#40=LINE($,#50,#80);\n#41=LINE($,#51,#81);\n#42=LINE($,#52,#82);\n#43=LINE($,#53,#83);\n#44=LINE($,#54,#80);\n#45=LINE($,#55,#81);\n#46=LINE($,#56,#82);\n#47=LINE($,#57,#83);\n#50=CARTESIAN_POINT($,(0,0,0));\n#51=CARTESIAN_POINT($,(10,0,0));\n#52=CARTESIAN_POINT($,(10,10,0));\n#53=CARTESIAN_POINT($,(0,10,0));\n#54=CARTESIAN_POINT($,(3,3,0));\n#55=CARTESIAN_POINT($,(7,3,0));\n#56=CARTESIAN_POINT($,(7,7,0));\n#57=CARTESIAN_POINT($,(3,7,0));\n#80=VECTOR($,#84,1.0);\n#81=VECTOR($,#85,1.0);\n#82=VECTOR($,#86,1.0);\n#83=VECTOR($,#87,1.0);\n#84=DIRECTION($,(1,0,0));\n#85=DIRECTION($,(0,1,0));\n#86=DIRECTION($,(-1,0,0));\n#87=DIRECTION($,(0,-1,0));\n#90=CARTESIAN_POINT($,(0,0,0));\n#91=DIRECTION($,(0,0,1));\n#92=DIRECTION($,(1,0,0));\nENDSEC;\nEND-ISO-10303-21;";++ public const string PlanarFaceEqualAreaLoops = "ISO-10303-21;\nHEADER;\nENDSEC;\nDATA;\n#1=MANIFOLD_SOLID_BREP('solid',#2);\n#2=CLOSED_SHELL($,(#3));\n#3=ADVANCED_FACE((#4,#5),#6,.T.);\n#4=FACE_BOUND($,#7,.T.);\n#5=FACE_BOUND($,#8,.T.);\n#6=PLANE($,#9);\n#7=EDGE_LOOP($,(#10,#11,#12,#13));\n#8=EDGE_LOOP($,(#14,#15,#16,#17));\n#9=AXIS2_PLACEMENT_3D($,#90,#91,#92);\n#10=ORIENTED_EDGE($,$,$,#20,.T.);\n#11=ORIENTED_EDGE($,$,$,#21,.T.);\n#12=ORIENTED_EDGE($,$,$,#22,.T.);\n#13=ORIENTED_EDGE($,$,$,#23,.T.);\n#14=ORIENTED_EDGE($,$,$,#24,.T.);\n#15=ORIENTED_EDGE($,$,$,#25,.T.);\n#16=ORIENTED_EDGE($,$,$,#26,.T.);\n#17=ORIENTED_EDGE($,$,$,#27,.T.);\n#20=EDGE_CURVE($,#30,#31,#40,.T.);\n#21=EDGE_CURVE($,#31,#32,#41,.T.);\n#22=EDGE_CURVE($,#32,#33,#42,.T.);\n#23=EDGE_CURVE($,#33,#30,#43,.T.);\n#24=EDGE_CURVE($,#34,#35,#44,.T.);\n#25=EDGE_CURVE($,#35,#36,#45,.T.);\n#26=EDGE_CURVE($,#36,#37,#46,.T.);\n#27=EDGE_CURVE($,#37,#34,#47,.T.);\n#30=VERTEX_POINT($,#50);\n#31=VERTEX_POINT($,#51);\n#32=VERTEX_POINT($,#52);\n#33=VERTEX_POINT($,#53);\n#34=VERTEX_POINT($,#54);\n#35=VERTEX_POINT($,#55);\n#36=VERTEX_POINT($,#56);\n#37=VERTEX_POINT($,#57);\n#40=LINE($,#50,#80);\n#41=LINE($,#51,#81);\n#42=LINE($,#52,#82);\n#43=LINE($,#53,#83);\n#44=LINE($,#54,#80);\n#45=LINE($,#55,#81);\n#46=LINE($,#56,#82);\n#47=LINE($,#57,#83);\n#50=CARTESIAN_POINT($,(0,0,0));\n#51=CARTESIAN_POINT($,(4,0,0));\n#52=CARTESIAN_POINT($,(4,4,0));\n#53=CARTESIAN_POINT($,(0,4,0));\n#54=CARTESIAN_POINT($,(10,10,0));\n#55=CARTESIAN_POINT($,(14,10,0));\n#56=CARTESIAN_POINT($,(14,14,0));\n#57=CARTESIAN_POINT($,(10,14,0));\n#80=VECTOR($,#84,1.0);\n#81=VECTOR($,#85,1.0);\n#82=VECTOR($,#86,1.0);\n#83=VECTOR($,#87,1.0);\n#84=DIRECTION($,(1,0,0));\n#85=DIRECTION($,(0,1,0));\n#86=DIRECTION($,(-1,0,0));\n#87=DIRECTION($,(0,-1,0));\n#90=CARTESIAN_POINT($,(0,0,0));\n#91=DIRECTION($,(0,0,1));\n#92=DIRECTION($,(1,0,0));\nENDSEC;\nEND-ISO-10303-21;";++ public const string PlanarFaceInnerOutsideOuter = "ISO-10303-21;\nHEADER;\nENDSEC;\nDATA;\n#1=MANIFOLD_SOLID_BREP('solid',#2);\n#2=CLOSED_SHELL($,(#3));\n#3=ADVANCED_FACE((#4,#5),#6,.T.);\n#4=FACE_OUTER_BOUND($,#7,.T.);\n#5=FACE_BOUND($,#8,.T.);\n#6=PLANE($,#9);\n#7=EDGE_LOOP($,(#10,#11,#12,#13));\n#8=EDGE_LOOP($,(#14,#15,#16,#17));\n#9=AXIS2_PLACEMENT_3D($,#90,#91,#92);\n#10=ORIENTED_EDGE($,$,$,#20,.T.);\n#11=ORIENTED_EDGE($,$,$,#21,.T.);\n#12=ORIENTED_EDGE($,$,$,#22,.T.);\n#13=ORIENTED_EDGE($,$,$,#23,.T.);\n#14=ORIENTED_EDGE($,$,$,#24,.T.);\n#15=ORIENTED_EDGE($,$,$,#25,.T.);\n#16=ORIENTED_EDGE($,$,$,#26,.T.);\n#17=ORIENTED_EDGE($,$,$,#27,.T.);\n#20=EDGE_CURVE($,#30,#31,#40,.T.);\n#21=EDGE_CURVE($,#31,#32,#41,.T.);\n#22=EDGE_CURVE($,#32,#33,#42,.T.);\n#23=EDGE_CURVE($,#33,#30,#43,.T.);\n#24=EDGE_CURVE($,#34,#35,#44,.T.);\n#25=EDGE_CURVE($,#35,#36,#45,.T.);\n#26=EDGE_CURVE($,#36,#37,#46,.T.);\n#27=EDGE_CURVE($,#37,#34,#47,.T.);\n#30=VERTEX_POINT($,#50);\n#31=VERTEX_POINT($,#51);\n#32=VERTEX_POINT($,#52);\n#33=VERTEX_POINT($,#53);\n#34=VERTEX_POINT($,#54);\n#35=VERTEX_POINT($,#55);\n#36=VERTEX_POINT($,#56);\n#37=VERTEX_POINT($,#57);\n#40=LINE($,#50,#80);\n#41=LINE($,#51,#81);\n#42=LINE($,#52,#82);\n#43=LINE($,#53,#83);\n#44=LINE($,#54,#80);\n#45=LINE($,#55,#81);\n#46=LINE($,#56,#82);\n#47=LINE($,#57,#83);\n#50=CARTESIAN_POINT($,(0,0,0));\n#51=CARTESIAN_POINT($,(4,0,0));\n#52=CARTESIAN_POINT($,(4,4,0));\n#53=CARTESIAN_POINT($,(0,4,0));\n#54=CARTESIAN_POINT($,(8,8,0));\n#55=CARTESIAN_POINT($,(10,8,0));\n#56=CARTESIAN_POINT($,(10,10,0));\n#57=CARTESIAN_POINT($,(8,10,0));\n#80=VECTOR($,#84,1.0);\n#81=VECTOR($,#85,1.0);\n#82=VECTOR($,#86,1.0);\n#83=VECTOR($,#87,1.0);\n#84=DIRECTION($,(1,0,0));\n#85=DIRECTION($,(0,1,0));\n#86=DIRECTION($,(-1,0,0));\n#87=DIRECTION($,(0,-1,0));\n#90=CARTESIAN_POINT($,(0,0,0));\n#91=DIRECTION($,(0,0,1));\n#92=DIRECTION($,(1,0,0));\nENDSEC;\nEND-ISO-10303-21;";++ public const string SphericalFaceWithTwoLoops = "ISO-10303-21;\nHEADER;\nENDSEC;\nDATA;\n#1=MANIFOLD_SOLID_BREP('solid',#2);\n#2=CLOSED_SHELL($,(#3));\n#3=ADVANCED_FACE((#4,#5),#6,.T.);\n#4=FACE_BOUND($,#7,.T.);\n#5=FACE_BOUND($,#8,.T.);\n#6=SPHERICAL_SURFACE($,#9,5.0);\n#7=EDGE_LOOP($,(#10));\n#8=EDGE_LOOP($,(#11));\n#9=AXIS2_PLACEMENT_3D($,#12,#13,#14);\n#10=ORIENTED_EDGE($,$,$,#20,.T.);\n#11=ORIENTED_EDGE($,$,$,#21,.T.);\n#20=EDGE_CURVE($,#30,#31,#40,.T.);\n#21=EDGE_CURVE($,#32,#33,#41,.T.);\n#30=VERTEX_POINT($,#50);\n#31=VERTEX_POINT($,#51);\n#32=VERTEX_POINT($,#52);\n#33=VERTEX_POINT($,#53);\n#40=LINE($,#50,#80);\n#41=LINE($,#52,#80);\n#50=CARTESIAN_POINT($,(0,0,5));\n#51=CARTESIAN_POINT($,(1,0,5));\n#52=CARTESIAN_POINT($,(0,1,5));\n#53=CARTESIAN_POINT($,(1,1,5));\n#80=VECTOR($,#81,1.0);\n#81=DIRECTION($,(1,0,0));\n#12=CARTESIAN_POINT($,(0,0,0));\n#13=DIRECTION($,(0,0,1));\n#14=DIRECTION($,(1,0,0));\nENDSEC;\nEND-ISO-10303-21;";++ public const string CylindricalFaceWithTwoLoops = "ISO-10303-21;\nHEADER;\nENDSEC;\nDATA;\n#1=MANIFOLD_SOLID_BREP('solid',#2);\n#2=CLOSED_SHELL($,(#3));\n#3=ADVANCED_FACE((#4,#5),#6,.T.);\n#4=FACE_OUTER_BOUND($,#7,.T.);\n#5=FACE_BOUND($,#8,.T.);\n#6=CYLINDRICAL_SURFACE($,#9,5.0);\n#7=EDGE_LOOP($,(#10));\n#8=EDGE_LOOP($,(#11));\n#9=AXIS2_PLACEMENT_3D($,#12,#13,#14);\n#10=ORIENTED_EDGE($,$,$,#20,.T.);\n#11=ORIENTED_EDGE($,$,$,#21,.T.);\n#20=EDGE_CURVE($,#30,#30,#40,.T.);\n#21=EDGE_CURVE($,#31,#31,#41,.T.);\n#30=VERTEX_POINT($,#50);\n#31=VERTEX_POINT($,#51);\n#40=CIRCLE($,#9,5.0);\n#41=CIRCLE($,#15,2.0);\n#15=AXIS2_PLACEMENT_3D($,#16,#13,#14);\n#50=CARTESIAN_POINT($,(5,0,0));\n#51=CARTESIAN_POINT($,(2,0,0));\n#12=CARTESIAN_POINT($,(0,0,0));\n#13=DIRECTION($,(0,0,1));\n#14=DIRECTION($,(1,0,0));\n#16=CARTESIAN_POINT($,(0,0,1));\nENDSEC;\nEND-ISO-10303-21;";+private static string CreateCanonicalBoxGolden(){var boxResult = BrepPrimitives.CreateBox(2d, 2d, 2d);diff --git a/Aetheris.Kernel.Core.Tests/Step242/Step242HoleSemanticsTests.cs b/Aetheris.Kernel.Core.Tests/Step242/Step242HoleSemanticsTests.csnew file mode 100644index 00000000..35383c96--- /dev/null+++ b/Aetheris.Kernel.Core.Tests/Step242/Step242HoleSemanticsTests.cs@@ -0,0 +1,72 @@+using Aetheris.Kernel.Core.Brep;+using Aetheris.Kernel.Core.Brep.Tessellation;+using Aetheris.Kernel.Core.Diagnostics;+using Aetheris.Kernel.Core.Step242;++namespace Aetheris.Kernel.Core.Tests.Step242;++public sealed class Step242HoleSemanticsTests+{+ [Fact]+ public void ImportBody_PlanarFaceWithHole_ImportsAndPreservesMultipleLoops()+ {+ var import = Step242Importer.ImportBody(Step242FixtureCorpus.PlanarFaceWithRectangularHole);++ Assert.True(import.IsSuccess);+ var body = import.Value;++ var validation = BrepBindingValidator.Validate(body, requireAllEdgeAndFaceBindings: true);+ Assert.True(validation.IsSuccess);++ var face = Assert.Single(body.Topology.Faces);+ Assert.True(face.LoopIds.Count >= 2);++ var tessellation = BrepDisplayTessellator.Tessellate(body);+ Assert.True(tessellation.IsSuccess);+ Assert.NotEmpty(tessellation.Value.FacePatches);+ }++ [Fact]+ public void ImportBody_MultiLoopPlanarEqualAreas_ReturnsAmbiguousOuterValidationFailure()+ {+ var import = Step242Importer.ImportBody(Step242FixtureCorpus.PlanarFaceEqualAreaLoops);++ Assert.False(import.IsSuccess);+ var diagnostic = Assert.Single(import.Diagnostics);+ Assert.Equal(KernelDiagnosticCode.ValidationFailed, diagnostic.Code);+ Assert.Equal("Importer.LoopRole.AmbiguousOuter", diagnostic.Source);+ }++ [Fact]+ public void ImportBody_MultiLoopPlanarInnerOutside_ReturnsInnerNotContainedValidationFailure()+ {+ var import = Step242Importer.ImportBody(Step242FixtureCorpus.PlanarFaceInnerOutsideOuter);++ Assert.False(import.IsSuccess);+ var diagnostic = Assert.Single(import.Diagnostics);+ Assert.Equal(KernelDiagnosticCode.ValidationFailed, diagnostic.Code);+ Assert.Equal("Importer.LoopRole.InnerNotContained", diagnostic.Source);+ }++ [Fact]+ public void ImportBody_MultiLoopSphericalFace_ReturnsUnsupportedSurfaceForHolesValidationFailure()+ {+ var import = Step242Importer.ImportBody(Step242FixtureCorpus.SphericalFaceWithTwoLoops);++ Assert.False(import.IsSuccess);+ var diagnostic = Assert.Single(import.Diagnostics);+ Assert.Equal(KernelDiagnosticCode.ValidationFailed, diagnostic.Code);+ Assert.Equal("Importer.LoopRole.UnsupportedSurfaceForHoles", diagnostic.Source);+ }++ [Fact]+ public void ImportBody_MultiLoopCylindricalFace_ReturnsDeterministicCylinderMappingFailure()+ {+ var import = Step242Importer.ImportBody(Step242FixtureCorpus.CylindricalFaceWithTwoLoops);++ Assert.False(import.IsSuccess);+ var diagnostic = Assert.Single(import.Diagnostics);+ Assert.Equal(KernelDiagnosticCode.ValidationFailed, diagnostic.Code);+ Assert.Equal("Importer.LoopRole.CylinderMappingFailed", diagnostic.Source);+ }+}diff --git a/Aetheris.Kernel.Core/Step242/Step242Importer.cs b/Aetheris.Kernel.Core/Step242/Step242Importer.csindex f8c348b7..3786c61e 100644--- a/Aetheris.Kernel.Core/Step242/Step242Importer.cs+++ b/Aetheris.Kernel.Core/Step242/Step242Importer.cs@@ -11,6 +11,11 @@ namespace Aetheris.Kernel.Core.Step242;public static class Step242Importer{+ private const double AreaEps = 1e-8d;+ private const double PointOnSurfaceEps = 1e-5d;+ private const double AngleUnwrapEps = 1e-8d;+ private const double ContainmentEps = 1e-8d;+public static KernelResult<BrepBody> ImportBody(string stepText){var parseResult = Step242SubsetParser.Parse(stepText);@@ -123,7 +128,19 @@ public static class Step242Importerreturn Failure("ADVANCED_FACE without bounds is unsupported in M23 subset.", $"Entity:{faceEntity.Id}");}- var loopIds = new List<LoopId>(boundRefsResult.Value.Count);+ var faceSameSenseResult = Step242SubsetDecoder.ReadBoolean(faceEntity, 2, "ADVANCED_FACE same_sense");+ if (!faceSameSenseResult.IsSuccess)+ {+ return KernelResult<BrepBody>.Failure(faceSameSenseResult.Diagnostics);+ }++ var bindSurfaceResult = DecodeSurfaceGeometry(document, surfaceEntityResult.Value, faceSameSenseResult.Value, nextSurfaceGeometryId);+ if (!bindSurfaceResult.IsSuccess)+ {+ return KernelResult<BrepBody>.Failure(bindSurfaceResult.Diagnostics);+ }++ var loopData = new List<LoopBuildData>(boundRefsResult.Value.Count);foreach (var boundRef in boundRefsResult.Value){var boundEntityResult = document.TryGetEntity(boundRef.TargetId);@@ -178,6 +195,9 @@ public static class Step242ImportercoedgeIds.Add(builder.AllocateCoedgeId());}+ var loopCoedges = new List<Coedge>(orientedEdgeRefsResult.Value.Count);+ var loopSamples = new List<Point3D>();+for (var i = 0; i < orientedEdgeRefsResult.Value.Count; i++){var orientedEdgeEntityResult = document.TryGetEntity(orientedEdgeRefsResult.Value[i].TargetId, "ORIENTED_EDGE");@@ -232,34 +252,44 @@ public static class Step242ImporterisReversed = !isReversed;}- coedges.Add(new Coedge(+ var coedge = new Coedge(coedgeIds[i],edgeIdResult.Value,loopId,coedgeIds[(i + 1) % coedgeIds.Count],coedgeIds[(i + coedgeIds.Count - 1) % coedgeIds.Count],- IsReversed: isReversed));+ IsReversed: isReversed);++ loopCoedges.Add(coedge);+ var sampleResult = SampleCoedgePoints(bindings.GetEdgeBinding(edgeIdResult.Value), geometry, coedge.IsReversed);+ if (!sampleResult.IsSuccess)+ {+ return KernelResult<BrepBody>.Failure(sampleResult.Diagnostics);+ }++ AppendLoopSamples(loopSamples, sampleResult.Value);}builder.AddLoop(new Loop(loopId, coedgeIds));- loopIds.Add(loopId);+ loopData.Add(new LoopBuildData(loopId, loopCoedges, loopSamples));}- var faceId = builder.AddFace(loopIds);- faceIds.Add(faceId);-- var faceSameSenseResult = Step242SubsetDecoder.ReadBoolean(faceEntity, 2, "ADVANCED_FACE same_sense");- if (!faceSameSenseResult.IsSuccess)+ var classifyResult = ClassifyAndNormalizeFaceLoops(loopData, bindSurfaceResult.Value.SurfaceGeometry);+ if (!classifyResult.IsSuccess){- return KernelResult<BrepBody>.Failure(faceSameSenseResult.Diagnostics);+ return KernelResult<BrepBody>.Failure(classifyResult.Diagnostics);}- var bindSurfaceResult = DecodeSurfaceGeometry(document, surfaceEntityResult.Value, faceSameSenseResult.Value, nextSurfaceGeometryId);- if (!bindSurfaceResult.IsSuccess)+ foreach (var loop in classifyResult.Value.OrderedLoops){- return KernelResult<BrepBody>.Failure(bindSurfaceResult.Diagnostics);+ coedges.AddRange(loop.Coedges);}+ var faceLoopIds = classifyResult.Value.OrderedLoops.Select(l => l.LoopId).ToList();++ var faceId = builder.AddFace(faceLoopIds);+ faceIds.Add(faceId);+var (surfaceGeometryId, surfaceGeometry) = bindSurfaceResult.Value;nextSurfaceGeometryId++;geometry.AddSurface(surfaceGeometryId, surfaceGeometry);@@ -583,6 +613,294 @@ public static class Step242Importerreturn offset.Dot(line.Direction.ToVector());}+ private static KernelResult<(IReadOnlyList<LoopBuildData> OrderedLoops)> ClassifyAndNormalizeFaceLoops(+ IReadOnlyList<LoopBuildData> loops,+ SurfaceGeometry surface)+ {+ if (loops.Count <= 1)+ {+ return KernelResult<(IReadOnlyList<LoopBuildData> OrderedLoops)>.Success((loops));+ }++ return surface.Kind switch+ {+ SurfaceGeometryKind.Plane => ClassifyAndNormalizePlanarLoops(loops, surface.Plane!.Value),+ SurfaceGeometryKind.Cylinder => LoopRoleFailure<(IReadOnlyList<LoopBuildData> OrderedLoops)>(+ "Cylinder multi-loop hole classification is not yet safely supported.",+ "Importer.LoopRole.CylinderMappingFailed"),+ SurfaceGeometryKind.Cone or SurfaceGeometryKind.Sphere => LoopRoleFailure<(IReadOnlyList<LoopBuildData> OrderedLoops)>(+ "Multi-loop hole classification is unsupported for this surface type.",+ "Importer.LoopRole.UnsupportedSurfaceForHoles"),+ _ => LoopRoleFailure<(IReadOnlyList<LoopBuildData> OrderedLoops)>(+ "Multi-loop hole classification is unsupported for this surface type.",+ "Importer.LoopRole.UnsupportedSurfaceForHoles")+ };+ }++ private static KernelResult<(IReadOnlyList<LoopBuildData> OrderedLoops)> ClassifyAndNormalizePlanarLoops(+ IReadOnlyList<LoopBuildData> loops,+ PlaneSurface plane)+ {+ var infos = new List<PlanarLoopInfo>(loops.Count);+ foreach (var loop in loops)+ {+ var projected = ProjectLoopToPlane(loop.Samples, plane);+ var uniqueCount = CountUniquePoints(projected);+ if (uniqueCount < 3)+ {+ return LoopRoleFailure<(IReadOnlyList<LoopBuildData> OrderedLoops)>("Loop projection is degenerate.", "Importer.LoopRole.DegenerateLoop");+ }++ var area = ComputeSignedArea(projected);+ infos.Add(new PlanarLoopInfo(loop, projected, area));+ }++ var orderedByArea = infos+ .OrderByDescending(info => double.Abs(info.SignedArea))+ .ToList();++ var outer = orderedByArea[0];+ if (orderedByArea.Count > 1 && double.Abs(double.Abs(outer.SignedArea) - double.Abs(orderedByArea[1].SignedArea)) <= AreaEps)+ {+ return LoopRoleFailure<(IReadOnlyList<LoopBuildData> OrderedLoops)>("Unable to choose a unique outer loop.", "Importer.LoopRole.AmbiguousOuter");+ }++ foreach (var candidate in infos.Where(i => i.Loop.LoopId != outer.Loop.LoopId))+ {+ var testPointResult = ChooseContainmentPoint(candidate.ProjectedPoints);+ if (!testPointResult.IsSuccess)+ {+ return KernelResult<(IReadOnlyList<LoopBuildData> OrderedLoops)>.Failure(testPointResult.Diagnostics);+ }++ if (!IsPointInPolygon(testPointResult.Value, outer.ProjectedPoints))+ {+ return LoopRoleFailure<(IReadOnlyList<LoopBuildData> OrderedLoops)>("Inner loop is not contained by outer loop.", "Importer.LoopRole.InnerNotContained");+ }+ }++ var innerLoops = infos.Where(i => i.Loop.LoopId != outer.Loop.LoopId).ToList();+ for (var i = 0; i < innerLoops.Count; i++)+ {+ for (var j = i + 1; j < innerLoops.Count; j++)+ {+ if (LoopsOverlap(innerLoops[i], innerLoops[j]))+ {+ return LoopRoleFailure<(IReadOnlyList<LoopBuildData> OrderedLoops)>("Inner loops overlap or are nested.", "Importer.LoopRole.InnerOverlap");+ }+ }+ }++ var normalizedOuter = NormalizeLoopWinding(outer.Loop, outer.SignedArea, shouldBePositive: true);+ var normalizedInners = innerLoops+ .OrderBy(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> OrderedLoops)>.Success((ordered));+ }++ private static LoopBuildData NormalizeLoopWinding(LoopBuildData loop, double signedArea, bool shouldBePositive)+ {+ if (shouldBePositive ? signedArea >= 0d : signedArea <= 0d)+ {+ return loop;+ }++ var flippedCoedges = loop.Coedges+ .Select(c => c with { IsReversed = !c.IsReversed })+ .ToList();++ return new LoopBuildData(loop.LoopId, flippedCoedges, loop.Samples);+ }++ private static bool LoopsOverlap(PlanarLoopInfo a, PlanarLoopInfo b)+ {+ var aPoint = ChooseContainmentPoint(a.ProjectedPoints);+ var bPoint = ChooseContainmentPoint(b.ProjectedPoints);+ if (!aPoint.IsSuccess || !bPoint.IsSuccess)+ {+ return true;+ }++ return IsPointInPolygon(aPoint.Value, b.ProjectedPoints) || IsPointInPolygon(bPoint.Value, a.ProjectedPoints);+ }++ private static KernelResult<UvPoint> ChooseContainmentPoint(IReadOnlyList<UvPoint> polygon)+ {+ for (var i = 0; i < polygon.Count - 1; i++)+ {+ var p = polygon[i];+ if (!IsPointNearPolygonEdge(p, polygon))+ {+ return KernelResult<UvPoint>.Success(p);+ }+ }++ return LoopRoleFailure<UvPoint>("Unable to choose a containment sample point.", "Importer.LoopRole.DegenerateLoop");+ }++ private static bool IsPointNearPolygonEdge(UvPoint point, IReadOnlyList<UvPoint> polygon)+ {+ for (var i = 0; i < polygon.Count - 1; i++)+ {+ if (DistancePointToSegment(point, polygon[i], polygon[i + 1]) <= ContainmentEps)+ {+ return true;+ }+ }++ return false;+ }++ private static double DistancePointToSegment(UvPoint p, UvPoint a, UvPoint b)+ {+ var ab = b - a;+ var ap = p - a;+ var denom = ab.Dot(ab);+ if (denom <= AreaEps)+ {+ return (p - a).Length;+ }++ var t = ap.Dot(ab) / denom;+ if (t < 0d)+ {+ t = 0d;+ }+ else if (t > 1d)+ {+ t = 1d;+ }+ var closest = a + (ab * t);+ return (p - closest).Length;+ }++ private static bool IsPointInPolygon(UvPoint point, IReadOnlyList<UvPoint> polygon)+ {+ var inside = false;+ for (var i = 0; i < polygon.Count - 1; i++)+ {+ var a = polygon[i];+ var b = polygon[i + 1];++ if (DistancePointToSegment(point, a, b) <= ContainmentEps)+ {+ return true;+ }++ var intersects = ((a.Y > point.Y) != (b.Y > point.Y))+ && (point.X < ((b.X - a.X) * (point.Y - a.Y) / ((b.Y - a.Y) + AngleUnwrapEps)) + a.X);+ if (intersects)+ {+ inside = !inside;+ }+ }++ return inside;+ }++ private static int CountUniquePoints(IReadOnlyList<UvPoint> points)+ {+ var unique = new List<UvPoint>();+ foreach (var point in points)+ {+ if (unique.Any(u => (u - point).Length <= ContainmentEps))+ {+ continue;+ }++ unique.Add(point);+ }++ return unique.Count;+ }++ private static List<UvPoint> ProjectLoopToPlane(IReadOnlyList<Point3D> samples, PlaneSurface plane)+ {+ var uv = new List<UvPoint>(samples.Count + 1);+ foreach (var sample in samples)+ {+ var offset = sample - plane.Origin;+ uv.Add(new UvPoint(offset.Dot(plane.UAxis.ToVector()), offset.Dot(plane.VAxis.ToVector())));+ }++ if (uv.Count > 0 && (uv[0] - uv[^1]).Length > ContainmentEps)+ {+ uv.Add(uv[0]);+ }++ return uv;+ }++ private static double ComputeSignedArea(IReadOnlyList<UvPoint> polygon)+ {+ var area = 0d;+ for (var i = 0; i < polygon.Count - 1; i++)+ {+ var p0 = polygon[i];+ var p1 = polygon[i + 1];+ area += (p0.X * p1.Y) - (p1.X * p0.Y);+ }++ return 0.5d * area;+ }++ private static void AppendLoopSamples(ICollection<Point3D> loopSamples, IReadOnlyList<Point3D> edgeSamples)+ {+ foreach (var point in edgeSamples)+ {+ if (loopSamples.Count > 0)+ {+ var last = loopSamples.Last();+ if ((last - point).Length <= PointOnSurfaceEps)+ {+ continue;+ }+ }++ loopSamples.Add(point);+ }+ }++ private static KernelResult<IReadOnlyList<Point3D>> SampleCoedgePoints(EdgeGeometryBinding edgeBinding, BrepGeometryStore geometry, bool isReversed)+ {+ if (!geometry.TryGetCurve(edgeBinding.CurveGeometryId, out var curve) || curve is null)+ {+ return LoopRoleFailure<IReadOnlyList<Point3D>>("Missing edge curve geometry for loop sampling.", "Importer.LoopRole.WindingConflict");+ }++ List<Point3D> points;+ switch (curve.Kind)+ {+ case CurveGeometryKind.Line3:+ var line = curve.Line3!.Value;+ var lineRange = edgeBinding.TrimInterval ?? new ParameterInterval(0d, 1d);+ points = [line.Evaluate(lineRange.Start), line.Evaluate(lineRange.End)];+ break;+ case CurveGeometryKind.Circle3:+ var circle = curve.Circle3!.Value;+ var trim = edgeBinding.TrimInterval ?? new ParameterInterval(0d, 2d * double.Pi);+ var mid = trim.Start + ((trim.End - trim.Start) * 0.5d);+ points = [circle.Evaluate(trim.Start), circle.Evaluate(mid), circle.Evaluate(trim.End)];+ break;+ default:+ return LoopRoleFailure<IReadOnlyList<Point3D>>("Unsupported curve kind for loop sampling.", "Importer.LoopRole.WindingConflict");+ }++ if (isReversed)+ {+ points.Reverse();+ }++ return KernelResult<IReadOnlyList<Point3D>>.Success(points);+ }++ private static KernelResult<T> LoopRoleFailure<T>(string message, string source) =>+ KernelResult<T>.Failure([new KernelDiagnostic(KernelDiagnosticCode.ValidationFailed, KernelDiagnosticSeverity.Error, message, source)]);+private static KernelResult<BrepBody> Failure(string message, string source) =>KernelResult<BrepBody>.Failure([new KernelDiagnostic(KernelDiagnosticCode.NotImplemented, KernelDiagnosticSeverity.Error, message, source)]);@@ -605,4 +923,21 @@ public static class Step242ImporterKernelResult<T>.Failure([new KernelDiagnostic(KernelDiagnosticCode.ValidationFailed, KernelDiagnosticSeverity.Error, message, source)]);private static string SourceFor(int _entityId, string stableSource) => stableSource;++ private sealed record LoopBuildData(LoopId LoopId, IReadOnlyList<Coedge> Coedges, IReadOnlyList<Point3D> Samples);++ private sealed record PlanarLoopInfo(LoopBuildData Loop, IReadOnlyList<UvPoint> ProjectedPoints, double SignedArea);++ private readonly record struct UvPoint(double X, double Y)+ {+ public static UvPoint operator +(UvPoint a, UvPoint b) => new(a.X + b.X, a.Y + b.Y);++ public static UvPoint operator -(UvPoint a, UvPoint b) => new(a.X - b.X, a.Y - b.Y);++ public static UvPoint operator *(UvPoint a, double s) => new(a.X * s, a.Y * s);++ public double Dot(UvPoint other) => (X * other.X) + (Y * other.Y);++ public double Length => double.Sqrt((X * X) + (Y * Y));+ }}diff --git a/docs/milestones.md b/docs/milestones.mdindex 0b9c4de5..37b6ee7b 100644--- a/docs/milestones.md+++ b/docs/milestones.md@@ -296,3 +296,11 @@- [x] Preserve deterministic no-throw diagnostics for parseable failures with stable source buckets (`Importer.Geometry.Circle*`, `Importer.Geometry.Cylinder`, `Importer.Geometry.Cone`, `Importer.Geometry.Sphere`).- [x] Add focused Tier1 importer tests covering import/validate/tessellate/pick smoke and deterministic circle-trim failure behavior.- [x] Keep out-of-scope geometry families deterministic expected-fail (for example `TOROIDAL_SURFACE`, NURBS/spline families).++## M35 — Multi-Loop Hole Rules + Validation Hardening (Tier1 Bounded)++- [x] Add deterministic multi-loop role classification for planar faces: unique outer selection by projected area magnitude, containment checks for inners, overlap rejection, and winding normalization.+- [x] Reject unsafe multi-loop hole scenarios with stable `ValidationFailed` diagnostics under `Importer.LoopRole.*` without silent acceptance.+- [x] Keep non-planar behavior deterministic: multi-loop conical/spherical faces fail with `Importer.LoopRole.UnsupportedSurfaceForHoles`; cylindrical multi-loop faces currently fail with `Importer.LoopRole.CylinderMappingFailed` until safe param-space mapping is implemented.+- [x] Add focused STEP fixture coverage for planar-hole success and deterministic ambiguity/containment/unsupported failures.+- [x] Preserve import determinism/no-throw behavior and avoid changes to exporter formatting or canonical hash semantics.