ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "GrTessellator.h" |
| 9 | |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 10 | #include "GrDefaultGeoProcFactory.h" |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 11 | #include "GrPathUtils.h" |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 12 | |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 13 | #include "SkArenaAlloc.h" |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 14 | #include "SkGeometry.h" |
| 15 | #include "SkPath.h" |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 16 | #include "SkPointPriv.h" |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 17 | #include "SkTDPQueue.h" |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 18 | |
Stephen White | 94b7e54 | 2018-01-04 14:01:10 -0500 | [diff] [blame] | 19 | #include <algorithm> |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 20 | #include <cstdio> |
| 21 | #include <utility> |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 22 | |
| 23 | /* |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 24 | * There are six stages to the basic algorithm: |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 25 | * |
| 26 | * 1) Linearize the path contours into piecewise linear segments (path_to_contours()). |
| 27 | * 2) Build a mesh of edges connecting the vertices (build_edges()). |
| 28 | * 3) Sort the vertices in Y (and secondarily in X) (merge_sort()). |
| 29 | * 4) Simplify the mesh by inserting new vertices at intersecting edges (simplify()). |
| 30 | * 5) Tessellate the simplified mesh into monotone polygons (tessellate()). |
| 31 | * 6) Triangulate the monotone polygons directly into a vertex buffer (polys_to_triangles()). |
| 32 | * |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 33 | * For screenspace antialiasing, the algorithm is modified as follows: |
| 34 | * |
| 35 | * Run steps 1-5 above to produce polygons. |
| 36 | * 5b) Apply fill rules to extract boundary contours from the polygons (extract_boundaries()). |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 37 | * 5c) Simplify boundaries to remove "pointy" vertices that cause inversions (simplify_boundary()). |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 38 | * 5d) Displace edges by half a pixel inward and outward along their normals. Intersect to find |
| 39 | * new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a new |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 40 | * antialiased mesh from those vertices (stroke_boundary()). |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 41 | * Run steps 3-6 above on the new mesh, and produce antialiased triangles. |
| 42 | * |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 43 | * The vertex sorting in step (3) is a merge sort, since it plays well with the linked list |
| 44 | * of vertices (and the necessity of inserting new vertices on intersection). |
| 45 | * |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 46 | * Stages (4) and (5) use an active edge list -- a list of all edges for which the |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 47 | * sweep line has crossed the top vertex, but not the bottom vertex. It's sorted |
| 48 | * left-to-right based on the point where both edges are active (when both top vertices |
| 49 | * have been seen, so the "lower" top vertex of the two). If the top vertices are equal |
| 50 | * (shared), it's sorted based on the last point where both edges are active, so the |
| 51 | * "upper" bottom vertex. |
| 52 | * |
| 53 | * The most complex step is the simplification (4). It's based on the Bentley-Ottman |
| 54 | * line-sweep algorithm, but due to floating point inaccuracy, the intersection points are |
| 55 | * not exact and may violate the mesh topology or active edge list ordering. We |
| 56 | * accommodate this by adjusting the topology of the mesh and AEL to match the intersection |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 57 | * points. This occurs in two ways: |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 58 | * |
| 59 | * A) Intersections may cause a shortened edge to no longer be ordered with respect to its |
| 60 | * neighbouring edges at the top or bottom vertex. This is handled by merging the |
| 61 | * edges (merge_collinear_edges()). |
| 62 | * B) Intersections may cause an edge to violate the left-to-right ordering of the |
Stephen White | 019f6c0 | 2017-06-09 10:06:26 -0400 | [diff] [blame] | 63 | * active edge list. This is handled by detecting potential violations and rewinding |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 64 | * the active edge list to the vertex before they occur (rewind() during merging, |
| 65 | * rewind_if_necessary() during splitting). |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 66 | * |
| 67 | * The tessellation steps (5) and (6) are based on "Triangulating Simple Polygons and |
| 68 | * Equivalent Problems" (Fournier and Montuno); also a line-sweep algorithm. Note that it |
| 69 | * currently uses a linked list for the active edge list, rather than a 2-3 tree as the |
| 70 | * paper describes. The 2-3 tree gives O(lg N) lookups, but insertion and removal also |
| 71 | * become O(lg N). In all the test cases, it was found that the cost of frequent O(lg N) |
| 72 | * insertions and removals was greater than the cost of infrequent O(N) lookups with the |
| 73 | * linked list implementation. With the latter, all removals are O(1), and most insertions |
| 74 | * are O(1), since we know the adjacent edge in the active edge list based on the topology. |
| 75 | * Only type 2 vertices (see paper) require the O(N) lookups, and these are much less |
| 76 | * frequent. There may be other data structures worth investigating, however. |
| 77 | * |
| 78 | * Note that the orientation of the line sweep algorithms is determined by the aspect ratio of the |
| 79 | * path bounds. When the path is taller than it is wide, we sort vertices based on increasing Y |
| 80 | * coordinate, and secondarily by increasing X coordinate. When the path is wider than it is tall, |
| 81 | * we sort by increasing X coordinate, but secondarily by *decreasing* Y coordinate. This is so |
| 82 | * that the "left" and "right" orientation in the code remains correct (edges to the left are |
| 83 | * increasing in Y; edges to the right are decreasing in Y). That is, the setting rotates 90 |
| 84 | * degrees counterclockwise, rather that transposing. |
| 85 | */ |
| 86 | |
| 87 | #define LOGGING_ENABLED 0 |
| 88 | |
| 89 | #if LOGGING_ENABLED |
| 90 | #define LOG printf |
| 91 | #else |
| 92 | #define LOG(...) |
| 93 | #endif |
| 94 | |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 95 | namespace { |
| 96 | |
Stephen White | 11f65e0 | 2017-02-16 19:00:39 -0500 | [diff] [blame] | 97 | const int kArenaChunkSize = 16 * 1024; |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 98 | const float kCosMiterAngle = 0.97f; // Corresponds to an angle of ~14 degrees. |
Stephen White | 11f65e0 | 2017-02-16 19:00:39 -0500 | [diff] [blame] | 99 | |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 100 | struct Vertex; |
| 101 | struct Edge; |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 102 | struct Event; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 103 | struct Poly; |
| 104 | |
| 105 | template <class T, T* T::*Prev, T* T::*Next> |
senorblanco | e6eaa32 | 2016-03-08 09:06:44 -0800 | [diff] [blame] | 106 | void list_insert(T* t, T* prev, T* next, T** head, T** tail) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 107 | t->*Prev = prev; |
| 108 | t->*Next = next; |
| 109 | if (prev) { |
| 110 | prev->*Next = t; |
| 111 | } else if (head) { |
| 112 | *head = t; |
| 113 | } |
| 114 | if (next) { |
| 115 | next->*Prev = t; |
| 116 | } else if (tail) { |
| 117 | *tail = t; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | template <class T, T* T::*Prev, T* T::*Next> |
senorblanco | e6eaa32 | 2016-03-08 09:06:44 -0800 | [diff] [blame] | 122 | void list_remove(T* t, T** head, T** tail) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 123 | if (t->*Prev) { |
| 124 | t->*Prev->*Next = t->*Next; |
| 125 | } else if (head) { |
| 126 | *head = t->*Next; |
| 127 | } |
| 128 | if (t->*Next) { |
| 129 | t->*Next->*Prev = t->*Prev; |
| 130 | } else if (tail) { |
| 131 | *tail = t->*Prev; |
| 132 | } |
| 133 | t->*Prev = t->*Next = nullptr; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Vertices are used in three ways: first, the path contours are converted into a |
| 138 | * circularly-linked list of Vertices for each contour. After edge construction, the same Vertices |
| 139 | * are re-ordered by the merge sort according to the sweep_lt comparator (usually, increasing |
| 140 | * in Y) using the same fPrev/fNext pointers that were used for the contours, to avoid |
| 141 | * reallocation. Finally, MonotonePolys are built containing a circularly-linked list of |
| 142 | * Vertices. (Currently, those Vertices are newly-allocated for the MonotonePolys, since |
| 143 | * an individual Vertex from the path mesh may belong to multiple |
| 144 | * MonotonePolys, so the original Vertices cannot be re-used. |
| 145 | */ |
| 146 | |
| 147 | struct Vertex { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 148 | Vertex(const SkPoint& point, uint8_t alpha) |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 149 | : fPoint(point), fPrev(nullptr), fNext(nullptr) |
| 150 | , fFirstEdgeAbove(nullptr), fLastEdgeAbove(nullptr) |
| 151 | , fFirstEdgeBelow(nullptr), fLastEdgeBelow(nullptr) |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 152 | , fLeftEnclosingEdge(nullptr), fRightEnclosingEdge(nullptr) |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 153 | , fPartner(nullptr) |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 154 | , fAlpha(alpha) |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 155 | #if LOGGING_ENABLED |
| 156 | , fID (-1.0f) |
| 157 | #endif |
| 158 | {} |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 159 | SkPoint fPoint; // Vertex position |
| 160 | Vertex* fPrev; // Linked list of contours, then Y-sorted vertices. |
| 161 | Vertex* fNext; // " |
| 162 | Edge* fFirstEdgeAbove; // Linked list of edges above this vertex. |
| 163 | Edge* fLastEdgeAbove; // " |
| 164 | Edge* fFirstEdgeBelow; // Linked list of edges below this vertex. |
| 165 | Edge* fLastEdgeBelow; // " |
| 166 | Edge* fLeftEnclosingEdge; // Nearest edge in the AEL left of this vertex. |
| 167 | Edge* fRightEnclosingEdge; // Nearest edge in the AEL right of this vertex. |
| 168 | Vertex* fPartner; // Corresponding inner or outer vertex (for AA). |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 169 | uint8_t fAlpha; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 170 | #if LOGGING_ENABLED |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 171 | float fID; // Identifier used for logging. |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 172 | #endif |
| 173 | }; |
| 174 | |
| 175 | /***************************************************************************************/ |
| 176 | |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 177 | struct AAParams { |
| 178 | bool fTweakAlpha; |
| 179 | GrColor fColor; |
| 180 | }; |
| 181 | |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 182 | typedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b); |
| 183 | |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 184 | bool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) { |
Stephen White | 16a40cb | 2017-02-23 11:10:01 -0500 | [diff] [blame] | 185 | return a.fX < b.fX || (a.fX == b.fX && a.fY > b.fY); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | bool sweep_lt_vert(const SkPoint& a, const SkPoint& b) { |
Stephen White | 16a40cb | 2017-02-23 11:10:01 -0500 | [diff] [blame] | 189 | return a.fY < b.fY || (a.fY == b.fY && a.fX < b.fX); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Stephen White | 16a40cb | 2017-02-23 11:10:01 -0500 | [diff] [blame] | 192 | struct Comparator { |
| 193 | enum class Direction { kVertical, kHorizontal }; |
| 194 | Comparator(Direction direction) : fDirection(direction) {} |
| 195 | bool sweep_lt(const SkPoint& a, const SkPoint& b) const { |
| 196 | return fDirection == Direction::kHorizontal ? sweep_lt_horiz(a, b) : sweep_lt_vert(a, b); |
| 197 | } |
Stephen White | 16a40cb | 2017-02-23 11:10:01 -0500 | [diff] [blame] | 198 | Direction fDirection; |
| 199 | }; |
| 200 | |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 201 | inline void* emit_vertex(Vertex* v, const AAParams* aaParams, void* data) { |
| 202 | if (!aaParams) { |
| 203 | SkPoint* d = static_cast<SkPoint*>(data); |
| 204 | *d++ = v->fPoint; |
| 205 | return d; |
| 206 | } |
| 207 | if (aaParams->fTweakAlpha) { |
| 208 | auto d = static_cast<GrDefaultGeoProcFactory::PositionColorAttr*>(data); |
| 209 | d->fPosition = v->fPoint; |
lsalzman | 8c8fcef | 2016-10-11 12:20:17 -0700 | [diff] [blame] | 210 | d->fColor = SkAlphaMulQ(aaParams->fColor, SkAlpha255To256(v->fAlpha)); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 211 | d++; |
| 212 | return d; |
| 213 | } |
| 214 | auto d = static_cast<GrDefaultGeoProcFactory::PositionColorCoverageAttr*>(data); |
| 215 | d->fPosition = v->fPoint; |
| 216 | d->fColor = aaParams->fColor; |
| 217 | d->fCoverage = GrNormalizeByteToFloat(v->fAlpha); |
| 218 | d++; |
| 219 | return d; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 220 | } |
| 221 | |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 222 | void* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, const AAParams* aaParams, void* data) { |
Stephen White | 95152e1 | 2017-12-18 10:52:44 -0500 | [diff] [blame] | 223 | LOG("emit_triangle %g (%g, %g) %d\n", v0->fID, v0->fPoint.fX, v0->fPoint.fY, v0->fAlpha); |
| 224 | LOG(" %g (%g, %g) %d\n", v1->fID, v1->fPoint.fX, v1->fPoint.fY, v1->fAlpha); |
| 225 | LOG(" %g (%g, %g) %d\n", v2->fID, v2->fPoint.fX, v2->fPoint.fY, v2->fAlpha); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 226 | #if TESSELLATOR_WIREFRAME |
| 227 | data = emit_vertex(v0, aaParams, data); |
| 228 | data = emit_vertex(v1, aaParams, data); |
| 229 | data = emit_vertex(v1, aaParams, data); |
| 230 | data = emit_vertex(v2, aaParams, data); |
| 231 | data = emit_vertex(v2, aaParams, data); |
| 232 | data = emit_vertex(v0, aaParams, data); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 233 | #else |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 234 | data = emit_vertex(v0, aaParams, data); |
| 235 | data = emit_vertex(v1, aaParams, data); |
| 236 | data = emit_vertex(v2, aaParams, data); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 237 | #endif |
| 238 | return data; |
| 239 | } |
| 240 | |
senorblanco | e6eaa32 | 2016-03-08 09:06:44 -0800 | [diff] [blame] | 241 | struct VertexList { |
| 242 | VertexList() : fHead(nullptr), fTail(nullptr) {} |
Stephen White | 16a40cb | 2017-02-23 11:10:01 -0500 | [diff] [blame] | 243 | VertexList(Vertex* head, Vertex* tail) : fHead(head), fTail(tail) {} |
senorblanco | e6eaa32 | 2016-03-08 09:06:44 -0800 | [diff] [blame] | 244 | Vertex* fHead; |
| 245 | Vertex* fTail; |
| 246 | void insert(Vertex* v, Vertex* prev, Vertex* next) { |
| 247 | list_insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, prev, next, &fHead, &fTail); |
| 248 | } |
| 249 | void append(Vertex* v) { |
| 250 | insert(v, fTail, nullptr); |
| 251 | } |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 252 | void append(const VertexList& list) { |
| 253 | if (!list.fHead) { |
| 254 | return; |
| 255 | } |
| 256 | if (fTail) { |
| 257 | fTail->fNext = list.fHead; |
| 258 | list.fHead->fPrev = fTail; |
| 259 | } else { |
| 260 | fHead = list.fHead; |
| 261 | } |
| 262 | fTail = list.fTail; |
| 263 | } |
senorblanco | e6eaa32 | 2016-03-08 09:06:44 -0800 | [diff] [blame] | 264 | void prepend(Vertex* v) { |
| 265 | insert(v, nullptr, fHead); |
| 266 | } |
Stephen White | bf6137e | 2017-01-04 15:43:26 -0500 | [diff] [blame] | 267 | void remove(Vertex* v) { |
| 268 | list_remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, &fHead, &fTail); |
| 269 | } |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 270 | void close() { |
| 271 | if (fHead && fTail) { |
| 272 | fTail->fNext = fHead; |
| 273 | fHead->fPrev = fTail; |
| 274 | } |
| 275 | } |
senorblanco | e6eaa32 | 2016-03-08 09:06:44 -0800 | [diff] [blame] | 276 | }; |
| 277 | |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 278 | // Round to nearest quarter-pixel. This is used for screenspace tessellation. |
| 279 | |
| 280 | inline void round(SkPoint* p) { |
| 281 | p->fX = SkScalarRoundToScalar(p->fX * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f); |
| 282 | p->fY = SkScalarRoundToScalar(p->fY * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f); |
| 283 | } |
| 284 | |
Stephen White | 94b7e54 | 2018-01-04 14:01:10 -0500 | [diff] [blame] | 285 | inline SkScalar double_to_clamped_scalar(double d) { |
| 286 | return SkDoubleToScalar(std::min((double) SK_ScalarMax, std::max(d, (double) -SK_ScalarMax))); |
| 287 | } |
| 288 | |
senorblanco | 49df8d1 | 2016-10-07 08:36:56 -0700 | [diff] [blame] | 289 | // A line equation in implicit form. fA * x + fB * y + fC = 0, for all points (x, y) on the line. |
| 290 | struct Line { |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 291 | Line(double a, double b, double c) : fA(a), fB(b), fC(c) {} |
senorblanco | 49df8d1 | 2016-10-07 08:36:56 -0700 | [diff] [blame] | 292 | Line(Vertex* p, Vertex* q) : Line(p->fPoint, q->fPoint) {} |
| 293 | Line(const SkPoint& p, const SkPoint& q) |
| 294 | : fA(static_cast<double>(q.fY) - p.fY) // a = dY |
| 295 | , fB(static_cast<double>(p.fX) - q.fX) // b = -dX |
| 296 | , fC(static_cast<double>(p.fY) * q.fX - // c = cross(q, p) |
| 297 | static_cast<double>(p.fX) * q.fY) {} |
| 298 | double dist(const SkPoint& p) const { |
| 299 | return fA * p.fX + fB * p.fY + fC; |
| 300 | } |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 301 | Line operator*(double v) const { |
| 302 | return Line(fA * v, fB * v, fC * v); |
| 303 | } |
senorblanco | 49df8d1 | 2016-10-07 08:36:56 -0700 | [diff] [blame] | 304 | double magSq() const { |
| 305 | return fA * fA + fB * fB; |
| 306 | } |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 307 | void normalize() { |
| 308 | double len = sqrt(this->magSq()); |
| 309 | if (len == 0.0) { |
| 310 | return; |
| 311 | } |
| 312 | double scale = 1.0f / len; |
| 313 | fA *= scale; |
| 314 | fB *= scale; |
| 315 | fC *= scale; |
| 316 | } |
| 317 | bool nearParallel(const Line& o) const { |
| 318 | return fabs(o.fA - fA) < 0.00001 && fabs(o.fB - fB) < 0.00001; |
| 319 | } |
senorblanco | 49df8d1 | 2016-10-07 08:36:56 -0700 | [diff] [blame] | 320 | |
| 321 | // Compute the intersection of two (infinite) Lines. |
Stephen White | 95152e1 | 2017-12-18 10:52:44 -0500 | [diff] [blame] | 322 | bool intersect(const Line& other, SkPoint* point) const { |
senorblanco | 49df8d1 | 2016-10-07 08:36:56 -0700 | [diff] [blame] | 323 | double denom = fA * other.fB - fB * other.fA; |
| 324 | if (denom == 0.0) { |
| 325 | return false; |
| 326 | } |
Stephen White | 94b7e54 | 2018-01-04 14:01:10 -0500 | [diff] [blame] | 327 | double scale = 1.0 / denom; |
| 328 | point->fX = double_to_clamped_scalar((fB * other.fC - other.fB * fC) * scale); |
| 329 | point->fY = double_to_clamped_scalar((other.fA * fC - fA * other.fC) * scale); |
Stephen White | b56dedf | 2017-03-02 10:35:56 -0500 | [diff] [blame] | 330 | round(point); |
senorblanco | 49df8d1 | 2016-10-07 08:36:56 -0700 | [diff] [blame] | 331 | return true; |
| 332 | } |
| 333 | double fA, fB, fC; |
| 334 | }; |
| 335 | |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 336 | /** |
| 337 | * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and |
| 338 | * "edge below" a vertex as well as for the active edge list is handled by isLeftOf()/isRightOf(). |
| 339 | * Note that an Edge will give occasionally dist() != 0 for its own endpoints (because floating |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 340 | * point). For speed, that case is only tested by the callers that require it (e.g., |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 341 | * rewind_if_necessary()). Edges also handle checking for intersection with other edges. |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 342 | * Currently, this converts the edges to the parametric form, in order to avoid doing a division |
| 343 | * until an intersection has been confirmed. This is slightly slower in the "found" case, but |
| 344 | * a lot faster in the "not found" case. |
| 345 | * |
| 346 | * The coefficients of the line equation stored in double precision to avoid catastrphic |
| 347 | * cancellation in the isLeftOf() and isRightOf() checks. Using doubles ensures that the result is |
| 348 | * correct in float, since it's a polynomial of degree 2. The intersect() function, being |
| 349 | * degree 5, is still subject to catastrophic cancellation. We deal with that by assuming its |
| 350 | * output may be incorrect, and adjusting the mesh topology to match (see comment at the top of |
| 351 | * this file). |
| 352 | */ |
| 353 | |
| 354 | struct Edge { |
Stephen White | 2f4686f | 2017-01-03 16:20:01 -0500 | [diff] [blame] | 355 | enum class Type { kInner, kOuter, kConnector }; |
| 356 | Edge(Vertex* top, Vertex* bottom, int winding, Type type) |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 357 | : fWinding(winding) |
| 358 | , fTop(top) |
| 359 | , fBottom(bottom) |
Stephen White | 2f4686f | 2017-01-03 16:20:01 -0500 | [diff] [blame] | 360 | , fType(type) |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 361 | , fLeft(nullptr) |
| 362 | , fRight(nullptr) |
| 363 | , fPrevEdgeAbove(nullptr) |
| 364 | , fNextEdgeAbove(nullptr) |
| 365 | , fPrevEdgeBelow(nullptr) |
| 366 | , fNextEdgeBelow(nullptr) |
| 367 | , fLeftPoly(nullptr) |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 368 | , fRightPoly(nullptr) |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 369 | , fEvent(nullptr) |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 370 | , fLeftPolyPrev(nullptr) |
| 371 | , fLeftPolyNext(nullptr) |
| 372 | , fRightPolyPrev(nullptr) |
senorblanco | 70f5251 | 2016-08-17 14:56:22 -0700 | [diff] [blame] | 373 | , fRightPolyNext(nullptr) |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 374 | , fOverlap(false) |
senorblanco | 70f5251 | 2016-08-17 14:56:22 -0700 | [diff] [blame] | 375 | , fUsedInLeftPoly(false) |
senorblanco | 49df8d1 | 2016-10-07 08:36:56 -0700 | [diff] [blame] | 376 | , fUsedInRightPoly(false) |
| 377 | , fLine(top, bottom) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 378 | } |
| 379 | int fWinding; // 1 == edge goes downward; -1 = edge goes upward. |
| 380 | Vertex* fTop; // The top vertex in vertex-sort-order (sweep_lt). |
| 381 | Vertex* fBottom; // The bottom vertex in vertex-sort-order. |
Stephen White | 2f4686f | 2017-01-03 16:20:01 -0500 | [diff] [blame] | 382 | Type fType; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 383 | Edge* fLeft; // The linked list of edges in the active edge list. |
| 384 | Edge* fRight; // " |
| 385 | Edge* fPrevEdgeAbove; // The linked list of edges in the bottom Vertex's "edges above". |
| 386 | Edge* fNextEdgeAbove; // " |
| 387 | Edge* fPrevEdgeBelow; // The linked list of edges in the top Vertex's "edges below". |
| 388 | Edge* fNextEdgeBelow; // " |
| 389 | Poly* fLeftPoly; // The Poly to the left of this edge, if any. |
| 390 | Poly* fRightPoly; // The Poly to the right of this edge, if any. |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 391 | Event* fEvent; |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 392 | Edge* fLeftPolyPrev; |
| 393 | Edge* fLeftPolyNext; |
| 394 | Edge* fRightPolyPrev; |
| 395 | Edge* fRightPolyNext; |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 396 | bool fOverlap; // True if there's an overlap region adjacent to this edge. |
senorblanco | 70f5251 | 2016-08-17 14:56:22 -0700 | [diff] [blame] | 397 | bool fUsedInLeftPoly; |
| 398 | bool fUsedInRightPoly; |
senorblanco | 49df8d1 | 2016-10-07 08:36:56 -0700 | [diff] [blame] | 399 | Line fLine; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 400 | double dist(const SkPoint& p) const { |
senorblanco | 49df8d1 | 2016-10-07 08:36:56 -0700 | [diff] [blame] | 401 | return fLine.dist(p); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 402 | } |
| 403 | bool isRightOf(Vertex* v) const { |
senorblanco | 49df8d1 | 2016-10-07 08:36:56 -0700 | [diff] [blame] | 404 | return fLine.dist(v->fPoint) < 0.0; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 405 | } |
| 406 | bool isLeftOf(Vertex* v) const { |
senorblanco | 49df8d1 | 2016-10-07 08:36:56 -0700 | [diff] [blame] | 407 | return fLine.dist(v->fPoint) > 0.0; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 408 | } |
| 409 | void recompute() { |
senorblanco | 49df8d1 | 2016-10-07 08:36:56 -0700 | [diff] [blame] | 410 | fLine = Line(fTop, fBottom); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 411 | } |
Stephen White | 95152e1 | 2017-12-18 10:52:44 -0500 | [diff] [blame] | 412 | bool intersect(const Edge& other, SkPoint* p, uint8_t* alpha = nullptr) const { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 413 | LOG("intersecting %g -> %g with %g -> %g\n", |
| 414 | fTop->fID, fBottom->fID, |
| 415 | other.fTop->fID, other.fBottom->fID); |
| 416 | if (fTop == other.fTop || fBottom == other.fBottom) { |
| 417 | return false; |
| 418 | } |
senorblanco | 49df8d1 | 2016-10-07 08:36:56 -0700 | [diff] [blame] | 419 | double denom = fLine.fA * other.fLine.fB - fLine.fB * other.fLine.fA; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 420 | if (denom == 0.0) { |
| 421 | return false; |
| 422 | } |
Stephen White | 8a0bfc5 | 2017-02-21 15:24:13 -0500 | [diff] [blame] | 423 | double dx = static_cast<double>(other.fTop->fPoint.fX) - fTop->fPoint.fX; |
| 424 | double dy = static_cast<double>(other.fTop->fPoint.fY) - fTop->fPoint.fY; |
| 425 | double sNumer = dy * other.fLine.fB + dx * other.fLine.fA; |
| 426 | double tNumer = dy * fLine.fB + dx * fLine.fA; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 427 | // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early. |
| 428 | // This saves us doing the divide below unless absolutely necessary. |
| 429 | if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom) |
| 430 | : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) { |
| 431 | return false; |
| 432 | } |
| 433 | double s = sNumer / denom; |
| 434 | SkASSERT(s >= 0.0 && s <= 1.0); |
senorblanco | 49df8d1 | 2016-10-07 08:36:56 -0700 | [diff] [blame] | 435 | p->fX = SkDoubleToScalar(fTop->fPoint.fX - s * fLine.fB); |
| 436 | p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fLine.fA); |
Stephen White | 56158ae | 2017-01-30 14:31:31 -0500 | [diff] [blame] | 437 | if (alpha) { |
Stephen White | 92eba8a | 2017-02-06 09:50:27 -0500 | [diff] [blame] | 438 | if (fType == Type::kConnector) { |
| 439 | *alpha = (1.0 - s) * fTop->fAlpha + s * fBottom->fAlpha; |
| 440 | } else if (other.fType == Type::kConnector) { |
| 441 | double t = tNumer / denom; |
| 442 | *alpha = (1.0 - t) * other.fTop->fAlpha + t * other.fBottom->fAlpha; |
Stephen White | 56158ae | 2017-01-30 14:31:31 -0500 | [diff] [blame] | 443 | } else if (fType == Type::kOuter && other.fType == Type::kOuter) { |
| 444 | *alpha = 0; |
| 445 | } else { |
Stephen White | 92eba8a | 2017-02-06 09:50:27 -0500 | [diff] [blame] | 446 | *alpha = 255; |
Stephen White | 56158ae | 2017-01-30 14:31:31 -0500 | [diff] [blame] | 447 | } |
| 448 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 449 | return true; |
| 450 | } |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 451 | }; |
| 452 | |
| 453 | struct EdgeList { |
Stephen White | 5ad721e | 2017-02-23 16:50:47 -0500 | [diff] [blame] | 454 | EdgeList() : fHead(nullptr), fTail(nullptr) {} |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 455 | Edge* fHead; |
| 456 | Edge* fTail; |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 457 | void insert(Edge* edge, Edge* prev, Edge* next) { |
| 458 | list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &fHead, &fTail); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 459 | } |
| 460 | void append(Edge* e) { |
| 461 | insert(e, fTail, nullptr); |
| 462 | } |
| 463 | void remove(Edge* edge) { |
| 464 | list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &fHead, &fTail); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 465 | } |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 466 | void removeAll() { |
| 467 | while (fHead) { |
| 468 | this->remove(fHead); |
| 469 | } |
| 470 | } |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 471 | void close() { |
| 472 | if (fHead && fTail) { |
| 473 | fTail->fRight = fHead; |
| 474 | fHead->fLeft = fTail; |
| 475 | } |
| 476 | } |
| 477 | bool contains(Edge* edge) const { |
| 478 | return edge->fLeft || edge->fRight || fHead == edge; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 479 | } |
| 480 | }; |
| 481 | |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 482 | struct Event { |
Stephen White | 77169c8 | 2018-06-05 09:15:59 -0400 | [diff] [blame] | 483 | Event(Edge* edge, bool isOuterBoundary, const SkPoint& point, uint8_t alpha) |
| 484 | : fEdge(edge), fIsOuterBoundary(isOuterBoundary), fPoint(point), fAlpha(alpha) |
| 485 | , fPrev(nullptr), fNext(nullptr) { |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 486 | } |
| 487 | Edge* fEdge; |
Stephen White | 77169c8 | 2018-06-05 09:15:59 -0400 | [diff] [blame] | 488 | bool fIsOuterBoundary; |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 489 | SkPoint fPoint; |
| 490 | uint8_t fAlpha; |
| 491 | Event* fPrev; |
| 492 | Event* fNext; |
| 493 | void apply(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc); |
| 494 | }; |
| 495 | |
| 496 | bool compare(Event* const& e1, Event* const& e2) { |
| 497 | return e1->fAlpha > e2->fAlpha; |
| 498 | } |
| 499 | |
| 500 | struct EventList : public SkTDPQueue<Event*, &compare> {}; |
| 501 | |
Stephen White | 77169c8 | 2018-06-05 09:15:59 -0400 | [diff] [blame] | 502 | void create_event(Edge* e, bool isOuterBoundary, EventList* events, SkArenaAlloc& alloc) { |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 503 | Edge bisector1(e->fTop, e->fTop->fPartner, 1, Edge::Type::kConnector); |
| 504 | Edge bisector2(e->fBottom, e->fBottom->fPartner, 1, Edge::Type::kConnector); |
| 505 | SkPoint p; |
| 506 | uint8_t alpha; |
| 507 | if (bisector1.intersect(bisector2, &p, &alpha)) { |
| 508 | LOG("found overlap edge %g -> %g, will collapse to %g,%g alpha %d\n", |
| 509 | e->fTop->fID, e->fBottom->fID, p.fX, p.fY, alpha); |
Stephen White | 77169c8 | 2018-06-05 09:15:59 -0400 | [diff] [blame] | 510 | e->fEvent = alloc.make<Event>(e, isOuterBoundary, p, alpha); |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 511 | events->insert(e->fEvent); |
| 512 | } |
| 513 | } |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 514 | |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 515 | /***************************************************************************************/ |
| 516 | |
| 517 | struct Poly { |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 518 | Poly(Vertex* v, int winding) |
| 519 | : fFirstVertex(v) |
| 520 | , fWinding(winding) |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 521 | , fHead(nullptr) |
| 522 | , fTail(nullptr) |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 523 | , fNext(nullptr) |
| 524 | , fPartner(nullptr) |
| 525 | , fCount(0) |
| 526 | { |
| 527 | #if LOGGING_ENABLED |
| 528 | static int gID = 0; |
| 529 | fID = gID++; |
| 530 | LOG("*** created Poly %d\n", fID); |
| 531 | #endif |
| 532 | } |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 533 | typedef enum { kLeft_Side, kRight_Side } Side; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 534 | struct MonotonePoly { |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 535 | MonotonePoly(Edge* edge, Side side) |
| 536 | : fSide(side) |
| 537 | , fFirstEdge(nullptr) |
| 538 | , fLastEdge(nullptr) |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 539 | , fPrev(nullptr) |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 540 | , fNext(nullptr) { |
| 541 | this->addEdge(edge); |
| 542 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 543 | Side fSide; |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 544 | Edge* fFirstEdge; |
| 545 | Edge* fLastEdge; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 546 | MonotonePoly* fPrev; |
| 547 | MonotonePoly* fNext; |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 548 | void addEdge(Edge* edge) { |
senorblanco | e6eaa32 | 2016-03-08 09:06:44 -0800 | [diff] [blame] | 549 | if (fSide == kRight_Side) { |
senorblanco | 212c7c3 | 2016-08-18 10:20:47 -0700 | [diff] [blame] | 550 | SkASSERT(!edge->fUsedInRightPoly); |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 551 | list_insert<Edge, &Edge::fRightPolyPrev, &Edge::fRightPolyNext>( |
| 552 | edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge); |
senorblanco | 70f5251 | 2016-08-17 14:56:22 -0700 | [diff] [blame] | 553 | edge->fUsedInRightPoly = true; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 554 | } else { |
senorblanco | 212c7c3 | 2016-08-18 10:20:47 -0700 | [diff] [blame] | 555 | SkASSERT(!edge->fUsedInLeftPoly); |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 556 | list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>( |
| 557 | edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge); |
senorblanco | 70f5251 | 2016-08-17 14:56:22 -0700 | [diff] [blame] | 558 | edge->fUsedInLeftPoly = true; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 559 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 560 | } |
| 561 | |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 562 | void* emit(const AAParams* aaParams, void* data) { |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 563 | Edge* e = fFirstEdge; |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 564 | VertexList vertices; |
| 565 | vertices.append(e->fTop); |
Stephen White | 651cbe9 | 2017-03-03 12:24:16 -0500 | [diff] [blame] | 566 | int count = 1; |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 567 | while (e != nullptr) { |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 568 | if (kRight_Side == fSide) { |
| 569 | vertices.append(e->fBottom); |
| 570 | e = e->fRightPolyNext; |
| 571 | } else { |
| 572 | vertices.prepend(e->fBottom); |
| 573 | e = e->fLeftPolyNext; |
| 574 | } |
Stephen White | 651cbe9 | 2017-03-03 12:24:16 -0500 | [diff] [blame] | 575 | count++; |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 576 | } |
| 577 | Vertex* first = vertices.fHead; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 578 | Vertex* v = first->fNext; |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 579 | while (v != vertices.fTail) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 580 | SkASSERT(v && v->fPrev && v->fNext); |
| 581 | Vertex* prev = v->fPrev; |
| 582 | Vertex* curr = v; |
| 583 | Vertex* next = v->fNext; |
Stephen White | 651cbe9 | 2017-03-03 12:24:16 -0500 | [diff] [blame] | 584 | if (count == 3) { |
| 585 | return emit_triangle(prev, curr, next, aaParams, data); |
| 586 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 587 | double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX; |
| 588 | double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY; |
| 589 | double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX; |
| 590 | double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY; |
| 591 | if (ax * by - ay * bx >= 0.0) { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 592 | data = emit_triangle(prev, curr, next, aaParams, data); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 593 | v->fPrev->fNext = v->fNext; |
| 594 | v->fNext->fPrev = v->fPrev; |
Stephen White | 651cbe9 | 2017-03-03 12:24:16 -0500 | [diff] [blame] | 595 | count--; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 596 | if (v->fPrev == first) { |
| 597 | v = v->fNext; |
| 598 | } else { |
| 599 | v = v->fPrev; |
| 600 | } |
| 601 | } else { |
| 602 | v = v->fNext; |
| 603 | } |
| 604 | } |
| 605 | return data; |
| 606 | } |
| 607 | }; |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 608 | Poly* addEdge(Edge* e, Side side, SkArenaAlloc& alloc) { |
senorblanco | 70f5251 | 2016-08-17 14:56:22 -0700 | [diff] [blame] | 609 | LOG("addEdge (%g -> %g) to poly %d, %s side\n", |
| 610 | e->fTop->fID, e->fBottom->fID, fID, side == kLeft_Side ? "left" : "right"); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 611 | Poly* partner = fPartner; |
| 612 | Poly* poly = this; |
senorblanco | 212c7c3 | 2016-08-18 10:20:47 -0700 | [diff] [blame] | 613 | if (side == kRight_Side) { |
| 614 | if (e->fUsedInRightPoly) { |
| 615 | return this; |
| 616 | } |
| 617 | } else { |
| 618 | if (e->fUsedInLeftPoly) { |
| 619 | return this; |
| 620 | } |
| 621 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 622 | if (partner) { |
| 623 | fPartner = partner->fPartner = nullptr; |
| 624 | } |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 625 | if (!fTail) { |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 626 | fHead = fTail = alloc.make<MonotonePoly>(e, side); |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 627 | fCount += 2; |
senorblanco | 93e3fff | 2016-06-07 12:36:00 -0700 | [diff] [blame] | 628 | } else if (e->fBottom == fTail->fLastEdge->fBottom) { |
| 629 | return poly; |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 630 | } else if (side == fTail->fSide) { |
| 631 | fTail->addEdge(e); |
| 632 | fCount++; |
| 633 | } else { |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 634 | e = alloc.make<Edge>(fTail->fLastEdge->fBottom, e->fBottom, 1, Edge::Type::kInner); |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 635 | fTail->addEdge(e); |
| 636 | fCount++; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 637 | if (partner) { |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 638 | partner->addEdge(e, side, alloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 639 | poly = partner; |
| 640 | } else { |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 641 | MonotonePoly* m = alloc.make<MonotonePoly>(e, side); |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 642 | m->fPrev = fTail; |
| 643 | fTail->fNext = m; |
| 644 | fTail = m; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 645 | } |
| 646 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 647 | return poly; |
| 648 | } |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 649 | void* emit(const AAParams* aaParams, void *data) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 650 | if (fCount < 3) { |
| 651 | return data; |
| 652 | } |
| 653 | LOG("emit() %d, size %d\n", fID, fCount); |
| 654 | for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 655 | data = m->emit(aaParams, data); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 656 | } |
| 657 | return data; |
| 658 | } |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 659 | Vertex* lastVertex() const { return fTail ? fTail->fLastEdge->fBottom : fFirstVertex; } |
| 660 | Vertex* fFirstVertex; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 661 | int fWinding; |
| 662 | MonotonePoly* fHead; |
| 663 | MonotonePoly* fTail; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 664 | Poly* fNext; |
| 665 | Poly* fPartner; |
| 666 | int fCount; |
| 667 | #if LOGGING_ENABLED |
| 668 | int fID; |
| 669 | #endif |
| 670 | }; |
| 671 | |
| 672 | /***************************************************************************************/ |
| 673 | |
| 674 | bool coincident(const SkPoint& a, const SkPoint& b) { |
| 675 | return a == b; |
| 676 | } |
| 677 | |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 678 | Poly* new_poly(Poly** head, Vertex* v, int winding, SkArenaAlloc& alloc) { |
| 679 | Poly* poly = alloc.make<Poly>(v, winding); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 680 | poly->fNext = *head; |
| 681 | *head = poly; |
| 682 | return poly; |
| 683 | } |
| 684 | |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 685 | void append_point_to_contour(const SkPoint& p, VertexList* contour, SkArenaAlloc& alloc) { |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 686 | Vertex* v = alloc.make<Vertex>(p, 255); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 687 | #if LOGGING_ENABLED |
| 688 | static float gID = 0.0f; |
| 689 | v->fID = gID++; |
| 690 | #endif |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 691 | contour->append(v); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 692 | } |
| 693 | |
Stephen White | 36e4f06 | 2017-03-27 16:11:31 -0400 | [diff] [blame] | 694 | SkScalar quad_error_at(const SkPoint pts[3], SkScalar t, SkScalar u) { |
| 695 | SkQuadCoeff quad(pts); |
| 696 | SkPoint p0 = to_point(quad.eval(t - 0.5f * u)); |
| 697 | SkPoint mid = to_point(quad.eval(t)); |
| 698 | SkPoint p1 = to_point(quad.eval(t + 0.5f * u)); |
Stephen White | e3a0be7 | 2017-06-12 11:43:18 -0400 | [diff] [blame] | 699 | if (!p0.isFinite() || !mid.isFinite() || !p1.isFinite()) { |
| 700 | return 0; |
| 701 | } |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 702 | return SkPointPriv::DistanceToLineSegmentBetweenSqd(mid, p0, p1); |
Stephen White | 36e4f06 | 2017-03-27 16:11:31 -0400 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | void append_quadratic_to_contour(const SkPoint pts[3], SkScalar toleranceSqd, VertexList* contour, |
| 706 | SkArenaAlloc& alloc) { |
| 707 | SkQuadCoeff quad(pts); |
| 708 | Sk2s aa = quad.fA * quad.fA; |
| 709 | SkScalar denom = 2.0f * (aa[0] + aa[1]); |
| 710 | Sk2s ab = quad.fA * quad.fB; |
| 711 | SkScalar t = denom ? (-ab[0] - ab[1]) / denom : 0.0f; |
| 712 | int nPoints = 1; |
Stephen White | e40c361 | 2018-01-09 11:49:08 -0500 | [diff] [blame] | 713 | SkScalar u = 1.0f; |
Stephen White | 36e4f06 | 2017-03-27 16:11:31 -0400 | [diff] [blame] | 714 | // Test possible subdivision values only at the point of maximum curvature. |
| 715 | // If it passes the flatness metric there, it'll pass everywhere. |
Stephen White | e40c361 | 2018-01-09 11:49:08 -0500 | [diff] [blame] | 716 | while (nPoints < GrPathUtils::kMaxPointsPerCurve) { |
Stephen White | 36e4f06 | 2017-03-27 16:11:31 -0400 | [diff] [blame] | 717 | u = 1.0f / nPoints; |
| 718 | if (quad_error_at(pts, t, u) < toleranceSqd) { |
| 719 | break; |
| 720 | } |
| 721 | nPoints++; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 722 | } |
Stephen White | 36e4f06 | 2017-03-27 16:11:31 -0400 | [diff] [blame] | 723 | for (int j = 1; j <= nPoints; j++) { |
| 724 | append_point_to_contour(to_point(quad.eval(j * u)), contour, alloc); |
| 725 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 726 | } |
| 727 | |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 728 | void generate_cubic_points(const SkPoint& p0, |
| 729 | const SkPoint& p1, |
| 730 | const SkPoint& p2, |
| 731 | const SkPoint& p3, |
| 732 | SkScalar tolSqd, |
| 733 | VertexList* contour, |
| 734 | int pointsLeft, |
| 735 | SkArenaAlloc& alloc) { |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 736 | SkScalar d1 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p1, p0, p3); |
| 737 | SkScalar d2 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p2, p0, p3); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 738 | if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) || |
| 739 | !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) { |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 740 | append_point_to_contour(p3, contour, alloc); |
| 741 | return; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 742 | } |
| 743 | const SkPoint q[] = { |
| 744 | { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) }, |
| 745 | { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) }, |
| 746 | { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) } |
| 747 | }; |
| 748 | const SkPoint r[] = { |
| 749 | { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) }, |
| 750 | { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) } |
| 751 | }; |
| 752 | const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) }; |
| 753 | pointsLeft >>= 1; |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 754 | generate_cubic_points(p0, q[0], r[0], s, tolSqd, contour, pointsLeft, alloc); |
| 755 | generate_cubic_points(s, r[1], q[2], p3, tolSqd, contour, pointsLeft, alloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | // Stage 1: convert the input path to a set of linear contours (linked list of Vertices). |
| 759 | |
| 760 | void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds, |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 761 | VertexList* contours, SkArenaAlloc& alloc, bool *isLinear) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 762 | SkScalar toleranceSqd = tolerance * tolerance; |
| 763 | |
| 764 | SkPoint pts[4]; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 765 | *isLinear = true; |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 766 | VertexList* contour = contours; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 767 | SkPath::Iter iter(path, false); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 768 | if (path.isInverseFillType()) { |
| 769 | SkPoint quad[4]; |
| 770 | clipBounds.toQuad(quad); |
senorblanco | 7ab96e9 | 2016-10-12 06:47:44 -0700 | [diff] [blame] | 771 | for (int i = 3; i >= 0; i--) { |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 772 | append_point_to_contour(quad[i], contours, alloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 773 | } |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 774 | contour++; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 775 | } |
| 776 | SkAutoConicToQuads converter; |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 777 | SkPath::Verb verb; |
Stephen White | 2cee283 | 2017-08-23 09:37:16 -0400 | [diff] [blame] | 778 | while ((verb = iter.next(pts, false)) != SkPath::kDone_Verb) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 779 | switch (verb) { |
| 780 | case SkPath::kConic_Verb: { |
| 781 | SkScalar weight = iter.conicWeight(); |
| 782 | const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd); |
| 783 | for (int i = 0; i < converter.countQuads(); ++i) { |
Stephen White | 36e4f06 | 2017-03-27 16:11:31 -0400 | [diff] [blame] | 784 | append_quadratic_to_contour(quadPts, toleranceSqd, contour, alloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 785 | quadPts += 2; |
| 786 | } |
| 787 | *isLinear = false; |
| 788 | break; |
| 789 | } |
| 790 | case SkPath::kMove_Verb: |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 791 | if (contour->fHead) { |
| 792 | contour++; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 793 | } |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 794 | append_point_to_contour(pts[0], contour, alloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 795 | break; |
| 796 | case SkPath::kLine_Verb: { |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 797 | append_point_to_contour(pts[1], contour, alloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 798 | break; |
| 799 | } |
| 800 | case SkPath::kQuad_Verb: { |
Stephen White | 36e4f06 | 2017-03-27 16:11:31 -0400 | [diff] [blame] | 801 | append_quadratic_to_contour(pts, toleranceSqd, contour, alloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 802 | *isLinear = false; |
| 803 | break; |
| 804 | } |
| 805 | case SkPath::kCubic_Verb: { |
| 806 | int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance); |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 807 | generate_cubic_points(pts[0], pts[1], pts[2], pts[3], toleranceSqd, contour, |
| 808 | pointsLeft, alloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 809 | *isLinear = false; |
| 810 | break; |
| 811 | } |
| 812 | case SkPath::kClose_Verb: |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 813 | case SkPath::kDone_Verb: |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 814 | break; |
| 815 | } |
| 816 | } |
| 817 | } |
| 818 | |
Stephen White | 4978906 | 2017-02-21 10:35:49 -0500 | [diff] [blame] | 819 | inline bool apply_fill_type(SkPath::FillType fillType, int winding) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 820 | switch (fillType) { |
| 821 | case SkPath::kWinding_FillType: |
| 822 | return winding != 0; |
| 823 | case SkPath::kEvenOdd_FillType: |
| 824 | return (winding & 1) != 0; |
| 825 | case SkPath::kInverseWinding_FillType: |
senorblanco | 7ab96e9 | 2016-10-12 06:47:44 -0700 | [diff] [blame] | 826 | return winding == 1; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 827 | case SkPath::kInverseEvenOdd_FillType: |
| 828 | return (winding & 1) == 1; |
| 829 | default: |
| 830 | SkASSERT(false); |
| 831 | return false; |
| 832 | } |
| 833 | } |
| 834 | |
Stephen White | 4978906 | 2017-02-21 10:35:49 -0500 | [diff] [blame] | 835 | inline bool apply_fill_type(SkPath::FillType fillType, Poly* poly) { |
| 836 | return poly && apply_fill_type(fillType, poly->fWinding); |
| 837 | } |
| 838 | |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 839 | Edge* new_edge(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc) { |
Stephen White | 2f4686f | 2017-01-03 16:20:01 -0500 | [diff] [blame] | 840 | int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 841 | Vertex* top = winding < 0 ? next : prev; |
| 842 | Vertex* bottom = winding < 0 ? prev : next; |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 843 | return alloc.make<Edge>(top, bottom, winding, type); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | void remove_edge(Edge* edge, EdgeList* edges) { |
| 847 | LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 848 | SkASSERT(edges->contains(edge)); |
| 849 | edges->remove(edge); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | void insert_edge(Edge* edge, Edge* prev, EdgeList* edges) { |
| 853 | LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 854 | SkASSERT(!edges->contains(edge)); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 855 | Edge* next = prev ? prev->fRight : edges->fHead; |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 856 | edges->insert(edge, prev, next); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | void find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) { |
Stephen White | 90732fd | 2017-03-02 16:16:33 -0500 | [diff] [blame] | 860 | if (v->fFirstEdgeAbove && v->fLastEdgeAbove) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 861 | *left = v->fFirstEdgeAbove->fLeft; |
| 862 | *right = v->fLastEdgeAbove->fRight; |
| 863 | return; |
| 864 | } |
| 865 | Edge* next = nullptr; |
| 866 | Edge* prev; |
| 867 | for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) { |
| 868 | if (prev->isLeftOf(v)) { |
| 869 | break; |
| 870 | } |
| 871 | next = prev; |
| 872 | } |
| 873 | *left = prev; |
| 874 | *right = next; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 875 | } |
| 876 | |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 877 | void insert_edge_above(Edge* edge, Vertex* v, Comparator& c) { |
| 878 | if (edge->fTop->fPoint == edge->fBottom->fPoint || |
Stephen White | e30cf80 | 2017-02-27 11:37:55 -0500 | [diff] [blame] | 879 | c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 880 | return; |
| 881 | } |
| 882 | LOG("insert edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID); |
| 883 | Edge* prev = nullptr; |
| 884 | Edge* next; |
| 885 | for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) { |
| 886 | if (next->isRightOf(edge->fTop)) { |
| 887 | break; |
| 888 | } |
| 889 | prev = next; |
| 890 | } |
senorblanco | e6eaa32 | 2016-03-08 09:06:44 -0800 | [diff] [blame] | 891 | list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>( |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 892 | edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove); |
| 893 | } |
| 894 | |
| 895 | void insert_edge_below(Edge* edge, Vertex* v, Comparator& c) { |
| 896 | if (edge->fTop->fPoint == edge->fBottom->fPoint || |
Stephen White | e30cf80 | 2017-02-27 11:37:55 -0500 | [diff] [blame] | 897 | c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 898 | return; |
| 899 | } |
| 900 | LOG("insert edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID); |
| 901 | Edge* prev = nullptr; |
| 902 | Edge* next; |
| 903 | for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) { |
| 904 | if (next->isRightOf(edge->fBottom)) { |
| 905 | break; |
| 906 | } |
| 907 | prev = next; |
| 908 | } |
senorblanco | e6eaa32 | 2016-03-08 09:06:44 -0800 | [diff] [blame] | 909 | list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>( |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 910 | edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow); |
| 911 | } |
| 912 | |
| 913 | void remove_edge_above(Edge* edge) { |
Stephen White | 7b37694 | 2018-05-22 11:51:32 -0400 | [diff] [blame] | 914 | SkASSERT(edge->fTop && edge->fBottom); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 915 | LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID, |
| 916 | edge->fBottom->fID); |
senorblanco | e6eaa32 | 2016-03-08 09:06:44 -0800 | [diff] [blame] | 917 | list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>( |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 918 | edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove); |
| 919 | } |
| 920 | |
| 921 | void remove_edge_below(Edge* edge) { |
Stephen White | 7b37694 | 2018-05-22 11:51:32 -0400 | [diff] [blame] | 922 | SkASSERT(edge->fTop && edge->fBottom); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 923 | LOG("removing edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID, |
| 924 | edge->fTop->fID); |
senorblanco | e6eaa32 | 2016-03-08 09:06:44 -0800 | [diff] [blame] | 925 | list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>( |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 926 | edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow); |
| 927 | } |
| 928 | |
Stephen White | e7a364d | 2017-01-11 16:19:26 -0500 | [diff] [blame] | 929 | void disconnect(Edge* edge) |
| 930 | { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 931 | remove_edge_above(edge); |
| 932 | remove_edge_below(edge); |
Stephen White | e7a364d | 2017-01-11 16:19:26 -0500 | [diff] [blame] | 933 | } |
| 934 | |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 935 | void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c); |
| 936 | |
| 937 | void rewind(EdgeList* activeEdges, Vertex** current, Vertex* dst, Comparator& c) { |
| 938 | if (!current || *current == dst || c.sweep_lt((*current)->fPoint, dst->fPoint)) { |
| 939 | return; |
| 940 | } |
| 941 | Vertex* v = *current; |
| 942 | LOG("rewinding active edges from vertex %g to vertex %g\n", v->fID, dst->fID); |
| 943 | while (v != dst) { |
| 944 | v = v->fPrev; |
| 945 | for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { |
| 946 | remove_edge(e, activeEdges); |
| 947 | } |
| 948 | Edge* leftEdge = v->fLeftEnclosingEdge; |
| 949 | for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) { |
| 950 | insert_edge(e, leftEdge, activeEdges); |
| 951 | leftEdge = e; |
| 952 | } |
| 953 | } |
| 954 | *current = v; |
| 955 | } |
| 956 | |
| 957 | void rewind_if_necessary(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c) { |
| 958 | if (!activeEdges || !current) { |
| 959 | return; |
| 960 | } |
| 961 | Vertex* top = edge->fTop; |
| 962 | Vertex* bottom = edge->fBottom; |
| 963 | if (edge->fLeft) { |
| 964 | Vertex* leftTop = edge->fLeft->fTop; |
| 965 | Vertex* leftBottom = edge->fLeft->fBottom; |
| 966 | if (c.sweep_lt(leftTop->fPoint, top->fPoint) && !edge->fLeft->isLeftOf(top)) { |
| 967 | rewind(activeEdges, current, leftTop, c); |
| 968 | } else if (c.sweep_lt(top->fPoint, leftTop->fPoint) && !edge->isRightOf(leftTop)) { |
| 969 | rewind(activeEdges, current, top, c); |
| 970 | } else if (c.sweep_lt(bottom->fPoint, leftBottom->fPoint) && |
| 971 | !edge->fLeft->isLeftOf(bottom)) { |
| 972 | rewind(activeEdges, current, leftTop, c); |
| 973 | } else if (c.sweep_lt(leftBottom->fPoint, bottom->fPoint) && !edge->isRightOf(leftBottom)) { |
| 974 | rewind(activeEdges, current, top, c); |
| 975 | } |
| 976 | } |
| 977 | if (edge->fRight) { |
| 978 | Vertex* rightTop = edge->fRight->fTop; |
| 979 | Vertex* rightBottom = edge->fRight->fBottom; |
| 980 | if (c.sweep_lt(rightTop->fPoint, top->fPoint) && !edge->fRight->isRightOf(top)) { |
| 981 | rewind(activeEdges, current, rightTop, c); |
| 982 | } else if (c.sweep_lt(top->fPoint, rightTop->fPoint) && !edge->isLeftOf(rightTop)) { |
| 983 | rewind(activeEdges, current, top, c); |
| 984 | } else if (c.sweep_lt(bottom->fPoint, rightBottom->fPoint) && |
| 985 | !edge->fRight->isRightOf(bottom)) { |
| 986 | rewind(activeEdges, current, rightTop, c); |
| 987 | } else if (c.sweep_lt(rightBottom->fPoint, bottom->fPoint) && |
| 988 | !edge->isLeftOf(rightBottom)) { |
| 989 | rewind(activeEdges, current, top, c); |
| 990 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 991 | } |
| 992 | } |
| 993 | |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 994 | void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 995 | remove_edge_below(edge); |
| 996 | edge->fTop = v; |
| 997 | edge->recompute(); |
| 998 | insert_edge_below(edge, v, c); |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 999 | rewind_if_necessary(edge, activeEdges, current, c); |
| 1000 | merge_collinear_edges(edge, activeEdges, current, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1001 | } |
| 1002 | |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1003 | void set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1004 | remove_edge_above(edge); |
| 1005 | edge->fBottom = v; |
| 1006 | edge->recompute(); |
| 1007 | insert_edge_above(edge, v, c); |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1008 | rewind_if_necessary(edge, activeEdges, current, c); |
| 1009 | merge_collinear_edges(edge, activeEdges, current, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1010 | } |
| 1011 | |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1012 | void merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current, |
| 1013 | Comparator& c) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1014 | if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) { |
| 1015 | LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n", |
| 1016 | edge->fTop->fPoint.fX, edge->fTop->fPoint.fY, |
| 1017 | edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY); |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1018 | rewind(activeEdges, current, edge->fTop, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1019 | other->fWinding += edge->fWinding; |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1020 | disconnect(edge); |
Stephen White | ec79c39 | 2018-05-18 11:49:21 -0400 | [diff] [blame] | 1021 | edge->fTop = edge->fBottom = nullptr; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1022 | } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1023 | rewind(activeEdges, current, edge->fTop, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1024 | other->fWinding += edge->fWinding; |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1025 | set_bottom(edge, other->fTop, activeEdges, current, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1026 | } else { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1027 | rewind(activeEdges, current, other->fTop, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1028 | edge->fWinding += other->fWinding; |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1029 | set_bottom(other, edge->fTop, activeEdges, current, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1030 | } |
| 1031 | } |
| 1032 | |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1033 | void merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current, |
| 1034 | Comparator& c) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1035 | if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) { |
| 1036 | LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n", |
| 1037 | edge->fTop->fPoint.fX, edge->fTop->fPoint.fY, |
| 1038 | edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY); |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1039 | rewind(activeEdges, current, edge->fTop, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1040 | other->fWinding += edge->fWinding; |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1041 | disconnect(edge); |
Stephen White | ec79c39 | 2018-05-18 11:49:21 -0400 | [diff] [blame] | 1042 | edge->fTop = edge->fBottom = nullptr; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1043 | } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1044 | rewind(activeEdges, current, other->fTop, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1045 | edge->fWinding += other->fWinding; |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1046 | set_top(other, edge->fBottom, activeEdges, current, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1047 | } else { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1048 | rewind(activeEdges, current, edge->fTop, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1049 | other->fWinding += edge->fWinding; |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1050 | set_top(edge, other->fBottom, activeEdges, current, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1051 | } |
| 1052 | } |
| 1053 | |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1054 | void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c) { |
Stephen White | 6eca90f | 2017-05-25 14:47:11 -0400 | [diff] [blame] | 1055 | for (;;) { |
| 1056 | if (edge->fPrevEdgeAbove && (edge->fTop == edge->fPrevEdgeAbove->fTop || |
| 1057 | !edge->fPrevEdgeAbove->isLeftOf(edge->fTop))) { |
Stephen White | 24289e0 | 2018-06-29 17:02:21 -0400 | [diff] [blame] | 1058 | merge_edges_above(edge->fPrevEdgeAbove, edge, activeEdges, current, c); |
Stephen White | 6eca90f | 2017-05-25 14:47:11 -0400 | [diff] [blame] | 1059 | } else if (edge->fNextEdgeAbove && (edge->fTop == edge->fNextEdgeAbove->fTop || |
| 1060 | !edge->isLeftOf(edge->fNextEdgeAbove->fTop))) { |
Stephen White | 24289e0 | 2018-06-29 17:02:21 -0400 | [diff] [blame] | 1061 | merge_edges_above(edge->fNextEdgeAbove, edge, activeEdges, current, c); |
Stephen White | 6eca90f | 2017-05-25 14:47:11 -0400 | [diff] [blame] | 1062 | } else if (edge->fPrevEdgeBelow && (edge->fBottom == edge->fPrevEdgeBelow->fBottom || |
| 1063 | !edge->fPrevEdgeBelow->isLeftOf(edge->fBottom))) { |
Stephen White | 24289e0 | 2018-06-29 17:02:21 -0400 | [diff] [blame] | 1064 | merge_edges_below(edge->fPrevEdgeBelow, edge, activeEdges, current, c); |
Stephen White | 6eca90f | 2017-05-25 14:47:11 -0400 | [diff] [blame] | 1065 | } else if (edge->fNextEdgeBelow && (edge->fBottom == edge->fNextEdgeBelow->fBottom || |
| 1066 | !edge->isLeftOf(edge->fNextEdgeBelow->fBottom))) { |
Stephen White | 24289e0 | 2018-06-29 17:02:21 -0400 | [diff] [blame] | 1067 | merge_edges_below(edge->fNextEdgeBelow, edge, activeEdges, current, c); |
Stephen White | 6eca90f | 2017-05-25 14:47:11 -0400 | [diff] [blame] | 1068 | } else { |
| 1069 | break; |
| 1070 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1071 | } |
Stephen White | 6eca90f | 2017-05-25 14:47:11 -0400 | [diff] [blame] | 1072 | SkASSERT(!edge->fPrevEdgeAbove || edge->fPrevEdgeAbove->isLeftOf(edge->fTop)); |
| 1073 | SkASSERT(!edge->fPrevEdgeBelow || edge->fPrevEdgeBelow->isLeftOf(edge->fBottom)); |
| 1074 | SkASSERT(!edge->fNextEdgeAbove || edge->fNextEdgeAbove->isRightOf(edge->fTop)); |
| 1075 | SkASSERT(!edge->fNextEdgeBelow || edge->fNextEdgeBelow->isRightOf(edge->fBottom)); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1076 | } |
| 1077 | |
Stephen White | 89042d5 | 2018-06-08 12:18:22 -0400 | [diff] [blame] | 1078 | bool split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c, |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1079 | SkArenaAlloc& alloc) { |
Stephen White | ec79c39 | 2018-05-18 11:49:21 -0400 | [diff] [blame] | 1080 | if (!edge->fTop || !edge->fBottom || v == edge->fTop || v == edge->fBottom) { |
Stephen White | 89042d5 | 2018-06-08 12:18:22 -0400 | [diff] [blame] | 1081 | return false; |
Stephen White | 0cb3167 | 2017-06-08 14:41:01 -0400 | [diff] [blame] | 1082 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1083 | LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n", |
| 1084 | edge->fTop->fID, edge->fBottom->fID, |
| 1085 | v->fID, v->fPoint.fX, v->fPoint.fY); |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1086 | Vertex* top; |
| 1087 | Vertex* bottom; |
Stephen White | 531a48e | 2018-06-01 09:49:39 -0400 | [diff] [blame] | 1088 | int winding = edge->fWinding; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1089 | if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1090 | top = v; |
| 1091 | bottom = edge->fTop; |
| 1092 | set_top(edge, v, activeEdges, current, c); |
Stephen White | e30cf80 | 2017-02-27 11:37:55 -0500 | [diff] [blame] | 1093 | } else if (c.sweep_lt(edge->fBottom->fPoint, v->fPoint)) { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1094 | top = edge->fBottom; |
| 1095 | bottom = v; |
| 1096 | set_bottom(edge, v, activeEdges, current, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1097 | } else { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1098 | top = v; |
| 1099 | bottom = edge->fBottom; |
| 1100 | set_bottom(edge, v, activeEdges, current, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1101 | } |
Stephen White | 531a48e | 2018-06-01 09:49:39 -0400 | [diff] [blame] | 1102 | Edge* newEdge = alloc.make<Edge>(top, bottom, winding, edge->fType); |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1103 | insert_edge_below(newEdge, top, c); |
| 1104 | insert_edge_above(newEdge, bottom, c); |
| 1105 | merge_collinear_edges(newEdge, activeEdges, current, c); |
Stephen White | 89042d5 | 2018-06-08 12:18:22 -0400 | [diff] [blame] | 1106 | return true; |
| 1107 | } |
| 1108 | |
| 1109 | bool intersect_edge_pair(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current, Comparator& c, SkArenaAlloc& alloc) { |
| 1110 | if (!left->fTop || !left->fBottom || !right->fTop || !right->fBottom) { |
| 1111 | return false; |
| 1112 | } |
| 1113 | if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) { |
| 1114 | if (!left->isLeftOf(right->fTop)) { |
| 1115 | return split_edge(left, right->fTop, activeEdges, current, c, alloc); |
| 1116 | } |
| 1117 | } else { |
| 1118 | if (!right->isRightOf(left->fTop)) { |
| 1119 | return split_edge(right, left->fTop, activeEdges, current, c, alloc); |
| 1120 | } |
| 1121 | } |
| 1122 | if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) { |
| 1123 | if (!left->isLeftOf(right->fBottom)) { |
| 1124 | return split_edge(left, right->fBottom, activeEdges, current, c, alloc); |
| 1125 | } |
| 1126 | } else { |
| 1127 | if (!right->isRightOf(left->fBottom)) { |
| 1128 | return split_edge(right, left->fBottom, activeEdges, current, c, alloc); |
| 1129 | } |
| 1130 | } |
| 1131 | return false; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1132 | } |
| 1133 | |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 1134 | Edge* connect(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc, |
Stephen White | 48ded38 | 2017-02-03 10:15:16 -0500 | [diff] [blame] | 1135 | int winding_scale = 1) { |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1136 | if (!prev || !next || prev->fPoint == next->fPoint) { |
| 1137 | return nullptr; |
| 1138 | } |
Stephen White | bf6137e | 2017-01-04 15:43:26 -0500 | [diff] [blame] | 1139 | Edge* edge = new_edge(prev, next, type, c, alloc); |
Stephen White | 8a0bfc5 | 2017-02-21 15:24:13 -0500 | [diff] [blame] | 1140 | insert_edge_below(edge, edge->fTop, c); |
| 1141 | insert_edge_above(edge, edge->fBottom, c); |
Stephen White | 48ded38 | 2017-02-03 10:15:16 -0500 | [diff] [blame] | 1142 | edge->fWinding *= winding_scale; |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1143 | merge_collinear_edges(edge, nullptr, nullptr, c); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1144 | return edge; |
| 1145 | } |
| 1146 | |
Stephen White | bf6137e | 2017-01-04 15:43:26 -0500 | [diff] [blame] | 1147 | void merge_vertices(Vertex* src, Vertex* dst, VertexList* mesh, Comparator& c, |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 1148 | SkArenaAlloc& alloc) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1149 | LOG("found coincident verts at %g, %g; merging %g into %g\n", src->fPoint.fX, src->fPoint.fY, |
| 1150 | src->fID, dst->fID); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1151 | dst->fAlpha = SkTMax(src->fAlpha, dst->fAlpha); |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 1152 | if (src->fPartner) { |
| 1153 | src->fPartner->fPartner = dst; |
| 1154 | } |
Stephen White | 7b37694 | 2018-05-22 11:51:32 -0400 | [diff] [blame] | 1155 | while (Edge* edge = src->fFirstEdgeAbove) { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1156 | set_bottom(edge, dst, nullptr, nullptr, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1157 | } |
Stephen White | 7b37694 | 2018-05-22 11:51:32 -0400 | [diff] [blame] | 1158 | while (Edge* edge = src->fFirstEdgeBelow) { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1159 | set_top(edge, dst, nullptr, nullptr, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1160 | } |
Stephen White | bf6137e | 2017-01-04 15:43:26 -0500 | [diff] [blame] | 1161 | mesh->remove(src); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1162 | } |
| 1163 | |
Stephen White | 95152e1 | 2017-12-18 10:52:44 -0500 | [diff] [blame] | 1164 | Vertex* create_sorted_vertex(const SkPoint& p, uint8_t alpha, VertexList* mesh, |
| 1165 | Vertex* reference, Comparator& c, SkArenaAlloc& alloc) { |
| 1166 | Vertex* prevV = reference; |
| 1167 | while (prevV && c.sweep_lt(p, prevV->fPoint)) { |
| 1168 | prevV = prevV->fPrev; |
| 1169 | } |
| 1170 | Vertex* nextV = prevV ? prevV->fNext : mesh->fHead; |
| 1171 | while (nextV && c.sweep_lt(nextV->fPoint, p)) { |
| 1172 | prevV = nextV; |
| 1173 | nextV = nextV->fNext; |
| 1174 | } |
| 1175 | Vertex* v; |
| 1176 | if (prevV && coincident(prevV->fPoint, p)) { |
| 1177 | v = prevV; |
| 1178 | } else if (nextV && coincident(nextV->fPoint, p)) { |
| 1179 | v = nextV; |
| 1180 | } else { |
| 1181 | v = alloc.make<Vertex>(p, alpha); |
| 1182 | #if LOGGING_ENABLED |
| 1183 | if (!prevV) { |
| 1184 | v->fID = mesh->fHead->fID - 1.0f; |
| 1185 | } else if (!nextV) { |
| 1186 | v->fID = mesh->fTail->fID + 1.0f; |
| 1187 | } else { |
| 1188 | v->fID = (prevV->fID + nextV->fID) * 0.5f; |
| 1189 | } |
| 1190 | #endif |
| 1191 | mesh->insert(v, prevV, nextV); |
| 1192 | } |
| 1193 | return v; |
| 1194 | } |
| 1195 | |
Stephen White | 53a0298 | 2018-05-30 22:47:46 -0400 | [diff] [blame] | 1196 | // If an edge's top and bottom points differ only by 1/2 machine epsilon in the primary |
| 1197 | // sort criterion, it may not be possible to split correctly, since there is no point which is |
| 1198 | // below the top and above the bottom. This function detects that case. |
| 1199 | bool nearly_flat(Comparator& c, Edge* edge) { |
| 1200 | SkPoint diff = edge->fBottom->fPoint - edge->fTop->fPoint; |
| 1201 | float primaryDiff = c.fDirection == Comparator::Direction::kHorizontal ? diff.fX : diff.fY; |
Stephen White | 13f3d8d | 2018-06-22 10:19:20 -0400 | [diff] [blame] | 1202 | return fabs(primaryDiff) < std::numeric_limits<float>::epsilon() && primaryDiff != 0.0f; |
Stephen White | 53a0298 | 2018-05-30 22:47:46 -0400 | [diff] [blame] | 1203 | } |
| 1204 | |
Stephen White | e62999f | 2018-06-05 18:45:07 -0400 | [diff] [blame] | 1205 | SkPoint clamp(SkPoint p, SkPoint min, SkPoint max, Comparator& c) { |
| 1206 | if (c.sweep_lt(p, min)) { |
| 1207 | return min; |
| 1208 | } else if (c.sweep_lt(max, p)) { |
| 1209 | return max; |
| 1210 | } else { |
| 1211 | return p; |
| 1212 | } |
| 1213 | } |
| 1214 | |
Stephen White | b141fcb | 2018-06-14 10:15:47 -0400 | [diff] [blame] | 1215 | bool check_for_intersection(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current, |
Stephen White | 0cb3167 | 2017-06-08 14:41:01 -0400 | [diff] [blame] | 1216 | VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) { |
Stephen White | b141fcb | 2018-06-14 10:15:47 -0400 | [diff] [blame] | 1217 | if (!left || !right) { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1218 | return false; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1219 | } |
Stephen White | 56158ae | 2017-01-30 14:31:31 -0500 | [diff] [blame] | 1220 | SkPoint p; |
| 1221 | uint8_t alpha; |
Stephen White | b141fcb | 2018-06-14 10:15:47 -0400 | [diff] [blame] | 1222 | if (left->intersect(*right, &p, &alpha) && p.isFinite()) { |
Ravi Mistry | bfe9598 | 2018-05-29 18:19:07 +0000 | [diff] [blame] | 1223 | Vertex* v; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1224 | LOG("found intersection, pt is %g, %g\n", p.fX, p.fY); |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1225 | Vertex* top = *current; |
| 1226 | // If the intersection point is above the current vertex, rewind to the vertex above the |
| 1227 | // intersection. |
Stephen White | 0cb3167 | 2017-06-08 14:41:01 -0400 | [diff] [blame] | 1228 | while (top && c.sweep_lt(p, top->fPoint)) { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1229 | top = top->fPrev; |
| 1230 | } |
Stephen White | b141fcb | 2018-06-14 10:15:47 -0400 | [diff] [blame] | 1231 | if (!nearly_flat(c, left)) { |
| 1232 | p = clamp(p, left->fTop->fPoint, left->fBottom->fPoint, c); |
Stephen White | e62999f | 2018-06-05 18:45:07 -0400 | [diff] [blame] | 1233 | } |
Stephen White | b141fcb | 2018-06-14 10:15:47 -0400 | [diff] [blame] | 1234 | if (!nearly_flat(c, right)) { |
| 1235 | p = clamp(p, right->fTop->fPoint, right->fBottom->fPoint, c); |
Stephen White | e62999f | 2018-06-05 18:45:07 -0400 | [diff] [blame] | 1236 | } |
Stephen White | b141fcb | 2018-06-14 10:15:47 -0400 | [diff] [blame] | 1237 | if (p == left->fTop->fPoint) { |
| 1238 | v = left->fTop; |
| 1239 | } else if (p == left->fBottom->fPoint) { |
| 1240 | v = left->fBottom; |
| 1241 | } else if (p == right->fTop->fPoint) { |
| 1242 | v = right->fTop; |
| 1243 | } else if (p == right->fBottom->fPoint) { |
| 1244 | v = right->fBottom; |
Ravi Mistry | bfe9598 | 2018-05-29 18:19:07 +0000 | [diff] [blame] | 1245 | } else { |
Stephen White | 95152e1 | 2017-12-18 10:52:44 -0500 | [diff] [blame] | 1246 | v = create_sorted_vertex(p, alpha, mesh, top, c, alloc); |
Stephen White | b141fcb | 2018-06-14 10:15:47 -0400 | [diff] [blame] | 1247 | if (left->fTop->fPartner) { |
| 1248 | Line line1 = left->fLine; |
| 1249 | Line line2 = right->fLine; |
| 1250 | int dir = left->fType == Edge::Type::kOuter ? -1 : 1; |
| 1251 | line1.fC += sqrt(left->fLine.magSq()) * (left->fWinding > 0 ? 1 : -1) * dir; |
| 1252 | line2.fC += sqrt(right->fLine.magSq()) * (right->fWinding > 0 ? 1 : -1) * dir; |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1253 | SkPoint p; |
| 1254 | if (line1.intersect(line2, &p)) { |
| 1255 | LOG("synthesizing partner (%g,%g) for intersection vertex %g\n", |
| 1256 | p.fX, p.fY, v->fID); |
| 1257 | v->fPartner = alloc.make<Vertex>(p, 255 - v->fAlpha); |
| 1258 | } |
| 1259 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1260 | } |
Stephen White | 0cb3167 | 2017-06-08 14:41:01 -0400 | [diff] [blame] | 1261 | rewind(activeEdges, current, top ? top : v, c); |
Stephen White | b141fcb | 2018-06-14 10:15:47 -0400 | [diff] [blame] | 1262 | split_edge(left, v, activeEdges, current, c, alloc); |
| 1263 | split_edge(right, v, activeEdges, current, c, alloc); |
Stephen White | 92eba8a | 2017-02-06 09:50:27 -0500 | [diff] [blame] | 1264 | v->fAlpha = SkTMax(v->fAlpha, alpha); |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1265 | return true; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1266 | } |
Stephen White | b141fcb | 2018-06-14 10:15:47 -0400 | [diff] [blame] | 1267 | return intersect_edge_pair(left, right, activeEdges, current, c, alloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1268 | } |
| 1269 | |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 1270 | void sanitize_contours(VertexList* contours, int contourCnt, bool approximate) { |
| 1271 | for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) { |
| 1272 | SkASSERT(contour->fHead); |
| 1273 | Vertex* prev = contour->fTail; |
Stephen White | 5926f2d | 2017-02-13 13:55:42 -0500 | [diff] [blame] | 1274 | if (approximate) { |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 1275 | round(&prev->fPoint); |
Stephen White | 5926f2d | 2017-02-13 13:55:42 -0500 | [diff] [blame] | 1276 | } |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 1277 | for (Vertex* v = contour->fHead; v;) { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1278 | if (approximate) { |
| 1279 | round(&v->fPoint); |
| 1280 | } |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 1281 | Vertex* next = v->fNext; |
Stephen White | 3de40f8 | 2018-06-28 09:36:49 -0400 | [diff] [blame] | 1282 | Vertex* nextWrap = next ? next : contour->fHead; |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 1283 | if (coincident(prev->fPoint, v->fPoint)) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1284 | LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY); |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 1285 | contour->remove(v); |
Stephen White | 73e7f80 | 2017-08-23 13:56:07 -0400 | [diff] [blame] | 1286 | } else if (!v->fPoint.isFinite()) { |
| 1287 | LOG("vertex %g,%g non-finite; removing\n", v->fPoint.fX, v->fPoint.fY); |
| 1288 | contour->remove(v); |
Stephen White | 3de40f8 | 2018-06-28 09:36:49 -0400 | [diff] [blame] | 1289 | } else if (Line(prev->fPoint, nextWrap->fPoint).dist(v->fPoint) == 0.0) { |
Stephen White | 06768ca | 2018-05-25 14:50:56 -0400 | [diff] [blame] | 1290 | LOG("vertex %g,%g collinear; removing\n", v->fPoint.fX, v->fPoint.fY); |
| 1291 | contour->remove(v); |
| 1292 | } else { |
| 1293 | prev = v; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1294 | } |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 1295 | v = next; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1296 | } |
| 1297 | } |
| 1298 | } |
| 1299 | |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1300 | bool merge_coincident_vertices(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) { |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 1301 | if (!mesh->fHead) { |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1302 | return false; |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 1303 | } |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1304 | bool merged = false; |
| 1305 | for (Vertex* v = mesh->fHead->fNext; v;) { |
| 1306 | Vertex* next = v->fNext; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1307 | if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) { |
| 1308 | v->fPoint = v->fPrev->fPoint; |
| 1309 | } |
| 1310 | if (coincident(v->fPrev->fPoint, v->fPoint)) { |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1311 | merge_vertices(v, v->fPrev, mesh, c, alloc); |
| 1312 | merged = true; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1313 | } |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1314 | v = next; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1315 | } |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1316 | return merged; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | // Stage 2: convert the contours to a mesh of edges connecting the vertices. |
| 1320 | |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 1321 | void build_edges(VertexList* contours, int contourCnt, VertexList* mesh, Comparator& c, |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 1322 | SkArenaAlloc& alloc) { |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 1323 | for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) { |
| 1324 | Vertex* prev = contour->fTail; |
| 1325 | for (Vertex* v = contour->fHead; v;) { |
| 1326 | Vertex* next = v->fNext; |
| 1327 | connect(prev, v, Edge::Type::kInner, c, alloc); |
| 1328 | mesh->append(v); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1329 | prev = v; |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 1330 | v = next; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1331 | } |
| 1332 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1333 | } |
| 1334 | |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1335 | void connect_partners(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) { |
| 1336 | for (Vertex* outer = mesh->fHead; outer; outer = outer->fNext) { |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 1337 | if (Vertex* inner = outer->fPartner) { |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1338 | if ((inner->fPrev || inner->fNext) && (outer->fPrev || outer->fNext)) { |
| 1339 | // Connector edges get zero winding, since they're only structural (i.e., to ensure |
| 1340 | // no 0-0-0 alpha triangles are produced), and shouldn't affect the poly winding |
| 1341 | // number. |
| 1342 | connect(outer, inner, Edge::Type::kConnector, c, alloc, 0); |
| 1343 | inner->fPartner = outer->fPartner = nullptr; |
| 1344 | } |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 1345 | } |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | template <CompareFunc sweep_lt> |
| 1350 | void sorted_merge(VertexList* front, VertexList* back, VertexList* result) { |
| 1351 | Vertex* a = front->fHead; |
| 1352 | Vertex* b = back->fHead; |
| 1353 | while (a && b) { |
| 1354 | if (sweep_lt(a->fPoint, b->fPoint)) { |
| 1355 | front->remove(a); |
| 1356 | result->append(a); |
| 1357 | a = front->fHead; |
| 1358 | } else { |
| 1359 | back->remove(b); |
| 1360 | result->append(b); |
| 1361 | b = back->fHead; |
| 1362 | } |
| 1363 | } |
| 1364 | result->append(*front); |
| 1365 | result->append(*back); |
| 1366 | } |
| 1367 | |
| 1368 | void sorted_merge(VertexList* front, VertexList* back, VertexList* result, Comparator& c) { |
| 1369 | if (c.fDirection == Comparator::Direction::kHorizontal) { |
| 1370 | sorted_merge<sweep_lt_horiz>(front, back, result); |
| 1371 | } else { |
| 1372 | sorted_merge<sweep_lt_vert>(front, back, result); |
| 1373 | } |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1374 | #if LOGGING_ENABLED |
| 1375 | float id = 0.0f; |
| 1376 | for (Vertex* v = result->fHead; v; v = v->fNext) { |
| 1377 | v->fID = id++; |
| 1378 | } |
| 1379 | #endif |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 1380 | } |
| 1381 | |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1382 | // Stage 3: sort the vertices by increasing sweep direction. |
| 1383 | |
Stephen White | 16a40cb | 2017-02-23 11:10:01 -0500 | [diff] [blame] | 1384 | template <CompareFunc sweep_lt> |
| 1385 | void merge_sort(VertexList* vertices) { |
| 1386 | Vertex* slow = vertices->fHead; |
| 1387 | if (!slow) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1388 | return; |
| 1389 | } |
Stephen White | 16a40cb | 2017-02-23 11:10:01 -0500 | [diff] [blame] | 1390 | Vertex* fast = slow->fNext; |
| 1391 | if (!fast) { |
| 1392 | return; |
| 1393 | } |
| 1394 | do { |
| 1395 | fast = fast->fNext; |
| 1396 | if (fast) { |
| 1397 | fast = fast->fNext; |
| 1398 | slow = slow->fNext; |
| 1399 | } |
| 1400 | } while (fast); |
| 1401 | VertexList front(vertices->fHead, slow); |
| 1402 | VertexList back(slow->fNext, vertices->fTail); |
| 1403 | front.fTail->fNext = back.fHead->fPrev = nullptr; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1404 | |
Stephen White | 16a40cb | 2017-02-23 11:10:01 -0500 | [diff] [blame] | 1405 | merge_sort<sweep_lt>(&front); |
| 1406 | merge_sort<sweep_lt>(&back); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1407 | |
Stephen White | 16a40cb | 2017-02-23 11:10:01 -0500 | [diff] [blame] | 1408 | vertices->fHead = vertices->fTail = nullptr; |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 1409 | sorted_merge<sweep_lt>(&front, &back, vertices); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1410 | } |
| 1411 | |
Stephen White | 95152e1 | 2017-12-18 10:52:44 -0500 | [diff] [blame] | 1412 | void dump_mesh(const VertexList& mesh) { |
| 1413 | #if LOGGING_ENABLED |
| 1414 | for (Vertex* v = mesh.fHead; v; v = v->fNext) { |
| 1415 | LOG("vertex %g (%g, %g) alpha %d", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha); |
| 1416 | if (Vertex* p = v->fPartner) { |
| 1417 | LOG(", partner %g (%g, %g) alpha %d\n", p->fID, p->fPoint.fX, p->fPoint.fY, p->fAlpha); |
| 1418 | } else { |
| 1419 | LOG(", null partner\n"); |
| 1420 | } |
| 1421 | for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) { |
| 1422 | LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding); |
| 1423 | } |
| 1424 | for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { |
| 1425 | LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding); |
| 1426 | } |
| 1427 | } |
| 1428 | #endif |
| 1429 | } |
| 1430 | |
Stephen White | 89042d5 | 2018-06-08 12:18:22 -0400 | [diff] [blame] | 1431 | #ifdef SK_DEBUG |
| 1432 | void validate_edge_pair(Edge* left, Edge* right, Comparator& c) { |
| 1433 | if (!left || !right) { |
| 1434 | return; |
| 1435 | } |
| 1436 | if (left->fTop == right->fTop) { |
| 1437 | SkASSERT(left->isLeftOf(right->fBottom)); |
| 1438 | SkASSERT(right->isRightOf(left->fBottom)); |
| 1439 | } else if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) { |
| 1440 | SkASSERT(left->isLeftOf(right->fTop)); |
| 1441 | } else { |
| 1442 | SkASSERT(right->isRightOf(left->fTop)); |
| 1443 | } |
| 1444 | if (left->fBottom == right->fBottom) { |
| 1445 | SkASSERT(left->isLeftOf(right->fTop)); |
| 1446 | SkASSERT(right->isRightOf(left->fTop)); |
| 1447 | } else if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) { |
| 1448 | SkASSERT(left->isLeftOf(right->fBottom)); |
| 1449 | } else { |
| 1450 | SkASSERT(right->isRightOf(left->fBottom)); |
| 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | void validate_edge_list(EdgeList* edges, Comparator& c) { |
| 1455 | Edge* left = edges->fHead; |
| 1456 | if (!left) { |
| 1457 | return; |
| 1458 | } |
| 1459 | for (Edge* right = left->fRight; right; right = right->fRight) { |
| 1460 | validate_edge_pair(left, right, c); |
| 1461 | left = right; |
| 1462 | } |
| 1463 | } |
| 1464 | #endif |
| 1465 | |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1466 | // Stage 4: Simplify the mesh by inserting new vertices at intersecting edges. |
| 1467 | |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1468 | bool simplify(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1469 | LOG("simplifying complex polygons\n"); |
| 1470 | EdgeList activeEdges; |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1471 | bool found = false; |
Stephen White | 0cb3167 | 2017-06-08 14:41:01 -0400 | [diff] [blame] | 1472 | for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1473 | if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) { |
| 1474 | continue; |
| 1475 | } |
Stephen White | 8a0bfc5 | 2017-02-21 15:24:13 -0500 | [diff] [blame] | 1476 | Edge* leftEnclosingEdge; |
| 1477 | Edge* rightEnclosingEdge; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1478 | bool restartChecks; |
| 1479 | do { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1480 | LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1481 | restartChecks = false; |
| 1482 | find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge); |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1483 | v->fLeftEnclosingEdge = leftEnclosingEdge; |
| 1484 | v->fRightEnclosingEdge = rightEnclosingEdge; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1485 | if (v->fFirstEdgeBelow) { |
Stephen White | bf6137e | 2017-01-04 15:43:26 -0500 | [diff] [blame] | 1486 | for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) { |
Stephen White | 89042d5 | 2018-06-08 12:18:22 -0400 | [diff] [blame] | 1487 | if (check_for_intersection(leftEnclosingEdge, edge, &activeEdges, &v, mesh, c, |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1488 | alloc)) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1489 | restartChecks = true; |
| 1490 | break; |
| 1491 | } |
Stephen White | 0cb3167 | 2017-06-08 14:41:01 -0400 | [diff] [blame] | 1492 | if (check_for_intersection(edge, rightEnclosingEdge, &activeEdges, &v, mesh, c, |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1493 | alloc)) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1494 | restartChecks = true; |
| 1495 | break; |
| 1496 | } |
| 1497 | } |
| 1498 | } else { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1499 | if (check_for_intersection(leftEnclosingEdge, rightEnclosingEdge, |
Stephen White | 0cb3167 | 2017-06-08 14:41:01 -0400 | [diff] [blame] | 1500 | &activeEdges, &v, mesh, c, alloc)) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1501 | restartChecks = true; |
| 1502 | } |
| 1503 | |
| 1504 | } |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1505 | found = found || restartChecks; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1506 | } while (restartChecks); |
Stephen White | 89042d5 | 2018-06-08 12:18:22 -0400 | [diff] [blame] | 1507 | #ifdef SK_DEBUG |
| 1508 | validate_edge_list(&activeEdges, c); |
| 1509 | #endif |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1510 | for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) { |
| 1511 | remove_edge(e, &activeEdges); |
| 1512 | } |
| 1513 | Edge* leftEdge = leftEnclosingEdge; |
| 1514 | for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { |
| 1515 | insert_edge(e, leftEdge, &activeEdges); |
| 1516 | leftEdge = e; |
| 1517 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1518 | } |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1519 | SkASSERT(!activeEdges.fHead && !activeEdges.fTail); |
| 1520 | return found; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1521 | } |
| 1522 | |
| 1523 | // Stage 5: Tessellate the simplified mesh into monotone polygons. |
| 1524 | |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 1525 | Poly* tessellate(const VertexList& vertices, SkArenaAlloc& alloc) { |
Stephen White | 95152e1 | 2017-12-18 10:52:44 -0500 | [diff] [blame] | 1526 | LOG("\ntessellating simple polygons\n"); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1527 | EdgeList activeEdges; |
| 1528 | Poly* polys = nullptr; |
Stephen White | bf6137e | 2017-01-04 15:43:26 -0500 | [diff] [blame] | 1529 | for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1530 | if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) { |
| 1531 | continue; |
| 1532 | } |
| 1533 | #if LOGGING_ENABLED |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1534 | LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1535 | #endif |
Stephen White | 8a0bfc5 | 2017-02-21 15:24:13 -0500 | [diff] [blame] | 1536 | Edge* leftEnclosingEdge; |
| 1537 | Edge* rightEnclosingEdge; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1538 | find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge); |
Stephen White | 8a0bfc5 | 2017-02-21 15:24:13 -0500 | [diff] [blame] | 1539 | Poly* leftPoly; |
| 1540 | Poly* rightPoly; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1541 | if (v->fFirstEdgeAbove) { |
| 1542 | leftPoly = v->fFirstEdgeAbove->fLeftPoly; |
| 1543 | rightPoly = v->fLastEdgeAbove->fRightPoly; |
| 1544 | } else { |
| 1545 | leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr; |
| 1546 | rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr; |
| 1547 | } |
| 1548 | #if LOGGING_ENABLED |
| 1549 | LOG("edges above:\n"); |
| 1550 | for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) { |
| 1551 | LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID, |
| 1552 | e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1); |
| 1553 | } |
| 1554 | LOG("edges below:\n"); |
| 1555 | for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { |
| 1556 | LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID, |
| 1557 | e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1); |
| 1558 | } |
| 1559 | #endif |
| 1560 | if (v->fFirstEdgeAbove) { |
| 1561 | if (leftPoly) { |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 1562 | leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, Poly::kRight_Side, alloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1563 | } |
| 1564 | if (rightPoly) { |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 1565 | rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Poly::kLeft_Side, alloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1566 | } |
| 1567 | for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1568 | Edge* rightEdge = e->fNextEdgeAbove; |
Stephen White | 8a0bfc5 | 2017-02-21 15:24:13 -0500 | [diff] [blame] | 1569 | remove_edge(e, &activeEdges); |
| 1570 | if (e->fRightPoly) { |
| 1571 | e->fRightPoly->addEdge(e, Poly::kLeft_Side, alloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1572 | } |
Stephen White | 8a0bfc5 | 2017-02-21 15:24:13 -0500 | [diff] [blame] | 1573 | if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != e->fRightPoly) { |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 1574 | rightEdge->fLeftPoly->addEdge(e, Poly::kRight_Side, alloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1575 | } |
| 1576 | } |
| 1577 | remove_edge(v->fLastEdgeAbove, &activeEdges); |
| 1578 | if (!v->fFirstEdgeBelow) { |
| 1579 | if (leftPoly && rightPoly && leftPoly != rightPoly) { |
| 1580 | SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr); |
| 1581 | rightPoly->fPartner = leftPoly; |
| 1582 | leftPoly->fPartner = rightPoly; |
| 1583 | } |
| 1584 | } |
| 1585 | } |
| 1586 | if (v->fFirstEdgeBelow) { |
| 1587 | if (!v->fFirstEdgeAbove) { |
senorblanco | 93e3fff | 2016-06-07 12:36:00 -0700 | [diff] [blame] | 1588 | if (leftPoly && rightPoly) { |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 1589 | if (leftPoly == rightPoly) { |
| 1590 | if (leftPoly->fTail && leftPoly->fTail->fSide == Poly::kLeft_Side) { |
| 1591 | leftPoly = new_poly(&polys, leftPoly->lastVertex(), |
| 1592 | leftPoly->fWinding, alloc); |
| 1593 | leftEnclosingEdge->fRightPoly = leftPoly; |
| 1594 | } else { |
| 1595 | rightPoly = new_poly(&polys, rightPoly->lastVertex(), |
| 1596 | rightPoly->fWinding, alloc); |
| 1597 | rightEnclosingEdge->fLeftPoly = rightPoly; |
| 1598 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1599 | } |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 1600 | Edge* join = alloc.make<Edge>(leftPoly->lastVertex(), v, 1, Edge::Type::kInner); |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 1601 | leftPoly = leftPoly->addEdge(join, Poly::kRight_Side, alloc); |
| 1602 | rightPoly = rightPoly->addEdge(join, Poly::kLeft_Side, alloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1603 | } |
| 1604 | } |
| 1605 | Edge* leftEdge = v->fFirstEdgeBelow; |
| 1606 | leftEdge->fLeftPoly = leftPoly; |
| 1607 | insert_edge(leftEdge, leftEnclosingEdge, &activeEdges); |
| 1608 | for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge; |
| 1609 | rightEdge = rightEdge->fNextEdgeBelow) { |
| 1610 | insert_edge(rightEdge, leftEdge, &activeEdges); |
| 1611 | int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0; |
| 1612 | winding += leftEdge->fWinding; |
| 1613 | if (winding != 0) { |
| 1614 | Poly* poly = new_poly(&polys, v, winding, alloc); |
| 1615 | leftEdge->fRightPoly = rightEdge->fLeftPoly = poly; |
| 1616 | } |
| 1617 | leftEdge = rightEdge; |
| 1618 | } |
| 1619 | v->fLastEdgeBelow->fRightPoly = rightPoly; |
| 1620 | } |
| 1621 | #if LOGGING_ENABLED |
| 1622 | LOG("\nactive edges:\n"); |
| 1623 | for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) { |
| 1624 | LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID, |
| 1625 | e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1); |
| 1626 | } |
| 1627 | #endif |
| 1628 | } |
| 1629 | return polys; |
| 1630 | } |
| 1631 | |
Stephen White | bf6137e | 2017-01-04 15:43:26 -0500 | [diff] [blame] | 1632 | void remove_non_boundary_edges(const VertexList& mesh, SkPath::FillType fillType, |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 1633 | SkArenaAlloc& alloc) { |
Stephen White | 4978906 | 2017-02-21 10:35:49 -0500 | [diff] [blame] | 1634 | LOG("removing non-boundary edges\n"); |
| 1635 | EdgeList activeEdges; |
Stephen White | bf6137e | 2017-01-04 15:43:26 -0500 | [diff] [blame] | 1636 | for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) { |
Stephen White | 4978906 | 2017-02-21 10:35:49 -0500 | [diff] [blame] | 1637 | if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) { |
| 1638 | continue; |
| 1639 | } |
| 1640 | Edge* leftEnclosingEdge; |
| 1641 | Edge* rightEnclosingEdge; |
| 1642 | find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge); |
| 1643 | bool prevFilled = leftEnclosingEdge && |
| 1644 | apply_fill_type(fillType, leftEnclosingEdge->fWinding); |
| 1645 | for (Edge* e = v->fFirstEdgeAbove; e;) { |
| 1646 | Edge* next = e->fNextEdgeAbove; |
| 1647 | remove_edge(e, &activeEdges); |
| 1648 | bool filled = apply_fill_type(fillType, e->fWinding); |
| 1649 | if (filled == prevFilled) { |
Stephen White | e7a364d | 2017-01-11 16:19:26 -0500 | [diff] [blame] | 1650 | disconnect(e); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1651 | } |
Stephen White | 4978906 | 2017-02-21 10:35:49 -0500 | [diff] [blame] | 1652 | prevFilled = filled; |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1653 | e = next; |
| 1654 | } |
Stephen White | 4978906 | 2017-02-21 10:35:49 -0500 | [diff] [blame] | 1655 | Edge* prev = leftEnclosingEdge; |
| 1656 | for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { |
| 1657 | if (prev) { |
| 1658 | e->fWinding += prev->fWinding; |
| 1659 | } |
| 1660 | insert_edge(e, prev, &activeEdges); |
| 1661 | prev = e; |
| 1662 | } |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1663 | } |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1664 | } |
| 1665 | |
Stephen White | 6641212 | 2017-03-01 11:48:27 -0500 | [diff] [blame] | 1666 | // Note: this is the normal to the edge, but not necessarily unit length. |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1667 | void get_edge_normal(const Edge* e, SkVector* normal) { |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1668 | normal->set(SkDoubleToScalar(e->fLine.fA), |
| 1669 | SkDoubleToScalar(e->fLine.fB)); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1670 | } |
| 1671 | |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1672 | void reconnect(Edge* edge, Vertex* src, Vertex* dst, Comparator& c) { |
| 1673 | disconnect(edge); |
| 1674 | if (src == edge->fTop) { |
| 1675 | edge->fTop = dst; |
| 1676 | } else { |
| 1677 | SkASSERT(src == edge->fBottom); |
| 1678 | edge->fBottom = dst; |
| 1679 | } |
| 1680 | if (edge->fEvent) { |
| 1681 | edge->fEvent->fEdge = nullptr; |
| 1682 | } |
| 1683 | if (edge->fTop == edge->fBottom) { |
| 1684 | return; |
| 1685 | } |
| 1686 | if (c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) { |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 1687 | using std::swap; |
| 1688 | swap(edge->fTop, edge->fBottom); |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1689 | edge->fWinding *= -1; |
| 1690 | } |
| 1691 | edge->recompute(); |
| 1692 | insert_edge_below(edge, edge->fTop, c); |
| 1693 | insert_edge_above(edge, edge->fBottom, c); |
| 1694 | merge_collinear_edges(edge, nullptr, nullptr, c); |
| 1695 | } |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1696 | |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1697 | // Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions |
| 1698 | // and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to |
| 1699 | // invert on stroking. |
| 1700 | |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 1701 | void simplify_boundary(EdgeList* boundary, Comparator& c, SkArenaAlloc& alloc) { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1702 | Edge* prevEdge = boundary->fTail; |
| 1703 | SkVector prevNormal; |
| 1704 | get_edge_normal(prevEdge, &prevNormal); |
| 1705 | for (Edge* e = boundary->fHead; e != nullptr;) { |
| 1706 | Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom; |
| 1707 | Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop; |
| 1708 | double dist = e->dist(prev->fPoint); |
| 1709 | SkVector normal; |
| 1710 | get_edge_normal(e, &normal); |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1711 | double denom = 0.0625f; |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1712 | if (prevNormal.dot(normal) < 0.0 && (dist * dist) <= denom) { |
Stephen White | bf6137e | 2017-01-04 15:43:26 -0500 | [diff] [blame] | 1713 | Edge* join = new_edge(prev, next, Edge::Type::kInner, c, alloc); |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1714 | if (prev->fPoint != next->fPoint) { |
| 1715 | join->fLine.normalize(); |
| 1716 | join->fLine = join->fLine * join->fWinding; |
| 1717 | } |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1718 | insert_edge(join, e, boundary); |
| 1719 | remove_edge(prevEdge, boundary); |
| 1720 | remove_edge(e, boundary); |
| 1721 | if (join->fLeft && join->fRight) { |
| 1722 | prevEdge = join->fLeft; |
| 1723 | e = join; |
| 1724 | } else { |
| 1725 | prevEdge = boundary->fTail; |
| 1726 | e = boundary->fHead; // join->fLeft ? join->fLeft : join; |
| 1727 | } |
| 1728 | get_edge_normal(prevEdge, &prevNormal); |
| 1729 | } else { |
| 1730 | prevEdge = e; |
| 1731 | prevNormal = normal; |
| 1732 | e = e->fRight; |
| 1733 | } |
| 1734 | } |
| 1735 | } |
| 1736 | |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1737 | void reconnect_all_overlap_edges(Vertex* src, Vertex* dst, Edge* current, Comparator& c) { |
| 1738 | if (src->fPartner) { |
| 1739 | src->fPartner->fPartner = dst; |
| 1740 | } |
| 1741 | for (Edge* e = src->fFirstEdgeAbove; e; ) { |
| 1742 | Edge* next = e->fNextEdgeAbove; |
| 1743 | if (e->fOverlap && e != current) { |
| 1744 | reconnect(e, src, dst, c); |
| 1745 | } |
| 1746 | e = next; |
| 1747 | } |
| 1748 | for (Edge* e = src->fFirstEdgeBelow; e; ) { |
| 1749 | Edge* next = e->fNextEdgeBelow; |
| 1750 | if (e->fOverlap && e != current) { |
| 1751 | reconnect(e, src, dst, c); |
| 1752 | } |
| 1753 | e = next; |
| 1754 | } |
| 1755 | } |
| 1756 | |
| 1757 | void Event::apply(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) { |
Stephen White | 24289e0 | 2018-06-29 17:02:21 -0400 | [diff] [blame] | 1758 | if (!fEdge || !fEdge->fTop || !fEdge->fBottom) { |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1759 | return; |
| 1760 | } |
| 1761 | Vertex* top = fEdge->fTop; |
| 1762 | Vertex* bottom = fEdge->fBottom; |
| 1763 | Vertex* dest = create_sorted_vertex(fPoint, fAlpha, mesh, fEdge->fTop, c, alloc); |
| 1764 | LOG("collapsing edge %g -> %g to %g (%g, %g) alpha %d\n", |
| 1765 | top->fID, bottom->fID, dest->fID, fPoint.fX, fPoint.fY, fAlpha); |
| 1766 | reconnect_all_overlap_edges(top, dest, fEdge, c); |
| 1767 | reconnect_all_overlap_edges(bottom, dest, fEdge, c); |
| 1768 | |
| 1769 | // Since the destination has multiple partners, give it none. |
| 1770 | dest->fPartner = nullptr; |
Stephen White | 77169c8 | 2018-06-05 09:15:59 -0400 | [diff] [blame] | 1771 | |
| 1772 | // Disconnect all collapsed edges except outer boundaries. |
| 1773 | // Those are required to preserve shape coverage and winding correctness. |
| 1774 | if (!fIsOuterBoundary) { |
| 1775 | disconnect(fEdge); |
| 1776 | } else { |
| 1777 | LOG("edge %g -> %g is outer boundary; not disconnecting.\n", |
| 1778 | fEdge->fTop->fID, fEdge->fBottom->fID); |
| 1779 | } |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1780 | |
| 1781 | // If top still has some connected edges, set its partner to dest. |
| 1782 | top->fPartner = top->fFirstEdgeAbove || top->fFirstEdgeBelow ? dest : nullptr; |
| 1783 | |
| 1784 | // If bottom still has some connected edges, set its partner to dest. |
| 1785 | bottom->fPartner = bottom->fFirstEdgeAbove || bottom->fFirstEdgeBelow ? dest : nullptr; |
| 1786 | } |
| 1787 | |
| 1788 | bool is_overlap_edge(Edge* e) { |
| 1789 | if (e->fType == Edge::Type::kOuter) { |
| 1790 | return e->fWinding != 0 && e->fWinding != 1; |
| 1791 | } else if (e->fType == Edge::Type::kInner) { |
| 1792 | return e->fWinding != 0 && e->fWinding != -2; |
| 1793 | } else { |
| 1794 | return false; |
| 1795 | } |
| 1796 | } |
| 1797 | |
| 1798 | // This is a stripped-down version of tessellate() which computes edges which |
| 1799 | // join two filled regions, which represent overlap regions, and collapses them. |
| 1800 | bool collapse_overlap_regions(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) { |
| 1801 | LOG("\nfinding overlap regions\n"); |
| 1802 | EdgeList activeEdges; |
| 1803 | EventList events; |
| 1804 | for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) { |
| 1805 | if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) { |
| 1806 | continue; |
| 1807 | } |
| 1808 | Edge* leftEnclosingEdge; |
| 1809 | Edge* rightEnclosingEdge; |
| 1810 | find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge); |
| 1811 | for (Edge* e = v->fLastEdgeAbove; e; e = e->fPrevEdgeAbove) { |
| 1812 | Edge* prev = e->fPrevEdgeAbove ? e->fPrevEdgeAbove : leftEnclosingEdge; |
| 1813 | remove_edge(e, &activeEdges); |
| 1814 | if (prev) { |
| 1815 | e->fWinding -= prev->fWinding; |
| 1816 | } |
| 1817 | } |
| 1818 | Edge* prev = leftEnclosingEdge; |
| 1819 | for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { |
| 1820 | if (prev) { |
| 1821 | e->fWinding += prev->fWinding; |
| 1822 | e->fOverlap = e->fOverlap || is_overlap_edge(prev); |
| 1823 | } |
| 1824 | e->fOverlap = e->fOverlap || is_overlap_edge(e); |
| 1825 | if (e->fOverlap) { |
Stephen White | 77169c8 | 2018-06-05 09:15:59 -0400 | [diff] [blame] | 1826 | // If this edge borders a zero-winding area, it's a boundary; don't disconnect it. |
| 1827 | bool isOuterBoundary = e->fType == Edge::Type::kOuter && |
| 1828 | (!prev || prev->fWinding == 0 || e->fWinding == 0); |
| 1829 | create_event(e, isOuterBoundary, &events, alloc); |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1830 | } |
| 1831 | insert_edge(e, prev, &activeEdges); |
| 1832 | prev = e; |
| 1833 | } |
| 1834 | } |
| 1835 | LOG("\ncollapsing overlap regions\n"); |
| 1836 | if (events.count() == 0) { |
| 1837 | return false; |
| 1838 | } |
| 1839 | while (events.count() > 0) { |
| 1840 | Event* event = events.peek(); |
| 1841 | events.pop(); |
| 1842 | event->apply(mesh, c, alloc); |
| 1843 | } |
| 1844 | return true; |
| 1845 | } |
| 1846 | |
| 1847 | bool inversion(Vertex* prev, Vertex* next, Edge* origEdge, Comparator& c) { |
| 1848 | if (!prev || !next) { |
| 1849 | return true; |
| 1850 | } |
| 1851 | int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1; |
| 1852 | return winding != origEdge->fWinding; |
| 1853 | } |
Stephen White | 92eba8a | 2017-02-06 09:50:27 -0500 | [diff] [blame] | 1854 | |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1855 | // Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to |
| 1856 | // find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a |
| 1857 | // new antialiased mesh from those vertices. |
| 1858 | |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1859 | void stroke_boundary(EdgeList* boundary, VertexList* innerMesh, VertexList* outerMesh, |
| 1860 | Comparator& c, SkArenaAlloc& alloc) { |
| 1861 | LOG("\nstroking boundary\n"); |
| 1862 | // A boundary with fewer than 3 edges is degenerate. |
| 1863 | if (!boundary->fHead || !boundary->fHead->fRight || !boundary->fHead->fRight->fRight) { |
| 1864 | return; |
| 1865 | } |
| 1866 | Edge* prevEdge = boundary->fTail; |
| 1867 | Vertex* prevV = prevEdge->fWinding > 0 ? prevEdge->fTop : prevEdge->fBottom; |
| 1868 | SkVector prevNormal; |
| 1869 | get_edge_normal(prevEdge, &prevNormal); |
| 1870 | double radius = 0.5; |
| 1871 | Line prevInner(prevEdge->fLine); |
| 1872 | prevInner.fC -= radius; |
| 1873 | Line prevOuter(prevEdge->fLine); |
| 1874 | prevOuter.fC += radius; |
| 1875 | VertexList innerVertices; |
| 1876 | VertexList outerVertices; |
| 1877 | bool innerInversion = true; |
| 1878 | bool outerInversion = true; |
| 1879 | for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) { |
| 1880 | Vertex* v = e->fWinding > 0 ? e->fTop : e->fBottom; |
| 1881 | SkVector normal; |
| 1882 | get_edge_normal(e, &normal); |
| 1883 | Line inner(e->fLine); |
| 1884 | inner.fC -= radius; |
| 1885 | Line outer(e->fLine); |
| 1886 | outer.fC += radius; |
| 1887 | SkPoint innerPoint, outerPoint; |
| 1888 | LOG("stroking vertex %g (%g, %g)\n", v->fID, v->fPoint.fX, v->fPoint.fY); |
| 1889 | if (!prevEdge->fLine.nearParallel(e->fLine) && prevInner.intersect(inner, &innerPoint) && |
| 1890 | prevOuter.intersect(outer, &outerPoint)) { |
| 1891 | float cosAngle = normal.dot(prevNormal); |
| 1892 | if (cosAngle < -kCosMiterAngle) { |
| 1893 | Vertex* nextV = e->fWinding > 0 ? e->fBottom : e->fTop; |
| 1894 | |
| 1895 | // This is a pointy vertex whose angle is smaller than the threshold; miter it. |
| 1896 | Line bisector(innerPoint, outerPoint); |
| 1897 | Line tangent(v->fPoint, v->fPoint + SkPoint::Make(bisector.fA, bisector.fB)); |
| 1898 | if (tangent.fA == 0 && tangent.fB == 0) { |
| 1899 | continue; |
| 1900 | } |
| 1901 | tangent.normalize(); |
| 1902 | Line innerTangent(tangent); |
| 1903 | Line outerTangent(tangent); |
| 1904 | innerTangent.fC -= 0.5; |
| 1905 | outerTangent.fC += 0.5; |
| 1906 | SkPoint innerPoint1, innerPoint2, outerPoint1, outerPoint2; |
| 1907 | if (prevNormal.cross(normal) > 0) { |
| 1908 | // Miter inner points |
| 1909 | if (!innerTangent.intersect(prevInner, &innerPoint1) || |
| 1910 | !innerTangent.intersect(inner, &innerPoint2) || |
| 1911 | !outerTangent.intersect(bisector, &outerPoint)) { |
Stephen White | f470b7e | 2018-01-04 16:45:51 -0500 | [diff] [blame] | 1912 | continue; |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1913 | } |
| 1914 | Line prevTangent(prevV->fPoint, |
| 1915 | prevV->fPoint + SkVector::Make(prevOuter.fA, prevOuter.fB)); |
| 1916 | Line nextTangent(nextV->fPoint, |
| 1917 | nextV->fPoint + SkVector::Make(outer.fA, outer.fB)); |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1918 | if (prevTangent.dist(outerPoint) > 0) { |
Stephen White | 4f34fca | 2018-01-11 16:14:04 -0500 | [diff] [blame] | 1919 | bisector.intersect(prevTangent, &outerPoint); |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1920 | } |
| 1921 | if (nextTangent.dist(outerPoint) < 0) { |
Stephen White | 4f34fca | 2018-01-11 16:14:04 -0500 | [diff] [blame] | 1922 | bisector.intersect(nextTangent, &outerPoint); |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1923 | } |
| 1924 | outerPoint1 = outerPoint2 = outerPoint; |
| 1925 | } else { |
| 1926 | // Miter outer points |
| 1927 | if (!outerTangent.intersect(prevOuter, &outerPoint1) || |
| 1928 | !outerTangent.intersect(outer, &outerPoint2)) { |
Stephen White | f470b7e | 2018-01-04 16:45:51 -0500 | [diff] [blame] | 1929 | continue; |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1930 | } |
| 1931 | Line prevTangent(prevV->fPoint, |
| 1932 | prevV->fPoint + SkVector::Make(prevInner.fA, prevInner.fB)); |
| 1933 | Line nextTangent(nextV->fPoint, |
| 1934 | nextV->fPoint + SkVector::Make(inner.fA, inner.fB)); |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1935 | if (prevTangent.dist(innerPoint) > 0) { |
Stephen White | 4f34fca | 2018-01-11 16:14:04 -0500 | [diff] [blame] | 1936 | bisector.intersect(prevTangent, &innerPoint); |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1937 | } |
| 1938 | if (nextTangent.dist(innerPoint) < 0) { |
Stephen White | 4f34fca | 2018-01-11 16:14:04 -0500 | [diff] [blame] | 1939 | bisector.intersect(nextTangent, &innerPoint); |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1940 | } |
| 1941 | innerPoint1 = innerPoint2 = innerPoint; |
| 1942 | } |
Stephen White | ea49523 | 2018-04-03 11:28:15 -0400 | [diff] [blame] | 1943 | if (!innerPoint1.isFinite() || !innerPoint2.isFinite() || |
| 1944 | !outerPoint1.isFinite() || !outerPoint2.isFinite()) { |
| 1945 | continue; |
| 1946 | } |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1947 | LOG("inner (%g, %g), (%g, %g), ", |
| 1948 | innerPoint1.fX, innerPoint1.fY, innerPoint2.fX, innerPoint2.fY); |
| 1949 | LOG("outer (%g, %g), (%g, %g)\n", |
| 1950 | outerPoint1.fX, outerPoint1.fY, outerPoint2.fX, outerPoint2.fY); |
| 1951 | Vertex* innerVertex1 = alloc.make<Vertex>(innerPoint1, 255); |
| 1952 | Vertex* innerVertex2 = alloc.make<Vertex>(innerPoint2, 255); |
| 1953 | Vertex* outerVertex1 = alloc.make<Vertex>(outerPoint1, 0); |
| 1954 | Vertex* outerVertex2 = alloc.make<Vertex>(outerPoint2, 0); |
| 1955 | innerVertex1->fPartner = outerVertex1; |
| 1956 | innerVertex2->fPartner = outerVertex2; |
| 1957 | outerVertex1->fPartner = innerVertex1; |
| 1958 | outerVertex2->fPartner = innerVertex2; |
| 1959 | if (!inversion(innerVertices.fTail, innerVertex1, prevEdge, c)) { |
| 1960 | innerInversion = false; |
| 1961 | } |
| 1962 | if (!inversion(outerVertices.fTail, outerVertex1, prevEdge, c)) { |
| 1963 | outerInversion = false; |
| 1964 | } |
| 1965 | innerVertices.append(innerVertex1); |
| 1966 | innerVertices.append(innerVertex2); |
| 1967 | outerVertices.append(outerVertex1); |
| 1968 | outerVertices.append(outerVertex2); |
| 1969 | } else { |
| 1970 | LOG("inner (%g, %g), ", innerPoint.fX, innerPoint.fY); |
| 1971 | LOG("outer (%g, %g)\n", outerPoint.fX, outerPoint.fY); |
| 1972 | Vertex* innerVertex = alloc.make<Vertex>(innerPoint, 255); |
| 1973 | Vertex* outerVertex = alloc.make<Vertex>(outerPoint, 0); |
| 1974 | innerVertex->fPartner = outerVertex; |
| 1975 | outerVertex->fPartner = innerVertex; |
| 1976 | if (!inversion(innerVertices.fTail, innerVertex, prevEdge, c)) { |
| 1977 | innerInversion = false; |
| 1978 | } |
| 1979 | if (!inversion(outerVertices.fTail, outerVertex, prevEdge, c)) { |
| 1980 | outerInversion = false; |
| 1981 | } |
| 1982 | innerVertices.append(innerVertex); |
| 1983 | outerVertices.append(outerVertex); |
| 1984 | } |
| 1985 | } |
| 1986 | prevInner = inner; |
| 1987 | prevOuter = outer; |
| 1988 | prevV = v; |
| 1989 | prevEdge = e; |
| 1990 | prevNormal = normal; |
| 1991 | } |
| 1992 | if (!inversion(innerVertices.fTail, innerVertices.fHead, prevEdge, c)) { |
| 1993 | innerInversion = false; |
| 1994 | } |
| 1995 | if (!inversion(outerVertices.fTail, outerVertices.fHead, prevEdge, c)) { |
| 1996 | outerInversion = false; |
| 1997 | } |
| 1998 | // Outer edges get 1 winding, and inner edges get -2 winding. This ensures that the interior |
| 1999 | // is always filled (1 + -2 = -1 for normal cases, 1 + 2 = 3 for thin features where the |
| 2000 | // interior inverts). |
| 2001 | // For total inversion cases, the shape has now reversed handedness, so invert the winding |
| 2002 | // so it will be detected during collapse_overlap_regions(). |
| 2003 | int innerWinding = innerInversion ? 2 : -2; |
| 2004 | int outerWinding = outerInversion ? -1 : 1; |
| 2005 | for (Vertex* v = innerVertices.fHead; v && v->fNext; v = v->fNext) { |
| 2006 | connect(v, v->fNext, Edge::Type::kInner, c, alloc, innerWinding); |
| 2007 | } |
| 2008 | connect(innerVertices.fTail, innerVertices.fHead, Edge::Type::kInner, c, alloc, innerWinding); |
| 2009 | for (Vertex* v = outerVertices.fHead; v && v->fNext; v = v->fNext) { |
| 2010 | connect(v, v->fNext, Edge::Type::kOuter, c, alloc, outerWinding); |
| 2011 | } |
| 2012 | connect(outerVertices.fTail, outerVertices.fHead, Edge::Type::kOuter, c, alloc, outerWinding); |
| 2013 | innerMesh->append(innerVertices); |
| 2014 | outerMesh->append(outerVertices); |
| 2015 | } |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2016 | |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 2017 | void extract_boundary(EdgeList* boundary, Edge* e, SkPath::FillType fillType, SkArenaAlloc& alloc) { |
Stephen White | 95152e1 | 2017-12-18 10:52:44 -0500 | [diff] [blame] | 2018 | LOG("\nextracting boundary\n"); |
Stephen White | 4978906 | 2017-02-21 10:35:49 -0500 | [diff] [blame] | 2019 | bool down = apply_fill_type(fillType, e->fWinding); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2020 | while (e) { |
| 2021 | e->fWinding = down ? 1 : -1; |
| 2022 | Edge* next; |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 2023 | e->fLine.normalize(); |
| 2024 | e->fLine = e->fLine * e->fWinding; |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2025 | boundary->append(e); |
| 2026 | if (down) { |
| 2027 | // Find outgoing edge, in clockwise order. |
| 2028 | if ((next = e->fNextEdgeAbove)) { |
| 2029 | down = false; |
| 2030 | } else if ((next = e->fBottom->fLastEdgeBelow)) { |
| 2031 | down = true; |
| 2032 | } else if ((next = e->fPrevEdgeAbove)) { |
| 2033 | down = false; |
| 2034 | } |
| 2035 | } else { |
| 2036 | // Find outgoing edge, in counter-clockwise order. |
| 2037 | if ((next = e->fPrevEdgeBelow)) { |
| 2038 | down = true; |
| 2039 | } else if ((next = e->fTop->fFirstEdgeAbove)) { |
| 2040 | down = false; |
| 2041 | } else if ((next = e->fNextEdgeBelow)) { |
| 2042 | down = true; |
| 2043 | } |
| 2044 | } |
Stephen White | e7a364d | 2017-01-11 16:19:26 -0500 | [diff] [blame] | 2045 | disconnect(e); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2046 | e = next; |
| 2047 | } |
| 2048 | } |
| 2049 | |
Stephen White | 5ad721e | 2017-02-23 16:50:47 -0500 | [diff] [blame] | 2050 | // Stage 5b: Extract boundaries from mesh, simplify and stroke them into a new mesh. |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2051 | |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2052 | void extract_boundaries(const VertexList& inMesh, VertexList* innerVertices, |
| 2053 | VertexList* outerVertices, SkPath::FillType fillType, |
Stephen White | 5ad721e | 2017-02-23 16:50:47 -0500 | [diff] [blame] | 2054 | Comparator& c, SkArenaAlloc& alloc) { |
| 2055 | remove_non_boundary_edges(inMesh, fillType, alloc); |
| 2056 | for (Vertex* v = inMesh.fHead; v; v = v->fNext) { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2057 | while (v->fFirstEdgeBelow) { |
Stephen White | 5ad721e | 2017-02-23 16:50:47 -0500 | [diff] [blame] | 2058 | EdgeList boundary; |
| 2059 | extract_boundary(&boundary, v->fFirstEdgeBelow, fillType, alloc); |
| 2060 | simplify_boundary(&boundary, c, alloc); |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2061 | stroke_boundary(&boundary, innerVertices, outerVertices, c, alloc); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2062 | } |
| 2063 | } |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2064 | } |
| 2065 | |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2066 | // This is a driver function that calls stages 2-5 in turn. |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2067 | |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 2068 | void contours_to_mesh(VertexList* contours, int contourCnt, bool antialias, |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 2069 | VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2070 | #if LOGGING_ENABLED |
| 2071 | for (int i = 0; i < contourCnt; ++i) { |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 2072 | Vertex* v = contours[i].fHead; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2073 | SkASSERT(v); |
| 2074 | LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY); |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 2075 | for (v = v->fNext; v; v = v->fNext) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2076 | LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY); |
| 2077 | } |
| 2078 | } |
| 2079 | #endif |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2080 | sanitize_contours(contours, contourCnt, antialias); |
Stephen White | bf6137e | 2017-01-04 15:43:26 -0500 | [diff] [blame] | 2081 | build_edges(contours, contourCnt, mesh, c, alloc); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2082 | } |
| 2083 | |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2084 | void sort_mesh(VertexList* vertices, Comparator& c, SkArenaAlloc& alloc) { |
Stephen White | bf6137e | 2017-01-04 15:43:26 -0500 | [diff] [blame] | 2085 | if (!vertices || !vertices->fHead) { |
Stephen White | 2f4686f | 2017-01-03 16:20:01 -0500 | [diff] [blame] | 2086 | return; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2087 | } |
| 2088 | |
| 2089 | // Sort vertices in Y (secondarily in X). |
Stephen White | 16a40cb | 2017-02-23 11:10:01 -0500 | [diff] [blame] | 2090 | if (c.fDirection == Comparator::Direction::kHorizontal) { |
| 2091 | merge_sort<sweep_lt_horiz>(vertices); |
| 2092 | } else { |
| 2093 | merge_sort<sweep_lt_vert>(vertices); |
| 2094 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2095 | #if LOGGING_ENABLED |
Stephen White | 2e2cb9b | 2017-01-09 13:11:18 -0500 | [diff] [blame] | 2096 | for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2097 | static float gID = 0.0f; |
| 2098 | v->fID = gID++; |
| 2099 | } |
| 2100 | #endif |
Stephen White | 2f4686f | 2017-01-03 16:20:01 -0500 | [diff] [blame] | 2101 | } |
| 2102 | |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 2103 | Poly* contours_to_polys(VertexList* contours, int contourCnt, SkPath::FillType fillType, |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2104 | const SkRect& pathBounds, bool antialias, VertexList* outerMesh, |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 2105 | SkArenaAlloc& alloc) { |
Stephen White | 16a40cb | 2017-02-23 11:10:01 -0500 | [diff] [blame] | 2106 | Comparator c(pathBounds.width() > pathBounds.height() ? Comparator::Direction::kHorizontal |
| 2107 | : Comparator::Direction::kVertical); |
Stephen White | bf6137e | 2017-01-04 15:43:26 -0500 | [diff] [blame] | 2108 | VertexList mesh; |
| 2109 | contours_to_mesh(contours, contourCnt, antialias, &mesh, c, alloc); |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2110 | sort_mesh(&mesh, c, alloc); |
| 2111 | merge_coincident_vertices(&mesh, c, alloc); |
Stephen White | 0cb3167 | 2017-06-08 14:41:01 -0400 | [diff] [blame] | 2112 | simplify(&mesh, c, alloc); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2113 | if (antialias) { |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2114 | VertexList innerMesh; |
| 2115 | extract_boundaries(mesh, &innerMesh, outerMesh, fillType, c, alloc); |
| 2116 | sort_mesh(&innerMesh, c, alloc); |
| 2117 | sort_mesh(outerMesh, c, alloc); |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 2118 | merge_coincident_vertices(&innerMesh, c, alloc); |
| 2119 | bool was_complex = merge_coincident_vertices(outerMesh, c, alloc); |
| 2120 | was_complex = simplify(&innerMesh, c, alloc) || was_complex; |
| 2121 | was_complex = simplify(outerMesh, c, alloc) || was_complex; |
| 2122 | LOG("\ninner mesh before:\n"); |
| 2123 | dump_mesh(innerMesh); |
| 2124 | LOG("\nouter mesh before:\n"); |
| 2125 | dump_mesh(*outerMesh); |
| 2126 | was_complex = collapse_overlap_regions(&innerMesh, c, alloc) || was_complex; |
| 2127 | was_complex = collapse_overlap_regions(outerMesh, c, alloc) || was_complex; |
| 2128 | if (was_complex) { |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2129 | LOG("found complex mesh; taking slow path\n"); |
| 2130 | VertexList aaMesh; |
Stephen White | 95152e1 | 2017-12-18 10:52:44 -0500 | [diff] [blame] | 2131 | LOG("\ninner mesh after:\n"); |
| 2132 | dump_mesh(innerMesh); |
| 2133 | LOG("\nouter mesh after:\n"); |
| 2134 | dump_mesh(*outerMesh); |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2135 | connect_partners(outerMesh, c, alloc); |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 2136 | connect_partners(&innerMesh, c, alloc); |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2137 | sorted_merge(&innerMesh, outerMesh, &aaMesh, c); |
| 2138 | merge_coincident_vertices(&aaMesh, c, alloc); |
Stephen White | 0cb3167 | 2017-06-08 14:41:01 -0400 | [diff] [blame] | 2139 | simplify(&aaMesh, c, alloc); |
Stephen White | 95152e1 | 2017-12-18 10:52:44 -0500 | [diff] [blame] | 2140 | dump_mesh(aaMesh); |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2141 | outerMesh->fHead = outerMesh->fTail = nullptr; |
| 2142 | return tessellate(aaMesh, alloc); |
| 2143 | } else { |
| 2144 | LOG("no complex polygons; taking fast path\n"); |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2145 | return tessellate(innerMesh, alloc); |
| 2146 | } |
Stephen White | 4978906 | 2017-02-21 10:35:49 -0500 | [diff] [blame] | 2147 | } else { |
| 2148 | return tessellate(mesh, alloc); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2149 | } |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2150 | } |
| 2151 | |
| 2152 | // Stage 6: Triangulate the monotone polygons into a vertex buffer. |
| 2153 | void* polys_to_triangles(Poly* polys, SkPath::FillType fillType, const AAParams* aaParams, |
| 2154 | void* data) { |
| 2155 | for (Poly* poly = polys; poly; poly = poly->fNext) { |
| 2156 | if (apply_fill_type(fillType, poly)) { |
| 2157 | data = poly->emit(aaParams, data); |
| 2158 | } |
| 2159 | } |
| 2160 | return data; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2161 | } |
| 2162 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2163 | Poly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds, |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2164 | int contourCnt, SkArenaAlloc& alloc, bool antialias, bool* isLinear, |
| 2165 | VertexList* outerMesh) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2166 | SkPath::FillType fillType = path.getFillType(); |
| 2167 | if (SkPath::IsInverseFillType(fillType)) { |
| 2168 | contourCnt++; |
| 2169 | } |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 2170 | std::unique_ptr<VertexList[]> contours(new VertexList[contourCnt]); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2171 | |
| 2172 | path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinear); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2173 | return contours_to_polys(contours.get(), contourCnt, path.getFillType(), path.getBounds(), |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2174 | antialias, outerMesh, alloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2175 | } |
| 2176 | |
Stephen White | 11f65e0 | 2017-02-16 19:00:39 -0500 | [diff] [blame] | 2177 | int get_contour_count(const SkPath& path, SkScalar tolerance) { |
| 2178 | int contourCnt; |
| 2179 | int maxPts = GrPathUtils::worstCasePointCount(path, &contourCnt, tolerance); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2180 | if (maxPts <= 0) { |
Stephen White | 11f65e0 | 2017-02-16 19:00:39 -0500 | [diff] [blame] | 2181 | return 0; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2182 | } |
Stephen White | 11f65e0 | 2017-02-16 19:00:39 -0500 | [diff] [blame] | 2183 | return contourCnt; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2184 | } |
| 2185 | |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 2186 | int64_t count_points(Poly* polys, SkPath::FillType fillType) { |
| 2187 | int64_t count = 0; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2188 | for (Poly* poly = polys; poly; poly = poly->fNext) { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2189 | if (apply_fill_type(fillType, poly) && poly->fCount >= 3) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2190 | count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3); |
| 2191 | } |
| 2192 | } |
| 2193 | return count; |
| 2194 | } |
| 2195 | |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 2196 | int64_t count_outer_mesh_points(const VertexList& outerMesh) { |
| 2197 | int64_t count = 0; |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2198 | for (Vertex* v = outerMesh.fHead; v; v = v->fNext) { |
| 2199 | for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { |
| 2200 | count += TESSELLATOR_WIREFRAME ? 12 : 6; |
| 2201 | } |
| 2202 | } |
| 2203 | return count; |
| 2204 | } |
| 2205 | |
| 2206 | void* outer_mesh_to_triangles(const VertexList& outerMesh, const AAParams* aaParams, void* data) { |
| 2207 | for (Vertex* v = outerMesh.fHead; v; v = v->fNext) { |
| 2208 | for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { |
| 2209 | Vertex* v0 = e->fTop; |
| 2210 | Vertex* v1 = e->fBottom; |
| 2211 | Vertex* v2 = e->fBottom->fPartner; |
| 2212 | Vertex* v3 = e->fTop->fPartner; |
| 2213 | data = emit_triangle(v0, v1, v2, aaParams, data); |
| 2214 | data = emit_triangle(v0, v2, v3, aaParams, data); |
| 2215 | } |
| 2216 | } |
| 2217 | return data; |
| 2218 | } |
| 2219 | |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2220 | } // namespace |
| 2221 | |
| 2222 | namespace GrTessellator { |
| 2223 | |
| 2224 | // Stage 6: Triangulate the monotone polygons into a vertex buffer. |
| 2225 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2226 | int PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds, |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2227 | VertexAllocator* vertexAllocator, bool antialias, const GrColor& color, |
| 2228 | bool canTweakAlphaForCoverage, bool* isLinear) { |
Stephen White | 11f65e0 | 2017-02-16 19:00:39 -0500 | [diff] [blame] | 2229 | int contourCnt = get_contour_count(path, tolerance); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2230 | if (contourCnt <= 0) { |
| 2231 | *isLinear = true; |
| 2232 | return 0; |
| 2233 | } |
Stephen White | 11f65e0 | 2017-02-16 19:00:39 -0500 | [diff] [blame] | 2234 | SkArenaAlloc alloc(kArenaChunkSize); |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2235 | VertexList outerMesh; |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2236 | Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, antialias, |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2237 | isLinear, &outerMesh); |
senorblanco | 7ab96e9 | 2016-10-12 06:47:44 -0700 | [diff] [blame] | 2238 | SkPath::FillType fillType = antialias ? SkPath::kWinding_FillType : path.getFillType(); |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 2239 | int64_t count64 = count_points(polys, fillType); |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2240 | if (antialias) { |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 2241 | count64 += count_outer_mesh_points(outerMesh); |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2242 | } |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 2243 | if (0 == count64 || count64 > SK_MaxS32) { |
Stephen White | ff60b17 | 2017-05-05 15:54:52 -0400 | [diff] [blame] | 2244 | return 0; |
| 2245 | } |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 2246 | int count = count64; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2247 | |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2248 | void* verts = vertexAllocator->lock(count); |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 2249 | if (!verts) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2250 | SkDebugf("Could not allocate vertices\n"); |
| 2251 | return 0; |
| 2252 | } |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2253 | |
| 2254 | LOG("emitting %d verts\n", count); |
| 2255 | AAParams aaParams; |
| 2256 | aaParams.fTweakAlpha = canTweakAlphaForCoverage; |
| 2257 | aaParams.fColor = color; |
| 2258 | |
| 2259 | void* end = polys_to_triangles(polys, fillType, antialias ? &aaParams : nullptr, verts); |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2260 | end = outer_mesh_to_triangles(outerMesh, &aaParams, end); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2261 | int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts)) |
| 2262 | / vertexAllocator->stride()); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2263 | SkASSERT(actualCount <= count); |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 2264 | vertexAllocator->unlock(actualCount); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2265 | return actualCount; |
| 2266 | } |
| 2267 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2268 | int PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds, |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2269 | GrTessellator::WindingVertex** verts) { |
Stephen White | 11f65e0 | 2017-02-16 19:00:39 -0500 | [diff] [blame] | 2270 | int contourCnt = get_contour_count(path, tolerance); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2271 | if (contourCnt <= 0) { |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 2272 | *verts = nullptr; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2273 | return 0; |
| 2274 | } |
Stephen White | 11f65e0 | 2017-02-16 19:00:39 -0500 | [diff] [blame] | 2275 | SkArenaAlloc alloc(kArenaChunkSize); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2276 | bool isLinear; |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2277 | Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, false, &isLinear, |
| 2278 | nullptr); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2279 | SkPath::FillType fillType = path.getFillType(); |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 2280 | int64_t count64 = count_points(polys, fillType); |
| 2281 | if (0 == count64 || count64 > SK_MaxS32) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2282 | *verts = nullptr; |
| 2283 | return 0; |
| 2284 | } |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 2285 | int count = count64; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2286 | |
| 2287 | *verts = new GrTessellator::WindingVertex[count]; |
| 2288 | GrTessellator::WindingVertex* vertsEnd = *verts; |
| 2289 | SkPoint* points = new SkPoint[count]; |
| 2290 | SkPoint* pointsEnd = points; |
| 2291 | for (Poly* poly = polys; poly; poly = poly->fNext) { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2292 | if (apply_fill_type(fillType, poly)) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2293 | SkPoint* start = pointsEnd; |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2294 | pointsEnd = static_cast<SkPoint*>(poly->emit(nullptr, pointsEnd)); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2295 | while (start != pointsEnd) { |
| 2296 | vertsEnd->fPos = *start; |
| 2297 | vertsEnd->fWinding = poly->fWinding; |
| 2298 | ++start; |
| 2299 | ++vertsEnd; |
| 2300 | } |
| 2301 | } |
| 2302 | } |
| 2303 | int actualCount = static_cast<int>(vertsEnd - *verts); |
| 2304 | SkASSERT(actualCount <= count); |
| 2305 | SkASSERT(pointsEnd - points == actualCount); |
| 2306 | delete[] points; |
| 2307 | return actualCount; |
| 2308 | } |
| 2309 | |
| 2310 | } // namespace |