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