fix(core): stop a part turning a program its offset copies share

Part.CloneAtOffset hands the copy the source's Program instance. The copy
cloned before rotating, but the source still owned the program, so rotating
the source turned the program under every tiled copy while their location
and bounds stayed put.

Program now carries a sticky SharedByParts flag that CloneAtOffset sets, and
a part copies its program before rotating when it does not own it or the
program is shared. The flag lives on the program rather than the part so a
restored cutting state, which reinstates ownership field for field, cannot
re-alias a shared program. Unrotated copies keep sharing one instance, which
the fill and overlap caches key on.

Update, ApplyLeadIns and ApplySingleLeadIn install a fresh program and now
own it, saving a needless copy on the next rotate.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Fable 5.2 committed 2026-11-12 19:09:28 -05:00
1 parent febeb76836
commit 32edc18ede
3 files changed
+241 -3

No files matched your search

+10
View File
@@ -37,6 +37,16 @@ namespace OpenNest.CNC
public double Rotation { get; protected set; }
/// <summary>
/// True once <see cref="Part.CloneAtOffset"/> has handed this instance to a second
/// part. A part must copy a shared program before changing it in place, whether or not
/// it owns it. Never cleared: the parts sharing it are not tracked, so a program that
/// was shared once is treated as shared for good, at the cost of one copy. It belongs
/// to the instance, not its content: <see cref="Clone"/> and <see cref="HeadCopy"/>
/// build a new program and leave it unset.
/// </summary>
internal bool SharedByParts { get; set; }
private void SetModeInc()
{
if (mode == Mode.Incremental)
+19 -3
View File
@@ -105,6 +105,7 @@ namespace OpenNest
};
var result = strategy.Apply(Program, approachPoint, nextPartStart);
Program = result.Program;
ownsProgram = true;
CuttingParameters = parameters;
HasManualLeadIns = true;
UpdateBounds();
@@ -124,6 +125,7 @@ namespace OpenNest
};
var result = strategy.ApplySingle(Program, point, entity, contourType);
Program = result.Program;
ownsProgram = true;
CuttingParameters = parameters;
HasManualLeadIns = true;
UpdateBounds();
@@ -315,6 +317,7 @@ namespace OpenNest
{
var rotation = Rotation;
Program = BaseDrawing.Program.Clone() as Program;
ownsProgram = true;
if (!Math.Tolerance.IsEqualTo(rotation, 0))
Program.Rotate(rotation);
@@ -428,12 +431,19 @@ namespace OpenNest
/// <summary>
/// Creates an offset copy of the part. Clones from the already-rotated
/// program (skips re-rotation) and computes the bounding box arithmetically
/// (skips Program.BoundingBox walk).
/// (skips Program.BoundingBox walk). The copy shares this part's program instance, so
/// the program is marked shared and neither part turns it in place afterwards.
/// </summary>
public Part CloneAtOffset(Vector offset)
{
// Share the Program instance — offset-only copies don't modify the program codes.
// This is a major performance win for tiling large patterns.
// This is a major performance win for tiling large patterns, and the fill and
// overlap caches key on the instance. Tiling copies one program many times, possibly
// from several threads: the flag only ever goes to true, so racing writers agree,
// and testing first keeps the hot loop from rewriting a line other threads read.
if (!Program.SharedByParts)
Program.SharedByParts = true;
var part = new Part(
BaseDrawing,
Program,
@@ -465,9 +475,15 @@ namespace OpenNest
preLeadInRotation = source.preLeadInRotation;
}
/// <summary>
/// Gives the part a program no other part holds before it is changed in place. Owning
/// the program is not enough: <see cref="CloneAtOffset"/> hands the same instance to
/// its copies and leaves the source an owner, and a restored cutting state can make a
/// part an owner of a shared program again.
/// </summary>
private void EnsureOwnedProgram()
{
if (!ownsProgram)
if (!ownsProgram || Program.SharedByParts)
{
Program = Program.Clone() as Program;
ownsProgram = true;
+212
View File
@@ -0,0 +1,212 @@
using OpenNest.CNC;
using OpenNest.CNC.CuttingPlanning;
using OpenNest.Geometry;
namespace OpenNest.Tests;
/// <summary>
/// <see cref="Part.CloneAtOffset"/> shares one program between the source and its copies. No
/// part may turn that program in place while another part still reads it.
/// </summary>
public class PartProgramSharingTests
{
private const double QuarterTurn = System.Math.PI / 2;
private static Part MakeSource()
{
// 4 x 1, so a quarter turn changes the bounds.
var pgm = new Program();
pgm.Codes.Add(new RapidMove(new Vector(0, 0)));
pgm.Codes.Add(new LinearMove(new Vector(0, 1)));
pgm.Codes.Add(new LinearMove(new Vector(4, 1)));
pgm.Codes.Add(new LinearMove(new Vector(4, 0)));
pgm.Codes.Add(new LinearMove(new Vector(0, 0)));
return new Part(new Drawing("test", pgm), new Vector(2, 3));
}
private static double[] Bounds(Part part) =>
[part.BoundingBox.X, part.BoundingBox.Y, part.BoundingBox.Length, part.BoundingBox.Width];
private static void AssertUntouched(Part part, Program expectedProgram, double expectedRotation,
double[] expectedBounds)
{
Assert.True(ProgramContent.Equal(part.Program, expectedProgram));
Assert.Equal(expectedRotation, part.Rotation);
Assert.Equal(expectedRotation, part.Program.Rotation);
Assert.Equal(expectedBounds, Bounds(part));
// The stored bounds must still be the bounds of the program the part holds.
var actual = part.Program.BoundingBox();
actual.Offset(part.Location);
Assert.Equal(expectedBounds[0], actual.X, 9);
Assert.Equal(expectedBounds[1], actual.Y, 9);
Assert.Equal(expectedBounds[2], actual.Length, 9);
Assert.Equal(expectedBounds[3], actual.Width, 9);
}
[Fact]
public void RotatingTheSource_LeavesTheCopyUnchanged()
{
var source = MakeSource();
var copy = source.CloneAtOffset(new Vector(10, 0));
var program = (Program)copy.Program.Clone();
var rotation = copy.Rotation;
var bounds = Bounds(copy);
source.Rotate(QuarterTurn);
AssertUntouched(copy, program, rotation, bounds);
Assert.NotSame(source.Program, copy.Program);
Assert.Equal(QuarterTurn, source.Rotation, 9);
}
[Fact]
public void RotatingTheSourceAroundAnOrigin_LeavesTheCopyUnchanged()
{
var source = MakeSource();
var copy = source.CloneAtOffset(new Vector(10, 0));
var program = (Program)copy.Program.Clone();
var rotation = copy.Rotation;
var bounds = Bounds(copy);
source.Rotate(QuarterTurn, new Vector(5, 5));
AssertUntouched(copy, program, rotation, bounds);
}
[Fact]
public void RotatingTheCopy_LeavesTheSourceUnchanged()
{
var source = MakeSource();
var copy = source.CloneAtOffset(new Vector(10, 0));
var program = (Program)source.Program.Clone();
var rotation = source.Rotation;
var bounds = Bounds(source);
copy.Rotate(QuarterTurn);
AssertUntouched(source, program, rotation, bounds);
Assert.NotSame(source.Program, copy.Program);
Assert.Equal(QuarterTurn, copy.Rotation, 9);
}
[Fact]
public void RotatingOneCopy_LeavesItsSiblingUnchanged()
{
var source = MakeSource();
var first = source.CloneAtOffset(new Vector(10, 0));
var second = first.CloneAtOffset(new Vector(10, 0));
var program = (Program)second.Program.Clone();
var rotation = second.Rotation;
var bounds = Bounds(second);
first.Rotate(QuarterTurn);
source.Rotate(-QuarterTurn);
AssertUntouched(second, program, rotation, bounds);
}
[Fact]
public void UnrotatedCopies_ShareTheSourceProgram()
{
var source = MakeSource();
var program = source.Program;
var first = source.CloneAtOffset(new Vector(10, 0));
var second = source.CloneAtOffset(new Vector(20, 0));
var third = first.CloneAtOffset(new Vector(0, 10));
first.Offset(1, 1);
Assert.Same(program, source.Program);
Assert.Same(program, first.Program);
Assert.Same(program, second.Program);
Assert.Same(program, third.Program);
Assert.True(program.SharedByParts);
}
[Fact]
public void CopiedProgram_StartsUnshared()
{
var source = MakeSource();
source.CloneAtOffset(new Vector(10, 0));
Assert.True(source.Program.SharedByParts);
Assert.False(((Program)source.Program.Clone()).SharedByParts);
Assert.False(source.Program.HeadCopy(2).SharedByParts);
Assert.False(((Part)source.Clone()).Program.SharedByParts);
// The part that turned gets a private program; turning it again copies nothing.
source.Rotate(QuarterTurn);
var program = source.Program;
Assert.False(program.SharedByParts);
source.Rotate(QuarterTurn);
Assert.Same(program, source.Program);
}
[Fact]
public void UnsharedPart_RotatesItsProgramInPlace()
{
var source = MakeSource();
var program = source.Program;
source.Rotate(QuarterTurn);
Assert.Same(program, source.Program);
}
[Fact]
public void RestoredCuttingState_RoundTripsAndStillProtectsTheCopy()
{
var source = MakeSource();
var state = source.CaptureCuttingState();
var sourceProgram = (Program)source.Program.Clone();
var sourceBounds = Bounds(source);
var copy = source.CloneAtOffset(new Vector(10, 0));
var copyProgram = (Program)copy.Program.Clone();
var copyRotation = copy.Rotation;
var copyBounds = Bounds(copy);
source.Rotate(QuarterTurn);
source.RestoreCuttingState(state);
// Field-exact: the same record comes back, references included.
Assert.Equal(state, source.CaptureCuttingState());
Assert.Same(state.Program, source.Program);
Assert.Same(state.BoundingBox, source.BoundingBox);
Assert.True(ProgramContent.Equal(source.Program, sourceProgram));
Assert.Equal(sourceBounds, Bounds(source));
// The restore re-aliased the copy's program with an owning part.
Assert.Same(source.Program, copy.Program);
source.Rotate(QuarterTurn);
AssertUntouched(copy, copyProgram, copyRotation, copyBounds);
Assert.NotSame(source.Program, copy.Program);
}
[Fact]
public void RotateAfterUpdate_KeepsTheProgramInstance()
{
var copy = MakeSource().CloneAtOffset(new Vector(10, 0));
copy.Update();
var program = copy.Program;
copy.Rotate(QuarterTurn);
Assert.Same(program, copy.Program);
}
[Fact]
public void RotateAfterApplyLeadIns_KeepsTheProgramInstance()
{
var copy = MakeSource().CloneAtOffset(new Vector(10, 0));
copy.ApplyLeadIns(new OpenNest.CNC.CuttingStrategy.CuttingParameters(), new Vector(-5, -5));
var program = copy.Program;
copy.Rotate(QuarterTurn);
Assert.Same(program, copy.Program);
}
}