blob: af695046eaba48d45a94f2e33c83a67c52fc379a [file] [log] [blame]
ethannicholase9709e82016-01-07 13:34:16 -08001/*
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 Dalton17dc4182020-03-25 16:18:16 -06008#ifndef GrTriangulator_DEFINED
9#define GrTriangulator_DEFINED
ethannicholase9709e82016-01-07 13:34:16 -080010
Chris Dalton57ea1fc2021-01-05 13:37:44 -070011#include "include/core/SkPath.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkPoint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/private/SkColorData.h"
Chris Dalton57ea1fc2021-01-05 13:37:44 -070014#include "src/core/SkArenaAlloc.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040015#include "src/gpu/GrColor.h"
senorblanco6599eff2016-03-10 08:38:45 -080016
Chris Daltond081dce2020-01-23 12:09:04 -070017class GrEagerVertexAllocator;
senorblanco6599eff2016-03-10 08:38:45 -080018struct SkRect;
ethannicholase9709e82016-01-07 13:34:16 -080019
Chris Dalton5045de32021-01-07 19:09:01 -070020#define TRIANGULATOR_LOGGING 0
Chris Dalton57ea1fc2021-01-05 13:37:44 -070021#define TRIANGULATOR_WIREFRAME 0
22
ethannicholase9709e82016-01-07 13:34:16 -080023/**
24 * Provides utility functions for converting paths to a collection of triangles.
25 */
Chris Dalton57ea1fc2021-01-05 13:37:44 -070026class GrTriangulator {
27public:
Chris Dalton854ee852021-01-05 15:12:59 -070028 static int PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
29 GrEagerVertexAllocator* vertexAllocator, bool* isLinear) {
30 GrTriangulator triangulator(path);
Chris Dalton37d16f12021-01-20 18:32:48 -070031 Poly* polys = triangulator.pathToPolys(tolerance, clipBounds, isLinear);
Chris Daltond5384792021-01-20 15:43:24 -070032 int count = triangulator.polysToTriangles(polys, vertexAllocator);
Chris Dalton854ee852021-01-05 15:12:59 -070033 return count;
Chris Dalton57ea1fc2021-01-05 13:37:44 -070034 }
ethannicholase9709e82016-01-07 13:34:16 -080035
Chris Dalton57ea1fc2021-01-05 13:37:44 -070036 struct WindingVertex {
37 SkPoint fPos;
38 int fWinding;
39 };
Chris Dalton6ccc0322020-01-29 11:38:16 -070040
Chris Dalton57ea1fc2021-01-05 13:37:44 -070041 // *DEPRECATED*: Once CCPR is removed this method will go away.
Chris Dalton6ccc0322020-01-29 11:38:16 -070042 //
Chris Dalton57ea1fc2021-01-05 13:37:44 -070043 // Triangulates a path to an array of vertices. Each triangle is represented as a set of three
44 // WindingVertex entries, each of which contains the position and winding count (which is the
45 // same for all three vertices of a triangle). The 'verts' out parameter is set to point to the
46 // resultant vertex array. CALLER IS RESPONSIBLE for deleting this buffer to avoid a memory
47 // leak!
48 static int PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
49 WindingVertex** verts);
50
Chris Dalton17ce8c52021-01-07 18:08:46 -070051 // Enums used by GrTriangulator internals.
52 typedef enum { kLeft_Side, kRight_Side } Side;
53 enum class EdgeType { kInner, kOuter, kConnector };
54
Chris Dalton57ea1fc2021-01-05 13:37:44 -070055 // Structs used by GrTriangulator internals.
56 struct Vertex;
57 struct VertexList;
Chris Dalton17ce8c52021-01-07 18:08:46 -070058 struct Line;
Chris Dalton57ea1fc2021-01-05 13:37:44 -070059 struct Edge;
60 struct EdgeList;
Chris Dalton17ce8c52021-01-07 18:08:46 -070061 struct MonotonePoly;
Chris Dalton57ea1fc2021-01-05 13:37:44 -070062 struct Poly;
63 struct Comparator;
64
Chris Dalton7cf3add2021-01-11 18:33:28 -070065protected:
Chris Dalton854ee852021-01-05 15:12:59 -070066 GrTriangulator(const SkPath& path) : fPath(path) {}
Chris Dalton7cf3add2021-01-11 18:33:28 -070067 virtual ~GrTriangulator() {}
Chris Dalton57ea1fc2021-01-05 13:37:44 -070068
69 // There are six stages to the basic algorithm:
70 //
71 // 1) Linearize the path contours into piecewise linear segments:
Chris Dalton37d16f12021-01-20 18:32:48 -070072 void pathToContours(float tolerance, const SkRect& clipBounds, VertexList* contours,
73 bool* isLinear);
Chris Dalton57ea1fc2021-01-05 13:37:44 -070074
75 // 2) Build a mesh of edges connecting the vertices:
Chris Dalton811dc6a2021-01-07 16:40:32 -070076 void contoursToMesh(VertexList* contours, int contourCnt, VertexList* mesh, const Comparator&);
Chris Dalton57ea1fc2021-01-05 13:37:44 -070077
Chris Dalton68b8bd52021-01-14 10:48:02 -070078 // 3) Sort the vertices in Y (and secondarily in X):
Chris Dalton47114db2021-01-06 00:35:20 -070079 static void SortedMerge(VertexList* front, VertexList* back, VertexList* result,
80 const Comparator&);
Chris Dalton57ea1fc2021-01-05 13:37:44 -070081 static void SortMesh(VertexList* vertices, const Comparator&);
82
83 // 4) Simplify the mesh by inserting new vertices at intersecting edges:
84 enum class SimplifyResult {
85 kAlreadySimple,
86 kFoundSelfIntersection,
87 kAbort
88 };
89
Chris Dalton811dc6a2021-01-07 16:40:32 -070090 SimplifyResult simplify(VertexList* mesh, const Comparator&);
Chris Dalton57ea1fc2021-01-05 13:37:44 -070091
92 // 5) Tessellate the simplified mesh into monotone polygons:
Chris Dalton93c2d812021-01-11 19:51:59 -070093 virtual Poly* tessellate(const VertexList& vertices, const Comparator&);
Chris Dalton57ea1fc2021-01-05 13:37:44 -070094
95 // 6) Triangulate the monotone polygons directly into a vertex buffer:
Chris Daltond5384792021-01-20 15:43:24 -070096 void* polysToTriangles(Poly* polys, void* data, SkPathFillType overrideFillType);
Chris Dalton57ea1fc2021-01-05 13:37:44 -070097
Chris Dalton57ea1fc2021-01-05 13:37:44 -070098 // The vertex sorting in step (3) is a merge sort, since it plays well with the linked list
99 // of vertices (and the necessity of inserting new vertices on intersection).
100 //
101 // Stages (4) and (5) use an active edge list -- a list of all edges for which the
102 // sweep line has crossed the top vertex, but not the bottom vertex. It's sorted
103 // left-to-right based on the point where both edges are active (when both top vertices
104 // have been seen, so the "lower" top vertex of the two). If the top vertices are equal
105 // (shared), it's sorted based on the last point where both edges are active, so the
106 // "upper" bottom vertex.
107 //
108 // The most complex step is the simplification (4). It's based on the Bentley-Ottman
109 // line-sweep algorithm, but due to floating point inaccuracy, the intersection points are
110 // not exact and may violate the mesh topology or active edge list ordering. We
111 // accommodate this by adjusting the topology of the mesh and AEL to match the intersection
112 // points. This occurs in two ways:
113 //
114 // A) Intersections may cause a shortened edge to no longer be ordered with respect to its
115 // neighbouring edges at the top or bottom vertex. This is handled by merging the
Chris Dalton68b8bd52021-01-14 10:48:02 -0700116 // edges (mergeCollinearVertices()).
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700117 // B) Intersections may cause an edge to violate the left-to-right ordering of the
118 // active edge list. This is handled by detecting potential violations and rewinding
119 // the active edge list to the vertex before they occur (rewind() during merging,
120 // rewind_if_necessary() during splitting).
121 //
122 // The tessellation steps (5) and (6) are based on "Triangulating Simple Polygons and
123 // Equivalent Problems" (Fournier and Montuno); also a line-sweep algorithm. Note that it
124 // currently uses a linked list for the active edge list, rather than a 2-3 tree as the
125 // paper describes. The 2-3 tree gives O(lg N) lookups, but insertion and removal also
126 // become O(lg N). In all the test cases, it was found that the cost of frequent O(lg N)
127 // insertions and removals was greater than the cost of infrequent O(N) lookups with the
128 // linked list implementation. With the latter, all removals are O(1), and most insertions
129 // are O(1), since we know the adjacent edge in the active edge list based on the topology.
130 // Only type 2 vertices (see paper) require the O(N) lookups, and these are much less
131 // frequent. There may be other data structures worth investigating, however.
132 //
133 // Note that the orientation of the line sweep algorithms is determined by the aspect ratio of
134 // the path bounds. When the path is taller than it is wide, we sort vertices based on
135 // increasing Y coordinate, and secondarily by increasing X coordinate. When the path is wider
136 // than it is tall, we sort by increasing X coordinate, but secondarily by *decreasing* Y
137 // coordinate. This is so that the "left" and "right" orientation in the code remains correct
138 // (edges to the left are increasing in Y; edges to the right are decreasing in Y). That is, the
139 // setting rotates 90 degrees counterclockwise, rather that transposing.
140
141 // Additional helpers and driver functions.
Chris Dalton9a4904f2021-01-07 19:10:14 -0700142 void* emitMonotonePoly(const MonotonePoly*, void* data);
143 void* emitTriangle(Vertex* prev, Vertex* curr, Vertex* next, int winding, void* data) const;
144 void* emitPoly(const Poly*, void *data);
Chris Dalton7cf3add2021-01-11 18:33:28 -0700145 Poly* makePoly(Poly** head, Vertex* v, int winding);
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700146 void appendPointToContour(const SkPoint& p, VertexList* contour);
147 void appendQuadraticToContour(const SkPoint[3], SkScalar toleranceSqd, VertexList* contour);
148 void generateCubicPoints(const SkPoint&, const SkPoint&, const SkPoint&, const SkPoint&,
149 SkScalar tolSqd, VertexList* contour, int pointsLeft);
Chris Dalton47114db2021-01-06 00:35:20 -0700150 bool applyFillType(int winding);
Chris Dalton7cf3add2021-01-11 18:33:28 -0700151 Edge* makeEdge(Vertex* prev, Vertex* next, EdgeType type, const Comparator&);
Chris Dalton68b8bd52021-01-14 10:48:02 -0700152 void setTop(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, const Comparator&);
153 void setBottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current,
154 const Comparator&);
155 void mergeEdgesAbove(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
156 const Comparator&);
157 void mergeEdgesBelow(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
158 const Comparator&);
Chris Dalton7cf3add2021-01-11 18:33:28 -0700159 Edge* makeConnectingEdge(Vertex* prev, Vertex* next, EdgeType, const Comparator&,
160 int windingScale = 1);
Chris Dalton68b8bd52021-01-14 10:48:02 -0700161 void mergeVertices(Vertex* src, Vertex* dst, VertexList* mesh, const Comparator&);
Chris Dalton47114db2021-01-06 00:35:20 -0700162 static void FindEnclosingEdges(Vertex* v, EdgeList* edges, Edge** left, Edge** right);
Chris Dalton68b8bd52021-01-14 10:48:02 -0700163 void mergeCollinearEdges(Edge* edge, EdgeList* activeEdges, Vertex** current,
164 const Comparator&);
Chris Dalton811dc6a2021-01-07 16:40:32 -0700165 bool splitEdge(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current,
166 const Comparator&);
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700167 bool intersectEdgePair(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700168 const Comparator&);
Chris Dalton7cf3add2021-01-11 18:33:28 -0700169 Vertex* makeSortedVertex(const SkPoint&, uint8_t alpha, VertexList* mesh, Vertex* reference,
170 const Comparator&);
171 void computeBisector(Edge* edge1, Edge* edge2, Vertex*);
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700172 bool checkForIntersection(Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700173 VertexList* mesh, const Comparator&);
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700174 void sanitizeContours(VertexList* contours, int contourCnt);
Chris Dalton811dc6a2021-01-07 16:40:32 -0700175 bool mergeCoincidentVertices(VertexList* mesh, const Comparator&);
176 void buildEdges(VertexList* contours, int contourCnt, VertexList* mesh, const Comparator&);
Chris Dalton93c2d812021-01-11 19:51:59 -0700177 Poly* contoursToPolys(VertexList* contours, int contourCnt);
Chris Dalton37d16f12021-01-20 18:32:48 -0700178 Poly* pathToPolys(float tolerance, const SkRect& clipBounds, bool* isLinear);
Chris Daltond5384792021-01-20 15:43:24 -0700179 static int64_t CountPoints(Poly* polys, SkPathFillType overrideFillType);
180 int polysToTriangles(Poly*, GrEagerVertexAllocator*);
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700181
182 constexpr static int kArenaChunkSize = 16 * 1024;
183 SkArenaAlloc fAlloc{kArenaChunkSize};
184 const SkPath fPath;
Chris Dalton854ee852021-01-05 15:12:59 -0700185
Chris Dalton68b8bd52021-01-14 10:48:02 -0700186 // Internal control knobs.
Chris Dalton854ee852021-01-05 15:12:59 -0700187 bool fRoundVerticesToQuarterPixel = false;
188 bool fEmitCoverage = false;
189 bool fCullCollinearVertices = true;
Chris Dalton57115c02021-01-12 18:12:18 -0700190 bool fDisallowSelfIntersection = false;
Chris Daltone4652052021-01-21 18:31:28 -0700191
192 // The breadcrumb triangles serve as a glue that erases T-junctions between a path's outer
193 // curves and its inner polygon triangulation. Drawing a path's outer curves, breadcrumb
194 // triangles, and inner polygon triangulation all together into the stencil buffer has the same
195 // identical rasterized effect as stenciling a classic Redbook fan.
196 //
197 // The breadcrumb triangles track all the edge splits that led from the original inner polygon
198 // edges to the final triangulation. Every time an edge splits, we emit a razor-thin breadcrumb
199 // triangle consisting of the edge's original endpoints and the split point. (We also add
200 // supplemental breadcrumb triangles to areas where abs(winding) > 1.)
201 //
202 // a
203 // /
204 // /
205 // /
206 // x <- Edge splits at x. New breadcrumb triangle is: [a, b, x].
207 // /
208 // /
209 // b
210 //
211 // The opposite-direction shared edges between the triangulation and breadcrumb triangles should
212 // all cancel out, leaving just the set of edges from the original polygon.
213 class BreadcrumbTriangleCollector {
214 public:
215 void push(SkPoint a, SkPoint b, SkPoint c, int winding) {
216 if (a != b && a != c && b != c) {
217 if (winding > 0) {
218 this->onPush(a, b, c, winding);
219 } else if (winding < 0) {
220 this->onPush(b, a, c, -winding);
221 }
222 }
223 }
224 virtual ~BreadcrumbTriangleCollector() {}
225 private:
226 virtual void onPush(SkPoint, SkPoint, SkPoint, int winding) = 0;
227 };
228
Chris Dalton68b8bd52021-01-14 10:48:02 -0700229 BreadcrumbTriangleCollector* fBreadcrumbTriangles = nullptr;
Chris Daltondcc8c542020-01-28 17:55:56 -0700230};
231
Chris Dalton5045de32021-01-07 19:09:01 -0700232/**
233 * Vertices are used in three ways: first, the path contours are converted into a
234 * circularly-linked list of Vertices for each contour. After edge construction, the same Vertices
235 * are re-ordered by the merge sort according to the sweep_lt comparator (usually, increasing
236 * in Y) using the same fPrev/fNext pointers that were used for the contours, to avoid
237 * reallocation. Finally, MonotonePolys are built containing a circularly-linked list of
238 * Vertices. (Currently, those Vertices are newly-allocated for the MonotonePolys, since
239 * an individual Vertex from the path mesh may belong to multiple
240 * MonotonePolys, so the original Vertices cannot be re-used.
241 */
242
243struct GrTriangulator::Vertex {
244 Vertex(const SkPoint& point, uint8_t alpha)
245 : fPoint(point), fPrev(nullptr), fNext(nullptr)
246 , fFirstEdgeAbove(nullptr), fLastEdgeAbove(nullptr)
247 , fFirstEdgeBelow(nullptr), fLastEdgeBelow(nullptr)
248 , fLeftEnclosingEdge(nullptr), fRightEnclosingEdge(nullptr)
249 , fPartner(nullptr)
250 , fAlpha(alpha)
251 , fSynthetic(false)
252#if TRIANGULATOR_LOGGING
253 , fID (-1.0f)
254#endif
255 {}
256 SkPoint fPoint; // Vertex position
257 Vertex* fPrev; // Linked list of contours, then Y-sorted vertices.
258 Vertex* fNext; // "
259 Edge* fFirstEdgeAbove; // Linked list of edges above this vertex.
260 Edge* fLastEdgeAbove; // "
261 Edge* fFirstEdgeBelow; // Linked list of edges below this vertex.
262 Edge* fLastEdgeBelow; // "
263 Edge* fLeftEnclosingEdge; // Nearest edge in the AEL left of this vertex.
264 Edge* fRightEnclosingEdge; // Nearest edge in the AEL right of this vertex.
265 Vertex* fPartner; // Corresponding inner or outer vertex (for AA).
266 uint8_t fAlpha;
267 bool fSynthetic; // Is this a synthetic vertex?
268#if TRIANGULATOR_LOGGING
269 float fID; // Identifier used for logging.
270#endif
Chris Dalton24472af2021-01-11 20:05:00 -0700271 bool isConnected() const { return this->fFirstEdgeAbove || this->fFirstEdgeBelow; }
Chris Dalton5045de32021-01-07 19:09:01 -0700272};
273
274struct GrTriangulator::VertexList {
275 VertexList() : fHead(nullptr), fTail(nullptr) {}
276 VertexList(Vertex* head, Vertex* tail) : fHead(head), fTail(tail) {}
277 Vertex* fHead;
278 Vertex* fTail;
279 void insert(Vertex* v, Vertex* prev, Vertex* next);
280 void append(Vertex* v) { insert(v, fTail, nullptr); }
281 void append(const VertexList& list) {
282 if (!list.fHead) {
283 return;
284 }
285 if (fTail) {
286 fTail->fNext = list.fHead;
287 list.fHead->fPrev = fTail;
288 } else {
289 fHead = list.fHead;
290 }
291 fTail = list.fTail;
292 }
293 void prepend(Vertex* v) { insert(v, nullptr, fHead); }
294 void remove(Vertex* v);
295 void close() {
296 if (fHead && fTail) {
297 fTail->fNext = fHead;
298 fHead->fPrev = fTail;
299 }
300 }
Chris Dalton24472af2021-01-11 20:05:00 -0700301#if TRIANGULATOR_LOGGING
302 void dump();
303#endif
Chris Dalton5045de32021-01-07 19:09:01 -0700304};
305
306// A line equation in implicit form. fA * x + fB * y + fC = 0, for all points (x, y) on the line.
307struct GrTriangulator::Line {
308 Line(double a, double b, double c) : fA(a), fB(b), fC(c) {}
309 Line(Vertex* p, Vertex* q) : Line(p->fPoint, q->fPoint) {}
310 Line(const SkPoint& p, const SkPoint& q)
311 : fA(static_cast<double>(q.fY) - p.fY) // a = dY
312 , fB(static_cast<double>(p.fX) - q.fX) // b = -dX
313 , fC(static_cast<double>(p.fY) * q.fX - // c = cross(q, p)
314 static_cast<double>(p.fX) * q.fY) {}
315 double dist(const SkPoint& p) const { return fA * p.fX + fB * p.fY + fC; }
316 Line operator*(double v) const { return Line(fA * v, fB * v, fC * v); }
317 double magSq() const { return fA * fA + fB * fB; }
318 void normalize() {
319 double len = sqrt(this->magSq());
320 if (len == 0.0) {
321 return;
322 }
323 double scale = 1.0f / len;
324 fA *= scale;
325 fB *= scale;
326 fC *= scale;
327 }
328 bool nearParallel(const Line& o) const {
329 return fabs(o.fA - fA) < 0.00001 && fabs(o.fB - fB) < 0.00001;
330 }
331
332 // Compute the intersection of two (infinite) Lines.
333 bool intersect(const Line& other, SkPoint* point) const;
334 double fA, fB, fC;
335};
336
337/**
338 * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and
339 * "edge below" a vertex as well as for the active edge list is handled by isLeftOf()/isRightOf().
340 * Note that an Edge will give occasionally dist() != 0 for its own endpoints (because floating
341 * point). For speed, that case is only tested by the callers that require it (e.g.,
342 * rewind_if_necessary()). Edges also handle checking for intersection with other edges.
343 * Currently, this converts the edges to the parametric form, in order to avoid doing a division
344 * until an intersection has been confirmed. This is slightly slower in the "found" case, but
345 * a lot faster in the "not found" case.
346 *
347 * The coefficients of the line equation stored in double precision to avoid catastrophic
348 * cancellation in the isLeftOf() and isRightOf() checks. Using doubles ensures that the result is
349 * correct in float, since it's a polynomial of degree 2. The intersect() function, being
350 * degree 5, is still subject to catastrophic cancellation. We deal with that by assuming its
351 * output may be incorrect, and adjusting the mesh topology to match (see comment at the top of
352 * this file).
353 */
354
355struct GrTriangulator::Edge {
356 Edge(Vertex* top, Vertex* bottom, int winding, EdgeType type)
357 : fWinding(winding)
358 , fTop(top)
359 , fBottom(bottom)
360 , fType(type)
361 , fLeft(nullptr)
362 , fRight(nullptr)
363 , fPrevEdgeAbove(nullptr)
364 , fNextEdgeAbove(nullptr)
365 , fPrevEdgeBelow(nullptr)
366 , fNextEdgeBelow(nullptr)
367 , fLeftPoly(nullptr)
368 , fRightPoly(nullptr)
369 , fLeftPolyPrev(nullptr)
370 , fLeftPolyNext(nullptr)
371 , fRightPolyPrev(nullptr)
372 , fRightPolyNext(nullptr)
373 , fUsedInLeftPoly(false)
374 , fUsedInRightPoly(false)
375 , fLine(top, bottom) {
376 }
377 int fWinding; // 1 == edge goes downward; -1 = edge goes upward.
378 Vertex* fTop; // The top vertex in vertex-sort-order (sweep_lt).
379 Vertex* fBottom; // The bottom vertex in vertex-sort-order.
380 EdgeType fType;
381 Edge* fLeft; // The linked list of edges in the active edge list.
382 Edge* fRight; // "
383 Edge* fPrevEdgeAbove; // The linked list of edges in the bottom Vertex's "edges above".
384 Edge* fNextEdgeAbove; // "
385 Edge* fPrevEdgeBelow; // The linked list of edges in the top Vertex's "edges below".
386 Edge* fNextEdgeBelow; // "
387 Poly* fLeftPoly; // The Poly to the left of this edge, if any.
388 Poly* fRightPoly; // The Poly to the right of this edge, if any.
389 Edge* fLeftPolyPrev;
390 Edge* fLeftPolyNext;
391 Edge* fRightPolyPrev;
392 Edge* fRightPolyNext;
393 bool fUsedInLeftPoly;
394 bool fUsedInRightPoly;
395 Line fLine;
396 double dist(const SkPoint& p) const { return fLine.dist(p); }
397 bool isRightOf(Vertex* v) const { return fLine.dist(v->fPoint) < 0.0; }
398 bool isLeftOf(Vertex* v) const { return fLine.dist(v->fPoint) > 0.0; }
399 void recompute() { fLine = Line(fTop, fBottom); }
Chris Dalton24472af2021-01-11 20:05:00 -0700400 void insertAbove(Vertex*, const Comparator&);
401 void insertBelow(Vertex*, const Comparator&);
402 void disconnect();
Chris Dalton5045de32021-01-07 19:09:01 -0700403 bool intersect(const Edge& other, SkPoint* p, uint8_t* alpha = nullptr) const;
404};
405
406struct GrTriangulator::EdgeList {
407 EdgeList() : fHead(nullptr), fTail(nullptr) {}
408 Edge* fHead;
409 Edge* fTail;
410 void insert(Edge* edge, Edge* prev, Edge* next);
Chris Dalton24472af2021-01-11 20:05:00 -0700411 void insert(Edge* edge, Edge* prev);
Chris Dalton5045de32021-01-07 19:09:01 -0700412 void append(Edge* e) { insert(e, fTail, nullptr); }
413 void remove(Edge* edge);
414 void removeAll() {
415 while (fHead) {
416 this->remove(fHead);
417 }
418 }
419 void close() {
420 if (fHead && fTail) {
421 fTail->fRight = fHead;
422 fHead->fLeft = fTail;
423 }
424 }
425 bool contains(Edge* edge) const { return edge->fLeft || edge->fRight || fHead == edge; }
426};
427
428struct GrTriangulator::MonotonePoly {
429 MonotonePoly(Edge* edge, Side side, int winding)
430 : fSide(side)
431 , fFirstEdge(nullptr)
432 , fLastEdge(nullptr)
433 , fPrev(nullptr)
434 , fNext(nullptr)
435 , fWinding(winding) {
436 this->addEdge(edge);
437 }
438 Side fSide;
439 Edge* fFirstEdge;
440 Edge* fLastEdge;
441 MonotonePoly* fPrev;
442 MonotonePoly* fNext;
443 int fWinding;
444 void addEdge(Edge*);
445 void* emit(bool emitCoverage, void* data);
446 void* emitTriangle(Vertex* prev, Vertex* curr, Vertex* next, bool emitCoverage,
447 void* data) const;
448};
449
450struct GrTriangulator::Poly {
451 Poly(Vertex* v, int winding)
452 : fFirstVertex(v)
453 , fWinding(winding)
454 , fHead(nullptr)
455 , fTail(nullptr)
456 , fNext(nullptr)
457 , fPartner(nullptr)
458 , fCount(0)
459 {
460#if TRIANGULATOR_LOGGING
461 static int gID = 0;
462 fID = gID++;
463 TESS_LOG("*** created Poly %d\n", fID);
464#endif
465 }
466 Poly* addEdge(Edge* e, Side side, SkArenaAlloc& alloc);
467 void* emit(bool emitCoverage, void *data);
468 Vertex* lastVertex() const { return fTail ? fTail->fLastEdge->fBottom : fFirstVertex; }
469 Vertex* fFirstVertex;
470 int fWinding;
471 MonotonePoly* fHead;
472 MonotonePoly* fTail;
473 Poly* fNext;
474 Poly* fPartner;
475 int fCount;
476#if TRIANGULATOR_LOGGING
477 int fID;
478#endif
479};
480
481struct GrTriangulator::Comparator {
482 enum class Direction { kVertical, kHorizontal };
483 Comparator(Direction direction) : fDirection(direction) {}
484 bool sweep_lt(const SkPoint& a, const SkPoint& b) const;
485 Direction fDirection;
486};
487
ethannicholase9709e82016-01-07 13:34:16 -0800488#endif