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