Files
OpenNest/OpenNest.Engine/RectanglePacking/FillBestFit.cs
T
ajandClaude Opus 5.5 e011fe8290 refactor: fold duplicated helpers into one shared copy each
Roslyn Bridge's duplicate scan found helpers copied verbatim between
projects, so a fix to one copy could silently miss the other. Each copy
now calls a single implementation with the same body:

- boundary nearest point: ClipperBridge.NearestBoundaryPoint (ErodedPierce,
  ScrapPierceSearch)
- nesting profile of a part: NestLayoutCheck.Profile (CommonLinePair)
- part label points: PartLabels in OpenNest.Desktop (WinForms LayoutPart)
- copied common-line groups: CommonLineGroup.AmongParts (ActionClone,
  PlaceTool)
- shared saved cutting parameters: CuttingParametersSerializer.SharedBy
  (MCP, Console)
- bounding box of boxes: generic BoundingBox.GetBoundingBox<T> (rectangle
  packing items)
- box proximity: Clearance.BoxesWithin (Expander)

Behavior is unchanged: every moved body is identical to the copies it
replaces, so the fill change in Expander and rectangle packing skips the
problem-nest corpus. Core, IO, Desktop and Engine suites and the label,
clone and common-line WinForms tests pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 17:50:53 -05:00

91 lines
3.0 KiB
C#

using System;
using OpenNest.Engine.Fill;
using OpenNest.Geometry;
using OpenNest.Math;
namespace OpenNest.Engine.RectanglePacking
{
internal class FillBestFit : FillEngine
{
public FillBestFit(Bin bin)
: base(bin) { }
public override void Fill(Item item)
{
var bin1 = BestFitHorizontal(item);
var bin2 = BestFitVertical(item);
if (bin1.Items.Count == bin2.Items.Count)
{
var usedArea1 = bin1.Items.GetBoundingBox().Area();
var usedArea2 = bin2.Items.GetBoundingBox().Area();
if (usedArea2 < usedArea1)
Bin.Items.AddRange(bin2.Items);
else
Bin.Items.AddRange(bin1.Items);
}
else if (bin1.Items.Count > bin2.Items.Count)
Bin.Items.AddRange(bin1.Items);
else
Bin.Items.AddRange(bin2.Items);
}
public override void Fill(Item item, int maxCount)
{
Fill(item);
if (Bin.Items.Count > maxCount)
Bin.Items.RemoveRange(maxCount, Bin.Items.Count - maxCount);
}
private Bin BestFitHorizontal(Item item) => BestFitAxis(item, horizontal: true);
private Bin BestFitVertical(Item item) => BestFitAxis(item, horizontal: false);
private Bin BestFitAxis(Item item, bool horizontal)
{
var bin = Bin.Clone() as Bin;
var primarySize = horizontal ? item.Length : item.Width;
var secondarySize = horizontal ? item.Width : item.Length;
var binPrimary = horizontal ? bin.Length : Bin.Width;
var binSecondary = horizontal ? bin.Width : Bin.Length;
var combo = BestCombination.FindFrom2(primarySize, secondarySize, binPrimary);
if (!combo.Found)
return bin;
var normalPrimary = combo.Count1;
var rotatePrimary = combo.Count2;
var normalSecondary = (int)
System.Math.Floor((binSecondary + Tolerance.Epsilon) / secondarySize);
var rotateSecondary = (int)
System.Math.Floor((binSecondary + Tolerance.Epsilon) / primarySize);
var (normalRows, normalCols) = horizontal
? (normalSecondary, normalPrimary)
: (normalPrimary, normalSecondary);
var (rotateRows, rotateCols) = horizontal
? (rotateSecondary, rotatePrimary)
: (rotatePrimary, rotateSecondary);
item.Location = bin.Location;
bin.Items.AddRange(FillGrid(item, normalRows, normalCols, int.MaxValue));
if (horizontal)
item.Location.X += item.Length * normalPrimary;
else
item.Location.Y += item.Width * normalPrimary;
item.Rotate();
bin.Items.AddRange(FillGrid(item, rotateRows, rotateCols, int.MaxValue));
return bin;
}
}
}