Skip to content

Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel

H8 harden analytic sphere/torus trim truth boundary

Yuechen Li7fe5ce23f8eb
diff --git a/Aetheris.Kernel.Core.Tests/Brep/Queries/AnalyticDisplayPacketBuilderTests.cs b/Aetheris.Kernel.Core.Tests/Brep/Queries/AnalyticDisplayPacketBuilderTests.cs
index f419d00b..9e0323a7 100644
--- a/Aetheris.Kernel.Core.Tests/Brep/Queries/AnalyticDisplayPacketBuilderTests.cs
+++ b/Aetheris.Kernel.Core.Tests/Brep/Queries/AnalyticDisplayPacketBuilderTests.cs
@@ -6,6 +6,8 @@ using Aetheris.Kernel.Core.Geometry;
using Aetheris.Kernel.Core.Geometry.Curves;
using Aetheris.Kernel.Core.Geometry.Surfaces;
using Aetheris.Kernel.Core.Math;
+using Aetheris.Kernel.Core.Step242;
+using Aetheris.Kernel.Core.Tests.Step242;
namespace Aetheris.Kernel.Core.Tests.Brep.Queries;
@@ -78,6 +80,56 @@ public sealed class AnalyticDisplayPacketBuilderTests
Assert.Empty(packet.FallbackFaces);
}
+ [Fact]
+ public void Build_TrimmedSphereFace_RoutesToUnsupportedTrimFallback()
+ {
+ var body = CreateTrimmedSphereBody();
+
+ var sphereFaceIds = body.Topology.Faces
+ .Select(face => face.Id)
+ .Where(faceId => body.GetFaceSurface(faceId).Kind == SurfaceGeometryKind.Sphere)
+ .ToArray();
+ Assert.NotEmpty(sphereFaceIds);
+ Assert.All(sphereFaceIds, faceId => Assert.NotEmpty(body.Topology.GetFace(faceId).LoopIds));
+
+ var packet = AnalyticDisplayPacketBuilder.Build(body);
+
+ Assert.DoesNotContain(packet.AnalyticFaces, face => face.SurfaceKind == SurfaceGeometryKind.Sphere);
+ Assert.Contains(packet.FallbackFaces, face => face.SurfaceKind == SurfaceGeometryKind.Sphere && face.Reason == AnalyticDisplayFallbackReason.UnsupportedTrim);
+ }
+
+ [Fact]
+ public void Build_TrimmedTorusFace_RoutesToUnsupportedTrimFallback()
+ {
+ var body = CreateTrimmedTorusBody();
+
+ var torusFaceIds = body.Topology.Faces
+ .Select(face => face.Id)
+ .Where(faceId => body.GetFaceSurface(faceId).Kind == SurfaceGeometryKind.Torus)
+ .ToArray();
+ Assert.NotEmpty(torusFaceIds);
+ Assert.All(torusFaceIds, faceId => Assert.NotEmpty(body.Topology.GetFace(faceId).LoopIds));
+
+ var packet = AnalyticDisplayPacketBuilder.Build(body);
+
+ Assert.DoesNotContain(packet.AnalyticFaces, face => face.SurfaceKind == SurfaceGeometryKind.Torus);
+ Assert.Contains(packet.FallbackFaces, face => face.SurfaceKind == SurfaceGeometryKind.Torus && face.Reason == AnalyticDisplayFallbackReason.UnsupportedTrim);
+ }
+
+ [Fact]
+ public void Build_WholePrimitiveSphereAndTorus_RemainAnalytic()
+ {
+ var sphere = BrepPrimitives.CreateSphere(2d).Value;
+ var spherePacket = AnalyticDisplayPacketBuilder.Build(sphere);
+ Assert.Contains(spherePacket.AnalyticFaces, face => face.SurfaceKind == SurfaceGeometryKind.Sphere);
+ Assert.DoesNotContain(spherePacket.FallbackFaces, face => face.SurfaceKind == SurfaceGeometryKind.Sphere);
+
+ var torus = BrepPrimitives.CreateTorus(4d, 1d).Value;
+ var torusPacket = AnalyticDisplayPacketBuilder.Build(torus);
+ Assert.Contains(torusPacket.AnalyticFaces, face => face.SurfaceKind == SurfaceGeometryKind.Torus);
+ Assert.DoesNotContain(torusPacket.FallbackFaces, face => face.SurfaceKind == SurfaceGeometryKind.Torus);
+ }
+
[Fact]
public void Build_UnsupportedBsplineFace_RoutesDeterministicFallbackReason()
{
@@ -207,6 +259,25 @@ public sealed class AnalyticDisplayPacketBuilderTests
return result.Value;
}
+ private static BrepBody CreateTrimmedSphereBody()
+ {
+ return ImportFromCorpus("testdata/step242/nist/STC/nist_stc_06_asme1_ap242-e3.stp");
+ }
+
+ private static BrepBody CreateTrimmedTorusBody()
+ {
+ return ImportFromCorpus("testdata/step242/nist/FTC/nist_ftc_11_asme1_ap242-e2.stp");
+ }
+
+ private static BrepBody ImportFromCorpus(string relativePath)
+ {
+ var absolutePath = Path.Combine(Step242CorpusManifestRunner.RepoRoot(), relativePath.Replace('/', Path.DirectorySeparatorChar));
+ var text = File.ReadAllText(absolutePath);
+ var import = Step242Importer.ImportBody(text);
+ Assert.True(import.IsSuccess);
+ return import.Value;
+ }
+
private static BrepBody CreateCone(double bottomRadius, double topRadius, double height)
{
var frame = new ExtrudeFrame3D(
diff --git a/Aetheris.Kernel.Core.Tests/Brep/Queries/AnalyticDisplayQueryTests.cs b/Aetheris.Kernel.Core.Tests/Brep/Queries/AnalyticDisplayQueryTests.cs
index 6c262ff6..f2c5f6b5 100644
--- a/Aetheris.Kernel.Core.Tests/Brep/Queries/AnalyticDisplayQueryTests.cs
+++ b/Aetheris.Kernel.Core.Tests/Brep/Queries/AnalyticDisplayQueryTests.cs
@@ -6,6 +6,8 @@ using Aetheris.Kernel.Core.Geometry;
using Aetheris.Kernel.Core.Geometry.Curves;
using Aetheris.Kernel.Core.Geometry.Surfaces;
using Aetheris.Kernel.Core.Math;
+using Aetheris.Kernel.Core.Step242;
+using Aetheris.Kernel.Core.Tests.Step242;
namespace Aetheris.Kernel.Core.Tests.Brep.Queries;
@@ -137,6 +139,43 @@ public sealed class AnalyticDisplayQueryTests
Assert.Equal(Aetheris.Kernel.Core.Geometry.SurfaceGeometryKind.Torus, hit.SurfaceKind);
}
+ [Fact]
+ public void TrimmedSphereFace_QueryRejectsAndMatchesPacketFallback()
+ {
+ var body = CreateTrimmedSphereBody();
+ var sphereFaceId = body.Topology.Faces
+ .Select(face => face.Id)
+ .First(faceId => body.GetFaceSurface(faceId).Kind == SurfaceGeometryKind.Sphere);
+ Assert.NotEmpty(body.Topology.GetFace(sphereFaceId).LoopIds);
+
+ var packet = AnalyticDisplayPacketBuilder.Build(body);
+ var ray = new Ray3D(new Point3D(-20d, 0d, 0d), Direction3D.Create(new Vector3D(1d, 0d, 0d)));
+
+ Assert.False(AnalyticDisplayQuery.TryIntersectFace(body, sphereFaceId, ray, out _));
+ Assert.Contains(packet.FallbackFaces, face => face.FaceId == sphereFaceId && face.Reason == AnalyticDisplayFallbackReason.UnsupportedTrim);
+ Assert.DoesNotContain(packet.AnalyticFaces, face => face.FaceId == sphereFaceId);
+ }
+
+ [Fact]
+ public void TrimmedTorusFace_QueryRejectsAndMatchesPacketFallback_Deterministically()
+ {
+ var body = CreateTrimmedTorusBody();
+ var torusFaceId = body.Topology.Faces
+ .Select(face => face.Id)
+ .First(faceId => body.GetFaceSurface(faceId).Kind == SurfaceGeometryKind.Torus);
+ Assert.NotEmpty(body.Topology.GetFace(torusFaceId).LoopIds);
+
+ var packetFirst = AnalyticDisplayPacketBuilder.Build(body);
+ var packetSecond = AnalyticDisplayPacketBuilder.Build(body);
+ var ray = new Ray3D(new Point3D(-20d, 0d, 0d), Direction3D.Create(new Vector3D(1d, 0d, 0d)));
+
+ Assert.False(AnalyticDisplayQuery.TryIntersectFace(body, torusFaceId, ray, out _));
+ Assert.False(AnalyticDisplayQuery.TryIntersectFace(body, torusFaceId, ray, out _));
+ Assert.Contains(packetFirst.FallbackFaces, face => face.FaceId == torusFaceId && face.Reason == AnalyticDisplayFallbackReason.UnsupportedTrim);
+ Assert.Contains(packetSecond.FallbackFaces, face => face.FaceId == torusFaceId && face.Reason == AnalyticDisplayFallbackReason.UnsupportedTrim);
+ Assert.Equal(PacketSignature(packetFirst), PacketSignature(packetSecond));
+ }
+
[Fact]
public void BodyLevelNearestHit_BoxAndDeterminism()
{
@@ -295,6 +334,32 @@ public sealed class AnalyticDisplayQueryTests
return result.Value;
}
+ private static BrepBody CreateTrimmedSphereBody()
+ {
+ return ImportFromCorpus("testdata/step242/nist/STC/nist_stc_06_asme1_ap242-e3.stp");
+ }
+
+ private static BrepBody CreateTrimmedTorusBody()
+ {
+ return ImportFromCorpus("testdata/step242/nist/FTC/nist_ftc_11_asme1_ap242-e2.stp");
+ }
+
+ private static BrepBody ImportFromCorpus(string relativePath)
+ {
+ var absolutePath = Path.Combine(Step242CorpusManifestRunner.RepoRoot(), relativePath.Replace('/', Path.DirectorySeparatorChar));
+ var text = File.ReadAllText(absolutePath);
+ var import = Step242Importer.ImportBody(text);
+ Assert.True(import.IsSuccess);
+ return import.Value;
+ }
+
+ private static string PacketSignature(AnalyticDisplayPacket packet)
+ {
+ var analytic = string.Join("|", packet.AnalyticFaces.Select(face => $"{face.FaceId.Value}:{face.ShellId.Value}:{face.ShellRole}:{face.SurfaceKind}:{face.LoopCount}:{face.DomainHint?.MinV}:{face.DomainHint?.MaxV}"));
+ var fallback = string.Join("|", packet.FallbackFaces.Select(face => $"{face.FaceId.Value}:{face.ShellId.Value}:{face.ShellRole}:{face.Reason}:{face.SurfaceKind}"));
+ return $"{packet.BodyId.Value};A[{analytic}];F[{fallback}]";
+ }
+
private static BrepBody CreateCone(double bottomRadius, double topRadius, double height)
{
var frame = new ExtrudeFrame3D(
diff --git a/Aetheris.Kernel.Core/Brep/Queries/AnalyticDisplayPacket.cs b/Aetheris.Kernel.Core/Brep/Queries/AnalyticDisplayPacket.cs
index 63c66524..97a7cbd3 100644
--- a/Aetheris.Kernel.Core/Brep/Queries/AnalyticDisplayPacket.cs
+++ b/Aetheris.Kernel.Core/Brep/Queries/AnalyticDisplayPacket.cs
@@ -1,5 +1,4 @@
using Aetheris.Kernel.Core.Geometry;
-using Aetheris.Kernel.Core.Geometry.Curves;
using Aetheris.Kernel.Core.Geometry.Surfaces;
using Aetheris.Kernel.Core.Math;
using Aetheris.Kernel.Core.Topology;
@@ -63,30 +62,18 @@ public static class AnalyticDisplayPacketBuilder
var faceIds = body.Topology.GetShell(shellId).FaceIds.OrderBy(faceId => faceId.Value);
foreach (var faceId in faceIds)
{
- if (!body.Bindings.TryGetFaceBinding(faceId, out var faceBinding))
+ if (!AnalyticDisplaySupportPolicy.TryGetSupportedSurface(body, faceId, out var surface, out var fallbackReason))
{
- fallbackFaces.Add(new AnalyticDisplayFallbackFaceEntry(faceId, shellId, shellRole, AnalyticDisplayFallbackReason.MissingFaceBinding));
- continue;
- }
-
- if (!body.Geometry.TryGetSurface(faceBinding.SurfaceGeometryId, out var surface) || surface is null)
- {
- fallbackFaces.Add(new AnalyticDisplayFallbackFaceEntry(faceId, shellId, shellRole, AnalyticDisplayFallbackReason.MissingSurfaceGeometry));
- continue;
- }
-
- if (!IsSupportedSurfaceKind(surface.Kind))
- {
- fallbackFaces.Add(new AnalyticDisplayFallbackFaceEntry(faceId, shellId, shellRole, AnalyticDisplayFallbackReason.UnsupportedSurfaceKind, surface.Kind));
- continue;
- }
-
- if (!IsSupportedTrim(body, faceId, surface.Kind))
- {
- fallbackFaces.Add(new AnalyticDisplayFallbackFaceEntry(faceId, shellId, shellRole, AnalyticDisplayFallbackReason.UnsupportedTrim, surface.Kind));
+ SurfaceGeometryKind? surfaceKind = body.Bindings.TryGetFaceBinding(faceId, out var binding)
+ && body.Geometry.TryGetSurface(binding.SurfaceGeometryId, out var candidate)
+ && candidate is not null
+ ? candidate.Kind
+ : (SurfaceGeometryKind?)null;
+ fallbackFaces.Add(new AnalyticDisplayFallbackFaceEntry(faceId, shellId, shellRole, fallbackReason, surfaceKind));
continue;
}
+ var faceBinding = body.Bindings.GetFaceBinding(faceId);
analyticFaces.Add(new AnalyticDisplayFaceEntry(
faceId,
shellId,
@@ -102,93 +89,6 @@ public static class AnalyticDisplayPacketBuilder
return new AnalyticDisplayPacket(bodyId, analyticFaces, fallbackFaces);
}
- private static bool IsSupportedSurfaceKind(SurfaceGeometryKind kind)
- => kind is SurfaceGeometryKind.Plane
- or SurfaceGeometryKind.Sphere
- or SurfaceGeometryKind.Cylinder
- or SurfaceGeometryKind.Cone
- or SurfaceGeometryKind.Torus;
-
- private static bool IsSupportedTrim(BrepBody body, FaceId faceId, SurfaceGeometryKind kind)
- {
- if (kind is SurfaceGeometryKind.Plane)
- {
- return IsSupportedPlanarTrim(body, faceId);
- }
-
- if (kind is SurfaceGeometryKind.Sphere or SurfaceGeometryKind.Torus)
- {
- return true;
- }
-
- var face = body.Topology.GetFace(faceId);
- if (face.LoopIds.Count != 1)
- {
- return false;
- }
-
- var loop = body.Topology.GetLoop(face.LoopIds[0]);
- foreach (var coedgeId in loop.CoedgeIds)
- {
- var edgeId = body.Topology.GetCoedge(coedgeId).EdgeId;
- if (!body.Bindings.TryGetEdgeBinding(edgeId, out var edgeBinding)
- || !edgeBinding.TrimInterval.HasValue
- || !body.Geometry.TryGetCurve(edgeBinding.CurveGeometryId, out var curve)
- || curve is null
- || (curve.Kind is not CurveGeometryKind.Line3 and not CurveGeometryKind.Circle3))
- {
- return false;
- }
- }
-
- return true;
- }
-
-
- private static bool IsSupportedPlanarTrim(BrepBody body, FaceId faceId)
- {
- if (!body.Bindings.TryGetFaceBinding(faceId, out var faceBinding)
- || !body.Geometry.TryGetSurface(faceBinding.SurfaceGeometryId, out var surface)
- || surface?.Plane is not PlaneSurface plane
- || !body.Topology.TryGetFace(faceId, out var face)
- || face is null)
- {
- return false;
- }
-
- var normal = plane.Normal.ToVector();
- var axisAlignedPlane = double.Abs(normal.X) > 0.9d || double.Abs(normal.Y) > 0.9d || double.Abs(normal.Z) > 0.9d;
- if (axisAlignedPlane)
- {
- return true;
- }
-
- if (face.LoopIds.Count != 1)
- {
- return false;
- }
-
- var loop = body.Topology.GetLoop(face.LoopIds[0]);
- if (loop.CoedgeIds.Count == 0)
- {
- return false;
- }
-
- foreach (var coedgeId in loop.CoedgeIds)
- {
- var edgeId = body.Topology.GetCoedge(coedgeId).EdgeId;
- if (!body.Bindings.TryGetEdgeBinding(edgeId, out var edgeBinding)
- || !body.Geometry.TryGetCurve(edgeBinding.CurveGeometryId, out var curve)
- || curve is null
- || curve.Kind != CurveGeometryKind.Circle3)
- {
- return false;
- }
- }
-
- return true;
- }
-
private static AnalyticDisplayFaceDomainHint? TryResolveDomainHint(BrepBody body, FaceId faceId, SurfaceGeometry surface)
{
switch (surface.Kind)
diff --git a/Aetheris.Kernel.Core/Brep/Queries/AnalyticDisplayQuery.cs b/Aetheris.Kernel.Core/Brep/Queries/AnalyticDisplayQuery.cs
index 192224ba..3179b9c6 100644
--- a/Aetheris.Kernel.Core/Brep/Queries/AnalyticDisplayQuery.cs
+++ b/Aetheris.Kernel.Core/Brep/Queries/AnalyticDisplayQuery.cs
@@ -71,9 +71,7 @@ public static class AnalyticDisplayQuery
var context = tolerance ?? ToleranceContext.Default;
hit = default;
- if (!body.Bindings.TryGetFaceBinding(faceId, out var binding)
- || !body.Geometry.TryGetSurface(binding.SurfaceGeometryId, out var surface)
- || surface is null)
+ if (!AnalyticDisplaySupportPolicy.TryGetSupportedSurface(body, faceId, out var surface, out _))
{
return false;
}
diff --git a/Aetheris.Kernel.Core/Brep/Queries/AnalyticDisplaySupportPolicy.cs b/Aetheris.Kernel.Core/Brep/Queries/AnalyticDisplaySupportPolicy.cs
new file mode 100644
index 00000000..d84a3bf4
--- /dev/null
+++ b/Aetheris.Kernel.Core/Brep/Queries/AnalyticDisplaySupportPolicy.cs
@@ -0,0 +1,171 @@
+using Aetheris.Kernel.Core.Geometry;
+using Aetheris.Kernel.Core.Geometry.Curves;
+using Aetheris.Kernel.Core.Topology;
+
+namespace Aetheris.Kernel.Core.Brep.Queries;
+
+internal static class AnalyticDisplaySupportPolicy
+{
+ internal static bool TryGetSupportedSurface(
+ BrepBody body,
+ FaceId faceId,
+ out SurfaceGeometry surface,
+ out AnalyticDisplayFallbackReason fallbackReason)
+ {
+ surface = null!;
+ fallbackReason = AnalyticDisplayFallbackReason.MissingFaceBinding;
+
+ if (!body.Bindings.TryGetFaceBinding(faceId, out var faceBinding))
+ {
+ return false;
+ }
+
+ if (!body.Geometry.TryGetSurface(faceBinding.SurfaceGeometryId, out var candidate) || candidate is null)
+ {
+ fallbackReason = AnalyticDisplayFallbackReason.MissingSurfaceGeometry;
+ return false;
+ }
+
+ if (!IsSupportedSurfaceKind(candidate.Kind))
+ {
+ fallbackReason = AnalyticDisplayFallbackReason.UnsupportedSurfaceKind;
+ return false;
+ }
+
+ if (!IsSupportedTrim(body, faceId, candidate.Kind))
+ {
+ fallbackReason = AnalyticDisplayFallbackReason.UnsupportedTrim;
+ return false;
+ }
+
+ surface = candidate;
+ return true;
+ }
+
+ private static bool IsSupportedSurfaceKind(SurfaceGeometryKind kind)
+ => kind is SurfaceGeometryKind.Plane
+ or SurfaceGeometryKind.Sphere
+ or SurfaceGeometryKind.Cylinder
+ or SurfaceGeometryKind.Cone
+ or SurfaceGeometryKind.Torus;
+
+ private static bool IsSupportedTrim(BrepBody body, FaceId faceId, SurfaceGeometryKind kind)
+ {
+ if (kind is SurfaceGeometryKind.Plane)
+ {
+ return IsSupportedPlanarTrim(body, faceId);
+ }
+
+ if (kind is SurfaceGeometryKind.Sphere or SurfaceGeometryKind.Torus)
+ {
+ return kind switch
+ {
+ SurfaceGeometryKind.Sphere => body.Topology.GetFace(faceId).LoopIds.Count == 0,
+ SurfaceGeometryKind.Torus => IsSupportedWholeNativeTorus(body, faceId),
+ _ => false,
+ };
+ }
+
+ var face = body.Topology.GetFace(faceId);
+ if (face.LoopIds.Count != 1)
+ {
+ return false;
+ }
+
+ var loop = body.Topology.GetLoop(face.LoopIds[0]);
+ foreach (var coedgeId in loop.CoedgeIds)
+ {
+ var edgeId = body.Topology.GetCoedge(coedgeId).EdgeId;
+ if (!body.Bindings.TryGetEdgeBinding(edgeId, out var edgeBinding)
+ || !edgeBinding.TrimInterval.HasValue
+ || !body.Geometry.TryGetCurve(edgeBinding.CurveGeometryId, out var curve)
+ || curve is null
+ || (curve.Kind is not CurveGeometryKind.Line3 and not CurveGeometryKind.Circle3))
+ {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ private static bool IsSupportedWholeNativeTorus(BrepBody body, FaceId faceId)
+ {
+ var face = body.Topology.GetFace(faceId);
+ if (face.LoopIds.Count != 1)
+ {
+ return false;
+ }
+
+ var loop = body.Topology.GetLoop(face.LoopIds[0]);
+ if (loop.CoedgeIds.Count != 4)
+ {
+ return false;
+ }
+
+ foreach (var coedgeId in loop.CoedgeIds)
+ {
+ var edgeId = body.Topology.GetCoedge(coedgeId).EdgeId;
+ if (!body.Bindings.TryGetEdgeBinding(edgeId, out var edgeBinding)
+ || !edgeBinding.TrimInterval.HasValue
+ || !body.Geometry.TryGetCurve(edgeBinding.CurveGeometryId, out var curve)
+ || curve is null
+ || curve.Kind != CurveGeometryKind.Circle3)
+ {
+ return false;
+ }
+
+ var trim = edgeBinding.TrimInterval.Value;
+ if (double.Abs(trim.Start) > 1e-9d || double.Abs(trim.End - (2d * double.Pi)) > 1e-9d)
+ {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ private static bool IsSupportedPlanarTrim(BrepBody body, FaceId faceId)
+ {
+ if (!body.Bindings.TryGetFaceBinding(faceId, out var faceBinding)
+ || !body.Geometry.TryGetSurface(faceBinding.SurfaceGeometryId, out var surface)
+ || surface?.Plane is not Geometry.Surfaces.PlaneSurface plane
+ || !body.Topology.TryGetFace(faceId, out var face)
+ || face is null)
+ {
+ return false;
+ }
+
+ var normal = plane.Normal.ToVector();
+ var axisAlignedPlane = double.Abs(normal.X) > 0.9d || double.Abs(normal.Y) > 0.9d || double.Abs(normal.Z) > 0.9d;
+ if (axisAlignedPlane)
+ {
+ return true;
+ }
+
+ if (face.LoopIds.Count != 1)
+ {
+ return false;
+ }
+
+ var loop = body.Topology.GetLoop(face.LoopIds[0]);
+ if (loop.CoedgeIds.Count == 0)
+ {
+ return false;
+ }
+
+ foreach (var coedgeId in loop.CoedgeIds)
+ {
+ var edgeId = body.Topology.GetCoedge(coedgeId).EdgeId;
+ if (!body.Bindings.TryGetEdgeBinding(edgeId, out var edgeBinding)
+ || !body.Geometry.TryGetCurve(edgeBinding.CurveGeometryId, out var curve)
+ || curve is null
+ || curve.Kind != CurveGeometryKind.Circle3)
+ {
+ return false;
+ }
+ }
+
+ return true;
+ }
+}