feat(expand-spacing): keep rows that share a pitch but stack unevenly

A single-drawing selection that fits no one lattice, such as staggered rows
topped by straight ones, now counts as a pattern when its rows share one
pitch along X (or Y). Each row stretches rigidly instead of the selection
falling back to the slow pair search.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-11-04 21:44:12 -05:00
1 parent 7f718f222c
commit 8700ae187a
3 files changed
+112 -3

No files matched your search

@@ -103,6 +103,48 @@ public class ExpanderPatternTests
Assert.Equal(rowMateGap, result.AchievedSpacing, 2);
}
[Fact]
public void Expand_StaggeredRowsToppedByStraightRows_SpreadsEveryRow()
{
// Three staggered rows, then two straight rows stacked on the wide
// rows' columns: no single lattice, but rows sharing one pitch.
const double gap = 0.2;
const double edge = 0.2;
const int columns = 9;
var pitch = 2 * Radius + gap;
var dx = pitch / 2;
var dy = System.Math.Sqrt(pitch * pitch - dx * dx);
var plate = MakePlate(2 * edge + 2 * Radius + (columns - 1) * dx, 40, edge);
var drawing = new Drawing("disk", Disk(Radius));
var parts = new List<Part>();
var rowYs = new List<double>();
var y = edge + Radius;
for (var row = 0; row < 5; row++)
{
var odd = row == 1;
rowYs.Add(y);
for (var column = odd ? 1 : 0; column < columns; column += 2)
parts.Add(Add(plate, drawing, edge + Radius + column * dx, y));
y += row < 2 ? dy : pitch;
}
var xs = parts.Select(p => p.Location.X).ToList();
var entryGap = parts.SelectMany((a, i) => parts.Skip(i + 1).Select(b => DiskGap(a, b))).Min();
var result = Expander.Expand(parts, plate);
Assert.True(result.KeptPattern);
Assert.True(result.PatternMoved);
for (var i = 0; i < parts.Count; i++)
Assert.Equal(xs[i], parts[i].Location.X, 6);
var spreadYs = parts.Select(p => p.Location.Y).Distinct(new Near()).OrderBy(v => v).ToList();
Assert.Equal(5, spreadYs.Count);
for (var row = 1; row < 5; row++)
Assert.True(spreadYs[row] - spreadYs[row - 1] > rowYs[row] - rowYs[row - 1] + 0.1);
var work = plate.WorkArea();
Assert.True(work.Top - parts.Max(p => p.BoundingBox.Top) < 0.02);
Assert.True(parts.SelectMany((a, i) => parts.Skip(i + 1).Select(b => DiskGap(a, b))).Min() >= entryGap - 1e-6);
}
[Fact]
public void Expand_GridWithRoomBothWays_FillsTheWorkAreaEvenly()
{
+69 -2
View File
@@ -18,9 +18,17 @@ namespace OpenNest.Engine.Fill
{
public Vector A;
/// <summary>Second translation; <see cref="Vector.Zero"/> for a single row.</summary>
/// <summary>Second translation; <see cref="Vector.Zero"/> for a single row
/// or for <see cref="Rows"/>.</summary>
public Vector B;
/// <summary>
/// True for rows that share the pitch <see cref="A"/> but stack at
/// their own spacing, such as staggered rows topped by straight ones.
/// Each part is its own lattice point.
/// </summary>
public bool Rows;
/// <summary>
/// Per selected part, the lattice point of its motif copy. Parts of
/// one copy share it, so stretching the lattice keeps every motif rigid.
@@ -39,6 +47,8 @@ namespace OpenNest.Engine.Fill
/// A pattern must over-determine its lattice: a row needs three parts
/// of one motif group and a grid four, since any two or three points
/// trivially fit some lattice. Every group needs at least two parts.
/// A single motif group that fits no one lattice may still form
/// <see cref="Rows"/>.
/// </summary>
public static RepeatingPattern Detect(IReadOnlyList<Part> parts)
{
@@ -50,6 +60,13 @@ namespace OpenNest.Engine.Fill
if (groups.Any(g => g.Count < 2))
return null;
return DetectLattice(groups, centers)
?? DetectRows(parts, groups, centers, horizontal: true)
?? DetectRows(parts, groups, centers, horizontal: false);
}
private static RepeatingPattern DetectLattice(List<List<int>> groups, Vector[] centers)
{
if (!TryBasis(groups, centers, out var a, out var b))
return null;
@@ -61,7 +78,7 @@ namespace OpenNest.Engine.Fill
// its part nearest that group's origin: the reduced motif offset,
// so an interlocked partner moves with its own copy.
var origin = centers[groups[0][0]];
var points = new Vector[parts.Count];
var points = new Vector[centers.Length];
foreach (var group in groups)
{
var groupOrigin = centers[group.OrderBy(i => centers[i].DistanceTo(origin)).First()];
@@ -76,6 +93,56 @@ namespace OpenNest.Engine.Fill
return new RepeatingPattern { A = a, B = b, LatticePoints = points };
}
/// <summary>
/// Rows of one motif along X (or Y): at least two rows, each of at
/// least two parts and one of at least three, whose steps along the
/// row are all whole multiples of one shared pitch. Rows may stack at
/// any spacing and offset, so stretching moves each row rigidly.
/// </summary>
private static RepeatingPattern DetectRows(
IReadOnlyList<Part> parts, List<List<int>> groups, Vector[] centers, bool horizontal)
{
if (groups.Count != 1)
return null;
double Along(Vector v) => horizontal ? v.X : v.Y;
double Across(Vector v) => horizontal ? v.Y : v.X;
// Parts of one row share their cross coordinate to within a
// sliver of the part's own size.
var box = parts[0].BoundingBox;
var rowTolerance = CoordinateTolerance * System.Math.Min(box.Length, box.Width);
var rows = new List<List<Vector>>();
foreach (var center in centers.OrderBy(Across))
{
if (rows.Count == 0 || Across(center) - Across(rows[^1][0]) > rowTolerance)
rows.Add(new List<Vector>());
rows[^1].Add(center);
}
if (rows.Count < 2 || rows.Any(r => r.Count < 2) || rows.Max(r => r.Count) < 3)
return null;
var steps = rows
.SelectMany(r =>
{
var along = r.Select(Along).OrderBy(x => x).ToList();
return along.Skip(1).Select((x, i) => x - along[i]);
})
.ToList();
var pitch = steps.Min();
if (pitch <= rowTolerance)
return null;
if (steps.Any(step => System.Math.Abs(step / pitch - System.Math.Round(step / pitch)) > CoordinateTolerance))
return null;
return new RepeatingPattern
{
A = horizontal ? new Vector(pitch, 0) : new Vector(0, pitch),
B = Vector.Zero,
Rows = true,
LatticePoints = centers.ToArray(),
};
}
private static List<List<int>> Group(IReadOnlyList<Part> parts)
{
var groups = new List<List<int>>();
+1 -1
View File
@@ -4,7 +4,7 @@
## Repeating patterns
A selection that repeats on a lattice, such as a fill, is stretched as one pattern instead of going through the pair search below. Parts of one drawing at one rotation form a motif group, and every group must sit on the same two lattice translations (one for a single row) to within 2% of a pitch. Each group needs at least two parts, and one group needs at least three for a row or four for a grid, because any two or three points fit some lattice. Cut-offs, overlapping selections and selections that start outside the work area always use the pair search.
A selection that repeats on a lattice, such as a fill, is stretched as one pattern instead of going through the pair search below. Parts of one drawing at one rotation form a motif group, and every group must sit on the same two lattice translations (one for a single row) to within 2% of a pitch. Each group needs at least two parts, and one group needs at least three for a row or four for a grid, because any two or three points fit some lattice. A single-drawing selection that fits no one lattice also counts when it forms rows along X (or Y): at least two rows of at least two parts, one of at least three, whose steps along the row are whole multiples of one shared pitch. Such rows may stack at any spacing and offset, as when staggered rows are topped by straight ones, and each part then stretches on its own. Cut-offs, overlapping selections and selections that start outside the work area always use the pair search.
The lattice is scaled along X, then along Y, about its center, and each part moves by the displacement of its motif copy's lattice point, so interlocked pairs and other multi-part motifs stay rigid. Along each axis the largest scale is used whose stretched selection fits the work area and keeps every floor: no selected gap closes below the smallest selected gap at entry, a gap to a nonselected part closes no further than that, and cut-offs and clamps keep their entry gaps. A part may not pass through a nonselected part, cut-off or clamp on its straight way from the entry layout. The stretched selection is tried centered on where it was, then held at its low edge, then at its high edge. An axis with no room keeps its pitch, so a fill whose rows run edge to edge along X only opens its rows along Y: the row gap stays and the diagonal gap grows. The status bar reads "Spread the repeating pattern" with the smallest gap, or says the pattern has no room to spread.