fix(plate): turn the grain when the plate is rotated 90 degrees

Grain belongs to the physical sheet, so Rotate Plate CW/CCW must turn it
too: a 60x120 sheet with side-to-side grain becomes a 120x60 sheet whose
grain runs front to back. Rotate90 swapped the size and turned the parts
but left GrainAngle alone, so bend-vs-grain warnings were wrong after a
rotation. A 180-degree turn leaves the grain's line unchanged.

Grain angles are lines, so they are kept in [0, pi).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-11-18 11:01:57 -05:00
1 parent 9ec3163495
commit 2be966187c
2 files changed
+78 -1

No files matched your search

+17 -1
View File
@@ -17,6 +17,9 @@ namespace OpenNest
var turned = Outline?.Rotated90(rotationDirection);
Size = new Size(Size.Length, Size.Width);
Outline = turned;
// The grain belongs to the sheet, so it turns with it.
GrainAngle = NormalizeGrainAngle(
GrainAngle + (rotationDirection == RotationType.CW ? -Angle.HalfPI : Angle.HalfPI));
// After Size swap above, new Size.Width = old Length (old X extent),
// new Size.Length = old Width (old Y extent).
@@ -90,7 +93,20 @@ namespace OpenNest
}
/// <summary>
/// Rotates the plate 180 degrees along with all parts.
/// A grain angle as a line direction in [0, π): grain has no head or tail, so
/// angles π apart are the same grain.
/// </summary>
public static double NormalizeGrainAngle(double angle)
{
var normalized = angle % System.Math.PI;
if (normalized < 0)
normalized += System.Math.PI;
// Values a hair under π are the same line as 0.
return System.Math.PI - normalized < Tolerance.Epsilon ? 0 : normalized;
}
/// <summary>
/// Rotates the plate 180 degrees along with all parts. The grain's line is unchanged.
/// </summary>
/// <param name="keepSameQuadrant"></param>
public void Rotate180(bool keepSameQuadrant = true)
+61
View File
@@ -0,0 +1,61 @@
using OpenNest.Geometry;
using OpenNest.Math;
namespace OpenNest.Tests;
/// <summary>
/// The grain belongs to the sheet: turning the plate turns its grain, and grain angles are
/// lines, so angles π apart are the same grain.
/// </summary>
public class PlateGrainTests
{
[Theory]
[InlineData(RotationType.CW)]
[InlineData(RotationType.CCW)]
public void Rotate90_TurnsTheGrainWithTheSheet(RotationType direction)
{
// A 60 x 120 sheet whose grain runs side to side along its 120" length.
var plate = new Plate(new Size(60, 120)) { GrainAngle = 0 };
plate.Rotate90(direction);
Assert.Equal(new Size(120, 60), plate.Size);
Assert.Equal(Angle.HalfPI, plate.GrainAngle, 9);
plate.Rotate90(direction);
Assert.Equal(0, plate.GrainAngle, 9);
}
[Fact]
public void Rotate90_TurnsAnAngledGrainByAQuarterTurn()
{
var plate = new Plate(new Size(60, 120)) { GrainAngle = Angle.ToRadians(30) };
plate.Rotate90(RotationType.CCW);
Assert.Equal(Angle.ToRadians(120), plate.GrainAngle, 9);
}
[Fact]
public void Rotate180_LeavesTheGrainLineAlone()
{
var plate = new Plate(new Size(60, 120)) { GrainAngle = Angle.ToRadians(30) };
plate.Rotate180();
Assert.Equal(Angle.ToRadians(30), plate.GrainAngle, 9);
}
[Theory]
[InlineData(0, 0)]
[InlineData(90, 90)]
[InlineData(180, 0)]
[InlineData(-90, 90)]
[InlineData(270, 90)]
[InlineData(210, 30)]
public void NormalizeGrainAngle_KeepsTheLineInZeroToPi(double degrees, double expected)
{
Assert.Equal(Angle.ToRadians(expected), Plate.NormalizeGrainAngle(Angle.ToRadians(degrees)), 9);
}
}