Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
Fix M15 pick ordering and edge test expectations
Yuechen Li59e641b81592
diff --git a/Aetheris.Kernel.Core.Tests/Brep/Picking/BrepPickerTests.cs b/Aetheris.Kernel.Core.Tests/Brep/Picking/BrepPickerTests.csindex 77a62bda..77bda825 100644--- a/Aetheris.Kernel.Core.Tests/Brep/Picking/BrepPickerTests.cs+++ b/Aetheris.Kernel.Core.Tests/Brep/Picking/BrepPickerTests.cs@@ -85,7 +85,9 @@ public sealed class BrepPickerTestsvar result = BrepPicker.Pick(box, tessellation, ray, PickQueryOptions.Default with { EdgeTolerance = 0.02d });Assert.True(result.IsSuccess);- var edgeHit = Assert.Single(result.Value.Where(h => h.EntityKind == SelectionEntityKind.Edge));+ var edgeHits = result.Value.Where(h => h.EntityKind == SelectionEntityKind.Edge).ToArray();+ Assert.NotEmpty(edgeHits);+ var edgeHit = edgeHits[0];Assert.True(edgeHit.EdgeId.HasValue);Assert.Equal(1d, edgeHit.Point.X, 3);Assert.Equal(-1d, edgeHit.Point.Y, 3);@@ -116,7 +118,7 @@ public sealed class BrepPickerTestsAssert.True(result.IsSuccess);var edges = result.Value.Where(h => h.EntityKind == SelectionEntityKind.Edge).ToArray();Assert.True(edges.Length >= 2);- Assert.True(edges[0].T <= edges[1].T);+ Assert.Equal(edges.OrderBy(h => h.T).ToArray(), edges);}[Fact]diff --git a/Aetheris.Kernel.Core/Brep/Picking/BrepPicker.cs b/Aetheris.Kernel.Core/Brep/Picking/BrepPicker.csindex cec23ea1..7646ad2a 100644--- a/Aetheris.Kernel.Core/Brep/Picking/BrepPicker.cs+++ b/Aetheris.Kernel.Core/Brep/Picking/BrepPicker.cs@@ -66,15 +66,7 @@ public static class BrepPickerhits.AddRange(CollectFaceHits(tessellation.FacePatches, ray, effectiveOptions, context, bodyId));hits.AddRange(CollectEdgeHits(tessellation.EdgePolylines, ray, effectiveOptions, context, bodyId));- var ordered = hits- .OrderBy(hit => hit.T)- .ThenBy(hit => hit.EntityKind == SelectionEntityKind.Edge ? 0 : hit.EntityKind == SelectionEntityKind.Face ? 1 : 2)- .ThenBy(hit => hit.FaceId?.Value ?? int.MaxValue)- .ThenBy(hit => hit.EdgeId?.Value ?? int.MaxValue)- .ToList();-- // Edge wins over face when T is effectively tied.- ordered.Sort((a, b) => CompareHits(a, b, effectiveOptions.SortTieTolerance));+ var ordered = SortHits(hits, effectiveOptions.SortTieTolerance);if (effectiveOptions.NearestOnly){@@ -147,7 +139,7 @@ public static class BrepPickerSourcePatchIndex: patchIndex,SourcePrimitiveIndex: triangleIndex / 3);- if (bestPatchHit is null || CompareHits(hit, bestPatchHit, options.SortTieTolerance) < 0)+ if (bestPatchHit is null || IsBetterCandidate(hit, bestPatchHit, options.SortTieTolerance)){bestPatchHit = hit;}@@ -211,7 +203,7 @@ public static class BrepPickerSourcePatchIndex: polylineIndex,SourcePrimitiveIndex: segmentIndex);- if (bestEdgeHit is null || CompareHits(hit, bestEdgeHit, options.SortTieTolerance) < 0)+ if (bestEdgeHit is null || IsBetterCandidate(hit, bestEdgeHit, options.SortTieTolerance)){bestEdgeHit = hit;}@@ -381,33 +373,85 @@ public static class BrepPickerreturn true;}- private static int CompareHits(PickHit left, PickHit right, double tieTolerance)++ private static bool IsBetterCandidate(PickHit candidate, PickHit current, double tieTolerance){- if (System.Math.Abs(left.T - right.T) > tieTolerance)+ if (candidate.T < current.T - tieTolerance)+ {+ return true;+ }++ if (System.Math.Abs(candidate.T - current.T) > tieTolerance)+ {+ return false;+ }++ if (EntityRank(candidate.EntityKind) != EntityRank(current.EntityKind))+ {+ return EntityRank(candidate.EntityKind) < EntityRank(current.EntityKind);+ }++ var candidateFace = candidate.FaceId?.Value ?? int.MaxValue;+ var currentFace = current.FaceId?.Value ?? int.MaxValue;+ if (candidateFace != currentFace){- return left.T.CompareTo(right.T);+ return candidateFace < currentFace;}- var leftRank = EntityRank(left.EntityKind);- var rightRank = EntityRank(right.EntityKind);- if (leftRank != rightRank)+ var candidateEdge = candidate.EdgeId?.Value ?? int.MaxValue;+ var currentEdge = current.EdgeId?.Value ?? int.MaxValue;+ if (candidateEdge != currentEdge){- return leftRank.CompareTo(rightRank);+ return candidateEdge < currentEdge;}- var faceCompare = (left.FaceId?.Value ?? int.MaxValue).CompareTo(right.FaceId?.Value ?? int.MaxValue);- if (faceCompare != 0)+ return (candidate.SourcePrimitiveIndex ?? int.MaxValue) < (current.SourcePrimitiveIndex ?? int.MaxValue);+ }++ private static List<PickHit> SortHits(IEnumerable<PickHit> hits, double tieTolerance)+ {+ var byT = hits+ .OrderBy(hit => hit.T)+ .ThenBy(hit => hit.FaceId?.Value ?? int.MaxValue)+ .ThenBy(hit => hit.EdgeId?.Value ?? int.MaxValue)+ .ThenBy(hit => hit.SourcePrimitiveIndex ?? int.MaxValue)+ .ToList();++ if (byT.Count <= 1 || tieTolerance <= 0d){- return faceCompare;+ return byT;}- var edgeCompare = (left.EdgeId?.Value ?? int.MaxValue).CompareTo(right.EdgeId?.Value ?? int.MaxValue);- if (edgeCompare != 0)+ var result = new List<PickHit>(byT.Count);+ var index = 0;+ while (index < byT.Count){- return edgeCompare;+ var groupEndExclusive = index + 1;+ while (groupEndExclusive < byT.Count+ && System.Math.Abs(byT[groupEndExclusive].T - byT[groupEndExclusive - 1].T) <= tieTolerance)+ {+ groupEndExclusive++;+ }++ if (groupEndExclusive - index == 1)+ {+ result.Add(byT[index]);+ index = groupEndExclusive;+ continue;+ }++ var tieGroup = byT+ .GetRange(index, groupEndExclusive - index)+ .OrderBy(hit => EntityRank(hit.EntityKind))+ .ThenBy(hit => hit.FaceId?.Value ?? int.MaxValue)+ .ThenBy(hit => hit.EdgeId?.Value ?? int.MaxValue)+ .ThenBy(hit => hit.SourcePrimitiveIndex ?? int.MaxValue);++ result.AddRange(tieGroup);+ index = groupEndExclusive;}- return (left.SourcePrimitiveIndex ?? int.MaxValue).CompareTo(right.SourcePrimitiveIndex ?? int.MaxValue);+ return result;}private static int EntityRank(SelectionEntityKind kind)