caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1 | /* |
| 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 | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 7 | #include "SkOpCoincidence.h" |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 8 | #include "SkOpContour.h" |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 9 | #include "SkOpSegment.h" |
| 10 | #include "SkPathWriter.h" |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 11 | #include "SkPointPriv.h" |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 12 | |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 13 | #include <utility> |
| 14 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 15 | /* |
| 16 | After computing raw intersections, post process all segments to: |
| 17 | - find small collections of points that can be collapsed to a single point |
| 18 | - find missing intersections to resolve differences caused by different algorithms |
| 19 | |
| 20 | Consider segments containing tiny or small intervals. Consider coincident segments |
| 21 | because coincidence finds intersections through distance measurement that non-coincident |
| 22 | intersection tests cannot. |
| 23 | */ |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 24 | |
| 25 | #define F (false) // discard the edge |
| 26 | #define T (true) // keep the edge |
| 27 | |
| 28 | static const bool gUnaryActiveEdge[2][2] = { |
| 29 | // from=0 from=1 |
| 30 | // to=0,1 to=0,1 |
| 31 | {F, T}, {T, F}, |
| 32 | }; |
| 33 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 34 | static const bool gActiveEdge[kXOR_SkPathOp + 1][2][2][2][2] = { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 35 | // miFrom=0 miFrom=1 |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 36 | // miTo=0 miTo=1 miTo=0 miTo=1 |
| 37 | // suFrom=0 1 suFrom=0 1 suFrom=0 1 suFrom=0 1 |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 38 | // suTo=0,1 suTo=0,1 suTo=0,1 suTo=0,1 suTo=0,1 suTo=0,1 suTo=0,1 suTo=0,1 |
| 39 | {{{{F, F}, {F, F}}, {{T, F}, {T, F}}}, {{{T, T}, {F, F}}, {{F, T}, {T, F}}}}, // mi - su |
| 40 | {{{{F, F}, {F, F}}, {{F, T}, {F, T}}}, {{{F, F}, {T, T}}, {{F, T}, {T, F}}}}, // mi & su |
| 41 | {{{{F, T}, {T, F}}, {{T, T}, {F, F}}}, {{{T, F}, {T, F}}, {{F, F}, {F, F}}}}, // mi | su |
| 42 | {{{{F, T}, {T, F}}, {{T, F}, {F, T}}}, {{{T, F}, {F, T}}, {{F, T}, {T, F}}}}, // mi ^ su |
| 43 | }; |
| 44 | |
| 45 | #undef F |
| 46 | #undef T |
| 47 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 48 | SkOpAngle* SkOpSegment::activeAngle(SkOpSpanBase* start, SkOpSpanBase** startPtr, |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 49 | SkOpSpanBase** endPtr, bool* done) { |
| 50 | if (SkOpAngle* result = activeAngleInner(start, startPtr, endPtr, done)) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 51 | return result; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 52 | } |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 53 | if (SkOpAngle* result = activeAngleOther(start, startPtr, endPtr, done)) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 54 | return result; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 55 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 56 | return nullptr; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 57 | } |
| 58 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 59 | SkOpAngle* SkOpSegment::activeAngleInner(SkOpSpanBase* start, SkOpSpanBase** startPtr, |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 60 | SkOpSpanBase** endPtr, bool* done) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 61 | SkOpSpan* upSpan = start->upCastable(); |
| 62 | if (upSpan) { |
| 63 | if (upSpan->windValue() || upSpan->oppValue()) { |
| 64 | SkOpSpanBase* next = upSpan->next(); |
| 65 | if (!*endPtr) { |
| 66 | *startPtr = start; |
| 67 | *endPtr = next; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 68 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 69 | if (!upSpan->done()) { |
| 70 | if (upSpan->windSum() != SK_MinS32) { |
| 71 | return spanToAngle(start, next); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 72 | } |
| 73 | *done = false; |
| 74 | } |
| 75 | } else { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 76 | SkASSERT(upSpan->done()); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 77 | } |
| 78 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 79 | SkOpSpan* downSpan = start->prev(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 80 | // edge leading into junction |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 81 | if (downSpan) { |
| 82 | if (downSpan->windValue() || downSpan->oppValue()) { |
| 83 | if (!*endPtr) { |
| 84 | *startPtr = start; |
| 85 | *endPtr = downSpan; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 86 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 87 | if (!downSpan->done()) { |
| 88 | if (downSpan->windSum() != SK_MinS32) { |
| 89 | return spanToAngle(start, downSpan); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 90 | } |
| 91 | *done = false; |
| 92 | } |
| 93 | } else { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 94 | SkASSERT(downSpan->done()); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 95 | } |
| 96 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 97 | return nullptr; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 98 | } |
| 99 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 100 | SkOpAngle* SkOpSegment::activeAngleOther(SkOpSpanBase* start, SkOpSpanBase** startPtr, |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 101 | SkOpSpanBase** endPtr, bool* done) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 102 | SkOpPtT* oPtT = start->ptT()->next(); |
| 103 | SkOpSegment* other = oPtT->segment(); |
| 104 | SkOpSpanBase* oSpan = oPtT->span(); |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 105 | return other->activeAngleInner(oSpan, startPtr, endPtr, done); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 106 | } |
| 107 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 108 | bool SkOpSegment::activeOp(SkOpSpanBase* start, SkOpSpanBase* end, int xorMiMask, int xorSuMask, |
| 109 | SkPathOp op) { |
| 110 | int sumMiWinding = this->updateWinding(end, start); |
| 111 | int sumSuWinding = this->updateOppWinding(end, start); |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 112 | #if DEBUG_LIMIT_WIND_SUM |
| 113 | SkASSERT(abs(sumMiWinding) <= DEBUG_LIMIT_WIND_SUM); |
| 114 | SkASSERT(abs(sumSuWinding) <= DEBUG_LIMIT_WIND_SUM); |
| 115 | #endif |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 116 | if (this->operand()) { |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 117 | using std::swap; |
| 118 | swap(sumMiWinding, sumSuWinding); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 119 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 120 | return this->activeOp(xorMiMask, xorSuMask, start, end, op, &sumMiWinding, &sumSuWinding); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 121 | } |
| 122 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 123 | bool SkOpSegment::activeOp(int xorMiMask, int xorSuMask, SkOpSpanBase* start, SkOpSpanBase* end, |
| 124 | SkPathOp op, int* sumMiWinding, int* sumSuWinding) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 125 | int maxWinding, sumWinding, oppMaxWinding, oppSumWinding; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 126 | this->setUpWindings(start, end, sumMiWinding, sumSuWinding, |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 127 | &maxWinding, &sumWinding, &oppMaxWinding, &oppSumWinding); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 128 | bool miFrom; |
| 129 | bool miTo; |
| 130 | bool suFrom; |
| 131 | bool suTo; |
| 132 | if (operand()) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 133 | miFrom = (oppMaxWinding & xorMiMask) != 0; |
| 134 | miTo = (oppSumWinding & xorMiMask) != 0; |
| 135 | suFrom = (maxWinding & xorSuMask) != 0; |
| 136 | suTo = (sumWinding & xorSuMask) != 0; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 137 | } else { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 138 | miFrom = (maxWinding & xorMiMask) != 0; |
| 139 | miTo = (sumWinding & xorMiMask) != 0; |
| 140 | suFrom = (oppMaxWinding & xorSuMask) != 0; |
| 141 | suTo = (oppSumWinding & xorSuMask) != 0; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 142 | } |
| 143 | bool result = gActiveEdge[op][miFrom][miTo][suFrom][suTo]; |
| 144 | #if DEBUG_ACTIVE_OP |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 145 | SkDebugf("%s id=%d t=%1.9g tEnd=%1.9g op=%s miFrom=%d miTo=%d suFrom=%d suTo=%d result=%d\n", |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 146 | __FUNCTION__, debugID(), start->t(), end->t(), |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 147 | SkPathOpsDebug::kPathOpStr[op], miFrom, miTo, suFrom, suTo, result); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 148 | #endif |
| 149 | return result; |
| 150 | } |
| 151 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 152 | bool SkOpSegment::activeWinding(SkOpSpanBase* start, SkOpSpanBase* end) { |
| 153 | int sumWinding = updateWinding(end, start); |
| 154 | return activeWinding(start, end, &sumWinding); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 155 | } |
| 156 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 157 | bool SkOpSegment::activeWinding(SkOpSpanBase* start, SkOpSpanBase* end, int* sumWinding) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 158 | int maxWinding; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 159 | setUpWinding(start, end, &maxWinding, sumWinding); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 160 | bool from = maxWinding != 0; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 161 | bool to = *sumWinding != 0; |
| 162 | bool result = gUnaryActiveEdge[from][to]; |
| 163 | return result; |
| 164 | } |
| 165 | |
caryclark | ef784fb | 2015-10-30 12:03:06 -0700 | [diff] [blame] | 166 | bool SkOpSegment::addCurveTo(const SkOpSpanBase* start, const SkOpSpanBase* end, |
| 167 | SkPathWriter* path) const { |
Cary Clark | cadc506 | 2018-08-06 17:24:04 -0400 | [diff] [blame] | 168 | const SkOpSpan* spanStart = start->starter(end); |
| 169 | FAIL_IF(spanStart->alreadyAdded()); |
| 170 | const_cast<SkOpSpan*>(spanStart)->markAdded(); |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 171 | SkDCurveSweep curvePart; |
| 172 | start->segment()->subDivide(start, end, &curvePart.fCurve); |
| 173 | curvePart.setCurveHullSweep(fVerb); |
| 174 | SkPath::Verb verb = curvePart.isCurve() ? fVerb : SkPath::kLine_Verb; |
| 175 | path->deferredMove(start->ptT()); |
| 176 | switch (verb) { |
| 177 | case SkPath::kLine_Verb: |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 178 | FAIL_IF(!path->deferredLine(end->ptT())); |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 179 | break; |
| 180 | case SkPath::kQuad_Verb: |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 181 | path->quadTo(curvePart.fCurve.fQuad[1].asSkPoint(), end->ptT()); |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 182 | break; |
| 183 | case SkPath::kConic_Verb: |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 184 | path->conicTo(curvePart.fCurve.fConic[1].asSkPoint(), end->ptT(), |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 185 | curvePart.fCurve.fConic.fWeight); |
| 186 | break; |
| 187 | case SkPath::kCubic_Verb: |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 188 | path->cubicTo(curvePart.fCurve.fCubic[1].asSkPoint(), |
| 189 | curvePart.fCurve.fCubic[2].asSkPoint(), end->ptT()); |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame] | 190 | break; |
| 191 | default: |
| 192 | SkASSERT(0); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 193 | } |
caryclark | ef784fb | 2015-10-30 12:03:06 -0700 | [diff] [blame] | 194 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 195 | } |
| 196 | |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 197 | const SkOpPtT* SkOpSegment::existing(double t, const SkOpSegment* opp) const { |
| 198 | const SkOpSpanBase* test = &fHead; |
| 199 | const SkOpPtT* testPtT; |
| 200 | SkPoint pt = this->ptAtT(t); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 201 | do { |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 202 | testPtT = test->ptT(); |
| 203 | if (testPtT->fT == t) { |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 204 | break; |
| 205 | } |
caryclark | ef4f32a | 2016-08-24 09:24:18 -0700 | [diff] [blame] | 206 | if (!this->match(testPtT, this, t, pt)) { |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 207 | if (t < testPtT->fT) { |
| 208 | return nullptr; |
| 209 | } |
| 210 | continue; |
| 211 | } |
| 212 | if (!opp) { |
| 213 | return testPtT; |
| 214 | } |
| 215 | const SkOpPtT* loop = testPtT->next(); |
| 216 | while (loop != testPtT) { |
| 217 | if (loop->segment() == this && loop->fT == t && loop->fPt == pt) { |
| 218 | goto foundMatch; |
| 219 | } |
| 220 | loop = loop->next(); |
| 221 | } |
| 222 | return nullptr; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 223 | } while ((test = test->upCast()->next())); |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 224 | foundMatch: |
| 225 | return opp && !test->contains(opp) ? nullptr : testPtT; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 226 | } |
| 227 | |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 228 | // break the span so that the coincident part does not change the angle of the remainder |
| 229 | bool SkOpSegment::addExpanded(double newT, const SkOpSpanBase* test, bool* startOver) { |
| 230 | if (this->contains(newT)) { |
| 231 | return true; |
| 232 | } |
caryclark | 29b2563 | 2016-08-25 11:27:17 -0700 | [diff] [blame] | 233 | this->globalState()->resetAllocatedOpSpan(); |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 234 | FAIL_IF(!between(0, newT, 1)); |
caryclark | 29b2563 | 2016-08-25 11:27:17 -0700 | [diff] [blame] | 235 | SkOpPtT* newPtT = this->addT(newT); |
| 236 | *startOver |= this->globalState()->allocatedOpSpan(); |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 237 | if (!newPtT) { |
| 238 | return false; |
| 239 | } |
| 240 | newPtT->fPt = this->ptAtT(newT); |
caryclark | 29b2563 | 2016-08-25 11:27:17 -0700 | [diff] [blame] | 241 | SkOpPtT* oppPrev = test->ptT()->oppPrev(newPtT); |
| 242 | if (oppPrev) { |
caryclark | 8016b26 | 2016-09-06 05:59:47 -0700 | [diff] [blame] | 243 | // const cast away to change linked list; pt/t values stays unchanged |
caryclark | 29b2563 | 2016-08-25 11:27:17 -0700 | [diff] [blame] | 244 | SkOpSpanBase* writableTest = const_cast<SkOpSpanBase*>(test); |
caryclark | 30b9fdd | 2016-08-31 14:36:29 -0700 | [diff] [blame] | 245 | writableTest->mergeMatches(newPtT->span()); |
caryclark | 29b2563 | 2016-08-25 11:27:17 -0700 | [diff] [blame] | 246 | writableTest->ptT()->addOpp(newPtT, oppPrev); |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 247 | writableTest->checkForCollapsedCoincidence(); |
| 248 | } |
| 249 | return true; |
| 250 | } |
| 251 | |
| 252 | // Please keep this in sync with debugAddT() |
Cary Clark | 73e597d | 2017-04-18 12:08:58 -0400 | [diff] [blame] | 253 | SkOpPtT* SkOpSegment::addT(double t, const SkPoint& pt) { |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 254 | debugValidate(); |
caryclark | 29b2563 | 2016-08-25 11:27:17 -0700 | [diff] [blame] | 255 | SkOpSpanBase* spanBase = &fHead; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 256 | do { |
caryclark | 29b2563 | 2016-08-25 11:27:17 -0700 | [diff] [blame] | 257 | SkOpPtT* result = spanBase->ptT(); |
caryclark | 27c015d | 2016-09-23 05:47:20 -0700 | [diff] [blame] | 258 | if (t == result->fT || (!zero_or_one(t) && this->match(result, this, t, pt))) { |
caryclark | 29b2563 | 2016-08-25 11:27:17 -0700 | [diff] [blame] | 259 | spanBase->bumpSpanAdds(); |
caryclark | ef4f32a | 2016-08-24 09:24:18 -0700 | [diff] [blame] | 260 | return result; |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 261 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 262 | if (t < result->fT) { |
| 263 | SkOpSpan* prev = result->span()->prev(); |
caryclark | 29b2563 | 2016-08-25 11:27:17 -0700 | [diff] [blame] | 264 | FAIL_WITH_NULL_IF(!prev); |
| 265 | // marks in global state that new op span has been allocated |
| 266 | SkOpSpan* span = this->insert(prev); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 267 | span->init(this, prev, t, pt); |
| 268 | this->debugValidate(); |
| 269 | #if DEBUG_ADD_T |
| 270 | SkDebugf("%s insert t=%1.9g segID=%d spanID=%d\n", __FUNCTION__, t, |
| 271 | span->segment()->debugID(), span->debugID()); |
| 272 | #endif |
caryclark | 08bc848 | 2015-04-24 09:08:57 -0700 | [diff] [blame] | 273 | span->bumpSpanAdds(); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 274 | return span->ptT(); |
| 275 | } |
caryclark | 29b2563 | 2016-08-25 11:27:17 -0700 | [diff] [blame] | 276 | FAIL_WITH_NULL_IF(spanBase == &fTail); |
| 277 | } while ((spanBase = spanBase->upCast()->next())); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 278 | SkASSERT(0); |
caryclark | 29b2563 | 2016-08-25 11:27:17 -0700 | [diff] [blame] | 279 | return nullptr; // we never get here, but need this to satisfy compiler |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Cary Clark | 73e597d | 2017-04-18 12:08:58 -0400 | [diff] [blame] | 282 | SkOpPtT* SkOpSegment::addT(double t) { |
| 283 | return addT(t, this->ptAtT(t)); |
| 284 | } |
| 285 | |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 286 | void SkOpSegment::calcAngles() { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 287 | bool activePrior = !fHead.isCanceled(); |
| 288 | if (activePrior && !fHead.simple()) { |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 289 | addStartSpan(); |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 290 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 291 | SkOpSpan* prior = &fHead; |
| 292 | SkOpSpanBase* spanBase = fHead.next(); |
| 293 | while (spanBase != &fTail) { |
| 294 | if (activePrior) { |
Herb Derby | ecc364c | 2017-04-19 15:09:48 -0400 | [diff] [blame] | 295 | SkOpAngle* priorAngle = this->globalState()->allocator()->make<SkOpAngle>(); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 296 | priorAngle->set(spanBase, prior); |
| 297 | spanBase->setFromAngle(priorAngle); |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 298 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 299 | SkOpSpan* span = spanBase->upCast(); |
| 300 | bool active = !span->isCanceled(); |
| 301 | SkOpSpanBase* next = span->next(); |
| 302 | if (active) { |
Herb Derby | ecc364c | 2017-04-19 15:09:48 -0400 | [diff] [blame] | 303 | SkOpAngle* angle = this->globalState()->allocator()->make<SkOpAngle>(); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 304 | angle->set(span, next); |
| 305 | span->setToAngle(angle); |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 306 | } |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 307 | activePrior = active; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 308 | prior = span; |
| 309 | spanBase = next; |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 310 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 311 | if (activePrior && !fTail.simple()) { |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 312 | addEndSpan(); |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 313 | } |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 314 | } |
| 315 | |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 316 | // Please keep this in sync with debugClearAll() |
| 317 | void SkOpSegment::clearAll() { |
| 318 | SkOpSpan* span = &fHead; |
| 319 | do { |
| 320 | this->clearOne(span); |
| 321 | } while ((span = span->next()->upCastable())); |
| 322 | this->globalState()->coincidence()->release(this); |
| 323 | } |
| 324 | |
| 325 | // Please keep this in sync with debugClearOne() |
| 326 | void SkOpSegment::clearOne(SkOpSpan* span) { |
| 327 | span->setWindValue(0); |
| 328 | span->setOppValue(0); |
| 329 | this->markDone(span); |
| 330 | } |
| 331 | |
Cary Clark | ba61029 | 2018-06-19 09:47:15 -0400 | [diff] [blame] | 332 | SkOpSpanBase::Collapsed SkOpSegment::collapsed(double s, double e) const { |
caryclark | 30b9fdd | 2016-08-31 14:36:29 -0700 | [diff] [blame] | 333 | const SkOpSpanBase* span = &fHead; |
| 334 | do { |
Cary Clark | ba61029 | 2018-06-19 09:47:15 -0400 | [diff] [blame] | 335 | SkOpSpanBase::Collapsed result = span->collapsed(s, e); |
| 336 | if (SkOpSpanBase::Collapsed::kNo != result) { |
| 337 | return result; |
caryclark | 30b9fdd | 2016-08-31 14:36:29 -0700 | [diff] [blame] | 338 | } |
| 339 | } while (span->upCastable() && (span = span->upCast()->next())); |
Cary Clark | ba61029 | 2018-06-19 09:47:15 -0400 | [diff] [blame] | 340 | return SkOpSpanBase::Collapsed::kNo; |
caryclark | 30b9fdd | 2016-08-31 14:36:29 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Cary Clark | 2587f41 | 2018-07-24 12:40:10 -0400 | [diff] [blame] | 343 | bool SkOpSegment::ComputeOneSum(const SkOpAngle* baseAngle, SkOpAngle* nextAngle, |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 344 | SkOpAngle::IncludeType includeType) { |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 345 | SkOpSegment* baseSegment = baseAngle->segment(); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 346 | int sumMiWinding = baseSegment->updateWindingReverse(baseAngle); |
| 347 | int sumSuWinding; |
| 348 | bool binary = includeType >= SkOpAngle::kBinarySingle; |
| 349 | if (binary) { |
| 350 | sumSuWinding = baseSegment->updateOppWindingReverse(baseAngle); |
| 351 | if (baseSegment->operand()) { |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 352 | using std::swap; |
| 353 | swap(sumMiWinding, sumSuWinding); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | SkOpSegment* nextSegment = nextAngle->segment(); |
| 357 | int maxWinding, sumWinding; |
Cary Clark | b64db38 | 2018-07-26 07:40:16 -0400 | [diff] [blame] | 358 | SkOpSpanBase* last = nullptr; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 359 | if (binary) { |
| 360 | int oppMaxWinding, oppSumWinding; |
| 361 | nextSegment->setUpWindings(nextAngle->start(), nextAngle->end(), &sumMiWinding, |
| 362 | &sumSuWinding, &maxWinding, &sumWinding, &oppMaxWinding, &oppSumWinding); |
Cary Clark | 2587f41 | 2018-07-24 12:40:10 -0400 | [diff] [blame] | 363 | if (!nextSegment->markAngle(maxWinding, sumWinding, oppMaxWinding, oppSumWinding, |
| 364 | nextAngle, &last)) { |
| 365 | return false; |
| 366 | } |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 367 | } else { |
| 368 | nextSegment->setUpWindings(nextAngle->start(), nextAngle->end(), &sumMiWinding, |
| 369 | &maxWinding, &sumWinding); |
Cary Clark | 2587f41 | 2018-07-24 12:40:10 -0400 | [diff] [blame] | 370 | if (!nextSegment->markAngle(maxWinding, sumWinding, nextAngle, &last)) { |
| 371 | return false; |
| 372 | } |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 373 | } |
| 374 | nextAngle->setLastMarked(last); |
Cary Clark | 2587f41 | 2018-07-24 12:40:10 -0400 | [diff] [blame] | 375 | return true; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Cary Clark | 2587f41 | 2018-07-24 12:40:10 -0400 | [diff] [blame] | 378 | bool SkOpSegment::ComputeOneSumReverse(SkOpAngle* baseAngle, SkOpAngle* nextAngle, |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 379 | SkOpAngle::IncludeType includeType) { |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 380 | SkOpSegment* baseSegment = baseAngle->segment(); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 381 | int sumMiWinding = baseSegment->updateWinding(baseAngle); |
| 382 | int sumSuWinding; |
| 383 | bool binary = includeType >= SkOpAngle::kBinarySingle; |
| 384 | if (binary) { |
| 385 | sumSuWinding = baseSegment->updateOppWinding(baseAngle); |
| 386 | if (baseSegment->operand()) { |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 387 | using std::swap; |
| 388 | swap(sumMiWinding, sumSuWinding); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | SkOpSegment* nextSegment = nextAngle->segment(); |
| 392 | int maxWinding, sumWinding; |
Cary Clark | b64db38 | 2018-07-26 07:40:16 -0400 | [diff] [blame] | 393 | SkOpSpanBase* last = nullptr; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 394 | if (binary) { |
| 395 | int oppMaxWinding, oppSumWinding; |
| 396 | nextSegment->setUpWindings(nextAngle->end(), nextAngle->start(), &sumMiWinding, |
| 397 | &sumSuWinding, &maxWinding, &sumWinding, &oppMaxWinding, &oppSumWinding); |
Cary Clark | 2587f41 | 2018-07-24 12:40:10 -0400 | [diff] [blame] | 398 | if (!nextSegment->markAngle(maxWinding, sumWinding, oppMaxWinding, oppSumWinding, |
| 399 | nextAngle, &last)) { |
| 400 | return false; |
| 401 | } |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 402 | } else { |
| 403 | nextSegment->setUpWindings(nextAngle->end(), nextAngle->start(), &sumMiWinding, |
| 404 | &maxWinding, &sumWinding); |
Cary Clark | 2587f41 | 2018-07-24 12:40:10 -0400 | [diff] [blame] | 405 | if (!nextSegment->markAngle(maxWinding, sumWinding, nextAngle, &last)) { |
| 406 | return false; |
| 407 | } |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 408 | } |
| 409 | nextAngle->setLastMarked(last); |
Cary Clark | 2587f41 | 2018-07-24 12:40:10 -0400 | [diff] [blame] | 410 | return true; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 411 | } |
| 412 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 413 | // at this point, the span is already ordered, or unorderable |
| 414 | int SkOpSegment::computeSum(SkOpSpanBase* start, SkOpSpanBase* end, |
| 415 | SkOpAngle::IncludeType includeType) { |
| 416 | SkASSERT(includeType != SkOpAngle::kUnaryXor); |
| 417 | SkOpAngle* firstAngle = this->spanToAngle(end, start); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 418 | if (nullptr == firstAngle || nullptr == firstAngle->next()) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 419 | return SK_NaN32; |
| 420 | } |
| 421 | // if all angles have a computed winding, |
| 422 | // or if no adjacent angles are orderable, |
| 423 | // or if adjacent orderable angles have no computed winding, |
| 424 | // there's nothing to do |
| 425 | // if two orderable angles are adjacent, and both are next to orderable angles, |
| 426 | // and one has winding computed, transfer to the other |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 427 | SkOpAngle* baseAngle = nullptr; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 428 | bool tryReverse = false; |
| 429 | // look for counterclockwise transfers |
| 430 | SkOpAngle* angle = firstAngle->previous(); |
| 431 | SkOpAngle* next = angle->next(); |
| 432 | firstAngle = next; |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 433 | do { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 434 | SkOpAngle* prior = angle; |
| 435 | angle = next; |
| 436 | next = angle->next(); |
| 437 | SkASSERT(prior->next() == angle); |
| 438 | SkASSERT(angle->next() == next); |
| 439 | if (prior->unorderable() || angle->unorderable() || next->unorderable()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 440 | baseAngle = nullptr; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 441 | continue; |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 442 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 443 | int testWinding = angle->starter()->windSum(); |
| 444 | if (SK_MinS32 != testWinding) { |
| 445 | baseAngle = angle; |
| 446 | tryReverse = true; |
| 447 | continue; |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 448 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 449 | if (baseAngle) { |
| 450 | ComputeOneSum(baseAngle, angle, includeType); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 451 | baseAngle = SK_MinS32 != angle->starter()->windSum() ? angle : nullptr; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 452 | } |
| 453 | } while (next != firstAngle); |
| 454 | if (baseAngle && SK_MinS32 == firstAngle->starter()->windSum()) { |
| 455 | firstAngle = baseAngle; |
| 456 | tryReverse = true; |
| 457 | } |
| 458 | if (tryReverse) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 459 | baseAngle = nullptr; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 460 | SkOpAngle* prior = firstAngle; |
| 461 | do { |
| 462 | angle = prior; |
| 463 | prior = angle->previous(); |
| 464 | SkASSERT(prior->next() == angle); |
| 465 | next = angle->next(); |
| 466 | if (prior->unorderable() || angle->unorderable() || next->unorderable()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 467 | baseAngle = nullptr; |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 468 | continue; |
| 469 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 470 | int testWinding = angle->starter()->windSum(); |
| 471 | if (SK_MinS32 != testWinding) { |
| 472 | baseAngle = angle; |
| 473 | continue; |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 474 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 475 | if (baseAngle) { |
| 476 | ComputeOneSumReverse(baseAngle, angle, includeType); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 477 | baseAngle = SK_MinS32 != angle->starter()->windSum() ? angle : nullptr; |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 478 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 479 | } while (prior != firstAngle); |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 480 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 481 | return start->starter(end)->windSum(); |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 482 | } |
| 483 | |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 484 | bool SkOpSegment::contains(double newT) const { |
| 485 | const SkOpSpanBase* spanBase = &fHead; |
| 486 | do { |
| 487 | if (spanBase->ptT()->contains(this, newT)) { |
| 488 | return true; |
| 489 | } |
| 490 | if (spanBase == &fTail) { |
| 491 | break; |
| 492 | } |
| 493 | spanBase = spanBase->upCast()->next(); |
| 494 | } while (true); |
| 495 | return false; |
| 496 | } |
| 497 | |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 498 | void SkOpSegment::release(const SkOpSpan* span) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 499 | if (span->done()) { |
caryclark | 08bc848 | 2015-04-24 09:08:57 -0700 | [diff] [blame] | 500 | --fDoneCount; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 501 | } |
caryclark | 08bc848 | 2015-04-24 09:08:57 -0700 | [diff] [blame] | 502 | --fCount; |
caryclark | 1597628 | 2016-07-21 05:48:43 -0700 | [diff] [blame] | 503 | SkOPASSERT(fCount >= fDoneCount); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Cary Clark | e47ae29 | 2016-10-05 08:51:39 -0400 | [diff] [blame] | 506 | #if DEBUG_ANGLE |
| 507 | // called only by debugCheckNearCoincidence |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 508 | double SkOpSegment::distSq(double t, const SkOpAngle* oppAngle) const { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 509 | SkDPoint testPt = this->dPtAtT(t); |
| 510 | SkDLine testPerp = {{ testPt, testPt }}; |
| 511 | SkDVector slope = this->dSlopeAtT(t); |
| 512 | testPerp[1].fX += slope.fY; |
| 513 | testPerp[1].fY -= slope.fX; |
| 514 | SkIntersections i; |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 515 | const SkOpSegment* oppSegment = oppAngle->segment(); |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 516 | (*CurveIntersectRay[oppSegment->verb()])(oppSegment->pts(), oppSegment->weight(), testPerp, &i); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 517 | double closestDistSq = SK_ScalarInfinity; |
| 518 | for (int index = 0; index < i.used(); ++index) { |
| 519 | if (!between(oppAngle->start()->t(), i[0][index], oppAngle->end()->t())) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 520 | continue; |
| 521 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 522 | double testDistSq = testPt.distanceSquared(i.pt(index)); |
| 523 | if (closestDistSq > testDistSq) { |
| 524 | closestDistSq = testDistSq; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 525 | } |
| 526 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 527 | return closestDistSq; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 528 | } |
Cary Clark | e47ae29 | 2016-10-05 08:51:39 -0400 | [diff] [blame] | 529 | #endif |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 530 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 531 | /* |
| 532 | The M and S variable name parts stand for the operators. |
| 533 | Mi stands for Minuend (see wiki subtraction, analogous to difference) |
| 534 | Su stands for Subtrahend |
| 535 | The Opp variable name part designates that the value is for the Opposite operator. |
| 536 | Opposite values result from combining coincident spans. |
| 537 | */ |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 538 | SkOpSegment* SkOpSegment::findNextOp(SkTDArray<SkOpSpanBase*>* chase, SkOpSpanBase** nextStart, |
Cary Clark | 1857ddb | 2018-07-11 11:01:43 -0400 | [diff] [blame] | 539 | SkOpSpanBase** nextEnd, bool* unsortable, bool* simple, |
| 540 | SkPathOp op, int xorMiMask, int xorSuMask) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 541 | SkOpSpanBase* start = *nextStart; |
| 542 | SkOpSpanBase* end = *nextEnd; |
| 543 | SkASSERT(start != end); |
| 544 | int step = start->step(end); |
| 545 | SkOpSegment* other = this->isSimple(nextStart, &step); // advances nextStart |
Cary Clark | 1857ddb | 2018-07-11 11:01:43 -0400 | [diff] [blame] | 546 | if ((*simple = other)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 547 | // mark the smaller of startIndex, endIndex done, and all adjacent |
| 548 | // spans with the same T value (but not 'other' spans) |
| 549 | #if DEBUG_WINDING |
| 550 | SkDebugf("%s simple\n", __FUNCTION__); |
| 551 | #endif |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 552 | SkOpSpan* startSpan = start->starter(end); |
| 553 | if (startSpan->done()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 554 | return nullptr; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 555 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 556 | markDone(startSpan); |
| 557 | *nextEnd = step > 0 ? (*nextStart)->upCast()->next() : (*nextStart)->prev(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 558 | return other; |
| 559 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 560 | SkOpSpanBase* endNear = step > 0 ? (*nextStart)->upCast()->next() : (*nextStart)->prev(); |
| 561 | SkASSERT(endNear == end); // is this ever not end? |
| 562 | SkASSERT(endNear); |
| 563 | SkASSERT(start != endNear); |
| 564 | SkASSERT((start->t() < endNear->t()) ^ (step < 0)); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 565 | // more than one viable candidate -- measure angles to find best |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 566 | int calcWinding = computeSum(start, endNear, SkOpAngle::kBinaryOpp); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 567 | bool sortable = calcWinding != SK_NaN32; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 568 | if (!sortable) { |
| 569 | *unsortable = true; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 570 | markDone(start->starter(end)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 571 | return nullptr; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 572 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 573 | SkOpAngle* angle = this->spanToAngle(end, start); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 574 | if (angle->unorderable()) { |
| 575 | *unsortable = true; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 576 | markDone(start->starter(end)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 577 | return nullptr; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 578 | } |
| 579 | #if DEBUG_SORT |
| 580 | SkDebugf("%s\n", __FUNCTION__); |
| 581 | angle->debugLoop(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 582 | #endif |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 583 | int sumMiWinding = updateWinding(end, start); |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 584 | if (sumMiWinding == SK_MinS32) { |
| 585 | *unsortable = true; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 586 | markDone(start->starter(end)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 587 | return nullptr; |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 588 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 589 | int sumSuWinding = updateOppWinding(end, start); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 590 | if (operand()) { |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 591 | using std::swap; |
| 592 | swap(sumMiWinding, sumSuWinding); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 593 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 594 | SkOpAngle* nextAngle = angle->next(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 595 | const SkOpAngle* foundAngle = nullptr; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 596 | bool foundDone = false; |
| 597 | // iterate through the angle, and compute everyone's winding |
| 598 | SkOpSegment* nextSegment; |
| 599 | int activeCount = 0; |
| 600 | do { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 601 | nextSegment = nextAngle->segment(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 602 | bool activeAngle = nextSegment->activeOp(xorMiMask, xorSuMask, nextAngle->start(), |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 603 | nextAngle->end(), op, &sumMiWinding, &sumSuWinding); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 604 | if (activeAngle) { |
| 605 | ++activeCount; |
| 606 | if (!foundAngle || (foundDone && activeCount & 1)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 607 | foundAngle = nextAngle; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 608 | foundDone = nextSegment->done(nextAngle); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | if (nextSegment->done()) { |
| 612 | continue; |
| 613 | } |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 614 | if (!activeAngle) { |
Cary Clark | c050a1a | 2018-06-25 08:45:40 -0400 | [diff] [blame] | 615 | (void) nextSegment->markAndChaseDone(nextAngle->start(), nextAngle->end(), nullptr); |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 616 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 617 | SkOpSpanBase* last = nextAngle->lastMarked(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 618 | if (last) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 619 | SkASSERT(!SkPathOpsDebug::ChaseContains(*chase, last)); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 620 | *chase->append() = last; |
| 621 | #if DEBUG_WINDING |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 622 | SkDebugf("%s chase.append segment=%d span=%d", __FUNCTION__, |
| 623 | last->segment()->debugID(), last->debugID()); |
| 624 | if (!last->final()) { |
| 625 | SkDebugf(" windSum=%d", last->upCast()->windSum()); |
| 626 | } |
| 627 | SkDebugf("\n"); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 628 | #endif |
| 629 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 630 | } while ((nextAngle = nextAngle->next()) != angle); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 631 | start->segment()->markDone(start->starter(end)); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 632 | if (!foundAngle) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 633 | return nullptr; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 634 | } |
| 635 | *nextStart = foundAngle->start(); |
| 636 | *nextEnd = foundAngle->end(); |
| 637 | nextSegment = foundAngle->segment(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 638 | #if DEBUG_WINDING |
| 639 | SkDebugf("%s from:[%d] to:[%d] start=%d end=%d\n", |
| 640 | __FUNCTION__, debugID(), nextSegment->debugID(), *nextStart, *nextEnd); |
| 641 | #endif |
| 642 | return nextSegment; |
| 643 | } |
| 644 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 645 | SkOpSegment* SkOpSegment::findNextWinding(SkTDArray<SkOpSpanBase*>* chase, |
| 646 | SkOpSpanBase** nextStart, SkOpSpanBase** nextEnd, bool* unsortable) { |
| 647 | SkOpSpanBase* start = *nextStart; |
| 648 | SkOpSpanBase* end = *nextEnd; |
| 649 | SkASSERT(start != end); |
| 650 | int step = start->step(end); |
| 651 | SkOpSegment* other = this->isSimple(nextStart, &step); // advances nextStart |
| 652 | if (other) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 653 | // mark the smaller of startIndex, endIndex done, and all adjacent |
| 654 | // spans with the same T value (but not 'other' spans) |
| 655 | #if DEBUG_WINDING |
| 656 | SkDebugf("%s simple\n", __FUNCTION__); |
| 657 | #endif |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 658 | SkOpSpan* startSpan = start->starter(end); |
| 659 | if (startSpan->done()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 660 | return nullptr; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 661 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 662 | markDone(startSpan); |
| 663 | *nextEnd = step > 0 ? (*nextStart)->upCast()->next() : (*nextStart)->prev(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 664 | return other; |
| 665 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 666 | SkOpSpanBase* endNear = step > 0 ? (*nextStart)->upCast()->next() : (*nextStart)->prev(); |
| 667 | SkASSERT(endNear == end); // is this ever not end? |
| 668 | SkASSERT(endNear); |
| 669 | SkASSERT(start != endNear); |
| 670 | SkASSERT((start->t() < endNear->t()) ^ (step < 0)); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 671 | // more than one viable candidate -- measure angles to find best |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 672 | int calcWinding = computeSum(start, endNear, SkOpAngle::kUnaryWinding); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 673 | bool sortable = calcWinding != SK_NaN32; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 674 | if (!sortable) { |
| 675 | *unsortable = true; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 676 | markDone(start->starter(end)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 677 | return nullptr; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 678 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 679 | SkOpAngle* angle = this->spanToAngle(end, start); |
| 680 | if (angle->unorderable()) { |
| 681 | *unsortable = true; |
| 682 | markDone(start->starter(end)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 683 | return nullptr; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 684 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 685 | #if DEBUG_SORT |
| 686 | SkDebugf("%s\n", __FUNCTION__); |
| 687 | angle->debugLoop(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 688 | #endif |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 689 | int sumWinding = updateWinding(end, start); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 690 | SkOpAngle* nextAngle = angle->next(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 691 | const SkOpAngle* foundAngle = nullptr; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 692 | bool foundDone = false; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 693 | // iterate through the angle, and compute everyone's winding |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 694 | SkOpSegment* nextSegment; |
| 695 | int activeCount = 0; |
| 696 | do { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 697 | nextSegment = nextAngle->segment(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 698 | bool activeAngle = nextSegment->activeWinding(nextAngle->start(), nextAngle->end(), |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 699 | &sumWinding); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 700 | if (activeAngle) { |
| 701 | ++activeCount; |
| 702 | if (!foundAngle || (foundDone && activeCount & 1)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 703 | foundAngle = nextAngle; |
| 704 | foundDone = nextSegment->done(nextAngle); |
| 705 | } |
| 706 | } |
| 707 | if (nextSegment->done()) { |
| 708 | continue; |
| 709 | } |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 710 | if (!activeAngle) { |
Cary Clark | c050a1a | 2018-06-25 08:45:40 -0400 | [diff] [blame] | 711 | (void) nextSegment->markAndChaseDone(nextAngle->start(), nextAngle->end(), nullptr); |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 712 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 713 | SkOpSpanBase* last = nextAngle->lastMarked(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 714 | if (last) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 715 | SkASSERT(!SkPathOpsDebug::ChaseContains(*chase, last)); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 716 | *chase->append() = last; |
| 717 | #if DEBUG_WINDING |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 718 | SkDebugf("%s chase.append segment=%d span=%d", __FUNCTION__, |
| 719 | last->segment()->debugID(), last->debugID()); |
| 720 | if (!last->final()) { |
| 721 | SkDebugf(" windSum=%d", last->upCast()->windSum()); |
| 722 | } |
| 723 | SkDebugf("\n"); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 724 | #endif |
| 725 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 726 | } while ((nextAngle = nextAngle->next()) != angle); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 727 | start->segment()->markDone(start->starter(end)); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 728 | if (!foundAngle) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 729 | return nullptr; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 730 | } |
| 731 | *nextStart = foundAngle->start(); |
| 732 | *nextEnd = foundAngle->end(); |
| 733 | nextSegment = foundAngle->segment(); |
| 734 | #if DEBUG_WINDING |
| 735 | SkDebugf("%s from:[%d] to:[%d] start=%d end=%d\n", |
| 736 | __FUNCTION__, debugID(), nextSegment->debugID(), *nextStart, *nextEnd); |
| 737 | #endif |
| 738 | return nextSegment; |
| 739 | } |
| 740 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 741 | SkOpSegment* SkOpSegment::findNextXor(SkOpSpanBase** nextStart, SkOpSpanBase** nextEnd, |
| 742 | bool* unsortable) { |
| 743 | SkOpSpanBase* start = *nextStart; |
| 744 | SkOpSpanBase* end = *nextEnd; |
| 745 | SkASSERT(start != end); |
| 746 | int step = start->step(end); |
| 747 | SkOpSegment* other = this->isSimple(nextStart, &step); // advances nextStart |
| 748 | if (other) { |
| 749 | // mark the smaller of startIndex, endIndex done, and all adjacent |
| 750 | // spans with the same T value (but not 'other' spans) |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 751 | #if DEBUG_WINDING |
| 752 | SkDebugf("%s simple\n", __FUNCTION__); |
| 753 | #endif |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 754 | SkOpSpan* startSpan = start->starter(end); |
| 755 | if (startSpan->done()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 756 | return nullptr; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 757 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 758 | markDone(startSpan); |
| 759 | *nextEnd = step > 0 ? (*nextStart)->upCast()->next() : (*nextStart)->prev(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 760 | return other; |
| 761 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 762 | SkDEBUGCODE(SkOpSpanBase* endNear = step > 0 ? (*nextStart)->upCast()->next() \ |
| 763 | : (*nextStart)->prev()); |
| 764 | SkASSERT(endNear == end); // is this ever not end? |
| 765 | SkASSERT(endNear); |
| 766 | SkASSERT(start != endNear); |
| 767 | SkASSERT((start->t() < endNear->t()) ^ (step < 0)); |
| 768 | SkOpAngle* angle = this->spanToAngle(end, start); |
caryclark | 27c8eb8 | 2015-07-06 11:38:33 -0700 | [diff] [blame] | 769 | if (!angle || angle->unorderable()) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 770 | *unsortable = true; |
| 771 | markDone(start->starter(end)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 772 | return nullptr; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 773 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 774 | #if DEBUG_SORT |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 775 | SkDebugf("%s\n", __FUNCTION__); |
| 776 | angle->debugLoop(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 777 | #endif |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 778 | SkOpAngle* nextAngle = angle->next(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 779 | const SkOpAngle* foundAngle = nullptr; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 780 | bool foundDone = false; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 781 | // iterate through the angle, and compute everyone's winding |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 782 | SkOpSegment* nextSegment; |
| 783 | int activeCount = 0; |
| 784 | do { |
Cary Clark | 5607143 | 2018-06-19 08:33:52 -0400 | [diff] [blame] | 785 | if (!nextAngle) { |
| 786 | return nullptr; |
| 787 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 788 | nextSegment = nextAngle->segment(); |
| 789 | ++activeCount; |
| 790 | if (!foundAngle || (foundDone && activeCount & 1)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 791 | foundAngle = nextAngle; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 792 | if (!(foundDone = nextSegment->done(nextAngle))) { |
| 793 | break; |
| 794 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 795 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 796 | nextAngle = nextAngle->next(); |
| 797 | } while (nextAngle != angle); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 798 | start->segment()->markDone(start->starter(end)); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 799 | if (!foundAngle) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 800 | return nullptr; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 801 | } |
| 802 | *nextStart = foundAngle->start(); |
| 803 | *nextEnd = foundAngle->end(); |
| 804 | nextSegment = foundAngle->segment(); |
| 805 | #if DEBUG_WINDING |
| 806 | SkDebugf("%s from:[%d] to:[%d] start=%d end=%d\n", |
| 807 | __FUNCTION__, debugID(), nextSegment->debugID(), *nextStart, *nextEnd); |
| 808 | #endif |
| 809 | return nextSegment; |
| 810 | } |
| 811 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 812 | SkOpGlobalState* SkOpSegment::globalState() const { |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 813 | return contour()->globalState(); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 814 | } |
| 815 | |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 816 | void SkOpSegment::init(SkPoint pts[], SkScalar weight, SkOpContour* contour, SkPath::Verb verb) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 817 | fContour = contour; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 818 | fNext = nullptr; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 819 | fPts = pts; |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 820 | fWeight = weight; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 821 | fVerb = verb; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 822 | fCount = 0; |
| 823 | fDoneCount = 0; |
| 824 | fVisited = false; |
| 825 | SkOpSpan* zeroSpan = &fHead; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 826 | zeroSpan->init(this, nullptr, 0, fPts[0]); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 827 | SkOpSpanBase* oneSpan = &fTail; |
| 828 | zeroSpan->setNext(oneSpan); |
| 829 | oneSpan->initBase(this, zeroSpan, 1, fPts[SkPathOpsVerbToPoints(fVerb)]); |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 830 | SkDEBUGCODE(fID = globalState()->nextSegmentID()); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 831 | } |
| 832 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 833 | bool SkOpSegment::isClose(double t, const SkOpSegment* opp) const { |
| 834 | SkDPoint cPt = this->dPtAtT(t); |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 835 | SkDVector dxdy = (*CurveDSlopeAtT[this->verb()])(this->pts(), this->weight(), t); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 836 | SkDLine perp = {{ cPt, {cPt.fX + dxdy.fY, cPt.fY - dxdy.fX} }}; |
| 837 | SkIntersections i; |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 838 | (*CurveIntersectRay[opp->verb()])(opp->pts(), opp->weight(), perp, &i); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 839 | int used = i.used(); |
| 840 | for (int index = 0; index < used; ++index) { |
| 841 | if (cPt.roughlyEqual(i.pt(index))) { |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 842 | return true; |
| 843 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 844 | } |
caryclark@google.com | a2bbc6e | 2013-11-01 17:36:03 +0000 | [diff] [blame] | 845 | return false; |
| 846 | } |
| 847 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 848 | bool SkOpSegment::isXor() const { |
| 849 | return fContour->isXor(); |
| 850 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 851 | |
caryclark | 5b5ddd7 | 2015-05-18 05:12:56 -0700 | [diff] [blame] | 852 | void SkOpSegment::markAllDone() { |
| 853 | SkOpSpan* span = this->head(); |
| 854 | do { |
| 855 | this->markDone(span); |
| 856 | } while ((span = span->next()->upCastable())); |
| 857 | } |
| 858 | |
Cary Clark | c050a1a | 2018-06-25 08:45:40 -0400 | [diff] [blame] | 859 | bool SkOpSegment::markAndChaseDone(SkOpSpanBase* start, SkOpSpanBase* end, SkOpSpanBase** found) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 860 | int step = start->step(end); |
| 861 | SkOpSpan* minSpan = start->starter(end); |
| 862 | markDone(minSpan); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 863 | SkOpSpanBase* last = nullptr; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 864 | SkOpSegment* other = this; |
Cary Clark | 918fb1f | 2016-11-15 13:22:25 -0500 | [diff] [blame] | 865 | SkOpSpan* priorDone = nullptr; |
| 866 | SkOpSpan* lastDone = nullptr; |
Cary Clark | c050a1a | 2018-06-25 08:45:40 -0400 | [diff] [blame] | 867 | int safetyNet = 100000; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 868 | while ((other = other->nextChase(&start, &step, &minSpan, &last))) { |
Cary Clark | c050a1a | 2018-06-25 08:45:40 -0400 | [diff] [blame] | 869 | if (!--safetyNet) { |
| 870 | return false; |
| 871 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 872 | if (other->done()) { |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 873 | SkASSERT(!last); |
| 874 | break; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 875 | } |
Cary Clark | 918fb1f | 2016-11-15 13:22:25 -0500 | [diff] [blame] | 876 | if (lastDone == minSpan || priorDone == minSpan) { |
Cary Clark | c050a1a | 2018-06-25 08:45:40 -0400 | [diff] [blame] | 877 | if (found) { |
| 878 | *found = nullptr; |
| 879 | } |
| 880 | return true; |
Cary Clark | 918fb1f | 2016-11-15 13:22:25 -0500 | [diff] [blame] | 881 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 882 | other->markDone(minSpan); |
Cary Clark | 918fb1f | 2016-11-15 13:22:25 -0500 | [diff] [blame] | 883 | priorDone = lastDone; |
| 884 | lastDone = minSpan; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 885 | } |
Cary Clark | c050a1a | 2018-06-25 08:45:40 -0400 | [diff] [blame] | 886 | if (found) { |
| 887 | *found = last; |
| 888 | } |
| 889 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 890 | } |
| 891 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 892 | bool SkOpSegment::markAndChaseWinding(SkOpSpanBase* start, SkOpSpanBase* end, int winding, |
| 893 | SkOpSpanBase** lastPtr) { |
| 894 | SkOpSpan* spanStart = start->starter(end); |
| 895 | int step = start->step(end); |
| 896 | bool success = markWinding(spanStart, winding); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 897 | SkOpSpanBase* last = nullptr; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 898 | SkOpSegment* other = this; |
Cary Clark | 5de5233 | 2018-08-30 12:58:23 -0400 | [diff] [blame] | 899 | int safetyNet = 100000; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 900 | while ((other = other->nextChase(&start, &step, &spanStart, &last))) { |
Cary Clark | 5de5233 | 2018-08-30 12:58:23 -0400 | [diff] [blame] | 901 | if (!--safetyNet) { |
| 902 | return false; |
| 903 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 904 | if (spanStart->windSum() != SK_MinS32) { |
caryclark | b36a3cd | 2016-10-18 07:59:44 -0700 | [diff] [blame] | 905 | // SkASSERT(spanStart->windSum() == winding); // FIXME: is this assert too aggressive? |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 906 | SkASSERT(!last); |
| 907 | break; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 908 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 909 | (void) other->markWinding(spanStart, winding); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 910 | } |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 911 | if (lastPtr) { |
| 912 | *lastPtr = last; |
| 913 | } |
| 914 | return success; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 915 | } |
| 916 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 917 | bool SkOpSegment::markAndChaseWinding(SkOpSpanBase* start, SkOpSpanBase* end, |
| 918 | int winding, int oppWinding, SkOpSpanBase** lastPtr) { |
| 919 | SkOpSpan* spanStart = start->starter(end); |
| 920 | int step = start->step(end); |
| 921 | bool success = markWinding(spanStart, winding, oppWinding); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 922 | SkOpSpanBase* last = nullptr; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 923 | SkOpSegment* other = this; |
Cary Clark | 4929a4a | 2018-10-17 14:12:41 -0400 | [diff] [blame^] | 924 | int safetyNet = 100000; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 925 | while ((other = other->nextChase(&start, &step, &spanStart, &last))) { |
Cary Clark | 4929a4a | 2018-10-17 14:12:41 -0400 | [diff] [blame^] | 926 | if (!--safetyNet) { |
| 927 | return false; |
| 928 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 929 | if (spanStart->windSum() != SK_MinS32) { |
| 930 | if (this->operand() == other->operand()) { |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 931 | if (spanStart->windSum() != winding || spanStart->oppSum() != oppWinding) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 932 | this->globalState()->setWindingFailed(); |
Cary Clark | 2587f41 | 2018-07-24 12:40:10 -0400 | [diff] [blame] | 933 | return true; // ... but let it succeed anyway |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 934 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 935 | } else { |
Cary Clark | 2587f41 | 2018-07-24 12:40:10 -0400 | [diff] [blame] | 936 | FAIL_IF(spanStart->windSum() != oppWinding); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 937 | SkASSERT(spanStart->oppSum() == winding); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 938 | } |
| 939 | SkASSERT(!last); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 940 | break; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 941 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 942 | if (this->operand() == other->operand()) { |
| 943 | (void) other->markWinding(spanStart, winding, oppWinding); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 944 | } else { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 945 | (void) other->markWinding(spanStart, oppWinding, winding); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 946 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 947 | } |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 948 | if (lastPtr) { |
| 949 | *lastPtr = last; |
| 950 | } |
| 951 | return success; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 952 | } |
| 953 | |
Cary Clark | 2587f41 | 2018-07-24 12:40:10 -0400 | [diff] [blame] | 954 | bool SkOpSegment::markAngle(int maxWinding, int sumWinding, const SkOpAngle* angle, |
| 955 | SkOpSpanBase** result) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 956 | SkASSERT(angle->segment() == this); |
| 957 | if (UseInnerWinding(maxWinding, sumWinding)) { |
| 958 | maxWinding = sumWinding; |
| 959 | } |
Cary Clark | 2587f41 | 2018-07-24 12:40:10 -0400 | [diff] [blame] | 960 | if (!markAndChaseWinding(angle->start(), angle->end(), maxWinding, result)) { |
| 961 | return false; |
| 962 | } |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 963 | #if DEBUG_WINDING |
Cary Clark | cadc506 | 2018-08-06 17:24:04 -0400 | [diff] [blame] | 964 | SkOpSpanBase* last = *result; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 965 | if (last) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 966 | SkDebugf("%s last seg=%d span=%d", __FUNCTION__, |
| 967 | last->segment()->debugID(), last->debugID()); |
| 968 | if (!last->final()) { |
| 969 | SkDebugf(" windSum="); |
| 970 | SkPathOpsDebug::WindingPrintf(last->upCast()->windSum()); |
| 971 | } |
| 972 | SkDebugf("\n"); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 973 | } |
| 974 | #endif |
Cary Clark | 2587f41 | 2018-07-24 12:40:10 -0400 | [diff] [blame] | 975 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 976 | } |
| 977 | |
Cary Clark | 2587f41 | 2018-07-24 12:40:10 -0400 | [diff] [blame] | 978 | bool SkOpSegment::markAngle(int maxWinding, int sumWinding, int oppMaxWinding, |
| 979 | int oppSumWinding, const SkOpAngle* angle, SkOpSpanBase** result) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 980 | SkASSERT(angle->segment() == this); |
| 981 | if (UseInnerWinding(maxWinding, sumWinding)) { |
| 982 | maxWinding = sumWinding; |
| 983 | } |
| 984 | if (oppMaxWinding != oppSumWinding && UseInnerWinding(oppMaxWinding, oppSumWinding)) { |
| 985 | oppMaxWinding = oppSumWinding; |
| 986 | } |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 987 | // caller doesn't require that this marks anything |
Cary Clark | 2587f41 | 2018-07-24 12:40:10 -0400 | [diff] [blame] | 988 | if (!markAndChaseWinding(angle->start(), angle->end(), maxWinding, oppMaxWinding, result)) { |
| 989 | return false; |
| 990 | } |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 991 | #if DEBUG_WINDING |
Cary Clark | 8762fb6 | 2018-10-16 16:06:24 -0400 | [diff] [blame] | 992 | if (result) { |
| 993 | SkOpSpanBase* last = *result; |
| 994 | if (last) { |
| 995 | SkDebugf("%s last segment=%d span=%d", __FUNCTION__, |
| 996 | last->segment()->debugID(), last->debugID()); |
| 997 | if (!last->final()) { |
| 998 | SkDebugf(" windSum="); |
| 999 | SkPathOpsDebug::WindingPrintf(last->upCast()->windSum()); |
| 1000 | } |
| 1001 | SkDebugf(" \n"); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1002 | } |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 1003 | } |
| 1004 | #endif |
Cary Clark | 2587f41 | 2018-07-24 12:40:10 -0400 | [diff] [blame] | 1005 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1008 | void SkOpSegment::markDone(SkOpSpan* span) { |
| 1009 | SkASSERT(this == span->segment()); |
| 1010 | if (span->done()) { |
| 1011 | return; |
| 1012 | } |
| 1013 | #if DEBUG_MARK_DONE |
| 1014 | debugShowNewWinding(__FUNCTION__, span, span->windSum(), span->oppSum()); |
| 1015 | #endif |
| 1016 | span->setDone(true); |
| 1017 | ++fDoneCount; |
| 1018 | debugValidate(); |
| 1019 | } |
| 1020 | |
| 1021 | bool SkOpSegment::markWinding(SkOpSpan* span, int winding) { |
| 1022 | SkASSERT(this == span->segment()); |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 1023 | SkASSERT(winding); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1024 | if (span->done()) { |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 1025 | return false; |
| 1026 | } |
| 1027 | #if DEBUG_MARK_DONE |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1028 | debugShowNewWinding(__FUNCTION__, span, winding); |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 1029 | #endif |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1030 | span->setWindSum(winding); |
| 1031 | debugValidate(); |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 1032 | return true; |
| 1033 | } |
| 1034 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1035 | bool SkOpSegment::markWinding(SkOpSpan* span, int winding, int oppWinding) { |
| 1036 | SkASSERT(this == span->segment()); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1037 | SkASSERT(winding || oppWinding); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1038 | if (span->done()) { |
| 1039 | return false; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1040 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1041 | #if DEBUG_MARK_DONE |
| 1042 | debugShowNewWinding(__FUNCTION__, span, winding, oppWinding); |
| 1043 | #endif |
| 1044 | span->setWindSum(winding); |
| 1045 | span->setOppSum(oppWinding); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1046 | debugValidate(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1047 | return true; |
| 1048 | } |
| 1049 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1050 | bool SkOpSegment::match(const SkOpPtT* base, const SkOpSegment* testParent, double testT, |
caryclark | ef4f32a | 2016-08-24 09:24:18 -0700 | [diff] [blame] | 1051 | const SkPoint& testPt) const { |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1052 | SkASSERT(this == base->segment()); |
| 1053 | if (this == testParent) { |
| 1054 | if (precisely_equal(base->fT, testT)) { |
| 1055 | return true; |
| 1056 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1057 | } |
| 1058 | if (!SkDPoint::ApproximatelyEqual(testPt, base->fPt)) { |
| 1059 | return false; |
| 1060 | } |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1061 | return this != testParent || !this->ptsDisjoint(base->fT, base->fPt, testT, testPt); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | static SkOpSegment* set_last(SkOpSpanBase** last, SkOpSpanBase* endSpan) { |
| 1065 | if (last) { |
| 1066 | *last = endSpan; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1067 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1068 | return nullptr; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1069 | } |
| 1070 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1071 | SkOpSegment* SkOpSegment::nextChase(SkOpSpanBase** startPtr, int* stepPtr, SkOpSpan** minPtr, |
| 1072 | SkOpSpanBase** last) const { |
| 1073 | SkOpSpanBase* origStart = *startPtr; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1074 | int step = *stepPtr; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1075 | SkOpSpanBase* endSpan = step > 0 ? origStart->upCast()->next() : origStart->prev(); |
| 1076 | SkASSERT(endSpan); |
| 1077 | SkOpAngle* angle = step > 0 ? endSpan->fromAngle() : endSpan->upCast()->toAngle(); |
| 1078 | SkOpSpanBase* foundSpan; |
| 1079 | SkOpSpanBase* otherEnd; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1080 | SkOpSegment* other; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1081 | if (angle == nullptr) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1082 | if (endSpan->t() != 0 && endSpan->t() != 1) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1083 | return nullptr; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1084 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1085 | SkOpPtT* otherPtT = endSpan->ptT()->next(); |
| 1086 | other = otherPtT->segment(); |
| 1087 | foundSpan = otherPtT->span(); |
caryclark | 343382e | 2016-06-29 08:18:38 -0700 | [diff] [blame] | 1088 | otherEnd = step > 0 |
| 1089 | ? foundSpan->upCastable() ? foundSpan->upCast()->next() : nullptr |
| 1090 | : foundSpan->prev(); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1091 | } else { |
| 1092 | int loopCount = angle->loopCount(); |
| 1093 | if (loopCount > 2) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1094 | return set_last(last, endSpan); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1095 | } |
| 1096 | const SkOpAngle* next = angle->next(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1097 | if (nullptr == next) { |
| 1098 | return nullptr; |
caryclark | 65b427c | 2014-09-18 10:32:57 -0700 | [diff] [blame] | 1099 | } |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1100 | #if DEBUG_WINDING |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 1101 | if (angle->debugSign() != next->debugSign() && !angle->segment()->contour()->isXor() |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1102 | && !next->segment()->contour()->isXor()) { |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1103 | SkDebugf("%s mismatched signs\n", __FUNCTION__); |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 1104 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1105 | #endif |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1106 | other = next->segment(); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1107 | foundSpan = endSpan = next->start(); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1108 | otherEnd = next->end(); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 1109 | } |
caryclark | 343382e | 2016-06-29 08:18:38 -0700 | [diff] [blame] | 1110 | if (!otherEnd) { |
| 1111 | return nullptr; |
| 1112 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1113 | int foundStep = foundSpan->step(otherEnd); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1114 | if (*stepPtr != foundStep) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1115 | return set_last(last, endSpan); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1116 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1117 | SkASSERT(*startPtr); |
caryclark | 65b427c | 2014-09-18 10:32:57 -0700 | [diff] [blame] | 1118 | // SkASSERT(otherEnd >= 0); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1119 | SkOpSpan* origMin = step < 0 ? origStart->prev() : origStart->upCast(); |
| 1120 | SkOpSpan* foundMin = foundSpan->starter(otherEnd); |
| 1121 | if (foundMin->windValue() != origMin->windValue() |
| 1122 | || foundMin->oppValue() != origMin->oppValue()) { |
| 1123 | return set_last(last, endSpan); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1124 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1125 | *startPtr = foundSpan; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1126 | *stepPtr = foundStep; |
| 1127 | if (minPtr) { |
| 1128 | *minPtr = foundMin; |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 1129 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1130 | return other; |
| 1131 | } |
| 1132 | |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1133 | // Please keep this in sync with DebugClearVisited() |
| 1134 | void SkOpSegment::ClearVisited(SkOpSpanBase* span) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1135 | // reset visited flag back to false |
| 1136 | do { |
| 1137 | SkOpPtT* ptT = span->ptT(), * stopPtT = ptT; |
| 1138 | while ((ptT = ptT->next()) != stopPtT) { |
| 1139 | SkOpSegment* opp = ptT->segment(); |
| 1140 | opp->resetVisited(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1141 | } |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 1142 | } while (!span->final() && (span = span->upCast()->next())); |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 1143 | } |
| 1144 | |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1145 | // Please keep this in sync with debugMissingCoincidence() |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1146 | // look for pairs of undetected coincident curves |
| 1147 | // assumes that segments going in have visited flag clear |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1148 | // Even though pairs of curves correct detect coincident runs, a run may be missed |
| 1149 | // if the coincidence is a product of multiple intersections. For instance, given |
| 1150 | // curves A, B, and C: |
| 1151 | // A-B intersect at a point 1; A-C and B-C intersect at point 2, so near |
| 1152 | // the end of C that the intersection is replaced with the end of C. |
| 1153 | // Even though A-B correctly do not detect an intersection at point 2, |
| 1154 | // the resulting run from point 1 to point 2 is coincident on A and B. |
| 1155 | bool SkOpSegment::missingCoincidence() { |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 1156 | if (this->done()) { |
caryclark | 27c8eb8 | 2015-07-06 11:38:33 -0700 | [diff] [blame] | 1157 | return false; |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 1158 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1159 | SkOpSpan* prior = nullptr; |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 1160 | SkOpSpanBase* spanBase = &fHead; |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1161 | bool result = false; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1162 | do { |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 1163 | SkOpPtT* ptT = spanBase->ptT(), * spanStopPtT = ptT; |
caryclark | c6d855f | 2016-08-11 11:59:48 -0700 | [diff] [blame] | 1164 | SkOPASSERT(ptT->span() == spanBase); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1165 | while ((ptT = ptT->next()) != spanStopPtT) { |
caryclark | 182b499 | 2015-05-14 05:45:54 -0700 | [diff] [blame] | 1166 | if (ptT->deleted()) { |
| 1167 | continue; |
| 1168 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1169 | SkOpSegment* opp = ptT->span()->segment(); |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 1170 | if (opp->done()) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1171 | continue; |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 1172 | } |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 1173 | // when opp is encounted the 1st time, continue; on 2nd encounter, look for coincidence |
| 1174 | if (!opp->visited()) { |
| 1175 | continue; |
| 1176 | } |
| 1177 | if (spanBase == &fHead) { |
| 1178 | continue; |
| 1179 | } |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1180 | if (ptT->segment() == this) { |
| 1181 | continue; |
| 1182 | } |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 1183 | SkOpSpan* span = spanBase->upCastable(); |
| 1184 | // FIXME?: this assumes that if the opposite segment is coincident then no more |
| 1185 | // coincidence needs to be detected. This may not be true. |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1186 | if (span && span->containsCoincidence(opp)) { |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 1187 | continue; |
| 1188 | } |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 1189 | if (spanBase->containsCoinEnd(opp)) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1190 | continue; |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1191 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1192 | SkOpPtT* priorPtT = nullptr, * priorStopPtT; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1193 | // find prior span containing opp segment |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1194 | SkOpSegment* priorOpp = nullptr; |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 1195 | SkOpSpan* priorTest = spanBase->prev(); |
| 1196 | while (!priorOpp && priorTest) { |
| 1197 | priorStopPtT = priorPtT = priorTest->ptT(); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1198 | while ((priorPtT = priorPtT->next()) != priorStopPtT) { |
caryclark | 182b499 | 2015-05-14 05:45:54 -0700 | [diff] [blame] | 1199 | if (priorPtT->deleted()) { |
| 1200 | continue; |
| 1201 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1202 | SkOpSegment* segment = priorPtT->span()->segment(); |
| 1203 | if (segment == opp) { |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 1204 | prior = priorTest; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1205 | priorOpp = opp; |
| 1206 | break; |
| 1207 | } |
| 1208 | } |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 1209 | priorTest = priorTest->prev(); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1210 | } |
| 1211 | if (!priorOpp) { |
| 1212 | continue; |
| 1213 | } |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 1214 | if (priorPtT == ptT) { |
| 1215 | continue; |
| 1216 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1217 | SkOpPtT* oppStart = prior->ptT(); |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 1218 | SkOpPtT* oppEnd = spanBase->ptT(); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1219 | bool swapped = priorPtT->fT > ptT->fT; |
| 1220 | if (swapped) { |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 1221 | using std::swap; |
| 1222 | swap(priorPtT, ptT); |
| 1223 | swap(oppStart, oppEnd); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1224 | } |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1225 | SkOpCoincidence* coincidences = this->globalState()->coincidence(); |
| 1226 | SkOpPtT* rootPriorPtT = priorPtT->span()->ptT(); |
| 1227 | SkOpPtT* rootPtT = ptT->span()->ptT(); |
| 1228 | SkOpPtT* rootOppStart = oppStart->span()->ptT(); |
| 1229 | SkOpPtT* rootOppEnd = oppEnd->span()->ptT(); |
| 1230 | if (coincidences->contains(rootPriorPtT, rootPtT, rootOppStart, rootOppEnd)) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1231 | goto swapBack; |
| 1232 | } |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1233 | if (this->testForCoincidence(rootPriorPtT, rootPtT, prior, spanBase, opp)) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1234 | // mark coincidence |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1235 | #if DEBUG_COINCIDENCE_VERBOSE |
| 1236 | SkDebugf("%s coinSpan=%d endSpan=%d oppSpan=%d oppEndSpan=%d\n", __FUNCTION__, |
| 1237 | rootPriorPtT->debugID(), rootPtT->debugID(), rootOppStart->debugID(), |
| 1238 | rootOppEnd->debugID()); |
| 1239 | #endif |
| 1240 | if (!coincidences->extend(rootPriorPtT, rootPtT, rootOppStart, rootOppEnd)) { |
| 1241 | coincidences->add(rootPriorPtT, rootPtT, rootOppStart, rootOppEnd); |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 1242 | } |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1243 | #if DEBUG_COINCIDENCE |
| 1244 | SkASSERT(coincidences->contains(rootPriorPtT, rootPtT, rootOppStart, rootOppEnd)); |
| 1245 | #endif |
| 1246 | result = true; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1247 | } |
| 1248 | swapBack: |
| 1249 | if (swapped) { |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 1250 | using std::swap; |
| 1251 | swap(priorPtT, ptT); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1252 | } |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 1253 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1254 | } while ((spanBase = spanBase->final() ? nullptr : spanBase->upCast()->next())); |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1255 | ClearVisited(&fHead); |
| 1256 | return result; |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 1257 | } |
| 1258 | |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1259 | // please keep this in sync with debugMoveMultiples() |
caryclark | 08bc848 | 2015-04-24 09:08:57 -0700 | [diff] [blame] | 1260 | // if a span has more than one intersection, merge the other segments' span as needed |
caryclark | d78c088 | 2016-02-24 09:03:07 -0800 | [diff] [blame] | 1261 | bool SkOpSegment::moveMultiples() { |
caryclark | 08bc848 | 2015-04-24 09:08:57 -0700 | [diff] [blame] | 1262 | debugValidate(); |
| 1263 | SkOpSpanBase* test = &fHead; |
| 1264 | do { |
| 1265 | int addCount = test->spanAddsCount(); |
Cary Clark | 7eb01e0 | 2016-12-08 14:36:32 -0500 | [diff] [blame] | 1266 | // FAIL_IF(addCount < 1); |
| 1267 | if (addCount <= 1) { |
caryclark | 08bc848 | 2015-04-24 09:08:57 -0700 | [diff] [blame] | 1268 | continue; |
| 1269 | } |
| 1270 | SkOpPtT* startPtT = test->ptT(); |
| 1271 | SkOpPtT* testPtT = startPtT; |
| 1272 | do { // iterate through all spans associated with start |
| 1273 | SkOpSpanBase* oppSpan = testPtT->span(); |
| 1274 | if (oppSpan->spanAddsCount() == addCount) { |
| 1275 | continue; |
| 1276 | } |
| 1277 | if (oppSpan->deleted()) { |
| 1278 | continue; |
| 1279 | } |
| 1280 | SkOpSegment* oppSegment = oppSpan->segment(); |
| 1281 | if (oppSegment == this) { |
| 1282 | continue; |
| 1283 | } |
| 1284 | // find range of spans to consider merging |
| 1285 | SkOpSpanBase* oppPrev = oppSpan; |
| 1286 | SkOpSpanBase* oppFirst = oppSpan; |
| 1287 | while ((oppPrev = oppPrev->prev())) { |
| 1288 | if (!roughly_equal(oppPrev->t(), oppSpan->t())) { |
| 1289 | break; |
| 1290 | } |
| 1291 | if (oppPrev->spanAddsCount() == addCount) { |
| 1292 | continue; |
| 1293 | } |
| 1294 | if (oppPrev->deleted()) { |
| 1295 | continue; |
| 1296 | } |
| 1297 | oppFirst = oppPrev; |
| 1298 | } |
| 1299 | SkOpSpanBase* oppNext = oppSpan; |
| 1300 | SkOpSpanBase* oppLast = oppSpan; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1301 | while ((oppNext = oppNext->final() ? nullptr : oppNext->upCast()->next())) { |
caryclark | 08bc848 | 2015-04-24 09:08:57 -0700 | [diff] [blame] | 1302 | if (!roughly_equal(oppNext->t(), oppSpan->t())) { |
| 1303 | break; |
| 1304 | } |
| 1305 | if (oppNext->spanAddsCount() == addCount) { |
| 1306 | continue; |
| 1307 | } |
| 1308 | if (oppNext->deleted()) { |
| 1309 | continue; |
| 1310 | } |
| 1311 | oppLast = oppNext; |
| 1312 | } |
| 1313 | if (oppFirst == oppLast) { |
| 1314 | continue; |
| 1315 | } |
| 1316 | SkOpSpanBase* oppTest = oppFirst; |
| 1317 | do { |
| 1318 | if (oppTest == oppSpan) { |
| 1319 | continue; |
| 1320 | } |
| 1321 | // check to see if the candidate meets specific criteria: |
| 1322 | // it contains spans of segments in test's loop but not including 'this' |
| 1323 | SkOpPtT* oppStartPtT = oppTest->ptT(); |
| 1324 | SkOpPtT* oppPtT = oppStartPtT; |
| 1325 | while ((oppPtT = oppPtT->next()) != oppStartPtT) { |
| 1326 | SkOpSegment* oppPtTSegment = oppPtT->segment(); |
| 1327 | if (oppPtTSegment == this) { |
| 1328 | goto tryNextSpan; |
| 1329 | } |
| 1330 | SkOpPtT* matchPtT = startPtT; |
| 1331 | do { |
| 1332 | if (matchPtT->segment() == oppPtTSegment) { |
| 1333 | goto foundMatch; |
| 1334 | } |
| 1335 | } while ((matchPtT = matchPtT->next()) != startPtT); |
| 1336 | goto tryNextSpan; |
| 1337 | foundMatch: // merge oppTest and oppSpan |
| 1338 | oppSegment->debugValidate(); |
caryclark | 30b9fdd | 2016-08-31 14:36:29 -0700 | [diff] [blame] | 1339 | oppTest->mergeMatches(oppSpan); |
| 1340 | oppTest->addOpp(oppSpan); |
caryclark | 08bc848 | 2015-04-24 09:08:57 -0700 | [diff] [blame] | 1341 | oppSegment->debugValidate(); |
| 1342 | goto checkNextSpan; |
| 1343 | } |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1344 | tryNextSpan: |
caryclark | 08bc848 | 2015-04-24 09:08:57 -0700 | [diff] [blame] | 1345 | ; |
| 1346 | } while (oppTest != oppLast && (oppTest = oppTest->upCast()->next())); |
| 1347 | } while ((testPtT = testPtT->next()) != startPtT); |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1348 | checkNextSpan: |
caryclark | 08bc848 | 2015-04-24 09:08:57 -0700 | [diff] [blame] | 1349 | ; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1350 | } while ((test = test->final() ? nullptr : test->upCast()->next())); |
caryclark | 08bc848 | 2015-04-24 09:08:57 -0700 | [diff] [blame] | 1351 | debugValidate(); |
caryclark | d78c088 | 2016-02-24 09:03:07 -0800 | [diff] [blame] | 1352 | return true; |
caryclark | 08bc848 | 2015-04-24 09:08:57 -0700 | [diff] [blame] | 1353 | } |
| 1354 | |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1355 | // adjacent spans may have points close by |
Cary Clark | 28da283 | 2017-03-21 10:30:50 -0400 | [diff] [blame] | 1356 | bool SkOpSegment::spansNearby(const SkOpSpanBase* refSpan, const SkOpSpanBase* checkSpan, |
| 1357 | bool* found) const { |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1358 | const SkOpPtT* refHead = refSpan->ptT(); |
| 1359 | const SkOpPtT* checkHead = checkSpan->ptT(); |
| 1360 | // if the first pt pair from adjacent spans are far apart, assume that all are far enough apart |
caryclark | 30b9fdd | 2016-08-31 14:36:29 -0700 | [diff] [blame] | 1361 | if (!SkDPoint::WayRoughlyEqual(refHead->fPt, checkHead->fPt)) { |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1362 | #if DEBUG_COINCIDENCE |
| 1363 | // verify that no combination of points are close |
| 1364 | const SkOpPtT* dBugRef = refHead; |
| 1365 | do { |
| 1366 | const SkOpPtT* dBugCheck = checkHead; |
| 1367 | do { |
caryclark | 30b9fdd | 2016-08-31 14:36:29 -0700 | [diff] [blame] | 1368 | SkOPASSERT(!SkDPoint::ApproximatelyEqual(dBugRef->fPt, dBugCheck->fPt)); |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1369 | dBugCheck = dBugCheck->next(); |
| 1370 | } while (dBugCheck != checkHead); |
| 1371 | dBugRef = dBugRef->next(); |
| 1372 | } while (dBugRef != refHead); |
| 1373 | #endif |
Cary Clark | 28da283 | 2017-03-21 10:30:50 -0400 | [diff] [blame] | 1374 | *found = false; |
| 1375 | return true; |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1376 | } |
| 1377 | // check only unique points |
| 1378 | SkScalar distSqBest = SK_ScalarMax; |
| 1379 | const SkOpPtT* refBest = nullptr; |
| 1380 | const SkOpPtT* checkBest = nullptr; |
| 1381 | const SkOpPtT* ref = refHead; |
| 1382 | do { |
| 1383 | if (ref->deleted()) { |
| 1384 | continue; |
| 1385 | } |
| 1386 | while (ref->ptAlreadySeen(refHead)) { |
| 1387 | ref = ref->next(); |
| 1388 | if (ref == refHead) { |
| 1389 | goto doneCheckingDistance; |
| 1390 | } |
| 1391 | } |
| 1392 | const SkOpPtT* check = checkHead; |
| 1393 | const SkOpSegment* refSeg = ref->segment(); |
Cary Clark | 28da283 | 2017-03-21 10:30:50 -0400 | [diff] [blame] | 1394 | int escapeHatch = 100000; // defend against infinite loops |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1395 | do { |
| 1396 | if (check->deleted()) { |
| 1397 | continue; |
| 1398 | } |
| 1399 | while (check->ptAlreadySeen(checkHead)) { |
| 1400 | check = check->next(); |
| 1401 | if (check == checkHead) { |
| 1402 | goto nextRef; |
| 1403 | } |
| 1404 | } |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 1405 | SkScalar distSq = SkPointPriv::DistanceToSqd(ref->fPt, check->fPt); |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1406 | if (distSqBest > distSq && (refSeg != check->segment() |
| 1407 | || !refSeg->ptsDisjoint(*ref, *check))) { |
| 1408 | distSqBest = distSq; |
| 1409 | refBest = ref; |
| 1410 | checkBest = check; |
| 1411 | } |
Cary Clark | 28da283 | 2017-03-21 10:30:50 -0400 | [diff] [blame] | 1412 | if (--escapeHatch <= 0) { |
| 1413 | return false; |
| 1414 | } |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1415 | } while ((check = check->next()) != checkHead); |
Cary Clark | 28da283 | 2017-03-21 10:30:50 -0400 | [diff] [blame] | 1416 | nextRef: |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1417 | ; |
| 1418 | } while ((ref = ref->next()) != refHead); |
| 1419 | doneCheckingDistance: |
Cary Clark | 28da283 | 2017-03-21 10:30:50 -0400 | [diff] [blame] | 1420 | *found = checkBest && refBest->segment()->match(refBest, checkBest->segment(), checkBest->fT, |
caryclark | ef4f32a | 2016-08-24 09:24:18 -0700 | [diff] [blame] | 1421 | checkBest->fPt); |
Cary Clark | 28da283 | 2017-03-21 10:30:50 -0400 | [diff] [blame] | 1422 | return true; |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | // Please keep this function in sync with debugMoveNearby() |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1426 | // Move nearby t values and pts so they all hang off the same span. Alignment happens later. |
Cary Clark | 28da283 | 2017-03-21 10:30:50 -0400 | [diff] [blame] | 1427 | bool SkOpSegment::moveNearby() { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1428 | debugValidate(); |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1429 | // release undeleted spans pointing to this seg that are linked to the primary span |
| 1430 | SkOpSpanBase* spanBase = &fHead; |
Cary Clark | 43938b8 | 2017-10-18 08:47:32 -0400 | [diff] [blame] | 1431 | int escapeHatch = 9999; // the largest count for a regular test is 50; for a fuzzer, 500 |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1432 | do { |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1433 | SkOpPtT* ptT = spanBase->ptT(); |
| 1434 | const SkOpPtT* headPtT = ptT; |
| 1435 | while ((ptT = ptT->next()) != headPtT) { |
Cary Clark | 43938b8 | 2017-10-18 08:47:32 -0400 | [diff] [blame] | 1436 | if (!--escapeHatch) { |
| 1437 | return false; |
| 1438 | } |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1439 | SkOpSpanBase* test = ptT->span(); |
| 1440 | if (ptT->segment() == this && !ptT->deleted() && test != spanBase |
| 1441 | && test->ptT() == ptT) { |
| 1442 | if (test->final()) { |
| 1443 | if (spanBase == &fHead) { |
| 1444 | this->clearAll(); |
Cary Clark | 28da283 | 2017-03-21 10:30:50 -0400 | [diff] [blame] | 1445 | return true; |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1446 | } |
| 1447 | spanBase->upCast()->release(ptT); |
| 1448 | } else if (test->prev()) { |
| 1449 | test->upCast()->release(headPtT); |
| 1450 | } |
| 1451 | break; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1452 | } |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 1453 | } |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1454 | spanBase = spanBase->upCast()->next(); |
| 1455 | } while (!spanBase->final()); |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1456 | // This loop looks for adjacent spans which are near by |
| 1457 | spanBase = &fHead; |
| 1458 | do { // iterate through all spans associated with start |
| 1459 | SkOpSpanBase* test = spanBase->upCast()->next(); |
Cary Clark | 28da283 | 2017-03-21 10:30:50 -0400 | [diff] [blame] | 1460 | bool found; |
| 1461 | if (!this->spansNearby(spanBase, test, &found)) { |
| 1462 | return false; |
| 1463 | } |
| 1464 | if (found) { |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1465 | if (test->final()) { |
| 1466 | if (spanBase->prev()) { |
| 1467 | test->merge(spanBase->upCast()); |
| 1468 | } else { |
| 1469 | this->clearAll(); |
Cary Clark | 28da283 | 2017-03-21 10:30:50 -0400 | [diff] [blame] | 1470 | return true; |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1471 | } |
| 1472 | } else { |
| 1473 | spanBase->merge(test->upCast()); |
| 1474 | } |
| 1475 | } |
| 1476 | spanBase = test; |
| 1477 | } while (!spanBase->final()); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1478 | debugValidate(); |
Cary Clark | 28da283 | 2017-03-21 10:30:50 -0400 | [diff] [blame] | 1479 | return true; |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 1480 | } |
| 1481 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1482 | bool SkOpSegment::operand() const { |
| 1483 | return fContour->operand(); |
| 1484 | } |
| 1485 | |
| 1486 | bool SkOpSegment::oppXor() const { |
| 1487 | return fContour->oppXor(); |
| 1488 | } |
| 1489 | |
| 1490 | bool SkOpSegment::ptsDisjoint(double t1, const SkPoint& pt1, double t2, const SkPoint& pt2) const { |
| 1491 | if (fVerb == SkPath::kLine_Verb) { |
| 1492 | return false; |
| 1493 | } |
| 1494 | // quads (and cubics) can loop back to nearly a line so that an opposite curve |
| 1495 | // hits in two places with very different t values. |
| 1496 | // OPTIMIZATION: curves could be preflighted so that, for example, something like |
| 1497 | // 'controls contained by ends' could avoid this check for common curves |
| 1498 | // 'ends are extremes in x or y' is cheaper to compute and real-world common |
| 1499 | // on the other hand, the below check is relatively inexpensive |
| 1500 | double midT = (t1 + t2) / 2; |
| 1501 | SkPoint midPt = this->ptAtT(midT); |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 1502 | double seDistSq = SkTMax(SkPointPriv::DistanceToSqd(pt1, pt2) * 2, FLT_EPSILON * 2); |
| 1503 | return SkPointPriv::DistanceToSqd(midPt, pt1) > seDistSq || |
| 1504 | SkPointPriv::DistanceToSqd(midPt, pt2) > seDistSq; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | void SkOpSegment::setUpWindings(SkOpSpanBase* start, SkOpSpanBase* end, int* sumMiWinding, |
| 1508 | int* maxWinding, int* sumWinding) { |
| 1509 | int deltaSum = SpanSign(start, end); |
| 1510 | *maxWinding = *sumMiWinding; |
| 1511 | *sumWinding = *sumMiWinding -= deltaSum; |
bungeman | 60e0fee | 2015-08-26 05:15:46 -0700 | [diff] [blame] | 1512 | SkASSERT(!DEBUG_LIMIT_WIND_SUM || SkTAbs(*sumWinding) <= DEBUG_LIMIT_WIND_SUM); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | void SkOpSegment::setUpWindings(SkOpSpanBase* start, SkOpSpanBase* end, int* sumMiWinding, |
| 1516 | int* sumSuWinding, int* maxWinding, int* sumWinding, int* oppMaxWinding, |
| 1517 | int* oppSumWinding) { |
| 1518 | int deltaSum = SpanSign(start, end); |
| 1519 | int oppDeltaSum = OppSign(start, end); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1520 | if (operand()) { |
| 1521 | *maxWinding = *sumSuWinding; |
| 1522 | *sumWinding = *sumSuWinding -= deltaSum; |
| 1523 | *oppMaxWinding = *sumMiWinding; |
| 1524 | *oppSumWinding = *sumMiWinding -= oppDeltaSum; |
| 1525 | } else { |
| 1526 | *maxWinding = *sumMiWinding; |
| 1527 | *sumWinding = *sumMiWinding -= deltaSum; |
| 1528 | *oppMaxWinding = *sumSuWinding; |
| 1529 | *oppSumWinding = *sumSuWinding -= oppDeltaSum; |
| 1530 | } |
bungeman | 60e0fee | 2015-08-26 05:15:46 -0700 | [diff] [blame] | 1531 | SkASSERT(!DEBUG_LIMIT_WIND_SUM || SkTAbs(*sumWinding) <= DEBUG_LIMIT_WIND_SUM); |
| 1532 | SkASSERT(!DEBUG_LIMIT_WIND_SUM || SkTAbs(*oppSumWinding) <= DEBUG_LIMIT_WIND_SUM); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1533 | } |
| 1534 | |
caryclark | b36a3cd | 2016-10-18 07:59:44 -0700 | [diff] [blame] | 1535 | bool SkOpSegment::sortAngles() { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1536 | SkOpSpanBase* span = &this->fHead; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1537 | do { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1538 | SkOpAngle* fromAngle = span->fromAngle(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1539 | SkOpAngle* toAngle = span->final() ? nullptr : span->upCast()->toAngle(); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1540 | if (!fromAngle && !toAngle) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1541 | continue; |
| 1542 | } |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 1543 | #if DEBUG_ANGLE |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1544 | bool wroteAfterHeader = false; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 1545 | #endif |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1546 | SkOpAngle* baseAngle = fromAngle; |
| 1547 | if (fromAngle && toAngle) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1548 | #if DEBUG_ANGLE |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1549 | SkDebugf("%s [%d] tStart=%1.9g [%d]\n", __FUNCTION__, debugID(), span->t(), |
| 1550 | span->debugID()); |
| 1551 | wroteAfterHeader = true; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1552 | #endif |
caryclark | b36a3cd | 2016-10-18 07:59:44 -0700 | [diff] [blame] | 1553 | FAIL_IF(!fromAngle->insert(toAngle)); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1554 | } else if (!fromAngle) { |
| 1555 | baseAngle = toAngle; |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 1556 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1557 | SkOpPtT* ptT = span->ptT(), * stopPtT = ptT; |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 1558 | do { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1559 | SkOpSpanBase* oSpan = ptT->span(); |
| 1560 | if (oSpan == span) { |
| 1561 | continue; |
| 1562 | } |
| 1563 | SkOpAngle* oAngle = oSpan->fromAngle(); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1564 | if (oAngle) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1565 | #if DEBUG_ANGLE |
| 1566 | if (!wroteAfterHeader) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1567 | SkDebugf("%s [%d] tStart=%1.9g [%d]\n", __FUNCTION__, debugID(), |
| 1568 | span->t(), span->debugID()); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1569 | wroteAfterHeader = true; |
| 1570 | } |
| 1571 | #endif |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1572 | if (!oAngle->loopContains(baseAngle)) { |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 1573 | baseAngle->insert(oAngle); |
| 1574 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1575 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1576 | if (!oSpan->final()) { |
| 1577 | oAngle = oSpan->upCast()->toAngle(); |
| 1578 | if (oAngle) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1579 | #if DEBUG_ANGLE |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1580 | if (!wroteAfterHeader) { |
| 1581 | SkDebugf("%s [%d] tStart=%1.9g [%d]\n", __FUNCTION__, debugID(), |
| 1582 | span->t(), span->debugID()); |
| 1583 | wroteAfterHeader = true; |
| 1584 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1585 | #endif |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1586 | if (!oAngle->loopContains(baseAngle)) { |
| 1587 | baseAngle->insert(oAngle); |
| 1588 | } |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 1589 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1590 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1591 | } while ((ptT = ptT->next()) != stopPtT); |
| 1592 | if (baseAngle->loopCount() == 1) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1593 | span->setFromAngle(nullptr); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1594 | if (toAngle) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1595 | span->upCast()->setToAngle(nullptr); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1596 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1597 | baseAngle = nullptr; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1598 | } |
| 1599 | #if DEBUG_SORT |
| 1600 | SkASSERT(!baseAngle || baseAngle->loopCount() > 1); |
| 1601 | #endif |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1602 | } while (!span->final() && (span = span->upCast()->next())); |
caryclark | b36a3cd | 2016-10-18 07:59:44 -0700 | [diff] [blame] | 1603 | return true; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1606 | bool SkOpSegment::subDivide(const SkOpSpanBase* start, const SkOpSpanBase* end, |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 1607 | SkDCurve* edge) const { |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 1608 | SkASSERT(start != end); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1609 | const SkOpPtT& startPtT = *start->ptT(); |
| 1610 | const SkOpPtT& endPtT = *end->ptT(); |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 1611 | SkDEBUGCODE(edge->fVerb = fVerb); |
| 1612 | edge->fCubic[0].set(startPtT.fPt); |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 1613 | int points = SkPathOpsVerbToPoints(fVerb); |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 1614 | edge->fCubic[points].set(endPtT.fPt); |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 1615 | if (fVerb == SkPath::kLine_Verb) { |
| 1616 | return false; |
| 1617 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1618 | double startT = startPtT.fT; |
| 1619 | double endT = endPtT.fT; |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 1620 | if ((startT == 0 || endT == 0) && (startT == 1 || endT == 1)) { |
| 1621 | // don't compute midpoints if we already have them |
| 1622 | if (fVerb == SkPath::kQuad_Verb) { |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 1623 | edge->fLine[1].set(fPts[1]); |
| 1624 | return false; |
| 1625 | } |
| 1626 | if (fVerb == SkPath::kConic_Verb) { |
| 1627 | edge->fConic[1].set(fPts[1]); |
| 1628 | edge->fConic.fWeight = fWeight; |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 1629 | return false; |
| 1630 | } |
| 1631 | SkASSERT(fVerb == SkPath::kCubic_Verb); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1632 | if (startT == 0) { |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 1633 | edge->fCubic[1].set(fPts[1]); |
| 1634 | edge->fCubic[2].set(fPts[2]); |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 1635 | return false; |
| 1636 | } |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 1637 | edge->fCubic[1].set(fPts[2]); |
| 1638 | edge->fCubic[2].set(fPts[1]); |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 1639 | return false; |
| 1640 | } |
| 1641 | if (fVerb == SkPath::kQuad_Verb) { |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 1642 | edge->fQuad[1] = SkDQuad::SubDivide(fPts, edge->fQuad[0], edge->fQuad[2], startT, endT); |
| 1643 | } else if (fVerb == SkPath::kConic_Verb) { |
| 1644 | edge->fConic[1] = SkDConic::SubDivide(fPts, fWeight, edge->fQuad[0], edge->fQuad[2], |
| 1645 | startT, endT, &edge->fConic.fWeight); |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 1646 | } else { |
| 1647 | SkASSERT(fVerb == SkPath::kCubic_Verb); |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 1648 | SkDCubic::SubDivide(fPts, edge->fCubic[0], edge->fCubic[3], startT, endT, &edge->fCubic[1]); |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 1649 | } |
| 1650 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
caryclark | 27c8eb8 | 2015-07-06 11:38:33 -0700 | [diff] [blame] | 1653 | bool SkOpSegment::testForCoincidence(const SkOpPtT* priorPtT, const SkOpPtT* ptT, |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1654 | const SkOpSpanBase* prior, const SkOpSpanBase* spanBase, const SkOpSegment* opp) const { |
caryclark | 27c8eb8 | 2015-07-06 11:38:33 -0700 | [diff] [blame] | 1655 | // average t, find mid pt |
| 1656 | double midT = (prior->t() + spanBase->t()) / 2; |
| 1657 | SkPoint midPt = this->ptAtT(midT); |
| 1658 | bool coincident = true; |
| 1659 | // if the mid pt is not near either end pt, project perpendicular through opp seg |
| 1660 | if (!SkDPoint::ApproximatelyEqual(priorPtT->fPt, midPt) |
| 1661 | && !SkDPoint::ApproximatelyEqual(ptT->fPt, midPt)) { |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1662 | if (priorPtT->span() == ptT->span()) { |
| 1663 | return false; |
| 1664 | } |
caryclark | 27c8eb8 | 2015-07-06 11:38:33 -0700 | [diff] [blame] | 1665 | coincident = false; |
| 1666 | SkIntersections i; |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1667 | SkDCurve curvePart; |
| 1668 | this->subDivide(prior, spanBase, &curvePart); |
| 1669 | SkDVector dxdy = (*CurveDDSlopeAtT[fVerb])(curvePart, 0.5f); |
| 1670 | SkDPoint partMidPt = (*CurveDDPointAtT[fVerb])(curvePart, 0.5f); |
| 1671 | SkDLine ray = {{{midPt.fX, midPt.fY}, {partMidPt.fX + dxdy.fY, partMidPt.fY - dxdy.fX}}}; |
| 1672 | SkDCurve oppPart; |
| 1673 | opp->subDivide(priorPtT->span(), ptT->span(), &oppPart); |
| 1674 | (*CurveDIntersectRay[opp->verb()])(oppPart, ray, &i); |
caryclark | 27c8eb8 | 2015-07-06 11:38:33 -0700 | [diff] [blame] | 1675 | // measure distance and see if it's small enough to denote coincidence |
| 1676 | for (int index = 0; index < i.used(); ++index) { |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1677 | if (!between(0, i[0][index], 1)) { |
| 1678 | continue; |
| 1679 | } |
caryclark | 27c8eb8 | 2015-07-06 11:38:33 -0700 | [diff] [blame] | 1680 | SkDPoint oppPt = i.pt(index); |
caryclark | 30b9fdd | 2016-08-31 14:36:29 -0700 | [diff] [blame] | 1681 | if (oppPt.approximatelyDEqual(midPt)) { |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 1682 | // the coincidence can occur at almost any angle |
| 1683 | coincident = true; |
caryclark | 27c8eb8 | 2015-07-06 11:38:33 -0700 | [diff] [blame] | 1684 | } |
| 1685 | } |
| 1686 | } |
| 1687 | return coincident; |
| 1688 | } |
| 1689 | |
Cary Clark | ab2d73b | 2016-12-16 17:17:25 -0500 | [diff] [blame] | 1690 | SkOpSpan* SkOpSegment::undoneSpan() { |
| 1691 | SkOpSpan* span = &fHead; |
| 1692 | SkOpSpanBase* next; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1693 | do { |
Cary Clark | ab2d73b | 2016-12-16 17:17:25 -0500 | [diff] [blame] | 1694 | next = span->next(); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1695 | if (!span->done()) { |
Cary Clark | ab2d73b | 2016-12-16 17:17:25 -0500 | [diff] [blame] | 1696 | return span; |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 1697 | } |
Cary Clark | ab2d73b | 2016-12-16 17:17:25 -0500 | [diff] [blame] | 1698 | } while (!next->final() && (span = next->upCast())); |
| 1699 | return nullptr; |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 1700 | } |
| 1701 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1702 | int SkOpSegment::updateOppWinding(const SkOpSpanBase* start, const SkOpSpanBase* end) const { |
| 1703 | const SkOpSpan* lesser = start->starter(end); |
| 1704 | int oppWinding = lesser->oppSum(); |
| 1705 | int oppSpanWinding = SkOpSegment::OppSign(start, end); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1706 | if (oppSpanWinding && UseInnerWinding(oppWinding - oppSpanWinding, oppWinding) |
| 1707 | && oppWinding != SK_MaxS32) { |
| 1708 | oppWinding -= oppSpanWinding; |
| 1709 | } |
| 1710 | return oppWinding; |
| 1711 | } |
| 1712 | |
| 1713 | int SkOpSegment::updateOppWinding(const SkOpAngle* angle) const { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1714 | const SkOpSpanBase* startSpan = angle->start(); |
| 1715 | const SkOpSpanBase* endSpan = angle->end(); |
| 1716 | return updateOppWinding(endSpan, startSpan); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | int SkOpSegment::updateOppWindingReverse(const SkOpAngle* angle) const { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1720 | const SkOpSpanBase* startSpan = angle->start(); |
| 1721 | const SkOpSpanBase* endSpan = angle->end(); |
| 1722 | return updateOppWinding(startSpan, endSpan); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 1725 | int SkOpSegment::updateWinding(SkOpSpanBase* start, SkOpSpanBase* end) { |
| 1726 | SkOpSpan* lesser = start->starter(end); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1727 | int winding = lesser->windSum(); |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 1728 | if (winding == SK_MinS32) { |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 1729 | winding = lesser->computeWindSum(); |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 1730 | } |
| 1731 | if (winding == SK_MinS32) { |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 1732 | return winding; |
| 1733 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1734 | int spanWinding = SkOpSegment::SpanSign(start, end); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 1735 | if (winding && UseInnerWinding(winding - spanWinding, winding) |
| 1736 | && winding != SK_MaxS32) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1737 | winding -= spanWinding; |
| 1738 | } |
| 1739 | return winding; |
| 1740 | } |
| 1741 | |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 1742 | int SkOpSegment::updateWinding(SkOpAngle* angle) { |
| 1743 | SkOpSpanBase* startSpan = angle->start(); |
| 1744 | SkOpSpanBase* endSpan = angle->end(); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1745 | return updateWinding(endSpan, startSpan); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 1746 | } |
| 1747 | |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 1748 | int SkOpSegment::updateWindingReverse(const SkOpAngle* angle) { |
| 1749 | SkOpSpanBase* startSpan = angle->start(); |
| 1750 | SkOpSpanBase* endSpan = angle->end(); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1751 | return updateWinding(startSpan, endSpan); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 1752 | } |
| 1753 | |
| 1754 | // OPTIMIZATION: does the following also work, and is it any faster? |
| 1755 | // return outerWinding * innerWinding > 0 |
| 1756 | // || ((outerWinding + innerWinding < 0) ^ ((outerWinding - innerWinding) < 0))) |
| 1757 | bool SkOpSegment::UseInnerWinding(int outerWinding, int innerWinding) { |
| 1758 | SkASSERT(outerWinding != SK_MaxS32); |
| 1759 | SkASSERT(innerWinding != SK_MaxS32); |
bungeman | 60e0fee | 2015-08-26 05:15:46 -0700 | [diff] [blame] | 1760 | int absOut = SkTAbs(outerWinding); |
| 1761 | int absIn = SkTAbs(innerWinding); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 1762 | bool result = absOut == absIn ? outerWinding < 0 : absOut < absIn; |
| 1763 | return result; |
| 1764 | } |
| 1765 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1766 | int SkOpSegment::windSum(const SkOpAngle* angle) const { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1767 | const SkOpSpan* minSpan = angle->start()->starter(angle->end()); |
| 1768 | return minSpan->windSum(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1769 | } |