blob: f34148390cb8c6cbdb21d4d0c129a746216dcd87 [file] [log] [blame]
caryclark@google.com07393ca2013-04-08 11:47:37 +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.coma2bbc6e2013-11-01 17:36:03 +00007#include "SkAddIntersections.h"
caryclark@google.com07393ca2013-04-08 11:47:37 +00008#include "SkOpEdgeBuilder.h"
9#include "SkPathOpsCommon.h"
10#include "SkPathWriter.h"
commit-bot@chromium.orgb76d3b62013-04-22 19:55:19 +000011#include "SkTSort.h"
caryclark@google.com07393ca2013-04-08 11:47:37 +000012
caryclark@google.comd892bd82013-06-17 14:10:36 +000013static int contourRangeCheckY(const SkTArray<SkOpContour*, true>& contourList, SkOpSegment** currentPtr,
caryclark@google.com07393ca2013-04-08 11:47:37 +000014 int* indexPtr, int* endIndexPtr, double* bestHit, SkScalar* bestDx,
15 bool* tryAgain, double* midPtr, bool opp) {
16 const int index = *indexPtr;
17 const int endIndex = *endIndexPtr;
18 const double mid = *midPtr;
19 const SkOpSegment* current = *currentPtr;
20 double tAtMid = current->tAtMid(index, endIndex, mid);
caryclark@google.com4fdbb222013-07-23 15:27:41 +000021 SkPoint basePt = current->ptAtT(tAtMid);
caryclark@google.com07393ca2013-04-08 11:47:37 +000022 int contourCount = contourList.count();
23 SkScalar bestY = SK_ScalarMin;
24 SkOpSegment* bestSeg = NULL;
caryclark@google.com3e475dc2013-04-11 14:09:50 +000025 int bestTIndex = 0;
caryclark@google.com07393ca2013-04-08 11:47:37 +000026 bool bestOpp;
27 bool hitSomething = false;
28 for (int cTest = 0; cTest < contourCount; ++cTest) {
29 SkOpContour* contour = contourList[cTest];
30 bool testOpp = contour->operand() ^ current->operand() ^ opp;
31 if (basePt.fY < contour->bounds().fTop) {
32 continue;
33 }
34 if (bestY > contour->bounds().fBottom) {
35 continue;
36 }
37 int segmentCount = contour->segments().count();
38 for (int test = 0; test < segmentCount; ++test) {
39 SkOpSegment* testSeg = &contour->segments()[test];
40 SkScalar testY = bestY;
41 double testHit;
42 int testTIndex = testSeg->crossedSpanY(basePt, &testY, &testHit, &hitSomething, tAtMid,
43 testOpp, testSeg == current);
44 if (testTIndex < 0) {
45 if (testTIndex == SK_MinS32) {
46 hitSomething = true;
47 bestSeg = NULL;
48 goto abortContours; // vertical encountered, return and try different point
49 }
50 continue;
51 }
52 if (testSeg == current && current->betweenTs(index, testHit, endIndex)) {
53 double baseT = current->t(index);
54 double endT = current->t(endIndex);
55 double newMid = (testHit - baseT) / (endT - baseT);
56#if DEBUG_WINDING
57 double midT = current->tAtMid(index, endIndex, mid);
58 SkPoint midXY = current->xyAtT(midT);
59 double newMidT = current->tAtMid(index, endIndex, newMid);
60 SkPoint newXY = current->xyAtT(newMidT);
61 SkDebugf("%s [%d] mid=%1.9g->%1.9g s=%1.9g (%1.9g,%1.9g) m=%1.9g (%1.9g,%1.9g)"
62 " n=%1.9g (%1.9g,%1.9g) e=%1.9g (%1.9g,%1.9g)\n", __FUNCTION__,
63 current->debugID(), mid, newMid,
64 baseT, current->xAtT(index), current->yAtT(index),
65 baseT + mid * (endT - baseT), midXY.fX, midXY.fY,
66 baseT + newMid * (endT - baseT), newXY.fX, newXY.fY,
67 endT, current->xAtT(endIndex), current->yAtT(endIndex));
68#endif
69 *midPtr = newMid * 2; // calling loop with divide by 2 before continuing
70 return SK_MinS32;
71 }
72 bestSeg = testSeg;
73 *bestHit = testHit;
74 bestOpp = testOpp;
75 bestTIndex = testTIndex;
76 bestY = testY;
77 }
78 }
79abortContours:
80 int result;
81 if (!bestSeg) {
82 result = hitSomething ? SK_MinS32 : 0;
83 } else {
84 if (bestSeg->windSum(bestTIndex) == SK_MinS32) {
85 *currentPtr = bestSeg;
86 *indexPtr = bestTIndex;
87 *endIndexPtr = bestSeg->nextSpan(bestTIndex, 1);
88 SkASSERT(*indexPtr != *endIndexPtr && *indexPtr >= 0 && *endIndexPtr >= 0);
89 *tryAgain = true;
90 return 0;
91 }
92 result = bestSeg->windingAtT(*bestHit, bestTIndex, bestOpp, bestDx);
caryclark@google.comf11a5af2013-04-10 18:55:11 +000093 SkASSERT(result == SK_MinS32 || *bestDx);
caryclark@google.com07393ca2013-04-08 11:47:37 +000094 }
95 double baseT = current->t(index);
96 double endT = current->t(endIndex);
97 *bestHit = baseT + mid * (endT - baseT);
98 return result;
99}
100
caryclark@google.comd892bd82013-06-17 14:10:36 +0000101SkOpSegment* FindUndone(SkTArray<SkOpContour*, true>& contourList, int* start, int* end) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000102 int contourCount = contourList.count();
103 SkOpSegment* result;
104 for (int cIndex = 0; cIndex < contourCount; ++cIndex) {
105 SkOpContour* contour = contourList[cIndex];
106 result = contour->undoneSegment(start, end);
107 if (result) {
108 return result;
109 }
110 }
111 return NULL;
112}
113
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000114SkOpSegment* FindChase(SkTDArray<SkOpSpan*>* chase, int* tIndex, int* endIndex) {
115 while (chase->count()) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000116 SkOpSpan* span;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000117 chase->pop(&span);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000118 const SkOpSpan& backPtr = span->fOther->span(span->fOtherIndex);
119 SkOpSegment* segment = backPtr.fOther;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000120 *tIndex = backPtr.fOtherIndex;
121 bool sortable = true;
122 bool done = true;
123 *endIndex = -1;
124 if (const SkOpAngle* last = segment->activeAngle(*tIndex, tIndex, endIndex, &done,
125 &sortable)) {
126 *tIndex = last->start();
127 *endIndex = last->end();
128 #if TRY_ROTATE
129 *chase->insert(0) = span;
130 #else
131 *chase->append() = span;
132 #endif
caryclark@google.com07393ca2013-04-08 11:47:37 +0000133 return last->segment();
134 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000135 if (done) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000136 continue;
137 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000138 if (!sortable) {
139 continue;
140 }
141 // find first angle, initialize winding to computed fWindSum
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000142 const SkOpAngle* angle = segment->spanToAngle(*tIndex, *endIndex);
143 const SkOpAngle* firstAngle;
144 SkDEBUGCODE(firstAngle = angle);
145 SkDEBUGCODE(bool loop = false);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000146 int winding;
147 do {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000148 angle = angle->next();
149 SkASSERT(angle != firstAngle || !loop);
150 SkDEBUGCODE(loop |= angle == firstAngle);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000151 segment = angle->segment();
152 winding = segment->windSum(angle);
153 } while (winding == SK_MinS32);
154 int spanWinding = segment->spanSign(angle->start(), angle->end());
155 #if DEBUG_WINDING
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000156 SkDebugf("%s winding=%d spanWinding=%d\n", __FUNCTION__, winding, spanWinding);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000157 #endif
158 // turn span winding into contour winding
159 if (spanWinding * winding < 0) {
160 winding += spanWinding;
161 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000162 // we care about first sign and whether wind sum indicates this
163 // edge is inside or outside. Maybe need to pass span winding
164 // or first winding or something into this function?
165 // advance to first undone angle, then return it and winding
166 // (to set whether edges are active or not)
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000167 firstAngle = angle;
168 winding -= firstAngle->segment()->spanSign(firstAngle);
169 while ((angle = angle->next()) != firstAngle) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000170 segment = angle->segment();
171 int maxWinding = winding;
172 winding -= segment->spanSign(angle);
173 #if DEBUG_SORT
174 SkDebugf("%s id=%d maxWinding=%d winding=%d sign=%d\n", __FUNCTION__,
175 segment->debugID(), maxWinding, winding, angle->sign());
176 #endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000177 *tIndex = angle->start();
178 *endIndex = angle->end();
179 int lesser = SkMin32(*tIndex, *endIndex);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000180 const SkOpSpan& nextSpan = segment->span(lesser);
181 if (!nextSpan.fDone) {
182 // FIXME: this be wrong? assign startWinding if edge is in
183 // same direction. If the direction is opposite, winding to
184 // assign is flipped sign or +/- 1?
185 if (SkOpSegment::UseInnerWinding(maxWinding, winding)) {
186 maxWinding = winding;
187 }
188 segment->markAndChaseWinding(angle, maxWinding, 0);
189 break;
190 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000191 }
192 *chase->insert(0) = span;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000193 return segment;
194 }
195 return NULL;
196}
197
caryclark@google.coma5e55922013-05-07 18:51:31 +0000198#if DEBUG_ACTIVE_SPANS || DEBUG_ACTIVE_SPANS_FIRST_ONLY
caryclark@google.comd892bd82013-06-17 14:10:36 +0000199void DebugShowActiveSpans(SkTArray<SkOpContour*, true>& contourList) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000200 int index;
201 for (index = 0; index < contourList.count(); ++ index) {
202 contourList[index]->debugShowActiveSpans();
203 }
204}
205#endif
206
caryclark@google.comd892bd82013-06-17 14:10:36 +0000207static SkOpSegment* findSortableTop(const SkTArray<SkOpContour*, true>& contourList,
caryclark@google.com07393ca2013-04-08 11:47:37 +0000208 int* index, int* endIndex, SkPoint* topLeft, bool* unsortable,
209 bool* done, bool onlySortable) {
210 SkOpSegment* result;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000211 const SkOpSegment* lastTopStart = NULL;
212 int lastIndex = -1, lastEndIndex = -1;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000213 do {
214 SkPoint bestXY = {SK_ScalarMax, SK_ScalarMax};
215 int contourCount = contourList.count();
216 SkOpSegment* topStart = NULL;
217 *done = true;
218 for (int cIndex = 0; cIndex < contourCount; ++cIndex) {
219 SkOpContour* contour = contourList[cIndex];
220 if (contour->done()) {
221 continue;
222 }
223 const SkPathOpsBounds& bounds = contour->bounds();
224 if (bounds.fBottom < topLeft->fY) {
225 *done = false;
226 continue;
227 }
228 if (bounds.fBottom == topLeft->fY && bounds.fRight < topLeft->fX) {
229 *done = false;
230 continue;
231 }
232 contour->topSortableSegment(*topLeft, &bestXY, &topStart);
233 if (!contour->done()) {
234 *done = false;
235 }
236 }
237 if (!topStart) {
238 return NULL;
239 }
240 *topLeft = bestXY;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000241 result = topStart->findTop(index, endIndex, unsortable);
242 if (!result) {
243 if (lastTopStart == topStart && lastIndex == *index && lastEndIndex == *endIndex) {
244 *done = true;
245 return NULL;
246 }
247 lastTopStart = topStart;
248 lastIndex = *index;
249 lastEndIndex = *endIndex;
250 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000251 } while (!result);
caryclark@google.com570863f2013-09-16 15:55:01 +0000252 if (result) {
253 *unsortable = false;
254 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000255 return result;
256}
257
caryclark@google.comd892bd82013-06-17 14:10:36 +0000258static int rightAngleWinding(const SkTArray<SkOpContour*, true>& contourList,
caryclark@google.com07393ca2013-04-08 11:47:37 +0000259 SkOpSegment** current, int* index, int* endIndex, double* tHit,
260 SkScalar* hitDx, bool* tryAgain, bool opp) {
261 double test = 0.9;
262 int contourWinding;
263 do {
264 contourWinding = contourRangeCheckY(contourList, current, index, endIndex, tHit, hitDx,
265 tryAgain, &test, opp);
266 if (contourWinding != SK_MinS32 || *tryAgain) {
267 return contourWinding;
268 }
269 test /= 2;
270 } while (!approximately_negative(test));
271 SkASSERT(0); // should be OK to comment out, but interested when this hits
272 return contourWinding;
273}
274
caryclark@google.comd892bd82013-06-17 14:10:36 +0000275static void skipVertical(const SkTArray<SkOpContour*, true>& contourList,
caryclark@google.com07393ca2013-04-08 11:47:37 +0000276 SkOpSegment** current, int* index, int* endIndex) {
277 if (!(*current)->isVertical(*index, *endIndex)) {
278 return;
279 }
280 int contourCount = contourList.count();
281 for (int cIndex = 0; cIndex < contourCount; ++cIndex) {
282 SkOpContour* contour = contourList[cIndex];
283 if (contour->done()) {
284 continue;
285 }
286 *current = contour->nonVerticalSegment(index, endIndex);
287 if (*current) {
288 return;
289 }
290 }
291}
292
caryclark@google.com570863f2013-09-16 15:55:01 +0000293SkOpSegment* FindSortableTop(const SkTArray<SkOpContour*, true>& contourList,
294 SkOpAngle::IncludeType angleIncludeType, bool* firstContour, int* indexPtr,
295 int* endIndexPtr, SkPoint* topLeft, bool* unsortable, bool* done) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000296 SkOpSegment* current = findSortableTop(contourList, indexPtr, endIndexPtr, topLeft, unsortable,
297 done, true);
298 if (!current) {
299 return NULL;
300 }
301 const int index = *indexPtr;
302 const int endIndex = *endIndexPtr;
303 if (*firstContour) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000304 current->initWinding(index, endIndex, angleIncludeType);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000305 *firstContour = false;
306 return current;
307 }
308 int minIndex = SkMin32(index, endIndex);
309 int sumWinding = current->windSum(minIndex);
310 if (sumWinding != SK_MinS32) {
311 return current;
312 }
caryclark@google.com570863f2013-09-16 15:55:01 +0000313 SkASSERT(current->windSum(SkMin32(index, endIndex)) == SK_MinS32);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000314 const SkOpSpan& span = current->span(endIndex);
315 if ((index < endIndex ? span.fFromAngleIndex : span.fToAngleIndex) < 0) {
316 current->addSimpleAngle(endIndex);
317 }
318 sumWinding = current->computeSum(index, endIndex, angleIncludeType);
caryclark@google.com570863f2013-09-16 15:55:01 +0000319 if (sumWinding != SK_MinS32 && sumWinding != SK_NaN32) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000320 return current;
321 }
322 int contourWinding;
323 int oppContourWinding = 0;
324 // the simple upward projection of the unresolved points hit unsortable angles
325 // shoot rays at right angles to the segment to find its winding, ignoring angle cases
326 bool tryAgain;
327 double tHit;
328 SkScalar hitDx = 0;
329 SkScalar hitOppDx = 0;
330 do {
331 // if current is vertical, find another candidate which is not
332 // if only remaining candidates are vertical, then they can be marked done
333 SkASSERT(*indexPtr != *endIndexPtr && *indexPtr >= 0 && *endIndexPtr >= 0);
334 skipVertical(contourList, &current, indexPtr, endIndexPtr);
skia.committer@gmail.com32840172013-04-09 07:01:27 +0000335
caryclark@google.com07393ca2013-04-08 11:47:37 +0000336 SkASSERT(*indexPtr != *endIndexPtr && *indexPtr >= 0 && *endIndexPtr >= 0);
337 tryAgain = false;
338 contourWinding = rightAngleWinding(contourList, &current, indexPtr, endIndexPtr, &tHit,
339 &hitDx, &tryAgain, false);
340 if (tryAgain) {
341 continue;
342 }
caryclark@google.com570863f2013-09-16 15:55:01 +0000343 if (angleIncludeType < SkOpAngle::kBinarySingle) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000344 break;
345 }
346 oppContourWinding = rightAngleWinding(contourList, &current, indexPtr, endIndexPtr, &tHit,
347 &hitOppDx, &tryAgain, true);
348 } while (tryAgain);
349 current->initWinding(*indexPtr, *endIndexPtr, tHit, contourWinding, hitDx, oppContourWinding,
350 hitOppDx);
351 return current;
352}
353
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000354static bool calcAngles(SkTArray<SkOpContour*, true>* contourList) {
355 int contourCount = (*contourList).count();
356 for (int cTest = 0; cTest < contourCount; ++cTest) {
357 SkOpContour* contour = (*contourList)[cTest];
358 if (!contour->calcAngles()) {
359 return false;
360 }
361 }
362 return true;
363}
364
365static void checkDuplicates(SkTArray<SkOpContour*, true>* contourList) {
366 int contourCount = (*contourList).count();
367 for (int cTest = 0; cTest < contourCount; ++cTest) {
368 SkOpContour* contour = (*contourList)[cTest];
369 contour->checkDuplicates();
370 }
371}
372
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000373static void checkEnds(SkTArray<SkOpContour*, true>* contourList) {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000374 // it's hard to determine if the end of a cubic or conic nearly intersects another curve.
375 // instead, look to see if the connecting curve intersected at that same end.
376 int contourCount = (*contourList).count();
377 for (int cTest = 0; cTest < contourCount; ++cTest) {
378 SkOpContour* contour = (*contourList)[cTest];
379 contour->checkEnds();
380 }
381}
382
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000383static void checkMultiples(SkTArray<SkOpContour*, true>* contourList) {
384 // it's hard to determine if the end of a cubic or conic nearly intersects another curve.
385 // instead, look to see if the connecting curve intersected at that same end.
386 int contourCount = (*contourList).count();
387 for (int cTest = 0; cTest < contourCount; ++cTest) {
388 SkOpContour* contour = (*contourList)[cTest];
389 contour->checkMultiples();
390 }
391}
392
393// A small interval of a pair of curves may collapse to lines for each, triggering coincidence
394static void checkSmall(SkTArray<SkOpContour*, true>* contourList) {
395 int contourCount = (*contourList).count();
396 for (int cTest = 0; cTest < contourCount; ++cTest) {
397 SkOpContour* contour = (*contourList)[cTest];
398 contour->checkSmall();
399 }
400}
401
caryclark@google.com570863f2013-09-16 15:55:01 +0000402// A tiny interval may indicate an undiscovered coincidence. Find and fix.
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000403static void checkTiny(SkTArray<SkOpContour*, true>* contourList) {
caryclark@google.com570863f2013-09-16 15:55:01 +0000404 int contourCount = (*contourList).count();
405 for (int cTest = 0; cTest < contourCount; ++cTest) {
406 SkOpContour* contour = (*contourList)[cTest];
407 contour->checkTiny();
408 }
409}
410
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000411static void fixOtherTIndex(SkTArray<SkOpContour*, true>* contourList) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000412 int contourCount = (*contourList).count();
413 for (int cTest = 0; cTest < contourCount; ++cTest) {
414 SkOpContour* contour = (*contourList)[cTest];
415 contour->fixOtherTIndex();
416 }
417}
418
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000419static void joinCoincidence(SkTArray<SkOpContour*, true>* contourList) {
420 int contourCount = (*contourList).count();
421 for (int cTest = 0; cTest < contourCount; ++cTest) {
422 SkOpContour* contour = (*contourList)[cTest];
423 contour->joinCoincidence();
424 }
425}
426
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000427static void sortAngles(SkTArray<SkOpContour*, true>* contourList) {
428 int contourCount = (*contourList).count();
429 for (int cTest = 0; cTest < contourCount; ++cTest) {
430 SkOpContour* contour = (*contourList)[cTest];
431 contour->sortAngles();
432 }
433}
434
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000435static void sortSegments(SkTArray<SkOpContour*, true>* contourList) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000436 int contourCount = (*contourList).count();
437 for (int cTest = 0; cTest < contourCount; ++cTest) {
438 SkOpContour* contour = (*contourList)[cTest];
439 contour->sortSegments();
440 }
441}
442
caryclark@google.comd892bd82013-06-17 14:10:36 +0000443void MakeContourList(SkTArray<SkOpContour>& contours, SkTArray<SkOpContour*, true>& list,
caryclark@google.com07393ca2013-04-08 11:47:37 +0000444 bool evenOdd, bool oppEvenOdd) {
445 int count = contours.count();
446 if (count == 0) {
447 return;
448 }
449 for (int index = 0; index < count; ++index) {
450 SkOpContour& contour = contours[index];
451 contour.setOppXor(contour.operand() ? evenOdd : oppEvenOdd);
caryclark@google.comd892bd82013-06-17 14:10:36 +0000452 list.push_back(&contour);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000453 }
commit-bot@chromium.orgb76d3b62013-04-22 19:55:19 +0000454 SkTQSort<SkOpContour>(list.begin(), list.end() - 1);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000455}
456
commit-bot@chromium.orgb76d3b62013-04-22 19:55:19 +0000457class DistanceLessThan {
458public:
459 DistanceLessThan(double* distances) : fDistances(distances) { }
460 double* fDistances;
461 bool operator()(const int one, const int two) {
462 return fDistances[one] < fDistances[two];
463 }
464};
465
caryclark@google.com07393ca2013-04-08 11:47:37 +0000466 /*
467 check start and end of each contour
468 if not the same, record them
469 match them up
470 connect closest
471 reassemble contour pieces into new path
472 */
473void Assemble(const SkPathWriter& path, SkPathWriter* simple) {
474#if DEBUG_PATH_CONSTRUCTION
475 SkDebugf("%s\n", __FUNCTION__);
476#endif
477 SkTArray<SkOpContour> contours;
478 SkOpEdgeBuilder builder(path, contours);
479 builder.finish();
480 int count = contours.count();
481 int outer;
caryclark@google.comd892bd82013-06-17 14:10:36 +0000482 SkTArray<int, true> runs(count); // indices of partial contours
caryclark@google.com07393ca2013-04-08 11:47:37 +0000483 for (outer = 0; outer < count; ++outer) {
484 const SkOpContour& eContour = contours[outer];
485 const SkPoint& eStart = eContour.start();
486 const SkPoint& eEnd = eContour.end();
487#if DEBUG_ASSEMBLE
488 SkDebugf("%s contour", __FUNCTION__);
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000489 if (!SkDPoint::ApproximatelyEqual(eStart, eEnd)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000490 SkDebugf("[%d]", runs.count());
491 } else {
492 SkDebugf(" ");
493 }
494 SkDebugf(" start=(%1.9g,%1.9g) end=(%1.9g,%1.9g)\n",
495 eStart.fX, eStart.fY, eEnd.fX, eEnd.fY);
496#endif
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000497 if (SkDPoint::ApproximatelyEqual(eStart, eEnd)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000498 eContour.toPath(simple);
499 continue;
500 }
caryclark@google.comd892bd82013-06-17 14:10:36 +0000501 runs.push_back(outer);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000502 }
503 count = runs.count();
504 if (count == 0) {
505 return;
506 }
caryclark@google.comd892bd82013-06-17 14:10:36 +0000507 SkTArray<int, true> sLink, eLink;
508 sLink.push_back_n(count);
509 eLink.push_back_n(count);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000510 int rIndex, iIndex;
511 for (rIndex = 0; rIndex < count; ++rIndex) {
512 sLink[rIndex] = eLink[rIndex] = SK_MaxS32;
513 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000514 const int ends = count * 2; // all starts and ends
515 const int entries = (ends - 1) * count; // folded triangle : n * (n - 1) / 2
caryclark@google.comd892bd82013-06-17 14:10:36 +0000516 SkTArray<double, true> distances;
517 distances.push_back_n(entries);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000518 for (rIndex = 0; rIndex < ends - 1; ++rIndex) {
519 outer = runs[rIndex >> 1];
520 const SkOpContour& oContour = contours[outer];
521 const SkPoint& oPt = rIndex & 1 ? oContour.end() : oContour.start();
522 const int row = rIndex < count - 1 ? rIndex * ends : (ends - rIndex - 2)
523 * ends - rIndex - 1;
524 for (iIndex = rIndex + 1; iIndex < ends; ++iIndex) {
525 int inner = runs[iIndex >> 1];
526 const SkOpContour& iContour = contours[inner];
527 const SkPoint& iPt = iIndex & 1 ? iContour.end() : iContour.start();
528 double dx = iPt.fX - oPt.fX;
529 double dy = iPt.fY - oPt.fY;
530 double dist = dx * dx + dy * dy;
531 distances[row + iIndex] = dist; // oStart distance from iStart
532 }
533 }
caryclark@google.comd892bd82013-06-17 14:10:36 +0000534 SkTArray<int, true> sortedDist;
535 sortedDist.push_back_n(entries);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000536 for (rIndex = 0; rIndex < entries; ++rIndex) {
537 sortedDist[rIndex] = rIndex;
538 }
commit-bot@chromium.orgb76d3b62013-04-22 19:55:19 +0000539 SkTQSort<int>(sortedDist.begin(), sortedDist.end() - 1, DistanceLessThan(distances.begin()));
caryclark@google.com07393ca2013-04-08 11:47:37 +0000540 int remaining = count; // number of start/end pairs
541 for (rIndex = 0; rIndex < entries; ++rIndex) {
542 int pair = sortedDist[rIndex];
543 int row = pair / ends;
544 int col = pair - row * ends;
545 int thingOne = row < col ? row : ends - row - 2;
546 int ndxOne = thingOne >> 1;
547 bool endOne = thingOne & 1;
548 int* linkOne = endOne ? eLink.begin() : sLink.begin();
549 if (linkOne[ndxOne] != SK_MaxS32) {
550 continue;
551 }
552 int thingTwo = row < col ? col : ends - row + col - 1;
553 int ndxTwo = thingTwo >> 1;
554 bool endTwo = thingTwo & 1;
555 int* linkTwo = endTwo ? eLink.begin() : sLink.begin();
556 if (linkTwo[ndxTwo] != SK_MaxS32) {
557 continue;
558 }
559 SkASSERT(&linkOne[ndxOne] != &linkTwo[ndxTwo]);
560 bool flip = endOne == endTwo;
561 linkOne[ndxOne] = flip ? ~ndxTwo : ndxTwo;
562 linkTwo[ndxTwo] = flip ? ~ndxOne : ndxOne;
563 if (!--remaining) {
564 break;
565 }
566 }
567 SkASSERT(!remaining);
568#if DEBUG_ASSEMBLE
569 for (rIndex = 0; rIndex < count; ++rIndex) {
570 int s = sLink[rIndex];
571 int e = eLink[rIndex];
572 SkDebugf("%s %c%d <- s%d - e%d -> %c%d\n", __FUNCTION__, s < 0 ? 's' : 'e',
573 s < 0 ? ~s : s, rIndex, rIndex, e < 0 ? 'e' : 's', e < 0 ? ~e : e);
574 }
575#endif
576 rIndex = 0;
577 do {
578 bool forward = true;
579 bool first = true;
580 int sIndex = sLink[rIndex];
581 SkASSERT(sIndex != SK_MaxS32);
582 sLink[rIndex] = SK_MaxS32;
583 int eIndex;
584 if (sIndex < 0) {
585 eIndex = sLink[~sIndex];
586 sLink[~sIndex] = SK_MaxS32;
587 } else {
588 eIndex = eLink[sIndex];
589 eLink[sIndex] = SK_MaxS32;
590 }
591 SkASSERT(eIndex != SK_MaxS32);
592#if DEBUG_ASSEMBLE
593 SkDebugf("%s sIndex=%c%d eIndex=%c%d\n", __FUNCTION__, sIndex < 0 ? 's' : 'e',
594 sIndex < 0 ? ~sIndex : sIndex, eIndex < 0 ? 's' : 'e',
595 eIndex < 0 ? ~eIndex : eIndex);
596#endif
597 do {
598 outer = runs[rIndex];
599 const SkOpContour& contour = contours[outer];
600 if (first) {
601 first = false;
602 const SkPoint* startPtr = &contour.start();
603 simple->deferredMove(startPtr[0]);
604 }
605 if (forward) {
606 contour.toPartialForward(simple);
607 } else {
608 contour.toPartialBackward(simple);
609 }
610#if DEBUG_ASSEMBLE
611 SkDebugf("%s rIndex=%d eIndex=%s%d close=%d\n", __FUNCTION__, rIndex,
612 eIndex < 0 ? "~" : "", eIndex < 0 ? ~eIndex : eIndex,
613 sIndex == ((rIndex != eIndex) ^ forward ? eIndex : ~eIndex));
614#endif
615 if (sIndex == ((rIndex != eIndex) ^ forward ? eIndex : ~eIndex)) {
616 simple->close();
617 break;
618 }
619 if (forward) {
620 eIndex = eLink[rIndex];
621 SkASSERT(eIndex != SK_MaxS32);
622 eLink[rIndex] = SK_MaxS32;
623 if (eIndex >= 0) {
624 SkASSERT(sLink[eIndex] == rIndex);
625 sLink[eIndex] = SK_MaxS32;
626 } else {
627 SkASSERT(eLink[~eIndex] == ~rIndex);
628 eLink[~eIndex] = SK_MaxS32;
629 }
630 } else {
631 eIndex = sLink[rIndex];
632 SkASSERT(eIndex != SK_MaxS32);
633 sLink[rIndex] = SK_MaxS32;
634 if (eIndex >= 0) {
635 SkASSERT(eLink[eIndex] == rIndex);
636 eLink[eIndex] = SK_MaxS32;
637 } else {
638 SkASSERT(sLink[~eIndex] == ~rIndex);
639 sLink[~eIndex] = SK_MaxS32;
640 }
641 }
642 rIndex = eIndex;
643 if (rIndex < 0) {
644 forward ^= 1;
645 rIndex = ~rIndex;
646 }
647 } while (true);
648 for (rIndex = 0; rIndex < count; ++rIndex) {
649 if (sLink[rIndex] != SK_MaxS32) {
650 break;
651 }
652 }
653 } while (rIndex < count);
654#if DEBUG_ASSEMBLE
655 for (rIndex = 0; rIndex < count; ++rIndex) {
656 SkASSERT(sLink[rIndex] == SK_MaxS32);
657 SkASSERT(eLink[rIndex] == SK_MaxS32);
658 }
659#endif
660}
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000661
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000662bool HandleCoincidence(SkTArray<SkOpContour*, true>* contourList, int total) {
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000663#if DEBUG_SHOW_WINDING
664 SkOpContour::debugShowWindingValues(contourList);
665#endif
666 CoincidenceCheck(contourList, total);
667#if DEBUG_SHOW_WINDING
668 SkOpContour::debugShowWindingValues(contourList);
669#endif
670 fixOtherTIndex(contourList);
671 checkEnds(contourList);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000672 checkMultiples(contourList);
673 checkDuplicates(contourList);
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000674 checkTiny(contourList);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000675 checkSmall(contourList);
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000676 joinCoincidence(contourList);
677 sortSegments(contourList);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000678 if (!calcAngles(contourList)) {
679 return false;
680 }
681 sortAngles(contourList);
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000682#if DEBUG_ACTIVE_SPANS || DEBUG_ACTIVE_SPANS_FIRST_ONLY
683 DebugShowActiveSpans(*contourList);
684#endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000685 return true;
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000686}