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:
@@ -70,19 +70,8 @@ namespace OpenNest.Engine.BestFit
|
||||
public List<Part> BuildSourceParts(Drawing drawing)
|
||||
{
|
||||
var parts = BuildCanonicalParts();
|
||||
var sourceAngle = drawing?.Source?.Angle ?? 0.0;
|
||||
|
||||
for (var i = 0; i < parts.Count; i++)
|
||||
{
|
||||
var p = parts[i];
|
||||
var rebound = Part.CreateAtOrigin(drawing, p.Rotation);
|
||||
var delta = p.BoundingBox.Location - rebound.BoundingBox.Location;
|
||||
rebound.Offset(delta);
|
||||
rebound.UpdateBounds();
|
||||
parts[i] = rebound;
|
||||
}
|
||||
|
||||
return NormalizeToCutOrigin(CanonicalFrame.FromCanonical(parts, sourceAngle));
|
||||
return NormalizeToCutOrigin(CanonicalFrame.RebindToOriginal(parts, drawing));
|
||||
}
|
||||
|
||||
public Box GetCutBounds(List<Part> parts)
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -59,7 +59,6 @@ namespace OpenNest
|
||||
|
||||
// Replace the item's Drawing with a canonical copy for the duration of this fill.
|
||||
// All internal methods see canonical geometry; this wrapper un-canonicalizes the final result.
|
||||
var sourceAngle = item.Drawing?.Source?.Angle ?? 0.0;
|
||||
var originalDrawing = item.Drawing;
|
||||
var canonicalItem = new NestItem
|
||||
{
|
||||
@@ -81,7 +80,7 @@ namespace OpenNest
|
||||
$"[Fill] Fast path: placed {fast.Count} parts for qty={canonicalItem.Quantity}"
|
||||
);
|
||||
WinnerPhase = NestPhase.Pairs;
|
||||
fast = RebindAndUnCanonicalize(fast, originalDrawing, sourceAngle);
|
||||
fast = RebindAndUnCanonicalize(fast, originalDrawing);
|
||||
ReportProgress(
|
||||
progress,
|
||||
new ProgressReport
|
||||
@@ -129,7 +128,7 @@ namespace OpenNest
|
||||
if (canonicalItem.Quantity > 0 && best.Count > canonicalItem.Quantity)
|
||||
best = ShrinkFiller.TrimToCount(best, canonicalItem.Quantity, TrimAxis);
|
||||
|
||||
best = RebindAndUnCanonicalize(best, originalDrawing, sourceAngle);
|
||||
best = RebindAndUnCanonicalize(best, originalDrawing);
|
||||
|
||||
ReportProgress(
|
||||
progress,
|
||||
@@ -150,31 +149,10 @@ namespace OpenNest
|
||||
/// <summary>
|
||||
/// Single exit point for canonical -> source frame conversion. Rebinds every Part to the
|
||||
/// original Drawing (so consumers see the user's drawing identity, not the transient canonical copy)
|
||||
/// and composes sourceAngle onto each Part's rotation via CanonicalFrame.FromCanonical.
|
||||
/// and composes the canonical angle onto each Part's rotation via CanonicalFrame.RebindToOriginal.
|
||||
/// </summary>
|
||||
private static List<Part> RebindAndUnCanonicalize(
|
||||
List<Part> parts,
|
||||
Drawing original,
|
||||
double sourceAngle
|
||||
)
|
||||
{
|
||||
if (parts == null || parts.Count == 0)
|
||||
return parts;
|
||||
|
||||
for (var i = 0; i < parts.Count; i++)
|
||||
{
|
||||
var p = parts[i];
|
||||
// Rebind to `original` while preserving world pose. CreateAtOrigin rotates
|
||||
// at the origin (keeping bbox at world (0,0)) then we offset to match p's bbox.
|
||||
var rebound = Part.CreateAtOrigin(original, p.Rotation);
|
||||
var delta = p.BoundingBox.Location - rebound.BoundingBox.Location;
|
||||
rebound.Offset(delta);
|
||||
rebound.UpdateBounds();
|
||||
parts[i] = rebound;
|
||||
}
|
||||
|
||||
return CanonicalFrame.FromCanonical(parts, sourceAngle);
|
||||
}
|
||||
private static List<Part> RebindAndUnCanonicalize(List<Part> parts, Drawing original) =>
|
||||
CanonicalFrame.RebindToOriginal(parts, original);
|
||||
|
||||
/// <summary>
|
||||
/// Fast path for qty 1-2: place a single part or a best-fit pair
|
||||
|
||||
@@ -393,7 +393,6 @@ namespace OpenNest
|
||||
// from a canonical drawing copy so geometry and coords share a frame; rebind
|
||||
// + un-rotate winning pair to the original drawing's frame before returning.
|
||||
var canonicalDrawing = CanonicalFrame.AsCanonicalCopy(item.Drawing);
|
||||
var sourceAngle = item.Drawing?.Source?.Angle ?? 0.0;
|
||||
|
||||
List<Part> bestPlacement = null;
|
||||
Box bestTarget = null;
|
||||
@@ -438,9 +437,9 @@ namespace OpenNest
|
||||
if (bestPlacement == null)
|
||||
continue;
|
||||
|
||||
// Rebind to the original drawing and compose sourceAngle onto rotation so the
|
||||
// final placed parts sit in the user's visible frame.
|
||||
bestPlacement = RebindPairToOriginal(bestPlacement, item.Drawing, sourceAngle);
|
||||
// Rebind to the original drawing and compose the canonical angle onto rotation so
|
||||
// the final placed parts sit in the user's visible frame.
|
||||
bestPlacement = RebindPairToOriginal(bestPlacement, item.Drawing);
|
||||
|
||||
result.AddRange(bestPlacement);
|
||||
item.Quantity = 0;
|
||||
@@ -460,31 +459,12 @@ namespace OpenNest
|
||||
|
||||
/// <summary>
|
||||
/// Rebinds each canonical-frame Part in the pair to the original Drawing at its current
|
||||
/// world pose, then composes sourceAngle onto each via CanonicalFrame.FromCanonical so
|
||||
/// the returned list is in the original drawing's visible frame. Mirrors
|
||||
/// DefaultNestEngine.RebindAndUnCanonicalize.
|
||||
/// world pose, then composes the canonical angle onto each via
|
||||
/// CanonicalFrame.RebindToOriginal so the returned list is in the original drawing's
|
||||
/// visible frame. Mirrors DefaultNestEngine.RebindAndUnCanonicalize.
|
||||
/// </summary>
|
||||
private static List<Part> RebindPairToOriginal(
|
||||
List<Part> parts,
|
||||
Drawing original,
|
||||
double sourceAngle
|
||||
)
|
||||
{
|
||||
if (parts == null || parts.Count == 0)
|
||||
return parts;
|
||||
|
||||
for (var i = 0; i < parts.Count; i++)
|
||||
{
|
||||
var p = parts[i];
|
||||
var rebound = Part.CreateAtOrigin(original, p.Rotation);
|
||||
var delta = p.BoundingBox.Location - rebound.BoundingBox.Location;
|
||||
rebound.Offset(delta);
|
||||
rebound.UpdateBounds();
|
||||
parts[i] = rebound;
|
||||
}
|
||||
|
||||
return CanonicalFrame.FromCanonical(parts, sourceAngle);
|
||||
}
|
||||
private static List<Part> RebindPairToOriginal(List<Part> parts, Drawing original) =>
|
||||
CanonicalFrame.RebindToOriginal(parts, original);
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether a drawing should use grid-fill (true) or bin-pack (false).
|
||||
|
||||
Reference in New Issue
Block a user