mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-04 07:19:24 -05:00
Expand Spacing moved nothing on a plate holding two groups of flat bars at different rotations: they are no single repeating pattern, and the pair search needs every pair to reach one spacing, which it could not do on the packed layout. When the pair search gains nothing on a packed selection, open X and then Y on their own, keeping the parts' order, and report the gap of each axis. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
109 lines
4.2 KiB
C#
109 lines
4.2 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using OpenNest.Engine.Fill;
|
|
using OpenNest.Engine.Tests.Jobs;
|
|
using OpenNest.Geometry;
|
|
using Xunit;
|
|
|
|
namespace OpenNest.Engine.Tests.Fill;
|
|
|
|
/// <summary>
|
|
/// Expand Spacing on a packed selection of two bar groups, which is no single
|
|
/// repeating pattern: each axis with room opens on its own.
|
|
/// </summary>
|
|
public class ExpanderSweepTests
|
|
{
|
|
private const double Gap = 0.25;
|
|
private const double Edge = 0.25;
|
|
|
|
/// <summary>Two columns of flat bars with three upright bars at their end,
|
|
/// all at a 0.25 gap but for a wider one before the uprights.</summary>
|
|
private static (Plate Plate, List<Part> Parts) TwoBarGroups(double widthY)
|
|
{
|
|
const int rows = 8;
|
|
const double barHeight = 2.21875; // eight rows fill 19.5 exactly
|
|
var plate = new Plate(new Size(widthY, 30)) { EdgeSpacing = new Spacing(Edge, Edge) };
|
|
var flat = new Drawing("flat", TestDrawingFactory.Rectangle(10, barHeight));
|
|
var upright = new Drawing("upright", TestDrawingFactory.Rectangle(2, 19.5));
|
|
var parts = new List<Part>();
|
|
for (var row = 0; row < rows; row++)
|
|
for (var column = 0; column < 2; column++)
|
|
parts.Add(new Part(flat, new Vector(Edge + column * (10 + Gap), Edge + row * (barHeight + Gap))));
|
|
for (var i = 0; i < 3; i++)
|
|
parts.Add(new Part(upright, new Vector(21.5 + i * (2 + Gap), Edge)));
|
|
foreach (var part in parts)
|
|
plate.Parts.Add(part);
|
|
return (plate, parts);
|
|
}
|
|
|
|
private static double SmallestGap(List<Part> parts) => parts
|
|
.SelectMany((a, i) => parts.Skip(i + 1).Select(b => BoxGap(a.BoundingBox, b.BoundingBox)))
|
|
.Min();
|
|
|
|
private static double BoxGap(Box a, Box b)
|
|
{
|
|
var x = System.Math.Max(0, System.Math.Max(b.Left - a.Right, a.Left - b.Right));
|
|
var y = System.Math.Max(0, System.Math.Max(b.Bottom - a.Top, a.Bottom - b.Top));
|
|
return System.Math.Sqrt(x * x + y * y);
|
|
}
|
|
|
|
[Fact]
|
|
public void Expand_PinnedAlongY_OpensX()
|
|
{
|
|
var (plate, parts) = TwoBarGroups(widthY: 20);
|
|
var ys = parts.Select(p => p.Location.Y).ToList();
|
|
|
|
var result = Expander.Expand(parts, plate);
|
|
|
|
Assert.Null(result.SpacingY);
|
|
Assert.NotNull(result.SpacingX);
|
|
Assert.True(result.SpacingX > 0.5, $"X={result.SpacingX:R}");
|
|
Assert.Empty(result.Violations);
|
|
for (var i = 0; i < parts.Count; i++)
|
|
Assert.Equal(ys[i], parts[i].Location.Y, 9);
|
|
|
|
// Every gap along the chain opened to the X spacing.
|
|
var firstRow = parts.Take(2).Concat(parts.Skip(16)).Select(p => p.BoundingBox).ToList();
|
|
for (var i = 1; i < firstRow.Count; i++)
|
|
Assert.True(firstRow[i].Left - firstRow[i - 1].Right >= result.SpacingX - 1e-6);
|
|
var work = plate.WorkArea();
|
|
Assert.True(parts.Max(p => p.BoundingBox.Right) <= work.Right + 1e-9);
|
|
Assert.True(SmallestGap(parts) >= Gap - 1e-6);
|
|
}
|
|
|
|
[Fact]
|
|
public void Expand_RoomOnBothAxes_OpensBoth()
|
|
{
|
|
var (plate, parts) = TwoBarGroups(widthY: 21);
|
|
|
|
var result = Expander.Expand(parts, plate);
|
|
|
|
Assert.True(result.AchievedSpacing > Gap + 0.05, $"achieved={result.AchievedSpacing:R}");
|
|
Assert.Empty(result.Violations);
|
|
Assert.True(SmallestGap(parts) >= result.AchievedSpacing - 1e-6);
|
|
var work = plate.WorkArea();
|
|
Assert.All(parts, p =>
|
|
{
|
|
Assert.True(p.BoundingBox.Right <= work.Right + 1e-9);
|
|
Assert.True(p.BoundingBox.Top <= work.Top + 1e-9);
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void Expand_NoRoomOnEitherAxis_MovesNothing()
|
|
{
|
|
var (plate, parts) = TwoBarGroups(widthY: 20);
|
|
foreach (var part in parts.Skip(16))
|
|
part.Offset(-0.75, 0); // close the wide gap before the uprights
|
|
plate.Size = new Size(20, 27.5); // and end the last one on the work-area edge
|
|
var before = parts.Select(p => p.Location).ToList();
|
|
|
|
var result = Expander.Expand(parts, plate);
|
|
|
|
Assert.Null(result.SpacingX);
|
|
Assert.Null(result.SpacingY);
|
|
for (var i = 0; i < parts.Count; i++)
|
|
Assert.Equal(0, parts[i].Location.DistanceTo(before[i]), 6);
|
|
}
|
|
}
|