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