mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-23 12:27:41 -05:00
feat(bends): colour each bend line by its angle to the sheet grain
Add Core BendGrain (across / with / other, 5 degree tolerance, 15 degree buckets mod 180) and draw each bend line in the plate view by kind: yellow across the grain, orange with it, blue otherwise. Replaces the whole-part orange outline warning, which only said that some bend was off. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
04ef7d7764
commit
b5069cca6f
4 files changed
+276
-91
No files matched your search
@@ -0,0 +1,103 @@
|
||||
using OpenNest.Math;
|
||||
|
||||
namespace OpenNest.Bending
|
||||
{
|
||||
/// <summary>How a bend line runs relative to the sheet's grain.</summary>
|
||||
public enum BendGrainKind
|
||||
{
|
||||
/// <summary>Perpendicular to the grain: the preferred direction, which avoids cracking.</summary>
|
||||
AcrossGrain,
|
||||
|
||||
/// <summary>Along the grain.</summary>
|
||||
WithGrain,
|
||||
|
||||
/// <summary>Any other angle.</summary>
|
||||
Other,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Classifies a bend line against the sheet grain. A bend line is undirected, so every angle
|
||||
/// is taken modulo 180°.
|
||||
/// </summary>
|
||||
public static class BendGrain
|
||||
{
|
||||
/// <summary>How far a bend may be from exactly across or exactly with the grain, in radians (5°).</summary>
|
||||
public static readonly double AngleTolerance = Angle.ToRadians(5);
|
||||
|
||||
/// <summary>The width of one <see cref="Bucket"/> step, in radians (15°).</summary>
|
||||
public static readonly double BucketStep = Angle.ToRadians(15);
|
||||
|
||||
/// <summary>The number of buckets in a half turn.</summary>
|
||||
public const int BucketCount = 12;
|
||||
|
||||
/// <summary>
|
||||
/// The angle between a bend line and the grain, in [0, π/2]: 0 is with the grain and
|
||||
/// π/2 is across it.
|
||||
/// </summary>
|
||||
public static double AngleToGrain(double lineAngle, double grainAngle)
|
||||
{
|
||||
var diff = Wrap(lineAngle - grainAngle);
|
||||
return System.Math.Min(diff, System.Math.PI - diff);
|
||||
}
|
||||
|
||||
/// <summary>Classifies a bend line (already rotated into the plate) against the grain.</summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Classifies <paramref name="bend"/> of a part placed at <paramref name="partRotation"/>.
|
||||
/// </summary>
|
||||
public static BendGrainKind Classify(Bend bend, double partRotation, double grainAngle) =>
|
||||
Classify(bend.LineAngle + partRotation, grainAngle);
|
||||
|
||||
/// <summary>
|
||||
/// The bend line's direction relative to the grain in 15° steps, 0 to
|
||||
/// <see cref="BucketCount"/> - 1. Unlike <see cref="AngleToGrain"/> 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
|
||||
/// <see cref="Classify(double, double)"/> calls <see cref="BendGrainKind.Other"/> falls in
|
||||
/// buckets 1 to 5 or 7 to 11.
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>An undirected line angle wrapped into [0, π).</summary>
|
||||
private static double Wrap(double angle)
|
||||
{
|
||||
var wrapped = angle % System.Math.PI;
|
||||
if (wrapped < 0)
|
||||
wrapped += System.Math.PI;
|
||||
return wrapped;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>The angle between two undirected lines, in [0, π/2].</summary>
|
||||
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;
|
||||
|
||||
Reference in new issue
Block a user