diff --git a/OpenNest.Core/Labels/BendLabels.cs b/OpenNest.Core/Labels/BendLabels.cs index edd0e59b..fe6f565c 100644 --- a/OpenNest.Core/Labels/BendLabels.cs +++ b/OpenNest.Core/Labels/BendLabels.cs @@ -157,11 +157,9 @@ namespace OpenNest.Labels { if (spot.Bend < 0 || spot.Bend >= bends.Count || !IsBend(bends[spot.Bend])) continue; - var line = bends[spot.Bend].LineAngle; - var letter = PartLabels.GroupLetter(line + partRotation, grainAngle); - // Along the line, turned at most a quarter so it never reads upside down. - strokes.AddRange(PartLabels.CircledLetter(letter, spot.Center, spot.Diameter, - LabelBox.ReadableRotation(line))); + 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))); } return strokes; } @@ -212,7 +210,17 @@ namespace OpenNest.Labels if (drawing == null || !double.IsFinite(partRotation) || !double.IsFinite(grainAngle)) return new List(); - var strokes = Layout(spots, drawing.Bends, partRotation, grainAngle); + // The spots lose the source offset; the bends must too, so each letter faces its line. + var offset = drawing.Source?.Offset ?? Vector.Zero; + var bends = drawing.Bends?.Select(bend => bend == null ? null : new Bend + { + StartPoint = bend.StartPoint - offset, + EndPoint = bend.EndPoint - offset, + Direction = bend.Direction, + Angle = bend.Angle, + Radius = bend.Radius, + }).ToList(); + var strokes = Layout(spots, bends, partRotation, grainAngle); return partRotation == 0 ? strokes : strokes.ConvertAll(stroke => stroke.Map(point => point.Rotate(partRotation))); @@ -229,6 +237,20 @@ 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) + { + 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; + } + // Undirected: a line and its reverse are parallel. private static bool Parallel(Bend a, Bend b) { diff --git a/OpenNest.Tests/Labels/BendLabelTests.cs b/OpenNest.Tests/Labels/BendLabelTests.cs index 278e71b2..03837225 100644 --- a/OpenNest.Tests/Labels/BendLabelTests.cs +++ b/OpenNest.Tests/Labels/BendLabelTests.cs @@ -225,13 +225,15 @@ 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: level beside the first, turned a quarter - // beside the second. - var second = LabelBox.ReadableRotation(bends[1].LineAngle); - var expected = PartLabels.CircledLetter('B', spots[0].Center, spots[0].Diameter) + // 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. + Assert.True(spots[0].Center.Y > 2 && spots[1].Center.X < 2); + var first = System.Math.PI; + 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)); Assert.Equal(Points(expected), Points(upright)); - var swapped = PartLabels.CircledLetter('A', spots[0].Center, spots[0].Diameter) + var swapped = PartLabels.CircledLetter('A', spots[0].Center, spots[0].Diameter, first) .Concat(PartLabels.CircledLetter('B', spots[1].Center, spots[1].Diameter, second)); Assert.Equal(Points(swapped), Points(turned)); @@ -243,6 +245,29 @@ public class BendLabelTests Assert.Contains(slantedSpots, spot => stroke.Points().All(p => p.DistanceTo(spot.Center) <= spot.Diameter / 2 + 1e-9)); } + [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) + { + // A vertical bend near the left edge, with a source offset so the drawing's bends and its + // program frame differ. + var offset = new Vector(-40, -30); + var drawing = new Drawing("flange", LeadPathValidationTests.Rectangle(0, 0, 10, 20)); + drawing.Source.Offset = offset; + 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); + + // 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); + var strokes = BendLabels.Strokes(drawing, [spot], 0, 0); + Assert.Equal(expected.SelectMany(s => s.Points()), strokes.SelectMany(s => s.Points())); + } + [Fact] public void DrawingSpots_FollowTheSourceOffset_AndStrokesTurnWithThePart() { diff --git a/docs/label-box.md b/docs/label-box.md index 0959a942..7d7f70a2 100644 --- a/docs/label-box.md +++ b/docs/label-box.md @@ -142,8 +142,8 @@ 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, 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 + - 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 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