Skip to content

Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel

Add analytic containment consensus diagnostics scaffold

Yuechen Li62220d8abbe7
diff --git a/Aetheris.CLI.Tests/CliBaselineTests.cs b/Aetheris.CLI.Tests/CliBaselineTests.cs
index 589d3219..75d8d60c 100644
--- a/Aetheris.CLI.Tests/CliBaselineTests.cs
+++ b/Aetheris.CLI.Tests/CliBaselineTests.cs
@@ -749,14 +749,15 @@ public sealed class CliBaselineTests
}
[Fact]
- public void AnalyzeVolume_ApproximateCurvedMixedVolume_FailsClearlyWhenContainmentUnsupported()
+ public void AnalyzeVolume_ApproximateCurvedMixedVolume_ReturnsNumericEstimate()
{
var stepPath = Path.Combine(RepoRoot, "testdata/firmament/exports/boolean_box_cylinder_hole.step");
var stdout = new StringWriter(); var stderr = new StringWriter();
var exitCode = Aetheris.CLI.CliRunner.Run(["analyze", "volume", stepPath, "--approximate", "--resolution", "48", "--json"], stdout, stderr);
- Assert.Equal(1, exitCode);
+ Assert.Equal(0, exitCode);
using var doc = JsonDocument.Parse(stdout.ToString());
- Assert.Contains("unsupported", doc.RootElement.GetProperty("error").GetString(), StringComparison.OrdinalIgnoreCase);
+ Assert.True(doc.RootElement.GetProperty("success").GetBoolean());
+ Assert.True(doc.RootElement.GetProperty("volume").GetDouble() > 0d);
}
[Fact]
diff --git a/Aetheris.Kernel.Core.Tests/Brep/Queries/BrepSpatialQueriesPointClassificationTests.cs b/Aetheris.Kernel.Core.Tests/Brep/Queries/BrepSpatialQueriesPointClassificationTests.cs
index 6117084c..8e7cabd0 100644
--- a/Aetheris.Kernel.Core.Tests/Brep/Queries/BrepSpatialQueriesPointClassificationTests.cs
+++ b/Aetheris.Kernel.Core.Tests/Brep/Queries/BrepSpatialQueriesPointClassificationTests.cs
@@ -4,12 +4,14 @@ using Aetheris.Kernel.Core.Brep.Boolean;
using Aetheris.Kernel.Core.Brep.Queries;
using Aetheris.Kernel.Core.Math;
using Aetheris.Kernel.Core.Numerics;
+using Aetheris.Kernel.Core.Step242;
using Aetheris.Kernel.Core.Topology;
namespace Aetheris.Kernel.Core.Tests.Brep.Queries;
public sealed class BrepSpatialQueriesPointClassificationTests
{
+ private static readonly string RepoRoot = FindRepoRoot();
[Fact]
public void Box_ClassifyPoint_CoversInsideOutsideBoundaryAndNearBoundaryTolerance()
{
@@ -91,4 +93,46 @@ public sealed class BrepSpatialQueriesPointClassificationTests
Assert.Contains(result.Diagnostics, d => d.Message.Contains("supports primitive", StringComparison.Ordinal));
Assert.Contains(result.Diagnostics, d => d.Message.Contains("strategy selected: unknown", StringComparison.Ordinal));
}
+
+ [Fact]
+ public void BoxMinusCylinder_ClassifyPoint_OutsideAndBoundaryishRemainConservative()
+ {
+ var body = ImportFixtureBody("testdata/firmament/exports/boolean_box_cylinder_hole.step");
+ var outside = BrepSpatialQueries.ClassifyPoint(body, new Point3D(8d, 0d, 0d)).Value;
+ Assert.True(outside is PointContainment.Outside or PointContainment.Unknown);
+
+ var boundaryish = BrepSpatialQueries.ClassifyPoint(body, new Point3D(3d, 0d, 0d)).Value;
+ Assert.True(boundaryish is PointContainment.Boundary or PointContainment.Unknown);
+ }
+
+ [Fact]
+ public void BoxMinusCylinder_DiagnosticTrace_CapturesRayConsensusDetails()
+ {
+ var body = ImportFixtureBody("testdata/firmament/exports/boolean_box_cylinder_hole.step");
+ var trace = BrepSpatialQueries.TraceMultiAxisConsensus(body, new Point3D(0d, 0d, 0d));
+ Assert.True(trace.ProviderAvailable);
+ Assert.Equal(6, trace.Rays.Count);
+ Assert.Contains(trace.Rays, r => r.Admissible);
+ Assert.All(trace.Rays, r => Assert.False(string.IsNullOrWhiteSpace(r.Axis)));
+ }
+
+ private static BrepBody ImportFixtureBody(string relativePath)
+ {
+ var fullPath = Path.Combine(RepoRoot, relativePath.Replace('/', Path.DirectorySeparatorChar));
+ var import = Step242Importer.ImportBody(File.ReadAllText(fullPath));
+ Assert.True(import.IsSuccess, string.Join(Environment.NewLine, import.Diagnostics.Select(d => d.Message)));
+ return import.Value;
+ }
+
+ private static string FindRepoRoot()
+ {
+ var dir = AppContext.BaseDirectory;
+ while (!string.IsNullOrWhiteSpace(dir))
+ {
+ if (File.Exists(Path.Combine(dir, "Aetheris.sln"))) return dir;
+ dir = Directory.GetParent(dir)?.FullName ?? string.Empty;
+ }
+
+ throw new DirectoryNotFoundException("Could not locate repository root.");
+ }
}
diff --git a/Aetheris.Kernel.Core/Brep/Queries/BrepSpatialQueries.cs b/Aetheris.Kernel.Core/Brep/Queries/BrepSpatialQueries.cs
index 9c679ec1..d8927d71 100644
--- a/Aetheris.Kernel.Core/Brep/Queries/BrepSpatialQueries.cs
+++ b/Aetheris.Kernel.Core/Brep/Queries/BrepSpatialQueries.cs
@@ -501,10 +501,55 @@ public static class BrepSpatialQueries
}
+ internal sealed record ContainmentRayHitTrace(
+ double T,
+ int FaceId,
+ string SurfaceKind,
+ string DomainClassification,
+ string HitQuality,
+ bool SeamDuplicateRisk,
+ bool NearEdge,
+ bool NearVertex,
+ string DomainSource,
+ string DomainReason);
+
+ internal sealed record ContainmentRayTrace(
+ string Axis,
+ int RawHitCount,
+ int CleanHitCount,
+ int DuplicateOrCoincidentCount,
+ bool HasSeamRisk,
+ bool HasBoundaryHit,
+ bool HasAmbiguousHit,
+ bool Admissible,
+ string? InadmissibleReason,
+ string? Vote,
+ IReadOnlyList<ContainmentRayHitTrace> Hits);
+
+ internal sealed record ContainmentConsensusTrace(
+ PointContainment FinalClassification,
+ bool ProviderAvailable,
+ string? Candidate,
+ IReadOnlyList<ContainmentRayTrace> Rays);
+
+ internal static ContainmentConsensusTrace TraceMultiAxisConsensus(BrepBody body, Point3D point, ToleranceContext? tolerance = null)
+ {
+ var context = tolerance ?? ToleranceContext.Default;
+ var resolvedPrimitive = TryResolvePrimitive(body, out var primitive, out var primitiveDiagnostic);
+ var containmentContext = BuildPointContainmentContext(body, point, context, resolvedPrimitive, primitive, primitiveDiagnostic);
+ var classification = ClassifyByMultiAxisRayConsensus(body, containmentContext, out var rays);
+ return new ContainmentConsensusTrace(classification, containmentContext.AnalyticRayProvider is not null, "multi_axis_ray_consensus", rays);
+ }
+
private static PointContainment ClassifyByMultiAxisRayConsensus(BrepBody body, PointContainmentContext context)
+ => ClassifyByMultiAxisRayConsensus(body, context, out _);
+
+ private static PointContainment ClassifyByMultiAxisRayConsensus(BrepBody body, PointContainmentContext context, out IReadOnlyList<ContainmentRayTrace> rayTraces)
{
+ var traces = new List<ContainmentRayTrace>();
if (context.AnalyticRayProvider is null)
{
+ rayTraces = traces;
return PointContainment.Unknown;
}
@@ -513,14 +558,65 @@ public static class BrepSpatialQueries
foreach (var dir in dirs)
{
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 != AnalyticHitQuality.Clean)) continue;
+ if (hits.Any(h => h.Domain.Classification == FaceDomainClassification.OnBoundary))
+ {
+ traces.Add(BuildTrace(dir, hits, admissible: false, "boundary-hit", vote: null));
+ rayTraces = traces;
+ return PointContainment.Boundary;
+ }
+ if (hits.Any(h => h.Quality is AnalyticHitQuality.Ambiguous or AnalyticHitQuality.UnsupportedSurface or AnalyticHitQuality.SeamDuplicateRisk))
+ {
+ traces.Add(BuildTrace(dir, hits, admissible: false, "ambiguous-or-unsupported-hit", vote: null));
+ continue;
+ }
+ if (hits.Any(h => h.Quality == AnalyticHitQuality.DuplicateOrCoincident))
+ {
+ traces.Add(BuildTrace(dir, hits, admissible: false, "duplicate-or-coincident-hit", vote: null));
+ continue;
+ }
var count = hits.Count(h => h.T > context.Tolerance.Linear);
- votes.Add((count % 2) == 1 ? PointContainment.Inside : PointContainment.Outside);
+ var vote = (count % 2) == 1 ? PointContainment.Inside : PointContainment.Outside;
+ votes.Add(vote);
+ traces.Add(BuildTrace(dir, hits, admissible: true, null, vote.ToString()));
}
+ rayTraces = traces;
if (votes.Count < 3) return PointContainment.Unknown;
return votes.All(v=>v==votes[0]) ? votes[0] : PointContainment.Unknown;
+
+ static string Axis(Direction3D d) => d switch
+ {
+ _ when d.X > 0.5d => "+X",
+ _ when d.X < -0.5d => "-X",
+ _ when d.Y > 0.5d => "+Y",
+ _ when d.Y < -0.5d => "-Y",
+ _ when d.Z > 0.5d => "+Z",
+ _ => "-Z"
+ };
+
+ static ContainmentRayTrace BuildTrace(Direction3D direction, IReadOnlyList<AnalyticRayHit> hits, bool admissible, string? inadmissibleReason, string? vote)
+ => new(
+ Axis(direction),
+ hits.Count,
+ hits.Count(h => h.Quality == AnalyticHitQuality.Clean),
+ hits.Count(h => h.Quality == AnalyticHitQuality.DuplicateOrCoincident),
+ hits.Any(h => h.Quality == AnalyticHitQuality.SeamDuplicateRisk),
+ hits.Any(h => h.Quality == AnalyticHitQuality.BoundaryHit),
+ hits.Any(h => h.Quality == AnalyticHitQuality.Ambiguous),
+ admissible,
+ inadmissibleReason,
+ vote,
+ hits.Select(h => new ContainmentRayHitTrace(
+ h.T,
+ h.FaceId.Value,
+ h.SurfaceKind.ToString(),
+ h.Domain.Classification.ToString(),
+ h.Quality.ToString(),
+ h.Domain.SeamDuplicateRisk,
+ h.Domain.NearEdge,
+ h.Domain.NearVertex,
+ h.Domain.Source,
+ h.Domain.Reason)).ToList());
}
private static bool TryCreateAnalyticRayProvider(BrepBody body, ToleranceContext tolerance, out AnalyticRayProvider? provider, out string reason)