blob: 5feef79801aa267d76f11328e1b4bdaacafdaf7a [file] [log] [blame]
caryclark@google.com07393ca2013-04-08 11:47:37 +00001/*
2* Copyright 2013 Google Inc.
3*
4* Use of this source code is governed by a BSD-style license that can be
5* found in the LICENSE file.
6*/
7#include "SkIntersections.h"
8#include "SkOpContour.h"
9#include "SkPathWriter.h"
commit-bot@chromium.orgb76d3b62013-04-22 19:55:19 +000010#include "SkTSort.h"
caryclark@google.com07393ca2013-04-08 11:47:37 +000011
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000012bool SkOpContour::addCoincident(int index, SkOpContour* other, int otherIndex,
caryclark@google.com07393ca2013-04-08 11:47:37 +000013 const SkIntersections& ts, bool swap) {
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000014 SkPoint pt0 = ts.pt(0).asSkPoint();
15 SkPoint pt1 = ts.pt(1).asSkPoint();
16 if (pt0 == pt1) {
17 // FIXME: one could imagine a case where it would be incorrect to ignore this
18 // suppose two self-intersecting cubics overlap to be coincident --
19 // this needs to check that by some measure the t values are far enough apart
20 // or needs to check to see if the self-intersection bit was set on the cubic segment
21 return false;
22 }
caryclark@google.comd892bd82013-06-17 14:10:36 +000023 SkCoincidence& coincidence = fCoincidences.push_back();
caryclark@google.com570863f2013-09-16 15:55:01 +000024 coincidence.fOther = other;
caryclark@google.com07393ca2013-04-08 11:47:37 +000025 coincidence.fSegments[0] = index;
26 coincidence.fSegments[1] = otherIndex;
27 coincidence.fTs[swap][0] = ts[0][0];
28 coincidence.fTs[swap][1] = ts[0][1];
29 coincidence.fTs[!swap][0] = ts[1][0];
30 coincidence.fTs[!swap][1] = ts[1][1];
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000031 coincidence.fPts[0] = pt0;
32 coincidence.fPts[1] = pt1;
33 return true;
caryclark@google.com07393ca2013-04-08 11:47:37 +000034}
35
36SkOpSegment* SkOpContour::nonVerticalSegment(int* start, int* end) {
37 int segmentCount = fSortedSegments.count();
38 SkASSERT(segmentCount > 0);
39 for (int sortedIndex = fFirstSorted; sortedIndex < segmentCount; ++sortedIndex) {
40 SkOpSegment* testSegment = fSortedSegments[sortedIndex];
41 if (testSegment->done()) {
42 continue;
43 }
44 *start = *end = 0;
45 while (testSegment->nextCandidate(start, end)) {
46 if (!testSegment->isVertical(*start, *end)) {
47 return testSegment;
48 }
49 }
50 }
51 return NULL;
52}
53
54// first pass, add missing T values
55// second pass, determine winding values of overlaps
56void SkOpContour::addCoincidentPoints() {
57 int count = fCoincidences.count();
58 for (int index = 0; index < count; ++index) {
59 SkCoincidence& coincidence = fCoincidences[index];
caryclark@google.com07393ca2013-04-08 11:47:37 +000060 int thisIndex = coincidence.fSegments[0];
61 SkOpSegment& thisOne = fSegments[thisIndex];
caryclark@google.com570863f2013-09-16 15:55:01 +000062 SkOpContour* otherContour = coincidence.fOther;
caryclark@google.com07393ca2013-04-08 11:47:37 +000063 int otherIndex = coincidence.fSegments[1];
64 SkOpSegment& other = otherContour->fSegments[otherIndex];
65 if ((thisOne.done() || other.done()) && thisOne.complete() && other.complete()) {
66 // OPTIMIZATION: remove from array
67 continue;
68 }
69 #if DEBUG_CONCIDENT
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000070 thisOne.debugShowTs("-");
71 other.debugShowTs("o");
caryclark@google.com07393ca2013-04-08 11:47:37 +000072 #endif
73 double startT = coincidence.fTs[0][0];
74 double endT = coincidence.fTs[0][1];
caryclark@google.coma5e55922013-05-07 18:51:31 +000075 bool startSwapped, oStartSwapped, cancelers;
76 if ((cancelers = startSwapped = startT > endT)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000077 SkTSwap(startT, endT);
caryclark@google.com07393ca2013-04-08 11:47:37 +000078 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000079 if (startT == endT) { // if one is very large the smaller may have collapsed to nothing
80 if (endT <= 1 - FLT_EPSILON) {
81 endT += FLT_EPSILON;
82 SkASSERT(endT <= 1);
83 } else {
84 startT -= FLT_EPSILON;
85 SkASSERT(startT >= 0);
86 }
87 }
caryclark@google.com07393ca2013-04-08 11:47:37 +000088 SkASSERT(!approximately_negative(endT - startT));
89 double oStartT = coincidence.fTs[1][0];
90 double oEndT = coincidence.fTs[1][1];
caryclark@google.coma5e55922013-05-07 18:51:31 +000091 if ((oStartSwapped = oStartT > oEndT)) {
92 SkTSwap(oStartT, oEndT);
caryclark@google.com07393ca2013-04-08 11:47:37 +000093 cancelers ^= true;
94 }
95 SkASSERT(!approximately_negative(oEndT - oStartT));
caryclark@google.coma5e55922013-05-07 18:51:31 +000096 if (cancelers) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000097 // make sure startT and endT have t entries
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000098 const SkPoint& startPt = coincidence.fPts[startSwapped];
caryclark@google.com07393ca2013-04-08 11:47:37 +000099 if (startT > 0 || oEndT < 1
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000100 || thisOne.isMissing(startT, startPt) || other.isMissing(oEndT, startPt)) {
101 thisOne.addTPair(startT, &other, oEndT, true, startPt);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000102 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000103 const SkPoint& oStartPt = coincidence.fPts[oStartSwapped];
caryclark@google.com07393ca2013-04-08 11:47:37 +0000104 if (oStartT > 0 || endT < 1
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000105 || thisOne.isMissing(endT, oStartPt) || other.isMissing(oStartT, oStartPt)) {
106 other.addTPair(oStartT, &thisOne, endT, true, oStartPt);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000107 }
108 } else {
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000109 const SkPoint& startPt = coincidence.fPts[startSwapped];
caryclark@google.com07393ca2013-04-08 11:47:37 +0000110 if (startT > 0 || oStartT > 0
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000111 || thisOne.isMissing(startT, startPt) || other.isMissing(oStartT, startPt)) {
112 thisOne.addTPair(startT, &other, oStartT, true, startPt);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000113 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000114 const SkPoint& oEndPt = coincidence.fPts[!oStartSwapped];
caryclark@google.com07393ca2013-04-08 11:47:37 +0000115 if (endT < 1 || oEndT < 1
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000116 || thisOne.isMissing(endT, oEndPt) || other.isMissing(oEndT, oEndPt)) {
117 other.addTPair(oEndT, &thisOne, endT, true, oEndPt);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000118 }
119 }
120 #if DEBUG_CONCIDENT
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000121 thisOne.debugShowTs("+");
122 other.debugShowTs("o");
caryclark@google.com07393ca2013-04-08 11:47:37 +0000123 #endif
124 }
125}
126
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000127bool SkOpContour::addPartialCoincident(int index, SkOpContour* other, int otherIndex,
caryclark@google.com570863f2013-09-16 15:55:01 +0000128 const SkIntersections& ts, int ptIndex, bool swap) {
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000129 SkPoint pt0 = ts.pt(ptIndex).asSkPoint();
130 SkPoint pt1 = ts.pt(ptIndex + 1).asSkPoint();
131 if (SkDPoint::ApproximatelyEqual(pt0, pt1)) {
132 // FIXME: one could imagine a case where it would be incorrect to ignore this
133 // suppose two self-intersecting cubics overlap to form a partial coincidence --
134 // although it isn't clear why the regular coincidence could wouldn't pick this up
135 // this is exceptional enough to ignore for now
136 return false;
137 }
caryclark@google.com570863f2013-09-16 15:55:01 +0000138 SkCoincidence& coincidence = fPartialCoincidences.push_back();
139 coincidence.fOther = other;
140 coincidence.fSegments[0] = index;
141 coincidence.fSegments[1] = otherIndex;
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000142 coincidence.fTs[swap][0] = ts[0][ptIndex];
143 coincidence.fTs[swap][1] = ts[0][ptIndex + 1];
144 coincidence.fTs[!swap][0] = ts[1][ptIndex];
145 coincidence.fTs[!swap][1] = ts[1][ptIndex + 1];
146 coincidence.fPts[0] = pt0;
147 coincidence.fPts[1] = pt1;
148 return true;
caryclark@google.com570863f2013-09-16 15:55:01 +0000149}
150
caryclark@google.com07393ca2013-04-08 11:47:37 +0000151void SkOpContour::calcCoincidentWinding() {
152 int count = fCoincidences.count();
caryclark@google.com570863f2013-09-16 15:55:01 +0000153#if DEBUG_CONCIDENT
154 if (count > 0) {
155 SkDebugf("%s count=%d\n", __FUNCTION__, count);
156 }
157#endif
caryclark@google.com07393ca2013-04-08 11:47:37 +0000158 for (int index = 0; index < count; ++index) {
159 SkCoincidence& coincidence = fCoincidences[index];
caryclark@google.com570863f2013-09-16 15:55:01 +0000160 calcCommonCoincidentWinding(coincidence);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000161 }
162}
163
caryclark@google.com570863f2013-09-16 15:55:01 +0000164void SkOpContour::calcPartialCoincidentWinding() {
165 int count = fPartialCoincidences.count();
166#if DEBUG_CONCIDENT
167 if (count > 0) {
168 SkDebugf("%s count=%d\n", __FUNCTION__, count);
169 }
170#endif
171 for (int index = 0; index < count; ++index) {
172 SkCoincidence& coincidence = fPartialCoincidences[index];
173 calcCommonCoincidentWinding(coincidence);
174 }
175}
176
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000177void SkOpContour::joinCoincidence(const SkTArray<SkCoincidence, true>& coincidences, bool partial) {
178 int count = coincidences.count();
179#if DEBUG_CONCIDENT
180 if (count > 0) {
181 SkDebugf("%s count=%d\n", __FUNCTION__, count);
182 }
183#endif
184 // look for a lineup where the partial implies another adjoining coincidence
185 for (int index = 0; index < count; ++index) {
186 const SkCoincidence& coincidence = coincidences[index];
187 int thisIndex = coincidence.fSegments[0];
188 SkOpSegment& thisOne = fSegments[thisIndex];
189 SkOpContour* otherContour = coincidence.fOther;
190 int otherIndex = coincidence.fSegments[1];
191 SkOpSegment& other = otherContour->fSegments[otherIndex];
192 double startT = coincidence.fTs[0][0];
193 double endT = coincidence.fTs[0][1];
194 if (startT == endT) { // this can happen in very large compares
195 continue;
196 }
197 double oStartT = coincidence.fTs[1][0];
198 double oEndT = coincidence.fTs[1][1];
199 if (oStartT == oEndT) {
200 continue;
201 }
202 bool swapStart = startT > endT;
203 bool swapOther = oStartT > oEndT;
204 if (swapStart) {
205 SkTSwap<double>(startT, endT);
206 SkTSwap<double>(oStartT, oEndT);
207 }
208 bool cancel = swapOther != swapStart;
209 int step = swapStart ? -1 : 1;
210 int oStep = swapOther ? -1 : 1;
211 double oMatchStart = cancel ? oEndT : oStartT;
212 if (partial ? startT != 0 || oMatchStart != 0 : (startT == 0) != (oMatchStart == 0)) {
213 bool added = false;
214 if (oMatchStart != 0) {
215 added = thisOne.joinCoincidence(false, &other, oMatchStart, oStep, cancel);
216 }
217 if (startT != 0 && !added) {
218 (void) other.joinCoincidence(cancel, &thisOne, startT, step, cancel);
219 }
220 }
221 double oMatchEnd = cancel ? oStartT : oEndT;
222 if (partial ? endT != 1 || oMatchEnd != 1 : (endT == 1) != (oMatchEnd == 1)) {
223 bool added = false;
224 if (oMatchEnd != 1) {
225 added = thisOne.joinCoincidence(true, &other, oMatchEnd, -oStep, cancel);
226 }
227 if (endT != 1 && !added) {
228 (void) other.joinCoincidence(!cancel, &thisOne, endT, -step, cancel);
229 }
230 }
231 }
232}
233
caryclark@google.com570863f2013-09-16 15:55:01 +0000234void SkOpContour::calcCommonCoincidentWinding(const SkCoincidence& coincidence) {
235 int thisIndex = coincidence.fSegments[0];
236 SkOpSegment& thisOne = fSegments[thisIndex];
237 if (thisOne.done()) {
238 return;
239 }
240 SkOpContour* otherContour = coincidence.fOther;
241 int otherIndex = coincidence.fSegments[1];
242 SkOpSegment& other = otherContour->fSegments[otherIndex];
243 if (other.done()) {
244 return;
245 }
246 double startT = coincidence.fTs[0][0];
247 double endT = coincidence.fTs[0][1];
248 const SkPoint* startPt = &coincidence.fPts[0];
249 const SkPoint* endPt = &coincidence.fPts[1];
250 bool cancelers;
251 if ((cancelers = startT > endT)) {
252 SkTSwap<double>(startT, endT);
253 SkTSwap<const SkPoint*>(startPt, endPt);
254 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000255 if (startT == endT) { // if span is very large, the smaller may have collapsed to nothing
256 if (endT <= 1 - FLT_EPSILON) {
257 endT += FLT_EPSILON;
258 SkASSERT(endT <= 1);
259 } else {
260 startT -= FLT_EPSILON;
261 SkASSERT(startT >= 0);
262 }
263 }
caryclark@google.com570863f2013-09-16 15:55:01 +0000264 SkASSERT(!approximately_negative(endT - startT));
265 double oStartT = coincidence.fTs[1][0];
266 double oEndT = coincidence.fTs[1][1];
267 if (oStartT > oEndT) {
268 SkTSwap<double>(oStartT, oEndT);
269 cancelers ^= true;
270 }
271 SkASSERT(!approximately_negative(oEndT - oStartT));
272 if (cancelers) {
273 thisOne.addTCancel(*startPt, *endPt, &other);
274 } else {
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000275 thisOne.addTCoincident(*startPt, *endPt, endT, &other);
caryclark@google.com570863f2013-09-16 15:55:01 +0000276 }
277#if DEBUG_CONCIDENT
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000278 thisOne.debugShowTs("p");
279 other.debugShowTs("o");
caryclark@google.com570863f2013-09-16 15:55:01 +0000280#endif
281}
282
caryclark@google.com07393ca2013-04-08 11:47:37 +0000283void SkOpContour::sortSegments() {
284 int segmentCount = fSegments.count();
caryclark@google.comd892bd82013-06-17 14:10:36 +0000285 fSortedSegments.push_back_n(segmentCount);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000286 for (int test = 0; test < segmentCount; ++test) {
caryclark@google.comd892bd82013-06-17 14:10:36 +0000287 fSortedSegments[test] = &fSegments[test];
caryclark@google.com07393ca2013-04-08 11:47:37 +0000288 }
commit-bot@chromium.orgb76d3b62013-04-22 19:55:19 +0000289 SkTQSort<SkOpSegment>(fSortedSegments.begin(), fSortedSegments.end() - 1);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000290 fFirstSorted = 0;
291}
292
293void SkOpContour::toPath(SkPathWriter* path) const {
294 int segmentCount = fSegments.count();
295 const SkPoint& pt = fSegments.front().pts()[0];
296 path->deferredMove(pt);
297 for (int test = 0; test < segmentCount; ++test) {
298 fSegments[test].addCurveTo(0, 1, path, true);
299 }
300 path->close();
301}
302
303void SkOpContour::topSortableSegment(const SkPoint& topLeft, SkPoint* bestXY,
304 SkOpSegment** topStart) {
305 int segmentCount = fSortedSegments.count();
306 SkASSERT(segmentCount > 0);
307 int sortedIndex = fFirstSorted;
308 fDone = true; // may be cleared below
309 for ( ; sortedIndex < segmentCount; ++sortedIndex) {
310 SkOpSegment* testSegment = fSortedSegments[sortedIndex];
311 if (testSegment->done()) {
312 if (sortedIndex == fFirstSorted) {
313 ++fFirstSorted;
314 }
315 continue;
316 }
317 fDone = false;
318 SkPoint testXY = testSegment->activeLeftTop(true, NULL);
319 if (*topStart) {
320 if (testXY.fY < topLeft.fY) {
321 continue;
322 }
323 if (testXY.fY == topLeft.fY && testXY.fX < topLeft.fX) {
324 continue;
325 }
326 if (bestXY->fY < testXY.fY) {
327 continue;
328 }
329 if (bestXY->fY == testXY.fY && bestXY->fX < testXY.fX) {
330 continue;
331 }
332 }
333 *topStart = testSegment;
334 *bestXY = testXY;
335 }
336}
337
338SkOpSegment* SkOpContour::undoneSegment(int* start, int* end) {
339 int segmentCount = fSegments.count();
340 for (int test = 0; test < segmentCount; ++test) {
341 SkOpSegment* testSegment = &fSegments[test];
342 if (testSegment->done()) {
343 continue;
344 }
345 testSegment->undoneSpan(start, end);
346 return testSegment;
347 }
348 return NULL;
349}
350
351#if DEBUG_SHOW_WINDING
352int SkOpContour::debugShowWindingValues(int totalSegments, int ofInterest) {
353 int count = fSegments.count();
354 int sum = 0;
355 for (int index = 0; index < count; ++index) {
356 sum += fSegments[index].debugShowWindingValues(totalSegments, ofInterest);
357 }
358// SkDebugf("%s sum=%d\n", __FUNCTION__, sum);
359 return sum;
360}
361
caryclark@google.com570863f2013-09-16 15:55:01 +0000362void SkOpContour::debugShowWindingValues(const SkTArray<SkOpContour*, true>& contourList) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000363// int ofInterest = 1 << 1 | 1 << 5 | 1 << 9 | 1 << 13;
364// int ofInterest = 1 << 4 | 1 << 8 | 1 << 12 | 1 << 16;
365 int ofInterest = 1 << 5 | 1 << 8;
366 int total = 0;
367 int index;
368 for (index = 0; index < contourList.count(); ++index) {
369 total += contourList[index]->segments().count();
370 }
371 int sum = 0;
372 for (index = 0; index < contourList.count(); ++index) {
373 sum += contourList[index]->debugShowWindingValues(total, ofInterest);
374 }
375// SkDebugf("%s total=%d\n", __FUNCTION__, sum);
376}
377#endif
378
379void SkOpContour::setBounds() {
380 int count = fSegments.count();
381 if (count == 0) {
382 SkDebugf("%s empty contour\n", __FUNCTION__);
383 SkASSERT(0);
384 // FIXME: delete empty contour?
385 return;
386 }
387 fBounds = fSegments.front().bounds();
388 for (int index = 1; index < count; ++index) {
389 fBounds.add(fSegments[index].bounds());
390 }
391}