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