Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
Add initial curved face-domain admission and analytic ray provider
Yuechen Li1074d435210e
diff --git a/Aetheris.Kernel.Core.Tests/Brep/Queries/FaceDomainQueryTests.cs b/Aetheris.Kernel.Core.Tests/Brep/Queries/FaceDomainQueryTests.csindex d3cd951f..6ba94c59 100644--- a/Aetheris.Kernel.Core.Tests/Brep/Queries/FaceDomainQueryTests.cs+++ b/Aetheris.Kernel.Core.Tests/Brep/Queries/FaceDomainQueryTests.cs@@ -49,17 +49,23 @@ public sealed class FaceDomainQueryTests}[Fact]- public void CurvedFace_Unsupported_ReturnsStructuredReason()+ public void CylinderFace_PointOnSurfaceWithinTrim_ReturnsInsideOrAmbiguous(){var cylinder = BrepPrimitives.CreateCylinder(2d, 6d).Value;var faceId = cylinder.Topology.Faces.Single(f => cylinder.GetFaceSurface(f.Id).Kind == SurfaceGeometryKind.Cylinder).Id;var result = FaceDomainQuery.TryClassifyPointOnFace(cylinder, faceId, new Point3D(2d, 0d, 0d));- Assert.False(result.IsSuccess);+ Assert.Equal("FaceDomainQuery.Cylinder", result.Source);+ Assert.Contains(result.Classification, new[] { FaceDomainClassification.Inside, FaceDomainClassification.Ambiguous });+ }++ [Fact]+ public void TorusFace_Unsupported_ReturnsStructuredReason()+ {+ var torus = BrepPrimitives.CreateTorus(4d, 1d).Value;+ var faceId = torus.Topology.Faces.First().Id;+ var result = FaceDomainQuery.TryClassifyPointOnFace(torus, faceId, new Point3D(5d, 0d, 0d));Assert.Equal(FaceDomainClassification.Unsupported, result.Classification);- Assert.True(result.UnsupportedSurface);- Assert.Contains("not implemented", result.Reason, StringComparison.OrdinalIgnoreCase);- Assert.Equal("FaceDomainQuery.UnsupportedCylinder", result.Source);}}diff --git a/Aetheris.Kernel.Core/Brep/Queries/BrepSpatialQueries.cs b/Aetheris.Kernel.Core/Brep/Queries/BrepSpatialQueries.csindex 4e46adc4..9c679ec1 100644--- a/Aetheris.Kernel.Core/Brep/Queries/BrepSpatialQueries.cs+++ b/Aetheris.Kernel.Core/Brep/Queries/BrepSpatialQueries.cs@@ -99,7 +99,7 @@ public static class BrepSpatialQueriesKernelDiagnostic primitiveDiagnostic){var hasPrimitive = resolvedPrimitive && primitive.Kind is PrimitiveKind.Box or PrimitiveKind.Cylinder or PrimitiveKind.Sphere;- var planarProvider = TryCreatePlanarRayProvider(body, tolerance, out var provider, out var unavailableReason);+ var analyticProvider = TryCreateAnalyticRayProvider(body, tolerance, out var provider, out var unavailableReason);return new PointContainmentContext(point,new BoundingBox3D(point, point),@@ -107,8 +107,8 @@ public static class BrepSpatialQuerieshasPrimitive,primitive,primitiveDiagnostic with { Severity = KernelDiagnosticSeverity.Warning },- GenericRayHitProviderAvailable: planarProvider,- PlanarRayProvider: provider,+ GenericRayHitProviderAvailable: analyticProvider,+ AnalyticRayProvider: provider,BoundaryFirstUnavailableReason: "Boundary-first containment is scaffolded but no trusted trimmed-face boundary distance query is available yet.",PlanarClosedShellUnavailableReason: "Planar closed-shell exact containment is scaffolded but not enabled in this milestone.",MultiAxisRayConsensusUnavailableReason: unavailableReason);@@ -141,7 +141,7 @@ public static class BrepSpatialQueriesPrimitiveDescriptor Primitive,KernelDiagnostic PrimitiveDiagnostic,bool GenericRayHitProviderAvailable,- PlanarRayProvider? PlanarRayProvider,+ AnalyticRayProvider? AnalyticRayProvider,string BoundaryFirstUnavailableReason,string PlanarClosedShellUnavailableReason,string MultiAxisRayConsensusUnavailableReason);@@ -503,7 +503,7 @@ public static class BrepSpatialQueriesprivate static PointContainment ClassifyByMultiAxisRayConsensus(BrepBody body, PointContainmentContext context){- if (context.PlanarRayProvider is null)+ if (context.AnalyticRayProvider is null){return PointContainment.Unknown;}@@ -512,9 +512,9 @@ public static class BrepSpatialQueriesvar votes = new List<PointContainment>();foreach (var dir in dirs){- var hits = context.PlanarRayProvider.Collect(new Ray3D(context.Point, dir));+ var hits = context.AnalyticRayProvider.Collect(new Ray3D(context.Point, dir));if (hits.Any(h => h.Domain.Classification == FaceDomainClassification.OnBoundary)) return PointContainment.Boundary;- if (hits.Any(h => h.Quality != PlanarHitQuality.Clean)) continue;+ if (hits.Any(h => h.Quality != AnalyticHitQuality.Clean)) continue;var count = hits.Count(h => h.T > context.Tolerance.Linear);votes.Add((count % 2) == 1 ? PointContainment.Inside : PointContainment.Outside);}@@ -523,57 +523,53 @@ public static class BrepSpatialQueriesreturn votes.All(v=>v==votes[0]) ? votes[0] : PointContainment.Unknown;}- private static bool TryCreatePlanarRayProvider(BrepBody body, ToleranceContext tolerance, out PlanarRayProvider? provider, out string reason)+ private static bool TryCreateAnalyticRayProvider(BrepBody body, ToleranceContext tolerance, out AnalyticRayProvider? provider, out string reason){provider = null;- var planarFaces = new List<(FaceId FaceId, PlaneSurface Plane)>();+ var faces = new List<(FaceId FaceId, SurfaceGeometry Surface)>();foreach (var binding in body.Bindings.FaceBindings){var surface = body.Geometry.GetSurface(binding.SurfaceGeometryId);- if (surface.Kind != SurfaceGeometryKind.Plane || surface.Plane is null)+ if (surface.Kind is not (SurfaceGeometryKind.Plane or SurfaceGeometryKind.Cylinder or SurfaceGeometryKind.Sphere or SurfaceGeometryKind.Cone or SurfaceGeometryKind.Torus)){- reason = "Generic planar ray-hit provider requires all faces to be planar in this milestone.";+ reason = "Generic analytic ray-hit provider requires analytic faces only (plane/cylinder/sphere/cone/torus).";return false;}- planarFaces.Add((binding.FaceId, surface.Plane.Value));+ faces.Add((binding.FaceId, surface));}- if (planarFaces.Count == 0)+ if (faces.Count == 0){reason = "Body has no bound faces.";return false;}- provider = new PlanarRayProvider(body, planarFaces, tolerance);+ provider = new AnalyticRayProvider(body, faces, tolerance);reason = "available";return true;}- private enum PlanarHitQuality { Clean, BoundaryHit, Ambiguous, NearTangent, UnsupportedSurface, DuplicateOrCoincident }- private sealed record PlanarRayHit(double T, Point3D Point, FaceId FaceId, SurfaceGeometryKind SurfaceKind, Direction3D Normal, FaceDomainQueryResult Domain, PlanarHitQuality Quality);- private sealed class PlanarRayProvider(BrepBody body, IReadOnlyList<(FaceId FaceId, PlaneSurface Plane)> faces, ToleranceContext tolerance)+ private enum AnalyticHitQuality { Clean, BoundaryHit, Ambiguous, NearTangent, UnsupportedSurface, DuplicateOrCoincident, SeamDuplicateRisk }+ private sealed record AnalyticRayHit(double T, Point3D Point, FaceId FaceId, SurfaceGeometryKind SurfaceKind, Direction3D Normal, FaceDomainQueryResult Domain, AnalyticHitQuality Quality);+ private sealed class AnalyticRayProvider(BrepBody body, IReadOnlyList<(FaceId FaceId, SurfaceGeometry Surface)> faces, ToleranceContext tolerance){- public IReadOnlyList<PlanarRayHit> Collect(Ray3D ray)+ public IReadOnlyList<AnalyticRayHit> Collect(Ray3D ray){- var hits = new List<PlanarRayHit>();- foreach (var (faceId, plane) in faces)+ var hits = new List<AnalyticRayHit>();+ foreach (var (faceId, surface) in faces){- var denom = plane.Normal.ToVector().Dot(ray.Direction.ToVector());- if (double.Abs(denom) <= tolerance.Linear) continue;- var t = (plane.Origin - ray.Origin).Dot(plane.Normal.ToVector()) / denom;- if (t < -tolerance.Linear) continue;- var c = ToleranceMath.ClampToZero(t, tolerance.Linear);- var p = ray.PointAt(c);- var domain = FaceDomainQuery.TryClassifyPointOnFace(body, faceId, p, tolerance);- if (domain.Classification == FaceDomainClassification.Outside) continue;+ if (!AnalyticDisplayQuery.TryIntersectFace(body, faceId, ray, out var hit, tolerance: tolerance)) continue;+ var domain = FaceDomainQuery.TryClassifyPointOnFace(body, faceId, hit.Position, tolerance);+ if (domain.Classification is FaceDomainClassification.Outside) continue;var q = domain.Classification switch{- FaceDomainClassification.Inside => PlanarHitQuality.Clean,- FaceDomainClassification.OnBoundary => PlanarHitQuality.BoundaryHit,- FaceDomainClassification.Unsupported => PlanarHitQuality.UnsupportedSurface,- _ => PlanarHitQuality.Ambiguous+ FaceDomainClassification.Inside => domain.SeamDuplicateRisk ? AnalyticHitQuality.SeamDuplicateRisk : AnalyticHitQuality.Clean,+ FaceDomainClassification.OnBoundary => AnalyticHitQuality.BoundaryHit,+ FaceDomainClassification.Unsupported => AnalyticHitQuality.UnsupportedSurface,+ FaceDomainClassification.Ambiguous => AnalyticHitQuality.Ambiguous,+ _ => AnalyticHitQuality.Ambiguous};- hits.Add(new PlanarRayHit(c, p, faceId, SurfaceGeometryKind.Plane, plane.Normal, domain, q));+ hits.Add(new AnalyticRayHit(hit.Distance, hit.Position, faceId, surface.Kind, hit.Normal, domain, q));}var ordered = hits.OrderBy(h=>h.T).ToList();@@ -581,8 +577,8 @@ public static class BrepSpatialQueries{if (ToleranceMath.AlmostEqual(ordered[i-1].T, ordered[i].T, tolerance)){- ordered[i-1] = ordered[i-1] with { Quality = PlanarHitQuality.DuplicateOrCoincident };- ordered[i] = ordered[i] with { Quality = PlanarHitQuality.DuplicateOrCoincident };+ ordered[i-1] = ordered[i-1] with { Quality = AnalyticHitQuality.DuplicateOrCoincident };+ ordered[i] = ordered[i] with { Quality = AnalyticHitQuality.DuplicateOrCoincident };}}return ordered;diff --git a/Aetheris.Kernel.Core/Brep/Queries/FaceDomainQuery.cs b/Aetheris.Kernel.Core/Brep/Queries/FaceDomainQuery.csindex 208a6f17..ea5930fc 100644--- a/Aetheris.Kernel.Core/Brep/Queries/FaceDomainQuery.cs+++ b/Aetheris.Kernel.Core/Brep/Queries/FaceDomainQuery.cs@@ -47,14 +47,94 @@ public static class FaceDomainQueryreturn surface.Kind switch{SurfaceGeometryKind.Plane when surface.Plane is PlaneSurface plane => ClassifyPlanar(body, faceId, plane, point, tol),- SurfaceGeometryKind.Cylinder => Unsupported(faceId, surface.Kind, "FaceDomainQuery.UnsupportedCylinder", "Surface projection/trim classification for cylindrical faces is not implemented in this milestone.", seamDuplicateRisk: true),- SurfaceGeometryKind.Sphere => Unsupported(faceId, surface.Kind, "FaceDomainQuery.UnsupportedSphere", "Surface projection/trim classification for spherical faces is not implemented in this milestone.", seamDuplicateRisk: true),+ SurfaceGeometryKind.Cylinder when surface.Cylinder is CylinderSurface cylinder => ClassifyCylinder(body, faceId, cylinder, point, tol),+ SurfaceGeometryKind.Sphere when surface.Sphere is SphereSurface sphere => ClassifySphere(faceId, sphere, point, tol),SurfaceGeometryKind.Cone => Unsupported(faceId, surface.Kind, "FaceDomainQuery.UnsupportedCone", "Surface projection/trim classification for conical faces is not implemented in this milestone.", seamDuplicateRisk: true),SurfaceGeometryKind.Torus => Unsupported(faceId, surface.Kind, "FaceDomainQuery.UnsupportedTorus", "Surface projection/trim classification for toroidal faces is not implemented in this milestone.", seamDuplicateRisk: true),SurfaceGeometryKind.BSplineSurfaceWithKnots => Unsupported(faceId, surface.Kind, "FaceDomainQuery.UnsupportedBSpline", "Surface projection/trim classification for bspline faces is not implemented in this milestone.", trimUnavailable: true),_ => Unsupported(faceId, surface.Kind, "FaceDomainQuery.UnsupportedSurface", "Surface kind is unsupported for face-domain query.")};}+ private static FaceDomainQueryResult ClassifySphere(FaceId faceId, SphereSurface sphere, Point3D point, ToleranceContext tolerance)+ {+ var local = point - sphere.Center;+ var r = local.Length;+ var residual = double.Abs(r - sphere.Radius);+ var u = double.Atan2(local.Dot(sphere.YAxis.ToVector()), local.Dot(sphere.XAxis.ToVector()));+ var v = double.Asin(double.Clamp(local.Dot(sphere.Axis.ToVector()) / double.Max(sphere.Radius, 1e-12d), -1d, 1d));+ var onBoundary = residual <= tolerance.Linear;+ var classification = onBoundary ? FaceDomainClassification.OnBoundary : FaceDomainClassification.Outside;+ return new FaceDomainQueryResult(+ IsSuccess: classification != FaceDomainClassification.Outside,+ faceId,+ SurfaceGeometryKind.Sphere,+ classification,+ (u, v),+ residual,+ NearEdge: onBoundary,+ NearVertex: false,+ SeamDuplicateRisk: double.Abs(double.Abs(u) - double.Pi) <= tolerance.Angular,+ TrimUnavailable: false,+ UnsupportedSurface: false,+ Source: "FaceDomainQuery.Sphere",+ Reason: classification.ToString());+ }++ private static FaceDomainQueryResult ClassifyCylinder(BrepBody body, FaceId faceId, CylinderSurface cylinder, Point3D point, ToleranceContext tolerance)+ {+ var axis = cylinder.Axis.ToVector();+ var delta = point - cylinder.Origin;+ var v = delta.Dot(axis);+ var radial = delta - (axis * v);+ var radialDistance = radial.Length;+ var residual = double.Abs(radialDistance - cylinder.Radius);+ ResolveAxialBounds(body, faceId, cylinder.Axis, cylinder.Origin, out var minV, out var maxV);+ if (minV.HasValue && (v < minV.Value - tolerance.Linear || v > maxV!.Value + tolerance.Linear))+ {+ return new FaceDomainQueryResult(false, faceId, SurfaceGeometryKind.Cylinder, FaceDomainClassification.Outside, (double.Atan2(radial.Dot(cylinder.YAxis.ToVector()), radial.Dot(cylinder.XAxis.ToVector())), v), residual, false, false, false, false, false, "FaceDomainQuery.Cylinder", "OutsideTrimVBounds");+ }++ var nearAxialBoundary = (minV.HasValue && double.Abs(v - minV.Value) <= tolerance.Linear)+ || (maxV.HasValue && double.Abs(v - maxV.Value) <= tolerance.Linear);+ var nearSurface = residual <= tolerance.Linear;+ var u = double.Atan2(radial.Dot(cylinder.YAxis.ToVector()), radial.Dot(cylinder.XAxis.ToVector()));+ var seamRisk = double.Abs(double.Abs(u) - double.Pi) <= tolerance.Angular;+ var classification = nearSurface+ ? (nearAxialBoundary || seamRisk ? FaceDomainClassification.Ambiguous : FaceDomainClassification.Inside)+ : FaceDomainClassification.Outside;+ return new FaceDomainQueryResult(+ classification != FaceDomainClassification.Ambiguous,+ faceId,+ SurfaceGeometryKind.Cylinder,+ classification,+ (u, v),+ residual,+ NearEdge: nearAxialBoundary,+ NearVertex: false,+ SeamDuplicateRisk: seamRisk,+ TrimUnavailable: false,+ UnsupportedSurface: false,+ Source: "FaceDomainQuery.Cylinder",+ Reason: classification.ToString());+ }++ private static void ResolveAxialBounds(BrepBody body, FaceId sideFaceId, Direction3D axis, Point3D axisOrigin, out double? minV, out double? maxV)+ {+ minV = null;+ maxV = null;+ var axisVector = axis.ToVector();+ foreach (var faceBinding in body.Bindings.FaceBindings)+ {+ if (faceBinding.FaceId == sideFaceId) continue;+ var surface = body.Geometry.GetSurface(faceBinding.SurfaceGeometryId);+ if (surface.Kind != SurfaceGeometryKind.Plane || surface.Plane is not PlaneSurface plane) continue;+ var dot = plane.Normal.ToVector().Dot(axisVector);+ if (double.Abs(double.Abs(dot) - 1d) > 1e-6d) continue;+ var pv = (plane.Origin - axisOrigin).Dot(axisVector);+ minV = !minV.HasValue ? pv : double.Min(minV.Value, pv);+ maxV = !maxV.HasValue ? pv : double.Max(maxV.Value, pv);+ }+ }private static FaceDomainQueryResult ClassifyPlanar(BrepBody body, FaceId faceId, PlaneSurface plane, Point3D point, ToleranceContext tolerance){