blob: 8baf72794bcd39cc223bc38903917144a3152d63 [file] [log] [blame]
caryclark@google.comc6825902012-02-03 22:07:47 +00001/*
2 * Copyright 2012 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
caryclark@google.com6680fb12012-02-07 22:10:51 +00008#include "CurveIntersection.h"
caryclark@google.coma5764232012-03-28 16:20:21 +00009#include "Intersections.h"
caryclark@google.comc6825902012-02-03 22:07:47 +000010#include "LineIntersection.h"
11#include "SkPath.h"
12#include "SkRect.h"
13#include "SkTArray.h"
14#include "SkTDArray.h"
caryclark@google.comd88e0892012-03-27 13:23:51 +000015#include "ShapeOps.h"
caryclark@google.comc6825902012-02-03 22:07:47 +000016#include "TSearch.h"
17
caryclark@google.com78e17132012-04-17 11:40:34 +000018#undef SkASSERT
19#define SkASSERT(cond) while (!(cond)) { sk_throw(); }
caryclark@google.com2e7f4c82012-03-20 21:11:59 +000020
caryclark@google.com78e17132012-04-17 11:40:34 +000021// FIXME: remove once debugging is complete
caryclark@google.comfa0588f2012-04-26 21:01:06 +000022#if 01 // set to 1 for no debugging whatsoever
caryclark@google.com78e17132012-04-17 11:40:34 +000023
caryclark@google.comfa0588f2012-04-26 21:01:06 +000024const bool gRunTestsInOneThread = false;
caryclark@google.com78e17132012-04-17 11:40:34 +000025
caryclark@google.com2e7f4c82012-03-20 21:11:59 +000026#define DEBUG_ACTIVE_LESS_THAN 0
caryclark@google.com78e17132012-04-17 11:40:34 +000027#define DEBUG_ADD 0
28#define DEBUG_ADD_BOTTOM_TS 0
29#define DEBUG_ADD_INTERSECTING_TS 0
30#define DEBUG_ADJUST_COINCIDENT 0
caryclark@google.comfb173422012-04-10 18:28:55 +000031#define DEBUG_ASSEMBLE 0
caryclark@google.com78e17132012-04-17 11:40:34 +000032#define DEBUG_BOTTOM 0
caryclark@google.comfb173422012-04-10 18:28:55 +000033#define DEBUG_BRIDGE 0
caryclark@google.com78e17132012-04-17 11:40:34 +000034#define DEBUG_DUMP 0
caryclark@google.com2e7f4c82012-03-20 21:11:59 +000035#define DEBUG_SORT_HORIZONTAL 0
36#define DEBUG_OUT 0
37#define DEBUG_OUT_LESS_THAN 0
caryclark@google.com198e0542012-03-30 18:47:02 +000038#define DEBUG_SPLIT 0
caryclark@google.comfb173422012-04-10 18:28:55 +000039#define DEBUG_STITCH_EDGE 0
caryclark@google.com78e17132012-04-17 11:40:34 +000040#define DEBUG_TRIM_LINE 0
caryclark@google.com2e7f4c82012-03-20 21:11:59 +000041
caryclark@google.com78e17132012-04-17 11:40:34 +000042#else
43
44const bool gRunTestsInOneThread = true;
45
caryclark@google.coma5764232012-03-28 16:20:21 +000046#define DEBUG_ACTIVE_LESS_THAN 0
caryclark@google.com78e17132012-04-17 11:40:34 +000047#define DEBUG_ADD 01
48#define DEBUG_ADD_BOTTOM_TS 0
49#define DEBUG_ADD_INTERSECTING_TS 0
50#define DEBUG_ADJUST_COINCIDENT 1
caryclark@google.comfb173422012-04-10 18:28:55 +000051#define DEBUG_ASSEMBLE 1
caryclark@google.com78e17132012-04-17 11:40:34 +000052#define DEBUG_BOTTOM 0
caryclark@google.comfb173422012-04-10 18:28:55 +000053#define DEBUG_BRIDGE 1
caryclark@google.com78e17132012-04-17 11:40:34 +000054#define DEBUG_DUMP 1
caryclark@google.com2e7f4c82012-03-20 21:11:59 +000055#define DEBUG_SORT_HORIZONTAL 01
56#define DEBUG_OUT 01
57#define DEBUG_OUT_LESS_THAN 0
caryclark@google.com198e0542012-03-30 18:47:02 +000058#define DEBUG_SPLIT 1
caryclark@google.comfb173422012-04-10 18:28:55 +000059#define DEBUG_STITCH_EDGE 1
caryclark@google.com78e17132012-04-17 11:40:34 +000060#define DEBUG_TRIM_LINE 1
caryclark@google.com752b60e2012-03-22 21:11:17 +000061
caryclark@google.com2e7f4c82012-03-20 21:11:59 +000062#endif
63
caryclark@google.comfb173422012-04-10 18:28:55 +000064#if DEBUG_ASSEMBLE || DEBUG_BRIDGE
65static const char* kLVerbStr[] = {"", "line", "quad", "cubic"};
66#endif
67#if DEBUG_STITCH_EDGE
68static const char* kUVerbStr[] = {"", "Line", "Quad", "Cubic"};
69#endif
70
caryclark@google.com6680fb12012-02-07 22:10:51 +000071static int LineIntersect(const SkPoint a[2], const SkPoint b[2],
caryclark@google.coma5764232012-03-28 16:20:21 +000072 Intersections& intersections) {
73 const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}};
74 const _Line bLine = {{b[0].fX, b[0].fY}, {b[1].fX, b[1].fY}};
75 return intersect(aLine, bLine, intersections.fT[0], intersections.fT[1]);
76}
77
78static int QuadLineIntersect(const SkPoint a[3], const SkPoint b[2],
79 Intersections& intersections) {
80 const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}};
81 const _Line bLine = {{b[0].fX, b[0].fY}, {b[1].fX, b[1].fY}};
caryclark@google.com198e0542012-03-30 18:47:02 +000082 intersect(aQuad, bLine, intersections);
83 return intersections.fUsed;
caryclark@google.coma5764232012-03-28 16:20:21 +000084}
85
86static int CubicLineIntersect(const SkPoint a[2], const SkPoint b[3],
87 Intersections& intersections) {
88 const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY},
89 {a[3].fX, a[3].fY}};
90 const _Line bLine = {{b[0].fX, b[0].fY}, {b[1].fX, b[1].fY}};
caryclark@google.com198e0542012-03-30 18:47:02 +000091 return intersect(aCubic, bLine, intersections.fT[0], intersections.fT[1]);
caryclark@google.coma5764232012-03-28 16:20:21 +000092}
93
94static int QuadIntersect(const SkPoint a[3], const SkPoint b[3],
95 Intersections& intersections) {
96 const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}};
97 const Quadratic bQuad = {{b[0].fX, b[0].fY}, {b[1].fX, b[1].fY}, {b[2].fX, b[2].fY}};
caryclark@google.com198e0542012-03-30 18:47:02 +000098 intersect(aQuad, bQuad, intersections);
99 return intersections.fUsed;
caryclark@google.coma5764232012-03-28 16:20:21 +0000100}
101
102static int CubicIntersect(const SkPoint a[4], const SkPoint b[4],
103 Intersections& intersections) {
104 const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY},
105 {a[3].fX, a[3].fY}};
106 const Cubic bCubic = {{b[0].fX, b[0].fY}, {b[1].fX, b[1].fY}, {b[2].fX, b[2].fY},
107 {b[3].fX, b[3].fY}};
caryclark@google.com198e0542012-03-30 18:47:02 +0000108 intersect(aCubic, bCubic, intersections);
109 return intersections.fUsed;
caryclark@google.comc6825902012-02-03 22:07:47 +0000110}
111
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000112static int LineIntersect(const SkPoint a[2], SkScalar left, SkScalar right,
113 SkScalar y, double aRange[2]) {
caryclark@google.coma5764232012-03-28 16:20:21 +0000114 const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}};
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000115 return horizontalLineIntersect(aLine, left, right, y, aRange);
caryclark@google.comc6825902012-02-03 22:07:47 +0000116}
117
caryclark@google.com198e0542012-03-30 18:47:02 +0000118static int QuadIntersect(const SkPoint a[3], SkScalar left, SkScalar right,
119 SkScalar y, double aRange[3]) {
120 const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}};
121 return horizontalIntersect(aQuad, left, right, y, aRange);
122}
123
124static int CubicIntersect(const SkPoint a[4], SkScalar left, SkScalar right,
125 SkScalar y, double aRange[4]) {
126 const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY},
127 {a[3].fX, a[3].fY}};
128 return horizontalIntersect(aCubic, left, right, y, aRange);
129}
130
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000131static void LineXYAtT(const SkPoint a[2], double t, SkPoint* out) {
caryclark@google.coma5764232012-03-28 16:20:21 +0000132 const _Line line = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}};
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000133 double x, y;
caryclark@google.coma5764232012-03-28 16:20:21 +0000134 xy_at_t(line, t, x, y);
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000135 out->fX = SkDoubleToScalar(x);
136 out->fY = SkDoubleToScalar(y);
caryclark@google.comcef7e3f2012-02-28 16:57:05 +0000137}
138
caryclark@google.coma5764232012-03-28 16:20:21 +0000139static void QuadXYAtT(const SkPoint a[3], double t, SkPoint* out) {
140 const Quadratic quad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}};
141 double x, y;
142 xy_at_t(quad, t, x, y);
143 out->fX = SkDoubleToScalar(x);
144 out->fY = SkDoubleToScalar(y);
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000145}
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000146
caryclark@google.coma5764232012-03-28 16:20:21 +0000147static void CubicXYAtT(const SkPoint a[4], double t, SkPoint* out) {
148 const Cubic cubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY},
149 {a[3].fX, a[3].fY}};
150 double x, y;
151 xy_at_t(cubic, t, x, y);
152 out->fX = SkDoubleToScalar(x);
153 out->fY = SkDoubleToScalar(y);
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000154}
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000155
caryclark@google.com6680fb12012-02-07 22:10:51 +0000156static SkScalar LineYAtT(const SkPoint a[2], double t) {
caryclark@google.coma5764232012-03-28 16:20:21 +0000157 const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}};
caryclark@google.com6680fb12012-02-07 22:10:51 +0000158 double y;
159 xy_at_t(aLine, t, *(double*) 0, y);
160 return SkDoubleToScalar(y);
161}
162
caryclark@google.coma5764232012-03-28 16:20:21 +0000163static SkScalar QuadYAtT(const SkPoint a[3], double t) {
164 const Quadratic quad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}};
165 double y;
166 xy_at_t(quad, t, *(double*) 0, y);
167 return SkDoubleToScalar(y);
168}
169
170static SkScalar CubicYAtT(const SkPoint a[4], double t) {
171 const Cubic cubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY},
172 {a[3].fX, a[3].fY}};
173 double y;
174 xy_at_t(cubic, t, *(double*) 0, y);
175 return SkDoubleToScalar(y);
176}
177
caryclark@google.com6680fb12012-02-07 22:10:51 +0000178static void LineSubDivide(const SkPoint a[2], double startT, double endT,
179 SkPoint sub[2]) {
caryclark@google.coma5764232012-03-28 16:20:21 +0000180 const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}};
caryclark@google.com6680fb12012-02-07 22:10:51 +0000181 _Line dst;
182 sub_divide(aLine, startT, endT, dst);
183 sub[0].fX = SkDoubleToScalar(dst[0].x);
184 sub[0].fY = SkDoubleToScalar(dst[0].y);
185 sub[1].fX = SkDoubleToScalar(dst[1].x);
186 sub[1].fY = SkDoubleToScalar(dst[1].y);
187}
188
caryclark@google.coma5764232012-03-28 16:20:21 +0000189static void QuadSubDivide(const SkPoint a[3], double startT, double endT,
190 SkPoint sub[3]) {
caryclark@google.com78e17132012-04-17 11:40:34 +0000191 const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY},
192 {a[2].fX, a[2].fY}};
caryclark@google.coma5764232012-03-28 16:20:21 +0000193 Quadratic dst;
194 sub_divide(aQuad, startT, endT, dst);
195 sub[0].fX = SkDoubleToScalar(dst[0].x);
196 sub[0].fY = SkDoubleToScalar(dst[0].y);
197 sub[1].fX = SkDoubleToScalar(dst[1].x);
198 sub[1].fY = SkDoubleToScalar(dst[1].y);
199 sub[2].fX = SkDoubleToScalar(dst[2].x);
200 sub[2].fY = SkDoubleToScalar(dst[2].y);
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000201}
caryclark@google.com6680fb12012-02-07 22:10:51 +0000202
caryclark@google.coma5764232012-03-28 16:20:21 +0000203static void CubicSubDivide(const SkPoint a[4], double startT, double endT,
204 SkPoint sub[4]) {
caryclark@google.com78e17132012-04-17 11:40:34 +0000205 const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY},
206 {a[2].fX, a[2].fY}, {a[3].fX, a[3].fY}};
caryclark@google.coma5764232012-03-28 16:20:21 +0000207 Cubic dst;
208 sub_divide(aCubic, startT, endT, dst);
209 sub[0].fX = SkDoubleToScalar(dst[0].x);
210 sub[0].fY = SkDoubleToScalar(dst[0].y);
211 sub[1].fX = SkDoubleToScalar(dst[1].x);
212 sub[1].fY = SkDoubleToScalar(dst[1].y);
213 sub[2].fX = SkDoubleToScalar(dst[2].x);
214 sub[2].fY = SkDoubleToScalar(dst[2].y);
215 sub[3].fX = SkDoubleToScalar(dst[3].x);
216 sub[3].fY = SkDoubleToScalar(dst[3].y);
217}
caryclark@google.comfb173422012-04-10 18:28:55 +0000218
219static void QuadSubBounds(const SkPoint a[3], double startT, double endT,
220 SkRect& bounds) {
221 SkPoint dst[3];
222 QuadSubDivide(a, startT, endT, dst);
223 bounds.fLeft = bounds.fRight = dst[0].fX;
224 bounds.fTop = bounds.fBottom = dst[0].fY;
225 for (int index = 1; index < 3; ++index) {
226 bounds.growToInclude(dst[index].fX, dst[index].fY);
227 }
228}
229
230static void CubicSubBounds(const SkPoint a[4], double startT, double endT,
231 SkRect& bounds) {
232 SkPoint dst[4];
233 CubicSubDivide(a, startT, endT, dst);
234 bounds.fLeft = bounds.fRight = dst[0].fX;
235 bounds.fTop = bounds.fBottom = dst[0].fY;
236 for (int index = 1; index < 4; ++index) {
237 bounds.growToInclude(dst[index].fX, dst[index].fY);
238 }
239}
240
caryclark@google.com78e17132012-04-17 11:40:34 +0000241static SkPath::Verb QuadReduceOrder(SkPoint a[4]) {
242 const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY},
243 {a[2].fX, a[2].fY}};
244 Quadratic dst;
245 int order = reduceOrder(aQuad, dst);
246 for (int index = 0; index < order; ++index) {
247 a[index].fX = SkDoubleToScalar(dst[index].x);
248 a[index].fY = SkDoubleToScalar(dst[index].y);
249 }
250 if (order == 1) { // FIXME: allow returning points, caller should discard
251 a[1] = a[0];
252 return (SkPath::Verb) order;
253 }
254 return (SkPath::Verb) (order - 1);
255}
256
257static SkPath::Verb CubicReduceOrder(SkPoint a[4]) {
258 const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY},
259 {a[2].fX, a[2].fY}, {a[3].fX, a[3].fY}};
260 Cubic dst;
261 int order = reduceOrder(aCubic, dst, kReduceOrder_QuadraticsAllowed);
262 for (int index = 0; index < order; ++index) {
263 a[index].fX = SkDoubleToScalar(dst[index].x);
264 a[index].fY = SkDoubleToScalar(dst[index].y);
265 }
266 if (order == 1) { // FIXME: allow returning points, caller should discard
267 a[1] = a[0];
268 return (SkPath::Verb) order;
269 }
270 return (SkPath::Verb) (order - 1);
271}
272
273static bool IsCoincident(const SkPoint a[2], const SkPoint& above,
274 const SkPoint& below) {
275 const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}};
276 const _Line bLine = {{above.fX, above.fY}, {below.fX, below.fY}};
277 return implicit_matches_ulps(aLine, bLine, 32);
278}
279
caryclark@google.comc6825902012-02-03 22:07:47 +0000280/*
281list of edges
282bounds for edge
283sort
284active T
285
286if a contour's bounds is outside of the active area, no need to create edges
287*/
288
289/* given one or more paths,
290 find the bounds of each contour, select the active contours
291 for each active contour, compute a set of edges
292 each edge corresponds to one or more lines and curves
293 leave edges unbroken as long as possible
294 when breaking edges, compute the t at the break but leave the control points alone
295
296 */
297
298void contourBounds(const SkPath& path, SkTDArray<SkRect>& boundsArray) {
299 SkPath::Iter iter(path, false);
300 SkPoint pts[4];
301 SkPath::Verb verb;
302 SkRect bounds;
303 bounds.setEmpty();
304 int count = 0;
305 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
306 switch (verb) {
307 case SkPath::kMove_Verb:
308 if (!bounds.isEmpty()) {
309 *boundsArray.append() = bounds;
310 }
311 bounds.set(pts[0].fX, pts[0].fY, pts[0].fX, pts[0].fY);
312 count = 0;
313 break;
314 case SkPath::kLine_Verb:
315 count = 1;
316 break;
317 case SkPath::kQuad_Verb:
318 count = 2;
319 break;
320 case SkPath::kCubic_Verb:
321 count = 3;
322 break;
323 case SkPath::kClose_Verb:
324 count = 0;
325 break;
326 default:
327 SkDEBUGFAIL("bad verb");
328 return;
329 }
330 for (int i = 1; i <= count; ++i) {
331 bounds.growToInclude(pts[i].fX, pts[i].fY);
332 }
333 }
334}
335
caryclark@google.comf8b000d2012-02-09 22:04:27 +0000336static bool extendLine(const SkPoint line[2], const SkPoint& add) {
337 // FIXME: allow this to extend lines that have slopes that are nearly equal
338 SkScalar dx1 = line[1].fX - line[0].fX;
339 SkScalar dy1 = line[1].fY - line[0].fY;
340 SkScalar dx2 = add.fX - line[0].fX;
341 SkScalar dy2 = add.fY - line[0].fY;
342 return dx1 * dy2 == dx2 * dy1;
343}
caryclark@google.com6680fb12012-02-07 22:10:51 +0000344
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000345// OPTIMIZATION: this should point to a list of input data rather than duplicating
346// the line data here. This would reduce the need to assemble the results.
caryclark@google.comf8b000d2012-02-09 22:04:27 +0000347struct OutEdge {
348 bool operator<(const OutEdge& rh) const {
caryclark@google.comcef7e3f2012-02-28 16:57:05 +0000349 const SkPoint& first = fPts[0];
350 const SkPoint& rhFirst = rh.fPts[0];
caryclark@google.comf8b000d2012-02-09 22:04:27 +0000351 return first.fY == rhFirst.fY
352 ? first.fX < rhFirst.fX
353 : first.fY < rhFirst.fY;
354 }
caryclark@google.com752b60e2012-03-22 21:11:17 +0000355
caryclark@google.comcef7e3f2012-02-28 16:57:05 +0000356 SkPoint fPts[4];
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000357 int fID; // id of edge generating data
caryclark@google.comcef7e3f2012-02-28 16:57:05 +0000358 uint8_t fVerb; // FIXME: not read from everywhere
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000359 bool fCloseCall; // edge is trimmable if not originally coincident
caryclark@google.comf8b000d2012-02-09 22:04:27 +0000360};
361
caryclark@google.com6680fb12012-02-07 22:10:51 +0000362class OutEdgeBuilder {
363public:
caryclark@google.comf8b000d2012-02-09 22:04:27 +0000364 OutEdgeBuilder(bool fill)
365 : fFill(fill) {
caryclark@google.com6680fb12012-02-07 22:10:51 +0000366 }
caryclark@google.comf8b000d2012-02-09 22:04:27 +0000367
caryclark@google.coma5764232012-03-28 16:20:21 +0000368 void addCurve(const SkPoint line[4], SkPath::Verb verb, int id,
369 bool closeCall) {
caryclark@google.comc17972e2012-02-20 21:33:22 +0000370 OutEdge& newEdge = fEdges.push_back();
caryclark@google.coma5764232012-03-28 16:20:21 +0000371 memcpy(newEdge.fPts, line, (verb + 1) * sizeof(SkPoint));
372 newEdge.fVerb = verb;
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000373 newEdge.fID = id;
374 newEdge.fCloseCall = closeCall;
375 }
caryclark@google.com752b60e2012-03-22 21:11:17 +0000376
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000377 bool trimLine(SkScalar y, int id) {
378 size_t count = fEdges.count();
379 while (count-- != 0) {
380 OutEdge& edge = fEdges[count];
381 if (edge.fID != id) {
382 continue;
383 }
384 if (edge.fCloseCall) {
385 return false;
386 }
387 SkASSERT(edge.fPts[0].fY <= y);
388 if (edge.fPts[1].fY <= y) {
389 continue;
390 }
391 edge.fPts[1].fX = edge.fPts[0].fX + (y - edge.fPts[0].fY)
392 * (edge.fPts[1].fX - edge.fPts[0].fX)
393 / (edge.fPts[1].fY - edge.fPts[0].fY);
394 edge.fPts[1].fY = y;
caryclark@google.com78e17132012-04-17 11:40:34 +0000395#if DEBUG_TRIM_LINE
396 SkDebugf("%s edge=%d %1.9g,%1.9g\n", __FUNCTION__, id,
397 edge.fPts[1].fX, y);
398#endif
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000399 return true;
400 }
401 return false;
caryclark@google.comf8b000d2012-02-09 22:04:27 +0000402 }
403
404 void assemble(SkPath& simple) {
caryclark@google.com6008c6562012-02-15 22:01:16 +0000405 size_t listCount = fEdges.count();
406 if (listCount == 0) {
407 return;
caryclark@google.comf8b000d2012-02-09 22:04:27 +0000408 }
caryclark@google.com6008c6562012-02-15 22:01:16 +0000409 do {
410 size_t listIndex = 0;
411 int advance = 1;
412 while (listIndex < listCount && fTops[listIndex] == 0) {
413 ++listIndex;
414 }
415 if (listIndex >= listCount) {
416 break;
417 }
caryclark@google.com4917f172012-03-05 22:01:21 +0000418 int closeEdgeIndex = -listIndex - 1;
caryclark@google.comfb173422012-04-10 18:28:55 +0000419 // the curve is deferred and not added right away because the
420 // following edge may extend the first curve.
caryclark@google.coma5764232012-03-28 16:20:21 +0000421 SkPoint firstPt, lastCurve[4];
422 uint8_t lastVerb;
caryclark@google.comfb173422012-04-10 18:28:55 +0000423#if DEBUG_ASSEMBLE
424 int firstIndex, lastIndex;
425 const int tab = 8;
426#endif
caryclark@google.com6008c6562012-02-15 22:01:16 +0000427 bool doMove = true;
428 int edgeIndex;
429 do {
caryclark@google.comcef7e3f2012-02-28 16:57:05 +0000430 SkPoint* ptArray = fEdges[listIndex].fPts;
431 uint8_t verb = fEdges[listIndex].fVerb;
caryclark@google.comfb173422012-04-10 18:28:55 +0000432 SkPoint* curve[4];
caryclark@google.com6008c6562012-02-15 22:01:16 +0000433 if (advance < 0) {
caryclark@google.comfb173422012-04-10 18:28:55 +0000434 curve[0] = &ptArray[verb];
435 if (verb == SkPath::kCubic_Verb) {
436 curve[1] = &ptArray[2];
437 curve[2] = &ptArray[1];
438 }
439 curve[verb] = &ptArray[0];
caryclark@google.com6008c6562012-02-15 22:01:16 +0000440 } else {
caryclark@google.comfb173422012-04-10 18:28:55 +0000441 curve[0] = &ptArray[0];
442 if (verb == SkPath::kCubic_Verb) {
443 curve[1] = &ptArray[1];
444 curve[2] = &ptArray[2];
445 }
446 curve[verb] = &ptArray[verb];
447 }
448 if (verb == SkPath::kQuad_Verb) {
449 curve[1] = &ptArray[1];
caryclark@google.com6008c6562012-02-15 22:01:16 +0000450 }
caryclark@google.coma5764232012-03-28 16:20:21 +0000451 if (doMove) {
caryclark@google.comfb173422012-04-10 18:28:55 +0000452 firstPt = *curve[0];
453 simple.moveTo(curve[0]->fX, curve[0]->fY);
454#if DEBUG_ASSEMBLE
455 SkDebugf("%s %d moveTo (%g,%g)\n", __FUNCTION__,
456 listIndex + 1, curve[0]->fX, curve[0]->fY);
457 firstIndex = listIndex;
458#endif
459 for (int index = 0; index <= verb; ++index) {
460 lastCurve[index] = *curve[index];
caryclark@google.coma5764232012-03-28 16:20:21 +0000461 }
caryclark@google.coma5764232012-03-28 16:20:21 +0000462 doMove = false;
463 } else {
caryclark@google.comfb173422012-04-10 18:28:55 +0000464 bool gap = lastCurve[lastVerb] != *curve[0];
465 if (gap || lastVerb != SkPath::kLine_Verb) { // output the accumulated curve before the gap
caryclark@google.coma5764232012-03-28 16:20:21 +0000466 // FIXME: see comment in bridge -- this probably
467 // conceals errors
caryclark@google.comfb173422012-04-10 18:28:55 +0000468 SkASSERT(fFill && UlpsDiff(lastCurve[lastVerb].fY,
469 curve[0]->fY) <= 10);
caryclark@google.coma5764232012-03-28 16:20:21 +0000470 switch (lastVerb) {
471 case SkPath::kLine_Verb:
472 simple.lineTo(lastCurve[1].fX, lastCurve[1].fY);
473 break;
474 case SkPath::kQuad_Verb:
475 simple.quadTo(lastCurve[1].fX, lastCurve[1].fY,
476 lastCurve[2].fX, lastCurve[2].fY);
477 break;
478 case SkPath::kCubic_Verb:
479 simple.cubicTo(lastCurve[1].fX, lastCurve[1].fY,
480 lastCurve[2].fX, lastCurve[2].fY,
481 lastCurve[3].fX, lastCurve[3].fY);
482 break;
caryclark@google.comcef7e3f2012-02-28 16:57:05 +0000483 }
caryclark@google.comfb173422012-04-10 18:28:55 +0000484#if DEBUG_ASSEMBLE
485 SkDebugf("%*s %d %sTo (%g,%g)\n", tab, "", lastIndex + 1,
486 kLVerbStr[lastVerb], lastCurve[lastVerb].fX,
487 lastCurve[lastVerb].fY);
488#endif
caryclark@google.coma5764232012-03-28 16:20:21 +0000489 }
caryclark@google.comfb173422012-04-10 18:28:55 +0000490 int firstCopy = 1;
caryclark@google.com78e17132012-04-17 11:40:34 +0000491 if (gap || (lastVerb == SkPath::kLine_Verb
492 && (verb != SkPath::kLine_Verb
493 || !extendLine(lastCurve, *curve[verb])))) {
caryclark@google.coma5764232012-03-28 16:20:21 +0000494 // FIXME: see comment in bridge -- this probably
495 // conceals errors
caryclark@google.comfb173422012-04-10 18:28:55 +0000496 SkASSERT(lastCurve[lastVerb] == *curve[0] ||
497 (fFill && UlpsDiff(lastCurve[lastVerb].fY,
498 curve[0]->fY) <= 10));
499 simple.lineTo(curve[0]->fX, curve[0]->fY);
500#if DEBUG_ASSEMBLE
501 SkDebugf("%*s %d gap lineTo (%g,%g)\n", tab, "",
502 lastIndex + 1, curve[0]->fX, curve[0]->fY);
503#endif
504 firstCopy = 0;
505 } else if (lastVerb != SkPath::kLine_Verb) {
506 firstCopy = 0;
caryclark@google.coma5764232012-03-28 16:20:21 +0000507 }
caryclark@google.comfb173422012-04-10 18:28:55 +0000508 for (int index = firstCopy; index <= verb; ++index) {
509 lastCurve[index] = *curve[index];
caryclark@google.coma5764232012-03-28 16:20:21 +0000510 }
caryclark@google.com6008c6562012-02-15 22:01:16 +0000511 }
caryclark@google.comfb173422012-04-10 18:28:55 +0000512 lastVerb = verb;
513#if DEBUG_ASSEMBLE
514 lastIndex = listIndex;
515#endif
caryclark@google.com6008c6562012-02-15 22:01:16 +0000516 if (advance < 0) {
517 edgeIndex = fTops[listIndex];
518 fTops[listIndex] = 0;
caryclark@google.comfb173422012-04-10 18:28:55 +0000519 } else {
caryclark@google.com6008c6562012-02-15 22:01:16 +0000520 edgeIndex = fBottoms[listIndex];
521 fBottoms[listIndex] = 0;
522 }
caryclark@google.com4917f172012-03-05 22:01:21 +0000523 if (edgeIndex) {
524 listIndex = abs(edgeIndex) - 1;
525 if (edgeIndex < 0) {
526 fTops[listIndex] = 0;
527 } else {
528 fBottoms[listIndex] = 0;
529 }
530 }
531 if (edgeIndex == closeEdgeIndex || edgeIndex == 0) {
caryclark@google.comfb173422012-04-10 18:28:55 +0000532 switch (lastVerb) {
533 case SkPath::kLine_Verb:
534 simple.lineTo(lastCurve[1].fX, lastCurve[1].fY);
535 break;
536 case SkPath::kQuad_Verb:
537 simple.quadTo(lastCurve[1].fX, lastCurve[1].fY,
538 lastCurve[2].fX, lastCurve[2].fY);
539 break;
540 case SkPath::kCubic_Verb:
541 simple.cubicTo(lastCurve[1].fX, lastCurve[1].fY,
542 lastCurve[2].fX, lastCurve[2].fY,
543 lastCurve[3].fX, lastCurve[3].fY);
544 break;
545 }
546#if DEBUG_ASSEMBLE
547 SkDebugf("%*s %d %sTo last (%g, %g)\n", tab, "",
548 lastIndex + 1, kLVerbStr[lastVerb],
549 lastCurve[lastVerb].fX, lastCurve[lastVerb].fY);
550#endif
caryclark@google.coma5764232012-03-28 16:20:21 +0000551 if (lastCurve[lastVerb] != firstPt) {
caryclark@google.comfb173422012-04-10 18:28:55 +0000552 simple.lineTo(firstPt.fX, firstPt.fY);
553#if DEBUG_ASSEMBLE
554 SkDebugf("%*s %d final line (%g, %g)\n", tab, "",
555 firstIndex + 1, firstPt.fX, firstPt.fY);
556#endif
caryclark@google.com4917f172012-03-05 22:01:21 +0000557 }
caryclark@google.comcef7e3f2012-02-28 16:57:05 +0000558 simple.close();
caryclark@google.comfb173422012-04-10 18:28:55 +0000559#if DEBUG_ASSEMBLE
560 SkDebugf("%*s close\n", tab, "");
561#endif
caryclark@google.comcef7e3f2012-02-28 16:57:05 +0000562 break;
563 }
caryclark@google.comfb173422012-04-10 18:28:55 +0000564 // if this and next edge go different directions
565#if DEBUG_ASSEMBLE
566 SkDebugf("%*s advance=%d edgeIndex=%d flip=%s\n", tab, "",
567 advance, edgeIndex, advance > 0 ^ edgeIndex < 0 ?
568 "true" : "false");
569#endif
caryclark@google.com6008c6562012-02-15 22:01:16 +0000570 if (advance > 0 ^ edgeIndex < 0) {
571 advance = -advance;
572 }
caryclark@google.com4917f172012-03-05 22:01:21 +0000573 } while (edgeIndex);
caryclark@google.com6008c6562012-02-15 22:01:16 +0000574 } while (true);
caryclark@google.comf8b000d2012-02-09 22:04:27 +0000575 }
caryclark@google.com752b60e2012-03-22 21:11:17 +0000576
caryclark@google.comcef7e3f2012-02-28 16:57:05 +0000577 // sort points by y, then x
578 // if x/y is identical, sort bottoms before tops
579 // if identical and both tops/bottoms, sort by angle
580 static bool lessThan(SkTArray<OutEdge>& edges, const int one,
581 const int two) {
582 const OutEdge& oneEdge = edges[abs(one) - 1];
583 int oneIndex = one < 0 ? 0 : oneEdge.fVerb;
584 const SkPoint& startPt1 = oneEdge.fPts[oneIndex];
585 const OutEdge& twoEdge = edges[abs(two) - 1];
586 int twoIndex = two < 0 ? 0 : twoEdge.fVerb;
587 const SkPoint& startPt2 = twoEdge.fPts[twoIndex];
588 if (startPt1.fY != startPt2.fY) {
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000589 #if DEBUG_OUT_LESS_THAN
590 SkDebugf("%s %d<%d (%g,%g) %s startPt1.fY < startPt2.fY\n", __FUNCTION__,
591 one, two, startPt1.fY, startPt2.fY,
592 startPt1.fY < startPt2.fY ? "true" : "false");
593 #endif
caryclark@google.comcef7e3f2012-02-28 16:57:05 +0000594 return startPt1.fY < startPt2.fY;
595 }
596 if (startPt1.fX != startPt2.fX) {
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000597 #if DEBUG_OUT_LESS_THAN
598 SkDebugf("%s %d<%d (%g,%g) %s startPt1.fX < startPt2.fX\n", __FUNCTION__,
599 one, two, startPt1.fX, startPt2.fX,
600 startPt1.fX < startPt2.fX ? "true" : "false");
601 #endif
caryclark@google.comcef7e3f2012-02-28 16:57:05 +0000602 return startPt1.fX < startPt2.fX;
603 }
604 const SkPoint& endPt1 = oneEdge.fPts[oneIndex ^ oneEdge.fVerb];
605 const SkPoint& endPt2 = twoEdge.fPts[twoIndex ^ twoEdge.fVerb];
606 SkScalar dy1 = startPt1.fY - endPt1.fY;
607 SkScalar dy2 = startPt2.fY - endPt2.fY;
608 SkScalar dy1y2 = dy1 * dy2;
609 if (dy1y2 < 0) { // different signs
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000610 #if DEBUG_OUT_LESS_THAN
caryclark@google.comcef7e3f2012-02-28 16:57:05 +0000611 SkDebugf("%s %d<%d %s dy1 > 0\n", __FUNCTION__, one, two,
612 dy1 > 0 ? "true" : "false");
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000613 #endif
caryclark@google.comcef7e3f2012-02-28 16:57:05 +0000614 return dy1 > 0; // one < two if one goes up and two goes down
615 }
616 if (dy1y2 == 0) {
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000617 #if DEBUG_OUT_LESS_THAN
618 SkDebugf("%s %d<%d %s endPt1.fX < endPt2.fX\n", __FUNCTION__,
619 one, two, endPt1.fX < endPt2.fX ? "true" : "false");
620 #endif
caryclark@google.comcef7e3f2012-02-28 16:57:05 +0000621 return endPt1.fX < endPt2.fX;
622 }
623 SkScalar dx1y2 = (startPt1.fX - endPt1.fX) * dy2;
624 SkScalar dx2y1 = (startPt2.fX - endPt2.fX) * dy1;
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000625 #if DEBUG_OUT_LESS_THAN
626 SkDebugf("%s %d<%d %s dy2 < 0 ^ dx1y2 < dx2y1\n", __FUNCTION__,
627 one, two, dy2 < 0 ^ dx1y2 < dx2y1 ? "true" : "false");
628 #endif
caryclark@google.comcef7e3f2012-02-28 16:57:05 +0000629 return dy2 > 0 ^ dx1y2 < dx2y1;
caryclark@google.comf8b000d2012-02-09 22:04:27 +0000630 }
631
caryclark@google.com6008c6562012-02-15 22:01:16 +0000632 // Sort the indices of paired points and then create more indices so
633 // assemble() can find the next edge and connect the top or bottom
caryclark@google.comf8b000d2012-02-09 22:04:27 +0000634 void bridge() {
635 size_t index;
636 size_t count = fEdges.count();
637 if (!count) {
638 return;
639 }
caryclark@google.comcef7e3f2012-02-28 16:57:05 +0000640 SkASSERT(!fFill || count > 1);
caryclark@google.comf8b000d2012-02-09 22:04:27 +0000641 fTops.setCount(count);
642 sk_bzero(fTops.begin(), sizeof(fTops[0]) * count);
643 fBottoms.setCount(count);
644 sk_bzero(fBottoms.begin(), sizeof(fBottoms[0]) * count);
caryclark@google.com6008c6562012-02-15 22:01:16 +0000645 SkTDArray<int> order;
646 for (index = 1; index <= count; ++index) {
caryclark@google.com6008c6562012-02-15 22:01:16 +0000647 *order.append() = -index;
caryclark@google.comf8b000d2012-02-09 22:04:27 +0000648 }
caryclark@google.comcef7e3f2012-02-28 16:57:05 +0000649 for (index = 1; index <= count; ++index) {
650 *order.append() = index;
651 }
652 QSort<SkTArray<OutEdge>, int>(fEdges, order.begin(), order.end() - 1, lessThan);
caryclark@google.com6008c6562012-02-15 22:01:16 +0000653 int* lastPtr = order.end() - 1;
654 int* leftPtr = order.begin();
655 while (leftPtr < lastPtr) {
656 int leftIndex = *leftPtr;
657 int leftOutIndex = abs(leftIndex) - 1;
658 const OutEdge& left = fEdges[leftOutIndex];
caryclark@google.comf8b000d2012-02-09 22:04:27 +0000659 int* rightPtr = leftPtr + 1;
caryclark@google.com6008c6562012-02-15 22:01:16 +0000660 int rightIndex = *rightPtr;
661 int rightOutIndex = abs(rightIndex) - 1;
662 const OutEdge& right = fEdges[rightOutIndex];
caryclark@google.comcef7e3f2012-02-28 16:57:05 +0000663 bool pairUp = fFill;
664 if (!pairUp) {
665 const SkPoint& leftMatch =
666 left.fPts[leftIndex < 0 ? 0 : left.fVerb];
667 const SkPoint& rightMatch =
668 right.fPts[rightIndex < 0 ? 0 : right.fVerb];
669 pairUp = leftMatch == rightMatch;
670 } else {
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000671 #if DEBUG_OUT
caryclark@google.comd88e0892012-03-27 13:23:51 +0000672 // FIXME : not happy that error in low bit is allowed
673 // this probably conceals error elsewhere
674 if (UlpsDiff(left.fPts[leftIndex < 0 ? 0 : left.fVerb].fY,
675 right.fPts[rightIndex < 0 ? 0 : right.fVerb].fY) > 1) {
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000676 *fMismatches.append() = leftIndex;
677 if (rightPtr == lastPtr) {
678 *fMismatches.append() = rightIndex;
679 }
680 pairUp = false;
681 }
682 #else
caryclark@google.comd88e0892012-03-27 13:23:51 +0000683 SkASSERT(UlpsDiff(left.fPts[leftIndex < 0 ? 0 : left.fVerb].fY,
684 right.fPts[rightIndex < 0 ? 0 : right.fVerb].fY) <= 10);
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000685 #endif
caryclark@google.comcef7e3f2012-02-28 16:57:05 +0000686 }
687 if (pairUp) {
caryclark@google.com6008c6562012-02-15 22:01:16 +0000688 if (leftIndex < 0) {
689 fTops[leftOutIndex] = rightIndex;
690 } else {
691 fBottoms[leftOutIndex] = rightIndex;
692 }
693 if (rightIndex < 0) {
694 fTops[rightOutIndex] = leftIndex;
695 } else {
696 fBottoms[rightOutIndex] = leftIndex;
697 }
caryclark@google.comf8b000d2012-02-09 22:04:27 +0000698 ++rightPtr;
699 }
700 leftPtr = rightPtr;
701 }
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000702#if DEBUG_OUT
703 int* mismatch = fMismatches.begin();
704 while (mismatch != fMismatches.end()) {
705 int leftIndex = *mismatch++;
706 int leftOutIndex = abs(leftIndex) - 1;
707 const OutEdge& left = fEdges[leftOutIndex];
708 const SkPoint& leftPt = left.fPts[leftIndex < 0 ? 0 : left.fVerb];
709 SkDebugf("%s left=%d %s (%1.9g,%1.9g)\n",
710 __FUNCTION__, left.fID, leftIndex < 0 ? "top" : "bot",
711 leftPt.fX, leftPt.fY);
712 }
713 SkASSERT(fMismatches.count() == 0);
714#endif
caryclark@google.comfb173422012-04-10 18:28:55 +0000715#if DEBUG_BRIDGE
716 for (index = 0; index < count; ++index) {
717 const OutEdge& edge = fEdges[index];
718 uint8_t verb = edge.fVerb;
719 SkDebugf("%s %d edge=%d %s (%1.9g,%1.9g) (%1.9g,%1.9g)\n",
720 index == 0 ? __FUNCTION__ : " ",
721 index + 1, edge.fID, kLVerbStr[verb], edge.fPts[0].fX,
722 edge.fPts[0].fY, edge.fPts[verb].fX, edge.fPts[verb].fY);
723 }
724 for (index = 0; index < count; ++index) {
725 SkDebugf(" top of % 2d connects to %s of % 2d\n", index + 1,
726 fTops[index] < 0 ? "top " : "bottom", abs(fTops[index]));
727 SkDebugf(" bottom of % 2d connects to %s of % 2d\n", index + 1,
728 fBottoms[index] < 0 ? "top " : "bottom", abs(fBottoms[index]));
729 }
730#endif
caryclark@google.comf8b000d2012-02-09 22:04:27 +0000731 }
732
caryclark@google.com6008c6562012-02-15 22:01:16 +0000733protected:
caryclark@google.com6680fb12012-02-07 22:10:51 +0000734 SkTArray<OutEdge> fEdges;
caryclark@google.com6008c6562012-02-15 22:01:16 +0000735 SkTDArray<int> fTops;
736 SkTDArray<int> fBottoms;
caryclark@google.comf8b000d2012-02-09 22:04:27 +0000737 bool fFill;
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000738#if DEBUG_OUT
739 SkTDArray<int> fMismatches;
740#endif
caryclark@google.com6680fb12012-02-07 22:10:51 +0000741};
742
caryclark@google.comc6825902012-02-03 22:07:47 +0000743// Bounds, unlike Rect, does not consider a vertical line to be empty.
744struct Bounds : public SkRect {
745 static bool Intersects(const Bounds& a, const Bounds& b) {
746 return a.fLeft <= b.fRight && b.fLeft <= a.fRight &&
747 a.fTop <= b.fBottom && b.fTop <= a.fBottom;
748 }
caryclark@google.com752b60e2012-03-22 21:11:17 +0000749
caryclark@google.com6008c6562012-02-15 22:01:16 +0000750 bool isEmpty() {
751 return fLeft > fRight || fTop > fBottom
752 || fLeft == fRight && fTop == fBottom
753 || isnan(fLeft) || isnan(fRight)
754 || isnan(fTop) || isnan(fBottom);
755 }
caryclark@google.comc6825902012-02-03 22:07:47 +0000756};
757
caryclark@google.com4917f172012-03-05 22:01:21 +0000758class Intercepts {
759public:
760 Intercepts()
761 : fTopIntercepts(0)
caryclark@google.com198e0542012-03-30 18:47:02 +0000762 , fBottomIntercepts(0)
763 , fExplicit(false) {
764 }
765
766 Intercepts& operator=(const Intercepts& src) {
767 fTs = src.fTs;
768 fTopIntercepts = src.fTopIntercepts;
769 fBottomIntercepts = src.fBottomIntercepts;
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000770 return *this;
caryclark@google.com198e0542012-03-30 18:47:02 +0000771 }
772
773 // OPTIMIZATION: remove this function if it's never called
774 double t(int tIndex) const {
775 if (tIndex == 0) {
776 return 0;
777 }
778 if (tIndex > fTs.count()) {
779 return 1;
780 }
781 return fTs[tIndex - 1];
caryclark@google.com4917f172012-03-05 22:01:21 +0000782 }
caryclark@google.com752b60e2012-03-22 21:11:17 +0000783
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000784#if DEBUG_DUMP
caryclark@google.coma5764232012-03-28 16:20:21 +0000785 void dump(const SkPoint* pts, SkPath::Verb verb) {
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000786 const char className[] = "Intercepts";
787 const int tab = 8;
788 for (int i = 0; i < fTs.count(); ++i) {
789 SkPoint out;
caryclark@google.coma5764232012-03-28 16:20:21 +0000790 switch (verb) {
791 case SkPath::kLine_Verb:
792 LineXYAtT(pts, fTs[i], &out);
793 break;
794 case SkPath::kQuad_Verb:
795 QuadXYAtT(pts, fTs[i], &out);
796 break;
797 case SkPath::kCubic_Verb:
798 CubicXYAtT(pts, fTs[i], &out);
799 break;
800 default:
801 SkASSERT(0);
802 }
caryclark@google.com752b60e2012-03-22 21:11:17 +0000803 SkDebugf("%*s.fTs[%d]=%1.9g (%1.9g,%1.9g)\n", tab + sizeof(className),
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000804 className, i, fTs[i], out.fX, out.fY);
805 }
caryclark@google.com198e0542012-03-30 18:47:02 +0000806 SkDebugf("%*s.fTopIntercepts=%u\n", tab + sizeof(className),
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000807 className, fTopIntercepts);
caryclark@google.com198e0542012-03-30 18:47:02 +0000808 SkDebugf("%*s.fBottomIntercepts=%u\n", tab + sizeof(className),
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000809 className, fBottomIntercepts);
caryclark@google.comfb173422012-04-10 18:28:55 +0000810 SkDebugf("%*s.fExplicit=%d\n", tab + sizeof(className),
811 className, fExplicit);
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000812 }
813#endif
814
caryclark@google.comc6825902012-02-03 22:07:47 +0000815 SkTDArray<double> fTs;
caryclark@google.com198e0542012-03-30 18:47:02 +0000816 unsigned char fTopIntercepts; // 0=init state 1=1 edge >1=multiple edges
817 unsigned char fBottomIntercepts;
818 bool fExplicit; // if set, suppress 0 and 1
819
caryclark@google.comc6825902012-02-03 22:07:47 +0000820};
821
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000822struct HorizontalEdge {
823 bool operator<(const HorizontalEdge& rh) const {
824 return fY == rh.fY ? fLeft == rh.fLeft ? fRight < rh.fRight
825 : fLeft < rh.fLeft : fY < rh.fY;
826 }
827
828#if DEBUG_DUMP
829 void dump() {
830 const char className[] = "HorizontalEdge";
831 const int tab = 4;
caryclark@google.com752b60e2012-03-22 21:11:17 +0000832 SkDebugf("%*s.fLeft=%1.9g\n", tab + sizeof(className), className, fLeft);
833 SkDebugf("%*s.fRight=%1.9g\n", tab + sizeof(className), className, fRight);
834 SkDebugf("%*s.fY=%1.9g\n", tab + sizeof(className), className, fY);
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000835 }
836#endif
837
838 SkScalar fLeft;
839 SkScalar fRight;
840 SkScalar fY;
841};
842
caryclark@google.com6680fb12012-02-07 22:10:51 +0000843struct InEdge {
844 bool operator<(const InEdge& rh) const {
caryclark@google.comc6825902012-02-03 22:07:47 +0000845 return fBounds.fTop == rh.fBounds.fTop
846 ? fBounds.fLeft < rh.fBounds.fLeft
847 : fBounds.fTop < rh.fBounds.fTop;
848 }
849
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000850 // Avoid collapsing t values that are close to the same since
851 // we walk ts to describe consecutive intersections. Since a pair of ts can
852 // be nearly equal, any problems caused by this should be taken care
853 // of later.
854 int add(double* ts, size_t count, ptrdiff_t verbIndex) {
caryclark@google.comc6825902012-02-03 22:07:47 +0000855 // FIXME: in the pathological case where there is a ton of intercepts, binary search?
caryclark@google.com6008c6562012-02-15 22:01:16 +0000856 bool foundIntercept = false;
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000857 int insertedAt = -1;
caryclark@google.com6008c6562012-02-15 22:01:16 +0000858 Intercepts& intercepts = fIntercepts[verbIndex];
caryclark@google.comc6825902012-02-03 22:07:47 +0000859 for (size_t index = 0; index < count; ++index) {
860 double t = ts[index];
caryclark@google.com4917f172012-03-05 22:01:21 +0000861 if (t <= 0) {
caryclark@google.com198e0542012-03-30 18:47:02 +0000862 intercepts.fTopIntercepts <<= 1;
caryclark@google.com4917f172012-03-05 22:01:21 +0000863 fContainsIntercepts |= ++intercepts.fTopIntercepts > 1;
864 continue;
865 }
866 if (t >= 1) {
caryclark@google.com198e0542012-03-30 18:47:02 +0000867 intercepts.fBottomIntercepts <<= 1;
caryclark@google.com4917f172012-03-05 22:01:21 +0000868 fContainsIntercepts |= ++intercepts.fBottomIntercepts > 1;
caryclark@google.com6008c6562012-02-15 22:01:16 +0000869 continue;
870 }
caryclark@google.com198e0542012-03-30 18:47:02 +0000871 fIntersected = true;
caryclark@google.com6008c6562012-02-15 22:01:16 +0000872 foundIntercept = true;
caryclark@google.comc6825902012-02-03 22:07:47 +0000873 size_t tCount = intercepts.fTs.count();
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000874 double delta;
caryclark@google.comc6825902012-02-03 22:07:47 +0000875 for (size_t idx2 = 0; idx2 < tCount; ++idx2) {
876 if (t <= intercepts.fTs[idx2]) {
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000877 // FIXME: ? if (t < intercepts.fTs[idx2]) // failed
878 delta = intercepts.fTs[idx2] - t;
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000879 if (delta > 0) {
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000880 insertedAt = idx2;
caryclark@google.comc6825902012-02-03 22:07:47 +0000881 *intercepts.fTs.insert(idx2) = t;
caryclark@google.comc6825902012-02-03 22:07:47 +0000882 }
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000883 goto nextPt;
caryclark@google.comc6825902012-02-03 22:07:47 +0000884 }
885 }
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000886 if (tCount == 0 || (delta = t - intercepts.fTs[tCount - 1]) > 0) {
887 insertedAt = tCount;
caryclark@google.comc6825902012-02-03 22:07:47 +0000888 *intercepts.fTs.append() = t;
889 }
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000890 nextPt:
891 ;
caryclark@google.comc6825902012-02-03 22:07:47 +0000892 }
caryclark@google.com4917f172012-03-05 22:01:21 +0000893 fContainsIntercepts |= foundIntercept;
caryclark@google.com2e7f4c82012-03-20 21:11:59 +0000894 return insertedAt;
caryclark@google.comc6825902012-02-03 22:07:47 +0000895 }
caryclark@google.com198e0542012-03-30 18:47:02 +0000896
caryclark@google.comfb173422012-04-10 18:28:55 +0000897 void addPartial(SkTArray<InEdge>& edges, int ptStart, int ptEnd,
caryclark@google.com198e0542012-03-30 18:47:02 +0000898 int verbStart, int verbEnd) {
899 InEdge* edge = edges.push_back_n(1);
900 int verbCount = verbEnd - verbStart;
901 edge->fIntercepts.push_back_n(verbCount);
caryclark@google.coma3f05fa2012-06-01 17:44:28 +0000902 // uint8_t* verbs = &fVerbs[verbStart];
caryclark@google.com198e0542012-03-30 18:47:02 +0000903 for (int ceptIdx = 0; ceptIdx < verbCount; ++ceptIdx) {
904 edge->fIntercepts[ceptIdx] = fIntercepts[verbStart + ceptIdx];
905 }
906 edge->fPts.append(ptEnd - ptStart, &fPts[ptStart]);
907 edge->fVerbs.append(verbCount, &fVerbs[verbStart]);
908 edge->setBounds();
caryclark@google.com198e0542012-03-30 18:47:02 +0000909 edge->fWinding = fWinding;
910 edge->fContainsIntercepts = fContainsIntercepts; // FIXME: may not be correct -- but do we need to know?
911 }
912
caryclark@google.comfb173422012-04-10 18:28:55 +0000913 void addSplit(SkTArray<InEdge>& edges, SkPoint* pts, uint8_t verb,
914 Intercepts& intercepts, int firstT, int lastT, bool flipped) {
caryclark@google.com198e0542012-03-30 18:47:02 +0000915 InEdge* edge = edges.push_back_n(1);
916 edge->fIntercepts.push_back_n(1);
caryclark@google.comfb173422012-04-10 18:28:55 +0000917 if (firstT == 0) {
918 *edge->fIntercepts[0].fTs.append() = 0;
919 } else {
920 *edge->fIntercepts[0].fTs.append() = intercepts.fTs[firstT - 1];
921 }
922 bool add1 = lastT == intercepts.fTs.count();
923 edge->fIntercepts[0].fTs.append(lastT - firstT, &intercepts.fTs[firstT]);
924 if (add1) {
925 *edge->fIntercepts[0].fTs.append() = 1;
926 }
caryclark@google.com198e0542012-03-30 18:47:02 +0000927 edge->fIntercepts[0].fExplicit = true;
caryclark@google.comfb173422012-04-10 18:28:55 +0000928 edge->fPts.append(verb + 1, pts);
caryclark@google.com198e0542012-03-30 18:47:02 +0000929 edge->fVerbs.append(1, &verb);
caryclark@google.comfb173422012-04-10 18:28:55 +0000930 // FIXME: bounds could be better for partial Ts
931 edge->setSubBounds();
caryclark@google.com198e0542012-03-30 18:47:02 +0000932 edge->fContainsIntercepts = fContainsIntercepts; // FIXME: may not be correct -- but do we need to know?
933 if (flipped) {
caryclark@google.comfb173422012-04-10 18:28:55 +0000934 edge->flipTs();
935 edge->fWinding = -fWinding;
936 } else {
937 edge->fWinding = fWinding;
caryclark@google.com198e0542012-03-30 18:47:02 +0000938 }
939 }
caryclark@google.comc6825902012-02-03 22:07:47 +0000940
caryclark@google.com6680fb12012-02-07 22:10:51 +0000941 bool cached(const InEdge* edge) {
caryclark@google.comc6825902012-02-03 22:07:47 +0000942 // FIXME: in the pathological case where there is a ton of edges, binary search?
943 size_t count = fCached.count();
944 for (size_t index = 0; index < count; ++index) {
945 if (edge == fCached[index]) {
946 return true;
947 }
948 if (edge < fCached[index]) {
949 *fCached.insert(index) = edge;
950 return false;
951 }
952 }
953 *fCached.append() = edge;
954 return false;
955 }
956
caryclark@google.comfb173422012-04-10 18:28:55 +0000957 void complete(signed char winding) {
caryclark@google.com198e0542012-03-30 18:47:02 +0000958 setBounds();
959 fIntercepts.push_back_n(fVerbs.count());
960 if ((fWinding = winding) < 0) { // reverse verbs, pts, if bottom to top
961 flip();
962 }
963 fContainsIntercepts = fIntersected = false;
caryclark@google.com198e0542012-03-30 18:47:02 +0000964 }
965
caryclark@google.comfb173422012-04-10 18:28:55 +0000966 void flip() {
caryclark@google.com198e0542012-03-30 18:47:02 +0000967 size_t index;
968 size_t last = fPts.count() - 1;
969 for (index = 0; index < last; ++index, --last) {
970 SkTSwap<SkPoint>(fPts[index], fPts[last]);
971 }
972 last = fVerbs.count() - 1;
973 for (index = 0; index < last; ++index, --last) {
974 SkTSwap<uint8_t>(fVerbs[index], fVerbs[last]);
975 }
976 }
caryclark@google.comfb173422012-04-10 18:28:55 +0000977
978 void flipTs() {
979 SkASSERT(fIntercepts.count() == 1);
980 Intercepts& intercepts = fIntercepts[0];
981 SkASSERT(intercepts.fExplicit);
982 SkTDArray<double>& ts = intercepts.fTs;
983 size_t index;
984 size_t last = ts.count() - 1;
985 for (index = 0; index < last; ++index, --last) {
986 SkTSwap<double>(ts[index], ts[last]);
987 }
988 }
caryclark@google.com198e0542012-03-30 18:47:02 +0000989
990 void reset() {
991 fCached.reset();
992 fIntercepts.reset();
993 fPts.reset();
994 fVerbs.reset();
995 fBounds.set(SK_ScalarMax, SK_ScalarMax, SK_ScalarMax, SK_ScalarMax);
caryclark@google.com198e0542012-03-30 18:47:02 +0000996 fWinding = 0;
997 fContainsIntercepts = false;
998 fIntersected = false;
999 }
1000
1001 void setBounds() {
caryclark@google.comc6825902012-02-03 22:07:47 +00001002 SkPoint* ptPtr = fPts.begin();
1003 SkPoint* ptLast = fPts.end();
1004 if (ptPtr == ptLast) {
caryclark@google.com198e0542012-03-30 18:47:02 +00001005 SkDebugf("%s empty edge\n", __FUNCTION__);
caryclark@google.comc6825902012-02-03 22:07:47 +00001006 SkASSERT(0);
1007 // FIXME: delete empty edge?
1008 return;
1009 }
1010 fBounds.set(ptPtr->fX, ptPtr->fY, ptPtr->fX, ptPtr->fY);
1011 ++ptPtr;
1012 while (ptPtr != ptLast) {
1013 fBounds.growToInclude(ptPtr->fX, ptPtr->fY);
1014 ++ptPtr;
1015 }
caryclark@google.com198e0542012-03-30 18:47:02 +00001016 }
caryclark@google.comfb173422012-04-10 18:28:55 +00001017
1018 // recompute bounds based on subrange of T values
1019 void setSubBounds() {
1020 SkASSERT(fIntercepts.count() == 1);
1021 Intercepts& intercepts = fIntercepts[0];
1022 SkASSERT(intercepts.fExplicit);
1023 SkASSERT(fVerbs.count() == 1);
1024 SkTDArray<double>& ts = intercepts.fTs;
1025 if (fVerbs[0] == SkPath::kQuad_Verb) {
1026 SkASSERT(fPts.count() == 3);
1027 QuadSubBounds(fPts.begin(), ts[0], ts[ts.count() - 1], fBounds);
1028 } else {
1029 SkASSERT(fVerbs[0] == SkPath::kCubic_Verb);
1030 SkASSERT(fPts.count() == 4);
1031 CubicSubBounds(fPts.begin(), ts[0], ts[ts.count() - 1], fBounds);
1032 }
1033 }
caryclark@google.com198e0542012-03-30 18:47:02 +00001034
caryclark@google.comfb173422012-04-10 18:28:55 +00001035 void splitInflectionPts(SkTArray<InEdge>& edges) {
caryclark@google.com198e0542012-03-30 18:47:02 +00001036 if (!fIntersected) {
1037 return;
1038 }
1039 uint8_t* verbs = fVerbs.begin();
1040 SkPoint* pts = fPts.begin();
1041 int lastVerb = 0;
1042 int lastPt = 0;
1043 uint8_t verb;
1044 bool edgeSplit = false;
1045 for (int ceptIdx = 0; ceptIdx < fIntercepts.count(); ++ceptIdx, pts += verb) {
1046 Intercepts& intercepts = fIntercepts[ceptIdx];
1047 verb = *verbs++;
1048 if (verb <= SkPath::kLine_Verb) {
1049 continue;
caryclark@google.com6680fb12012-02-07 22:10:51 +00001050 }
caryclark@google.com198e0542012-03-30 18:47:02 +00001051 size_t tCount = intercepts.fTs.count();
1052 if (!tCount) {
1053 continue;
1054 }
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001055 size_t tIndex = (size_t) -1;
caryclark@google.com198e0542012-03-30 18:47:02 +00001056 SkScalar y = pts[0].fY;
1057 int lastSplit = 0;
1058 int firstSplit = -1;
1059 bool curveSplit = false;
1060 while (++tIndex < tCount) {
1061 double nextT = intercepts.fTs[tIndex];
1062 SkScalar nextY = verb == SkPath::kQuad_Verb
1063 ? QuadYAtT(pts, nextT) : CubicYAtT(pts, nextT);
1064 if (nextY < y) {
1065 edgeSplit = curveSplit = true;
1066 if (firstSplit < 0) {
1067 firstSplit = tIndex;
1068 int nextPt = pts - fPts.begin();
1069 int nextVerb = verbs - 1 - fVerbs.begin();
1070 if (lastVerb < nextVerb) {
caryclark@google.comfb173422012-04-10 18:28:55 +00001071 addPartial(edges, lastPt, nextPt, lastVerb, nextVerb);
caryclark@google.com198e0542012-03-30 18:47:02 +00001072 #if DEBUG_SPLIT
caryclark@google.comfb173422012-04-10 18:28:55 +00001073 SkDebugf("%s addPartial 1\n", __FUNCTION__);
caryclark@google.com198e0542012-03-30 18:47:02 +00001074 #endif
1075 }
1076 lastPt = nextPt;
1077 lastVerb = nextVerb;
1078 }
1079 } else {
1080 if (firstSplit >= 0) {
1081 if (lastSplit < firstSplit) {
caryclark@google.comfb173422012-04-10 18:28:55 +00001082 addSplit(edges, pts, verb, intercepts,
1083 lastSplit, firstSplit, false);
caryclark@google.com198e0542012-03-30 18:47:02 +00001084 #if DEBUG_SPLIT
caryclark@google.comfb173422012-04-10 18:28:55 +00001085 SkDebugf("%s addSplit 1 tIndex=%d,%d\n",
1086 __FUNCTION__, lastSplit, firstSplit);
caryclark@google.com198e0542012-03-30 18:47:02 +00001087 #endif
1088 }
caryclark@google.comfb173422012-04-10 18:28:55 +00001089 addSplit(edges, pts, verb, intercepts,
1090 firstSplit, tIndex, true);
caryclark@google.com198e0542012-03-30 18:47:02 +00001091 #if DEBUG_SPLIT
caryclark@google.comfb173422012-04-10 18:28:55 +00001092 SkDebugf("%s addSplit 2 tIndex=%d,%d flip\n",
1093 __FUNCTION__, firstSplit, tIndex);
caryclark@google.com198e0542012-03-30 18:47:02 +00001094 #endif
1095 lastSplit = tIndex;
1096 firstSplit = -1;
1097 }
1098 }
1099 y = nextY;
1100 }
1101 if (curveSplit) {
1102 if (firstSplit < 0) {
1103 firstSplit = lastSplit;
1104 } else {
caryclark@google.comfb173422012-04-10 18:28:55 +00001105 addSplit(edges, pts, verb, intercepts, lastSplit,
1106 firstSplit, false);
caryclark@google.com198e0542012-03-30 18:47:02 +00001107 #if DEBUG_SPLIT
caryclark@google.comfb173422012-04-10 18:28:55 +00001108 SkDebugf("%s addSplit 3 tIndex=%d,%d\n", __FUNCTION__,
1109 lastSplit, firstSplit);
caryclark@google.com198e0542012-03-30 18:47:02 +00001110 #endif
1111 }
caryclark@google.comfb173422012-04-10 18:28:55 +00001112 addSplit(edges, pts, verb, intercepts, firstSplit,
1113 tIndex, pts[verb].fY < y);
caryclark@google.com198e0542012-03-30 18:47:02 +00001114 #if DEBUG_SPLIT
caryclark@google.comfb173422012-04-10 18:28:55 +00001115 SkDebugf("%s addSplit 4 tIndex=%d,%d %s\n", __FUNCTION__,
1116 firstSplit, tIndex, pts[verb].fY < y ? "flip" : "");
caryclark@google.com198e0542012-03-30 18:47:02 +00001117 #endif
caryclark@google.com6680fb12012-02-07 22:10:51 +00001118 }
1119 }
caryclark@google.com198e0542012-03-30 18:47:02 +00001120 // collapse remainder -- if there's nothing left, clear it somehow?
1121 if (edgeSplit) {
1122 int nextVerb = verbs - 1 - fVerbs.begin();
1123 if (lastVerb < nextVerb) {
1124 int nextPt = pts - fPts.begin();
caryclark@google.comfb173422012-04-10 18:28:55 +00001125 addPartial(edges, lastPt, nextPt, lastVerb, nextVerb);
caryclark@google.com198e0542012-03-30 18:47:02 +00001126 #if DEBUG_SPLIT
caryclark@google.comfb173422012-04-10 18:28:55 +00001127 SkDebugf("%s addPartial 2\n", __FUNCTION__);
caryclark@google.com198e0542012-03-30 18:47:02 +00001128 #endif
1129 }
1130 // OPTIMIZATION: reuse the edge instead of marking it empty
1131 reset();
1132 }
caryclark@google.comc6825902012-02-03 22:07:47 +00001133 }
1134
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001135#if DEBUG_DUMP
1136 void dump() {
1137 int i;
1138 const char className[] = "InEdge";
1139 const int tab = 4;
1140 SkDebugf("InEdge %p (edge=%d)\n", this, fID);
1141 for (i = 0; i < fCached.count(); ++i) {
1142 SkDebugf("%*s.fCached[%d]=0x%08x\n", tab + sizeof(className),
1143 className, i, fCached[i]);
1144 }
1145 uint8_t* verbs = fVerbs.begin();
1146 SkPoint* pts = fPts.begin();
1147 for (i = 0; i < fIntercepts.count(); ++i) {
1148 SkDebugf("%*s.fIntercepts[%d]:\n", tab + sizeof(className),
1149 className, i);
caryclark@google.coma5764232012-03-28 16:20:21 +00001150 fIntercepts[i].dump(pts, (SkPath::Verb) *verbs);
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001151 pts += *verbs++;
1152 }
1153 for (i = 0; i < fPts.count(); ++i) {
caryclark@google.com752b60e2012-03-22 21:11:17 +00001154 SkDebugf("%*s.fPts[%d]=(%1.9g,%1.9g)\n", tab + sizeof(className),
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001155 className, i, fPts[i].fX, fPts[i].fY);
1156 }
1157 for (i = 0; i < fVerbs.count(); ++i) {
1158 SkDebugf("%*s.fVerbs[%d]=%d\n", tab + sizeof(className),
1159 className, i, fVerbs[i]);
1160 }
caryclark@google.comfb173422012-04-10 18:28:55 +00001161 SkDebugf("%*s.fBounds=(%1.9g, %1.9g, %1.9g, %1.9g)\n", tab + sizeof(className),
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001162 className, fBounds.fLeft, fBounds.fTop,
1163 fBounds.fRight, fBounds.fBottom);
1164 SkDebugf("%*s.fWinding=%d\n", tab + sizeof(className), className,
1165 fWinding);
1166 SkDebugf("%*s.fContainsIntercepts=%d\n", tab + sizeof(className),
1167 className, fContainsIntercepts);
caryclark@google.com198e0542012-03-30 18:47:02 +00001168 SkDebugf("%*s.fIntersected=%d\n", tab + sizeof(className),
1169 className, fIntersected);
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001170 }
1171#endif
1172
caryclark@google.com198e0542012-03-30 18:47:02 +00001173 // FIXME: temporary data : move this to a separate struct?
caryclark@google.com6680fb12012-02-07 22:10:51 +00001174 SkTDArray<const InEdge*> fCached; // list of edges already intercepted
caryclark@google.comc6825902012-02-03 22:07:47 +00001175 SkTArray<Intercepts> fIntercepts; // one per verb
caryclark@google.com4917f172012-03-05 22:01:21 +00001176
caryclark@google.comc6825902012-02-03 22:07:47 +00001177 // persistent data
1178 SkTDArray<SkPoint> fPts;
1179 SkTDArray<uint8_t> fVerbs;
1180 Bounds fBounds;
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001181 int fID;
caryclark@google.comc6825902012-02-03 22:07:47 +00001182 signed char fWinding;
caryclark@google.com6680fb12012-02-07 22:10:51 +00001183 bool fContainsIntercepts;
caryclark@google.com198e0542012-03-30 18:47:02 +00001184 bool fIntersected;
caryclark@google.comc6825902012-02-03 22:07:47 +00001185};
1186
caryclark@google.com6680fb12012-02-07 22:10:51 +00001187class InEdgeBuilder {
caryclark@google.comc6825902012-02-03 22:07:47 +00001188public:
1189
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001190InEdgeBuilder(const SkPath& path, bool ignoreHorizontal, SkTArray<InEdge>& edges,
1191 SkTDArray<HorizontalEdge>& horizontalEdges)
caryclark@google.comc6825902012-02-03 22:07:47 +00001192 : fPath(path)
1193 , fCurrentEdge(NULL)
1194 , fEdges(edges)
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001195 , fHorizontalEdges(horizontalEdges)
caryclark@google.comc6825902012-02-03 22:07:47 +00001196 , fIgnoreHorizontal(ignoreHorizontal)
caryclark@google.com198e0542012-03-30 18:47:02 +00001197 , fContainsCurves(false)
caryclark@google.comc6825902012-02-03 22:07:47 +00001198{
1199 walk();
1200}
1201
caryclark@google.com198e0542012-03-30 18:47:02 +00001202bool containsCurves() const {
1203 return fContainsCurves;
1204}
1205
caryclark@google.comc6825902012-02-03 22:07:47 +00001206protected:
1207
1208void addEdge() {
caryclark@google.comf8b000d2012-02-09 22:04:27 +00001209 SkASSERT(fCurrentEdge);
caryclark@google.comc6825902012-02-03 22:07:47 +00001210 fCurrentEdge->fPts.append(fPtCount - fPtOffset, &fPts[fPtOffset]);
1211 fPtOffset = 1;
1212 *fCurrentEdge->fVerbs.append() = fVerb;
1213}
1214
caryclark@google.com6008c6562012-02-15 22:01:16 +00001215bool complete() {
1216 if (fCurrentEdge && fCurrentEdge->fVerbs.count()) {
caryclark@google.comfb173422012-04-10 18:28:55 +00001217 fCurrentEdge->complete(fWinding);
caryclark@google.com6008c6562012-02-15 22:01:16 +00001218 fCurrentEdge = NULL;
1219 return true;
1220 }
1221 return false;
1222}
1223
caryclark@google.com78e17132012-04-17 11:40:34 +00001224int direction(SkPath::Verb verb) {
1225 fPtCount = verb + 1;
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001226 if (fIgnoreHorizontal && isHorizontal()) {
caryclark@google.comc6825902012-02-03 22:07:47 +00001227 return 0;
1228 }
caryclark@google.com78e17132012-04-17 11:40:34 +00001229 return fPts[0].fY == fPts[verb].fY
1230 ? fPts[0].fX == fPts[verb].fX ? 0 : fPts[0].fX < fPts[verb].fX
1231 ? 1 : -1 : fPts[0].fY < fPts[verb].fY ? 1 : -1;
caryclark@google.comc6825902012-02-03 22:07:47 +00001232}
1233
1234bool isHorizontal() {
1235 SkScalar y = fPts[0].fY;
1236 for (int i = 1; i < fPtCount; ++i) {
1237 if (fPts[i].fY != y) {
1238 return false;
1239 }
1240 }
1241 return true;
1242}
1243
1244void startEdge() {
caryclark@google.com6008c6562012-02-15 22:01:16 +00001245 if (!fCurrentEdge) {
1246 fCurrentEdge = fEdges.push_back_n(1);
1247 }
caryclark@google.comc6825902012-02-03 22:07:47 +00001248 fWinding = 0;
1249 fPtOffset = 0;
1250}
1251
1252void walk() {
1253 SkPath::Iter iter(fPath, true);
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001254 int winding = 0;
caryclark@google.comc6825902012-02-03 22:07:47 +00001255 while ((fVerb = iter.next(fPts)) != SkPath::kDone_Verb) {
1256 switch (fVerb) {
1257 case SkPath::kMove_Verb:
caryclark@google.comc6825902012-02-03 22:07:47 +00001258 startEdge();
1259 continue;
1260 case SkPath::kLine_Verb:
caryclark@google.com78e17132012-04-17 11:40:34 +00001261 winding = direction(SkPath::kLine_Verb);
caryclark@google.comc6825902012-02-03 22:07:47 +00001262 break;
1263 case SkPath::kQuad_Verb:
caryclark@google.com78e17132012-04-17 11:40:34 +00001264 fVerb = QuadReduceOrder(fPts);
1265 winding = direction(fVerb);
1266 fContainsCurves |= fVerb == SkPath::kQuad_Verb;
caryclark@google.comc6825902012-02-03 22:07:47 +00001267 break;
1268 case SkPath::kCubic_Verb:
caryclark@google.com78e17132012-04-17 11:40:34 +00001269 fVerb = CubicReduceOrder(fPts);
1270 winding = direction(fVerb);
1271 fContainsCurves |= fVerb >= SkPath::kQuad_Verb;
caryclark@google.comc6825902012-02-03 22:07:47 +00001272 break;
1273 case SkPath::kClose_Verb:
caryclark@google.comf8b000d2012-02-09 22:04:27 +00001274 SkASSERT(fCurrentEdge);
caryclark@google.com6008c6562012-02-15 22:01:16 +00001275 complete();
caryclark@google.comc6825902012-02-03 22:07:47 +00001276 continue;
1277 default:
1278 SkDEBUGFAIL("bad verb");
1279 return;
1280 }
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001281 if (winding == 0) {
1282 HorizontalEdge* horizontalEdge = fHorizontalEdges.append();
1283 // FIXME: for degenerate quads and cubics, compute x extremes
1284 horizontalEdge->fLeft = fPts[0].fX;
1285 horizontalEdge->fRight = fPts[fVerb].fX;
1286 horizontalEdge->fY = fPts[0].fY;
1287 if (horizontalEdge->fLeft > horizontalEdge->fRight) {
1288 SkTSwap<SkScalar>(horizontalEdge->fLeft, horizontalEdge->fRight);
1289 }
caryclark@google.com6008c6562012-02-15 22:01:16 +00001290 if (complete()) {
1291 startEdge();
1292 }
caryclark@google.comc6825902012-02-03 22:07:47 +00001293 continue;
1294 }
1295 if (fWinding + winding == 0) {
1296 // FIXME: if prior verb or this verb is a horizontal line, reverse
1297 // it instead of starting a new edge
caryclark@google.comf8b000d2012-02-09 22:04:27 +00001298 SkASSERT(fCurrentEdge);
caryclark@google.comcef7e3f2012-02-28 16:57:05 +00001299 if (complete()) {
1300 startEdge();
1301 }
caryclark@google.comc6825902012-02-03 22:07:47 +00001302 }
1303 fWinding = winding;
1304 addEdge();
1305 }
caryclark@google.com6b9cfb32012-02-16 21:24:41 +00001306 if (!complete()) {
1307 if (fCurrentEdge) {
1308 fEdges.pop_back();
1309 }
1310 }
caryclark@google.comc6825902012-02-03 22:07:47 +00001311}
1312
1313private:
1314 const SkPath& fPath;
caryclark@google.com6680fb12012-02-07 22:10:51 +00001315 InEdge* fCurrentEdge;
1316 SkTArray<InEdge>& fEdges;
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001317 SkTDArray<HorizontalEdge>& fHorizontalEdges;
caryclark@google.comc6825902012-02-03 22:07:47 +00001318 SkPoint fPts[4];
1319 SkPath::Verb fVerb;
1320 int fPtCount;
1321 int fPtOffset;
1322 int8_t fWinding;
caryclark@google.comc6825902012-02-03 22:07:47 +00001323 bool fIgnoreHorizontal;
caryclark@google.com198e0542012-03-30 18:47:02 +00001324 bool fContainsCurves;
caryclark@google.comc6825902012-02-03 22:07:47 +00001325};
1326
caryclark@google.com6680fb12012-02-07 22:10:51 +00001327struct WorkEdge {
1328 SkScalar bottom() const {
1329 return fPts[verb()].fY;
caryclark@google.comc6825902012-02-03 22:07:47 +00001330 }
1331
caryclark@google.com6680fb12012-02-07 22:10:51 +00001332 void init(const InEdge* edge) {
1333 fEdge = edge;
1334 fPts = edge->fPts.begin();
1335 fVerb = edge->fVerbs.begin();
caryclark@google.comc6825902012-02-03 22:07:47 +00001336 }
1337
caryclark@google.comcef7e3f2012-02-28 16:57:05 +00001338 bool advance() {
caryclark@google.com6680fb12012-02-07 22:10:51 +00001339 SkASSERT(fVerb < fEdge->fVerbs.end());
1340 fPts += *fVerb++;
1341 return fVerb != fEdge->fVerbs.end();
caryclark@google.comc6825902012-02-03 22:07:47 +00001342 }
caryclark@google.com78e17132012-04-17 11:40:34 +00001343
1344 const SkPoint* lastPoints() const {
1345 SkASSERT(fPts >= fEdge->fPts.begin() + lastVerb());
1346 return &fPts[-lastVerb()];
1347 }
caryclark@google.com752b60e2012-03-22 21:11:17 +00001348
caryclark@google.comcd4421d2012-03-01 19:16:31 +00001349 SkPath::Verb lastVerb() const {
1350 SkASSERT(fVerb > fEdge->fVerbs.begin());
1351 return (SkPath::Verb) fVerb[-1];
1352 }
1353
caryclark@google.com78e17132012-04-17 11:40:34 +00001354 const SkPoint* points() const {
1355 return fPts;
1356 }
caryclark@google.comc6825902012-02-03 22:07:47 +00001357
1358 SkPath::Verb verb() const {
1359 return (SkPath::Verb) *fVerb;
1360 }
1361
caryclark@google.com6008c6562012-02-15 22:01:16 +00001362 ptrdiff_t verbIndex() const {
caryclark@google.com6680fb12012-02-07 22:10:51 +00001363 return fVerb - fEdge->fVerbs.begin();
1364 }
caryclark@google.com752b60e2012-03-22 21:11:17 +00001365
caryclark@google.com6680fb12012-02-07 22:10:51 +00001366 int winding() const {
1367 return fEdge->fWinding;
caryclark@google.comc6825902012-02-03 22:07:47 +00001368 }
1369
caryclark@google.com6680fb12012-02-07 22:10:51 +00001370 const InEdge* fEdge;
caryclark@google.comc6825902012-02-03 22:07:47 +00001371 const SkPoint* fPts;
1372 const uint8_t* fVerb;
caryclark@google.comc6825902012-02-03 22:07:47 +00001373};
1374
caryclark@google.com6680fb12012-02-07 22:10:51 +00001375// always constructed with SkTDArray because new edges are inserted
1376// this may be a inappropriate optimization, suggesting that a separate array of
1377// ActiveEdge* may be faster to insert and search
caryclark@google.comcd4421d2012-03-01 19:16:31 +00001378
1379// OPTIMIZATION: Brian suggests that global sorting should be unnecessary, since
1380// as active edges are introduced, only local sorting should be required
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001381class ActiveEdge {
1382public:
caryclark@google.com78e17132012-04-17 11:40:34 +00001383 // this logic must be kept in sync with tooCloseToCall
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001384 // callers expect this to only read fAbove, fTangent
caryclark@google.com6008c6562012-02-15 22:01:16 +00001385 bool operator<(const ActiveEdge& rh) const {
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001386 if (fVerb == rh.fVerb) {
1387 // FIXME: don't know what to do if verb is quad, cubic
1388 return abCompare(fAbove, fBelow, rh.fAbove, rh.fBelow);
caryclark@google.comd88e0892012-03-27 13:23:51 +00001389 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001390 // figure out which is quad, line
1391 // if cached data says line did not intersect quad, use top/bottom
1392 if (fVerb != SkPath::kLine_Verb ? noIntersect(rh) : rh.noIntersect(*this)) {
1393 return abCompare(fAbove, fBelow, rh.fAbove, rh.fBelow);
1394 }
1395 // use whichever of top/tangent tangent/bottom overlaps more
1396 // with line top/bot
1397 // assumes quad/cubic can already be upconverted to cubic/cubic
1398 const SkPoint* line[2];
1399 const SkPoint* curve[4];
1400 if (fVerb != SkPath::kLine_Verb) {
1401 line[0] = &rh.fAbove;
1402 line[1] = &rh.fBelow;
1403 curve[0] = &fAbove;
1404 curve[1] = &fTangent;
1405 curve[2] = &fBelow;
1406 } else {
1407 line[0] = &fAbove;
1408 line[1] = &fBelow;
1409 curve[0] = &rh.fAbove;
1410 curve[1] = &rh.fTangent;
1411 curve[2] = &rh.fBelow;
1412 }
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001413 // FIXME: code has been abandoned, incomplete....
1414 return false;
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001415 }
1416
1417 bool abCompare(const SkPoint& a1, const SkPoint& a2, const SkPoint& b1,
1418 const SkPoint& b2) const {
1419 double topD = a1.fX - b1.fX;
1420 if (b1.fY < a1.fY) {
1421 topD = (b2.fY - b1.fY) * topD - (a1.fY - b1.fY) * (b2.fX - b1.fX);
1422 } else if (b1.fY > a1.fY) {
1423 topD = (a2.fY - a1.fY) * topD + (b1.fY - a1.fY) * (a2.fX - a1.fX);
1424 }
1425 double botD = a2.fX - b2.fX;
1426 if (b2.fY > a2.fY) {
1427 botD = (b2.fY - b1.fY) * botD - (a2.fY - b2.fY) * (b2.fX - b1.fX);
1428 } else if (b2.fY < a2.fY) {
1429 botD = (a2.fY - a1.fY) * botD + (b2.fY - a2.fY) * (a2.fX - a1.fX);
caryclark@google.comd88e0892012-03-27 13:23:51 +00001430 }
1431 // return sign of greater absolute value
1432 return (fabs(topD) > fabs(botD) ? topD : botD) < 0;
1433 }
1434
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001435 // If a pair of edges are nearly coincident for some span, add a T in the
1436 // edge so it can be shortened to match the other edge. Note that another
1437 // approach is to trim the edge after it is added to the OutBuilder list --
1438 // FIXME: since this has no effect if the edge is already done (i.e.,
1439 // fYBottom >= y) maybe this can only be done by calling trimLine later.
1440 void addTatYBelow(SkScalar y) {
1441 if (fBelow.fY <= y || fYBottom >= y) {
1442 return;
1443 }
1444 addTatYInner(y);
1445 fFixBelow = true;
1446 }
caryclark@google.com752b60e2012-03-22 21:11:17 +00001447
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001448 void addTatYAbove(SkScalar y) {
1449 if (fBelow.fY <= y) {
1450 return;
1451 }
1452 addTatYInner(y);
1453 }
1454
1455 void addTatYInner(SkScalar y) {
caryclark@google.com752b60e2012-03-22 21:11:17 +00001456 if (fWorkEdge.fPts[0].fY > y) {
1457 backup(y);
1458 }
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001459 SkScalar left = fWorkEdge.fPts[0].fX;
1460 SkScalar right = fWorkEdge.fPts[1].fX;
1461 if (left > right) {
1462 SkTSwap(left, right);
1463 }
caryclark@google.com752b60e2012-03-22 21:11:17 +00001464 double ts[2];
caryclark@google.com78e17132012-04-17 11:40:34 +00001465 SkASSERT(fWorkEdge.fVerb[0] == SkPath::kLine_Verb);
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001466 int pts = LineIntersect(fWorkEdge.fPts, left, right, y, ts);
1467 SkASSERT(pts == 1);
1468 // An ActiveEdge or WorkEdge has no need to modify the T values computed
1469 // in the InEdge, except in the following case. If a pair of edges are
1470 // nearly coincident, this may not be detected when the edges are
1471 // intersected. Later, when sorted, and this near-coincidence is found,
1472 // an additional t value must be added, requiring the cast below.
1473 InEdge* writable = const_cast<InEdge*>(fWorkEdge.fEdge);
1474 int insertedAt = writable->add(ts, pts, fWorkEdge.verbIndex());
caryclark@google.com752b60e2012-03-22 21:11:17 +00001475 #if DEBUG_ADJUST_COINCIDENT
1476 SkDebugf("%s edge=%d y=%1.9g t=%1.9g\n", __FUNCTION__, ID(), y, ts[0]);
1477 #endif
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001478 if (insertedAt >= 0) {
1479 if (insertedAt + 1 < fTIndex) {
1480 SkASSERT(insertedAt + 2 == fTIndex);
1481 --fTIndex;
1482 }
1483 }
1484 }
caryclark@google.com6008c6562012-02-15 22:01:16 +00001485
caryclark@google.comcd4421d2012-03-01 19:16:31 +00001486 bool advanceT() {
caryclark@google.com198e0542012-03-30 18:47:02 +00001487 SkASSERT(fTIndex <= fTs->count() - fExplicitTs);
1488 return ++fTIndex <= fTs->count() - fExplicitTs;
caryclark@google.comcd4421d2012-03-01 19:16:31 +00001489 }
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001490
caryclark@google.comcd4421d2012-03-01 19:16:31 +00001491 bool advance() {
1492 // FIXME: flip sense of next
1493 bool result = fWorkEdge.advance();
1494 fDone = !result;
1495 initT();
1496 return result;
1497 }
caryclark@google.com752b60e2012-03-22 21:11:17 +00001498
1499 void backup(SkScalar y) {
1500 do {
1501 SkASSERT(fWorkEdge.fEdge->fVerbs.begin() < fWorkEdge.fVerb);
1502 fWorkEdge.fPts -= *--fWorkEdge.fVerb;
1503 SkASSERT(fWorkEdge.fEdge->fPts.begin() <= fWorkEdge.fPts);
1504 } while (fWorkEdge.fPts[0].fY >= y);
1505 initT();
caryclark@google.com198e0542012-03-30 18:47:02 +00001506 SkASSERT(!fExplicitTs);
caryclark@google.com752b60e2012-03-22 21:11:17 +00001507 fTIndex = fTs->count() + 1;
1508 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001509
1510 void calcAboveBelow(double tAbove, double tBelow) {
1511 fVerb = fWorkEdge.verb();
1512 switch (fVerb) {
1513 case SkPath::kLine_Verb:
1514 LineXYAtT(fWorkEdge.fPts, tAbove, &fAbove);
1515 LineXYAtT(fWorkEdge.fPts, tBelow, &fTangent);
1516 fBelow = fTangent;
1517 break;
1518 case SkPath::kQuad_Verb:
1519 // FIXME: put array in struct to avoid copy?
1520 SkPoint quad[3];
1521 QuadSubDivide(fWorkEdge.fPts, tAbove, tBelow, quad);
1522 fAbove = quad[0];
1523 fTangent = quad[0] != quad[1] ? quad[1] : quad[2];
1524 fBelow = quad[2];
1525 break;
1526 case SkPath::kCubic_Verb:
1527 SkPoint cubic[3];
1528 CubicSubDivide(fWorkEdge.fPts, tAbove, tBelow, cubic);
1529 fAbove = cubic[0];
1530 // FIXME: can't see how quad logic for how tangent is used
1531 // extends to cubic
1532 fTangent = cubic[0] != cubic[1] ? cubic[1]
1533 : cubic[0] != cubic[2] ? cubic[2] : cubic[3];
1534 fBelow = cubic[3];
1535 break;
1536 default:
1537 SkASSERT(0);
1538 }
1539 }
caryclark@google.com752b60e2012-03-22 21:11:17 +00001540
caryclark@google.comcd4421d2012-03-01 19:16:31 +00001541 void calcLeft(SkScalar y) {
caryclark@google.comcef7e3f2012-02-28 16:57:05 +00001542 // OPTIMIZE: put a kDone_Verb at the end of the verb list?
caryclark@google.com4917f172012-03-05 22:01:21 +00001543 if (fDone || fBelow.fY > y) {
caryclark@google.comcef7e3f2012-02-28 16:57:05 +00001544 return; // nothing to do; use last
caryclark@google.com4917f172012-03-05 22:01:21 +00001545 }
caryclark@google.comcd4421d2012-03-01 19:16:31 +00001546 calcLeft();
caryclark@google.com752b60e2012-03-22 21:11:17 +00001547 if (fAbove.fY == fBelow.fY) {
1548 SkDebugf("%s edge=%d fAbove.fY != fBelow.fY %1.9g\n", __FUNCTION__,
1549 ID(), fAbove.fY);
1550 }
caryclark@google.comcd4421d2012-03-01 19:16:31 +00001551 }
caryclark@google.com752b60e2012-03-22 21:11:17 +00001552
caryclark@google.comcd4421d2012-03-01 19:16:31 +00001553 void calcLeft() {
caryclark@google.com198e0542012-03-30 18:47:02 +00001554 int add = (fTIndex <= fTs->count() - fExplicitTs) - 1;
caryclark@google.coma5764232012-03-28 16:20:21 +00001555 double tAbove = t(fTIndex + add);
caryclark@google.coma5764232012-03-28 16:20:21 +00001556 double tBelow = t(fTIndex - ~add);
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001557 // OPTIMIZATION: if fAbove, fBelow have already been computed
1558 // for the fTIndex, don't do it again
1559 calcAboveBelow(tAbove, tBelow);
1560 // For identical x, this lets us know which edge is first.
1561 // If both edges have T values < 1, check x at next T (fBelow).
caryclark@google.coma5764232012-03-28 16:20:21 +00001562 SkASSERT(tAbove != tBelow);
1563 // FIXME: this can loop forever
1564 // need a break if we hit the end
caryclark@google.com198e0542012-03-30 18:47:02 +00001565 // FIXME: in unit test, figure out how explicit Ts work as well
caryclark@google.coma5764232012-03-28 16:20:21 +00001566 while (fAbove.fY == fBelow.fY) {
1567 if (add < 0 || fTIndex == fTs->count()) {
1568 add -= 1;
1569 SkASSERT(fTIndex + add >= 0);
1570 tAbove = t(fTIndex + add);
caryclark@google.coma5764232012-03-28 16:20:21 +00001571 } else {
1572 add += 1;
1573 SkASSERT(fTIndex - ~add <= fTs->count() + 1);
1574 tBelow = t(fTIndex - ~add);
caryclark@google.coma5764232012-03-28 16:20:21 +00001575 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001576 calcAboveBelow(tAbove, tBelow);
caryclark@google.coma5764232012-03-28 16:20:21 +00001577 }
caryclark@google.coma5764232012-03-28 16:20:21 +00001578 fTAbove = tAbove;
1579 fTBelow = tBelow;
caryclark@google.com6008c6562012-02-15 22:01:16 +00001580 }
1581
caryclark@google.com752b60e2012-03-22 21:11:17 +00001582 bool done(SkScalar bottom) const {
1583 return fDone || fYBottom >= bottom;
caryclark@google.comcd4421d2012-03-01 19:16:31 +00001584 }
caryclark@google.com78e17132012-04-17 11:40:34 +00001585
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001586 void fixBelow() {
1587 if (fFixBelow) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001588 fTBelow = nextT();
1589 calcAboveBelow(fTAbove, fTBelow);
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001590 fFixBelow = false;
1591 }
1592 }
1593
caryclark@google.com6680fb12012-02-07 22:10:51 +00001594 void init(const InEdge* edge) {
1595 fWorkEdge.init(edge);
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001596 fDone = false;
caryclark@google.com6680fb12012-02-07 22:10:51 +00001597 initT();
caryclark@google.com4917f172012-03-05 22:01:21 +00001598 fBelow.fY = SK_ScalarMin;
caryclark@google.comcef7e3f2012-02-28 16:57:05 +00001599 fYBottom = SK_ScalarMin;
caryclark@google.com6680fb12012-02-07 22:10:51 +00001600 }
caryclark@google.com752b60e2012-03-22 21:11:17 +00001601
caryclark@google.com6680fb12012-02-07 22:10:51 +00001602 void initT() {
caryclark@google.com6008c6562012-02-15 22:01:16 +00001603 const Intercepts& intercepts = fWorkEdge.fEdge->fIntercepts.front();
1604 SkASSERT(fWorkEdge.verbIndex() <= fWorkEdge.fEdge->fIntercepts.count());
1605 const Intercepts* interceptPtr = &intercepts + fWorkEdge.verbIndex();
caryclark@google.com198e0542012-03-30 18:47:02 +00001606 fTs = &interceptPtr->fTs;
1607 fExplicitTs = interceptPtr->fExplicit;
caryclark@google.com6008c6562012-02-15 22:01:16 +00001608 // the above is conceptually the same as
1609 // fTs = &fWorkEdge.fEdge->fIntercepts[fWorkEdge.verbIndex()].fTs;
1610 // but templated arrays don't allow returning a pointer to the end() element
caryclark@google.com6680fb12012-02-07 22:10:51 +00001611 fTIndex = 0;
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001612 if (!fDone) {
1613 fVerb = fWorkEdge.verb();
1614 }
1615 SkASSERT(fVerb > SkPath::kMove_Verb);
caryclark@google.comc6825902012-02-03 22:07:47 +00001616 }
caryclark@google.com752b60e2012-03-22 21:11:17 +00001617
caryclark@google.comcd4421d2012-03-01 19:16:31 +00001618 // OPTIMIZATION: record if two edges are coincident when the are intersected
1619 // It's unclear how to do this -- seems more complicated than recording the
1620 // t values, since the same t values could exist intersecting non-coincident
1621 // edges.
caryclark@google.com78e17132012-04-17 11:40:34 +00001622 bool isCoincidentWith(const ActiveEdge* edge) const {
caryclark@google.comd88e0892012-03-27 13:23:51 +00001623 if (fAbove != edge->fAbove || fBelow != edge->fBelow) {
1624 return false;
1625 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001626 if (fVerb != edge->fVerb) {
caryclark@google.comcd4421d2012-03-01 19:16:31 +00001627 return false;
1628 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001629 switch (fVerb) {
caryclark@google.com78e17132012-04-17 11:40:34 +00001630 case SkPath::kLine_Verb:
caryclark@google.com4917f172012-03-05 22:01:21 +00001631 return true;
caryclark@google.comcef7e3f2012-02-28 16:57:05 +00001632 default:
caryclark@google.com78e17132012-04-17 11:40:34 +00001633 // FIXME: add support for quads, cubics
caryclark@google.comcef7e3f2012-02-28 16:57:05 +00001634 SkASSERT(0);
caryclark@google.com78e17132012-04-17 11:40:34 +00001635 return false;
caryclark@google.comcef7e3f2012-02-28 16:57:05 +00001636 }
1637 return false;
1638 }
caryclark@google.com752b60e2012-03-22 21:11:17 +00001639
caryclark@google.com78e17132012-04-17 11:40:34 +00001640 bool isUnordered(const ActiveEdge* edge) const {
1641 return fAbove == edge->fAbove && fBelow == edge->fBelow;
1642 }
1643
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001644// SkPath::Verb lastVerb() const {
1645// return fDone ? fWorkEdge.lastVerb() : fWorkEdge.verb();
1646// }
caryclark@google.com78e17132012-04-17 11:40:34 +00001647
1648 const SkPoint* lastPoints() const {
1649 return fDone ? fWorkEdge.lastPoints() : fWorkEdge.points();
1650 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001651
1652 bool noIntersect(const ActiveEdge& ) const {
1653 // incomplete
1654 return false;
1655 }
caryclark@google.com78e17132012-04-17 11:40:34 +00001656
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001657 // The shortest close call edge should be moved into a position where
1658 // it contributes if the winding is transitioning to or from zero.
1659 bool swapClose(const ActiveEdge* next, int prev, int wind, int mask) const {
caryclark@google.comd88e0892012-03-27 13:23:51 +00001660#if DEBUG_ADJUST_COINCIDENT
1661 SkDebugf("%s edge=%d (%g) next=%d (%g) prev=%d wind=%d nextWind=%d\n",
1662 __FUNCTION__, ID(), fBelow.fY, next->ID(), next->fBelow.fY,
1663 prev, wind, wind + next->fWorkEdge.winding());
1664#endif
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001665 if ((prev & mask) == 0 || (wind & mask) == 0) {
1666 return next->fBelow.fY < fBelow.fY;
1667 }
1668 int nextWinding = wind + next->fWorkEdge.winding();
1669 if ((nextWinding & mask) == 0) {
1670 return fBelow.fY < next->fBelow.fY;
1671 }
1672 return false;
1673 }
caryclark@google.com752b60e2012-03-22 21:11:17 +00001674
caryclark@google.comcd4421d2012-03-01 19:16:31 +00001675 bool swapCoincident(const ActiveEdge* edge, SkScalar bottom) const {
1676 if (fBelow.fY >= bottom || fDone || edge->fDone) {
1677 return false;
1678 }
1679 ActiveEdge thisWork = *this;
1680 ActiveEdge edgeWork = *edge;
1681 while ((thisWork.advanceT() || thisWork.advance())
1682 && (edgeWork.advanceT() || edgeWork.advance())) {
1683 thisWork.calcLeft();
1684 edgeWork.calcLeft();
1685 if (thisWork < edgeWork) {
1686 return false;
1687 }
1688 if (edgeWork < thisWork) {
1689 return true;
1690 }
1691 }
1692 return false;
1693 }
caryclark@google.com78e17132012-04-17 11:40:34 +00001694
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001695 bool swapUnordered(const ActiveEdge* edge, SkScalar /* bottom */) const {
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001696 SkASSERT(fVerb != SkPath::kLine_Verb
1697 || edge->fVerb != SkPath::kLine_Verb);
caryclark@google.com78e17132012-04-17 11:40:34 +00001698 if (fDone || edge->fDone) {
1699 return false;
1700 }
1701 ActiveEdge thisWork, edgeWork;
1702 extractAboveBelow(thisWork);
1703 edge->extractAboveBelow(edgeWork);
1704 return edgeWork < thisWork;
1705 }
caryclark@google.com752b60e2012-03-22 21:11:17 +00001706
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001707 bool tooCloseToCall(const ActiveEdge* edge) const {
1708 int ulps;
caryclark@google.comd88e0892012-03-27 13:23:51 +00001709 double t1, t2, b1, b2;
caryclark@google.com78e17132012-04-17 11:40:34 +00001710 // This logic must be kept in sync with operator <
caryclark@google.comd88e0892012-03-27 13:23:51 +00001711 if (edge->fAbove.fY < fAbove.fY) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001712 t1 = (edge->fTangent.fY - edge->fAbove.fY) * (fAbove.fX - edge->fAbove.fX);
1713 t2 = (fAbove.fY - edge->fAbove.fY) * (edge->fTangent.fX - edge->fAbove.fX);
caryclark@google.comd88e0892012-03-27 13:23:51 +00001714 } else if (edge->fAbove.fY > fAbove.fY) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001715 t1 = (fTangent.fY - fAbove.fY) * (fAbove.fX - edge->fAbove.fX);
1716 t2 = (fAbove.fY - edge->fAbove.fY) * (fTangent.fX - fAbove.fX);
caryclark@google.comd88e0892012-03-27 13:23:51 +00001717 } else {
1718 t1 = fAbove.fX;
1719 t2 = edge->fAbove.fX;
1720 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001721 if (edge->fTangent.fY > fTangent.fY) {
1722 b1 = (edge->fTangent.fY - edge->fAbove.fY) * (fTangent.fX - edge->fTangent.fX);
1723 b2 = (fTangent.fY - edge->fTangent.fY) * (edge->fTangent.fX - edge->fAbove.fX);
1724 } else if (edge->fTangent.fY < fTangent.fY) {
1725 b1 = (fTangent.fY - fAbove.fY) * (fTangent.fX - edge->fTangent.fX);
1726 b2 = (fTangent.fY - edge->fTangent.fY) * (fTangent.fX - fAbove.fX);
caryclark@google.comd88e0892012-03-27 13:23:51 +00001727 } else {
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001728 b1 = fTangent.fX;
1729 b2 = edge->fTangent.fX;
caryclark@google.comd88e0892012-03-27 13:23:51 +00001730 }
1731 if (fabs(t1 - t2) > fabs(b1 - b2)) {
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001732 ulps = UlpsDiff((float) t1, (float) t2);
caryclark@google.comd88e0892012-03-27 13:23:51 +00001733 } else {
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001734 ulps = UlpsDiff((float) b1, (float) b2);
caryclark@google.comd88e0892012-03-27 13:23:51 +00001735 }
1736#if DEBUG_ADJUST_COINCIDENT
1737 SkDebugf("%s this=%d edge=%d ulps=%d\n", __FUNCTION__, ID(), edge->ID(),
1738 ulps);
1739#endif
caryclark@google.com78e17132012-04-17 11:40:34 +00001740 if (ulps < 0 || ulps > 32) {
1741 return false;
1742 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001743 if (fVerb == SkPath::kLine_Verb && edge->fVerb == SkPath::kLine_Verb) {
caryclark@google.com78e17132012-04-17 11:40:34 +00001744 return true;
1745 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001746 if (fVerb != SkPath::kLine_Verb && edge->fVerb != SkPath::kLine_Verb) {
caryclark@google.com78e17132012-04-17 11:40:34 +00001747 return false;
1748 }
1749
1750 double ts[2];
1751 bool isLine = true;
1752 bool curveQuad = true;
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001753 if (fVerb == SkPath::kCubic_Verb) {
caryclark@google.com78e17132012-04-17 11:40:34 +00001754 ts[0] = (fTAbove * 2 + fTBelow) / 3;
1755 ts[1] = (fTAbove + fTBelow * 2) / 3;
1756 curveQuad = isLine = false;
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001757 } else if (edge->fVerb == SkPath::kCubic_Verb) {
caryclark@google.com78e17132012-04-17 11:40:34 +00001758 ts[0] = (edge->fTAbove * 2 + edge->fTBelow) / 3;
1759 ts[1] = (edge->fTAbove + edge->fTBelow * 2) / 3;
1760 curveQuad = false;
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001761 } else if (fVerb == SkPath::kQuad_Verb) {
caryclark@google.com78e17132012-04-17 11:40:34 +00001762 ts[0] = fTAbove;
1763 ts[1] = (fTAbove + fTBelow) / 2;
1764 isLine = false;
1765 } else {
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001766 SkASSERT(edge->fVerb == SkPath::kQuad_Verb);
caryclark@google.com78e17132012-04-17 11:40:34 +00001767 ts[0] = edge->fTAbove;
1768 ts[1] = (edge->fTAbove + edge->fTBelow) / 2;
1769 }
1770 const SkPoint* curvePts = isLine ? edge->lastPoints() : lastPoints();
1771 const ActiveEdge* lineEdge = isLine ? this : edge;
1772 SkPoint curveSample[2];
1773 for (int index = 0; index < 2; ++index) {
1774 if (curveQuad) {
1775 QuadXYAtT(curvePts, ts[index], &curveSample[index]);
1776 } else {
1777 CubicXYAtT(curvePts, ts[index], &curveSample[index]);
1778 }
1779 }
1780 return IsCoincident(curveSample, lineEdge->fAbove, lineEdge->fBelow);
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001781 }
caryclark@google.com198e0542012-03-30 18:47:02 +00001782
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001783 double nextT() const {
caryclark@google.com198e0542012-03-30 18:47:02 +00001784 SkASSERT(fTIndex <= fTs->count() - fExplicitTs);
caryclark@google.comcef7e3f2012-02-28 16:57:05 +00001785 return t(fTIndex + 1);
1786 }
caryclark@google.com6680fb12012-02-07 22:10:51 +00001787
caryclark@google.comcef7e3f2012-02-28 16:57:05 +00001788 double t() const {
caryclark@google.com198e0542012-03-30 18:47:02 +00001789 return t(fTIndex);
caryclark@google.com6680fb12012-02-07 22:10:51 +00001790 }
1791
caryclark@google.comcef7e3f2012-02-28 16:57:05 +00001792 double t(int tIndex) const {
caryclark@google.com198e0542012-03-30 18:47:02 +00001793 if (fExplicitTs) {
1794 SkASSERT(tIndex < fTs->count());
1795 return (*fTs)[tIndex];
1796 }
caryclark@google.comcef7e3f2012-02-28 16:57:05 +00001797 if (tIndex == 0) {
1798 return 0;
1799 }
1800 if (tIndex > fTs->count()) {
1801 return 1;
1802 }
1803 return (*fTs)[tIndex - 1];
1804 }
1805
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001806 // FIXME: debugging only
caryclark@google.com752b60e2012-03-22 21:11:17 +00001807 int ID() const {
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001808 return fWorkEdge.fEdge->fID;
1809 }
1810
caryclark@google.com78e17132012-04-17 11:40:34 +00001811private:
1812 // utility used only by swapUnordered
1813 void extractAboveBelow(ActiveEdge& extracted) const {
1814 SkPoint curve[4];
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001815 switch (fVerb) {
caryclark@google.com78e17132012-04-17 11:40:34 +00001816 case SkPath::kLine_Verb:
1817 extracted.fAbove = fAbove;
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001818 extracted.fTangent = fTangent;
caryclark@google.com78e17132012-04-17 11:40:34 +00001819 return;
1820 case SkPath::kQuad_Verb:
1821 QuadSubDivide(lastPoints(), fTAbove, fTBelow, curve);
1822 break;
1823 case SkPath::kCubic_Verb:
1824 CubicSubDivide(lastPoints(), fTAbove, fTBelow, curve);
1825 break;
1826 default:
1827 SkASSERT(0);
1828 }
1829 extracted.fAbove = curve[0];
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001830 extracted.fTangent = curve[1];
caryclark@google.com78e17132012-04-17 11:40:34 +00001831 }
1832
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001833public:
caryclark@google.com6680fb12012-02-07 22:10:51 +00001834 WorkEdge fWorkEdge;
1835 const SkTDArray<double>* fTs;
caryclark@google.comcd4421d2012-03-01 19:16:31 +00001836 SkPoint fAbove;
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001837 SkPoint fTangent;
caryclark@google.comcd4421d2012-03-01 19:16:31 +00001838 SkPoint fBelow;
caryclark@google.com78e17132012-04-17 11:40:34 +00001839 double fTAbove; // OPTIMIZATION: only required if edge has quads or cubics
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001840 double fTBelow;
caryclark@google.comcef7e3f2012-02-28 16:57:05 +00001841 SkScalar fYBottom;
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001842 int fCoincident;
caryclark@google.com6680fb12012-02-07 22:10:51 +00001843 int fTIndex;
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001844 SkPath::Verb fVerb;
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001845 bool fSkip; // OPTIMIZATION: use bitfields?
1846 bool fCloseCall;
caryclark@google.comcef7e3f2012-02-28 16:57:05 +00001847 bool fDone;
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001848 bool fFixBelow;
caryclark@google.com198e0542012-03-30 18:47:02 +00001849 bool fExplicitTs;
caryclark@google.comc6825902012-02-03 22:07:47 +00001850};
1851
caryclark@google.com6680fb12012-02-07 22:10:51 +00001852static void addToActive(SkTDArray<ActiveEdge>& activeEdges, const InEdge* edge) {
caryclark@google.com6680fb12012-02-07 22:10:51 +00001853 size_t count = activeEdges.count();
1854 for (size_t index = 0; index < count; ++index) {
caryclark@google.com6680fb12012-02-07 22:10:51 +00001855 if (edge == activeEdges[index].fWorkEdge.fEdge) {
1856 return;
1857 }
1858 }
1859 ActiveEdge* active = activeEdges.append();
1860 active->init(edge);
1861}
1862
caryclark@google.com4917f172012-03-05 22:01:21 +00001863// Find any intersections in the range of active edges. A pair of edges, on
1864// either side of another edge, may change the winding contribution for part of
1865// the edge.
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001866// Keep horizontal edges just for
caryclark@google.com4917f172012-03-05 22:01:21 +00001867// the purpose of computing when edges change their winding contribution, since
1868// this is essentially computing the horizontal intersection.
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001869static void addBottomT(InEdge** currentPtr, InEdge** lastPtr,
1870 HorizontalEdge** horizontal) {
1871 InEdge** testPtr = currentPtr - 1;
1872 HorizontalEdge* horzEdge = *horizontal;
1873 SkScalar left = horzEdge->fLeft;
1874 SkScalar bottom = horzEdge->fY;
1875 while (++testPtr != lastPtr) {
1876 InEdge* test = *testPtr;
1877 if (test->fBounds.fBottom <= bottom || test->fBounds.fRight <= left) {
1878 continue;
1879 }
1880 WorkEdge wt;
1881 wt.init(test);
1882 do {
1883 HorizontalEdge** sorted = horizontal;
1884 horzEdge = *sorted;
caryclark@google.comf8b000d2012-02-09 22:04:27 +00001885 do {
caryclark@google.com198e0542012-03-30 18:47:02 +00001886 double wtTs[4];
1887 int pts;
1888 uint8_t verb = wt.verb();
1889 switch (verb) {
1890 case SkPath::kLine_Verb:
1891 pts = LineIntersect(wt.fPts, horzEdge->fLeft,
1892 horzEdge->fRight, horzEdge->fY, wtTs);
1893 break;
1894 case SkPath::kQuad_Verb:
1895 pts = QuadIntersect(wt.fPts, horzEdge->fLeft,
1896 horzEdge->fRight, horzEdge->fY, wtTs);
1897 break;
1898 case SkPath::kCubic_Verb:
1899 pts = CubicIntersect(wt.fPts, horzEdge->fLeft,
1900 horzEdge->fRight, horzEdge->fY, wtTs);
1901 break;
1902 }
1903 if (pts) {
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001904#if DEBUG_ADD_BOTTOM_TS
caryclark@google.com198e0542012-03-30 18:47:02 +00001905 for (int x = 0; x < pts; ++x) {
1906 SkDebugf("%s y=%g wtTs[0]=%g (%g,%g", __FUNCTION__,
1907 horzEdge->fY, wtTs[x], wt.fPts[0].fX, wt.fPts[0].fY);
1908 for (int y = 0; y < verb; ++y) {
1909 SkDebugf(" %g,%g", wt.fPts[y + 1].fX, wt.fPts[y + 1].fY));
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001910 }
caryclark@google.com198e0542012-03-30 18:47:02 +00001911 SkDebugf(")\n");
caryclark@google.comf8b000d2012-02-09 22:04:27 +00001912 }
caryclark@google.com198e0542012-03-30 18:47:02 +00001913 if (pts > verb) {
1914 SkASSERT(0); // FIXME ? should this work?
1915 SkDebugf("%s wtTs[1]=%g\n", __FUNCTION__, wtTs[1]);
1916 }
1917#endif
1918 test->add(wtTs, pts, wt.verbIndex());
caryclark@google.comf8b000d2012-02-09 22:04:27 +00001919 }
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00001920 horzEdge = *++sorted;
1921 } while (horzEdge->fY == bottom
1922 && horzEdge->fLeft <= test->fBounds.fRight);
1923 } while (wt.advance());
caryclark@google.comf8b000d2012-02-09 22:04:27 +00001924 }
1925}
1926
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001927#if DEBUG_ADD_INTERSECTING_TS
caryclark@google.coma5764232012-03-28 16:20:21 +00001928static void debugShowLineIntersection(int pts, const WorkEdge& wt,
1929 const WorkEdge& wn, const double wtTs[2], const double wnTs[2]) {
caryclark@google.coma5764232012-03-28 16:20:21 +00001930 if (!pts) {
1931 return;
1932 }
1933 SkPoint wtOutPt, wnOutPt;
1934 LineXYAtT(wt.fPts, wtTs[0], &wtOutPt);
1935 LineXYAtT(wn.fPts, wnTs[0], &wnOutPt);
caryclark@google.comfb173422012-04-10 18:28:55 +00001936 SkDebugf("%s wtTs[0]=%g (%g,%g, %g,%g) (%g,%g)\n",
caryclark@google.coma5764232012-03-28 16:20:21 +00001937 __FUNCTION__,
1938 wtTs[0], wt.fPts[0].fX, wt.fPts[0].fY,
caryclark@google.comfb173422012-04-10 18:28:55 +00001939 wt.fPts[1].fX, wt.fPts[1].fY, wtOutPt.fX, wtOutPt.fY);
caryclark@google.coma5764232012-03-28 16:20:21 +00001940 if (pts == 2) {
1941 SkDebugf("%s wtTs[1]=%g\n", __FUNCTION__, wtTs[1]);
1942 }
caryclark@google.comfb173422012-04-10 18:28:55 +00001943 SkDebugf("%s wnTs[0]=%g (%g,%g, %g,%g) (%g,%g)\n",
caryclark@google.coma5764232012-03-28 16:20:21 +00001944 __FUNCTION__,
1945 wnTs[0], wn.fPts[0].fX, wn.fPts[0].fY,
caryclark@google.comfb173422012-04-10 18:28:55 +00001946 wn.fPts[1].fX, wn.fPts[1].fY, wnOutPt.fX, wnOutPt.fY);
caryclark@google.coma5764232012-03-28 16:20:21 +00001947 if (pts == 2) {
1948 SkDebugf("%s wnTs[1]=%g\n", __FUNCTION__, wnTs[1]);
1949 }
caryclark@google.coma5764232012-03-28 16:20:21 +00001950}
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001951#else
1952static void debugShowLineIntersection(int , const WorkEdge& ,
1953 const WorkEdge& , const double [2], const double [2]) {
1954}
1955#endif
caryclark@google.coma5764232012-03-28 16:20:21 +00001956
caryclark@google.comf8b000d2012-02-09 22:04:27 +00001957static void addIntersectingTs(InEdge** currentPtr, InEdge** lastPtr) {
caryclark@google.comcef7e3f2012-02-28 16:57:05 +00001958 InEdge** testPtr = currentPtr - 1;
caryclark@google.com752b60e2012-03-22 21:11:17 +00001959 // FIXME: lastPtr should be past the point of interest, so
1960 // test below should be lastPtr - 2
1961 // that breaks testSimplifyTriangle22, so further investigation is needed
caryclark@google.comcef7e3f2012-02-28 16:57:05 +00001962 while (++testPtr != lastPtr - 1) {
1963 InEdge* test = *testPtr;
1964 InEdge** nextPtr = testPtr;
1965 do {
1966 InEdge* next = *++nextPtr;
caryclark@google.com198e0542012-03-30 18:47:02 +00001967 // FIXME: this compares against the sentinel sometimes
1968 // OPTIMIZATION: this may never be needed since this gets called
1969 // in two passes now. Verify that double hits are appropriate.
caryclark@google.comcef7e3f2012-02-28 16:57:05 +00001970 if (test->cached(next)) {
1971 continue;
1972 }
1973 if (!Bounds::Intersects(test->fBounds, next->fBounds)) {
1974 continue;
1975 }
caryclark@google.comf8b000d2012-02-09 22:04:27 +00001976 WorkEdge wt, wn;
1977 wt.init(test);
1978 wn.init(next);
1979 do {
caryclark@google.coma5764232012-03-28 16:20:21 +00001980 int pts;
1981 Intersections ts;
1982 bool swap = false;
1983 switch (wt.verb()) {
1984 case SkPath::kLine_Verb:
1985 switch (wn.verb()) {
1986 case SkPath::kLine_Verb: {
1987 pts = LineIntersect(wt.fPts, wn.fPts, ts);
1988 debugShowLineIntersection(pts, wt, wn,
1989 ts.fT[0], ts.fT[1]);
1990 break;
1991 }
1992 case SkPath::kQuad_Verb: {
1993 swap = true;
1994 pts = QuadLineIntersect(wn.fPts, wt.fPts, ts);
1995 break;
1996 }
1997 case SkPath::kCubic_Verb: {
1998 swap = true;
1999 pts = CubicLineIntersect(wn.fPts, wt.fPts, ts);
2000 break;
2001 }
2002 default:
2003 SkASSERT(0);
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002004 }
caryclark@google.coma5764232012-03-28 16:20:21 +00002005 break;
2006 case SkPath::kQuad_Verb:
2007 switch (wn.verb()) {
2008 case SkPath::kLine_Verb: {
2009 pts = QuadLineIntersect(wt.fPts, wn.fPts, ts);
2010 break;
2011 }
2012 case SkPath::kQuad_Verb: {
2013 pts = QuadIntersect(wt.fPts, wn.fPts, ts);
2014 break;
2015 }
2016 case SkPath::kCubic_Verb: {
2017 // FIXME: promote quad to cubic
2018 pts = CubicIntersect(wt.fPts, wn.fPts, ts);
2019 break;
2020 }
2021 default:
2022 SkASSERT(0);
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002023 }
caryclark@google.coma5764232012-03-28 16:20:21 +00002024 break;
2025 case SkPath::kCubic_Verb:
2026 switch (wn.verb()) {
2027 case SkPath::kLine_Verb: {
2028 pts = CubicLineIntersect(wt.fPts, wn.fPts, ts);
2029 break;
2030 }
2031 case SkPath::kQuad_Verb: {
2032 // FIXME: promote quad to cubic
2033 pts = CubicIntersect(wt.fPts, wn.fPts, ts);
2034 break;
2035 }
2036 case SkPath::kCubic_Verb: {
2037 pts = CubicIntersect(wt.fPts, wn.fPts, ts);
2038 break;
2039 }
2040 default:
2041 SkASSERT(0);
2042 }
2043 break;
2044 default:
2045 SkASSERT(0);
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002046 }
caryclark@google.coma5764232012-03-28 16:20:21 +00002047 test->add(ts.fT[swap], pts, wt.verbIndex());
2048 next->add(ts.fT[!swap], pts, wn.verbIndex());
caryclark@google.comcef7e3f2012-02-28 16:57:05 +00002049 } while (wt.bottom() <= wn.bottom() ? wt.advance() : wn.advance());
2050 } while (nextPtr != lastPtr);
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002051 }
2052}
2053
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002054static InEdge** advanceEdges(SkTDArray<ActiveEdge>* activeEdges,
caryclark@google.com6008c6562012-02-15 22:01:16 +00002055 InEdge** currentPtr, InEdge** lastPtr, SkScalar y) {
2056 InEdge** testPtr = currentPtr - 1;
2057 while (++testPtr != lastPtr) {
2058 if ((*testPtr)->fBounds.fBottom > y) {
2059 continue;
2060 }
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002061 if (activeEdges) {
2062 InEdge* test = *testPtr;
2063 ActiveEdge* activePtr = activeEdges->begin() - 1;
2064 ActiveEdge* lastActive = activeEdges->end();
2065 while (++activePtr != lastActive) {
2066 if (activePtr->fWorkEdge.fEdge == test) {
2067 activeEdges->remove(activePtr - activeEdges->begin());
2068 break;
2069 }
caryclark@google.com6008c6562012-02-15 22:01:16 +00002070 }
2071 }
2072 if (testPtr == currentPtr) {
2073 ++currentPtr;
2074 }
2075 }
2076 return currentPtr;
2077}
2078
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002079// OPTIMIZE: inline?
2080static HorizontalEdge** advanceHorizontal(HorizontalEdge** edge, SkScalar y) {
2081 while ((*edge)->fY < y) {
2082 ++edge;
2083 }
2084 return edge;
2085}
2086
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002087// compute bottom taking into account any intersected edges
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002088static SkScalar computeInterceptBottom(SkTDArray<ActiveEdge>& activeEdges,
2089 SkScalar y, SkScalar bottom) {
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002090 ActiveEdge* activePtr = activeEdges.begin() - 1;
2091 ActiveEdge* lastActive = activeEdges.end();
2092 while (++activePtr != lastActive) {
2093 const InEdge* test = activePtr->fWorkEdge.fEdge;
2094 if (!test->fContainsIntercepts) {
2095 continue;
2096 }
2097 WorkEdge wt;
2098 wt.init(test);
2099 do {
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002100 const Intercepts& intercepts = test->fIntercepts[wt.verbIndex()];
caryclark@google.com4917f172012-03-05 22:01:21 +00002101 if (intercepts.fTopIntercepts > 1) {
2102 SkScalar yTop = wt.fPts[0].fY;
2103 if (yTop > y && bottom > yTop) {
2104 bottom = yTop;
2105 }
2106 }
2107 if (intercepts.fBottomIntercepts > 1) {
2108 SkScalar yBottom = wt.fPts[wt.verb()].fY;
2109 if (yBottom > y && bottom > yBottom) {
2110 bottom = yBottom;
2111 }
2112 }
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002113 const SkTDArray<double>& fTs = intercepts.fTs;
2114 size_t count = fTs.count();
2115 for (size_t index = 0; index < count; ++index) {
caryclark@google.coma5764232012-03-28 16:20:21 +00002116 SkScalar yIntercept;
2117 switch (wt.verb()) {
2118 case SkPath::kLine_Verb: {
2119 yIntercept = LineYAtT(wt.fPts, fTs[index]);
2120 break;
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002121 }
caryclark@google.coma5764232012-03-28 16:20:21 +00002122 case SkPath::kQuad_Verb: {
2123 yIntercept = QuadYAtT(wt.fPts, fTs[index]);
2124 break;
2125 }
2126 case SkPath::kCubic_Verb: {
2127 yIntercept = CubicYAtT(wt.fPts, fTs[index]);
2128 break;
2129 }
2130 default:
2131 SkASSERT(0); // should never get here
2132 }
2133 if (yIntercept > y && bottom > yIntercept) {
2134 bottom = yIntercept;
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002135 }
2136 }
caryclark@google.comcef7e3f2012-02-28 16:57:05 +00002137 } while (wt.advance());
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002138 }
caryclark@google.com198e0542012-03-30 18:47:02 +00002139#if DEBUG_BOTTOM
2140 SkDebugf("%s bottom=%1.9g\n", __FUNCTION__, bottom);
2141#endif
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002142 return bottom;
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002143}
2144
2145static SkScalar findBottom(InEdge** currentPtr,
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002146 InEdge** edgeListEnd, SkTDArray<ActiveEdge>* activeEdges, SkScalar y,
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00002147 bool /*asFill*/, InEdge**& testPtr) {
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002148 InEdge* current = *currentPtr;
2149 SkScalar bottom = current->fBounds.fBottom;
caryclark@google.com752b60e2012-03-22 21:11:17 +00002150
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002151 // find the list of edges that cross y
caryclark@google.com6008c6562012-02-15 22:01:16 +00002152 InEdge* test = *testPtr;
2153 while (testPtr != edgeListEnd) {
2154 SkScalar testTop = test->fBounds.fTop;
2155 if (bottom <= testTop) {
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002156 break;
2157 }
caryclark@google.com6008c6562012-02-15 22:01:16 +00002158 SkScalar testBottom = test->fBounds.fBottom;
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002159 // OPTIMIZATION: Shortening the bottom is only interesting when filling
2160 // and when the edge is to the left of a longer edge. If it's a framing
2161 // edge, or part of the right, it won't effect the longer edges.
caryclark@google.com6008c6562012-02-15 22:01:16 +00002162 if (testTop > y) {
2163 bottom = testTop;
2164 break;
2165 }
2166 if (y < testBottom) {
2167 if (bottom > testBottom) {
2168 bottom = testBottom;
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002169 }
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002170 if (activeEdges) {
2171 addToActive(*activeEdges, test);
2172 }
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002173 }
caryclark@google.com6008c6562012-02-15 22:01:16 +00002174 test = *++testPtr;
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002175 }
caryclark@google.com198e0542012-03-30 18:47:02 +00002176#if DEBUG_BOTTOM
2177 SkDebugf("%s %d bottom=%1.9g\n", __FUNCTION__, activeEdges ? 2 : 1, bottom);
2178#endif
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002179 return bottom;
2180}
2181
2182static void makeEdgeList(SkTArray<InEdge>& edges, InEdge& edgeSentinel,
2183 SkTDArray<InEdge*>& edgeList) {
2184 size_t edgeCount = edges.count();
2185 if (edgeCount == 0) {
2186 return;
2187 }
caryclark@google.comfb173422012-04-10 18:28:55 +00002188 int id = 0;
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002189 for (size_t index = 0; index < edgeCount; ++index) {
caryclark@google.comfb173422012-04-10 18:28:55 +00002190 InEdge& edge = edges[index];
2191 if (!edge.fWinding) {
2192 continue;
2193 }
2194 edge.fID = ++id;
2195 *edgeList.append() = &edge;
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002196 }
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002197 *edgeList.append() = &edgeSentinel;
caryclark@google.comcd4421d2012-03-01 19:16:31 +00002198 QSort<InEdge>(edgeList.begin(), edgeList.end() - 1);
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002199}
2200
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002201static void makeHorizontalList(SkTDArray<HorizontalEdge>& edges,
2202 HorizontalEdge& edgeSentinel, SkTDArray<HorizontalEdge*>& edgeList) {
2203 size_t edgeCount = edges.count();
2204 if (edgeCount == 0) {
2205 return;
2206 }
2207 for (size_t index = 0; index < edgeCount; ++index) {
2208 *edgeList.append() = &edges[index];
2209 }
2210 edgeSentinel.fLeft = edgeSentinel.fRight = edgeSentinel.fY = SK_ScalarMax;
2211 *edgeList.append() = &edgeSentinel;
2212 QSort<HorizontalEdge>(edgeList.begin(), edgeList.end() - 1);
2213}
caryclark@google.com6b9cfb32012-02-16 21:24:41 +00002214
2215static void skipCoincidence(int lastWinding, int winding, int windingMask,
2216 ActiveEdge* activePtr, ActiveEdge* firstCoincident) {
2217 if (((lastWinding & windingMask) == 0) ^ (winding & windingMask) != 0) {
2218 return;
2219 }
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002220 // FIXME: ? shouldn't this be if (lastWinding & windingMask) ?
caryclark@google.com6b9cfb32012-02-16 21:24:41 +00002221 if (lastWinding) {
caryclark@google.com752b60e2012-03-22 21:11:17 +00002222#if DEBUG_ADJUST_COINCIDENT
2223 SkDebugf("%s edge=%d 1 set skip=false\n", __FUNCTION__, activePtr->ID());
2224#endif
caryclark@google.com6b9cfb32012-02-16 21:24:41 +00002225 activePtr->fSkip = false;
2226 } else {
caryclark@google.com752b60e2012-03-22 21:11:17 +00002227#if DEBUG_ADJUST_COINCIDENT
2228 SkDebugf("%s edge=%d 2 set skip=false\n", __FUNCTION__, firstCoincident->ID());
2229#endif
caryclark@google.com6b9cfb32012-02-16 21:24:41 +00002230 firstCoincident->fSkip = false;
2231 }
2232}
2233
caryclark@google.com6008c6562012-02-15 22:01:16 +00002234static void sortHorizontal(SkTDArray<ActiveEdge>& activeEdges,
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002235 SkTDArray<ActiveEdge*>& edgeList, SkScalar y) {
caryclark@google.com6008c6562012-02-15 22:01:16 +00002236 size_t edgeCount = activeEdges.count();
2237 if (edgeCount == 0) {
2238 return;
2239 }
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002240#if DEBUG_SORT_HORIZONTAL
caryclark@google.comd88e0892012-03-27 13:23:51 +00002241 const int tab = 3; // FIXME: debugging only
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002242 SkDebugf("%s y=%1.9g\n", __FUNCTION__, y);
2243#endif
caryclark@google.com6008c6562012-02-15 22:01:16 +00002244 size_t index;
2245 for (index = 0; index < edgeCount; ++index) {
2246 ActiveEdge& activeEdge = activeEdges[index];
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002247 do {
2248 activeEdge.calcLeft(y);
2249 // skip segments that don't span y
2250 if (activeEdge.fAbove != activeEdge.fBelow) {
2251 break;
2252 }
2253 if (activeEdge.fDone) {
2254#if DEBUG_SORT_HORIZONTAL
2255 SkDebugf("%*s edge=%d done\n", tab, "", activeEdge.ID());
2256#endif
2257 goto nextEdge;
2258 }
2259#if DEBUG_SORT_HORIZONTAL
2260 SkDebugf("%*s edge=%d above==below\n", tab, "", activeEdge.ID());
2261#endif
2262 } while (activeEdge.advanceT() || activeEdge.advance());
2263#if DEBUG_SORT_HORIZONTAL
2264 SkDebugf("%*s edge=%d above=(%1.9g,%1.9g) (%1.9g) below=(%1.9g,%1.9g)"
2265 " (%1.9g)\n", tab, "", activeEdge.ID(),
2266 activeEdge.fAbove.fX, activeEdge.fAbove.fY, activeEdge.fTAbove,
2267 activeEdge.fBelow.fX, activeEdge.fBelow.fY, activeEdge.fTBelow);
2268#endif
2269 activeEdge.fSkip = activeEdge.fCloseCall = activeEdge.fFixBelow = false;
caryclark@google.com6008c6562012-02-15 22:01:16 +00002270 *edgeList.append() = &activeEdge;
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002271nextEdge:
2272 ;
caryclark@google.com6008c6562012-02-15 22:01:16 +00002273 }
caryclark@google.comcd4421d2012-03-01 19:16:31 +00002274 QSort<ActiveEdge>(edgeList.begin(), edgeList.end() - 1);
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002275}
2276
2277// remove coincident edges
2278// OPTIMIZE: remove edges? This is tricky because the current logic expects
2279// the winding count to be maintained while skipping coincident edges. In
2280// addition to removing the coincident edges, the remaining edges would need
2281// to have a different winding value, possibly different per intercept span.
2282static SkScalar adjustCoincident(SkTDArray<ActiveEdge*>& edgeList,
2283 int windingMask, SkScalar y, SkScalar bottom, OutEdgeBuilder& outBuilder)
2284{
2285#if DEBUG_ADJUST_COINCIDENT
2286 SkDebugf("%s y=%1.9g bottom=%1.9g\n", __FUNCTION__, y, bottom);
2287#endif
2288 size_t edgeCount = edgeList.count();
2289 if (edgeCount == 0) {
2290 return bottom;
2291 }
caryclark@google.com78e17132012-04-17 11:40:34 +00002292 ActiveEdge* activePtr, * nextPtr = edgeList[0];
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002293 size_t index;
2294 bool foundCoincident = false;
2295 int firstIndex = 0;
caryclark@google.com6008c6562012-02-15 22:01:16 +00002296 for (index = 1; index < edgeCount; ++index) {
caryclark@google.com78e17132012-04-17 11:40:34 +00002297 activePtr = nextPtr;
2298 nextPtr = edgeList[index];
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002299 if (firstIndex != index - 1 && activePtr->fVerb > SkPath::kLine_Verb
2300 && nextPtr->fVerb == SkPath::kLine_Verb
2301 && activePtr->isUnordered(nextPtr)) {
2302 // swap the line with the curve
2303 // back up to the previous edge and retest
2304 SkTSwap<ActiveEdge*>(edgeList[index - 1], edgeList[index]);
2305 SkASSERT(index > 1);
2306 index -= 2;
2307 nextPtr = edgeList[index];
2308 continue;
caryclark@google.com78e17132012-04-17 11:40:34 +00002309 }
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002310 bool closeCall = false;
2311 activePtr->fCoincident = firstIndex;
caryclark@google.com78e17132012-04-17 11:40:34 +00002312 if (activePtr->isCoincidentWith(nextPtr)
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002313 || (closeCall = activePtr->tooCloseToCall(nextPtr))) {
2314 activePtr->fSkip = nextPtr->fSkip = foundCoincident = true;
2315 activePtr->fCloseCall = nextPtr->fCloseCall = closeCall;
caryclark@google.com78e17132012-04-17 11:40:34 +00002316 } else if (activePtr->isUnordered(nextPtr)) {
2317 foundCoincident = true;
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002318 } else {
2319 firstIndex = index;
2320 }
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002321 }
caryclark@google.com78e17132012-04-17 11:40:34 +00002322 nextPtr->fCoincident = firstIndex;
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002323 if (!foundCoincident) {
2324 return bottom;
2325 }
2326 int winding = 0;
caryclark@google.com78e17132012-04-17 11:40:34 +00002327 nextPtr = edgeList[0];
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002328 for (index = 1; index < edgeCount; ++index) {
2329 int priorWinding = winding;
caryclark@google.com6b9cfb32012-02-16 21:24:41 +00002330 winding += activePtr->fWorkEdge.winding();
caryclark@google.com78e17132012-04-17 11:40:34 +00002331 activePtr = nextPtr;
2332 nextPtr = edgeList[index];
2333 SkASSERT(activePtr == edgeList[index - 1]);
2334 SkASSERT(nextPtr == edgeList[index]);
2335 if (activePtr->fCoincident != nextPtr->fCoincident) {
2336 continue;
2337 }
2338 // the coincident edges may not have been sorted above -- advance
2339 // the edges and resort if needed
2340 // OPTIMIZE: if sorting is done incrementally as new edges are added
2341 // and not all at once as is done here, fold this test into the
2342 // current less than test.
2343 while ((!activePtr->fSkip || !nextPtr->fSkip)
2344 && activePtr->fCoincident == nextPtr->fCoincident) {
2345 if (activePtr->swapUnordered(nextPtr, bottom)) {
2346 winding -= activePtr->fWorkEdge.winding();
2347 SkASSERT(activePtr == edgeList[index - 1]);
2348 SkASSERT(nextPtr == edgeList[index]);
2349 SkTSwap<ActiveEdge*>(edgeList[index - 1], edgeList[index]);
2350 if (--index == 0) {
2351 winding += activePtr->fWorkEdge.winding();
2352 break;
2353 }
2354 // back up one
2355 activePtr = edgeList[index - 1];
2356 continue;
2357 }
2358 SkASSERT(activePtr == edgeList[index - 1]);
2359 SkASSERT(nextPtr == edgeList[index]);
2360 break;
2361 }
2362 if (activePtr->fSkip && nextPtr->fSkip) {
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002363 if (activePtr->fCloseCall ? activePtr->swapClose(nextPtr,
2364 priorWinding, winding, windingMask)
2365 : activePtr->swapCoincident(nextPtr, bottom)) {
caryclark@google.com4917f172012-03-05 22:01:21 +00002366 winding -= activePtr->fWorkEdge.winding();
caryclark@google.com78e17132012-04-17 11:40:34 +00002367 SkASSERT(activePtr == edgeList[index - 1]);
2368 SkASSERT(nextPtr == edgeList[index]);
caryclark@google.comcd4421d2012-03-01 19:16:31 +00002369 SkTSwap<ActiveEdge*>(edgeList[index - 1], edgeList[index]);
2370 SkTSwap<ActiveEdge*>(activePtr, nextPtr);
caryclark@google.com4917f172012-03-05 22:01:21 +00002371 winding += activePtr->fWorkEdge.winding();
caryclark@google.com78e17132012-04-17 11:40:34 +00002372 SkASSERT(activePtr == edgeList[index - 1]);
2373 SkASSERT(nextPtr == edgeList[index]);
caryclark@google.comcd4421d2012-03-01 19:16:31 +00002374 }
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002375 }
2376 }
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002377 int firstCoincidentWinding = 0;
2378 ActiveEdge* firstCoincident = NULL;
2379 winding = 0;
2380 activePtr = edgeList[0];
2381 for (index = 1; index < edgeCount; ++index) {
2382 int priorWinding = winding;
caryclark@google.com6b9cfb32012-02-16 21:24:41 +00002383 winding += activePtr->fWorkEdge.winding();
caryclark@google.com78e17132012-04-17 11:40:34 +00002384 nextPtr = edgeList[index];
2385 if (activePtr->fSkip && nextPtr->fSkip
2386 && activePtr->fCoincident == nextPtr->fCoincident) {
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002387 if (!firstCoincident) {
2388 firstCoincident = activePtr;
2389 firstCoincidentWinding = priorWinding;
2390 }
2391 if (activePtr->fCloseCall) {
2392 // If one of the edges has already been added to out as a non
2393 // coincident edge, trim it back to the top of this span
2394 if (outBuilder.trimLine(y, activePtr->ID())) {
2395 activePtr->addTatYAbove(y);
caryclark@google.com752b60e2012-03-22 21:11:17 +00002396 #if DEBUG_ADJUST_COINCIDENT
2397 SkDebugf("%s 1 edge=%d y=%1.9g (was fYBottom=%1.9g)\n",
2398 __FUNCTION__, activePtr->ID(), y, activePtr->fYBottom);
2399 #endif
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002400 activePtr->fYBottom = y;
2401 }
2402 if (outBuilder.trimLine(y, nextPtr->ID())) {
2403 nextPtr->addTatYAbove(y);
caryclark@google.com752b60e2012-03-22 21:11:17 +00002404 #if DEBUG_ADJUST_COINCIDENT
2405 SkDebugf("%s 2 edge=%d y=%1.9g (was fYBottom=%1.9g)\n",
2406 __FUNCTION__, nextPtr->ID(), y, nextPtr->fYBottom);
2407 #endif
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002408 nextPtr->fYBottom = y;
2409 }
2410 // add missing t values so edges can be the same length
2411 SkScalar testY = activePtr->fBelow.fY;
2412 nextPtr->addTatYBelow(testY);
2413 if (bottom > testY && testY > y) {
caryclark@google.com752b60e2012-03-22 21:11:17 +00002414 #if DEBUG_ADJUST_COINCIDENT
2415 SkDebugf("%s 3 edge=%d bottom=%1.9g (was bottom=%1.9g)\n",
2416 __FUNCTION__, activePtr->ID(), testY, bottom);
2417 #endif
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002418 bottom = testY;
2419 }
2420 testY = nextPtr->fBelow.fY;
2421 activePtr->addTatYBelow(testY);
2422 if (bottom > testY && testY > y) {
caryclark@google.com752b60e2012-03-22 21:11:17 +00002423 #if DEBUG_ADJUST_COINCIDENT
2424 SkDebugf("%s 4 edge=%d bottom=%1.9g (was bottom=%1.9g)\n",
2425 __FUNCTION__, nextPtr->ID(), testY, bottom);
2426 #endif
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002427 bottom = testY;
2428 }
2429 }
2430 } else if (firstCoincident) {
2431 skipCoincidence(firstCoincidentWinding, winding, windingMask,
2432 activePtr, firstCoincident);
2433 firstCoincident = NULL;
2434 }
2435 activePtr = nextPtr;
2436 }
2437 if (firstCoincident) {
2438 winding += activePtr->fWorkEdge.winding();
2439 skipCoincidence(firstCoincidentWinding, winding, windingMask, activePtr,
caryclark@google.com6b9cfb32012-02-16 21:24:41 +00002440 firstCoincident);
2441 }
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002442 // fix up the bottom for close call edges. OPTIMIZATION: maybe this could
2443 // be in the loop above, but moved here since loop above reads fBelow and
2444 // it felt unsafe to write it in that loop
2445 for (index = 0; index < edgeCount; ++index) {
2446 (edgeList[index])->fixBelow();
2447 }
2448 return bottom;
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002449}
2450
2451// stitch edge and t range that satisfies operation
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00002452static void stitchEdge(SkTDArray<ActiveEdge*>& edgeList, SkScalar
2453#if DEBUG_STITCH_EDGE
2454y
2455#endif
2456,
caryclark@google.com752b60e2012-03-22 21:11:17 +00002457 SkScalar bottom, int windingMask, bool fill, OutEdgeBuilder& outBuilder) {
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002458 int winding = 0;
caryclark@google.com6008c6562012-02-15 22:01:16 +00002459 ActiveEdge** activeHandle = edgeList.begin() - 1;
2460 ActiveEdge** lastActive = edgeList.end();
caryclark@google.com78e17132012-04-17 11:40:34 +00002461#if DEBUG_STITCH_EDGE
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00002462 const int tab = 7; // FIXME: debugging only
caryclark@google.com78e17132012-04-17 11:40:34 +00002463 SkDebugf("%s y=%1.9g bottom=%1.9g\n", __FUNCTION__, y, bottom);
2464#endif
caryclark@google.com6008c6562012-02-15 22:01:16 +00002465 while (++activeHandle != lastActive) {
2466 ActiveEdge* activePtr = *activeHandle;
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002467 const WorkEdge& wt = activePtr->fWorkEdge;
2468 int lastWinding = winding;
2469 winding += wt.winding();
caryclark@google.com78e17132012-04-17 11:40:34 +00002470#if DEBUG_STITCH_EDGE
2471 SkDebugf("%*s edge=%d lastWinding=%d winding=%d skip=%d close=%d"
2472 " above=%1.9g below=%1.9g\n",
2473 tab-4, "", activePtr->ID(), lastWinding,
2474 winding, activePtr->fSkip, activePtr->fCloseCall,
2475 activePtr->fTAbove, activePtr->fTBelow);
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002476#endif
caryclark@google.com752b60e2012-03-22 21:11:17 +00002477 if (activePtr->done(bottom)) {
caryclark@google.com78e17132012-04-17 11:40:34 +00002478#if DEBUG_STITCH_EDGE
2479 SkDebugf("%*s fDone=%d || fYBottom=%1.9g >= bottom\n", tab, "",
2480 activePtr->fDone, activePtr->fYBottom);
2481#endif
caryclark@google.com752b60e2012-03-22 21:11:17 +00002482 continue;
caryclark@google.comcef7e3f2012-02-28 16:57:05 +00002483 }
caryclark@google.comc17972e2012-02-20 21:33:22 +00002484 int opener = (lastWinding & windingMask) == 0;
2485 bool closer = (winding & windingMask) == 0;
2486 SkASSERT(!opener | !closer);
2487 bool inWinding = opener | closer;
caryclark@google.coma5764232012-03-28 16:20:21 +00002488 SkPoint clippedPts[4];
caryclark@google.com4917f172012-03-05 22:01:21 +00002489 const SkPoint* clipped = NULL;
caryclark@google.comcef7e3f2012-02-28 16:57:05 +00002490 bool moreToDo, aboveBottom;
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002491 do {
2492 double currentT = activePtr->t();
2493 const SkPoint* points = wt.fPts;
caryclark@google.comcef7e3f2012-02-28 16:57:05 +00002494 double nextT;
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002495 SkPath::Verb verb = activePtr->fVerb;
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002496 do {
caryclark@google.comcef7e3f2012-02-28 16:57:05 +00002497 nextT = activePtr->nextT();
caryclark@google.coma5764232012-03-28 16:20:21 +00002498 // FIXME: obtuse: want efficient way to say
2499 // !currentT && currentT != 1 || !nextT && nextT != 1
2500 if (currentT * nextT != 0 || currentT + nextT != 1) {
2501 // OPTIMIZATION: if !inWinding, we only need
2502 // clipped[1].fY
2503 switch (verb) {
2504 case SkPath::kLine_Verb:
2505 LineSubDivide(points, currentT, nextT, clippedPts);
2506 break;
2507 case SkPath::kQuad_Verb:
2508 QuadSubDivide(points, currentT, nextT, clippedPts);
2509 break;
2510 case SkPath::kCubic_Verb:
2511 CubicSubDivide(points, currentT, nextT, clippedPts);
2512 break;
2513 default:
2514 SkASSERT(0);
2515 break;
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002516 }
caryclark@google.coma5764232012-03-28 16:20:21 +00002517 clipped = clippedPts;
caryclark@google.comcef7e3f2012-02-28 16:57:05 +00002518 } else {
caryclark@google.coma5764232012-03-28 16:20:21 +00002519 clipped = points;
2520 }
2521 if (inWinding && !activePtr->fSkip && (fill ? clipped[0].fY
2522 != clipped[verb].fY : clipped[0] != clipped[verb])) {
caryclark@google.comfb173422012-04-10 18:28:55 +00002523#if DEBUG_STITCH_EDGE
2524 SkDebugf("%*s add%s %1.9g,%1.9g %1.9g,%1.9g edge=%d"
2525 " v=%d t=(%1.9g,%1.9g)\n", tab, "",
2526 kUVerbStr[verb], clipped[0].fX, clipped[0].fY,
2527 clipped[verb].fX, clipped[verb].fY,
2528 activePtr->ID(),
2529 activePtr->fWorkEdge.fVerb
2530 - activePtr->fWorkEdge.fEdge->fVerbs.begin(),
2531 currentT, nextT);
2532#endif
caryclark@google.coma5764232012-03-28 16:20:21 +00002533 outBuilder.addCurve(clipped, (SkPath::Verb) verb,
2534 activePtr->fWorkEdge.fEdge->fID,
2535 activePtr->fCloseCall);
2536 } else {
caryclark@google.comfb173422012-04-10 18:28:55 +00002537#if DEBUG_STITCH_EDGE
2538 SkDebugf("%*s skip%s %1.9g,%1.9g %1.9g,%1.9g"
2539 " edge=%d v=%d t=(%1.9g,%1.9g)\n", tab, "",
2540 kUVerbStr[verb], clipped[0].fX, clipped[0].fY,
2541 clipped[verb].fX, clipped[verb].fY,
2542 activePtr->ID(),
2543 activePtr->fWorkEdge.fVerb
2544 - activePtr->fWorkEdge.fEdge->fVerbs.begin(),
2545 currentT, nextT);
2546#endif
caryclark@google.coma5764232012-03-28 16:20:21 +00002547 }
2548 // by advancing fAbove/fBelow, the next call to sortHorizontal
2549 // will use these values if they're still valid instead of
2550 // recomputing
caryclark@google.com78e17132012-04-17 11:40:34 +00002551 if (clipped[verb].fY > activePtr->fBelow.fY
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002552 && bottom >= activePtr->fBelow.fY
2553 && verb == SkPath::kLine_Verb) {
caryclark@google.coma5764232012-03-28 16:20:21 +00002554 activePtr->fAbove = activePtr->fBelow;
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002555 activePtr->fBelow = activePtr->fTangent = clipped[verb];
caryclark@google.com78e17132012-04-17 11:40:34 +00002556 activePtr->fTAbove = activePtr->fTBelow < 1
2557 ? activePtr->fTBelow : 0;
caryclark@google.coma5764232012-03-28 16:20:21 +00002558 activePtr->fTBelow = nextT;
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002559 }
2560 currentT = nextT;
caryclark@google.comcef7e3f2012-02-28 16:57:05 +00002561 moreToDo = activePtr->advanceT();
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002562 activePtr->fYBottom = clipped[verb].fY; // was activePtr->fCloseCall ? bottom :
2563
2564 // clearing the fSkip/fCloseCall bit here means that trailing edges
2565 // fall out of sync, if one edge is long and another is a series of short pieces
2566 // if fSkip/fCloseCall is set, need to recompute coincidence/too-close-to-call
2567 // after advancing
2568 // another approach would be to restrict bottom to smaller part of close call
2569 // maybe this is already happening with coincidence when intersection is computed,
2570 // and needs to be added to the close call computation as well
2571 // this is hard to do because that the bottom is important is not known when
2572 // the lines are intersected; only when the computation for edge sorting is done
2573 // does the need for new bottoms become apparent.
2574 // maybe this is good incentive to scrap the current sort and do an insertion
2575 // sort that can take this into consideration when the x value is computed
2576
2577 // FIXME: initialized in sortHorizontal, cleared here as well so
2578 // that next edge is not skipped -- but should skipped edges ever
2579 // continue? (probably not)
caryclark@google.com752b60e2012-03-22 21:11:17 +00002580 aboveBottom = clipped[verb].fY < bottom;
2581 if (clipped[0].fY != clipped[verb].fY) {
2582 activePtr->fSkip = false;
2583 activePtr->fCloseCall = false;
2584 aboveBottom &= !activePtr->fCloseCall;
caryclark@google.com78e17132012-04-17 11:40:34 +00002585 }
2586#if DEBUG_STITCH_EDGE
2587 else {
caryclark@google.com752b60e2012-03-22 21:11:17 +00002588 if (activePtr->fSkip || activePtr->fCloseCall) {
caryclark@google.com78e17132012-04-17 11:40:34 +00002589 SkDebugf("%s skip or close == %1.9g\n", __FUNCTION__,
2590 clippedPts[0].fY);
caryclark@google.com752b60e2012-03-22 21:11:17 +00002591 }
2592 }
caryclark@google.com78e17132012-04-17 11:40:34 +00002593#endif
caryclark@google.comcef7e3f2012-02-28 16:57:05 +00002594 } while (moreToDo & aboveBottom);
2595 } while ((moreToDo || activePtr->advance()) & aboveBottom);
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002596 }
2597}
2598
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00002599#if DEBUG_DUMP
caryclark@google.com198e0542012-03-30 18:47:02 +00002600static void dumpEdgeList(const SkTDArray<InEdge*>& edgeList,
2601 const InEdge& edgeSentinel) {
caryclark@google.com198e0542012-03-30 18:47:02 +00002602 InEdge** debugPtr = edgeList.begin();
2603 do {
2604 (*debugPtr++)->dump();
2605 } while (*debugPtr != &edgeSentinel);
caryclark@google.com198e0542012-03-30 18:47:02 +00002606}
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00002607#else
2608static void dumpEdgeList(const SkTDArray<InEdge*>& ,
2609 const InEdge& ) {
2610}
2611#endif
caryclark@google.com198e0542012-03-30 18:47:02 +00002612
caryclark@google.comc6825902012-02-03 22:07:47 +00002613void simplify(const SkPath& path, bool asFill, SkPath& simple) {
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002614 // returns 1 for evenodd, -1 for winding, regardless of inverse-ness
2615 int windingMask = (path.getFillType() & 1) ? 1 : -1;
2616 simple.reset();
2617 simple.setFillType(SkPath::kEvenOdd_FillType);
caryclark@google.comc6825902012-02-03 22:07:47 +00002618 // turn path into list of edges increasing in y
caryclark@google.comcef7e3f2012-02-28 16:57:05 +00002619 // if an edge is a quad or a cubic with a y extrema, note it, but leave it
2620 // unbroken. Once we have a list, sort it, then walk the list (walk edges
2621 // twice that have y extrema's on top) and detect crossings -- look for raw
2622 // bounds that cross over, then tight bounds that cross
caryclark@google.com6680fb12012-02-07 22:10:51 +00002623 SkTArray<InEdge> edges;
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002624 SkTDArray<HorizontalEdge> horizontalEdges;
2625 InEdgeBuilder builder(path, asFill, edges, horizontalEdges);
caryclark@google.com6680fb12012-02-07 22:10:51 +00002626 SkTDArray<InEdge*> edgeList;
caryclark@google.com6680fb12012-02-07 22:10:51 +00002627 InEdge edgeSentinel;
caryclark@google.comfb173422012-04-10 18:28:55 +00002628 edgeSentinel.reset();
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002629 makeEdgeList(edges, edgeSentinel, edgeList);
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002630 SkTDArray<HorizontalEdge*> horizontalList;
2631 HorizontalEdge horizontalSentinel;
2632 makeHorizontalList(horizontalEdges, horizontalSentinel, horizontalList);
caryclark@google.com6680fb12012-02-07 22:10:51 +00002633 InEdge** currentPtr = edgeList.begin();
caryclark@google.com4917f172012-03-05 22:01:21 +00002634 if (!currentPtr) {
2635 return;
2636 }
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002637 // find all intersections between edges
2638// beyond looking for horizontal intercepts, we need to know if any active edges
2639// intersect edges below 'bottom', but above the active edge segment.
2640// maybe it makes more sense to compute all intercepts before doing anything
2641// else, since the intercept list is long-lived, at least in the current design.
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002642 SkScalar y = (*currentPtr)->fBounds.fTop;
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002643 HorizontalEdge** currentHorizontal = horizontalList.begin();
caryclark@google.comc6825902012-02-03 22:07:47 +00002644 do {
caryclark@google.com6680fb12012-02-07 22:10:51 +00002645 InEdge** lastPtr = currentPtr; // find the edge below the bottom of the first set
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002646 SkScalar bottom = findBottom(currentPtr, edgeList.end(),
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002647 NULL, y, asFill, lastPtr);
caryclark@google.comc17972e2012-02-20 21:33:22 +00002648 if (lastPtr > currentPtr) {
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002649 if (currentHorizontal) {
2650 if ((*currentHorizontal)->fY < SK_ScalarMax) {
2651 addBottomT(currentPtr, lastPtr, currentHorizontal);
2652 }
2653 currentHorizontal = advanceHorizontal(currentHorizontal, bottom);
2654 }
caryclark@google.comc17972e2012-02-20 21:33:22 +00002655 addIntersectingTs(currentPtr, lastPtr);
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002656 }
2657 y = bottom;
2658 currentPtr = advanceEdges(NULL, currentPtr, lastPtr, y);
2659 } while (*currentPtr != &edgeSentinel);
caryclark@google.com198e0542012-03-30 18:47:02 +00002660 // if a quadratic or cubic now has an intermediate T value, see if the Ts
2661 // on either side cause the Y values to monotonically increase. If not, split
2662 // the curve at the new T.
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002663
2664 // try an alternate approach which does not split curves or stitch edges
2665 // (may still need adjustCoincident, though)
2666 // the idea is to output non-intersecting contours, then figure out their
2667 // respective winding contribution
2668 // each contour will need to know whether it is CW or CCW, and then whether
2669 // a ray from that contour hits any a contour that contains it. The ray can
2670 // move to the left and then arbitrarily move up or down (as long as it never
2671 // moves to the right) to find a reference sibling contour or containing
2672 // contour. If the contour is part of an intersection, the companion contour
2673 // that is part of the intersection can determine the containership.
caryclark@google.com198e0542012-03-30 18:47:02 +00002674 if (builder.containsCurves()) {
2675 currentPtr = edgeList.begin();
2676 SkTArray<InEdge> splits;
2677 do {
caryclark@google.comfb173422012-04-10 18:28:55 +00002678 (*currentPtr)->splitInflectionPts(splits);
caryclark@google.com198e0542012-03-30 18:47:02 +00002679 } while (*++currentPtr != &edgeSentinel);
2680 if (splits.count()) {
caryclark@google.com198e0542012-03-30 18:47:02 +00002681 for (int index = 0; index < splits.count(); ++index) {
2682 edges.push_back(splits[index]);
2683 }
caryclark@google.comfb173422012-04-10 18:28:55 +00002684 edgeList.reset();
caryclark@google.com198e0542012-03-30 18:47:02 +00002685 makeEdgeList(edges, edgeSentinel, edgeList);
2686 }
2687 }
2688 dumpEdgeList(edgeList, edgeSentinel);
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002689 // walk the sorted edges from top to bottom, computing accumulated winding
2690 SkTDArray<ActiveEdge> activeEdges;
2691 OutEdgeBuilder outBuilder(asFill);
2692 currentPtr = edgeList.begin();
2693 y = (*currentPtr)->fBounds.fTop;
2694 do {
2695 InEdge** lastPtr = currentPtr; // find the edge below the bottom of the first set
2696 SkScalar bottom = findBottom(currentPtr, edgeList.end(),
2697 &activeEdges, y, asFill, lastPtr);
2698 if (lastPtr > currentPtr) {
2699 bottom = computeInterceptBottom(activeEdges, y, bottom);
caryclark@google.comc17972e2012-02-20 21:33:22 +00002700 SkTDArray<ActiveEdge*> activeEdgeList;
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002701 sortHorizontal(activeEdges, activeEdgeList, y);
2702 bottom = adjustCoincident(activeEdgeList, windingMask, y, bottom,
2703 outBuilder);
caryclark@google.com752b60e2012-03-22 21:11:17 +00002704 stitchEdge(activeEdgeList, y, bottom, windingMask, asFill, outBuilder);
caryclark@google.comc17972e2012-02-20 21:33:22 +00002705 }
caryclark@google.comc6825902012-02-03 22:07:47 +00002706 y = bottom;
caryclark@google.com2e7f4c82012-03-20 21:11:59 +00002707 // OPTIMIZATION: as edges expire, InEdge allocations could be released
2708 currentPtr = advanceEdges(&activeEdges, currentPtr, lastPtr, y);
caryclark@google.comc6825902012-02-03 22:07:47 +00002709 } while (*currentPtr != &edgeSentinel);
caryclark@google.comc6825902012-02-03 22:07:47 +00002710 // assemble output path from string of pts, verbs
caryclark@google.comf8b000d2012-02-09 22:04:27 +00002711 outBuilder.bridge();
2712 outBuilder.assemble(simple);
caryclark@google.comc6825902012-02-03 22:07:47 +00002713}