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 | } |
Stephen White | 8a3c059 | 2019-05-29 11:26:16 -0400 | [diff] [blame] | 541 | Vertex* top = edge->fEdge->fTop; |
| 542 | Vertex* bottom = edge->fEdge->fBottom; |
| 543 | if (!top || !bottom ) { |
| 544 | return; |
| 545 | } |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 546 | Line line = edge->fEdge->fLine; |
| 547 | line.fC = -(dest->fPoint.fX * line.fA + dest->fPoint.fY * line.fB); |
| 548 | Edge bisector(v, v->fPartner, 1, Edge::Type::kConnector); |
| 549 | SkPoint p; |
| 550 | uint8_t alpha = dest->fAlpha; |
Stephen White | 8a3c059 | 2019-05-29 11:26:16 -0400 | [diff] [blame] | 551 | if (line.intersect(bisector.fLine, &p) && !c.sweep_lt(p, top->fPoint) && |
| 552 | c.sweep_lt(p, bottom->fPoint)) { |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 553 | LOG("found p edge event for %g, %g (original %g -> %g), will collapse to %g,%g alpha %d\n", |
Stephen White | 8a3c059 | 2019-05-29 11:26:16 -0400 | [diff] [blame] | 554 | dest->fID, v->fID, top->fID, bottom->fID, p.fX, p.fY, alpha); |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 555 | edge->fEvent = alloc.make<Event>(edge, p, alpha); |
| 556 | events->push(edge->fEvent); |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 557 | } |
| 558 | } |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 559 | |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 560 | /***************************************************************************************/ |
| 561 | |
| 562 | struct Poly { |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 563 | Poly(Vertex* v, int winding) |
| 564 | : fFirstVertex(v) |
| 565 | , fWinding(winding) |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 566 | , fHead(nullptr) |
| 567 | , fTail(nullptr) |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 568 | , fNext(nullptr) |
| 569 | , fPartner(nullptr) |
| 570 | , fCount(0) |
| 571 | { |
| 572 | #if LOGGING_ENABLED |
| 573 | static int gID = 0; |
| 574 | fID = gID++; |
| 575 | LOG("*** created Poly %d\n", fID); |
| 576 | #endif |
| 577 | } |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 578 | typedef enum { kLeft_Side, kRight_Side } Side; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 579 | struct MonotonePoly { |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 580 | MonotonePoly(Edge* edge, Side side) |
| 581 | : fSide(side) |
| 582 | , fFirstEdge(nullptr) |
| 583 | , fLastEdge(nullptr) |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 584 | , fPrev(nullptr) |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 585 | , fNext(nullptr) { |
| 586 | this->addEdge(edge); |
| 587 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 588 | Side fSide; |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 589 | Edge* fFirstEdge; |
| 590 | Edge* fLastEdge; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 591 | MonotonePoly* fPrev; |
| 592 | MonotonePoly* fNext; |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 593 | void addEdge(Edge* edge) { |
senorblanco | e6eaa32 | 2016-03-08 09:06:44 -0800 | [diff] [blame] | 594 | if (fSide == kRight_Side) { |
senorblanco | 212c7c3 | 2016-08-18 10:20:47 -0700 | [diff] [blame] | 595 | SkASSERT(!edge->fUsedInRightPoly); |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 596 | list_insert<Edge, &Edge::fRightPolyPrev, &Edge::fRightPolyNext>( |
| 597 | edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge); |
senorblanco | 70f5251 | 2016-08-17 14:56:22 -0700 | [diff] [blame] | 598 | edge->fUsedInRightPoly = true; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 599 | } else { |
senorblanco | 212c7c3 | 2016-08-18 10:20:47 -0700 | [diff] [blame] | 600 | SkASSERT(!edge->fUsedInLeftPoly); |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 601 | list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>( |
| 602 | edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge); |
senorblanco | 70f5251 | 2016-08-17 14:56:22 -0700 | [diff] [blame] | 603 | edge->fUsedInLeftPoly = true; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 604 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 605 | } |
| 606 | |
Brian Osman | 0995fd5 | 2019-01-09 09:52:25 -0500 | [diff] [blame] | 607 | void* emit(bool emitCoverage, void* data) { |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 608 | Edge* e = fFirstEdge; |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 609 | VertexList vertices; |
| 610 | vertices.append(e->fTop); |
Stephen White | 651cbe9 | 2017-03-03 12:24:16 -0500 | [diff] [blame] | 611 | int count = 1; |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 612 | while (e != nullptr) { |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 613 | if (kRight_Side == fSide) { |
| 614 | vertices.append(e->fBottom); |
| 615 | e = e->fRightPolyNext; |
| 616 | } else { |
| 617 | vertices.prepend(e->fBottom); |
| 618 | e = e->fLeftPolyNext; |
| 619 | } |
Stephen White | 651cbe9 | 2017-03-03 12:24:16 -0500 | [diff] [blame] | 620 | count++; |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 621 | } |
| 622 | Vertex* first = vertices.fHead; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 623 | Vertex* v = first->fNext; |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 624 | while (v != vertices.fTail) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 625 | SkASSERT(v && v->fPrev && v->fNext); |
| 626 | Vertex* prev = v->fPrev; |
| 627 | Vertex* curr = v; |
| 628 | Vertex* next = v->fNext; |
Stephen White | 651cbe9 | 2017-03-03 12:24:16 -0500 | [diff] [blame] | 629 | if (count == 3) { |
Brian Osman | 0995fd5 | 2019-01-09 09:52:25 -0500 | [diff] [blame] | 630 | return emit_triangle(prev, curr, next, emitCoverage, data); |
Stephen White | 651cbe9 | 2017-03-03 12:24:16 -0500 | [diff] [blame] | 631 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 632 | double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX; |
| 633 | double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY; |
| 634 | double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX; |
| 635 | double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY; |
| 636 | if (ax * by - ay * bx >= 0.0) { |
Brian Osman | 0995fd5 | 2019-01-09 09:52:25 -0500 | [diff] [blame] | 637 | data = emit_triangle(prev, curr, next, emitCoverage, data); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 638 | v->fPrev->fNext = v->fNext; |
| 639 | v->fNext->fPrev = v->fPrev; |
Stephen White | 651cbe9 | 2017-03-03 12:24:16 -0500 | [diff] [blame] | 640 | count--; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 641 | if (v->fPrev == first) { |
| 642 | v = v->fNext; |
| 643 | } else { |
| 644 | v = v->fPrev; |
| 645 | } |
| 646 | } else { |
| 647 | v = v->fNext; |
| 648 | } |
| 649 | } |
| 650 | return data; |
| 651 | } |
| 652 | }; |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 653 | Poly* addEdge(Edge* e, Side side, SkArenaAlloc& alloc) { |
senorblanco | 70f5251 | 2016-08-17 14:56:22 -0700 | [diff] [blame] | 654 | LOG("addEdge (%g -> %g) to poly %d, %s side\n", |
| 655 | e->fTop->fID, e->fBottom->fID, fID, side == kLeft_Side ? "left" : "right"); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 656 | Poly* partner = fPartner; |
| 657 | Poly* poly = this; |
senorblanco | 212c7c3 | 2016-08-18 10:20:47 -0700 | [diff] [blame] | 658 | if (side == kRight_Side) { |
| 659 | if (e->fUsedInRightPoly) { |
| 660 | return this; |
| 661 | } |
| 662 | } else { |
| 663 | if (e->fUsedInLeftPoly) { |
| 664 | return this; |
| 665 | } |
| 666 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 667 | if (partner) { |
| 668 | fPartner = partner->fPartner = nullptr; |
| 669 | } |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 670 | if (!fTail) { |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 671 | fHead = fTail = alloc.make<MonotonePoly>(e, side); |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 672 | fCount += 2; |
senorblanco | 93e3fff | 2016-06-07 12:36:00 -0700 | [diff] [blame] | 673 | } else if (e->fBottom == fTail->fLastEdge->fBottom) { |
| 674 | return poly; |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 675 | } else if (side == fTail->fSide) { |
| 676 | fTail->addEdge(e); |
| 677 | fCount++; |
| 678 | } else { |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 679 | e = alloc.make<Edge>(fTail->fLastEdge->fBottom, e->fBottom, 1, Edge::Type::kInner); |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 680 | fTail->addEdge(e); |
| 681 | fCount++; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 682 | if (partner) { |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 683 | partner->addEdge(e, side, alloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 684 | poly = partner; |
| 685 | } else { |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 686 | MonotonePoly* m = alloc.make<MonotonePoly>(e, side); |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 687 | m->fPrev = fTail; |
| 688 | fTail->fNext = m; |
| 689 | fTail = m; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 690 | } |
| 691 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 692 | return poly; |
| 693 | } |
Brian Osman | 0995fd5 | 2019-01-09 09:52:25 -0500 | [diff] [blame] | 694 | void* emit(bool emitCoverage, void *data) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 695 | if (fCount < 3) { |
| 696 | return data; |
| 697 | } |
| 698 | LOG("emit() %d, size %d\n", fID, fCount); |
| 699 | for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) { |
Brian Osman | 0995fd5 | 2019-01-09 09:52:25 -0500 | [diff] [blame] | 700 | data = m->emit(emitCoverage, data); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 701 | } |
| 702 | return data; |
| 703 | } |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 704 | Vertex* lastVertex() const { return fTail ? fTail->fLastEdge->fBottom : fFirstVertex; } |
| 705 | Vertex* fFirstVertex; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 706 | int fWinding; |
| 707 | MonotonePoly* fHead; |
| 708 | MonotonePoly* fTail; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 709 | Poly* fNext; |
| 710 | Poly* fPartner; |
| 711 | int fCount; |
| 712 | #if LOGGING_ENABLED |
| 713 | int fID; |
| 714 | #endif |
| 715 | }; |
| 716 | |
| 717 | /***************************************************************************************/ |
| 718 | |
| 719 | bool coincident(const SkPoint& a, const SkPoint& b) { |
| 720 | return a == b; |
| 721 | } |
| 722 | |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 723 | Poly* new_poly(Poly** head, Vertex* v, int winding, SkArenaAlloc& alloc) { |
| 724 | Poly* poly = alloc.make<Poly>(v, winding); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 725 | poly->fNext = *head; |
| 726 | *head = poly; |
| 727 | return poly; |
| 728 | } |
| 729 | |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 730 | void append_point_to_contour(const SkPoint& p, VertexList* contour, SkArenaAlloc& alloc) { |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 731 | Vertex* v = alloc.make<Vertex>(p, 255); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 732 | #if LOGGING_ENABLED |
| 733 | static float gID = 0.0f; |
| 734 | v->fID = gID++; |
| 735 | #endif |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 736 | contour->append(v); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 737 | } |
| 738 | |
Stephen White | 36e4f06 | 2017-03-27 16:11:31 -0400 | [diff] [blame] | 739 | SkScalar quad_error_at(const SkPoint pts[3], SkScalar t, SkScalar u) { |
| 740 | SkQuadCoeff quad(pts); |
| 741 | SkPoint p0 = to_point(quad.eval(t - 0.5f * u)); |
| 742 | SkPoint mid = to_point(quad.eval(t)); |
| 743 | SkPoint p1 = to_point(quad.eval(t + 0.5f * u)); |
Stephen White | e3a0be7 | 2017-06-12 11:43:18 -0400 | [diff] [blame] | 744 | if (!p0.isFinite() || !mid.isFinite() || !p1.isFinite()) { |
| 745 | return 0; |
| 746 | } |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 747 | return SkPointPriv::DistanceToLineSegmentBetweenSqd(mid, p0, p1); |
Stephen White | 36e4f06 | 2017-03-27 16:11:31 -0400 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | void append_quadratic_to_contour(const SkPoint pts[3], SkScalar toleranceSqd, VertexList* contour, |
| 751 | SkArenaAlloc& alloc) { |
| 752 | SkQuadCoeff quad(pts); |
| 753 | Sk2s aa = quad.fA * quad.fA; |
| 754 | SkScalar denom = 2.0f * (aa[0] + aa[1]); |
| 755 | Sk2s ab = quad.fA * quad.fB; |
| 756 | SkScalar t = denom ? (-ab[0] - ab[1]) / denom : 0.0f; |
| 757 | int nPoints = 1; |
Stephen White | e40c361 | 2018-01-09 11:49:08 -0500 | [diff] [blame] | 758 | SkScalar u = 1.0f; |
Stephen White | 36e4f06 | 2017-03-27 16:11:31 -0400 | [diff] [blame] | 759 | // Test possible subdivision values only at the point of maximum curvature. |
| 760 | // If it passes the flatness metric there, it'll pass everywhere. |
Stephen White | e40c361 | 2018-01-09 11:49:08 -0500 | [diff] [blame] | 761 | while (nPoints < GrPathUtils::kMaxPointsPerCurve) { |
Stephen White | 36e4f06 | 2017-03-27 16:11:31 -0400 | [diff] [blame] | 762 | u = 1.0f / nPoints; |
| 763 | if (quad_error_at(pts, t, u) < toleranceSqd) { |
| 764 | break; |
| 765 | } |
| 766 | nPoints++; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 767 | } |
Stephen White | 36e4f06 | 2017-03-27 16:11:31 -0400 | [diff] [blame] | 768 | for (int j = 1; j <= nPoints; j++) { |
| 769 | append_point_to_contour(to_point(quad.eval(j * u)), contour, alloc); |
| 770 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 771 | } |
| 772 | |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 773 | void generate_cubic_points(const SkPoint& p0, |
| 774 | const SkPoint& p1, |
| 775 | const SkPoint& p2, |
| 776 | const SkPoint& p3, |
| 777 | SkScalar tolSqd, |
| 778 | VertexList* contour, |
| 779 | int pointsLeft, |
| 780 | SkArenaAlloc& alloc) { |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 781 | SkScalar d1 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p1, p0, p3); |
| 782 | SkScalar d2 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p2, p0, p3); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 783 | if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) || |
| 784 | !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) { |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 785 | append_point_to_contour(p3, contour, alloc); |
| 786 | return; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 787 | } |
| 788 | const SkPoint q[] = { |
| 789 | { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) }, |
| 790 | { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) }, |
| 791 | { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) } |
| 792 | }; |
| 793 | const SkPoint r[] = { |
| 794 | { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) }, |
| 795 | { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) } |
| 796 | }; |
| 797 | const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) }; |
| 798 | pointsLeft >>= 1; |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 799 | generate_cubic_points(p0, q[0], r[0], s, tolSqd, contour, pointsLeft, alloc); |
| 800 | generate_cubic_points(s, r[1], q[2], p3, tolSqd, contour, pointsLeft, alloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | // Stage 1: convert the input path to a set of linear contours (linked list of Vertices). |
| 804 | |
| 805 | void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds, |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 806 | VertexList* contours, SkArenaAlloc& alloc, bool *isLinear) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 807 | SkScalar toleranceSqd = tolerance * tolerance; |
| 808 | |
| 809 | SkPoint pts[4]; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 810 | *isLinear = true; |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 811 | VertexList* contour = contours; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 812 | SkPath::Iter iter(path, false); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 813 | if (path.isInverseFillType()) { |
| 814 | SkPoint quad[4]; |
| 815 | clipBounds.toQuad(quad); |
senorblanco | 7ab96e9 | 2016-10-12 06:47:44 -0700 | [diff] [blame] | 816 | for (int i = 3; i >= 0; i--) { |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 817 | append_point_to_contour(quad[i], contours, alloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 818 | } |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 819 | contour++; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 820 | } |
| 821 | SkAutoConicToQuads converter; |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 822 | SkPath::Verb verb; |
Stephen White | 2cee283 | 2017-08-23 09:37:16 -0400 | [diff] [blame] | 823 | while ((verb = iter.next(pts, false)) != SkPath::kDone_Verb) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 824 | switch (verb) { |
| 825 | case SkPath::kConic_Verb: { |
| 826 | SkScalar weight = iter.conicWeight(); |
| 827 | const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd); |
| 828 | for (int i = 0; i < converter.countQuads(); ++i) { |
Stephen White | 36e4f06 | 2017-03-27 16:11:31 -0400 | [diff] [blame] | 829 | append_quadratic_to_contour(quadPts, toleranceSqd, contour, alloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 830 | quadPts += 2; |
| 831 | } |
| 832 | *isLinear = false; |
| 833 | break; |
| 834 | } |
| 835 | case SkPath::kMove_Verb: |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 836 | if (contour->fHead) { |
| 837 | contour++; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 838 | } |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 839 | append_point_to_contour(pts[0], contour, alloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 840 | break; |
| 841 | case SkPath::kLine_Verb: { |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 842 | append_point_to_contour(pts[1], contour, alloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 843 | break; |
| 844 | } |
| 845 | case SkPath::kQuad_Verb: { |
Stephen White | 36e4f06 | 2017-03-27 16:11:31 -0400 | [diff] [blame] | 846 | append_quadratic_to_contour(pts, toleranceSqd, contour, alloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 847 | *isLinear = false; |
| 848 | break; |
| 849 | } |
| 850 | case SkPath::kCubic_Verb: { |
| 851 | int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance); |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 852 | generate_cubic_points(pts[0], pts[1], pts[2], pts[3], toleranceSqd, contour, |
| 853 | pointsLeft, alloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 854 | *isLinear = false; |
| 855 | break; |
| 856 | } |
| 857 | case SkPath::kClose_Verb: |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 858 | case SkPath::kDone_Verb: |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 859 | break; |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | |
Stephen White | 4978906 | 2017-02-21 10:35:49 -0500 | [diff] [blame] | 864 | inline bool apply_fill_type(SkPath::FillType fillType, int winding) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 865 | switch (fillType) { |
| 866 | case SkPath::kWinding_FillType: |
| 867 | return winding != 0; |
| 868 | case SkPath::kEvenOdd_FillType: |
| 869 | return (winding & 1) != 0; |
| 870 | case SkPath::kInverseWinding_FillType: |
senorblanco | 7ab96e9 | 2016-10-12 06:47:44 -0700 | [diff] [blame] | 871 | return winding == 1; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 872 | case SkPath::kInverseEvenOdd_FillType: |
| 873 | return (winding & 1) == 1; |
| 874 | default: |
| 875 | SkASSERT(false); |
| 876 | return false; |
| 877 | } |
| 878 | } |
| 879 | |
Stephen White | 4978906 | 2017-02-21 10:35:49 -0500 | [diff] [blame] | 880 | inline bool apply_fill_type(SkPath::FillType fillType, Poly* poly) { |
| 881 | return poly && apply_fill_type(fillType, poly->fWinding); |
| 882 | } |
| 883 | |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 884 | 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] | 885 | int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 886 | Vertex* top = winding < 0 ? next : prev; |
| 887 | Vertex* bottom = winding < 0 ? prev : next; |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 888 | return alloc.make<Edge>(top, bottom, winding, type); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | void remove_edge(Edge* edge, EdgeList* edges) { |
| 892 | LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 893 | SkASSERT(edges->contains(edge)); |
| 894 | edges->remove(edge); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | void insert_edge(Edge* edge, Edge* prev, EdgeList* edges) { |
| 898 | LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 899 | SkASSERT(!edges->contains(edge)); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 900 | Edge* next = prev ? prev->fRight : edges->fHead; |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 901 | edges->insert(edge, prev, next); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | void find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) { |
Stephen White | 90732fd | 2017-03-02 16:16:33 -0500 | [diff] [blame] | 905 | if (v->fFirstEdgeAbove && v->fLastEdgeAbove) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 906 | *left = v->fFirstEdgeAbove->fLeft; |
| 907 | *right = v->fLastEdgeAbove->fRight; |
| 908 | return; |
| 909 | } |
| 910 | Edge* next = nullptr; |
| 911 | Edge* prev; |
| 912 | for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) { |
| 913 | if (prev->isLeftOf(v)) { |
| 914 | break; |
| 915 | } |
| 916 | next = prev; |
| 917 | } |
| 918 | *left = prev; |
| 919 | *right = next; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 920 | } |
| 921 | |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 922 | void insert_edge_above(Edge* edge, Vertex* v, Comparator& c) { |
| 923 | if (edge->fTop->fPoint == edge->fBottom->fPoint || |
Stephen White | e30cf80 | 2017-02-27 11:37:55 -0500 | [diff] [blame] | 924 | c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 925 | return; |
| 926 | } |
| 927 | LOG("insert edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID); |
| 928 | Edge* prev = nullptr; |
| 929 | Edge* next; |
| 930 | for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) { |
| 931 | if (next->isRightOf(edge->fTop)) { |
| 932 | break; |
| 933 | } |
| 934 | prev = next; |
| 935 | } |
senorblanco | e6eaa32 | 2016-03-08 09:06:44 -0800 | [diff] [blame] | 936 | list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>( |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 937 | edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove); |
| 938 | } |
| 939 | |
| 940 | void insert_edge_below(Edge* edge, Vertex* v, Comparator& c) { |
| 941 | if (edge->fTop->fPoint == edge->fBottom->fPoint || |
Stephen White | e30cf80 | 2017-02-27 11:37:55 -0500 | [diff] [blame] | 942 | c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 943 | return; |
| 944 | } |
| 945 | LOG("insert edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID); |
| 946 | Edge* prev = nullptr; |
| 947 | Edge* next; |
| 948 | for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) { |
| 949 | if (next->isRightOf(edge->fBottom)) { |
| 950 | break; |
| 951 | } |
| 952 | prev = next; |
| 953 | } |
senorblanco | e6eaa32 | 2016-03-08 09:06:44 -0800 | [diff] [blame] | 954 | list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>( |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 955 | edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow); |
| 956 | } |
| 957 | |
| 958 | void remove_edge_above(Edge* edge) { |
Stephen White | 7b37694 | 2018-05-22 11:51:32 -0400 | [diff] [blame] | 959 | SkASSERT(edge->fTop && edge->fBottom); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 960 | LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID, |
| 961 | edge->fBottom->fID); |
senorblanco | e6eaa32 | 2016-03-08 09:06:44 -0800 | [diff] [blame] | 962 | list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>( |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 963 | edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove); |
| 964 | } |
| 965 | |
| 966 | void remove_edge_below(Edge* edge) { |
Stephen White | 7b37694 | 2018-05-22 11:51:32 -0400 | [diff] [blame] | 967 | SkASSERT(edge->fTop && edge->fBottom); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 968 | LOG("removing edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID, |
| 969 | edge->fTop->fID); |
senorblanco | e6eaa32 | 2016-03-08 09:06:44 -0800 | [diff] [blame] | 970 | list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>( |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 971 | edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow); |
| 972 | } |
| 973 | |
Stephen White | e7a364d | 2017-01-11 16:19:26 -0500 | [diff] [blame] | 974 | void disconnect(Edge* edge) |
| 975 | { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 976 | remove_edge_above(edge); |
| 977 | remove_edge_below(edge); |
Stephen White | e7a364d | 2017-01-11 16:19:26 -0500 | [diff] [blame] | 978 | } |
| 979 | |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 980 | void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c); |
| 981 | |
| 982 | void rewind(EdgeList* activeEdges, Vertex** current, Vertex* dst, Comparator& c) { |
| 983 | if (!current || *current == dst || c.sweep_lt((*current)->fPoint, dst->fPoint)) { |
| 984 | return; |
| 985 | } |
| 986 | Vertex* v = *current; |
| 987 | LOG("rewinding active edges from vertex %g to vertex %g\n", v->fID, dst->fID); |
| 988 | while (v != dst) { |
| 989 | v = v->fPrev; |
| 990 | for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { |
| 991 | remove_edge(e, activeEdges); |
| 992 | } |
| 993 | Edge* leftEdge = v->fLeftEnclosingEdge; |
| 994 | for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) { |
| 995 | insert_edge(e, leftEdge, activeEdges); |
| 996 | leftEdge = e; |
| 997 | } |
| 998 | } |
| 999 | *current = v; |
| 1000 | } |
| 1001 | |
| 1002 | void rewind_if_necessary(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c) { |
| 1003 | if (!activeEdges || !current) { |
| 1004 | return; |
| 1005 | } |
| 1006 | Vertex* top = edge->fTop; |
| 1007 | Vertex* bottom = edge->fBottom; |
| 1008 | if (edge->fLeft) { |
| 1009 | Vertex* leftTop = edge->fLeft->fTop; |
| 1010 | Vertex* leftBottom = edge->fLeft->fBottom; |
| 1011 | if (c.sweep_lt(leftTop->fPoint, top->fPoint) && !edge->fLeft->isLeftOf(top)) { |
| 1012 | rewind(activeEdges, current, leftTop, c); |
| 1013 | } else if (c.sweep_lt(top->fPoint, leftTop->fPoint) && !edge->isRightOf(leftTop)) { |
| 1014 | rewind(activeEdges, current, top, c); |
| 1015 | } else if (c.sweep_lt(bottom->fPoint, leftBottom->fPoint) && |
| 1016 | !edge->fLeft->isLeftOf(bottom)) { |
| 1017 | rewind(activeEdges, current, leftTop, c); |
| 1018 | } else if (c.sweep_lt(leftBottom->fPoint, bottom->fPoint) && !edge->isRightOf(leftBottom)) { |
| 1019 | rewind(activeEdges, current, top, c); |
| 1020 | } |
| 1021 | } |
| 1022 | if (edge->fRight) { |
| 1023 | Vertex* rightTop = edge->fRight->fTop; |
| 1024 | Vertex* rightBottom = edge->fRight->fBottom; |
| 1025 | if (c.sweep_lt(rightTop->fPoint, top->fPoint) && !edge->fRight->isRightOf(top)) { |
| 1026 | rewind(activeEdges, current, rightTop, c); |
| 1027 | } else if (c.sweep_lt(top->fPoint, rightTop->fPoint) && !edge->isLeftOf(rightTop)) { |
| 1028 | rewind(activeEdges, current, top, c); |
| 1029 | } else if (c.sweep_lt(bottom->fPoint, rightBottom->fPoint) && |
| 1030 | !edge->fRight->isRightOf(bottom)) { |
| 1031 | rewind(activeEdges, current, rightTop, c); |
| 1032 | } else if (c.sweep_lt(rightBottom->fPoint, bottom->fPoint) && |
| 1033 | !edge->isLeftOf(rightBottom)) { |
| 1034 | rewind(activeEdges, current, top, c); |
| 1035 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1036 | } |
| 1037 | } |
| 1038 | |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1039 | void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1040 | remove_edge_below(edge); |
| 1041 | edge->fTop = v; |
| 1042 | edge->recompute(); |
| 1043 | insert_edge_below(edge, v, c); |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1044 | rewind_if_necessary(edge, activeEdges, current, c); |
| 1045 | merge_collinear_edges(edge, activeEdges, current, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1046 | } |
| 1047 | |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1048 | void set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, Comparator& c) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1049 | remove_edge_above(edge); |
| 1050 | edge->fBottom = v; |
| 1051 | edge->recompute(); |
| 1052 | insert_edge_above(edge, v, c); |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1053 | rewind_if_necessary(edge, activeEdges, current, c); |
| 1054 | merge_collinear_edges(edge, activeEdges, current, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1055 | } |
| 1056 | |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1057 | void merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current, |
| 1058 | Comparator& c) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1059 | if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) { |
| 1060 | LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n", |
| 1061 | edge->fTop->fPoint.fX, edge->fTop->fPoint.fY, |
| 1062 | edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY); |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1063 | rewind(activeEdges, current, edge->fTop, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1064 | other->fWinding += edge->fWinding; |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1065 | disconnect(edge); |
Stephen White | ec79c39 | 2018-05-18 11:49:21 -0400 | [diff] [blame] | 1066 | edge->fTop = edge->fBottom = nullptr; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1067 | } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1068 | rewind(activeEdges, current, edge->fTop, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1069 | other->fWinding += edge->fWinding; |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1070 | set_bottom(edge, other->fTop, activeEdges, current, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1071 | } else { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1072 | rewind(activeEdges, current, other->fTop, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1073 | edge->fWinding += other->fWinding; |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1074 | set_bottom(other, edge->fTop, activeEdges, current, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1078 | void merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current, |
| 1079 | Comparator& c) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1080 | if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) { |
| 1081 | LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n", |
| 1082 | edge->fTop->fPoint.fX, edge->fTop->fPoint.fY, |
| 1083 | edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY); |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1084 | rewind(activeEdges, current, edge->fTop, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1085 | other->fWinding += edge->fWinding; |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1086 | disconnect(edge); |
Stephen White | ec79c39 | 2018-05-18 11:49:21 -0400 | [diff] [blame] | 1087 | edge->fTop = edge->fBottom = nullptr; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1088 | } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1089 | rewind(activeEdges, current, other->fTop, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1090 | edge->fWinding += other->fWinding; |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1091 | set_top(other, edge->fBottom, activeEdges, current, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1092 | } else { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1093 | rewind(activeEdges, current, edge->fTop, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1094 | other->fWinding += edge->fWinding; |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1095 | set_top(edge, other->fBottom, activeEdges, current, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1096 | } |
| 1097 | } |
| 1098 | |
Stephen White | d26b4d8 | 2018-07-26 10:02:27 -0400 | [diff] [blame] | 1099 | bool top_collinear(Edge* left, Edge* right) { |
| 1100 | if (!left || !right) { |
| 1101 | return false; |
| 1102 | } |
| 1103 | return left->fTop->fPoint == right->fTop->fPoint || |
| 1104 | !left->isLeftOf(right->fTop) || !right->isRightOf(left->fTop); |
| 1105 | } |
| 1106 | |
| 1107 | bool bottom_collinear(Edge* left, Edge* right) { |
| 1108 | if (!left || !right) { |
| 1109 | return false; |
| 1110 | } |
| 1111 | return left->fBottom->fPoint == right->fBottom->fPoint || |
| 1112 | !left->isLeftOf(right->fBottom) || !right->isRightOf(left->fBottom); |
| 1113 | } |
| 1114 | |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1115 | void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, Comparator& c) { |
Stephen White | 6eca90f | 2017-05-25 14:47:11 -0400 | [diff] [blame] | 1116 | for (;;) { |
Stephen White | d26b4d8 | 2018-07-26 10:02:27 -0400 | [diff] [blame] | 1117 | if (top_collinear(edge->fPrevEdgeAbove, edge)) { |
Stephen White | 24289e0 | 2018-06-29 17:02:21 -0400 | [diff] [blame] | 1118 | merge_edges_above(edge->fPrevEdgeAbove, edge, activeEdges, current, c); |
Stephen White | d26b4d8 | 2018-07-26 10:02:27 -0400 | [diff] [blame] | 1119 | } else if (top_collinear(edge, edge->fNextEdgeAbove)) { |
Stephen White | 24289e0 | 2018-06-29 17:02:21 -0400 | [diff] [blame] | 1120 | merge_edges_above(edge->fNextEdgeAbove, edge, activeEdges, current, c); |
Stephen White | d26b4d8 | 2018-07-26 10:02:27 -0400 | [diff] [blame] | 1121 | } else if (bottom_collinear(edge->fPrevEdgeBelow, edge)) { |
Stephen White | 24289e0 | 2018-06-29 17:02:21 -0400 | [diff] [blame] | 1122 | merge_edges_below(edge->fPrevEdgeBelow, edge, activeEdges, current, c); |
Stephen White | d26b4d8 | 2018-07-26 10:02:27 -0400 | [diff] [blame] | 1123 | } else if (bottom_collinear(edge, edge->fNextEdgeBelow)) { |
Stephen White | 24289e0 | 2018-06-29 17:02:21 -0400 | [diff] [blame] | 1124 | merge_edges_below(edge->fNextEdgeBelow, edge, activeEdges, current, c); |
Stephen White | 6eca90f | 2017-05-25 14:47:11 -0400 | [diff] [blame] | 1125 | } else { |
| 1126 | break; |
| 1127 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1128 | } |
Stephen White | d26b4d8 | 2018-07-26 10:02:27 -0400 | [diff] [blame] | 1129 | SkASSERT(!top_collinear(edge->fPrevEdgeAbove, edge)); |
| 1130 | SkASSERT(!top_collinear(edge, edge->fNextEdgeAbove)); |
| 1131 | SkASSERT(!bottom_collinear(edge->fPrevEdgeBelow, edge)); |
| 1132 | SkASSERT(!bottom_collinear(edge, edge->fNextEdgeBelow)); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1133 | } |
| 1134 | |
Stephen White | 89042d5 | 2018-06-08 12:18:22 -0400 | [diff] [blame] | 1135 | 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] | 1136 | SkArenaAlloc& alloc) { |
Stephen White | ec79c39 | 2018-05-18 11:49:21 -0400 | [diff] [blame] | 1137 | if (!edge->fTop || !edge->fBottom || v == edge->fTop || v == edge->fBottom) { |
Stephen White | 89042d5 | 2018-06-08 12:18:22 -0400 | [diff] [blame] | 1138 | return false; |
Stephen White | 0cb3167 | 2017-06-08 14:41:01 -0400 | [diff] [blame] | 1139 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1140 | LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n", |
| 1141 | edge->fTop->fID, edge->fBottom->fID, |
| 1142 | v->fID, v->fPoint.fX, v->fPoint.fY); |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1143 | Vertex* top; |
| 1144 | Vertex* bottom; |
Stephen White | 531a48e | 2018-06-01 09:49:39 -0400 | [diff] [blame] | 1145 | int winding = edge->fWinding; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1146 | if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1147 | top = v; |
| 1148 | bottom = edge->fTop; |
| 1149 | set_top(edge, v, activeEdges, current, c); |
Stephen White | e30cf80 | 2017-02-27 11:37:55 -0500 | [diff] [blame] | 1150 | } else if (c.sweep_lt(edge->fBottom->fPoint, v->fPoint)) { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1151 | top = edge->fBottom; |
| 1152 | bottom = v; |
| 1153 | set_bottom(edge, v, activeEdges, current, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1154 | } else { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1155 | top = v; |
| 1156 | bottom = edge->fBottom; |
| 1157 | set_bottom(edge, v, activeEdges, current, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1158 | } |
Stephen White | 531a48e | 2018-06-01 09:49:39 -0400 | [diff] [blame] | 1159 | Edge* newEdge = alloc.make<Edge>(top, bottom, winding, edge->fType); |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1160 | insert_edge_below(newEdge, top, c); |
| 1161 | insert_edge_above(newEdge, bottom, c); |
| 1162 | merge_collinear_edges(newEdge, activeEdges, current, c); |
Stephen White | 89042d5 | 2018-06-08 12:18:22 -0400 | [diff] [blame] | 1163 | return true; |
| 1164 | } |
| 1165 | |
| 1166 | bool intersect_edge_pair(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current, Comparator& c, SkArenaAlloc& alloc) { |
| 1167 | if (!left->fTop || !left->fBottom || !right->fTop || !right->fBottom) { |
| 1168 | return false; |
| 1169 | } |
Stephen White | 1c5fd18 | 2018-07-12 15:54:05 -0400 | [diff] [blame] | 1170 | if (left->fTop == right->fTop || left->fBottom == right->fBottom) { |
| 1171 | return false; |
| 1172 | } |
Stephen White | 89042d5 | 2018-06-08 12:18:22 -0400 | [diff] [blame] | 1173 | if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) { |
| 1174 | if (!left->isLeftOf(right->fTop)) { |
Stephen White | 1c5fd18 | 2018-07-12 15:54:05 -0400 | [diff] [blame] | 1175 | rewind(activeEdges, current, right->fTop, c); |
Stephen White | 89042d5 | 2018-06-08 12:18:22 -0400 | [diff] [blame] | 1176 | return split_edge(left, right->fTop, activeEdges, current, c, alloc); |
| 1177 | } |
| 1178 | } else { |
| 1179 | if (!right->isRightOf(left->fTop)) { |
Stephen White | 1c5fd18 | 2018-07-12 15:54:05 -0400 | [diff] [blame] | 1180 | rewind(activeEdges, current, left->fTop, c); |
Stephen White | 89042d5 | 2018-06-08 12:18:22 -0400 | [diff] [blame] | 1181 | return split_edge(right, left->fTop, activeEdges, current, c, alloc); |
| 1182 | } |
| 1183 | } |
| 1184 | if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) { |
| 1185 | if (!left->isLeftOf(right->fBottom)) { |
Stephen White | 1c5fd18 | 2018-07-12 15:54:05 -0400 | [diff] [blame] | 1186 | rewind(activeEdges, current, right->fBottom, c); |
Stephen White | 89042d5 | 2018-06-08 12:18:22 -0400 | [diff] [blame] | 1187 | return split_edge(left, right->fBottom, activeEdges, current, c, alloc); |
| 1188 | } |
| 1189 | } else { |
| 1190 | if (!right->isRightOf(left->fBottom)) { |
Stephen White | 1c5fd18 | 2018-07-12 15:54:05 -0400 | [diff] [blame] | 1191 | rewind(activeEdges, current, left->fBottom, c); |
Stephen White | 89042d5 | 2018-06-08 12:18:22 -0400 | [diff] [blame] | 1192 | return split_edge(right, left->fBottom, activeEdges, current, c, alloc); |
| 1193 | } |
| 1194 | } |
| 1195 | return false; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1196 | } |
| 1197 | |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 1198 | 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] | 1199 | int winding_scale = 1) { |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1200 | if (!prev || !next || prev->fPoint == next->fPoint) { |
| 1201 | return nullptr; |
| 1202 | } |
Stephen White | bf6137e | 2017-01-04 15:43:26 -0500 | [diff] [blame] | 1203 | Edge* edge = new_edge(prev, next, type, c, alloc); |
Stephen White | 8a0bfc5 | 2017-02-21 15:24:13 -0500 | [diff] [blame] | 1204 | insert_edge_below(edge, edge->fTop, c); |
| 1205 | insert_edge_above(edge, edge->fBottom, c); |
Stephen White | 48ded38 | 2017-02-03 10:15:16 -0500 | [diff] [blame] | 1206 | edge->fWinding *= winding_scale; |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1207 | merge_collinear_edges(edge, nullptr, nullptr, c); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1208 | return edge; |
| 1209 | } |
| 1210 | |
Stephen White | bf6137e | 2017-01-04 15:43:26 -0500 | [diff] [blame] | 1211 | void merge_vertices(Vertex* src, Vertex* dst, VertexList* mesh, Comparator& c, |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 1212 | SkArenaAlloc& alloc) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1213 | LOG("found coincident verts at %g, %g; merging %g into %g\n", src->fPoint.fX, src->fPoint.fY, |
| 1214 | src->fID, dst->fID); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1215 | dst->fAlpha = SkTMax(src->fAlpha, dst->fAlpha); |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 1216 | if (src->fPartner) { |
| 1217 | src->fPartner->fPartner = dst; |
| 1218 | } |
Stephen White | 7b37694 | 2018-05-22 11:51:32 -0400 | [diff] [blame] | 1219 | while (Edge* edge = src->fFirstEdgeAbove) { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1220 | set_bottom(edge, dst, nullptr, nullptr, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1221 | } |
Stephen White | 7b37694 | 2018-05-22 11:51:32 -0400 | [diff] [blame] | 1222 | while (Edge* edge = src->fFirstEdgeBelow) { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1223 | set_top(edge, dst, nullptr, nullptr, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1224 | } |
Stephen White | bf6137e | 2017-01-04 15:43:26 -0500 | [diff] [blame] | 1225 | mesh->remove(src); |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 1226 | dst->fSynthetic = true; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1227 | } |
| 1228 | |
Stephen White | 95152e1 | 2017-12-18 10:52:44 -0500 | [diff] [blame] | 1229 | Vertex* create_sorted_vertex(const SkPoint& p, uint8_t alpha, VertexList* mesh, |
| 1230 | Vertex* reference, Comparator& c, SkArenaAlloc& alloc) { |
| 1231 | Vertex* prevV = reference; |
| 1232 | while (prevV && c.sweep_lt(p, prevV->fPoint)) { |
| 1233 | prevV = prevV->fPrev; |
| 1234 | } |
| 1235 | Vertex* nextV = prevV ? prevV->fNext : mesh->fHead; |
| 1236 | while (nextV && c.sweep_lt(nextV->fPoint, p)) { |
| 1237 | prevV = nextV; |
| 1238 | nextV = nextV->fNext; |
| 1239 | } |
| 1240 | Vertex* v; |
| 1241 | if (prevV && coincident(prevV->fPoint, p)) { |
| 1242 | v = prevV; |
| 1243 | } else if (nextV && coincident(nextV->fPoint, p)) { |
| 1244 | v = nextV; |
| 1245 | } else { |
| 1246 | v = alloc.make<Vertex>(p, alpha); |
| 1247 | #if LOGGING_ENABLED |
| 1248 | if (!prevV) { |
| 1249 | v->fID = mesh->fHead->fID - 1.0f; |
| 1250 | } else if (!nextV) { |
| 1251 | v->fID = mesh->fTail->fID + 1.0f; |
| 1252 | } else { |
| 1253 | v->fID = (prevV->fID + nextV->fID) * 0.5f; |
| 1254 | } |
| 1255 | #endif |
| 1256 | mesh->insert(v, prevV, nextV); |
| 1257 | } |
| 1258 | return v; |
| 1259 | } |
| 1260 | |
Stephen White | 53a0298 | 2018-05-30 22:47:46 -0400 | [diff] [blame] | 1261 | // If an edge's top and bottom points differ only by 1/2 machine epsilon in the primary |
| 1262 | // sort criterion, it may not be possible to split correctly, since there is no point which is |
| 1263 | // below the top and above the bottom. This function detects that case. |
| 1264 | bool nearly_flat(Comparator& c, Edge* edge) { |
| 1265 | SkPoint diff = edge->fBottom->fPoint - edge->fTop->fPoint; |
| 1266 | float primaryDiff = c.fDirection == Comparator::Direction::kHorizontal ? diff.fX : diff.fY; |
Stephen White | 13f3d8d | 2018-06-22 10:19:20 -0400 | [diff] [blame] | 1267 | return fabs(primaryDiff) < std::numeric_limits<float>::epsilon() && primaryDiff != 0.0f; |
Stephen White | 53a0298 | 2018-05-30 22:47:46 -0400 | [diff] [blame] | 1268 | } |
| 1269 | |
Stephen White | e62999f | 2018-06-05 18:45:07 -0400 | [diff] [blame] | 1270 | SkPoint clamp(SkPoint p, SkPoint min, SkPoint max, Comparator& c) { |
| 1271 | if (c.sweep_lt(p, min)) { |
| 1272 | return min; |
| 1273 | } else if (c.sweep_lt(max, p)) { |
| 1274 | return max; |
| 1275 | } else { |
| 1276 | return p; |
| 1277 | } |
| 1278 | } |
| 1279 | |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 1280 | void compute_bisector(Edge* edge1, Edge* edge2, Vertex* v, SkArenaAlloc& alloc) { |
| 1281 | Line line1 = edge1->fLine; |
| 1282 | Line line2 = edge2->fLine; |
| 1283 | line1.normalize(); |
| 1284 | line2.normalize(); |
| 1285 | double cosAngle = line1.fA * line2.fA + line1.fB * line2.fB; |
| 1286 | if (cosAngle > 0.999) { |
| 1287 | return; |
| 1288 | } |
| 1289 | line1.fC += edge1->fWinding > 0 ? -1 : 1; |
| 1290 | line2.fC += edge2->fWinding > 0 ? -1 : 1; |
| 1291 | SkPoint p; |
| 1292 | if (line1.intersect(line2, &p)) { |
| 1293 | uint8_t alpha = edge1->fType == Edge::Type::kOuter ? 255 : 0; |
| 1294 | v->fPartner = alloc.make<Vertex>(p, alpha); |
| 1295 | LOG("computed bisector (%g,%g) alpha %d for vertex %g\n", p.fX, p.fY, alpha, v->fID); |
| 1296 | } |
| 1297 | } |
| 1298 | |
Stephen White | b141fcb | 2018-06-14 10:15:47 -0400 | [diff] [blame] | 1299 | bool check_for_intersection(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current, |
Stephen White | 0cb3167 | 2017-06-08 14:41:01 -0400 | [diff] [blame] | 1300 | VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) { |
Stephen White | b141fcb | 2018-06-14 10:15:47 -0400 | [diff] [blame] | 1301 | if (!left || !right) { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1302 | return false; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1303 | } |
Stephen White | 56158ae | 2017-01-30 14:31:31 -0500 | [diff] [blame] | 1304 | SkPoint p; |
| 1305 | uint8_t alpha; |
Stephen White | b141fcb | 2018-06-14 10:15:47 -0400 | [diff] [blame] | 1306 | if (left->intersect(*right, &p, &alpha) && p.isFinite()) { |
Ravi Mistry | bfe9598 | 2018-05-29 18:19:07 +0000 | [diff] [blame] | 1307 | Vertex* v; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1308 | LOG("found intersection, pt is %g, %g\n", p.fX, p.fY); |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1309 | Vertex* top = *current; |
| 1310 | // If the intersection point is above the current vertex, rewind to the vertex above the |
| 1311 | // intersection. |
Stephen White | 0cb3167 | 2017-06-08 14:41:01 -0400 | [diff] [blame] | 1312 | while (top && c.sweep_lt(p, top->fPoint)) { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1313 | top = top->fPrev; |
| 1314 | } |
Stephen White | b141fcb | 2018-06-14 10:15:47 -0400 | [diff] [blame] | 1315 | if (!nearly_flat(c, left)) { |
| 1316 | p = clamp(p, left->fTop->fPoint, left->fBottom->fPoint, c); |
Stephen White | e62999f | 2018-06-05 18:45:07 -0400 | [diff] [blame] | 1317 | } |
Stephen White | b141fcb | 2018-06-14 10:15:47 -0400 | [diff] [blame] | 1318 | if (!nearly_flat(c, right)) { |
| 1319 | p = clamp(p, right->fTop->fPoint, right->fBottom->fPoint, c); |
Stephen White | e62999f | 2018-06-05 18:45:07 -0400 | [diff] [blame] | 1320 | } |
Stephen White | b141fcb | 2018-06-14 10:15:47 -0400 | [diff] [blame] | 1321 | if (p == left->fTop->fPoint) { |
| 1322 | v = left->fTop; |
| 1323 | } else if (p == left->fBottom->fPoint) { |
| 1324 | v = left->fBottom; |
| 1325 | } else if (p == right->fTop->fPoint) { |
| 1326 | v = right->fTop; |
| 1327 | } else if (p == right->fBottom->fPoint) { |
| 1328 | v = right->fBottom; |
Ravi Mistry | bfe9598 | 2018-05-29 18:19:07 +0000 | [diff] [blame] | 1329 | } else { |
Stephen White | 95152e1 | 2017-12-18 10:52:44 -0500 | [diff] [blame] | 1330 | v = create_sorted_vertex(p, alpha, mesh, top, c, alloc); |
Stephen White | b141fcb | 2018-06-14 10:15:47 -0400 | [diff] [blame] | 1331 | if (left->fTop->fPartner) { |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 1332 | v->fSynthetic = true; |
| 1333 | compute_bisector(left, right, v, alloc); |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1334 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1335 | } |
Stephen White | 0cb3167 | 2017-06-08 14:41:01 -0400 | [diff] [blame] | 1336 | rewind(activeEdges, current, top ? top : v, c); |
Stephen White | b141fcb | 2018-06-14 10:15:47 -0400 | [diff] [blame] | 1337 | split_edge(left, v, activeEdges, current, c, alloc); |
| 1338 | split_edge(right, v, activeEdges, current, c, alloc); |
Stephen White | 92eba8a | 2017-02-06 09:50:27 -0500 | [diff] [blame] | 1339 | v->fAlpha = SkTMax(v->fAlpha, alpha); |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1340 | return true; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1341 | } |
Stephen White | b141fcb | 2018-06-14 10:15:47 -0400 | [diff] [blame] | 1342 | return intersect_edge_pair(left, right, activeEdges, current, c, alloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1343 | } |
| 1344 | |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 1345 | void sanitize_contours(VertexList* contours, int contourCnt, bool approximate) { |
| 1346 | for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) { |
| 1347 | SkASSERT(contour->fHead); |
| 1348 | Vertex* prev = contour->fTail; |
Stephen White | 5926f2d | 2017-02-13 13:55:42 -0500 | [diff] [blame] | 1349 | if (approximate) { |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 1350 | round(&prev->fPoint); |
Stephen White | 5926f2d | 2017-02-13 13:55:42 -0500 | [diff] [blame] | 1351 | } |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 1352 | for (Vertex* v = contour->fHead; v;) { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1353 | if (approximate) { |
| 1354 | round(&v->fPoint); |
| 1355 | } |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 1356 | Vertex* next = v->fNext; |
Stephen White | 3de40f8 | 2018-06-28 09:36:49 -0400 | [diff] [blame] | 1357 | Vertex* nextWrap = next ? next : contour->fHead; |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 1358 | if (coincident(prev->fPoint, v->fPoint)) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1359 | 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] | 1360 | contour->remove(v); |
Stephen White | 73e7f80 | 2017-08-23 13:56:07 -0400 | [diff] [blame] | 1361 | } else if (!v->fPoint.isFinite()) { |
| 1362 | LOG("vertex %g,%g non-finite; removing\n", v->fPoint.fX, v->fPoint.fY); |
| 1363 | contour->remove(v); |
Stephen White | 3de40f8 | 2018-06-28 09:36:49 -0400 | [diff] [blame] | 1364 | } else if (Line(prev->fPoint, nextWrap->fPoint).dist(v->fPoint) == 0.0) { |
Stephen White | 06768ca | 2018-05-25 14:50:56 -0400 | [diff] [blame] | 1365 | LOG("vertex %g,%g collinear; removing\n", v->fPoint.fX, v->fPoint.fY); |
| 1366 | contour->remove(v); |
| 1367 | } else { |
| 1368 | prev = v; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1369 | } |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 1370 | v = next; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1371 | } |
| 1372 | } |
| 1373 | } |
| 1374 | |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1375 | bool merge_coincident_vertices(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) { |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 1376 | if (!mesh->fHead) { |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1377 | return false; |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 1378 | } |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1379 | bool merged = false; |
| 1380 | for (Vertex* v = mesh->fHead->fNext; v;) { |
| 1381 | Vertex* next = v->fNext; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1382 | if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) { |
| 1383 | v->fPoint = v->fPrev->fPoint; |
| 1384 | } |
| 1385 | if (coincident(v->fPrev->fPoint, v->fPoint)) { |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1386 | merge_vertices(v, v->fPrev, mesh, c, alloc); |
| 1387 | merged = true; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1388 | } |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1389 | v = next; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1390 | } |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1391 | return merged; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | // Stage 2: convert the contours to a mesh of edges connecting the vertices. |
| 1395 | |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 1396 | void build_edges(VertexList* contours, int contourCnt, VertexList* mesh, Comparator& c, |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 1397 | SkArenaAlloc& alloc) { |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 1398 | for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) { |
| 1399 | Vertex* prev = contour->fTail; |
| 1400 | for (Vertex* v = contour->fHead; v;) { |
| 1401 | Vertex* next = v->fNext; |
| 1402 | connect(prev, v, Edge::Type::kInner, c, alloc); |
| 1403 | mesh->append(v); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1404 | prev = v; |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 1405 | v = next; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1406 | } |
| 1407 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1408 | } |
| 1409 | |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1410 | void connect_partners(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) { |
| 1411 | for (Vertex* outer = mesh->fHead; outer; outer = outer->fNext) { |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 1412 | if (Vertex* inner = outer->fPartner) { |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1413 | if ((inner->fPrev || inner->fNext) && (outer->fPrev || outer->fNext)) { |
| 1414 | // Connector edges get zero winding, since they're only structural (i.e., to ensure |
| 1415 | // no 0-0-0 alpha triangles are produced), and shouldn't affect the poly winding |
| 1416 | // number. |
| 1417 | connect(outer, inner, Edge::Type::kConnector, c, alloc, 0); |
| 1418 | inner->fPartner = outer->fPartner = nullptr; |
| 1419 | } |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 1420 | } |
| 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | template <CompareFunc sweep_lt> |
| 1425 | void sorted_merge(VertexList* front, VertexList* back, VertexList* result) { |
| 1426 | Vertex* a = front->fHead; |
| 1427 | Vertex* b = back->fHead; |
| 1428 | while (a && b) { |
| 1429 | if (sweep_lt(a->fPoint, b->fPoint)) { |
| 1430 | front->remove(a); |
| 1431 | result->append(a); |
| 1432 | a = front->fHead; |
| 1433 | } else { |
| 1434 | back->remove(b); |
| 1435 | result->append(b); |
| 1436 | b = back->fHead; |
| 1437 | } |
| 1438 | } |
| 1439 | result->append(*front); |
| 1440 | result->append(*back); |
| 1441 | } |
| 1442 | |
| 1443 | void sorted_merge(VertexList* front, VertexList* back, VertexList* result, Comparator& c) { |
| 1444 | if (c.fDirection == Comparator::Direction::kHorizontal) { |
| 1445 | sorted_merge<sweep_lt_horiz>(front, back, result); |
| 1446 | } else { |
| 1447 | sorted_merge<sweep_lt_vert>(front, back, result); |
| 1448 | } |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1449 | #if LOGGING_ENABLED |
| 1450 | float id = 0.0f; |
| 1451 | for (Vertex* v = result->fHead; v; v = v->fNext) { |
| 1452 | v->fID = id++; |
| 1453 | } |
| 1454 | #endif |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 1455 | } |
| 1456 | |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1457 | // Stage 3: sort the vertices by increasing sweep direction. |
| 1458 | |
Stephen White | 16a40cb | 2017-02-23 11:10:01 -0500 | [diff] [blame] | 1459 | template <CompareFunc sweep_lt> |
| 1460 | void merge_sort(VertexList* vertices) { |
| 1461 | Vertex* slow = vertices->fHead; |
| 1462 | if (!slow) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1463 | return; |
| 1464 | } |
Stephen White | 16a40cb | 2017-02-23 11:10:01 -0500 | [diff] [blame] | 1465 | Vertex* fast = slow->fNext; |
| 1466 | if (!fast) { |
| 1467 | return; |
| 1468 | } |
| 1469 | do { |
| 1470 | fast = fast->fNext; |
| 1471 | if (fast) { |
| 1472 | fast = fast->fNext; |
| 1473 | slow = slow->fNext; |
| 1474 | } |
| 1475 | } while (fast); |
| 1476 | VertexList front(vertices->fHead, slow); |
| 1477 | VertexList back(slow->fNext, vertices->fTail); |
| 1478 | front.fTail->fNext = back.fHead->fPrev = nullptr; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1479 | |
Stephen White | 16a40cb | 2017-02-23 11:10:01 -0500 | [diff] [blame] | 1480 | merge_sort<sweep_lt>(&front); |
| 1481 | merge_sort<sweep_lt>(&back); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1482 | |
Stephen White | 16a40cb | 2017-02-23 11:10:01 -0500 | [diff] [blame] | 1483 | vertices->fHead = vertices->fTail = nullptr; |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 1484 | sorted_merge<sweep_lt>(&front, &back, vertices); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1485 | } |
| 1486 | |
Stephen White | 95152e1 | 2017-12-18 10:52:44 -0500 | [diff] [blame] | 1487 | void dump_mesh(const VertexList& mesh) { |
| 1488 | #if LOGGING_ENABLED |
| 1489 | for (Vertex* v = mesh.fHead; v; v = v->fNext) { |
| 1490 | LOG("vertex %g (%g, %g) alpha %d", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha); |
| 1491 | if (Vertex* p = v->fPartner) { |
| 1492 | LOG(", partner %g (%g, %g) alpha %d\n", p->fID, p->fPoint.fX, p->fPoint.fY, p->fAlpha); |
| 1493 | } else { |
| 1494 | LOG(", null partner\n"); |
| 1495 | } |
| 1496 | for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) { |
| 1497 | LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding); |
| 1498 | } |
| 1499 | for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { |
| 1500 | LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding); |
| 1501 | } |
| 1502 | } |
| 1503 | #endif |
| 1504 | } |
| 1505 | |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 1506 | void dump_skel(const SSEdgeList& ssEdges) { |
| 1507 | #if LOGGING_ENABLED |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 1508 | for (SSEdge* edge : ssEdges) { |
| 1509 | if (edge->fEdge) { |
Stephen White | 8a3c059 | 2019-05-29 11:26:16 -0400 | [diff] [blame] | 1510 | LOG("skel edge %g -> %g", |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 1511 | edge->fPrev->fVertex->fID, |
Stephen White | 8a3c059 | 2019-05-29 11:26:16 -0400 | [diff] [blame] | 1512 | edge->fNext->fVertex->fID); |
| 1513 | if (edge->fEdge->fTop && edge->fEdge->fBottom) { |
| 1514 | LOG(" (original %g -> %g)\n", |
| 1515 | edge->fEdge->fTop->fID, |
| 1516 | edge->fEdge->fBottom->fID); |
| 1517 | } else { |
| 1518 | LOG("\n"); |
| 1519 | } |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 1520 | } |
| 1521 | } |
| 1522 | #endif |
| 1523 | } |
| 1524 | |
Stephen White | 89042d5 | 2018-06-08 12:18:22 -0400 | [diff] [blame] | 1525 | #ifdef SK_DEBUG |
| 1526 | void validate_edge_pair(Edge* left, Edge* right, Comparator& c) { |
| 1527 | if (!left || !right) { |
| 1528 | return; |
| 1529 | } |
| 1530 | if (left->fTop == right->fTop) { |
| 1531 | SkASSERT(left->isLeftOf(right->fBottom)); |
| 1532 | SkASSERT(right->isRightOf(left->fBottom)); |
| 1533 | } else if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) { |
| 1534 | SkASSERT(left->isLeftOf(right->fTop)); |
| 1535 | } else { |
| 1536 | SkASSERT(right->isRightOf(left->fTop)); |
| 1537 | } |
| 1538 | if (left->fBottom == right->fBottom) { |
| 1539 | SkASSERT(left->isLeftOf(right->fTop)); |
| 1540 | SkASSERT(right->isRightOf(left->fTop)); |
| 1541 | } else if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) { |
| 1542 | SkASSERT(left->isLeftOf(right->fBottom)); |
| 1543 | } else { |
| 1544 | SkASSERT(right->isRightOf(left->fBottom)); |
| 1545 | } |
| 1546 | } |
| 1547 | |
| 1548 | void validate_edge_list(EdgeList* edges, Comparator& c) { |
| 1549 | Edge* left = edges->fHead; |
| 1550 | if (!left) { |
| 1551 | return; |
| 1552 | } |
| 1553 | for (Edge* right = left->fRight; right; right = right->fRight) { |
| 1554 | validate_edge_pair(left, right, c); |
| 1555 | left = right; |
| 1556 | } |
| 1557 | } |
| 1558 | #endif |
| 1559 | |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1560 | // Stage 4: Simplify the mesh by inserting new vertices at intersecting edges. |
| 1561 | |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 1562 | bool connected(Vertex* v) { |
| 1563 | return v->fFirstEdgeAbove || v->fFirstEdgeBelow; |
| 1564 | } |
| 1565 | |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1566 | bool simplify(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1567 | LOG("simplifying complex polygons\n"); |
| 1568 | EdgeList activeEdges; |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1569 | bool found = false; |
Stephen White | 0cb3167 | 2017-06-08 14:41:01 -0400 | [diff] [blame] | 1570 | for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) { |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 1571 | if (!connected(v)) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1572 | continue; |
| 1573 | } |
Stephen White | 8a0bfc5 | 2017-02-21 15:24:13 -0500 | [diff] [blame] | 1574 | Edge* leftEnclosingEdge; |
| 1575 | Edge* rightEnclosingEdge; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1576 | bool restartChecks; |
| 1577 | do { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1578 | 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] | 1579 | restartChecks = false; |
| 1580 | find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge); |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1581 | v->fLeftEnclosingEdge = leftEnclosingEdge; |
| 1582 | v->fRightEnclosingEdge = rightEnclosingEdge; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1583 | if (v->fFirstEdgeBelow) { |
Stephen White | bf6137e | 2017-01-04 15:43:26 -0500 | [diff] [blame] | 1584 | for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) { |
Stephen White | 89042d5 | 2018-06-08 12:18:22 -0400 | [diff] [blame] | 1585 | if (check_for_intersection(leftEnclosingEdge, edge, &activeEdges, &v, mesh, c, |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1586 | alloc)) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1587 | restartChecks = true; |
| 1588 | break; |
| 1589 | } |
Stephen White | 0cb3167 | 2017-06-08 14:41:01 -0400 | [diff] [blame] | 1590 | if (check_for_intersection(edge, rightEnclosingEdge, &activeEdges, &v, mesh, c, |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1591 | alloc)) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1592 | restartChecks = true; |
| 1593 | break; |
| 1594 | } |
| 1595 | } |
| 1596 | } else { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1597 | if (check_for_intersection(leftEnclosingEdge, rightEnclosingEdge, |
Stephen White | 0cb3167 | 2017-06-08 14:41:01 -0400 | [diff] [blame] | 1598 | &activeEdges, &v, mesh, c, alloc)) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1599 | restartChecks = true; |
| 1600 | } |
| 1601 | |
| 1602 | } |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1603 | found = found || restartChecks; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1604 | } while (restartChecks); |
Stephen White | 89042d5 | 2018-06-08 12:18:22 -0400 | [diff] [blame] | 1605 | #ifdef SK_DEBUG |
| 1606 | validate_edge_list(&activeEdges, c); |
| 1607 | #endif |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1608 | for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) { |
| 1609 | remove_edge(e, &activeEdges); |
| 1610 | } |
| 1611 | Edge* leftEdge = leftEnclosingEdge; |
| 1612 | for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { |
| 1613 | insert_edge(e, leftEdge, &activeEdges); |
| 1614 | leftEdge = e; |
| 1615 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1616 | } |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1617 | SkASSERT(!activeEdges.fHead && !activeEdges.fTail); |
| 1618 | return found; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1619 | } |
| 1620 | |
| 1621 | // Stage 5: Tessellate the simplified mesh into monotone polygons. |
| 1622 | |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 1623 | Poly* tessellate(const VertexList& vertices, SkArenaAlloc& alloc) { |
Stephen White | 95152e1 | 2017-12-18 10:52:44 -0500 | [diff] [blame] | 1624 | LOG("\ntessellating simple polygons\n"); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1625 | EdgeList activeEdges; |
| 1626 | Poly* polys = nullptr; |
Stephen White | bf6137e | 2017-01-04 15:43:26 -0500 | [diff] [blame] | 1627 | for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) { |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 1628 | if (!connected(v)) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1629 | continue; |
| 1630 | } |
| 1631 | #if LOGGING_ENABLED |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1632 | 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] | 1633 | #endif |
Stephen White | 8a0bfc5 | 2017-02-21 15:24:13 -0500 | [diff] [blame] | 1634 | Edge* leftEnclosingEdge; |
| 1635 | Edge* rightEnclosingEdge; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1636 | find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge); |
Stephen White | 8a0bfc5 | 2017-02-21 15:24:13 -0500 | [diff] [blame] | 1637 | Poly* leftPoly; |
| 1638 | Poly* rightPoly; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1639 | if (v->fFirstEdgeAbove) { |
| 1640 | leftPoly = v->fFirstEdgeAbove->fLeftPoly; |
| 1641 | rightPoly = v->fLastEdgeAbove->fRightPoly; |
| 1642 | } else { |
| 1643 | leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr; |
| 1644 | rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr; |
| 1645 | } |
| 1646 | #if LOGGING_ENABLED |
| 1647 | LOG("edges above:\n"); |
| 1648 | for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) { |
| 1649 | LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID, |
| 1650 | e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1); |
| 1651 | } |
| 1652 | LOG("edges below:\n"); |
| 1653 | for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { |
| 1654 | LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID, |
| 1655 | e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1); |
| 1656 | } |
| 1657 | #endif |
| 1658 | if (v->fFirstEdgeAbove) { |
| 1659 | if (leftPoly) { |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 1660 | leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, Poly::kRight_Side, alloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1661 | } |
| 1662 | if (rightPoly) { |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 1663 | rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Poly::kLeft_Side, alloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1664 | } |
| 1665 | for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1666 | Edge* rightEdge = e->fNextEdgeAbove; |
Stephen White | 8a0bfc5 | 2017-02-21 15:24:13 -0500 | [diff] [blame] | 1667 | remove_edge(e, &activeEdges); |
| 1668 | if (e->fRightPoly) { |
| 1669 | e->fRightPoly->addEdge(e, Poly::kLeft_Side, alloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1670 | } |
Stephen White | 8a0bfc5 | 2017-02-21 15:24:13 -0500 | [diff] [blame] | 1671 | if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != e->fRightPoly) { |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 1672 | rightEdge->fLeftPoly->addEdge(e, Poly::kRight_Side, alloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1673 | } |
| 1674 | } |
| 1675 | remove_edge(v->fLastEdgeAbove, &activeEdges); |
| 1676 | if (!v->fFirstEdgeBelow) { |
| 1677 | if (leftPoly && rightPoly && leftPoly != rightPoly) { |
| 1678 | SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr); |
| 1679 | rightPoly->fPartner = leftPoly; |
| 1680 | leftPoly->fPartner = rightPoly; |
| 1681 | } |
| 1682 | } |
| 1683 | } |
| 1684 | if (v->fFirstEdgeBelow) { |
| 1685 | if (!v->fFirstEdgeAbove) { |
senorblanco | 93e3fff | 2016-06-07 12:36:00 -0700 | [diff] [blame] | 1686 | if (leftPoly && rightPoly) { |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 1687 | if (leftPoly == rightPoly) { |
| 1688 | if (leftPoly->fTail && leftPoly->fTail->fSide == Poly::kLeft_Side) { |
| 1689 | leftPoly = new_poly(&polys, leftPoly->lastVertex(), |
| 1690 | leftPoly->fWinding, alloc); |
| 1691 | leftEnclosingEdge->fRightPoly = leftPoly; |
| 1692 | } else { |
| 1693 | rightPoly = new_poly(&polys, rightPoly->lastVertex(), |
| 1694 | rightPoly->fWinding, alloc); |
| 1695 | rightEnclosingEdge->fLeftPoly = rightPoly; |
| 1696 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1697 | } |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 1698 | Edge* join = alloc.make<Edge>(leftPoly->lastVertex(), v, 1, Edge::Type::kInner); |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 1699 | leftPoly = leftPoly->addEdge(join, Poly::kRight_Side, alloc); |
| 1700 | rightPoly = rightPoly->addEdge(join, Poly::kLeft_Side, alloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1701 | } |
| 1702 | } |
| 1703 | Edge* leftEdge = v->fFirstEdgeBelow; |
| 1704 | leftEdge->fLeftPoly = leftPoly; |
| 1705 | insert_edge(leftEdge, leftEnclosingEdge, &activeEdges); |
| 1706 | for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge; |
| 1707 | rightEdge = rightEdge->fNextEdgeBelow) { |
| 1708 | insert_edge(rightEdge, leftEdge, &activeEdges); |
| 1709 | int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0; |
| 1710 | winding += leftEdge->fWinding; |
| 1711 | if (winding != 0) { |
| 1712 | Poly* poly = new_poly(&polys, v, winding, alloc); |
| 1713 | leftEdge->fRightPoly = rightEdge->fLeftPoly = poly; |
| 1714 | } |
| 1715 | leftEdge = rightEdge; |
| 1716 | } |
| 1717 | v->fLastEdgeBelow->fRightPoly = rightPoly; |
| 1718 | } |
| 1719 | #if LOGGING_ENABLED |
| 1720 | LOG("\nactive edges:\n"); |
| 1721 | for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) { |
| 1722 | LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID, |
| 1723 | e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1); |
| 1724 | } |
| 1725 | #endif |
| 1726 | } |
| 1727 | return polys; |
| 1728 | } |
| 1729 | |
Stephen White | bf6137e | 2017-01-04 15:43:26 -0500 | [diff] [blame] | 1730 | void remove_non_boundary_edges(const VertexList& mesh, SkPath::FillType fillType, |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 1731 | SkArenaAlloc& alloc) { |
Stephen White | 4978906 | 2017-02-21 10:35:49 -0500 | [diff] [blame] | 1732 | LOG("removing non-boundary edges\n"); |
| 1733 | EdgeList activeEdges; |
Stephen White | bf6137e | 2017-01-04 15:43:26 -0500 | [diff] [blame] | 1734 | for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) { |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 1735 | if (!connected(v)) { |
Stephen White | 4978906 | 2017-02-21 10:35:49 -0500 | [diff] [blame] | 1736 | continue; |
| 1737 | } |
| 1738 | Edge* leftEnclosingEdge; |
| 1739 | Edge* rightEnclosingEdge; |
| 1740 | find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge); |
| 1741 | bool prevFilled = leftEnclosingEdge && |
| 1742 | apply_fill_type(fillType, leftEnclosingEdge->fWinding); |
| 1743 | for (Edge* e = v->fFirstEdgeAbove; e;) { |
| 1744 | Edge* next = e->fNextEdgeAbove; |
| 1745 | remove_edge(e, &activeEdges); |
| 1746 | bool filled = apply_fill_type(fillType, e->fWinding); |
| 1747 | if (filled == prevFilled) { |
Stephen White | e7a364d | 2017-01-11 16:19:26 -0500 | [diff] [blame] | 1748 | disconnect(e); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1749 | } |
Stephen White | 4978906 | 2017-02-21 10:35:49 -0500 | [diff] [blame] | 1750 | prevFilled = filled; |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1751 | e = next; |
| 1752 | } |
Stephen White | 4978906 | 2017-02-21 10:35:49 -0500 | [diff] [blame] | 1753 | Edge* prev = leftEnclosingEdge; |
| 1754 | for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { |
| 1755 | if (prev) { |
| 1756 | e->fWinding += prev->fWinding; |
| 1757 | } |
| 1758 | insert_edge(e, prev, &activeEdges); |
| 1759 | prev = e; |
| 1760 | } |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1761 | } |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1762 | } |
| 1763 | |
Stephen White | 6641212 | 2017-03-01 11:48:27 -0500 | [diff] [blame] | 1764 | // Note: this is the normal to the edge, but not necessarily unit length. |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1765 | void get_edge_normal(const Edge* e, SkVector* normal) { |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1766 | normal->set(SkDoubleToScalar(e->fLine.fA), |
| 1767 | SkDoubleToScalar(e->fLine.fB)); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | // Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions |
| 1771 | // and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to |
| 1772 | // invert on stroking. |
| 1773 | |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 1774 | void simplify_boundary(EdgeList* boundary, Comparator& c, SkArenaAlloc& alloc) { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1775 | Edge* prevEdge = boundary->fTail; |
| 1776 | SkVector prevNormal; |
| 1777 | get_edge_normal(prevEdge, &prevNormal); |
| 1778 | for (Edge* e = boundary->fHead; e != nullptr;) { |
| 1779 | Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom; |
| 1780 | Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop; |
Stephen White | cfe1264 | 2018-09-26 17:25:59 -0400 | [diff] [blame] | 1781 | double distPrev = e->dist(prev->fPoint); |
| 1782 | double distNext = prevEdge->dist(next->fPoint); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1783 | SkVector normal; |
| 1784 | get_edge_normal(e, &normal); |
Stephen White | cfe1264 | 2018-09-26 17:25:59 -0400 | [diff] [blame] | 1785 | constexpr double kQuarterPixelSq = 0.25f * 0.25f; |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 1786 | if (prev == next) { |
| 1787 | remove_edge(prevEdge, boundary); |
| 1788 | remove_edge(e, boundary); |
| 1789 | prevEdge = boundary->fTail; |
| 1790 | e = boundary->fHead; |
| 1791 | if (prevEdge) { |
| 1792 | get_edge_normal(prevEdge, &prevNormal); |
| 1793 | } |
| 1794 | } else if (prevNormal.dot(normal) < 0.0 && |
Stephen White | cfe1264 | 2018-09-26 17:25:59 -0400 | [diff] [blame] | 1795 | (distPrev * distPrev <= kQuarterPixelSq || distNext * distNext <= kQuarterPixelSq)) { |
Stephen White | bf6137e | 2017-01-04 15:43:26 -0500 | [diff] [blame] | 1796 | Edge* join = new_edge(prev, next, Edge::Type::kInner, c, alloc); |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1797 | if (prev->fPoint != next->fPoint) { |
| 1798 | join->fLine.normalize(); |
| 1799 | join->fLine = join->fLine * join->fWinding; |
| 1800 | } |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1801 | insert_edge(join, e, boundary); |
| 1802 | remove_edge(prevEdge, boundary); |
| 1803 | remove_edge(e, boundary); |
| 1804 | if (join->fLeft && join->fRight) { |
| 1805 | prevEdge = join->fLeft; |
| 1806 | e = join; |
| 1807 | } else { |
| 1808 | prevEdge = boundary->fTail; |
| 1809 | e = boundary->fHead; // join->fLeft ? join->fLeft : join; |
| 1810 | } |
| 1811 | get_edge_normal(prevEdge, &prevNormal); |
| 1812 | } else { |
| 1813 | prevEdge = e; |
| 1814 | prevNormal = normal; |
| 1815 | e = e->fRight; |
| 1816 | } |
| 1817 | } |
| 1818 | } |
| 1819 | |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 1820 | void ss_connect(Vertex* v, Vertex* dest, Comparator& c, SkArenaAlloc& alloc) { |
| 1821 | if (v == dest) { |
| 1822 | return; |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1823 | } |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 1824 | LOG("ss_connecting vertex %g to vertex %g\n", v->fID, dest->fID); |
| 1825 | if (v->fSynthetic) { |
| 1826 | connect(v, dest, Edge::Type::kConnector, c, alloc, 0); |
| 1827 | } else if (v->fPartner) { |
| 1828 | LOG("setting %g's partner to %g ", v->fPartner->fID, dest->fID); |
| 1829 | LOG("and %g's partner to null\n", v->fID); |
| 1830 | v->fPartner->fPartner = dest; |
| 1831 | v->fPartner = nullptr; |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1832 | } |
| 1833 | } |
| 1834 | |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 1835 | void Event::apply(VertexList* mesh, Comparator& c, EventList* events, SkArenaAlloc& alloc) { |
| 1836 | if (!fEdge) { |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1837 | return; |
| 1838 | } |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 1839 | Vertex* prev = fEdge->fPrev->fVertex; |
| 1840 | Vertex* next = fEdge->fNext->fVertex; |
| 1841 | SSEdge* prevEdge = fEdge->fPrev->fPrev; |
| 1842 | SSEdge* nextEdge = fEdge->fNext->fNext; |
| 1843 | if (!prevEdge || !nextEdge || !prevEdge->fEdge || !nextEdge->fEdge) { |
| 1844 | return; |
Stephen White | 77169c8 | 2018-06-05 09:15:59 -0400 | [diff] [blame] | 1845 | } |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 1846 | Vertex* dest = create_sorted_vertex(fPoint, fAlpha, mesh, prev, c, alloc); |
| 1847 | dest->fSynthetic = true; |
| 1848 | SSVertex* ssv = alloc.make<SSVertex>(dest); |
| 1849 | LOG("collapsing %g, %g (original edge %g -> %g) to %g (%g, %g) alpha %d\n", |
| 1850 | prev->fID, next->fID, fEdge->fEdge->fTop->fID, fEdge->fEdge->fBottom->fID, |
| 1851 | dest->fID, fPoint.fX, fPoint.fY, fAlpha); |
| 1852 | fEdge->fEdge = nullptr; |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1853 | |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 1854 | ss_connect(prev, dest, c, alloc); |
| 1855 | ss_connect(next, dest, c, alloc); |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1856 | |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 1857 | prevEdge->fNext = nextEdge->fPrev = ssv; |
| 1858 | ssv->fPrev = prevEdge; |
| 1859 | ssv->fNext = nextEdge; |
| 1860 | if (!prevEdge->fEdge || !nextEdge->fEdge) { |
| 1861 | return; |
| 1862 | } |
| 1863 | if (prevEdge->fEvent) { |
| 1864 | prevEdge->fEvent->fEdge = nullptr; |
| 1865 | } |
| 1866 | if (nextEdge->fEvent) { |
| 1867 | nextEdge->fEvent->fEdge = nullptr; |
| 1868 | } |
| 1869 | if (prevEdge->fPrev == nextEdge->fNext) { |
| 1870 | ss_connect(prevEdge->fPrev->fVertex, dest, c, alloc); |
| 1871 | prevEdge->fEdge = nextEdge->fEdge = nullptr; |
| 1872 | } else { |
| 1873 | compute_bisector(prevEdge->fEdge, nextEdge->fEdge, dest, alloc); |
| 1874 | SkASSERT(prevEdge != fEdge && nextEdge != fEdge); |
| 1875 | if (dest->fPartner) { |
| 1876 | create_event(prevEdge, events, alloc); |
| 1877 | create_event(nextEdge, events, alloc); |
| 1878 | } else { |
| 1879 | create_event(prevEdge, prevEdge->fPrev->fVertex, nextEdge, dest, events, c, alloc); |
| 1880 | create_event(nextEdge, nextEdge->fNext->fVertex, prevEdge, dest, events, c, alloc); |
| 1881 | } |
| 1882 | } |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1883 | } |
| 1884 | |
| 1885 | bool is_overlap_edge(Edge* e) { |
| 1886 | if (e->fType == Edge::Type::kOuter) { |
| 1887 | return e->fWinding != 0 && e->fWinding != 1; |
| 1888 | } else if (e->fType == Edge::Type::kInner) { |
| 1889 | return e->fWinding != 0 && e->fWinding != -2; |
| 1890 | } else { |
| 1891 | return false; |
| 1892 | } |
| 1893 | } |
| 1894 | |
| 1895 | // This is a stripped-down version of tessellate() which computes edges which |
| 1896 | // join two filled regions, which represent overlap regions, and collapses them. |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 1897 | bool collapse_overlap_regions(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc, |
| 1898 | EventComparator comp) { |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1899 | LOG("\nfinding overlap regions\n"); |
| 1900 | EdgeList activeEdges; |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 1901 | EventList events(comp); |
| 1902 | SSVertexMap ssVertices; |
| 1903 | SSEdgeList ssEdges; |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1904 | for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) { |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 1905 | if (!connected(v)) { |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1906 | continue; |
| 1907 | } |
| 1908 | Edge* leftEnclosingEdge; |
| 1909 | Edge* rightEnclosingEdge; |
| 1910 | find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge); |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 1911 | for (Edge* e = v->fLastEdgeAbove; e && e != leftEnclosingEdge;) { |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1912 | Edge* prev = e->fPrevEdgeAbove ? e->fPrevEdgeAbove : leftEnclosingEdge; |
| 1913 | remove_edge(e, &activeEdges); |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 1914 | bool leftOverlap = prev && is_overlap_edge(prev); |
| 1915 | bool rightOverlap = is_overlap_edge(e); |
| 1916 | bool isOuterBoundary = e->fType == Edge::Type::kOuter && |
| 1917 | (!prev || prev->fWinding == 0 || e->fWinding == 0); |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1918 | if (prev) { |
| 1919 | e->fWinding -= prev->fWinding; |
| 1920 | } |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 1921 | if (leftOverlap && rightOverlap) { |
| 1922 | LOG("found interior overlap edge %g -> %g, disconnecting\n", |
| 1923 | e->fTop->fID, e->fBottom->fID); |
| 1924 | disconnect(e); |
| 1925 | } else if (leftOverlap || rightOverlap) { |
| 1926 | LOG("found overlap edge %g -> %g%s\n", e->fTop->fID, e->fBottom->fID, |
| 1927 | isOuterBoundary ? ", is outer boundary" : ""); |
| 1928 | Vertex* prevVertex = e->fWinding < 0 ? e->fBottom : e->fTop; |
| 1929 | Vertex* nextVertex = e->fWinding < 0 ? e->fTop : e->fBottom; |
| 1930 | SSVertex* ssPrev = ssVertices[prevVertex]; |
| 1931 | if (!ssPrev) { |
| 1932 | ssPrev = ssVertices[prevVertex] = alloc.make<SSVertex>(prevVertex); |
| 1933 | } |
| 1934 | SSVertex* ssNext = ssVertices[nextVertex]; |
| 1935 | if (!ssNext) { |
| 1936 | ssNext = ssVertices[nextVertex] = alloc.make<SSVertex>(nextVertex); |
| 1937 | } |
| 1938 | SSEdge* ssEdge = alloc.make<SSEdge>(e, ssPrev, ssNext); |
| 1939 | ssEdges.push_back(ssEdge); |
| 1940 | // SkASSERT(!ssPrev->fNext && !ssNext->fPrev); |
| 1941 | ssPrev->fNext = ssNext->fPrev = ssEdge; |
| 1942 | create_event(ssEdge, &events, alloc); |
| 1943 | if (!isOuterBoundary) { |
| 1944 | disconnect(e); |
| 1945 | } |
| 1946 | } |
| 1947 | e = prev; |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1948 | } |
| 1949 | Edge* prev = leftEnclosingEdge; |
| 1950 | for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { |
| 1951 | if (prev) { |
| 1952 | e->fWinding += prev->fWinding; |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1953 | } |
| 1954 | insert_edge(e, prev, &activeEdges); |
| 1955 | prev = e; |
| 1956 | } |
| 1957 | } |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 1958 | bool complex = events.size() > 0; |
| 1959 | |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1960 | LOG("\ncollapsing overlap regions\n"); |
Stephen White | 8a3c059 | 2019-05-29 11:26:16 -0400 | [diff] [blame] | 1961 | LOG("skeleton before:\n"); |
| 1962 | dump_skel(ssEdges); |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 1963 | while (events.size() > 0) { |
| 1964 | Event* event = events.top(); |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1965 | events.pop(); |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 1966 | event->apply(mesh, c, &events, alloc); |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1967 | } |
Stephen White | 8a3c059 | 2019-05-29 11:26:16 -0400 | [diff] [blame] | 1968 | LOG("skeleton after:\n"); |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 1969 | dump_skel(ssEdges); |
| 1970 | for (SSEdge* edge : ssEdges) { |
| 1971 | if (Edge* e = edge->fEdge) { |
| 1972 | connect(edge->fPrev->fVertex, edge->fNext->fVertex, e->fType, c, alloc, 0); |
| 1973 | } |
| 1974 | } |
| 1975 | return complex; |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1976 | } |
| 1977 | |
| 1978 | bool inversion(Vertex* prev, Vertex* next, Edge* origEdge, Comparator& c) { |
| 1979 | if (!prev || !next) { |
| 1980 | return true; |
| 1981 | } |
| 1982 | int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1; |
| 1983 | return winding != origEdge->fWinding; |
| 1984 | } |
Stephen White | 92eba8a | 2017-02-06 09:50:27 -0500 | [diff] [blame] | 1985 | |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1986 | // Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to |
| 1987 | // find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a |
| 1988 | // new antialiased mesh from those vertices. |
| 1989 | |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1990 | void stroke_boundary(EdgeList* boundary, VertexList* innerMesh, VertexList* outerMesh, |
| 1991 | Comparator& c, SkArenaAlloc& alloc) { |
| 1992 | LOG("\nstroking boundary\n"); |
| 1993 | // A boundary with fewer than 3 edges is degenerate. |
| 1994 | if (!boundary->fHead || !boundary->fHead->fRight || !boundary->fHead->fRight->fRight) { |
| 1995 | return; |
| 1996 | } |
| 1997 | Edge* prevEdge = boundary->fTail; |
| 1998 | Vertex* prevV = prevEdge->fWinding > 0 ? prevEdge->fTop : prevEdge->fBottom; |
| 1999 | SkVector prevNormal; |
| 2000 | get_edge_normal(prevEdge, &prevNormal); |
| 2001 | double radius = 0.5; |
| 2002 | Line prevInner(prevEdge->fLine); |
| 2003 | prevInner.fC -= radius; |
| 2004 | Line prevOuter(prevEdge->fLine); |
| 2005 | prevOuter.fC += radius; |
| 2006 | VertexList innerVertices; |
| 2007 | VertexList outerVertices; |
| 2008 | bool innerInversion = true; |
| 2009 | bool outerInversion = true; |
| 2010 | for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) { |
| 2011 | Vertex* v = e->fWinding > 0 ? e->fTop : e->fBottom; |
| 2012 | SkVector normal; |
| 2013 | get_edge_normal(e, &normal); |
| 2014 | Line inner(e->fLine); |
| 2015 | inner.fC -= radius; |
| 2016 | Line outer(e->fLine); |
| 2017 | outer.fC += radius; |
| 2018 | SkPoint innerPoint, outerPoint; |
| 2019 | LOG("stroking vertex %g (%g, %g)\n", v->fID, v->fPoint.fX, v->fPoint.fY); |
| 2020 | if (!prevEdge->fLine.nearParallel(e->fLine) && prevInner.intersect(inner, &innerPoint) && |
| 2021 | prevOuter.intersect(outer, &outerPoint)) { |
| 2022 | float cosAngle = normal.dot(prevNormal); |
| 2023 | if (cosAngle < -kCosMiterAngle) { |
| 2024 | Vertex* nextV = e->fWinding > 0 ? e->fBottom : e->fTop; |
| 2025 | |
| 2026 | // This is a pointy vertex whose angle is smaller than the threshold; miter it. |
| 2027 | Line bisector(innerPoint, outerPoint); |
| 2028 | Line tangent(v->fPoint, v->fPoint + SkPoint::Make(bisector.fA, bisector.fB)); |
| 2029 | if (tangent.fA == 0 && tangent.fB == 0) { |
| 2030 | continue; |
| 2031 | } |
| 2032 | tangent.normalize(); |
| 2033 | Line innerTangent(tangent); |
| 2034 | Line outerTangent(tangent); |
| 2035 | innerTangent.fC -= 0.5; |
| 2036 | outerTangent.fC += 0.5; |
| 2037 | SkPoint innerPoint1, innerPoint2, outerPoint1, outerPoint2; |
| 2038 | if (prevNormal.cross(normal) > 0) { |
| 2039 | // Miter inner points |
| 2040 | if (!innerTangent.intersect(prevInner, &innerPoint1) || |
| 2041 | !innerTangent.intersect(inner, &innerPoint2) || |
| 2042 | !outerTangent.intersect(bisector, &outerPoint)) { |
Stephen White | f470b7e | 2018-01-04 16:45:51 -0500 | [diff] [blame] | 2043 | continue; |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 2044 | } |
| 2045 | Line prevTangent(prevV->fPoint, |
| 2046 | prevV->fPoint + SkVector::Make(prevOuter.fA, prevOuter.fB)); |
| 2047 | Line nextTangent(nextV->fPoint, |
| 2048 | nextV->fPoint + SkVector::Make(outer.fA, outer.fB)); |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 2049 | if (prevTangent.dist(outerPoint) > 0) { |
Stephen White | 4f34fca | 2018-01-11 16:14:04 -0500 | [diff] [blame] | 2050 | bisector.intersect(prevTangent, &outerPoint); |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 2051 | } |
| 2052 | if (nextTangent.dist(outerPoint) < 0) { |
Stephen White | 4f34fca | 2018-01-11 16:14:04 -0500 | [diff] [blame] | 2053 | bisector.intersect(nextTangent, &outerPoint); |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 2054 | } |
| 2055 | outerPoint1 = outerPoint2 = outerPoint; |
| 2056 | } else { |
| 2057 | // Miter outer points |
| 2058 | if (!outerTangent.intersect(prevOuter, &outerPoint1) || |
| 2059 | !outerTangent.intersect(outer, &outerPoint2)) { |
Stephen White | f470b7e | 2018-01-04 16:45:51 -0500 | [diff] [blame] | 2060 | continue; |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 2061 | } |
| 2062 | Line prevTangent(prevV->fPoint, |
| 2063 | prevV->fPoint + SkVector::Make(prevInner.fA, prevInner.fB)); |
| 2064 | Line nextTangent(nextV->fPoint, |
| 2065 | nextV->fPoint + SkVector::Make(inner.fA, inner.fB)); |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 2066 | if (prevTangent.dist(innerPoint) > 0) { |
Stephen White | 4f34fca | 2018-01-11 16:14:04 -0500 | [diff] [blame] | 2067 | bisector.intersect(prevTangent, &innerPoint); |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 2068 | } |
| 2069 | if (nextTangent.dist(innerPoint) < 0) { |
Stephen White | 4f34fca | 2018-01-11 16:14:04 -0500 | [diff] [blame] | 2070 | bisector.intersect(nextTangent, &innerPoint); |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 2071 | } |
| 2072 | innerPoint1 = innerPoint2 = innerPoint; |
| 2073 | } |
Stephen White | ea49523 | 2018-04-03 11:28:15 -0400 | [diff] [blame] | 2074 | if (!innerPoint1.isFinite() || !innerPoint2.isFinite() || |
| 2075 | !outerPoint1.isFinite() || !outerPoint2.isFinite()) { |
| 2076 | continue; |
| 2077 | } |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 2078 | LOG("inner (%g, %g), (%g, %g), ", |
| 2079 | innerPoint1.fX, innerPoint1.fY, innerPoint2.fX, innerPoint2.fY); |
| 2080 | LOG("outer (%g, %g), (%g, %g)\n", |
| 2081 | outerPoint1.fX, outerPoint1.fY, outerPoint2.fX, outerPoint2.fY); |
| 2082 | Vertex* innerVertex1 = alloc.make<Vertex>(innerPoint1, 255); |
| 2083 | Vertex* innerVertex2 = alloc.make<Vertex>(innerPoint2, 255); |
| 2084 | Vertex* outerVertex1 = alloc.make<Vertex>(outerPoint1, 0); |
| 2085 | Vertex* outerVertex2 = alloc.make<Vertex>(outerPoint2, 0); |
| 2086 | innerVertex1->fPartner = outerVertex1; |
| 2087 | innerVertex2->fPartner = outerVertex2; |
| 2088 | outerVertex1->fPartner = innerVertex1; |
| 2089 | outerVertex2->fPartner = innerVertex2; |
| 2090 | if (!inversion(innerVertices.fTail, innerVertex1, prevEdge, c)) { |
| 2091 | innerInversion = false; |
| 2092 | } |
| 2093 | if (!inversion(outerVertices.fTail, outerVertex1, prevEdge, c)) { |
| 2094 | outerInversion = false; |
| 2095 | } |
| 2096 | innerVertices.append(innerVertex1); |
| 2097 | innerVertices.append(innerVertex2); |
| 2098 | outerVertices.append(outerVertex1); |
| 2099 | outerVertices.append(outerVertex2); |
| 2100 | } else { |
| 2101 | LOG("inner (%g, %g), ", innerPoint.fX, innerPoint.fY); |
| 2102 | LOG("outer (%g, %g)\n", outerPoint.fX, outerPoint.fY); |
| 2103 | Vertex* innerVertex = alloc.make<Vertex>(innerPoint, 255); |
| 2104 | Vertex* outerVertex = alloc.make<Vertex>(outerPoint, 0); |
| 2105 | innerVertex->fPartner = outerVertex; |
| 2106 | outerVertex->fPartner = innerVertex; |
| 2107 | if (!inversion(innerVertices.fTail, innerVertex, prevEdge, c)) { |
| 2108 | innerInversion = false; |
| 2109 | } |
| 2110 | if (!inversion(outerVertices.fTail, outerVertex, prevEdge, c)) { |
| 2111 | outerInversion = false; |
| 2112 | } |
| 2113 | innerVertices.append(innerVertex); |
| 2114 | outerVertices.append(outerVertex); |
| 2115 | } |
| 2116 | } |
| 2117 | prevInner = inner; |
| 2118 | prevOuter = outer; |
| 2119 | prevV = v; |
| 2120 | prevEdge = e; |
| 2121 | prevNormal = normal; |
| 2122 | } |
| 2123 | if (!inversion(innerVertices.fTail, innerVertices.fHead, prevEdge, c)) { |
| 2124 | innerInversion = false; |
| 2125 | } |
| 2126 | if (!inversion(outerVertices.fTail, outerVertices.fHead, prevEdge, c)) { |
| 2127 | outerInversion = false; |
| 2128 | } |
| 2129 | // Outer edges get 1 winding, and inner edges get -2 winding. This ensures that the interior |
| 2130 | // is always filled (1 + -2 = -1 for normal cases, 1 + 2 = 3 for thin features where the |
| 2131 | // interior inverts). |
| 2132 | // For total inversion cases, the shape has now reversed handedness, so invert the winding |
| 2133 | // so it will be detected during collapse_overlap_regions(). |
| 2134 | int innerWinding = innerInversion ? 2 : -2; |
| 2135 | int outerWinding = outerInversion ? -1 : 1; |
| 2136 | for (Vertex* v = innerVertices.fHead; v && v->fNext; v = v->fNext) { |
| 2137 | connect(v, v->fNext, Edge::Type::kInner, c, alloc, innerWinding); |
| 2138 | } |
| 2139 | connect(innerVertices.fTail, innerVertices.fHead, Edge::Type::kInner, c, alloc, innerWinding); |
| 2140 | for (Vertex* v = outerVertices.fHead; v && v->fNext; v = v->fNext) { |
| 2141 | connect(v, v->fNext, Edge::Type::kOuter, c, alloc, outerWinding); |
| 2142 | } |
| 2143 | connect(outerVertices.fTail, outerVertices.fHead, Edge::Type::kOuter, c, alloc, outerWinding); |
| 2144 | innerMesh->append(innerVertices); |
| 2145 | outerMesh->append(outerVertices); |
| 2146 | } |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2147 | |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 2148 | void extract_boundary(EdgeList* boundary, Edge* e, SkPath::FillType fillType, SkArenaAlloc& alloc) { |
Stephen White | 95152e1 | 2017-12-18 10:52:44 -0500 | [diff] [blame] | 2149 | LOG("\nextracting boundary\n"); |
Stephen White | 4978906 | 2017-02-21 10:35:49 -0500 | [diff] [blame] | 2150 | bool down = apply_fill_type(fillType, e->fWinding); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2151 | while (e) { |
| 2152 | e->fWinding = down ? 1 : -1; |
| 2153 | Edge* next; |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 2154 | e->fLine.normalize(); |
| 2155 | e->fLine = e->fLine * e->fWinding; |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2156 | boundary->append(e); |
| 2157 | if (down) { |
| 2158 | // Find outgoing edge, in clockwise order. |
| 2159 | if ((next = e->fNextEdgeAbove)) { |
| 2160 | down = false; |
| 2161 | } else if ((next = e->fBottom->fLastEdgeBelow)) { |
| 2162 | down = true; |
| 2163 | } else if ((next = e->fPrevEdgeAbove)) { |
| 2164 | down = false; |
| 2165 | } |
| 2166 | } else { |
| 2167 | // Find outgoing edge, in counter-clockwise order. |
| 2168 | if ((next = e->fPrevEdgeBelow)) { |
| 2169 | down = true; |
| 2170 | } else if ((next = e->fTop->fFirstEdgeAbove)) { |
| 2171 | down = false; |
| 2172 | } else if ((next = e->fNextEdgeBelow)) { |
| 2173 | down = true; |
| 2174 | } |
| 2175 | } |
Stephen White | e7a364d | 2017-01-11 16:19:26 -0500 | [diff] [blame] | 2176 | disconnect(e); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2177 | e = next; |
| 2178 | } |
| 2179 | } |
| 2180 | |
Stephen White | 5ad721e | 2017-02-23 16:50:47 -0500 | [diff] [blame] | 2181 | // 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] | 2182 | |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2183 | void extract_boundaries(const VertexList& inMesh, VertexList* innerVertices, |
| 2184 | VertexList* outerVertices, SkPath::FillType fillType, |
Stephen White | 5ad721e | 2017-02-23 16:50:47 -0500 | [diff] [blame] | 2185 | Comparator& c, SkArenaAlloc& alloc) { |
| 2186 | remove_non_boundary_edges(inMesh, fillType, alloc); |
| 2187 | for (Vertex* v = inMesh.fHead; v; v = v->fNext) { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2188 | while (v->fFirstEdgeBelow) { |
Stephen White | 5ad721e | 2017-02-23 16:50:47 -0500 | [diff] [blame] | 2189 | EdgeList boundary; |
| 2190 | extract_boundary(&boundary, v->fFirstEdgeBelow, fillType, alloc); |
| 2191 | simplify_boundary(&boundary, c, alloc); |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2192 | stroke_boundary(&boundary, innerVertices, outerVertices, c, alloc); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2193 | } |
| 2194 | } |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2195 | } |
| 2196 | |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2197 | // This is a driver function that calls stages 2-5 in turn. |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2198 | |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 2199 | void contours_to_mesh(VertexList* contours, int contourCnt, bool antialias, |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 2200 | VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2201 | #if LOGGING_ENABLED |
| 2202 | for (int i = 0; i < contourCnt; ++i) { |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 2203 | Vertex* v = contours[i].fHead; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2204 | SkASSERT(v); |
| 2205 | 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] | 2206 | for (v = v->fNext; v; v = v->fNext) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2207 | LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY); |
| 2208 | } |
| 2209 | } |
| 2210 | #endif |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2211 | sanitize_contours(contours, contourCnt, antialias); |
Stephen White | bf6137e | 2017-01-04 15:43:26 -0500 | [diff] [blame] | 2212 | build_edges(contours, contourCnt, mesh, c, alloc); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2213 | } |
| 2214 | |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2215 | void sort_mesh(VertexList* vertices, Comparator& c, SkArenaAlloc& alloc) { |
Stephen White | bf6137e | 2017-01-04 15:43:26 -0500 | [diff] [blame] | 2216 | if (!vertices || !vertices->fHead) { |
Stephen White | 2f4686f | 2017-01-03 16:20:01 -0500 | [diff] [blame] | 2217 | return; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2218 | } |
| 2219 | |
| 2220 | // Sort vertices in Y (secondarily in X). |
Stephen White | 16a40cb | 2017-02-23 11:10:01 -0500 | [diff] [blame] | 2221 | if (c.fDirection == Comparator::Direction::kHorizontal) { |
| 2222 | merge_sort<sweep_lt_horiz>(vertices); |
| 2223 | } else { |
| 2224 | merge_sort<sweep_lt_vert>(vertices); |
| 2225 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2226 | #if LOGGING_ENABLED |
Stephen White | 2e2cb9b | 2017-01-09 13:11:18 -0500 | [diff] [blame] | 2227 | for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2228 | static float gID = 0.0f; |
| 2229 | v->fID = gID++; |
| 2230 | } |
| 2231 | #endif |
Stephen White | 2f4686f | 2017-01-03 16:20:01 -0500 | [diff] [blame] | 2232 | } |
| 2233 | |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 2234 | Poly* contours_to_polys(VertexList* contours, int contourCnt, SkPath::FillType fillType, |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2235 | const SkRect& pathBounds, bool antialias, VertexList* outerMesh, |
Herb Derby | 5cdc9dd | 2017-02-13 12:10:46 -0500 | [diff] [blame] | 2236 | SkArenaAlloc& alloc) { |
Stephen White | 16a40cb | 2017-02-23 11:10:01 -0500 | [diff] [blame] | 2237 | Comparator c(pathBounds.width() > pathBounds.height() ? Comparator::Direction::kHorizontal |
| 2238 | : Comparator::Direction::kVertical); |
Stephen White | bf6137e | 2017-01-04 15:43:26 -0500 | [diff] [blame] | 2239 | VertexList mesh; |
| 2240 | contours_to_mesh(contours, contourCnt, antialias, &mesh, c, alloc); |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2241 | sort_mesh(&mesh, c, alloc); |
| 2242 | merge_coincident_vertices(&mesh, c, alloc); |
Stephen White | 0cb3167 | 2017-06-08 14:41:01 -0400 | [diff] [blame] | 2243 | simplify(&mesh, c, alloc); |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 2244 | LOG("\nsimplified mesh:\n"); |
| 2245 | dump_mesh(mesh); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2246 | if (antialias) { |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2247 | VertexList innerMesh; |
| 2248 | extract_boundaries(mesh, &innerMesh, outerMesh, fillType, c, alloc); |
| 2249 | sort_mesh(&innerMesh, c, alloc); |
| 2250 | sort_mesh(outerMesh, c, alloc); |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 2251 | merge_coincident_vertices(&innerMesh, c, alloc); |
| 2252 | bool was_complex = merge_coincident_vertices(outerMesh, c, alloc); |
| 2253 | was_complex = simplify(&innerMesh, c, alloc) || was_complex; |
| 2254 | was_complex = simplify(outerMesh, c, alloc) || was_complex; |
| 2255 | LOG("\ninner mesh before:\n"); |
| 2256 | dump_mesh(innerMesh); |
| 2257 | LOG("\nouter mesh before:\n"); |
| 2258 | dump_mesh(*outerMesh); |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 2259 | EventComparator eventLT(EventComparator::Op::kLessThan); |
| 2260 | EventComparator eventGT(EventComparator::Op::kGreaterThan); |
| 2261 | was_complex = collapse_overlap_regions(&innerMesh, c, alloc, eventLT) || was_complex; |
| 2262 | was_complex = collapse_overlap_regions(outerMesh, c, alloc, eventGT) || was_complex; |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 2263 | if (was_complex) { |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2264 | LOG("found complex mesh; taking slow path\n"); |
| 2265 | VertexList aaMesh; |
Stephen White | 95152e1 | 2017-12-18 10:52:44 -0500 | [diff] [blame] | 2266 | LOG("\ninner mesh after:\n"); |
| 2267 | dump_mesh(innerMesh); |
| 2268 | LOG("\nouter mesh after:\n"); |
| 2269 | dump_mesh(*outerMesh); |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2270 | connect_partners(outerMesh, c, alloc); |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 2271 | connect_partners(&innerMesh, c, alloc); |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2272 | sorted_merge(&innerMesh, outerMesh, &aaMesh, c); |
| 2273 | merge_coincident_vertices(&aaMesh, c, alloc); |
Stephen White | 0cb3167 | 2017-06-08 14:41:01 -0400 | [diff] [blame] | 2274 | simplify(&aaMesh, c, alloc); |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 2275 | LOG("combined and simplified mesh:\n"); |
Stephen White | 95152e1 | 2017-12-18 10:52:44 -0500 | [diff] [blame] | 2276 | dump_mesh(aaMesh); |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2277 | outerMesh->fHead = outerMesh->fTail = nullptr; |
| 2278 | return tessellate(aaMesh, alloc); |
| 2279 | } else { |
| 2280 | LOG("no complex polygons; taking fast path\n"); |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2281 | return tessellate(innerMesh, alloc); |
| 2282 | } |
Stephen White | 4978906 | 2017-02-21 10:35:49 -0500 | [diff] [blame] | 2283 | } else { |
| 2284 | return tessellate(mesh, alloc); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2285 | } |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2286 | } |
| 2287 | |
| 2288 | // Stage 6: Triangulate the monotone polygons into a vertex buffer. |
Brian Osman | 0995fd5 | 2019-01-09 09:52:25 -0500 | [diff] [blame] | 2289 | void* polys_to_triangles(Poly* polys, SkPath::FillType fillType, bool emitCoverage, void* data) { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2290 | for (Poly* poly = polys; poly; poly = poly->fNext) { |
| 2291 | if (apply_fill_type(fillType, poly)) { |
Brian Osman | 0995fd5 | 2019-01-09 09:52:25 -0500 | [diff] [blame] | 2292 | data = poly->emit(emitCoverage, data); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2293 | } |
| 2294 | } |
| 2295 | return data; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2296 | } |
| 2297 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2298 | Poly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds, |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2299 | int contourCnt, SkArenaAlloc& alloc, bool antialias, bool* isLinear, |
| 2300 | VertexList* outerMesh) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2301 | SkPath::FillType fillType = path.getFillType(); |
| 2302 | if (SkPath::IsInverseFillType(fillType)) { |
| 2303 | contourCnt++; |
| 2304 | } |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 2305 | std::unique_ptr<VertexList[]> contours(new VertexList[contourCnt]); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2306 | |
| 2307 | path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinear); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2308 | return contours_to_polys(contours.get(), contourCnt, path.getFillType(), path.getBounds(), |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2309 | antialias, outerMesh, alloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2310 | } |
| 2311 | |
Stephen White | 11f65e0 | 2017-02-16 19:00:39 -0500 | [diff] [blame] | 2312 | int get_contour_count(const SkPath& path, SkScalar tolerance) { |
| 2313 | int contourCnt; |
| 2314 | int maxPts = GrPathUtils::worstCasePointCount(path, &contourCnt, tolerance); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2315 | if (maxPts <= 0) { |
Stephen White | 11f65e0 | 2017-02-16 19:00:39 -0500 | [diff] [blame] | 2316 | return 0; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2317 | } |
Stephen White | 11f65e0 | 2017-02-16 19:00:39 -0500 | [diff] [blame] | 2318 | return contourCnt; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2319 | } |
| 2320 | |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 2321 | int64_t count_points(Poly* polys, SkPath::FillType fillType) { |
| 2322 | int64_t count = 0; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2323 | for (Poly* poly = polys; poly; poly = poly->fNext) { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2324 | if (apply_fill_type(fillType, poly) && poly->fCount >= 3) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2325 | count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3); |
| 2326 | } |
| 2327 | } |
| 2328 | return count; |
| 2329 | } |
| 2330 | |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 2331 | int64_t count_outer_mesh_points(const VertexList& outerMesh) { |
| 2332 | int64_t count = 0; |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2333 | for (Vertex* v = outerMesh.fHead; v; v = v->fNext) { |
| 2334 | for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { |
| 2335 | count += TESSELLATOR_WIREFRAME ? 12 : 6; |
| 2336 | } |
| 2337 | } |
| 2338 | return count; |
| 2339 | } |
| 2340 | |
Brian Osman | 0995fd5 | 2019-01-09 09:52:25 -0500 | [diff] [blame] | 2341 | void* outer_mesh_to_triangles(const VertexList& outerMesh, bool emitCoverage, void* data) { |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2342 | for (Vertex* v = outerMesh.fHead; v; v = v->fNext) { |
| 2343 | for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { |
| 2344 | Vertex* v0 = e->fTop; |
| 2345 | Vertex* v1 = e->fBottom; |
| 2346 | Vertex* v2 = e->fBottom->fPartner; |
| 2347 | Vertex* v3 = e->fTop->fPartner; |
Brian Osman | 0995fd5 | 2019-01-09 09:52:25 -0500 | [diff] [blame] | 2348 | data = emit_triangle(v0, v1, v2, emitCoverage, data); |
| 2349 | data = emit_triangle(v0, v2, v3, emitCoverage, data); |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2350 | } |
| 2351 | } |
| 2352 | return data; |
| 2353 | } |
| 2354 | |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2355 | } // namespace |
| 2356 | |
| 2357 | namespace GrTessellator { |
| 2358 | |
| 2359 | // Stage 6: Triangulate the monotone polygons into a vertex buffer. |
| 2360 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2361 | int PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds, |
Brian Osman | 0995fd5 | 2019-01-09 09:52:25 -0500 | [diff] [blame] | 2362 | VertexAllocator* vertexAllocator, bool antialias, bool* isLinear) { |
Stephen White | 11f65e0 | 2017-02-16 19:00:39 -0500 | [diff] [blame] | 2363 | int contourCnt = get_contour_count(path, tolerance); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2364 | if (contourCnt <= 0) { |
| 2365 | *isLinear = true; |
| 2366 | return 0; |
| 2367 | } |
Stephen White | 11f65e0 | 2017-02-16 19:00:39 -0500 | [diff] [blame] | 2368 | SkArenaAlloc alloc(kArenaChunkSize); |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2369 | VertexList outerMesh; |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2370 | Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, antialias, |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2371 | isLinear, &outerMesh); |
senorblanco | 7ab96e9 | 2016-10-12 06:47:44 -0700 | [diff] [blame] | 2372 | SkPath::FillType fillType = antialias ? SkPath::kWinding_FillType : path.getFillType(); |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 2373 | int64_t count64 = count_points(polys, fillType); |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2374 | if (antialias) { |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 2375 | count64 += count_outer_mesh_points(outerMesh); |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2376 | } |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 2377 | if (0 == count64 || count64 > SK_MaxS32) { |
Stephen White | ff60b17 | 2017-05-05 15:54:52 -0400 | [diff] [blame] | 2378 | return 0; |
| 2379 | } |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 2380 | int count = count64; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2381 | |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2382 | void* verts = vertexAllocator->lock(count); |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 2383 | if (!verts) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2384 | SkDebugf("Could not allocate vertices\n"); |
| 2385 | return 0; |
| 2386 | } |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2387 | |
| 2388 | LOG("emitting %d verts\n", count); |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 2389 | void* end = polys_to_triangles(polys, fillType, antialias, verts); |
| 2390 | end = outer_mesh_to_triangles(outerMesh, true, end); |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 2391 | |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2392 | int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts)) |
| 2393 | / vertexAllocator->stride()); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2394 | SkASSERT(actualCount <= count); |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 2395 | vertexAllocator->unlock(actualCount); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2396 | return actualCount; |
| 2397 | } |
| 2398 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2399 | int PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds, |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2400 | GrTessellator::WindingVertex** verts) { |
Stephen White | 11f65e0 | 2017-02-16 19:00:39 -0500 | [diff] [blame] | 2401 | int contourCnt = get_contour_count(path, tolerance); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2402 | if (contourCnt <= 0) { |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 2403 | *verts = nullptr; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2404 | return 0; |
| 2405 | } |
Stephen White | 11f65e0 | 2017-02-16 19:00:39 -0500 | [diff] [blame] | 2406 | SkArenaAlloc alloc(kArenaChunkSize); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2407 | bool isLinear; |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 2408 | Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, false, &isLinear, |
| 2409 | nullptr); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2410 | SkPath::FillType fillType = path.getFillType(); |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 2411 | int64_t count64 = count_points(polys, fillType); |
| 2412 | if (0 == count64 || count64 > SK_MaxS32) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2413 | *verts = nullptr; |
| 2414 | return 0; |
| 2415 | } |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 2416 | int count = count64; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2417 | |
| 2418 | *verts = new GrTessellator::WindingVertex[count]; |
| 2419 | GrTessellator::WindingVertex* vertsEnd = *verts; |
| 2420 | SkPoint* points = new SkPoint[count]; |
| 2421 | SkPoint* pointsEnd = points; |
| 2422 | for (Poly* poly = polys; poly; poly = poly->fNext) { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 2423 | if (apply_fill_type(fillType, poly)) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2424 | SkPoint* start = pointsEnd; |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 2425 | pointsEnd = static_cast<SkPoint*>(poly->emit(false, pointsEnd)); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 2426 | while (start != pointsEnd) { |
| 2427 | vertsEnd->fPos = *start; |
| 2428 | vertsEnd->fWinding = poly->fWinding; |
| 2429 | ++start; |
| 2430 | ++vertsEnd; |
| 2431 | } |
| 2432 | } |
| 2433 | } |
| 2434 | int actualCount = static_cast<int>(vertsEnd - *verts); |
| 2435 | SkASSERT(actualCount <= count); |
| 2436 | SkASSERT(pointsEnd - points == actualCount); |
| 2437 | delete[] points; |
| 2438 | return actualCount; |
| 2439 | } |
| 2440 | |
| 2441 | } // namespace |