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