diff --git a/OpenNest.Engine/Fill/FillLinear.cs b/OpenNest.Engine/Fill/FillLinear.cs
index fb9a94a6..761a0413 100644
--- a/OpenNest.Engine/Fill/FillLinear.cs
+++ b/OpenNest.Engine/Fill/FillLinear.cs
@@ -128,7 +128,11 @@ namespace OpenNest.Engine.Fill
/// geometry inflated by half-spacing — same primitive the Compactor uses — so arcs
/// are exact and no bbox clamp is needed.
///
- private double FindPatternCopyDistance(Pattern patternA, NestDirection direction, OffsetPerimeterCache cache)
+ /// Farthest distance the caller can use. The search stops with
+ /// positive infinity as soon as one part pair proves the copy must move farther; the
+ /// exact distance is returned whenever it is within this bound.
+ private double FindPatternCopyDistance(Pattern patternA, NestDirection direction, OffsetPerimeterCache cache,
+ double useful = double.PositiveInfinity)
{
if (patternA.Parts.Count == 1)
return FindCopyDistance(patternA.Parts[0], direction, cache);
@@ -205,6 +209,10 @@ namespace OpenNest.Engine.Fill
if (copyDist > maxCopyDistance)
maxCopyDistance = copyDist;
+
+ // The copy distance is the maximum over every pair, so it can only grow.
+ if (maxCopyDistance > useful)
+ return double.PositiveInfinity;
}
}
@@ -232,9 +240,10 @@ namespace OpenNest.Engine.Fill
int maxParts = 0
)
{
- var copyDistance = FindPatternCopyDistance(basePattern, direction, cache);
+ var copyDistance = FindPatternCopyDistance(basePattern, direction, cache,
+ UsefulCopyDistance(basePattern, direction, maxParts));
- if (copyDistance <= 0)
+ if (copyDistance <= 0 || double.IsPositiveInfinity(copyDistance))
return new List();
var dim = GetDimension(basePattern.BoundingBox, direction);
@@ -291,6 +300,27 @@ namespace OpenNest.Engine.Fill
return result;
}
+ ///
+ /// Farthest copy distance at which can still add a part: the
+ /// first whole copy must end within the limit, or, for a multi-part pattern, one part of
+ /// it within the work area. Padded by beyond the
+ /// placement checks' own tolerance, so rounding in the clones' bounds never matters.
+ /// A row tiled across a strip it already fills (Stripe's estimate rows) needs no exact
+ /// copy distance: one slide past this bound settles that nothing fits.
+ ///
+ private double UsefulCopyDistance(Pattern basePattern, NestDirection direction, int maxParts)
+ {
+ var box = basePattern.BoundingBox;
+ var useful = GetLimit(direction) - GetStart(box, direction) - GetDimension(box, direction);
+ var partial = basePattern.Parts.Count > 1 && !(maxParts > 0 && basePattern.Parts.Count >= maxParts);
+ if (partial)
+ foreach (var part in basePattern.Parts)
+ useful = System.Math.Max(useful, direction == NestDirection.Horizontal
+ ? WorkArea.Right - part.BoundingBox.Right
+ : WorkArea.Top - part.BoundingBox.Top);
+ return useful + 2 * Tolerance.Epsilon;
+ }
+
///
/// Fallback tiling using bounding-box spacing when geometry-aware tiling
/// produces overlapping parts.
diff --git a/OpenNest.Engine/Fill/StripeFiller.cs b/OpenNest.Engine/Fill/StripeFiller.cs
index 47dfd17e..3810976d 100644
--- a/OpenNest.Engine/Fill/StripeFiller.cs
+++ b/OpenNest.Engine/Fill/StripeFiller.cs
@@ -181,9 +181,10 @@ public class StripeFiller
var spacing = _context.Plate.PartSpacing;
var sheetSpan = GetDimension(_context.WorkArea, axis);
+ var bounds = new RotatedBounds(pairParts);
var (angle, waste, count) = variant == 0
- ? ConvergeStripeAngle(pairParts, sheetSpan, spacing, axis, _context.Token, _context.MaxQuantity)
- : ConvergeStripeAngleShrink(pairParts, sheetSpan, spacing, axis, _context.Token, _context.MaxQuantity);
+ ? ConvergeStripeAngle(bounds, sheetSpan, spacing, axis, _context.Token, _context.MaxQuantity)
+ : ConvergeStripeAngleShrink(bounds, sheetSpan, spacing, axis, _context.Token, _context.MaxQuantity);
var layout = new StripeLayout { Angle = angle, Waste = waste, Count = count };
if (count > 0)
@@ -392,10 +393,38 @@ public class StripeFiller
return null;
}
+ ///
+ /// Bounding boxes of one pattern's parts rotated about their common center, by angle. A
+ /// box is a pure function of the parts and the angle, and the angle searches sample the
+ /// same angles over and over (every scans the same
+ /// grid), so each is built once. The parts must not change while it is in use.
+ ///
+ private sealed class RotatedBounds
+ {
+ private readonly Dictionary boxes = new();
+
+ public RotatedBounds(List parts) => Parts = parts;
+
+ public List Parts { get; }
+
+ public Box At(double angle)
+ {
+ if (!boxes.TryGetValue(angle, out var box))
+ boxes[angle] = box = FillHelpers.BuildRotatedPattern(Parts, angle).BoundingBox;
+ return box;
+ }
+ }
+
public static double FindAngleForTargetSpan(
List patternParts,
double targetSpan,
NestDirection axis
+ ) => FindAngleForTargetSpan(new RotatedBounds(patternParts), targetSpan, axis);
+
+ private static double FindAngleForTargetSpan(
+ RotatedBounds patternParts,
+ double targetSpan,
+ NestDirection axis
)
{
var bestAngle = 0.0;
@@ -445,9 +474,9 @@ public class StripeFiller
/// Returns the rotation angle that orients the pair with its short side
/// along the given axis. Returns 0 if already oriented, PI/2 if rotated.
///
- private static double OrientShortSideAlong(List patternParts, NestDirection axis)
+ private static double OrientShortSideAlong(RotatedBounds patternParts, NestDirection axis)
{
- var box = FillHelpers.BuildRotatedPattern(patternParts, 0).BoundingBox;
+ var box = patternParts.At(0);
var span0 = GetDimension(box, axis);
var perpSpan0 = axis == NestDirection.Horizontal ? box.Width : box.Length;
@@ -470,6 +499,15 @@ public class StripeFiller
NestDirection axis,
CancellationToken token = default,
int maxParts = 0
+ ) => ConvergeStripeAngle(new RotatedBounds(patternParts), sheetSpan, spacing, axis, token, maxParts);
+
+ private static (double Angle, double Waste, int Count) ConvergeStripeAngle(
+ RotatedBounds patternParts,
+ double sheetSpan,
+ double spacing,
+ NestDirection axis,
+ CancellationToken token,
+ int maxParts
)
{
var startAngle = OrientShortSideAlong(patternParts, axis);
@@ -487,11 +525,19 @@ public class StripeFiller
NestDirection axis,
CancellationToken token = default,
int maxParts = 0
+ ) => ConvergeStripeAngleShrink(new RotatedBounds(patternParts), sheetSpan, spacing, axis, token, maxParts);
+
+ private static (double Angle, double Waste, int Count) ConvergeStripeAngleShrink(
+ RotatedBounds patternParts,
+ double sheetSpan,
+ double spacing,
+ NestDirection axis,
+ CancellationToken token,
+ int maxParts
)
{
var baseAngle = OrientShortSideAlong(patternParts, axis);
- var naturalPattern = FillHelpers.BuildRotatedPattern(patternParts, baseAngle);
- var naturalSpan = GetDimension(naturalPattern.BoundingBox, axis);
+ var naturalSpan = GetDimension(patternParts.At(baseAngle), axis);
if (naturalSpan + spacing <= 0)
return (0, double.MaxValue, 0);
@@ -508,7 +554,7 @@ public class StripeFiller
}
private static (double Angle, double Waste, int Count) ConvergeFromAngle(
- List patternParts,
+ RotatedBounds patternParts,
double startAngle,
double sheetSpan,
double spacing,
@@ -527,7 +573,7 @@ public class StripeFiller
{
token.ThrowIfCancellationRequested();
- var rotated = FillHelpers.BuildRotatedPattern(patternParts, currentAngle);
+ var rotated = FillHelpers.BuildRotatedPattern(patternParts.Parts, currentAngle);
var pairSpan = GetDimension(rotated.BoundingBox, axis);
var perpDim =
axis == NestDirection.Horizontal
@@ -587,7 +633,7 @@ public class StripeFiller
}
private static double BisectForTarget(
- List patternParts,
+ RotatedBounds patternParts,
double lo,
double hi,
double targetSpan,
@@ -625,12 +671,12 @@ public class StripeFiller
return bestAngle;
}
- private static double GetRotatedSpan(List patternParts, double angle, NestDirection axis)
+ private static double GetRotatedSpan(RotatedBounds patternParts, double angle, NestDirection axis)
{
- var rotated = FillHelpers.BuildRotatedPattern(patternParts, angle);
+ var box = patternParts.At(angle);
return axis == NestDirection.Horizontal
- ? rotated.BoundingBox.Length
- : rotated.BoundingBox.Width;
+ ? box.Length
+ : box.Width;
}
private static double GetDimension(Box box, NestDirection axis)