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