From 99546e7eefd127bfeea5ea55bc1138a4b50285f3 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Sat, 21 Mar 2026 12:43:45 -0400 Subject: [PATCH] feat: add IFillComparer hooks to NestEngineBase Add virtual comparer, direction, and angle-building hooks to NestEngineBase so subclasses can override scoring and direction policy. Rewire IsBetterFill to delegate to the comparer instead of calling FillScore directly. Co-Authored-By: Claude Sonnet 4.6 --- OpenNest.Engine/NestEngineBase.cs | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/OpenNest.Engine/NestEngineBase.cs b/OpenNest.Engine/NestEngineBase.cs index 9315f9e..909e772 100644 --- a/OpenNest.Engine/NestEngineBase.cs +++ b/OpenNest.Engine/NestEngineBase.cs @@ -1,4 +1,6 @@ +using OpenNest.Engine; using OpenNest.Engine.Fill; +using OpenNest.Engine.Strategies; using OpenNest.Geometry; using System; using System.Collections.Generic; @@ -31,6 +33,25 @@ namespace OpenNest public abstract string Description { get; } + // --- Engine policy --- + + private IFillComparer _comparer; + + protected IFillComparer Comparer => _comparer ??= CreateComparer(); + + protected virtual IFillComparer CreateComparer() => new DefaultFillComparer(); + + public virtual NestDirection? PreferredDirection => null; + + public virtual List BuildAngles(NestItem item, double bestRotation, Box workArea) + { + return new List { bestRotation, bestRotation + OpenNest.Math.Angle.HalfPI }; + } + + protected virtual void RecordProductiveAngles(List angleResults) { } + + protected FillPolicy BuildPolicy() => new FillPolicy(Comparer, PreferredDirection); + // --- Virtual methods (side-effect-free, return parts) --- public virtual List Fill(NestItem item, Box workArea, @@ -255,15 +276,7 @@ namespace OpenNest } protected bool IsBetterFill(List candidate, List current, Box workArea) - { - if (candidate == null || candidate.Count == 0) - return false; - - if (current == null || current.Count == 0) - return true; - - return FillScore.Compute(candidate, workArea) > FillScore.Compute(current, workArea); - } + => Comparer.IsBetter(candidate, current, workArea); protected bool IsBetterValidFill(List candidate, List current, Box workArea) {