#nullable enable using System; using System.Collections.Generic; using System.Linq; using System.Threading; using Clipper2Lib; using OpenNest.Engine.BestFit; using OpenNest.Engine.Jobs; using OpenNest.Engine.Jobs.Adapters; using OpenNest.Engine.Jobs.Placement; using OpenNest.Geometry; using OpenNest.Math; namespace OpenNest.Engine.NestingEngines.Irregular; /// /// Per-solve, per-spacing Fill proposals: a type's Lattice pattern in a rectangle, as a block for /// the frontier packer () or as a band (). Only the members /// occupy material. /// /// Every proposal is a whole Fill of its own, so the catalog keeps what it built, charges it to /// the solve's work meter and limits the blocks a type may ask for. Later decisions read that /// meter: when a proposal is charged, and how much, is part of the engine's result. /// internal sealed class BlockCatalog : IDisposable { /// Fewest copies in a block or band; a Fill of fewer proposes nothing. private const int MinCopies = 3; /// Most blocks prepares per type; bands are not limited. private const int MaxPreparationsPerType = 8; /// Free rectangles of a sheet that blocks are offered: its largest. private const int MaxRectangles = 2; /// Clearance kept from occupied material beyond its spacing and flattening tolerance. private const double OccupiedMargin = 0.001; /// Miter limit of the offset that grows occupied material by its clearance. private const double MiterLimit = 2; /// Margin for Fill's boundary rounding in a . private const double FillRoundingMargin = 0.001; /// Decimals a certified pose's position is rounded to. private const int PositionDecimals = 8; /// Decimals a certified pose's rotation, in radians, is rounded to. private const int RotationDecimals = 10; private readonly double spacing; /// The spacing a band is filled at: one grid unit wider; see . private readonly double bandSpacing; private readonly WorkCounter counter; private readonly long budget; private readonly Func? drawingFor; /// The catalog's own drawing per type, made only without . private readonly Dictionary drawings = new(); /// Per type, the orientations members are bound to: the type's own, its pairs' and /// those its proposals added. private readonly Dictionary> orientations = new(); private readonly Dictionary footprints = new(); /// Per type, the blocks has prepared. private readonly Dictionary preparations = new(); private readonly Dictionary> blocks = new(); /// Guarded by its own lock, as are its entries' members and charge. private readonly Dictionary bands = new(); /// What a pattern is kept under: its type, the copies wanted and the size of its box. private readonly record struct PatternKey(int Type, int Quantity, double Length, double Width); /// A pattern asked for: up to Quantity copies of the type in a box the size of /// the rectangle. private readonly record struct Request(PartType Type, int Quantity, Box Rectangle) { public PatternKey Key => new(Type.Index, Quantity, Rectangle.Length, Rectangle.Width); /// Whether nothing can come of it: too few copies wanted, a type with no outline or /// a box with no area. public bool IsEmpty => Quantity < MinCopies || Type.Orientations.Count == 0 || Rectangle.Length <= 0 || Rectangle.Width <= 0; /// /// Work charged for one Fill proposal. The Fill is a full independent NFP-based pack whose /// own operations never touch the solve's effort meter; this charges it by the factors that /// drive its real cost, demand and outline complexity, so the shared budget sees it. /// public long Cost { get { var vertices = Type.Orientations.Count > 0 ? Type.Orientations[0].Outline.Count : 1; return (long)Quantity * Quantity * System.Math.Max(1, vertices); } } } /// /// Lower bounds on the true part's width at any rotation and on its area: the outline's /// convex-hull width and area, less what flattening within the outline's tolerance (and a /// margin for Fill's boundary rounding) could hide. /// private readonly record struct Footprint(double Width, double Area) { public static Footprint Of(Orientation orientation) { var outline = orientation.Outline; var width = HullWidth(outline); var perimeter = Perimeter(outline); return new(double.IsFinite(width) ? width - 2 * orientation.Tolerance - FillRoundingMargin : 0, System.Math.Abs(Clipper.Area(outline)) - perimeter * (orientation.Tolerance + FillRoundingMargin)); } /// The convex hull's least extent across any of its edges; infinite for a hull with /// no edge. private static double HullWidth(PathD outline) { var hull = ConvexHull.Compute(outline.Select(p => new Vector(p.x, p.y)).ToList()).Vertices; var width = double.PositiveInfinity; for (var i = 0; i < hull.Count; i++) { var a = hull[i]; var b = hull[(i + 1) % hull.Count]; var length = a.DistanceTo(b); if (length <= Tolerance.Epsilon) continue; var across = hull.Max(p => System.Math.Abs((b.X - a.X) * (p.Y - a.Y) - (b.Y - a.Y) * (p.X - a.X)) / length); width = System.Math.Min(width, across); } return width; } private static double Perimeter(PathD outline) { var perimeter = 0.0; for (var i = 0; i < outline.Count; i++) { var a = outline[i]; var b = outline[(i + 1) % outline.Count]; perimeter += System.Math.Sqrt((b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y)); } return perimeter; } } /// /// A built band pattern: its members, or while only prefetched, its certified poses awaiting /// orientations; no poses is an empty pattern. The work it cost is charged on its first /// ordinary request. /// private sealed class BandEntry(NestJobPlacement[]? poses, long cost, double rejected) { /// X extent of a fill the certification rejected; zero when none was. public double Rejected { get; } = rejected; public IReadOnlyList? Members { get; set; } = poses == null ? Array.Empty() : null; public NestJobPlacement[]? Poses { get; } = poses; public long Cost { get; } = cost; public bool Charged { get; set; } } /// Shared private drawings whose best-fit entries the caller owns; /// null makes the catalog own one per type and release it on dispose. public BlockCatalog(double spacing, IReadOnlyList types, IReadOnlyDictionary> pairs, WorkCounter counter, long budget, Func? drawingFor = null) { this.spacing = spacing; bandSpacing = spacing + System.Math.Pow(10, -NoFitCache.Precision); this.counter = counter; this.budget = budget; this.drawingFor = drawingFor; foreach (var type in types) { orientations[type.Index] = WithPairOrientations(type, pairs); if (type.Orientations.Count > 0) footprints[type.Index] = Footprint.Of(type.Orientations[0]); } } /// The type's orientations, then those of its pairs that are not among them. private static List WithPairOrientations(PartType type, IReadOnlyDictionary> pairs) { var poses = type.Orientations.ToList(); if (pairs.TryGetValue(type.Index, out var found)) poses.AddRange(found.SelectMany(p => new[] { p.A, p.B })); return poses.Distinct().ToList(); } /// Blocks prepared so far, over all types. internal int PreparationCount => preparations.Values.Sum(); /// /// The free rectangles of a work area blocks are offered: the largest clear of the parts /// placed, their spacing included. /// internal static IReadOnlyList Rectangles(Box work, IReadOnlyList placed, double spacing) => MaximalRectangles.InRegion(FreeRegion(work, placed, spacing)).OrderByDescending(b => b.Area()) .ThenBy(b => b.Left).ThenBy(b => b.Bottom).ThenBy(b => b.Length).Take(MaxRectangles).ToArray(); private static PathsD FreeRegion(Box work, IReadOnlyList placed, double spacing) { var free = new PathsD { new PathD { new(work.Left, work.Bottom), new(work.Right, work.Bottom), new(work.Right, work.Top), new(work.Left, work.Top), } }; foreach (var part in placed) { // Physical occupied material, not a reference-point region for any moving part. // The catalog currently prepares solid outlines; future profile preparation owns holes. var blocked = Clipper.InflatePaths(new PathsD { part.Orientation.Outline }, spacing + part.Orientation.Tolerance + OccupiedMargin, JoinType.Miter, EndType.Polygon, MiterLimit, NoFitCache.Precision); free = Clipper.Difference(free, Clipper.TranslatePaths(blocked, part.X, part.Y), FillRule.NonZero, NoFitCache.Precision); } return free; } /// /// The type's Lattice pattern in the rectangle, up to copies, as a /// block: kept per type, quantity and rectangle size once prepared. Empty when no block comes /// of it, or when the type's preparations or the solve's work budget are spent. /// public IReadOnlyList Get(PartType type, int quantity, Box rectangle, CancellationToken token) { token.ThrowIfCancellationRequested(); var request = new Request(type, quantity, rectangle); if (request.IsEmpty) return Array.Empty(); var key = request.Key; if (blocks.TryGetValue(key, out var kept)) return kept; if (CannotPrepare(request)) return Array.Empty(); preparations[type.Index] = PreparationsOf(type) + 1; var block = Prepare(request, token); blocks[key] = block; return block; } /// /// Whether no block is prepared for the request: its rectangle is smaller than three parts, /// or the type's preparations or the solve's work budget are spent. /// private bool CannotPrepare(Request request) => request.Rectangle.Area() < MinCopies * request.Type.Area || PreparationsOf(request.Type) >= MaxPreparationsPerType || counter.Value >= budget; private int PreparationsOf(PartType type) => preparations.GetValueOrDefault(type.Index); /// /// Charges a block's proposal and makes it. A rectangle that cannot hold a block is charged /// and counted as if filled, so the meter, and every later decision it steers, reads the same /// as when the Fill ran. /// private IReadOnlyList Prepare(Request request, CancellationToken token) { counter.Add(request.Cost); if (CannotHoldBlock(request)) return Array.Empty(); var type = request.Type; var poses = Propose(request, spacing, DrawingOf(type), token, out _); return poses == null ? Array.Empty() : Bind(type, poses, orientations[type.Index], token); } /// /// True when the rectangle cannot hold three copies of the type: it is narrower than the part /// at every rotation, or smaller than three parts. Fill could return at most two members there, /// which never form a block, so its proposal is known to be empty without running it. /// private bool CannotHoldBlock(Request request) { var rectangle = request.Rectangle; return footprints.TryGetValue(request.Type.Index, out var footprint) && (System.Math.Min(rectangle.Length, rectangle.Width) < footprint.Width || rectangle.Area() < MinCopies * footprint.Area); } /// /// The type's Lattice pattern in a box of the given size, up to /// copies, for a band; unlike it is not limited in attempts per type. The /// pattern is filled one grid unit wider than the spacing: Fill sets parts exactly the spacing /// apart, which the stricter certification can reject for curved parts (a stack of circles), /// and losing the band then loses its densest pattern. /// /// With a drawing (see ) the pattern /// is built ahead of need, which may run in parallel with other prefetches, even of the same /// type, each with its own drawing. A prefetch charges no work and adds no orientations: the /// pattern's first ordinary request charges it and binds its poses to the catalog's /// orientations, so the work meter and the orientations read as if the pattern were built then. /// public IReadOnlyList Band(PartType type, int quantity, Box rectangle, CancellationToken token, Drawing? prefetch = null) { token.ThrowIfCancellationRequested(); var request = new Request(type, quantity, rectangle); if (request.IsEmpty) return Array.Empty(); var entry = BandFor(request, token, prefetch); if (prefetch != null) return entry.Members ?? Bind(type, entry.Poses!, orientations[type.Index].ToList(), token); lock (bands) { // The first ordinary request pays for the pattern, whichever request built it. if (!entry.Charged) { counter.Add(entry.Cost); entry.Charged = true; } if (entry.Members != null) return entry.Members; } // Ordinary requests of one type run on one thread at a time. var bound = Bind(type, entry.Poses!, orientations[type.Index], token); lock (bands) entry.Members = bound; return bound; } /// /// X extent of the fill a request already made got back and its /// certification rejected (Fill can set copies of neighboring columns closer than the /// spacing); zero when the request is unknown or nothing was rejected. A band search asks /// again just short of it, where Fill must choose another pattern. /// public double RejectedBandLength(PartType type, int quantity, Box rectangle) { lock (bands) return bands.TryGetValue(new Request(type, quantity, rectangle).Key, out var entry) ? entry.Rejected : 0; } /// The band's entry, its pattern proposed here when the catalog has none; charges /// nothing and binds nothing. private BandEntry BandFor(Request request, CancellationToken token, Drawing? prefetch) { var key = request.Key; BandEntry? entry; lock (bands) bands.TryGetValue(key, out entry); if (entry != null) return entry; // Types may build bands in parallel: each type's drawing and orientation list are its // own. Prefetches of one type fill their own drawings and only read the orientations. var rejected = 0.0; var poses = CannotHoldBlock(request) ? null : Propose(request, bandSpacing, prefetch ?? DrawingOf(request.Type), token, out rejected); lock (bands) { if (!bands.TryGetValue(key, out entry)) bands[key] = entry = new BandEntry(poses, request.Cost, rejected); return entry; } } /// /// A private drawing of the type for one prefetching thread: Fill sorts a drawing's cached /// best-fit list in place, so two fills of one drawing must not run at once. It draws the same /// best-fit candidates from the geometry pool; release it with . /// public static Drawing PrefetchDrawing(PartType type) => DrawingJobMapper.CreateDrawing(type.Part); public static void ReleasePrefetch(Drawing drawing) => BestFitCache.Invalidate(drawing); private Drawing DrawingOf(PartType type) => drawingFor?.Invoke(type.Part) ?? OwnDrawing(type); private Drawing OwnDrawing(PartType type) { lock (drawings) { if (!drawings.TryGetValue(type.Index, out var drawing)) drawings[type.Index] = drawing = DrawingJobMapper.CreateDrawing(type.Part); return drawing; } } public void Dispose() { foreach (var drawing in drawings.Values) BestFitCache.Invalidate(drawing); drawings.Clear(); } /// /// The certified poses of the request's Fill of at /// ; null when Fill fails or certification rejects it, then /// with the X extent of the rejected fill in (zero otherwise). /// private NestJobPlacement[]? Propose(Request request, double fillSpacing, Drawing drawing, CancellationToken token, out double rejected) { rejected = 0; var (type, quantity, rectangle) = request; try { var members = PrivatePlateFill.Run(drawing, type.Part.Rotation, fillSpacing, rectangle.Length, rectangle.Width, quantity, token); token.ThrowIfCancellationRequested(); var kept = members.Take(quantity).ToArray(); var poses = Certify(type, kept, spacing, token); if (poses == null && kept.Length >= MinCopies) { var extent = kept.Max(p => p.BoundingBox.Right) - kept.Min(p => p.BoundingBox.Left); rejected = double.IsFinite(extent) ? System.Math.Max(0, extent) : 0; } return poses; } catch (Exception ex) when (IsProposalFailure(ex)) { return null; } } /// Whether the exception is a proposal that failed rather than a solve that must stop. private static bool IsProposalFailure(Exception ex) => ex is ArgumentException or InvalidOperationException or NotSupportedException or ArithmeticException; /// The members certified and bound to ; empty when they /// cannot form a block. internal static IReadOnlyList Resolve(PartType type, IReadOnlyList members, List orientations, double spacing, CancellationToken token = default) { var poses = Certify(type, members, spacing, token); return poses == null ? Array.Empty() : Bind(type, poses, orientations, token); } /// /// The members' poses, quantized and ordered, when every pair keeps the spacing; null when /// they cannot form a block. /// private static NestJobPlacement[]? Certify(PartType type, IReadOnlyList members, double spacing, CancellationToken token) { token.ThrowIfCancellationRequested(); if (members.Count < MinCopies) return null; var geometry = JobPartGeometry.TryRead(type.Part.Geometry); if (geometry == null) return null; var poses = QuantizedPoses(type, members); if (poses.Any(p => !IsAllowed(type, p))) return null; return KeepSpacing(geometry, poses, spacing, token) ? poses : null; } /// /// The members' poses in a fixed order. Canonical rebinding is already performed by FillItem; /// quantization removes sub-grid arithmetic differences from equivalent Fill proposals, and /// the poses certified are the quantized ones. /// private static NestJobPlacement[] QuantizedPoses(PartType type, IReadOnlyList members) => members.Select(p => new NestJobPlacement(type.Part.Id, 0, System.Math.Round(p.Location.X, PositionDecimals), System.Math.Round(p.Location.Y, PositionDecimals), System.Math.Round(Angle.NormalizeRad(p.Rotation), RotationDecimals))) .OrderBy(p => p.X).ThenBy(p => p.Y).ThenBy(p => p.Rotation).ToArray(); private static bool IsAllowed(PartType type, NestJobPlacement pose) => double.IsFinite(pose.X) && double.IsFinite(pose.Y) && double.IsFinite(pose.Rotation) && type.Part.Rotation.Allows(pose.Rotation); /// Whether every pair of the poses keeps the spacing. private static bool KeepSpacing(JobPartGeometry geometry, NestJobPlacement[] poses, double spacing, CancellationToken token) { // Each member outlined once, not once per pair it is tested in. var prepared = poses.Select(p => NestLayoutCheck.Prepare(geometry, p, spacing)).ToArray(); for (var i = 0; i < poses.Length; i++) for (var j = i + 1; j < poses.Length; j++) { token.ThrowIfCancellationRequested(); if (!NestLayoutCheck.Clears(prepared[i], prepared[j])) return false; } return true; } /// /// The certified poses as members relative to the first, each at the orientation of its /// rotation, which is added to when missing; empty when one /// cannot be made. /// private static IReadOnlyList Bind(PartType type, NestJobPlacement[] poses, List orientations, CancellationToken token) { try { var result = new List(); foreach (var pose in poses) { token.ThrowIfCancellationRequested(); var orientation = OrientationAt(type, orientations, pose.Rotation); if (orientation == null) return Array.Empty(); result.Add(new Placed(orientation, pose.X - poses[0].X, pose.Y - poses[0].Y)); } return result; } catch (Exception ex) when (IsProposalFailure(ex)) { return Array.Empty(); } } /// The orientation at the rotation, made and added to /// when missing; null when it cannot be made. private static Orientation? OrientationAt(PartType type, List orientations, double rotation) { var orientation = orientations.FirstOrDefault(o => o.Rotation == rotation); if (orientation != null) return orientation; orientation = PartCatalog.CreateOrientation(type, orientations.Max(o => o.Index) + 1, rotation); if (orientation != null) orientations.Add(orientation); return orientation; } }