feat: add VerticalRemnantEngine and HorizontalRemnantEngine
Two new engine classes subclassing DefaultNestEngine that override CreateComparer, PreferredDirection, and BuildAngles to optimize for preserving side remnants. Both registered in NestEngineRegistry and covered by 6 integration tests. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
42
OpenNest.Engine/HorizontalRemnantEngine.cs
Normal file
42
OpenNest.Engine/HorizontalRemnantEngine.cs
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using OpenNest.Engine;
|
||||||
|
using OpenNest.Engine.Fill;
|
||||||
|
using OpenNest.Geometry;
|
||||||
|
using OpenNest.Math;
|
||||||
|
|
||||||
|
namespace OpenNest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Optimizes for the largest top-side horizontal drop.
|
||||||
|
/// Scores by count first, then minimizes Y-extent.
|
||||||
|
/// Prefers vertical nest direction and angles that keep parts narrow in Y.
|
||||||
|
/// </summary>
|
||||||
|
public class HorizontalRemnantEngine : DefaultNestEngine
|
||||||
|
{
|
||||||
|
public HorizontalRemnantEngine(Plate plate) : base(plate) { }
|
||||||
|
|
||||||
|
public override string Name => "Horizontal Remnant";
|
||||||
|
|
||||||
|
public override string Description => "Optimizes for largest top-side horizontal drop";
|
||||||
|
|
||||||
|
protected override IFillComparer CreateComparer() => new HorizontalRemnantComparer();
|
||||||
|
|
||||||
|
public override NestDirection? PreferredDirection => NestDirection.Vertical;
|
||||||
|
|
||||||
|
public override List<double> BuildAngles(NestItem item, double bestRotation, Box workArea)
|
||||||
|
{
|
||||||
|
var baseAngles = new List<double> { bestRotation, bestRotation + Angle.HalfPI };
|
||||||
|
baseAngles.Sort((a, b) => RotatedHeight(item, a).CompareTo(RotatedHeight(item, b)));
|
||||||
|
return baseAngles;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static double RotatedHeight(NestItem item, double angle)
|
||||||
|
{
|
||||||
|
var bb = item.Drawing.Program.BoundingBox();
|
||||||
|
var cos = System.Math.Abs(System.Math.Cos(angle));
|
||||||
|
var sin = System.Math.Abs(System.Math.Sin(angle));
|
||||||
|
return bb.Length * cos + bb.Width * sin;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,6 +24,14 @@ namespace OpenNest
|
|||||||
Register("NFP",
|
Register("NFP",
|
||||||
"NFP-based mixed-part nesting with simulated annealing",
|
"NFP-based mixed-part nesting with simulated annealing",
|
||||||
plate => new NfpNestEngine(plate));
|
plate => new NfpNestEngine(plate));
|
||||||
|
|
||||||
|
Register("Vertical Remnant",
|
||||||
|
"Optimizes for largest right-side vertical drop",
|
||||||
|
plate => new VerticalRemnantEngine(plate));
|
||||||
|
|
||||||
|
Register("Horizontal Remnant",
|
||||||
|
"Optimizes for largest top-side horizontal drop",
|
||||||
|
plate => new HorizontalRemnantEngine(plate));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IReadOnlyList<NestEngineInfo> AvailableEngines => engines;
|
public static IReadOnlyList<NestEngineInfo> AvailableEngines => engines;
|
||||||
|
|||||||
42
OpenNest.Engine/VerticalRemnantEngine.cs
Normal file
42
OpenNest.Engine/VerticalRemnantEngine.cs
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using OpenNest.Engine;
|
||||||
|
using OpenNest.Engine.Fill;
|
||||||
|
using OpenNest.Geometry;
|
||||||
|
using OpenNest.Math;
|
||||||
|
|
||||||
|
namespace OpenNest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Optimizes for the largest right-side vertical drop.
|
||||||
|
/// Scores by count first, then minimizes X-extent.
|
||||||
|
/// Prefers horizontal nest direction and angles that keep parts narrow in X.
|
||||||
|
/// </summary>
|
||||||
|
public class VerticalRemnantEngine : DefaultNestEngine
|
||||||
|
{
|
||||||
|
public VerticalRemnantEngine(Plate plate) : base(plate) { }
|
||||||
|
|
||||||
|
public override string Name => "Vertical Remnant";
|
||||||
|
|
||||||
|
public override string Description => "Optimizes for largest right-side vertical drop";
|
||||||
|
|
||||||
|
protected override IFillComparer CreateComparer() => new VerticalRemnantComparer();
|
||||||
|
|
||||||
|
public override NestDirection? PreferredDirection => NestDirection.Horizontal;
|
||||||
|
|
||||||
|
public override List<double> BuildAngles(NestItem item, double bestRotation, Box workArea)
|
||||||
|
{
|
||||||
|
var baseAngles = new List<double> { bestRotation, bestRotation + Angle.HalfPI };
|
||||||
|
baseAngles.Sort((a, b) => RotatedWidth(item, a).CompareTo(RotatedWidth(item, b)));
|
||||||
|
return baseAngles;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static double RotatedWidth(NestItem item, double angle)
|
||||||
|
{
|
||||||
|
var bb = item.Drawing.Program.BoundingBox();
|
||||||
|
var cos = System.Math.Abs(System.Math.Cos(angle));
|
||||||
|
var sin = System.Math.Abs(System.Math.Sin(angle));
|
||||||
|
return bb.Width * cos + bb.Length * sin;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
92
OpenNest.Tests/RemnantEngineTests.cs
Normal file
92
OpenNest.Tests/RemnantEngineTests.cs
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
using OpenNest.Engine;
|
||||||
|
using OpenNest.Engine.Fill;
|
||||||
|
using OpenNest.Geometry;
|
||||||
|
|
||||||
|
namespace OpenNest.Tests;
|
||||||
|
|
||||||
|
public class RemnantEngineTests
|
||||||
|
{
|
||||||
|
private static Drawing MakeRectDrawing(double w, double h, string name = "rect")
|
||||||
|
{
|
||||||
|
var pgm = new OpenNest.CNC.Program();
|
||||||
|
pgm.Codes.Add(new OpenNest.CNC.RapidMove(new Vector(0, 0)));
|
||||||
|
pgm.Codes.Add(new OpenNest.CNC.LinearMove(new Vector(w, 0)));
|
||||||
|
pgm.Codes.Add(new OpenNest.CNC.LinearMove(new Vector(w, h)));
|
||||||
|
pgm.Codes.Add(new OpenNest.CNC.LinearMove(new Vector(0, h)));
|
||||||
|
pgm.Codes.Add(new OpenNest.CNC.LinearMove(new Vector(0, 0)));
|
||||||
|
return new Drawing(name, pgm);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void VerticalRemnantEngine_UsesVerticalRemnantComparer()
|
||||||
|
{
|
||||||
|
var plate = new Plate(60, 120);
|
||||||
|
var engine = new VerticalRemnantEngine(plate);
|
||||||
|
Assert.Equal("Vertical Remnant", engine.Name);
|
||||||
|
Assert.Equal(NestDirection.Horizontal, engine.PreferredDirection);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void HorizontalRemnantEngine_UsesHorizontalRemnantComparer()
|
||||||
|
{
|
||||||
|
var plate = new Plate(60, 120);
|
||||||
|
var engine = new HorizontalRemnantEngine(plate);
|
||||||
|
Assert.Equal("Horizontal Remnant", engine.Name);
|
||||||
|
Assert.Equal(NestDirection.Vertical, engine.PreferredDirection);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void VerticalRemnantEngine_Fill_ProducesResults()
|
||||||
|
{
|
||||||
|
var plate = new Plate(60, 120);
|
||||||
|
var engine = new VerticalRemnantEngine(plate);
|
||||||
|
var item = new NestItem { Drawing = MakeRectDrawing(20, 10) };
|
||||||
|
|
||||||
|
var parts = engine.Fill(item, plate.WorkArea(), null, System.Threading.CancellationToken.None);
|
||||||
|
|
||||||
|
Assert.True(parts.Count > 0, "VerticalRemnantEngine should fill parts");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void HorizontalRemnantEngine_Fill_ProducesResults()
|
||||||
|
{
|
||||||
|
var plate = new Plate(60, 120);
|
||||||
|
var engine = new HorizontalRemnantEngine(plate);
|
||||||
|
var item = new NestItem { Drawing = MakeRectDrawing(20, 10) };
|
||||||
|
|
||||||
|
var parts = engine.Fill(item, plate.WorkArea(), null, System.Threading.CancellationToken.None);
|
||||||
|
|
||||||
|
Assert.True(parts.Count > 0, "HorizontalRemnantEngine should fill parts");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Registry_ContainsBothRemnantEngines()
|
||||||
|
{
|
||||||
|
var names = NestEngineRegistry.AvailableEngines.Select(e => e.Name).ToList();
|
||||||
|
Assert.Contains("Vertical Remnant", names);
|
||||||
|
Assert.Contains("Horizontal Remnant", names);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void VerticalRemnantEngine_ProducesTighterXExtent_ThanDefault()
|
||||||
|
{
|
||||||
|
var plate = new Plate(60, 120);
|
||||||
|
var drawing = MakeRectDrawing(20, 10);
|
||||||
|
var item = new NestItem { Drawing = drawing };
|
||||||
|
|
||||||
|
var defaultEngine = new DefaultNestEngine(plate);
|
||||||
|
var remnantEngine = new VerticalRemnantEngine(plate);
|
||||||
|
|
||||||
|
var defaultParts = defaultEngine.Fill(item, plate.WorkArea(), null, System.Threading.CancellationToken.None);
|
||||||
|
var remnantParts = remnantEngine.Fill(item, plate.WorkArea(), null, System.Threading.CancellationToken.None);
|
||||||
|
|
||||||
|
Assert.True(defaultParts.Count > 0);
|
||||||
|
Assert.True(remnantParts.Count > 0);
|
||||||
|
|
||||||
|
var defaultXExtent = defaultParts.Max(p => p.BoundingBox.Right) - defaultParts.Min(p => p.BoundingBox.Left);
|
||||||
|
var remnantXExtent = remnantParts.Max(p => p.BoundingBox.Right) - remnantParts.Min(p => p.BoundingBox.Left);
|
||||||
|
|
||||||
|
Assert.True(remnantXExtent <= defaultXExtent + 0.01,
|
||||||
|
$"Remnant X-extent ({remnantXExtent:F1}) should be <= default ({defaultXExtent:F1})");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user