From 2be966187cb6485d53abf92e70de6a4a3f54cd57 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Wed, 18 Nov 2026 11:01:57 -0500 Subject: [PATCH] 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 --- OpenNest.Core/Plate.Transforms.cs | 18 ++++++++- OpenNest.Tests/PlateGrainTests.cs | 61 +++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 OpenNest.Tests/PlateGrainTests.cs diff --git a/OpenNest.Core/Plate.Transforms.cs b/OpenNest.Core/Plate.Transforms.cs index 1c26df54..c4fb70b4 100644 --- a/OpenNest.Core/Plate.Transforms.cs +++ b/OpenNest.Core/Plate.Transforms.cs @@ -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 } /// - /// 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. + /// + 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; + } + + /// + /// Rotates the plate 180 degrees along with all parts. The grain's line is unchanged. /// /// public void Rotate180(bool keepSameQuadrant = true) diff --git a/OpenNest.Tests/PlateGrainTests.cs b/OpenNest.Tests/PlateGrainTests.cs new file mode 100644 index 00000000..cb3f0a9b --- /dev/null +++ b/OpenNest.Tests/PlateGrainTests.cs @@ -0,0 +1,61 @@ +using OpenNest.Geometry; +using OpenNest.Math; + +namespace OpenNest.Tests; + +/// +/// The grain belongs to the sheet: turning the plate turns its grain, and grain angles are +/// lines, so angles π apart are the same grain. +/// +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); + } +}