Skip to content

Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel

Implement M15c trim-aware cylinder and cone tessellation

Yuechen Li45dff73ccf72
diff --git a/Aetheris.Kernel.Core.Tests/Brep/Tessellation/BrepDisplayTessellatorTests.cs b/Aetheris.Kernel.Core.Tests/Brep/Tessellation/BrepDisplayTessellatorTests.cs
index 1074ceb4..e5dbc877 100644
--- a/Aetheris.Kernel.Core.Tests/Brep/Tessellation/BrepDisplayTessellatorTests.cs
+++ b/Aetheris.Kernel.Core.Tests/Brep/Tessellation/BrepDisplayTessellatorTests.cs
@@ -273,6 +273,145 @@ public sealed class BrepDisplayTessellatorTests
Assert.All(result.Value.FacePatches, patch => Assert.NotEmpty(patch.TriangleIndices));
}
+
+ [Fact]
+ public void Tessellate_TrimmedCylinderSurfaceWithHole_RespectsOuterLoopAndHole_Deterministically()
+ {
+ var cylinder = new CylinderSurface(
+ new Point3D(0d, 0d, 0d),
+ Direction3D.Create(new Vector3D(0d, 0d, 1d)),
+ 2d,
+ Direction3D.Create(new Vector3D(1d, 0d, 0d)));
+ var outerBounds = (UMin: 0.6d, UMax: 2.4d, VMin: 0.5d, VMax: 3.5d);
+ var holeBounds = (UMin: 1.2d, UMax: 1.8d, VMin: 1.4d, VMax: 2.4d);
+ var body = CreateTrimmedCylinderSurfaceBody(cylinder, outerBounds, holeBounds);
+ var options = DisplayTessellationOptions.Create(double.Pi / 8d, 0.15d, 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;
+ Assert.All(GetTriangleUvCentroids(firstPatch, point => ProjectCylinderUv(cylinder, point, outerMidU)), centroid =>
+ {
+ Assert.True(IsInsideUvRectangle(centroid, outerBounds));
+ Assert.False(IsInsideUvRectangle(centroid, holeBounds));
+ });
+ }
+
+ [Fact]
+ public void Tessellate_TrimmedConeSurfaceWithHole_RespectsOuterLoopAndHole_Deterministically()
+ {
+ var cone = new ConeSurface(
+ placementOrigin: new Point3D(0d, 0d, 1d),
+ axis: Direction3D.Create(new Vector3D(0d, 0d, 1d)),
+ placementRadius: 1d,
+ semiAngleRadians: double.Pi / 6d,
+ referenceAxis: Direction3D.Create(new Vector3D(1d, 0d, 0d)));
+ var outerBounds = (UMin: 0.5d, UMax: 2.2d, VMin: 1d, VMax: 3.4d);
+ var holeBounds = (UMin: 1.0d, UMax: 1.6d, VMin: 1.7d, VMax: 2.5d);
+ var body = CreateTrimmedConeSurfaceBody(cone, outerBounds, holeBounds);
+ var options = DisplayTessellationOptions.Create(double.Pi / 8d, 0.15d, 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;
+ Assert.All(GetTriangleUvCentroids(firstPatch, point => ProjectConeUv(cone, point, outerMidU)), centroid =>
+ {
+ Assert.True(IsInsideUvRectangle(centroid, outerBounds));
+ Assert.False(IsInsideUvRectangle(centroid, holeBounds));
+ });
+ }
+
+ [Fact]
+ public void Tessellate_TrimmedCylinderSurfaceAcrossSeam_UsesDeterministicUnwrap()
+ {
+ var cylinder = new CylinderSurface(
+ new Point3D(0d, 0d, 0d),
+ Direction3D.Create(new Vector3D(0d, 0d, 1d)),
+ 2d,
+ Direction3D.Create(new Vector3D(1d, 0d, 0d)));
+ var outerBounds = (UMin: 5.8d, UMax: 6.7d, VMin: 0.6d, VMax: 3.2d);
+ var body = CreateTrimmedCylinderSurfaceBody(cylinder, outerBounds, holeBounds: null);
+ var options = DisplayTessellationOptions.Create(double.Pi / 8d, 0.15d, 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;
+ Assert.All(GetTriangleUvCentroids(patch, point => ProjectCylinderUv(cylinder, point, outerMidU)), centroid =>
+ Assert.True(IsInsideUvRectangle(centroid, outerBounds)));
+ }
+
+ [Fact]
+ public void Tessellate_TrimmedCylinderSurface_WhenProjectionFails_EmitsWarningAndSkipsWithoutFallback()
+ {
+ var cylinder = new CylinderSurface(
+ new Point3D(0d, 0d, 0d),
+ Direction3D.Create(new Vector3D(0d, 0d, 1d)),
+ 2d,
+ Direction3D.Create(new Vector3D(1d, 0d, 0d)));
+ var outerBounds = (UMin: 0.6d, UMax: 2.4d, VMin: 0.5d, VMax: 3.5d);
+ var holeBounds = (UMin: 1.2d, UMax: 1.8d, VMin: 1.4d, VMax: 2.4d);
+ var body = CreateTrimmedCylinderSurfaceBody(cylinder, outerBounds, holeBounds, topRadiusOffset: 0.35d);
+
+ 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_SimpleCylinderAndConeFaces_RemainSupported()
+ {
+ var cylinder = BrepPrimitives.CreateCylinder(1.5d, 4d);
+ var cone = BrepRevolve.Create(
+ [new ProfilePoint2D(1d, 0d), new ProfilePoint2D(2d, 3d)],
+ DefaultFrame,
+ DefaultAxis);
+
+ var cylinderResult = BrepDisplayTessellator.Tessellate(cylinder.Value);
+ var coneResult = BrepDisplayTessellator.Tessellate(cone.Value);
+
+ Assert.True(cylinderResult.IsSuccess);
+ Assert.True(coneResult.IsSuccess);
+ Assert.Contains(cylinderResult.Value.FacePatches, patch => patch.TriangleIndices.Count > 0);
+ Assert.Contains(coneResult.Value.FacePatches, patch => patch.TriangleIndices.Count > 0);
+ }
+
private static void AssertPointEqualWithinTolerance(Point3D expected, Point3D actual, int precision)
{
Assert.Equal(expected.X, actual.X, precision);
@@ -444,4 +583,251 @@ public sealed class BrepDisplayTessellatorTests
knotValuesU: [0d, 1d],
knotValuesV: [0d, 1d],
knotSpec: "UNSPECIFIED");
+
+ private static BrepBody CreateTrimmedCylinderSurfaceBody(
+ CylinderSurface cylinder,
+ (double UMin, double UMax, double VMin, double VMax) outerBounds,
+ (double UMin, double UMax, double VMin, double VMax)? holeBounds,
+ double topRadiusOffset = 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>
+ {
+ AddCylinderLoop(builder, geometry, bindings, vertexPoints, cylinder, outerBounds, ref curveId, topRadiusOffset),
+ };
+
+ if (holeBounds is { } hole)
+ {
+ loops.Add(AddCylinderLoop(builder, geometry, bindings, vertexPoints, cylinder, hole, ref curveId, topRadiusOffset: 0d));
+ }
+
+ var face = builder.AddFace(loops);
+ var shell = builder.AddShell([face]);
+ builder.AddBody([shell]);
+
+ geometry.AddSurface(new SurfaceGeometryId(1), SurfaceGeometry.FromCylinder(cylinder));
+ 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,
+ (double UMin, double UMax, double VMin, double VMax)? holeBounds)
+ {
+ 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>
+ {
+ AddConeLoop(builder, geometry, bindings, vertexPoints, cone, outerBounds, ref curveId),
+ };
+
+ if (holeBounds is { } hole)
+ {
+ loops.Add(AddConeLoop(builder, geometry, bindings, vertexPoints, cone, hole, ref curveId));
+ }
+
+ var face = builder.AddFace(loops);
+ var shell = builder.AddShell([face]);
+ builder.AddBody([shell]);
+
+ geometry.AddSurface(new SurfaceGeometryId(1), SurfaceGeometry.FromCone(cone));
+ bindings.AddFaceBinding(new FaceGeometryBinding(face, new SurfaceGeometryId(1)));
+ return new BrepBody(builder.Model, geometry, bindings, vertexPoints);
+ }
+
+ private static LoopId AddCylinderLoop(
+ TopologyBuilder builder,
+ BrepGeometryStore geometry,
+ BrepBindingModel bindings,
+ Dictionary<VertexId, Point3D> vertexPoints,
+ CylinderSurface cylinder,
+ (double UMin, double UMax, double VMin, double VMax) bounds,
+ ref int curveId,
+ double topRadiusOffset)
+ => AddPeriodicRectangleLoop(
+ builder,
+ geometry,
+ bindings,
+ vertexPoints,
+ bounds,
+ ref curveId,
+ (u, v) => EvaluateCylinderPoint(cylinder, u, v, v == bounds.VMax ? topRadiusOffset : 0d),
+ v => CreateCircleAtCylinderV(cylinder, v, v == bounds.VMax ? topRadiusOffset : 0d));
+
+ private static LoopId AddConeLoop(
+ TopologyBuilder builder,
+ BrepGeometryStore geometry,
+ BrepBindingModel bindings,
+ Dictionary<VertexId, Point3D> vertexPoints,
+ ConeSurface cone,
+ (double UMin, double UMax, double VMin, double VMax) bounds,
+ ref int curveId)
+ => AddPeriodicRectangleLoop(
+ builder,
+ geometry,
+ bindings,
+ vertexPoints,
+ bounds,
+ ref curveId,
+ cone.Evaluate,
+ v => CreateCircleAtConeV(cone, v));
+
+ private static LoopId AddPeriodicRectangleLoop(
+ TopologyBuilder builder,
+ BrepGeometryStore geometry,
+ BrepBindingModel bindings,
+ Dictionary<VertexId, Point3D> vertexPoints,
+ (double UMin, double UMax, double VMin, double VMax) bounds,
+ ref int curveId,
+ Func<double, double, Point3D> evaluate,
+ Func<double, Circle3Curve> createCircleAtV)
+ {
+ var vertices = new[]
+ {
+ evaluate(bounds.UMin, bounds.VMin),
+ evaluate(bounds.UMax, bounds.VMin),
+ evaluate(bounds.UMax, bounds.VMax),
+ evaluate(bounds.UMin, bounds.VMax),
+ };
+
+ 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(createCircleAtV(bounds.VMin)));
+ bindings.AddEdgeBinding(new EdgeGeometryBinding(edgeIds[0], new CurveGeometryId(curveId++), new ParameterInterval(bounds.UMin, bounds.UMax)));
+
+ var rightLine = CreateLine(vertices[1], vertices[2], out var rightLength);
+ geometry.AddCurve(new CurveGeometryId(curveId), CurveGeometry.FromLine(rightLine));
+ bindings.AddEdgeBinding(new EdgeGeometryBinding(edgeIds[1], new CurveGeometryId(curveId++), new ParameterInterval(0d, rightLength)));
+
+ geometry.AddCurve(new CurveGeometryId(curveId), CurveGeometry.FromCircle(createCircleAtV(bounds.VMax)));
+ 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))));
+
+ var leftLine = CreateLine(vertices[3], vertices[0], out var leftLength);
+ geometry.AddCurve(new CurveGeometryId(curveId), CurveGeometry.FromLine(leftLine));
+ bindings.AddEdgeBinding(new EdgeGeometryBinding(edgeIds[3], new CurveGeometryId(curveId++), new ParameterInterval(0d, leftLength)));
+
+ 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 Point3D EvaluateCylinderPoint(CylinderSurface cylinder, double u, double v, double radiusOffset)
+ {
+ var radius = cylinder.Radius + radiusOffset;
+ var radial = (cylinder.XAxis.ToVector() * (radius * System.Math.Cos(u))) + (cylinder.YAxis.ToVector() * (radius * System.Math.Sin(u)));
+ return cylinder.Origin + (cylinder.Axis.ToVector() * v) + radial;
+ }
+
+ private static Circle3Curve CreateCircleAtCylinderV(CylinderSurface cylinder, double v, double radiusOffset)
+ => new(
+ cylinder.Origin + (cylinder.Axis.ToVector() * v),
+ cylinder.Axis,
+ cylinder.Radius + radiusOffset,
+ cylinder.XAxis);
+
+ private static Circle3Curve CreateCircleAtConeV(ConeSurface cone, double v)
+ => new(
+ cone.Apex + (cone.Axis.ToVector() * v),
+ cone.Axis,
+ v * System.Math.Tan(cone.SemiAngleRadians),
+ cone.ReferenceAxis);
+
+ private static IEnumerable<(double U, double V)> GetTriangleUvCentroids(
+ DisplayFaceMeshPatch patch,
+ Func<Point3D, (double U, double V)> projectPoint)
+ {
+ for (var i = 0; i < patch.TriangleIndices.Count; i += 3)
+ {
+ var a = projectPoint(patch.Positions[patch.TriangleIndices[i]]);
+ var b = projectPoint(patch.Positions[patch.TriangleIndices[i + 1]]);
+ var c = projectPoint(patch.Positions[patch.TriangleIndices[i + 2]]);
+ yield return ((a.U + b.U + c.U) / 3d, (a.V + b.V + c.V) / 3d);
+ }
+ }
+
+ private static (double U, double V) ProjectCylinderUv(CylinderSurface cylinder, Point3D point, double referenceU)
+ {
+ var offset = point - cylinder.Origin;
+ var axial = offset.Dot(cylinder.Axis.ToVector());
+ var radial = offset - (cylinder.Axis.ToVector() * axial);
+ var angle = System.Math.Atan2(radial.Dot(cylinder.YAxis.ToVector()), radial.Dot(cylinder.XAxis.ToVector()));
+ return (UnwrapNearReference(angle, referenceU), axial);
+ }
+
+ private static (double U, double V) ProjectConeUv(ConeSurface cone, Point3D point, double referenceU)
+ {
+ var offset = point - cone.Apex;
+ var axial = offset.Dot(cone.Axis.ToVector());
+ var radial = offset - (cone.Axis.ToVector() * axial);
+ var xAxis = Direction3D.Create(cone.ReferenceAxis.ToVector() - (cone.Axis.ToVector() * cone.ReferenceAxis.ToVector().Dot(cone.Axis.ToVector()))).ToVector();
+ var yAxis = Direction3D.Create(cone.Axis.ToVector().Cross(xAxis)).ToVector();
+ var angle = radial.Length <= 1e-9d ? 0d : System.Math.Atan2(radial.Dot(yAxis), radial.Dot(xAxis));
+ return (UnwrapNearReference(angle, referenceU), axial);
+ }
+
+ private static double UnwrapNearReference(double angle, double reference)
+ {
+ var candidate = angle;
+ while (candidate < reference - double.Pi)
+ {
+ candidate += 2d * double.Pi;
+ }
+
+ while (candidate > reference + double.Pi)
+ {
+ candidate -= 2d * double.Pi;
+ }
+
+ return candidate;
+ }
+
+ private static bool IsInsideUvRectangle(
+ (double U, double V) uv,
+ (double UMin, double UMax, double VMin, double VMax) bounds)
+ => uv.U >= bounds.UMin - 1e-6d
+ && uv.U <= bounds.UMax + 1e-6d
+ && uv.V >= bounds.VMin - 1e-6d
+ && uv.V <= bounds.VMax + 1e-6d;
}
diff --git a/Aetheris.Kernel.Core/Brep/Tessellation/BrepDisplayTessellator.cs b/Aetheris.Kernel.Core/Brep/Tessellation/BrepDisplayTessellator.cs
index 6ad5485f..da62b6b6 100644
--- a/Aetheris.Kernel.Core/Brep/Tessellation/BrepDisplayTessellator.cs
+++ b/Aetheris.Kernel.Core/Brep/Tessellation/BrepDisplayTessellator.cs
@@ -345,6 +345,74 @@ public static class BrepDisplayTessellator
}
private static KernelResult<DisplayFaceMeshPatch> TessellateCylinderFace(BrepBody body, FaceId faceId, CylinderSurface cylinder, DisplayTessellationOptions options)
+ {
+ var loopIds = body.GetLoopIds(faceId);
+ if (loopIds.Count > 1)
+ {
+ var uvLoopsResult = TryBuildPeriodicTrimmedSurfaceUvLoops(body, faceId, loopIds, point => TryProjectPointToCylinderUv(cylinder, point), options);
+ if (!uvLoopsResult.IsSuccess)
+ {
+ return KernelResult<DisplayFaceMeshPatch>.Success(
+ CreateEmptyPlanarPatch(faceId),
+ [CreateValidationWarning(
+ $"Face {faceId.Value} cylinder trim evaluation failed; skipping face patch to avoid misleading untrimmed geometry.",
+ TrimEvaluationFailedSource)]);
+ }
+
+ return TrimmedSurfaceTessellator.Tessellate(
+ faceId,
+ uvLoopsResult.Value,
+ (u, v) => cylinder.Evaluate(u, v),
+ (u, _) => cylinder.Normal(u).ToVector(),
+ options,
+ double.NegativeInfinity,
+ double.PositiveInfinity,
+ double.NegativeInfinity,
+ double.PositiveInfinity,
+ CreateValidationWarning);
+ }
+
+ return TessellateLegacyCylinderFace(body, faceId, cylinder, options);
+ }
+
+ private static KernelResult<DisplayFaceMeshPatch> TessellateConeFace(BrepBody body, FaceId faceId, ConeSurface cone, DisplayTessellationOptions options)
+ {
+ var loopIds = body.GetLoopIds(faceId);
+ if (loopIds.Count > 0)
+ {
+ var uvLoopsResult = TryBuildPeriodicTrimmedSurfaceUvLoops(body, faceId, loopIds, point => TryProjectPointToConeUv(cone, point), options);
+ if (!uvLoopsResult.IsSuccess)
+ {
+ return KernelResult<DisplayFaceMeshPatch>.Success(
+ CreateEmptyPlanarPatch(faceId),
+ [CreateValidationWarning(
+ $"Face {faceId.Value} cone trim evaluation failed; skipping face patch to avoid misleading untrimmed geometry.",
+ TrimEvaluationFailedSource)]);
+ }
+
+ if (loopIds.Count == 1 && UsesFullPeriodicAngularSpan(uvLoopsResult.Value) && UsesSimpleAnalyticRevolvedLoopTopology(body, loopIds[0]))
+ {
+ return TessellateLegacyConeFace(body, faceId, cone, options);
+ }
+
+ return TrimmedSurfaceTessellator.Tessellate(
+ faceId,
+ uvLoopsResult.Value,
+ (u, v) => cone.Evaluate(u, v),
+ (u, _) => cone.Normal(u).ToVector(),
+ options,
+ double.NegativeInfinity,
+ double.PositiveInfinity,
+ double.NegativeInfinity,
+ double.PositiveInfinity,
+ CreateValidationWarning);
+ }
+
+ return TessellateLegacyConeFace(body, faceId, cone, options);
+ }
+
+
+ private static KernelResult<DisplayFaceMeshPatch> TessellateLegacyCylinderFace(BrepBody body, FaceId faceId, CylinderSurface cylinder, DisplayTessellationOptions options)
{
var trimPatch = TryResolveCylinderTrimPatch(body, faceId, cylinder);
if (!trimPatch.IsSuccess)
@@ -394,7 +462,7 @@ public static class BrepDisplayTessellator
trimPatch.Diagnostics);
}
- private static KernelResult<DisplayFaceMeshPatch> TessellateConeFace(BrepBody body, FaceId faceId, ConeSurface cone, DisplayTessellationOptions options)
+ private static KernelResult<DisplayFaceMeshPatch> TessellateLegacyConeFace(BrepBody body, FaceId faceId, ConeSurface cone, DisplayTessellationOptions options)
{
var parameters = GetRevolvedFaceParameters(
body,
@@ -418,6 +486,72 @@ public static class BrepDisplayTessellator
parameters.Value.VEnd));
}
+ private static bool UsesSimpleAnalyticRevolvedLoopTopology(BrepBody body, LoopId loopId)
+ {
+ var coedges = body.GetCoedgeIds(loopId).Select(id => body.Topology.GetCoedge(id)).ToArray();
+ if (coedges.Length == 0 || coedges.Length > 4)
+ {
+ return false;
+ }
+
+ return coedges
+ .Select(coedge => body.GetEdgeCurve(coedge.EdgeId).Kind)
+ .All(kind => kind is CurveGeometryKind.Line3 or CurveGeometryKind.Circle3);
+ }
+
+ private static bool UsesFullPeriodicAngularSpan(IReadOnlyList<IReadOnlyList<(double U, double V)>> uvLoops)
+ {
+ if (uvLoops.Count == 0)
+ {
+ return false;
+ }
+
+ var outerLoop = uvLoops
+ .Select(NormalizePeriodicLoop)
+ .OrderByDescending(loop => System.Math.Abs(ComputePeriodicLoopSignedArea(loop)))
+ .First();
+ if (outerLoop.Count == 0)
+ {
+ return false;
+ }
+
+ var span = outerLoop.Max(point => point.U) - outerLoop.Min(point => point.U);
+ return span >= ((2d * double.Pi) - 0.25d);
+ }
+
+ private static List<(double U, double V)> NormalizePeriodicLoop(IReadOnlyList<(double U, double V)> loop)
+ {
+ var normalized = new List<(double U, double V)>(loop.Count);
+ foreach (var point in loop)
+ {
+ if (normalized.Count == 0 || System.Math.Abs(normalized[^1].U - point.U) > 1e-9d || System.Math.Abs(normalized[^1].V - point.V) > 1e-9d)
+ {
+ normalized.Add(point);
+ }
+ }
+
+ if (normalized.Count > 1
+ && System.Math.Abs(normalized[0].U - normalized[^1].U) <= 1e-9d
+ && System.Math.Abs(normalized[0].V - normalized[^1].V) <= 1e-9d)
+ {
+ normalized.RemoveAt(normalized.Count - 1);
+ }
+
+ return normalized;
+ }
+
+ private static double ComputePeriodicLoopSignedArea(IReadOnlyList<(double U, double V)> loop)
+ {
+ var area = 0d;
+ for (var i = 0; i < loop.Count; i++)
+ {
+ var current = loop[i];
+ var next = loop[(i + 1) % loop.Count];
+ area += (current.U * next.V) - (next.U * current.V);
+ }
+
+ return area * 0.5d;
+ }
private static KernelResult<DisplayFaceMeshPatch> TessellateBSplineSurfaceFace(BrepBody body, FaceId faceId, BSplineSurfaceWithKnots surface, DisplayTessellationOptions options)
{
@@ -544,6 +678,165 @@ public static class BrepDisplayTessellator
return (bestU, bestV);
}
+ private static KernelResult<IReadOnlyList<IReadOnlyList<(double U, double V)>>> TryBuildPeriodicTrimmedSurfaceUvLoops(
+ 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 = TryBuildPeriodicTrimmedSurfaceUvLoop(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)>> TryBuildPeriodicTrimmedSurfaceUvLoop(
+ 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 currentUOffset = 0d;
+ var wrappedSpanTolerance = double.Pi + 1e-6d;
+ 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 parameter domain.",
+ TrimEvaluationFailedSource)]);
+ }
+
+ var wrappedU = uv.Value.U;
+ if (previousWrappedU.HasValue)
+ {
+ var delta = wrappedU - previousWrappedU.Value;
+ if (delta > double.Pi)
+ {
+ currentUOffset -= 2d * double.Pi;
+ }
+ else if (delta < -double.Pi)
+ {
+ currentUOffset += 2d * double.Pi;
+ }
+
+ var adjustedDelta = (wrappedU + currentUOffset) - uvPoints[^1].U;
+ if (double.Abs(adjustedDelta) > wrappedSpanTolerance)
+ {
+ return KernelResult<IReadOnlyList<(double U, double V)>>.Failure([
+ CreateNotImplemented(
+ $"Face {faceId.Value} periodic trim loop crossed the angular seam ambiguously and could not be normalized deterministically.",
+ TrimEvaluationFailedSource)]);
+ }
+ }
+
+ AppendUniqueUvPoint(uvPoints, (wrappedU + currentUOffset, uv.Value.V));
+ previousWrappedU = wrappedU;
+ }
+ }
+
+ 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 (double U, double V)? TryProjectPointToCylinderUv(CylinderSurface cylinder, Point3D point)
+ {
+ var axis = cylinder.Axis.ToVector();
+ var xAxis = cylinder.XAxis.ToVector();
+ var yAxis = cylinder.YAxis.ToVector();
+ var offset = point - cylinder.Origin;
+ var axial = offset.Dot(axis);
+ var radial = offset - (axis * axial);
+ var radialLength = radial.Length;
+ var tolerance = System.Math.Max(1e-5d, cylinder.Radius * 1e-4d);
+ if (!double.IsFinite(radialLength) || System.Math.Abs(radialLength - cylinder.Radius) > tolerance)
+ {
+ return null;
+ }
+
+ var angle = NormalizeToZeroTwoPi(double.Atan2(radial.Dot(yAxis), radial.Dot(xAxis)));
+ return (angle, axial);
+ }
+
+ private static (double U, double V)? TryProjectPointToConeUv(ConeSurface cone, Point3D point)
+ {
+ var axis = cone.Axis.ToVector();
+ var xAxis = cone.ReferenceAxis.ToVector();
+ var projectedX = xAxis - (axis * xAxis.Dot(axis));
+ if (!Direction3D.TryCreate(projectedX, out var coneXAxis))
+ {
+ return null;
+ }
+
+ if (!Direction3D.TryCreate(axis.Cross(coneXAxis.ToVector()), out var coneYAxis))
+ {
+ return null;
+ }
+
+ var offset = point - cone.Apex;
+ var axial = offset.Dot(axis);
+ if (!double.IsFinite(axial) || axial < -1e-6d)
+ {
+ return null;
+ }
+
+ var radial = offset - (axis * axial);
+ var radialLength = radial.Length;
+ var expectedRadius = axial * double.Tan(cone.SemiAngleRadians);
+ var tolerance = System.Math.Max(1e-5d, expectedRadius * 1e-4d);
+ if (System.Math.Abs(radialLength - expectedRadius) > tolerance)
+ {
+ return null;
+ }
+
+ var angle = radialLength <= 1e-9d
+ ? 0d
+ : NormalizeToZeroTwoPi(double.Atan2(radial.Dot(coneYAxis.ToVector()), radial.Dot(coneXAxis.ToVector())));
+ return (angle, axial);
+ }
+
private static KernelResult<IReadOnlyList<IReadOnlyList<(double U, double V)>>> TryBuildTrimmedSurfaceUvLoops(
BrepBody body,
FaceId faceId,