mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-12-01 15:41:35 -05:00
Expand Spacing kept the first selected part fixed and stalled on full plates. - A pair of selected parts now shares each push (later part half, earlier part the rest, later part any remainder), so no selected part is an anchor. Both shares run to the full target, so a part between two short gaps no longer ping-pongs while its partners never move. - Clamps act like the work-area edge: a part may close on the keep-out but never enter it, and the clamp gap neither chases the target spacing nor counts as the achieved spacing. Before, clamps held their full entry gap and a part resting just above one capped the whole search. - A part flush on an edge can slide along it again. A push that leaned into the edge by rounding (parts turned a quarter or half) divided zero room by a tiny component and pinned every move. - Faster search with identical layouts: per-part pair lists, clearances remembered at the positions measured, box-proven clamp pairs skipped, trial-move gaps measured only up to their threshold, the overlap clipping run only for outlines that touch, cross or nest, and ring distances measured at offsets without copying outlines. Clearance gains the bounded and offset distance used for this. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
97 lines
3.7 KiB
C#
97 lines
3.7 KiB
C#
using System.Collections.Generic;
|
|
using OpenNest.Clamps;
|
|
using OpenNest.CNC;
|
|
using OpenNest.Engine.Fill;
|
|
using OpenNest.Engine.Tests.Jobs;
|
|
using OpenNest.Geometry;
|
|
using Xunit;
|
|
|
|
namespace OpenNest.Engine.Tests.Fill;
|
|
|
|
public class ExpanderQualityReviewTests
|
|
{
|
|
private static Part AddSquare(Plate plate, double x, double y, double size)
|
|
{
|
|
var part = new Part(
|
|
new Drawing("neutral", TestDrawingFactory.Rectangle(size, size)), new Vector(x, y));
|
|
plate.Parts.Add(part);
|
|
return part;
|
|
}
|
|
|
|
private static Plate MakePlate(double length) =>
|
|
new(new Size(12, length)) { EdgeSpacing = new Spacing(0, 0) };
|
|
|
|
[Fact]
|
|
public void TinyZoneContainedInMover_MustNotReturnClearPositiveSpacing()
|
|
{
|
|
var plate = MakePlate(30);
|
|
plate.ClampZones.Add(new ClampZone("tiny", new Box(5, 5, 1e-5, 1e-5)));
|
|
var anchor = AddSquare(plate, 1, 4, 2);
|
|
var mover = AddSquare(plate, 4, 4, 2);
|
|
// Independent oracle: [5,5.00001]^2 lies strictly inside [4,6]^2.
|
|
var result = Expander.Expand(
|
|
new List<Part> { anchor, mover },
|
|
plate,
|
|
new Expander.Options { InitialStep = 3, MaxSpacing = 3 }
|
|
);
|
|
Assert.True(
|
|
result.Violations.Count > 0,
|
|
$"Physical clamp is still inside mover but report is clear: spacing={result.AchievedSpacing:R}, violations={result.Violations.Count}"
|
|
);
|
|
Assert.True(result.Violations[0].Achieved < 0);
|
|
}
|
|
|
|
[Fact]
|
|
public void TinyZoneInsideCutoutIsNotReportedAsMaterialOverlap()
|
|
{
|
|
var plate = MakePlate(30);
|
|
plate.ClampZones.Add(new ClampZone("tiny", new Box(5, 5, 1e-5, 1e-5)));
|
|
var frameProgram = TestDrawingFactory.Rectangle(10, 10);
|
|
frameProgram.MoveTo(4, 4);
|
|
frameProgram.LineTo(6, 4);
|
|
frameProgram.LineTo(6, 6);
|
|
frameProgram.LineTo(4, 6);
|
|
frameProgram.LineTo(4, 4);
|
|
var frame = new Part(new Drawing("neutral-frame", frameProgram), Vector.Zero);
|
|
plate.Parts.Add(frame);
|
|
|
|
var result = Expander.Separate(new List<Part> { frame }, plate, spacing: 0.5);
|
|
Assert.True(result.Converged, $"zone inside cutout reported as material: {result.Violations.Count}");
|
|
Assert.Empty(result.Violations);
|
|
Assert.Equal(0, frame.Location.X);
|
|
}
|
|
|
|
[Fact]
|
|
public void EdgeLimitedConvexOverlap_ShouldRetainCertifiedPartialImprovement()
|
|
{
|
|
// The anchor sits on the low edge, so only the mover can open the overlap.
|
|
var plate = MakePlate(6.5);
|
|
var anchor = AddSquare(plate, 0, 4, 4);
|
|
var mover = AddSquare(plate, 2, 4, 4);
|
|
var result = Expander.Expand(
|
|
new List<Part> { anchor, mover }, plate, new Expander.Options { MaxSpacing = 1 }
|
|
);
|
|
// Along x in [2,2.5], penetration is 4-x and strictly decreases.
|
|
Assert.True(
|
|
mover.Location.X >= 2.49,
|
|
$"Lost safe partial-overlap improvement: x={mover.Location.X:R}; expected edge at 2.5"
|
|
);
|
|
Assert.NotEmpty(result.Violations);
|
|
Assert.Equal(0, anchor.Location.X);
|
|
Assert.True(mover.Location.X + 4 <= 6.5 + 1e-9);
|
|
}
|
|
|
|
[Fact]
|
|
public void NegativeTarget_ConvexOverlapCanReachLessNegativeClearance()
|
|
{
|
|
var plate = MakePlate(30);
|
|
var anchor = AddSquare(plate, 5, 4, 4);
|
|
var mover = AddSquare(plate, 7, 4, 4);
|
|
var result = Expander.Separate(new List<Part> { anchor, mover }, plate, -1);
|
|
var opened = result.Positions[1].X - result.Positions[0].X;
|
|
Assert.True(result.Converged, $"Signed target -1 was not reached; opened={opened:R}");
|
|
Assert.True(opened >= 3 - 1e-6, $"opened={opened:R}");
|
|
Assert.Equal(7, mover.Location.X);
|
|
}
|
|
}
|