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