merge: resolve conflicts from remote nesting progress changes
Kept using OpenNest.Api in Timing.cs and EditNestForm.cs alongside remote's reorganized usings and namespace changes. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
35
OpenNest.Engine/Fill/AccumulatingProgress.cs
Normal file
35
OpenNest.Engine/Fill/AccumulatingProgress.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace OpenNest.Engine.Fill
|
||||
{
|
||||
/// <summary>
|
||||
/// Wraps an IProgress to prepend previously placed parts to each report,
|
||||
/// so the UI shows the full picture during incremental fills.
|
||||
/// </summary>
|
||||
internal class AccumulatingProgress : IProgress<NestProgress>
|
||||
{
|
||||
private readonly IProgress<NestProgress> inner;
|
||||
private readonly List<Part> previousParts;
|
||||
|
||||
public AccumulatingProgress(IProgress<NestProgress> inner, List<Part> previousParts)
|
||||
{
|
||||
this.inner = inner;
|
||||
this.previousParts = previousParts;
|
||||
}
|
||||
|
||||
public void Report(NestProgress value)
|
||||
{
|
||||
if (value.BestParts != null && previousParts.Count > 0)
|
||||
{
|
||||
var combined = new List<Part>(previousParts.Count + value.BestParts.Count);
|
||||
combined.AddRange(previousParts);
|
||||
combined.AddRange(value.BestParts);
|
||||
value.BestParts = combined;
|
||||
value.BestPartCount = combined.Count;
|
||||
}
|
||||
|
||||
inner.Report(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
114
OpenNest.Engine/Fill/AngleCandidateBuilder.cs
Normal file
114
OpenNest.Engine/Fill/AngleCandidateBuilder.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using OpenNest.Engine.ML;
|
||||
using OpenNest.Geometry;
|
||||
using OpenNest.Math;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
|
||||
namespace OpenNest.Engine.Fill
|
||||
{
|
||||
/// <summary>
|
||||
/// Builds candidate rotation angles for single-item fill. Encapsulates the
|
||||
/// full pipeline: base angles, narrow-area sweep, ML prediction, and
|
||||
/// known-good pruning across fills.
|
||||
/// </summary>
|
||||
public class AngleCandidateBuilder
|
||||
{
|
||||
private readonly HashSet<double> knownGoodAngles = new();
|
||||
|
||||
public bool ForceFullSweep { get; set; }
|
||||
|
||||
public List<double> Build(NestItem item, double bestRotation, Box workArea)
|
||||
{
|
||||
var baseAngles = new[] { bestRotation, bestRotation + Angle.HalfPI };
|
||||
|
||||
if (knownGoodAngles.Count > 0 && !ForceFullSweep)
|
||||
return BuildPrunedList(baseAngles);
|
||||
|
||||
var angles = new List<double>(baseAngles);
|
||||
|
||||
if (NeedsSweep(item, bestRotation, workArea))
|
||||
AddSweepAngles(angles);
|
||||
|
||||
if (!ForceFullSweep && angles.Count > 2)
|
||||
angles = ApplyMlPrediction(item, workArea, baseAngles, angles);
|
||||
|
||||
return angles;
|
||||
}
|
||||
|
||||
private bool NeedsSweep(NestItem item, double bestRotation, Box workArea)
|
||||
{
|
||||
var testPart = new Part(item.Drawing);
|
||||
if (!bestRotation.IsEqualTo(0))
|
||||
testPart.Rotate(bestRotation);
|
||||
testPart.UpdateBounds();
|
||||
|
||||
var partLongestSide = System.Math.Max(testPart.BoundingBox.Width, testPart.BoundingBox.Length);
|
||||
var workAreaShortSide = System.Math.Min(workArea.Width, workArea.Length);
|
||||
return workAreaShortSide < partLongestSide || ForceFullSweep;
|
||||
}
|
||||
|
||||
private static void AddSweepAngles(List<double> angles)
|
||||
{
|
||||
var step = Angle.ToRadians(5);
|
||||
for (var a = 0.0; a < System.Math.PI; a += step)
|
||||
{
|
||||
if (!ContainsAngle(angles, a))
|
||||
angles.Add(a);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<double> ApplyMlPrediction(
|
||||
NestItem item, Box workArea, double[] baseAngles, List<double> fallback)
|
||||
{
|
||||
var features = FeatureExtractor.Extract(item.Drawing);
|
||||
if (features == null)
|
||||
return fallback;
|
||||
|
||||
var predicted = AnglePredictor.PredictAngles(features, workArea.Width, workArea.Length);
|
||||
if (predicted == null)
|
||||
return fallback;
|
||||
|
||||
var mlAngles = new List<double>(predicted);
|
||||
foreach (var b in baseAngles)
|
||||
{
|
||||
if (!ContainsAngle(mlAngles, b))
|
||||
mlAngles.Add(b);
|
||||
}
|
||||
|
||||
Debug.WriteLine($"[AngleCandidateBuilder] ML: {fallback.Count} angles -> {mlAngles.Count} predicted");
|
||||
return mlAngles;
|
||||
}
|
||||
|
||||
private List<double> BuildPrunedList(double[] baseAngles)
|
||||
{
|
||||
var pruned = new List<double>(baseAngles);
|
||||
foreach (var a in knownGoodAngles)
|
||||
{
|
||||
if (!ContainsAngle(pruned, a))
|
||||
pruned.Add(a);
|
||||
}
|
||||
|
||||
Debug.WriteLine($"[AngleCandidateBuilder] Pruned to {pruned.Count} angles (known-good)");
|
||||
return pruned;
|
||||
}
|
||||
|
||||
private static bool ContainsAngle(List<double> angles, double angle)
|
||||
{
|
||||
return angles.Any(existing => existing.IsEqualTo(angle));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Records angles that produced results. These are used to prune
|
||||
/// subsequent Build() calls.
|
||||
/// </summary>
|
||||
public void RecordProductive(List<AngleResult> angleResults)
|
||||
{
|
||||
foreach (var ar in angleResults)
|
||||
{
|
||||
if (ar.PartCount > 0)
|
||||
knownGoodAngles.Add(Angle.ToRadians(ar.AngleDeg));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
80
OpenNest.Engine/Fill/BestCombination.cs
Normal file
80
OpenNest.Engine/Fill/BestCombination.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
using OpenNest.Math;
|
||||
|
||||
namespace OpenNest
|
||||
{
|
||||
internal static class BestCombination
|
||||
{
|
||||
public static bool FindFrom2(double length1, double length2, double overallLength, out int count1, out int count2)
|
||||
{
|
||||
overallLength += Tolerance.Epsilon;
|
||||
|
||||
if (length1 > overallLength)
|
||||
{
|
||||
if (length2 > overallLength)
|
||||
{
|
||||
count1 = 0;
|
||||
count2 = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
count1 = 0;
|
||||
count2 = (int)System.Math.Floor(overallLength / length2);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (length2 > overallLength)
|
||||
{
|
||||
count1 = (int)System.Math.Floor(overallLength / length1);
|
||||
count2 = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
var maxCountLength1 = (int)System.Math.Floor(overallLength / length1);
|
||||
|
||||
count1 = maxCountLength1;
|
||||
count2 = 0;
|
||||
|
||||
var remnant = overallLength - maxCountLength1 * length1;
|
||||
|
||||
if (remnant.IsEqualTo(0))
|
||||
return true;
|
||||
|
||||
for (int countLength1 = 0; countLength1 <= maxCountLength1; ++countLength1)
|
||||
{
|
||||
var remnant1 = overallLength - countLength1 * length1;
|
||||
|
||||
if (remnant1 >= length2)
|
||||
{
|
||||
var countLength2 = (int)System.Math.Floor(remnant1 / length2);
|
||||
var remnant2 = remnant1 - length2 * countLength2;
|
||||
|
||||
if (!(remnant2 < remnant))
|
||||
continue;
|
||||
|
||||
count1 = countLength1;
|
||||
count2 = countLength2;
|
||||
|
||||
if (remnant2.IsEqualTo(0))
|
||||
break;
|
||||
|
||||
remnant = remnant2;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!(remnant1 < remnant))
|
||||
continue;
|
||||
|
||||
count1 = countLength1;
|
||||
count2 = 0;
|
||||
|
||||
if (remnant1.IsEqualTo(0))
|
||||
break;
|
||||
|
||||
remnant = remnant1;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
178
OpenNest.Engine/Fill/Compactor.cs
Normal file
178
OpenNest.Engine/Fill/Compactor.cs
Normal file
@@ -0,0 +1,178 @@
|
||||
using OpenNest.Geometry;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace OpenNest.Engine.Fill
|
||||
{
|
||||
/// <summary>
|
||||
/// Pushes a group of parts left and down to close gaps after placement.
|
||||
/// Uses the same directional-distance logic as PlateView.PushSelected
|
||||
/// but operates on Part objects directly.
|
||||
/// </summary>
|
||||
public static class Compactor
|
||||
{
|
||||
private const double ChordTolerance = 0.001;
|
||||
|
||||
public static double Push(List<Part> movingParts, Plate plate, PushDirection direction)
|
||||
{
|
||||
var obstacleParts = plate.Parts
|
||||
.Where(p => !movingParts.Contains(p))
|
||||
.ToList();
|
||||
|
||||
return Push(movingParts, obstacleParts, plate.WorkArea(), plate.PartSpacing, direction);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pushes movingParts along an arbitrary angle (radians, 0 = right, π/2 = up).
|
||||
/// </summary>
|
||||
public static double Push(List<Part> movingParts, Plate plate, double angle)
|
||||
{
|
||||
var obstacleParts = plate.Parts
|
||||
.Where(p => !movingParts.Contains(p))
|
||||
.ToList();
|
||||
|
||||
var direction = new Vector(System.Math.Cos(angle), System.Math.Sin(angle));
|
||||
return Push(movingParts, obstacleParts, plate.WorkArea(), plate.PartSpacing, direction);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pushes movingParts along an arbitrary angle (radians, 0 = right, π/2 = up).
|
||||
/// </summary>
|
||||
public static double Push(List<Part> movingParts, List<Part> obstacleParts,
|
||||
Box workArea, double partSpacing, Vector direction)
|
||||
{
|
||||
var opposite = -direction;
|
||||
|
||||
var obstacleBoxes = new Box[obstacleParts.Count];
|
||||
var obstacleLines = new List<Line>[obstacleParts.Count];
|
||||
|
||||
for (var i = 0; i < obstacleParts.Count; i++)
|
||||
obstacleBoxes[i] = obstacleParts[i].BoundingBox;
|
||||
|
||||
var halfSpacing = partSpacing / 2;
|
||||
var distance = double.MaxValue;
|
||||
|
||||
foreach (var moving in movingParts)
|
||||
{
|
||||
var edgeDist = SpatialQuery.EdgeDistance(moving.BoundingBox, workArea, direction);
|
||||
if (edgeDist <= 0)
|
||||
distance = 0;
|
||||
else if (edgeDist < distance)
|
||||
distance = edgeDist;
|
||||
|
||||
var movingBox = moving.BoundingBox;
|
||||
List<Line> movingLines = null;
|
||||
|
||||
for (var i = 0; i < obstacleBoxes.Length; i++)
|
||||
{
|
||||
var reverseGap = SpatialQuery.DirectionalGap(movingBox, obstacleBoxes[i], opposite);
|
||||
if (reverseGap > 0)
|
||||
continue;
|
||||
|
||||
var gap = SpatialQuery.DirectionalGap(movingBox, obstacleBoxes[i], direction);
|
||||
if (gap >= distance)
|
||||
continue;
|
||||
|
||||
if (!SpatialQuery.PerpendicularOverlap(movingBox, obstacleBoxes[i], direction))
|
||||
continue;
|
||||
|
||||
movingLines ??= halfSpacing > 0
|
||||
? PartGeometry.GetOffsetPartLines(moving, halfSpacing, direction, ChordTolerance)
|
||||
: PartGeometry.GetPartLines(moving, direction, ChordTolerance);
|
||||
|
||||
obstacleLines[i] ??= halfSpacing > 0
|
||||
? PartGeometry.GetOffsetPartLines(obstacleParts[i], halfSpacing, opposite, ChordTolerance)
|
||||
: PartGeometry.GetPartLines(obstacleParts[i], opposite, ChordTolerance);
|
||||
|
||||
var d = SpatialQuery.DirectionalDistance(movingLines, obstacleLines[i], direction);
|
||||
if (d < distance)
|
||||
distance = d;
|
||||
}
|
||||
}
|
||||
|
||||
if (distance < double.MaxValue && distance > 0)
|
||||
{
|
||||
var offset = direction * distance;
|
||||
foreach (var moving in movingParts)
|
||||
moving.Offset(offset);
|
||||
return distance;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static double Push(List<Part> movingParts, List<Part> obstacleParts,
|
||||
Box workArea, double partSpacing, PushDirection direction)
|
||||
{
|
||||
var vector = SpatialQuery.DirectionToOffset(direction, 1.0);
|
||||
return Push(movingParts, obstacleParts, workArea, partSpacing, vector);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pushes movingParts using bounding-box distances only (no geometry lines).
|
||||
/// Much faster but less precise — use as a coarse positioning pass before
|
||||
/// a full geometry Push.
|
||||
/// </summary>
|
||||
public static double PushBoundingBox(List<Part> movingParts, Plate plate, PushDirection direction)
|
||||
{
|
||||
var obstacleParts = plate.Parts
|
||||
.Where(p => !movingParts.Contains(p))
|
||||
.ToList();
|
||||
|
||||
return PushBoundingBox(movingParts, obstacleParts, plate.WorkArea(), plate.PartSpacing, direction);
|
||||
}
|
||||
|
||||
public static double PushBoundingBox(List<Part> movingParts, List<Part> obstacleParts,
|
||||
Box workArea, double partSpacing, PushDirection direction)
|
||||
{
|
||||
var obstacleBoxes = new Box[obstacleParts.Count];
|
||||
for (var i = 0; i < obstacleParts.Count; i++)
|
||||
obstacleBoxes[i] = obstacleParts[i].BoundingBox;
|
||||
|
||||
var opposite = SpatialQuery.OppositeDirection(direction);
|
||||
var isHorizontal = SpatialQuery.IsHorizontalDirection(direction);
|
||||
var distance = double.MaxValue;
|
||||
|
||||
foreach (var moving in movingParts)
|
||||
{
|
||||
var edgeDist = SpatialQuery.EdgeDistance(moving.BoundingBox, workArea, direction);
|
||||
if (edgeDist <= 0)
|
||||
distance = 0;
|
||||
else if (edgeDist < distance)
|
||||
distance = edgeDist;
|
||||
|
||||
var movingBox = moving.BoundingBox;
|
||||
|
||||
for (var i = 0; i < obstacleBoxes.Length; i++)
|
||||
{
|
||||
var reverseGap = SpatialQuery.DirectionalGap(movingBox, obstacleBoxes[i], opposite);
|
||||
if (reverseGap > 0)
|
||||
continue;
|
||||
|
||||
var perpOverlap = isHorizontal
|
||||
? movingBox.IsHorizontalTo(obstacleBoxes[i], out _)
|
||||
: movingBox.IsVerticalTo(obstacleBoxes[i], out _);
|
||||
|
||||
if (!perpOverlap)
|
||||
continue;
|
||||
|
||||
var gap = SpatialQuery.DirectionalGap(movingBox, obstacleBoxes[i], direction);
|
||||
var d = gap - partSpacing;
|
||||
if (d < 0) d = 0;
|
||||
if (d < distance)
|
||||
distance = d;
|
||||
}
|
||||
}
|
||||
|
||||
if (distance < double.MaxValue && distance > 0)
|
||||
{
|
||||
var offset = SpatialQuery.DirectionToOffset(direction, distance);
|
||||
foreach (var moving in movingParts)
|
||||
moving.Offset(offset);
|
||||
return distance;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
372
OpenNest.Engine/Fill/FillExtents.cs
Normal file
372
OpenNest.Engine/Fill/FillExtents.cs
Normal file
@@ -0,0 +1,372 @@
|
||||
using OpenNest.Geometry;
|
||||
using OpenNest.Math;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Threading;
|
||||
|
||||
namespace OpenNest.Engine.Fill
|
||||
{
|
||||
public class FillExtents
|
||||
{
|
||||
private const int MaxIterations = 10;
|
||||
|
||||
private readonly Box workArea;
|
||||
private readonly double partSpacing;
|
||||
private readonly double halfSpacing;
|
||||
|
||||
public FillExtents(Box workArea, double partSpacing)
|
||||
{
|
||||
this.workArea = workArea;
|
||||
this.partSpacing = partSpacing;
|
||||
halfSpacing = partSpacing / 2;
|
||||
}
|
||||
|
||||
public List<Part> Fill(Drawing drawing, double rotationAngle = 0,
|
||||
int plateNumber = 0,
|
||||
CancellationToken token = default,
|
||||
IProgress<NestProgress> progress = null,
|
||||
List<Engine.BestFit.BestFitResult> bestFits = null)
|
||||
{
|
||||
var pair = BuildPair(drawing, rotationAngle);
|
||||
if (pair == null)
|
||||
return new List<Part>();
|
||||
|
||||
var column = BuildColumn(pair.Value.part1, pair.Value.part2, pair.Value.pairBbox);
|
||||
if (column.Count == 0)
|
||||
return new List<Part>();
|
||||
|
||||
NestEngineBase.ReportProgress(progress, NestPhase.Extents, plateNumber,
|
||||
column, workArea, $"Extents: initial column {column.Count} parts");
|
||||
|
||||
var adjusted = AdjustColumn(pair.Value, column, token);
|
||||
|
||||
NestEngineBase.ReportProgress(progress, NestPhase.Extents, plateNumber,
|
||||
adjusted, workArea, $"Extents: adjusted column {adjusted.Count} parts");
|
||||
|
||||
var result = RepeatColumns(adjusted, token);
|
||||
|
||||
NestEngineBase.ReportProgress(progress, NestPhase.Extents, plateNumber,
|
||||
result, workArea, $"Extents: {result.Count} parts total");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// --- Step 1: Pair Construction ---
|
||||
|
||||
private (Part part1, Part part2, Box pairBbox)? BuildPair(Drawing drawing, double rotationAngle)
|
||||
{
|
||||
var part1 = Part.CreateAtOrigin(drawing, rotationAngle);
|
||||
var part2 = Part.CreateAtOrigin(drawing, rotationAngle + System.Math.PI);
|
||||
|
||||
// Check that each part fits in the work area individually.
|
||||
if (part1.BoundingBox.Width > workArea.Width + Tolerance.Epsilon ||
|
||||
part1.BoundingBox.Length > workArea.Length + Tolerance.Epsilon)
|
||||
return null;
|
||||
|
||||
// Slide part2 toward part1 from the right using geometry-aware distance.
|
||||
var boundary1 = new PartBoundary(part1, halfSpacing);
|
||||
var boundary2 = new PartBoundary(part2, halfSpacing);
|
||||
|
||||
// Position part2 to the right of part1 at bounding box width distance.
|
||||
var startOffset = part1.BoundingBox.Width + part2.BoundingBox.Width + partSpacing;
|
||||
part2.Offset(startOffset, 0);
|
||||
part2.UpdateBounds();
|
||||
|
||||
// Slide part2 left toward part1.
|
||||
var movingLines = boundary2.GetLines(part2.Location, PushDirection.Left);
|
||||
var stationaryLines = boundary1.GetLines(part1.Location, PushDirection.Right);
|
||||
var dist = SpatialQuery.DirectionalDistance(movingLines, stationaryLines, PushDirection.Left);
|
||||
|
||||
if (dist < double.MaxValue && dist > 0)
|
||||
{
|
||||
part2.Offset(-dist, 0);
|
||||
part2.UpdateBounds();
|
||||
}
|
||||
|
||||
// Re-anchor pair to work area origin.
|
||||
var pairBbox = ((IEnumerable<IBoundable>)new IBoundable[] { part1, part2 }).GetBoundingBox();
|
||||
var anchor = new Vector(workArea.X - pairBbox.Left, workArea.Y - pairBbox.Bottom);
|
||||
part1.Offset(anchor);
|
||||
part2.Offset(anchor);
|
||||
part1.UpdateBounds();
|
||||
part2.UpdateBounds();
|
||||
|
||||
pairBbox = ((IEnumerable<IBoundable>)new IBoundable[] { part1, part2 }).GetBoundingBox();
|
||||
|
||||
// Verify pair fits in work area.
|
||||
if (pairBbox.Width > workArea.Width + Tolerance.Epsilon ||
|
||||
pairBbox.Length > workArea.Length + Tolerance.Epsilon)
|
||||
return null;
|
||||
|
||||
return (part1, part2, pairBbox);
|
||||
}
|
||||
|
||||
// --- Step 2: Build Column (tile vertically) ---
|
||||
|
||||
private List<Part> BuildColumn(Part part1, Part part2, Box pairBbox)
|
||||
{
|
||||
var column = new List<Part> { (Part)part1.Clone(), (Part)part2.Clone() };
|
||||
|
||||
// Find geometry-aware copy distance for the pair vertically.
|
||||
var boundary1 = new PartBoundary(part1, halfSpacing);
|
||||
var boundary2 = new PartBoundary(part2, halfSpacing);
|
||||
|
||||
// Compute vertical copy distance using bounding boxes as starting point,
|
||||
// then slide down to find true geometry distance.
|
||||
var pairHeight = pairBbox.Length;
|
||||
var testOffset = new Vector(0, pairHeight);
|
||||
|
||||
// Create test parts for slide distance measurement.
|
||||
var testPart1 = part1.CloneAtOffset(testOffset);
|
||||
var testPart2 = part2.CloneAtOffset(testOffset);
|
||||
|
||||
// Find minimum distance from test pair sliding down toward original pair.
|
||||
var copyDistance = FindVerticalCopyDistance(
|
||||
part1, part2, testPart1, testPart2,
|
||||
boundary1, boundary2, pairHeight);
|
||||
|
||||
if (copyDistance <= 0)
|
||||
return column;
|
||||
|
||||
var count = 1;
|
||||
while (true)
|
||||
{
|
||||
var nextBottom = pairBbox.Bottom + copyDistance * count;
|
||||
if (nextBottom + pairHeight > workArea.Top + Tolerance.Epsilon)
|
||||
break;
|
||||
|
||||
var offset = new Vector(0, copyDistance * count);
|
||||
column.Add(part1.CloneAtOffset(offset));
|
||||
column.Add(part2.CloneAtOffset(offset));
|
||||
count++;
|
||||
}
|
||||
|
||||
return column;
|
||||
}
|
||||
|
||||
private double FindVerticalCopyDistance(
|
||||
Part origPart1, Part origPart2,
|
||||
Part testPart1, Part testPart2,
|
||||
PartBoundary boundary1, PartBoundary boundary2,
|
||||
double pairHeight)
|
||||
{
|
||||
// Check all 4 combinations: test parts sliding down toward original parts.
|
||||
var minSlide = double.MaxValue;
|
||||
|
||||
// Test1 -> Orig1
|
||||
var d = SlideDistance(boundary1, testPart1.Location, boundary1, origPart1.Location, PushDirection.Down);
|
||||
if (d < minSlide) minSlide = d;
|
||||
|
||||
// Test1 -> Orig2
|
||||
d = SlideDistance(boundary1, testPart1.Location, boundary2, origPart2.Location, PushDirection.Down);
|
||||
if (d < minSlide) minSlide = d;
|
||||
|
||||
// Test2 -> Orig1
|
||||
d = SlideDistance(boundary2, testPart2.Location, boundary1, origPart1.Location, PushDirection.Down);
|
||||
if (d < minSlide) minSlide = d;
|
||||
|
||||
// Test2 -> Orig2
|
||||
d = SlideDistance(boundary2, testPart2.Location, boundary2, origPart2.Location, PushDirection.Down);
|
||||
if (d < minSlide) minSlide = d;
|
||||
|
||||
if (minSlide >= double.MaxValue || minSlide < 0)
|
||||
return pairHeight + partSpacing;
|
||||
|
||||
// Match FillLinear.ComputeCopyDistance: copyDist = startOffset - slide,
|
||||
// clamped so it never goes below pairHeight + partSpacing to prevent
|
||||
// bounding-box overlap from spurious slide values.
|
||||
var copyDist = pairHeight - minSlide;
|
||||
|
||||
return System.Math.Max(copyDist, pairHeight + partSpacing);
|
||||
}
|
||||
|
||||
private static double SlideDistance(
|
||||
PartBoundary movingBoundary, Vector movingLocation,
|
||||
PartBoundary stationaryBoundary, Vector stationaryLocation,
|
||||
PushDirection direction)
|
||||
{
|
||||
var opposite = SpatialQuery.OppositeDirection(direction);
|
||||
var movingEdges = movingBoundary.GetEdges(direction);
|
||||
var stationaryEdges = stationaryBoundary.GetEdges(opposite);
|
||||
|
||||
return SpatialQuery.DirectionalDistance(
|
||||
movingEdges, movingLocation,
|
||||
stationaryEdges, stationaryLocation,
|
||||
direction);
|
||||
}
|
||||
|
||||
// --- Step 3: Iterative Adjustment ---
|
||||
|
||||
private List<Part> AdjustColumn(
|
||||
(Part part1, Part part2, Box pairBbox) pair,
|
||||
List<Part> column,
|
||||
CancellationToken token)
|
||||
{
|
||||
var originalPairWidth = pair.pairBbox.Width;
|
||||
|
||||
for (var iteration = 0; iteration < MaxIterations; iteration++)
|
||||
{
|
||||
if (token.IsCancellationRequested)
|
||||
break;
|
||||
|
||||
// Measure current gap.
|
||||
var topEdge = double.MinValue;
|
||||
foreach (var p in column)
|
||||
if (p.BoundingBox.Top > topEdge)
|
||||
topEdge = p.BoundingBox.Top;
|
||||
|
||||
var gap = workArea.Top - topEdge;
|
||||
|
||||
if (gap <= Tolerance.Epsilon)
|
||||
break;
|
||||
|
||||
var pairCount = column.Count / 2;
|
||||
if (pairCount <= 0)
|
||||
break;
|
||||
|
||||
var adjustment = gap / pairCount;
|
||||
if (adjustment <= Tolerance.Epsilon)
|
||||
break;
|
||||
|
||||
// Try adjusting the pair and rebuilding the column.
|
||||
var adjusted = TryAdjustPair(pair, adjustment, originalPairWidth);
|
||||
if (adjusted == null)
|
||||
break;
|
||||
|
||||
var newColumn = BuildColumn(adjusted.Value.part1, adjusted.Value.part2, adjusted.Value.pairBbox);
|
||||
if (newColumn.Count == 0)
|
||||
break;
|
||||
|
||||
column = newColumn;
|
||||
pair = adjusted.Value;
|
||||
}
|
||||
|
||||
return column;
|
||||
}
|
||||
|
||||
private (Part part1, Part part2, Box pairBbox)? TryAdjustPair(
|
||||
(Part part1, Part part2, Box pairBbox) pair,
|
||||
double adjustment, double originalPairWidth)
|
||||
{
|
||||
// Try shifting part2 up first.
|
||||
var result = TryShiftDirection(pair, adjustment, originalPairWidth);
|
||||
|
||||
if (result != null)
|
||||
return result;
|
||||
|
||||
// Up made the pair wider — try down instead.
|
||||
return TryShiftDirection(pair, -adjustment, originalPairWidth);
|
||||
}
|
||||
|
||||
private (Part part1, Part part2, Box pairBbox)? TryShiftDirection(
|
||||
(Part part1, Part part2, Box pairBbox) pair,
|
||||
double verticalShift, double originalPairWidth)
|
||||
{
|
||||
// Clone parts so we don't mutate the originals.
|
||||
var p1 = (Part)pair.part1.Clone();
|
||||
var p2 = (Part)pair.part2.Clone();
|
||||
|
||||
// Separate: shift part2 right so bounding boxes don't touch.
|
||||
p2.Offset(partSpacing, 0);
|
||||
p2.UpdateBounds();
|
||||
|
||||
// Apply the vertical shift.
|
||||
p2.Offset(0, verticalShift);
|
||||
p2.UpdateBounds();
|
||||
|
||||
// Compact part2 left toward part1.
|
||||
var moving = new List<Part> { p2 };
|
||||
var obstacles = new List<Part> { p1 };
|
||||
Compactor.Push(moving, obstacles, workArea, partSpacing, PushDirection.Left);
|
||||
|
||||
// Check if the pair got wider.
|
||||
var newBbox = ((IEnumerable<IBoundable>)new IBoundable[] { p1, p2 }).GetBoundingBox();
|
||||
|
||||
if (newBbox.Width > originalPairWidth + Tolerance.Epsilon)
|
||||
return null;
|
||||
|
||||
// Re-anchor to work area origin.
|
||||
var anchor = new Vector(workArea.X - newBbox.Left, workArea.Y - newBbox.Bottom);
|
||||
p1.Offset(anchor);
|
||||
p2.Offset(anchor);
|
||||
p1.UpdateBounds();
|
||||
p2.UpdateBounds();
|
||||
|
||||
newBbox = ((IEnumerable<IBoundable>)new IBoundable[] { p1, p2 }).GetBoundingBox();
|
||||
return (p1, p2, newBbox);
|
||||
}
|
||||
|
||||
// --- Step 4: Horizontal Repetition ---
|
||||
|
||||
private List<Part> RepeatColumns(List<Part> column, CancellationToken token)
|
||||
{
|
||||
if (column.Count == 0)
|
||||
return column;
|
||||
|
||||
var columnBbox = ((IEnumerable<IBoundable>)column).GetBoundingBox();
|
||||
var columnWidth = columnBbox.Width;
|
||||
|
||||
// Create a test column shifted right by columnWidth + spacing.
|
||||
var testOffset = columnWidth + partSpacing;
|
||||
var testColumn = new List<Part>(column.Count);
|
||||
foreach (var part in column)
|
||||
testColumn.Add(part.CloneAtOffset(new Vector(testOffset, 0)));
|
||||
|
||||
// Compact the test column left against the original column.
|
||||
var distanceMoved = Compactor.Push(testColumn, column, workArea, partSpacing, PushDirection.Left);
|
||||
|
||||
// Derive the true copy distance from where the test column ended up.
|
||||
var testBbox = ((IEnumerable<IBoundable>)testColumn).GetBoundingBox();
|
||||
var copyDistance = testBbox.Left - columnBbox.Left;
|
||||
|
||||
if (copyDistance <= Tolerance.Epsilon)
|
||||
copyDistance = columnWidth + partSpacing;
|
||||
|
||||
Debug.WriteLine($"[FillExtents] Column copy distance: {copyDistance:F2} (bbox width: {columnWidth:F2}, spacing: {partSpacing:F2})");
|
||||
|
||||
// Build all columns.
|
||||
var result = new List<Part>(column);
|
||||
|
||||
// Add the test column we already computed as column 2.
|
||||
foreach (var part in testColumn)
|
||||
{
|
||||
if (IsWithinWorkArea(part))
|
||||
result.Add(part);
|
||||
}
|
||||
|
||||
// Tile additional columns at the copy distance.
|
||||
var colIndex = 2;
|
||||
while (!token.IsCancellationRequested)
|
||||
{
|
||||
var offset = new Vector(copyDistance * colIndex, 0);
|
||||
var anyFit = false;
|
||||
|
||||
foreach (var part in column)
|
||||
{
|
||||
var clone = part.CloneAtOffset(offset);
|
||||
if (IsWithinWorkArea(clone))
|
||||
{
|
||||
result.Add(clone);
|
||||
anyFit = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!anyFit)
|
||||
break;
|
||||
|
||||
colIndex++;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private bool IsWithinWorkArea(Part part)
|
||||
{
|
||||
return part.BoundingBox.Right <= workArea.Right + Tolerance.Epsilon &&
|
||||
part.BoundingBox.Top <= workArea.Top + Tolerance.Epsilon &&
|
||||
part.BoundingBox.Left >= workArea.Left - Tolerance.Epsilon &&
|
||||
part.BoundingBox.Bottom >= workArea.Bottom - Tolerance.Epsilon;
|
||||
}
|
||||
}
|
||||
}
|
||||
630
OpenNest.Engine/Fill/FillLinear.cs
Normal file
630
OpenNest.Engine/Fill/FillLinear.cs
Normal file
@@ -0,0 +1,630 @@
|
||||
using OpenNest.Geometry;
|
||||
using OpenNest.Math;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OpenNest.Engine.Fill
|
||||
{
|
||||
public class FillLinear
|
||||
{
|
||||
public FillLinear(Box workArea, double partSpacing)
|
||||
{
|
||||
PartSpacing = partSpacing;
|
||||
WorkArea = new Box(workArea.X, workArea.Y, workArea.Width, workArea.Length);
|
||||
}
|
||||
|
||||
public Box WorkArea { get; }
|
||||
|
||||
public double PartSpacing { get; }
|
||||
|
||||
public double HalfSpacing => PartSpacing / 2;
|
||||
|
||||
/// <summary>
|
||||
/// Optional multi-part patterns (e.g. interlocking pairs) to try in remainder strips.
|
||||
/// </summary>
|
||||
public List<Pattern> RemainderPatterns { get; set; }
|
||||
|
||||
private static Vector MakeOffset(NestDirection direction, double distance)
|
||||
{
|
||||
return direction == NestDirection.Horizontal
|
||||
? new Vector(distance, 0)
|
||||
: new Vector(0, distance);
|
||||
}
|
||||
|
||||
private static PushDirection GetPushDirection(NestDirection direction)
|
||||
{
|
||||
return direction == NestDirection.Horizontal
|
||||
? PushDirection.Left
|
||||
: PushDirection.Down;
|
||||
}
|
||||
|
||||
private static double GetDimension(Box box, NestDirection direction)
|
||||
{
|
||||
return direction == NestDirection.Horizontal ? box.Width : box.Length;
|
||||
}
|
||||
|
||||
private static double GetStart(Box box, NestDirection direction)
|
||||
{
|
||||
return direction == NestDirection.Horizontal ? box.Left : box.Bottom;
|
||||
}
|
||||
|
||||
private double GetLimit(NestDirection direction)
|
||||
{
|
||||
return direction == NestDirection.Horizontal ? WorkArea.Right : WorkArea.Top;
|
||||
}
|
||||
|
||||
private static NestDirection PerpendicularAxis(NestDirection direction)
|
||||
{
|
||||
return direction == NestDirection.Horizontal
|
||||
? NestDirection.Vertical
|
||||
: NestDirection.Horizontal;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Computes the slide distance for the push algorithm, returning the
|
||||
/// geometry-aware copy distance along the given axis.
|
||||
/// </summary>
|
||||
private double ComputeCopyDistance(double bboxDim, double slideDistance)
|
||||
{
|
||||
if (slideDistance >= double.MaxValue || slideDistance < 0)
|
||||
return bboxDim + PartSpacing;
|
||||
|
||||
// The geometry-aware slide can produce a copy distance smaller than
|
||||
// the part itself when inflated corner/arc vertices interact spuriously.
|
||||
// Clamp to bboxDim + PartSpacing to prevent bounding box overlap.
|
||||
return System.Math.Max(bboxDim - slideDistance, bboxDim + PartSpacing);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds the geometry-aware copy distance between two identical parts along an axis.
|
||||
/// Both parts are inflated by half-spacing for symmetric spacing.
|
||||
/// </summary>
|
||||
private double FindCopyDistance(Part partA, NestDirection direction, PartBoundary boundary)
|
||||
{
|
||||
var bboxDim = GetDimension(partA.BoundingBox, direction);
|
||||
var pushDir = GetPushDirection(direction);
|
||||
|
||||
var locationBOffset = MakeOffset(direction, bboxDim);
|
||||
|
||||
// Use the most efficient array-based overload to avoid all allocations.
|
||||
var slideDistance = SpatialQuery.DirectionalDistance(
|
||||
boundary.GetEdges(pushDir), partA.Location + locationBOffset,
|
||||
boundary.GetEdges(SpatialQuery.OppositeDirection(pushDir)), partA.Location,
|
||||
pushDir);
|
||||
|
||||
return ComputeCopyDistance(bboxDim, slideDistance);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds the geometry-aware copy distance between two identical patterns along an axis.
|
||||
/// Checks every pair of parts across adjacent patterns so that multi-part
|
||||
/// patterns (e.g. interlocking pairs) maintain spacing between ALL parts.
|
||||
/// Both sides are inflated by half-spacing for symmetric spacing.
|
||||
/// </summary>
|
||||
private double FindPatternCopyDistance(Pattern patternA, NestDirection direction, PartBoundary[] boundaries)
|
||||
{
|
||||
if (patternA.Parts.Count <= 1)
|
||||
return FindSinglePartPatternCopyDistance(patternA, direction, boundaries[0]);
|
||||
|
||||
var bboxDim = GetDimension(patternA.BoundingBox, direction);
|
||||
var pushDir = GetPushDirection(direction);
|
||||
var opposite = SpatialQuery.OppositeDirection(pushDir);
|
||||
|
||||
// bboxDim already spans max(upper) - min(lower) across all parts,
|
||||
// so the start offset just needs to push beyond that plus spacing.
|
||||
var startOffset = bboxDim + PartSpacing + Tolerance.Epsilon;
|
||||
var offset = MakeOffset(direction, startOffset);
|
||||
|
||||
var maxCopyDistance = FindMaxPairDistance(
|
||||
patternA.Parts, boundaries, offset, pushDir, opposite, startOffset);
|
||||
|
||||
if (maxCopyDistance < Tolerance.Epsilon)
|
||||
return bboxDim + PartSpacing;
|
||||
|
||||
return maxCopyDistance;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests every pair of parts across adjacent pattern copies and returns the
|
||||
/// maximum copy distance found. Returns 0 if no valid slide was found.
|
||||
/// </summary>
|
||||
private static double FindMaxPairDistance(
|
||||
List<Part> parts, PartBoundary[] boundaries, Vector offset,
|
||||
PushDirection pushDir, PushDirection opposite, double startOffset)
|
||||
{
|
||||
var maxCopyDistance = 0.0;
|
||||
|
||||
for (var j = 0; j < parts.Count; j++)
|
||||
{
|
||||
var movingEdges = boundaries[j].GetEdges(pushDir);
|
||||
var locationB = parts[j].Location + offset;
|
||||
|
||||
for (var i = 0; i < parts.Count; i++)
|
||||
{
|
||||
var slideDistance = SpatialQuery.DirectionalDistance(
|
||||
movingEdges, locationB,
|
||||
boundaries[i].GetEdges(opposite), parts[i].Location,
|
||||
pushDir);
|
||||
|
||||
if (slideDistance >= double.MaxValue || slideDistance < 0)
|
||||
continue;
|
||||
|
||||
var copyDist = startOffset - slideDistance;
|
||||
|
||||
if (copyDist > maxCopyDistance)
|
||||
maxCopyDistance = copyDist;
|
||||
}
|
||||
}
|
||||
|
||||
return maxCopyDistance;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fast path for single-part patterns — no cross-part conflicts possible.
|
||||
/// </summary>
|
||||
private double FindSinglePartPatternCopyDistance(Pattern patternA, NestDirection direction, PartBoundary boundary)
|
||||
{
|
||||
var template = patternA.Parts[0];
|
||||
return FindCopyDistance(template, direction, boundary);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets offset boundary lines for all parts in a pattern using a shared boundary.
|
||||
/// </summary>
|
||||
private static List<Line> GetPatternLines(Pattern pattern, PartBoundary boundary, PushDirection direction)
|
||||
{
|
||||
var lines = new List<Line>();
|
||||
|
||||
foreach (var part in pattern.Parts)
|
||||
lines.AddRange(boundary.GetLines(part.Location, direction));
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets boundary lines for all parts in a pattern, with an additional
|
||||
/// location offset applied. Avoids cloning the pattern.
|
||||
/// </summary>
|
||||
private static List<Line> GetOffsetPatternLines(Pattern pattern, Vector offset, PartBoundary boundary, PushDirection direction)
|
||||
{
|
||||
var lines = new List<Line>();
|
||||
|
||||
foreach (var part in pattern.Parts)
|
||||
lines.AddRange(boundary.GetLines(part.Location + offset, direction));
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates boundaries for all parts in a pattern. Parts that share the same
|
||||
/// program geometry (same drawing and rotation) reuse the same boundary instance.
|
||||
/// </summary>
|
||||
private PartBoundary[] CreateBoundaries(Pattern pattern)
|
||||
{
|
||||
var boundaries = new PartBoundary[pattern.Parts.Count];
|
||||
var cache = new List<(Drawing drawing, double rotation, PartBoundary boundary)>();
|
||||
|
||||
for (var i = 0; i < pattern.Parts.Count; i++)
|
||||
{
|
||||
var part = pattern.Parts[i];
|
||||
PartBoundary found = null;
|
||||
|
||||
foreach (var entry in cache)
|
||||
{
|
||||
if (entry.drawing == part.BaseDrawing && entry.rotation.IsEqualTo(part.Rotation))
|
||||
{
|
||||
found = entry.boundary;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (found == null)
|
||||
{
|
||||
found = new PartBoundary(part, HalfSpacing);
|
||||
cache.Add((part.BaseDrawing, part.Rotation, found));
|
||||
}
|
||||
|
||||
boundaries[i] = found;
|
||||
}
|
||||
|
||||
return boundaries;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tiles a pattern along the given axis, returning the cloned parts
|
||||
/// (does not include the original pattern's parts). For multi-part
|
||||
/// patterns, also adds individual parts from the next incomplete copy
|
||||
/// that still fit within the work area.
|
||||
/// </summary>
|
||||
private List<Part> TilePattern(Pattern basePattern, NestDirection direction, PartBoundary[] boundaries)
|
||||
{
|
||||
var copyDistance = FindPatternCopyDistance(basePattern, direction, boundaries);
|
||||
|
||||
if (copyDistance <= 0)
|
||||
return new List<Part>();
|
||||
|
||||
var dim = GetDimension(basePattern.BoundingBox, direction);
|
||||
var start = GetStart(basePattern.BoundingBox, direction);
|
||||
var limit = GetLimit(direction);
|
||||
|
||||
var estimatedCopies = (int)((limit - start - dim) / copyDistance);
|
||||
var result = new List<Part>(estimatedCopies * basePattern.Parts.Count);
|
||||
|
||||
var count = 1;
|
||||
|
||||
while (true)
|
||||
{
|
||||
var nextPos = start + copyDistance * count;
|
||||
|
||||
if (nextPos + dim > limit + Tolerance.Epsilon)
|
||||
break;
|
||||
|
||||
var offset = MakeOffset(direction, copyDistance * count);
|
||||
|
||||
foreach (var part in basePattern.Parts)
|
||||
result.Add(part.CloneAtOffset(offset));
|
||||
|
||||
count++;
|
||||
}
|
||||
|
||||
// For multi-part patterns, try to place individual parts from the
|
||||
// next copy that didn't fit as a whole. This handles cases where
|
||||
// e.g. a 2-part pair only partially fits — one part may still be
|
||||
// within the work area even though the full pattern exceeds it.
|
||||
if (basePattern.Parts.Count > 1)
|
||||
{
|
||||
var offset = MakeOffset(direction, copyDistance * count);
|
||||
|
||||
foreach (var basePart in basePattern.Parts)
|
||||
{
|
||||
var part = basePart.CloneAtOffset(offset);
|
||||
|
||||
if (part.BoundingBox.Right <= WorkArea.Right + Tolerance.Epsilon &&
|
||||
part.BoundingBox.Top <= WorkArea.Top + Tolerance.Epsilon &&
|
||||
part.BoundingBox.Left >= WorkArea.Left - Tolerance.Epsilon &&
|
||||
part.BoundingBox.Bottom >= WorkArea.Bottom - Tolerance.Epsilon)
|
||||
{
|
||||
result.Add(part);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a seed pattern containing a single part positioned at the work area origin.
|
||||
/// Returns an empty pattern if the part does not fit.
|
||||
/// </summary>
|
||||
private Pattern MakeSeedPattern(Drawing drawing, double rotationAngle)
|
||||
{
|
||||
var pattern = new Pattern();
|
||||
|
||||
var template = new Part(drawing);
|
||||
|
||||
if (!rotationAngle.IsEqualTo(0))
|
||||
template.Rotate(rotationAngle);
|
||||
|
||||
template.Offset(WorkArea.Location - template.BoundingBox.Location);
|
||||
|
||||
if (template.BoundingBox.Width > WorkArea.Width + Tolerance.Epsilon ||
|
||||
template.BoundingBox.Length > WorkArea.Length + Tolerance.Epsilon)
|
||||
return pattern;
|
||||
|
||||
pattern.Parts.Add(template);
|
||||
pattern.UpdateBounds();
|
||||
return pattern;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fills the work area by tiling the pattern along the primary axis to form
|
||||
/// a row, then tiling that row along the perpendicular axis to form a grid.
|
||||
/// After the grid is formed, fills the remaining strip with individual parts.
|
||||
/// </summary>
|
||||
private List<Part> FillGrid(Pattern pattern, NestDirection direction)
|
||||
{
|
||||
var perpAxis = PerpendicularAxis(direction);
|
||||
var boundaries = CreateBoundaries(pattern);
|
||||
|
||||
// Step 1: Tile along primary axis
|
||||
var row = new List<Part>(pattern.Parts);
|
||||
row.AddRange(TilePattern(pattern, direction, boundaries));
|
||||
|
||||
// If primary tiling didn't produce copies, just tile along perpendicular
|
||||
if (row.Count <= pattern.Parts.Count)
|
||||
{
|
||||
row.AddRange(TilePattern(pattern, perpAxis, boundaries));
|
||||
return row;
|
||||
}
|
||||
|
||||
// Step 2: Build row pattern and tile along perpendicular axis
|
||||
var rowPattern = new Pattern();
|
||||
rowPattern.Parts.AddRange(row);
|
||||
rowPattern.UpdateBounds();
|
||||
|
||||
var rowBoundaries = CreateBoundaries(rowPattern);
|
||||
var gridResult = new List<Part>(rowPattern.Parts);
|
||||
gridResult.AddRange(TilePattern(rowPattern, perpAxis, rowBoundaries));
|
||||
|
||||
// Step 3: Fill remaining strip
|
||||
var remaining = FillRemainingStrip(gridResult, pattern, perpAxis, direction);
|
||||
if (remaining.Count > 0)
|
||||
gridResult.AddRange(remaining);
|
||||
|
||||
// Step 4: Try fewer rows optimization
|
||||
var fewerResult = TryFewerRows(gridResult, rowPattern, pattern, perpAxis, direction);
|
||||
if (fewerResult != null && fewerResult.Count > gridResult.Count)
|
||||
return fewerResult;
|
||||
|
||||
return gridResult;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries removing the last row/column from the grid and re-filling the
|
||||
/// larger remainder strip. Returns null if this doesn't improve the total.
|
||||
/// </summary>
|
||||
private List<Part> TryFewerRows(
|
||||
List<Part> fullResult, Pattern rowPattern, Pattern seedPattern,
|
||||
NestDirection tiledAxis, NestDirection primaryAxis)
|
||||
{
|
||||
var rowPartCount = rowPattern.Parts.Count;
|
||||
|
||||
if (fullResult.Count < rowPartCount * 2)
|
||||
return null;
|
||||
|
||||
var fewerParts = new List<Part>(fullResult.Count - rowPartCount);
|
||||
|
||||
for (var i = 0; i < fullResult.Count - rowPartCount; i++)
|
||||
fewerParts.Add(fullResult[i]);
|
||||
|
||||
var remaining = FillRemainingStrip(fewerParts, seedPattern, tiledAxis, primaryAxis);
|
||||
|
||||
if (remaining.Count <= rowPartCount)
|
||||
return null;
|
||||
|
||||
fewerParts.AddRange(remaining);
|
||||
return fewerParts;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// After tiling full rows/columns, fills the remaining strip with individual
|
||||
/// parts. The strip is the leftover space along the tiled axis between the
|
||||
/// last full row/column and the work area boundary. Each unique drawing and
|
||||
/// rotation from the seed pattern is tried in both directions.
|
||||
/// </summary>
|
||||
private List<Part> FillRemainingStrip(
|
||||
List<Part> placedParts, Pattern seedPattern,
|
||||
NestDirection tiledAxis, NestDirection primaryAxis)
|
||||
{
|
||||
var placedEdge = FindPlacedEdge(placedParts, tiledAxis);
|
||||
var remainingStrip = BuildRemainingStrip(placedEdge, tiledAxis);
|
||||
|
||||
if (remainingStrip == null)
|
||||
return new List<Part>();
|
||||
|
||||
var rotations = BuildRotationSet(seedPattern);
|
||||
var best = FindBestFill(rotations, remainingStrip);
|
||||
|
||||
if (RemainderPatterns != null)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"[FillRemainingStrip] Strip: {remainingStrip.Width:F1}x{remainingStrip.Length:F1}, individual best={best?.Count ?? 0}, trying {RemainderPatterns.Count} patterns");
|
||||
|
||||
foreach (var pattern in RemainderPatterns)
|
||||
{
|
||||
var filler = new FillLinear(remainingStrip, PartSpacing);
|
||||
var h = filler.Fill(pattern, NestDirection.Horizontal);
|
||||
var v = filler.Fill(pattern, NestDirection.Vertical);
|
||||
|
||||
System.Diagnostics.Debug.WriteLine($"[FillRemainingStrip] Pattern ({pattern.Parts.Count} parts, bbox={pattern.BoundingBox.Width:F1}x{pattern.BoundingBox.Length:F1}): H={h?.Count ?? 0}, V={v?.Count ?? 0}");
|
||||
|
||||
if (h != null && h.Count > (best?.Count ?? 0))
|
||||
best = h;
|
||||
if (v != null && v.Count > (best?.Count ?? 0))
|
||||
best = v;
|
||||
}
|
||||
|
||||
System.Diagnostics.Debug.WriteLine($"[FillRemainingStrip] Final best={best?.Count ?? 0}");
|
||||
}
|
||||
|
||||
return best ?? new List<Part>();
|
||||
}
|
||||
|
||||
private static double FindPlacedEdge(List<Part> placedParts, NestDirection tiledAxis)
|
||||
{
|
||||
var placedEdge = double.MinValue;
|
||||
|
||||
foreach (var part in placedParts)
|
||||
{
|
||||
var edge = tiledAxis == NestDirection.Vertical
|
||||
? part.BoundingBox.Top
|
||||
: part.BoundingBox.Right;
|
||||
|
||||
if (edge > placedEdge)
|
||||
placedEdge = edge;
|
||||
}
|
||||
|
||||
return placedEdge;
|
||||
}
|
||||
|
||||
private Box BuildRemainingStrip(double placedEdge, NestDirection tiledAxis)
|
||||
{
|
||||
if (tiledAxis == NestDirection.Vertical)
|
||||
{
|
||||
var bottom = placedEdge + PartSpacing;
|
||||
var height = WorkArea.Top - bottom;
|
||||
|
||||
if (height <= Tolerance.Epsilon)
|
||||
return null;
|
||||
|
||||
return new Box(WorkArea.X, bottom, WorkArea.Width, height);
|
||||
}
|
||||
else
|
||||
{
|
||||
var left = placedEdge + PartSpacing;
|
||||
var width = WorkArea.Right - left;
|
||||
|
||||
if (width <= Tolerance.Epsilon)
|
||||
return null;
|
||||
|
||||
return new Box(left, WorkArea.Y, width, WorkArea.Length);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds a set of (drawing, rotation) candidates: cardinal orientations
|
||||
/// (0° and 90°) for each unique drawing, plus any seed pattern rotations
|
||||
/// not already covered.
|
||||
/// </summary>
|
||||
private static List<(Drawing drawing, double rotation)> BuildRotationSet(Pattern seedPattern)
|
||||
{
|
||||
var rotations = new List<(Drawing drawing, double rotation)>();
|
||||
var drawings = new List<Drawing>();
|
||||
|
||||
foreach (var seedPart in seedPattern.Parts)
|
||||
{
|
||||
var found = false;
|
||||
|
||||
foreach (var d in drawings)
|
||||
{
|
||||
if (d == seedPart.BaseDrawing)
|
||||
{
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found)
|
||||
drawings.Add(seedPart.BaseDrawing);
|
||||
}
|
||||
|
||||
foreach (var drawing in drawings)
|
||||
{
|
||||
rotations.Add((drawing, 0));
|
||||
rotations.Add((drawing, Angle.HalfPI));
|
||||
}
|
||||
|
||||
foreach (var seedPart in seedPattern.Parts)
|
||||
{
|
||||
var skip = false;
|
||||
|
||||
foreach (var (d, r) in rotations)
|
||||
{
|
||||
if (d == seedPart.BaseDrawing && r.IsEqualTo(seedPart.Rotation))
|
||||
{
|
||||
skip = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!skip)
|
||||
rotations.Add((seedPart.BaseDrawing, seedPart.Rotation));
|
||||
}
|
||||
|
||||
return rotations;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries all rotation candidates in both directions in parallel, returns the
|
||||
/// fill with the most parts.
|
||||
/// </summary>
|
||||
private List<Part> FindBestFill(List<(Drawing drawing, double rotation)> rotations, Box strip)
|
||||
{
|
||||
var bag = new System.Collections.Concurrent.ConcurrentBag<List<Part>>();
|
||||
|
||||
Parallel.ForEach(rotations, entry =>
|
||||
{
|
||||
var filler = new FillLinear(strip, PartSpacing);
|
||||
var h = filler.Fill(entry.drawing, entry.rotation, NestDirection.Horizontal);
|
||||
var v = filler.Fill(entry.drawing, entry.rotation, NestDirection.Vertical);
|
||||
|
||||
if (h != null && h.Count > 0)
|
||||
bag.Add(h);
|
||||
|
||||
if (v != null && v.Count > 0)
|
||||
bag.Add(v);
|
||||
});
|
||||
|
||||
List<Part> best = null;
|
||||
|
||||
foreach (var candidate in bag)
|
||||
{
|
||||
if (best == null || candidate.Count > best.Count)
|
||||
best = candidate;
|
||||
}
|
||||
|
||||
return best ?? new List<Part>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fills a single row of identical parts along one axis using geometry-aware spacing.
|
||||
/// </summary>
|
||||
public Pattern FillRow(Drawing drawing, double rotationAngle, NestDirection direction)
|
||||
{
|
||||
var seed = MakeSeedPattern(drawing, rotationAngle);
|
||||
|
||||
if (seed.Parts.Count == 0)
|
||||
return seed;
|
||||
|
||||
var template = seed.Parts[0];
|
||||
var boundary = new PartBoundary(template, HalfSpacing);
|
||||
|
||||
var copyDistance = FindCopyDistance(template, direction, boundary);
|
||||
|
||||
if (copyDistance <= 0)
|
||||
return seed;
|
||||
|
||||
var dim = GetDimension(template.BoundingBox, direction);
|
||||
var start = GetStart(template.BoundingBox, direction);
|
||||
var limit = GetLimit(direction);
|
||||
|
||||
var count = 1;
|
||||
|
||||
while (true)
|
||||
{
|
||||
var nextPos = start + copyDistance * count;
|
||||
|
||||
if (nextPos + dim > limit + Tolerance.Epsilon)
|
||||
break;
|
||||
|
||||
var clone = template.CloneAtOffset(MakeOffset(direction, copyDistance * count));
|
||||
seed.Parts.Add(clone);
|
||||
count++;
|
||||
}
|
||||
|
||||
seed.UpdateBounds();
|
||||
return seed;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fills the work area by tiling a pre-built pattern along both axes.
|
||||
/// </summary>
|
||||
public List<Part> Fill(Pattern pattern, NestDirection primaryAxis)
|
||||
{
|
||||
if (pattern.Parts.Count == 0)
|
||||
return new List<Part>();
|
||||
|
||||
var offset = WorkArea.Location - pattern.BoundingBox.Location;
|
||||
var basePattern = pattern.Clone(offset);
|
||||
|
||||
if (basePattern.BoundingBox.Width > WorkArea.Width + Tolerance.Epsilon ||
|
||||
basePattern.BoundingBox.Length > WorkArea.Length + Tolerance.Epsilon)
|
||||
return new List<Part>();
|
||||
|
||||
return FillGrid(basePattern, primaryAxis);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fills the work area by creating a seed part, then recursively tiling
|
||||
/// along the primary axis and then the perpendicular axis.
|
||||
/// </summary>
|
||||
public List<Part> Fill(Drawing drawing, double rotationAngle, NestDirection primaryAxis)
|
||||
{
|
||||
var seed = MakeSeedPattern(drawing, rotationAngle);
|
||||
|
||||
if (seed.Parts.Count == 0)
|
||||
return new List<Part>();
|
||||
|
||||
return FillGrid(seed, primaryAxis);
|
||||
}
|
||||
}
|
||||
}
|
||||
70
OpenNest.Engine/Fill/FillScore.cs
Normal file
70
OpenNest.Engine/Fill/FillScore.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using OpenNest.Geometry;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace OpenNest.Engine.Fill
|
||||
{
|
||||
public readonly struct FillScore : System.IComparable<FillScore>
|
||||
{
|
||||
public int Count { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Total part area / bounding box area of all placed parts.
|
||||
/// </summary>
|
||||
public double Density { get; }
|
||||
|
||||
public FillScore(int count, double density)
|
||||
{
|
||||
Count = count;
|
||||
Density = density;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Computes a fill score from placed parts and the work area they were placed in.
|
||||
/// </summary>
|
||||
public static FillScore Compute(List<Part> parts, Box workArea)
|
||||
{
|
||||
if (parts == null || parts.Count == 0)
|
||||
return default;
|
||||
|
||||
var totalPartArea = 0.0;
|
||||
var minX = double.MaxValue;
|
||||
var minY = double.MaxValue;
|
||||
var maxX = double.MinValue;
|
||||
var maxY = double.MinValue;
|
||||
|
||||
foreach (var part in parts)
|
||||
{
|
||||
totalPartArea += part.BaseDrawing.Area;
|
||||
var bb = part.BoundingBox;
|
||||
|
||||
if (bb.Left < minX) minX = bb.Left;
|
||||
if (bb.Bottom < minY) minY = bb.Bottom;
|
||||
if (bb.Right > maxX) maxX = bb.Right;
|
||||
if (bb.Top > maxY) maxY = bb.Top;
|
||||
}
|
||||
|
||||
var bboxArea = (maxX - minX) * (maxY - minY);
|
||||
var density = bboxArea > 0 ? totalPartArea / bboxArea : 0;
|
||||
|
||||
return new FillScore(parts.Count, density);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lexicographic comparison: count, then density.
|
||||
/// </summary>
|
||||
public int CompareTo(FillScore other)
|
||||
{
|
||||
var c = Count.CompareTo(other.Count);
|
||||
|
||||
if (c != 0)
|
||||
return c;
|
||||
|
||||
return Density.CompareTo(other.Density);
|
||||
}
|
||||
|
||||
public static bool operator >(FillScore a, FillScore b) => a.CompareTo(b) > 0;
|
||||
public static bool operator <(FillScore a, FillScore b) => a.CompareTo(b) < 0;
|
||||
public static bool operator >=(FillScore a, FillScore b) => a.CompareTo(b) >= 0;
|
||||
public static bool operator <=(FillScore a, FillScore b) => a.CompareTo(b) <= 0;
|
||||
}
|
||||
}
|
||||
145
OpenNest.Engine/Fill/PairFiller.cs
Normal file
145
OpenNest.Engine/Fill/PairFiller.cs
Normal file
@@ -0,0 +1,145 @@
|
||||
using OpenNest.Engine.BestFit;
|
||||
using OpenNest.Engine.Strategies;
|
||||
using OpenNest.Geometry;
|
||||
using OpenNest.Math;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
|
||||
namespace OpenNest.Engine.Fill
|
||||
{
|
||||
/// <summary>
|
||||
/// Fills a work area using interlocking part pairs from BestFitCache.
|
||||
/// </summary>
|
||||
public class PairFiller
|
||||
{
|
||||
private const int MaxTopCandidates = 50;
|
||||
private const int MaxStripCandidates = 100;
|
||||
private const double MinStripUtilization = 0.3;
|
||||
private const int EarlyExitMinTried = 10;
|
||||
private const int EarlyExitStaleLimit = 10;
|
||||
|
||||
private readonly Size plateSize;
|
||||
private readonly double partSpacing;
|
||||
|
||||
/// <summary>
|
||||
/// The best-fit results computed during the last Fill call.
|
||||
/// Available after Fill returns so callers can reuse without recomputing.
|
||||
/// </summary>
|
||||
public List<BestFitResult> BestFits { get; private set; }
|
||||
|
||||
public PairFiller(Size plateSize, double partSpacing)
|
||||
{
|
||||
this.plateSize = plateSize;
|
||||
this.partSpacing = partSpacing;
|
||||
}
|
||||
|
||||
public List<Part> Fill(NestItem item, Box workArea,
|
||||
int plateNumber = 0,
|
||||
CancellationToken token = default,
|
||||
IProgress<NestProgress> progress = null)
|
||||
{
|
||||
BestFits = BestFitCache.GetOrCompute(
|
||||
item.Drawing, plateSize.Length, plateSize.Width, partSpacing);
|
||||
|
||||
var candidates = SelectPairCandidates(BestFits, workArea);
|
||||
Debug.WriteLine($"[PairFiller] Total: {BestFits.Count}, Kept: {BestFits.Count(r => r.Keep)}, Trying: {candidates.Count}");
|
||||
Debug.WriteLine($"[PairFiller] Plate: {plateSize.Length:F2}x{plateSize.Width:F2}, WorkArea: {workArea.Width:F2}x{workArea.Length:F2}");
|
||||
|
||||
List<Part> best = null;
|
||||
var bestScore = default(FillScore);
|
||||
var sinceImproved = 0;
|
||||
|
||||
try
|
||||
{
|
||||
for (var i = 0; i < candidates.Count; i++)
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
var filled = EvaluateCandidate(candidates[i], item.Drawing, workArea);
|
||||
|
||||
if (filled != null && filled.Count > 0)
|
||||
{
|
||||
var score = FillScore.Compute(filled, workArea);
|
||||
if (best == null || score > bestScore)
|
||||
{
|
||||
best = filled;
|
||||
bestScore = score;
|
||||
sinceImproved = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
sinceImproved++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sinceImproved++;
|
||||
}
|
||||
|
||||
NestEngineBase.ReportProgress(progress, NestPhase.Pairs, plateNumber, best, workArea,
|
||||
$"Pairs: {i + 1}/{candidates.Count} candidates, best = {bestScore.Count} parts");
|
||||
|
||||
if (i + 1 >= EarlyExitMinTried && sinceImproved >= EarlyExitStaleLimit)
|
||||
{
|
||||
Debug.WriteLine($"[PairFiller] Early exit at {i + 1}/{candidates.Count} — no improvement in last {sinceImproved} candidates");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
Debug.WriteLine("[PairFiller] Cancelled mid-phase, using results so far");
|
||||
}
|
||||
|
||||
Debug.WriteLine($"[PairFiller] Best pair result: {bestScore.Count} parts, density={bestScore.Density:P1}");
|
||||
return best ?? new List<Part>();
|
||||
}
|
||||
|
||||
private List<Part> EvaluateCandidate(BestFitResult candidate, Drawing drawing, Box workArea)
|
||||
{
|
||||
var pairParts = candidate.BuildParts(drawing);
|
||||
var engine = new FillLinear(workArea, partSpacing);
|
||||
|
||||
var p0 = FillHelpers.BuildRotatedPattern(pairParts, 0);
|
||||
var p90 = FillHelpers.BuildRotatedPattern(pairParts, Angle.HalfPI);
|
||||
engine.RemainderPatterns = new List<Pattern> { p0, p90 };
|
||||
|
||||
return FillHelpers.FillPattern(engine, pairParts, candidate.HullAngles, workArea);
|
||||
}
|
||||
|
||||
private List<BestFitResult> SelectPairCandidates(List<BestFitResult> bestFits, Box workArea)
|
||||
{
|
||||
var kept = bestFits.Where(r => r.Keep).ToList();
|
||||
var top = kept.Take(MaxTopCandidates).ToList();
|
||||
|
||||
var workShortSide = System.Math.Min(workArea.Width, workArea.Length);
|
||||
var plateShortSide = System.Math.Min(plateSize.Width, plateSize.Length);
|
||||
|
||||
if (workShortSide < plateShortSide * 0.5)
|
||||
{
|
||||
var stripCandidates = bestFits
|
||||
.Where(r => r.ShortestSide <= workShortSide + Tolerance.Epsilon
|
||||
&& r.Utilization >= MinStripUtilization)
|
||||
.OrderByDescending(r => r.Utilization);
|
||||
|
||||
var existing = new HashSet<BestFitResult>(top);
|
||||
|
||||
foreach (var r in stripCandidates)
|
||||
{
|
||||
if (top.Count >= MaxStripCandidates)
|
||||
break;
|
||||
|
||||
if (existing.Add(r))
|
||||
top.Add(r);
|
||||
}
|
||||
|
||||
Debug.WriteLine($"[PairFiller] Strip mode: {top.Count} candidates (shortSide <= {workShortSide:F1})");
|
||||
}
|
||||
|
||||
return top;
|
||||
}
|
||||
}
|
||||
}
|
||||
165
OpenNest.Engine/Fill/PartBoundary.cs
Normal file
165
OpenNest.Engine/Fill/PartBoundary.cs
Normal file
@@ -0,0 +1,165 @@
|
||||
using OpenNest.Converters;
|
||||
using OpenNest.Geometry;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace OpenNest.Engine.Fill
|
||||
{
|
||||
/// <summary>
|
||||
/// Pre-computed offset boundary polygons for a part's geometry.
|
||||
/// Polygons are stored at program-local origin (no location applied)
|
||||
/// and can be efficiently translated to any location when extracting lines.
|
||||
/// Directional edge filtering is pre-computed once in the constructor.
|
||||
/// </summary>
|
||||
public class PartBoundary
|
||||
{
|
||||
private const double PolygonTolerance = 0.01;
|
||||
|
||||
private readonly List<Polygon> _polygons;
|
||||
private readonly (Vector start, Vector end)[] _leftEdges;
|
||||
private readonly (Vector start, Vector end)[] _rightEdges;
|
||||
private readonly (Vector start, Vector end)[] _upEdges;
|
||||
private readonly (Vector start, Vector end)[] _downEdges;
|
||||
|
||||
public PartBoundary(Part part, double spacing)
|
||||
{
|
||||
var entities = ConvertProgram.ToGeometry(part.Program)
|
||||
.Where(e => e.Layer != SpecialLayers.Rapid)
|
||||
.ToList();
|
||||
|
||||
var definedShape = new ShapeProfile(entities);
|
||||
var perimeter = definedShape.Perimeter;
|
||||
_polygons = new List<Polygon>();
|
||||
|
||||
if (perimeter != null)
|
||||
{
|
||||
var offsetEntity = perimeter.OffsetEntity(spacing, OffsetSide.Left) as Shape;
|
||||
|
||||
if (offsetEntity != null)
|
||||
{
|
||||
// Circumscribe arcs so polygon vertices are always outside
|
||||
// the true arc — guarantees the boundary never under-estimates.
|
||||
var polygon = offsetEntity.ToPolygonWithTolerance(PolygonTolerance, circumscribe: true);
|
||||
polygon.RemoveSelfIntersections();
|
||||
_polygons.Add(polygon);
|
||||
}
|
||||
}
|
||||
|
||||
PrecomputeDirectionalEdges(
|
||||
out _leftEdges, out _rightEdges, out _upEdges, out _downEdges);
|
||||
}
|
||||
|
||||
private void PrecomputeDirectionalEdges(
|
||||
out (Vector start, Vector end)[] leftEdges,
|
||||
out (Vector start, Vector end)[] rightEdges,
|
||||
out (Vector start, Vector end)[] upEdges,
|
||||
out (Vector start, Vector end)[] downEdges)
|
||||
{
|
||||
var left = new List<(Vector, Vector)>();
|
||||
var right = new List<(Vector, Vector)>();
|
||||
var up = new List<(Vector, Vector)>();
|
||||
var down = new List<(Vector, Vector)>();
|
||||
|
||||
foreach (var polygon in _polygons)
|
||||
{
|
||||
var verts = polygon.Vertices;
|
||||
|
||||
if (verts.Count < 3)
|
||||
{
|
||||
for (var i = 1; i < verts.Count; i++)
|
||||
{
|
||||
var edge = (verts[i - 1], verts[i]);
|
||||
left.Add(edge);
|
||||
right.Add(edge);
|
||||
up.Add(edge);
|
||||
down.Add(edge);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
var sign = polygon.RotationDirection() == RotationType.CCW ? 1.0 : -1.0;
|
||||
|
||||
for (var i = 1; i < verts.Count; i++)
|
||||
{
|
||||
var dx = verts[i].X - verts[i - 1].X;
|
||||
var dy = verts[i].Y - verts[i - 1].Y;
|
||||
var edge = (verts[i - 1], verts[i]);
|
||||
|
||||
if (-sign * dy > 0) left.Add(edge);
|
||||
if (sign * dy > 0) right.Add(edge);
|
||||
if (-sign * dx > 0) up.Add(edge);
|
||||
if (sign * dx > 0) down.Add(edge);
|
||||
}
|
||||
}
|
||||
|
||||
leftEdges = left.OrderBy(e => System.Math.Min(e.Item1.Y, e.Item2.Y)).ToArray();
|
||||
rightEdges = right.OrderBy(e => System.Math.Min(e.Item1.Y, e.Item2.Y)).ToArray();
|
||||
upEdges = up.OrderBy(e => System.Math.Min(e.Item1.X, e.Item2.X)).ToArray();
|
||||
downEdges = down.OrderBy(e => System.Math.Min(e.Item1.X, e.Item2.X)).ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns offset boundary lines translated to the given location,
|
||||
/// filtered to edges whose outward normal faces the specified direction.
|
||||
/// </summary>
|
||||
public List<Line> GetLines(Vector location, PushDirection facingDirection)
|
||||
{
|
||||
var edges = GetDirectionalEdges(facingDirection);
|
||||
var lines = new List<Line>(edges.Length);
|
||||
|
||||
foreach (var (start, end) in edges)
|
||||
lines.Add(new Line(start.Offset(location), end.Offset(location)));
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns all offset boundary lines translated to the given location.
|
||||
/// </summary>
|
||||
public List<Line> GetLines(Vector location)
|
||||
{
|
||||
var lines = new List<Line>();
|
||||
|
||||
foreach (var polygon in _polygons)
|
||||
{
|
||||
var verts = polygon.Vertices;
|
||||
|
||||
if (verts.Count < 2)
|
||||
continue;
|
||||
|
||||
var last = verts[0].Offset(location);
|
||||
|
||||
for (var i = 1; i < verts.Count; i++)
|
||||
{
|
||||
var current = verts[i].Offset(location);
|
||||
lines.Add(new Line(last, current));
|
||||
last = current;
|
||||
}
|
||||
}
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
private (Vector start, Vector end)[] GetDirectionalEdges(PushDirection direction)
|
||||
{
|
||||
switch (direction)
|
||||
{
|
||||
case PushDirection.Left: return _leftEdges;
|
||||
case PushDirection.Right: return _rightEdges;
|
||||
case PushDirection.Up: return _upEdges;
|
||||
case PushDirection.Down: return _downEdges;
|
||||
default: return _leftEdges;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the pre-computed edge arrays for the given direction.
|
||||
/// These are in part-local coordinates (no translation applied).
|
||||
/// </summary>
|
||||
public (Vector start, Vector end)[] GetEdges(PushDirection direction)
|
||||
{
|
||||
return GetDirectionalEdges(direction);
|
||||
}
|
||||
}
|
||||
}
|
||||
33
OpenNest.Engine/Fill/Pattern.cs
Normal file
33
OpenNest.Engine/Fill/Pattern.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using OpenNest.Geometry;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace OpenNest.Engine.Fill
|
||||
{
|
||||
public class Pattern
|
||||
{
|
||||
public Pattern()
|
||||
{
|
||||
Parts = new List<Part>();
|
||||
}
|
||||
|
||||
public List<Part> Parts { get; }
|
||||
|
||||
public Box BoundingBox { get; private set; }
|
||||
|
||||
public void UpdateBounds()
|
||||
{
|
||||
BoundingBox = Parts.GetBoundingBox();
|
||||
}
|
||||
|
||||
public Pattern Clone(Vector offset)
|
||||
{
|
||||
var pattern = new Pattern();
|
||||
|
||||
foreach (var part in Parts)
|
||||
pattern.Parts.Add(part.CloneAtOffset(offset));
|
||||
|
||||
pattern.UpdateBounds();
|
||||
return pattern;
|
||||
}
|
||||
}
|
||||
}
|
||||
52
OpenNest.Engine/Fill/PatternTiler.cs
Normal file
52
OpenNest.Engine/Fill/PatternTiler.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using OpenNest.Geometry;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace OpenNest.Engine.Fill
|
||||
{
|
||||
public static class PatternTiler
|
||||
{
|
||||
public static List<Part> Tile(List<Part> cell, Size plateSize, double partSpacing)
|
||||
{
|
||||
if (cell == null || cell.Count == 0)
|
||||
return new List<Part>();
|
||||
|
||||
var cellBox = cell.GetBoundingBox();
|
||||
var halfSpacing = partSpacing / 2;
|
||||
|
||||
var cellWidth = cellBox.Width + partSpacing;
|
||||
var cellHeight = cellBox.Length + partSpacing;
|
||||
|
||||
if (cellWidth <= 0 || cellHeight <= 0)
|
||||
return new List<Part>();
|
||||
|
||||
// Size.Width = X-axis, Size.Length = Y-axis
|
||||
var cols = (int)System.Math.Floor(plateSize.Width / cellWidth);
|
||||
var rows = (int)System.Math.Floor(plateSize.Length / cellHeight);
|
||||
|
||||
if (cols <= 0 || rows <= 0)
|
||||
return new List<Part>();
|
||||
|
||||
// Shift cell so parts start at halfSpacing inset, ensuring symmetric
|
||||
// spacing between adjacent tiled cells on all sides.
|
||||
var cellOrigin = cellBox.Location;
|
||||
var baseOffset = new Vector(halfSpacing - cellOrigin.X, halfSpacing - cellOrigin.Y);
|
||||
|
||||
var result = new List<Part>(cols * rows * cell.Count);
|
||||
|
||||
for (var row = 0; row < rows; row++)
|
||||
{
|
||||
for (var col = 0; col < cols; col++)
|
||||
{
|
||||
var tileOffset = baseOffset + new Vector(col * cellWidth, row * cellHeight);
|
||||
|
||||
foreach (var part in cell)
|
||||
{
|
||||
result.Add(part.CloneAtOffset(tileOffset));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
112
OpenNest.Engine/Fill/RemnantFiller.cs
Normal file
112
OpenNest.Engine/Fill/RemnantFiller.cs
Normal file
@@ -0,0 +1,112 @@
|
||||
using OpenNest.Geometry;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
|
||||
namespace OpenNest.Engine.Fill
|
||||
{
|
||||
/// <summary>
|
||||
/// Iteratively fills remnant boxes with items using a RemnantFinder.
|
||||
/// After each fill, re-discovers free rectangles and tries again
|
||||
/// until no more items can be placed.
|
||||
/// </summary>
|
||||
public class RemnantFiller
|
||||
{
|
||||
private readonly RemnantFinder finder;
|
||||
private readonly double spacing;
|
||||
|
||||
public RemnantFiller(Box workArea, double spacing)
|
||||
{
|
||||
this.spacing = spacing;
|
||||
finder = new RemnantFinder(workArea);
|
||||
}
|
||||
|
||||
public void AddObstacles(IEnumerable<Part> parts)
|
||||
{
|
||||
foreach (var part in parts)
|
||||
finder.AddObstacle(part.BoundingBox.Offset(spacing));
|
||||
}
|
||||
|
||||
public List<Part> FillItems(
|
||||
List<NestItem> items,
|
||||
Func<NestItem, Box, List<Part>> fillFunc,
|
||||
CancellationToken token = default,
|
||||
IProgress<NestProgress> progress = null)
|
||||
{
|
||||
if (items == null || items.Count == 0)
|
||||
return new List<Part>();
|
||||
|
||||
var allParts = new List<Part>();
|
||||
var madeProgress = true;
|
||||
|
||||
// Track quantities locally — do not mutate the input NestItem objects.
|
||||
var localQty = new Dictionary<string, int>();
|
||||
foreach (var item in items)
|
||||
localQty[item.Drawing.Name] = item.Quantity;
|
||||
|
||||
while (madeProgress && !token.IsCancellationRequested)
|
||||
{
|
||||
madeProgress = false;
|
||||
|
||||
var minRemnantDim = double.MaxValue;
|
||||
foreach (var item in items)
|
||||
{
|
||||
var qty = localQty[item.Drawing.Name];
|
||||
if (qty <= 0)
|
||||
continue;
|
||||
var bb = item.Drawing.Program.BoundingBox();
|
||||
var dim = System.Math.Min(bb.Width, bb.Length);
|
||||
if (dim < minRemnantDim)
|
||||
minRemnantDim = dim;
|
||||
}
|
||||
|
||||
if (minRemnantDim == double.MaxValue)
|
||||
break;
|
||||
|
||||
var freeBoxes = finder.FindRemnants(minRemnantDim);
|
||||
|
||||
if (freeBoxes.Count == 0)
|
||||
break;
|
||||
|
||||
foreach (var item in items)
|
||||
{
|
||||
if (token.IsCancellationRequested)
|
||||
break;
|
||||
|
||||
var qty = localQty[item.Drawing.Name];
|
||||
if (qty == 0)
|
||||
continue;
|
||||
|
||||
var itemBbox = item.Drawing.Program.BoundingBox();
|
||||
var minItemDim = System.Math.Min(itemBbox.Width, itemBbox.Length);
|
||||
|
||||
foreach (var box in freeBoxes)
|
||||
{
|
||||
if (System.Math.Min(box.Width, box.Length) < minItemDim)
|
||||
continue;
|
||||
|
||||
var fillItem = new NestItem { Drawing = item.Drawing, Quantity = qty };
|
||||
var remnantParts = fillFunc(fillItem, box);
|
||||
|
||||
if (remnantParts != null && remnantParts.Count > 0)
|
||||
{
|
||||
allParts.AddRange(remnantParts);
|
||||
localQty[item.Drawing.Name] = System.Math.Max(0, qty - remnantParts.Count);
|
||||
|
||||
foreach (var p in remnantParts)
|
||||
finder.AddObstacle(p.BoundingBox.Offset(spacing));
|
||||
|
||||
madeProgress = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (madeProgress)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return allParts;
|
||||
}
|
||||
}
|
||||
}
|
||||
396
OpenNest.Engine/Fill/RemnantFinder.cs
Normal file
396
OpenNest.Engine/Fill/RemnantFinder.cs
Normal file
@@ -0,0 +1,396 @@
|
||||
using OpenNest.Geometry;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace OpenNest.Engine.Fill
|
||||
{
|
||||
/// <summary>
|
||||
/// A remnant box with a priority tier.
|
||||
/// 0 = within the used envelope (best), 1 = extends past one edge, 2 = fully outside.
|
||||
/// </summary>
|
||||
public struct TieredRemnant
|
||||
{
|
||||
public Box Box;
|
||||
public int Priority;
|
||||
|
||||
public TieredRemnant(Box box, int priority)
|
||||
{
|
||||
Box = box;
|
||||
Priority = priority;
|
||||
}
|
||||
}
|
||||
|
||||
public class RemnantFinder
|
||||
{
|
||||
private readonly Box workArea;
|
||||
|
||||
public List<Box> Obstacles { get; } = new();
|
||||
|
||||
private struct CellGrid
|
||||
{
|
||||
public bool[,] Empty;
|
||||
public List<double> XCoords;
|
||||
public List<double> YCoords;
|
||||
public int Rows;
|
||||
public int Cols;
|
||||
}
|
||||
|
||||
public RemnantFinder(Box workArea, List<Box> obstacles = null)
|
||||
{
|
||||
this.workArea = workArea;
|
||||
|
||||
if (obstacles != null)
|
||||
Obstacles.AddRange(obstacles);
|
||||
}
|
||||
|
||||
public void AddObstacle(Box obstacle) => Obstacles.Add(obstacle);
|
||||
|
||||
public void AddObstacles(IEnumerable<Box> obstacles) => Obstacles.AddRange(obstacles);
|
||||
|
||||
public void ClearObstacles() => Obstacles.Clear();
|
||||
|
||||
public List<Box> FindRemnants(double minDimension = 0)
|
||||
{
|
||||
var grid = BuildGrid();
|
||||
|
||||
if (grid.Rows <= 0 || grid.Cols <= 0)
|
||||
return new List<Box>();
|
||||
|
||||
var merged = MergeCells(grid);
|
||||
var sized = FilterBySize(merged, minDimension);
|
||||
var unique = RemoveDominated(sized);
|
||||
SortByEdgeProximity(unique);
|
||||
return unique;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds remnants and splits them into priority tiers based on the
|
||||
/// bounding box of all placed parts (the "used envelope").
|
||||
/// Priority 0: fully within the used envelope — compact, preferred.
|
||||
/// Priority 1: extends past one edge of the envelope.
|
||||
/// Priority 2: fully outside the envelope — last resort.
|
||||
/// </summary>
|
||||
public List<TieredRemnant> FindTieredRemnants(double minDimension = 0)
|
||||
{
|
||||
var remnants = FindRemnants(minDimension);
|
||||
|
||||
if (Obstacles.Count == 0 || remnants.Count == 0)
|
||||
return remnants.Select(r => new TieredRemnant(r, 0)).ToList();
|
||||
|
||||
var envelope = ComputeEnvelope();
|
||||
var results = new List<TieredRemnant>();
|
||||
|
||||
foreach (var remnant in remnants)
|
||||
{
|
||||
var before = results.Count;
|
||||
SplitAtEnvelope(remnant, envelope, minDimension, results);
|
||||
|
||||
// If all splits fell below minDim, keep the original unsplit.
|
||||
if (results.Count == before)
|
||||
results.Add(new TieredRemnant(remnant, 1));
|
||||
}
|
||||
|
||||
results.Sort((a, b) =>
|
||||
{
|
||||
if (a.Priority != b.Priority)
|
||||
return a.Priority.CompareTo(b.Priority);
|
||||
return b.Box.Area().CompareTo(a.Box.Area());
|
||||
});
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
public static RemnantFinder FromPlate(Plate plate)
|
||||
{
|
||||
var obstacles = new List<Box>(plate.Parts.Count);
|
||||
|
||||
foreach (var part in plate.Parts)
|
||||
obstacles.Add(part.BoundingBox.Offset(plate.PartSpacing));
|
||||
|
||||
return new RemnantFinder(plate.WorkArea(), obstacles);
|
||||
}
|
||||
|
||||
private CellGrid BuildGrid()
|
||||
{
|
||||
var clipped = ClipObstacles();
|
||||
|
||||
var xs = new SortedSet<double> { workArea.Left, workArea.Right };
|
||||
var ys = new SortedSet<double> { workArea.Bottom, workArea.Top };
|
||||
|
||||
foreach (var obs in clipped)
|
||||
{
|
||||
xs.Add(obs.Left);
|
||||
xs.Add(obs.Right);
|
||||
ys.Add(obs.Bottom);
|
||||
ys.Add(obs.Top);
|
||||
}
|
||||
|
||||
var grid = new CellGrid
|
||||
{
|
||||
XCoords = xs.ToList(),
|
||||
YCoords = ys.ToList(),
|
||||
};
|
||||
|
||||
grid.Cols = grid.XCoords.Count - 1;
|
||||
grid.Rows = grid.YCoords.Count - 1;
|
||||
|
||||
if (grid.Cols <= 0 || grid.Rows <= 0)
|
||||
{
|
||||
grid.Empty = new bool[0, 0];
|
||||
return grid;
|
||||
}
|
||||
|
||||
grid.Empty = new bool[grid.Rows, grid.Cols];
|
||||
|
||||
for (var r = 0; r < grid.Rows; r++)
|
||||
{
|
||||
for (var c = 0; c < grid.Cols; c++)
|
||||
{
|
||||
var cell = new Box(grid.XCoords[c], grid.YCoords[r],
|
||||
grid.XCoords[c + 1] - grid.XCoords[c],
|
||||
grid.YCoords[r + 1] - grid.YCoords[r]);
|
||||
|
||||
grid.Empty[r, c] = !OverlapsAny(cell, clipped);
|
||||
}
|
||||
}
|
||||
|
||||
return grid;
|
||||
}
|
||||
|
||||
private List<Box> ClipObstacles()
|
||||
{
|
||||
var clipped = new List<Box>(Obstacles.Count);
|
||||
|
||||
foreach (var obs in Obstacles)
|
||||
{
|
||||
var c = ClipToWorkArea(obs);
|
||||
if (c.Width > 0 && c.Length > 0)
|
||||
clipped.Add(c);
|
||||
}
|
||||
|
||||
return clipped;
|
||||
}
|
||||
|
||||
private static bool OverlapsAny(Box cell, List<Box> obstacles)
|
||||
{
|
||||
foreach (var obs in obstacles)
|
||||
{
|
||||
if (cell.Left < obs.Right && cell.Right > obs.Left &&
|
||||
cell.Bottom < obs.Top && cell.Top > obs.Bottom)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static List<Box> FilterBySize(List<Box> boxes, double minDimension)
|
||||
{
|
||||
if (minDimension <= 0)
|
||||
return boxes;
|
||||
|
||||
var result = new List<Box>();
|
||||
|
||||
foreach (var box in boxes)
|
||||
{
|
||||
if (box.Width >= minDimension && box.Length >= minDimension)
|
||||
result.Add(box);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static List<Box> RemoveDominated(List<Box> boxes)
|
||||
{
|
||||
boxes.Sort((a, b) => b.Area().CompareTo(a.Area()));
|
||||
var results = new List<Box>();
|
||||
|
||||
foreach (var box in boxes)
|
||||
{
|
||||
var dominated = false;
|
||||
|
||||
foreach (var larger in results)
|
||||
{
|
||||
if (IsContainedIn(box, larger))
|
||||
{
|
||||
dominated = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!dominated)
|
||||
results.Add(box);
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
private static bool IsContainedIn(Box inner, Box outer)
|
||||
{
|
||||
var eps = Math.Tolerance.Epsilon;
|
||||
return inner.Left >= outer.Left - eps &&
|
||||
inner.Right <= outer.Right + eps &&
|
||||
inner.Bottom >= outer.Bottom - eps &&
|
||||
inner.Top <= outer.Top + eps;
|
||||
}
|
||||
|
||||
private void SortByEdgeProximity(List<Box> boxes)
|
||||
{
|
||||
boxes.Sort((a, b) =>
|
||||
{
|
||||
var aEdge = TouchesEdge(a) ? 1 : 0;
|
||||
var bEdge = TouchesEdge(b) ? 1 : 0;
|
||||
|
||||
if (aEdge != bEdge)
|
||||
return bEdge.CompareTo(aEdge);
|
||||
|
||||
return b.Area().CompareTo(a.Area());
|
||||
});
|
||||
}
|
||||
|
||||
private bool TouchesEdge(Box box)
|
||||
{
|
||||
return box.Left <= workArea.Left + Math.Tolerance.Epsilon
|
||||
|| box.Right >= workArea.Right - Math.Tolerance.Epsilon
|
||||
|| box.Bottom <= workArea.Bottom + Math.Tolerance.Epsilon
|
||||
|| box.Top >= workArea.Top - Math.Tolerance.Epsilon;
|
||||
}
|
||||
|
||||
private Box ComputeEnvelope()
|
||||
{
|
||||
var envLeft = double.MaxValue;
|
||||
var envBottom = double.MaxValue;
|
||||
var envRight = double.MinValue;
|
||||
var envTop = double.MinValue;
|
||||
|
||||
foreach (var obs in Obstacles)
|
||||
{
|
||||
if (obs.Left < envLeft) envLeft = obs.Left;
|
||||
if (obs.Bottom < envBottom) envBottom = obs.Bottom;
|
||||
if (obs.Right > envRight) envRight = obs.Right;
|
||||
if (obs.Top > envTop) envTop = obs.Top;
|
||||
}
|
||||
|
||||
return new Box(envLeft, envBottom, envRight - envLeft, envTop - envBottom);
|
||||
}
|
||||
|
||||
private static void SplitAtEnvelope(Box remnant, Box envelope, double minDim, List<TieredRemnant> results)
|
||||
{
|
||||
var eps = Math.Tolerance.Epsilon;
|
||||
|
||||
// Fully within the envelope.
|
||||
if (remnant.Left >= envelope.Left - eps && remnant.Right <= envelope.Right + eps &&
|
||||
remnant.Bottom >= envelope.Bottom - eps && remnant.Top <= envelope.Top + eps)
|
||||
{
|
||||
results.Add(new TieredRemnant(remnant, 0));
|
||||
return;
|
||||
}
|
||||
|
||||
// Fully outside the envelope (no overlap).
|
||||
if (remnant.Left >= envelope.Right - eps || remnant.Right <= envelope.Left + eps ||
|
||||
remnant.Bottom >= envelope.Top - eps || remnant.Top <= envelope.Bottom + eps)
|
||||
{
|
||||
results.Add(new TieredRemnant(remnant, 2));
|
||||
return;
|
||||
}
|
||||
|
||||
// Partially overlapping — split at envelope edges.
|
||||
var innerLeft = System.Math.Max(remnant.Left, envelope.Left);
|
||||
var innerBottom = System.Math.Max(remnant.Bottom, envelope.Bottom);
|
||||
var innerRight = System.Math.Min(remnant.Right, envelope.Right);
|
||||
var innerTop = System.Math.Min(remnant.Top, envelope.Top);
|
||||
|
||||
// Inner portion (priority 0).
|
||||
TryAdd(results, innerLeft, innerBottom, innerRight - innerLeft, innerTop - innerBottom, 0, minDim);
|
||||
|
||||
// Edge extensions (priority 1).
|
||||
if (remnant.Right > envelope.Right + eps)
|
||||
TryAdd(results, envelope.Right, remnant.Bottom, remnant.Right - envelope.Right, remnant.Length, 1, minDim);
|
||||
|
||||
if (remnant.Left < envelope.Left - eps)
|
||||
TryAdd(results, remnant.Left, remnant.Bottom, envelope.Left - remnant.Left, remnant.Length, 1, minDim);
|
||||
|
||||
if (remnant.Top > envelope.Top + eps)
|
||||
TryAdd(results, innerLeft, envelope.Top, innerRight - innerLeft, remnant.Top - envelope.Top, 1, minDim);
|
||||
|
||||
if (remnant.Bottom < envelope.Bottom - eps)
|
||||
TryAdd(results, innerLeft, remnant.Bottom, innerRight - innerLeft, envelope.Bottom - remnant.Bottom, 1, minDim);
|
||||
|
||||
// Corner extensions (priority 2).
|
||||
if (remnant.Right > envelope.Right + eps && remnant.Top > envelope.Top + eps)
|
||||
TryAdd(results, envelope.Right, envelope.Top, remnant.Right - envelope.Right, remnant.Top - envelope.Top, 2, minDim);
|
||||
|
||||
if (remnant.Right > envelope.Right + eps && remnant.Bottom < envelope.Bottom - eps)
|
||||
TryAdd(results, envelope.Right, remnant.Bottom, remnant.Right - envelope.Right, envelope.Bottom - remnant.Bottom, 2, minDim);
|
||||
|
||||
if (remnant.Left < envelope.Left - eps && remnant.Top > envelope.Top + eps)
|
||||
TryAdd(results, remnant.Left, envelope.Top, envelope.Left - remnant.Left, remnant.Top - envelope.Top, 2, minDim);
|
||||
|
||||
if (remnant.Left < envelope.Left - eps && remnant.Bottom < envelope.Bottom - eps)
|
||||
TryAdd(results, remnant.Left, remnant.Bottom, envelope.Left - remnant.Left, envelope.Bottom - remnant.Bottom, 2, minDim);
|
||||
}
|
||||
|
||||
private static void TryAdd(List<TieredRemnant> results, double x, double y, double w, double h, int priority, double minDim)
|
||||
{
|
||||
if (w >= minDim && h >= minDim)
|
||||
results.Add(new TieredRemnant(new Box(x, y, w, h), priority));
|
||||
}
|
||||
|
||||
private Box ClipToWorkArea(Box obs)
|
||||
{
|
||||
var left = System.Math.Max(obs.Left, workArea.Left);
|
||||
var bottom = System.Math.Max(obs.Bottom, workArea.Bottom);
|
||||
var right = System.Math.Min(obs.Right, workArea.Right);
|
||||
var top = System.Math.Min(obs.Top, workArea.Top);
|
||||
|
||||
if (right <= left || top <= bottom)
|
||||
return Box.Empty;
|
||||
|
||||
return new Box(left, bottom, right - left, top - bottom);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds maximal empty rectangles using the histogram method.
|
||||
/// For each row, builds a height histogram of consecutive empty cells
|
||||
/// above, then extracts the largest rectangles from the histogram.
|
||||
/// </summary>
|
||||
private static List<Box> MergeCells(CellGrid grid)
|
||||
{
|
||||
var height = new int[grid.Rows, grid.Cols];
|
||||
|
||||
for (var c = 0; c < grid.Cols; c++)
|
||||
{
|
||||
for (var r = 0; r < grid.Rows; r++)
|
||||
height[r, c] = grid.Empty[r, c] ? (r > 0 ? height[r - 1, c] + 1 : 1) : 0;
|
||||
}
|
||||
|
||||
var candidates = new List<Box>();
|
||||
|
||||
for (var r = 0; r < grid.Rows; r++)
|
||||
{
|
||||
var stack = new Stack<(int startCol, int h)>();
|
||||
|
||||
for (var c = 0; c <= grid.Cols; c++)
|
||||
{
|
||||
var h = c < grid.Cols ? height[r, c] : 0;
|
||||
var startCol = c;
|
||||
|
||||
while (stack.Count > 0 && stack.Peek().h > h)
|
||||
{
|
||||
var top = stack.Pop();
|
||||
startCol = top.startCol;
|
||||
|
||||
candidates.Add(new Box(
|
||||
grid.XCoords[top.startCol], grid.YCoords[r - top.h + 1],
|
||||
grid.XCoords[c] - grid.XCoords[top.startCol],
|
||||
grid.YCoords[r + 1] - grid.YCoords[r - top.h + 1]));
|
||||
}
|
||||
|
||||
if (h > 0)
|
||||
stack.Push((startCol, h));
|
||||
}
|
||||
}
|
||||
|
||||
return candidates;
|
||||
}
|
||||
}
|
||||
}
|
||||
124
OpenNest.Engine/Fill/RotationAnalysis.cs
Normal file
124
OpenNest.Engine/Fill/RotationAnalysis.cs
Normal file
@@ -0,0 +1,124 @@
|
||||
using OpenNest.Converters;
|
||||
using OpenNest.Geometry;
|
||||
using OpenNest.Math;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace OpenNest.Engine.Fill
|
||||
{
|
||||
internal static class RotationAnalysis
|
||||
{
|
||||
/// <summary>
|
||||
/// Finds the rotation angle that minimizes the bounding rectangle of a drawing's
|
||||
/// largest shape, constrained by the NestItem's rotation range.
|
||||
/// </summary>
|
||||
public static double FindBestRotation(NestItem item)
|
||||
{
|
||||
var entities = ConvertProgram.ToGeometry(item.Drawing.Program)
|
||||
.Where(e => e.Layer != SpecialLayers.Rapid);
|
||||
|
||||
var shapes = ShapeBuilder.GetShapes(entities);
|
||||
|
||||
if (shapes.Count == 0)
|
||||
return 0;
|
||||
|
||||
// Find the largest shape (outer profile).
|
||||
var largest = shapes[0];
|
||||
var largestArea = largest.Area();
|
||||
|
||||
for (var i = 1; i < shapes.Count; i++)
|
||||
{
|
||||
var area = shapes[i].Area();
|
||||
if (area > largestArea)
|
||||
{
|
||||
largest = shapes[i];
|
||||
largestArea = area;
|
||||
}
|
||||
}
|
||||
|
||||
// Convert to polygon so arcs are properly represented as line segments.
|
||||
// Shape.FindBestRotation() uses Entity cardinal points which are incorrect
|
||||
// for arcs that don't sweep through all 4 cardinal directions.
|
||||
var polygon = largest.ToPolygonWithTolerance(0.1);
|
||||
|
||||
BoundingRectangleResult result;
|
||||
|
||||
if (item.RotationStart.IsEqualTo(0) && item.RotationEnd.IsEqualTo(0))
|
||||
result = polygon.FindBestRotation();
|
||||
else
|
||||
result = polygon.FindBestRotation(item.RotationStart, item.RotationEnd);
|
||||
|
||||
// Negate the angle to align the minimum bounding rectangle with the axes.
|
||||
return -result.Angle;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Computes the convex hull of the parts' geometry and returns the unique
|
||||
/// edge angles, suitable for use as candidate rotation angles.
|
||||
/// </summary>
|
||||
public static List<double> FindHullEdgeAngles(List<Part> parts)
|
||||
{
|
||||
var points = new List<Vector>();
|
||||
|
||||
foreach (var part in parts)
|
||||
{
|
||||
var entities = ConvertProgram.ToGeometry(part.Program)
|
||||
.Where(e => e.Layer != SpecialLayers.Rapid);
|
||||
|
||||
var shapes = ShapeBuilder.GetShapes(entities);
|
||||
|
||||
foreach (var shape in shapes)
|
||||
{
|
||||
var polygon = shape.ToPolygonWithTolerance(0.1);
|
||||
|
||||
foreach (var vertex in polygon.Vertices)
|
||||
points.Add(vertex + part.Location);
|
||||
}
|
||||
}
|
||||
|
||||
if (points.Count < 3)
|
||||
return new List<double> { 0 };
|
||||
|
||||
var hull = ConvexHull.Compute(points);
|
||||
return GetHullEdgeAngles(hull);
|
||||
}
|
||||
|
||||
public static List<double> GetHullEdgeAngles(Polygon hull)
|
||||
{
|
||||
var vertices = hull.Vertices;
|
||||
var n = hull.IsClosed() ? vertices.Count - 1 : vertices.Count;
|
||||
|
||||
// Collect edges with their squared length so we can sort by longest first.
|
||||
var edges = new List<(double angle, double lengthSq)>();
|
||||
|
||||
for (var i = 0; i < n; i++)
|
||||
{
|
||||
var next = (i + 1) % n;
|
||||
var dx = vertices[next].X - vertices[i].X;
|
||||
var dy = vertices[next].Y - vertices[i].Y;
|
||||
var lengthSq = dx * dx + dy * dy;
|
||||
|
||||
if (lengthSq < Tolerance.Epsilon)
|
||||
continue;
|
||||
|
||||
var angle = -System.Math.Atan2(dy, dx);
|
||||
|
||||
if (!edges.Any(e => e.angle.IsEqualTo(angle)))
|
||||
edges.Add((angle, lengthSq));
|
||||
}
|
||||
|
||||
// Longest edges first — they produce the flattest tiling rows.
|
||||
edges.Sort((a, b) => b.lengthSq.CompareTo(a.lengthSq));
|
||||
|
||||
var angles = new List<double>(edges.Count + 1) { 0 };
|
||||
|
||||
foreach (var (angle, _) in edges)
|
||||
{
|
||||
if (!angles.Any(a => a.IsEqualTo(angle)))
|
||||
angles.Add(angle);
|
||||
}
|
||||
|
||||
return angles;
|
||||
}
|
||||
}
|
||||
}
|
||||
75
OpenNest.Engine/Fill/ShrinkFiller.cs
Normal file
75
OpenNest.Engine/Fill/ShrinkFiller.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using OpenNest.Geometry;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
|
||||
namespace OpenNest.Engine.Fill
|
||||
{
|
||||
public enum ShrinkAxis { Width, Height }
|
||||
|
||||
public class ShrinkResult
|
||||
{
|
||||
public List<Part> Parts { get; set; }
|
||||
public double Dimension { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fills a box then iteratively shrinks one axis by the spacing amount
|
||||
/// until the part count drops. Returns the tightest box that still fits
|
||||
/// the same number of parts.
|
||||
/// </summary>
|
||||
public static class ShrinkFiller
|
||||
{
|
||||
public static ShrinkResult Shrink(
|
||||
Func<NestItem, Box, List<Part>> fillFunc,
|
||||
NestItem item, Box box,
|
||||
double spacing,
|
||||
ShrinkAxis axis,
|
||||
CancellationToken token = default,
|
||||
int maxIterations = 20)
|
||||
{
|
||||
var parts = fillFunc(item, box);
|
||||
|
||||
if (parts == null || parts.Count == 0)
|
||||
return new ShrinkResult { Parts = parts ?? new List<Part>(), Dimension = 0 };
|
||||
|
||||
var targetCount = parts.Count;
|
||||
var bestParts = parts;
|
||||
var bestDim = MeasureDimension(parts, box, axis);
|
||||
|
||||
for (var i = 0; i < maxIterations; i++)
|
||||
{
|
||||
if (token.IsCancellationRequested)
|
||||
break;
|
||||
|
||||
var trialDim = bestDim - spacing;
|
||||
if (trialDim <= 0)
|
||||
break;
|
||||
|
||||
var trialBox = axis == ShrinkAxis.Width
|
||||
? new Box(box.X, box.Y, trialDim, box.Length)
|
||||
: new Box(box.X, box.Y, box.Width, trialDim);
|
||||
|
||||
var trialParts = fillFunc(item, trialBox);
|
||||
|
||||
if (trialParts == null || trialParts.Count < targetCount)
|
||||
break;
|
||||
|
||||
bestParts = trialParts;
|
||||
bestDim = MeasureDimension(trialParts, box, axis);
|
||||
}
|
||||
|
||||
return new ShrinkResult { Parts = bestParts, Dimension = bestDim };
|
||||
}
|
||||
|
||||
private static double MeasureDimension(List<Part> parts, Box box, ShrinkAxis axis)
|
||||
{
|
||||
var placedBox = parts.Cast<IBoundable>().GetBoundingBox();
|
||||
|
||||
return axis == ShrinkAxis.Width
|
||||
? placedBox.Right - box.X
|
||||
: placedBox.Top - box.Y;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user