mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-29 05:51:34 -05:00
refactor: merge duplicated geometry, settings-file and form helpers
Roslyn Bridge flagged these as copy-paste clusters; each pair now shares one implementation so a fix lands everywhere at once: - Shape.OffsetInward/OffsetOutward differed only in the target winding. - BestFitFinder kept its own copy of SpatialQuery's arc-extreme points. - SplitDrawingForm rebuilt DrawingSplitter's clip-region grid for preview. - GeometryOptimizer's collinear/coradial finders (one misspelled GetCoradialArs) become one generic FindRelated. - Five settings files repeated the write-with-retry loop, and two also repeated the unit parser and unreadable-file filter (SettingsFile). - ClampsForm and SupportGridForm repeated their row/box builders. - Vertical/HorizontalRemnantComparer share a RemnantComparer base. Behavior is unchanged except that Engine/NestStorage settings loads now also tolerate NotSupported/Security exceptions, matching the other settings files' never-throw contract. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4028dafb26
commit
b550640ef0
18 files changed
+291
-517
No files matched your search
@@ -191,7 +191,7 @@ namespace OpenNest.Engine.BestFit
|
||||
{
|
||||
points.Add(arc.StartPoint());
|
||||
points.Add(arc.EndPoint());
|
||||
AddArcExtremes(points, arc);
|
||||
SpatialQuery.AddArcExtremeVertices(points, arc);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -220,35 +220,6 @@ namespace OpenNest.Engine.BestFit
|
||||
return hullAngles;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the cardinal extreme points of an arc (0°, 90°, 180°, 270°)
|
||||
/// if they fall within the arc's angular span.
|
||||
/// </summary>
|
||||
private static void AddArcExtremes(List<Vector> points, Arc arc)
|
||||
{
|
||||
var a1 = arc.StartAngle;
|
||||
var a2 = arc.EndAngle;
|
||||
|
||||
if (arc.IsReversed)
|
||||
Generic.Swap(ref a1, ref a2);
|
||||
|
||||
// Right (0°)
|
||||
if (Angle.IsBetweenRad(Angle.TwoPI, a1, a2))
|
||||
points.Add(new Vector(arc.Center.X + arc.Radius, arc.Center.Y));
|
||||
|
||||
// Top (90°)
|
||||
if (Angle.IsBetweenRad(Angle.HalfPI, a1, a2))
|
||||
points.Add(new Vector(arc.Center.X, arc.Center.Y + arc.Radius));
|
||||
|
||||
// Left (180°)
|
||||
if (Angle.IsBetweenRad(System.Math.PI, a1, a2))
|
||||
points.Add(new Vector(arc.Center.X - arc.Radius, arc.Center.Y));
|
||||
|
||||
// Bottom (270°)
|
||||
if (Angle.IsBetweenRad(System.Math.PI * 1.5, a1, a2))
|
||||
points.Add(new Vector(arc.Center.X, arc.Center.Y - arc.Radius));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Minimum angular separation (radians) between hull-derived rotation candidates.
|
||||
/// Tessellated arcs produce many hull edges with nearly identical angles;
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using OpenNest.Geometry;
|
||||
using OpenNest.Math;
|
||||
|
||||
namespace OpenNest.Engine.Fill
|
||||
{
|
||||
@@ -8,44 +6,10 @@ namespace OpenNest.Engine.Fill
|
||||
/// Ranks fill results to minimize Y-extent (preserve top-side horizontal remnant).
|
||||
/// Tiebreak chain: count > smallest Y-extent > highest density.
|
||||
/// </summary>
|
||||
public class HorizontalRemnantComparer : IFillComparer
|
||||
public class HorizontalRemnantComparer : RemnantComparer
|
||||
{
|
||||
public bool IsBetter(List<Part> candidate, List<Part> current, Box workArea)
|
||||
{
|
||||
if (candidate == null || candidate.Count == 0)
|
||||
return false;
|
||||
protected override double Low(Box box) => box.Bottom;
|
||||
|
||||
if (current == null || current.Count == 0)
|
||||
return true;
|
||||
|
||||
if (candidate.Count != current.Count)
|
||||
return candidate.Count > current.Count;
|
||||
|
||||
var candExtent = YExtent(candidate);
|
||||
var currExtent = YExtent(current);
|
||||
|
||||
if (!candExtent.IsEqualTo(currExtent))
|
||||
return candExtent < currExtent;
|
||||
|
||||
return FillScore.Compute(candidate, workArea).Density
|
||||
> FillScore.Compute(current, workArea).Density;
|
||||
}
|
||||
|
||||
private static double YExtent(List<Part> parts)
|
||||
{
|
||||
var minY = double.MaxValue;
|
||||
var maxY = double.MinValue;
|
||||
|
||||
foreach (var part in parts)
|
||||
{
|
||||
var bb = part.BoundingBox;
|
||||
if (bb.Bottom < minY)
|
||||
minY = bb.Bottom;
|
||||
if (bb.Top > maxY)
|
||||
maxY = bb.Top;
|
||||
}
|
||||
|
||||
return maxY - minY;
|
||||
}
|
||||
protected override double High(Box box) => box.Top;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
using System.Collections.Generic;
|
||||
using OpenNest.Geometry;
|
||||
using OpenNest.Math;
|
||||
|
||||
namespace OpenNest.Engine.Fill
|
||||
{
|
||||
/// <summary>
|
||||
/// Ranks fill results to keep one side of the work area free for a remnant.
|
||||
/// Tiebreak chain: count > smallest extent along the ranked axis > highest density.
|
||||
/// </summary>
|
||||
public abstract class RemnantComparer : IFillComparer
|
||||
{
|
||||
/// <summary>The lower edge of a bounding box along the ranked axis.</summary>
|
||||
protected abstract double Low(Box box);
|
||||
|
||||
/// <summary>The upper edge of a bounding box along the ranked axis.</summary>
|
||||
protected abstract double High(Box box);
|
||||
|
||||
public bool IsBetter(List<Part> candidate, List<Part> current, Box workArea)
|
||||
{
|
||||
if (candidate == null || candidate.Count == 0)
|
||||
return false;
|
||||
|
||||
if (current == null || current.Count == 0)
|
||||
return true;
|
||||
|
||||
if (candidate.Count != current.Count)
|
||||
return candidate.Count > current.Count;
|
||||
|
||||
var candExtent = Extent(candidate);
|
||||
var currExtent = Extent(current);
|
||||
|
||||
if (!candExtent.IsEqualTo(currExtent))
|
||||
return candExtent < currExtent;
|
||||
|
||||
return FillScore.Compute(candidate, workArea).Density
|
||||
> FillScore.Compute(current, workArea).Density;
|
||||
}
|
||||
|
||||
private double Extent(List<Part> parts)
|
||||
{
|
||||
var min = double.MaxValue;
|
||||
var max = double.MinValue;
|
||||
|
||||
foreach (var part in parts)
|
||||
{
|
||||
var bb = part.BoundingBox;
|
||||
var low = Low(bb);
|
||||
var high = High(bb);
|
||||
if (low < min)
|
||||
min = low;
|
||||
if (high > max)
|
||||
max = high;
|
||||
}
|
||||
|
||||
return max - min;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using OpenNest.Geometry;
|
||||
using OpenNest.Math;
|
||||
|
||||
namespace OpenNest.Engine.Fill
|
||||
{
|
||||
@@ -8,44 +6,10 @@ namespace OpenNest.Engine.Fill
|
||||
/// Ranks fill results to minimize X-extent (preserve right-side vertical remnant).
|
||||
/// Tiebreak chain: count > smallest X-extent > highest density.
|
||||
/// </summary>
|
||||
public class VerticalRemnantComparer : IFillComparer
|
||||
public class VerticalRemnantComparer : RemnantComparer
|
||||
{
|
||||
public bool IsBetter(List<Part> candidate, List<Part> current, Box workArea)
|
||||
{
|
||||
if (candidate == null || candidate.Count == 0)
|
||||
return false;
|
||||
protected override double Low(Box box) => box.Left;
|
||||
|
||||
if (current == null || current.Count == 0)
|
||||
return true;
|
||||
|
||||
if (candidate.Count != current.Count)
|
||||
return candidate.Count > current.Count;
|
||||
|
||||
var candExtent = XExtent(candidate);
|
||||
var currExtent = XExtent(current);
|
||||
|
||||
if (!candExtent.IsEqualTo(currExtent))
|
||||
return candExtent < currExtent;
|
||||
|
||||
return FillScore.Compute(candidate, workArea).Density
|
||||
> FillScore.Compute(current, workArea).Density;
|
||||
}
|
||||
|
||||
private static double XExtent(List<Part> parts)
|
||||
{
|
||||
var minX = double.MaxValue;
|
||||
var maxX = double.MinValue;
|
||||
|
||||
foreach (var part in parts)
|
||||
{
|
||||
var bb = part.BoundingBox;
|
||||
if (bb.Left < minX)
|
||||
minX = bb.Left;
|
||||
if (bb.Right > maxX)
|
||||
maxX = bb.Right;
|
||||
}
|
||||
|
||||
return maxX - minX;
|
||||
}
|
||||
protected override double High(Box box) => box.Right;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user