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