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