Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
M38: fix planar loop ordering in tessellator (box renders correctly)
Yuechen Li76c09aa0be29
diff --git a/Aetheris.Kernel.Core.Tests/Brep/Tessellation/BrepDisplayTessellatorBoxRenderingTests.cs b/Aetheris.Kernel.Core.Tests/Brep/Tessellation/BrepDisplayTessellatorBoxRenderingTests.csnew file mode 100644index 00000000..6a2bf3f0--- /dev/null+++ b/Aetheris.Kernel.Core.Tests/Brep/Tessellation/BrepDisplayTessellatorBoxRenderingTests.cs@@ -0,0 +1,127 @@+using Aetheris.Kernel.Core.Brep;+using Aetheris.Kernel.Core.Brep.Tessellation;+using Aetheris.Kernel.Core.Math;++namespace Aetheris.Kernel.Core.Tests.Brep.Tessellation;++public sealed class BrepDisplayTessellatorBoxRenderingTests+{+ private const double Tolerance = 1e-6d;++ [Fact]+ public void CreateBox_TessellatesToSixPlanarPatches_WithExpectedBounds()+ {+ const double width = 3d;+ const double depth = 2d;+ const double height = 1d;++ var box = BrepPrimitives.CreateBox(width, depth, height);+ var result = BrepDisplayTessellator.Tessellate(box.Value);++ Assert.True(result.IsSuccess);+ Assert.Equal(6, result.Value.FacePatches.Count);++ var allPositions = result.Value.FacePatches.SelectMany(p => p.Positions).ToArray();+ Assert.NotEmpty(allPositions);++ var halfWidth = width / 2d;+ var halfDepth = depth / 2d;+ var halfHeight = height / 2d;++ AssertAlmostEqual(-halfWidth, allPositions.Min(p => p.X));+ AssertAlmostEqual(halfWidth, allPositions.Max(p => p.X));+ AssertAlmostEqual(-halfDepth, allPositions.Min(p => p.Y));+ AssertAlmostEqual(halfDepth, allPositions.Max(p => p.Y));+ AssertAlmostEqual(-halfHeight, allPositions.Min(p => p.Z));+ AssertAlmostEqual(halfHeight, allPositions.Max(p => p.Z));++ foreach (var patch in result.Value.FacePatches)+ {+ Assert.NotEmpty(patch.TriangleIndices);+ Assert.Equal(0, patch.TriangleIndices.Count % 3);++ var planeNormal = patch.Normals[0];+ var planePoint = patch.Positions[0];+ foreach (var point in patch.Positions)+ {+ var distance = (point - planePoint).Dot(planeNormal);+ Assert.True(double.Abs(distance) <= 1e-5d);+ }++ for (var i = 0; i < patch.TriangleIndices.Count; i += 3)+ {+ var i0 = patch.TriangleIndices[i];+ var i1 = patch.TriangleIndices[i + 1];+ var i2 = patch.TriangleIndices[i + 2];++ Assert.InRange(i0, 0, patch.Positions.Count - 1);+ Assert.InRange(i1, 0, patch.Positions.Count - 1);+ Assert.InRange(i2, 0, patch.Positions.Count - 1);++ var area2 = (patch.Positions[i1] - patch.Positions[i0])+ .Cross(patch.Positions[i2] - patch.Positions[i0])+ .Length;+ Assert.True(area2 > Tolerance, $"Degenerate triangle detected in face {patch.FaceId.Value}.");+ }+ }+ }++ [Fact]+ public void CreateBox_PlanarLoopOrdering_IsContiguous()+ {+ var box = BrepPrimitives.CreateBox(3d, 2d, 1d);+ var result = BrepDisplayTessellator.Tessellate(box.Value);++ Assert.True(result.IsSuccess);++ var expectedEdgeLengths = new[] { 1d, 2d, 3d };+ foreach (var patch in result.Value.FacePatches)+ {+ Assert.Equal(4, patch.Positions.Count);++ for (var i = 0; i < patch.Positions.Count; i++)+ {+ var current = patch.Positions[i];+ var next = patch.Positions[(i + 1) % patch.Positions.Count];+ var edgeLength = (next - current).Length;+ Assert.True(+ expectedEdgeLengths.Any(expected => double.Abs(edgeLength - expected) <= Tolerance),+ $"Unexpected edge length {edgeLength} for face {patch.FaceId.Value}.");+ }++ Assert.True(IsConvex(patch.Positions, patch.Normals[0]), $"Face {patch.FaceId.Value} was not convex.");+ }+ }++ private static bool IsConvex(IReadOnlyList<Point3D> points, Vector3D normal)+ {+ var sign = 0;+ for (var i = 0; i < points.Count; i++)+ {+ var a = points[i];+ var b = points[(i + 1) % points.Count];+ var c = points[(i + 2) % points.Count];+ var cross = (b - a).Cross(c - b);+ var dot = cross.Dot(normal);+ if (double.Abs(dot) <= Tolerance)+ {+ continue;+ }++ var nextSign = dot > 0d ? 1 : -1;+ if (sign == 0)+ {+ sign = nextSign;+ }+ else if (sign != nextSign)+ {+ return false;+ }+ }++ return sign != 0;+ }++ private static void AssertAlmostEqual(double expected, double actual)+ => Assert.True(double.Abs(expected - actual) <= Tolerance, $"Expected {expected} but found {actual}.");+}diff --git a/Aetheris.Kernel.Core/Brep/Tessellation/BrepDisplayTessellator.cs b/Aetheris.Kernel.Core/Brep/Tessellation/BrepDisplayTessellator.csindex 72a1a112..1a4c27ae 100644--- a/Aetheris.Kernel.Core/Brep/Tessellation/BrepDisplayTessellator.cs+++ b/Aetheris.Kernel.Core/Brep/Tessellation/BrepDisplayTessellator.cs@@ -10,6 +10,8 @@ namespace Aetheris.Kernel.Core.Brep.Tessellation;public static class BrepDisplayTessellator{+ private const double LoopEndpointTolerance = 1e-6d;+public static KernelResult<DisplayTessellationResult> Tessellate(BrepBody body, DisplayTessellationOptions? options = null){if (body is null)@@ -82,24 +84,26 @@ public static class BrepDisplayTessellatorif (coedges.All(c => body.GetEdgeCurve(c.EdgeId).Kind == CurveGeometryKind.Line3)){- var polygonPoints = new List<Point3D>(coedges.Length);- foreach (var coedge in coedges)+ var polygonPointsResult = BuildOrderedPlanarLoopPoints(body, coedges, faceId);+ if (!polygonPointsResult.IsSuccess){- var endpoints = GetEdgeEndpoints(body, coedge.EdgeId, coedge.IsReversed);- if (!endpoints.IsSuccess)- {- return KernelResult<DisplayFaceMeshPatch>.Failure(endpoints.Diagnostics);- }-- polygonPoints.Add(endpoints.Value.Start);+ return KernelResult<DisplayFaceMeshPatch>.Failure(polygonPointsResult.Diagnostics);}+ var polygonPoints = polygonPointsResult.Value;+if (polygonPoints.Count < 3){return KernelResult<DisplayFaceMeshPatch>.Failure([CreateNotImplemented($"Face {faceId.Value} planar loop must contain at least three line coedges.")]);}- return KernelResult<DisplayFaceMeshPatch>.Success(CreatePlanarPatch(faceId, polygonPoints, plane.Normal.ToVector()));+ var indices = BuildPlanarTriangleIndices(polygonPoints, plane.Normal.ToVector());+ if (indices is null)+ {+ return KernelResult<DisplayFaceMeshPatch>.Failure([CreateNotImplemented($"Face {faceId.Value} planar loop triangulation requires a convex polygon.")]);+ }++ return KernelResult<DisplayFaceMeshPatch>.Success(CreatePlanarPatch(faceId, polygonPoints, plane.Normal.ToVector(), indices));}if (coedges.Length == 1 && body.GetEdgeCurve(coedges[0].EdgeId).Kind == CurveGeometryKind.Circle3)@@ -334,9 +338,113 @@ public static class BrepDisplayTessellatorreturn new DisplayFaceMeshPatch(faceId, positions, normals, indices);}- private static DisplayFaceMeshPatch CreatePlanarPatch(FaceId faceId, IReadOnlyList<Point3D> polygonPoints, Vector3D normal)+ private static DisplayFaceMeshPatch CreatePlanarPatch(FaceId faceId, IReadOnlyList<Point3D> polygonPoints, Vector3D normal, IReadOnlyList<int> triangleIndices){var normals = Enumerable.Repeat(normal, polygonPoints.Count).ToArray();+ return new DisplayFaceMeshPatch(faceId, polygonPoints.ToArray(), normals, triangleIndices.ToArray());+ }++ private static KernelResult<IReadOnlyList<Point3D>> BuildOrderedPlanarLoopPoints(BrepBody body, IReadOnlyList<Coedge> coedges, FaceId faceId)+ {+ var vertexPointsResult = BuildLoopVertexPointLookup(body, coedges, faceId);+ if (!vertexPointsResult.IsSuccess)+ {+ return KernelResult<IReadOnlyList<Point3D>>.Failure(vertexPointsResult.Diagnostics);+ }++ var vertexPoints = vertexPointsResult.Value;+ var orientedEndpoints = new List<(Point3D Start, Point3D End)>(coedges.Count);+ foreach (var coedge in coedges)+ {+ var endpoints = GetEdgeEndpoints(body, coedge.EdgeId, coedge.IsReversed, vertexPoints);+ if (!endpoints.IsSuccess)+ {+ return KernelResult<IReadOnlyList<Point3D>>.Failure(endpoints.Diagnostics);+ }++ orientedEndpoints.Add(endpoints.Value);+ }++ var orderedVertices = new List<Point3D>(coedges.Count);+ var used = new bool[coedges.Count];+ var currentIndex = 0;+ used[currentIndex] = true;++ var currentEdge = orientedEndpoints[currentIndex];+ orderedVertices.Add(currentEdge.Start);+ var currentEnd = currentEdge.End;++ for (var step = 1; step < coedges.Count; step++)+ {+ var nextIndex = FindNextCoedgeIndex(orientedEndpoints, used, currentEnd, out var nextStart, out var nextEnd);+ if (nextIndex < 0)+ {+ return KernelResult<IReadOnlyList<Point3D>>.Failure([CreateNotImplemented($"Face {faceId.Value} planar loop line coedges do not form a contiguous chain.")]);+ }++ used[nextIndex] = true;+ if (!PointsAlmostEqual(orderedVertices[^1], nextStart))+ {+ orderedVertices.Add(nextStart);+ }++ currentEnd = nextEnd;+ }++ if (PointsAlmostEqual(orderedVertices[0], currentEnd))+ {+ return KernelResult<IReadOnlyList<Point3D>>.Success(orderedVertices);+ }++ return KernelResult<IReadOnlyList<Point3D>>.Failure([CreateNotImplemented($"Face {faceId.Value} planar loop line coedges did not close after chaining.")]);+ }++ private static int FindNextCoedgeIndex(+ IReadOnlyList<(Point3D Start, Point3D End)> endpoints,+ IReadOnlyList<bool> used,+ Point3D currentEnd,+ out Point3D nextStart,+ out Point3D nextEnd)+ {+ var matchIndex = -1;+ nextStart = default;+ nextEnd = default;++ for (var i = 0; i < endpoints.Count; i++)+ {+ if (used[i])+ {+ continue;+ }++ var (start, end) = endpoints[i];+ if (PointsAlmostEqual(start, currentEnd))+ {+ matchIndex = i;+ nextStart = start;+ nextEnd = end;+ break;+ }++ if (PointsAlmostEqual(end, currentEnd))+ {+ matchIndex = i;+ nextStart = end;+ nextEnd = start;+ break;+ }+ }++ return matchIndex;+ }++ private static IReadOnlyList<int>? BuildPlanarTriangleIndices(IReadOnlyList<Point3D> polygonPoints, Vector3D expectedNormal)+ {+ if (!IsConvexPolygon(polygonPoints, expectedNormal))+ {+ return null;+ }+var indices = new List<int>((polygonPoints.Count - 2) * 3);for (var i = 1; i < polygonPoints.Count - 1; i++){@@ -345,9 +453,56 @@ public static class BrepDisplayTessellatorindices.Add(i + 1);}- return new DisplayFaceMeshPatch(faceId, polygonPoints.ToArray(), normals, indices);+ if (indices.Count >= 3)+ {+ var firstNormal = (polygonPoints[indices[1]] - polygonPoints[indices[0]])+ .Cross(polygonPoints[indices[2]] - polygonPoints[indices[0]]);+ if (firstNormal.Dot(expectedNormal) < 0d)+ {+ for (var i = 0; i < indices.Count; i += 3)+ {+ (indices[i + 1], indices[i + 2]) = (indices[i + 2], indices[i + 1]);+ }+ }+ }++ return indices;}+ private static bool IsConvexPolygon(IReadOnlyList<Point3D> polygonPoints, Vector3D expectedNormal)+ {+ var referenceSign = 0;+ for (var i = 0; i < polygonPoints.Count; i++)+ {+ var a = polygonPoints[i];+ var b = polygonPoints[(i + 1) % polygonPoints.Count];+ var c = polygonPoints[(i + 2) % polygonPoints.Count];+ var cross = (b - a).Cross(c - b);+ var dot = cross.Dot(expectedNormal);+ if (double.Abs(dot) <= LoopEndpointTolerance)+ {+ continue;+ }++ var sign = dot > 0d ? 1 : -1;+ if (referenceSign == 0)+ {+ referenceSign = sign;+ continue;+ }++ if (referenceSign != sign)+ {+ return false;+ }+ }++ return referenceSign != 0;+ }++ private static bool PointsAlmostEqual(Point3D left, Point3D right)+ => (left - right).LengthSquared <= (LoopEndpointTolerance * LoopEndpointTolerance);+private static KernelResult<DisplayEdgePolyline> TessellateEdge(BrepBody body, EdgeId edgeId, DisplayTessellationOptions options){if (!body.TryGetEdgeCurveGeometry(edgeId, out var curve) || curve is null)@@ -392,22 +547,106 @@ public static class BrepDisplayTessellator}}- private static KernelResult<(Point3D Start, Point3D End)> GetEdgeEndpoints(BrepBody body, EdgeId edgeId, bool reversed)+ private static KernelResult<Dictionary<VertexId, Point3D>> BuildLoopVertexPointLookup(BrepBody body, IReadOnlyList<Coedge> coedges, FaceId faceId)+ {+ var requiredVertexIds = new HashSet<VertexId>();+ foreach (var coedge in coedges)+ {+ if (!body.TryGetEdgeVertices(coedge.EdgeId, out var startVertexId, out var endVertexId))+ {+ return KernelResult<Dictionary<VertexId, Point3D>>.Failure([CreateNotImplemented($"Face {faceId.Value} references missing edge topology for edge {coedge.EdgeId.Value}.")]);+ }++ requiredVertexIds.Add(startVertexId);+ requiredVertexIds.Add(endVertexId);+ }++ var vertexPoints = new Dictionary<VertexId, Point3D>();+ foreach (var vertexId in requiredVertexIds.OrderBy(v => v.Value))+ {+ var pointResult = GetVertexPoint(body, vertexId);+ if (!pointResult.IsSuccess)+ {+ return KernelResult<Dictionary<VertexId, Point3D>>.Failure(pointResult.Diagnostics);+ }++ vertexPoints[vertexId] = pointResult.Value;+ }++ return KernelResult<Dictionary<VertexId, Point3D>>.Success(vertexPoints);+ }++ private static KernelResult<Point3D> GetVertexPoint(BrepBody body, VertexId vertexId)+ {+ foreach (var edge in body.Topology.Edges.OrderBy(e => e.Id.Value))+ {+ if (edge.StartVertexId != vertexId)+ {+ continue;+ }++ var edgePoint = EvaluateEdgeEndpoint(body, edge.Id, useStart: true);+ if (edgePoint.IsSuccess)+ {+ return KernelResult<Point3D>.Success(edgePoint.Value);+ }+ }++ foreach (var edge in body.Topology.Edges.OrderBy(e => e.Id.Value))+ {+ if (edge.EndVertexId != vertexId)+ {+ continue;+ }++ var edgePoint = EvaluateEdgeEndpoint(body, edge.Id, useStart: false);+ if (edgePoint.IsSuccess)+ {+ return KernelResult<Point3D>.Success(edgePoint.Value);+ }+ }++ return KernelResult<Point3D>.Failure([CreateNotImplemented($"Vertex {vertexId.Value} cannot be resolved to a geometric point for tessellation.")]);+ }++ private static KernelResult<Point3D> EvaluateEdgeEndpoint(BrepBody body, EdgeId edgeId, bool useStart){if (!body.TryGetEdgeCurveGeometry(edgeId, out var curve) || curve is null || curve.Kind != CurveGeometryKind.Line3){- return KernelResult<(Point3D, Point3D)>.Failure([CreateNotImplemented($"Edge {edgeId.Value} planar polygon tessellation requires line geometry.")]);+ return KernelResult<Point3D>.Failure([CreateNotImplemented($"Edge {edgeId.Value} planar polygon tessellation requires line geometry.")]);}if (!body.Bindings.TryGetEdgeBinding(edgeId, out var binding)){- return KernelResult<(Point3D, Point3D)>.Failure([CreateNotImplemented($"Edge {edgeId.Value} is missing curve binding.")]);+ return KernelResult<Point3D>.Failure([CreateNotImplemented($"Edge {edgeId.Value} is missing curve binding.")]);}- var line = curve.Line3!.Value;var interval = binding.TrimInterval ?? new ParameterInterval(0d, 1d);- var start = line.Evaluate(interval.Start);- var end = line.Evaluate(interval.End);+ var parameter = useStart ? interval.Start : interval.End;+ return KernelResult<Point3D>.Success(curve.Line3!.Value.Evaluate(parameter));+ }++ private static KernelResult<(Point3D Start, Point3D End)> GetEdgeEndpoints(+ BrepBody body,+ EdgeId edgeId,+ bool reversed,+ IReadOnlyDictionary<VertexId, Point3D> vertexPoints)+ {+ if (!body.TryGetEdgeCurveGeometry(edgeId, out var curve) || curve is null || curve.Kind != CurveGeometryKind.Line3)+ {+ return KernelResult<(Point3D, Point3D)>.Failure([CreateNotImplemented($"Edge {edgeId.Value} planar polygon tessellation requires line geometry.")]);+ }++ if (!body.TryGetEdgeVertices(edgeId, out var startVertexId, out var endVertexId))+ {+ return KernelResult<(Point3D, Point3D)>.Failure([CreateNotImplemented($"Edge {edgeId.Value} is missing topology endpoints.")]);+ }++ if (!vertexPoints.TryGetValue(startVertexId, out var start) || !vertexPoints.TryGetValue(endVertexId, out var end))+ {+ return KernelResult<(Point3D, Point3D)>.Failure([CreateNotImplemented($"Edge {edgeId.Value} references unresolved vertex points.")]);+ }+return KernelResult<(Point3D, Point3D)>.Success(reversed ? (end, start) : (start, end));}