Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
M21: soft-use accepted BSpline scaffold in display preparation fallback
Yuechen Lidfee4f1b9513
diff --git a/Aetheris.Kernel.Core.Tests/Brep/Tessellation/DisplayPreparationFallbackBuilderTests.cs b/Aetheris.Kernel.Core.Tests/Brep/Tessellation/DisplayPreparationFallbackBuilderTests.csnew file mode 100644index 00000000..56648b24--- /dev/null+++ b/Aetheris.Kernel.Core.Tests/Brep/Tessellation/DisplayPreparationFallbackBuilderTests.cs@@ -0,0 +1,83 @@+using Aetheris.Kernel.Core.Brep;+using Aetheris.Kernel.Core.Brep.Tessellation;+using Aetheris.Kernel.Core.Step242;++namespace Aetheris.Kernel.Core.Tests.Brep.Tessellation;++public sealed class DisplayPreparationFallbackBuilderTests+{+ [Fact]+ public void Build_SingleFaceBsplineBody_UsesAcceptedScaffoldPatch()+ {+ var body = ImportRepresentativeBsplineBody();++ var result = DisplayPreparationFallbackBuilder.Build(body);++ Assert.True(result.IsSuccess);+ var patch = Assert.Single(result.Value.FacePatches);+ Assert.Equal(DisplayFaceMeshSource.BsplineUvScaffold, patch.Source);+ Assert.Null(patch.ScaffoldRejectionReason);+ }++ [Fact]+ public void Build_StrictScaffoldThreshold_RejectedAndFallsBackToTessellator()+ {+ var body = ImportRepresentativeBsplineBody();++ var result = DisplayPreparationFallbackBuilder.Build(+ body,+ tessellationOptions: null,+ scaffoldOptions: new DisplayPreparationBsplineScaffoldOptions(+ USegments: 12,+ VSegments: 12,+ Acceptance: new BsplineUvGridScaffoldAcceptanceThresholds(+ MaxTriangleDensityRatioVsFallback: 0.01d)));++ Assert.True(result.IsSuccess);+ var patch = Assert.Single(result.Value.FacePatches);+ Assert.Equal(DisplayFaceMeshSource.Tessellator, patch.Source);+ Assert.Equal(BsplineUvGridScaffoldRejectionReason.TooDenseVsFallback.ToString(), patch.ScaffoldRejectionReason);+ }++ [Fact]+ public void Build_UnsupportedBody_StaysOnExistingFallbackWithoutScaffoldAttempt()+ {+ var body = BrepPrimitives.CreateBox(2d, 2d, 2d).Value;++ var result = DisplayPreparationFallbackBuilder.Build(body);++ Assert.True(result.IsSuccess);+ Assert.All(result.Value.FacePatches, patch =>+ {+ Assert.Equal(DisplayFaceMeshSource.Tessellator, patch.Source);+ Assert.Null(patch.ScaffoldRejectionReason);+ });+ }++ [Fact]+ public void Build_AcceptedScaffoldCase_IsDeterministic()+ {+ var body = ImportRepresentativeBsplineBody();++ var first = DisplayPreparationFallbackBuilder.Build(body);+ var second = DisplayPreparationFallbackBuilder.Build(body);++ Assert.True(first.IsSuccess);+ Assert.True(second.IsSuccess);+ Assert.Equal(CreateSignature(first.Value), CreateSignature(second.Value));+ }++ private static string CreateSignature(DisplayTessellationResult result)+ => string.Join(+ "|",+ result.FacePatches.Select(patch =>+ $"{patch.FaceId.Value}:{patch.Source}:{patch.ScaffoldRejectionReason}:{patch.Positions.Count}:{patch.TriangleIndices.Count}:{string.Join(',', patch.TriangleIndices.Take(24))}"));++ private static BrepBody ImportRepresentativeBsplineBody()+ {+ const string text = "ISO-10303-21;\nHEADER;\nENDSEC;\nDATA;\n#1=MANIFOLD_SOLID_BREP('s',#2);\n#2=CLOSED_SHELL($,(#3));\n#3=ADVANCED_FACE((#4),#5,.T.);\n#4=FACE_OUTER_BOUND($,#6,.T.);\n#5=B_SPLINE_SURFACE_WITH_KNOTS('',3,3,((#100,#101,#102,#103),(#104,#105,#106,#107),(#108,#109,#110,#111),(#112,#113,#114,#115)),.UNSPECIFIED.,.F.,.F.,.F.,(4,4),(4,4),(0.,1.),(0.,1.),.UNSPECIFIED.);\n#6=EDGE_LOOP($,(#10,#11,#12,#13));\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#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#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($,#51,#81);\n#42=LINE($,#52,#82);\n#43=LINE($,#53,#83);\n#50=CARTESIAN_POINT($,(0,0,0));\n#51=CARTESIAN_POINT($,(1,0,0));\n#52=CARTESIAN_POINT($,(1,1,0));\n#53=CARTESIAN_POINT($,(0,1,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#100=CARTESIAN_POINT($,(0,0,0));\n#101=CARTESIAN_POINT($,(0,0.33,0));\n#102=CARTESIAN_POINT($,(0,0.66,0));\n#103=CARTESIAN_POINT($,(0,1,0));\n#104=CARTESIAN_POINT($,(0.33,0,0));\n#105=CARTESIAN_POINT($,(0.33,0.33,0));\n#106=CARTESIAN_POINT($,(0.33,0.66,0));\n#107=CARTESIAN_POINT($,(0.33,1,0));\n#108=CARTESIAN_POINT($,(0.66,0,0));\n#109=CARTESIAN_POINT($,(0.66,0.33,0));\n#110=CARTESIAN_POINT($,(0.66,0.66,0));\n#111=CARTESIAN_POINT($,(0.66,1,0));\n#112=CARTESIAN_POINT($,(1,0,0));\n#113=CARTESIAN_POINT($,(1,0.33,0));\n#114=CARTESIAN_POINT($,(1,0.66,0));\n#115=CARTESIAN_POINT($,(1,1,0));\nENDSEC;\nEND-ISO-10303-21;";+ var import = Step242Importer.ImportBody(text);+ Assert.True(import.IsSuccess);+ return import.Value;+ }+}diff --git a/Aetheris.Kernel.Core/Brep/Tessellation/DisplayPreparationFallbackBuilder.cs b/Aetheris.Kernel.Core/Brep/Tessellation/DisplayPreparationFallbackBuilder.csnew file mode 100644index 00000000..551b1436--- /dev/null+++ b/Aetheris.Kernel.Core/Brep/Tessellation/DisplayPreparationFallbackBuilder.cs@@ -0,0 +1,145 @@+using Aetheris.Kernel.Core.Geometry.Surfaces;+using Aetheris.Kernel.Core.Math;+using Aetheris.Kernel.Core.Results;+using Aetheris.Kernel.Core.Topology;++namespace Aetheris.Kernel.Core.Brep.Tessellation;++public static class DisplayPreparationFallbackBuilder+{+ public static KernelResult<DisplayTessellationResult> Build(+ BrepBody body,+ DisplayTessellationOptions? tessellationOptions = null)+ => Build(body, tessellationOptions, null);++ internal static KernelResult<DisplayTessellationResult> Build(+ BrepBody body,+ DisplayTessellationOptions? tessellationOptions,+ DisplayPreparationBsplineScaffoldOptions? scaffoldOptions)+ {+ var tessellation = BrepDisplayTessellator.Tessellate(body, tessellationOptions);+ if (!tessellation.IsSuccess)+ {+ return tessellation;+ }++ var effectiveScaffoldOptions = scaffoldOptions ?? DisplayPreparationBsplineScaffoldOptions.ConservativeDefaults;+ var fallbackByFace = tessellation.Value.FacePatches.ToDictionary(patch => patch.FaceId);+ var scaffoldBuilder = new BsplineUvGridScaffoldBuilder();++ foreach (var face in body.Topology.Faces.OrderBy(f => f.Id.Value))+ {+ if (!fallbackByFace.TryGetValue(face.Id, out var existingPatch))+ {+ continue;+ }++ if (!IsWithinBoundedSubset(body, face.Id))+ {+ continue;+ }++ if (!body.TryGetFaceSurfaceGeometry(face.Id, out var surface) || surface?.BSplineSurfaceWithKnots is not BSplineSurfaceWithKnots bspline)+ {+ continue;+ }++ var request = new BsplineUvGridScaffoldBuildRequest(+ effectiveScaffoldOptions.USegments,+ effectiveScaffoldOptions.VSegments,+ TrimMask: null,+ ReferencePositions: existingPatch.Positions,+ ReferenceTriangleCount: existingPatch.TriangleIndices.Count / 3,+ Acceptance: effectiveScaffoldOptions.Acceptance);++ var scaffold = scaffoldBuilder.Build(bspline, request);+ if (scaffold.Acceptance != BsplineUvGridScaffoldAcceptance.Accepted || scaffold.Mesh is null)+ {+ fallbackByFace[face.Id] = existingPatch with+ {+ Source = DisplayFaceMeshSource.Tessellator,+ ScaffoldRejectionReason = scaffold.RejectionReason.ToString(),+ };+ continue;+ }++ fallbackByFace[face.Id] = new DisplayFaceMeshPatch(+ face.Id,+ scaffold.Mesh.Positions.ToArray(),+ ComputeVertexNormals(scaffold.Mesh.Positions, scaffold.Mesh.TriangleIndices),+ scaffold.Mesh.TriangleIndices.ToArray(),+ DisplayFaceMeshSource.BsplineUvScaffold,+ null);+ }++ var orderedFaces = body.Topology.Faces+ .OrderBy(f => f.Id.Value)+ .Select(face => fallbackByFace[face.Id])+ .ToArray();++ return KernelResult<DisplayTessellationResult>.Success(+ new DisplayTessellationResult(orderedFaces, tessellation.Value.EdgePolylines),+ tessellation.Diagnostics);+ }++ private static bool IsWithinBoundedSubset(BrepBody body, FaceId faceId)+ {+ if (body.Topology.Faces.Count() != 1)+ {+ return false;+ }++ var loopIds = body.GetLoopIds(faceId);+ return loopIds.Count == 1;+ }++ private static IReadOnlyList<Vector3D> ComputeVertexNormals(IReadOnlyList<Point3D> positions, IReadOnlyList<int> triangleIndices)+ {+ var accumulators = new Vector3D[positions.Count];++ for (var i = 0; i + 2 < triangleIndices.Count; i += 3)+ {+ var i0 = triangleIndices[i];+ var i1 = triangleIndices[i + 1];+ var i2 = triangleIndices[i + 2];++ var p0 = positions[i0];+ var p1 = positions[i1];+ var p2 = positions[i2];+ var faceNormal = (p1 - p0).Cross(p2 - p0);+ if (faceNormal.Length <= 1e-12d)+ {+ continue;+ }++ accumulators[i0] += faceNormal;+ accumulators[i1] += faceNormal;+ accumulators[i2] += faceNormal;+ }++ var normals = new Vector3D[positions.Count];+ for (var i = 0; i < accumulators.Length; i++)+ {+ var candidate = accumulators[i];+ if (!candidate.TryNormalize(out var normalized))+ {+ normalized = new Vector3D(0d, 0d, 1d);+ }++ normals[i] = normalized;+ }++ return normals;+ }+}++internal sealed record DisplayPreparationBsplineScaffoldOptions(+ int USegments,+ int VSegments,+ BsplineUvGridScaffoldAcceptanceThresholds Acceptance)+{+ public static DisplayPreparationBsplineScaffoldOptions ConservativeDefaults { get; } = new(+ 12,+ 12,+ BsplineUvGridScaffoldAcceptanceThresholds.ConservativeDefaults);+}diff --git a/Aetheris.Kernel.Core/Brep/Tessellation/DisplayTessellationResult.cs b/Aetheris.Kernel.Core/Brep/Tessellation/DisplayTessellationResult.csindex 64b69e0b..76e5dcf7 100644--- a/Aetheris.Kernel.Core/Brep/Tessellation/DisplayTessellationResult.cs+++ b/Aetheris.Kernel.Core/Brep/Tessellation/DisplayTessellationResult.cs@@ -7,11 +7,19 @@ public sealed record DisplayTessellationResult(IReadOnlyList<DisplayFaceMeshPatch> FacePatches,IReadOnlyList<DisplayEdgePolyline> EdgePolylines);+public enum DisplayFaceMeshSource+{+ Tessellator,+ BsplineUvScaffold,+}+public sealed record DisplayFaceMeshPatch(FaceId FaceId,IReadOnlyList<Point3D> Positions,IReadOnlyList<Vector3D> Normals,- IReadOnlyList<int> TriangleIndices);+ IReadOnlyList<int> TriangleIndices,+ DisplayFaceMeshSource Source = DisplayFaceMeshSource.Tessellator,+ string? ScaffoldRejectionReason = null);public sealed record DisplayEdgePolyline(EdgeId EdgeId,diff --git a/Aetheris.Server.Tests/KernelApiIntegrationTests.cs b/Aetheris.Server.Tests/KernelApiIntegrationTests.csindex f406093c..39dee5c5 100644--- a/Aetheris.Server.Tests/KernelApiIntegrationTests.cs+++ b/Aetheris.Server.Tests/KernelApiIntegrationTests.cs@@ -172,6 +172,28 @@ public sealed class KernelApiIntegrationTests : IClassFixture<WebApplicationFactAssert.NotEmpty(prepared.Data.TessellationFallback!.FacePatches);}+ [Fact]+ public async Task DisplayPrepare_BoundedBsplineFallback_UsesSoftScaffoldSource()+ {+ var document = await CreateDocumentAsync("/api/v1/documents");++ var importResponse = await _client.PostAsJsonAsync(+ $"/api/v1/documents/{document.Data!.DocumentId}/import/step",+ new StepImportRequestDto(RepresentativeSingleFaceBsplineStep, "BoundedBspline"));+ importResponse.EnsureSuccessStatusCode();+ var imported = await importResponse.Content.ReadFromJsonAsync<ApiResponseDto<StepImportResponseDto>>();+ Assert.NotNull(imported);+ Assert.True(imported!.Success);++ var prepared = await PrepareDisplayAsync(document.Data.DocumentId, imported.Data!.OccurrenceId);++ Assert.Equal("fallback-only", prepared.Data!.Lane);+ Assert.NotNull(prepared.Data.TessellationFallback);+ var patch = Assert.Single(prepared.Data.TessellationFallback!.FacePatches);+ Assert.Equal("BsplineUvScaffold", patch.Source);+ Assert.Null(patch.ScaffoldRejectionReason);+ }+[Fact]public async Task DisplayPrepare_SameBodyTwice_IsDeterministic(){@@ -674,4 +696,6 @@ public sealed class KernelApiIntegrationTests : IClassFixture<WebApplicationFactprivate static string GetRepositoryPath(string relativePath)=> Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "..", "..", "..", "..", relativePath));++ private const string RepresentativeSingleFaceBsplineStep = "ISO-10303-21;\nHEADER;\nENDSEC;\nDATA;\n#1=MANIFOLD_SOLID_BREP('s',#2);\n#2=CLOSED_SHELL($,(#3));\n#3=ADVANCED_FACE((#4),#5,.T.);\n#4=FACE_OUTER_BOUND($,#6,.T.);\n#5=B_SPLINE_SURFACE_WITH_KNOTS('',3,3,((#100,#101,#102,#103),(#104,#105,#106,#107),(#108,#109,#110,#111),(#112,#113,#114,#115)),.UNSPECIFIED.,.F.,.F.,.F.,(4,4),(4,4),(0.,1.),(0.,1.),.UNSPECIFIED.);\n#6=EDGE_LOOP($,(#10,#11,#12,#13));\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#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#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($,#51,#81);\n#42=LINE($,#52,#82);\n#43=LINE($,#53,#83);\n#50=CARTESIAN_POINT($,(0,0,0));\n#51=CARTESIAN_POINT($,(1,0,0));\n#52=CARTESIAN_POINT($,(1,1,0));\n#53=CARTESIAN_POINT($,(0,1,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#100=CARTESIAN_POINT($,(0,0,0));\n#101=CARTESIAN_POINT($,(0,0.33,0));\n#102=CARTESIAN_POINT($,(0,0.66,0));\n#103=CARTESIAN_POINT($,(0,1,0));\n#104=CARTESIAN_POINT($,(0.33,0,0));\n#105=CARTESIAN_POINT($,(0.33,0.33,0));\n#106=CARTESIAN_POINT($,(0.33,0.66,0));\n#107=CARTESIAN_POINT($,(0.33,1,0));\n#108=CARTESIAN_POINT($,(0.66,0,0));\n#109=CARTESIAN_POINT($,(0.66,0.33,0));\n#110=CARTESIAN_POINT($,(0.66,0.66,0));\n#111=CARTESIAN_POINT($,(0.66,1,0));\n#112=CARTESIAN_POINT($,(1,0,0));\n#113=CARTESIAN_POINT($,(1,0.33,0));\n#114=CARTESIAN_POINT($,(1,0.66,0));\n#115=CARTESIAN_POINT($,(1,1,0));\nENDSEC;\nEND-ISO-10303-21;";}diff --git a/Aetheris.Server/Api/ApiMappings.cs b/Aetheris.Server/Api/ApiMappings.csindex 8f976ffe..5a4d9ede 100644--- a/Aetheris.Server/Api/ApiMappings.cs+++ b/Aetheris.Server/Api/ApiMappings.cs@@ -62,7 +62,9 @@ public static class ApiMappingsp.FaceId.Value,p.Positions.Select(position => ToPointDto(transform.Apply(position))).ToArray(),p.Normals.Select(normal => ToVectorDto(transform.Apply(normal))).ToArray(),- p.TriangleIndices.ToArray())).ToArray(),+ p.TriangleIndices.ToArray(),+ p.Source.ToString(),+ p.ScaffoldRejectionReason)).ToArray(),result.EdgePolylines.Select(e => new EdgePolylineDto(e.EdgeId.Value,e.Points.Select(point => ToPointDto(transform.Apply(point))).ToArray(),diff --git a/Aetheris.Server/Api/KernelEndpoints.cs b/Aetheris.Server/Api/KernelEndpoints.csindex 59965fd6..c4de10c7 100644--- a/Aetheris.Server/Api/KernelEndpoints.cs+++ b/Aetheris.Server/Api/KernelEndpoints.cs@@ -302,7 +302,7 @@ public static class KernelEndpointsif (lane is not "analytic-only"){- var tessellateKernel = BrepDisplayTessellator.Tessellate(body, ApiMappings.BuildTessellationOptions(request?.TessellationOptions));+ var tessellateKernel = DisplayPreparationFallbackBuilder.Build(body, ApiMappings.BuildTessellationOptions(request?.TessellationOptions));if (!tessellateKernel.IsSuccess){return ApiMappings.KernelFailure(tessellateKernel.Diagnostics);diff --git a/Aetheris.Server/Contracts/ApiContracts.cs b/Aetheris.Server/Contracts/ApiContracts.csindex eb3609da..b7a214af 100644--- a/Aetheris.Server/Contracts/ApiContracts.cs+++ b/Aetheris.Server/Contracts/ApiContracts.cs@@ -68,7 +68,13 @@ public sealed record TessellateRequestDto(TessellationOptionsDto? Options);public sealed record TessellationResponseDto(IReadOnlyList<FacePatchDto> FacePatches, IReadOnlyList<EdgePolylineDto> EdgePolylines);-public sealed record FacePatchDto(int FaceId, IReadOnlyList<Point3Dto> Positions, IReadOnlyList<Vector3Dto> Normals, IReadOnlyList<int> TriangleIndices);+public sealed record FacePatchDto(+ int FaceId,+ IReadOnlyList<Point3Dto> Positions,+ IReadOnlyList<Vector3Dto> Normals,+ IReadOnlyList<int> TriangleIndices,+ string Source,+ string? ScaffoldRejectionReason);public sealed record EdgePolylineDto(int EdgeId, IReadOnlyList<Point3Dto> Points, bool IsClosed);