mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-10-19 22:21:39 -04:00
105 lines
3.8 KiB
C#
105 lines
3.8 KiB
C#
using System;
|
|
using OpenNest.CNC;
|
|
|
|
namespace OpenNest.Diagnostics;
|
|
|
|
/// <summary>
|
|
/// Cheap ordered identity/pose check, not a geometry hash. In-place geometry editors must
|
|
/// explicitly invalidate before mutating. Capture and match only on the model's UI thread.
|
|
/// </summary>
|
|
public sealed class OverlapGeometryStamp
|
|
{
|
|
private readonly Plate plate;
|
|
private readonly Entry[] entries;
|
|
private readonly (string Id, long X, long Y, long Length, long Width)[] zones;
|
|
|
|
private OverlapGeometryStamp(Plate plate)
|
|
{
|
|
this.plate = plate;
|
|
entries = new Entry[plate.Parts.Count];
|
|
for (var i = 0; i < entries.Length; i++)
|
|
entries[i] = new Entry(plate.Parts[i]);
|
|
zones = new (string, long, long, long, long)[plate.ClampZones.Count];
|
|
for (var i = 0; i < zones.Length; i++)
|
|
{
|
|
var zone = plate.ClampZones[i];
|
|
var box = zone.Bounds;
|
|
zones[i] = (zone.Id, Bits(box.X), Bits(box.Y), Bits(box.Length), Bits(box.Width));
|
|
}
|
|
}
|
|
|
|
private static long Bits(double value) => BitConverter.DoubleToInt64Bits(value);
|
|
|
|
public static OverlapGeometryStamp Capture(Plate plate) => new(plate);
|
|
|
|
internal bool SameZones(Plate current)
|
|
{
|
|
if (!ReferenceEquals(plate, current) || current.ClampZones.Count != zones.Length)
|
|
return false;
|
|
for (var i = 0; i < zones.Length; i++)
|
|
{
|
|
var zone = current.ClampZones[i];
|
|
var box = zone.Bounds;
|
|
if (zones[i] != (zone.Id, Bits(box.X), Bits(box.Y), Bits(box.Length), Bits(box.Width)))
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public bool Matches(Plate current)
|
|
{
|
|
if (current.Parts.Count != entries.Length || !SameZones(current))
|
|
return false;
|
|
for (var i = 0; i < entries.Length; i++)
|
|
if (!entries[i].Matches(current.Parts[i]))
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Conservative display reuse: only unchanged ordered slots on the same plate survive.
|
|
/// Index-changing edits may hide extra pairs, but cannot attach old geometry to new parts.
|
|
/// </summary>
|
|
internal bool[] UnchangedSlots(Plate current)
|
|
{
|
|
var matches = new bool[entries.Length];
|
|
if (!ReferenceEquals(plate, current))
|
|
return matches;
|
|
for (var i = 0; i < entries.Length && i < current.Parts.Count; i++)
|
|
matches[i] = entries[i].Matches(current.Parts[i]);
|
|
return matches;
|
|
}
|
|
|
|
private readonly struct Entry
|
|
{
|
|
private readonly Part part;
|
|
private readonly Drawing drawing;
|
|
private readonly Program placedProgram;
|
|
private readonly Program cleanProgram;
|
|
private readonly long x, y, rotation;
|
|
private readonly bool isCutOff;
|
|
|
|
public Entry(Part part)
|
|
{
|
|
this.part = part;
|
|
drawing = part.BaseDrawing;
|
|
placedProgram = part.Program;
|
|
cleanProgram = drawing.Program;
|
|
x = BitConverter.DoubleToInt64Bits(part.Location.X);
|
|
y = BitConverter.DoubleToInt64Bits(part.Location.Y);
|
|
rotation = BitConverter.DoubleToInt64Bits(part.Rotation);
|
|
isCutOff = drawing.IsCutOff;
|
|
}
|
|
|
|
public bool Matches(Part current) =>
|
|
ReferenceEquals(part, current)
|
|
&& ReferenceEquals(drawing, current.BaseDrawing)
|
|
&& ReferenceEquals(placedProgram, current.Program)
|
|
&& ReferenceEquals(cleanProgram, current.BaseDrawing.Program)
|
|
&& x == BitConverter.DoubleToInt64Bits(current.Location.X)
|
|
&& y == BitConverter.DoubleToInt64Bits(current.Location.Y)
|
|
&& rotation == BitConverter.DoubleToInt64Bits(current.Rotation)
|
|
&& isCutOff == current.BaseDrawing.IsCutOff;
|
|
}
|
|
}
|