fix: correct Width/Length axis mapping and add spiral center-fill

Box constructor and derived properties (Right, Top, Center, Translate, Offset)
had Width and Length swapped — Length is X axis, Width is Y axis. Corrected
across Core geometry, plate bounding box, rectangle packing, fill algorithms,
tests, and UI renderers.

Added FillSpiral with center remnant detection and recursive FillBest on
the gap between the 4 spiral quadrants. RectFill.FillBest now compares
spiral+center vs full best-fit fairly. BestCombination returns a
CombinationResult record instead of out params.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-03 21:22:55 -04:00
parent e50a7c82cf
commit c5943e22eb
55 changed files with 433 additions and 257 deletions

View File

@@ -29,11 +29,15 @@ namespace OpenNest.RectanglePacking
Bin.Items.AddRange(bin1.Items);
else
Bin.Items.AddRange(bin2.Items);
}
}
public override void Fill(Item item, int maxCount)
{
throw new NotImplementedException();
Fill(item);
if (Bin.Items.Count > maxCount)
Bin.Items.RemoveRange(maxCount, Bin.Items.Count - maxCount);
}
private Bin BestFitHorizontal(Item item) => BestFitAxis(item, horizontal: true);
@@ -44,14 +48,18 @@ namespace OpenNest.RectanglePacking
{
var bin = Bin.Clone() as Bin;
var primarySize = horizontal ? item.Width : item.Length;
var secondarySize = horizontal ? item.Length : item.Width;
var binPrimary = horizontal ? bin.Width : Bin.Length;
var binSecondary = horizontal ? bin.Length : Bin.Width;
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;
if (!BestCombination.FindFrom2(primarySize, secondarySize, binPrimary, out var normalPrimary, out var rotatePrimary))
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);
@@ -67,9 +75,9 @@ namespace OpenNest.RectanglePacking
bin.Items.AddRange(FillGrid(item, normalRows, normalCols, int.MaxValue));
if (horizontal)
item.Location.X += item.Width * normalPrimary;
item.Location.X += item.Length * normalPrimary;
else
item.Location.Y += item.Length * normalPrimary;
item.Location.Y += item.Width * normalPrimary;
item.Rotate();

View File

@@ -27,8 +27,8 @@ namespace OpenNest.RectanglePacking
{
for (var j = 0; j < innerCount; j++)
{
var x = (columnMajor ? i : j) * item.Width + item.X;
var y = (columnMajor ? j : i) * item.Length + item.Y;
var x = (columnMajor ? i : j) * item.Length + item.X;
var y = (columnMajor ? j : i) * item.Width + item.Y;
var clone = item.Clone() as Item;
clone.Location = new Vector(x, y);

View File

@@ -14,16 +14,16 @@ namespace OpenNest.RectanglePacking
public override void Fill(Item item)
{
var ycount = (int)System.Math.Floor((Bin.Length + Tolerance.Epsilon) / item.Length);
var xcount = (int)System.Math.Floor((Bin.Width + Tolerance.Epsilon) / item.Width);
var ycount = (int)System.Math.Floor((Bin.Width + Tolerance.Epsilon) / item.Width);
var xcount = (int)System.Math.Floor((Bin.Length + Tolerance.Epsilon) / item.Length);
for (int i = 0; i < xcount; i++)
{
var x = item.Width * i + Bin.X;
var x = item.Length * i + Bin.X;
for (int j = 0; j < ycount; j++)
{
var y = item.Length * j + Bin.Y;
var y = item.Width * j + Bin.Y;
var addedItem = item.Clone() as Item;
addedItem.Location = new Vector(x, y);
@@ -35,8 +35,8 @@ namespace OpenNest.RectanglePacking
public override void Fill(Item item, int maxCount)
{
var ycount = (int)System.Math.Floor((Bin.Length + Tolerance.Epsilon) / item.Length);
var xcount = (int)System.Math.Floor((Bin.Width + Tolerance.Epsilon) / item.Width);
var ycount = (int)System.Math.Floor((Bin.Width + Tolerance.Epsilon) / item.Width);
var xcount = (int)System.Math.Floor((Bin.Length + Tolerance.Epsilon) / item.Length);
var count = ycount * xcount;
if (count <= maxCount)

View File

@@ -0,0 +1,83 @@
using OpenNest.Geometry;
using OpenNest.Math;
namespace OpenNest.RectanglePacking
{
internal class FillSpiral : FillEngine
{
public Box CenterRemnant { get; private set; }
public FillSpiral(Bin bin)
: base(bin)
{
}
public override void Fill(Item item)
{
Fill(item, int.MaxValue);
}
public override void Fill(Item item, int maxCount)
{
if (item == null) return;
// Width = Y axis, Length = X axis
var comboY = BestCombination.FindFrom2(item.Width, item.Length, Bin.Width);
var comboX = BestCombination.FindFrom2(item.Length, item.Width, Bin.Length);
if (!comboY.Found || !comboX.Found)
return;
var q14size = new Size(
item.Width * comboY.Count1,
item.Length * comboX.Count1);
var q23size = new Size(
item.Length * comboY.Count2,
item.Width * comboX.Count2);
if ((q14size.Width > q23size.Width && q14size.Length > q23size.Length) ||
(q23size.Width > q14size.Width && q23size.Length > q14size.Length))
return; // cant do an efficient spiral fill
// Q1: normal orientation at bin origin
item.Location = Bin.Location;
var q1 = FillGrid(item, comboY.Count1, comboX.Count1, maxCount);
Bin.Items.AddRange(q1);
// Q2: rotated, above Q1
item.Rotate();
item.Location = new Vector(Bin.X, Bin.Y + q14size.Width);
var q2 = FillGrid(item, comboY.Count2, comboX.Count2, maxCount - Bin.Items.Count);
Bin.Items.AddRange(q2);
// Q3: rotated, right of Q1
item.Location = new Vector(Bin.X + q14size.Length, Bin.Y);
var q3 = FillGrid(item, comboY.Count2, comboX.Count2, maxCount - Bin.Items.Count);
Bin.Items.AddRange(q3);
// Q4: normal orientation, diagonal from Q1
item.Rotate();
item.Location = new Vector(
Bin.X + q23size.Length,
Bin.Y + q23size.Width);
var q4 = FillGrid(item, comboY.Count1, comboX.Count1, maxCount);
Bin.Items.AddRange(q4);
// Compute center remnant — the rectangular gap between the 4 quadrants
// Only valid when all 4 quadrants have items; otherwise the "center"
// overlaps an occupied quadrant and recursion never terminates.
var centerW = System.Math.Abs(q14size.Length - q23size.Length);
var centerH = System.Math.Abs(q14size.Width - q23size.Width);
if (comboY.Count1 > 0 && comboY.Count2 > 0 && comboX.Count1 > 0 && comboX.Count2 > 0
&& centerW > Tolerance.Epsilon && centerH > Tolerance.Epsilon)
{
CenterRemnant = new Box(
Bin.X + System.Math.Min(q14size.Length, q23size.Length),
Bin.Y + System.Math.Min(q14size.Width, q23size.Width),
centerW,
centerH);
}
}
}
}

View File

@@ -37,8 +37,8 @@ namespace OpenNest.RectanglePacking
double minX = items[0].X;
double minY = items[0].Y;
double maxX = items[0].X + items[0].Width;
double maxY = items[0].Y + items[0].Length;
double maxX = items[0].Right;
double maxY = items[0].Top;
foreach (var box in items)
{

View File

@@ -16,11 +16,11 @@ namespace OpenNest.RectanglePacking
public override void Pack(List<Item> items)
{
items = items.OrderBy(i => -i.Length).ToList();
items = items.OrderBy(i => -i.Width).ToList();
foreach (var item in items)
{
if (item.Length > Bin.Length)
if (item.Width > Bin.Width)
continue;
var level = FindLevel(item);
@@ -36,10 +36,10 @@ namespace OpenNest.RectanglePacking
{
foreach (var level in levels)
{
if (level.Height < item.Length)
if (level.Height < item.Width)
continue;
if (level.RemainingWidth < item.Width)
if (level.RemainingLength < item.Length)
continue;
return level;
@@ -58,12 +58,12 @@ namespace OpenNest.RectanglePacking
var remaining = Bin.Top - y;
if (remaining < item.Length)
if (remaining < item.Width)
return null;
var level = new Level(Bin);
level.Y = y;
level.Height = item.Length;
level.Height = item.Width;
levels.Add(level);
@@ -93,9 +93,9 @@ namespace OpenNest.RectanglePacking
set { NextItemLocation.Y = value; }
}
public double Width
public double LevelLength
{
get { return Parent.Width; }
get { return Parent.Length; }
}
public double Height { get; set; }
@@ -105,9 +105,9 @@ namespace OpenNest.RectanglePacking
get { return Y + Height; }
}
public double RemainingWidth
public double RemainingLength
{
get { return X + Width - NextItemLocation.X; }
get { return X + LevelLength - NextItemLocation.X; }
}
public void AddItem(Item item)
@@ -115,7 +115,7 @@ namespace OpenNest.RectanglePacking
item.Location = NextItemLocation;
Parent.Items.Add(item);
NextItemLocation = new Vector(NextItemLocation.X + item.Width, NextItemLocation.Y);
NextItemLocation = new Vector(NextItemLocation.X + item.Length, NextItemLocation.Y);
}
}
}

View File

@@ -0,0 +1,44 @@
using OpenNest.Geometry;
namespace OpenNest.RectanglePacking
{
internal static class RectFill
{
public static void FillBest(Bin bin, Item item, int maxCount = int.MaxValue)
{
var spiralBin = bin.Clone() as Bin;
var spiral = new FillSpiral(spiralBin);
spiral.Fill(item, maxCount);
// Recursively fill the center remnant of the spiral
if (spiralBin.Items.Count > 0 && spiral.CenterRemnant != null)
{
var center = spiral.CenterRemnant;
var fitsNormal = item.Length <= center.Length && item.Width <= center.Width;
var fitsRotated = item.Width <= center.Length && item.Length <= center.Width;
if (fitsNormal || fitsRotated)
{
var remaining = maxCount - spiralBin.Items.Count;
FillBest(center.Location, center.Size, spiralBin, item, remaining);
}
}
var bestFitBin = bin.Clone() as Bin;
new FillBestFit(bestFitBin).Fill(item, maxCount);
var winner = spiralBin.Items.Count >= bestFitBin.Items.Count ? spiralBin : bestFitBin;
bin.Items.AddRange(winner.Items);
}
public static void FillBest(Vector location, Size size, Bin target, Item item, int maxCount)
{
if (size.Width <= 0 || size.Length <= 0 || maxCount <= 0)
return;
var bin = new Bin { Location = location, Size = size };
FillBest(bin, item, maxCount);
target.Items.AddRange(bin.Items);
}
}
}