refactor: extract IPairEvaluator interface from PairEvaluator

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-07 18:19:05 -05:00
parent dd7383467b
commit b83d09c3a7
3 changed files with 37 additions and 8 deletions

View File

@@ -1,5 +1,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using OpenNest.Converters;
using OpenNest.Engine.BestFit.Tiling;
using OpenNest.Geometry;
@@ -9,12 +11,12 @@ namespace OpenNest.Engine.BestFit
{
public class BestFitFinder
{
private readonly PairEvaluator _evaluator;
private readonly IPairEvaluator _evaluator;
private readonly BestFitFilter _filter;
public BestFitFinder(double maxPlateWidth, double maxPlateHeight)
public BestFitFinder(double maxPlateWidth, double maxPlateHeight, IPairEvaluator evaluator = null)
{
_evaluator = new PairEvaluator();
_evaluator = evaluator ?? new PairEvaluator();
_filter = new BestFitFilter
{
MaxPlateWidth = maxPlateWidth,
@@ -30,12 +32,16 @@ namespace OpenNest.Engine.BestFit
{
var strategies = BuildStrategies(drawing);
var allCandidates = new List<PairCandidate>();
var candidateBags = new ConcurrentBag<List<PairCandidate>>();
foreach (var strategy in strategies)
allCandidates.AddRange(strategy.GenerateCandidates(drawing, spacing, stepSize));
Parallel.ForEach(strategies, strategy =>
{
candidateBags.Add(strategy.GenerateCandidates(drawing, spacing, stepSize));
});
var results = allCandidates.Select(c => _evaluator.Evaluate(c)).ToList();
var allCandidates = candidateBags.SelectMany(c => c).ToList();
var results = _evaluator.EvaluateAll(allCandidates);
_filter.Apply(results);

View File

@@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace OpenNest.Engine.BestFit
{
public interface IPairEvaluator
{
List<BestFitResult> EvaluateAll(List<PairCandidate> candidates);
}
}

View File

@@ -1,15 +1,29 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using OpenNest.Converters;
using OpenNest.Geometry;
using OpenNest.Math;
namespace OpenNest.Engine.BestFit
{
public class PairEvaluator
public class PairEvaluator : IPairEvaluator
{
private const double ChordTolerance = 0.01;
public List<BestFitResult> EvaluateAll(List<PairCandidate> candidates)
{
var resultBag = new ConcurrentBag<BestFitResult>();
Parallel.ForEach(candidates, c =>
{
resultBag.Add(Evaluate(c));
});
return resultBag.ToList();
}
public BestFitResult Evaluate(PairCandidate candidate)
{
var drawing = candidate.Drawing;