Skip to content

Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel

Add trim-aware torus tessellation

Yuechen Lie592bdefc265
diff --git a/Aetheris.Kernel.Core.Tests/Brep/Tessellation/BrepDisplayTessellatorTests.cs b/Aetheris.Kernel.Core.Tests/Brep/Tessellation/BrepDisplayTessellatorTests.cs
index 40c9b9af..6340cece 100644
--- a/Aetheris.Kernel.Core.Tests/Brep/Tessellation/BrepDisplayTessellatorTests.cs
+++ b/Aetheris.Kernel.Core.Tests/Brep/Tessellation/BrepDisplayTessellatorTests.cs
@@ -379,6 +379,127 @@ public sealed class BrepDisplayTessellatorTests
});
}
+ [Fact]
+ public void Tessellate_TrimmedTorusSurface_RespectsOuterLoop_Deterministically()
+ {
+ var torus = new TorusSurface(
+ Point3D.Origin,
+ Direction3D.Create(new Vector3D(0d, 0d, 1d)),
+ 4d,
+ 1.25d,
+ Direction3D.Create(new Vector3D(1d, 0d, 0d)));
+ var outerBounds = (UMin: 0.4d, UMax: 2.2d, VMin: 0.5d, VMax: 2.3d);
+ var body = CreateTrimmedTorusSurfaceBody(torus, outerBounds, holeBounds: null);
+ var options = DisplayTessellationOptions.Create(double.Pi / 8d, 0.2d, minimumSegments: 12, maximumSegments: 12).Value;
+
+ var first = BrepDisplayTessellator.Tessellate(body, options);
+ var second = BrepDisplayTessellator.Tessellate(body, options);
+
+ Assert.True(first.IsSuccess);
+ Assert.True(second.IsSuccess);
+ Assert.DoesNotContain(first.Diagnostics, diagnostic => diagnostic.Source == "Viewer.Tessellation.TrimEvaluationFailed");
+
+ var firstPatch = Assert.Single(first.Value.FacePatches);
+ var secondPatch = Assert.Single(second.Value.FacePatches);
+ Assert.NotEmpty(firstPatch.TriangleIndices);
+ Assert.Equal(firstPatch.Positions, secondPatch.Positions);
+ Assert.Equal(firstPatch.Normals, secondPatch.Normals);
+ Assert.Equal(firstPatch.TriangleIndices, secondPatch.TriangleIndices);
+
+ var outerMidU = (outerBounds.UMin + outerBounds.UMax) * 0.5d;
+ var outerMidV = (outerBounds.VMin + outerBounds.VMax) * 0.5d;
+ Assert.All(GetTriangleUvSamples(firstPatch, point => ProjectTorusUv(torus, point, outerMidU, outerMidV)), sample =>
+ Assert.True(IsInsideUvRectangle(sample, outerBounds)));
+ }
+
+ [Fact]
+ public void Tessellate_TrimmedTorusSurfaceWithHole_PreservesHole_Deterministically()
+ {
+ var torus = new TorusSurface(
+ Point3D.Origin,
+ Direction3D.Create(new Vector3D(0d, 0d, 1d)),
+ 4d,
+ 1.25d,
+ Direction3D.Create(new Vector3D(1d, 0d, 0d)));
+ var outerBounds = (UMin: 0.5d, UMax: 2.4d, VMin: 0.6d, VMax: 2.5d);
+ var holeBounds = (UMin: 1.1d, UMax: 1.7d, VMin: 1.2d, VMax: 1.9d);
+ var body = CreateTrimmedTorusSurfaceBody(torus, outerBounds, holeBounds);
+ var options = DisplayTessellationOptions.Create(double.Pi / 8d, 0.2d, minimumSegments: 12, maximumSegments: 12).Value;
+
+ var first = BrepDisplayTessellator.Tessellate(body, options);
+ var second = BrepDisplayTessellator.Tessellate(body, options);
+
+ Assert.True(first.IsSuccess);
+ Assert.True(second.IsSuccess);
+ Assert.DoesNotContain(first.Diagnostics, diagnostic => diagnostic.Source == "Viewer.Tessellation.TrimEvaluationFailed");
+
+ var firstPatch = Assert.Single(first.Value.FacePatches);
+ var secondPatch = Assert.Single(second.Value.FacePatches);
+ Assert.NotEmpty(firstPatch.TriangleIndices);
+ Assert.Equal(firstPatch.Positions, secondPatch.Positions);
+ Assert.Equal(firstPatch.Normals, secondPatch.Normals);
+ Assert.Equal(firstPatch.TriangleIndices, secondPatch.TriangleIndices);
+
+ var outerMidU = (outerBounds.UMin + outerBounds.UMax) * 0.5d;
+ var outerMidV = (outerBounds.VMin + outerBounds.VMax) * 0.5d;
+ Assert.All(GetTriangleUvCentroids(firstPatch, point => ProjectTorusUv(torus, point, outerMidU, outerMidV)), centroid =>
+ {
+ Assert.True(IsInsideUvRectangle(centroid, outerBounds));
+ Assert.False(IsInsideUvRectangle(centroid, holeBounds));
+ });
+ }
+
+ [Fact]
+ public void Tessellate_TrimmedTorusSurfaceAcrossSeam_UsesDeterministicUnwrap()
+ {
+ var torus = new TorusSurface(
+ Point3D.Origin,
+ Direction3D.Create(new Vector3D(0d, 0d, 1d)),
+ 4d,
+ 1.25d,
+ Direction3D.Create(new Vector3D(1d, 0d, 0d)));
+ var outerBounds = (UMin: 5.7d, UMax: 6.7d, VMin: 5.8d, VMax: 6.6d);
+ var body = CreateTrimmedTorusSurfaceBody(torus, outerBounds, holeBounds: null);
+ var options = DisplayTessellationOptions.Create(double.Pi / 8d, 0.2d, minimumSegments: 12, maximumSegments: 12).Value;
+
+ var result = BrepDisplayTessellator.Tessellate(body, options);
+
+ Assert.True(result.IsSuccess);
+ Assert.DoesNotContain(result.Diagnostics, diagnostic => diagnostic.Source == "Viewer.Tessellation.TrimEvaluationFailed");
+
+ var patch = Assert.Single(result.Value.FacePatches);
+ Assert.NotEmpty(patch.TriangleIndices);
+
+ var outerMidU = (outerBounds.UMin + outerBounds.UMax) * 0.5d;
+ var outerMidV = (outerBounds.VMin + outerBounds.VMax) * 0.5d;
+ Assert.All(GetTriangleUvCentroids(patch, point => ProjectTorusUv(torus, point, outerMidU, outerMidV)), centroid =>
+ Assert.True(IsInsideUvRectangle(centroid, outerBounds)));
+ }
+
+ [Fact]
+ public void Tessellate_TrimmedTorusSurface_WhenProjectionFails_EmitsWarningAndSkipsWithoutFallback()
+ {
+ var torus = new TorusSurface(
+ Point3D.Origin,
+ Direction3D.Create(new Vector3D(0d, 0d, 1d)),
+ 4d,
+ 1.25d,
+ Direction3D.Create(new Vector3D(1d, 0d, 0d)));
+ var outerBounds = (UMin: 0.4d, UMax: 2.2d, VMin: 0.5d, VMax: 2.3d);
+ var body = CreateTrimmedTorusSurfaceBody(torus, outerBounds, holeBounds: null, topRadialOffset: 0.4d);
+
+ var result = BrepDisplayTessellator.Tessellate(body);
+
+ Assert.True(result.IsSuccess);
+ var diagnostic = Assert.Single(result.Diagnostics);
+ Assert.Equal(KernelDiagnosticCode.ValidationFailed, diagnostic.Code);
+ Assert.Equal("Viewer.Tessellation.TrimEvaluationFailed", diagnostic.Source);
+
+ var patch = Assert.Single(result.Value.FacePatches);
+ Assert.Empty(patch.Positions);
+ Assert.Empty(patch.TriangleIndices);
+ }
+
[Fact]
public void Tessellate_TrimmedCylinderSurfaceAcrossSeam_UsesDeterministicUnwrap()
{
@@ -690,6 +811,36 @@ public sealed class BrepDisplayTessellatorTests
return new BrepBody(builder.Model, geometry, bindings, vertexPoints);
}
+ private static BrepBody CreateTrimmedTorusSurfaceBody(
+ TorusSurface torus,
+ (double UMin, double UMax, double VMin, double VMax) outerBounds,
+ (double UMin, double UMax, double VMin, double VMax)? holeBounds,
+ double topRadialOffset = 0d)
+ {
+ var builder = new TopologyBuilder();
+ var geometry = new BrepGeometryStore();
+ var bindings = new BrepBindingModel();
+ var vertexPoints = new Dictionary<VertexId, Point3D>();
+ var curveId = 1;
+ var loops = new List<LoopId>
+ {
+ AddTorusLoop(builder, geometry, bindings, vertexPoints, torus, outerBounds, ref curveId, topRadialOffset),
+ };
+
+ if (holeBounds is { } hole)
+ {
+ loops.Add(AddTorusLoop(builder, geometry, bindings, vertexPoints, torus, hole, ref curveId, topRadialOffset: 0d));
+ }
+
+ var face = builder.AddFace(loops);
+ var shell = builder.AddShell([face]);
+ builder.AddBody([shell]);
+
+ geometry.AddSurface(new SurfaceGeometryId(1), SurfaceGeometry.FromTorus(torus));
+ bindings.AddFaceBinding(new FaceGeometryBinding(face, new SurfaceGeometryId(1)));
+ return new BrepBody(builder.Model, geometry, bindings, vertexPoints);
+ }
+
private static BrepBody CreateTrimmedConeSurfaceBody(
ConeSurface cone,
(double UMin, double UMax, double VMin, double VMax) outerBounds,
@@ -756,6 +907,77 @@ public sealed class BrepDisplayTessellatorTests
cone.Evaluate,
v => CreateCircleAtConeV(cone, v));
+ private static LoopId AddTorusLoop(
+ TopologyBuilder builder,
+ BrepGeometryStore geometry,
+ BrepBindingModel bindings,
+ Dictionary<VertexId, Point3D> vertexPoints,
+ TorusSurface torus,
+ (double UMin, double UMax, double VMin, double VMax) bounds,
+ ref int curveId,
+ double topRadialOffset)
+ {
+ var vertices = new[]
+ {
+ EvaluateTorusPoint(torus, bounds.UMin, bounds.VMin, radialOffset: 0d),
+ EvaluateTorusPoint(torus, bounds.UMax, bounds.VMin, radialOffset: 0d),
+ EvaluateTorusPoint(torus, bounds.UMax, bounds.VMax, radialOffset: topRadialOffset),
+ EvaluateTorusPoint(torus, bounds.UMin, bounds.VMax, radialOffset: topRadialOffset),
+ };
+
+ var vertexIds = vertices
+ .Select(point =>
+ {
+ var vertexId = builder.AddVertex();
+ vertexPoints[vertexId] = point;
+ return vertexId;
+ })
+ .ToArray();
+
+ var loopId = builder.AllocateLoopId();
+ var edgeIds = new[]
+ {
+ builder.AddEdge(vertexIds[0], vertexIds[1]),
+ builder.AddEdge(vertexIds[1], vertexIds[2]),
+ builder.AddEdge(vertexIds[2], vertexIds[3]),
+ builder.AddEdge(vertexIds[3], vertexIds[0]),
+ };
+
+ geometry.AddCurve(new CurveGeometryId(curveId), CurveGeometry.FromCircle(CreateCircleAtTorusV(torus, bounds.VMin)));
+ bindings.AddEdgeBinding(new EdgeGeometryBinding(edgeIds[0], new CurveGeometryId(curveId++), new ParameterInterval(bounds.UMin, bounds.UMax)));
+
+ geometry.AddCurve(new CurveGeometryId(curveId), CurveGeometry.FromCircle(CreateCircleAtTorusU(torus, bounds.UMax)));
+ bindings.AddEdgeBinding(new EdgeGeometryBinding(edgeIds[1], new CurveGeometryId(curveId++), new ParameterInterval(bounds.VMin, bounds.VMax)));
+
+ geometry.AddCurve(new CurveGeometryId(curveId), CurveGeometry.FromCircle(CreateCircleAtTorusV(torus, bounds.VMax, topRadialOffset)));
+ bindings.AddEdgeBinding(new EdgeGeometryBinding(edgeIds[2], new CurveGeometryId(curveId++), new ParameterInterval(System.Math.Min(bounds.UMin, bounds.UMax), System.Math.Max(bounds.UMin, bounds.UMax))));
+
+ geometry.AddCurve(new CurveGeometryId(curveId), CurveGeometry.FromCircle(CreateCircleAtTorusU(torus, bounds.UMin)));
+ bindings.AddEdgeBinding(new EdgeGeometryBinding(edgeIds[3], new CurveGeometryId(curveId++), new ParameterInterval(bounds.VMin, bounds.VMax)));
+
+ var coedgeIds = new[]
+ {
+ builder.AllocateCoedgeId(),
+ builder.AllocateCoedgeId(),
+ builder.AllocateCoedgeId(),
+ builder.AllocateCoedgeId(),
+ };
+
+ for (var i = 0; i < coedgeIds.Length; i++)
+ {
+ builder.AddCoedge(new Coedge(
+ coedgeIds[i],
+ edgeIds[i],
+ loopId,
+ coedgeIds[(i + 1) % coedgeIds.Length],
+ coedgeIds[(i - 1 + coedgeIds.Length) % coedgeIds.Length],
+ IsReversed: false));
+ }
+
+ builder.AddLoop(new Loop(loopId, coedgeIds));
+ return loopId;
+ }
+
private static LoopId AddPeriodicRectangleLoop(
TopologyBuilder builder,
BrepGeometryStore geometry,
@@ -850,6 +1072,23 @@ public sealed class BrepDisplayTessellatorTests
v * System.Math.Tan(cone.SemiAngleRadians),
cone.ReferenceAxis);
+ private static Circle3Curve CreateCircleAtTorusV(TorusSurface torus, double v, double radialOffset = 0d)
+ => new(
+ torus.Center + (torus.Axis.ToVector() * (torus.MinorRadius * System.Math.Sin(v))),
+ torus.Axis,
+ torus.MajorRadius + (torus.MinorRadius * System.Math.Cos(v)) + radialOffset,
+ torus.XAxis);
+
+ private static Circle3Curve CreateCircleAtTorusU(TorusSurface torus, double u)
+ {
+ var majorDirection = (torus.XAxis.ToVector() * System.Math.Cos(u)) + (torus.YAxis.ToVector() * System.Math.Sin(u));
+ return new Circle3Curve(
+ torus.Center + (majorDirection * torus.MajorRadius),
+ Direction3D.Create(majorDirection.Cross(torus.Axis.ToVector())),
+ torus.MinorRadius,
+ Direction3D.Create(majorDirection));
+ }
+
private static IEnumerable<(double U, double V)> GetTriangleUvCentroids(
DisplayFaceMeshPatch patch,
Func<Point3D, (double U, double V)> projectPoint)
@@ -903,6 +1142,23 @@ public sealed class BrepDisplayTessellatorTests
return (UnwrapNearReference(angle, referenceU), axial);
}
+ private static Point3D EvaluateTorusPoint(TorusSurface torus, double u, double v, double radialOffset)
+ {
+ var majorDirection = (torus.XAxis.ToVector() * System.Math.Cos(u)) + (torus.YAxis.ToVector() * System.Math.Sin(u));
+ var radialDistance = torus.MajorRadius + ((torus.MinorRadius + radialOffset) * System.Math.Cos(v));
+ return torus.Center + (majorDirection * radialDistance) + (torus.Axis.ToVector() * ((torus.MinorRadius + radialOffset) * System.Math.Sin(v)));
+ }
+
+ private static (double U, double V) ProjectTorusUv(TorusSurface torus, Point3D point, double referenceU, double referenceV)
+ {
+ var offset = point - torus.Center;
+ var axial = offset.Dot(torus.Axis.ToVector());
+ var planar = offset - (torus.Axis.ToVector() * axial);
+ var u = System.Math.Atan2(planar.Dot(torus.YAxis.ToVector()), planar.Dot(torus.XAxis.ToVector()));
+ var v = System.Math.Atan2(axial, planar.Length - torus.MajorRadius);
+ return (UnwrapNearReference(u, referenceU), UnwrapNearReference(v, referenceV));
+ }
+
private static double UnwrapNearReference(double angle, double reference)
{
var candidate = angle;
diff --git a/Aetheris.Kernel.Core/Brep/Tessellation/BrepDisplayTessellator.cs b/Aetheris.Kernel.Core/Brep/Tessellation/BrepDisplayTessellator.cs
index da62b6b6..a80875ef 100644
--- a/Aetheris.Kernel.Core/Brep/Tessellation/BrepDisplayTessellator.cs
+++ b/Aetheris.Kernel.Core/Brep/Tessellation/BrepDisplayTessellator.cs
@@ -678,6 +678,98 @@ public static class BrepDisplayTessellator
return (bestU, bestV);
}
+ private static KernelResult<IReadOnlyList<IReadOnlyList<(double U, double V)>>> TryBuildDoublyPeriodicTrimmedSurfaceUvLoops(
+ BrepBody body,
+ FaceId faceId,
+ IReadOnlyList<LoopId> loopIds,
+ Func<Point3D, (double U, double V)?> projectPointToUv,
+ DisplayTessellationOptions options)
+ {
+ var uvLoops = new List<IReadOnlyList<(double U, double V)>>(loopIds.Count);
+ foreach (var loopId in loopIds.OrderBy(id => id.Value))
+ {
+ var uvLoop = TryBuildDoublyPeriodicTrimmedSurfaceUvLoop(body, faceId, loopId, projectPointToUv, options);
+ if (!uvLoop.IsSuccess)
+ {
+ return KernelResult<IReadOnlyList<IReadOnlyList<(double U, double V)>>>.Failure(uvLoop.Diagnostics);
+ }
+
+ uvLoops.Add(uvLoop.Value);
+ }
+
+ return KernelResult<IReadOnlyList<IReadOnlyList<(double U, double V)>>>.Success(uvLoops);
+ }
+
+ private static KernelResult<IReadOnlyList<(double U, double V)>> TryBuildDoublyPeriodicTrimmedSurfaceUvLoop(
+ BrepBody body,
+ FaceId faceId,
+ LoopId loopId,
+ Func<Point3D, (double U, double V)?> projectPointToUv,
+ DisplayTessellationOptions options)
+ {
+ var coedges = body.GetCoedgeIds(loopId).Select(id => body.Topology.GetCoedge(id)).ToArray();
+ if (coedges.Length < 1)
+ {
+ return KernelResult<IReadOnlyList<(double U, double V)>>.Failure([
+ CreateNotImplemented(
+ $"Face {faceId.Value} periodic trim loop must contain at least one coedge.",
+ TrimEvaluationFailedSource)]);
+ }
+
+ var vertexPointsResult = BuildLoopVertexPointLookup(body, coedges, faceId);
+ if (!vertexPointsResult.IsSuccess)
+ {
+ return KernelResult<IReadOnlyList<(double U, double V)>>.Failure(vertexPointsResult.Diagnostics);
+ }
+
+ var uvPoints = new List<(double U, double V)>();
+ double? previousWrappedU = null;
+ double? previousWrappedV = null;
+ double currentUOffset = 0d;
+ double currentVOffset = 0d;
+
+ foreach (var coedge in coedges)
+ {
+ var sampledCurve = TrySampleCoedgeForTrimEvaluation(body, faceId, coedge, vertexPointsResult.Value, options);
+ if (!sampledCurve.IsSuccess)
+ {
+ return KernelResult<IReadOnlyList<(double U, double V)>>.Failure(sampledCurve.Diagnostics);
+ }
+
+ foreach (var point in sampledCurve.Value)
+ {
+ var uv = projectPointToUv(point);
+ if (!uv.HasValue)
+ {
+ return KernelResult<IReadOnlyList<(double U, double V)>>.Failure([
+ CreateNotImplemented(
+ $"Face {faceId.Value} periodic trim tessellation could not project loop samples into the native torus parameter domain.",
+ TrimEvaluationFailedSource)]);
+ }
+
+ var unwrapResult = TryUnwrapDoublyPeriodicUvPoint(
+ uvPoints,
+ uv.Value,
+ ref previousWrappedU,
+ ref currentUOffset,
+ ref previousWrappedV,
+ ref currentVOffset,
+ faceId);
+ if (!unwrapResult.IsSuccess)
+ {
+ return KernelResult<IReadOnlyList<(double U, double V)>>.Failure(unwrapResult.Diagnostics);
+ }
+ }
+ }
+
+ if (uvPoints.Count > 1 && UvPointsAlmostEqual(uvPoints[0], uvPoints[^1]))
+ {
+ uvPoints.RemoveAt(uvPoints.Count - 1);
+ }
+
+ return KernelResult<IReadOnlyList<(double U, double V)>>.Success(uvPoints);
+ }
+
private static KernelResult<IReadOnlyList<IReadOnlyList<(double U, double V)>>> TryBuildPeriodicTrimmedSurfaceUvLoops(
BrepBody body,
FaceId faceId,
@@ -781,6 +873,100 @@ public static class BrepDisplayTessellator
return KernelResult<IReadOnlyList<(double U, double V)>>.Success(uvPoints);
}
+ private static KernelResult<bool> TryUnwrapDoublyPeriodicUvPoint(
+ List<(double U, double V)> uvPoints,
+ (double U, double V) wrappedUv,
+ ref double? previousWrappedU,
+ ref double currentUOffset,
+ ref double? previousWrappedV,
+ ref double currentVOffset,
+ FaceId faceId)
+ {
+ var unwrappedUResult = TryUnwrapPeriodicCoordinate(wrappedUv.U, uvPoints, isUCoordinate: true, ref previousWrappedU, ref currentUOffset, faceId);
+ if (!unwrappedUResult.IsSuccess)
+ {
+ return KernelResult<bool>.Failure(unwrappedUResult.Diagnostics);
+ }
+
+ var unwrappedVResult = TryUnwrapPeriodicCoordinate(wrappedUv.V, uvPoints, isUCoordinate: false, ref previousWrappedV, ref currentVOffset, faceId);
+ if (!unwrappedVResult.IsSuccess)
+ {
+ return KernelResult<bool>.Failure(unwrappedVResult.Diagnostics);
+ }
+
+ AppendUniqueUvPoint(uvPoints, (unwrappedUResult.Value, unwrappedVResult.Value));
+ return KernelResult<bool>.Success(true);
+ }
+
+ private static KernelResult<double> TryUnwrapPeriodicCoordinate(
+ double wrappedValue,
+ IReadOnlyList<(double U, double V)> uvPoints,
+ bool isUCoordinate,
+ ref double? previousWrappedValue,
+ ref double currentOffset,
+ FaceId faceId)
+ {
+ var wrappedSpanTolerance = double.Pi + 1e-6d;
+ if (previousWrappedValue.HasValue)
+ {
+ var delta = wrappedValue - previousWrappedValue.Value;
+ if (delta > double.Pi)
+ {
+ currentOffset -= 2d * double.Pi;
+ }
+ else if (delta < -double.Pi)
+ {
+ currentOffset += 2d * double.Pi;
+ }
+
+ if (uvPoints.Count > 0)
+ {
+ var adjustedValue = wrappedValue + currentOffset;
+ var previousValue = isUCoordinate ? uvPoints[^1].U : uvPoints[^1].V;
+ if (double.Abs(adjustedValue - previousValue) > wrappedSpanTolerance)
+ {
+ var coordinateName = isUCoordinate ? "major" : "minor";
+ return KernelResult<double>.Failure([
+ CreateNotImplemented(
+ $"Face {faceId.Value} torus trim loop crossed the {coordinateName} angular seam ambiguously and could not be normalized deterministically.",
+ TrimEvaluationFailedSource)]);
+ }
+ }
+ }
+
+ previousWrappedValue = wrappedValue;
+ return KernelResult<double>.Success(wrappedValue + currentOffset);
+ }
+
+ private static (double U, double V)? TryProjectPointToTorusUv(TorusSurface torus, Point3D point)
+ {
+ var axis = torus.Axis.ToVector();
+ var xAxis = torus.XAxis.ToVector();
+ var yAxis = torus.YAxis.ToVector();
+ var offset = point - torus.Center;
+ var axial = offset.Dot(axis);
+ var planar = offset - (axis * axial);
+ var planarLength = planar.Length;
+ if (!double.IsFinite(planarLength) || planarLength <= 1e-9d)
+ {
+ return null;
+ }
+
+ var u = NormalizeToZeroTwoPi(double.Atan2(planar.Dot(yAxis), planar.Dot(xAxis)));
+ var radialFromMajor = planarLength - torus.MajorRadius;
+ var v = NormalizeToZeroTwoPi(double.Atan2(axial, radialFromMajor));
+
+ var residual = torus.Evaluate(u, v) - point;
+ var residualDistance = System.Math.Sqrt(residual.Dot(residual));
+ var tolerance = System.Math.Max(1e-5d, System.Math.Max(torus.MajorRadius, torus.MinorRadius) * 1e-4d);
+ if (!double.IsFinite(residualDistance) || residualDistance > tolerance)
+ {
+ return null;
+ }
+
+ return (u, v);
+ }
+
private static (double U, double V)? TryProjectPointToCylinderUv(CylinderSurface cylinder, Point3D point)
{
var axis = cylinder.Axis.ToVector();
@@ -1036,6 +1222,32 @@ public static class BrepDisplayTessellator
private static KernelResult<DisplayFaceMeshPatch> TessellateTorusFace(BrepBody body, FaceId faceId, TorusSurface torus, DisplayTessellationOptions options)
{
+ var loopIds = body.GetLoopIds(faceId);
+ if (loopIds.Count > 0)
+ {
+ var uvLoopsResult = TryBuildDoublyPeriodicTrimmedSurfaceUvLoops(body, faceId, loopIds, point => TryProjectPointToTorusUv(torus, point), options);
+ if (!uvLoopsResult.IsSuccess)
+ {
+ return KernelResult<DisplayFaceMeshPatch>.Success(
+ CreateEmptyPlanarPatch(faceId),
+ [CreateValidationWarning(
+ $"Face {faceId.Value} torus trim evaluation failed; skipping face patch to avoid misleading untrimmed geometry.",
+ TrimEvaluationFailedSource)]);
+ }
+
+ return TrimmedSurfaceTessellator.Tessellate(
+ faceId,
+ uvLoopsResult.Value,
+ (u, v) => torus.Evaluate(u, v),
+ (u, v) => torus.Normal(u, v).ToVector(),
+ options,
+ double.NegativeInfinity,
+ double.PositiveInfinity,
+ double.NegativeInfinity,
+ double.PositiveInfinity,
+ CreateValidationWarning);
+ }
+
var parameters = GetRevolvedFaceParameters(body, faceId, options, radiusHint: torus.MajorRadius + torus.MinorRadius, allowThreeCoedgeConeTopology: false, axialParameterFromPoint: point => TorusMinorAngleOf(torus, point));
if (!parameters.IsSuccess)
{