using System; using System.Collections.Generic; using System.Linq; using OpenNest.Math; namespace OpenNest.Geometry; public class ArcCandidate { public int ShapeIndex { get; set; } public int StartIndex { get; set; } public int EndIndex { get; set; } public int LineCount => EndIndex - StartIndex + 1; public Arc FittedArc { get; set; } public double MaxDeviation { get; set; } public Box BoundingBox { get; set; } public bool IsSelected { get; set; } = true; /// First point of the original line segments this candidate covers. public Vector FirstPoint { get; set; } /// Last point of the original line segments this candidate covers. public Vector LastPoint { get; set; } /// The original polyline vertices this candidate covers, in order. internal List Points { get; set; } } /// /// A mirror axis defined by a point on the axis and a unit direction vector. /// public class MirrorAxisResult { public static readonly MirrorAxisResult None = new(Vector.Invalid, Vector.Invalid, 0); public Vector Point { get; } public Vector Direction { get; } public double Score { get; } public bool IsValid => Point.IsValid(); public MirrorAxisResult(Vector point, Vector direction, double score) { Point = point; Direction = direction; Score = score; } /// Reflects a point across this axis. public Vector Reflect(Vector p) { var dx = p.X - Point.X; var dy = p.Y - Point.Y; var dot = dx * Direction.X + dy * Direction.Y; return new Vector(p.X - 2 * (dx - dot * Direction.X), p.Y - 2 * (dy - dot * Direction.Y)); } } public class GeometrySimplifier { public double Tolerance { get; set; } = 0.004; public int MinLines { get; set; } = 3; public List Analyze(Shape shape) { var candidates = new List(); var entities = shape.Entities; var i = 0; while (i < entities.Count) { if (entities[i] is not Line and not Arc) { i++; continue; } var runStart = i; var layerName = entities[i].Layer?.Name; var lineCount = 0; while ( i < entities.Count && (entities[i] is Line || entities[i] is Arc) && entities[i].Layer?.Name == layerName ) { if (entities[i] is Line) lineCount++; i++; } var runEnd = i - 1; if (lineCount >= MinLines) FindCandidatesInRun(entities, runStart, runEnd, candidates); } return candidates; } public Shape Apply(Shape shape, List candidates) { var selected = candidates.Where(c => c.IsSelected).OrderBy(c => c.StartIndex).ToList(); var newEntities = new List(); var i = 0; foreach (var candidate in selected) { while (i < candidate.StartIndex) { newEntities.Add(shape.Entities[i]); i++; } newEntities.Add(candidate.FittedArc); i = candidate.EndIndex + 1; } while (i < shape.Entities.Count) { newEntities.Add(shape.Entities[i]); i++; } var result = new Shape(); result.Entities.AddRange(newEntities); return result; } /// /// Detects the mirror axis of a shape by testing candidate axes through the /// centroid. Uses PCA to find principal directions, then also tests horizontal /// and vertical. Works for shapes rotated at any angle. /// public static MirrorAxisResult DetectMirrorAxis(Shape shape) { var midpoints = new List(); foreach (var e in shape.Entities) midpoints.Add(e.BoundingBox.Center); if (midpoints.Count < 4) return MirrorAxisResult.None; var centroid = new Vector(midpoints.Average(p => p.X), midpoints.Average(p => p.Y)); var cx = centroid.X; var cy = centroid.Y; // Covariance matrix for PCA var cxx = 0.0; var cxy = 0.0; var cyy = 0.0; foreach (var p in midpoints) { var dx = p.X - cx; var dy = p.Y - cy; cxx += dx * dx; cxy += dx * dy; cyy += dy * dy; } // Eigenvectors of 2x2 symmetric matrix via analytic formula var trace = cxx + cyy; var det = cxx * cyy - cxy * cxy; var disc = System.Math.Sqrt(System.Math.Max(0, trace * trace / 4 - det)); var lambda1 = trace / 2 + disc; var lambda2 = trace / 2 - disc; var candidates = new List(); // PCA eigenvectors (major and minor axes) if (System.Math.Abs(cxy) > 1e-10) { candidates.Add(Normalize(new Vector(lambda1 - cyy, cxy))); candidates.Add(Normalize(new Vector(lambda2 - cyy, cxy))); } else { candidates.Add(new Vector(1, 0)); candidates.Add(new Vector(0, 1)); } // Also always test pure horizontal and vertical candidates.Add(new Vector(1, 0)); candidates.Add(new Vector(0, 1)); // Score each candidate axis var bestResult = MirrorAxisResult.None; foreach (var dir in candidates) { var score = MirrorMatchScore(midpoints, centroid, dir); if (score > bestResult.Score) bestResult = new MirrorAxisResult(centroid, dir, score); } return bestResult.Score >= 0.8 ? bestResult : MirrorAxisResult.None; } private static double NormalizeAngle(double angle) => angle < 0 ? angle + Angle.TwoPI : angle; private static Vector Normalize(Vector v) { var len = System.Math.Sqrt(v.X * v.X + v.Y * v.Y); return len < 1e-10 ? new Vector(1, 0) : new Vector(v.X / len, v.Y / len); } private static double PerpendicularDistance(Vector point, Vector axisPoint, Vector axisDir) { var dx = point.X - axisPoint.X; var dy = point.Y - axisPoint.Y; var dot = dx * axisDir.X + dy * axisDir.Y; var px = dx - dot * axisDir.X; var py = dy - dot * axisDir.Y; return System.Math.Sqrt(px * px + py * py); } private static double MirrorMatchScore(List points, Vector axisPoint, Vector axisDir) { var matchTol = 0.1; var matched = 0; for (var i = 0; i < points.Count; i++) { var p = points[i]; var dist = PerpendicularDistance(p, axisPoint, axisDir); // Points on the axis count as matched if (dist < matchTol) { matched++; continue; } // Reflect across axis and look for partner var reflected = new MirrorAxisResult(axisPoint, axisDir, 0).Reflect(p); for (var j = 0; j < points.Count; j++) { if (i == j) continue; var d = reflected.DistanceTo(points[j]); if (d < matchTol) { matched++; break; } } } return (double)matched / points.Count; } /// /// Pairs candidates across a mirror axis and forces each pair to use /// the same arc (mirrored). The candidate with more lines or lower /// deviation is kept as the source. /// public void Symmetrize(List candidates, MirrorAxisResult axis) { if (!axis.IsValid || candidates.Count < 2) return; var paired = new HashSet(); for (var i = 0; i < candidates.Count; i++) { if (paired.Contains(i)) continue; var ci = candidates[i]; var ciCenter = ci.BoundingBox.Center; if (PerpendicularDistance(ciCenter, axis.Point, axis.Direction) < 0.1) continue; // on the axis var mirrorCenter = axis.Reflect(ciCenter); var bestJ = -1; var bestDist = double.MaxValue; for (var j = i + 1; j < candidates.Count; j++) { if (paired.Contains(j)) continue; var d = mirrorCenter.DistanceTo(candidates[j].BoundingBox.Center); if (d < bestDist) { bestDist = d; bestJ = j; } } var matchTol = System.Math.Max(ci.BoundingBox.Width, ci.BoundingBox.Length) * 0.5; if (bestJ < 0 || bestDist > matchTol) continue; paired.Add(i); paired.Add(bestJ); var cj = candidates[bestJ]; var sourceIdx = i; var targetIdx = bestJ; if ( cj.LineCount > ci.LineCount || (cj.LineCount == ci.LineCount && cj.MaxDeviation < ci.MaxDeviation) ) { sourceIdx = bestJ; targetIdx = i; } var source = candidates[sourceIdx]; var target = candidates[targetIdx]; var mirrored = MirrorArc(source.FittedArc, axis); // Only consider the mirrored arc if it lands near the target's boundary // points. A drawing is rarely symmetric to the last digit, so the mirror // only lends its radius: the arc is refit through the target's own // endpoints, or the neighbors would no longer meet it. var startDist = mirrored.StartPoint().DistanceTo(target.FirstPoint); var endDist = mirrored.EndPoint().DistanceTo(target.LastPoint); if (startDist > Tolerance || endDist > Tolerance) continue; var refit = FitWithRadius(target, mirrored); if (refit.arc != null) { target.FittedArc = refit.arc; target.MaxDeviation = refit.deviation; } } } /// /// Fits an arc of 's radius exactly through the /// target's endpoints, on the side of its center, and accepts it only if it /// stays within tolerance of the target's original segments. /// private (Arc arc, double deviation) FitWithRadius(ArcCandidate target, Arc mirrored) { var points = target.Points; if (points == null || points.Count < 3) return (null, 0); var p1 = points[0]; var pn = points[^1]; var dx = pn.X - p1.X; var dy = pn.Y - p1.Y; var chordLen = System.Math.Sqrt(dx * dx + dy * dy); var halfChord = chordLen / 2; if (chordLen < 1e-10 || mirrored.Radius < halfChord) return (null, 0); var mx = (p1.X + pn.X) / 2; var my = (p1.Y + pn.Y) / 2; var nx = -dy / chordLen; var ny = dx / chordLen; var offset = System.Math.Sqrt(mirrored.Radius * mirrored.Radius - halfChord * halfChord); var side = (mirrored.Center.X - mx) * nx + (mirrored.Center.Y - my) * ny >= 0 ? 1 : -1; var center = new Vector(mx + side * offset * nx, my + side * offset * ny); var isReversed = ArcFit.SumSignedAngles(center, points) < 0; if (isReversed != target.FittedArc.IsReversed) return (null, 0); var deviation = System.Math.Max( ArcFit.MaxRadialDeviation(points, center.X, center.Y, mirrored.Radius), MaxArcToSegmentDeviation(points, center, mirrored.Radius, isReversed) ); if (deviation > Tolerance) return (null, 0); // The target's own fit meets its neighbors smoothly; a different radius // through the same endpoints turns at its ends, which would add corners. var arc = CreateArc(center, mirrored.Radius, points, target.FittedArc); var own = target.FittedArc; if ( AngleBetween(ArcTangent(arc, arc.StartAngle), ArcTangent(own, own.StartAngle)) > MirrorTangentAllowance || AngleBetween(ArcTangent(arc, arc.EndAngle), ArcTangent(own, own.EndAngle)) > MirrorTangentAllowance ) return (null, 0); return (arc, deviation); } private static Arc MirrorArc(Arc arc, MirrorAxisResult axis) { var mirrorCenter = axis.Reflect(arc.Center); // Reflect start and end points, then compute new angles var sp = arc.StartPoint(); var ep = arc.EndPoint(); var mirrorSp = axis.Reflect(sp); var mirrorEp = axis.Reflect(ep); // Mirroring reverses winding — swap start/end to preserve arc direction var mirrorStart = NormalizeAngle( PortableMath.Atan2(mirrorEp.Y - mirrorCenter.Y, mirrorEp.X - mirrorCenter.X) ); var mirrorEnd = NormalizeAngle( PortableMath.Atan2(mirrorSp.Y - mirrorCenter.Y, mirrorSp.X - mirrorCenter.X) ); var result = new Arc(mirrorCenter, arc.Radius, mirrorStart, mirrorEnd, arc.IsReversed); result.Layer = arc.Layer; result.Color = arc.Color; return result; } private void FindCandidatesInRun( List entities, int runStart, int runEnd, List candidates ) { var j = runStart; // The arcs fitted back to back up to j, each meeting the next. They are the // tail of candidates, in order. var chain = new List(); var lastStart = runEnd - MinLines + 1; // Trimming and dropping move j backwards; each pass still shortens or removes an // arc, so this bound is never reached in practice. It only guards the UI thread. var budget = 16 * (runEnd - runStart + 1) + 16; while (budget-- > 0) { if (j > lastStart) { // The run's last arc meets whatever follows the run (or lines it left). var (resume, droppedStart) = SettleChainEnd(entities, chain, candidates); if (resume < 0 && droppedStart >= 0) { // Dropped arcs left lines that a fresh start may still fit. resume = droppedStart + 1; chain.Clear(); } if (resume < 0 || resume > lastStart) break; j = resume; continue; } var previous = chain.Count > 0 ? chain[^1] : null; var chainedTangent = previous == null ? Vector.Invalid : EndTangent(previous.Candidate); ArcFitResult result = null; var strict = false; if (previous != null) { // Leave the previous arc along its own end tangent, so the two meet // smoothly. Failing that, give back the previous arc's last lines one at // a time: an arc grown as far as tolerance allows often ends on a // tangent no following arc can continue. result = TryFitArcAt(entities, j, runEnd, chainedTangent, strictStart: true); strict = result != null; if (result == null && TryShortenPrevious(entities, runEnd, previous, candidates, out var shortened, out var next)) { chain[^1] = shortened; j = shortened.Candidate.EndIndex + 1; chainedTangent = EndTangent(shortened.Candidate); result = next; strict = true; } } result ??= TryFitArcAt(entities, j, runEnd, chainedTangent); var candidate = result == null ? null : ToCandidate(entities, j, result); // A fallback fit is not tangent to what precedes it. Keep it only where the // drawing itself turns at least as sharply, or it adds a corner the original // does not have (two same-way arcs across an inflection, say); otherwise the // original line stays and the next arc starts after it. if (candidate != null && !strict && AddsCorner(entities, j, chainedTangent, candidate.FittedArc)) candidate = null; if (candidate == null) { // The line at j stays, so the chain's last arc now meets it. var (resume, droppedStart) = SettleChainEnd(entities, chain, candidates); if (resume >= 0) { j = resume; continue; } j = droppedStart >= 0 ? droppedStart + 1 : j + 1; chain.Clear(); continue; } candidates.Add(candidate); chain.Add(new ChainLink(candidate, chainedTangent, strict)); j = result.EndIndex + 1; } } private static Vector EndTangent(ArcCandidate candidate) => ComputeEndTangent(candidate.FittedArc.Center, candidate.Points); /// /// Makes the chain's last arc meet the original line after it without a corner the /// drawing does not have: trims it, or failing that drops it (keeping its lines) /// and settles the arc before it. Returns where to resume chaining after a trim /// (or -1), and the start of the earliest dropped arc (or -1). /// private (int resume, int droppedStart) SettleChainEnd( List entities, List chain, List candidates ) { var droppedStart = -1; while (chain.Count > 0) { var last = chain[^1]; if (!EndAddsCorner(entities, last.Candidate)) break; if (TryTrimEnd(entities, last, candidates, out var trimmed)) { chain[^1] = trimmed; return (trimmed.Candidate.EndIndex + 1, droppedStart); } candidates.RemoveAt(candidates.Count - 1); chain.RemoveAt(chain.Count - 1); droppedStart = last.Candidate.StartIndex; } return (-1, droppedStart); } /// /// True when 's arc meets the original entity after it /// at a sharper angle than the original polyline turns at that vertex. /// private static bool EndAddsCorner(List entities, ArcCandidate candidate) { var last = entities[candidate.EndIndex]; var after = candidate.EndIndex + 1 < entities.Count ? entities[candidate.EndIndex + 1] : entities[0]; if ( ReferenceEquals(after, last) || !EntityEndpoints.TryGet(last, out _, out var lastEnd) || !EntityEndpoints.TryGet(after, out var afterStart, out _) || lastEnd.DistanceTo(afterStart) > 1e-6 ) return false; var arc = candidate.FittedArc; var originalTurn = AngleBetween(GetExitDirection(last), GetEntryDirection(after)); var turn = AngleBetween(ArcTangent(arc, arc.EndAngle), GetEntryDirection(after)); return turn > originalTurn + CornerAllowance; } /// /// Ends (the last candidate) at the latest line where /// it meets the next original line without an added corner, keeping its start /// condition. Replaces the candidate on success. /// private bool TryTrimEnd( List entities, ChainLink previous, List candidates, out ChainLink trimmed ) { trimmed = null; var prev = previous.Candidate; for (var end = prev.EndIndex - 1; end >= prev.StartIndex + MinLines - 1; end--) { var shortened = TryFitArcAt(entities, prev.StartIndex, end, previous.ChainedIn, previous.Strict); if (shortened == null || shortened.EndIndex != end) continue; var candidate = ToCandidate(entities, prev.StartIndex, shortened); if (EndAddsCorner(entities, candidate)) continue; if (!previous.Strict && AddsCorner(entities, prev.StartIndex, previous.ChainedIn, candidate.FittedArc)) continue; candidates[^1] = candidate; trimmed = previous with { Candidate = candidate }; return true; } return false; } /// Extra corner, beyond the original polyline's turn, that a joint may have. private static readonly double CornerAllowance = Angle.ToRadians(1); /// How far a mirrored refit may turn the target arc's end tangents. private static readonly double MirrorTangentAllowance = Angle.ToRadians(0.5); /// /// True when , starting at entity , /// meets the geometry before it at a sharper angle than the original polyline /// turns at that vertex. is the previous arc's end /// tangent, or invalid to use the original entity before . /// private static bool AddsCorner(List entities, int start, Vector incoming, Arc arc) { var before = start > 0 ? entities[start - 1] : entities[^1]; if ( ReferenceEquals(before, entities[start]) || !EntityEndpoints.TryGet(before, out _, out var beforeEnd) || !EntityEndpoints.TryGet(entities[start], out var startPoint, out _) || beforeEnd.DistanceTo(startPoint) > 1e-6 ) return false; var originalTurn = AngleBetween(GetExitDirection(before), GetEntryDirection(entities[start])); var turn = AngleBetween( incoming.IsValid() ? incoming : GetExitDirection(before), ArcTangent(arc, arc.StartAngle) ); return turn > originalTurn + CornerAllowance; } private static double AngleBetween(Vector a, Vector b) { var cross = a.X * b.Y - a.Y * b.X; var dot = a.X * b.X + a.Y * b.Y; if (cross == 0 && dot == 0) return 0; return System.Math.Abs(PortableMath.Atan2(cross, dot)); } private sealed record ChainLink(ArcCandidate Candidate, Vector ChainedIn, bool Strict); /// /// Shortens (the last candidate) until an arc can start /// right after it along its end tangent. Replaces the candidate on success. /// private bool TryShortenPrevious( List entities, int runEnd, ChainLink previous, List candidates, out ChainLink shortenedLink, out ArcFitResult next ) { shortenedLink = null; next = null; var prev = previous.Candidate; for (var end = prev.EndIndex - 1; end >= prev.StartIndex + MinLines - 1; end--) { var shortened = TryFitArcAt(entities, prev.StartIndex, end, previous.ChainedIn, previous.Strict); if (shortened == null || shortened.EndIndex != end) continue; // Refit without an exact start tangent, the shorter arc starts differently. var candidate = ToCandidate(entities, prev.StartIndex, shortened); if (!previous.Strict && AddsCorner(entities, prev.StartIndex, previous.ChainedIn, candidate.FittedArc)) continue; // Only worth it if the next arc then reaches past the old end; otherwise this // just splits one arc in two. next = TryFitArcAt(entities, end + 1, runEnd, EndTangent(candidate), strictStart: true); if (next == null || next.EndIndex <= prev.EndIndex) continue; candidates[^1] = candidate; shortenedLink = previous with { Candidate = candidate }; return true; } return false; } private static ArcCandidate ToCandidate(List entities, int start, ArcFitResult result) { var arc = CreateArc(result.Center, result.Radius, result.Points, entities[start]); return new ArcCandidate { StartIndex = start, EndIndex = result.EndIndex, FittedArc = arc, MaxDeviation = result.Deviation, BoundingBox = result.Points.GetBoundingBox(), FirstPoint = arc.StartPoint(), LastPoint = arc.EndPoint(), Points = result.Points, }; } private record ArcFitResult( Vector Center, double Radius, double Deviation, List Points, int EndIndex ); /// /// Fits the longest arc starting at and ending no later /// than . With , the arc must /// leave exactly along (no balanced fallback). /// private ArcFitResult TryFitArcAt( List entities, int start, int runEnd, Vector chainedTangent, bool strictStart = false ) { var k = start + MinLines - 1; if (k > runEnd) return null; var points = CollectPoints(entities, start, k); if (points.Count < 3) return null; var startTangent = EstimateStartTangent(entities, start, points, chainedTangent); if (strictStart && chainedTangent.IsValid()) startTangent = startTangent with { Strict = true }; var endTangent = EstimateEndTangent(entities, k, points); var (center, radius, dev) = TryFit(points, startTangent, endTangent); if (!center.IsValid()) return null; // Extend the arc as far as possible while (k + 1 <= runEnd) { var extPoints = CollectPoints(entities, start, k + 1); if (extPoints.Count < 3) break; var extEndTangent = EstimateEndTangent(entities, k + 1, extPoints); var (nc, nr, nd) = TryFit(extPoints, startTangent, extEndTangent); if (!nc.IsValid()) break; k++; center = nc; radius = nr; dev = nd; points = extPoints; } // Reject arcs that subtend a tiny angle — these are nearly-straight lines // that happen to fit a huge circle. Applied after extension so that many small // segments can accumulate enough sweep to qualify. var sweep = System.Math.Abs(ArcFit.SumSignedAngles(center, points)); if (sweep < Angle.ToRadians(5)) return null; return new ArcFitResult(center, radius, dev, points, k); } private (Vector center, double radius, double deviation) TryFit( List points, TangentEstimate start, TangentEstimate end ) { foreach (var (center, radius, dev) in FitAttempts(points, start, end)) { if (!center.IsValid() || dev > Tolerance) continue; // Check that the arc doesn't bulge away from the original line segments var isReversed = ArcFit.SumSignedAngles(center, points) < 0; var arcDev = MaxArcToSegmentDeviation(points, center, radius, isReversed); if (arcDev > Tolerance) continue; return (center, radius, System.Math.Max(dev, arcDev)); } return (Vector.Invalid, 0, 0); } /// /// Yields fit attempts in preference order. A trusted tangent (chained from the /// previous arc, an adjacent original arc, or a long straight edge) is enforced /// exactly on its side; otherwise the tangency error is balanced between both /// endpoints. The unconstrained mirror-axis fit is the last resort. Every attempt /// passes exactly through both endpoints, so no gaps are introduced. /// private IEnumerable<(Vector center, double radius, double deviation)> FitAttempts( List points, TangentEstimate start, TangentEstimate end ) { if (start.Strict) { yield return ArcFit.FitWithStartTangent(points, start.Direction); yield break; } if (start.Trusted && !end.Trusted) { yield return ArcFit.FitWithStartTangent(points, start.Direction); yield return ArcFit.FitThroughEndpointsWithTangents( points, start.Direction, end.Direction ); yield return FitWithEndTangent(points, end.Direction); } else if (end.Trusted && !start.Trusted) { yield return FitWithEndTangent(points, end.Direction); yield return ArcFit.FitThroughEndpointsWithTangents( points, start.Direction, end.Direction ); yield return ArcFit.FitWithStartTangent(points, start.Direction); } else { yield return ArcFit.FitThroughEndpointsWithTangents( points, start.Direction, end.Direction ); yield return ArcFit.FitWithStartTangent(points, start.Direction); yield return FitWithEndTangent(points, end.Direction); } yield return FitMirrorAxis(points); } /// /// Fits an arc through both endpoints with an exact tangent at the last point, /// by running the start-tangent fit on the reversed point sequence. /// private static (Vector center, double radius, double deviation) FitWithEndTangent( List points, Vector endTangent ) { var reversed = new List(points); reversed.Reverse(); return ArcFit.FitWithStartTangent(reversed, new Vector(-endTangent.X, -endTangent.Y)); } /// /// An estimated tangent direction at a fit endpoint. Trusted estimates come from /// exact geometry (a chained arc, an adjacent original arc, or a long straight /// edge) and are enforced exactly; untrusted ones are derived from the polyline /// vertices and only guide the fit. /// private readonly record struct TangentEstimate(Vector Direction, bool Trusted) { /// Enforce this tangent exactly, with no balanced or free fallback. public bool Strict { get; init; } } /// Segment-length ratio above which a neighboring line counts as a true /// straight edge (rather than another chord of the tessellated curve). private const double NeighborEdgeFactor = 3.0; private static TangentEstimate EstimateStartTangent( List entities, int start, List points, Vector chainedTangent ) { if (chainedTangent.IsValid()) return new TangentEstimate(chainedTangent, true); if (entities[start] is Arc startArc) return new TangentEstimate(GetEntryDirection(startArc), true); var firstChordLen = points[0].DistanceTo(points[1]); if (start > 0) { var prev = entities[start - 1]; if (EntityEndpoints.TryGet(prev, out _, out var prevEnd) && prevEnd.DistanceTo(points[0]) < 1e-6) { if (prev is Arc) return new TangentEstimate(GetExitDirection(prev), true); if ( prev is Line prevLine && prevLine.StartPoint.DistanceTo(prevLine.EndPoint) >= NeighborEdgeFactor * firstChordLen ) return new TangentEstimate(GetExitDirection(prevLine), true); } } var chord = new Vector(points[1].X - points[0].X, points[1].Y - points[0].Y); if (points.Count >= 3) return new TangentEstimate( EstimateVertexTangent(points[0], points[1], points[2], chord), false ); return new TangentEstimate(chord, false); } private static TangentEstimate EstimateEndTangent( List entities, int k, List points ) { if (entities[k] is Arc endArc) return new TangentEstimate(GetExitDirection(endArc), true); var lastChordLen = points[^1].DistanceTo(points[^2]); if (k + 1 < entities.Count) { var next = entities[k + 1]; if (EntityEndpoints.TryGet(next, out var nextStart, out _) && nextStart.DistanceTo(points[^1]) < 1e-6) { if (next is Arc nextArc) return new TangentEstimate(GetEntryDirection(nextArc), true); if ( next is Line nextLine && nextLine.StartPoint.DistanceTo(nextLine.EndPoint) >= NeighborEdgeFactor * lastChordLen ) return new TangentEstimate(GetExitDirection(nextLine), true); } } var chord = new Vector(points[^1].X - points[^2].X, points[^1].Y - points[^2].Y); if (points.Count >= 3) return new TangentEstimate( EstimateVertexTangent(points[^1], points[^2], points[^3], chord), false ); return new TangentEstimate(chord, false); } /// /// Estimates the curve tangent at a polyline vertex from the circle through it and /// its two nearest neighbors. A raw chord direction is off from the true tangent by /// half the chord's subtended angle; the circumcircle estimate removes that bias. /// Falls back to the travel direction when the three points are collinear. /// private static Vector EstimateVertexTangent(Vector at, Vector b, Vector c, Vector travel) { var d = 2 * (at.X * (b.Y - c.Y) + b.X * (c.Y - at.Y) + c.X * (at.Y - b.Y)); if (System.Math.Abs(d) < 1e-14) return travel; var sqA = at.X * at.X + at.Y * at.Y; var sqB = b.X * b.X + b.Y * b.Y; var sqC = c.X * c.X + c.Y * c.Y; var cx = (sqA * (b.Y - c.Y) + sqB * (c.Y - at.Y) + sqC * (at.Y - b.Y)) / d; var cy = (sqA * (c.X - b.X) + sqB * (at.X - c.X) + sqC * (b.X - at.X)) / d; var tangent = new Vector(-(at.Y - cy), at.X - cx); if (tangent.X * travel.X + tangent.Y * travel.Y < 0) tangent = new Vector(-tangent.X, -tangent.Y); return tangent; } /// /// Returns the entry direction (tangent at start point) of an entity. /// private static Vector GetEntryDirection(Entity entity) => entity switch { Line line => LineDirection(line), Arc arc => ArcTangent(arc, arc.StartAngle), _ => Vector.Invalid, }; private static Vector LineDirection(Line line) => new(line.EndPoint.X - line.StartPoint.X, line.EndPoint.Y - line.StartPoint.Y); /// The direction of travel along an arc at . private static Vector ArcTangent(Arc arc, double angle) => arc.IsReversed ? new Vector(PortableMath.Sin(angle), -PortableMath.Cos(angle)) : new Vector(-PortableMath.Sin(angle), PortableMath.Cos(angle)); /// /// Computes the tangent direction at the last point of a fitted arc, /// used to chain tangent continuity to the next arc. /// private static Vector ComputeEndTangent(Vector center, List points) { var lastPt = points[^1]; var rx = lastPt.X - center.X; var ry = lastPt.Y - center.Y; var sign = ArcFit.SumSignedAngles(center, points) >= 0 ? 1 : -1; return new Vector(-sign * ry, sign * rx); } /// /// Fits a circular arc using the mirror axis approach. The center is constrained /// to the perpendicular bisector of the chord (P1->Pn), guaranteeing the arc /// passes exactly through both endpoints. Golden section search optimizes position. /// private (Vector center, double radius, double deviation) FitMirrorAxis(List points) { if (points.Count < 3) return (Vector.Invalid, 0, double.MaxValue); var p1 = points[0]; var pn = points[^1]; var mx = (p1.X + pn.X) / 2; var my = (p1.Y + pn.Y) / 2; var dx = pn.X - p1.X; var dy = pn.Y - p1.Y; var chordLen = System.Math.Sqrt(dx * dx + dy * dy); if (chordLen < 1e-10) return (Vector.Invalid, 0, double.MaxValue); var halfChord = chordLen / 2; var nx = -dy / chordLen; var ny = dx / chordLen; var maxSagitta = 0.0; for (var i = 1; i < points.Count - 1; i++) { var proj = (points[i].X - mx) * nx + (points[i].Y - my) * ny; if (System.Math.Abs(proj) > System.Math.Abs(maxSagitta)) maxSagitta = proj; } if (System.Math.Abs(maxSagitta) < 1e-10) return (Vector.Invalid, 0, double.MaxValue); var dInit = (maxSagitta * maxSagitta - halfChord * halfChord) / (2 * maxSagitta); var range = System.Math.Max(System.Math.Abs(dInit) * 2, halfChord); var dOpt = GoldenSectionMin( dInit - range, dInit + range, d => ArcFit.MaxRadialDeviation( points, mx + d * nx, my + d * ny, System.Math.Sqrt(halfChord * halfChord + d * d) ) ); var center = new Vector(mx + dOpt * nx, my + dOpt * ny); var radius = System.Math.Sqrt(halfChord * halfChord + dOpt * dOpt); return (center, radius, ArcFit.MaxRadialDeviation(points, center.X, center.Y, radius)); } private static double GoldenSectionMin(double low, double high, Func eval) { var phi = (System.Math.Sqrt(5) - 1) / 2; for (var iter = 0; iter < 30; iter++) { var d1 = high - phi * (high - low); var d2 = low + phi * (high - low); if (eval(d1) < eval(d2)) high = d2; else low = d1; if (high - low < 1e-6) break; } return (low + high) / 2; } private static List CollectPoints(List entities, int start, int end) { var points = new List(); for (var i = start; i <= end; i++) { switch (entities[i]) { case Line line: if (i == start) points.Add(line.StartPoint); points.Add(line.EndPoint); break; case Arc arc: if (i == start) points.Add(arc.StartPoint()); var segments = System.Math.Max(2, arc.SegmentsForTolerance(0.1)); var arcPoints = arc.ToPoints(segments); for (var j = 1; j < arcPoints.Count; j++) points.Add(arcPoints[j]); break; } } return points; } private static Arc CreateArc( Vector center, double radius, List points, Entity sourceEntity ) { var firstPoint = points[0]; var lastPoint = points[^1]; var startAngle = NormalizeAngle( PortableMath.Atan2(firstPoint.Y - center.Y, firstPoint.X - center.X) ); var endAngle = NormalizeAngle( PortableMath.Atan2(lastPoint.Y - center.Y, lastPoint.X - center.X) ); var isReversed = ArcFit.SumSignedAngles(center, points) < 0; var arc = new Arc(center, radius, startAngle, endAngle, isReversed); arc.Layer = sourceEntity.Layer; arc.Color = sourceEntity.Color; return arc; } /// /// Returns the exit direction (tangent at endpoint) of an entity. /// private static Vector GetExitDirection(Entity entity) => entity switch { Line line => LineDirection(line), Arc arc => ArcTangent(arc, arc.EndAngle), _ => Vector.Invalid, }; /// /// Measures the maximum distance from sampled points along the fitted arc /// back to the original line segments. This catches cases where points lie /// on a large circle but the arc bulges far from the original straight geometry. /// private static double MaxArcToSegmentDeviation( List points, Vector center, double radius, bool isReversed ) { var startAngle = PortableMath.Atan2(points[0].Y - center.Y, points[0].X - center.X); var endAngle = PortableMath.Atan2(points[^1].Y - center.Y, points[^1].X - center.X); var sweep = endAngle - startAngle; if (isReversed) { if (sweep > 0) sweep -= Angle.TwoPI; } else { if (sweep < 0) sweep += Angle.TwoPI; } var sampleCount = System.Math.Max(10, (int)(System.Math.Abs(sweep) * radius * 10)); sampleCount = System.Math.Min(sampleCount, 100); var maxDev = 0.0; for (var i = 1; i < sampleCount; i++) { var t = (double)i / sampleCount; var angle = startAngle + sweep * t; var px = center.X + radius * PortableMath.Cos(angle); var py = center.Y + radius * PortableMath.Sin(angle); var arcPt = new Vector(px, py); var minDist = double.MaxValue; for (var j = 0; j < points.Count - 1; j++) { var dist = DistanceToSegment(arcPt, points[j], points[j + 1]); if (dist < minDist) minDist = dist; } if (minDist > maxDev) maxDev = minDist; } return maxDev; } private static double DistanceToSegment(Vector p, Vector a, Vector b) { var dx = b.X - a.X; var dy = b.Y - a.Y; var lenSq = dx * dx + dy * dy; if (lenSq < 1e-20) return System.Math.Sqrt((p.X - a.X) * (p.X - a.X) + (p.Y - a.Y) * (p.Y - a.Y)); var t = ((p.X - a.X) * dx + (p.Y - a.Y) * dy) / lenSq; t = System.Math.Max(0, System.Math.Min(1, t)); var projX = a.X + t * dx; var projY = a.Y + t * dy; return System.Math.Sqrt((p.X - projX) * (p.X - projX) + (p.Y - projY) * (p.Y - projY)); } }