diff --git a/OpenNest.Core/Bending/BendGrain.cs b/OpenNest.Core/Bending/BendGrain.cs
new file mode 100644
index 00000000..366ac4b9
--- /dev/null
+++ b/OpenNest.Core/Bending/BendGrain.cs
@@ -0,0 +1,103 @@
+using OpenNest.Math;
+
+namespace OpenNest.Bending
+{
+ /// How a bend line runs relative to the sheet's grain.
+ public enum BendGrainKind
+ {
+ /// Perpendicular to the grain: the preferred direction, which avoids cracking.
+ AcrossGrain,
+
+ /// Along the grain.
+ WithGrain,
+
+ /// Any other angle.
+ Other,
+ }
+
+ ///
+ /// Classifies a bend line against the sheet grain. A bend line is undirected, so every angle
+ /// is taken modulo 180°.
+ ///
+ public static class BendGrain
+ {
+ /// How far a bend may be from exactly across or exactly with the grain, in radians (5°).
+ public static readonly double AngleTolerance = Angle.ToRadians(5);
+
+ /// The width of one step, in radians (15°).
+ public static readonly double BucketStep = Angle.ToRadians(15);
+
+ /// The number of buckets in a half turn.
+ public const int BucketCount = 12;
+
+ ///
+ /// The angle between a bend line and the grain, in [0, π/2]: 0 is with the grain and
+ /// π/2 is across it.
+ ///
+ public static double AngleToGrain(double lineAngle, double grainAngle)
+ {
+ var diff = Wrap(lineAngle - grainAngle);
+ return System.Math.Min(diff, System.Math.PI - diff);
+ }
+
+ /// Classifies a bend line (already rotated into the plate) against the grain.
+ public static BendGrainKind Classify(double lineAngle, double grainAngle)
+ {
+ var angle = AngleToGrain(lineAngle, grainAngle);
+ var slack = OpenNest.Math.Tolerance.Epsilon;
+
+ if (angle <= AngleTolerance + slack)
+ return BendGrainKind.WithGrain;
+
+ return angle >= Angle.HalfPI - AngleTolerance - slack
+ ? BendGrainKind.AcrossGrain
+ : BendGrainKind.Other;
+ }
+
+ ///
+ /// Classifies of a part placed at .
+ ///
+ public static BendGrainKind Classify(Bend bend, double partRotation, double grainAngle) =>
+ Classify(bend.LineAngle + partRotation, grainAngle);
+
+ ///
+ /// The bend line's direction relative to the grain in 15° steps, 0 to
+ /// - 1. Unlike it keeps the side of the
+ /// grain, so a bend 30° one way and one 30° the other way are different buckets (they need
+ /// the part turned differently). 0 is with the grain and 6 is across it; every bend
+ /// calls falls in
+ /// buckets 1 to 5 or 7 to 11.
+ ///
+ public static int Bucket(double lineAngle, double grainAngle)
+ {
+ var kind = Classify(lineAngle, grainAngle);
+ if (kind == BendGrainKind.WithGrain)
+ return 0;
+ if (kind == BendGrainKind.AcrossGrain)
+ return BucketCount / 2;
+
+ // A bend just outside the tolerance can round onto the with/across bucket: nudge it to
+ // the neighbouring bucket on its own side so those two stay exclusive to their kinds.
+ var wrapped = Wrap(lineAngle - grainAngle);
+ var bucket = (int)System.Math.Round(wrapped / BucketStep) % BucketCount;
+ var firstSide = wrapped < Angle.HalfPI;
+
+ if (bucket == 0)
+ return firstSide ? 1 : BucketCount - 1;
+
+ if (bucket == BucketCount / 2)
+ return firstSide ? BucketCount / 2 - 1 : BucketCount / 2 + 1;
+
+ return bucket;
+ }
+
+ /// An undirected line angle wrapped into [0, π).
+ private static double Wrap(double angle)
+ {
+ var wrapped = angle % System.Math.PI;
+ if (wrapped < 0)
+ wrapped += System.Math.PI;
+ return wrapped;
+ }
+ }
+}
diff --git a/OpenNest.Tests/Bending/BendGrainTests.cs b/OpenNest.Tests/Bending/BendGrainTests.cs
new file mode 100644
index 00000000..5339c170
--- /dev/null
+++ b/OpenNest.Tests/Bending/BendGrainTests.cs
@@ -0,0 +1,126 @@
+using OpenNest.Bending;
+using OpenNest.Geometry;
+using OpenNest.Math;
+
+namespace OpenNest.Tests.Bending;
+
+public class BendGrainTests
+{
+ private static double Rad(double degrees) => Angle.ToRadians(degrees);
+
+ // Grain angle 0 runs along X, so a bend along Y (90°) is across it.
+ [Theory]
+ [InlineData(90, BendGrainKind.AcrossGrain)]
+ [InlineData(270, BendGrainKind.AcrossGrain)] // direction of the line does not matter
+ [InlineData(-90, BendGrainKind.AcrossGrain)]
+ [InlineData(0, BendGrainKind.WithGrain)]
+ [InlineData(180, BendGrainKind.WithGrain)]
+ [InlineData(360, BendGrainKind.WithGrain)]
+ [InlineData(45, BendGrainKind.Other)]
+ [InlineData(135, BendGrainKind.Other)]
+ [InlineData(30, BendGrainKind.Other)]
+ public void Classify_UsesTheAngleToTheGrainModuloAHalfTurn(double bendDegrees, BendGrainKind expected)
+ {
+ Assert.Equal(expected, BendGrain.Classify(Rad(bendDegrees), grainAngle: 0));
+ }
+
+ [Theory]
+ [InlineData(85, BendGrainKind.AcrossGrain)] // exactly on the 5° tolerance
+ [InlineData(95, BendGrainKind.AcrossGrain)]
+ [InlineData(87, BendGrainKind.AcrossGrain)]
+ [InlineData(84.9, BendGrainKind.Other)]
+ [InlineData(95.1, BendGrainKind.Other)]
+ [InlineData(5, BendGrainKind.WithGrain)] // exactly on the 5° tolerance
+ [InlineData(-5, BendGrainKind.WithGrain)]
+ [InlineData(5.1, BendGrainKind.Other)]
+ [InlineData(174.9, BendGrainKind.Other)]
+ [InlineData(175, BendGrainKind.WithGrain)]
+ public void Classify_HonorsTheFiveDegreeTolerance(double bendDegrees, BendGrainKind expected)
+ {
+ Assert.Equal(expected, BendGrain.Classify(Rad(bendDegrees), grainAngle: 0));
+ }
+
+ [Theory]
+ [InlineData(0, 90, 0, BendGrainKind.AcrossGrain)]
+ [InlineData(90, 90, 0, BendGrainKind.WithGrain)] // part turned a quarter, bend now with the grain
+ [InlineData(180, 90, 0, BendGrainKind.AcrossGrain)] // a half turn changes nothing
+ [InlineData(0, 0, 90, BendGrainKind.AcrossGrain)] // grain along Y
+ [InlineData(90, 0, 90, BendGrainKind.WithGrain)] // bend along X, grain along Y
+ [InlineData(45, 90, 45, BendGrainKind.AcrossGrain)] // bend lands at 135°, grain at 45°
+ [InlineData(45, 0, 45, BendGrainKind.WithGrain)] // bend lands on the 45° grain
+ public void Classify_IsRotationAndGrainAware(
+ double rotationDegrees, double bendDegrees, double grainDegrees, BendGrainKind expected)
+ {
+ var angle = Rad(bendDegrees);
+ var bend = new Bend
+ {
+ StartPoint = new Vector(0, 0),
+ EndPoint = new Vector(10 * System.Math.Cos(angle), 10 * System.Math.Sin(angle)),
+ };
+
+ Assert.Equal(expected, BendGrain.Classify(bend, Rad(rotationDegrees), Rad(grainDegrees)));
+ }
+
+ [Fact]
+ public void Classify_IsTheSameWhenEitherLineIsTurnedAHalfTurn()
+ {
+ for (var degrees = -360; degrees <= 360; degrees += 7)
+ {
+ var angle = Rad(degrees);
+ Assert.Equal(
+ BendGrain.Classify(angle, 0),
+ BendGrain.Classify(angle + System.Math.PI, 0));
+ Assert.Equal(
+ BendGrain.Classify(angle, Rad(30)),
+ BendGrain.Classify(angle, Rad(30) + System.Math.PI));
+ }
+ }
+
+ [Theory]
+ [InlineData(0, 0)]
+ [InlineData(90, 90)]
+ [InlineData(30, 30)]
+ [InlineData(120, 60)]
+ [InlineData(135, 45)]
+ [InlineData(180, 0)]
+ public void AngleToGrain_IsTheAcuteAngleBetweenTheLines(double bendDegrees, double expectedDegrees)
+ {
+ Assert.Equal(expectedDegrees, Angle.ToDegrees(BendGrain.AngleToGrain(Rad(bendDegrees), 0)), 9);
+ }
+
+ [Theory]
+ [InlineData(0, 0)]
+ [InlineData(90, 6)]
+ [InlineData(15, 1)]
+ [InlineData(30, 2)]
+ [InlineData(45, 3)]
+ [InlineData(150, 10)]
+ [InlineData(165, 11)]
+ [InlineData(180, 0)]
+ [InlineData(-30, 10)]
+ public void Bucket_NumbersTheDirectionInFifteenDegreeSteps(double bendDegrees, int expected)
+ {
+ Assert.Equal(expected, BendGrain.Bucket(Rad(bendDegrees), 0));
+ }
+
+ [Fact]
+ public void Bucket_KeepsTheSideOfTheGrain()
+ {
+ Assert.NotEqual(BendGrain.Bucket(Rad(30), 0), BendGrain.Bucket(Rad(-30), 0));
+ }
+
+ [Fact]
+ public void Bucket_ReservesZeroAndSixForWithAndAcrossTheGrain()
+ {
+ for (var degrees = -360.0; degrees <= 360; degrees += 0.5)
+ {
+ var angle = Rad(degrees);
+ var bucket = BendGrain.Bucket(angle, 0);
+ var kind = BendGrain.Classify(angle, 0);
+
+ Assert.InRange(bucket, 0, BendGrain.BucketCount - 1);
+ Assert.Equal(kind == BendGrainKind.WithGrain, bucket == 0);
+ Assert.Equal(kind == BendGrainKind.AcrossGrain, bucket == BendGrain.BucketCount / 2);
+ }
+ }
+}
diff --git a/OpenNest.WinForms.Tests/Controls/BendLinePlacementTests.cs b/OpenNest.WinForms.Tests/Controls/BendLinePlacementTests.cs
index 66ef4197..6f94b0a2 100644
--- a/OpenNest.WinForms.Tests/Controls/BendLinePlacementTests.cs
+++ b/OpenNest.WinForms.Tests/Controls/BendLinePlacementTests.cs
@@ -38,40 +38,27 @@ public class BendLinePlacementTests
Assert.Equal(25, world.Y, 9);
}
- // Grain angle 0 runs along X, the long side-to-side direction of a 60x120 sheet.
- // Bends should run across it (perpendicular) to avoid cracking.
[Theory]
- [InlineData(0, 0, true)] // bend along X, with the grain
- [InlineData(0, 90, false)] // bend along Y, across the grain
- [InlineData(90, 90, true)] // rotated 90°, now with the grain
- [InlineData(0, 45, true)] // diagonal
- [InlineData(0, 87, false)] // inside the 5° tolerance
- [InlineData(0, 270, false)] // direction of the bend line does not matter
- public void HasBendNotAcrossGrain_FlagsBendsThatAreNotPerpendicularToTheGrain(
- double rotationDegrees, double bendDegrees, bool flagged)
+ [InlineData(BendGrainKind.AcrossGrain)]
+ [InlineData(BendGrainKind.WithGrain)]
+ [InlineData(BendGrainKind.Other)]
+ public void BendGrainColor_GivesEachKindItsOwnColor(BendGrainKind kind)
{
- var angle = Angle.ToRadians(bendDegrees);
- var drawing = new Drawing("bent", new CNC.Program());
- drawing.Bends.Add(new Bend
+ var colors = new[]
{
- StartPoint = new Vector(0, 0),
- EndPoint = new Vector(10 * System.Math.Cos(angle), 10 * System.Math.Sin(angle)),
- });
- var part = new Part(drawing, new Vector(0, 0));
- if (rotationDegrees != 0)
- part.Rotate(Angle.ToRadians(rotationDegrees), part.Location);
+ PlateRenderer.BendGrainColor(BendGrainKind.AcrossGrain),
+ PlateRenderer.BendGrainColor(BendGrainKind.WithGrain),
+ PlateRenderer.BendGrainColor(BendGrainKind.Other),
+ };
- Assert.Equal(flagged, PlateRenderer.HasBendNotAcrossGrain(part, grainAngle: 0));
+ Assert.Equal(3, colors.Distinct().Count());
+ Assert.Equal(colors[(int)kind], PlateRenderer.BendGrainColor(kind));
}
[Fact]
- public void HasBendNotAcrossGrain_FlagsAPartWhenAnyBendRunsWithTheGrain()
+ public void BendGrainColor_KeepsTheBendColorAcrossTheGrainAndOrangeWithIt()
{
- var drawing = new Drawing("bracket", new CNC.Program());
- drawing.Bends.Add(new Bend { StartPoint = new Vector(5, 0), EndPoint = new Vector(5, 10) });
- drawing.Bends.Add(new Bend { StartPoint = new Vector(0, 5), EndPoint = new Vector(10, 5) });
- var part = new Part(drawing, new Vector(0, 0));
-
- Assert.True(PlateRenderer.HasBendNotAcrossGrain(part, grainAngle: 0));
+ Assert.Equal(System.Drawing.Color.Yellow, PlateRenderer.BendGrainColor(BendGrainKind.AcrossGrain));
+ Assert.Equal(System.Drawing.Color.FromArgb(255, 140, 0), PlateRenderer.BendGrainColor(BendGrainKind.WithGrain));
}
}
diff --git a/OpenNest/Controls/PlateRenderer.cs b/OpenNest/Controls/PlateRenderer.cs
index d9526ab0..c325d1d8 100644
--- a/OpenNest/Controls/PlateRenderer.cs
+++ b/OpenNest/Controls/PlateRenderer.cs
@@ -165,7 +165,6 @@ namespace OpenNest.Controls
part.Draw(g, number.ToString(), muted);
DrawBendLines(g, part.BasePart, muted);
DrawEtchMarks(g, part.BasePart, muted);
- DrawGrainWarning(g, part, muted);
}
var previewParts = view.PreviewParts;
@@ -529,6 +528,19 @@ namespace OpenNest.Controls
}
}
+ // Bend lines are coloured by how they run against the sheet's grain. Across the grain is
+ // the preferred direction, so it keeps the plain bend colour; the others stand out.
+ private static readonly Color AcrossGrainBendColor = Color.Yellow;
+ private static readonly Color WithGrainBendColor = Color.FromArgb(255, 140, 0);
+ private static readonly Color OtherGrainBendColor = Color.DeepSkyBlue;
+
+ internal static Color BendGrainColor(BendGrainKind kind) => kind switch
+ {
+ BendGrainKind.WithGrain => WithGrainBendColor,
+ BendGrainKind.Other => OtherGrainBendColor,
+ _ => AcrossGrainBendColor,
+ };
+
private void DrawBendLines(Graphics g, Part part, bool muted)
{
if (
@@ -538,11 +550,21 @@ namespace OpenNest.Controls
)
return;
- using var bendPen = new Pen(muted ? Color.Gray : Color.Yellow, 1.5f)
+ var plate = view.DisplayPlate;
+ using var pen = new Pen(Color.Gray, 1.5f)
{
DashStyle = System.Drawing.Drawing2D.DashStyle.Dash,
};
- DrawBendLines(g, part, bendPen);
+
+ foreach (var bend in part.BaseDrawing.Bends)
+ {
+ // Without a plate there is no grain to compare with: plain bend colour.
+ var kind = plate == null
+ ? BendGrainKind.AcrossGrain
+ : BendGrain.Classify(bend, part.Rotation, plate.GrainAngle);
+ pen.Color = muted ? Color.Gray : BendGrainColor(kind);
+ DrawBendLine(g, part, bend, pen);
+ }
}
private void DrawBendLines(Graphics g, Part part, Pen bendPen)
@@ -551,12 +573,15 @@ namespace OpenNest.Controls
return;
foreach (var bend in part.BaseDrawing.Bends)
- {
- var pt1 = view.PointWorldToGraph(BendToWorld(part, bend.StartPoint));
- var pt2 = view.PointWorldToGraph(BendToWorld(part, bend.EndPoint));
+ DrawBendLine(g, part, bend, bendPen);
+ }
- g.DrawLine(bendPen, pt1, pt2);
- }
+ private void DrawBendLine(Graphics g, Part part, Bend bend, Pen bendPen)
+ {
+ var pt1 = view.PointWorldToGraph(BendToWorld(part, bend.StartPoint));
+ var pt2 = view.PointWorldToGraph(BendToWorld(part, bend.EndPoint));
+
+ g.DrawLine(bendPen, pt1, pt2);
}
///
@@ -616,62 +641,6 @@ namespace OpenNest.Controls
}
}
- private void DrawGrainWarning(Graphics g, LayoutPart layoutPart, bool muted)
- {
- var plate = view.DisplayPlate;
- var part = layoutPart.BasePart;
- if (
- !view.ShowBendLines
- || plate == null
- || part.BaseDrawing.Bends == null
- || part.BaseDrawing.Bends.Count == 0
- || layoutPart.Path == null
- )
- return;
-
- if (!HasBendNotAcrossGrain(part, plate.GrainAngle))
- return;
-
- // Trace the part's own cut contours, not its bounding box, so the warning
- // stays on the part instead of running across its neighbors.
- using var warnPen = new Pen(muted ? Color.Gray : Color.FromArgb(220, 255, 140, 0), 2.5f)
- {
- LineJoin = LineJoin.Round,
- };
- g.DrawPath(warnPen, layoutPart.Path);
- }
-
- ///
- /// Whether any of the placed part's bends is not across the grain. Bend lines should run
- /// perpendicular to the sheet's grain to avoid cracking on the outside of the bend, so a
- /// bend more than 5° off perpendicular is flagged.
- ///
- internal static bool HasBendNotAcrossGrain(Part part, double grainAngle)
- {
- var bends = part.BaseDrawing.Bends;
- if (bends == null)
- return false;
-
- var tolerance = Angle.ToRadians(5);
- foreach (var bend in bends)
- {
- var diff = LineAngleBetween(bend.LineAngle + part.Rotation, grainAngle);
- if (System.Math.Abs(diff - Angle.HalfPI) > tolerance)
- return true;
- }
-
- return false;
- }
-
- /// The angle between two undirected lines, in [0, π/2].
- private static double LineAngleBetween(double a, double b)
- {
- var diff = (a - b) % System.Math.PI;
- if (diff < 0)
- diff += System.Math.PI;
- return System.Math.Min(diff, System.Math.PI - diff);
- }
-
private void DrawOffsetGeometry(Graphics g)
{
var layoutParts = view.DisplayLayoutParts;