Files
OpenNest/OpenNest.Core/Geometry/BoundingBox.cs
T
ajandClaude Opus 5.5 e011fe8290 refactor: fold duplicated helpers into one shared copy each
Roslyn Bridge's duplicate scan found helpers copied verbatim between
projects, so a fix to one copy could silently miss the other. Each copy
now calls a single implementation with the same body:

- boundary nearest point: ClipperBridge.NearestBoundaryPoint (ErodedPierce,
  ScrapPierceSearch)
- nesting profile of a part: NestLayoutCheck.Profile (CommonLinePair)
- part label points: PartLabels in OpenNest.Desktop (WinForms LayoutPart)
- copied common-line groups: CommonLineGroup.AmongParts (ActionClone,
  PlaceTool)
- shared saved cutting parameters: CuttingParametersSerializer.SharedBy
  (MCP, Console)
- bounding box of boxes: generic BoundingBox.GetBoundingBox<T> (rectangle
  packing items)
- box proximity: Clearance.BoxesWithin (Expander)

Behavior is unchanged: every moved body is identical to the copies it
replaces, so the fill change in Expander and rectangle packing skips the
problem-nest corpus. Core, IO, Desktop and Engine suites and the label,
clone and common-line WinForms tests pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 17:50:53 -05:00

117 lines
3.6 KiB
C#

using System.Collections.Generic;
using System.Linq;
namespace OpenNest.Geometry
{
public static class BoundingBox
{
public static Box GetBoundingBox(this IList<Box> boxes) => GetBoundingBox<Box>(boxes);
/// <summary>The box around every box in the list, or <see cref="Box.Empty"/> when it is empty.</summary>
public static Box GetBoundingBox<T>(this IList<T> boxes)
where T : Box
{
if (boxes.Count == 0)
return Box.Empty;
double minX = boxes[0].X;
double minY = boxes[0].Y;
double maxX = boxes[0].Right;
double maxY = boxes[0].Top;
foreach (var box in boxes)
{
if (box.Left < minX)
minX = box.Left;
if (box.Right > maxX)
maxX = box.Right;
if (box.Bottom < minY)
minY = box.Bottom;
if (box.Top > maxY)
maxY = box.Top;
}
return new Box(minX, minY, maxX - minX, maxY - minY);
}
public static Box GetBoundingBox(this IList<Vector> pts)
{
if (pts.Count == 0)
return Box.Empty;
var first = pts[0];
var minX = first.X;
var maxX = first.X;
var minY = first.Y;
var maxY = first.Y;
for (int i = 1; i < pts.Count; ++i)
{
var vertex = pts[i];
if (vertex.X < minX)
minX = vertex.X;
else if (vertex.X > maxX)
maxX = vertex.X;
if (vertex.Y < minY)
minY = vertex.Y;
else if (vertex.Y > maxY)
maxY = vertex.Y;
}
return new Box(minX, minY, maxX - minX, maxY - minY);
}
public static Box GetBoundingBox(this IEnumerable<IBoundable> items)
{
var first = items.FirstOrDefault();
if (first == null)
return Box.Empty;
double left = first.Left;
double bottom = first.Bottom;
double right = first.Right;
double top = first.Top;
foreach (var box in items)
{
if (box.Left < left)
left = box.Left;
if (box.Right > right)
right = box.Right;
if (box.Bottom < bottom)
bottom = box.Bottom;
if (box.Top > top)
top = box.Top;
}
return new Box(left, bottom, right - left, top - bottom);
}
/// <summary>
/// Whether <paramref name="inner"/> lies inside <paramref name="outer"/>, allowing
/// <see cref="Math.Tolerance.Epsilon"/> on every side.
/// </summary>
internal static bool IsWithin(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;
}
/// <summary>The smallest box covering both boxes.</summary>
internal static Box Union(Box a, Box b)
{
var left = System.Math.Min(a.Left, b.Left);
var bottom = System.Math.Min(a.Bottom, b.Bottom);
var right = System.Math.Max(a.Right, b.Right);
var top = System.Math.Max(a.Top, b.Top);
return new Box(left, bottom, right - left, top - bottom);
}
}
}