blob: ea57756b60089e0a9b18c72c392aab4109e4f063 [file] [log] [blame]
caryclark54359292015-03-26 07:52:43 -07001/*
2 * Copyright 2014 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 */
Mike Kleinc0bd9f92019-04-23 12:05:21 -05007#include "src/pathops/SkOpCoincidence.h"
8#include "src/pathops/SkOpContour.h"
9#include "src/pathops/SkOpSegment.h"
10#include "src/pathops/SkPathWriter.h"
caryclark54359292015-03-26 07:52:43 -070011
12bool SkOpPtT::alias() const {
13 return this->span()->ptT() != this;
14}
15
caryclark30b9fdd2016-08-31 14:36:29 -070016const SkOpPtT* SkOpPtT::active() const {
17 if (!fDeleted) {
18 return this;
19 }
20 const SkOpPtT* ptT = this;
21 const SkOpPtT* stopPtT = ptT;
22 while ((ptT = ptT->next()) != stopPtT) {
23 if (ptT->fSpan == fSpan && !ptT->fDeleted) {
24 return ptT;
25 }
26 }
Cary Clark74b42902018-03-09 07:38:47 -050027 return nullptr; // should never return deleted; caller must abort
caryclark30b9fdd2016-08-31 14:36:29 -070028}
29
caryclark27c8eb82015-07-06 11:38:33 -070030bool SkOpPtT::contains(const SkOpPtT* check) const {
caryclarke7bb5b22016-09-22 05:20:07 -070031 SkOPASSERT(this != check);
caryclark27c8eb82015-07-06 11:38:33 -070032 const SkOpPtT* ptT = this;
33 const SkOpPtT* stopPtT = ptT;
34 while ((ptT = ptT->next()) != stopPtT) {
35 if (ptT == check) {
36 return true;
37 }
38 }
39 return false;
40}
41
caryclark55888e42016-07-18 10:01:36 -070042bool SkOpPtT::contains(const SkOpSegment* segment, const SkPoint& pt) const {
43 SkASSERT(this->segment() != segment);
44 const SkOpPtT* ptT = this;
caryclark27c8eb82015-07-06 11:38:33 -070045 const SkOpPtT* stopPtT = ptT;
46 while ((ptT = ptT->next()) != stopPtT) {
caryclark55888e42016-07-18 10:01:36 -070047 if (ptT->fPt == pt && ptT->segment() == segment) {
48 return true;
49 }
50 }
51 return false;
52}
53
54bool SkOpPtT::contains(const SkOpSegment* segment, double t) const {
55 const SkOpPtT* ptT = this;
56 const SkOpPtT* stopPtT = ptT;
57 while ((ptT = ptT->next()) != stopPtT) {
58 if (ptT->fT == t && ptT->segment() == segment) {
59 return true;
60 }
61 }
62 return false;
63}
64
65const SkOpPtT* SkOpPtT::contains(const SkOpSegment* check) const {
66 SkASSERT(this->segment() != check);
67 const SkOpPtT* ptT = this;
68 const SkOpPtT* stopPtT = ptT;
69 while ((ptT = ptT->next()) != stopPtT) {
70 if (ptT->segment() == check && !ptT->deleted()) {
caryclark27c8eb82015-07-06 11:38:33 -070071 return ptT;
72 }
73 }
halcanary96fcdcc2015-08-27 07:41:13 -070074 return nullptr;
caryclark27c8eb82015-07-06 11:38:33 -070075}
76
caryclark54359292015-03-26 07:52:43 -070077SkOpContour* SkOpPtT::contour() const {
78 return segment()->contour();
79}
80
caryclark55888e42016-07-18 10:01:36 -070081const SkOpPtT* SkOpPtT::find(const SkOpSegment* segment) const {
82 const SkOpPtT* ptT = this;
caryclark27c8eb82015-07-06 11:38:33 -070083 const SkOpPtT* stopPtT = ptT;
84 do {
caryclark55888e42016-07-18 10:01:36 -070085 if (ptT->segment() == segment && !ptT->deleted()) {
caryclark27c8eb82015-07-06 11:38:33 -070086 return ptT;
87 }
88 ptT = ptT->fNext;
89 } while (stopPtT != ptT);
caryclark55888e42016-07-18 10:01:36 -070090// SkASSERT(0);
halcanary96fcdcc2015-08-27 07:41:13 -070091 return nullptr;
caryclark27c8eb82015-07-06 11:38:33 -070092}
93
caryclark54359292015-03-26 07:52:43 -070094SkOpGlobalState* SkOpPtT::globalState() const {
caryclark55888e42016-07-18 10:01:36 -070095 return contour()->globalState();
caryclark54359292015-03-26 07:52:43 -070096}
97
98void SkOpPtT::init(SkOpSpanBase* span, double t, const SkPoint& pt, bool duplicate) {
99 fT = t;
100 fPt = pt;
101 fSpan = span;
102 fNext = this;
103 fDuplicatePt = duplicate;
104 fDeleted = false;
caryclark55888e42016-07-18 10:01:36 -0700105 fCoincident = false;
caryclark1049f122015-04-20 08:31:59 -0700106 SkDEBUGCODE(fID = span->globalState()->nextPtTID());
caryclark54359292015-03-26 07:52:43 -0700107}
108
109bool SkOpPtT::onEnd() const {
110 const SkOpSpanBase* span = this->span();
111 if (span->ptT() != this) {
112 return false;
113 }
114 const SkOpSegment* segment = this->segment();
115 return span == segment->head() || span == segment->tail();
116}
117
caryclark55888e42016-07-18 10:01:36 -0700118bool SkOpPtT::ptAlreadySeen(const SkOpPtT* check) const {
119 while (this != check) {
120 if (this->fPt == check->fPt) {
121 return true;
122 }
123 check = check->fNext;
124 }
125 return false;
126}
127
caryclark54359292015-03-26 07:52:43 -0700128SkOpPtT* SkOpPtT::prev() {
129 SkOpPtT* result = this;
130 SkOpPtT* next = this;
131 while ((next = next->fNext) != this) {
132 result = next;
133 }
134 SkASSERT(result->fNext == this);
135 return result;
136}
137
caryclark54359292015-03-26 07:52:43 -0700138const SkOpSegment* SkOpPtT::segment() const {
139 return span()->segment();
140}
141
142SkOpSegment* SkOpPtT::segment() {
143 return span()->segment();
144}
145
caryclark55888e42016-07-18 10:01:36 -0700146void SkOpPtT::setDeleted() {
147 SkASSERT(this->span()->debugDeleted() || this->span()->ptT() != this);
caryclark15976282016-07-21 05:48:43 -0700148 SkOPASSERT(!fDeleted);
caryclark55888e42016-07-18 10:01:36 -0700149 fDeleted = true;
150}
151
Cary Clarkd058b182018-11-29 09:02:32 -0500152bool SkOpSpanBase::addOpp(SkOpSpanBase* opp) {
caryclark29b25632016-08-25 11:27:17 -0700153 SkOpPtT* oppPrev = this->ptT()->oppPrev(opp->ptT());
caryclark30b9fdd2016-08-31 14:36:29 -0700154 if (!oppPrev) {
Cary Clarkd058b182018-11-29 09:02:32 -0500155 return true;
caryclark54359292015-03-26 07:52:43 -0700156 }
Cary Clarkd058b182018-11-29 09:02:32 -0500157 FAIL_IF(!this->mergeMatches(opp));
caryclark30b9fdd2016-08-31 14:36:29 -0700158 this->ptT()->addOpp(opp->ptT(), oppPrev);
159 this->checkForCollapsedCoincidence();
Cary Clarkd058b182018-11-29 09:02:32 -0500160 return true;
caryclark55888e42016-07-18 10:01:36 -0700161}
162
Cary Clarkba610292018-06-19 09:47:15 -0400163SkOpSpanBase::Collapsed SkOpSpanBase::collapsed(double s, double e) const {
caryclark30b9fdd2016-08-31 14:36:29 -0700164 const SkOpPtT* start = &fPtT;
Cary Clarkba610292018-06-19 09:47:15 -0400165 const SkOpPtT* startNext = nullptr;
caryclark30b9fdd2016-08-31 14:36:29 -0700166 const SkOpPtT* walk = start;
167 double min = walk->fT;
168 double max = min;
169 const SkOpSegment* segment = this->segment();
Cary Clark4929a4a2018-10-17 14:12:41 -0400170 int safetyNet = 100000;
caryclark30b9fdd2016-08-31 14:36:29 -0700171 while ((walk = walk->next()) != start) {
Cary Clark4929a4a2018-10-17 14:12:41 -0400172 if (!--safetyNet) {
173 return Collapsed::kError;
174 }
Cary Clarkba610292018-06-19 09:47:15 -0400175 if (walk == startNext) {
176 return Collapsed::kError;
177 }
caryclark30b9fdd2016-08-31 14:36:29 -0700178 if (walk->segment() != segment) {
179 continue;
180 }
181 min = SkTMin(min, walk->fT);
182 max = SkTMax(max, walk->fT);
183 if (between(min, s, max) && between(min, e, max)) {
Cary Clarkba610292018-06-19 09:47:15 -0400184 return Collapsed::kYes;
caryclark30b9fdd2016-08-31 14:36:29 -0700185 }
Cary Clarkba610292018-06-19 09:47:15 -0400186 startNext = start->next();
caryclark30b9fdd2016-08-31 14:36:29 -0700187 }
Cary Clarkba610292018-06-19 09:47:15 -0400188 return Collapsed::kNo;
caryclark30b9fdd2016-08-31 14:36:29 -0700189}
190
caryclark54359292015-03-26 07:52:43 -0700191bool SkOpSpanBase::contains(const SkOpSpanBase* span) const {
192 const SkOpPtT* start = &fPtT;
193 const SkOpPtT* check = &span->fPtT;
caryclarkef4f32a2016-08-24 09:24:18 -0700194 SkOPASSERT(start != check);
caryclark54359292015-03-26 07:52:43 -0700195 const SkOpPtT* walk = start;
196 while ((walk = walk->next()) != start) {
197 if (walk == check) {
198 return true;
199 }
200 }
201 return false;
202}
203
caryclark55888e42016-07-18 10:01:36 -0700204const SkOpPtT* SkOpSpanBase::contains(const SkOpSegment* segment) const {
205 const SkOpPtT* start = &fPtT;
206 const SkOpPtT* walk = start;
caryclark54359292015-03-26 07:52:43 -0700207 while ((walk = walk->next()) != start) {
caryclark55888e42016-07-18 10:01:36 -0700208 if (walk->deleted()) {
209 continue;
210 }
211 if (walk->segment() == segment && walk->span()->ptT() == walk) {
caryclark54359292015-03-26 07:52:43 -0700212 return walk;
213 }
214 }
halcanary96fcdcc2015-08-27 07:41:13 -0700215 return nullptr;
caryclark54359292015-03-26 07:52:43 -0700216}
217
218bool SkOpSpanBase::containsCoinEnd(const SkOpSegment* segment) const {
219 SkASSERT(this->segment() != segment);
220 const SkOpSpanBase* next = this;
221 while ((next = next->fCoinEnd) != this) {
222 if (next->segment() == segment) {
223 return true;
224 }
225 }
226 return false;
227}
228
229SkOpContour* SkOpSpanBase::contour() const {
230 return segment()->contour();
231}
232
233SkOpGlobalState* SkOpSpanBase::globalState() const {
caryclark55888e42016-07-18 10:01:36 -0700234 return contour()->globalState();
caryclark54359292015-03-26 07:52:43 -0700235}
236
237void SkOpSpanBase::initBase(SkOpSegment* segment, SkOpSpan* prev, double t, const SkPoint& pt) {
238 fSegment = segment;
239 fPtT.init(this, t, pt, false);
240 fCoinEnd = this;
halcanary96fcdcc2015-08-27 07:41:13 -0700241 fFromAngle = nullptr;
caryclark54359292015-03-26 07:52:43 -0700242 fPrev = prev;
caryclark08bc8482015-04-24 09:08:57 -0700243 fSpanAdds = 0;
caryclark54359292015-03-26 07:52:43 -0700244 fAligned = true;
245 fChased = false;
caryclark1049f122015-04-20 08:31:59 -0700246 SkDEBUGCODE(fCount = 1);
247 SkDEBUGCODE(fID = globalState()->nextSpanID());
caryclark30b9fdd2016-08-31 14:36:29 -0700248 SkDEBUGCODE(fDebugDeleted = false);
caryclark54359292015-03-26 07:52:43 -0700249}
250
251// this pair of spans share a common t value or point; merge them and eliminate duplicates
252// this does not compute the best t or pt value; this merely moves all data into a single list
253void SkOpSpanBase::merge(SkOpSpan* span) {
254 SkOpPtT* spanPtT = span->ptT();
255 SkASSERT(this->t() != spanPtT->fT);
256 SkASSERT(!zero_or_one(spanPtT->fT));
mtklein18300a32016-03-16 13:53:35 -0700257 span->release(this->ptT());
caryclark55888e42016-07-18 10:01:36 -0700258 if (this->contains(span)) {
caryclark30b9fdd2016-08-31 14:36:29 -0700259 SkOPASSERT(0); // check to see if this ever happens -- should have been found earlier
caryclark55888e42016-07-18 10:01:36 -0700260 return; // merge is already in the ptT loop
261 }
caryclark54359292015-03-26 07:52:43 -0700262 SkOpPtT* remainder = spanPtT->next();
caryclark55888e42016-07-18 10:01:36 -0700263 this->ptT()->insert(spanPtT);
caryclark54359292015-03-26 07:52:43 -0700264 while (remainder != spanPtT) {
265 SkOpPtT* next = remainder->next();
266 SkOpPtT* compare = spanPtT->next();
267 while (compare != spanPtT) {
268 SkOpPtT* nextC = compare->next();
269 if (nextC->span() == remainder->span() && nextC->fT == remainder->fT) {
270 goto tryNextRemainder;
271 }
272 compare = nextC;
273 }
274 spanPtT->insert(remainder);
275tryNextRemainder:
276 remainder = next;
277 }
caryclark08bc8482015-04-24 09:08:57 -0700278 fSpanAdds += span->fSpanAdds;
caryclark30b9fdd2016-08-31 14:36:29 -0700279}
280
caryclark55888e42016-07-18 10:01:36 -0700281// please keep in sync with debugCheckForCollapsedCoincidence()
282void SkOpSpanBase::checkForCollapsedCoincidence() {
283 SkOpCoincidence* coins = this->globalState()->coincidence();
284 if (coins->isEmpty()) {
285 return;
286 }
287// the insert above may have put both ends of a coincident run in the same span
288// for each coincident ptT in loop; see if its opposite in is also in the loop
289// this implementation is the motivation for marking that a ptT is referenced by a coincident span
290 SkOpPtT* head = this->ptT();
291 SkOpPtT* test = head;
292 do {
293 if (!test->coincident()) {
294 continue;
295 }
296 coins->markCollapsed(test);
297 } while ((test = test->next()) != head);
caryclark30b9fdd2016-08-31 14:36:29 -0700298 coins->releaseDeleted();
299}
300
301// please keep in sync with debugMergeMatches()
302// Look to see if pt-t linked list contains same segment more than once
303// if so, and if each pt-t is directly pointed to by spans in that segment,
304// merge them
305// keep the points, but remove spans so that the segment doesn't have 2 or more
306// spans pointing to the same pt-t loop at different loop elements
Cary Clarkd058b182018-11-29 09:02:32 -0500307bool SkOpSpanBase::mergeMatches(SkOpSpanBase* opp) {
caryclark30b9fdd2016-08-31 14:36:29 -0700308 SkOpPtT* test = &fPtT;
309 SkOpPtT* testNext;
310 const SkOpPtT* stop = test;
Cary Clarkd058b182018-11-29 09:02:32 -0500311 int safetyHatch = 1000000;
caryclark30b9fdd2016-08-31 14:36:29 -0700312 do {
Cary Clarkd058b182018-11-29 09:02:32 -0500313 if (!--safetyHatch) {
314 return false;
315 }
caryclark30b9fdd2016-08-31 14:36:29 -0700316 testNext = test->next();
317 if (test->deleted()) {
318 continue;
319 }
320 SkOpSpanBase* testBase = test->span();
321 SkASSERT(testBase->ptT() == test);
322 SkOpSegment* segment = test->segment();
323 if (segment->done()) {
324 continue;
325 }
326 SkOpPtT* inner = opp->ptT();
327 const SkOpPtT* innerStop = inner;
328 do {
329 if (inner->segment() != segment) {
330 continue;
331 }
332 if (inner->deleted()) {
333 continue;
334 }
335 SkOpSpanBase* innerBase = inner->span();
336 SkASSERT(innerBase->ptT() == inner);
Ben Wagner63fd7602017-10-09 15:45:33 -0400337 // when the intersection is first detected, the span base is marked if there are
caryclark30b9fdd2016-08-31 14:36:29 -0700338 // more than one point in the intersection.
339 if (!zero_or_one(inner->fT)) {
340 innerBase->upCast()->release(test);
341 } else {
caryclarkb393a492016-09-07 08:21:09 -0700342 SkOPASSERT(inner->fT != test->fT);
caryclark30b9fdd2016-08-31 14:36:29 -0700343 if (!zero_or_one(test->fT)) {
344 testBase->upCast()->release(inner);
345 } else {
346 segment->markAllDone(); // mark segment as collapsed
347 SkDEBUGCODE(testBase->debugSetDeleted());
348 test->setDeleted();
349 SkDEBUGCODE(innerBase->debugSetDeleted());
350 inner->setDeleted();
351 }
352 }
353#ifdef SK_DEBUG // assert if another undeleted entry points to segment
354 const SkOpPtT* debugInner = inner;
355 while ((debugInner = debugInner->next()) != innerStop) {
356 if (debugInner->segment() != segment) {
357 continue;
358 }
359 if (debugInner->deleted()) {
360 continue;
361 }
362 SkOPASSERT(0);
363 }
364#endif
365 break;
366 } while ((inner = inner->next()) != innerStop);
367 } while ((test = testNext) != stop);
368 this->checkForCollapsedCoincidence();
Cary Clarkd058b182018-11-29 09:02:32 -0500369 return true;
caryclark54359292015-03-26 07:52:43 -0700370}
371
caryclarkbca19f72015-05-13 08:23:48 -0700372int SkOpSpan::computeWindSum() {
373 SkOpGlobalState* globals = this->globalState();
374 SkOpContour* contourHead = globals->contourHead();
375 int windTry = 0;
376 while (!this->sortableTop(contourHead) && ++windTry < SkOpGlobalState::kMaxWindingTries) {
caryclarkbca19f72015-05-13 08:23:48 -0700377 }
378 return this->windSum();
379}
380
caryclark54359292015-03-26 07:52:43 -0700381bool SkOpSpan::containsCoincidence(const SkOpSegment* segment) const {
382 SkASSERT(this->segment() != segment);
383 const SkOpSpan* next = fCoincident;
384 do {
385 if (next->segment() == segment) {
386 return true;
387 }
388 } while ((next = next->fCoincident) != this);
389 return false;
390}
391
caryclark55888e42016-07-18 10:01:36 -0700392void SkOpSpan::init(SkOpSegment* segment, SkOpSpan* prev, double t, const SkPoint& pt) {
393 SkASSERT(t != 1);
394 initBase(segment, prev, t, pt);
395 fCoincident = this;
396 fToAngle = nullptr;
397 fWindSum = fOppSum = SK_MinS32;
398 fWindValue = 1;
399 fOppValue = 0;
400 fTopTTry = 0;
401 fChased = fDone = false;
402 segment->bumpCount();
403 fAlreadyAdded = false;
404}
405
406// Please keep this in sync with debugInsertCoincidence()
caryclark81a478c2016-09-09 09:37:57 -0700407bool SkOpSpan::insertCoincidence(const SkOpSegment* segment, bool flipped, bool ordered) {
caryclark55888e42016-07-18 10:01:36 -0700408 if (this->containsCoincidence(segment)) {
409 return true;
410 }
411 SkOpPtT* next = &fPtT;
412 while ((next = next->next()) != &fPtT) {
413 if (next->segment() == segment) {
caryclark1493b972016-07-19 11:29:14 -0700414 SkOpSpan* span;
caryclark81a478c2016-09-09 09:37:57 -0700415 SkOpSpanBase* base = next->span();
416 if (!ordered) {
caryclarke6522ea2016-10-17 07:54:33 -0700417 const SkOpPtT* spanEndPtT = fNext->contains(segment);
418 FAIL_IF(!spanEndPtT);
419 const SkOpSpanBase* spanEnd = spanEndPtT->span();
caryclark81a478c2016-09-09 09:37:57 -0700420 const SkOpPtT* start = base->ptT()->starter(spanEnd->ptT());
caryclark45f04b82016-09-21 08:46:56 -0700421 FAIL_IF(!start->span()->upCastable());
caryclark81a478c2016-09-09 09:37:57 -0700422 span = const_cast<SkOpSpan*>(start->span()->upCast());
423 } else if (flipped) {
424 span = base->prev();
425 FAIL_IF(!span);
caryclark1493b972016-07-19 11:29:14 -0700426 } else {
caryclark81a478c2016-09-09 09:37:57 -0700427 FAIL_IF(!base->upCastable());
caryclark1493b972016-07-19 11:29:14 -0700428 span = base->upCast();
caryclark55888e42016-07-18 10:01:36 -0700429 }
430 this->insertCoincidence(span);
431 return true;
432 }
433 }
434#if DEBUG_COINCIDENCE
435 SkASSERT(0); // FIXME? if we get here, the span is missing its opposite segment...
436#endif
437 return true;
438}
439
440void SkOpSpan::release(const SkOpPtT* kept) {
caryclark30b9fdd2016-08-31 14:36:29 -0700441 SkDEBUGCODE(fDebugDeleted = true);
caryclarkb393a492016-09-07 08:21:09 -0700442 SkOPASSERT(kept->span() != this);
caryclark54359292015-03-26 07:52:43 -0700443 SkASSERT(!final());
444 SkOpSpan* prev = this->prev();
445 SkASSERT(prev);
446 SkOpSpanBase* next = this->next();
447 SkASSERT(next);
448 prev->setNext(next);
449 next->setPrev(prev);
mtklein18300a32016-03-16 13:53:35 -0700450 this->segment()->release(this);
caryclark218f21a2015-06-29 11:41:52 -0700451 SkOpCoincidence* coincidence = this->globalState()->coincidence();
452 if (coincidence) {
453 coincidence->fixUp(this->ptT(), kept);
454 }
caryclark54359292015-03-26 07:52:43 -0700455 this->ptT()->setDeleted();
caryclark55888e42016-07-18 10:01:36 -0700456 SkOpPtT* stopPtT = this->ptT();
457 SkOpPtT* testPtT = stopPtT;
458 const SkOpSpanBase* keptSpan = kept->span();
459 do {
460 if (this == testPtT->span()) {
461 testPtT->setSpan(keptSpan);
462 }
463 } while ((testPtT = testPtT->next()) != stopPtT);
caryclark54359292015-03-26 07:52:43 -0700464}
465
466void SkOpSpan::setOppSum(int oppSum) {
467 SkASSERT(!final());
468 if (fOppSum != SK_MinS32 && fOppSum != oppSum) {
469 this->globalState()->setWindingFailed();
470 return;
471 }
bungeman60e0fee2015-08-26 05:15:46 -0700472 SkASSERT(!DEBUG_LIMIT_WIND_SUM || SkTAbs(oppSum) <= DEBUG_LIMIT_WIND_SUM);
caryclark54359292015-03-26 07:52:43 -0700473 fOppSum = oppSum;
474}
caryclark624637c2015-05-11 07:21:27 -0700475
476void SkOpSpan::setWindSum(int windSum) {
477 SkASSERT(!final());
478 if (fWindSum != SK_MinS32 && fWindSum != windSum) {
479 this->globalState()->setWindingFailed();
480 return;
481 }
bungeman60e0fee2015-08-26 05:15:46 -0700482 SkASSERT(!DEBUG_LIMIT_WIND_SUM || SkTAbs(windSum) <= DEBUG_LIMIT_WIND_SUM);
caryclark624637c2015-05-11 07:21:27 -0700483 fWindSum = windSum;
484}