refactor(fill): simplify FindPatternCopyDistance — extract pair loop, remove redundant span calculation
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) <noreply@anthropic.com>
This commit is contained in:
@@ -110,47 +110,40 @@ namespace OpenNest.Engine.Fill
|
|||||||
var pushDir = GetPushDirection(direction);
|
var pushDir = GetPushDirection(direction);
|
||||||
var opposite = SpatialQuery.OppositeDirection(pushDir);
|
var opposite = SpatialQuery.OppositeDirection(pushDir);
|
||||||
|
|
||||||
// Compute a starting offset large enough that every part-pair in
|
// bboxDim already spans max(upper) - min(lower) across all parts,
|
||||||
// patternB has its offset geometry beyond patternA's offset geometry.
|
// so the start offset just needs to push beyond that plus spacing.
|
||||||
var maxUpper = double.MinValue;
|
var startOffset = bboxDim + PartSpacing + Tolerance.Epsilon;
|
||||||
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);
|
|
||||||
|
|
||||||
var offset = MakeOffset(direction, startOffset);
|
var offset = MakeOffset(direction, startOffset);
|
||||||
|
|
||||||
// Pre-cache edge arrays.
|
var maxCopyDistance = FindMaxPairDistance(
|
||||||
var movingEdges = new (Vector start, Vector end)[patternA.Parts.Count][];
|
patternA.Parts, boundaries, offset, pushDir, opposite, startOffset);
|
||||||
var stationaryEdges = new (Vector start, Vector end)[patternA.Parts.Count][];
|
|
||||||
|
|
||||||
for (var i = 0; i < patternA.Parts.Count; i++)
|
if (maxCopyDistance < Tolerance.Epsilon)
|
||||||
{
|
return bboxDim + PartSpacing;
|
||||||
movingEdges[i] = boundaries[i].GetEdges(pushDir);
|
|
||||||
stationaryEdges[i] = boundaries[i].GetEdges(opposite);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
return maxCopyDistance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tests every pair of parts across adjacent pattern copies and returns the
|
||||||
|
/// maximum copy distance found. Returns 0 if no valid slide was found.
|
||||||
|
/// </summary>
|
||||||
|
private static double FindMaxPairDistance(
|
||||||
|
List<Part> parts, PartBoundary[] boundaries, Vector offset,
|
||||||
|
PushDirection pushDir, PushDirection opposite, double startOffset)
|
||||||
|
{
|
||||||
var maxCopyDistance = 0.0;
|
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(
|
var slideDistance = SpatialQuery.DirectionalDistance(
|
||||||
movingEdges[j], locationB,
|
movingEdges, locationB,
|
||||||
stationaryEdges[i], patternA.Parts[i].Location,
|
boundaries[i].GetEdges(opposite), parts[i].Location,
|
||||||
pushDir);
|
pushDir);
|
||||||
|
|
||||||
if (slideDistance >= double.MaxValue || slideDistance < 0)
|
if (slideDistance >= double.MaxValue || slideDistance < 0)
|
||||||
@@ -163,9 +156,6 @@ namespace OpenNest.Engine.Fill
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (maxCopyDistance < Tolerance.Epsilon)
|
|
||||||
return bboxDim + PartSpacing;
|
|
||||||
|
|
||||||
return maxCopyDistance;
|
return maxCopyDistance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user