diff --git a/OpenNest.Engine/FillLinear.cs b/OpenNest.Engine/FillLinear.cs new file mode 100644 index 0000000..5c30843 --- /dev/null +++ b/OpenNest.Engine/FillLinear.cs @@ -0,0 +1,66 @@ +using System.Collections.Generic; +using OpenNest.Geometry; +using OpenNest.Math; + +namespace OpenNest +{ + public class FillLinear + { + public FillLinear(Box workArea, double partSpacing) + { + WorkArea = workArea; + PartSpacing = partSpacing; + } + + public Box WorkArea { get; } + + public double PartSpacing { get; } + + /// + /// Finds the geometry-aware copy distance between two identical parts along an axis. + /// Places part B at bounding box offset from part A, then pushes B back toward A + /// using directional distance to find the tightest non-overlapping position. + /// + private double FindCopyDistance(Part partA, NestDirection direction) + { + var bbox = partA.BoundingBox; + double bboxDim; + PushDirection pushDir; + Vector copyOffset; + + if (direction == NestDirection.Horizontal) + { + bboxDim = bbox.Width; + pushDir = PushDirection.Left; + copyOffset = new Vector(bboxDim, 0); + } + else + { + bboxDim = bbox.Height; + pushDir = PushDirection.Down; + copyOffset = new Vector(0, bboxDim); + } + + // Create part B offset by bounding box dimension (guaranteed no overlap). + var partB = (Part)partA.Clone(); + partB.Offset(copyOffset); + + // Get geometry lines for push calculation. + var opposite = Helper.OppositeDirection(pushDir); + + var movingLines = PartSpacing > 0 + ? Helper.GetOffsetPartLines(partB, PartSpacing, pushDir) + : Helper.GetPartLines(partB, pushDir); + + var stationaryLines = Helper.GetPartLines(partA, opposite); + + // Find how far B can slide toward A. + var slideDistance = Helper.DirectionalDistance(movingLines, stationaryLines, pushDir); + + if (slideDistance >= double.MaxValue || slideDistance < 0) + return bboxDim; + + return bboxDim - slideDistance; + } + } +} diff --git a/OpenNest.Engine/OpenNest.Engine.csproj b/OpenNest.Engine/OpenNest.Engine.csproj index ee66de5..5b2df11 100644 --- a/OpenNest.Engine/OpenNest.Engine.csproj +++ b/OpenNest.Engine/OpenNest.Engine.csproj @@ -42,6 +42,7 @@ +