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