Skip to content

Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel

Update BSpline regression expectations with runtime-verified blockers

Yuechen Li3a7e68440297
diff --git a/Aetheris.Kernel.Core.Tests/Geometry/BSplineSurfaceWithKnotsTests.cs b/Aetheris.Kernel.Core.Tests/Geometry/BSplineSurfaceWithKnotsTests.cs
new file mode 100644
index 00000000..a592a5f7
--- /dev/null
+++ b/Aetheris.Kernel.Core.Tests/Geometry/BSplineSurfaceWithKnotsTests.cs
@@ -0,0 +1,54 @@
+using Aetheris.Kernel.Core.Geometry.Surfaces;
+using Aetheris.Kernel.Core.Math;
+
+namespace Aetheris.Kernel.Core.Tests.Geometry;
+
+public sealed class BSplineSurfaceWithKnotsTests
+{
+ [Fact]
+ public void Evaluate_AtDomainCorners_ReturnsCornerControlPoints()
+ {
+ var surface = CreateBilinearSurface();
+
+ Assert.Equal(new Point3D(0d, 0d, 0d), surface.Evaluate(surface.DomainStartU, surface.DomainStartV));
+ Assert.Equal(new Point3D(0d, 1d, 0d), surface.Evaluate(surface.DomainStartU, surface.DomainEndV));
+ Assert.Equal(new Point3D(1d, 0d, 0d), surface.Evaluate(surface.DomainEndU, surface.DomainStartV));
+ Assert.Equal(new Point3D(1d, 1d, 1d), surface.Evaluate(surface.DomainEndU, surface.DomainEndV));
+ }
+
+ [Fact]
+ public void Evaluate_IsDeterministic_ForRepeatedCalls()
+ {
+ var surface = CreateBilinearSurface();
+ var u = (surface.DomainStartU + surface.DomainEndU) * 0.5d;
+ var v = (surface.DomainStartV + surface.DomainEndV) * 0.25d;
+
+ var first = surface.Evaluate(u, v);
+ var second = surface.Evaluate(u, v);
+
+ Assert.Equal(first, second);
+ }
+
+ private static BSplineSurfaceWithKnots CreateBilinearSurface()
+ {
+ var controlPoints = new[]
+ {
+ new[] { new Point3D(0d, 0d, 0d), new Point3D(0d, 1d, 0d) },
+ new[] { new Point3D(1d, 0d, 0d), new Point3D(1d, 1d, 1d) }
+ };
+
+ return new BSplineSurfaceWithKnots(
+ degreeU: 1,
+ degreeV: 1,
+ controlPoints: controlPoints,
+ surfaceForm: "UNSPECIFIED",
+ uClosed: false,
+ vClosed: false,
+ selfIntersect: false,
+ knotMultiplicitiesU: new[] { 2, 2 },
+ knotMultiplicitiesV: new[] { 2, 2 },
+ knotValuesU: new[] { 0d, 1d },
+ knotValuesV: new[] { 0d, 1d },
+ knotSpec: "UNSPECIFIED");
+ }
+}
diff --git a/Aetheris.Kernel.Core.Tests/Step242/Step242BSplineSurfaceWithKnotsTests.cs b/Aetheris.Kernel.Core.Tests/Step242/Step242BSplineSurfaceWithKnotsTests.cs
index 76a85a9c..b8def56c 100644
--- a/Aetheris.Kernel.Core.Tests/Step242/Step242BSplineSurfaceWithKnotsTests.cs
+++ b/Aetheris.Kernel.Core.Tests/Step242/Step242BSplineSurfaceWithKnotsTests.cs
@@ -61,14 +61,13 @@ public sealed class Step242BSplineSurfaceWithKnotsTests
Assert.False(string.IsNullOrWhiteSpace(run.FirstDiagnostic.MessagePrefix));
if (string.Equals(relativePath, "testdata/step242/nist/CTC/nist_ctc_02_asme1_ap242-e2.stp", StringComparison.Ordinal))
{
- Assert.Equal("tessellator", run.FirstFailureLayer);
- Assert.Equal(string.Empty, run.FirstDiagnostic.Source);
+ Assert.Equal("exporter", run.FirstFailureLayer);
Assert.DoesNotContain("sphere tessellation supports only untrimmed sphere faces with zero loops.", run.FirstDiagnostic.MessagePrefix, StringComparison.Ordinal);
}
}
[Theory]
- [InlineData("testdata/step242/nist/CTC/nist_ctc_02_asme1_ap242-e2.stp", "tessellator", "", "Face 74 has unsupported surface kind 'BSplineSurfaceWithKnots'.")]
+ [InlineData("testdata/step242/nist/CTC/nist_ctc_02_asme1_ap242-e2.stp", "exporter", "Face:1", "")]
[InlineData("testdata/step242/nist/FTC/nist_ftc_07_asme1_ap242-e2.stp", "tessellator", "", "Face 20 curved tessellation supports repeated torus/revolved families with mixed line/circle loops; this topology family is still unsupported. Observed")]
[InlineData("testdata/step242/nist/STC/nist_stc_08_asme1_ap242-e3.stp", "tessellator", "", "Face 62 curved tessellation does not support this torus/revolved boundary topology yet. Observed")]
public void Step242_TrimmedSphereTargets_AdvancePastOldUntrimmedSphereBlocker_AndRemainDeterministic(
@@ -93,7 +92,15 @@ public sealed class Step242BSplineSurfaceWithKnotsTests
Assert.DoesNotContain("sphere tessellation supports only untrimmed sphere faces with zero loops.", first.FirstDiagnostic.MessagePrefix, StringComparison.Ordinal);
Assert.Equal(expectedLayer, first.FirstFailureLayer);
Assert.Equal(expectedSource, first.FirstDiagnostic.Source);
- Assert.StartsWith(expectedMessagePrefix, first.FirstDiagnostic.MessagePrefix, StringComparison.Ordinal);
+ if (!string.IsNullOrEmpty(expectedMessagePrefix))
+ {
+ Assert.StartsWith(expectedMessagePrefix, first.FirstDiagnostic.MessagePrefix, StringComparison.Ordinal);
+ }
+
+ if (string.Equals(relativePath, "testdata/step242/nist/CTC/nist_ctc_02_asme1_ap242-e2.stp", StringComparison.Ordinal))
+ {
+ Assert.DoesNotContain("unsupported surface kind 'BSplineSurfaceWithKnots'", first.FirstDiagnostic.MessagePrefix, StringComparison.Ordinal);
+ }
Assert.Equal(first.FirstFailureLayer, second.FirstFailureLayer);
Assert.Equal(first.FirstDiagnostic.Source, second.FirstDiagnostic.Source);
Assert.Equal(first.FirstDiagnostic.MessagePrefix, second.FirstDiagnostic.MessagePrefix);
diff --git a/Aetheris.Kernel.Core/Brep/Tessellation/BrepDisplayTessellator.cs b/Aetheris.Kernel.Core/Brep/Tessellation/BrepDisplayTessellator.cs
index e640ddf8..312aca12 100644
--- a/Aetheris.Kernel.Core/Brep/Tessellation/BrepDisplayTessellator.cs
+++ b/Aetheris.Kernel.Core/Brep/Tessellation/BrepDisplayTessellator.cs
@@ -19,6 +19,7 @@ public static class BrepDisplayTessellator
private const string CylinderTrimDegenerateSource = "Viewer.Tessellation.CylinderTrimDegenerate";
private const string CylinderTrimAmbiguousUsedShorterSpanSource = "Viewer.Tessellation.CylinderTrimAmbiguousUsedShorterSpan";
private const string CurvedTopologyUnsupportedSource = "Viewer.Tessellation.CurvedTopologyUnsupported";
+ private const string BSplineSurfaceTrimUnsupportedSource = "Viewer.Tessellation.BSplineSurfaceTrimUnsupported";
public static KernelResult<DisplayTessellationResult> Tessellate(BrepBody body, DisplayTessellationOptions? options = null)
{
@@ -79,6 +80,7 @@ public static class BrepDisplayTessellator
SurfaceGeometryKind.Cone => TessellateConeFace(body, faceId, surface.Cone!.Value, options),
SurfaceGeometryKind.Sphere => TessellateSphereFace(body, faceId, surface.Sphere!.Value, options),
SurfaceGeometryKind.Torus => TessellateTorusFace(body, faceId, surface.Torus!.Value, options),
+ SurfaceGeometryKind.BSplineSurfaceWithKnots => TessellateBSplineSurfaceFace(body, faceId, surface.BSplineSurfaceWithKnots!, options),
_ => KernelResult<DisplayFaceMeshPatch>.Failure([CreateNotImplemented($"Face {faceId.Value} has unsupported surface kind '{surface.Kind}'.")]),
};
}
@@ -302,6 +304,221 @@ public static class BrepDisplayTessellator
parameters.Value.VEnd));
}
+
+ private static KernelResult<DisplayFaceMeshPatch> TessellateBSplineSurfaceFace(BrepBody body, FaceId faceId, BSplineSurfaceWithKnots surface, DisplayTessellationOptions options)
+ {
+ var loopIds = body.GetLoopIds(faceId);
+ if (loopIds.Count > 1)
+ {
+ return KernelResult<DisplayFaceMeshPatch>.Failure([
+ CreateNotImplemented($"Face {faceId.Value} BSpline surface tessellation currently supports zero or one trim loop. Observed {loopIds.Count} loops.", BSplineSurfaceTrimUnsupportedSource)]);
+ }
+
+ var uStart = surface.DomainStartU;
+ var uEnd = surface.DomainEndU;
+ var vStart = surface.DomainStartV;
+ var vEnd = surface.DomainEndV;
+
+ if (loopIds.Count == 1)
+ {
+ var trimPatch = TryResolveBSplineTrimPatch(body, faceId, surface, loopIds[0]);
+ if (!trimPatch.IsSuccess)
+ {
+ return KernelResult<DisplayFaceMeshPatch>.Failure(trimPatch.Diagnostics);
+ }
+
+ uStart = trimPatch.Value.UStart;
+ uEnd = trimPatch.Value.UEnd;
+ vStart = trimPatch.Value.VStart;
+ vEnd = trimPatch.Value.VEnd;
+ }
+
+ var uSpan = uEnd - uStart;
+ var vSpan = vEnd - vStart;
+ var uSegments = System.Math.Max(options.MinimumSegments, System.Math.Clamp((int)double.Ceiling(double.Abs(uSpan) / options.AngularToleranceRadians), options.MinimumSegments, options.MaximumSegments));
+ var vSegments = System.Math.Max(1, System.Math.Clamp((int)double.Ceiling(double.Abs(vSpan) / options.ChordTolerance), 1, options.MaximumSegments));
+
+ return KernelResult<DisplayFaceMeshPatch>.Success(CreateBoundedGridPatch(
+ faceId,
+ uSegments,
+ vSegments,
+ (u, v) => surface.Evaluate(u, v),
+ (u, v) => EvaluateBSplineNormal(surface, u, v),
+ uStart,
+ uEnd,
+ vStart,
+ vEnd));
+ }
+
+ private static KernelResult<(double UStart, double UEnd, double VStart, double VEnd)> TryResolveBSplineTrimPatch(
+ BrepBody body,
+ FaceId faceId,
+ BSplineSurfaceWithKnots surface,
+ LoopId loopId)
+ {
+ const double minSpan = 1e-8d;
+
+ var coedges = body.GetCoedgeIds(loopId).Select(id => body.Topology.GetCoedge(id)).ToArray();
+ if (coedges.Length < 3)
+ {
+ return KernelResult<(double, double, double, double)>.Failure([
+ CreateNotImplemented($"Face {faceId.Value} BSpline trim loop must contain at least three coedges. Observed {coedges.Length}.", BSplineSurfaceTrimUnsupportedSource)]);
+ }
+
+ var vertexPointsResult = BuildLoopVertexPointLookup(body, coedges, faceId);
+ if (!vertexPointsResult.IsSuccess)
+ {
+ return KernelResult<(double, double, double, double)>.Failure(vertexPointsResult.Diagnostics);
+ }
+
+ var samples = new List<Point3D>();
+ foreach (var coedge in coedges)
+ {
+ var endpoints = GetEdgeEndpoints(body, coedge.EdgeId, coedge.IsReversed, vertexPointsResult.Value);
+ if (!endpoints.IsSuccess)
+ {
+ return KernelResult<(double, double, double, double)>.Failure(endpoints.Diagnostics);
+ }
+
+ samples.Add(endpoints.Value.Start);
+ samples.Add(endpoints.Value.End);
+
+ var edgePolyline = TessellateEdge(body, coedge.EdgeId, DisplayTessellationOptions.Default);
+ if (!edgePolyline.IsSuccess)
+ {
+ continue;
+ }
+
+ var edgePoints = coedge.IsReversed
+ ? edgePolyline.Value.Points.Reverse().ToArray()
+ : edgePolyline.Value.Points;
+ samples.AddRange(edgePoints);
+ }
+
+ if (samples.Count == 0)
+ {
+ return KernelResult<(double, double, double, double)>.Failure([
+ CreateNotImplemented($"Face {faceId.Value} BSpline trim tessellation could not derive loop samples.", BSplineSurfaceTrimUnsupportedSource)]);
+ }
+
+ var uvSamples = samples
+ .Select(point => TryProjectPointToBSplineUv(surface, point))
+ .Where(result => result.HasValue)
+ .Select(result => result!.Value)
+ .ToArray();
+
+ if (uvSamples.Length == 0)
+ {
+ return KernelResult<(double, double, double, double)>.Failure([
+ CreateNotImplemented($"Face {faceId.Value} BSpline trim tessellation could not project loop samples into the BSpline parametric domain.", BSplineSurfaceTrimUnsupportedSource)]);
+ }
+
+ var uStart = System.Math.Max(surface.DomainStartU, uvSamples.Min(s => s.U));
+ var uEnd = System.Math.Min(surface.DomainEndU, uvSamples.Max(s => s.U));
+ var vStart = System.Math.Max(surface.DomainStartV, uvSamples.Min(s => s.V));
+ var vEnd = System.Math.Min(surface.DomainEndV, uvSamples.Max(s => s.V));
+
+ if ((uEnd - uStart) <= minSpan || (vEnd - vStart) <= minSpan)
+ {
+ return KernelResult<(double, double, double, double)>.Failure([
+ CreateNotImplemented($"Face {faceId.Value} BSpline trim tessellation derived a degenerate UV span.", BSplineSurfaceTrimUnsupportedSource)]);
+ }
+
+ return KernelResult<(double, double, double, double)>.Success((uStart, uEnd, vStart, vEnd));
+ }
+
+ private static (double U, double V)? TryProjectPointToBSplineUv(BSplineSurfaceWithKnots surface, Point3D point)
+ {
+ var uStart = surface.DomainStartU;
+ var uEnd = surface.DomainEndU;
+ var vStart = surface.DomainStartV;
+ var vEnd = surface.DomainEndV;
+
+ const int coarseSegments = 6;
+ var bestU = uStart;
+ var bestV = vStart;
+ var bestDistanceSquared = double.PositiveInfinity;
+
+ for (var iu = 0; iu <= coarseSegments; iu++)
+ {
+ var tu = (double)iu / coarseSegments;
+ var u = uStart + ((uEnd - uStart) * tu);
+ for (var iv = 0; iv <= coarseSegments; iv++)
+ {
+ var tv = (double)iv / coarseSegments;
+ var v = vStart + ((vEnd - vStart) * tv);
+ var sample = surface.Evaluate(u, v);
+ var delta = sample - point;
+ var distanceSquared = delta.Dot(delta);
+ if (distanceSquared < bestDistanceSquared)
+ {
+ bestDistanceSquared = distanceSquared;
+ bestU = u;
+ bestV = v;
+ }
+ }
+ }
+
+ var uStep = (uEnd - uStart) / coarseSegments;
+ var vStep = (vEnd - vStart) / coarseSegments;
+
+ for (var iteration = 0; iteration < 3; iteration++)
+ {
+ var nextUStep = uStep * 0.5d;
+ var nextVStep = vStep * 0.5d;
+ var localBestU = bestU;
+ var localBestV = bestV;
+ var localBestDistance = bestDistanceSquared;
+
+ for (var du = -1; du <= 1; du++)
+ {
+ var u = System.Math.Clamp(bestU + (du * uStep), uStart, uEnd);
+ for (var dv = -1; dv <= 1; dv++)
+ {
+ var v = System.Math.Clamp(bestV + (dv * vStep), vStart, vEnd);
+ var sample = surface.Evaluate(u, v);
+ var delta = sample - point;
+ var distanceSquared = delta.Dot(delta);
+ if (distanceSquared < localBestDistance)
+ {
+ localBestDistance = distanceSquared;
+ localBestU = u;
+ localBestV = v;
+ }
+ }
+ }
+
+ bestU = localBestU;
+ bestV = localBestV;
+ bestDistanceSquared = localBestDistance;
+ uStep = nextUStep;
+ vStep = nextVStep;
+ }
+
+ return (bestU, bestV);
+ }
+
+ private static Vector3D EvaluateBSplineNormal(BSplineSurfaceWithKnots surface, double u, double v)
+ {
+ var du = System.Math.Max((surface.DomainEndU - surface.DomainStartU) * 1e-4d, 1e-6d);
+ var dv = System.Math.Max((surface.DomainEndV - surface.DomainStartV) * 1e-4d, 1e-6d);
+
+ var uMinus = System.Math.Clamp(u - du, surface.DomainStartU, surface.DomainEndU);
+ var uPlus = System.Math.Clamp(u + du, surface.DomainStartU, surface.DomainEndU);
+ var vMinus = System.Math.Clamp(v - dv, surface.DomainStartV, surface.DomainEndV);
+ var vPlus = System.Math.Clamp(v + dv, surface.DomainStartV, surface.DomainEndV);
+
+ var tangentU = surface.Evaluate(uPlus, v) - surface.Evaluate(uMinus, v);
+ var tangentV = surface.Evaluate(u, vPlus) - surface.Evaluate(u, vMinus);
+ var normal = tangentU.Cross(tangentV);
+ if (!normal.TryNormalize(out var normalized))
+ {
+ return new Vector3D(0d, 0d, 1d);
+ }
+
+ return normalized;
+ }
+
private static KernelResult<DisplayFaceMeshPatch> TessellateSphereFace(BrepBody body, FaceId faceId, SphereSurface sphere, DisplayTessellationOptions options)
{
var trimPatchResult = TryResolveSphereTrimPatch(body, faceId, sphere);
diff --git a/Aetheris.Kernel.Core/Geometry/Surfaces/BSplineSurfaceWithKnots.cs b/Aetheris.Kernel.Core/Geometry/Surfaces/BSplineSurfaceWithKnots.cs
index cd9b1ab9..f6ecb658 100644
--- a/Aetheris.Kernel.Core/Geometry/Surfaces/BSplineSurfaceWithKnots.cs
+++ b/Aetheris.Kernel.Core/Geometry/Surfaces/BSplineSurfaceWithKnots.cs
@@ -91,6 +91,50 @@ public sealed record BSplineSurfaceWithKnots
public IReadOnlyList<double> FullKnotsV { get; }
public string KnotSpec { get; }
+ public double DomainStartU => FullKnotsU[DegreeU];
+
+ public double DomainEndU => FullKnotsU[FullKnotsU.Count - DegreeU - 1];
+
+ public double DomainStartV => FullKnotsV[DegreeV];
+
+ public double DomainEndV => FullKnotsV[FullKnotsV.Count - DegreeV - 1];
+
+ public Point3D Evaluate(double u, double v)
+ {
+ if (!double.IsFinite(u) || !double.IsFinite(v))
+ {
+ throw new ArgumentOutOfRangeException(nameof(u), "Parameters must be finite.");
+ }
+
+ var uClamped = ClampToDomain(u, DomainStartU, DomainEndU);
+ var vClamped = ClampToDomain(v, DomainStartV, DomainEndV);
+
+ if (double.Abs(uClamped - DomainEndU) <= 1e-12d && double.Abs(vClamped - DomainEndV) <= 1e-12d)
+ {
+ return ControlPoints[^1][^1];
+ }
+
+ var rowPoints = new Point3D[ControlPoints.Count];
+ for (var i = 0; i < ControlPoints.Count; i++)
+ {
+ rowPoints[i] = EvaluateCurve(
+ ControlPoints[i],
+ DegreeV,
+ FullKnotsV,
+ DomainStartV,
+ DomainEndV,
+ vClamped);
+ }
+
+ return EvaluateCurve(
+ rowPoints,
+ DegreeU,
+ FullKnotsU,
+ DomainStartU,
+ DomainEndU,
+ uClamped);
+ }
+
private static IReadOnlyList<double> ExpandKnots(IReadOnlyList<int> multiplicities, IReadOnlyList<double> values)
{
if (multiplicities is null || values is null || multiplicities.Count == 0 || multiplicities.Count != values.Count)
@@ -125,4 +169,90 @@ public sealed record BSplineSurfaceWithKnots
}
}
}
+
+ private static Point3D EvaluateCurve(
+ IReadOnlyList<Point3D> controlPoints,
+ int degree,
+ IReadOnlyList<double> fullKnots,
+ double domainStart,
+ double domainEnd,
+ double parameter)
+ {
+ var u = ClampToDomain(parameter, domainStart, domainEnd);
+ if (double.Abs(u - domainEnd) <= 1e-12d)
+ {
+ return controlPoints[^1];
+ }
+
+ var span = FindSpan(controlPoints.Count, degree, fullKnots, u);
+ var d = new Point3D[degree + 1];
+ for (var j = 0; j <= degree; j++)
+ {
+ d[j] = controlPoints[span - degree + j];
+ }
+
+ for (var r = 1; r <= degree; r++)
+ {
+ for (var j = degree; j >= r; j--)
+ {
+ var leftKnot = fullKnots[span - degree + j];
+ var rightKnot = fullKnots[span + 1 + j - r];
+ var denominator = rightKnot - leftKnot;
+ var alpha = double.Abs(denominator) <= 1e-15d ? 0d : (u - leftKnot) / denominator;
+ d[j] = Lerp(d[j - 1], d[j], alpha);
+ }
+ }
+
+ return d[degree];
+ }
+
+ private static int FindSpan(int controlPointCount, int degree, IReadOnlyList<double> fullKnots, double parameter)
+ {
+ var n = controlPointCount - 1;
+ if (parameter >= fullKnots[n + 1])
+ {
+ return n;
+ }
+
+ var low = degree;
+ var high = n + 1;
+ var mid = (low + high) / 2;
+ while (parameter < fullKnots[mid] || parameter >= fullKnots[mid + 1])
+ {
+ if (parameter < fullKnots[mid])
+ {
+ high = mid;
+ }
+ else
+ {
+ low = mid;
+ }
+
+ mid = (low + high) / 2;
+ }
+
+ return mid;
+ }
+
+ private static double ClampToDomain(double parameter, double start, double end)
+ {
+ if (parameter <= start)
+ {
+ return start;
+ }
+
+ if (parameter >= end)
+ {
+ return end;
+ }
+
+ return parameter;
+ }
+
+ private static Point3D Lerp(Point3D left, Point3D right, double alpha)
+ {
+ var clamped = System.Math.Clamp(alpha, 0d, 1d);
+ var delta = right - left;
+ return left + (delta * clamped);
+ }
}