diff --git a/OpenNest.Core/Dimensions/DimensionBuilder.cs b/OpenNest.Core/Dimensions/DimensionBuilder.cs index 768dcb9d..71479a5b 100644 --- a/OpenNest.Core/Dimensions/DimensionBuilder.cs +++ b/OpenNest.Core/Dimensions/DimensionBuilder.cs @@ -20,22 +20,27 @@ namespace OpenNest.Dimensions /// public static Box Extents(IEnumerable entities) { - var measured = entities - .Where(e => SpecialLayers.IsMaterial(e.Layer) && !ScribeLayerRepair.IsMarkLayer(e.Layer)) - .ToList(); + var measured = Measured(entities); return measured.Count == 0 ? null : measured.GetBoundingBox(); } + private static List Measured(IEnumerable entities) => + entities + .Where(e => SpecialLayers.IsMaterial(e.Layer) && !ScribeLayerRepair.IsMarkLayer(e.Layer)) + .ToList(); + public static DimensionLayout Layout(IEnumerable entities, DrawingDimension dimension) { var kind = dimension.Kind; if (dimension.Offset == 0 || !double.IsFinite(dimension.Offset)) return new DimensionLayout { Kind = kind, Error = "its offset is not usable" }; - var box = Extents(entities); - if (box == null) + var measured = Measured(entities); + if (measured.Count == 0) return new DimensionLayout { Kind = kind, Error = "there is no cut geometry to measure" }; + var box = measured.GetBoundingBox(); + var offset = dimension.Offset; if (kind == DimensionKind.OverallHorizontal) { @@ -47,6 +52,7 @@ namespace OpenNest.Dimensions { Kind = kind, Extents = box, + Profile = measured, Value = box.Length, ExtensionStart = new Vector(box.Left, edge), ExtensionEnd = new Vector(box.Right, edge), @@ -63,6 +69,7 @@ namespace OpenNest.Dimensions { Kind = kind, Extents = box, + Profile = measured, Value = box.Width, ExtensionStart = new Vector(side, box.Bottom), ExtensionEnd = new Vector(side, box.Top), diff --git a/OpenNest.Core/Dimensions/DimensionLayout.cs b/OpenNest.Core/Dimensions/DimensionLayout.cs index 31ed2d4e..3bb4c4b3 100644 --- a/OpenNest.Core/Dimensions/DimensionLayout.cs +++ b/OpenNest.Core/Dimensions/DimensionLayout.cs @@ -1,15 +1,21 @@ +using System; +using System.Collections.Generic; using System.Globalization; using OpenNest.Geometry; namespace OpenNest.Dimensions { /// - /// A dimension resolved against a part's geometry. Extension lines run from - /// to and from - /// to ; the dimension line joins the last two. + /// A dimension resolved against a part's geometry. Extension lines run along the extents, on + /// the lines through and and through + /// and , out from the part to + /// ; the dimension line joins the last two. /// public sealed class DimensionLayout { + private const int MaxExtensionSteps = 64; + private const double ExtensionPrecision = 0.05; + public DimensionKind Kind { get; init; } /// Why the dimension could not be resolved; null when it was. @@ -26,10 +32,50 @@ namespace OpenNest.Dimensions /// The part's extents the dimension was measured from. public Box Extents { get; init; } + /// The geometry behind . + public IReadOnlyList Profile { get; init; } = Array.Empty(); + public string ValueText => Value.ToString("0.000", CultureInfo.InvariantCulture); public Vector MidPoint => new((LineStart.X + LineEnd.X) / 2, (LineStart.Y + LineEnd.Y) / 2); + /// + /// Where an extension line leaving on the dimension line first + /// meets the profile offset by , walking toward the part by the + /// distance known to be free at each step. itself when it is + /// already that close. + /// + public Vector ExtensionTip(Vector foot, double clearance) + { + var horizontal = Kind == DimensionKind.OverallHorizontal; + var position = horizontal ? foot.Y : foot.X; + var center = horizontal + ? (Extents.Bottom + Extents.Top) / 2 + : (Extents.Left + Extents.Right) / 2; + var sign = System.Math.Sign(center - position); + var direction = horizontal ? new Vector(0, sign) : new Vector(sign, 0); + + // The walk ends at the far side of the part at the latest: the line touches the part. + var limit = System.Math.Abs(center - position) + (horizontal ? Extents.Width : Extents.Length) / 2; + var travelled = 0.0; + for (var i = 0; i < MaxExtensionSteps && travelled < limit; i++) + { + var free = DistanceToProfile(foot + direction * travelled) - clearance; + if (free <= clearance * ExtensionPrecision) + break; + travelled += free; + } + return foot + direction * System.Math.Min(travelled, limit); + } + + private double DistanceToProfile(Vector point) + { + var nearest = double.MaxValue; + foreach (var entity in Profile) + nearest = System.Math.Min(nearest, point.DistanceTo(entity.ClosestPointTo(point))); + return nearest; + } + /// /// The offset that puts the dimension line through , on the side of /// the part's center the point is on and at least clear of it. diff --git a/OpenNest.Tests/Dimensions/DimensionExtensionTests.cs b/OpenNest.Tests/Dimensions/DimensionExtensionTests.cs new file mode 100644 index 00000000..72d06ff2 --- /dev/null +++ b/OpenNest.Tests/Dimensions/DimensionExtensionTests.cs @@ -0,0 +1,57 @@ +using OpenNest.Dimensions; +using OpenNest.Geometry; + +namespace OpenNest.Tests.Dimensions; + +public class DimensionExtensionTests +{ + private const double Clearance = 0.1; + + private static List Loop(params Vector[] points) + { + var layer = new Layer("0") { IsVisible = true }; + var entities = new List(); + for (var i = 0; i < points.Length; i++) + entities.Add(new Line(points[i], points[(i + 1) % points.Length]) { Layer = layer }); + return entities; + } + + private static DimensionLayout Horizontal(List entities, double offset) => + DimensionBuilder.Layout(entities, + new DrawingDimension { Kind = DimensionKind.OverallHorizontal, Offset = offset }); + + [Fact] + public void OnARectangle_TheLineStopsAtTheOffsetEdge() + { + var layout = Horizontal(Loop(new(0, 0), new(10, 0), new(10, 6), new(0, 6)), -1); + + var tip = layout.ExtensionTip(layout.LineStart, Clearance); + + Assert.Equal(0, tip.X, 6); + Assert.Equal(-Clearance, tip.Y, 1); + } + + [Fact] + public void OnADiamond_TheLineFollowsTheExtentsUntilItMeetsTheOffsetProfile() + { + var part = Loop(new(5, 0), new(10, 3), new(5, 6), new(0, 3)); + var layout = Horizontal(part, -1); + + var tip = layout.ExtensionTip(layout.LineStart, Clearance); + + Assert.Equal(0, tip.X, 6); + Assert.InRange(tip.Y, 0, 3); + var distance = part.Min(e => tip.DistanceTo(e.ClosestPointTo(tip))); + Assert.InRange(distance, Clearance, Clearance * 1.06); + } + + [Fact] + public void AFootAlreadyWithinTheClearance_StaysPut() + { + var layout = Horizontal(Loop(new(0, 0), new(10, 0), new(10, 6), new(0, 6)), -0.05); + + var tip = layout.ExtensionTip(layout.LineStart, Clearance); + + Assert.Equal(layout.LineStart.Y, tip.Y, 6); + } +} diff --git a/OpenNest/Controls/EntityView.Dimensions.cs b/OpenNest/Controls/EntityView.Dimensions.cs index b305ab2e..96171593 100644 --- a/OpenNest/Controls/EntityView.Dimensions.cs +++ b/OpenNest/Controls/EntityView.Dimensions.cs @@ -120,8 +120,9 @@ namespace OpenNest.Controls return; var along = new PointF((float)((to.X - from.X) / length), (float)((to.Y - from.Y) / length)); - DrawExtensionLine(g, pen, PointWorldToGraph(layout.ExtensionStart), from); - DrawExtensionLine(g, pen, PointWorldToGraph(layout.ExtensionEnd), to); + var clearance = LengthGuiToWorld(DimensionGapPixels); + DrawExtensionLine(g, pen, PointWorldToGraph(layout.ExtensionTip(layout.LineStart, clearance)), from); + DrawExtensionLine(g, pen, PointWorldToGraph(layout.ExtensionTip(layout.LineEnd, clearance)), to); g.DrawLine(pen, from, to); DrawArrow(g, brush, from, along); DrawArrow(g, brush, to, new PointF(-along.X, -along.Y)); @@ -137,8 +138,7 @@ namespace OpenNest.Controls g.Restore(state); } - // Starts clear of the part and runs a little past the dimension line, the way drafted - // extension lines do. + // Runs a little past the dimension line, the way drafted extension lines do. private static void DrawExtensionLine(Graphics g, Pen pen, PointF from, PointF to) { var dx = to.X - from.X; @@ -146,11 +146,9 @@ namespace OpenNest.Controls var length = (float)System.Math.Sqrt(dx * dx + dy * dy); if (length < 1) return; - var start = new PointF(from.X + dx / length * DimensionGapPixels, - from.Y + dy / length * DimensionGapPixels); var end = new PointF(to.X + dx / length * DimensionOvershootPixels, to.Y + dy / length * DimensionOvershootPixels); - g.DrawLine(pen, start, end); + g.DrawLine(pen, from, end); } private static void DrawArrow(Graphics g, Brush brush, PointF tip, PointF direction)