diff --git a/OpenNest.Core/Labels/BendLabels.cs b/OpenNest.Core/Labels/BendLabels.cs
index 9b0d0960..edd0e59b 100644
--- a/OpenNest.Core/Labels/BendLabels.cs
+++ b/OpenNest.Core/Labels/BendLabels.cs
@@ -17,7 +17,7 @@ namespace OpenNest.Labels
///
/// The bend group letter etched beside each bend line, so a part whose bends fall in several
- /// groups shows which bend is which (the label box only lists the groups).
+ /// groups shows which bend is which. Parallel bends close together share one letter.
///
///
/// Each letter sits in a circle on flat material just past the bend's radius band, never on
@@ -28,6 +28,16 @@ namespace OpenNest.Labels
///
public static class BendLabels
{
+ // Bends within this angle of each other are parallel: always in one group, however the
+ // part is turned against the grain.
+ private static readonly double ParallelTolerance = Angle.ToRadians(1);
+
+ ///
+ /// How near a letter must be to a parallel bend's line for that bend to share it instead
+ /// of getting its own: 12 inches, or 300 mm.
+ ///
+ public static double SharedReach(Units units) => units == Units.Millimeters ? 300 : 12;
+
///
/// A spot for each bend's letter that has room for one, in the frame of
/// , and (the
@@ -41,7 +51,8 @@ namespace OpenNest.Labels
/// the line and then the right; the first that fits is kept. A spot fits on material a
/// clearance inside every edge, clear of every bend's band, of scribe marks and break
/// cuts, of the label box and of the letters placed before it. A bend with no such spot
- /// gets none.
+ /// gets none. Neither does a bend with a letter already placed for a parallel bend within
+ /// of its line: the two are always in one group, so they share it.
///
public static List Place(IEnumerable entities, IReadOnlyList bends, LabelBox box,
Units units = Units.Inches)
@@ -81,12 +92,17 @@ namespace OpenNest.Labels
// Where a circle's center may be, per size: the free region eroded by its radius.
var centers = new PathsD[sizes.Count];
+ var reach = SharedReach(units);
for (var index = 0; index < bends.Count; index++)
{
var bend = bends[index];
if (!IsBend(bend))
continue;
+ // A parallel bend's letter close by says this bend's group too.
+ if (spots.Any(s => Parallel(bends[s.Bend], bend) && DistanceTo(bend, s.Center) <= reach))
+ continue;
+
var length = bend.Length;
var along = new Vector((bend.EndPoint.X - bend.StartPoint.X) / length, (bend.EndPoint.Y - bend.StartPoint.Y) / length);
var left = new Vector(-along.Y, along.X);
@@ -213,6 +229,23 @@ namespace OpenNest.Labels
private static bool IsBend(Bend bend) => bend != null && bend.Length > Tolerance.Epsilon;
+ // Undirected: a line and its reverse are parallel.
+ private static bool Parallel(Bend a, Bend b)
+ {
+ var between = System.Math.Abs(System.Math.IEEERemainder(a.LineAngle - b.LineAngle, System.Math.PI));
+ return between <= ParallelTolerance;
+ }
+
+ // The distance from a point to the nearest point of a bend's line segment.
+ private static double DistanceTo(Bend bend, Vector point)
+ {
+ var dx = bend.EndPoint.X - bend.StartPoint.X;
+ var dy = bend.EndPoint.Y - bend.StartPoint.Y;
+ var along = ((point.X - bend.StartPoint.X) * dx + (point.Y - bend.StartPoint.Y) * dy) / (dx * dx + dy * dy);
+ along = System.Math.Clamp(along, 0, 1);
+ return point.DistanceTo(new Vector(bend.StartPoint.X + dx * along, bend.StartPoint.Y + dy * along));
+ }
+
// Half the width kept clear along a bend line: the clearance, or half the arc the bend
// rolls the material through when that is wider, so the letter stays on flat material.
private static double Band(Bend bend, double clearance) =>
diff --git a/OpenNest.Tests/Labels/BendLabelTests.cs b/OpenNest.Tests/Labels/BendLabelTests.cs
index 561671a0..278e71b2 100644
--- a/OpenNest.Tests/Labels/BendLabelTests.cs
+++ b/OpenNest.Tests/Labels/BendLabelTests.cs
@@ -159,6 +159,51 @@ public class BendLabelTests
Assert.NotEmpty(BendLabels.Strokes(unboxed, spots, 0, 0));
}
+ [Fact]
+ public void ParallelBendsCloseTogether_ShareOneLetter()
+ {
+ // An inch apart, as on a channel's two flanges: the first bend's letter says both.
+ var near = new List { Bend(2, 5, 28, 5), Bend(2, 6, 28, 6) };
+ var shared = Assert.Single(BendLabels.Place(Rectangle(0, 0, 30, 30), near, null));
+ Assert.Equal(0, shared.Bend);
+
+ // Running the other way is still parallel.
+ var reversed = new List { Bend(2, 5, 28, 5), Bend(28, 6, 2, 6) };
+ Assert.Single(BendLabels.Place(Rectangle(0, 0, 30, 30), reversed, null));
+
+ // Past twelve inches from the letter, the second bend gets its own.
+ var far = new List { Bend(2, 5, 28, 5), Bend(2, 25, 28, 25) };
+ Assert.Equal([0, 1], BendLabels.Place(Rectangle(0, 0, 30, 30), far, null).Select(s => s.Bend));
+
+ // So does a parallel bend in line with the first but far along it.
+ var inLine = new List { Bend(2, 5, 8, 5), Bend(40, 5, 46, 5) };
+ Assert.Equal([0, 1], BendLabels.Place(Rectangle(0, 0, 48, 30), inLine, null).Select(s => s.Bend));
+ }
+
+ [Fact]
+ public void ABendAtAnotherAngle_KeepsItsOwnLetter_HoweverClose()
+ {
+ // The slanted bend lies between two parallel ones: it may be another group.
+ var bends = new List { Bend(2, 5, 28, 5), Bend(2, 7, 28, 9), Bend(2, 12, 28, 12) };
+
+ var spots = BendLabels.Place(Rectangle(0, 0, 30, 30), bends, null);
+
+ Assert.Equal([0, 1], spots.Select(s => s.Bend));
+ }
+
+ [Fact]
+ public void SharedReach_IsTwelveInchesOrThreeHundredMillimeters()
+ {
+ Assert.Equal(12, BendLabels.SharedReach(Units.Inches));
+ Assert.Equal(300, BendLabels.SharedReach(Units.Millimeters));
+
+ // 200 mm apart shares; 400 mm apart does not.
+ var near = new List { Bend(50, 100, 700, 100), Bend(50, 300, 700, 300) };
+ Assert.Single(BendLabels.Place(Rectangle(0, 0, 750, 750), near, null, Units.Millimeters));
+ var far = new List { Bend(50, 100, 700, 100), Bend(50, 500, 700, 500) };
+ Assert.Equal(2, BendLabels.Place(Rectangle(0, 0, 750, 750), far, null, Units.Millimeters).Count);
+ }
+
[Fact]
public void NothingToPlace_IsEmpty()
{
diff --git a/docs/label-box.md b/docs/label-box.md
index 0228cba2..0959a942 100644
--- a/docs/label-box.md
+++ b/docs/label-box.md
@@ -137,6 +137,11 @@ A `labelMode` number that names no choice (the reader accepts numbers) loads as
- Fit: on material a clearance inside every edge and hole, clear of every bend's band, of scribe
and etch marks and break cuts, of the label box and of the letters placed before it. A bend
with no such spot gets no letter.
+ - Sharing: a bend gets no letter of its own when one is already placed, for a bend parallel to
+ it (within 1°), within 12 inches (300 mm) of its line (`BendLabels.SharedReach`). Parallel
+ bends are in one group however the part is turned, so two bends an inch apart show one
+ letter, not two. Bends are taken in the drawing's order, so the earlier one carries it. A
+ bend at any other angle keeps its own letter, however close.
- Each letter reads along its own bend line, turned at most a quarter so it is never upside
down. Where they go depends only on the drawing (its cut geometry, bends and box), so every
part of a drawing gets them in the same place; which letter each one is depends on the part's