fix(engine): make canonical-frame fills orientation-invariant

Part.Rotation is cumulative, so rebinding canonical parts with
CreateAtOrigin(original, p.Rotation) double-counted the drawing's own
rotation, and FromCanonical rotated each part about its Location, which
moved it off its slot and out of the work area. Add
CanonicalFrame.RebindToOriginal (rotation = part - original program
rotation, footprint aligned to the canonical part) and use it in the
three places that duplicated the old logic.

The MBR only fixes the frame modulo 90 degrees and nest results are not
90-degree symmetric (an L gave 56/43/42/42 parts by orientation).
CanonicalAngle.Compute now picks one of the four orientations from the
centroid offset; symmetric shapes keep the MBR orientation.

Fixes the three NestInvarianceTests.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
aj
2026-09-21 11:07:44 -04:00
co-authored by Claude Sonnet 5
parent 2a855139d2
commit a764a70e52
7 changed files with 221 additions and 68 deletions
+32
View File
@@ -47,6 +47,38 @@ namespace OpenNest.Engine
return copy;
}
/// <summary>
/// Rebinds canonical-frame placed parts to the original drawing while preserving each
/// part's world footprint.
///
/// <see cref="Part.Rotation"/> is cumulative: it includes the rotation already baked into
/// the drawing's program. The canonical copy carries original + sourceAngle, so a canonical
/// part's rotation minus the original program's rotation is exactly the rotation (engine
/// rotation plus sourceAngle) that turns the original into the same shape. Each part is
/// rebuilt from the original at that rotation and translated to sit where the canonical
/// part did. Rotating a finished part about its Location instead would shift it out of place.
/// </summary>
public static List<Part> RebindToOriginal(List<Part> canonicalParts, Drawing original)
{
if (canonicalParts == null || canonicalParts.Count == 0)
return canonicalParts;
var baseRotation = original.Program.Rotation;
for (var i = 0; i < canonicalParts.Count; i++)
{
var canonical = canonicalParts[i];
var rebound = Part.CreateAtOrigin(
original,
Angle.NormalizeRad(canonical.Rotation - baseRotation)
);
rebound.Offset(canonical.BoundingBox.Location - rebound.BoundingBox.Location);
rebound.UpdateBounds();
canonicalParts[i] = rebound;
}
return canonicalParts;
}
/// <summary>
/// Composes the source drawing's canonical angle onto each placed part so the
/// returned list is in the drawing's original (visible) frame.