Files
OpenNest/OpenNest.Engine/Fill/RemnantComparer.cs
T
ajandClaude Opus 5.5 1206f04114 fix(fill): lay remnant quantities of round parts across the full side
A Vertical or Horizontal Remnant fill of a quantity of round parts left
a deep remnant on the table: twelve 8 in. flanges took 25.6 in. of a
60 in. sheet's length and stopped 16 in. short of its far edge, where
two full-height columns need 15.1 in. Three causes:

- The low-quantity shrink scaled both sides, so stripes could not span
  the side the remnant runs along. It now narrows only the trimmed axis.
- The ranking compared untrimmed counts, so the full-sheet layout always
  beat the full-height columns and was then trimmed. It now ranks the
  layout as it will be kept.
- The trim kept overlapping bounding boxes as interlocked pairs, which
  staggered discs always are. Discs are now trimmed one by one.

The circle strategy also offers each stagger mode's stripes in both
directions to a remnant ranking. Every change is limited to remnant
fills of round parts: applied to other shapes, the trimmed ranking let
a tilted bar layout block the rest of a nest, and the disc trim moved
the Irregular engine's sample layout, so every other fill is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 20:23:05 -05:00

90 lines
3.2 KiB
C#

using System.Collections.Generic;
using OpenNest.Geometry;
using OpenNest.Math;
namespace OpenNest.Engine.Fill
{
/// <summary>
/// Ranks fill results to keep one side of the work area free for a remnant.
/// Tiebreak chain: count > smallest extent along the ranked axis > highest density.
/// With a <see cref="Quantity"/>, layouts holding at least that many parts tie on count and
/// are ranked by their extent as they will be kept (trimmed to the quantity along
/// <see cref="TrimAxis"/>), then by their untrimmed count and density.
/// </summary>
public abstract class RemnantComparer : IFillComparer
{
/// <summary>
/// The quantity the fill will be trimmed to; 0 ranks every layout as it stands.
/// </summary>
public int Quantity { get; init; }
/// <summary>
/// Whether the trim to <see cref="Quantity"/> keeps interlocked pairs whole; false for
/// round parts, which never interlock.
/// </summary>
public bool KeepsPairs { get; init; } = true;
/// <summary>The axis the filler trims surplus parts from (the ranked axis).</summary>
protected abstract ShrinkAxis TrimAxis { get; }
/// <summary>The lower edge of a bounding box along the ranked axis.</summary>
protected abstract double Low(Box box);
/// <summary>The upper edge of a bounding box along the ranked axis.</summary>
protected abstract double High(Box box);
public bool IsBetter(List<Part> candidate, List<Part> current, Box workArea)
{
if (candidate == null || candidate.Count == 0)
return false;
if (current == null || current.Count == 0)
return true;
var candKept = Kept(candidate);
var currKept = Kept(current);
if (candKept.Count != currKept.Count)
return candKept.Count > currKept.Count;
var candExtent = Extent(candKept);
var currExtent = Extent(currKept);
if (!candExtent.IsEqualTo(currExtent))
return candExtent < currExtent;
// Kept layouts tying on extent fall back to the untrimmed ranking: the pattern
// holding more parts packs tighter.
if (candidate.Count != current.Count)
return candidate.Count > current.Count;
return FillScore.Compute(candidate, workArea).Density
> FillScore.Compute(current, workArea).Density;
}
private List<Part> Kept(List<Part> parts) =>
Quantity > 0 && parts.Count > Quantity
? ShrinkFiller.TrimToCount(parts, Quantity, TrimAxis, KeepsPairs)
: parts;
private double Extent(List<Part> parts)
{
var min = double.MaxValue;
var max = double.MinValue;
foreach (var part in parts)
{
var bb = part.BoundingBox;
var low = Low(bb);
var high = High(bb);
if (low < min)
min = low;
if (high > max)
max = high;
}
return max - min;
}
}
}