blob: 859755f5f6a7fa1c29061eb3784b272f46d82f82 [file] [log] [blame]
caryclark@google.comfa0588f2012-04-26 21:01:06 +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 */
caryclark@google.comb45a1b42012-05-18 20:50:33 +00007#include "Simplify.h"
caryclark@google.comfa0588f2012-04-26 21:01:06 +00008
9#undef SkASSERT
10#define SkASSERT(cond) while (!(cond)) { sk_throw(); }
11
caryclark@google.com15fa1382012-05-07 20:49:36 +000012// Terminology:
13// A Path contains one of more Contours
14// A Contour is made up of Segment array
caryclark@google.comb45a1b42012-05-18 20:50:33 +000015// A Segment is described by a Verb and a Point array with 2, 3, or 4 points
16// A Verb is one of Line, Quad(ratic), or Cubic
caryclark@google.com15fa1382012-05-07 20:49:36 +000017// A Segment contains a Span array
18// A Span is describes a portion of a Segment using starting and ending T
19// T values range from 0 to 1, where 0 is the first Point in the Segment
20
caryclark@google.comfa0588f2012-04-26 21:01:06 +000021// FIXME: remove once debugging is complete
22#if 0 // set to 1 for no debugging whatsoever
23
24//const bool gxRunTestsInOneThread = false;
25
26#define DEBUG_ADD_INTERSECTING_TS 0
27#define DEBUG_BRIDGE 0
caryclark@google.com8dcf1142012-07-02 20:27:02 +000028#define DEBUG_CROSS 0
caryclark@google.comfa0588f2012-04-26 21:01:06 +000029#define DEBUG_DUMP 0
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000030#define DEBUG_PATH_CONSTRUCTION 0
caryclark@google.com027de222012-07-12 12:52:50 +000031#define DEBUG_ACTIVE_SPANS 0
caryclark@google.com8dcf1142012-07-02 20:27:02 +000032#define DEBUG_WINDING 0
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000033#define DEBUG_UNUSED 0 // set to expose unused functions
caryclark@google.com8dcf1142012-07-02 20:27:02 +000034#define DEBUG_MARK_DONE 0
caryclark@google.comfa0588f2012-04-26 21:01:06 +000035
36#else
37
38//const bool gRunTestsInOneThread = true;
39
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000040#define DEBUG_ADD_INTERSECTING_TS 0
caryclark@google.comfa0588f2012-04-26 21:01:06 +000041#define DEBUG_BRIDGE 1
caryclark@google.com8dcf1142012-07-02 20:27:02 +000042#define DEBUG_CROSS 1
caryclark@google.comfa0588f2012-04-26 21:01:06 +000043#define DEBUG_DUMP 1
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000044#define DEBUG_PATH_CONSTRUCTION 1
caryclark@google.com210acaf2012-07-12 21:05:13 +000045#define DEBUG_ACTIVE_SPANS 01
46#define DEBUG_WINDING 01
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000047#define DEBUG_UNUSED 0 // set to expose unused functions
caryclark@google.com210acaf2012-07-12 21:05:13 +000048#define DEBUG_MARK_DONE 01
caryclark@google.comfa0588f2012-04-26 21:01:06 +000049
50#endif
51
caryclark@google.com027de222012-07-12 12:52:50 +000052#if DEBUG_ACTIVE_SPANS && !DEBUG_DUMP
53#undef DEBUG_DUMP
54#define DEBUG_DUMP 1
55#endif
56
caryclark@google.comfa0588f2012-04-26 21:01:06 +000057#if DEBUG_DUMP
58static const char* kLVerbStr[] = {"", "line", "quad", "cubic"};
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000059// static const char* kUVerbStr[] = {"", "Line", "Quad", "Cubic"};
caryclark@google.comfa0588f2012-04-26 21:01:06 +000060static int gContourID;
61static int gSegmentID;
62#endif
63
caryclark@google.com8dcf1142012-07-02 20:27:02 +000064#ifndef DEBUG_TEST
65#define DEBUG_TEST 0
66#endif
67
caryclark@google.comfa0588f2012-04-26 21:01:06 +000068static int LineIntersect(const SkPoint a[2], const SkPoint b[2],
69 Intersections& intersections) {
70 const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}};
71 const _Line bLine = {{b[0].fX, b[0].fY}, {b[1].fX, b[1].fY}};
72 return intersect(aLine, bLine, intersections.fT[0], intersections.fT[1]);
73}
74
75static int QuadLineIntersect(const SkPoint a[3], const SkPoint b[2],
76 Intersections& intersections) {
77 const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}};
78 const _Line bLine = {{b[0].fX, b[0].fY}, {b[1].fX, b[1].fY}};
79 intersect(aQuad, bLine, intersections);
80 return intersections.fUsed;
81}
82
83static int CubicLineIntersect(const SkPoint a[2], const SkPoint b[3],
84 Intersections& intersections) {
85 const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY},
86 {a[3].fX, a[3].fY}};
87 const _Line bLine = {{b[0].fX, b[0].fY}, {b[1].fX, b[1].fY}};
88 return intersect(aCubic, bLine, intersections.fT[0], intersections.fT[1]);
89}
90
91static int QuadIntersect(const SkPoint a[3], const SkPoint b[3],
92 Intersections& intersections) {
93 const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}};
94 const Quadratic bQuad = {{b[0].fX, b[0].fY}, {b[1].fX, b[1].fY}, {b[2].fX, b[2].fY}};
95 intersect(aQuad, bQuad, intersections);
96 return intersections.fUsed;
97}
98
99static int CubicIntersect(const SkPoint a[4], const SkPoint b[4],
100 Intersections& intersections) {
101 const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY},
102 {a[3].fX, a[3].fY}};
103 const Cubic bCubic = {{b[0].fX, b[0].fY}, {b[1].fX, b[1].fY}, {b[2].fX, b[2].fY},
104 {b[3].fX, b[3].fY}};
105 intersect(aCubic, bCubic, intersections);
106 return intersections.fUsed;
107}
108
109static int HLineIntersect(const SkPoint a[2], SkScalar left, SkScalar right,
110 SkScalar y, bool flipped, Intersections& intersections) {
111 const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}};
112 return horizontalIntersect(aLine, left, right, y, flipped, intersections);
113}
114
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000115static int HQuadIntersect(const SkPoint a[3], SkScalar left, SkScalar right,
116 SkScalar y, bool flipped, Intersections& intersections) {
117 const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}};
118 return horizontalIntersect(aQuad, left, right, y, flipped, intersections);
119}
120
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000121static int HCubicIntersect(const SkPoint a[4], SkScalar left, SkScalar right,
122 SkScalar y, bool flipped, Intersections& intersections) {
123 const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY},
124 {a[3].fX, a[3].fY}};
125 return horizontalIntersect(aCubic, left, right, y, flipped, intersections);
126}
127
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000128static int VLineIntersect(const SkPoint a[2], SkScalar top, SkScalar bottom,
129 SkScalar x, bool flipped, Intersections& intersections) {
130 const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}};
131 return verticalIntersect(aLine, top, bottom, x, flipped, intersections);
132}
133
134static int VQuadIntersect(const SkPoint a[3], SkScalar top, SkScalar bottom,
135 SkScalar x, bool flipped, Intersections& intersections) {
136 const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}};
137 return verticalIntersect(aQuad, top, bottom, x, flipped, intersections);
138}
139
140static int VCubicIntersect(const SkPoint a[4], SkScalar top, SkScalar bottom,
141 SkScalar x, bool flipped, Intersections& intersections) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000142 const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY},
143 {a[3].fX, a[3].fY}};
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000144 return verticalIntersect(aCubic, top, bottom, x, flipped, intersections);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000145}
146
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000147static int (* const VSegmentIntersect[])(const SkPoint [], SkScalar ,
148 SkScalar , SkScalar , bool , Intersections& ) = {
149 NULL,
150 VLineIntersect,
151 VQuadIntersect,
152 VCubicIntersect
153};
154
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000155static void LineXYAtT(const SkPoint a[2], double t, SkPoint* out) {
156 const _Line line = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}};
157 double x, y;
158 xy_at_t(line, t, x, y);
159 out->fX = SkDoubleToScalar(x);
160 out->fY = SkDoubleToScalar(y);
161}
162
163static void QuadXYAtT(const SkPoint a[3], double t, SkPoint* out) {
164 const Quadratic quad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}};
165 double x, y;
166 xy_at_t(quad, t, x, y);
167 out->fX = SkDoubleToScalar(x);
168 out->fY = SkDoubleToScalar(y);
169}
170
171static void CubicXYAtT(const SkPoint a[4], double t, SkPoint* out) {
172 const Cubic cubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY},
173 {a[3].fX, a[3].fY}};
174 double x, y;
175 xy_at_t(cubic, t, x, y);
176 out->fX = SkDoubleToScalar(x);
177 out->fY = SkDoubleToScalar(y);
178}
179
180static void (* const SegmentXYAtT[])(const SkPoint [], double , SkPoint* ) = {
181 NULL,
182 LineXYAtT,
183 QuadXYAtT,
184 CubicXYAtT
185};
186
187static SkScalar LineXAtT(const SkPoint a[2], double t) {
188 const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}};
189 double x;
190 xy_at_t(aLine, t, x, *(double*) 0);
191 return SkDoubleToScalar(x);
192}
193
194static SkScalar QuadXAtT(const SkPoint a[3], double t) {
195 const Quadratic quad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}};
196 double x;
197 xy_at_t(quad, t, x, *(double*) 0);
198 return SkDoubleToScalar(x);
199}
200
201static SkScalar CubicXAtT(const SkPoint a[4], double t) {
202 const Cubic cubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY},
203 {a[3].fX, a[3].fY}};
204 double x;
205 xy_at_t(cubic, t, x, *(double*) 0);
206 return SkDoubleToScalar(x);
207}
208
209static SkScalar (* const SegmentXAtT[])(const SkPoint [], double ) = {
210 NULL,
211 LineXAtT,
212 QuadXAtT,
213 CubicXAtT
214};
215
216static SkScalar LineYAtT(const SkPoint a[2], double t) {
217 const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}};
218 double y;
219 xy_at_t(aLine, t, *(double*) 0, y);
220 return SkDoubleToScalar(y);
221}
222
223static SkScalar QuadYAtT(const SkPoint a[3], double t) {
224 const Quadratic quad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}};
225 double y;
226 xy_at_t(quad, t, *(double*) 0, y);
227 return SkDoubleToScalar(y);
228}
229
230static SkScalar CubicYAtT(const SkPoint a[4], double t) {
231 const Cubic cubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY},
232 {a[3].fX, a[3].fY}};
233 double y;
234 xy_at_t(cubic, t, *(double*) 0, y);
235 return SkDoubleToScalar(y);
236}
237
238static SkScalar (* const SegmentYAtT[])(const SkPoint [], double ) = {
239 NULL,
240 LineYAtT,
241 QuadYAtT,
242 CubicYAtT
243};
244
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000245static SkScalar LineDXAtT(const SkPoint a[2], double ) {
246 return a[1].fX - a[0].fX;
247}
248
249static SkScalar QuadDXAtT(const SkPoint a[3], double t) {
250 const Quadratic quad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}};
251 double x;
252 dxdy_at_t(quad, t, x, *(double*) 0);
253 return SkDoubleToScalar(x);
254}
255
256static SkScalar CubicDXAtT(const SkPoint a[4], double t) {
257 const Cubic cubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY},
258 {a[3].fX, a[3].fY}};
259 double x;
260 dxdy_at_t(cubic, t, x, *(double*) 0);
261 return SkDoubleToScalar(x);
262}
263
264static SkScalar (* const SegmentDXAtT[])(const SkPoint [], double ) = {
265 NULL,
266 LineDXAtT,
267 QuadDXAtT,
268 CubicDXAtT
269};
270
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000271static void LineSubDivide(const SkPoint a[2], double startT, double endT,
272 SkPoint sub[2]) {
273 const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}};
274 _Line dst;
275 sub_divide(aLine, startT, endT, dst);
276 sub[0].fX = SkDoubleToScalar(dst[0].x);
277 sub[0].fY = SkDoubleToScalar(dst[0].y);
278 sub[1].fX = SkDoubleToScalar(dst[1].x);
279 sub[1].fY = SkDoubleToScalar(dst[1].y);
280}
281
282static void QuadSubDivide(const SkPoint a[3], double startT, double endT,
283 SkPoint sub[3]) {
284 const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY},
285 {a[2].fX, a[2].fY}};
286 Quadratic dst;
287 sub_divide(aQuad, startT, endT, dst);
288 sub[0].fX = SkDoubleToScalar(dst[0].x);
289 sub[0].fY = SkDoubleToScalar(dst[0].y);
290 sub[1].fX = SkDoubleToScalar(dst[1].x);
291 sub[1].fY = SkDoubleToScalar(dst[1].y);
292 sub[2].fX = SkDoubleToScalar(dst[2].x);
293 sub[2].fY = SkDoubleToScalar(dst[2].y);
294}
295
296static void CubicSubDivide(const SkPoint a[4], double startT, double endT,
297 SkPoint sub[4]) {
298 const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY},
299 {a[2].fX, a[2].fY}, {a[3].fX, a[3].fY}};
300 Cubic dst;
301 sub_divide(aCubic, startT, endT, dst);
302 sub[0].fX = SkDoubleToScalar(dst[0].x);
303 sub[0].fY = SkDoubleToScalar(dst[0].y);
304 sub[1].fX = SkDoubleToScalar(dst[1].x);
305 sub[1].fY = SkDoubleToScalar(dst[1].y);
306 sub[2].fX = SkDoubleToScalar(dst[2].x);
307 sub[2].fY = SkDoubleToScalar(dst[2].y);
308 sub[3].fX = SkDoubleToScalar(dst[3].x);
309 sub[3].fY = SkDoubleToScalar(dst[3].y);
310}
311
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000312static void (* const SegmentSubDivide[])(const SkPoint [], double , double ,
313 SkPoint []) = {
314 NULL,
315 LineSubDivide,
316 QuadSubDivide,
317 CubicSubDivide
318};
319
caryclark@google.com65f9f0a2012-05-23 18:09:25 +0000320#if DEBUG_UNUSED
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000321static void QuadSubBounds(const SkPoint a[3], double startT, double endT,
322 SkRect& bounds) {
323 SkPoint dst[3];
324 QuadSubDivide(a, startT, endT, dst);
325 bounds.fLeft = bounds.fRight = dst[0].fX;
326 bounds.fTop = bounds.fBottom = dst[0].fY;
327 for (int index = 1; index < 3; ++index) {
328 bounds.growToInclude(dst[index].fX, dst[index].fY);
329 }
330}
331
332static void CubicSubBounds(const SkPoint a[4], double startT, double endT,
333 SkRect& bounds) {
334 SkPoint dst[4];
335 CubicSubDivide(a, startT, endT, dst);
336 bounds.fLeft = bounds.fRight = dst[0].fX;
337 bounds.fTop = bounds.fBottom = dst[0].fY;
338 for (int index = 1; index < 4; ++index) {
339 bounds.growToInclude(dst[index].fX, dst[index].fY);
340 }
341}
caryclark@google.com65f9f0a2012-05-23 18:09:25 +0000342#endif
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000343
caryclark@google.com15fa1382012-05-07 20:49:36 +0000344static SkPath::Verb QuadReduceOrder(const SkPoint a[3],
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000345 SkTDArray<SkPoint>& reducePts) {
346 const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY},
347 {a[2].fX, a[2].fY}};
348 Quadratic dst;
349 int order = reduceOrder(aQuad, dst);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000350 if (order == 3) {
351 return SkPath::kQuad_Verb;
352 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000353 for (int index = 0; index < order; ++index) {
354 SkPoint* pt = reducePts.append();
355 pt->fX = SkDoubleToScalar(dst[index].x);
356 pt->fY = SkDoubleToScalar(dst[index].y);
357 }
358 return (SkPath::Verb) (order - 1);
359}
360
361static SkPath::Verb CubicReduceOrder(const SkPoint a[4],
362 SkTDArray<SkPoint>& reducePts) {
363 const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY},
364 {a[2].fX, a[2].fY}, {a[3].fX, a[3].fY}};
365 Cubic dst;
366 int order = reduceOrder(aCubic, dst, kReduceOrder_QuadraticsAllowed);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000367 if (order == 4) {
368 return SkPath::kCubic_Verb;
369 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000370 for (int index = 0; index < order; ++index) {
371 SkPoint* pt = reducePts.append();
372 pt->fX = SkDoubleToScalar(dst[index].x);
373 pt->fY = SkDoubleToScalar(dst[index].y);
374 }
375 return (SkPath::Verb) (order - 1);
376}
377
caryclark@google.com15fa1382012-05-07 20:49:36 +0000378static bool QuadIsLinear(const SkPoint a[3]) {
379 const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY},
380 {a[2].fX, a[2].fY}};
381 return isLinear(aQuad, 0, 2);
382}
383
384static bool CubicIsLinear(const SkPoint a[4]) {
385 const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY},
386 {a[2].fX, a[2].fY}, {a[3].fX, a[3].fY}};
387 return isLinear(aCubic, 0, 3);
388}
389
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000390static SkScalar LineLeftMost(const SkPoint a[2], double startT, double endT) {
391 const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}};
392 double x[2];
393 xy_at_t(aLine, startT, x[0], *(double*) 0);
caryclark@google.com495f8e42012-05-31 13:13:11 +0000394 xy_at_t(aLine, endT, x[1], *(double*) 0);
395 return SkMinScalar((float) x[0], (float) x[1]);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000396}
397
398static SkScalar QuadLeftMost(const SkPoint a[3], double startT, double endT) {
399 const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY},
400 {a[2].fX, a[2].fY}};
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000401 return (float) leftMostT(aQuad, startT, endT);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000402}
403
404static SkScalar CubicLeftMost(const SkPoint a[4], double startT, double endT) {
405 const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY},
406 {a[2].fX, a[2].fY}, {a[3].fX, a[3].fY}};
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000407 return (float) leftMostT(aCubic, startT, endT);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000408}
409
410static SkScalar (* const SegmentLeftMost[])(const SkPoint [], double , double) = {
411 NULL,
412 LineLeftMost,
413 QuadLeftMost,
414 CubicLeftMost
415};
416
caryclark@google.com65f9f0a2012-05-23 18:09:25 +0000417#if DEBUG_UNUSED
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000418static bool IsCoincident(const SkPoint a[2], const SkPoint& above,
419 const SkPoint& below) {
420 const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}};
421 const _Line bLine = {{above.fX, above.fY}, {below.fX, below.fY}};
422 return implicit_matches_ulps(aLine, bLine, 32);
423}
caryclark@google.com65f9f0a2012-05-23 18:09:25 +0000424#endif
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000425
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000426class Segment;
427
caryclark@google.com15fa1382012-05-07 20:49:36 +0000428// sorting angles
429// given angles of {dx dy ddx ddy dddx dddy} sort them
430class Angle {
431public:
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000432 // FIXME: this is bogus for quads and cubics
433 // if the quads and cubics' line from end pt to ctrl pt are coincident,
434 // there's no obvious way to determine the curve ordering from the
435 // derivatives alone. In particular, if one quadratic's coincident tangent
436 // is longer than the other curve, the final control point can place the
437 // longer curve on either side of the shorter one.
438 // Using Bezier curve focus http://cagd.cs.byu.edu/~tom/papers/bezclip.pdf
439 // may provide some help, but nothing has been figured out yet.
caryclark@google.com15fa1382012-05-07 20:49:36 +0000440 bool operator<(const Angle& rh) const {
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000441 if ((fDy < 0) ^ (rh.fDy < 0)) {
442 return fDy < 0;
caryclark@google.com15fa1382012-05-07 20:49:36 +0000443 }
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000444 if (fDy == 0 && rh.fDy == 0 && fDx != rh.fDx) {
445 return fDx < rh.fDx;
446 }
447 SkScalar cmp = fDx * rh.fDy - rh.fDx * fDy;
caryclark@google.com15fa1382012-05-07 20:49:36 +0000448 if (cmp) {
449 return cmp < 0;
450 }
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000451 if ((fDDy < 0) ^ (rh.fDDy < 0)) {
452 return fDDy < 0;
caryclark@google.com15fa1382012-05-07 20:49:36 +0000453 }
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000454 if (fDDy == 0 && rh.fDDy == 0 && fDDx != rh.fDDx) {
455 return fDDx < rh.fDDx;
456 }
457 cmp = fDDx * rh.fDDy - rh.fDDx * fDDy;
caryclark@google.com15fa1382012-05-07 20:49:36 +0000458 if (cmp) {
459 return cmp < 0;
460 }
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000461 if ((fDDDy < 0) ^ (rh.fDDDy < 0)) {
462 return fDDDy < 0;
caryclark@google.com15fa1382012-05-07 20:49:36 +0000463 }
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000464 if (fDDDy == 0 && rh.fDDDy == 0) {
465 return fDDDx < rh.fDDDx;
466 }
467 return fDDDx * rh.fDDDy < rh.fDDDx * fDDDy;
caryclark@google.com15fa1382012-05-07 20:49:36 +0000468 }
469
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000470 int end() const {
471 return fEnd;
472 }
473
caryclark@google.com88f7d0c2012-06-07 21:09:20 +0000474 bool isHorizontal() const {
475 return fDy == 0 && fDDy == 0 && fDDDy == 0;
476 }
477
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000478 // since all angles share a point, this needs to know which point
479 // is the common origin, i.e., whether the center is at pts[0] or pts[verb]
480 // practically, this should only be called by addAngle
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000481 void set(const SkPoint* pts, SkPath::Verb verb, const Segment* segment,
caryclark@google.coma3f05fa2012-06-01 17:44:28 +0000482 int start, int end) {
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000483 SkASSERT(start != end);
484 fSegment = segment;
485 fStart = start;
486 fEnd = end;
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000487 fDx = pts[1].fX - pts[0].fX; // b - a
488 fDy = pts[1].fY - pts[0].fY;
caryclark@google.com15fa1382012-05-07 20:49:36 +0000489 if (verb == SkPath::kLine_Verb) {
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000490 fDDx = fDDy = fDDDx = fDDDy = 0;
caryclark@google.com15fa1382012-05-07 20:49:36 +0000491 return;
492 }
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000493 fDDx = pts[2].fX - pts[1].fX - fDx; // a - 2b + c
494 fDDy = pts[2].fY - pts[1].fY - fDy;
caryclark@google.com15fa1382012-05-07 20:49:36 +0000495 if (verb == SkPath::kQuad_Verb) {
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000496 fDDDx = fDDDy = 0;
caryclark@google.com15fa1382012-05-07 20:49:36 +0000497 return;
498 }
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000499 fDDDx = pts[3].fX + 3 * (pts[1].fX - pts[2].fX) - pts[0].fX;
500 fDDDy = pts[3].fY + 3 * (pts[1].fY - pts[2].fY) - pts[0].fY;
501 }
502
503 // noncoincident quads/cubics may have the same initial angle
504 // as lines, so must sort by derivatives as well
505 // if flatness turns out to be a reasonable way to sort, use the below:
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000506 void setFlat(const SkPoint* pts, SkPath::Verb verb, Segment* segment,
caryclark@google.coma3f05fa2012-06-01 17:44:28 +0000507 int start, int end) {
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000508 fSegment = segment;
509 fStart = start;
510 fEnd = end;
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000511 fDx = pts[1].fX - pts[0].fX; // b - a
512 fDy = pts[1].fY - pts[0].fY;
513 if (verb == SkPath::kLine_Verb) {
514 fDDx = fDDy = fDDDx = fDDDy = 0;
515 return;
516 }
517 if (verb == SkPath::kQuad_Verb) {
518 int uplsX = FloatAsInt(pts[2].fX - pts[1].fY - fDx);
519 int uplsY = FloatAsInt(pts[2].fY - pts[1].fY - fDy);
520 int larger = std::max(abs(uplsX), abs(uplsY));
521 int shift = 0;
522 double flatT;
523 SkPoint ddPt; // FIXME: get rid of copy (change fDD_ to point)
524 LineParameters implicitLine;
525 _Line tangent = {{pts[0].fX, pts[0].fY}, {pts[1].fX, pts[1].fY}};
526 implicitLine.lineEndPoints(tangent);
527 implicitLine.normalize();
528 while (larger > UlpsEpsilon * 1024) {
529 larger >>= 2;
530 ++shift;
531 flatT = 0.5 / (1 << shift);
532 QuadXYAtT(pts, flatT, &ddPt);
533 _Point _pt = {ddPt.fX, ddPt.fY};
534 double distance = implicitLine.pointDistance(_pt);
535 if (approximately_zero(distance)) {
536 SkDebugf("%s ulps too small %1.9g\n", __FUNCTION__, distance);
537 break;
538 }
539 }
540 flatT = 0.5 / (1 << shift);
541 QuadXYAtT(pts, flatT, &ddPt);
542 fDDx = ddPt.fX - pts[0].fX;
543 fDDy = ddPt.fY - pts[0].fY;
544 SkASSERT(fDDx != 0 || fDDy != 0);
545 fDDDx = fDDDy = 0;
546 return;
547 }
548 SkASSERT(0); // FIXME: add cubic case
549 }
550
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000551 Segment* segment() const {
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000552 return const_cast<Segment*>(fSegment);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000553 }
554
555 int sign() const {
caryclark@google.com495f8e42012-05-31 13:13:11 +0000556 return SkSign32(fStart - fEnd);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000557 }
caryclark@google.coma3f05fa2012-06-01 17:44:28 +0000558
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000559 int start() const {
560 return fStart;
caryclark@google.com15fa1382012-05-07 20:49:36 +0000561 }
562
563private:
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000564 SkScalar fDx;
565 SkScalar fDy;
566 SkScalar fDDx;
567 SkScalar fDDy;
568 SkScalar fDDDx;
569 SkScalar fDDDy;
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000570 const Segment* fSegment;
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000571 int fStart;
572 int fEnd;
caryclark@google.com15fa1382012-05-07 20:49:36 +0000573};
574
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000575static void sortAngles(SkTDArray<Angle>& angles, SkTDArray<Angle*>& angleList) {
576 int angleCount = angles.count();
577 int angleIndex;
578 angleList.setReserve(angleCount);
579 for (angleIndex = 0; angleIndex < angleCount; ++angleIndex) {
580 *angleList.append() = &angles[angleIndex];
581 }
582 QSort<Angle>(angleList.begin(), angleList.end() - 1);
583}
584
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000585// Bounds, unlike Rect, does not consider a line to be empty.
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000586struct Bounds : public SkRect {
587 static bool Intersects(const Bounds& a, const Bounds& b) {
588 return a.fLeft <= b.fRight && b.fLeft <= a.fRight &&
589 a.fTop <= b.fBottom && b.fTop <= a.fBottom;
590 }
591
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000592 void add(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) {
593 if (left < fLeft) {
594 fLeft = left;
595 }
596 if (top < fTop) {
597 fTop = top;
598 }
599 if (right > fRight) {
600 fRight = right;
601 }
602 if (bottom > fBottom) {
603 fBottom = bottom;
604 }
605 }
606
607 void add(const Bounds& toAdd) {
608 add(toAdd.fLeft, toAdd.fTop, toAdd.fRight, toAdd.fBottom);
609 }
610
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000611 bool isEmpty() {
612 return fLeft > fRight || fTop > fBottom
613 || fLeft == fRight && fTop == fBottom
614 || isnan(fLeft) || isnan(fRight)
615 || isnan(fTop) || isnan(fBottom);
616 }
617
618 void setCubicBounds(const SkPoint a[4]) {
619 _Rect dRect;
620 Cubic cubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY},
621 {a[2].fX, a[2].fY}, {a[3].fX, a[3].fY}};
622 dRect.setBounds(cubic);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000623 set((float) dRect.left, (float) dRect.top, (float) dRect.right,
624 (float) dRect.bottom);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000625 }
626
627 void setQuadBounds(const SkPoint a[3]) {
628 const Quadratic quad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY},
629 {a[2].fX, a[2].fY}};
630 _Rect dRect;
631 dRect.setBounds(quad);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000632 set((float) dRect.left, (float) dRect.top, (float) dRect.right,
633 (float) dRect.bottom);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000634 }
635};
636
caryclark@google.com15fa1382012-05-07 20:49:36 +0000637struct Span {
caryclark@google.coma833b5c2012-04-30 19:38:50 +0000638 Segment* fOther;
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000639 mutable SkPoint const* fPt; // lazily computed as needed
640 double fT;
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000641 double fOtherT; // value at fOther[fOtherIndex].fT
642 int fOtherIndex; // can't be used during intersection
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000643 int fWindSum; // accumulated from contours surrounding this one
644 int fWindValue; // 0 == canceled; 1 == normal; >1 == coincident
caryclark@google.com65f9f0a2012-05-23 18:09:25 +0000645 bool fDone; // if set, this span to next higher T has been processed
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000646};
647
648class Segment {
649public:
650 Segment() {
651#if DEBUG_DUMP
652 fID = ++gSegmentID;
653#endif
654 }
caryclark@google.comfa4a6e92012-07-11 17:52:32 +0000655
caryclark@google.com9764cc62012-07-12 19:29:45 +0000656 bool activeAngle(int index, int& done, SkTDArray<Angle>& angles) const {
657 if (activeAngleInner(index, done, angles)) {
658 return true;
659 }
caryclark@google.comfa4a6e92012-07-11 17:52:32 +0000660 double referenceT = fTs[index].fT;
661 int lesser = index;
662 while (--lesser >= 0 && referenceT - fTs[lesser].fT < FLT_EPSILON) {
caryclark@google.com9764cc62012-07-12 19:29:45 +0000663 if (activeAngleOther(lesser, done, angles)) {
caryclark@google.comfa4a6e92012-07-11 17:52:32 +0000664 return true;
665 }
666 }
667 do {
caryclark@google.com9764cc62012-07-12 19:29:45 +0000668 if (activeAngleOther(index, done, angles)) {
caryclark@google.comfa4a6e92012-07-11 17:52:32 +0000669 return true;
670 }
671 } while (++index < fTs.count() && fTs[index].fT - referenceT < FLT_EPSILON);
672 return false;
673 }
674
caryclark@google.com9764cc62012-07-12 19:29:45 +0000675 bool activeAngleOther(int index, int& done, SkTDArray<Angle>& angles) const {
caryclark@google.comfa4a6e92012-07-11 17:52:32 +0000676 Span* span = &fTs[index];
677 Segment* other = span->fOther;
678 int oIndex = span->fOtherIndex;
caryclark@google.com9764cc62012-07-12 19:29:45 +0000679 return other->activeAngleInner(oIndex, done, angles);
680 }
681
682 bool activeAngleInner(int index, int& done, SkTDArray<Angle>& angles) const {
683 int next = nextSpan(index, 1);
684 if (next > 0) {
caryclark@google.com9764cc62012-07-12 19:29:45 +0000685 const Span& upSpan = fTs[index];
caryclark@google.com210acaf2012-07-12 21:05:13 +0000686 if (upSpan.fWindValue) {
687 addAngle(angles, index, next);
688 if (upSpan.fDone) {
689 done++;
690 } else if (upSpan.fWindSum != SK_MinS32) {
691 return true;
692 }
caryclark@google.com9764cc62012-07-12 19:29:45 +0000693 }
caryclark@google.comfa4a6e92012-07-11 17:52:32 +0000694 }
caryclark@google.com9764cc62012-07-12 19:29:45 +0000695 int prev = nextSpan(index, -1);
caryclark@google.comfa4a6e92012-07-11 17:52:32 +0000696 // edge leading into junction
caryclark@google.com9764cc62012-07-12 19:29:45 +0000697 if (prev >= 0) {
caryclark@google.com9764cc62012-07-12 19:29:45 +0000698 const Span& downSpan = fTs[prev];
caryclark@google.com210acaf2012-07-12 21:05:13 +0000699 if (downSpan.fWindValue) {
700 addAngle(angles, index, prev);
701 if (downSpan.fDone) {
702 done++;
703 } else if (downSpan.fWindSum != SK_MinS32) {
704 return true;
705 }
caryclark@google.com9764cc62012-07-12 19:29:45 +0000706 }
707 }
708 return false;
caryclark@google.comfa4a6e92012-07-11 17:52:32 +0000709 }
710
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000711 SkScalar activeTop() const {
712 SkASSERT(!done());
713 int count = fTs.count();
714 SkScalar result = SK_ScalarMax;
715 bool lastDone = true;
716 for (int index = 0; index < count; ++index) {
717 bool done = fTs[index].fDone;
718 if (!done || !lastDone) {
719 SkScalar y = yAtT(index);
720 if (result > y) {
721 result = y;
722 }
723 }
724 lastDone = done;
725 }
726 SkASSERT(result < SK_ScalarMax);
727 return result;
728 }
729
730 void addAngle(SkTDArray<Angle>& angles, int start, int end) const {
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000731 SkASSERT(start != end);
732 SkPoint edge[4];
733 (*SegmentSubDivide[fVerb])(fPts, fTs[start].fT, fTs[end].fT, edge);
734 Angle* angle = angles.append();
caryclark@google.coma3f05fa2012-06-01 17:44:28 +0000735 angle->set(edge, fVerb, this, start, end);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000736 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +0000737
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000738 void addCubic(const SkPoint pts[4]) {
739 init(pts, SkPath::kCubic_Verb);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000740 fBounds.setCubicBounds(pts);
741 }
742
caryclark@google.com88f7d0c2012-06-07 21:09:20 +0000743 // FIXME: this needs to defer add for aligned consecutive line segments
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000744 SkPoint addCurveTo(int start, int end, SkPath& path, bool active) {
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000745 SkPoint edge[4];
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000746 // OPTIMIZE? if not active, skip remainder and return xy_at_t(end)
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000747 (*SegmentSubDivide[fVerb])(fPts, fTs[start].fT, fTs[end].fT, edge);
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000748 if (active) {
caryclark@google.com65f9f0a2012-05-23 18:09:25 +0000749 #if DEBUG_PATH_CONSTRUCTION
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000750 SkDebugf("%s %s (%1.9g,%1.9g)", __FUNCTION__,
751 kLVerbStr[fVerb], edge[1].fX, edge[1].fY);
752 if (fVerb > 1) {
753 SkDebugf(" (%1.9g,%1.9g)", edge[2].fX, edge[2].fY);
754 }
755 if (fVerb > 2) {
756 SkDebugf(" (%1.9g,%1.9g)", edge[3].fX, edge[3].fY);
757 }
758 SkDebugf("\n");
caryclark@google.com65f9f0a2012-05-23 18:09:25 +0000759 #endif
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000760 switch (fVerb) {
761 case SkPath::kLine_Verb:
762 path.lineTo(edge[1].fX, edge[1].fY);
763 break;
764 case SkPath::kQuad_Verb:
765 path.quadTo(edge[1].fX, edge[1].fY, edge[2].fX, edge[2].fY);
766 break;
767 case SkPath::kCubic_Verb:
768 path.cubicTo(edge[1].fX, edge[1].fY, edge[2].fX, edge[2].fY,
769 edge[3].fX, edge[3].fY);
770 break;
771 }
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000772 }
caryclark@google.com88f7d0c2012-06-07 21:09:20 +0000773 return edge[fVerb];
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000774 }
775
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000776 void addLine(const SkPoint pts[2]) {
777 init(pts, SkPath::kLine_Verb);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000778 fBounds.set(pts, 2);
779 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +0000780
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000781 const SkPoint& addMoveTo(int tIndex, SkPath& path, bool active) {
782 const SkPoint& pt = xyAtT(tIndex);
783 if (active) {
caryclark@google.com65f9f0a2012-05-23 18:09:25 +0000784 #if DEBUG_PATH_CONSTRUCTION
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000785 SkDebugf("%s (%1.9g,%1.9g)\n", __FUNCTION__, pt.fX, pt.fY);
caryclark@google.com65f9f0a2012-05-23 18:09:25 +0000786 #endif
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000787 path.moveTo(pt.fX, pt.fY);
788 }
caryclark@google.com88f7d0c2012-06-07 21:09:20 +0000789 return pt;
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000790 }
791
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000792 // add 2 to edge or out of range values to get T extremes
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000793 void addOtherT(int index, double otherT, int otherIndex) {
794 Span& span = fTs[index];
795 span.fOtherT = otherT;
796 span.fOtherIndex = otherIndex;
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000797 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +0000798
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000799 void addQuad(const SkPoint pts[3]) {
800 init(pts, SkPath::kQuad_Verb);
caryclark@google.comfa0588f2012-04-26 21:01:06 +0000801 fBounds.setQuadBounds(pts);
802 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000803
804 // Defer all coincident edge processing until
805 // after normal intersections have been computed
caryclark@google.coma833b5c2012-04-30 19:38:50 +0000806
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000807// no need to be tricky; insert in normal T order
808// resolve overlapping ts when considering coincidence later
809
810 // add non-coincident intersection. Resulting edges are sorted in T.
811 int addT(double newT, Segment* other) {
caryclark@google.com15fa1382012-05-07 20:49:36 +0000812 // FIXME: in the pathological case where there is a ton of intercepts,
813 // binary search?
814 int insertedAt = -1;
caryclark@google.com15fa1382012-05-07 20:49:36 +0000815 size_t tCount = fTs.count();
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000816 for (size_t index = 0; index < tCount; ++index) {
caryclark@google.com15fa1382012-05-07 20:49:36 +0000817 // OPTIMIZATION: if there are three or more identical Ts, then
818 // the fourth and following could be further insertion-sorted so
819 // that all the edges are clockwise or counterclockwise.
820 // This could later limit segment tests to the two adjacent
821 // neighbors, although it doesn't help with determining which
822 // circular direction to go in.
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000823 if (newT < fTs[index].fT) {
824 insertedAt = index;
825 break;
caryclark@google.com15fa1382012-05-07 20:49:36 +0000826 }
827 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000828 Span* span;
829 if (insertedAt >= 0) {
830 span = fTs.insert(insertedAt);
831 } else {
832 insertedAt = tCount;
833 span = fTs.append();
834 }
caryclark@google.com15fa1382012-05-07 20:49:36 +0000835 span->fT = newT;
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000836 span->fOther = other;
caryclark@google.coma3f05fa2012-06-01 17:44:28 +0000837 span->fPt = NULL;
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000838 span->fWindSum = SK_MinS32;
839 span->fWindValue = 1;
840 if ((span->fDone = newT == 1)) {
caryclark@google.com65f9f0a2012-05-23 18:09:25 +0000841 ++fDoneSpans;
842 }
caryclark@google.com15fa1382012-05-07 20:49:36 +0000843 return insertedAt;
844 }
845
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000846 // set spans from start to end to decrement by one
847 // note this walks other backwards
848 // FIMXE: there's probably an edge case that can be constructed where
849 // two span in one segment are separated by float epsilon on one span but
850 // not the other, if one segment is very small. For this
851 // case the counts asserted below may or may not be enough to separate the
852 // spans. Even if the counts work out, what if the spanw aren't correctly
853 // sorted? It feels better in such a case to match the span's other span
854 // pointer since both coincident segments must contain the same spans.
855 void addTCancel(double startT, double endT, Segment& other,
856 double oStartT, double oEndT) {
857 SkASSERT(endT - startT >= FLT_EPSILON);
858 SkASSERT(oEndT - oStartT >= FLT_EPSILON);
859 int index = 0;
860 while (startT - fTs[index].fT >= FLT_EPSILON) {
861 ++index;
862 }
caryclark@google.comb9738012012-07-03 19:53:30 +0000863 int oIndex = other.fTs.count();
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000864 while (other.fTs[--oIndex].fT - oEndT > -FLT_EPSILON)
865 ;
866 Span* test = &fTs[index];
867 Span* oTest = &other.fTs[oIndex];
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000868 do {
869 bool decrement = test->fWindValue && oTest->fWindValue;
870 Span* end = test;
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000871 do {
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000872 if (decrement) {
caryclark@google.comb9738012012-07-03 19:53:30 +0000873 SkASSERT(end->fWindValue > 0);
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000874 if (--(end->fWindValue) == 0) {
875 end->fDone = true;
876 ++fDoneSpans;
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000877 }
878 }
879 end = &fTs[++index];
880 } while (end->fT - test->fT < FLT_EPSILON);
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000881 Span* oTestStart = oTest;
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000882 do {
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000883 if (decrement) {
caryclark@google.comb9738012012-07-03 19:53:30 +0000884 SkASSERT(oTestStart->fWindValue > 0);
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000885 if (--(oTestStart->fWindValue) == 0) {
886 oTestStart->fDone = true;
887 ++other.fDoneSpans;
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000888 }
889 }
890 if (!oIndex) {
891 break;
892 }
893 oTestStart = &other.fTs[--oIndex];
894 } while (oTest->fT - oTestStart->fT < FLT_EPSILON);
895 test = end;
896 oTest = oTestStart;
897 } while (test->fT < endT - FLT_EPSILON);
898 SkASSERT(!oIndex || oTest->fT <= oStartT - FLT_EPSILON);
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000899 }
900
901 // set spans from start to end to increment the greater by one and decrement
902 // the lesser
903 void addTCoincident(double startT, double endT, Segment& other,
904 double oStartT, double oEndT) {
905 SkASSERT(endT - startT >= FLT_EPSILON);
906 SkASSERT(oEndT - oStartT >= FLT_EPSILON);
907 int index = 0;
908 while (startT - fTs[index].fT >= FLT_EPSILON) {
909 ++index;
910 }
911 int oIndex = 0;
912 while (oStartT - other.fTs[oIndex].fT >= FLT_EPSILON) {
913 ++oIndex;
914 }
915 Span* test = &fTs[index];
916 Span* oTest = &other.fTs[oIndex];
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000917 SkTDArray<double> outsideTs;
918 SkTDArray<double> oOutsideTs;
919 do {
caryclark@google.comb9738012012-07-03 19:53:30 +0000920 bool transfer = test->fWindValue && oTest->fWindValue;
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000921 bool decrementOther = test->fWindValue >= oTest->fWindValue;
922 Span* end = test;
923 double startT = end->fT;
924 double oStartT = oTest->fT;
925 do {
caryclark@google.comb9738012012-07-03 19:53:30 +0000926 if (transfer) {
927 if (decrementOther) {
928 ++(end->fWindValue);
929 } else {
930 SkASSERT(end->fWindValue > 0);
931 if (--(end->fWindValue) == 0) {
932 end->fDone = true;
933 ++fDoneSpans;
934 int outCount = outsideTs.count();
935 if (outCount == 0 || end->fT - outsideTs[outCount - 2]
936 >= FLT_EPSILON) {
937 *outsideTs.append() = end->fT;
938 *outsideTs.append() = oStartT;
939 }
940 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000941 }
942 }
943 end = &fTs[++index];
944 } while (end->fT - test->fT < FLT_EPSILON);
945 Span* oEnd = oTest;
946 do {
caryclark@google.comb9738012012-07-03 19:53:30 +0000947 if (transfer) {
948 if (decrementOther) {
949 SkASSERT(oEnd->fWindValue > 0);
950 if (--(oEnd->fWindValue) == 0) {
951 oEnd->fDone = true;
952 ++other.fDoneSpans;
953 int oOutCount = oOutsideTs.count();
954 if (oOutCount == 0 || oEnd->fT
955 - oOutsideTs[oOutCount - 2] >= FLT_EPSILON) {
956 *oOutsideTs.append() = oEnd->fT;
957 *oOutsideTs.append() = startT;
958 }
959 }
960 } else {
961 ++(oEnd->fWindValue);
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000962 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000963 }
964 oEnd = &other.fTs[++oIndex];
965 } while (oEnd->fT - oTest->fT < FLT_EPSILON);
966 test = end;
967 oTest = oEnd;
968 } while (test->fT < endT - FLT_EPSILON);
969 SkASSERT(oTest->fT < oEndT + FLT_EPSILON);
970 SkASSERT(oTest->fT > oEndT - FLT_EPSILON);
971 if (!done() && outsideTs.count()) {
caryclark@google.comb9738012012-07-03 19:53:30 +0000972 addTOutsides(outsideTs, other, oEndT);
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000973 }
974 if (!other.done() && oOutsideTs.count()) {
caryclark@google.comb9738012012-07-03 19:53:30 +0000975 other.addTOutsides(oOutsideTs, *this, endT);
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000976 }
977 }
978
caryclark@google.comb9738012012-07-03 19:53:30 +0000979 void addTOutsides(const SkTDArray<double>& outsideTs, Segment& other,
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000980 double otherEnd) {
981 int count = outsideTs.count();
982 double endT = 0;
983 int endSpan = 0;
984 for (int index = 0; index < count; index += 2) {
985 double t = outsideTs[index];
986 double otherT = outsideTs[index + 1];
987 if (t > 1 - FLT_EPSILON) {
988 return;
989 }
990 if (t - endT > FLT_EPSILON) {
caryclark@google.comfa4a6e92012-07-11 17:52:32 +0000991 endSpan = addTDonePair(t, other, otherT);
caryclark@google.com8dcf1142012-07-02 20:27:02 +0000992 }
993 do {
994 endT = fTs[++endSpan].fT;
995 } while (endT - t < FLT_EPSILON);
996 }
997 addTPair(endT, other, otherEnd);
998 }
999
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00001000 // match the other.fWindValue to its mates
1001 int addTDonePair(double t, Segment& other, double otherT) {
1002 int insertedAt = addTPair(t, other, otherT);
1003 Span& end = fTs[insertedAt];
1004 SkASSERT(end.fWindValue == 1);
1005 end.fWindValue = 0;
1006 end.fDone = true;
1007 ++fDoneSpans;
1008 Span& otherEnd = other.fTs[end.fOtherIndex];
1009 Span* match = NULL;
1010 if (end.fOtherIndex > 0) {
1011 match = &other.fTs[end.fOtherIndex - 1];
1012 }
1013 if (!match || match->fT < otherT) {
1014 match = &other.fTs[end.fOtherIndex + 1];
1015 }
1016 otherEnd.fWindValue = match->fWindValue;
1017 return insertedAt;
1018 }
1019
caryclark@google.comb9738012012-07-03 19:53:30 +00001020 int addTPair(double t, Segment& other, double otherT) {
1021 int insertedAt = addT(t, &other);
1022 int otherInsertedAt = other.addT(otherT, this);
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001023 addOtherT(insertedAt, otherT, otherInsertedAt);
caryclark@google.comb9738012012-07-03 19:53:30 +00001024 other.addOtherT(otherInsertedAt, t, insertedAt);
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001025 return insertedAt;
1026 }
1027
1028 void addTwoAngles(int start, int end, SkTDArray<Angle>& angles) const {
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001029 // add edge leading into junction
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001030 if (fTs[SkMin32(end, start)].fWindValue > 0) {
1031 addAngle(angles, end, start);
1032 }
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001033 // add edge leading away from junction
caryclark@google.com495f8e42012-05-31 13:13:11 +00001034 int step = SkSign32(end - start);
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001035 int tIndex = nextSpan(end, step);
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001036 if (tIndex >= 0 && fTs[SkMin32(end, tIndex)].fWindValue > 0) {
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001037 addAngle(angles, end, tIndex);
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001038 }
1039 }
1040
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001041 const Bounds& bounds() const {
1042 return fBounds;
1043 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +00001044
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001045 void buildAngles(int index, SkTDArray<Angle>& angles) const {
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001046 double referenceT = fTs[index].fT;
1047 int lesser = index;
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001048 while (--lesser >= 0 && referenceT - fTs[lesser].fT < FLT_EPSILON) {
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001049 buildAnglesInner(lesser, angles);
1050 }
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001051 do {
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001052 buildAnglesInner(index, angles);
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001053 } while (++index < fTs.count() && fTs[index].fT - referenceT < FLT_EPSILON);
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001054 }
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001055
1056 void buildAnglesInner(int index, SkTDArray<Angle>& angles) const {
1057 Span* span = &fTs[index];
1058 Segment* other = span->fOther;
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001059 // if there is only one live crossing, and no coincidence, continue
1060 // in the same direction
1061 // if there is coincidence, the only choice may be to reverse direction
1062 // find edge on either side of intersection
1063 int oIndex = span->fOtherIndex;
1064 // if done == -1, prior span has already been processed
1065 int step = 1;
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001066 int next = other->nextSpan(oIndex, step);
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001067 if (next < 0) {
1068 step = -step;
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001069 next = other->nextSpan(oIndex, step);
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001070 }
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001071 // add candidate into and away from junction
1072 other->addTwoAngles(next, oIndex, angles);
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001073 }
1074
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001075 bool cancels(const Segment& other) const {
caryclark@google.comb9738012012-07-03 19:53:30 +00001076 SkASSERT(fVerb == SkPath::kLine_Verb);
1077 SkASSERT(other.fVerb == SkPath::kLine_Verb);
1078 SkPoint dxy = fPts[0] - fPts[1];
1079 SkPoint odxy = other.fPts[0] - other.fPts[1];
1080 return dxy.fX * odxy.fX < 0 || dxy.fY * odxy.fY < 0;
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001081 }
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001082
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001083 // figure out if the segment's ascending T goes clockwise or not
1084 // not enough context to write this as shown
1085 // instead, add all segments meeting at the top
1086 // sort them using buildAngleList
1087 // find the first in the sort
1088 // see if ascendingT goes to top
caryclark@google.com65f9f0a2012-05-23 18:09:25 +00001089 bool clockwise(int /* tIndex */) const {
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001090 SkASSERT(0); // incomplete
1091 return false;
1092 }
1093
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001094 int crossedSpan(const SkPoint& basePt, SkScalar& bestY, double& hitT) const {
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001095 int bestT = -1;
1096 SkScalar top = bounds().fTop;
1097 SkScalar bottom = bounds().fBottom;
caryclark@google.com210acaf2012-07-12 21:05:13 +00001098 int end = 0;
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001099 do {
caryclark@google.com210acaf2012-07-12 21:05:13 +00001100 int start = end;
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001101 end = nextSpan(start, 1);
1102 SkPoint edge[4];
1103 // OPTIMIZE: wrap this so that if start==0 end==fTCount-1 we can
1104 // work with the original data directly
1105 (*SegmentSubDivide[fVerb])(fPts, fTs[start].fT, fTs[end].fT, edge);
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00001106 // intersect ray starting at basePt with edge
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001107 Intersections intersections;
1108 int pts = (*VSegmentIntersect[fVerb])(edge, top, bottom, basePt.fX,
1109 false, intersections);
1110 if (pts == 0) {
1111 continue;
caryclark@google.com495f8e42012-05-31 13:13:11 +00001112 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001113 if (pts > 1 && fVerb == SkPath::kLine_Verb) {
1114 // if the intersection is edge on, wait for another one
1115 continue;
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001116 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001117 SkASSERT(pts == 1); // FIXME: more code required to disambiguate
1118 SkPoint pt;
1119 double foundT = intersections.fT[0][0];
1120 (*SegmentXYAtT[fVerb])(fPts, foundT, &pt);
1121 if (bestY < pt.fY) {
1122 bestY = pt.fY;
1123 bestT = foundT < 1 ? start : end;
1124 hitT = foundT;
1125 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001126 } while (fTs[end].fT != 1);
1127 return bestT;
caryclark@google.com495f8e42012-05-31 13:13:11 +00001128 }
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001129
caryclark@google.com15fa1382012-05-07 20:49:36 +00001130 bool done() const {
caryclark@google.comaf46cff2012-05-22 21:12:00 +00001131 SkASSERT(fDoneSpans <= fTs.count());
1132 return fDoneSpans == fTs.count();
caryclark@google.coma833b5c2012-04-30 19:38:50 +00001133 }
1134
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001135 // so the span needs to contain the pairing info found here
1136 // this should include the winding computed for the edge, and
1137 // what edge it connects to, and whether it is discarded
1138 // (maybe discarded == abs(winding) > 1) ?
1139 // only need derivatives for duration of sorting, add a new struct
1140 // for pairings, remove extra spans that have zero length and
1141 // reference an unused other
1142 // for coincident, the last span on the other may be marked done
1143 // (always?)
1144
1145 // if loop is exhausted, contour may be closed.
1146 // FIXME: pass in close point so we can check for closure
1147
1148 // given a segment, and a sense of where 'inside' is, return the next
1149 // segment. If this segment has an intersection, or ends in multiple
1150 // segments, find the mate that continues the outside.
1151 // note that if there are multiples, but no coincidence, we can limit
1152 // choices to connections in the correct direction
1153
1154 // mark found segments as done
1155
caryclark@google.com15fa1382012-05-07 20:49:36 +00001156 // start is the index of the beginning T of this edge
1157 // it is guaranteed to have an end which describes a non-zero length (?)
1158 // winding -1 means ccw, 1 means cw
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001159 // firstFind allows coincident edges to be treated differently
caryclark@google.com5c286d32012-07-13 11:57:28 +00001160 Segment* findNext(SkTDArray<Span*>& chase, int winding,
1161 const int startIndex, const int endIndex,
1162 int& nextStart, int& nextEnd, int& flipped, bool firstFind
1163 ,bool active /* active param is debugging only */ ) {
caryclark@google.com1577e8f2012-05-22 17:01:14 +00001164 SkASSERT(startIndex != endIndex);
caryclark@google.com15fa1382012-05-07 20:49:36 +00001165 int count = fTs.count();
caryclark@google.com1577e8f2012-05-22 17:01:14 +00001166 SkASSERT(startIndex < endIndex ? startIndex < count - 1
1167 : startIndex > 0);
caryclark@google.com495f8e42012-05-31 13:13:11 +00001168 int step = SkSign32(endIndex - startIndex);
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001169 int end = nextSpan(startIndex, step);
caryclark@google.com1577e8f2012-05-22 17:01:14 +00001170 SkASSERT(end >= 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001171 Span* endSpan = &fTs[end];
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001172 Segment* other;
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001173 if (isSimple(end)) {
caryclark@google.comaf46cff2012-05-22 21:12:00 +00001174 // mark the smaller of startIndex, endIndex done, and all adjacent
1175 // spans with the same T value (but not 'other' spans)
caryclark@google.com495f8e42012-05-31 13:13:11 +00001176 markDone(SkMin32(startIndex, endIndex), winding);
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001177 other = endSpan->fOther;
caryclark@google.com495f8e42012-05-31 13:13:11 +00001178 nextStart = endSpan->fOtherIndex;
1179 nextEnd = nextStart + step;
1180 SkASSERT(step < 0 ? nextEnd >= 0 : nextEnd < other->fTs.count());
caryclark@google.com15fa1382012-05-07 20:49:36 +00001181 return other;
1182 }
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001183 // more than one viable candidate -- measure angles to find best
caryclark@google.com15fa1382012-05-07 20:49:36 +00001184 SkTDArray<Angle> angles;
caryclark@google.comaf46cff2012-05-22 21:12:00 +00001185 SkASSERT(startIndex - endIndex != 0);
1186 SkASSERT((startIndex - endIndex < 0) ^ (step < 0));
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001187 addTwoAngles(startIndex, end, angles);
1188 buildAngles(end, angles);
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001189 SkTDArray<Angle*> sorted;
1190 sortAngles(angles, sorted);
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001191 int angleCount = angles.count();
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00001192 int firstIndex = findStartingEdge(sorted, startIndex, end);
1193
caryclark@google.com1577e8f2012-05-22 17:01:14 +00001194 SkASSERT(firstIndex >= 0);
caryclark@google.com495f8e42012-05-31 13:13:11 +00001195 int startWinding = winding;
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001196 int nextIndex = firstIndex + 1;
1197 int lastIndex = firstIndex != 0 ? firstIndex : angleCount;
1198 const Angle* foundAngle = NULL;
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001199 // iterate through the angle, and compute everyone's winding
1200 bool firstEdge = true;
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001201 do {
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001202 if (nextIndex == angleCount) {
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001203 nextIndex = 0;
1204 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001205 const Angle* nextAngle = sorted[nextIndex];
caryclark@google.com495f8e42012-05-31 13:13:11 +00001206 int maxWinding = winding;
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001207 Segment* nextSegment = nextAngle->segment();
1208 int windValue = nextSegment->windValue(nextAngle);
1209 SkASSERT(windValue > 0);
1210 winding -= nextAngle->sign() * windValue;
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00001211 #if DEBUG_WINDING
1212 SkDebugf("%s maxWinding=%d winding=%d\n", __FUNCTION__, maxWinding,
1213 winding);
1214 #endif
1215 if (maxWinding * winding < 0) {
1216 flipped = -flipped;
1217 SkDebugf("flipped sign %d %d\n", maxWinding, winding);
1218 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001219 firstEdge = false;
caryclark@google.com88f7d0c2012-06-07 21:09:20 +00001220 if (!winding) {
caryclark@google.com5c286d32012-07-13 11:57:28 +00001221 if (!active) {
1222 SkASSERT(nextAngle->segment() == this);
1223 markWinding(SkMin32(nextAngle->start(), nextAngle->end()),
1224 maxWinding);
1225 SkDebugf("%s inactive\n", __FUNCTION__);
1226 return NULL;
1227 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001228 if (!foundAngle) {
1229 foundAngle = nextAngle;
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001230 }
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00001231 continue;
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001232 }
1233 if (nextSegment->done()) {
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00001234 continue;
caryclark@google.com495f8e42012-05-31 13:13:11 +00001235 }
1236 // if the winding is non-zero, nextAngle does not connect to
1237 // current chain. If we haven't done so already, mark the angle
1238 // as done, record the winding value, and mark connected unambiguous
1239 // segments as well.
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00001240 if (nextSegment->windSum(nextAngle) == SK_MinS32) {
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001241 if (abs(maxWinding) < abs(winding)) {
1242 maxWinding = winding;
1243 }
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00001244 Span* last;
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001245 if (foundAngle) {
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00001246 last = nextSegment->markAndChaseWinding(nextAngle, maxWinding);
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001247 } else {
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00001248 last = nextSegment->markAndChaseDone(nextAngle, maxWinding);
1249 }
1250 if (last) {
1251 *chase.append() = last;
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001252 }
caryclark@google.com495f8e42012-05-31 13:13:11 +00001253 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001254 } while (++nextIndex != lastIndex);
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00001255 sorted[firstIndex]->segment()->
1256 markDone(SkMin32(startIndex, endIndex), startWinding);
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001257 if (!foundAngle) {
1258 return NULL;
1259 }
1260 nextStart = foundAngle->start();
1261 nextEnd = foundAngle->end();
1262 return foundAngle->segment();
caryclark@google.com1577e8f2012-05-22 17:01:14 +00001263 }
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00001264
1265 int findStartingEdge(SkTDArray<Angle*>& sorted, int start, int end) {
1266 int angleCount = sorted.count();
1267 int firstIndex = -1;
1268 for (int angleIndex = 0; angleIndex < angleCount; ++angleIndex) {
1269 const Angle* angle = sorted[angleIndex];
1270 if (angle->segment() == this && angle->start() == end &&
1271 angle->end() == start) {
1272 firstIndex = angleIndex;
1273 break;
1274 }
1275 }
1276 return firstIndex;
1277 }
1278
caryclark@google.com1577e8f2012-05-22 17:01:14 +00001279 // FIXME: this is tricky code; needs its own unit test
caryclark@google.com65f9f0a2012-05-23 18:09:25 +00001280 void findTooCloseToCall(int /* winding */ ) { // FIXME: winding should be considered
caryclark@google.com15fa1382012-05-07 20:49:36 +00001281 int count = fTs.count();
1282 if (count < 3) { // require t=0, x, 1 at minimum
1283 return;
1284 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +00001285 int matchIndex = 0;
caryclark@google.com15fa1382012-05-07 20:49:36 +00001286 int moCount;
1287 Span* match;
1288 Segment* mOther;
1289 do {
1290 match = &fTs[matchIndex];
1291 mOther = match->fOther;
1292 moCount = mOther->fTs.count();
caryclark@google.com1577e8f2012-05-22 17:01:14 +00001293 if (moCount >= 3) {
1294 break;
1295 }
1296 if (++matchIndex >= count) {
1297 return;
1298 }
1299 } while (true); // require t=0, x, 1 at minimum
caryclark@google.com15fa1382012-05-07 20:49:36 +00001300 // OPTIMIZATION: defer matchPt until qualifying toCount is found?
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001301 const SkPoint* matchPt = &xyAtT(match);
caryclark@google.coma833b5c2012-04-30 19:38:50 +00001302 // look for a pair of nearby T values that map to the same (x,y) value
1303 // if found, see if the pair of other segments share a common point. If
1304 // so, the span from here to there is coincident.
caryclark@google.com15fa1382012-05-07 20:49:36 +00001305 for (int index = matchIndex + 1; index < count; ++index) {
1306 Span* test = &fTs[index];
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001307 if (test->fDone) {
caryclark@google.com495f8e42012-05-31 13:13:11 +00001308 continue;
1309 }
caryclark@google.com15fa1382012-05-07 20:49:36 +00001310 Segment* tOther = test->fOther;
1311 int toCount = tOther->fTs.count();
1312 if (toCount < 3) { // require t=0, x, 1 at minimum
caryclark@google.coma833b5c2012-04-30 19:38:50 +00001313 continue;
1314 }
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001315 const SkPoint* testPt = &xyAtT(test);
1316 if (*matchPt != *testPt) {
caryclark@google.coma833b5c2012-04-30 19:38:50 +00001317 matchIndex = index;
caryclark@google.com15fa1382012-05-07 20:49:36 +00001318 moCount = toCount;
1319 match = test;
1320 mOther = tOther;
caryclark@google.coma833b5c2012-04-30 19:38:50 +00001321 matchPt = testPt;
1322 continue;
1323 }
caryclark@google.com1577e8f2012-05-22 17:01:14 +00001324 int moStart = -1;
1325 int moEnd = -1;
1326 double moStartT, moEndT;
caryclark@google.coma833b5c2012-04-30 19:38:50 +00001327 for (int moIndex = 0; moIndex < moCount; ++moIndex) {
caryclark@google.com15fa1382012-05-07 20:49:36 +00001328 Span& moSpan = mOther->fTs[moIndex];
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001329 if (moSpan.fDone) {
caryclark@google.com495f8e42012-05-31 13:13:11 +00001330 continue;
1331 }
caryclark@google.com15fa1382012-05-07 20:49:36 +00001332 if (moSpan.fOther == this) {
1333 if (moSpan.fOtherT == match->fT) {
1334 moStart = moIndex;
caryclark@google.com1577e8f2012-05-22 17:01:14 +00001335 moStartT = moSpan.fT;
caryclark@google.com15fa1382012-05-07 20:49:36 +00001336 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +00001337 continue;
1338 }
caryclark@google.com1577e8f2012-05-22 17:01:14 +00001339 if (moSpan.fOther == tOther) {
1340 SkASSERT(moEnd == -1);
1341 moEnd = moIndex;
1342 moEndT = moSpan.fT;
caryclark@google.com15fa1382012-05-07 20:49:36 +00001343 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +00001344 }
caryclark@google.com1577e8f2012-05-22 17:01:14 +00001345 if (moStart < 0 || moEnd < 0) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001346 continue;
1347 }
caryclark@google.com1577e8f2012-05-22 17:01:14 +00001348 // FIXME: if moStartT, moEndT are initialized to NaN, can skip this test
1349 if (moStartT == moEndT) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001350 continue;
1351 }
caryclark@google.com1577e8f2012-05-22 17:01:14 +00001352 int toStart = -1;
1353 int toEnd = -1;
1354 double toStartT, toEndT;
1355 for (int toIndex = 0; toIndex < toCount; ++toIndex) {
1356 Span& toSpan = tOther->fTs[toIndex];
1357 if (toSpan.fOther == this) {
1358 if (toSpan.fOtherT == test->fT) {
1359 toStart = toIndex;
1360 toStartT = toSpan.fT;
1361 }
1362 continue;
1363 }
1364 if (toSpan.fOther == mOther && toSpan.fOtherT == moEndT) {
1365 SkASSERT(toEnd == -1);
1366 toEnd = toIndex;
1367 toEndT = toSpan.fT;
1368 }
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001369 }
caryclark@google.com1577e8f2012-05-22 17:01:14 +00001370 // FIXME: if toStartT, toEndT are initialized to NaN, can skip this test
1371 if (toStart <= 0 || toEnd <= 0) {
1372 continue;
1373 }
1374 if (toStartT == toEndT) {
1375 continue;
1376 }
1377 // test to see if the segment between there and here is linear
1378 if (!mOther->isLinear(moStart, moEnd)
1379 || !tOther->isLinear(toStart, toEnd)) {
1380 continue;
1381 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001382 // FIXME: defer implementation until the rest works
1383 // this may share code with regular coincident detection
1384 SkASSERT(0);
1385 #if 0
1386 if (flipped) {
1387 mOther->addTCancel(moStart, moEnd, tOther, tStart, tEnd);
1388 } else {
1389 mOther->addTCoincident(moStart, moEnd, tOther, tStart, tEnd);
1390 }
1391 #endif
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001392 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001393 }
1394
caryclark@google.coma833b5c2012-04-30 19:38:50 +00001395 // OPTIMIZATION : for a pair of lines, can we compute points at T (cached)
1396 // and use more concise logic like the old edge walker code?
1397 // FIXME: this needs to deal with coincident edges
caryclark@google.com1577e8f2012-05-22 17:01:14 +00001398 Segment* findTop(int& tIndex, int& endIndex) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001399 // iterate through T intersections and return topmost
1400 // topmost tangent from y-min to first pt is closer to horizontal
caryclark@google.com88f7d0c2012-06-07 21:09:20 +00001401 SkASSERT(!done());
1402 int firstT;
1403 int lastT;
caryclark@google.com88f7d0c2012-06-07 21:09:20 +00001404 SkPoint topPt;
1405 topPt.fY = SK_ScalarMax;
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001406 int count = fTs.count();
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001407 // see if either end is not done since we want smaller Y of the pair
caryclark@google.com88f7d0c2012-06-07 21:09:20 +00001408 bool lastDone = true;
1409 for (int index = 0; index < count; ++index) {
caryclark@google.com15fa1382012-05-07 20:49:36 +00001410 const Span& span = fTs[index];
caryclark@google.com88f7d0c2012-06-07 21:09:20 +00001411 if (!span.fDone || !lastDone) {
1412 const SkPoint& intercept = xyAtT(&span);
1413 if (topPt.fY > intercept.fY || (topPt.fY == intercept.fY
1414 && topPt.fX > intercept.fX)) {
1415 topPt = intercept;
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001416 firstT = lastT = index;
caryclark@google.com88f7d0c2012-06-07 21:09:20 +00001417 } else if (topPt == intercept) {
1418 lastT = index;
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001419 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001420 }
caryclark@google.com88f7d0c2012-06-07 21:09:20 +00001421 lastDone = span.fDone;
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001422 }
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001423 // sort the edges to find the leftmost
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001424 int step = 1;
1425 int end = nextSpan(firstT, step);
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001426 if (end == -1) {
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001427 step = -1;
1428 end = nextSpan(firstT, step);
caryclark@google.com65f9f0a2012-05-23 18:09:25 +00001429 SkASSERT(end != -1);
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001430 }
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001431 // if the topmost T is not on end, or is three-way or more, find left
1432 // look for left-ness from tLeft to firstT (matching y of other)
1433 SkTDArray<Angle> angles;
1434 SkASSERT(firstT - end != 0);
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001435 addTwoAngles(end, firstT, angles);
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001436 buildAngles(firstT, angles);
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001437 SkTDArray<Angle*> sorted;
1438 sortAngles(angles, sorted);
caryclark@google.com88f7d0c2012-06-07 21:09:20 +00001439 // skip edges that have already been processed
1440 firstT = -1;
1441 Segment* leftSegment;
1442 do {
1443 const Angle* angle = sorted[++firstT];
1444 leftSegment = angle->segment();
1445 tIndex = angle->end();
1446 endIndex = angle->start();
1447 } while (leftSegment->fTs[SkMin32(tIndex, endIndex)].fDone);
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001448 return leftSegment;
1449 }
1450
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001451 // FIXME: not crazy about this
1452 // when the intersections are performed, the other index is into an
1453 // incomplete array. as the array grows, the indices become incorrect
1454 // while the following fixes the indices up again, it isn't smart about
1455 // skipping segments whose indices are already correct
1456 // assuming we leave the code that wrote the index in the first place
1457 void fixOtherTIndex() {
1458 int iCount = fTs.count();
1459 for (int i = 0; i < iCount; ++i) {
1460 Span& iSpan = fTs[i];
1461 double oT = iSpan.fOtherT;
1462 Segment* other = iSpan.fOther;
1463 int oCount = other->fTs.count();
1464 for (int o = 0; o < oCount; ++o) {
1465 Span& oSpan = other->fTs[o];
1466 if (oT == oSpan.fT && this == oSpan.fOther) {
1467 iSpan.fOtherIndex = o;
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001468 break;
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001469 }
1470 }
1471 }
1472 }
1473
caryclark@google.com495f8e42012-05-31 13:13:11 +00001474 // OPTIMIZATION: uses tail recursion. Unwise?
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00001475 Span* innerChaseDone(int index, int step, int winding) {
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001476 int end = nextSpan(index, step);
caryclark@google.com9764cc62012-07-12 19:29:45 +00001477 SkASSERT(end >= 0);
1478 if (multipleSpans(end)) {
1479 return &fTs[end];
caryclark@google.com495f8e42012-05-31 13:13:11 +00001480 }
caryclark@google.com88f7d0c2012-06-07 21:09:20 +00001481 const Span& endSpan = fTs[end];
1482 Segment* other = endSpan.fOther;
1483 index = endSpan.fOtherIndex;
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001484 int otherEnd = other->nextSpan(index, step);
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00001485 Span* last = other->innerChaseDone(index, step, winding);
caryclark@google.com495f8e42012-05-31 13:13:11 +00001486 other->markDone(SkMin32(index, otherEnd), winding);
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00001487 return last;
caryclark@google.com495f8e42012-05-31 13:13:11 +00001488 }
1489
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00001490 Span* innerChaseWinding(int index, int step, int winding) {
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001491 int end = nextSpan(index, step);
caryclark@google.com9764cc62012-07-12 19:29:45 +00001492 SkASSERT(end >= 0);
1493 if (multipleSpans(end)) {
1494 return &fTs[end];
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001495 }
1496 const Span& endSpan = fTs[end];
1497 Segment* other = endSpan.fOther;
1498 index = endSpan.fOtherIndex;
1499 int otherEnd = other->nextSpan(index, step);
1500 int min = SkMin32(index, otherEnd);
1501 if (other->fTs[min].fWindSum != SK_MinS32) {
1502 SkASSERT(other->fTs[index].fWindSum == winding);
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00001503 return NULL;
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001504 }
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00001505 Span* last = other->innerChaseWinding(index, step, winding);
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001506 other->markWinding(min, winding);
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00001507 return last;
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001508 }
1509
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001510 void init(const SkPoint pts[], SkPath::Verb verb) {
1511 fPts = pts;
1512 fVerb = verb;
caryclark@google.comaf46cff2012-05-22 21:12:00 +00001513 fDoneSpans = 0;
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001514 }
1515
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001516 bool intersected() const {
1517 return fTs.count() > 0;
1518 }
caryclark@google.com15fa1382012-05-07 20:49:36 +00001519
1520 bool isLinear(int start, int end) const {
1521 if (fVerb == SkPath::kLine_Verb) {
1522 return true;
1523 }
1524 if (fVerb == SkPath::kQuad_Verb) {
1525 SkPoint qPart[3];
1526 QuadSubDivide(fPts, fTs[start].fT, fTs[end].fT, qPart);
1527 return QuadIsLinear(qPart);
1528 } else {
1529 SkASSERT(fVerb == SkPath::kCubic_Verb);
1530 SkPoint cPart[4];
1531 CubicSubDivide(fPts, fTs[start].fT, fTs[end].fT, cPart);
1532 return CubicIsLinear(cPart);
1533 }
1534 }
caryclark@google.comb9738012012-07-03 19:53:30 +00001535
1536 // OPTIMIZE: successive calls could start were the last leaves off
1537 // or calls could specialize to walk forwards or backwards
1538 bool isMissing(double startT) const {
1539 size_t tCount = fTs.count();
1540 for (size_t index = 0; index < tCount; ++index) {
1541 if (fabs(startT - fTs[index].fT) < FLT_EPSILON) {
1542 return false;
1543 }
1544 }
1545 return true;
1546 }
1547
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001548 bool isSimple(int end) const {
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001549 int count = fTs.count();
1550 if (count == 2) {
1551 return true;
1552 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001553 double t = fTs[end].fT;
1554 if (t < FLT_EPSILON) {
1555 return fTs[1].fT >= FLT_EPSILON;
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001556 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001557 if (t > 1 - FLT_EPSILON) {
1558 return fTs[count - 2].fT <= 1 - FLT_EPSILON;
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001559 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001560 return false;
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001561 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001562
1563 bool isHorizontal() const {
1564 return fBounds.fTop == fBounds.fBottom;
1565 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +00001566
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001567 bool isVertical() const {
1568 return fBounds.fLeft == fBounds.fRight;
1569 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +00001570
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001571 SkScalar leftMost(int start, int end) const {
1572 return (*SegmentLeftMost[fVerb])(fPts, fTs[start].fT, fTs[end].fT);
1573 }
caryclark@google.comaf46cff2012-05-22 21:12:00 +00001574
caryclark@google.com495f8e42012-05-31 13:13:11 +00001575 // this span is excluded by the winding rule -- chase the ends
1576 // as long as they are unambiguous to mark connections as done
1577 // and give them the same winding value
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00001578 Span* markAndChaseDone(const Angle* angle, int winding) {
caryclark@google.com495f8e42012-05-31 13:13:11 +00001579 int index = angle->start();
1580 int endIndex = angle->end();
1581 int step = SkSign32(endIndex - index);
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00001582 Span* last = innerChaseDone(index, step, winding);
caryclark@google.com495f8e42012-05-31 13:13:11 +00001583 markDone(SkMin32(index, endIndex), winding);
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00001584 return last;
caryclark@google.com495f8e42012-05-31 13:13:11 +00001585 }
1586
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00001587 Span* markAndChaseWinding(const Angle* angle, int winding) {
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001588 int index = angle->start();
1589 int endIndex = angle->end();
1590 int min = SkMin32(index, endIndex);
1591 int step = SkSign32(endIndex - index);
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00001592 Span* last = innerChaseWinding(index, step, winding);
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001593 markWinding(min, winding);
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00001594 return last;
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001595 }
1596
caryclark@google.com495f8e42012-05-31 13:13:11 +00001597 // FIXME: this should also mark spans with equal (x,y)
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001598 // This may be called when the segment is already marked done. While this
1599 // wastes time, it shouldn't do any more than spin through the T spans.
1600 // OPTIMIZATION: abort on first done found (assuming that this code is
1601 // always called to mark segments done).
caryclark@google.com495f8e42012-05-31 13:13:11 +00001602 void markDone(int index, int winding) {
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001603 // SkASSERT(!done());
caryclark@google.comaf46cff2012-05-22 21:12:00 +00001604 double referenceT = fTs[index].fT;
1605 int lesser = index;
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001606 while (--lesser >= 0 && referenceT - fTs[lesser].fT < FLT_EPSILON) {
caryclark@google.com495f8e42012-05-31 13:13:11 +00001607 Span& span = fTs[lesser];
caryclark@google.com88f7d0c2012-06-07 21:09:20 +00001608 if (span.fDone) {
1609 continue;
1610 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001611 #if DEBUG_MARK_DONE
1612 const SkPoint& pt = xyAtT(&span);
1613 SkDebugf("%s segment=%d index=%d t=%1.9g pt=(%1.9g,%1.9g) wind=%d\n",
1614 __FUNCTION__, fID, lesser, span.fT, pt.fX, pt.fY, winding);
1615 #endif
caryclark@google.com88f7d0c2012-06-07 21:09:20 +00001616 span.fDone = true;
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001617 SkASSERT(span.fWindSum == SK_MinS32 || span.fWindSum == winding);
1618 span.fWindSum = winding;
caryclark@google.com65f9f0a2012-05-23 18:09:25 +00001619 fDoneSpans++;
caryclark@google.comaf46cff2012-05-22 21:12:00 +00001620 }
1621 do {
caryclark@google.com495f8e42012-05-31 13:13:11 +00001622 Span& span = fTs[index];
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001623 // SkASSERT(!span.fDone);
1624 if (span.fDone) {
1625 continue;
1626 }
1627 #if DEBUG_MARK_DONE
1628 const SkPoint& pt = xyAtT(&span);
1629 SkDebugf("%s segment=%d index=%d t=%1.9g pt=(%1.9g,%1.9g) wind=%d\n",
1630 __FUNCTION__, fID, index, span.fT, pt.fX, pt.fY, winding);
1631 #endif
1632 span.fDone = true;
1633 SkASSERT(span.fWindSum == SK_MinS32 || span.fWindSum == winding);
1634 span.fWindSum = winding;
1635 fDoneSpans++;
1636 } while (++index < fTs.count() && fTs[index].fT - referenceT < FLT_EPSILON);
1637 }
1638
1639 void markWinding(int index, int winding) {
1640 SkASSERT(!done());
1641 double referenceT = fTs[index].fT;
1642 int lesser = index;
1643 while (--lesser >= 0 && referenceT - fTs[lesser].fT < FLT_EPSILON) {
1644 Span& span = fTs[lesser];
1645 if (span.fDone) {
1646 continue;
1647 }
1648 SkASSERT(span.fWindValue == 1 || winding == 0);
1649 SkASSERT(span.fWindSum == SK_MinS32 || span.fWindSum == winding);
1650 #if DEBUG_MARK_DONE
1651 const SkPoint& pt = xyAtT(&span);
1652 SkDebugf("%s segment=%d index=%d t=%1.9g pt=(%1.9g,%1.9g) wind=%d\n",
1653 __FUNCTION__, fID, lesser, span.fT, pt.fX, pt.fY, winding);
1654 #endif
1655 span.fWindSum = winding;
1656 }
1657 do {
1658 Span& span = fTs[index];
caryclark@google.com88f7d0c2012-06-07 21:09:20 +00001659 // SkASSERT(!span.fDone || span.fCoincident);
1660 if (span.fDone) {
1661 continue;
1662 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001663 SkASSERT(span.fWindValue == 1 || winding == 0);
1664 SkASSERT(span.fWindSum == SK_MinS32 || span.fWindSum == winding);
1665 #if DEBUG_MARK_DONE
1666 const SkPoint& pt = xyAtT(&span);
1667 SkDebugf("%s segment=%d index=%d t=%1.9g pt=(%1.9g,%1.9g) wind=%d\n",
1668 __FUNCTION__, fID, index, span.fT, pt.fX, pt.fY, winding);
1669 #endif
1670 span.fWindSum = winding;
1671 } while (++index < fTs.count() && fTs[index].fT - referenceT < FLT_EPSILON);
caryclark@google.comaf46cff2012-05-22 21:12:00 +00001672 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001673
caryclark@google.com9764cc62012-07-12 19:29:45 +00001674 // return span if when chasing, two or more radiating spans are not done
1675 // OPTIMIZATION: ? multiple spans is detected when there is only one valid
1676 // candidate and the remaining spans have windValue == 0 (canceled by
1677 // coincidence). The coincident edges could either be removed altogether,
1678 // or this code could be more complicated in detecting this case. Worth it?
1679 bool multipleSpans(int end) const {
1680 return end > 0 && end < fTs.count() - 1;
caryclark@google.com88f7d0c2012-06-07 21:09:20 +00001681 }
1682
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001683 // This has callers for two different situations: one establishes the end
1684 // of the current span, and one establishes the beginning of the next span
1685 // (thus the name). When this is looking for the end of the current span,
1686 // coincidence is found when the beginning Ts contain -step and the end
1687 // contains step. When it is looking for the beginning of the next, the
1688 // first Ts found can be ignored and the last Ts should contain -step.
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001689 // OPTIMIZATION: probably should split into two functions
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001690 int nextSpan(int from, int step) const {
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001691 const Span& fromSpan = fTs[from];
caryclark@google.com495f8e42012-05-31 13:13:11 +00001692 int count = fTs.count();
1693 int to = from;
caryclark@google.com495f8e42012-05-31 13:13:11 +00001694 while (step > 0 ? ++to < count : --to >= 0) {
1695 const Span& span = fTs[to];
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001696 if ((step > 0 ? span.fT - fromSpan.fT : fromSpan.fT - span.fT) < FLT_EPSILON) {
caryclark@google.com495f8e42012-05-31 13:13:11 +00001697 continue;
1698 }
caryclark@google.com495f8e42012-05-31 13:13:11 +00001699 return to;
1700 }
1701 return -1;
1702 }
1703
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001704 const SkPoint* pts() const {
1705 return fPts;
1706 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +00001707
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001708 void reset() {
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001709 init(NULL, (SkPath::Verb) -1);
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001710 fBounds.set(SK_ScalarMax, SK_ScalarMax, SK_ScalarMax, SK_ScalarMax);
1711 fTs.reset();
caryclark@google.coma833b5c2012-04-30 19:38:50 +00001712 }
1713
caryclark@google.com1577e8f2012-05-22 17:01:14 +00001714 // OPTIMIZATION: mark as debugging only if used solely by tests
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001715 const Span& span(int tIndex) const {
1716 return fTs[tIndex];
1717 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001718
1719 int spanSign(int startIndex, int endIndex) const {
1720 return startIndex < endIndex ? -fTs[startIndex].fWindValue :
1721 fTs[endIndex].fWindValue;
1722 }
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001723
1724 // OPTIMIZATION: mark as debugging only if used solely by tests
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001725 double t(int tIndex) const {
1726 return fTs[tIndex].fT;
1727 }
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001728
1729 void updatePts(const SkPoint pts[]) {
1730 fPts = pts;
1731 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +00001732
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001733 SkPath::Verb verb() const {
1734 return fVerb;
1735 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001736
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00001737 int windSum(int tIndex) const {
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001738 return fTs[tIndex].fWindSum;
1739 }
caryclark@google.com495f8e42012-05-31 13:13:11 +00001740
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00001741 int windSum(const Angle* angle) const {
caryclark@google.com495f8e42012-05-31 13:13:11 +00001742 int start = angle->start();
1743 int end = angle->end();
1744 int index = SkMin32(start, end);
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00001745 return windSum(index);
caryclark@google.com495f8e42012-05-31 13:13:11 +00001746 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +00001747
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001748 int windValue(int tIndex) const {
1749 return fTs[tIndex].fWindValue;
1750 }
1751
1752 int windValue(const Angle* angle) const {
1753 int start = angle->start();
1754 int end = angle->end();
1755 int index = SkMin32(start, end);
1756 return windValue(index);
1757 }
1758
1759 SkScalar xAtT(const Span* span) const {
1760 return xyAtT(span).fX;
1761 }
1762
1763 const SkPoint& xyAtT(int index) const {
1764 return xyAtT(&fTs[index]);
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001765 }
1766
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001767 const SkPoint& xyAtT(const Span* span) const {
1768 if (!span->fPt) {
1769 if (span->fT == 0) {
1770 span->fPt = &fPts[0];
1771 } else if (span->fT == 1) {
1772 span->fPt = &fPts[fVerb];
1773 } else {
1774 SkPoint* pt = fIntersections.append();
1775 (*SegmentXYAtT[fVerb])(fPts, span->fT, pt);
1776 span->fPt = pt;
1777 }
1778 }
1779 return *span->fPt;
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001780 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001781
1782 SkScalar yAtT(int index) const {
1783 return yAtT(&fTs[index]);
1784 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001785
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001786 SkScalar yAtT(const Span* span) const {
1787 return xyAtT(span).fY;
caryclark@google.coma833b5c2012-04-30 19:38:50 +00001788 }
1789
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001790#if DEBUG_DUMP
1791 void dump() const {
1792 const char className[] = "Segment";
1793 const int tab = 4;
1794 for (int i = 0; i < fTs.count(); ++i) {
1795 SkPoint out;
1796 (*SegmentXYAtT[fVerb])(fPts, t(i), &out);
1797 SkDebugf("%*s [%d] %s.fTs[%d]=%1.9g (%1.9g,%1.9g) other=%d"
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001798 " otherT=%1.9g windSum=%d\n",
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001799 tab + sizeof(className), className, fID,
1800 kLVerbStr[fVerb], i, fTs[i].fT, out.fX, out.fY,
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001801 fTs[i].fOther->fID, fTs[i].fOtherT, fTs[i].fWindSum);
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001802 }
caryclark@google.com15fa1382012-05-07 20:49:36 +00001803 SkDebugf("%*s [%d] fBounds=(l:%1.9g, t:%1.9g r:%1.9g, b:%1.9g)",
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001804 tab + sizeof(className), className, fID,
caryclark@google.com15fa1382012-05-07 20:49:36 +00001805 fBounds.fLeft, fBounds.fTop, fBounds.fRight, fBounds.fBottom);
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001806 }
1807#endif
1808
caryclark@google.com027de222012-07-12 12:52:50 +00001809#if DEBUG_ACTIVE_SPANS
1810 void debugShowActiveSpans(int contourID, int segmentIndex) {
1811 if (done()) {
1812 return;
1813 }
1814 for (int i = 0; i < fTs.count(); ++i) {
1815 if (fTs[i].fDone) {
1816 continue;
1817 }
1818 SkDebugf("%s contour=%d segment=%d (%d)", __FUNCTION__, contourID,
1819 segmentIndex, fID);
1820 SkDebugf(" (%1.9g,%1.9g", fPts[0].fX, fPts[0].fY);
1821 for (int vIndex = 1; vIndex <= fVerb; ++vIndex) {
1822 SkDebugf(" %1.9g,%1.9g", fPts[vIndex].fX, fPts[vIndex].fY);
1823 }
1824 const Span* span = &fTs[i];
1825 SkDebugf(") fT=%d (%1.9g) (%1.9g,%1.9g)", i, fTs[i].fT,
1826 xAtT(span), yAtT(i));
1827 const Segment* other = fTs[i].fOther;
1828 SkDebugf(" other=%d otherT=%1.9g otherIndex=%d", other->fID,
1829 fTs[i].fOtherT, fTs[i].fOtherIndex);
1830 SkDebugf(" windSum=%d windValue=%d\n", fTs[i].fWindSum,
1831 fTs[i].fWindValue);
1832 }
1833 }
1834#endif
1835
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001836private:
1837 const SkPoint* fPts;
1838 SkPath::Verb fVerb;
1839 Bounds fBounds;
caryclark@google.com15fa1382012-05-07 20:49:36 +00001840 SkTDArray<Span> fTs; // two or more (always includes t=0 t=1)
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00001841 // OPTIMIZATION:if intersections array is a pointer, the it could only
1842 // be allocated as needed instead of always initialized -- though maybe
1843 // the initialization is lightweight enough that it hardly matters
1844 mutable SkTDArray<SkPoint> fIntersections;
caryclark@google.comaf46cff2012-05-22 21:12:00 +00001845 int fDoneSpans; // used for quick check that segment is finished
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001846#if DEBUG_DUMP
1847 int fID;
1848#endif
1849};
1850
caryclark@google.comb9738012012-07-03 19:53:30 +00001851class Contour;
1852
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001853struct Coincidence {
caryclark@google.comb9738012012-07-03 19:53:30 +00001854 Contour* fContours[2];
1855 int fSegments[2];
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001856 double fTs[2][2];
1857};
1858
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001859class Contour {
1860public:
1861 Contour() {
1862 reset();
1863#if DEBUG_DUMP
1864 fID = ++gContourID;
1865#endif
1866 }
1867
1868 bool operator<(const Contour& rh) const {
1869 return fBounds.fTop == rh.fBounds.fTop
1870 ? fBounds.fLeft < rh.fBounds.fLeft
1871 : fBounds.fTop < rh.fBounds.fTop;
1872 }
1873
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001874 void addCoincident(int index, Contour* other, int otherIndex,
1875 const Intersections& ts, bool swap) {
1876 Coincidence& coincidence = *fCoincidences.append();
caryclark@google.comb9738012012-07-03 19:53:30 +00001877 coincidence.fContours[0] = this;
1878 coincidence.fContours[1] = other;
1879 coincidence.fSegments[0] = index;
1880 coincidence.fSegments[1] = otherIndex;
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001881 coincidence.fTs[swap][0] = ts.fT[0][0];
1882 coincidence.fTs[swap][1] = ts.fT[0][1];
1883 coincidence.fTs[!swap][0] = ts.fT[1][0];
1884 coincidence.fTs[!swap][1] = ts.fT[1][1];
1885 }
1886
1887 void addCross(const Contour* crosser) {
1888#ifdef DEBUG_CROSS
1889 for (int index = 0; index < fCrosses.count(); ++index) {
1890 SkASSERT(fCrosses[index] != crosser);
1891 }
1892#endif
1893 *fCrosses.append() = crosser;
1894 }
1895
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001896 void addCubic(const SkPoint pts[4]) {
1897 fSegments.push_back().addCubic(pts);
1898 fContainsCurves = true;
1899 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +00001900
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001901 int addLine(const SkPoint pts[2]) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001902 fSegments.push_back().addLine(pts);
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001903 return fSegments.count();
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001904 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001905
1906 void addOtherT(int segIndex, int tIndex, double otherT, int otherIndex) {
1907 fSegments[segIndex].addOtherT(tIndex, otherT, otherIndex);
1908 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +00001909
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001910 int addQuad(const SkPoint pts[3]) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001911 fSegments.push_back().addQuad(pts);
1912 fContainsCurves = true;
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001913 return fSegments.count();
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001914 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +00001915
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001916 int addT(int segIndex, double newT, Contour* other, int otherIndex) {
1917 containsIntercepts();
1918 return fSegments[segIndex].addT(newT, &other->fSegments[otherIndex]);
1919 }
1920
caryclark@google.coma833b5c2012-04-30 19:38:50 +00001921 const Bounds& bounds() const {
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001922 return fBounds;
1923 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +00001924
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001925 void complete() {
1926 setBounds();
1927 fContainsIntercepts = false;
1928 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +00001929
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001930 void containsIntercepts() {
1931 fContainsIntercepts = true;
1932 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +00001933
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001934 const Segment* crossedSegment(const SkPoint& basePt, SkScalar& bestY,
1935 int &tIndex, double& hitT) {
1936 int segmentCount = fSegments.count();
1937 const Segment* bestSegment = NULL;
1938 for (int test = 0; test < segmentCount; ++test) {
1939 Segment* testSegment = &fSegments[test];
1940 const SkRect& bounds = testSegment->bounds();
1941 if (bounds.fTop < bestY) {
1942 continue;
1943 }
1944 if (bounds.fTop > basePt.fY) {
1945 continue;
1946 }
1947 if (bounds.fLeft > basePt.fX) {
1948 continue;
1949 }
1950 if (bounds.fRight < basePt.fX) {
1951 continue;
1952 }
1953 double testHitT;
1954 int testT = testSegment->crossedSpan(basePt, bestY, testHitT);
1955 if (testT >= 0) {
1956 bestSegment = testSegment;
1957 tIndex = testT;
1958 hitT = testHitT;
1959 }
1960 }
1961 return bestSegment;
1962 }
1963
1964 bool crosses(const Contour* crosser) const {
1965 if (this == crosser) {
1966 return true;
1967 }
1968 for (int index = 0; index < fCrosses.count(); ++index) {
1969 if (fCrosses[index] == crosser) {
1970 return true;
1971 }
1972 }
1973 return false;
1974 }
1975
caryclark@google.coma833b5c2012-04-30 19:38:50 +00001976 void findTooCloseToCall(int winding) {
1977 int segmentCount = fSegments.count();
1978 for (int sIndex = 0; sIndex < segmentCount; ++sIndex) {
1979 fSegments[sIndex].findTooCloseToCall(winding);
1980 }
1981 }
1982
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001983 void fixOtherTIndex() {
1984 int segmentCount = fSegments.count();
1985 for (int sIndex = 0; sIndex < segmentCount; ++sIndex) {
1986 fSegments[sIndex].fixOtherTIndex();
1987 }
1988 }
1989
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001990 void reset() {
1991 fSegments.reset();
1992 fBounds.set(SK_ScalarMax, SK_ScalarMax, SK_ScalarMax, SK_ScalarMax);
caryclark@google.com15fa1382012-05-07 20:49:36 +00001993 fContainsCurves = fContainsIntercepts = false;
caryclark@google.com66ca2fb2012-07-03 14:30:08 +00001994 fWindingSum = SK_MinS32;
caryclark@google.comfa0588f2012-04-26 21:01:06 +00001995 }
caryclark@google.comb9738012012-07-03 19:53:30 +00001996
caryclark@google.com8dcf1142012-07-02 20:27:02 +00001997 void resolveCoincidence(int winding) {
1998 int count = fCoincidences.count();
1999 for (int index = 0; index < count; ++index) {
2000 Coincidence& coincidence = fCoincidences[index];
caryclark@google.comb9738012012-07-03 19:53:30 +00002001 Contour* thisContour = coincidence.fContours[0];
2002 Contour* otherContour = coincidence.fContours[1];
2003 int thisIndex = coincidence.fSegments[0];
2004 int otherIndex = coincidence.fSegments[1];
2005 Segment& thisOne = thisContour->fSegments[thisIndex];
2006 Segment& other = otherContour->fSegments[otherIndex];
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002007 double startT = coincidence.fTs[0][0];
2008 double endT = coincidence.fTs[0][1];
2009 if (startT > endT) {
2010 SkTSwap<double>(startT, endT);
2011 }
2012 SkASSERT(endT - startT >= FLT_EPSILON);
2013 double oStartT = coincidence.fTs[1][0];
2014 double oEndT = coincidence.fTs[1][1];
2015 if (oStartT > oEndT) {
2016 SkTSwap<double>(oStartT, oEndT);
2017 }
2018 SkASSERT(oEndT - oStartT >= FLT_EPSILON);
caryclark@google.comb9738012012-07-03 19:53:30 +00002019 if (winding > 0 || thisOne.cancels(other)) {
2020 // make sure startT and endT have t entries
2021 if (thisOne.isMissing(startT) || other.isMissing(oEndT)) {
2022 thisOne.addTPair(startT, other, oEndT);
2023 }
2024 if (thisOne.isMissing(endT) || other.isMissing(oStartT)) {
2025 other.addTPair(oStartT, thisOne, endT);
2026 }
2027 thisOne.addTCancel(startT, endT, other, oStartT, oEndT);
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002028 } else {
caryclark@google.comb9738012012-07-03 19:53:30 +00002029 if (thisOne.isMissing(startT) || other.isMissing(oStartT)) {
2030 thisOne.addTPair(startT, other, oStartT);
2031 }
2032 if (thisOne.isMissing(endT) || other.isMissing(oEndT)) {
2033 other.addTPair(oEndT, thisOne, endT);
2034 }
2035 thisOne.addTCoincident(startT, endT, other, oStartT, oEndT);
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002036 }
2037 }
2038 }
2039
2040 const SkTArray<Segment>& segments() {
2041 return fSegments;
2042 }
2043
2044 void setWinding(int winding) {
2045 SkASSERT(fWindingSum < 0);
2046 fWindingSum = winding;
2047 }
2048
caryclark@google.com15fa1382012-05-07 20:49:36 +00002049 // OPTIMIZATION: feel pretty uneasy about this. It seems like once again
2050 // we need to sort and walk edges in y, but that on the surface opens the
2051 // same can of worms as before. But then, this is a rough sort based on
2052 // segments' top, and not a true sort, so it could be ameniable to regular
2053 // sorting instead of linear searching. Still feel like I'm missing something
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002054 Segment* topSegment(SkScalar& bestY) {
caryclark@google.com15fa1382012-05-07 20:49:36 +00002055 int segmentCount = fSegments.count();
2056 SkASSERT(segmentCount > 0);
2057 int best = -1;
2058 Segment* bestSegment = NULL;
2059 while (++best < segmentCount) {
2060 Segment* testSegment = &fSegments[best];
2061 if (testSegment->done()) {
2062 continue;
2063 }
2064 bestSegment = testSegment;
2065 break;
caryclark@google.coma833b5c2012-04-30 19:38:50 +00002066 }
caryclark@google.com15fa1382012-05-07 20:49:36 +00002067 if (!bestSegment) {
2068 return NULL;
caryclark@google.coma833b5c2012-04-30 19:38:50 +00002069 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002070 SkScalar bestTop = bestSegment->activeTop();
caryclark@google.com15fa1382012-05-07 20:49:36 +00002071 for (int test = best + 1; test < segmentCount; ++test) {
2072 Segment* testSegment = &fSegments[test];
2073 if (testSegment->done()) {
2074 continue;
2075 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002076 if (testSegment->bounds().fTop > bestTop) {
2077 continue;
2078 }
2079 SkScalar testTop = testSegment->activeTop();
caryclark@google.com15fa1382012-05-07 20:49:36 +00002080 if (bestTop > testTop) {
2081 bestTop = testTop;
2082 bestSegment = testSegment;
2083 }
2084 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002085 bestY = bestTop;
caryclark@google.com15fa1382012-05-07 20:49:36 +00002086 return bestSegment;
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002087 }
2088
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002089 int updateSegment(int index, const SkPoint* pts) {
2090 Segment& segment = fSegments[index];
2091 segment.updatePts(pts);
2092 return segment.verb() + 1;
2093 }
2094
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00002095 int windSum() {
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002096 if (fWindingSum >= 0) {
2097 return fWindingSum;
2098 }
2099 // check peers
2100 int count = fCrosses.count();
2101 for (int index = 0; index < count; ++index) {
2102 const Contour* crosser = fCrosses[index];
2103 if (0 <= crosser->fWindingSum) {
2104 fWindingSum = crosser->fWindingSum;
2105 break;
2106 }
2107 }
2108 return fWindingSum;
2109 }
2110
2111#if DEBUG_TEST
2112 SkTArray<Segment>& debugSegments() {
2113 return fSegments;
2114 }
2115#endif
2116
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002117#if DEBUG_DUMP
2118 void dump() {
2119 int i;
2120 const char className[] = "Contour";
2121 const int tab = 4;
2122 SkDebugf("%s %p (contour=%d)\n", className, this, fID);
2123 for (i = 0; i < fSegments.count(); ++i) {
2124 SkDebugf("%*s.fSegments[%d]:\n", tab + sizeof(className),
2125 className, i);
2126 fSegments[i].dump();
2127 }
2128 SkDebugf("%*s.fBounds=(l:%1.9g, t:%1.9g r:%1.9g, b:%1.9g)\n",
2129 tab + sizeof(className), className,
2130 fBounds.fLeft, fBounds.fTop,
2131 fBounds.fRight, fBounds.fBottom);
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002132 SkDebugf("%*s.fContainsIntercepts=%d\n", tab + sizeof(className),
2133 className, fContainsIntercepts);
2134 SkDebugf("%*s.fContainsCurves=%d\n", tab + sizeof(className),
2135 className, fContainsCurves);
2136 }
2137#endif
2138
caryclark@google.com027de222012-07-12 12:52:50 +00002139#if DEBUG_ACTIVE_SPANS
2140 void debugShowActiveSpans() {
2141 for (int index = 0; index < fSegments.count(); ++index) {
2142 fSegments[index].debugShowActiveSpans(fID, index);
2143 }
2144 }
2145#endif
2146
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002147protected:
2148 void setBounds() {
2149 int count = fSegments.count();
2150 if (count == 0) {
2151 SkDebugf("%s empty contour\n", __FUNCTION__);
2152 SkASSERT(0);
2153 // FIXME: delete empty contour?
2154 return;
2155 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002156 fBounds = fSegments.front().bounds();
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002157 for (int index = 1; index < count; ++index) {
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002158 fBounds.add(fSegments[index].bounds());
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002159 }
2160 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +00002161
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002162private:
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002163 SkTArray<Segment> fSegments;
2164 SkTDArray<Coincidence> fCoincidences;
2165 SkTDArray<const Contour*> fCrosses;
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002166 Bounds fBounds;
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002167 bool fContainsIntercepts;
2168 bool fContainsCurves;
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002169 int fWindingSum; // initial winding number outside
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002170#if DEBUG_DUMP
2171 int fID;
2172#endif
2173};
2174
2175class EdgeBuilder {
2176public:
2177
caryclark@google.coma833b5c2012-04-30 19:38:50 +00002178EdgeBuilder(const SkPath& path, SkTArray<Contour>& contours)
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002179 : fPath(path)
2180 , fCurrentContour(NULL)
2181 , fContours(contours)
2182{
2183#if DEBUG_DUMP
2184 gContourID = 0;
2185 gSegmentID = 0;
2186#endif
2187 walk();
2188}
2189
2190protected:
2191
2192void complete() {
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002193 if (fCurrentContour && fCurrentContour->segments().count()) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002194 fCurrentContour->complete();
2195 fCurrentContour = NULL;
2196 }
2197}
2198
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002199void walk() {
2200 // FIXME:remove once we can access path pts directly
2201 SkPath::RawIter iter(fPath); // FIXME: access path directly when allowed
2202 SkPoint pts[4];
2203 SkPath::Verb verb;
2204 do {
2205 verb = iter.next(pts);
2206 *fPathVerbs.append() = verb;
2207 if (verb == SkPath::kMove_Verb) {
2208 *fPathPts.append() = pts[0];
2209 } else if (verb >= SkPath::kLine_Verb && verb <= SkPath::kCubic_Verb) {
2210 fPathPts.append(verb, &pts[1]);
2211 }
2212 } while (verb != SkPath::kDone_Verb);
2213 // FIXME: end of section to remove once path pts are accessed directly
caryclark@google.coma833b5c2012-04-30 19:38:50 +00002214
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002215 SkPath::Verb reducedVerb;
2216 uint8_t* verbPtr = fPathVerbs.begin();
2217 const SkPoint* pointsPtr = fPathPts.begin();
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002218 const SkPoint* finalCurveStart = NULL;
2219 const SkPoint* finalCurveEnd = NULL;
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002220 while ((verb = (SkPath::Verb) *verbPtr++) != SkPath::kDone_Verb) {
2221 switch (verb) {
2222 case SkPath::kMove_Verb:
2223 complete();
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002224 if (!fCurrentContour) {
2225 fCurrentContour = fContours.push_back_n(1);
2226 finalCurveEnd = pointsPtr++;
2227 *fExtra.append() = -1; // start new contour
2228 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002229 continue;
2230 case SkPath::kLine_Verb:
2231 // skip degenerate points
2232 if (pointsPtr[-1].fX != pointsPtr[0].fX
2233 || pointsPtr[-1].fY != pointsPtr[0].fY) {
2234 fCurrentContour->addLine(&pointsPtr[-1]);
2235 }
2236 break;
2237 case SkPath::kQuad_Verb:
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002238
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002239 reducedVerb = QuadReduceOrder(&pointsPtr[-1], fReducePts);
2240 if (reducedVerb == 0) {
2241 break; // skip degenerate points
2242 }
2243 if (reducedVerb == 1) {
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002244 *fExtra.append() =
2245 fCurrentContour->addLine(fReducePts.end() - 2);
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002246 break;
2247 }
2248 fCurrentContour->addQuad(&pointsPtr[-1]);
2249 break;
2250 case SkPath::kCubic_Verb:
2251 reducedVerb = CubicReduceOrder(&pointsPtr[-1], fReducePts);
2252 if (reducedVerb == 0) {
2253 break; // skip degenerate points
2254 }
2255 if (reducedVerb == 1) {
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002256 *fExtra.append() =
2257 fCurrentContour->addLine(fReducePts.end() - 2);
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002258 break;
2259 }
2260 if (reducedVerb == 2) {
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002261 *fExtra.append() =
2262 fCurrentContour->addQuad(fReducePts.end() - 3);
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002263 break;
2264 }
2265 fCurrentContour->addCubic(&pointsPtr[-1]);
2266 break;
2267 case SkPath::kClose_Verb:
2268 SkASSERT(fCurrentContour);
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002269 if (finalCurveStart && finalCurveEnd
2270 && *finalCurveStart != *finalCurveEnd) {
2271 *fReducePts.append() = *finalCurveStart;
2272 *fReducePts.append() = *finalCurveEnd;
2273 *fExtra.append() =
2274 fCurrentContour->addLine(fReducePts.end() - 2);
2275 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002276 complete();
2277 continue;
2278 default:
2279 SkDEBUGFAIL("bad verb");
2280 return;
2281 }
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002282 finalCurveStart = &pointsPtr[verb - 1];
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002283 pointsPtr += verb;
2284 SkASSERT(fCurrentContour);
2285 }
2286 complete();
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002287 if (fCurrentContour && !fCurrentContour->segments().count()) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002288 fContours.pop_back();
2289 }
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002290 // correct pointers in contours since fReducePts may have moved as it grew
2291 int cIndex = 0;
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002292 int extraCount = fExtra.count();
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002293 SkASSERT(extraCount == 0 || fExtra[0] == -1);
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002294 int eIndex = 0;
2295 int rIndex = 0;
2296 while (++eIndex < extraCount) {
2297 int offset = fExtra[eIndex];
2298 if (offset < 0) {
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002299 ++cIndex;
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002300 continue;
2301 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002302 fCurrentContour = &fContours[cIndex];
2303 rIndex += fCurrentContour->updateSegment(offset - 1,
2304 &fReducePts[rIndex]);
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002305 }
2306 fExtra.reset(); // we're done with this
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002307}
2308
2309private:
2310 const SkPath& fPath;
2311 SkTDArray<SkPoint> fPathPts; // FIXME: point directly to path pts instead
caryclark@google.coma833b5c2012-04-30 19:38:50 +00002312 SkTDArray<uint8_t> fPathVerbs; // FIXME: remove
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002313 Contour* fCurrentContour;
2314 SkTArray<Contour>& fContours;
2315 SkTDArray<SkPoint> fReducePts; // segments created on the fly
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002316 SkTDArray<int> fExtra; // -1 marks new contour, > 0 offsets into contour
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002317};
2318
2319class Work {
2320public:
2321 enum SegmentType {
2322 kHorizontalLine_Segment = -1,
2323 kVerticalLine_Segment = 0,
2324 kLine_Segment = SkPath::kLine_Verb,
2325 kQuad_Segment = SkPath::kQuad_Verb,
2326 kCubic_Segment = SkPath::kCubic_Verb,
2327 };
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002328
2329 void addCoincident(Work& other, const Intersections& ts, bool swap) {
2330 fContour->addCoincident(fIndex, other.fContour, other.fIndex, ts, swap);
2331 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +00002332
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002333 // FIXME: does it make sense to write otherIndex now if we're going to
2334 // fix it up later?
2335 void addOtherT(int index, double otherT, int otherIndex) {
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002336 fContour->addOtherT(fIndex, index, otherT, otherIndex);
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002337 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +00002338
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002339 // Avoid collapsing t values that are close to the same since
2340 // we walk ts to describe consecutive intersections. Since a pair of ts can
2341 // be nearly equal, any problems caused by this should be taken care
2342 // of later.
2343 // On the edge or out of range values are negative; add 2 to get end
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002344 int addT(double newT, const Work& other) {
2345 return fContour->addT(fIndex, newT, other.fContour, other.fIndex);
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002346 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +00002347
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002348 bool advance() {
2349 return ++fIndex < fLast;
2350 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +00002351
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002352 SkScalar bottom() const {
2353 return bounds().fBottom;
2354 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +00002355
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002356 const Bounds& bounds() const {
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002357 return fContour->segments()[fIndex].bounds();
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002358 }
2359
2360 const SkPoint* cubic() const {
2361 return fCubic;
2362 }
2363
2364 void init(Contour* contour) {
2365 fContour = contour;
2366 fIndex = 0;
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002367 fLast = contour->segments().count();
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002368 }
caryclark@google.com66ca2fb2012-07-03 14:30:08 +00002369
2370 bool isAdjacent(const Work& next) {
2371 return fContour == next.fContour && fIndex + 1 == next.fIndex;
2372 }
2373
2374 bool isFirstLast(const Work& next) {
2375 return fContour == next.fContour && fIndex == 0
2376 && next.fIndex == fLast - 1;
2377 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002378
2379 SkScalar left() const {
2380 return bounds().fLeft;
2381 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +00002382
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002383 void promoteToCubic() {
2384 fCubic[0] = pts()[0];
2385 fCubic[2] = pts()[1];
2386 fCubic[3] = pts()[2];
2387 fCubic[1].fX = (fCubic[0].fX + fCubic[2].fX * 2) / 3;
2388 fCubic[1].fY = (fCubic[0].fY + fCubic[2].fY * 2) / 3;
2389 fCubic[2].fX = (fCubic[3].fX + fCubic[2].fX * 2) / 3;
2390 fCubic[2].fY = (fCubic[3].fY + fCubic[2].fY * 2) / 3;
2391 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +00002392
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002393 const SkPoint* pts() const {
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002394 return fContour->segments()[fIndex].pts();
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002395 }
2396
2397 SkScalar right() const {
2398 return bounds().fRight;
2399 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +00002400
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002401 ptrdiff_t segmentIndex() const {
2402 return fIndex;
2403 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +00002404
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002405 SegmentType segmentType() const {
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002406 const Segment& segment = fContour->segments()[fIndex];
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002407 SegmentType type = (SegmentType) segment.verb();
2408 if (type != kLine_Segment) {
2409 return type;
2410 }
2411 if (segment.isHorizontal()) {
2412 return kHorizontalLine_Segment;
2413 }
2414 if (segment.isVertical()) {
2415 return kVerticalLine_Segment;
2416 }
2417 return kLine_Segment;
2418 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +00002419
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002420 bool startAfter(const Work& after) {
2421 fIndex = after.fIndex;
2422 return advance();
2423 }
2424
2425 SkScalar top() const {
2426 return bounds().fTop;
2427 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +00002428
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002429 SkPath::Verb verb() const {
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002430 return fContour->segments()[fIndex].verb();
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002431 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +00002432
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002433 SkScalar x() const {
2434 return bounds().fLeft;
2435 }
2436
2437 bool xFlipped() const {
2438 return x() != pts()[0].fX;
2439 }
2440
2441 SkScalar y() const {
2442 return bounds().fTop;
2443 }
2444
2445 bool yFlipped() const {
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002446 return y() != pts()[0].fY;
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002447 }
2448
2449protected:
2450 Contour* fContour;
2451 SkPoint fCubic[4];
2452 int fIndex;
2453 int fLast;
2454};
2455
caryclark@google.com65f9f0a2012-05-23 18:09:25 +00002456#if DEBUG_ADD_INTERSECTING_TS
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002457static void debugShowLineIntersection(int pts, const Work& wt,
2458 const Work& wn, const double wtTs[2], const double wnTs[2]) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002459 if (!pts) {
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002460 SkDebugf("%s no intersect (%1.9g,%1.9g %1.9g,%1.9g) (%1.9g,%1.9g %1.9g,%1.9g)\n",
2461 __FUNCTION__, wt.pts()[0].fX, wt.pts()[0].fY,
2462 wt.pts()[1].fX, wt.pts()[1].fY, wn.pts()[0].fX, wn.pts()[0].fY,
2463 wn.pts()[1].fX, wn.pts()[1].fY);
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002464 return;
2465 }
2466 SkPoint wtOutPt, wnOutPt;
2467 LineXYAtT(wt.pts(), wtTs[0], &wtOutPt);
2468 LineXYAtT(wn.pts(), wnTs[0], &wnOutPt);
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002469 SkDebugf("%s wtTs[0]=%g (%g,%g, %g,%g) (%g,%g)",
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002470 __FUNCTION__,
2471 wtTs[0], wt.pts()[0].fX, wt.pts()[0].fY,
2472 wt.pts()[1].fX, wt.pts()[1].fY, wtOutPt.fX, wtOutPt.fY);
2473 if (pts == 2) {
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002474 SkDebugf(" wtTs[1]=%g", wtTs[1]);
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002475 }
caryclark@google.comb9738012012-07-03 19:53:30 +00002476 SkDebugf(" wnTs[0]=%g (%g,%g, %g,%g) (%g,%g)",
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002477 wnTs[0], wn.pts()[0].fX, wn.pts()[0].fY,
2478 wn.pts()[1].fX, wn.pts()[1].fY, wnOutPt.fX, wnOutPt.fY);
2479 if (pts == 2) {
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002480 SkDebugf(" wnTs[1]=%g", wnTs[1]);
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002481 }
caryclark@google.comb9738012012-07-03 19:53:30 +00002482 SkDebugf("\n");
2483}
caryclark@google.com65f9f0a2012-05-23 18:09:25 +00002484#else
2485static void debugShowLineIntersection(int , const Work& ,
2486 const Work& , const double [2], const double [2]) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002487}
caryclark@google.com65f9f0a2012-05-23 18:09:25 +00002488#endif
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002489
caryclark@google.com65f9f0a2012-05-23 18:09:25 +00002490static bool addIntersectTs(Contour* test, Contour* next) {
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002491
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002492 if (test != next) {
2493 if (test->bounds().fBottom < next->bounds().fTop) {
2494 return false;
2495 }
2496 if (!Bounds::Intersects(test->bounds(), next->bounds())) {
2497 return true;
2498 }
2499 }
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002500 Work wt;
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002501 wt.init(test);
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002502 bool foundCommonContour = test == next;
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002503 do {
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002504 Work wn;
2505 wn.init(next);
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002506 if (test == next && !wn.startAfter(wt)) {
2507 continue;
2508 }
2509 do {
2510 if (!Bounds::Intersects(wt.bounds(), wn.bounds())) {
2511 continue;
2512 }
2513 int pts;
2514 Intersections ts;
2515 bool swap = false;
2516 switch (wt.segmentType()) {
2517 case Work::kHorizontalLine_Segment:
2518 swap = true;
2519 switch (wn.segmentType()) {
2520 case Work::kHorizontalLine_Segment:
2521 case Work::kVerticalLine_Segment:
2522 case Work::kLine_Segment: {
2523 pts = HLineIntersect(wn.pts(), wt.left(),
2524 wt.right(), wt.y(), wt.xFlipped(), ts);
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002525 debugShowLineIntersection(pts, wt, wn,
2526 ts.fT[1], ts.fT[0]);
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002527 break;
2528 }
2529 case Work::kQuad_Segment: {
2530 pts = HQuadIntersect(wn.pts(), wt.left(),
2531 wt.right(), wt.y(), wt.xFlipped(), ts);
2532 break;
2533 }
2534 case Work::kCubic_Segment: {
2535 pts = HCubicIntersect(wn.pts(), wt.left(),
2536 wt.right(), wt.y(), wt.xFlipped(), ts);
2537 break;
2538 }
2539 default:
2540 SkASSERT(0);
2541 }
2542 break;
2543 case Work::kVerticalLine_Segment:
2544 swap = true;
2545 switch (wn.segmentType()) {
2546 case Work::kHorizontalLine_Segment:
2547 case Work::kVerticalLine_Segment:
2548 case Work::kLine_Segment: {
2549 pts = VLineIntersect(wn.pts(), wt.top(),
2550 wt.bottom(), wt.x(), wt.yFlipped(), ts);
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002551 debugShowLineIntersection(pts, wt, wn,
2552 ts.fT[1], ts.fT[0]);
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002553 break;
2554 }
2555 case Work::kQuad_Segment: {
2556 pts = VQuadIntersect(wn.pts(), wt.top(),
2557 wt.bottom(), wt.x(), wt.yFlipped(), ts);
2558 break;
2559 }
2560 case Work::kCubic_Segment: {
2561 pts = VCubicIntersect(wn.pts(), wt.top(),
2562 wt.bottom(), wt.x(), wt.yFlipped(), ts);
2563 break;
2564 }
2565 default:
2566 SkASSERT(0);
2567 }
2568 break;
2569 case Work::kLine_Segment:
2570 switch (wn.segmentType()) {
2571 case Work::kHorizontalLine_Segment:
2572 pts = HLineIntersect(wt.pts(), wn.left(),
2573 wn.right(), wn.y(), wn.xFlipped(), ts);
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002574 debugShowLineIntersection(pts, wt, wn,
2575 ts.fT[1], ts.fT[0]);
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002576 break;
2577 case Work::kVerticalLine_Segment:
2578 pts = VLineIntersect(wt.pts(), wn.top(),
2579 wn.bottom(), wn.x(), wn.yFlipped(), ts);
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002580 debugShowLineIntersection(pts, wt, wn,
2581 ts.fT[1], ts.fT[0]);
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002582 break;
2583 case Work::kLine_Segment: {
2584 pts = LineIntersect(wt.pts(), wn.pts(), ts);
2585 debugShowLineIntersection(pts, wt, wn,
2586 ts.fT[1], ts.fT[0]);
2587 break;
2588 }
2589 case Work::kQuad_Segment: {
2590 swap = true;
2591 pts = QuadLineIntersect(wn.pts(), wt.pts(), ts);
2592 break;
2593 }
2594 case Work::kCubic_Segment: {
2595 swap = true;
2596 pts = CubicLineIntersect(wn.pts(), wt.pts(), ts);
2597 break;
2598 }
2599 default:
2600 SkASSERT(0);
2601 }
2602 break;
2603 case Work::kQuad_Segment:
2604 switch (wn.segmentType()) {
2605 case Work::kHorizontalLine_Segment:
2606 pts = HQuadIntersect(wt.pts(), wn.left(),
2607 wn.right(), wn.y(), wn.xFlipped(), ts);
2608 break;
2609 case Work::kVerticalLine_Segment:
2610 pts = VQuadIntersect(wt.pts(), wn.top(),
2611 wn.bottom(), wn.x(), wn.yFlipped(), ts);
2612 break;
2613 case Work::kLine_Segment: {
2614 pts = QuadLineIntersect(wt.pts(), wn.pts(), ts);
2615 break;
2616 }
2617 case Work::kQuad_Segment: {
2618 pts = QuadIntersect(wt.pts(), wn.pts(), ts);
2619 break;
2620 }
2621 case Work::kCubic_Segment: {
2622 wt.promoteToCubic();
2623 pts = CubicIntersect(wt.cubic(), wn.pts(), ts);
2624 break;
2625 }
2626 default:
2627 SkASSERT(0);
2628 }
2629 break;
2630 case Work::kCubic_Segment:
2631 switch (wn.segmentType()) {
2632 case Work::kHorizontalLine_Segment:
2633 pts = HCubicIntersect(wt.pts(), wn.left(),
2634 wn.right(), wn.y(), wn.xFlipped(), ts);
2635 break;
2636 case Work::kVerticalLine_Segment:
2637 pts = VCubicIntersect(wt.pts(), wn.top(),
2638 wn.bottom(), wn.x(), wn.yFlipped(), ts);
2639 break;
2640 case Work::kLine_Segment: {
2641 pts = CubicLineIntersect(wt.pts(), wn.pts(), ts);
2642 break;
2643 }
2644 case Work::kQuad_Segment: {
2645 wn.promoteToCubic();
2646 pts = CubicIntersect(wt.pts(), wn.cubic(), ts);
2647 break;
2648 }
2649 case Work::kCubic_Segment: {
2650 pts = CubicIntersect(wt.pts(), wn.pts(), ts);
2651 break;
2652 }
2653 default:
2654 SkASSERT(0);
2655 }
2656 break;
2657 default:
2658 SkASSERT(0);
2659 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002660 if (!foundCommonContour && pts > 0) {
2661 test->addCross(next);
2662 next->addCross(test);
2663 foundCommonContour = true;
2664 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002665 // in addition to recording T values, record matching segment
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00002666 if (pts == 2 && wn.segmentType() <= Work::kLine_Segment
2667 && wt.segmentType() <= Work::kLine_Segment) {
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002668 wt.addCoincident(wn, ts, swap);
2669 continue;
caryclark@google.coma3f05fa2012-06-01 17:44:28 +00002670 }
caryclark@google.com15fa1382012-05-07 20:49:36 +00002671 for (int pt = 0; pt < pts; ++pt) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002672 SkASSERT(ts.fT[0][pt] >= 0 && ts.fT[0][pt] <= 1);
2673 SkASSERT(ts.fT[1][pt] >= 0 && ts.fT[1][pt] <= 1);
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002674 int testTAt = wt.addT(ts.fT[swap][pt], wn);
2675 int nextTAt = wn.addT(ts.fT[!swap][pt], wt);
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002676 wt.addOtherT(testTAt, ts.fT[!swap][pt], nextTAt);
2677 wn.addOtherT(nextTAt, ts.fT[swap][pt], testTAt);
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002678 }
2679 } while (wn.advance());
2680 } while (wt.advance());
2681 return true;
2682}
2683
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002684// resolve any coincident pairs found while intersecting, and
caryclark@google.coma833b5c2012-04-30 19:38:50 +00002685// see if coincidence is formed by clipping non-concident segments
2686static void coincidenceCheck(SkTDArray<Contour*>& contourList, int winding) {
2687 int contourCount = contourList.count();
caryclark@google.comf25edfe2012-06-01 18:20:10 +00002688 for (int cIndex = 0; cIndex < contourCount; ++cIndex) {
caryclark@google.coma833b5c2012-04-30 19:38:50 +00002689 Contour* contour = contourList[cIndex];
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00002690 contour->findTooCloseToCall(winding);
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002691 }
2692 for (int cIndex = 0; cIndex < contourCount; ++cIndex) {
2693 Contour* contour = contourList[cIndex];
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00002694 contour->resolveCoincidence(winding);
caryclark@google.coma833b5c2012-04-30 19:38:50 +00002695 }
2696}
2697
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002698// project a ray from the top of the contour up and see if it hits anything
2699// note: when we compute line intersections, we keep track of whether
2700// two contours touch, so we need only look at contours not touching this one.
2701// OPTIMIZATION: sort contourList vertically to avoid linear walk
2702static int innerContourCheck(SkTDArray<Contour*>& contourList,
2703 Contour* baseContour, const SkPoint& basePt) {
2704 int contourCount = contourList.count();
2705 int winding = 0;
2706 SkScalar bestY = SK_ScalarMin;
2707 for (int cTest = 0; cTest < contourCount; ++cTest) {
2708 Contour* contour = contourList[cTest];
2709 if (basePt.fY < contour->bounds().fTop) {
2710 continue;
2711 }
2712 if (bestY > contour->bounds().fBottom) {
2713 continue;
2714 }
2715 if (baseContour->crosses(contour)) {
2716 continue;
2717 }
2718 int tIndex;
2719 double tHit;
2720 const Segment* test = contour->crossedSegment(basePt, bestY, tIndex,
2721 tHit);
2722 if (!test) {
2723 continue;
2724 }
2725 // If the ray hit the end of a span, we need to construct the wheel of
2726 // angles to find the span closest to the ray -- even if there are just
2727 // two spokes on the wheel.
2728 if (tHit == test->t(tIndex)) {
2729 SkTDArray<Angle> angles;
2730 int end = test->nextSpan(tIndex, 1);
2731 if (end < 0) {
2732 end = test->nextSpan(tIndex, -1);
2733 }
2734 test->addTwoAngles(tIndex, end, angles);
2735 // test->buildAnglesInner(tIndex, angles);
2736 test->buildAngles(tIndex, angles);
2737 SkTDArray<Angle*> sorted;
2738 sortAngles(angles, sorted);
2739 const Angle* angle = sorted[0];
2740 test = angle->segment();
2741 SkScalar testDx = (*SegmentDXAtT[test->verb()])(test->pts(), tHit);
2742 if (testDx == 0) {
2743 angle = *(sorted.end() - 1);
2744 test = angle->segment();
2745 SkASSERT((*SegmentDXAtT[test->verb()])(test->pts(), tHit) != 0);
2746 }
2747 tIndex = angle->start(); // lesser Y
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00002748 winding = test->windSum(SkMin32(tIndex, angle->end()));
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002749 #if DEBUG_WINDING
2750 SkDebugf("%s 1 winding=%d\n", __FUNCTION__, winding);
2751 #endif
2752 } else {
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00002753 winding = test->windSum(tIndex);
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002754 #if DEBUG_WINDING
2755 SkDebugf("%s 2 winding=%d\n", __FUNCTION__, winding);
2756 #endif
2757 }
2758 // see if a + change in T results in a +/- change in X (compute x'(T))
2759 SkScalar dx = (*SegmentDXAtT[test->verb()])(test->pts(), tHit);
2760 #if DEBUG_WINDING
2761 SkDebugf("%s dx=%1.9g\n", __FUNCTION__, dx);
2762 #endif
2763 SkASSERT(dx != 0);
2764 if (winding * dx > 0) { // if same signs, result is negative
2765 winding += dx > 0 ? -1 : 1;
2766 #if DEBUG_WINDING
2767 SkDebugf("%s 3 winding=%d\n", __FUNCTION__, winding);
2768 #endif
2769 }
2770 }
2771 baseContour->setWinding(winding);
2772 return winding;
2773}
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002774
2775// OPTIMIZATION: not crazy about linear search here to find top active y.
2776// seems like we should break down and do the sort, or maybe sort each
2777// contours' segments?
2778// Once the segment array is built, there's no reason I can think of not to
2779// sort it in Y. hmmm
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002780// FIXME: return the contour found to pass to inner contour check
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002781static Segment* findTopContour(SkTDArray<Contour*>& contourList,
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002782 Contour*& topContour) {
2783 int contourCount = contourList.count();
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002784 int cIndex = 0;
2785 Segment* topStart;
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002786 SkScalar bestY = SK_ScalarMax;
2787 Contour* contour;
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002788 do {
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002789 contour = contourList[cIndex];
2790 topStart = contour->topSegment(bestY);
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002791 } while (!topStart && ++cIndex < contourCount);
2792 if (!topStart) {
2793 return NULL;
2794 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002795 topContour = contour;
2796 while (++cIndex < contourCount) {
2797 contour = contourList[cIndex];
2798 if (bestY < contour->bounds().fTop) {
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002799 continue;
2800 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002801 SkScalar testY = SK_ScalarMax;
2802 Segment* test = contour->topSegment(testY);
2803 if (!test || bestY <= testY) {
2804 continue;
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002805 }
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002806 topContour = contour;
2807 topStart = test;
2808 bestY = testY;
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002809 }
2810 return topStart;
2811}
2812
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00002813static Segment* findChase(SkTDArray<Span*>& chase, int& tIndex, int& endIndex) {
2814 while (chase.count()) {
caryclark@google.com9764cc62012-07-12 19:29:45 +00002815 Span* span = chase[chase.count() - 1];
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00002816 const Span& backPtr = span->fOther->span(span->fOtherIndex);
2817 Segment* segment = backPtr.fOther;
2818 tIndex = backPtr.fOtherIndex;
caryclark@google.com9764cc62012-07-12 19:29:45 +00002819 SkTDArray<Angle> angles;
2820 int done = 0;
2821 if (segment->activeAngle(tIndex, done, angles)) {
2822 Angle* last = angles.end() - 1;
2823 tIndex = last->start();
2824 endIndex = last->end();
2825 return last->segment();
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00002826 }
caryclark@google.com9764cc62012-07-12 19:29:45 +00002827 if (done == angles.count()) {
2828 chase.pop(&span);
2829 continue;
2830 }
2831 SkTDArray<Angle*> sorted;
2832 sortAngles(angles, sorted);
2833 // find first angle, initialize winding to computed fWindSum
2834 int firstIndex = -1;
2835 const Angle* angle;
2836 int winding;
2837 do {
2838 angle = sorted[++firstIndex];
2839 winding = angle->segment()->windSum(angle);
2840 } while (winding == SK_MinS32);
2841 int firstSign = angle->sign();
2842 if (firstSign * winding > 0) {
2843 winding -= firstSign;
2844 }
caryclark@google.com210acaf2012-07-12 21:05:13 +00002845 // SkDebugf("%s firstSign=%d\n", __FUNCTION__, firstSign);
caryclark@google.com9764cc62012-07-12 19:29:45 +00002846 // we care about first sign and whether wind sum indicates this
2847 // edge is inside or outside. Maybe need to pass span winding
2848 // or first winding or something into this function?
2849 // advance to first undone angle, then return it and winding
2850 // (to set whether edges are active or not)
2851 int nextIndex = firstIndex + 1;
2852 int angleCount = sorted.count();
2853 int lastIndex = firstIndex != 0 ? firstIndex : angleCount;
2854 do {
2855 SkASSERT(nextIndex != firstIndex);
2856 if (nextIndex == angleCount) {
2857 nextIndex = 0;
2858 }
2859 const Angle* angle = sorted[nextIndex];
2860 segment = angle->segment();
2861 int windValue = segment->windValue(angle);
2862 SkASSERT(windValue > 0);
2863 int maxWinding = winding;
2864 winding -= angle->sign() * windValue;
2865 if (maxWinding * winding < 0) {
2866 SkDebugf("%s flipped sign %d %d\n", __FUNCTION__, maxWinding, winding);
2867 }
2868 tIndex = angle->start();
2869 endIndex = angle->end();
2870 int lesser = SkMin32(tIndex, endIndex);
2871 const Span& nextSpan = segment->span(lesser);
2872 if (!nextSpan.fDone) {
2873 // FIXME: this be wrong. assign startWinding if edge is in
2874 // same direction. If the direction is opposite, winding to
2875 // assign is flipped sign or +/- 1?
2876 if (abs(maxWinding) < abs(winding)) {
2877 maxWinding = winding;
2878 }
2879 segment->markWinding(lesser, maxWinding);
2880 break;
2881 }
2882 } while (++nextIndex != lastIndex);
2883 return segment;
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00002884 }
2885 return NULL;
2886}
2887
caryclark@google.com027de222012-07-12 12:52:50 +00002888#if DEBUG_ACTIVE_SPANS
2889static void debugShowActiveSpans(SkTDArray<Contour*>& contourList) {
2890 for (int index = 0; index < contourList.count(); ++ index) {
2891 contourList[index]->debugShowActiveSpans();
2892 }
2893}
2894#endif
2895
caryclark@google.comfa0588f2012-04-26 21:01:06 +00002896// Each segment may have an inside or an outside. Segments contained within
2897// winding may have insides on either side, and form a contour that should be
2898// ignored. Segments that are coincident with opposing direction segments may
2899// have outsides on either side, and should also disappear.
2900// 'Normal' segments will have one inside and one outside. Subsequent connections
2901// when winding should follow the intersection direction. If more than one edge
2902// is an option, choose first edge that continues the inside.
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002903 // since we start with leftmost top edge, we'll traverse through a
2904 // smaller angle counterclockwise to get to the next edge.
caryclark@google.com1577e8f2012-05-22 17:01:14 +00002905static void bridge(SkTDArray<Contour*>& contourList, SkPath& simple) {
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002906 bool firstContour = true;
caryclark@google.com15fa1382012-05-07 20:49:36 +00002907 do {
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002908 Contour* topContour;
2909 Segment* topStart = findTopContour(contourList, topContour);
caryclark@google.com15fa1382012-05-07 20:49:36 +00002910 if (!topStart) {
2911 break;
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00002912 }
caryclark@google.com15fa1382012-05-07 20:49:36 +00002913 // Start at the top. Above the top is outside, below is inside.
caryclark@google.com495f8e42012-05-31 13:13:11 +00002914 // follow edges to intersection by changing the index by direction.
2915 int index, endIndex;
caryclark@google.com88f7d0c2012-06-07 21:09:20 +00002916 Segment* current = topStart->findTop(index, endIndex);
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00002917 int contourWinding;
2918 if (firstContour) {
2919 contourWinding = 0;
2920 firstContour = false;
2921 } else {
2922 const SkPoint& topPoint = current->xyAtT(endIndex);
2923 contourWinding = innerContourCheck(contourList, topContour, topPoint);
2924#if DEBUG_WINDING
2925 SkDebugf("%s contourWinding=%d\n", __FUNCTION__, contourWinding);
2926#endif
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002927 }
caryclark@google.com88f7d0c2012-06-07 21:09:20 +00002928 SkPoint lastPt;
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002929 bool firstTime = true;
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00002930 int winding = contourWinding;
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002931 int spanWinding = current->spanSign(index, endIndex);
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00002932 // int firstWinding = contourWinding + spanWinding;
2933 // FIXME: needs work. While it works in limited situations, it does
2934 // not always compute winding correctly. Active should be removed and instead
2935 // the initial winding should be correctly passed in so that if the
2936 // inner contour is wound the same way, it never finds an accumulated
2937 // winding of zero. Inside 'find next', we need to look for transitions
2938 // other than zero when resolving sorted angles.
2939 SkTDArray<Span*> chaseArray;
caryclark@google.comb45a1b42012-05-18 20:50:33 +00002940 do {
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00002941 bool active = winding * spanWinding <= 0;
2942 const SkPoint* firstPt = NULL;
2943 do {
2944 SkASSERT(!current->done());
2945 int nextStart, nextEnd, flipped = 1;
2946 Segment* next = current->findNext(chaseArray,
2947 winding + spanWinding, index,
caryclark@google.com5c286d32012-07-13 11:57:28 +00002948 endIndex, nextStart, nextEnd, flipped, firstTime
2949 , active /* active is debugging only */ );
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00002950 if (!next) {
2951 break;
2952 }
2953 if (!firstPt) {
2954 firstPt = &current->addMoveTo(index, simple, active);
2955 }
2956 lastPt = current->addCurveTo(index, endIndex, simple, active);
2957 current = next;
2958 index = nextStart;
2959 endIndex = nextEnd;
2960 spanWinding = SkSign32(spanWinding) * flipped * next->windValue(
2961 SkMin32(nextStart, nextEnd));
2962 #if DEBUG_WINDING
2963 SkDebugf("%s spanWinding=%d\n", __FUNCTION__, spanWinding);
2964 #endif
2965 firstTime = false;
2966 } while (*firstPt != lastPt && (active || !current->done()));
2967 if (firstPt && active) {
2968 #if DEBUG_PATH_CONSTRUCTION
2969 SkDebugf("%s close\n", __FUNCTION__);
2970 #endif
2971 simple.close();
2972 }
2973 current = findChase(chaseArray, index, endIndex);
caryclark@google.com027de222012-07-12 12:52:50 +00002974#if DEBUG_ACTIVE_SPANS
2975 debugShowActiveSpans(contourList);
2976#endif
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00002977 if (!current) {
caryclark@google.com495f8e42012-05-31 13:13:11 +00002978 break;
2979 }
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00002980 int lesser = SkMin32(index, endIndex);
2981 spanWinding = current->windSum(lesser);
2982 int spanValue = current->windValue(lesser);
2983 SkASSERT(spanWinding != SK_MinS32);
2984 int spanSign = current->spanSign(index, endIndex);
2985 #if DEBUG_WINDING
2986 SkDebugf("%s spanWinding=%d spanSign=%d winding=%d spanValue=%d\n",
2987 __FUNCTION__, spanWinding, spanSign, winding, spanValue);
2988 #endif
2989 if (spanWinding * spanSign < 0) {
2990 #if DEBUG_WINDING
2991 SkDebugf("%s spanWinding * spanSign < 0\n", __FUNCTION__);
2992 #endif
caryclark@google.com9764cc62012-07-12 19:29:45 +00002993 // SkTSwap<int>(index, endIndex);
caryclark@google.com495f8e42012-05-31 13:13:11 +00002994 }
caryclark@google.comfa4a6e92012-07-11 17:52:32 +00002995 if (abs(spanWinding) > spanValue) {
2996 #if DEBUG_WINDING
2997 SkDebugf("%s abs(spanWinding) > spanValue\n", __FUNCTION__);
2998 #endif
2999 winding = spanWinding;
3000 spanWinding = spanValue * SkSign32(spanWinding);
3001 winding -= spanWinding;
3002 }
3003 } while (true);
caryclark@google.com1577e8f2012-05-22 17:01:14 +00003004 } while (true);
caryclark@google.comfa0588f2012-04-26 21:01:06 +00003005}
3006
caryclark@google.comb45a1b42012-05-18 20:50:33 +00003007static void fixOtherTIndex(SkTDArray<Contour*>& contourList) {
3008 int contourCount = contourList.count();
3009 for (int cTest = 0; cTest < contourCount; ++cTest) {
3010 Contour* contour = contourList[cTest];
3011 contour->fixOtherTIndex();
3012 }
3013}
3014
caryclark@google.com1577e8f2012-05-22 17:01:14 +00003015static void makeContourList(SkTArray<Contour>& contours,
caryclark@google.comfa0588f2012-04-26 21:01:06 +00003016 SkTDArray<Contour*>& list) {
caryclark@google.coma833b5c2012-04-30 19:38:50 +00003017 int count = contours.count();
caryclark@google.comfa0588f2012-04-26 21:01:06 +00003018 if (count == 0) {
3019 return;
3020 }
caryclark@google.coma833b5c2012-04-30 19:38:50 +00003021 for (int index = 0; index < count; ++index) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +00003022 *list.append() = &contours[index];
3023 }
caryclark@google.comfa0588f2012-04-26 21:01:06 +00003024 QSort<Contour>(list.begin(), list.end() - 1);
3025}
3026
caryclark@google.com65f9f0a2012-05-23 18:09:25 +00003027void simplifyx(const SkPath& path, SkPath& simple) {
caryclark@google.comfa0588f2012-04-26 21:01:06 +00003028 // returns 1 for evenodd, -1 for winding, regardless of inverse-ness
caryclark@google.coma833b5c2012-04-30 19:38:50 +00003029 int winding = (path.getFillType() & 1) ? 1 : -1;
caryclark@google.comfa0588f2012-04-26 21:01:06 +00003030 simple.reset();
3031 simple.setFillType(SkPath::kEvenOdd_FillType);
3032
3033 // turn path into list of segments
3034 SkTArray<Contour> contours;
3035 // FIXME: add self-intersecting cubics' T values to segment
3036 EdgeBuilder builder(path, contours);
3037 SkTDArray<Contour*> contourList;
caryclark@google.com1577e8f2012-05-22 17:01:14 +00003038 makeContourList(contours, contourList);
caryclark@google.comfa0588f2012-04-26 21:01:06 +00003039 Contour** currentPtr = contourList.begin();
3040 if (!currentPtr) {
3041 return;
3042 }
caryclark@google.com1577e8f2012-05-22 17:01:14 +00003043 Contour** listEnd = contourList.end();
caryclark@google.comfa0588f2012-04-26 21:01:06 +00003044 // find all intersections between segments
3045 do {
3046 Contour** nextPtr = currentPtr;
3047 Contour* current = *currentPtr++;
3048 Contour* next;
3049 do {
3050 next = *nextPtr++;
caryclark@google.com65f9f0a2012-05-23 18:09:25 +00003051 } while (addIntersectTs(current, next) && nextPtr != listEnd);
caryclark@google.com1577e8f2012-05-22 17:01:14 +00003052 } while (currentPtr != listEnd);
caryclark@google.coma833b5c2012-04-30 19:38:50 +00003053 // eat through coincident edges
3054 coincidenceCheck(contourList, winding);
caryclark@google.com66ca2fb2012-07-03 14:30:08 +00003055 fixOtherTIndex(contourList);
caryclark@google.comfa0588f2012-04-26 21:01:06 +00003056 // construct closed contours
caryclark@google.com1577e8f2012-05-22 17:01:14 +00003057 bridge(contourList, simple);
caryclark@google.comfa0588f2012-04-26 21:01:06 +00003058}
3059