Replace RotationAnalysis.FindBestRotation with PartClassifier.Classify in RunPipeline, propagate ClassificationResult through BuildAngles signatures and FillContext.PartType, and rewrite AngleCandidateBuilder to dispatch on part type (Circle=1 angle, Rectangle=2, Irregular=full sweep). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
114 lines
4.0 KiB
C#
114 lines
4.0 KiB
C#
using OpenNest.Engine;
|
|
using OpenNest.Engine.Fill;
|
|
using OpenNest.Geometry;
|
|
|
|
namespace OpenNest.Tests;
|
|
|
|
public class AngleCandidateBuilderTests
|
|
{
|
|
private static Drawing MakeRectDrawing(double w, double h)
|
|
{
|
|
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("rect", pgm);
|
|
}
|
|
|
|
private static ClassificationResult MakeClassification(double primaryAngle = 0, PartType type = PartType.Irregular)
|
|
=> new ClassificationResult { PrimaryAngle = primaryAngle, Type = type };
|
|
|
|
[Fact]
|
|
public void Build_ReturnsAtLeastTwoAngles()
|
|
{
|
|
var builder = new AngleCandidateBuilder();
|
|
var item = new NestItem { Drawing = MakeRectDrawing(20, 10) };
|
|
var workArea = new Box(0, 0, 100, 100);
|
|
|
|
var angles = builder.Build(item, MakeClassification(), workArea);
|
|
|
|
Assert.True(angles.Count >= 2);
|
|
}
|
|
|
|
[Fact]
|
|
public void Build_RectangleType_NarrowWorkArea_UsesBaseAnglesOnly()
|
|
{
|
|
var builder = new AngleCandidateBuilder();
|
|
var item = new NestItem { Drawing = MakeRectDrawing(20, 10) };
|
|
var narrowArea = new Box(0, 0, 100, 8); // narrower than part's longest side
|
|
|
|
var angles = builder.Build(item, MakeClassification(0, PartType.Rectangle), narrowArea);
|
|
|
|
// Rectangle classification always returns exactly 2 angles regardless of work area
|
|
Assert.Equal(2, angles.Count);
|
|
}
|
|
|
|
[Fact]
|
|
public void ForceFullSweep_ProducesFullSweep()
|
|
{
|
|
var builder = new AngleCandidateBuilder { ForceFullSweep = true };
|
|
var item = new NestItem { Drawing = MakeRectDrawing(5, 5) };
|
|
var workArea = new Box(0, 0, 100, 100);
|
|
|
|
var angles = builder.Build(item, MakeClassification(), workArea);
|
|
|
|
// Full sweep at 5deg steps = ~36 angles (0 to 175), plus base angles
|
|
Assert.True(angles.Count > 10);
|
|
}
|
|
|
|
[Fact]
|
|
public void RecordProductive_PrunesSubsequentBuilds()
|
|
{
|
|
var builder = new AngleCandidateBuilder { ForceFullSweep = true };
|
|
var item = new NestItem { Drawing = MakeRectDrawing(20, 10) };
|
|
var workArea = new Box(0, 0, 100, 8);
|
|
|
|
// First build — full sweep
|
|
var firstAngles = builder.Build(item, MakeClassification(), workArea);
|
|
|
|
// Record some as productive
|
|
var productive = new List<AngleResult>
|
|
{
|
|
new AngleResult { AngleDeg = 0, PartCount = 5 },
|
|
new AngleResult { AngleDeg = 45, PartCount = 3 },
|
|
};
|
|
builder.RecordProductive(productive);
|
|
|
|
// Second build — should be pruned to known-good + base angles
|
|
builder.ForceFullSweep = false;
|
|
var secondAngles = builder.Build(item, MakeClassification(), workArea);
|
|
|
|
Assert.True(secondAngles.Count < firstAngles.Count,
|
|
$"Pruned ({secondAngles.Count}) should be fewer than full ({firstAngles.Count})");
|
|
}
|
|
|
|
[Fact]
|
|
public void Build_RectanglePart_ReturnsTwoAngles()
|
|
{
|
|
var builder = new AngleCandidateBuilder();
|
|
var item = new NestItem { Drawing = MakeRectDrawing(20, 10) };
|
|
var workArea = new Box(0, 0, 100, 100);
|
|
var classification = MakeClassification(0, PartType.Rectangle);
|
|
|
|
var angles = builder.Build(item, classification, workArea);
|
|
|
|
Assert.Equal(2, angles.Count);
|
|
}
|
|
|
|
[Fact]
|
|
public void Build_CirclePart_ReturnsOneAngle()
|
|
{
|
|
var builder = new AngleCandidateBuilder();
|
|
var item = new NestItem { Drawing = MakeRectDrawing(10, 10) };
|
|
var workArea = new Box(0, 0, 100, 100);
|
|
var classification = MakeClassification(0, PartType.Circle);
|
|
|
|
var angles = builder.Build(item, classification, workArea);
|
|
|
|
Assert.Single(angles);
|
|
Assert.Equal(0, angles[0]);
|
|
}
|
|
}
|