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); + } +}