diff --git a/OpenNest.Core/Labels/BendLabels.cs b/OpenNest.Core/Labels/BendLabels.cs index fe6f565c..6d2a9e9a 100644 --- a/OpenNest.Core/Labels/BendLabels.cs +++ b/OpenNest.Core/Labels/BendLabels.cs @@ -11,9 +11,11 @@ namespace OpenNest.Labels { /// /// Where one bend's group letter is etched: the circle's center and diameter, and the index - /// of the bend in the list the spots were placed for. + /// of the bend in the list the spots were placed for. is 1 when the + /// letter's top points to the left of the bend line (from its start to its end), -1 when it + /// points to the right, and 0 when unknown (the top then points toward the line). /// - public readonly record struct BendLabelSpot(int Bend, Vector Center, double Diameter); + public readonly record struct BendLabelSpot(int Bend, Vector Center, double Diameter, int Facing = 0); /// /// The bend group letter etched beside each bend line, so a part whose bends fall in several @@ -90,6 +92,18 @@ namespace OpenNest.Labels if (free.Count == 0) return spots; + // The part's material reaches, for turning each letter to the nearer edge. + var extents = all.Where(e => SpecialLayers.IsMaterial(e.Layer) && !ScribeLayerRepair.IsMarkLayer(e.Layer)) + .SelectMany(e => + { + var b = e.BoundingBox; + return new[] + { + new Vector(b.Left, b.Bottom), new Vector(b.Right, b.Bottom), + new Vector(b.Right, b.Top), new Vector(b.Left, b.Top), + }; + }).ToList(); + // 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); @@ -128,7 +142,7 @@ namespace OpenNest.Labels || spots.Any(s => s.Center.DistanceTo(center) < s.Diameter / 2 + radius + clearance)) continue; - spots.Add(new BendLabelSpot(index, center, diameter)); + spots.Add(new BendLabelSpot(index, center, diameter, Facing(extents, center, left, side))); placed = true; break; } @@ -159,7 +173,7 @@ namespace OpenNest.Labels continue; var bend = bends[spot.Bend]; var letter = PartLabels.GroupLetter(bend.LineAngle + partRotation, grainAngle); - strokes.AddRange(PartLabels.CircledLetter(letter, spot.Center, spot.Diameter, Facing(bend, spot.Center))); + strokes.AddRange(PartLabels.CircledLetter(letter, spot.Center, spot.Diameter, Rotation(bend, spot))); } return strokes; } @@ -237,18 +251,37 @@ namespace OpenNest.Labels private static bool IsBend(Bend bend) => bend != null && bend.Length > Tolerance.Epsilon; - // The letter's rotation: along the bend line, with its top toward the line, so it reads - // upright from the outer edge of the flange it sits on, whichever side of the line that is. - private static double Facing(Bend bend, Vector center) + // Which way a letter's top points, across its bend line: away from the nearer edge of the + // part, measured across the line, so it reads upright from that edge. When both are as near, + // toward the line (away from the side it sits on). + private static int Facing(List extents, Vector center, Vector left, int side) { - var dx = bend.EndPoint.X - bend.StartPoint.X; - var dy = bend.EndPoint.Y - bend.StartPoint.Y; - // Which side of the line, from its start to its end, the letter is on. - var cross = dx * (center.Y - bend.StartPoint.Y) - dy * (center.X - bend.StartPoint.X); - if (System.Math.Abs(cross) <= Tolerance.Epsilon * bend.Length) - return LabelBox.ReadableRotation(bend.LineAngle); - // On the right the text runs from the line's start to its end; on the left, back. - return cross > 0 ? bend.LineAngle + System.Math.PI : bend.LineAngle; + if (extents.Count == 0) + return -side; + var toLeft = extents.Max(p => (p.X - center.X) * left.X + (p.Y - center.Y) * left.Y); + var toRight = extents.Max(p => -((p.X - center.X) * left.X + (p.Y - center.Y) * left.Y)); + if (System.Math.Abs(toLeft - toRight) <= Tolerance.Epsilon) + return -side; + return toLeft < toRight ? -1 : 1; + } + + // The letter's rotation: along the bend line, with its top as the spot faces. A spot with + // no facing has its top toward the line. + private static double Rotation(Bend bend, BendLabelSpot spot) + { + var facing = spot.Facing; + if (facing == 0) + { + var dx = bend.EndPoint.X - bend.StartPoint.X; + var dy = bend.EndPoint.Y - bend.StartPoint.Y; + // Which side of the line, from its start to its end, the letter is on. + var cross = dx * (spot.Center.Y - bend.StartPoint.Y) - dy * (spot.Center.X - bend.StartPoint.X); + if (System.Math.Abs(cross) <= Tolerance.Epsilon * bend.Length) + return LabelBox.ReadableRotation(bend.LineAngle); + facing = cross > 0 ? -1 : 1; + } + // Top to the left: the text runs from the line's start to its end; to the right, back. + return facing > 0 ? bend.LineAngle : bend.LineAngle + System.Math.PI; } // Undirected: a line and its reverse are parallel. diff --git a/OpenNest.Tests/Labels/BendLabelTests.cs b/OpenNest.Tests/Labels/BendLabelTests.cs index 03837225..1c2f0413 100644 --- a/OpenNest.Tests/Labels/BendLabelTests.cs +++ b/OpenNest.Tests/Labels/BendLabelTests.cs @@ -225,10 +225,10 @@ public class BendLabelTests var turned = BendLabels.Layout(spots, bends, Quarter, 0); List Points(IEnumerable strokes) => strokes.SelectMany(s => s.Points()).ToList(); - // Each letter reads along its own bend line with its top toward the line: both sit on - // the left of their lines, above the first and left of the second. + // Each letter reads along its own bend line, upright from the part's nearer edge across + // it: the bottom edge for the first, the left edge for the second. Assert.True(spots[0].Center.Y > 2 && spots[1].Center.X < 2); - var first = System.Math.PI; + var first = 0.0; var second = 3 * Quarter; var expected = PartLabels.CircledLetter('B', spots[0].Center, spots[0].Diameter, first) .Concat(PartLabels.CircledLetter('A', spots[1].Center, spots[1].Diameter, second)); @@ -246,9 +246,9 @@ public class BendLabelTests } [Theory] - [InlineData(1.5, 1)] // Room on the left of an upward line: the letter sits there. - [InlineData(0.4, -1)] // No room on the left: it sits on the right. - public void Letter_ReadsUprightFromTheOuterEdgeOfItsFlange(double line, int side) + [InlineData(1.5, 1)] // Room on the left of an upward line: the letter sits on the flange. + [InlineData(0.4, -1)] // No room on the left: it sits past the line. + public void Letter_ReadsUprightFromThePartsNearerEdgeAcrossTheLine(double line, int side) { // A vertical bend near the left edge, with a source offset so the drawing's bends and its // program frame differ. @@ -258,12 +258,12 @@ public class BendLabelTests drawing.Bends.Add(Bend(line + offset.X, offset.Y, line + offset.X, 20 + offset.Y)); var spot = Assert.Single(BendLabels.Spots(drawing)); - var towardLine = System.Math.Sign(line - spot.Center.X); - Assert.Equal(side, towardLine); + Assert.Equal(side, System.Math.Sign(line - spot.Center.X)); - // The letter's up, (-sin r, cos r), points from the flange's edge toward the line. - var rotation = towardLine > 0 ? 3 * Quarter : Quarter; - var expected = PartLabels.CircledLetter('A', spot.Center, spot.Diameter, rotation); + // On the flange or past the line, the letter's up, (-sin r, cos r), points away from the + // left edge, the nearer one across the line. + Assert.Equal(-1, spot.Facing); + var expected = PartLabels.CircledLetter('A', spot.Center, spot.Diameter, 3 * Quarter); var strokes = BendLabels.Strokes(drawing, [spot], 0, 0); Assert.Equal(expected.SelectMany(s => s.Points()), strokes.SelectMany(s => s.Points())); } diff --git a/docs/label-box.md b/docs/label-box.md index 7d7f70a2..bb6d2a6e 100644 --- a/docs/label-box.md +++ b/docs/label-box.md @@ -142,8 +142,9 @@ A `labelMode` number that names no choice (the reader accepts numbers) loads as 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 with its top toward the line, so it reads upright - from the outer edge of the flange it sits on, whichever side of the line that is. Where they go depends only on the drawing (its cut geometry, bends and box), so every + - Each letter reads along its own bend line, upright from the part's nearer edge across that + line (its bottom toward the side where the material ends sooner, measured from the letter), + whichever side of the line it sits on; with both as near, its top points to the line. 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 rotation against the grain. - A letter may sit on a breakoff's scrap (only the break cut itself is kept clear): it is read