mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-23 03:17:40 -05:00
A Vertical or Horizontal Remnant fill of a quantity of round parts left a deep remnant on the table: twelve 8 in. flanges took 25.6 in. of a 60 in. sheet's length and stopped 16 in. short of its far edge, where two full-height columns need 15.1 in. Three causes: - The low-quantity shrink scaled both sides, so stripes could not span the side the remnant runs along. It now narrows only the trimmed axis. - The ranking compared untrimmed counts, so the full-sheet layout always beat the full-height columns and was then trimmed. It now ranks the layout as it will be kept. - The trim kept overlapping bounding boxes as interlocked pairs, which staggered discs always are. Discs are now trimmed one by one. The circle strategy also offers each stagger mode's stripes in both directions to a remnant ranking. Every change is limited to remnant fills of round parts: applied to other shapes, the trimmed ranking let a tilted bar layout block the rest of a nest, and the disc trim moved the Irregular engine's sample layout, so every other fill is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
124 lines
4.5 KiB
C#
124 lines
4.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using OpenNest.Engine;
|
|
using OpenNest.Engine.Fill;
|
|
using OpenNest.Math;
|
|
|
|
namespace OpenNest.Engine.Jobs.Placement.Fillers;
|
|
|
|
/// <summary>
|
|
/// Filler-only settings that distinguish Fill from the remnant fills: how candidates are ranked,
|
|
/// the preferred direction, the axis surplus parts are trimmed from, and how angles are chosen.
|
|
/// Strategies see only the <see cref="Strategies.FillPolicy"/> the filler builds from it.
|
|
/// </summary>
|
|
internal sealed class PlateFillPolicy
|
|
{
|
|
private readonly Func<int, bool, IFillComparer> comparerFactory;
|
|
private readonly Func<NestItem, double, double> angleExtent;
|
|
|
|
private PlateFillPolicy(
|
|
Func<int, bool, IFillComparer> comparerFactory,
|
|
NestDirection? preferredDirection,
|
|
ShrinkAxis trimAxis,
|
|
Func<NestItem, double, double> angleExtent,
|
|
bool keepsRemnant = false
|
|
)
|
|
{
|
|
this.comparerFactory = comparerFactory;
|
|
PreferredDirection = preferredDirection;
|
|
TrimAxis = trimAxis;
|
|
this.angleExtent = angleExtent;
|
|
KeepsRemnant = keepsRemnant;
|
|
}
|
|
|
|
/// <summary>Plain fill: default ranking and the filler's learned angle candidates.</summary>
|
|
internal static PlateFillPolicy Fill { get; } = new(
|
|
(_, _) => new DefaultFillComparer(),
|
|
null,
|
|
ShrinkAxis.Width,
|
|
null
|
|
);
|
|
|
|
/// <summary>Vertical remnant: minimize X-extent, prefer horizontal placement.</summary>
|
|
internal static PlateFillPolicy VerticalRemnant { get; } = new(
|
|
(quantity, keepsPairs) => new VerticalRemnantComparer
|
|
{
|
|
Quantity = quantity,
|
|
KeepsPairs = keepsPairs,
|
|
},
|
|
NestDirection.Horizontal,
|
|
ShrinkAxis.Width,
|
|
RotatedWidth,
|
|
keepsRemnant: true
|
|
);
|
|
|
|
/// <summary>Horizontal remnant: minimize Y-extent, prefer vertical placement.</summary>
|
|
internal static PlateFillPolicy HorizontalRemnant { get; } = new(
|
|
(quantity, keepsPairs) => new HorizontalRemnantComparer
|
|
{
|
|
Quantity = quantity,
|
|
KeepsPairs = keepsPairs,
|
|
},
|
|
NestDirection.Vertical,
|
|
ShrinkAxis.Length,
|
|
RotatedHeight,
|
|
keepsRemnant: true
|
|
);
|
|
|
|
internal NestDirection? PreferredDirection { get; }
|
|
|
|
internal ShrinkAxis TrimAxis { get; }
|
|
|
|
/// <summary>
|
|
/// True for the remnant fills: a quantity of round parts is laid out across the whole side
|
|
/// the remnant runs along, so their low-quantity fill only narrows the work area along
|
|
/// <see cref="TrimAxis"/>.
|
|
/// </summary>
|
|
internal bool KeepsRemnant { get; }
|
|
|
|
/// <summary>
|
|
/// True when the filler's <see cref="AngleCandidateBuilder"/> chooses the angles; false when
|
|
/// <see cref="BuildFixedAngles"/> replaces it entirely.
|
|
/// </summary>
|
|
internal bool UsesAngleBuilder => angleExtent == null;
|
|
|
|
/// <summary>
|
|
/// The ranking for a fill; a remnant fill given a positive <paramref name="quantity"/> ranks
|
|
/// layouts as they will be trimmed to it, keeping interlocked pairs whole when
|
|
/// <paramref name="keepsPairs"/>.
|
|
/// </summary>
|
|
internal IFillComparer CreateComparer(int quantity = 0, bool keepsPairs = true) =>
|
|
comparerFactory(System.Math.Max(0, quantity), keepsPairs);
|
|
|
|
/// <summary>
|
|
/// The remnant angles: the primary angle and its quarter turn, narrowest extent first. No
|
|
/// circle case, sweep or learned angles, and the item's rotation range is ignored.
|
|
/// </summary>
|
|
internal List<double> BuildFixedAngles(NestItem item, ClassificationResult classification)
|
|
{
|
|
var baseAngles = new List<double>
|
|
{
|
|
classification.PrimaryAngle,
|
|
classification.PrimaryAngle + Angle.HalfPI,
|
|
};
|
|
baseAngles.Sort((left, right) => angleExtent(item, left).CompareTo(angleExtent(item, right)));
|
|
return baseAngles;
|
|
}
|
|
|
|
private static double RotatedWidth(NestItem item, double angle)
|
|
{
|
|
var boundingBox = item.Drawing.Program.BoundingBox();
|
|
var cos = System.Math.Abs(PortableMath.Cos(angle));
|
|
var sin = System.Math.Abs(PortableMath.Sin(angle));
|
|
return boundingBox.Length * cos + boundingBox.Width * sin;
|
|
}
|
|
|
|
private static double RotatedHeight(NestItem item, double angle)
|
|
{
|
|
var boundingBox = item.Drawing.Program.BoundingBox();
|
|
var cos = System.Math.Abs(PortableMath.Cos(angle));
|
|
var sin = System.Math.Abs(PortableMath.Sin(angle));
|
|
return boundingBox.Width * cos + boundingBox.Length * sin;
|
|
}
|
|
}
|