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