#nullable enable
using System;
using System.Collections.Generic;
namespace OpenNest.Engine.Jobs.Cutouts;
/// What cutout filling maximizes: the job's material seated in cutouts, read as insert
/// outline area, since every square inch in a hole is one the sheet no longer has to give.
/// Copy count plays no part, so one large part beats a handful of tiny ones of less area.
internal static class CutoutSeatedArea
{
private const double Tie = 1e-6;
/// The area a part takes off a sheet: its outline, holes included.
internal static double Outline(JobPartGeometry geometry) => System.Math.Abs(geometry.Perimeter.Area());
internal static double Of(IReadOnlyList poses, Func geometry)
{
var area = 0.0;
foreach (var pose in poses)
area += Outline(geometry(pose.PartId));
return area;
}
/// True when seats more area, beyond rounding: areas
/// summed in another order count as the same.
internal static bool More(double candidate, double incumbent) =>
candidate - incumbent > Tie * System.Math.Max(candidate, incumbent);
/// True when should replace
/// : it seats more area, or the same area more compactly.
internal static bool Better(IReadOnlyList candidate,
IReadOnlyList incumbent, Func geometry)
{
var area = Of(candidate, geometry);
var other = Of(incumbent, geometry);
return More(area, other)
|| (!More(other, area) && CutoutPackerProposal.Tighter(candidate, incumbent, geometry));
}
}