mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-20 20:27:42 -05:00
A plate with a custom outline was pushed against its bounding box only, so a part pushed toward a missing corner ran through it to the box edge and ended off the sheet. Compactor's plate pushes (Push, PushBoundingBox, SettlePlacement, Settle) now also stop at the walls of the outline: the part of the work area that is not usable material (a missing corner, an obstacle, each grown by the largest edge spacing). A mover is measured as the edge review measures it, by the circumscribed perimeter of its clean material without leads, so it rests where that review still passes. A box push stops 0.0012 short of a wall so the geometry push after it starts clear. A rectangular sheet has no walls and pushes exactly as before. SheetOutline.FromGeometry eroded the whole imported outline by the chord tolerance, so a sheet drawn 60 x 27 was stored as 59.998 x 26.998 with a chamfer at every inside corner. Each arc is now chorded on the side that takes material away (ClipperBridge.FlattenOneSided) and straight edges are kept exactly. The plate header, the plate list and Edit Plate's outline size show the size rounded to four decimals instead of the raw double. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
633 lines
26 KiB
C#
633 lines
26 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using OpenNest.Geometry;
|
|
using OpenNest.Math;
|
|
|
|
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
|
|
{
|
|
/// <summary>
|
|
/// The plate's clamp keep-outs (zones grown square by the clamp clearance) as static push
|
|
/// obstacles (C1), held at zero spacing rather than the part spacing. Unlike ordinary already-overlapping parts,
|
|
/// a clamp stays a wall even when a selected member straddles it: dropping it would
|
|
/// let other clear members move into the zone. Empty zone lists change every
|
|
/// caller's arithmetic by nothing (C2).
|
|
/// </summary>
|
|
private static ObstacleSet ZoneObstacles(Plate plate) =>
|
|
new(Jobs.InteractiveClamps.ObstacleParts(plate), 0);
|
|
|
|
/// <summary>
|
|
/// Pushes movingParts to the first contact with the work-area edge, another part, a
|
|
/// clamp keep-out or, on an outlined sheet, the edge of its usable material.
|
|
/// </summary>
|
|
public static double Push(List<Part> movingParts, Plate plate, PushDirection direction) =>
|
|
Push(movingParts, plate, SpatialQuery.DirectionToOffset(direction, 1.0));
|
|
|
|
/// <summary>
|
|
/// Pushes movingParts along an arbitrary angle (radians, 0 = right, π/2 = up).
|
|
/// </summary>
|
|
public static double Push(List<Part> movingParts, Plate plate, double angle) =>
|
|
Push(movingParts, plate, new Vector(PortableMath.Cos(angle), PortableMath.Sin(angle)));
|
|
|
|
private static double Push(List<Part> movingParts, Plate plate, Vector direction,
|
|
Jobs.InteractiveOutline outline = null)
|
|
{
|
|
var obstacleParts = plate.Parts
|
|
.Where(p => !movingParts.Contains(p) && !IntersectsAny(p, movingParts))
|
|
.ToList();
|
|
|
|
return Push(movingParts, plate.WorkArea(), direction,
|
|
outline ?? Jobs.InteractiveOutline.Of(plate),
|
|
new ObstacleSet(obstacleParts, plate.PartSpacing), ZoneObstacles(plate));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Moves movingParts one repeat step along <paramref name="direction"/>: the
|
|
/// distance at which a copy left behind would sit at the plate's part spacing,
|
|
/// measured on the parts' geometry like a fill's copy distance, so shapes that
|
|
/// nest into their own copy step less than their size. Unlike a push it always
|
|
/// moves the full step, like a nudge: it ignores the work-area edge and other
|
|
/// parts, so it can land off the plate or on a part. Returns the distance moved.
|
|
/// </summary>
|
|
public static double Step(List<Part> movingParts, Plate plate, PushDirection direction)
|
|
{
|
|
if (movingParts.Count == 0)
|
|
return 0;
|
|
|
|
var pattern = new Pattern();
|
|
pattern.Parts.AddRange(movingParts);
|
|
pattern.UpdateBounds();
|
|
var axis = SpatialQuery.IsHorizontalDirection(direction)
|
|
? NestDirection.Horizontal
|
|
: NestDirection.Vertical;
|
|
var step = new FillLinear(plate.WorkArea(), plate.PartSpacing).CopyDistance(pattern, axis);
|
|
if (!double.IsFinite(step) || step <= 0)
|
|
return 0;
|
|
|
|
var offset = SpatialQuery.DirectionToOffset(direction, step);
|
|
foreach (var part in movingParts)
|
|
part.Offset(offset);
|
|
return step;
|
|
}
|
|
|
|
/// <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
|
|
) => Push(movingParts, workArea, direction, new ObstacleSet(obstacleParts, partSpacing));
|
|
|
|
/// <summary>
|
|
/// Pushes movingParts to the first contact with the work-area edge or an obstacle,
|
|
/// keeping each obstacle set's own spacing.
|
|
/// </summary>
|
|
internal static double Push(
|
|
List<Part> movingParts,
|
|
Box workArea,
|
|
Vector direction,
|
|
params ObstacleSet[] obstacleSets
|
|
) => Push(movingParts, workArea, direction, null, obstacleSets);
|
|
|
|
/// <summary>As the push above, also stopping at the walls of an outlined sheet
|
|
/// (<paramref name="outline"/>; null for none).</summary>
|
|
private static double Push(
|
|
List<Part> movingParts,
|
|
Box workArea,
|
|
Vector direction,
|
|
Jobs.InteractiveOutline outline,
|
|
params ObstacleSet[] obstacleSets
|
|
)
|
|
{
|
|
var opposite = -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;
|
|
|
|
foreach (var set in obstacleSets)
|
|
distance = set.Travel(moving, direction, opposite, distance);
|
|
|
|
if (outline != null)
|
|
distance = outline.Travel(moving, direction, distance);
|
|
}
|
|
|
|
if (distance < double.MaxValue && distance > 0)
|
|
{
|
|
var offset = direction * distance;
|
|
foreach (var moving in movingParts)
|
|
moving.Offset(offset);
|
|
return distance;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Obstacles kept at one spacing, with their bounds and spacing-offset contours
|
|
/// cached for one push.
|
|
/// </summary>
|
|
internal sealed class ObstacleSet
|
|
{
|
|
private readonly List<Part> parts;
|
|
private readonly Box[] spacingBoxes;
|
|
private readonly List<Entity>[] entities;
|
|
private readonly double halfSpacing;
|
|
|
|
public ObstacleSet(List<Part> parts, double spacing)
|
|
{
|
|
this.parts = parts;
|
|
Spacing = System.Math.Max(0, spacing);
|
|
halfSpacing = Spacing / 2;
|
|
Boxes = new Box[parts.Count];
|
|
spacingBoxes = new Box[parts.Count];
|
|
entities = new List<Entity>[parts.Count];
|
|
for (var i = 0; i < parts.Count; i++)
|
|
{
|
|
Boxes[i] = parts[i].BoundingBox;
|
|
spacingBoxes[i] = SpacingBounds(Boxes[i], halfSpacing);
|
|
}
|
|
}
|
|
|
|
public double Spacing { get; }
|
|
|
|
public Box[] Boxes { get; }
|
|
|
|
/// <summary>The shorter of <paramref name="distance"/> and how far
|
|
/// <paramref name="moving"/> travels before it comes within the spacing.</summary>
|
|
public double Travel(Part moving, Vector direction, Vector opposite, double distance)
|
|
{
|
|
var movingBox = moving.BoundingBox;
|
|
List<Entity> movingEntities = null;
|
|
|
|
// Check if any obstacle is inside the moving part — only then
|
|
// do we need cutout entities on the moving part.
|
|
var needCutouts = false;
|
|
for (var i = 0; i < Boxes.Length; i++)
|
|
{
|
|
if (movingBox.Contains(Boxes[i]))
|
|
{
|
|
needCutouts = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Broad-phase bounds must enclose the spacing-offset contours.
|
|
// Raw bounds can miss near passes and overestimate the safe travel.
|
|
var movingSpacingBox = SpacingBounds(movingBox, halfSpacing);
|
|
for (var i = 0; i < Boxes.Length; i++)
|
|
{
|
|
var obstacleSpacingBox = spacingBoxes[i];
|
|
var reverseGap = SpatialQuery.DirectionalGap(
|
|
movingSpacingBox,
|
|
obstacleSpacingBox,
|
|
opposite
|
|
);
|
|
// An obstacle wholly behind the moving part, its bounds touching
|
|
// included, can only fall further behind. A part placed exactly at the
|
|
// spacing touches its neighbor's offset contour, and rounding in that
|
|
// contact can make the slide kernel block the push away from it.
|
|
if (IsBehind(reverseGap))
|
|
continue;
|
|
|
|
var gap = SpatialQuery.DirectionalGap(
|
|
movingSpacingBox,
|
|
obstacleSpacingBox,
|
|
direction
|
|
);
|
|
if (gap >= distance)
|
|
continue;
|
|
|
|
if (
|
|
!SpatialQuery.PerpendicularOverlap(
|
|
movingSpacingBox,
|
|
obstacleSpacingBox,
|
|
direction
|
|
)
|
|
)
|
|
continue;
|
|
|
|
movingEntities ??=
|
|
halfSpacing > 0
|
|
? (
|
|
needCutouts
|
|
? PartGeometry.GetOffsetPartEntities(moving, halfSpacing)
|
|
: PartGeometry.GetOffsetPerimeterEntities(moving, halfSpacing)
|
|
)
|
|
: (
|
|
needCutouts
|
|
? PartGeometry.GetPartEntities(moving)
|
|
: PartGeometry.GetPerimeterEntities(moving)
|
|
);
|
|
|
|
// A moving part can be inside an obstacle's cutout. Omitting that
|
|
// loop would let it cross the inner wall before seeing the perimeter.
|
|
entities[i] ??=
|
|
halfSpacing > 0
|
|
? PartGeometry.GetOffsetPartEntities(parts[i], halfSpacing)
|
|
: PartGeometry.GetPartEntities(parts[i]);
|
|
|
|
// Contacts left by a previous push only block directions that would
|
|
// push material into material; the kernel classifies them.
|
|
var d = SpatialQuery.DirectionalDistance(
|
|
movingEntities,
|
|
entities[i],
|
|
direction
|
|
);
|
|
|
|
if (d < distance)
|
|
distance = d;
|
|
}
|
|
|
|
return distance;
|
|
}
|
|
}
|
|
|
|
/// <summary>True when an obstacle's bounds end at or behind the moving bounds'
|
|
/// trailing side (<paramref name="reverseGap"/> measured against the push).</summary>
|
|
private static bool IsBehind(double reverseGap) => reverseGap >= -Tolerance.Epsilon;
|
|
|
|
private static Box SpacingBounds(Box box, double spacing)
|
|
{
|
|
return new Box(
|
|
box.Left - spacing,
|
|
box.Bottom - spacing,
|
|
box.Length + 2 * spacing,
|
|
box.Width + 2 * spacing
|
|
);
|
|
}
|
|
|
|
private static bool IntersectsAny(Part candidate, List<Part> parts)
|
|
{
|
|
for (var i = 0; i < parts.Count; i++)
|
|
{
|
|
if (!candidate.Intersects(parts[i], out _))
|
|
continue;
|
|
|
|
// Part.Intersects compares outer perimeters only. A valid insert in a
|
|
// cutout must remain an obstacle, not be discarded as already overlapping.
|
|
var a = new ShapeProfile(PartGeometry.GetPartEntities(candidate));
|
|
var b = new ShapeProfile(PartGeometry.GetPartEntities(parts[i]));
|
|
if (a.Cutouts.Count == 0 && b.Cutouts.Count == 0)
|
|
return true;
|
|
if (Collision.HasOverlap(
|
|
a.Perimeter.ToPolygonWithTolerance(0.001),
|
|
b.Perimeter.ToPolygonWithTolerance(0.001),
|
|
a.Cutouts.Select(hole => hole.ToPolygonWithTolerance(0.001)).ToList(),
|
|
b.Cutouts.Select(hole => hole.ToPolygonWithTolerance(0.001)).ToList()))
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
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
|
|
) => PushBoundingBox(movingParts, plate, direction, null);
|
|
|
|
private static double PushBoundingBox(
|
|
List<Part> movingParts,
|
|
Plate plate,
|
|
PushDirection direction,
|
|
Jobs.InteractiveOutline outline
|
|
)
|
|
{
|
|
var obstacleParts = plate.Parts
|
|
.Where(p => !movingParts.Contains(p) && !IntersectsAny(p, movingParts))
|
|
.ToList();
|
|
var zones = ZoneObstacles(plate);
|
|
|
|
return PushBoundingBox(
|
|
movingParts,
|
|
plate.WorkArea(),
|
|
direction,
|
|
outline ?? Jobs.InteractiveOutline.Of(plate),
|
|
(BoundingBoxes(obstacleParts), plate.PartSpacing),
|
|
(zones.Boxes, zones.Spacing)
|
|
);
|
|
}
|
|
|
|
public static double PushBoundingBox(
|
|
List<Part> movingParts,
|
|
List<Part> obstacleParts,
|
|
Box workArea,
|
|
double partSpacing,
|
|
PushDirection direction
|
|
) => PushBoundingBox(movingParts, workArea, direction, null, (BoundingBoxes(obstacleParts), partSpacing));
|
|
|
|
private static Box[] BoundingBoxes(List<Part> parts)
|
|
{
|
|
var boxes = new Box[parts.Count];
|
|
for (var i = 0; i < parts.Count; i++)
|
|
boxes[i] = parts[i].BoundingBox;
|
|
return boxes;
|
|
}
|
|
|
|
private static double PushBoundingBox(
|
|
List<Part> movingParts,
|
|
Box workArea,
|
|
PushDirection direction,
|
|
Jobs.InteractiveOutline outline,
|
|
params (Box[] Boxes, double Spacing)[] obstacleSets
|
|
)
|
|
{
|
|
var opposite = SpatialQuery.OppositeDirection(direction);
|
|
var isHorizontal = SpatialQuery.IsHorizontalDirection(direction);
|
|
var vector = SpatialQuery.DirectionToOffset(direction, 1.0);
|
|
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;
|
|
|
|
foreach (var (obstacleBoxes, spacing) in obstacleSets)
|
|
{
|
|
for (var i = 0; i < obstacleBoxes.Length; i++)
|
|
{
|
|
var reverseGap = SpatialQuery.DirectionalGap(
|
|
movingBox,
|
|
obstacleBoxes[i],
|
|
opposite
|
|
);
|
|
if (IsBehind(reverseGap))
|
|
continue;
|
|
|
|
// An obstacle beside the path but nearer than the spacing blocks
|
|
// too: passing it would leave the boxes closer than the spacing.
|
|
// One exactly at the spacing is slid along.
|
|
var sideGap = isHorizontal
|
|
? System.Math.Max(movingBox.Bottom - obstacleBoxes[i].Top,
|
|
obstacleBoxes[i].Bottom - movingBox.Top)
|
|
: System.Math.Max(movingBox.Left - obstacleBoxes[i].Right,
|
|
obstacleBoxes[i].Left - movingBox.Right);
|
|
|
|
if (sideGap >= (spacing > 0 ? spacing - Tolerance.Epsilon : 0))
|
|
continue;
|
|
|
|
var gap = SpatialQuery.DirectionalGap(movingBox, obstacleBoxes[i], direction);
|
|
var d = gap - spacing - 0.002;
|
|
if (d < 0)
|
|
d = 0;
|
|
if (d < distance)
|
|
distance = d;
|
|
}
|
|
}
|
|
|
|
// An outlined sheet's edge is a wall for the box, as the work area is.
|
|
if (outline != null)
|
|
distance = outline.Travel(movingBox, vector, distance);
|
|
}
|
|
|
|
if (distance < double.MaxValue && distance > 0)
|
|
{
|
|
var offset = SpatialQuery.DirectionToOffset(direction, distance);
|
|
foreach (var moving in movingParts)
|
|
moving.Offset(offset);
|
|
return distance;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Settles a copied placement against the plate. <see cref="SettleOrder.ClosestToCorner"/>
|
|
/// tries both axis orders and keeps the group closest to the quadrant's work-area
|
|
/// corner; the other orders push along the named axis first. A coarse box pass
|
|
/// runs only when no moving box starts inside an existing part's box. A push ignores
|
|
/// a part the group starts on, so it can end clear of that part yet inside its
|
|
/// spacing: an order that keeps the part spacing always beats one that does not, and
|
|
/// a named order that does not falls back to the other one. With
|
|
/// <paramref name="boundingBoxesOnly"/> every push uses bounding boxes alone, so the
|
|
/// group stops at the part spacing from obstacles' boxes, never nesting into their shapes.
|
|
/// </summary>
|
|
public static void SettlePlacement(
|
|
List<Part> movingParts,
|
|
Plate plate,
|
|
PushDirection horizontal,
|
|
PushDirection vertical,
|
|
int maxIterations = 20,
|
|
SettleOrder order = SettleOrder.ClosestToCorner,
|
|
bool boundingBoxesOnly = false
|
|
)
|
|
{
|
|
if (movingParts.Count == 0)
|
|
return;
|
|
|
|
var workArea = plate.WorkArea();
|
|
// Built once: every push of every trial meets the same walls.
|
|
var outline = Jobs.InteractiveOutline.Of(plate);
|
|
var skipBoxes = boundingBoxesOnly || movingParts.Any(moving =>
|
|
plate.Parts.Any(obstacle => moving.BoundingBox.Intersects(obstacle.BoundingBox))
|
|
);
|
|
double PushOnce(List<Part> trial, PushDirection direction) => boundingBoxesOnly
|
|
? PushBoundingBox(trial, plate, direction, outline)
|
|
: Push(trial, plate, SpatialQuery.DirectionToOffset(direction, 1.0), outline);
|
|
var bestScore = double.MaxValue;
|
|
var bestKeepsSpacing = false;
|
|
Vector[] best = null;
|
|
// A named order's opposite comes last, tried only when the named one ends too close.
|
|
var firstAxes = order switch
|
|
{
|
|
SettleOrder.HorizontalFirst => new[] { horizontal, vertical },
|
|
SettleOrder.VerticalFirst => new[] { vertical, horizontal },
|
|
_ => new[] { horizontal, vertical },
|
|
};
|
|
var fallbacks = order == SettleOrder.ClosestToCorner ? 0 : 1;
|
|
|
|
for (var attempt = 0; attempt < firstAxes.Length; attempt++)
|
|
{
|
|
var isFallback = attempt >= firstAxes.Length - fallbacks;
|
|
if (isFallback && bestKeepsSpacing)
|
|
break;
|
|
|
|
var first = firstAxes[attempt];
|
|
var second = first == horizontal ? vertical : horizontal;
|
|
var trial = movingParts.Select(p => (Part)p.Clone()).ToList();
|
|
if (!skipBoxes)
|
|
{
|
|
PushBoundingBox(trial, plate, first, outline);
|
|
PushBoundingBox(trial, plate, second, outline);
|
|
}
|
|
|
|
for (var i = 0; i < maxIterations; i++)
|
|
{
|
|
var moved = PushOnce(trial, first) + PushOnce(trial, second);
|
|
if (moved < 0.01)
|
|
break;
|
|
}
|
|
|
|
var bounds = trial.GetBoundingBox();
|
|
var dx = horizontal == PushDirection.Left
|
|
? bounds.Left - workArea.Left : workArea.Right - bounds.Right;
|
|
var dy = vertical == PushDirection.Down
|
|
? bounds.Bottom - workArea.Bottom : workArea.Top - bounds.Top;
|
|
var score = dx * dx + dy * dy;
|
|
var keepsSpacing = KeepsSpacing(trial, movingParts, plate);
|
|
if (best == null || (keepsSpacing && !bestKeepsSpacing)
|
|
|| (!isFallback && keepsSpacing == bestKeepsSpacing && score < bestScore))
|
|
{
|
|
bestScore = score;
|
|
bestKeepsSpacing = keepsSpacing;
|
|
best = trial.Select(p => p.Location).ToArray();
|
|
}
|
|
}
|
|
|
|
for (var i = 0; i < movingParts.Count; i++)
|
|
movingParts[i].Location = best[i];
|
|
}
|
|
|
|
/// <summary>
|
|
/// True when no settled part shares material with a placed part or sits closer to it
|
|
/// than the plate's part spacing, measured as the spacing review measures it. Zero
|
|
/// spacing enforces nothing, so every placement keeps it.
|
|
/// </summary>
|
|
private static bool KeepsSpacing(List<Part> settled, List<Part> movingParts, Plate plate)
|
|
{
|
|
var spacing = plate.PartSpacing;
|
|
if (!Diagnostics.PartSpacingReview.Enforces(spacing))
|
|
return true;
|
|
|
|
var contours = new Dictionary<Part, NativeContours>();
|
|
foreach (var part in settled)
|
|
{
|
|
var reach = SpacingBounds(part.BoundingBox, spacing);
|
|
foreach (var obstacle in plate.Parts)
|
|
{
|
|
if (obstacle.BaseDrawing.IsCutOff || movingParts.Contains(obstacle)
|
|
|| settled.Contains(obstacle) || !reach.Intersects(obstacle.BoundingBox))
|
|
continue;
|
|
if (Contours(part, contours) is { } moved && Contours(obstacle, contours) is { } placed
|
|
&& moved.TooClose(placed, spacing))
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <summary>A part's world-space material; null when it has no closed perimeter.</summary>
|
|
private static NativeContours Contours(Part part, Dictionary<Part, NativeContours> cache)
|
|
{
|
|
if (cache.TryGetValue(part, out var cached))
|
|
return cached;
|
|
|
|
var entities = PartGeometry.GetPartEntities(part);
|
|
var profile = entities.Count == 0 ? null : new ShapeProfile(entities);
|
|
var contours = profile?.Perimeter == null ? null : NativeContours.Of(profile, new Vector(0, 0));
|
|
cache[part] = contours;
|
|
return contours;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Repeatedly pushes parts left then down until total movement per
|
|
/// iteration falls below the given threshold.
|
|
/// </summary>
|
|
public static void Settle(
|
|
List<Part> parts,
|
|
Box workArea,
|
|
double partSpacing,
|
|
double threshold = 0.01,
|
|
int maxIterations = 20
|
|
)
|
|
{
|
|
if (parts.Count < 2)
|
|
return;
|
|
|
|
var noObstacles = new List<Part>();
|
|
|
|
for (var i = 0; i < maxIterations; i++)
|
|
{
|
|
var moved = 0.0;
|
|
moved += Push(parts, noObstacles, workArea, partSpacing, PushDirection.Left);
|
|
moved += Push(parts, noObstacles, workArea, partSpacing, PushDirection.Down);
|
|
|
|
if (moved < threshold)
|
|
break;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// As <see cref="Settle(List{Part}, Box, double, double, int)"/>, settling inside the
|
|
/// plate's work area with its clamp zones as static obstacles: settling never parks
|
|
/// a part inside a keep-out (C1), and a clear cluster still compacts against zones
|
|
/// like any other obstacle. With no zones this behaves exactly like settling in the
|
|
/// plate's work area (C2).
|
|
/// </summary>
|
|
public static void Settle(
|
|
List<Part> parts,
|
|
Plate plate,
|
|
double threshold = 0.01,
|
|
int maxIterations = 20
|
|
) => Settle(parts, plate, plate.WorkArea(), threshold, maxIterations);
|
|
|
|
/// <summary>Keep a fill group's own work-area bounds while excluding plate clamps.</summary>
|
|
public static void Settle(
|
|
List<Part> parts,
|
|
Plate plate,
|
|
Box workArea,
|
|
double threshold = 0.01,
|
|
int maxIterations = 20
|
|
)
|
|
{
|
|
if (parts.Count < 2)
|
|
return;
|
|
|
|
var obstacles = plate.Parts.Where(part => !parts.Contains(part)
|
|
&& !part.BaseDrawing.IsCutOff).ToList();
|
|
var zones = Jobs.InteractiveClamps.ObstacleParts(plate);
|
|
var outline = Jobs.InteractiveOutline.Of(plate);
|
|
|
|
for (var i = 0; i < maxIterations; i++)
|
|
{
|
|
var moved = 0.0;
|
|
foreach (var direction in new[] { PushDirection.Left, PushDirection.Down })
|
|
moved += Push(parts, workArea, SpatialQuery.DirectionToOffset(direction, 1.0),
|
|
outline,
|
|
new ObstacleSet(obstacles, plate.PartSpacing),
|
|
new ObstacleSet(zones, 0));
|
|
|
|
if (moved < threshold)
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|