mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-29 00:21:34 -05:00
refactor(geometry): share box containment, union, rotation and cutout range checks
Private copies of tolerant box containment, box union, vector rotation and the cutout proposals' coordinate-range check now use one helper each: BoundingBox.IsWithin and BoundingBox.Union, Vector.Rotate, and CutoutCoordinates.WithinRange. Tests pin the helpers bit-for-bit against frozen copies of the removed bodies. No behavior change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
082a50bbcd
commit
e231e44f72
15 files changed
+226
-119
No files matched your search
@@ -2,7 +2,6 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using OpenNest.Geometry;
|
||||
using OpenNest.Math;
|
||||
|
||||
namespace OpenNest.CNC.CuttingPlanning;
|
||||
|
||||
@@ -56,17 +55,4 @@ internal static class NestedPartContainment
|
||||
ExecutionMotionReader.ReadSupported(clean, location, null, token).Motions
|
||||
.Where(m => !m.Rapid && m.Layer is LayerType.Cut or LayerType.Display or LayerType.DisplayPerimeter && m.Curve != null)
|
||||
.Select(m => m.Curve.ToEntity().BoundingBox).ToList().GetBoundingBox();
|
||||
|
||||
/// <summary>Whether a host's bounds admit an inner part's bounds as a containment candidate.</summary>
|
||||
internal static bool Within(Box inner, Box outer) =>
|
||||
inner.Left >= outer.Left - Tolerance.Epsilon && inner.Right <= outer.Right + Tolerance.Epsilon
|
||||
&& inner.Bottom >= outer.Bottom - Tolerance.Epsilon && inner.Top <= outer.Top + Tolerance.Epsilon;
|
||||
|
||||
/// <summary>The union of two 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);
|
||||
return new(left, bottom, System.Math.Max(a.Right, b.Right) - left, System.Math.Max(a.Top, b.Top) - bottom);
|
||||
}
|
||||
}
|
||||
@@ -37,7 +37,7 @@ internal static class NestedPartReview
|
||||
continue;
|
||||
var program = FixedLeadReview.Clean(part);
|
||||
cleanBounds[index] = NestedPartContainment.CleanBounds(program, part.Location, token);
|
||||
hostBounds[index] = NestedPartContainment.Union(part.BoundingBox, cleanBounds[index]);
|
||||
hostBounds[index] = BoundingBox.Union(part.BoundingBox, cleanBounds[index]);
|
||||
clean[index] = program;
|
||||
}
|
||||
catch (Exception exception) when (IsInvalid(exception))
|
||||
@@ -52,7 +52,7 @@ internal static class NestedPartReview
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
if (inner == host || clean[inner] == null || clean[host] == null
|
||||
|| !NestedPartContainment.Within(cleanBounds[inner], hostBounds[host]))
|
||||
|| !BoundingBox.IsWithin(cleanBounds[inner], hostBounds[host]))
|
||||
continue;
|
||||
var inside = NestedPartContainment.InsideCutout(Material(inner), Material(host), token,
|
||||
out var ambiguity);
|
||||
|
||||
@@ -85,5 +85,28 @@ namespace OpenNest.Geometry
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -504,7 +504,7 @@ namespace OpenNest.Geometry
|
||||
var newC = Centroid(@new.Outer.Samples);
|
||||
for (var i = 0; i < seeds.Count; i++)
|
||||
{
|
||||
var rotated = Rotate(newC, seeds[i].Item1);
|
||||
var rotated = newC.Rotate(seeds[i].Item1);
|
||||
seeds[i] = (seeds[i].Item1, oldC - rotated);
|
||||
}
|
||||
return seeds;
|
||||
@@ -569,7 +569,7 @@ namespace OpenNest.Geometry
|
||||
{
|
||||
var p = @new.Outer.Samples[i].Position;
|
||||
var m = preMirror(p.X, p.Y);
|
||||
positions[idx++] = Rotate(new Vector(m.X, m.Y), rotation) + translation;
|
||||
positions[idx++] = new Vector(m.X, m.Y).Rotate(rotation) + translation;
|
||||
}
|
||||
if (!includeHoles)
|
||||
return;
|
||||
@@ -578,7 +578,7 @@ namespace OpenNest.Geometry
|
||||
{
|
||||
var p = @new.Holes[h].Samples[i].Position;
|
||||
var m = preMirror(p.X, p.Y);
|
||||
positions[idx++] = Rotate(new Vector(m.X, m.Y), rotation) + translation;
|
||||
positions[idx++] = new Vector(m.X, m.Y).Rotate(rotation) + translation;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -650,7 +650,7 @@ namespace OpenNest.Geometry
|
||||
return null; // nonfinite update
|
||||
|
||||
rotation += dTheta;
|
||||
translation = Rotate(translation, dTheta) + dT;
|
||||
translation = translation.Rotate(dTheta) + dT;
|
||||
|
||||
var rms = System.Math.Sqrt(order.Average(i => residuals[i] * residuals[i]));
|
||||
|
||||
@@ -790,7 +790,7 @@ namespace OpenNest.Geometry
|
||||
if (double.IsNaN(dTheta) || double.IsInfinity(dTheta))
|
||||
return false;
|
||||
|
||||
var r = Rotate(cq, dTheta);
|
||||
var r = cq.Rotate(dTheta);
|
||||
dTranslation = ct - r;
|
||||
return !double.IsNaN(dTranslation.X) && !double.IsNaN(dTranslation.Y);
|
||||
}
|
||||
@@ -866,11 +866,11 @@ namespace OpenNest.Geometry
|
||||
var outerCenter = Centroid(old.Outer.Samples);
|
||||
foreach (var flip in new[] { System.Math.PI, System.Math.PI / 2, -System.Math.PI / 2 })
|
||||
{
|
||||
var anchor = Rotate(-outerCenter, flip) + outerCenter;
|
||||
var anchor = (-outerCenter).Rotate(flip) + outerCenter;
|
||||
var probe = new Candidate
|
||||
{
|
||||
Rotation = NormalizeAngle(best.Rotation + flip),
|
||||
Translation = Rotate(best.Translation, flip) + anchor,
|
||||
Translation = best.Translation.Rotate(flip) + anchor,
|
||||
};
|
||||
var refined = FitIcp(
|
||||
old,
|
||||
@@ -971,8 +971,8 @@ namespace OpenNest.Geometry
|
||||
var limit = options.SamplingSpacing * 0.5;
|
||||
foreach (var s in @new.Outer.Samples)
|
||||
{
|
||||
var pa = Rotate(s.Position, a.Rotation) + a.Translation;
|
||||
var pb = Rotate(s.Position, best.Rotation) + best.Translation;
|
||||
var pa = s.Position.Rotate(a.Rotation) + a.Translation;
|
||||
var pb = s.Position.Rotate(best.Rotation) + best.Translation;
|
||||
if (pa.DistanceTo(pb) > limit)
|
||||
return true;
|
||||
}
|
||||
@@ -993,7 +993,7 @@ namespace OpenNest.Geometry
|
||||
var newTotal = 0;
|
||||
foreach (var s in AllNewSamples(@new))
|
||||
{
|
||||
var p = Rotate(s.Position, pose.Rotation) + pose.Translation;
|
||||
var p = s.Position.Rotate(pose.Rotation) + pose.Translation;
|
||||
newTotal++;
|
||||
if (TryClosestSegment(p, oldSegments, out var c) && p.DistanceTo(c) <= spanBound)
|
||||
newMatched++;
|
||||
@@ -1005,8 +1005,8 @@ namespace OpenNest.Geometry
|
||||
var posed = AllNewSegments(@new);
|
||||
for (var i = 0; i < posed.Length; i++)
|
||||
posedNewSegments[i] = new Line(
|
||||
Rotate(posed[i].StartPoint, pose.Rotation) + pose.Translation,
|
||||
Rotate(posed[i].EndPoint, pose.Rotation) + pose.Translation
|
||||
posed[i].StartPoint.Rotate(pose.Rotation) + pose.Translation,
|
||||
posed[i].EndPoint.Rotate(pose.Rotation) + pose.Translation
|
||||
);
|
||||
|
||||
var oldMatched = 0;
|
||||
@@ -1034,7 +1034,7 @@ namespace OpenNest.Geometry
|
||||
var distances = new List<double>();
|
||||
foreach (var s in AllNewSamples(@new))
|
||||
{
|
||||
var p = Rotate(s.Position, pose.Rotation) + pose.Translation;
|
||||
var p = s.Position.Rotate(pose.Rotation) + pose.Translation;
|
||||
if (TryClosestSegment(p, oldSegments, out var c))
|
||||
distances.Add(p.DistanceTo(c));
|
||||
}
|
||||
@@ -1096,7 +1096,7 @@ namespace OpenNest.Geometry
|
||||
var path = new PathD(points.Length);
|
||||
foreach (var p in points)
|
||||
{
|
||||
var q = Rotate(p, rotation) + translation;
|
||||
var q = p.Rotate(rotation) + translation;
|
||||
if (double.IsNaN(q.X) || double.IsNaN(q.Y))
|
||||
return null;
|
||||
path.Add(new PointD(q.X, q.Y));
|
||||
@@ -1137,13 +1137,6 @@ namespace OpenNest.Geometry
|
||||
return samples.Length > 0 ? sum * (1.0 / samples.Length) : new Vector();
|
||||
}
|
||||
|
||||
private static Vector Rotate(Vector v, double angle)
|
||||
{
|
||||
var cos = System.Math.Cos(angle);
|
||||
var sin = System.Math.Sin(angle);
|
||||
return new Vector(v.X * cos - v.Y * sin, v.X * sin + v.Y * cos);
|
||||
}
|
||||
|
||||
private static double NormalizeAngle(double angle)
|
||||
{
|
||||
var twoPi = 2 * System.Math.PI;
|
||||
|
||||
@@ -279,7 +279,7 @@ public static class MaximalRectangles
|
||||
|
||||
foreach (var larger in results)
|
||||
{
|
||||
if (IsContainedIn(box, larger))
|
||||
if (BoundingBox.IsWithin(box, larger))
|
||||
{
|
||||
dominated = true;
|
||||
break;
|
||||
@@ -292,13 +292,4 @@ public static class MaximalRectangles
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -741,7 +741,7 @@ public static class DrawingSplitter
|
||||
continue;
|
||||
|
||||
var bbB = shapes[j].BoundingBox;
|
||||
if (!BoxContainsBox(bbB, bbA))
|
||||
if (!BoundingBox.IsWithin(bbA, bbB))
|
||||
continue;
|
||||
if (!polygons[j].ContainsPoint(repA))
|
||||
continue;
|
||||
@@ -779,7 +779,7 @@ public static class DrawingSplitter
|
||||
continue;
|
||||
|
||||
var bbJ = shapes[j].BoundingBox;
|
||||
if (!BoxContainsBox(shapes[i].BoundingBox, bbJ))
|
||||
if (!BoundingBox.IsWithin(bbJ, shapes[i].BoundingBox))
|
||||
continue;
|
||||
|
||||
var rep = FirstVertexOf(shapes[j]);
|
||||
@@ -816,19 +816,6 @@ public static class DrawingSplitter
|
||||
return GetStartPoint(shape.Entities[0]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// True iff box <paramref name="inner"/> is entirely inside box
|
||||
/// <paramref name="outer"/> (tolerant comparison).
|
||||
/// </summary>
|
||||
private static bool BoxContainsBox(Box outer, Box inner)
|
||||
{
|
||||
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 static Vector GetStartPoint(Entity entity)
|
||||
{
|
||||
return entity switch
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
using OpenNest.Engine.Jobs.Cutouts;
|
||||
using OpenNest.Geometry;
|
||||
|
||||
namespace OpenNest.Engine.Tests.Jobs.Cutouts;
|
||||
|
||||
public class CutoutCoordinatesTests
|
||||
{
|
||||
private const double Max = CutoutCoordinates.MaxCoordinate;
|
||||
|
||||
[Fact]
|
||||
public void WithinRange_AcceptsTheLimitOnEverySide() =>
|
||||
Assert.True(CutoutCoordinates.WithinRange(new Box(-Max, -Max, 2 * Max, 2 * Max)));
|
||||
|
||||
[Theory]
|
||||
[InlineData(-2e8, 0, 2e8, 1)] // left only: right edge stays at 0
|
||||
[InlineData(0, 0, 2e8, 1)] // right
|
||||
[InlineData(0, -2e8, 1, 2e8)] // bottom only: top edge stays at 0
|
||||
[InlineData(0, 0, 1, 2e8)] // top
|
||||
public void WithinRange_RejectsASideBeyondTheLimit(double x, double y, double w, double h) =>
|
||||
Assert.False(CutoutCoordinates.WithinRange(new Box(x, y, w, h)));
|
||||
|
||||
[Theory]
|
||||
[InlineData(double.NaN, 0, 1, 1)]
|
||||
[InlineData(0, 0, double.PositiveInfinity, 1)]
|
||||
[InlineData(0, double.NegativeInfinity, 1, 1)]
|
||||
[InlineData(0, 0, 1, double.NaN)]
|
||||
public void WithinRange_RejectsNonFiniteSides(double x, double y, double w, double h) =>
|
||||
Assert.False(CutoutCoordinates.WithinRange(new Box(x, y, w, h)));
|
||||
}
|
||||
@@ -109,7 +109,7 @@ internal sealed class CuttingDependencyGraph
|
||||
token.ThrowIfCancellationRequested();
|
||||
// Bounds only select candidates; containment itself is proven on native material.
|
||||
if (inner != host && !nodes[inner].IsCutOff && !nodes[host].IsCutOff
|
||||
&& NestedPartContainment.Within(nodes[inner].CleanBounds, nodes[host].HostBounds))
|
||||
&& BoundingBox.IsWithin(nodes[inner].CleanBounds, nodes[host].HostBounds))
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -216,7 +216,9 @@ internal sealed class DependencyNode
|
||||
CutOff = cutOff;
|
||||
PlacedBounds = Copy(placedBounds);
|
||||
CleanBounds = cleanBounds == null ? null : Copy(cleanBounds);
|
||||
HostBounds = cleanBounds == null ? PlacedBounds : NestedPartContainment.Union(PlacedBounds, CleanBounds);
|
||||
HostBounds = cleanBounds == null
|
||||
? PlacedBounds
|
||||
: BoundingBox.Union(PlacedBounds, CleanBounds);
|
||||
this.materialFactory = materialFactory;
|
||||
}
|
||||
|
||||
|
||||
@@ -130,7 +130,7 @@ internal static class CutoutCirclePacker
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
var center = hole.Center + offset;
|
||||
var origin = center + Rotate(disc.Item.PartOffset, disc.Rotation);
|
||||
var origin = center + disc.Item.PartOffset.Rotate(disc.Rotation);
|
||||
var pose = new NestJobPlacement(disc.Part.Id,
|
||||
accepted.Count(p => p.Pose.PartId == disc.Part.Id), origin.X, origin.Y, disc.Rotation);
|
||||
var geometry = geometries[disc.Part.Id];
|
||||
@@ -247,11 +247,4 @@ internal static class CutoutCirclePacker
|
||||
var angle = System.Math.Atan2(p.X, -p.Y);
|
||||
return angle < 0 ? angle + 2 * System.Math.PI : angle;
|
||||
}
|
||||
|
||||
private static Vector Rotate(Vector v, double angle)
|
||||
{
|
||||
var sine = System.Math.Sin(angle);
|
||||
var cosine = System.Math.Cos(angle);
|
||||
return new Vector(v.X * cosine - v.Y * sine, v.X * sine + v.Y * cosine);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using OpenNest.Geometry;
|
||||
|
||||
namespace OpenNest.Engine.Jobs.Cutouts;
|
||||
|
||||
/// <summary>The coordinate range the cutout proposals accept.</summary>
|
||||
internal static class CutoutCoordinates
|
||||
{
|
||||
/// <summary>Largest coordinate magnitude the proposals process.</summary>
|
||||
internal const double MaxCoordinate = 1e8;
|
||||
|
||||
/// <summary>Whether every side of <paramref name="box"/> is finite and within range.</summary>
|
||||
internal static bool WithinRange(Box box) =>
|
||||
double.IsFinite(box.Left) && double.IsFinite(box.Right)
|
||||
&& double.IsFinite(box.Bottom) && double.IsFinite(box.Top)
|
||||
&& System.Math.Abs(box.Left) <= MaxCoordinate
|
||||
&& System.Math.Abs(box.Right) <= MaxCoordinate
|
||||
&& System.Math.Abs(box.Bottom) <= MaxCoordinate
|
||||
&& System.Math.Abs(box.Top) <= MaxCoordinate;
|
||||
}
|
||||
@@ -14,7 +14,6 @@ internal static class CutoutNfpProposal
|
||||
// Fixed chord tolerance in the shared round offset otherwise makes extremely large
|
||||
// spacing values allocate an unbounded number of arc vertices. This conservative
|
||||
// test-only kernel declines scales it cannot process safely.
|
||||
private const double MaxCoordinate = 1e8;
|
||||
private const double MaxSpacing = 100;
|
||||
|
||||
/// <summary>Returns one frame-local pose inside a closed cutout, or null if no pose is certified.
|
||||
@@ -38,12 +37,13 @@ internal static class CutoutNfpProposal
|
||||
var frameGeometry = JobPartGeometry.TryRead(frame.Geometry);
|
||||
var insertGeometry = JobPartGeometry.TryRead(insert.Geometry);
|
||||
if (frameGeometry == null || insertGeometry == null
|
||||
|| !WithinRange(frameGeometry.Bounds) || !WithinRange(insertGeometry.Bounds))
|
||||
|| !CutoutCoordinates.WithinRange(frameGeometry.Bounds)
|
||||
|| !CutoutCoordinates.WithinRange(insertGeometry.Bounds))
|
||||
return null;
|
||||
ArgumentOutOfRangeException.ThrowIfNegative(cutoutIndex);
|
||||
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(cutoutIndex, frameGeometry.Cutouts.Count);
|
||||
var cutout = frameGeometry.Cutouts[cutoutIndex];
|
||||
if (!cutout.IsClosed() || !WithinRange(cutout.BoundingBox)
|
||||
if (!cutout.IsClosed() || !CutoutCoordinates.WithinRange(cutout.BoundingBox)
|
||||
|| spacing * 2 >= System.Math.Min(cutout.BoundingBox.Length, cutout.BoundingBox.Width))
|
||||
return null;
|
||||
var hole = CutoutLatticeFill.Positive(ClipperBridge.ToPath(
|
||||
@@ -92,12 +92,6 @@ internal static class CutoutNfpProposal
|
||||
return null;
|
||||
}
|
||||
|
||||
private static bool WithinRange(Box box) =>
|
||||
double.IsFinite(box.Left) && double.IsFinite(box.Right)
|
||||
&& double.IsFinite(box.Bottom) && double.IsFinite(box.Top)
|
||||
&& System.Math.Abs(box.Left) <= MaxCoordinate && System.Math.Abs(box.Right) <= MaxCoordinate
|
||||
&& System.Math.Abs(box.Bottom) <= MaxCoordinate && System.Math.Abs(box.Top) <= MaxCoordinate;
|
||||
|
||||
/// <summary>Sample component interiors, then points just inside each boundary toward
|
||||
/// its component's center. This bounded search is not a complete inner-fit solver.</summary>
|
||||
private static IEnumerable<(double X, double Y)> Candidates(PathsD region)
|
||||
|
||||
@@ -20,7 +20,6 @@ namespace OpenNest.Engine.Jobs.Cutouts;
|
||||
/// </summary>
|
||||
internal static class CutoutPackerProposal
|
||||
{
|
||||
private const double MaxCoordinate = 1e8;
|
||||
private const double MaxSpacing = 100;
|
||||
private const double Tie = 1e-6;
|
||||
|
||||
@@ -44,19 +43,19 @@ internal static class CutoutPackerProposal
|
||||
if (inserts.Count == 0 || !double.IsFinite(spacing) || spacing < 0 || spacing > MaxSpacing)
|
||||
return Array.Empty<NestJobPlacement>();
|
||||
var frameGeometry = memo?.Read(frame.Geometry) ?? JobPartGeometry.TryRead(frame.Geometry);
|
||||
if (frameGeometry == null || !WithinRange(frameGeometry.Bounds))
|
||||
if (frameGeometry == null || !CutoutCoordinates.WithinRange(frameGeometry.Bounds))
|
||||
return Array.Empty<NestJobPlacement>();
|
||||
ArgumentOutOfRangeException.ThrowIfNegative(cutoutIndex);
|
||||
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(cutoutIndex, frameGeometry.Cutouts.Count);
|
||||
var cutout = frameGeometry.Cutouts[cutoutIndex];
|
||||
if (!cutout.IsClosed() || !WithinRange(cutout.BoundingBox)
|
||||
if (!cutout.IsClosed() || !CutoutCoordinates.WithinRange(cutout.BoundingBox)
|
||||
|| spacing * 2 >= System.Math.Min(cutout.BoundingBox.Length, cutout.BoundingBox.Width))
|
||||
return Array.Empty<NestJobPlacement>();
|
||||
var geometries = new Dictionary<string, JobPartGeometry>(StringComparer.Ordinal);
|
||||
foreach (var insert in inserts)
|
||||
{
|
||||
var geometry = memo?.Read(insert.Geometry) ?? JobPartGeometry.TryRead(insert.Geometry);
|
||||
if (geometry == null || !WithinRange(geometry.Bounds))
|
||||
if (geometry == null || !CutoutCoordinates.WithinRange(geometry.Bounds))
|
||||
return Array.Empty<NestJobPlacement>();
|
||||
geometries[insert.Id] = geometry;
|
||||
}
|
||||
@@ -194,10 +193,4 @@ internal static class CutoutPackerProposal
|
||||
new Vector(pose.X, pose.Y));
|
||||
return (CutoutLatticeFill.Positive(path), CutoutLatticeFill.FlattenTolerance);
|
||||
}
|
||||
|
||||
private static bool WithinRange(Box box) =>
|
||||
double.IsFinite(box.Left) && double.IsFinite(box.Right)
|
||||
&& double.IsFinite(box.Bottom) && double.IsFinite(box.Top)
|
||||
&& System.Math.Abs(box.Left) <= MaxCoordinate && System.Math.Abs(box.Right) <= MaxCoordinate
|
||||
&& System.Math.Abs(box.Bottom) <= MaxCoordinate && System.Math.Abs(box.Top) <= MaxCoordinate;
|
||||
}
|
||||
@@ -264,10 +264,10 @@ internal static class DiscPacker
|
||||
foreach (var (type, center) in placed)
|
||||
{
|
||||
counts[type.Index]++;
|
||||
var origin = center + Rotate(type.Item.PartOffset, type.Rotation);
|
||||
var origin = center + type.Item.PartOffset.Rotate(type.Rotation);
|
||||
poses.Add((type.Id, origin.X, origin.Y, type.Rotation));
|
||||
var box = new Box(center.X - type.Radius, center.Y - type.Radius, 2 * type.Radius, 2 * type.Radius);
|
||||
envelope = envelope == null ? box : Union(envelope, box);
|
||||
envelope = envelope == null ? box : BoundingBox.Union(envelope, box);
|
||||
}
|
||||
// Summed per type in type order, the form of the stock loop's demand area.
|
||||
var area = types.Sum(t => counts[t.Index] * t.MaterialArea);
|
||||
@@ -346,20 +346,4 @@ internal static class DiscPacker
|
||||
if (across > Epsilon)
|
||||
yield return new Vector(mx + uy * across, my - ux * across);
|
||||
}
|
||||
|
||||
private static Vector Rotate(Vector v, double angle)
|
||||
{
|
||||
var sine = System.Math.Sin(angle);
|
||||
var cosine = System.Math.Cos(angle);
|
||||
return new Vector(v.X * cosine - v.Y * sine, v.X * sine + v.Y * cosine);
|
||||
}
|
||||
|
||||
private static Box Union(Box a, Box b)
|
||||
{
|
||||
var l = System.Math.Min(a.Left, b.Left);
|
||||
var bo = System.Math.Min(a.Bottom, b.Bottom);
|
||||
var r = System.Math.Max(a.Right, b.Right);
|
||||
var t = System.Math.Max(a.Top, b.Top);
|
||||
return new Box(l, bo, r - l, t - bo);
|
||||
}
|
||||
}
|
||||
@@ -76,7 +76,7 @@ internal static class SheetPacker
|
||||
{
|
||||
counts[p.Type.Index]++;
|
||||
var box = new Box(work.Left + p.Left, work.Bottom + p.Bottom, p.Orientation.Width, p.Orientation.Height);
|
||||
envelope = envelope == null ? box : Union(envelope, box);
|
||||
envelope = envelope == null ? box : BoundingBox.Union(envelope, box);
|
||||
}
|
||||
// Summed per type in type order, the same form as the engine's demand area: plans placing
|
||||
// the same parts get bit-identical areas, so the engine's material-area tie-break cannot
|
||||
@@ -141,13 +141,4 @@ internal static class SheetPacker
|
||||
sheet.Block(new Rect(left, bottom, right - left, top - bottom));
|
||||
}
|
||||
}
|
||||
|
||||
private static Box Union(Box a, Box b)
|
||||
{
|
||||
var l = System.Math.Min(a.Left, b.Left);
|
||||
var bo = System.Math.Min(a.Bottom, b.Bottom);
|
||||
var r = System.Math.Max(a.Right, b.Right);
|
||||
var t = System.Math.Max(a.Top, b.Top);
|
||||
return new Box(l, bo, r - l, t - bo);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
using System;
|
||||
using OpenNest.Geometry;
|
||||
using OpenNest.Math;
|
||||
using Xunit;
|
||||
|
||||
namespace OpenNest.Tests.Geometry;
|
||||
|
||||
/// <summary>
|
||||
/// The shared box and rotation helpers replaced private copies in the splitter, maximal
|
||||
/// rectangles, nested-part review, packers and aligner. The frozen copies below are those
|
||||
/// donors' bodies; the shared helpers must return the same bits.
|
||||
/// </summary>
|
||||
public class BoundingBoxHelperTests
|
||||
{
|
||||
private const double Eps = Tolerance.Epsilon;
|
||||
|
||||
private static readonly Box Outer = new(0, 0, 10, 10);
|
||||
|
||||
[Theory]
|
||||
[InlineData(-1, 0, 11, 10)] // left
|
||||
[InlineData(0, 0, 11, 10)] // right
|
||||
[InlineData(0, -1, 10, 11)] // bottom
|
||||
[InlineData(0, 0, 10, 11)] // top
|
||||
public void IsWithin_AllowsEpsilonOnEachSideButNotMore(double x, double y, double w, double h)
|
||||
{
|
||||
Box Grown(double d) => new(x * d, y * d, 10 + (w - 10) * d, 10 + (h - 10) * d);
|
||||
|
||||
Assert.True(BoundingBox.IsWithin(Grown(Eps), Outer));
|
||||
Assert.False(BoundingBox.IsWithin(Grown(2 * Eps), Outer));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsWithin_ArgumentOrderIsInnerThenOuter()
|
||||
{
|
||||
var inner = new Box(2, 2, 3, 3);
|
||||
|
||||
Assert.True(BoundingBox.IsWithin(inner, Outer));
|
||||
Assert.False(BoundingBox.IsWithin(Outer, inner));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Helpers_MatchFrozenDonorsBitForBit()
|
||||
{
|
||||
var random = new Random(20261101);
|
||||
for (var i = 0; i < 2000; i++)
|
||||
{
|
||||
var a = RandomBox(random);
|
||||
var b = RandomBox(random);
|
||||
|
||||
Assert.Equal(FrozenIsContainedIn(a, b), BoundingBox.IsWithin(a, b));
|
||||
AssertSameBits(FrozenUnion(a, b), BoundingBox.Union(a, b));
|
||||
|
||||
var v = new Vector(Value(random), Value(random));
|
||||
var angle = (random.NextDouble() - 0.5) * 100;
|
||||
var expected = FrozenRotate(v, angle);
|
||||
var actual = v.Rotate(angle);
|
||||
AssertSameBits(expected.X, actual.X);
|
||||
AssertSameBits(expected.Y, actual.Y);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Union_PropagatesNaNLikeTheDonors()
|
||||
{
|
||||
var a = new Box(double.NaN, 0, 1, 1);
|
||||
var b = new Box(0, 0, 2, 2);
|
||||
|
||||
AssertSameBits(FrozenUnion(a, b), BoundingBox.Union(a, b));
|
||||
Assert.False(BoundingBox.IsWithin(a, b));
|
||||
}
|
||||
|
||||
private static double Value(Random random) => (random.NextDouble() - 0.5) * 1e3;
|
||||
|
||||
private static Box RandomBox(Random random)
|
||||
{
|
||||
// Near-equal boxes exercise the epsilon edges as well as clear in/out cases.
|
||||
var scale = random.Next(2) == 0 ? 1e-9 : 10;
|
||||
return new Box(random.NextDouble() * scale, random.NextDouble() * scale,
|
||||
random.NextDouble() * scale, random.NextDouble() * scale);
|
||||
}
|
||||
|
||||
private static void AssertSameBits(Box expected, Box actual)
|
||||
{
|
||||
AssertSameBits(expected.Left, actual.Left);
|
||||
AssertSameBits(expected.Bottom, actual.Bottom);
|
||||
AssertSameBits(expected.Right, actual.Right);
|
||||
AssertSameBits(expected.Top, actual.Top);
|
||||
}
|
||||
|
||||
private static void AssertSameBits(double expected, double actual) =>
|
||||
Assert.Equal(
|
||||
BitConverter.DoubleToInt64Bits(expected), BitConverter.DoubleToInt64Bits(actual));
|
||||
|
||||
// Frozen from MaximalRectangles.IsContainedIn (identical to DrawingSplitter.BoxContainsBox
|
||||
// and NestedPartContainment.Within).
|
||||
private static bool FrozenIsContainedIn(Box inner, Box outer)
|
||||
{
|
||||
var eps = Tolerance.Epsilon;
|
||||
return inner.Left >= outer.Left - eps
|
||||
&& inner.Right <= outer.Right + eps
|
||||
&& inner.Bottom >= outer.Bottom - eps
|
||||
&& inner.Top <= outer.Top + eps;
|
||||
}
|
||||
|
||||
// Frozen from DiscPacker.Union / SheetPacker.Union / NestedPartContainment.Union.
|
||||
private static Box FrozenUnion(Box a, Box b)
|
||||
{
|
||||
var l = System.Math.Min(a.Left, b.Left);
|
||||
var bo = System.Math.Min(a.Bottom, b.Bottom);
|
||||
var r = System.Math.Max(a.Right, b.Right);
|
||||
var t = System.Math.Max(a.Top, b.Top);
|
||||
return new Box(l, bo, r - l, t - bo);
|
||||
}
|
||||
|
||||
// Frozen from DiscPacker.Rotate / CutoutCirclePacker.Rotate / DrawingAligner.Rotate.
|
||||
private static Vector FrozenRotate(Vector v, double angle)
|
||||
{
|
||||
var sine = System.Math.Sin(angle);
|
||||
var cosine = System.Math.Cos(angle);
|
||||
return new Vector(v.X * cosine - v.Y * sine, v.X * sine + v.Y * cosine);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user