|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Six Labors Split License. |
| 3 | + |
| 4 | +using System.Runtime.CompilerServices; |
| 5 | + |
| 6 | +namespace SixLabors.PolygonClipper; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Represents an edge that is currently active in the sweep-line. |
| 10 | +/// </summary> |
| 11 | +/// <remarks> |
| 12 | +/// The sweep assumes a Y-axis-positive-down coordinate system. "Bottom" and "Top" |
| 13 | +/// refer to the lower and upper scanline endpoints (larger and smaller Y respectively). |
| 14 | +/// </remarks> |
| 15 | +internal sealed class ActiveEdge |
| 16 | +{ |
| 17 | +#pragma warning disable SA1401 // Hot sweep state uses fields to avoid accessor overhead. |
| 18 | + /// <summary> |
| 19 | + /// The lower endpoint of the edge in scanline order. |
| 20 | + /// </summary> |
| 21 | + public Vertex Bottom; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// The upper endpoint of the edge in scanline order. |
| 25 | + /// </summary> |
| 26 | + public Vertex Top; |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// The X coordinate where the edge intersects the current scanline. |
| 30 | + /// </summary> |
| 31 | + public double CurrentX; |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// The delta-X per delta-Y for the edge (its scanline slope). |
| 35 | + /// </summary> |
| 36 | + public double Dx; |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// The winding delta contributed by this edge (+1 or -1). |
| 40 | + /// </summary> |
| 41 | + public int WindDelta; |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// The accumulated winding count for this edge. |
| 45 | + /// </summary> |
| 46 | + public int WindCount; |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// The output record this edge is contributing to, if any. |
| 50 | + /// </summary> |
| 51 | + public OutputRecord? OutputRecord; |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// The previous edge in the Active Edge List (AEL). |
| 55 | + /// </summary> |
| 56 | + public ActiveEdge? PrevInAel; |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// The next edge in the Active Edge List (AEL). |
| 60 | + /// </summary> |
| 61 | + public ActiveEdge? NextInAel; |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// The previous edge in the Sorted Edge List (SEL). |
| 65 | + /// </summary> |
| 66 | + public ActiveEdge? PrevInSel; |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// The next edge in the Sorted Edge List (SEL). |
| 70 | + /// </summary> |
| 71 | + public ActiveEdge? NextInSel; |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// The temporary link used when sorting intersections. |
| 75 | + /// </summary> |
| 76 | + public ActiveEdge? Jump; |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// The current top vertex for this edge's bound. |
| 80 | + /// </summary> |
| 81 | + public SweepVertex? VertexTop; |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// The local minima that spawned this edge. |
| 85 | + /// </summary> |
| 86 | + public LocalMinima LocalMin; |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Indicates whether this edge is the left bound of its pair. |
| 90 | + /// </summary> |
| 91 | + public bool IsLeftBound; |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// The pending join state for this edge. |
| 95 | + /// </summary> |
| 96 | + public JoinWith JoinWith; |
| 97 | +#pragma warning restore SA1401 |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Gets a value indicating whether this edge currently contributes to output. |
| 101 | + /// </summary> |
| 102 | + public bool IsHot => this.OutputRecord != null; |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Gets a value indicating whether the edge is horizontal within tolerance. |
| 106 | + /// </summary> |
| 107 | + public bool IsHorizontal => this.Top.Y == this.Bottom.Y; |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// Gets a value indicating whether a horizontal edge is heading right. |
| 111 | + /// </summary> |
| 112 | + public bool IsHeadingRightHorizontal => double.IsNegativeInfinity(this.Dx); |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// Gets a value indicating whether a horizontal edge is heading left. |
| 116 | + /// </summary> |
| 117 | + public bool IsHeadingLeftHorizontal => double.IsPositiveInfinity(this.Dx); |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// Gets a value indicating whether the current top vertex is a local maxima. |
| 121 | + /// </summary> |
| 122 | + public bool IsMaxima => this.VertexTop != null && this.VertexTop.IsMaxima; |
| 123 | + |
| 124 | + /// <summary> |
| 125 | + /// Gets a value indicating whether this edge is the front edge of its output record. |
| 126 | + /// </summary> |
| 127 | + public bool IsFront => this.OutputRecord != null && this == this.OutputRecord.FrontEdge; |
| 128 | + |
| 129 | + /// <summary> |
| 130 | + /// Gets the next input vertex along the bound in the winding direction. |
| 131 | + /// </summary> |
| 132 | + public SweepVertex NextVertex => this.WindDelta > 0 ? this.VertexTop!.Next! : this.VertexTop!.Prev!; |
| 133 | + |
| 134 | + /// <summary> |
| 135 | + /// Gets the vertex two steps behind the current top, used for turn tests. |
| 136 | + /// </summary> |
| 137 | + public SweepVertex PrevPrevVertex => this.WindDelta > 0 ? this.VertexTop!.Prev!.Prev! : this.VertexTop!.Next!.Next!; |
| 138 | + |
| 139 | + /// <summary> |
| 140 | + /// Finds the previous hot edge in the AEL, if any. |
| 141 | + /// </summary> |
| 142 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 143 | + public ActiveEdge? GetPrevHotEdge() |
| 144 | + { |
| 145 | + ActiveEdge? prev = this.PrevInAel; |
| 146 | + while (prev != null && !prev.IsHot) |
| 147 | + { |
| 148 | + prev = prev.PrevInAel; |
| 149 | + } |
| 150 | + |
| 151 | + return prev; |
| 152 | + } |
| 153 | + |
| 154 | + /// <summary> |
| 155 | + /// Calculates the X coordinate where this edge intersects the scanline at <paramref name="currentY" />. |
| 156 | + /// </summary> |
| 157 | + // This method sits on the hottest path in large self-intersection workloads. |
| 158 | + // AggressiveOptimization consistently improves codegen here versus tiered defaults. |
| 159 | + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] |
| 160 | + public static double TopX(ActiveEdge edge, double currentY) |
| 161 | + { |
| 162 | + if (currentY == edge.Top.Y || edge.Top.X == edge.Bottom.X) |
| 163 | + { |
| 164 | + return edge.Top.X; |
| 165 | + } |
| 166 | + |
| 167 | + if (currentY == edge.Bottom.Y) |
| 168 | + { |
| 169 | + return edge.Bottom.X; |
| 170 | + } |
| 171 | + |
| 172 | + return edge.Bottom.X + (edge.Dx * (currentY - edge.Bottom.Y)); |
| 173 | + } |
| 174 | + |
| 175 | + /// <summary> |
| 176 | + /// Recomputes <see cref="Dx" /> from the current endpoints. |
| 177 | + /// </summary> |
| 178 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 179 | + public void UpdateDx() => this.Dx = GetDx(this.Bottom, this.Top); |
| 180 | + |
| 181 | + /// <summary> |
| 182 | + /// Computes delta-X per delta-Y, returning infinities for horizontal edges. |
| 183 | + /// </summary> |
| 184 | + private static double GetDx(Vertex pt1, Vertex pt2) |
| 185 | + { |
| 186 | + double dy = pt2.Y - pt1.Y; |
| 187 | + if (dy != 0) |
| 188 | + { |
| 189 | + return (pt2.X - pt1.X) / dy; |
| 190 | + } |
| 191 | + |
| 192 | + return pt2.X > pt1.X ? double.NegativeInfinity : double.PositiveInfinity; |
| 193 | + } |
| 194 | +} |
0 commit comments