From f73bb2bc2f617c739a7d3b5ae8825a838f14b53b Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Wed, 18 Mar 2026 21:04:35 -0400 Subject: [PATCH] =?UTF-8?q?refactor(fill):=20simplify=20FindPatternCopyDis?= =?UTF-8?q?tance=20=E2=80=94=20extract=20pair=20loop,=20remove=20redundant?= =?UTF-8?q?=20span=20calculation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The pattern bounding box already computes max(upper) - min(lower), so the manual loop was redundant. Extract the N×N pair distance loop into a static FindMaxPairDistance helper. Drop pre-cached edge arrays since GetEdges() returns stored references with zero allocation. Co-Authored-By: Claude Opus 4.6 (1M context) --- OpenNest.Engine/Fill/FillLinear.cs | 58 +++++++++++++----------------- 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/OpenNest.Engine/Fill/FillLinear.cs b/OpenNest.Engine/Fill/FillLinear.cs index 674bff3..4a09190 100644 --- a/OpenNest.Engine/Fill/FillLinear.cs +++ b/OpenNest.Engine/Fill/FillLinear.cs @@ -110,47 +110,40 @@ namespace OpenNest.Engine.Fill var pushDir = GetPushDirection(direction); var opposite = SpatialQuery.OppositeDirection(pushDir); - // Compute a starting offset large enough that every part-pair in - // patternB has its offset geometry beyond patternA's offset geometry. - var maxUpper = double.MinValue; - var minLower = double.MaxValue; - - for (var i = 0; i < patternA.Parts.Count; i++) - { - var bb = patternA.Parts[i].BoundingBox; - var upper = direction == NestDirection.Horizontal ? bb.Right : bb.Top; - var lower = direction == NestDirection.Horizontal ? bb.Left : bb.Bottom; - - if (upper > maxUpper) maxUpper = upper; - if (lower < minLower) minLower = lower; - } - - var startOffset = System.Math.Max(bboxDim, - maxUpper - minLower + PartSpacing + Tolerance.Epsilon); - + // bboxDim already spans max(upper) - min(lower) across all parts, + // so the start offset just needs to push beyond that plus spacing. + var startOffset = bboxDim + PartSpacing + Tolerance.Epsilon; var offset = MakeOffset(direction, startOffset); - // Pre-cache edge arrays. - var movingEdges = new (Vector start, Vector end)[patternA.Parts.Count][]; - var stationaryEdges = new (Vector start, Vector end)[patternA.Parts.Count][]; + var maxCopyDistance = FindMaxPairDistance( + patternA.Parts, boundaries, offset, pushDir, opposite, startOffset); - for (var i = 0; i < patternA.Parts.Count; i++) - { - movingEdges[i] = boundaries[i].GetEdges(pushDir); - stationaryEdges[i] = boundaries[i].GetEdges(opposite); - } + if (maxCopyDistance < Tolerance.Epsilon) + return bboxDim + PartSpacing; + return maxCopyDistance; + } + + /// + /// Tests every pair of parts across adjacent pattern copies and returns the + /// maximum copy distance found. Returns 0 if no valid slide was found. + /// + private static double FindMaxPairDistance( + List parts, PartBoundary[] boundaries, Vector offset, + PushDirection pushDir, PushDirection opposite, double startOffset) + { var maxCopyDistance = 0.0; - for (var j = 0; j < patternA.Parts.Count; j++) + for (var j = 0; j < parts.Count; j++) { - var locationB = patternA.Parts[j].Location + offset; + var movingEdges = boundaries[j].GetEdges(pushDir); + var locationB = parts[j].Location + offset; - for (var i = 0; i < patternA.Parts.Count; i++) + for (var i = 0; i < parts.Count; i++) { var slideDistance = SpatialQuery.DirectionalDistance( - movingEdges[j], locationB, - stationaryEdges[i], patternA.Parts[i].Location, + movingEdges, locationB, + boundaries[i].GetEdges(opposite), parts[i].Location, pushDir); if (slideDistance >= double.MaxValue || slideDistance < 0) @@ -163,9 +156,6 @@ namespace OpenNest.Engine.Fill } } - if (maxCopyDistance < Tolerance.Epsilon) - return bboxDim + PartSpacing; - return maxCopyDistance; }