ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Chris Dalton | 17dc418 | 2020-03-25 16:18:16 -0600 | [diff] [blame] | 8 | #include "src/gpu/GrTriangulator.h" |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 9 | |
Chris Dalton | d081dce | 2020-01-23 12:09:04 -0700 | [diff] [blame] | 10 | #include "src/gpu/GrEagerVertexAllocator.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/gpu/GrVertexWriter.h" |
Michael Ludwig | 663afe5 | 2019-06-03 16:46:19 -0400 | [diff] [blame] | 12 | #include "src/gpu/geometry/GrPathUtils.h" |
Chris Dalton | d60c919 | 2021-01-07 18:30:08 +0000 | [diff] [blame] | 13 | |
| 14 | #include "src/core/SkGeometry.h" |
| 15 | #include "src/core/SkPointPriv.h" |
| 16 | |
Stephen White | 94b7e54 | 2018-01-04 14:01:10 -0500 | [diff] [blame] | 17 | #include <algorithm> |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 18 | |
Chris Dalton | d60c919 | 2021-01-07 18:30:08 +0000 | [diff] [blame] | 19 | |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 20 | #if TRIANGULATOR_LOGGING |
Chris Dalton | d60c919 | 2021-01-07 18:30:08 +0000 | [diff] [blame] | 21 | #define TESS_LOG printf |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 22 | #define DUMP_MESH(M) (M).dump() |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 23 | #else |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 24 | #define TESS_LOG(...) |
Chris Dalton | 7cf3add | 2021-01-11 18:33:28 -0700 | [diff] [blame] | 25 | #define DUMP_MESH(M) |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 26 | #endif |
| 27 | |
Chris Dalton | 17ce8c5 | 2021-01-07 18:08:46 -0700 | [diff] [blame] | 28 | using EdgeType = GrTriangulator::EdgeType; |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 29 | using Vertex = GrTriangulator::Vertex; |
| 30 | using VertexList = GrTriangulator::VertexList; |
Chris Dalton | 17ce8c5 | 2021-01-07 18:08:46 -0700 | [diff] [blame] | 31 | using Line = GrTriangulator::Line; |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 32 | using Edge = GrTriangulator::Edge; |
| 33 | using EdgeList = GrTriangulator::EdgeList; |
| 34 | using Poly = GrTriangulator::Poly; |
Chris Dalton | 17ce8c5 | 2021-01-07 18:08:46 -0700 | [diff] [blame] | 35 | using MonotonePoly = GrTriangulator::MonotonePoly; |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 36 | using Comparator = GrTriangulator::Comparator; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 37 | |
| 38 | template <class T, T* T::*Prev, T* T::*Next> |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 39 | 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] | 40 | t->*Prev = prev; |
| 41 | t->*Next = next; |
| 42 | if (prev) { |
| 43 | prev->*Next = t; |
| 44 | } else if (head) { |
| 45 | *head = t; |
| 46 | } |
| 47 | if (next) { |
| 48 | next->*Prev = t; |
| 49 | } else if (tail) { |
| 50 | *tail = t; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | template <class T, T* T::*Prev, T* T::*Next> |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 55 | static void list_remove(T* t, T** head, T** tail) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 56 | if (t->*Prev) { |
| 57 | t->*Prev->*Next = t->*Next; |
| 58 | } else if (head) { |
| 59 | *head = t->*Next; |
| 60 | } |
| 61 | if (t->*Next) { |
| 62 | t->*Next->*Prev = t->*Prev; |
| 63 | } else if (tail) { |
| 64 | *tail = t->*Prev; |
| 65 | } |
| 66 | t->*Prev = t->*Next = nullptr; |
| 67 | } |
| 68 | |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 69 | typedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b); |
| 70 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 71 | static bool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) { |
Stephen White | 16a40cb | 2017-02-23 11:10:01 -0500 | [diff] [blame] | 72 | return a.fX < b.fX || (a.fX == b.fX && a.fY > b.fY); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 75 | static bool sweep_lt_vert(const SkPoint& a, const SkPoint& b) { |
Stephen White | 16a40cb | 2017-02-23 11:10:01 -0500 | [diff] [blame] | 76 | return a.fY < b.fY || (a.fY == b.fY && a.fX < b.fX); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 79 | bool GrTriangulator::Comparator::sweep_lt(const SkPoint& a, const SkPoint& b) const { |
| 80 | return fDirection == Direction::kHorizontal ? sweep_lt_horiz(a, b) : sweep_lt_vert(a, b); |
| 81 | } |
Stephen White | 16a40cb | 2017-02-23 11:10:01 -0500 | [diff] [blame] | 82 | |
Chris Dalton | d60c919 | 2021-01-07 18:30:08 +0000 | [diff] [blame] | 83 | static inline void* emit_vertex(Vertex* v, bool emitCoverage, void* data) { |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 84 | GrVertexWriter verts{data}; |
| 85 | verts.write(v->fPoint); |
| 86 | |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 87 | if (emitCoverage) { |
| 88 | verts.write(GrNormalizeByteToFloat(v->fAlpha)); |
| 89 | } |
Brian Osman | 0995fd5 | 2019-01-09 09:52:25 -0500 | [diff] [blame] | 90 | |
Brian Osman | f9aabff | 2018-11-13 16:11:38 -0500 | [diff] [blame] | 91 | return verts.fPtr; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Chris Dalton | d60c919 | 2021-01-07 18:30:08 +0000 | [diff] [blame] | 94 | static void* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, bool emitCoverage, void* data) { |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 95 | TESS_LOG("emit_triangle %g (%g, %g) %d\n", v0->fID, v0->fPoint.fX, v0->fPoint.fY, v0->fAlpha); |
| 96 | TESS_LOG(" %g (%g, %g) %d\n", v1->fID, v1->fPoint.fX, v1->fPoint.fY, v1->fAlpha); |
| 97 | 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] | 98 | #if TESSELLATOR_WIREFRAME |
Brian Osman | 0995fd5 | 2019-01-09 09:52:25 -0500 | [diff] [blame] | 99 | data = emit_vertex(v0, emitCoverage, data); |
| 100 | data = emit_vertex(v1, emitCoverage, data); |
| 101 | data = emit_vertex(v1, emitCoverage, data); |
| 102 | data = emit_vertex(v2, emitCoverage, data); |
| 103 | data = emit_vertex(v2, emitCoverage, data); |
| 104 | data = emit_vertex(v0, emitCoverage, data); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 105 | #else |
Brian Osman | 0995fd5 | 2019-01-09 09:52:25 -0500 | [diff] [blame] | 106 | data = emit_vertex(v0, emitCoverage, data); |
| 107 | data = emit_vertex(v1, emitCoverage, data); |
| 108 | data = emit_vertex(v2, emitCoverage, data); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 109 | #endif |
| 110 | return data; |
| 111 | } |
| 112 | |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 113 | void GrTriangulator::VertexList::insert(Vertex* v, Vertex* prev, Vertex* next) { |
| 114 | list_insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, prev, next, &fHead, &fTail); |
| 115 | } |
| 116 | |
| 117 | void GrTriangulator::VertexList::remove(Vertex* v) { |
| 118 | list_remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, &fHead, &fTail); |
| 119 | } |
senorblanco | e6eaa32 | 2016-03-08 09:06:44 -0800 | [diff] [blame] | 120 | |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 121 | // Round to nearest quarter-pixel. This is used for screenspace tessellation. |
| 122 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 123 | static inline void round(SkPoint* p) { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 124 | p->fX = SkScalarRoundToScalar(p->fX * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f); |
| 125 | p->fY = SkScalarRoundToScalar(p->fY * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f); |
| 126 | } |
| 127 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 128 | static inline SkScalar double_to_clamped_scalar(double d) { |
Stephen White | 94b7e54 | 2018-01-04 14:01:10 -0500 | [diff] [blame] | 129 | return SkDoubleToScalar(std::min((double) SK_ScalarMax, std::max(d, (double) -SK_ScalarMax))); |
| 130 | } |
| 131 | |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 132 | bool GrTriangulator::Line::intersect(const Line& other, SkPoint* point) const { |
| 133 | double denom = fA * other.fB - fB * other.fA; |
| 134 | if (denom == 0.0) { |
| 135 | return false; |
senorblanco | 49df8d1 | 2016-10-07 08:36:56 -0700 | [diff] [blame] | 136 | } |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 137 | double scale = 1.0 / denom; |
| 138 | point->fX = double_to_clamped_scalar((fB * other.fC - other.fB * fC) * scale); |
| 139 | point->fY = double_to_clamped_scalar((other.fA * fC - fA * other.fC) * scale); |
| 140 | round(point); |
| 141 | return point->isFinite(); |
| 142 | } |
Chris Dalton | d60c919 | 2021-01-07 18:30:08 +0000 | [diff] [blame] | 143 | |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 144 | bool GrTriangulator::Edge::intersect(const Edge& other, SkPoint* p, uint8_t* alpha) const { |
| 145 | TESS_LOG("intersecting %g -> %g with %g -> %g\n", |
| 146 | fTop->fID, fBottom->fID, other.fTop->fID, other.fBottom->fID); |
| 147 | if (fTop == other.fTop || fBottom == other.fBottom) { |
| 148 | return false; |
Chris Dalton | d60c919 | 2021-01-07 18:30:08 +0000 | [diff] [blame] | 149 | } |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 150 | double denom = fLine.fA * other.fLine.fB - fLine.fB * other.fLine.fA; |
| 151 | if (denom == 0.0) { |
| 152 | return false; |
Chris Dalton | d60c919 | 2021-01-07 18:30:08 +0000 | [diff] [blame] | 153 | } |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 154 | double dx = static_cast<double>(other.fTop->fPoint.fX) - fTop->fPoint.fX; |
| 155 | double dy = static_cast<double>(other.fTop->fPoint.fY) - fTop->fPoint.fY; |
| 156 | double sNumer = dy * other.fLine.fB + dx * other.fLine.fA; |
| 157 | double tNumer = dy * fLine.fB + dx * fLine.fA; |
| 158 | // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early. |
| 159 | // This saves us doing the divide below unless absolutely necessary. |
| 160 | if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom) |
| 161 | : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) { |
| 162 | return false; |
Chris Dalton | d60c919 | 2021-01-07 18:30:08 +0000 | [diff] [blame] | 163 | } |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 164 | double s = sNumer / denom; |
| 165 | SkASSERT(s >= 0.0 && s <= 1.0); |
| 166 | p->fX = SkDoubleToScalar(fTop->fPoint.fX - s * fLine.fB); |
| 167 | p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fLine.fA); |
| 168 | if (alpha) { |
| 169 | if (fType == EdgeType::kConnector) { |
| 170 | *alpha = (1.0 - s) * fTop->fAlpha + s * fBottom->fAlpha; |
| 171 | } else if (other.fType == EdgeType::kConnector) { |
| 172 | double t = tNumer / denom; |
| 173 | *alpha = (1.0 - t) * other.fTop->fAlpha + t * other.fBottom->fAlpha; |
| 174 | } else if (fType == EdgeType::kOuter && other.fType == EdgeType::kOuter) { |
| 175 | *alpha = 0; |
| 176 | } else { |
| 177 | *alpha = 255; |
| 178 | } |
Chris Dalton | d60c919 | 2021-01-07 18:30:08 +0000 | [diff] [blame] | 179 | } |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 180 | return true; |
| 181 | } |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 182 | |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 183 | void GrTriangulator::EdgeList::insert(Edge* edge, Edge* prev, Edge* next) { |
| 184 | list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &fHead, &fTail); |
| 185 | } |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 186 | |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 187 | void GrTriangulator::EdgeList::remove(Edge* edge) { |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 188 | TESS_LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID); |
| 189 | SkASSERT(this->contains(edge)); |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 190 | list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &fHead, &fTail); |
| 191 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 192 | |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 193 | void GrTriangulator::MonotonePoly::addEdge(Edge* edge) { |
| 194 | if (fSide == kRight_Side) { |
| 195 | SkASSERT(!edge->fUsedInRightPoly); |
| 196 | list_insert<Edge, &Edge::fRightPolyPrev, &Edge::fRightPolyNext>( |
| 197 | edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge); |
| 198 | edge->fUsedInRightPoly = true; |
| 199 | } else { |
| 200 | SkASSERT(!edge->fUsedInLeftPoly); |
| 201 | list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>( |
| 202 | edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge); |
| 203 | edge->fUsedInLeftPoly = true; |
Chris Dalton | 17ce8c5 | 2021-01-07 18:08:46 -0700 | [diff] [blame] | 204 | } |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 205 | } |
Chris Dalton | 17ce8c5 | 2021-01-07 18:08:46 -0700 | [diff] [blame] | 206 | |
Chris Dalton | 9a4904f | 2021-01-07 19:10:14 -0700 | [diff] [blame] | 207 | void* GrTriangulator::emitMonotonePoly(const MonotonePoly* monotonePoly, void* data) { |
Chris Dalton | 93c2d81 | 2021-01-11 19:51:59 -0700 | [diff] [blame] | 208 | SkASSERT(monotonePoly->fWinding != 0); |
Chris Dalton | 9a4904f | 2021-01-07 19:10:14 -0700 | [diff] [blame] | 209 | Edge* e = monotonePoly->fFirstEdge; |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 210 | VertexList vertices; |
| 211 | vertices.append(e->fTop); |
| 212 | int count = 1; |
| 213 | while (e != nullptr) { |
Chris Dalton | 9a4904f | 2021-01-07 19:10:14 -0700 | [diff] [blame] | 214 | if (kRight_Side == monotonePoly->fSide) { |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 215 | vertices.append(e->fBottom); |
| 216 | e = e->fRightPolyNext; |
| 217 | } else { |
| 218 | vertices.prepend(e->fBottom); |
| 219 | e = e->fLeftPolyNext; |
Chris Dalton | 17ce8c5 | 2021-01-07 18:08:46 -0700 | [diff] [blame] | 220 | } |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 221 | count++; |
| 222 | } |
| 223 | Vertex* first = vertices.fHead; |
| 224 | Vertex* v = first->fNext; |
| 225 | while (v != vertices.fTail) { |
| 226 | SkASSERT(v && v->fPrev && v->fNext); |
| 227 | Vertex* prev = v->fPrev; |
| 228 | Vertex* curr = v; |
| 229 | Vertex* next = v->fNext; |
| 230 | if (count == 3) { |
Chris Dalton | 9a4904f | 2021-01-07 19:10:14 -0700 | [diff] [blame] | 231 | return this->emitTriangle(prev, curr, next, monotonePoly->fWinding, data); |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 232 | } |
| 233 | double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX; |
| 234 | double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY; |
| 235 | double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX; |
| 236 | double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY; |
| 237 | if (ax * by - ay * bx >= 0.0) { |
Chris Dalton | 9a4904f | 2021-01-07 19:10:14 -0700 | [diff] [blame] | 238 | data = this->emitTriangle(prev, curr, next, monotonePoly->fWinding, data); |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 239 | v->fPrev->fNext = v->fNext; |
| 240 | v->fNext->fPrev = v->fPrev; |
| 241 | count--; |
| 242 | if (v->fPrev == first) { |
Chris Dalton | 17ce8c5 | 2021-01-07 18:08:46 -0700 | [diff] [blame] | 243 | v = v->fNext; |
Chris Dalton | d60c919 | 2021-01-07 18:30:08 +0000 | [diff] [blame] | 244 | } else { |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 245 | v = v->fPrev; |
Chris Dalton | d60c919 | 2021-01-07 18:30:08 +0000 | [diff] [blame] | 246 | } |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 247 | } else { |
| 248 | v = v->fNext; |
Chris Dalton | d60c919 | 2021-01-07 18:30:08 +0000 | [diff] [blame] | 249 | } |
Chris Dalton | 75887a2 | 2021-01-06 00:23:13 -0700 | [diff] [blame] | 250 | } |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 251 | return data; |
| 252 | } |
| 253 | |
Chris Dalton | 9a4904f | 2021-01-07 19:10:14 -0700 | [diff] [blame] | 254 | void* GrTriangulator::emitTriangle(Vertex* prev, Vertex* curr, Vertex* next, int winding, |
| 255 | void* data) const { |
Chris Dalton | 93c2d81 | 2021-01-11 19:51:59 -0700 | [diff] [blame] | 256 | if (winding > 0) { |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 257 | // Ensure our triangles always wind in the same direction as if the path had been |
| 258 | // triangulated as a simple fan (a la red book). |
| 259 | std::swap(prev, next); |
| 260 | } |
Chris Dalton | 93c2d81 | 2021-01-11 19:51:59 -0700 | [diff] [blame] | 261 | return emit_triangle(prev, curr, next, fEmitCoverage, data); |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | Poly* GrTriangulator::Poly::addEdge(Edge* e, Side side, SkArenaAlloc& alloc) { |
| 265 | TESS_LOG("addEdge (%g -> %g) to poly %d, %s side\n", |
| 266 | e->fTop->fID, e->fBottom->fID, fID, side == kLeft_Side ? "left" : "right"); |
| 267 | Poly* partner = fPartner; |
| 268 | Poly* poly = this; |
| 269 | if (side == kRight_Side) { |
| 270 | if (e->fUsedInRightPoly) { |
| 271 | return this; |
Chris Dalton | d60c919 | 2021-01-07 18:30:08 +0000 | [diff] [blame] | 272 | } |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 273 | } else { |
| 274 | if (e->fUsedInLeftPoly) { |
| 275 | return this; |
Chris Dalton | d60c919 | 2021-01-07 18:30:08 +0000 | [diff] [blame] | 276 | } |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 277 | } |
| 278 | if (partner) { |
| 279 | fPartner = partner->fPartner = nullptr; |
| 280 | } |
| 281 | if (!fTail) { |
| 282 | fHead = fTail = alloc.make<MonotonePoly>(e, side, fWinding); |
| 283 | fCount += 2; |
| 284 | } else if (e->fBottom == fTail->fLastEdge->fBottom) { |
| 285 | return poly; |
| 286 | } else if (side == fTail->fSide) { |
| 287 | fTail->addEdge(e); |
| 288 | fCount++; |
| 289 | } else { |
| 290 | e = alloc.make<Edge>(fTail->fLastEdge->fBottom, e->fBottom, 1, EdgeType::kInner); |
| 291 | fTail->addEdge(e); |
| 292 | fCount++; |
| 293 | if (partner) { |
| 294 | partner->addEdge(e, side, alloc); |
| 295 | poly = partner; |
| 296 | } else { |
| 297 | MonotonePoly* m = alloc.make<MonotonePoly>(e, side, fWinding); |
| 298 | m->fPrev = fTail; |
| 299 | fTail->fNext = m; |
| 300 | fTail = m; |
| 301 | } |
| 302 | } |
| 303 | return poly; |
| 304 | } |
Chris Dalton | 9a4904f | 2021-01-07 19:10:14 -0700 | [diff] [blame] | 305 | void* GrTriangulator::emitPoly(const Poly* poly, void *data) { |
| 306 | if (poly->fCount < 3) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 307 | return data; |
| 308 | } |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 309 | TESS_LOG("emit() %d, size %d\n", fID, fCount); |
Chris Dalton | 9a4904f | 2021-01-07 19:10:14 -0700 | [diff] [blame] | 310 | for (MonotonePoly* m = poly->fHead; m != nullptr; m = m->fNext) { |
| 311 | data = this->emitMonotonePoly(m, data); |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 312 | } |
| 313 | return data; |
| 314 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 315 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 316 | static bool coincident(const SkPoint& a, const SkPoint& b) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 317 | return a == b; |
| 318 | } |
| 319 | |
Chris Dalton | 7cf3add | 2021-01-11 18:33:28 -0700 | [diff] [blame] | 320 | Poly* GrTriangulator::makePoly(Poly** head, Vertex* v, int winding) { |
| 321 | Poly* poly = fAlloc.make<Poly>(v, winding); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 322 | poly->fNext = *head; |
| 323 | *head = poly; |
| 324 | return poly; |
| 325 | } |
| 326 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 327 | void GrTriangulator::appendPointToContour(const SkPoint& p, VertexList* contour) { |
| 328 | Vertex* v = fAlloc.make<Vertex>(p, 255); |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 329 | #if TRIANGULATOR_LOGGING |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 330 | static float gID = 0.0f; |
| 331 | v->fID = gID++; |
| 332 | #endif |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 333 | contour->append(v); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 336 | 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] | 337 | SkQuadCoeff quad(pts); |
| 338 | SkPoint p0 = to_point(quad.eval(t - 0.5f * u)); |
| 339 | SkPoint mid = to_point(quad.eval(t)); |
| 340 | SkPoint p1 = to_point(quad.eval(t + 0.5f * u)); |
Stephen White | e3a0be7 | 2017-06-12 11:43:18 -0400 | [diff] [blame] | 341 | if (!p0.isFinite() || !mid.isFinite() || !p1.isFinite()) { |
| 342 | return 0; |
| 343 | } |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 344 | return SkPointPriv::DistanceToLineSegmentBetweenSqd(mid, p0, p1); |
Stephen White | 36e4f06 | 2017-03-27 16:11:31 -0400 | [diff] [blame] | 345 | } |
| 346 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 347 | void GrTriangulator::appendQuadraticToContour(const SkPoint pts[3], SkScalar toleranceSqd, |
| 348 | VertexList* contour) { |
Stephen White | 36e4f06 | 2017-03-27 16:11:31 -0400 | [diff] [blame] | 349 | SkQuadCoeff quad(pts); |
| 350 | Sk2s aa = quad.fA * quad.fA; |
| 351 | SkScalar denom = 2.0f * (aa[0] + aa[1]); |
| 352 | Sk2s ab = quad.fA * quad.fB; |
| 353 | SkScalar t = denom ? (-ab[0] - ab[1]) / denom : 0.0f; |
| 354 | int nPoints = 1; |
Stephen White | e40c361 | 2018-01-09 11:49:08 -0500 | [diff] [blame] | 355 | SkScalar u = 1.0f; |
Stephen White | 36e4f06 | 2017-03-27 16:11:31 -0400 | [diff] [blame] | 356 | // Test possible subdivision values only at the point of maximum curvature. |
| 357 | // If it passes the flatness metric there, it'll pass everywhere. |
Stephen White | e40c361 | 2018-01-09 11:49:08 -0500 | [diff] [blame] | 358 | while (nPoints < GrPathUtils::kMaxPointsPerCurve) { |
Stephen White | 36e4f06 | 2017-03-27 16:11:31 -0400 | [diff] [blame] | 359 | u = 1.0f / nPoints; |
| 360 | if (quad_error_at(pts, t, u) < toleranceSqd) { |
| 361 | break; |
| 362 | } |
| 363 | nPoints++; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 364 | } |
Stephen White | 36e4f06 | 2017-03-27 16:11:31 -0400 | [diff] [blame] | 365 | for (int j = 1; j <= nPoints; j++) { |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 366 | this->appendPointToContour(to_point(quad.eval(j * u)), contour); |
Stephen White | 36e4f06 | 2017-03-27 16:11:31 -0400 | [diff] [blame] | 367 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 368 | } |
| 369 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 370 | void GrTriangulator::generateCubicPoints(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2, |
| 371 | const SkPoint& p3, SkScalar tolSqd, VertexList* contour, |
| 372 | int pointsLeft) { |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 373 | SkScalar d1 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p1, p0, p3); |
| 374 | SkScalar d2 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p2, p0, p3); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 375 | if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) || |
| 376 | !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) { |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 377 | this->appendPointToContour(p3, contour); |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 378 | return; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 379 | } |
| 380 | const SkPoint q[] = { |
| 381 | { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) }, |
| 382 | { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) }, |
| 383 | { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) } |
| 384 | }; |
| 385 | const SkPoint r[] = { |
| 386 | { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) }, |
| 387 | { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) } |
| 388 | }; |
| 389 | const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) }; |
| 390 | pointsLeft >>= 1; |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 391 | this->generateCubicPoints(p0, q[0], r[0], s, tolSqd, contour, pointsLeft); |
| 392 | this->generateCubicPoints(s, r[1], q[2], p3, tolSqd, contour, pointsLeft); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | // Stage 1: convert the input path to a set of linear contours (linked list of Vertices). |
| 396 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 397 | void GrTriangulator::pathToContours(float tolerance, const SkRect& clipBounds, |
| 398 | VertexList* contours) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 399 | SkScalar toleranceSqd = tolerance * tolerance; |
Chris Dalton | 7156db2 | 2020-05-07 22:06:28 +0000 | [diff] [blame] | 400 | SkPoint pts[4]; |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 401 | fIsLinear = true; |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 402 | VertexList* contour = contours; |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 403 | SkPath::Iter iter(fPath, false); |
| 404 | if (fPath.isInverseFillType()) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 405 | SkPoint quad[4]; |
| 406 | clipBounds.toQuad(quad); |
senorblanco | 7ab96e9 | 2016-10-12 06:47:44 -0700 | [diff] [blame] | 407 | for (int i = 3; i >= 0; i--) { |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 408 | this->appendPointToContour(quad[i], contours); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 409 | } |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 410 | contour++; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 411 | } |
| 412 | SkAutoConicToQuads converter; |
Chris Dalton | 7156db2 | 2020-05-07 22:06:28 +0000 | [diff] [blame] | 413 | SkPath::Verb verb; |
| 414 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 415 | switch (verb) { |
Chris Dalton | 7156db2 | 2020-05-07 22:06:28 +0000 | [diff] [blame] | 416 | case SkPath::kConic_Verb: { |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 417 | fIsLinear = false; |
Chris Dalton | 854ee85 | 2021-01-05 15:12:59 -0700 | [diff] [blame] | 418 | if (fSimpleInnerPolygons) { |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 419 | this->appendPointToContour(pts[2], contour); |
Chris Dalton | 6ccc032 | 2020-01-29 11:38:16 -0700 | [diff] [blame] | 420 | break; |
| 421 | } |
Chris Dalton | 7156db2 | 2020-05-07 22:06:28 +0000 | [diff] [blame] | 422 | SkScalar weight = iter.conicWeight(); |
| 423 | const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 424 | for (int i = 0; i < converter.countQuads(); ++i) { |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 425 | this->appendQuadraticToContour(quadPts, toleranceSqd, contour); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 426 | quadPts += 2; |
| 427 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 428 | break; |
| 429 | } |
Chris Dalton | 7156db2 | 2020-05-07 22:06:28 +0000 | [diff] [blame] | 430 | case SkPath::kMove_Verb: |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 431 | if (contour->fHead) { |
| 432 | contour++; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 433 | } |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 434 | this->appendPointToContour(pts[0], contour); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 435 | break; |
Chris Dalton | 7156db2 | 2020-05-07 22:06:28 +0000 | [diff] [blame] | 436 | case SkPath::kLine_Verb: { |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 437 | this->appendPointToContour(pts[1], contour); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 438 | break; |
| 439 | } |
Chris Dalton | 7156db2 | 2020-05-07 22:06:28 +0000 | [diff] [blame] | 440 | case SkPath::kQuad_Verb: { |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 441 | fIsLinear = false; |
Chris Dalton | 854ee85 | 2021-01-05 15:12:59 -0700 | [diff] [blame] | 442 | if (fSimpleInnerPolygons) { |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 443 | this->appendPointToContour(pts[2], contour); |
Chris Dalton | 6ccc032 | 2020-01-29 11:38:16 -0700 | [diff] [blame] | 444 | break; |
| 445 | } |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 446 | this->appendQuadraticToContour(pts, toleranceSqd, contour); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 447 | break; |
| 448 | } |
Chris Dalton | 7156db2 | 2020-05-07 22:06:28 +0000 | [diff] [blame] | 449 | case SkPath::kCubic_Verb: { |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 450 | fIsLinear = false; |
Chris Dalton | 854ee85 | 2021-01-05 15:12:59 -0700 | [diff] [blame] | 451 | if (fSimpleInnerPolygons) { |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 452 | this->appendPointToContour(pts[3], contour); |
Chris Dalton | 6ccc032 | 2020-01-29 11:38:16 -0700 | [diff] [blame] | 453 | break; |
| 454 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 455 | int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance); |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 456 | this->generateCubicPoints(pts[0], pts[1], pts[2], pts[3], toleranceSqd, contour, |
| 457 | pointsLeft); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 458 | break; |
| 459 | } |
Chris Dalton | 7156db2 | 2020-05-07 22:06:28 +0000 | [diff] [blame] | 460 | case SkPath::kClose_Verb: |
| 461 | case SkPath::kDone_Verb: |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 462 | break; |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 467 | static inline bool apply_fill_type(SkPathFillType fillType, int winding) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 468 | switch (fillType) { |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 469 | case SkPathFillType::kWinding: |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 470 | return winding != 0; |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 471 | case SkPathFillType::kEvenOdd: |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 472 | return (winding & 1) != 0; |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 473 | case SkPathFillType::kInverseWinding: |
senorblanco | 7ab96e9 | 2016-10-12 06:47:44 -0700 | [diff] [blame] | 474 | return winding == 1; |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 475 | case SkPathFillType::kInverseEvenOdd: |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 476 | return (winding & 1) == 1; |
| 477 | default: |
| 478 | SkASSERT(false); |
| 479 | return false; |
| 480 | } |
| 481 | } |
| 482 | |
Chris Dalton | 47114db | 2021-01-06 00:35:20 -0700 | [diff] [blame^] | 483 | bool GrTriangulator::applyFillType(int winding) { |
| 484 | return apply_fill_type(fPath.getFillType(), winding); |
| 485 | } |
| 486 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 487 | static inline bool apply_fill_type(SkPathFillType fillType, Poly* poly) { |
Stephen White | 4978906 | 2017-02-21 10:35:49 -0500 | [diff] [blame] | 488 | return poly && apply_fill_type(fillType, poly->fWinding); |
| 489 | } |
| 490 | |
Chris Dalton | 7cf3add | 2021-01-11 18:33:28 -0700 | [diff] [blame] | 491 | Edge* GrTriangulator::makeEdge(Vertex* prev, Vertex* next, EdgeType type, const Comparator& c) { |
Stephen White | 2f4686f | 2017-01-03 16:20:01 -0500 | [diff] [blame] | 492 | int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 493 | Vertex* top = winding < 0 ? next : prev; |
| 494 | Vertex* bottom = winding < 0 ? prev : next; |
Chris Dalton | 7cf3add | 2021-01-11 18:33:28 -0700 | [diff] [blame] | 495 | return fAlloc.make<Edge>(top, bottom, winding, type); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 496 | } |
| 497 | |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 498 | void EdgeList::insert(Edge* edge, Edge* prev) { |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 499 | TESS_LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID); |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 500 | SkASSERT(!this->contains(edge)); |
| 501 | Edge* next = prev ? prev->fRight : fHead; |
| 502 | this->insert(edge, prev, next); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 503 | } |
| 504 | |
Chris Dalton | 47114db | 2021-01-06 00:35:20 -0700 | [diff] [blame^] | 505 | void GrTriangulator::FindEnclosingEdges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) { |
Stephen White | 90732fd | 2017-03-02 16:16:33 -0500 | [diff] [blame] | 506 | if (v->fFirstEdgeAbove && v->fLastEdgeAbove) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 507 | *left = v->fFirstEdgeAbove->fLeft; |
| 508 | *right = v->fLastEdgeAbove->fRight; |
| 509 | return; |
| 510 | } |
| 511 | Edge* next = nullptr; |
| 512 | Edge* prev; |
| 513 | for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) { |
| 514 | if (prev->isLeftOf(v)) { |
| 515 | break; |
| 516 | } |
| 517 | next = prev; |
| 518 | } |
| 519 | *left = prev; |
| 520 | *right = next; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 521 | } |
| 522 | |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 523 | void GrTriangulator::Edge::insertAbove(Vertex* v, const Comparator& c) { |
| 524 | if (fTop->fPoint == fBottom->fPoint || |
| 525 | c.sweep_lt(fBottom->fPoint, fTop->fPoint)) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 526 | return; |
| 527 | } |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 528 | TESS_LOG("insert edge (%g -> %g) above vertex %g\n", fTop->fID, fBottom->fID, v->fID); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 529 | Edge* prev = nullptr; |
| 530 | Edge* next; |
Chris Dalton | d60c919 | 2021-01-07 18:30:08 +0000 | [diff] [blame] | 531 | for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) { |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 532 | if (next->isRightOf(fTop)) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 533 | break; |
| 534 | } |
| 535 | prev = next; |
| 536 | } |
senorblanco | e6eaa32 | 2016-03-08 09:06:44 -0800 | [diff] [blame] | 537 | list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>( |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 538 | this, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 539 | } |
| 540 | |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 541 | void GrTriangulator::Edge::insertBelow(Vertex* v, const Comparator& c) { |
| 542 | if (fTop->fPoint == fBottom->fPoint || |
| 543 | c.sweep_lt(fBottom->fPoint, fTop->fPoint)) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 544 | return; |
| 545 | } |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 546 | TESS_LOG("insert edge (%g -> %g) below vertex %g\n", fTop->fID, fBottom->fID, v->fID); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 547 | Edge* prev = nullptr; |
| 548 | Edge* next; |
Chris Dalton | d60c919 | 2021-01-07 18:30:08 +0000 | [diff] [blame] | 549 | for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) { |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 550 | if (next->isRightOf(fBottom)) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 551 | break; |
| 552 | } |
| 553 | prev = next; |
| 554 | } |
senorblanco | e6eaa32 | 2016-03-08 09:06:44 -0800 | [diff] [blame] | 555 | list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>( |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 556 | this, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 557 | } |
| 558 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 559 | static void remove_edge_above(Edge* edge) { |
Stephen White | 7b37694 | 2018-05-22 11:51:32 -0400 | [diff] [blame] | 560 | SkASSERT(edge->fTop && edge->fBottom); |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 561 | TESS_LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID, |
| 562 | edge->fBottom->fID); |
senorblanco | e6eaa32 | 2016-03-08 09:06:44 -0800 | [diff] [blame] | 563 | list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>( |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 564 | edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove); |
| 565 | } |
| 566 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 567 | static void remove_edge_below(Edge* edge) { |
Stephen White | 7b37694 | 2018-05-22 11:51:32 -0400 | [diff] [blame] | 568 | SkASSERT(edge->fTop && edge->fBottom); |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 569 | TESS_LOG("removing edge (%g -> %g) below vertex %g\n", |
| 570 | edge->fTop->fID, edge->fBottom->fID, edge->fTop->fID); |
senorblanco | e6eaa32 | 2016-03-08 09:06:44 -0800 | [diff] [blame] | 571 | list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>( |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 572 | edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow); |
| 573 | } |
| 574 | |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 575 | void GrTriangulator::Edge::disconnect() { |
| 576 | remove_edge_above(this); |
| 577 | remove_edge_below(this); |
Stephen White | e7a364d | 2017-01-11 16:19:26 -0500 | [diff] [blame] | 578 | } |
| 579 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 580 | static void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, |
Chris Dalton | 811dc6a | 2021-01-07 16:40:32 -0700 | [diff] [blame] | 581 | const Comparator& c); |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 582 | |
Chris Dalton | 811dc6a | 2021-01-07 16:40:32 -0700 | [diff] [blame] | 583 | static void rewind(EdgeList* activeEdges, Vertex** current, Vertex* dst, const Comparator& c) { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 584 | if (!current || *current == dst || c.sweep_lt((*current)->fPoint, dst->fPoint)) { |
| 585 | return; |
| 586 | } |
| 587 | Vertex* v = *current; |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 588 | 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] | 589 | while (v != dst) { |
| 590 | v = v->fPrev; |
| 591 | for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 592 | activeEdges->remove(e); |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 593 | } |
| 594 | Edge* leftEdge = v->fLeftEnclosingEdge; |
| 595 | for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) { |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 596 | activeEdges->insert(e, leftEdge); |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 597 | leftEdge = e; |
Stephen White | c03e698 | 2020-02-06 16:32:14 -0500 | [diff] [blame] | 598 | Vertex* top = e->fTop; |
| 599 | if (c.sweep_lt(top->fPoint, dst->fPoint) && |
| 600 | ((top->fLeftEnclosingEdge && !top->fLeftEnclosingEdge->isLeftOf(e->fTop)) || |
| 601 | (top->fRightEnclosingEdge && !top->fRightEnclosingEdge->isRightOf(e->fTop)))) { |
| 602 | dst = top; |
| 603 | } |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | *current = v; |
| 607 | } |
| 608 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 609 | static void rewind_if_necessary(Edge* edge, EdgeList* activeEdges, Vertex** current, |
Chris Dalton | 811dc6a | 2021-01-07 16:40:32 -0700 | [diff] [blame] | 610 | const Comparator& c) { |
Stephen White | c03e698 | 2020-02-06 16:32:14 -0500 | [diff] [blame] | 611 | if (!activeEdges || !current) { |
| 612 | return; |
| 613 | } |
| 614 | Vertex* top = edge->fTop; |
| 615 | Vertex* bottom = edge->fBottom; |
| 616 | if (edge->fLeft) { |
| 617 | Vertex* leftTop = edge->fLeft->fTop; |
| 618 | Vertex* leftBottom = edge->fLeft->fBottom; |
| 619 | if (c.sweep_lt(leftTop->fPoint, top->fPoint) && !edge->fLeft->isLeftOf(top)) { |
| 620 | rewind(activeEdges, current, leftTop, c); |
| 621 | } else if (c.sweep_lt(top->fPoint, leftTop->fPoint) && !edge->isRightOf(leftTop)) { |
| 622 | rewind(activeEdges, current, top, c); |
| 623 | } else if (c.sweep_lt(bottom->fPoint, leftBottom->fPoint) && |
| 624 | !edge->fLeft->isLeftOf(bottom)) { |
| 625 | rewind(activeEdges, current, leftTop, c); |
| 626 | } else if (c.sweep_lt(leftBottom->fPoint, bottom->fPoint) && !edge->isRightOf(leftBottom)) { |
| 627 | rewind(activeEdges, current, top, c); |
| 628 | } |
| 629 | } |
| 630 | if (edge->fRight) { |
| 631 | Vertex* rightTop = edge->fRight->fTop; |
| 632 | Vertex* rightBottom = edge->fRight->fBottom; |
| 633 | if (c.sweep_lt(rightTop->fPoint, top->fPoint) && !edge->fRight->isRightOf(top)) { |
| 634 | rewind(activeEdges, current, rightTop, c); |
| 635 | } else if (c.sweep_lt(top->fPoint, rightTop->fPoint) && !edge->isLeftOf(rightTop)) { |
| 636 | rewind(activeEdges, current, top, c); |
| 637 | } else if (c.sweep_lt(bottom->fPoint, rightBottom->fPoint) && |
| 638 | !edge->fRight->isRightOf(bottom)) { |
| 639 | rewind(activeEdges, current, rightTop, c); |
| 640 | } else if (c.sweep_lt(rightBottom->fPoint, bottom->fPoint) && |
| 641 | !edge->isLeftOf(rightBottom)) { |
| 642 | rewind(activeEdges, current, top, c); |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | |
Chris Dalton | 811dc6a | 2021-01-07 16:40:32 -0700 | [diff] [blame] | 647 | static void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, |
| 648 | const Comparator& c) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 649 | remove_edge_below(edge); |
| 650 | edge->fTop = v; |
| 651 | edge->recompute(); |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 652 | edge->insertBelow(v, c); |
Stephen White | c03e698 | 2020-02-06 16:32:14 -0500 | [diff] [blame] | 653 | rewind_if_necessary(edge, activeEdges, current, c); |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 654 | merge_collinear_edges(edge, activeEdges, current, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 655 | } |
| 656 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 657 | static void set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, |
Chris Dalton | 811dc6a | 2021-01-07 16:40:32 -0700 | [diff] [blame] | 658 | const Comparator& c) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 659 | remove_edge_above(edge); |
| 660 | edge->fBottom = v; |
| 661 | edge->recompute(); |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 662 | edge->insertAbove(v, c); |
Stephen White | c03e698 | 2020-02-06 16:32:14 -0500 | [diff] [blame] | 663 | rewind_if_necessary(edge, activeEdges, current, c); |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 664 | merge_collinear_edges(edge, activeEdges, current, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 665 | } |
| 666 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 667 | static void merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current, |
Chris Dalton | 811dc6a | 2021-01-07 16:40:32 -0700 | [diff] [blame] | 668 | const Comparator& c) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 669 | if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) { |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 670 | TESS_LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n", |
| 671 | edge->fTop->fPoint.fX, edge->fTop->fPoint.fY, |
| 672 | edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY); |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 673 | rewind(activeEdges, current, edge->fTop, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 674 | other->fWinding += edge->fWinding; |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 675 | edge->disconnect(); |
Stephen White | ec79c39 | 2018-05-18 11:49:21 -0400 | [diff] [blame] | 676 | edge->fTop = edge->fBottom = nullptr; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 677 | } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 678 | rewind(activeEdges, current, edge->fTop, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 679 | other->fWinding += edge->fWinding; |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 680 | set_bottom(edge, other->fTop, activeEdges, current, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 681 | } else { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 682 | rewind(activeEdges, current, other->fTop, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 683 | edge->fWinding += other->fWinding; |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 684 | set_bottom(other, edge->fTop, activeEdges, current, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 688 | static void merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current, |
Chris Dalton | 811dc6a | 2021-01-07 16:40:32 -0700 | [diff] [blame] | 689 | const Comparator& c) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 690 | if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) { |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 691 | TESS_LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n", |
| 692 | edge->fTop->fPoint.fX, edge->fTop->fPoint.fY, |
| 693 | edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY); |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 694 | rewind(activeEdges, current, edge->fTop, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 695 | other->fWinding += edge->fWinding; |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 696 | edge->disconnect(); |
Stephen White | ec79c39 | 2018-05-18 11:49:21 -0400 | [diff] [blame] | 697 | edge->fTop = edge->fBottom = nullptr; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 698 | } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 699 | rewind(activeEdges, current, other->fTop, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 700 | edge->fWinding += other->fWinding; |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 701 | set_top(other, edge->fBottom, activeEdges, current, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 702 | } else { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 703 | rewind(activeEdges, current, edge->fTop, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 704 | other->fWinding += edge->fWinding; |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 705 | set_top(edge, other->fBottom, activeEdges, current, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 706 | } |
| 707 | } |
| 708 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 709 | static bool top_collinear(Edge* left, Edge* right) { |
Stephen White | d26b4d8 | 2018-07-26 10:02:27 -0400 | [diff] [blame] | 710 | if (!left || !right) { |
| 711 | return false; |
| 712 | } |
| 713 | return left->fTop->fPoint == right->fTop->fPoint || |
| 714 | !left->isLeftOf(right->fTop) || !right->isRightOf(left->fTop); |
| 715 | } |
| 716 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 717 | static bool bottom_collinear(Edge* left, Edge* right) { |
Stephen White | d26b4d8 | 2018-07-26 10:02:27 -0400 | [diff] [blame] | 718 | if (!left || !right) { |
| 719 | return false; |
| 720 | } |
| 721 | return left->fBottom->fPoint == right->fBottom->fPoint || |
| 722 | !left->isLeftOf(right->fBottom) || !right->isRightOf(left->fBottom); |
| 723 | } |
| 724 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 725 | static void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current, |
Chris Dalton | 811dc6a | 2021-01-07 16:40:32 -0700 | [diff] [blame] | 726 | const Comparator& c) { |
Stephen White | 6eca90f | 2017-05-25 14:47:11 -0400 | [diff] [blame] | 727 | for (;;) { |
Stephen White | d26b4d8 | 2018-07-26 10:02:27 -0400 | [diff] [blame] | 728 | if (top_collinear(edge->fPrevEdgeAbove, edge)) { |
Stephen White | 24289e0 | 2018-06-29 17:02:21 -0400 | [diff] [blame] | 729 | merge_edges_above(edge->fPrevEdgeAbove, edge, activeEdges, current, c); |
Stephen White | d26b4d8 | 2018-07-26 10:02:27 -0400 | [diff] [blame] | 730 | } else if (top_collinear(edge, edge->fNextEdgeAbove)) { |
Stephen White | 24289e0 | 2018-06-29 17:02:21 -0400 | [diff] [blame] | 731 | merge_edges_above(edge->fNextEdgeAbove, edge, activeEdges, current, c); |
Stephen White | d26b4d8 | 2018-07-26 10:02:27 -0400 | [diff] [blame] | 732 | } else if (bottom_collinear(edge->fPrevEdgeBelow, edge)) { |
Stephen White | 24289e0 | 2018-06-29 17:02:21 -0400 | [diff] [blame] | 733 | merge_edges_below(edge->fPrevEdgeBelow, edge, activeEdges, current, c); |
Stephen White | d26b4d8 | 2018-07-26 10:02:27 -0400 | [diff] [blame] | 734 | } else if (bottom_collinear(edge, edge->fNextEdgeBelow)) { |
Stephen White | 24289e0 | 2018-06-29 17:02:21 -0400 | [diff] [blame] | 735 | merge_edges_below(edge->fNextEdgeBelow, edge, activeEdges, current, c); |
Stephen White | 6eca90f | 2017-05-25 14:47:11 -0400 | [diff] [blame] | 736 | } else { |
| 737 | break; |
| 738 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 739 | } |
Stephen White | d26b4d8 | 2018-07-26 10:02:27 -0400 | [diff] [blame] | 740 | SkASSERT(!top_collinear(edge->fPrevEdgeAbove, edge)); |
| 741 | SkASSERT(!top_collinear(edge, edge->fNextEdgeAbove)); |
| 742 | SkASSERT(!bottom_collinear(edge->fPrevEdgeBelow, edge)); |
| 743 | SkASSERT(!bottom_collinear(edge, edge->fNextEdgeBelow)); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 744 | } |
| 745 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 746 | bool GrTriangulator::splitEdge(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, |
Chris Dalton | 811dc6a | 2021-01-07 16:40:32 -0700 | [diff] [blame] | 747 | const Comparator& c) { |
Stephen White | ec79c39 | 2018-05-18 11:49:21 -0400 | [diff] [blame] | 748 | if (!edge->fTop || !edge->fBottom || v == edge->fTop || v == edge->fBottom) { |
Stephen White | 89042d5 | 2018-06-08 12:18:22 -0400 | [diff] [blame] | 749 | return false; |
Stephen White | 0cb3167 | 2017-06-08 14:41:01 -0400 | [diff] [blame] | 750 | } |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 751 | TESS_LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n", |
| 752 | 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] | 753 | Vertex* top; |
| 754 | Vertex* bottom; |
Stephen White | 531a48e | 2018-06-01 09:49:39 -0400 | [diff] [blame] | 755 | int winding = edge->fWinding; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 756 | if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 757 | top = v; |
| 758 | bottom = edge->fTop; |
| 759 | set_top(edge, v, activeEdges, current, c); |
Stephen White | e30cf80 | 2017-02-27 11:37:55 -0500 | [diff] [blame] | 760 | } else if (c.sweep_lt(edge->fBottom->fPoint, v->fPoint)) { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 761 | top = edge->fBottom; |
| 762 | bottom = v; |
| 763 | set_bottom(edge, v, activeEdges, current, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 764 | } else { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 765 | top = v; |
| 766 | bottom = edge->fBottom; |
| 767 | set_bottom(edge, v, activeEdges, current, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 768 | } |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 769 | Edge* newEdge = fAlloc.make<Edge>(top, bottom, winding, edge->fType); |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 770 | newEdge->insertBelow(top, c); |
| 771 | newEdge->insertAbove(bottom, c); |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 772 | merge_collinear_edges(newEdge, activeEdges, current, c); |
Stephen White | 89042d5 | 2018-06-08 12:18:22 -0400 | [diff] [blame] | 773 | return true; |
| 774 | } |
| 775 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 776 | bool GrTriangulator::intersectEdgePair(Edge* left, Edge* right, EdgeList* activeEdges, |
Chris Dalton | 811dc6a | 2021-01-07 16:40:32 -0700 | [diff] [blame] | 777 | Vertex** current, const Comparator& c) { |
Stephen White | 89042d5 | 2018-06-08 12:18:22 -0400 | [diff] [blame] | 778 | if (!left->fTop || !left->fBottom || !right->fTop || !right->fBottom) { |
| 779 | return false; |
| 780 | } |
Stephen White | 1c5fd18 | 2018-07-12 15:54:05 -0400 | [diff] [blame] | 781 | if (left->fTop == right->fTop || left->fBottom == right->fBottom) { |
| 782 | return false; |
| 783 | } |
Stephen White | 89042d5 | 2018-06-08 12:18:22 -0400 | [diff] [blame] | 784 | if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) { |
| 785 | if (!left->isLeftOf(right->fTop)) { |
Stephen White | 1c5fd18 | 2018-07-12 15:54:05 -0400 | [diff] [blame] | 786 | rewind(activeEdges, current, right->fTop, c); |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 787 | return this->splitEdge(left, right->fTop, activeEdges, current, c); |
Stephen White | 89042d5 | 2018-06-08 12:18:22 -0400 | [diff] [blame] | 788 | } |
| 789 | } else { |
| 790 | if (!right->isRightOf(left->fTop)) { |
Stephen White | 1c5fd18 | 2018-07-12 15:54:05 -0400 | [diff] [blame] | 791 | rewind(activeEdges, current, left->fTop, c); |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 792 | return this->splitEdge(right, left->fTop, activeEdges, current, c); |
Stephen White | 89042d5 | 2018-06-08 12:18:22 -0400 | [diff] [blame] | 793 | } |
| 794 | } |
| 795 | if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) { |
| 796 | if (!left->isLeftOf(right->fBottom)) { |
Stephen White | 1c5fd18 | 2018-07-12 15:54:05 -0400 | [diff] [blame] | 797 | rewind(activeEdges, current, right->fBottom, c); |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 798 | return this->splitEdge(left, right->fBottom, activeEdges, current, c); |
Stephen White | 89042d5 | 2018-06-08 12:18:22 -0400 | [diff] [blame] | 799 | } |
| 800 | } else { |
| 801 | if (!right->isRightOf(left->fBottom)) { |
Stephen White | 1c5fd18 | 2018-07-12 15:54:05 -0400 | [diff] [blame] | 802 | rewind(activeEdges, current, left->fBottom, c); |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 803 | return this->splitEdge(right, left->fBottom, activeEdges, current, c); |
Stephen White | 89042d5 | 2018-06-08 12:18:22 -0400 | [diff] [blame] | 804 | } |
| 805 | } |
| 806 | return false; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 807 | } |
| 808 | |
Chris Dalton | 7cf3add | 2021-01-11 18:33:28 -0700 | [diff] [blame] | 809 | Edge* GrTriangulator::makeConnectingEdge(Vertex* prev, Vertex* next, EdgeType type, |
| 810 | const Comparator& c, int windingScale) { |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 811 | if (!prev || !next || prev->fPoint == next->fPoint) { |
| 812 | return nullptr; |
| 813 | } |
Chris Dalton | 7cf3add | 2021-01-11 18:33:28 -0700 | [diff] [blame] | 814 | Edge* edge = this->makeEdge(prev, next, type, c); |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 815 | edge->insertBelow(edge->fTop, c); |
| 816 | edge->insertAbove(edge->fBottom, c); |
Chris Dalton | 7cf3add | 2021-01-11 18:33:28 -0700 | [diff] [blame] | 817 | edge->fWinding *= windingScale; |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 818 | merge_collinear_edges(edge, nullptr, nullptr, c); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 819 | return edge; |
| 820 | } |
| 821 | |
Chris Dalton | 811dc6a | 2021-01-07 16:40:32 -0700 | [diff] [blame] | 822 | 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] | 823 | TESS_LOG("found coincident verts at %g, %g; merging %g into %g\n", |
| 824 | src->fPoint.fX, src->fPoint.fY, src->fID, dst->fID); |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 825 | dst->fAlpha = std::max(src->fAlpha, dst->fAlpha); |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 826 | if (src->fPartner) { |
| 827 | src->fPartner->fPartner = dst; |
| 828 | } |
Stephen White | 7b37694 | 2018-05-22 11:51:32 -0400 | [diff] [blame] | 829 | while (Edge* edge = src->fFirstEdgeAbove) { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 830 | set_bottom(edge, dst, nullptr, nullptr, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 831 | } |
Stephen White | 7b37694 | 2018-05-22 11:51:32 -0400 | [diff] [blame] | 832 | while (Edge* edge = src->fFirstEdgeBelow) { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 833 | set_top(edge, dst, nullptr, nullptr, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 834 | } |
Stephen White | bf6137e | 2017-01-04 15:43:26 -0500 | [diff] [blame] | 835 | mesh->remove(src); |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 836 | dst->fSynthetic = true; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 837 | } |
| 838 | |
Chris Dalton | 7cf3add | 2021-01-11 18:33:28 -0700 | [diff] [blame] | 839 | Vertex* GrTriangulator::makeSortedVertex(const SkPoint& p, uint8_t alpha, VertexList* mesh, |
| 840 | Vertex* reference, const Comparator& c) { |
Stephen White | 95152e1 | 2017-12-18 10:52:44 -0500 | [diff] [blame] | 841 | Vertex* prevV = reference; |
| 842 | while (prevV && c.sweep_lt(p, prevV->fPoint)) { |
| 843 | prevV = prevV->fPrev; |
| 844 | } |
| 845 | Vertex* nextV = prevV ? prevV->fNext : mesh->fHead; |
| 846 | while (nextV && c.sweep_lt(nextV->fPoint, p)) { |
| 847 | prevV = nextV; |
| 848 | nextV = nextV->fNext; |
| 849 | } |
| 850 | Vertex* v; |
| 851 | if (prevV && coincident(prevV->fPoint, p)) { |
| 852 | v = prevV; |
| 853 | } else if (nextV && coincident(nextV->fPoint, p)) { |
| 854 | v = nextV; |
| 855 | } else { |
Chris Dalton | 7cf3add | 2021-01-11 18:33:28 -0700 | [diff] [blame] | 856 | v = fAlloc.make<Vertex>(p, alpha); |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 857 | #if TRIANGULATOR_LOGGING |
Stephen White | 95152e1 | 2017-12-18 10:52:44 -0500 | [diff] [blame] | 858 | if (!prevV) { |
| 859 | v->fID = mesh->fHead->fID - 1.0f; |
| 860 | } else if (!nextV) { |
| 861 | v->fID = mesh->fTail->fID + 1.0f; |
| 862 | } else { |
| 863 | v->fID = (prevV->fID + nextV->fID) * 0.5f; |
| 864 | } |
| 865 | #endif |
| 866 | mesh->insert(v, prevV, nextV); |
| 867 | } |
| 868 | return v; |
| 869 | } |
| 870 | |
Stephen White | 53a0298 | 2018-05-30 22:47:46 -0400 | [diff] [blame] | 871 | // If an edge's top and bottom points differ only by 1/2 machine epsilon in the primary |
| 872 | // sort criterion, it may not be possible to split correctly, since there is no point which is |
| 873 | // below the top and above the bottom. This function detects that case. |
Chris Dalton | 811dc6a | 2021-01-07 16:40:32 -0700 | [diff] [blame] | 874 | static bool nearly_flat(const Comparator& c, Edge* edge) { |
Stephen White | 53a0298 | 2018-05-30 22:47:46 -0400 | [diff] [blame] | 875 | SkPoint diff = edge->fBottom->fPoint - edge->fTop->fPoint; |
| 876 | float primaryDiff = c.fDirection == Comparator::Direction::kHorizontal ? diff.fX : diff.fY; |
Stephen White | 13f3d8d | 2018-06-22 10:19:20 -0400 | [diff] [blame] | 877 | return fabs(primaryDiff) < std::numeric_limits<float>::epsilon() && primaryDiff != 0.0f; |
Stephen White | 53a0298 | 2018-05-30 22:47:46 -0400 | [diff] [blame] | 878 | } |
| 879 | |
Chris Dalton | 811dc6a | 2021-01-07 16:40:32 -0700 | [diff] [blame] | 880 | static SkPoint clamp(SkPoint p, SkPoint min, SkPoint max, const Comparator& c) { |
Stephen White | e62999f | 2018-06-05 18:45:07 -0400 | [diff] [blame] | 881 | if (c.sweep_lt(p, min)) { |
| 882 | return min; |
| 883 | } else if (c.sweep_lt(max, p)) { |
| 884 | return max; |
| 885 | } else { |
| 886 | return p; |
| 887 | } |
| 888 | } |
| 889 | |
Chris Dalton | 7cf3add | 2021-01-11 18:33:28 -0700 | [diff] [blame] | 890 | void GrTriangulator::computeBisector(Edge* edge1, Edge* edge2, Vertex* v) { |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 891 | Line line1 = edge1->fLine; |
| 892 | Line line2 = edge2->fLine; |
| 893 | line1.normalize(); |
| 894 | line2.normalize(); |
| 895 | double cosAngle = line1.fA * line2.fA + line1.fB * line2.fB; |
| 896 | if (cosAngle > 0.999) { |
| 897 | return; |
| 898 | } |
| 899 | line1.fC += edge1->fWinding > 0 ? -1 : 1; |
| 900 | line2.fC += edge2->fWinding > 0 ? -1 : 1; |
| 901 | SkPoint p; |
| 902 | if (line1.intersect(line2, &p)) { |
Chris Dalton | 17ce8c5 | 2021-01-07 18:08:46 -0700 | [diff] [blame] | 903 | uint8_t alpha = edge1->fType == EdgeType::kOuter ? 255 : 0; |
Chris Dalton | 7cf3add | 2021-01-11 18:33:28 -0700 | [diff] [blame] | 904 | v->fPartner = fAlloc.make<Vertex>(p, alpha); |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 905 | 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] | 906 | } |
| 907 | } |
| 908 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 909 | bool GrTriangulator::checkForIntersection(Edge* left, Edge* right, EdgeList* activeEdges, |
Chris Dalton | 811dc6a | 2021-01-07 16:40:32 -0700 | [diff] [blame] | 910 | Vertex** current, VertexList* mesh, const Comparator& c) { |
Stephen White | b141fcb | 2018-06-14 10:15:47 -0400 | [diff] [blame] | 911 | if (!left || !right) { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 912 | return false; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 913 | } |
Stephen White | 56158ae | 2017-01-30 14:31:31 -0500 | [diff] [blame] | 914 | SkPoint p; |
| 915 | uint8_t alpha; |
Stephen White | b141fcb | 2018-06-14 10:15:47 -0400 | [diff] [blame] | 916 | if (left->intersect(*right, &p, &alpha) && p.isFinite()) { |
Ravi Mistry | bfe9598 | 2018-05-29 18:19:07 +0000 | [diff] [blame] | 917 | Vertex* v; |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 918 | 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] | 919 | Vertex* top = *current; |
| 920 | // If the intersection point is above the current vertex, rewind to the vertex above the |
| 921 | // intersection. |
Stephen White | 0cb3167 | 2017-06-08 14:41:01 -0400 | [diff] [blame] | 922 | while (top && c.sweep_lt(p, top->fPoint)) { |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 923 | top = top->fPrev; |
| 924 | } |
Stephen White | b141fcb | 2018-06-14 10:15:47 -0400 | [diff] [blame] | 925 | if (!nearly_flat(c, left)) { |
| 926 | p = clamp(p, left->fTop->fPoint, left->fBottom->fPoint, c); |
Stephen White | e62999f | 2018-06-05 18:45:07 -0400 | [diff] [blame] | 927 | } |
Stephen White | b141fcb | 2018-06-14 10:15:47 -0400 | [diff] [blame] | 928 | if (!nearly_flat(c, right)) { |
| 929 | p = clamp(p, right->fTop->fPoint, right->fBottom->fPoint, c); |
Stephen White | e62999f | 2018-06-05 18:45:07 -0400 | [diff] [blame] | 930 | } |
Stephen White | b141fcb | 2018-06-14 10:15:47 -0400 | [diff] [blame] | 931 | if (p == left->fTop->fPoint) { |
| 932 | v = left->fTop; |
| 933 | } else if (p == left->fBottom->fPoint) { |
| 934 | v = left->fBottom; |
| 935 | } else if (p == right->fTop->fPoint) { |
| 936 | v = right->fTop; |
| 937 | } else if (p == right->fBottom->fPoint) { |
| 938 | v = right->fBottom; |
Ravi Mistry | bfe9598 | 2018-05-29 18:19:07 +0000 | [diff] [blame] | 939 | } else { |
Chris Dalton | 7cf3add | 2021-01-11 18:33:28 -0700 | [diff] [blame] | 940 | v = this->makeSortedVertex(p, alpha, mesh, top, c); |
Stephen White | b141fcb | 2018-06-14 10:15:47 -0400 | [diff] [blame] | 941 | if (left->fTop->fPartner) { |
Stephen White | c4dbc37 | 2019-05-22 10:50:14 -0400 | [diff] [blame] | 942 | v->fSynthetic = true; |
Chris Dalton | 7cf3add | 2021-01-11 18:33:28 -0700 | [diff] [blame] | 943 | this->computeBisector(left, right, v); |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 944 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 945 | } |
Stephen White | 0cb3167 | 2017-06-08 14:41:01 -0400 | [diff] [blame] | 946 | rewind(activeEdges, current, top ? top : v, c); |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 947 | this->splitEdge(left, v, activeEdges, current, c); |
| 948 | this->splitEdge(right, v, activeEdges, current, c); |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 949 | v->fAlpha = std::max(v->fAlpha, alpha); |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 950 | return true; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 951 | } |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 952 | return this->intersectEdgePair(left, right, activeEdges, current, c); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 953 | } |
| 954 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 955 | void GrTriangulator::sanitizeContours(VertexList* contours, int contourCnt) { |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 956 | for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) { |
| 957 | SkASSERT(contour->fHead); |
| 958 | Vertex* prev = contour->fTail; |
Chris Dalton | 854ee85 | 2021-01-05 15:12:59 -0700 | [diff] [blame] | 959 | if (fRoundVerticesToQuarterPixel) { |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 960 | round(&prev->fPoint); |
Stephen White | 5926f2d | 2017-02-13 13:55:42 -0500 | [diff] [blame] | 961 | } |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 962 | for (Vertex* v = contour->fHead; v;) { |
Chris Dalton | 854ee85 | 2021-01-05 15:12:59 -0700 | [diff] [blame] | 963 | if (fRoundVerticesToQuarterPixel) { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 964 | round(&v->fPoint); |
| 965 | } |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 966 | Vertex* next = v->fNext; |
Stephen White | 3de40f8 | 2018-06-28 09:36:49 -0400 | [diff] [blame] | 967 | Vertex* nextWrap = next ? next : contour->fHead; |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 968 | if (coincident(prev->fPoint, v->fPoint)) { |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 969 | 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] | 970 | contour->remove(v); |
Stephen White | 73e7f80 | 2017-08-23 13:56:07 -0400 | [diff] [blame] | 971 | } else if (!v->fPoint.isFinite()) { |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 972 | 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] | 973 | contour->remove(v); |
Chris Dalton | 854ee85 | 2021-01-05 15:12:59 -0700 | [diff] [blame] | 974 | } else if (fCullCollinearVertices && |
Chris Dalton | 6ccc032 | 2020-01-29 11:38:16 -0700 | [diff] [blame] | 975 | Line(prev->fPoint, nextWrap->fPoint).dist(v->fPoint) == 0.0) { |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 976 | 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] | 977 | contour->remove(v); |
| 978 | } else { |
| 979 | prev = v; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 980 | } |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 981 | v = next; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 982 | } |
| 983 | } |
| 984 | } |
| 985 | |
Chris Dalton | 811dc6a | 2021-01-07 16:40:32 -0700 | [diff] [blame] | 986 | bool GrTriangulator::mergeCoincidentVertices(VertexList* mesh, const Comparator& c) { |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 987 | if (!mesh->fHead) { |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 988 | return false; |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 989 | } |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 990 | bool merged = false; |
| 991 | for (Vertex* v = mesh->fHead->fNext; v;) { |
| 992 | Vertex* next = v->fNext; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 993 | if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) { |
| 994 | v->fPoint = v->fPrev->fPoint; |
| 995 | } |
| 996 | if (coincident(v->fPrev->fPoint, v->fPoint)) { |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 997 | merge_vertices(v, v->fPrev, mesh, c); |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 998 | merged = true; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 999 | } |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1000 | v = next; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1001 | } |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1002 | return merged; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | // Stage 2: convert the contours to a mesh of edges connecting the vertices. |
| 1006 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 1007 | void GrTriangulator::buildEdges(VertexList* contours, int contourCnt, VertexList* mesh, |
Chris Dalton | 811dc6a | 2021-01-07 16:40:32 -0700 | [diff] [blame] | 1008 | const Comparator& c) { |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 1009 | for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) { |
| 1010 | Vertex* prev = contour->fTail; |
| 1011 | for (Vertex* v = contour->fHead; v;) { |
| 1012 | Vertex* next = v->fNext; |
Chris Dalton | 7cf3add | 2021-01-11 18:33:28 -0700 | [diff] [blame] | 1013 | this->makeConnectingEdge(prev, v, EdgeType::kInner, c); |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 1014 | mesh->append(v); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1015 | prev = v; |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 1016 | v = next; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1017 | } |
| 1018 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1019 | } |
| 1020 | |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 1021 | template <CompareFunc sweep_lt> |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 1022 | static void sorted_merge(VertexList* front, VertexList* back, VertexList* result) { |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 1023 | Vertex* a = front->fHead; |
| 1024 | Vertex* b = back->fHead; |
| 1025 | while (a && b) { |
| 1026 | if (sweep_lt(a->fPoint, b->fPoint)) { |
| 1027 | front->remove(a); |
| 1028 | result->append(a); |
| 1029 | a = front->fHead; |
| 1030 | } else { |
| 1031 | back->remove(b); |
| 1032 | result->append(b); |
| 1033 | b = back->fHead; |
| 1034 | } |
| 1035 | } |
| 1036 | result->append(*front); |
| 1037 | result->append(*back); |
| 1038 | } |
| 1039 | |
Chris Dalton | 47114db | 2021-01-06 00:35:20 -0700 | [diff] [blame^] | 1040 | void GrTriangulator::SortedMerge(VertexList* front, VertexList* back, VertexList* result, |
| 1041 | const Comparator& c) { |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 1042 | if (c.fDirection == Comparator::Direction::kHorizontal) { |
| 1043 | sorted_merge<sweep_lt_horiz>(front, back, result); |
| 1044 | } else { |
| 1045 | sorted_merge<sweep_lt_vert>(front, back, result); |
| 1046 | } |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 1047 | #if TRIANGULATOR_LOGGING |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1048 | float id = 0.0f; |
| 1049 | for (Vertex* v = result->fHead; v; v = v->fNext) { |
| 1050 | v->fID = id++; |
| 1051 | } |
| 1052 | #endif |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 1053 | } |
| 1054 | |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1055 | // Stage 3: sort the vertices by increasing sweep direction. |
| 1056 | |
Stephen White | 16a40cb | 2017-02-23 11:10:01 -0500 | [diff] [blame] | 1057 | template <CompareFunc sweep_lt> |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 1058 | static void merge_sort(VertexList* vertices) { |
Stephen White | 16a40cb | 2017-02-23 11:10:01 -0500 | [diff] [blame] | 1059 | Vertex* slow = vertices->fHead; |
| 1060 | if (!slow) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1061 | return; |
| 1062 | } |
Stephen White | 16a40cb | 2017-02-23 11:10:01 -0500 | [diff] [blame] | 1063 | Vertex* fast = slow->fNext; |
| 1064 | if (!fast) { |
| 1065 | return; |
| 1066 | } |
| 1067 | do { |
| 1068 | fast = fast->fNext; |
| 1069 | if (fast) { |
| 1070 | fast = fast->fNext; |
| 1071 | slow = slow->fNext; |
| 1072 | } |
| 1073 | } while (fast); |
| 1074 | VertexList front(vertices->fHead, slow); |
| 1075 | VertexList back(slow->fNext, vertices->fTail); |
| 1076 | front.fTail->fNext = back.fHead->fPrev = nullptr; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1077 | |
Stephen White | 16a40cb | 2017-02-23 11:10:01 -0500 | [diff] [blame] | 1078 | merge_sort<sweep_lt>(&front); |
| 1079 | merge_sort<sweep_lt>(&back); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1080 | |
Stephen White | 16a40cb | 2017-02-23 11:10:01 -0500 | [diff] [blame] | 1081 | vertices->fHead = vertices->fTail = nullptr; |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 1082 | sorted_merge<sweep_lt>(&front, &back, vertices); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1083 | } |
| 1084 | |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 1085 | #if TRIANGULATOR_LOGGING |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 1086 | void VertexList::dump() { |
| 1087 | for (Vertex* v = fHead; v; v = v->fNext) { |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 1088 | 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] | 1089 | if (Vertex* p = v->fPartner) { |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 1090 | TESS_LOG(", partner %g (%g, %g) alpha %d\n", |
| 1091 | p->fID, p->fPoint.fX, p->fPoint.fY, p->fAlpha); |
Stephen White | 95152e1 | 2017-12-18 10:52:44 -0500 | [diff] [blame] | 1092 | } else { |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 1093 | TESS_LOG(", null partner\n"); |
Stephen White | 95152e1 | 2017-12-18 10:52:44 -0500 | [diff] [blame] | 1094 | } |
| 1095 | for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) { |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 1096 | 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] | 1097 | } |
| 1098 | for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 1099 | 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] | 1100 | } |
| 1101 | } |
Chris Dalton | d60c919 | 2021-01-07 18:30:08 +0000 | [diff] [blame] | 1102 | } |
Chris Dalton | 7cf3add | 2021-01-11 18:33:28 -0700 | [diff] [blame] | 1103 | #endif |
Stephen White | 95152e1 | 2017-12-18 10:52:44 -0500 | [diff] [blame] | 1104 | |
Stephen White | 89042d5 | 2018-06-08 12:18:22 -0400 | [diff] [blame] | 1105 | #ifdef SK_DEBUG |
Chris Dalton | 811dc6a | 2021-01-07 16:40:32 -0700 | [diff] [blame] | 1106 | static void validate_edge_pair(Edge* left, Edge* right, const Comparator& c) { |
Stephen White | 89042d5 | 2018-06-08 12:18:22 -0400 | [diff] [blame] | 1107 | if (!left || !right) { |
| 1108 | return; |
| 1109 | } |
| 1110 | if (left->fTop == right->fTop) { |
| 1111 | SkASSERT(left->isLeftOf(right->fBottom)); |
| 1112 | SkASSERT(right->isRightOf(left->fBottom)); |
| 1113 | } else if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) { |
| 1114 | SkASSERT(left->isLeftOf(right->fTop)); |
| 1115 | } else { |
| 1116 | SkASSERT(right->isRightOf(left->fTop)); |
| 1117 | } |
| 1118 | if (left->fBottom == right->fBottom) { |
| 1119 | SkASSERT(left->isLeftOf(right->fTop)); |
| 1120 | SkASSERT(right->isRightOf(left->fTop)); |
| 1121 | } else if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) { |
| 1122 | SkASSERT(left->isLeftOf(right->fBottom)); |
| 1123 | } else { |
| 1124 | SkASSERT(right->isRightOf(left->fBottom)); |
| 1125 | } |
| 1126 | } |
| 1127 | |
Chris Dalton | 811dc6a | 2021-01-07 16:40:32 -0700 | [diff] [blame] | 1128 | static void validate_edge_list(EdgeList* edges, const Comparator& c) { |
Stephen White | 89042d5 | 2018-06-08 12:18:22 -0400 | [diff] [blame] | 1129 | Edge* left = edges->fHead; |
| 1130 | if (!left) { |
| 1131 | return; |
| 1132 | } |
| 1133 | for (Edge* right = left->fRight; right; right = right->fRight) { |
| 1134 | validate_edge_pair(left, right, c); |
| 1135 | left = right; |
| 1136 | } |
| 1137 | } |
| 1138 | #endif |
| 1139 | |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1140 | // Stage 4: Simplify the mesh by inserting new vertices at intersecting edges. |
| 1141 | |
Chris Dalton | 811dc6a | 2021-01-07 16:40:32 -0700 | [diff] [blame] | 1142 | GrTriangulator::SimplifyResult GrTriangulator::simplify(VertexList* mesh, const Comparator& c) { |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 1143 | TESS_LOG("simplifying complex polygons\n"); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1144 | EdgeList activeEdges; |
Chris Dalton | 6ccc032 | 2020-01-29 11:38:16 -0700 | [diff] [blame] | 1145 | auto result = SimplifyResult::kAlreadySimple; |
Stephen White | 0cb3167 | 2017-06-08 14:41:01 -0400 | [diff] [blame] | 1146 | for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) { |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 1147 | if (!v->isConnected()) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1148 | continue; |
| 1149 | } |
Stephen White | 8a0bfc5 | 2017-02-21 15:24:13 -0500 | [diff] [blame] | 1150 | Edge* leftEnclosingEdge; |
| 1151 | Edge* rightEnclosingEdge; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1152 | bool restartChecks; |
| 1153 | do { |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 1154 | TESS_LOG("\nvertex %g: (%g,%g), alpha %d\n", |
| 1155 | v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1156 | restartChecks = false; |
Chris Dalton | 47114db | 2021-01-06 00:35:20 -0700 | [diff] [blame^] | 1157 | FindEnclosingEdges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge); |
Stephen White | 3b5a3fa | 2017-06-06 14:51:19 -0400 | [diff] [blame] | 1158 | v->fLeftEnclosingEdge = leftEnclosingEdge; |
| 1159 | v->fRightEnclosingEdge = rightEnclosingEdge; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1160 | if (v->fFirstEdgeBelow) { |
Stephen White | bf6137e | 2017-01-04 15:43:26 -0500 | [diff] [blame] | 1161 | for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) { |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 1162 | if (this->checkForIntersection( |
| 1163 | leftEnclosingEdge, edge, &activeEdges, &v, mesh, c) || |
| 1164 | this->checkForIntersection( |
| 1165 | edge, rightEnclosingEdge, &activeEdges, &v, mesh, c)) { |
Chris Dalton | 854ee85 | 2021-01-05 15:12:59 -0700 | [diff] [blame] | 1166 | if (fSimpleInnerPolygons) { |
Chris Dalton | 6ccc032 | 2020-01-29 11:38:16 -0700 | [diff] [blame] | 1167 | return SimplifyResult::kAbort; |
| 1168 | } |
| 1169 | result = SimplifyResult::kFoundSelfIntersection; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1170 | restartChecks = true; |
| 1171 | break; |
| 1172 | } |
| 1173 | } |
| 1174 | } else { |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 1175 | if (this->checkForIntersection(leftEnclosingEdge, rightEnclosingEdge, &activeEdges, |
| 1176 | &v, mesh, c)) { |
Chris Dalton | 854ee85 | 2021-01-05 15:12:59 -0700 | [diff] [blame] | 1177 | if (fSimpleInnerPolygons) { |
Chris Dalton | 6ccc032 | 2020-01-29 11:38:16 -0700 | [diff] [blame] | 1178 | return SimplifyResult::kAbort; |
| 1179 | } |
| 1180 | result = SimplifyResult::kFoundSelfIntersection; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1181 | restartChecks = true; |
| 1182 | } |
| 1183 | |
| 1184 | } |
| 1185 | } while (restartChecks); |
Stephen White | 89042d5 | 2018-06-08 12:18:22 -0400 | [diff] [blame] | 1186 | #ifdef SK_DEBUG |
| 1187 | validate_edge_list(&activeEdges, c); |
| 1188 | #endif |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1189 | for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) { |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 1190 | activeEdges.remove(e); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1191 | } |
| 1192 | Edge* leftEdge = leftEnclosingEdge; |
| 1193 | for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 1194 | activeEdges.insert(e, leftEdge); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1195 | leftEdge = e; |
| 1196 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1197 | } |
Stephen White | e260c46 | 2017-12-19 18:09:54 -0500 | [diff] [blame] | 1198 | SkASSERT(!activeEdges.fHead && !activeEdges.fTail); |
Chris Dalton | 6ccc032 | 2020-01-29 11:38:16 -0700 | [diff] [blame] | 1199 | return result; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | // Stage 5: Tessellate the simplified mesh into monotone polygons. |
| 1203 | |
Chris Dalton | 93c2d81 | 2021-01-11 19:51:59 -0700 | [diff] [blame] | 1204 | Poly* GrTriangulator::tessellate(const VertexList& vertices, const Comparator&) { |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 1205 | TESS_LOG("\ntessellating simple polygons\n"); |
Chris Dalton | 6ccc032 | 2020-01-29 11:38:16 -0700 | [diff] [blame] | 1206 | int maxWindMagnitude = std::numeric_limits<int>::max(); |
Chris Dalton | 854ee85 | 2021-01-05 15:12:59 -0700 | [diff] [blame] | 1207 | if (fSimpleInnerPolygons && !SkPathFillType_IsEvenOdd(fPath.getFillType())) { |
Chris Dalton | 6ccc032 | 2020-01-29 11:38:16 -0700 | [diff] [blame] | 1208 | maxWindMagnitude = 1; |
| 1209 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1210 | EdgeList activeEdges; |
| 1211 | Poly* polys = nullptr; |
Stephen White | bf6137e | 2017-01-04 15:43:26 -0500 | [diff] [blame] | 1212 | for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) { |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 1213 | if (!v->isConnected()) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1214 | continue; |
| 1215 | } |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 1216 | #if TRIANGULATOR_LOGGING |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 1217 | 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] | 1218 | #endif |
Stephen White | 8a0bfc5 | 2017-02-21 15:24:13 -0500 | [diff] [blame] | 1219 | Edge* leftEnclosingEdge; |
| 1220 | Edge* rightEnclosingEdge; |
Chris Dalton | 47114db | 2021-01-06 00:35:20 -0700 | [diff] [blame^] | 1221 | FindEnclosingEdges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge); |
Stephen White | 8a0bfc5 | 2017-02-21 15:24:13 -0500 | [diff] [blame] | 1222 | Poly* leftPoly; |
| 1223 | Poly* rightPoly; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1224 | if (v->fFirstEdgeAbove) { |
| 1225 | leftPoly = v->fFirstEdgeAbove->fLeftPoly; |
| 1226 | rightPoly = v->fLastEdgeAbove->fRightPoly; |
| 1227 | } else { |
| 1228 | leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr; |
| 1229 | rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr; |
| 1230 | } |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 1231 | #if TRIANGULATOR_LOGGING |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 1232 | TESS_LOG("edges above:\n"); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1233 | for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) { |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 1234 | TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n", |
| 1235 | e->fTop->fID, e->fBottom->fID, |
| 1236 | e->fLeftPoly ? e->fLeftPoly->fID : -1, |
| 1237 | e->fRightPoly ? e->fRightPoly->fID : -1); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1238 | } |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 1239 | TESS_LOG("edges below:\n"); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1240 | for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 1241 | TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n", |
| 1242 | e->fTop->fID, e->fBottom->fID, |
| 1243 | e->fLeftPoly ? e->fLeftPoly->fID : -1, |
| 1244 | e->fRightPoly ? e->fRightPoly->fID : -1); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1245 | } |
| 1246 | #endif |
| 1247 | if (v->fFirstEdgeAbove) { |
| 1248 | if (leftPoly) { |
Chris Dalton | 17ce8c5 | 2021-01-07 18:08:46 -0700 | [diff] [blame] | 1249 | leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, kRight_Side, fAlloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1250 | } |
| 1251 | if (rightPoly) { |
Chris Dalton | 17ce8c5 | 2021-01-07 18:08:46 -0700 | [diff] [blame] | 1252 | rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, kLeft_Side, fAlloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1253 | } |
| 1254 | for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1255 | Edge* rightEdge = e->fNextEdgeAbove; |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 1256 | activeEdges.remove(e); |
Stephen White | 8a0bfc5 | 2017-02-21 15:24:13 -0500 | [diff] [blame] | 1257 | if (e->fRightPoly) { |
Chris Dalton | 17ce8c5 | 2021-01-07 18:08:46 -0700 | [diff] [blame] | 1258 | e->fRightPoly->addEdge(e, kLeft_Side, fAlloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1259 | } |
Stephen White | 8a0bfc5 | 2017-02-21 15:24:13 -0500 | [diff] [blame] | 1260 | if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != e->fRightPoly) { |
Chris Dalton | 17ce8c5 | 2021-01-07 18:08:46 -0700 | [diff] [blame] | 1261 | rightEdge->fLeftPoly->addEdge(e, kRight_Side, fAlloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1262 | } |
| 1263 | } |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 1264 | activeEdges.remove(v->fLastEdgeAbove); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1265 | if (!v->fFirstEdgeBelow) { |
| 1266 | if (leftPoly && rightPoly && leftPoly != rightPoly) { |
| 1267 | SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr); |
| 1268 | rightPoly->fPartner = leftPoly; |
| 1269 | leftPoly->fPartner = rightPoly; |
| 1270 | } |
| 1271 | } |
| 1272 | } |
| 1273 | if (v->fFirstEdgeBelow) { |
| 1274 | if (!v->fFirstEdgeAbove) { |
senorblanco | 93e3fff | 2016-06-07 12:36:00 -0700 | [diff] [blame] | 1275 | if (leftPoly && rightPoly) { |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 1276 | if (leftPoly == rightPoly) { |
Chris Dalton | 17ce8c5 | 2021-01-07 18:08:46 -0700 | [diff] [blame] | 1277 | if (leftPoly->fTail && leftPoly->fTail->fSide == kLeft_Side) { |
Chris Dalton | 7cf3add | 2021-01-11 18:33:28 -0700 | [diff] [blame] | 1278 | leftPoly = this->makePoly(&polys, leftPoly->lastVertex(), |
| 1279 | leftPoly->fWinding); |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 1280 | leftEnclosingEdge->fRightPoly = leftPoly; |
| 1281 | } else { |
Chris Dalton | 7cf3add | 2021-01-11 18:33:28 -0700 | [diff] [blame] | 1282 | rightPoly = this->makePoly(&polys, rightPoly->lastVertex(), |
| 1283 | rightPoly->fWinding); |
senorblanco | 531237e | 2016-06-02 11:36:48 -0700 | [diff] [blame] | 1284 | rightEnclosingEdge->fLeftPoly = rightPoly; |
| 1285 | } |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1286 | } |
Chris Dalton | d60c919 | 2021-01-07 18:30:08 +0000 | [diff] [blame] | 1287 | Edge* join = fAlloc.make<Edge>(leftPoly->lastVertex(), v, 1, |
Chris Dalton | 17ce8c5 | 2021-01-07 18:08:46 -0700 | [diff] [blame] | 1288 | EdgeType::kInner); |
| 1289 | leftPoly = leftPoly->addEdge(join, kRight_Side, fAlloc); |
| 1290 | rightPoly = rightPoly->addEdge(join, kLeft_Side, fAlloc); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1291 | } |
| 1292 | } |
| 1293 | Edge* leftEdge = v->fFirstEdgeBelow; |
| 1294 | leftEdge->fLeftPoly = leftPoly; |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 1295 | activeEdges.insert(leftEdge, leftEnclosingEdge); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1296 | for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge; |
| 1297 | rightEdge = rightEdge->fNextEdgeBelow) { |
Chris Dalton | 24472af | 2021-01-11 20:05:00 -0700 | [diff] [blame] | 1298 | activeEdges.insert(rightEdge, leftEdge); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1299 | int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0; |
| 1300 | winding += leftEdge->fWinding; |
| 1301 | if (winding != 0) { |
Chris Dalton | 6ccc032 | 2020-01-29 11:38:16 -0700 | [diff] [blame] | 1302 | if (abs(winding) > maxWindMagnitude) { |
| 1303 | return nullptr; // We can't have weighted wind in kSimpleInnerPolygons mode |
| 1304 | } |
Chris Dalton | 7cf3add | 2021-01-11 18:33:28 -0700 | [diff] [blame] | 1305 | Poly* poly = this->makePoly(&polys, v, winding); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1306 | leftEdge->fRightPoly = rightEdge->fLeftPoly = poly; |
| 1307 | } |
| 1308 | leftEdge = rightEdge; |
| 1309 | } |
| 1310 | v->fLastEdgeBelow->fRightPoly = rightPoly; |
| 1311 | } |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 1312 | #if TRIANGULATOR_LOGGING |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 1313 | TESS_LOG("\nactive edges:\n"); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1314 | for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) { |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 1315 | TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n", |
| 1316 | e->fTop->fID, e->fBottom->fID, |
| 1317 | e->fLeftPoly ? e->fLeftPoly->fID : -1, |
| 1318 | e->fRightPoly ? e->fRightPoly->fID : -1); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1319 | } |
| 1320 | #endif |
| 1321 | } |
| 1322 | return polys; |
| 1323 | } |
| 1324 | |
Stephen White | bda29c0 | 2017-03-13 15:10:13 -0400 | [diff] [blame] | 1325 | // This is a driver function that calls stages 2-5 in turn. |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1326 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 1327 | void GrTriangulator::contoursToMesh(VertexList* contours, int contourCnt, VertexList* mesh, |
Chris Dalton | 811dc6a | 2021-01-07 16:40:32 -0700 | [diff] [blame] | 1328 | const Comparator& c) { |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 1329 | #if TRIANGULATOR_LOGGING |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1330 | for (int i = 0; i < contourCnt; ++i) { |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 1331 | Vertex* v = contours[i].fHead; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1332 | SkASSERT(v); |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 1333 | 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] | 1334 | for (v = v->fNext; v; v = v->fNext) { |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 1335 | 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] | 1336 | } |
| 1337 | } |
| 1338 | #endif |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 1339 | this->sanitizeContours(contours, contourCnt); |
| 1340 | this->buildEdges(contours, contourCnt, mesh, c); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1341 | } |
| 1342 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 1343 | void GrTriangulator::SortMesh(VertexList* vertices, const Comparator& c) { |
Stephen White | bf6137e | 2017-01-04 15:43:26 -0500 | [diff] [blame] | 1344 | if (!vertices || !vertices->fHead) { |
Stephen White | 2f4686f | 2017-01-03 16:20:01 -0500 | [diff] [blame] | 1345 | return; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | // Sort vertices in Y (secondarily in X). |
Stephen White | 16a40cb | 2017-02-23 11:10:01 -0500 | [diff] [blame] | 1349 | if (c.fDirection == Comparator::Direction::kHorizontal) { |
| 1350 | merge_sort<sweep_lt_horiz>(vertices); |
| 1351 | } else { |
| 1352 | merge_sort<sweep_lt_vert>(vertices); |
| 1353 | } |
Chris Dalton | 5045de3 | 2021-01-07 19:09:01 -0700 | [diff] [blame] | 1354 | #if TRIANGULATOR_LOGGING |
Stephen White | 2e2cb9b | 2017-01-09 13:11:18 -0500 | [diff] [blame] | 1355 | for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1356 | static float gID = 0.0f; |
| 1357 | v->fID = gID++; |
| 1358 | } |
| 1359 | #endif |
Stephen White | 2f4686f | 2017-01-03 16:20:01 -0500 | [diff] [blame] | 1360 | } |
| 1361 | |
Chris Dalton | 93c2d81 | 2021-01-11 19:51:59 -0700 | [diff] [blame] | 1362 | Poly* GrTriangulator::contoursToPolys(VertexList* contours, int contourCnt) { |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 1363 | const SkRect& pathBounds = fPath.getBounds(); |
Stephen White | 16a40cb | 2017-02-23 11:10:01 -0500 | [diff] [blame] | 1364 | Comparator c(pathBounds.width() > pathBounds.height() ? Comparator::Direction::kHorizontal |
| 1365 | : Comparator::Direction::kVertical); |
Stephen White | bf6137e | 2017-01-04 15:43:26 -0500 | [diff] [blame] | 1366 | VertexList mesh; |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 1367 | this->contoursToMesh(contours, contourCnt, &mesh, c); |
| 1368 | SortMesh(&mesh, c); |
| 1369 | this->mergeCoincidentVertices(&mesh, c); |
| 1370 | if (SimplifyResult::kAbort == this->simplify(&mesh, c)) { |
Chris Dalton | 6ccc032 | 2020-01-29 11:38:16 -0700 | [diff] [blame] | 1371 | return nullptr; |
| 1372 | } |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 1373 | TESS_LOG("\nsimplified mesh:\n"); |
Chris Dalton | 7cf3add | 2021-01-11 18:33:28 -0700 | [diff] [blame] | 1374 | DUMP_MESH(mesh); |
Chris Dalton | 93c2d81 | 2021-01-11 19:51:59 -0700 | [diff] [blame] | 1375 | return this->tessellate(mesh, c); |
Chris Dalton | 7cf3add | 2021-01-11 18:33:28 -0700 | [diff] [blame] | 1376 | } |
| 1377 | |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1378 | // Stage 6: Triangulate the monotone polygons into a vertex buffer. |
Chris Dalton | 93c2d81 | 2021-01-11 19:51:59 -0700 | [diff] [blame] | 1379 | void* GrTriangulator::polysToTrianglesImpl(Poly* polys, void* data, |
| 1380 | SkPathFillType overrideFillType) { |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1381 | for (Poly* poly = polys; poly; poly = poly->fNext) { |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 1382 | if (apply_fill_type(overrideFillType, poly)) { |
Chris Dalton | 9a4904f | 2021-01-07 19:10:14 -0700 | [diff] [blame] | 1383 | data = this->emitPoly(poly, data); |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1384 | } |
| 1385 | } |
| 1386 | return data; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1387 | } |
| 1388 | |
Chris Dalton | 93c2d81 | 2021-01-11 19:51:59 -0700 | [diff] [blame] | 1389 | Poly* GrTriangulator::pathToPolys(float tolerance, const SkRect& clipBounds, int contourCnt) { |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 1390 | if (SkPathFillType_IsInverse(fPath.getFillType())) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1391 | contourCnt++; |
| 1392 | } |
Stephen White | 3a9aab9 | 2017-03-07 14:07:18 -0500 | [diff] [blame] | 1393 | std::unique_ptr<VertexList[]> contours(new VertexList[contourCnt]); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1394 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 1395 | this->pathToContours(tolerance, clipBounds, contours.get()); |
Chris Dalton | 93c2d81 | 2021-01-11 19:51:59 -0700 | [diff] [blame] | 1396 | return this->contoursToPolys(contours.get(), contourCnt); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1397 | } |
| 1398 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 1399 | static int get_contour_count(const SkPath& path, SkScalar tolerance) { |
Chris Dalton | c71b3d4 | 2020-01-08 21:29:59 -0700 | [diff] [blame] | 1400 | // We could theoretically be more aggressive about not counting empty contours, but we need to |
| 1401 | // actually match the exact number of contour linked lists the tessellator will create later on. |
| 1402 | int contourCnt = 1; |
| 1403 | bool hasPoints = false; |
| 1404 | |
Chris Dalton | 7156db2 | 2020-05-07 22:06:28 +0000 | [diff] [blame] | 1405 | SkPath::Iter iter(path, false); |
| 1406 | SkPath::Verb verb; |
| 1407 | SkPoint pts[4]; |
Chris Dalton | c71b3d4 | 2020-01-08 21:29:59 -0700 | [diff] [blame] | 1408 | bool first = true; |
Chris Dalton | 7156db2 | 2020-05-07 22:06:28 +0000 | [diff] [blame] | 1409 | while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { |
Chris Dalton | c71b3d4 | 2020-01-08 21:29:59 -0700 | [diff] [blame] | 1410 | switch (verb) { |
Chris Dalton | 7156db2 | 2020-05-07 22:06:28 +0000 | [diff] [blame] | 1411 | case SkPath::kMove_Verb: |
Chris Dalton | c71b3d4 | 2020-01-08 21:29:59 -0700 | [diff] [blame] | 1412 | if (!first) { |
| 1413 | ++contourCnt; |
| 1414 | } |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 1415 | [[fallthrough]]; |
Chris Dalton | 7156db2 | 2020-05-07 22:06:28 +0000 | [diff] [blame] | 1416 | case SkPath::kLine_Verb: |
| 1417 | case SkPath::kConic_Verb: |
| 1418 | case SkPath::kQuad_Verb: |
| 1419 | case SkPath::kCubic_Verb: |
Chris Dalton | c71b3d4 | 2020-01-08 21:29:59 -0700 | [diff] [blame] | 1420 | hasPoints = true; |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 1421 | break; |
Chris Dalton | c71b3d4 | 2020-01-08 21:29:59 -0700 | [diff] [blame] | 1422 | default: |
| 1423 | break; |
| 1424 | } |
| 1425 | first = false; |
| 1426 | } |
| 1427 | if (!hasPoints) { |
Stephen White | 11f65e0 | 2017-02-16 19:00:39 -0500 | [diff] [blame] | 1428 | return 0; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1429 | } |
Stephen White | 11f65e0 | 2017-02-16 19:00:39 -0500 | [diff] [blame] | 1430 | return contourCnt; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1431 | } |
| 1432 | |
Chris Dalton | 93c2d81 | 2021-01-11 19:51:59 -0700 | [diff] [blame] | 1433 | int64_t GrTriangulator::countPointsImpl(Poly* polys, SkPathFillType overrideFillType) const { |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 1434 | int64_t count = 0; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1435 | for (Poly* poly = polys; poly; poly = poly->fNext) { |
Chris Dalton | 93c2d81 | 2021-01-11 19:51:59 -0700 | [diff] [blame] | 1436 | if (apply_fill_type(overrideFillType, poly) && poly->fCount >= 3) { |
Chris Dalton | 17dc418 | 2020-03-25 16:18:16 -0600 | [diff] [blame] | 1437 | count += (poly->fCount - 2) * (TRIANGULATOR_WIREFRAME ? 6 : 3); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1438 | } |
| 1439 | } |
| 1440 | return count; |
| 1441 | } |
| 1442 | |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1443 | // Stage 6: Triangulate the monotone polygons into a vertex buffer. |
| 1444 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 1445 | int GrTriangulator::pathToTriangles(float tolerance, const SkRect& clipBounds, |
Chris Dalton | 93c2d81 | 2021-01-11 19:51:59 -0700 | [diff] [blame] | 1446 | GrEagerVertexAllocator* vertexAllocator) { |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 1447 | int contourCnt = get_contour_count(fPath, tolerance); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1448 | if (contourCnt <= 0) { |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 1449 | fIsLinear = true; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1450 | return 0; |
| 1451 | } |
Chris Dalton | 93c2d81 | 2021-01-11 19:51:59 -0700 | [diff] [blame] | 1452 | Poly* polys = this->pathToPolys(tolerance, clipBounds, contourCnt); |
| 1453 | int64_t count64 = this->countPoints(polys); |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 1454 | if (0 == count64 || count64 > SK_MaxS32) { |
Stephen White | ff60b17 | 2017-05-05 15:54:52 -0400 | [diff] [blame] | 1455 | return 0; |
| 1456 | } |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 1457 | int count = count64; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1458 | |
Chris Dalton | 854ee85 | 2021-01-05 15:12:59 -0700 | [diff] [blame] | 1459 | size_t vertexStride = sizeof(SkPoint); |
| 1460 | if (fEmitCoverage) { |
| 1461 | vertexStride += sizeof(float); |
| 1462 | } |
Chris Dalton | d081dce | 2020-01-23 12:09:04 -0700 | [diff] [blame] | 1463 | void* verts = vertexAllocator->lock(vertexStride, count); |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 1464 | if (!verts) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1465 | SkDebugf("Could not allocate vertices\n"); |
| 1466 | return 0; |
| 1467 | } |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1468 | |
Brian Salomon | 120e7d6 | 2019-09-11 10:29:22 -0400 | [diff] [blame] | 1469 | TESS_LOG("emitting %d verts\n", count); |
Chris Dalton | 93c2d81 | 2021-01-11 19:51:59 -0700 | [diff] [blame] | 1470 | void* end = this->polysToTriangles(polys, verts); |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 1471 | |
senorblanco | f57372d | 2016-08-31 10:36:19 -0700 | [diff] [blame] | 1472 | 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] | 1473 | / vertexStride); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1474 | SkASSERT(actualCount <= count); |
senorblanco | 6599eff | 2016-03-10 08:38:45 -0800 | [diff] [blame] | 1475 | vertexAllocator->unlock(actualCount); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1476 | return actualCount; |
| 1477 | } |
| 1478 | |
Chris Dalton | 57ea1fc | 2021-01-05 13:37:44 -0700 | [diff] [blame] | 1479 | int GrTriangulator::PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds, |
| 1480 | WindingVertex** verts) { |
Stephen White | 11f65e0 | 2017-02-16 19:00:39 -0500 | [diff] [blame] | 1481 | int contourCnt = get_contour_count(path, tolerance); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1482 | if (contourCnt <= 0) { |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 1483 | *verts = nullptr; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1484 | return 0; |
| 1485 | } |
Chris Dalton | 854ee85 | 2021-01-05 15:12:59 -0700 | [diff] [blame] | 1486 | GrTriangulator triangulator(path); |
Chris Dalton | 93c2d81 | 2021-01-11 19:51:59 -0700 | [diff] [blame] | 1487 | Poly* polys = triangulator.pathToPolys(tolerance, clipBounds, contourCnt); |
| 1488 | int64_t count64 = triangulator.countPoints(polys); |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 1489 | if (0 == count64 || count64 > SK_MaxS32) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1490 | *verts = nullptr; |
| 1491 | return 0; |
| 1492 | } |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 1493 | int count = count64; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1494 | |
Chris Dalton | 17dc418 | 2020-03-25 16:18:16 -0600 | [diff] [blame] | 1495 | *verts = new WindingVertex[count]; |
| 1496 | WindingVertex* vertsEnd = *verts; |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1497 | SkPoint* points = new SkPoint[count]; |
| 1498 | SkPoint* pointsEnd = points; |
| 1499 | for (Poly* poly = polys; poly; poly = poly->fNext) { |
Chris Dalton | 93c2d81 | 2021-01-11 19:51:59 -0700 | [diff] [blame] | 1500 | if (apply_fill_type(path.getFillType(), poly)) { |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1501 | SkPoint* start = pointsEnd; |
Chris Dalton | 9a4904f | 2021-01-07 19:10:14 -0700 | [diff] [blame] | 1502 | pointsEnd = static_cast<SkPoint*>(triangulator.emitPoly(poly, pointsEnd)); |
ethannicholas | e9709e8 | 2016-01-07 13:34:16 -0800 | [diff] [blame] | 1503 | while (start != pointsEnd) { |
| 1504 | vertsEnd->fPos = *start; |
| 1505 | vertsEnd->fWinding = poly->fWinding; |
| 1506 | ++start; |
| 1507 | ++vertsEnd; |
| 1508 | } |
| 1509 | } |
| 1510 | } |
| 1511 | int actualCount = static_cast<int>(vertsEnd - *verts); |
| 1512 | SkASSERT(actualCount <= count); |
| 1513 | SkASSERT(pointsEnd - points == actualCount); |
| 1514 | delete[] points; |
| 1515 | return actualCount; |
| 1516 | } |