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 | ccec0f9 | 2015-03-24 07:28:17 -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" |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 11 | |
| 12 | /* |
| 13 | After computing raw intersections, post process all segments to: |
| 14 | - find small collections of points that can be collapsed to a single point |
| 15 | - find missing intersections to resolve differences caused by different algorithms |
| 16 | |
| 17 | Consider segments containing tiny or small intervals. Consider coincident segments |
| 18 | because coincidence finds intersections through distance measurement that non-coincident |
| 19 | intersection tests cannot. |
| 20 | */ |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 21 | |
| 22 | #define F (false) // discard the edge |
| 23 | #define T (true) // keep the edge |
| 24 | |
| 25 | static const bool gUnaryActiveEdge[2][2] = { |
| 26 | // from=0 from=1 |
| 27 | // to=0,1 to=0,1 |
| 28 | {F, T}, {T, F}, |
| 29 | }; |
| 30 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 31 | static const bool gActiveEdge[kXOR_PathOp + 1][2][2][2][2] = { |
| 32 | // miFrom=0 miFrom=1 |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 33 | // miTo=0 miTo=1 miTo=0 miTo=1 |
| 34 | // 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] | 35 | // suTo=0,1 suTo=0,1 suTo=0,1 suTo=0,1 suTo=0,1 suTo=0,1 suTo=0,1 suTo=0,1 |
| 36 | {{{{F, F}, {F, F}}, {{T, F}, {T, F}}}, {{{T, T}, {F, F}}, {{F, T}, {T, F}}}}, // mi - su |
| 37 | {{{{F, F}, {F, F}}, {{F, T}, {F, T}}}, {{{F, F}, {T, T}}, {{F, T}, {T, F}}}}, // mi & su |
| 38 | {{{{F, T}, {T, F}}, {{T, T}, {F, F}}}, {{{T, F}, {T, F}}, {{F, F}, {F, F}}}}, // mi | su |
| 39 | {{{{F, T}, {T, F}}, {{T, F}, {F, T}}}, {{{T, F}, {F, T}}, {{F, T}, {T, F}}}}, // mi ^ su |
| 40 | }; |
| 41 | |
| 42 | #undef F |
| 43 | #undef T |
| 44 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 45 | SkOpAngle* SkOpSegment::activeAngle(SkOpSpanBase* start, SkOpSpanBase** startPtr, |
| 46 | SkOpSpanBase** endPtr, bool* done, bool* sortable) { |
| 47 | if (SkOpAngle* result = activeAngleInner(start, startPtr, endPtr, done, sortable)) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 48 | return result; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 49 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 50 | if (SkOpAngle* result = activeAngleOther(start, startPtr, endPtr, done, sortable)) { |
| 51 | return result; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 52 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 53 | return NULL; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 54 | } |
| 55 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 56 | SkOpAngle* SkOpSegment::activeAngleInner(SkOpSpanBase* start, SkOpSpanBase** startPtr, |
| 57 | SkOpSpanBase** endPtr, bool* done, bool* sortable) { |
| 58 | SkOpSpan* upSpan = start->upCastable(); |
| 59 | if (upSpan) { |
| 60 | if (upSpan->windValue() || upSpan->oppValue()) { |
| 61 | SkOpSpanBase* next = upSpan->next(); |
| 62 | if (!*endPtr) { |
| 63 | *startPtr = start; |
| 64 | *endPtr = next; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 65 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 66 | if (!upSpan->done()) { |
| 67 | if (upSpan->windSum() != SK_MinS32) { |
| 68 | return spanToAngle(start, next); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 69 | } |
| 70 | *done = false; |
| 71 | } |
| 72 | } else { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 73 | SkASSERT(upSpan->done()); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 74 | } |
| 75 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 76 | SkOpSpan* downSpan = start->prev(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 77 | // edge leading into junction |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 78 | if (downSpan) { |
| 79 | if (downSpan->windValue() || downSpan->oppValue()) { |
| 80 | if (!*endPtr) { |
| 81 | *startPtr = start; |
| 82 | *endPtr = downSpan; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 83 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 84 | if (!downSpan->done()) { |
| 85 | if (downSpan->windSum() != SK_MinS32) { |
| 86 | return spanToAngle(start, downSpan); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 87 | } |
| 88 | *done = false; |
| 89 | } |
| 90 | } else { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 91 | SkASSERT(downSpan->done()); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 94 | return NULL; |
| 95 | } |
| 96 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 97 | SkOpAngle* SkOpSegment::activeAngleOther(SkOpSpanBase* start, SkOpSpanBase** startPtr, |
| 98 | SkOpSpanBase** endPtr, bool* done, bool* sortable) { |
| 99 | SkOpPtT* oPtT = start->ptT()->next(); |
| 100 | SkOpSegment* other = oPtT->segment(); |
| 101 | SkOpSpanBase* oSpan = oPtT->span(); |
| 102 | return other->activeAngleInner(oSpan, startPtr, endPtr, done, sortable); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 103 | } |
| 104 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 105 | SkPoint SkOpSegment::activeLeftTop(SkOpSpanBase** firstSpan) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 106 | SkASSERT(!done()); |
| 107 | SkPoint topPt = {SK_ScalarMax, SK_ScalarMax}; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 108 | // see if either end is not done since we want smaller Y of the pair |
| 109 | bool lastDone = true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 110 | double lastT = -1; |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 111 | SkOpSpanBase* span = &fHead; |
| 112 | do { |
| 113 | if (lastDone && (span->final() || span->upCast()->done())) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 114 | goto next; |
| 115 | } |
| 116 | { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 117 | const SkPoint& xy = span->pt(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 118 | if (topPt.fY > xy.fY || (topPt.fY == xy.fY && topPt.fX > xy.fX)) { |
| 119 | topPt = xy; |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 120 | if (firstSpan) { |
| 121 | *firstSpan = span; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | if (fVerb != SkPath::kLine_Verb && !lastDone) { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 125 | SkPoint curveTop = (*CurveTop[SkPathOpsVerbToPoints(fVerb)])(fPts, lastT, |
| 126 | span->t()); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 127 | if (topPt.fY > curveTop.fY || (topPt.fY == curveTop.fY |
| 128 | && topPt.fX > curveTop.fX)) { |
| 129 | topPt = curveTop; |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 130 | if (firstSpan) { |
| 131 | *firstSpan = span; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 135 | lastT = span->t(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 136 | } |
| 137 | next: |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 138 | if (span->final()) { |
| 139 | break; |
| 140 | } |
| 141 | lastDone = span->upCast()->done(); |
| 142 | } while ((span = span->upCast()->next())); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 143 | return topPt; |
| 144 | } |
| 145 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 146 | bool SkOpSegment::activeOp(SkOpSpanBase* start, SkOpSpanBase* end, int xorMiMask, int xorSuMask, |
| 147 | SkPathOp op) { |
| 148 | int sumMiWinding = this->updateWinding(end, start); |
| 149 | int sumSuWinding = this->updateOppWinding(end, start); |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 150 | #if DEBUG_LIMIT_WIND_SUM |
| 151 | SkASSERT(abs(sumMiWinding) <= DEBUG_LIMIT_WIND_SUM); |
| 152 | SkASSERT(abs(sumSuWinding) <= DEBUG_LIMIT_WIND_SUM); |
| 153 | #endif |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 154 | if (this->operand()) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 155 | SkTSwap<int>(sumMiWinding, sumSuWinding); |
| 156 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 157 | return this->activeOp(xorMiMask, xorSuMask, start, end, op, &sumMiWinding, &sumSuWinding); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 158 | } |
| 159 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 160 | bool SkOpSegment::activeOp(int xorMiMask, int xorSuMask, SkOpSpanBase* start, SkOpSpanBase* end, |
| 161 | SkPathOp op, int* sumMiWinding, int* sumSuWinding) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 162 | int maxWinding, sumWinding, oppMaxWinding, oppSumWinding; |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 163 | this->setUpWindings(start, end, sumMiWinding, sumSuWinding, |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 164 | &maxWinding, &sumWinding, &oppMaxWinding, &oppSumWinding); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 165 | bool miFrom; |
| 166 | bool miTo; |
| 167 | bool suFrom; |
| 168 | bool suTo; |
| 169 | if (operand()) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 170 | miFrom = (oppMaxWinding & xorMiMask) != 0; |
| 171 | miTo = (oppSumWinding & xorMiMask) != 0; |
| 172 | suFrom = (maxWinding & xorSuMask) != 0; |
| 173 | suTo = (sumWinding & xorSuMask) != 0; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 174 | } else { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 175 | miFrom = (maxWinding & xorMiMask) != 0; |
| 176 | miTo = (sumWinding & xorMiMask) != 0; |
| 177 | suFrom = (oppMaxWinding & xorSuMask) != 0; |
| 178 | suTo = (oppSumWinding & xorSuMask) != 0; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 179 | } |
| 180 | bool result = gActiveEdge[op][miFrom][miTo][suFrom][suTo]; |
| 181 | #if DEBUG_ACTIVE_OP |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 182 | SkDebugf("%s id=%d t=%1.9g tEnd=%1.9g op=%s miFrom=%d miTo=%d suFrom=%d suTo=%d result=%d\n", |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 183 | __FUNCTION__, debugID(), start->t(), end->t(), |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 184 | SkPathOpsDebug::kPathOpStr[op], miFrom, miTo, suFrom, suTo, result); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 185 | #endif |
| 186 | return result; |
| 187 | } |
| 188 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 189 | bool SkOpSegment::activeWinding(SkOpSpanBase* start, SkOpSpanBase* end) { |
| 190 | int sumWinding = updateWinding(end, start); |
| 191 | return activeWinding(start, end, &sumWinding); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 192 | } |
| 193 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 194 | bool SkOpSegment::activeWinding(SkOpSpanBase* start, SkOpSpanBase* end, int* sumWinding) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 195 | int maxWinding; |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 196 | setUpWinding(start, end, &maxWinding, sumWinding); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 197 | bool from = maxWinding != 0; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 198 | bool to = *sumWinding != 0; |
| 199 | bool result = gUnaryActiveEdge[from][to]; |
| 200 | return result; |
| 201 | } |
| 202 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 203 | void SkOpSegment::addCurveTo(const SkOpSpanBase* start, const SkOpSpanBase* end, |
| 204 | SkPathWriter* path, bool active) const { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 205 | SkPoint edge[4]; |
| 206 | const SkPoint* ePtr; |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 207 | if ((start == &fHead && end == &fTail) || (start == &fTail && end == &fHead)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 208 | ePtr = fPts; |
| 209 | } else { |
| 210 | // OPTIMIZE? if not active, skip remainder and return xyAtT(end) |
| 211 | subDivide(start, end, edge); |
| 212 | ePtr = edge; |
| 213 | } |
| 214 | if (active) { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 215 | bool reverse = ePtr == fPts && start != &fHead; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 216 | if (reverse) { |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 217 | path->deferredMoveLine(ePtr[SkPathOpsVerbToPoints(fVerb)]); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 218 | switch (fVerb) { |
| 219 | case SkPath::kLine_Verb: |
| 220 | path->deferredLine(ePtr[0]); |
| 221 | break; |
| 222 | case SkPath::kQuad_Verb: |
| 223 | path->quadTo(ePtr[1], ePtr[0]); |
| 224 | break; |
| 225 | case SkPath::kCubic_Verb: |
| 226 | path->cubicTo(ePtr[2], ePtr[1], ePtr[0]); |
| 227 | break; |
| 228 | default: |
| 229 | SkASSERT(0); |
| 230 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 231 | } else { |
| 232 | path->deferredMoveLine(ePtr[0]); |
| 233 | switch (fVerb) { |
| 234 | case SkPath::kLine_Verb: |
| 235 | path->deferredLine(ePtr[1]); |
| 236 | break; |
| 237 | case SkPath::kQuad_Verb: |
| 238 | path->quadTo(ePtr[1], ePtr[2]); |
| 239 | break; |
| 240 | case SkPath::kCubic_Verb: |
| 241 | path->cubicTo(ePtr[1], ePtr[2], ePtr[3]); |
| 242 | break; |
| 243 | default: |
| 244 | SkASSERT(0); |
| 245 | } |
| 246 | } |
| 247 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 248 | } |
| 249 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 250 | SkOpPtT* SkOpSegment::addMissing(double t, SkOpSegment* opp, SkChunkAlloc* allocator) { |
| 251 | SkOpSpanBase* existing = NULL; |
| 252 | SkOpSpanBase* test = &fHead; |
| 253 | double testT; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 254 | do { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 255 | if ((testT = test->ptT()->fT) >= t) { |
| 256 | if (testT == t) { |
| 257 | existing = test; |
| 258 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 259 | break; |
| 260 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 261 | } while ((test = test->upCast()->next())); |
| 262 | SkOpPtT* result; |
| 263 | if (existing && existing->contains(opp)) { |
| 264 | result = existing->ptT(); |
| 265 | } else { |
| 266 | result = this->addT(t, SkOpSegment::kNoAlias, allocator); |
| 267 | } |
| 268 | SkASSERT(result); |
| 269 | return result; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 270 | } |
| 271 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 272 | SkOpAngle* SkOpSegment::addSingletonAngleDown(SkOpSegment** otherPtr, SkOpAngle** anglePtr, |
| 273 | SkChunkAlloc* allocator) { |
| 274 | SkOpSpan* startSpan = fTail.prev(); |
| 275 | SkASSERT(startSpan); |
| 276 | SkOpAngle* angle = SkOpTAllocator<SkOpAngle>::Allocate(allocator); |
| 277 | *anglePtr = angle; |
| 278 | angle->set(&fTail, startSpan); |
| 279 | fTail.setFromAngle(angle); |
| 280 | SkOpSegment* other = NULL; // these initializations silence a release build warning |
| 281 | SkOpSpan* oStartSpan = NULL; |
| 282 | SkOpSpanBase* oEndSpan = NULL; |
| 283 | SkOpPtT* ptT = fTail.ptT(), * startPtT = ptT; |
| 284 | while ((ptT = ptT->next()) != startPtT) { |
| 285 | other = ptT->segment(); |
| 286 | oStartSpan = ptT->span()->upCastable(); |
| 287 | if (oStartSpan && oStartSpan->windValue()) { |
| 288 | oEndSpan = oStartSpan->next(); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 289 | break; |
| 290 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 291 | oEndSpan = ptT->span(); |
| 292 | oStartSpan = oEndSpan->prev(); |
| 293 | if (oStartSpan && oStartSpan->windValue()) { |
| 294 | break; |
| 295 | } |
| 296 | } |
| 297 | SkOpAngle* oAngle = SkOpTAllocator<SkOpAngle>::Allocate(allocator); |
| 298 | oAngle->set(oStartSpan, oEndSpan); |
| 299 | oStartSpan->setToAngle(oAngle); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 300 | *otherPtr = other; |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 301 | return oAngle; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 302 | } |
| 303 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 304 | SkOpAngle* SkOpSegment::addSingletonAngles(int step, SkChunkAlloc* allocator) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 305 | SkOpSegment* other; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 306 | SkOpAngle* angle, * otherAngle; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 307 | if (step > 0) { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 308 | otherAngle = addSingletonAngleUp(&other, &angle, allocator); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 309 | } else { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 310 | otherAngle = addSingletonAngleDown(&other, &angle, allocator); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 311 | } |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 312 | angle->insert(otherAngle); |
| 313 | return angle; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 314 | } |
| 315 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 316 | SkOpAngle* SkOpSegment::addSingletonAngleUp(SkOpSegment** otherPtr, SkOpAngle** anglePtr, |
| 317 | SkChunkAlloc* allocator) { |
| 318 | SkOpSpanBase* endSpan = fHead.next(); |
| 319 | SkASSERT(endSpan); |
| 320 | SkOpAngle* angle = SkOpTAllocator<SkOpAngle>::Allocate(allocator); |
| 321 | *anglePtr = angle; |
| 322 | angle->set(&fHead, endSpan); |
| 323 | fHead.setToAngle(angle); |
| 324 | SkOpSegment* other = NULL; // these initializations silence a release build warning |
| 325 | SkOpSpan* oStartSpan = NULL; |
| 326 | SkOpSpanBase* oEndSpan = NULL; |
| 327 | SkOpPtT* ptT = fHead.ptT(), * startPtT = ptT; |
| 328 | while ((ptT = ptT->next()) != startPtT) { |
| 329 | other = ptT->segment(); |
| 330 | oEndSpan = ptT->span(); |
| 331 | oStartSpan = oEndSpan->prev(); |
| 332 | if (oStartSpan && oStartSpan->windValue()) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 333 | break; |
| 334 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 335 | oStartSpan = oEndSpan->upCastable(); |
| 336 | if (oStartSpan && oStartSpan->windValue()) { |
| 337 | oEndSpan = oStartSpan->next(); |
caryclark | 19eb3b2 | 2014-07-18 05:08:14 -0700 | [diff] [blame] | 338 | break; |
| 339 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 340 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 341 | SkOpAngle* oAngle = SkOpTAllocator<SkOpAngle>::Allocate(allocator); |
| 342 | oAngle->set(oEndSpan, oStartSpan); |
| 343 | oEndSpan->setFromAngle(oAngle); |
| 344 | *otherPtr = other; |
| 345 | return oAngle; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 346 | } |
| 347 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 348 | SkOpPtT* SkOpSegment::addT(double t, AllowAlias allowAlias, SkChunkAlloc* allocator) { |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 349 | debugValidate(); |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 350 | SkPoint pt = this->ptAtT(t); |
| 351 | SkOpSpanBase* span = &fHead; |
| 352 | do { |
| 353 | SkOpPtT* result = span->ptT(); |
| 354 | if (t == result->fT) { |
| 355 | return result; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 356 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 357 | if (this->match(result, this, t, pt)) { |
| 358 | // see if any existing alias matches segment, pt, and t |
| 359 | SkOpPtT* loop = result->next(); |
| 360 | bool duplicatePt = false; |
| 361 | while (loop != result) { |
| 362 | bool ptMatch = loop->fPt == pt; |
| 363 | if (loop->segment() == this && loop->fT == t && ptMatch) { |
| 364 | return result; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 365 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 366 | duplicatePt |= ptMatch; |
| 367 | loop = loop->next(); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 368 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 369 | if (kNoAlias == allowAlias) { |
| 370 | return result; |
| 371 | } |
| 372 | SkOpPtT* alias = SkOpTAllocator<SkOpPtT>::Allocate(allocator); |
| 373 | alias->init(result->span(), t, pt, duplicatePt); |
| 374 | result->insert(alias); |
| 375 | result->span()->unaligned(); |
| 376 | this->debugValidate(); |
| 377 | #if DEBUG_ADD_T |
| 378 | SkDebugf("%s alias t=%1.9g segID=%d spanID=%d\n", __FUNCTION__, t, |
| 379 | alias->segment()->debugID(), alias->span()->debugID()); |
| 380 | #endif |
| 381 | return alias; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 382 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 383 | if (t < result->fT) { |
| 384 | SkOpSpan* prev = result->span()->prev(); |
| 385 | SkOpSpan* span = insert(prev, allocator); |
| 386 | span->init(this, prev, t, pt); |
| 387 | this->debugValidate(); |
| 388 | #if DEBUG_ADD_T |
| 389 | SkDebugf("%s insert t=%1.9g segID=%d spanID=%d\n", __FUNCTION__, t, |
| 390 | span->segment()->debugID(), span->debugID()); |
| 391 | #endif |
| 392 | return span->ptT(); |
| 393 | } |
| 394 | SkASSERT(span != &fTail); |
| 395 | } while ((span = span->upCast()->next())); |
| 396 | SkASSERT(0); |
| 397 | return NULL; |
| 398 | } |
| 399 | |
| 400 | // choose a solitary t and pt value; remove aliases; align the opposite ends |
| 401 | void SkOpSegment::align() { |
| 402 | debugValidate(); |
| 403 | SkOpSpanBase* span = &fHead; |
| 404 | if (!span->aligned()) { |
| 405 | span->alignEnd(0, fPts[0]); |
| 406 | } |
| 407 | while ((span = span->upCast()->next())) { |
| 408 | if (span == &fTail) { |
| 409 | break; |
| 410 | } |
| 411 | span->align(); |
| 412 | } |
| 413 | if (!span->aligned()) { |
| 414 | span->alignEnd(1, fPts[SkPathOpsVerbToPoints(fVerb)]); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 415 | } |
| 416 | debugValidate(); |
| 417 | } |
| 418 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 419 | bool SkOpSegment::BetweenTs(const SkOpSpanBase* lesser, double testT, |
| 420 | const SkOpSpanBase* greater) { |
| 421 | if (lesser->t() > greater->t()) { |
| 422 | SkTSwap<const SkOpSpanBase*>(lesser, greater); |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 423 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 424 | return approximately_between(lesser->t(), testT, greater->t()); |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 425 | } |
| 426 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 427 | void SkOpSegment::calcAngles(SkChunkAlloc* allocator) { |
| 428 | bool activePrior = !fHead.isCanceled(); |
| 429 | if (activePrior && !fHead.simple()) { |
| 430 | addStartSpan(allocator); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 431 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 432 | SkOpSpan* prior = &fHead; |
| 433 | SkOpSpanBase* spanBase = fHead.next(); |
| 434 | while (spanBase != &fTail) { |
| 435 | if (activePrior) { |
| 436 | SkOpAngle* priorAngle = SkOpTAllocator<SkOpAngle>::Allocate(allocator); |
| 437 | priorAngle->set(spanBase, prior); |
| 438 | spanBase->setFromAngle(priorAngle); |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 439 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 440 | SkOpSpan* span = spanBase->upCast(); |
| 441 | bool active = !span->isCanceled(); |
| 442 | SkOpSpanBase* next = span->next(); |
| 443 | if (active) { |
| 444 | SkOpAngle* angle = SkOpTAllocator<SkOpAngle>::Allocate(allocator); |
| 445 | angle->set(span, next); |
| 446 | span->setToAngle(angle); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 447 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 448 | activePrior = active; |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 449 | prior = span; |
| 450 | spanBase = next; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 451 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 452 | if (activePrior && !fTail.simple()) { |
| 453 | addEndSpan(allocator); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 454 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 455 | } |
| 456 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 457 | void SkOpSegment::checkAngleCoin(SkOpCoincidence* coincidences, SkChunkAlloc* allocator) { |
| 458 | SkOpSpanBase* base = &fHead; |
| 459 | SkOpSpan* span; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 460 | do { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 461 | SkOpAngle* angle = base->fromAngle(); |
| 462 | if (angle && angle->fCheckCoincidence) { |
| 463 | angle->checkNearCoincidence(); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 464 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 465 | if (base->final()) { |
| 466 | break; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 467 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 468 | span = base->upCast(); |
| 469 | angle = span->toAngle(); |
| 470 | if (angle && angle->fCheckCoincidence) { |
| 471 | angle->checkNearCoincidence(); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 472 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 473 | } while ((base = span->next())); |
| 474 | } |
| 475 | |
| 476 | // from http://stackoverflow.com/questions/1165647/how-to-determine-if-a-list-of-polygon-points-are-in-clockwise-order |
| 477 | bool SkOpSegment::clockwise(const SkOpSpanBase* start, const SkOpSpanBase* end, bool* swap) const { |
| 478 | SkASSERT(fVerb != SkPath::kLine_Verb); |
| 479 | SkPoint edge[4]; |
| 480 | if (fVerb == SkPath::kCubic_Verb) { |
| 481 | double startT = start->t(); |
| 482 | double endT = end->t(); |
| 483 | bool flip = startT > endT; |
| 484 | SkDCubic cubic; |
| 485 | cubic.set(fPts); |
| 486 | double inflectionTs[2]; |
| 487 | int inflections = cubic.findInflections(inflectionTs); |
| 488 | for (int index = 0; index < inflections; ++index) { |
| 489 | double inflectionT = inflectionTs[index]; |
| 490 | if (between(startT, inflectionT, endT)) { |
| 491 | if (flip) { |
| 492 | if (inflectionT != endT) { |
| 493 | startT = inflectionT; |
| 494 | } |
| 495 | } else { |
| 496 | if (inflectionT != startT) { |
| 497 | endT = inflectionT; |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | SkDCubic part = cubic.subDivide(startT, endT); |
| 503 | for (int index = 0; index < 4; ++index) { |
| 504 | edge[index] = part[index].asSkPoint(); |
| 505 | } |
| 506 | } else { |
| 507 | subDivide(start, end, edge); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 508 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 509 | bool sumSet = false; |
| 510 | int points = SkPathOpsVerbToPoints(fVerb); |
| 511 | double sum = (edge[0].fX - edge[points].fX) * (edge[0].fY + edge[points].fY); |
| 512 | if (!sumSet) { |
| 513 | for (int idx = 0; idx < points; ++idx){ |
| 514 | sum += (edge[idx + 1].fX - edge[idx].fX) * (edge[idx + 1].fY + edge[idx].fY); |
| 515 | } |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 516 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 517 | if (fVerb == SkPath::kCubic_Verb) { |
| 518 | SkDCubic cubic; |
| 519 | cubic.set(edge); |
| 520 | *swap = sum > 0 && !cubic.monotonicInY(); |
| 521 | } else { |
| 522 | SkDQuad quad; |
| 523 | quad.set(edge); |
| 524 | *swap = sum > 0 && !quad.monotonicInY(); |
| 525 | } |
| 526 | return sum <= 0; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 527 | } |
| 528 | |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 529 | void SkOpSegment::ComputeOneSum(const SkOpAngle* baseAngle, SkOpAngle* nextAngle, |
| 530 | SkOpAngle::IncludeType includeType) { |
| 531 | const SkOpSegment* baseSegment = baseAngle->segment(); |
| 532 | int sumMiWinding = baseSegment->updateWindingReverse(baseAngle); |
| 533 | int sumSuWinding; |
| 534 | bool binary = includeType >= SkOpAngle::kBinarySingle; |
| 535 | if (binary) { |
| 536 | sumSuWinding = baseSegment->updateOppWindingReverse(baseAngle); |
| 537 | if (baseSegment->operand()) { |
| 538 | SkTSwap<int>(sumMiWinding, sumSuWinding); |
| 539 | } |
| 540 | } |
| 541 | SkOpSegment* nextSegment = nextAngle->segment(); |
| 542 | int maxWinding, sumWinding; |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 543 | SkOpSpanBase* last; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 544 | if (binary) { |
| 545 | int oppMaxWinding, oppSumWinding; |
| 546 | nextSegment->setUpWindings(nextAngle->start(), nextAngle->end(), &sumMiWinding, |
| 547 | &sumSuWinding, &maxWinding, &sumWinding, &oppMaxWinding, &oppSumWinding); |
| 548 | last = nextSegment->markAngle(maxWinding, sumWinding, oppMaxWinding, oppSumWinding, |
commit-bot@chromium.org | 866f4e3 | 2013-11-21 17:04:29 +0000 | [diff] [blame] | 549 | nextAngle); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 550 | } else { |
| 551 | nextSegment->setUpWindings(nextAngle->start(), nextAngle->end(), &sumMiWinding, |
| 552 | &maxWinding, &sumWinding); |
commit-bot@chromium.org | 866f4e3 | 2013-11-21 17:04:29 +0000 | [diff] [blame] | 553 | last = nextSegment->markAngle(maxWinding, sumWinding, nextAngle); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 554 | } |
| 555 | nextAngle->setLastMarked(last); |
| 556 | } |
| 557 | |
| 558 | void SkOpSegment::ComputeOneSumReverse(const SkOpAngle* baseAngle, SkOpAngle* nextAngle, |
| 559 | SkOpAngle::IncludeType includeType) { |
| 560 | const SkOpSegment* baseSegment = baseAngle->segment(); |
| 561 | int sumMiWinding = baseSegment->updateWinding(baseAngle); |
| 562 | int sumSuWinding; |
| 563 | bool binary = includeType >= SkOpAngle::kBinarySingle; |
| 564 | if (binary) { |
| 565 | sumSuWinding = baseSegment->updateOppWinding(baseAngle); |
| 566 | if (baseSegment->operand()) { |
| 567 | SkTSwap<int>(sumMiWinding, sumSuWinding); |
| 568 | } |
| 569 | } |
| 570 | SkOpSegment* nextSegment = nextAngle->segment(); |
| 571 | int maxWinding, sumWinding; |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 572 | SkOpSpanBase* last; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 573 | if (binary) { |
| 574 | int oppMaxWinding, oppSumWinding; |
| 575 | nextSegment->setUpWindings(nextAngle->end(), nextAngle->start(), &sumMiWinding, |
| 576 | &sumSuWinding, &maxWinding, &sumWinding, &oppMaxWinding, &oppSumWinding); |
| 577 | last = nextSegment->markAngle(maxWinding, sumWinding, oppMaxWinding, oppSumWinding, |
commit-bot@chromium.org | 866f4e3 | 2013-11-21 17:04:29 +0000 | [diff] [blame] | 578 | nextAngle); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 579 | } else { |
| 580 | nextSegment->setUpWindings(nextAngle->end(), nextAngle->start(), &sumMiWinding, |
| 581 | &maxWinding, &sumWinding); |
commit-bot@chromium.org | 866f4e3 | 2013-11-21 17:04:29 +0000 | [diff] [blame] | 582 | last = nextSegment->markAngle(maxWinding, sumWinding, nextAngle); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 583 | } |
| 584 | nextAngle->setLastMarked(last); |
| 585 | } |
| 586 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 587 | // at this point, the span is already ordered, or unorderable |
| 588 | int SkOpSegment::computeSum(SkOpSpanBase* start, SkOpSpanBase* end, |
| 589 | SkOpAngle::IncludeType includeType) { |
| 590 | SkASSERT(includeType != SkOpAngle::kUnaryXor); |
| 591 | SkOpAngle* firstAngle = this->spanToAngle(end, start); |
| 592 | if (NULL == firstAngle || NULL == firstAngle->next()) { |
| 593 | return SK_NaN32; |
| 594 | } |
| 595 | // if all angles have a computed winding, |
| 596 | // or if no adjacent angles are orderable, |
| 597 | // or if adjacent orderable angles have no computed winding, |
| 598 | // there's nothing to do |
| 599 | // if two orderable angles are adjacent, and both are next to orderable angles, |
| 600 | // and one has winding computed, transfer to the other |
| 601 | SkOpAngle* baseAngle = NULL; |
| 602 | bool tryReverse = false; |
| 603 | // look for counterclockwise transfers |
| 604 | SkOpAngle* angle = firstAngle->previous(); |
| 605 | SkOpAngle* next = angle->next(); |
| 606 | firstAngle = next; |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 607 | do { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 608 | SkOpAngle* prior = angle; |
| 609 | angle = next; |
| 610 | next = angle->next(); |
| 611 | SkASSERT(prior->next() == angle); |
| 612 | SkASSERT(angle->next() == next); |
| 613 | if (prior->unorderable() || angle->unorderable() || next->unorderable()) { |
| 614 | baseAngle = NULL; |
| 615 | continue; |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 616 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 617 | int testWinding = angle->starter()->windSum(); |
| 618 | if (SK_MinS32 != testWinding) { |
| 619 | baseAngle = angle; |
| 620 | tryReverse = true; |
| 621 | continue; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 622 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 623 | if (baseAngle) { |
| 624 | ComputeOneSum(baseAngle, angle, includeType); |
| 625 | baseAngle = SK_MinS32 != angle->starter()->windSum() ? angle : NULL; |
| 626 | } |
| 627 | } while (next != firstAngle); |
| 628 | if (baseAngle && SK_MinS32 == firstAngle->starter()->windSum()) { |
| 629 | firstAngle = baseAngle; |
| 630 | tryReverse = true; |
| 631 | } |
| 632 | if (tryReverse) { |
| 633 | baseAngle = NULL; |
| 634 | SkOpAngle* prior = firstAngle; |
| 635 | do { |
| 636 | angle = prior; |
| 637 | prior = angle->previous(); |
| 638 | SkASSERT(prior->next() == angle); |
| 639 | next = angle->next(); |
| 640 | if (prior->unorderable() || angle->unorderable() || next->unorderable()) { |
| 641 | baseAngle = NULL; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 642 | continue; |
| 643 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 644 | int testWinding = angle->starter()->windSum(); |
| 645 | if (SK_MinS32 != testWinding) { |
| 646 | baseAngle = angle; |
| 647 | continue; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 648 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 649 | if (baseAngle) { |
| 650 | ComputeOneSumReverse(baseAngle, angle, includeType); |
| 651 | baseAngle = SK_MinS32 != angle->starter()->windSum() ? angle : NULL; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 652 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 653 | } while (prior != firstAngle); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 654 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 655 | return start->starter(end)->windSum(); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 656 | } |
| 657 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 658 | SkOpSpan* SkOpSegment::crossedSpanY(const SkPoint& basePt, double mid, bool opp, bool current, |
| 659 | SkScalar* bestY, double* hitT, bool* hitSomething, bool* vertical) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 660 | SkScalar bottom = fBounds.fBottom; |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 661 | *vertical = false; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 662 | if (bottom <= *bestY) { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 663 | return NULL; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 664 | } |
| 665 | SkScalar top = fBounds.fTop; |
| 666 | if (top >= basePt.fY) { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 667 | return NULL; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 668 | } |
| 669 | if (fBounds.fLeft > basePt.fX) { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 670 | return NULL; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 671 | } |
| 672 | if (fBounds.fRight < basePt.fX) { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 673 | return NULL; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 674 | } |
| 675 | if (fBounds.fLeft == fBounds.fRight) { |
| 676 | // if vertical, and directly above test point, wait for another one |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 677 | *vertical = AlmostEqualUlps(basePt.fX, fBounds.fLeft); |
| 678 | return NULL; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 679 | } |
| 680 | // intersect ray starting at basePt with edge |
| 681 | SkIntersections intersections; |
| 682 | // OPTIMIZE: use specialty function that intersects ray with curve, |
| 683 | // returning t values only for curve (we don't care about t on ray) |
caryclark@google.com | a2bbc6e | 2013-11-01 17:36:03 +0000 | [diff] [blame] | 684 | intersections.allowNear(false); |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 685 | int pts = (intersections.*CurveVertical[SkPathOpsVerbToPoints(fVerb)]) |
| 686 | (fPts, top, bottom, basePt.fX, false); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 687 | if (pts == 0 || (current && pts == 1)) { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 688 | return NULL; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 689 | } |
| 690 | if (current) { |
| 691 | SkASSERT(pts > 1); |
| 692 | int closestIdx = 0; |
| 693 | double closest = fabs(intersections[0][0] - mid); |
| 694 | for (int idx = 1; idx < pts; ++idx) { |
| 695 | double test = fabs(intersections[0][idx] - mid); |
| 696 | if (closest > test) { |
| 697 | closestIdx = idx; |
| 698 | closest = test; |
| 699 | } |
| 700 | } |
| 701 | intersections.quickRemoveOne(closestIdx, --pts); |
| 702 | } |
| 703 | double bestT = -1; |
| 704 | for (int index = 0; index < pts; ++index) { |
| 705 | double foundT = intersections[0][index]; |
| 706 | if (approximately_less_than_zero(foundT) |
| 707 | || approximately_greater_than_one(foundT)) { |
| 708 | continue; |
| 709 | } |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 710 | SkScalar testY = (*CurvePointAtT[SkPathOpsVerbToPoints(fVerb)])(fPts, foundT).fY; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 711 | if (approximately_negative(testY - *bestY) |
| 712 | || approximately_negative(basePt.fY - testY)) { |
| 713 | continue; |
| 714 | } |
| 715 | if (pts > 1 && fVerb == SkPath::kLine_Verb) { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 716 | *vertical = true; |
| 717 | return NULL; // if the intersection is edge on, wait for another one |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 718 | } |
| 719 | if (fVerb > SkPath::kLine_Verb) { |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 720 | SkScalar dx = (*CurveSlopeAtT[SkPathOpsVerbToPoints(fVerb)])(fPts, foundT).fX; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 721 | if (approximately_zero(dx)) { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 722 | *vertical = true; |
| 723 | return NULL; // hit vertical, wait for another one |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 724 | } |
| 725 | } |
| 726 | *bestY = testY; |
| 727 | bestT = foundT; |
| 728 | } |
| 729 | if (bestT < 0) { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 730 | return NULL; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 731 | } |
| 732 | SkASSERT(bestT >= 0); |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 733 | SkASSERT(bestT < 1); |
| 734 | SkOpSpanBase* testTSpanBase = &this->fHead; |
| 735 | SkOpSpanBase* nextTSpan; |
| 736 | double endT = 0; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 737 | do { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 738 | nextTSpan = testTSpanBase->upCast()->next(); |
| 739 | endT = nextTSpan->t(); |
| 740 | if (endT >= bestT) { |
| 741 | break; |
| 742 | } |
| 743 | testTSpanBase = nextTSpan; |
| 744 | } while (testTSpanBase); |
| 745 | SkOpSpan* bestTSpan = NULL; |
| 746 | SkOpSpan* testTSpan = testTSpanBase->upCast(); |
| 747 | if (!testTSpan->isCanceled()) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 748 | *hitT = bestT; |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 749 | bestTSpan = testTSpan; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 750 | *hitSomething = true; |
| 751 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 752 | return bestTSpan; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 753 | } |
| 754 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 755 | void SkOpSegment::detach(const SkOpSpan* span) { |
| 756 | if (span->done()) { |
| 757 | --this->fDoneCount; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 758 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 759 | --this->fCount; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 760 | } |
| 761 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 762 | double SkOpSegment::distSq(double t, SkOpAngle* oppAngle) { |
| 763 | SkDPoint testPt = this->dPtAtT(t); |
| 764 | SkDLine testPerp = {{ testPt, testPt }}; |
| 765 | SkDVector slope = this->dSlopeAtT(t); |
| 766 | testPerp[1].fX += slope.fY; |
| 767 | testPerp[1].fY -= slope.fX; |
| 768 | SkIntersections i; |
| 769 | SkOpSegment* oppSegment = oppAngle->segment(); |
| 770 | int oppPtCount = SkPathOpsVerbToPoints(oppSegment->verb()); |
| 771 | (*CurveIntersectRay[oppPtCount])(oppSegment->pts(), testPerp, &i); |
| 772 | double closestDistSq = SK_ScalarInfinity; |
| 773 | for (int index = 0; index < i.used(); ++index) { |
| 774 | 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] | 775 | continue; |
| 776 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 777 | double testDistSq = testPt.distanceSquared(i.pt(index)); |
| 778 | if (closestDistSq > testDistSq) { |
| 779 | closestDistSq = testDistSq; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 780 | } |
| 781 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 782 | return closestDistSq; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 783 | } |
| 784 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 785 | /* |
| 786 | The M and S variable name parts stand for the operators. |
| 787 | Mi stands for Minuend (see wiki subtraction, analogous to difference) |
| 788 | Su stands for Subtrahend |
| 789 | The Opp variable name part designates that the value is for the Opposite operator. |
| 790 | Opposite values result from combining coincident spans. |
| 791 | */ |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 792 | SkOpSegment* SkOpSegment::findNextOp(SkTDArray<SkOpSpanBase*>* chase, SkOpSpanBase** nextStart, |
| 793 | SkOpSpanBase** nextEnd, bool* unsortable, SkPathOp op, int xorMiMask, int xorSuMask) { |
| 794 | SkOpSpanBase* start = *nextStart; |
| 795 | SkOpSpanBase* end = *nextEnd; |
| 796 | SkASSERT(start != end); |
| 797 | int step = start->step(end); |
| 798 | SkOpSegment* other = this->isSimple(nextStart, &step); // advances nextStart |
| 799 | if (other) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 800 | // mark the smaller of startIndex, endIndex done, and all adjacent |
| 801 | // spans with the same T value (but not 'other' spans) |
| 802 | #if DEBUG_WINDING |
| 803 | SkDebugf("%s simple\n", __FUNCTION__); |
| 804 | #endif |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 805 | SkOpSpan* startSpan = start->starter(end); |
| 806 | if (startSpan->done()) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 807 | return NULL; |
| 808 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 809 | markDone(startSpan); |
| 810 | *nextEnd = step > 0 ? (*nextStart)->upCast()->next() : (*nextStart)->prev(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 811 | return other; |
| 812 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 813 | SkOpSpanBase* endNear = step > 0 ? (*nextStart)->upCast()->next() : (*nextStart)->prev(); |
| 814 | SkASSERT(endNear == end); // is this ever not end? |
| 815 | SkASSERT(endNear); |
| 816 | SkASSERT(start != endNear); |
| 817 | SkASSERT((start->t() < endNear->t()) ^ (step < 0)); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 818 | // more than one viable candidate -- measure angles to find best |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 819 | int calcWinding = computeSum(start, endNear, SkOpAngle::kBinaryOpp); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 820 | bool sortable = calcWinding != SK_NaN32; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 821 | if (!sortable) { |
| 822 | *unsortable = true; |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 823 | markDone(start->starter(end)); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 824 | return NULL; |
| 825 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 826 | SkOpAngle* angle = this->spanToAngle(end, start); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 827 | if (angle->unorderable()) { |
| 828 | *unsortable = true; |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 829 | markDone(start->starter(end)); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 830 | return NULL; |
| 831 | } |
| 832 | #if DEBUG_SORT |
| 833 | SkDebugf("%s\n", __FUNCTION__); |
| 834 | angle->debugLoop(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 835 | #endif |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 836 | int sumMiWinding = updateWinding(end, start); |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 837 | if (sumMiWinding == SK_MinS32) { |
| 838 | *unsortable = true; |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 839 | markDone(start->starter(end)); |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 840 | return NULL; |
| 841 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 842 | int sumSuWinding = updateOppWinding(end, start); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 843 | if (operand()) { |
| 844 | SkTSwap<int>(sumMiWinding, sumSuWinding); |
| 845 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 846 | SkOpAngle* nextAngle = angle->next(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 847 | const SkOpAngle* foundAngle = NULL; |
| 848 | bool foundDone = false; |
| 849 | // iterate through the angle, and compute everyone's winding |
| 850 | SkOpSegment* nextSegment; |
| 851 | int activeCount = 0; |
| 852 | do { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 853 | nextSegment = nextAngle->segment(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 854 | bool activeAngle = nextSegment->activeOp(xorMiMask, xorSuMask, nextAngle->start(), |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 855 | nextAngle->end(), op, &sumMiWinding, &sumSuWinding); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 856 | if (activeAngle) { |
| 857 | ++activeCount; |
| 858 | if (!foundAngle || (foundDone && activeCount & 1)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 859 | foundAngle = nextAngle; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 860 | foundDone = nextSegment->done(nextAngle); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 861 | } |
| 862 | } |
| 863 | if (nextSegment->done()) { |
| 864 | continue; |
| 865 | } |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 866 | if (!activeAngle) { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 867 | (void) nextSegment->markAndChaseDone(nextAngle->start(), nextAngle->end()); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 868 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 869 | SkOpSpanBase* last = nextAngle->lastMarked(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 870 | if (last) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 871 | SkASSERT(!SkPathOpsDebug::ChaseContains(*chase, last)); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 872 | *chase->append() = last; |
| 873 | #if DEBUG_WINDING |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 874 | SkDebugf("%s chase.append segment=%d span=%d", __FUNCTION__, |
| 875 | last->segment()->debugID(), last->debugID()); |
| 876 | if (!last->final()) { |
| 877 | SkDebugf(" windSum=%d", last->upCast()->windSum()); |
| 878 | } |
| 879 | SkDebugf("\n"); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 880 | #endif |
| 881 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 882 | } while ((nextAngle = nextAngle->next()) != angle); |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 883 | start->segment()->markDone(start->starter(end)); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 884 | if (!foundAngle) { |
| 885 | return NULL; |
| 886 | } |
| 887 | *nextStart = foundAngle->start(); |
| 888 | *nextEnd = foundAngle->end(); |
| 889 | nextSegment = foundAngle->segment(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 890 | #if DEBUG_WINDING |
| 891 | SkDebugf("%s from:[%d] to:[%d] start=%d end=%d\n", |
| 892 | __FUNCTION__, debugID(), nextSegment->debugID(), *nextStart, *nextEnd); |
| 893 | #endif |
| 894 | return nextSegment; |
| 895 | } |
| 896 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 897 | SkOpSegment* SkOpSegment::findNextWinding(SkTDArray<SkOpSpanBase*>* chase, |
| 898 | SkOpSpanBase** nextStart, SkOpSpanBase** nextEnd, bool* unsortable) { |
| 899 | SkOpSpanBase* start = *nextStart; |
| 900 | SkOpSpanBase* end = *nextEnd; |
| 901 | SkASSERT(start != end); |
| 902 | int step = start->step(end); |
| 903 | SkOpSegment* other = this->isSimple(nextStart, &step); // advances nextStart |
| 904 | if (other) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 905 | // mark the smaller of startIndex, endIndex done, and all adjacent |
| 906 | // spans with the same T value (but not 'other' spans) |
| 907 | #if DEBUG_WINDING |
| 908 | SkDebugf("%s simple\n", __FUNCTION__); |
| 909 | #endif |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 910 | SkOpSpan* startSpan = start->starter(end); |
| 911 | if (startSpan->done()) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 912 | return NULL; |
| 913 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 914 | markDone(startSpan); |
| 915 | *nextEnd = step > 0 ? (*nextStart)->upCast()->next() : (*nextStart)->prev(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 916 | return other; |
| 917 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 918 | SkOpSpanBase* endNear = step > 0 ? (*nextStart)->upCast()->next() : (*nextStart)->prev(); |
| 919 | SkASSERT(endNear == end); // is this ever not end? |
| 920 | SkASSERT(endNear); |
| 921 | SkASSERT(start != endNear); |
| 922 | SkASSERT((start->t() < endNear->t()) ^ (step < 0)); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 923 | // more than one viable candidate -- measure angles to find best |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 924 | int calcWinding = computeSum(start, endNear, SkOpAngle::kUnaryWinding); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 925 | bool sortable = calcWinding != SK_NaN32; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 926 | if (!sortable) { |
| 927 | *unsortable = true; |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 928 | markDone(start->starter(end)); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 929 | return NULL; |
| 930 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 931 | SkOpAngle* angle = this->spanToAngle(end, start); |
| 932 | if (angle->unorderable()) { |
| 933 | *unsortable = true; |
| 934 | markDone(start->starter(end)); |
| 935 | return NULL; |
| 936 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 937 | #if DEBUG_SORT |
| 938 | SkDebugf("%s\n", __FUNCTION__); |
| 939 | angle->debugLoop(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 940 | #endif |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 941 | int sumWinding = updateWinding(end, start); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 942 | SkOpAngle* nextAngle = angle->next(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 943 | const SkOpAngle* foundAngle = NULL; |
| 944 | bool foundDone = false; |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 945 | // iterate through the angle, and compute everyone's winding |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 946 | SkOpSegment* nextSegment; |
| 947 | int activeCount = 0; |
| 948 | do { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 949 | nextSegment = nextAngle->segment(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 950 | bool activeAngle = nextSegment->activeWinding(nextAngle->start(), nextAngle->end(), |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 951 | &sumWinding); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 952 | if (activeAngle) { |
| 953 | ++activeCount; |
| 954 | if (!foundAngle || (foundDone && activeCount & 1)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 955 | foundAngle = nextAngle; |
| 956 | foundDone = nextSegment->done(nextAngle); |
| 957 | } |
| 958 | } |
| 959 | if (nextSegment->done()) { |
| 960 | continue; |
| 961 | } |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 962 | if (!activeAngle) { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 963 | (void) nextSegment->markAndChaseDone(nextAngle->start(), nextAngle->end()); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 964 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 965 | SkOpSpanBase* last = nextAngle->lastMarked(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 966 | if (last) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 967 | SkASSERT(!SkPathOpsDebug::ChaseContains(*chase, last)); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 968 | *chase->append() = last; |
| 969 | #if DEBUG_WINDING |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 970 | SkDebugf("%s chase.append segment=%d span=%d", __FUNCTION__, |
| 971 | last->segment()->debugID(), last->debugID()); |
| 972 | if (!last->final()) { |
| 973 | SkDebugf(" windSum=%d", last->upCast()->windSum()); |
| 974 | } |
| 975 | SkDebugf("\n"); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 976 | #endif |
| 977 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 978 | } while ((nextAngle = nextAngle->next()) != angle); |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 979 | start->segment()->markDone(start->starter(end)); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 980 | if (!foundAngle) { |
| 981 | return NULL; |
| 982 | } |
| 983 | *nextStart = foundAngle->start(); |
| 984 | *nextEnd = foundAngle->end(); |
| 985 | nextSegment = foundAngle->segment(); |
| 986 | #if DEBUG_WINDING |
| 987 | SkDebugf("%s from:[%d] to:[%d] start=%d end=%d\n", |
| 988 | __FUNCTION__, debugID(), nextSegment->debugID(), *nextStart, *nextEnd); |
| 989 | #endif |
| 990 | return nextSegment; |
| 991 | } |
| 992 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 993 | SkOpSegment* SkOpSegment::findNextXor(SkOpSpanBase** nextStart, SkOpSpanBase** nextEnd, |
| 994 | bool* unsortable) { |
| 995 | SkOpSpanBase* start = *nextStart; |
| 996 | SkOpSpanBase* end = *nextEnd; |
| 997 | SkASSERT(start != end); |
| 998 | int step = start->step(end); |
| 999 | SkOpSegment* other = this->isSimple(nextStart, &step); // advances nextStart |
| 1000 | if (other) { |
| 1001 | // mark the smaller of startIndex, endIndex done, and all adjacent |
| 1002 | // spans with the same T value (but not 'other' spans) |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1003 | #if DEBUG_WINDING |
| 1004 | SkDebugf("%s simple\n", __FUNCTION__); |
| 1005 | #endif |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1006 | SkOpSpan* startSpan = start->starter(end); |
| 1007 | if (startSpan->done()) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1008 | return NULL; |
| 1009 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1010 | markDone(startSpan); |
| 1011 | *nextEnd = step > 0 ? (*nextStart)->upCast()->next() : (*nextStart)->prev(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1012 | return other; |
| 1013 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1014 | SkDEBUGCODE(SkOpSpanBase* endNear = step > 0 ? (*nextStart)->upCast()->next() \ |
| 1015 | : (*nextStart)->prev()); |
| 1016 | SkASSERT(endNear == end); // is this ever not end? |
| 1017 | SkASSERT(endNear); |
| 1018 | SkASSERT(start != endNear); |
| 1019 | SkASSERT((start->t() < endNear->t()) ^ (step < 0)); |
| 1020 | SkOpAngle* angle = this->spanToAngle(end, start); |
| 1021 | if (angle->unorderable()) { |
| 1022 | *unsortable = true; |
| 1023 | markDone(start->starter(end)); |
| 1024 | return NULL; |
| 1025 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1026 | #if DEBUG_SORT |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1027 | SkDebugf("%s\n", __FUNCTION__); |
| 1028 | angle->debugLoop(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1029 | #endif |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1030 | SkOpAngle* nextAngle = angle->next(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1031 | const SkOpAngle* foundAngle = NULL; |
| 1032 | bool foundDone = false; |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1033 | // iterate through the angle, and compute everyone's winding |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1034 | SkOpSegment* nextSegment; |
| 1035 | int activeCount = 0; |
| 1036 | do { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1037 | nextSegment = nextAngle->segment(); |
| 1038 | ++activeCount; |
| 1039 | if (!foundAngle || (foundDone && activeCount & 1)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1040 | foundAngle = nextAngle; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1041 | if (!(foundDone = nextSegment->done(nextAngle))) { |
| 1042 | break; |
| 1043 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1044 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1045 | nextAngle = nextAngle->next(); |
| 1046 | } while (nextAngle != angle); |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1047 | start->segment()->markDone(start->starter(end)); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1048 | if (!foundAngle) { |
| 1049 | return NULL; |
| 1050 | } |
| 1051 | *nextStart = foundAngle->start(); |
| 1052 | *nextEnd = foundAngle->end(); |
| 1053 | nextSegment = foundAngle->segment(); |
| 1054 | #if DEBUG_WINDING |
| 1055 | SkDebugf("%s from:[%d] to:[%d] start=%d end=%d\n", |
| 1056 | __FUNCTION__, debugID(), nextSegment->debugID(), *nextStart, *nextEnd); |
| 1057 | #endif |
| 1058 | return nextSegment; |
| 1059 | } |
| 1060 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1061 | SkOpSegment* SkOpSegment::findTop(bool firstPass, SkOpSpanBase** startPtr, SkOpSpanBase** endPtr, |
| 1062 | bool* unsortable, SkChunkAlloc* allocator) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1063 | // iterate through T intersections and return topmost |
| 1064 | // topmost tangent from y-min to first pt is closer to horizontal |
| 1065 | SkASSERT(!done()); |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1066 | SkOpSpanBase* firstT = NULL; |
| 1067 | (void) this->activeLeftTop(&firstT); |
| 1068 | if (!firstT) { |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 1069 | *unsortable = !firstPass; |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1070 | firstT = &fHead; |
| 1071 | while (firstT->upCast()->done()) { |
| 1072 | firstT = firstT->upCast()->next(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1073 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1074 | *startPtr = firstT; |
| 1075 | *endPtr = firstT->upCast()->next(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1076 | return this; |
| 1077 | } |
| 1078 | // sort the edges to find the leftmost |
| 1079 | int step = 1; |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1080 | SkOpSpanBase* end; |
| 1081 | if (firstT->final() || firstT->upCast()->done()) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1082 | step = -1; |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1083 | end = firstT->prev(); |
| 1084 | SkASSERT(end); |
| 1085 | } else { |
| 1086 | end = firstT->upCast()->next(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1087 | } |
| 1088 | // if the topmost T is not on end, or is three-way or more, find left |
| 1089 | // look for left-ness from tLeft to firstT (matching y of other) |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1090 | SkASSERT(firstT != end); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1091 | SkOpAngle* markAngle = spanToAngle(firstT, end); |
| 1092 | if (!markAngle) { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1093 | markAngle = addSingletonAngles(step, allocator); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 1094 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1095 | markAngle->markStops(); |
caryclark | e4097e3 | 2014-06-18 07:24:19 -0700 | [diff] [blame] | 1096 | const SkOpAngle* baseAngle = markAngle->next() == markAngle && !isVertical() ? markAngle |
| 1097 | : markAngle->findFirst(); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1098 | if (!baseAngle) { |
| 1099 | return NULL; // nothing to do |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 1100 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1101 | SkScalar top = SK_ScalarMax; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1102 | const SkOpAngle* firstAngle = NULL; |
| 1103 | const SkOpAngle* angle = baseAngle; |
| 1104 | do { |
| 1105 | if (!angle->unorderable()) { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1106 | const SkOpSegment* next = angle->segment(); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1107 | SkPathOpsBounds bounds; |
| 1108 | next->subDivideBounds(angle->end(), angle->start(), &bounds); |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 1109 | bool nearSame = AlmostEqualUlps(top, bounds.top()); |
| 1110 | bool lowerSector = !firstAngle || angle->sectorEnd() < firstAngle->sectorStart(); |
| 1111 | bool lesserSector = top > bounds.fTop; |
| 1112 | if (lesserSector && (!nearSame || lowerSector)) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1113 | top = bounds.fTop; |
| 1114 | firstAngle = angle; |
| 1115 | } |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 1116 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1117 | angle = angle->next(); |
| 1118 | } while (angle != baseAngle); |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 1119 | if (!firstAngle) { |
| 1120 | return NULL; // if all are unorderable, give up |
| 1121 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1122 | #if DEBUG_SORT |
| 1123 | SkDebugf("%s\n", __FUNCTION__); |
| 1124 | firstAngle->debugLoop(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1125 | #endif |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1126 | // skip edges that have already been processed |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1127 | angle = firstAngle; |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 1128 | SkOpSegment* leftSegment = NULL; |
| 1129 | bool looped = false; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1130 | do { |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 1131 | *unsortable = angle->unorderable(); |
| 1132 | if (firstPass || !*unsortable) { |
| 1133 | leftSegment = angle->segment(); |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1134 | *startPtr = angle->end(); |
| 1135 | *endPtr = angle->start(); |
| 1136 | const SkOpSpan* firstSpan = (*startPtr)->starter(*endPtr); |
| 1137 | if (!firstSpan->done()) { |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 1138 | break; |
| 1139 | } |
| 1140 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1141 | angle = angle->next(); |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 1142 | looped = true; |
| 1143 | } while (angle != firstAngle); |
| 1144 | if (angle == firstAngle && looped) { |
| 1145 | return NULL; |
| 1146 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1147 | if (leftSegment->verb() >= SkPath::kQuad_Verb) { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1148 | SkOpSpanBase* start = *startPtr; |
| 1149 | SkOpSpanBase* end = *endPtr; |
caryclark | e4097e3 | 2014-06-18 07:24:19 -0700 | [diff] [blame] | 1150 | bool swap; |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1151 | if (!leftSegment->clockwise(start, end, &swap)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1152 | #if DEBUG_SWAP_TOP |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1153 | SkDebugf("%s swap=%d inflections=%d monotonic=%d\n", |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 1154 | __FUNCTION__, |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1155 | swap, leftSegment->debugInflections(start, end), |
| 1156 | leftSegment->monotonicInY(start, end)); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1157 | #endif |
| 1158 | if (swap) { |
| 1159 | // FIXME: I doubt it makes sense to (necessarily) swap if the edge was not the first |
| 1160 | // sorted but merely the first not already processed (i.e., not done) |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1161 | SkTSwap(*startPtr, *endPtr); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1162 | } |
| 1163 | } |
| 1164 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1165 | return leftSegment; |
| 1166 | } |
| 1167 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1168 | SkOpGlobalState* SkOpSegment::globalState() const { |
| 1169 | return contour()->globalState(); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1172 | void SkOpSegment::init(SkPoint pts[], SkOpContour* contour, SkPath::Verb verb) { |
| 1173 | fContour = contour; |
| 1174 | fNext = NULL; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1175 | fPts = pts; |
| 1176 | fVerb = verb; |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1177 | fCount = 0; |
| 1178 | fDoneCount = 0; |
| 1179 | fVisited = false; |
| 1180 | SkOpSpan* zeroSpan = &fHead; |
| 1181 | zeroSpan->init(this, NULL, 0, fPts[0]); |
| 1182 | SkOpSpanBase* oneSpan = &fTail; |
| 1183 | zeroSpan->setNext(oneSpan); |
| 1184 | oneSpan->initBase(this, zeroSpan, 1, fPts[SkPathOpsVerbToPoints(fVerb)]); |
| 1185 | PATH_OPS_DEBUG_CODE(fID = globalState()->nextSegmentID()); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1188 | void SkOpSegment::initWinding(SkOpSpanBase* start, SkOpSpanBase* end, |
| 1189 | SkOpAngle::IncludeType angleIncludeType) { |
| 1190 | int local = SkOpSegment::SpanSign(start, end); |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 1191 | SkDEBUGCODE(bool success); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1192 | if (angleIncludeType == SkOpAngle::kBinarySingle) { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1193 | int oppLocal = SkOpSegment::OppSign(start, end); |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 1194 | SkDEBUGCODE(success =) markAndChaseWinding(start, end, local, oppLocal, NULL); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1195 | // OPTIMIZATION: the reverse mark and chase could skip the first marking |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 1196 | SkDEBUGCODE(success |=) markAndChaseWinding(end, start, local, oppLocal, NULL); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1197 | } else { |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 1198 | SkDEBUGCODE(success =) markAndChaseWinding(start, end, local, NULL); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1199 | // OPTIMIZATION: the reverse mark and chase could skip the first marking |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 1200 | SkDEBUGCODE(success |=) markAndChaseWinding(end, start, local, NULL); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1201 | } |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 1202 | SkASSERT(success); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | /* |
| 1206 | when we start with a vertical intersect, we try to use the dx to determine if the edge is to |
| 1207 | the left or the right of vertical. This determines if we need to add the span's |
| 1208 | sign or not. However, this isn't enough. |
| 1209 | If the supplied sign (winding) is zero, then we didn't hit another vertical span, so dx is needed. |
| 1210 | If there was a winding, then it may or may not need adjusting. If the span the winding was borrowed |
| 1211 | from has the same x direction as this span, the winding should change. If the dx is opposite, then |
| 1212 | the same winding is shared by both. |
| 1213 | */ |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1214 | bool SkOpSegment::initWinding(SkOpSpanBase* start, SkOpSpanBase* end, double tHit, |
| 1215 | int winding, SkScalar hitDx, int oppWind, SkScalar hitOppDx) { |
| 1216 | SkASSERT(this == start->segment()); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1217 | SkASSERT(hitDx || !winding); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1218 | SkScalar dx = (*CurveSlopeAtT[SkPathOpsVerbToPoints(fVerb)])(fPts, tHit).fX; |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1219 | // SkASSERT(dx); |
| 1220 | int windVal = start->starter(end)->windValue(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1221 | #if DEBUG_WINDING_AT_T |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1222 | SkDebugf("%s id=%d oldWinding=%d hitDx=%c dx=%c windVal=%d", __FUNCTION__, debugID(), winding, |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1223 | hitDx ? hitDx > 0 ? '+' : '-' : '0', dx > 0 ? '+' : '-', windVal); |
| 1224 | #endif |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1225 | int sideWind = winding + (dx < 0 ? windVal : -windVal); |
| 1226 | if (abs(winding) < abs(sideWind)) { |
| 1227 | winding = sideWind; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1228 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1229 | SkDEBUGCODE(int oppLocal = SkOpSegment::OppSign(start, end)); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1230 | SkASSERT(hitOppDx || !oppWind || !oppLocal); |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1231 | int oppWindVal = start->starter(end)->oppValue(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1232 | if (!oppWind) { |
| 1233 | oppWind = dx < 0 ? oppWindVal : -oppWindVal; |
| 1234 | } else if (hitOppDx * dx >= 0) { |
| 1235 | int oppSideWind = oppWind + (dx < 0 ? oppWindVal : -oppWindVal); |
| 1236 | if (abs(oppWind) < abs(oppSideWind)) { |
| 1237 | oppWind = oppSideWind; |
| 1238 | } |
| 1239 | } |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1240 | #if DEBUG_WINDING_AT_T |
| 1241 | SkDebugf(" winding=%d oppWind=%d\n", winding, oppWind); |
| 1242 | #endif |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 1243 | // if this fails to mark (because the edges are too small) inform caller to try again |
| 1244 | bool success = markAndChaseWinding(start, end, winding, oppWind, NULL); |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 1245 | // OPTIMIZATION: the reverse mark and chase could skip the first marking |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 1246 | success |= markAndChaseWinding(end, start, winding, oppWind, NULL); |
| 1247 | return success; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1250 | bool SkOpSegment::isClose(double t, const SkOpSegment* opp) const { |
| 1251 | SkDPoint cPt = this->dPtAtT(t); |
| 1252 | int pts = SkPathOpsVerbToPoints(this->verb()); |
| 1253 | SkDVector dxdy = (*CurveDSlopeAtT[pts])(this->pts(), t); |
| 1254 | SkDLine perp = {{ cPt, {cPt.fX + dxdy.fY, cPt.fY - dxdy.fX} }}; |
| 1255 | SkIntersections i; |
| 1256 | int oppPts = SkPathOpsVerbToPoints(opp->verb()); |
| 1257 | (*CurveIntersectRay[oppPts])(opp->pts(), perp, &i); |
| 1258 | int used = i.used(); |
| 1259 | for (int index = 0; index < used; ++index) { |
| 1260 | if (cPt.roughlyEqual(i.pt(index))) { |
commit-bot@chromium.org | 866f4e3 | 2013-11-21 17:04:29 +0000 | [diff] [blame] | 1261 | return true; |
| 1262 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1263 | } |
caryclark@google.com | a2bbc6e | 2013-11-01 17:36:03 +0000 | [diff] [blame] | 1264 | return false; |
| 1265 | } |
| 1266 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1267 | bool SkOpSegment::isXor() const { |
| 1268 | return fContour->isXor(); |
| 1269 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1270 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1271 | SkOpSpanBase* SkOpSegment::markAndChaseDone(SkOpSpanBase* start, SkOpSpanBase* end) { |
| 1272 | int step = start->step(end); |
| 1273 | SkOpSpan* minSpan = start->starter(end); |
| 1274 | markDone(minSpan); |
| 1275 | SkOpSpanBase* last = NULL; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1276 | SkOpSegment* other = this; |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1277 | while ((other = other->nextChase(&start, &step, &minSpan, &last))) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1278 | if (other->done()) { |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1279 | SkASSERT(!last); |
| 1280 | break; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1281 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1282 | other->markDone(minSpan); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1283 | } |
| 1284 | return last; |
| 1285 | } |
| 1286 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1287 | bool SkOpSegment::markAndChaseWinding(SkOpSpanBase* start, SkOpSpanBase* end, int winding, |
| 1288 | SkOpSpanBase** lastPtr) { |
| 1289 | SkOpSpan* spanStart = start->starter(end); |
| 1290 | int step = start->step(end); |
| 1291 | bool success = markWinding(spanStart, winding); |
| 1292 | SkOpSpanBase* last = NULL; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1293 | SkOpSegment* other = this; |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1294 | while ((other = other->nextChase(&start, &step, &spanStart, &last))) { |
| 1295 | if (spanStart->windSum() != SK_MinS32) { |
| 1296 | SkASSERT(spanStart->windSum() == winding); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1297 | SkASSERT(!last); |
| 1298 | break; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1299 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1300 | (void) other->markWinding(spanStart, winding); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1301 | } |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 1302 | if (lastPtr) { |
| 1303 | *lastPtr = last; |
| 1304 | } |
| 1305 | return success; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1308 | bool SkOpSegment::markAndChaseWinding(SkOpSpanBase* start, SkOpSpanBase* end, |
| 1309 | int winding, int oppWinding, SkOpSpanBase** lastPtr) { |
| 1310 | SkOpSpan* spanStart = start->starter(end); |
| 1311 | int step = start->step(end); |
| 1312 | bool success = markWinding(spanStart, winding, oppWinding); |
| 1313 | SkOpSpanBase* last = NULL; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1314 | SkOpSegment* other = this; |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1315 | while ((other = other->nextChase(&start, &step, &spanStart, &last))) { |
| 1316 | if (spanStart->windSum() != SK_MinS32) { |
| 1317 | if (this->operand() == other->operand()) { |
| 1318 | SkASSERT(spanStart->windSum() == winding); |
| 1319 | if (spanStart->oppSum() != oppWinding) { |
| 1320 | this->globalState()->setWindingFailed(); |
| 1321 | return false; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1322 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1323 | } else { |
| 1324 | SkASSERT(spanStart->windSum() == oppWinding); |
| 1325 | SkASSERT(spanStart->oppSum() == winding); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1326 | } |
| 1327 | SkASSERT(!last); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1328 | break; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1329 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1330 | if (this->operand() == other->operand()) { |
| 1331 | (void) other->markWinding(spanStart, winding, oppWinding); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1332 | } else { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1333 | (void) other->markWinding(spanStart, oppWinding, winding); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1334 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1335 | } |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 1336 | if (lastPtr) { |
| 1337 | *lastPtr = last; |
| 1338 | } |
| 1339 | return success; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1342 | SkOpSpanBase* SkOpSegment::markAngle(int maxWinding, int sumWinding, const SkOpAngle* angle) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1343 | SkASSERT(angle->segment() == this); |
| 1344 | if (UseInnerWinding(maxWinding, sumWinding)) { |
| 1345 | maxWinding = sumWinding; |
| 1346 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1347 | SkOpSpanBase* last; |
| 1348 | (void) markAndChaseWinding(angle->start(), angle->end(), maxWinding, &last); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 1349 | #if DEBUG_WINDING |
| 1350 | if (last) { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1351 | SkDebugf("%s last seg=%d span=%d", __FUNCTION__, |
| 1352 | last->segment()->debugID(), last->debugID()); |
| 1353 | if (!last->final()) { |
| 1354 | SkDebugf(" windSum="); |
| 1355 | SkPathOpsDebug::WindingPrintf(last->upCast()->windSum()); |
| 1356 | } |
| 1357 | SkDebugf("\n"); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 1358 | } |
| 1359 | #endif |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1360 | return last; |
| 1361 | } |
| 1362 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1363 | SkOpSpanBase* SkOpSegment::markAngle(int maxWinding, int sumWinding, int oppMaxWinding, |
| 1364 | int oppSumWinding, const SkOpAngle* angle) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1365 | SkASSERT(angle->segment() == this); |
| 1366 | if (UseInnerWinding(maxWinding, sumWinding)) { |
| 1367 | maxWinding = sumWinding; |
| 1368 | } |
| 1369 | if (oppMaxWinding != oppSumWinding && UseInnerWinding(oppMaxWinding, oppSumWinding)) { |
| 1370 | oppMaxWinding = oppSumWinding; |
| 1371 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1372 | SkOpSpanBase* last = NULL; |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 1373 | // caller doesn't require that this marks anything |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1374 | (void) markAndChaseWinding(angle->start(), angle->end(), maxWinding, oppMaxWinding, &last); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 1375 | #if DEBUG_WINDING |
| 1376 | if (last) { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1377 | SkDebugf("%s last segment=%d span=%d", __FUNCTION__, |
| 1378 | last->segment()->debugID(), last->debugID()); |
| 1379 | if (!last->final()) { |
| 1380 | SkDebugf(" windSum="); |
| 1381 | SkPathOpsDebug::WindingPrintf(last->upCast()->windSum()); |
| 1382 | } |
| 1383 | SkDebugf(" \n"); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 1384 | } |
| 1385 | #endif |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1386 | return last; |
| 1387 | } |
| 1388 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1389 | void SkOpSegment::markDone(SkOpSpan* span) { |
| 1390 | SkASSERT(this == span->segment()); |
| 1391 | if (span->done()) { |
| 1392 | return; |
| 1393 | } |
| 1394 | #if DEBUG_MARK_DONE |
| 1395 | debugShowNewWinding(__FUNCTION__, span, span->windSum(), span->oppSum()); |
| 1396 | #endif |
| 1397 | span->setDone(true); |
| 1398 | ++fDoneCount; |
| 1399 | debugValidate(); |
| 1400 | } |
| 1401 | |
| 1402 | bool SkOpSegment::markWinding(SkOpSpan* span, int winding) { |
| 1403 | SkASSERT(this == span->segment()); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1404 | SkASSERT(winding); |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1405 | if (span->done()) { |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 1406 | return false; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1407 | } |
| 1408 | #if DEBUG_MARK_DONE |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1409 | debugShowNewWinding(__FUNCTION__, span, winding); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1410 | #endif |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1411 | span->setWindSum(winding); |
| 1412 | debugValidate(); |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 1413 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1416 | bool SkOpSegment::markWinding(SkOpSpan* span, int winding, int oppWinding) { |
| 1417 | SkASSERT(this == span->segment()); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1418 | SkASSERT(winding || oppWinding); |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1419 | if (span->done()) { |
| 1420 | return false; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1421 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1422 | #if DEBUG_MARK_DONE |
| 1423 | debugShowNewWinding(__FUNCTION__, span, winding, oppWinding); |
| 1424 | #endif |
| 1425 | span->setWindSum(winding); |
| 1426 | span->setOppSum(oppWinding); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1427 | debugValidate(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1428 | return true; |
| 1429 | } |
| 1430 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1431 | bool SkOpSegment::match(const SkOpPtT* base, const SkOpSegment* testParent, double testT, |
| 1432 | const SkPoint& testPt) const { |
| 1433 | const SkOpSegment* baseParent = base->segment(); |
| 1434 | if (this == baseParent && this == testParent && precisely_equal(base->fT, testT)) { |
| 1435 | return true; |
| 1436 | } |
| 1437 | if (!SkDPoint::ApproximatelyEqual(testPt, base->fPt)) { |
| 1438 | return false; |
| 1439 | } |
| 1440 | return !ptsDisjoint(base->fT, base->fPt, testT, testPt); |
| 1441 | } |
| 1442 | |
| 1443 | static SkOpSegment* set_last(SkOpSpanBase** last, SkOpSpanBase* endSpan) { |
| 1444 | if (last) { |
| 1445 | *last = endSpan; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1446 | } |
| 1447 | return NULL; |
| 1448 | } |
| 1449 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1450 | bool SkOpSegment::monotonicInY(const SkOpSpanBase* start, const SkOpSpanBase* end) const { |
| 1451 | SkASSERT(fVerb != SkPath::kLine_Verb); |
| 1452 | if (fVerb == SkPath::kQuad_Verb) { |
| 1453 | SkDQuad dst = SkDQuad::SubDivide(fPts, start->t(), end->t()); |
| 1454 | return dst.monotonicInY(); |
| 1455 | } |
| 1456 | SkASSERT(fVerb == SkPath::kCubic_Verb); |
| 1457 | SkDCubic dst = SkDCubic::SubDivide(fPts, start->t(), end->t()); |
| 1458 | return dst.monotonicInY(); |
| 1459 | } |
| 1460 | |
| 1461 | bool SkOpSegment::NextCandidate(SkOpSpanBase* span, SkOpSpanBase** start, |
| 1462 | SkOpSpanBase** end) { |
| 1463 | while (span->final() || span->upCast()->done()) { |
| 1464 | if (span->final()) { |
| 1465 | return false; |
| 1466 | } |
| 1467 | span = span->upCast()->next(); |
| 1468 | } |
| 1469 | *start = span; |
| 1470 | *end = span->upCast()->next(); |
| 1471 | return true; |
| 1472 | } |
| 1473 | |
| 1474 | SkOpSegment* SkOpSegment::nextChase(SkOpSpanBase** startPtr, int* stepPtr, SkOpSpan** minPtr, |
| 1475 | SkOpSpanBase** last) const { |
| 1476 | SkOpSpanBase* origStart = *startPtr; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1477 | int step = *stepPtr; |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1478 | SkOpSpanBase* endSpan = step > 0 ? origStart->upCast()->next() : origStart->prev(); |
| 1479 | SkASSERT(endSpan); |
| 1480 | SkOpAngle* angle = step > 0 ? endSpan->fromAngle() : endSpan->upCast()->toAngle(); |
| 1481 | SkOpSpanBase* foundSpan; |
| 1482 | SkOpSpanBase* otherEnd; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1483 | SkOpSegment* other; |
| 1484 | if (angle == NULL) { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1485 | if (endSpan->t() != 0 && endSpan->t() != 1) { |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1486 | return NULL; |
| 1487 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1488 | SkOpPtT* otherPtT = endSpan->ptT()->next(); |
| 1489 | other = otherPtT->segment(); |
| 1490 | foundSpan = otherPtT->span(); |
| 1491 | otherEnd = step > 0 ? foundSpan->upCast()->next() : foundSpan->prev(); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1492 | } else { |
| 1493 | int loopCount = angle->loopCount(); |
| 1494 | if (loopCount > 2) { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1495 | return set_last(last, endSpan); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1496 | } |
| 1497 | const SkOpAngle* next = angle->next(); |
caryclark | 65b427c | 2014-09-18 10:32:57 -0700 | [diff] [blame] | 1498 | if (NULL == next) { |
| 1499 | return NULL; |
| 1500 | } |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1501 | #if DEBUG_WINDING |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1502 | if (angle->sign() != next->sign() && !angle->segment()->contour()->isXor() |
| 1503 | && !next->segment()->contour()->isXor()) { |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1504 | SkDebugf("%s mismatched signs\n", __FUNCTION__); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1505 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1506 | #endif |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1507 | other = next->segment(); |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1508 | foundSpan = endSpan = next->start(); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1509 | otherEnd = next->end(); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 1510 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1511 | int foundStep = foundSpan->step(otherEnd); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1512 | if (*stepPtr != foundStep) { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1513 | return set_last(last, endSpan); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1514 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1515 | SkASSERT(*startPtr); |
| 1516 | if (!otherEnd) { |
caryclark | 65b427c | 2014-09-18 10:32:57 -0700 | [diff] [blame] | 1517 | return NULL; |
| 1518 | } |
| 1519 | // SkASSERT(otherEnd >= 0); |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1520 | SkOpSpan* origMin = step < 0 ? origStart->prev() : origStart->upCast(); |
| 1521 | SkOpSpan* foundMin = foundSpan->starter(otherEnd); |
| 1522 | if (foundMin->windValue() != origMin->windValue() |
| 1523 | || foundMin->oppValue() != origMin->oppValue()) { |
| 1524 | return set_last(last, endSpan); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1525 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1526 | *startPtr = foundSpan; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1527 | *stepPtr = foundStep; |
| 1528 | if (minPtr) { |
| 1529 | *minPtr = foundMin; |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 1530 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1531 | return other; |
| 1532 | } |
| 1533 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1534 | static void clear_visited(SkOpSpan* span) { |
| 1535 | // reset visited flag back to false |
| 1536 | do { |
| 1537 | SkOpPtT* ptT = span->ptT(), * stopPtT = ptT; |
| 1538 | while ((ptT = ptT->next()) != stopPtT) { |
| 1539 | SkOpSegment* opp = ptT->segment(); |
| 1540 | opp->resetVisited(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1541 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1542 | } while ((span = span->next()->upCastable())); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1545 | // look for pairs of undetected coincident curves |
| 1546 | // assumes that segments going in have visited flag clear |
| 1547 | // curve/curve intersection should now do a pretty good job of finding coincident runs so |
| 1548 | // this may be only be necessary for line/curve pairs -- so skip unless this is a line and the |
| 1549 | // the opp is not a line |
| 1550 | void SkOpSegment::missingCoincidence(SkOpCoincidence* coincidences, SkChunkAlloc* allocator) { |
| 1551 | if (this->verb() != SkPath::kLine_Verb) { |
| 1552 | return; |
| 1553 | } |
| 1554 | SkOpSpan* prior = NULL; |
| 1555 | SkOpSpan* span = &fHead; |
| 1556 | do { |
| 1557 | SkOpPtT* ptT = span->ptT(), * spanStopPtT = ptT; |
| 1558 | SkASSERT(ptT->span() == span); |
| 1559 | while ((ptT = ptT->next()) != spanStopPtT) { |
| 1560 | SkOpSegment* opp = ptT->span()->segment(); |
| 1561 | if (opp->setVisited()) { |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 1562 | continue; |
| 1563 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1564 | if (opp->verb() == SkPath::kLine_Verb) { |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 1565 | continue; |
| 1566 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1567 | if (span->containsCoincidence(opp)) { // FIXME: this assumes that if the opposite |
| 1568 | // segment is coincident then no more coincidence |
| 1569 | // needs to be detected. This may not be true. |
| 1570 | continue; |
caryclark | 5e27e0e | 2014-08-12 07:46:33 -0700 | [diff] [blame] | 1571 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1572 | if (span->containsCoinEnd(opp)) { |
| 1573 | continue; |
| 1574 | } |
| 1575 | // if already visited and visited again, check for coin |
| 1576 | if (span == &fHead) { |
| 1577 | continue; |
| 1578 | } |
| 1579 | SkOpPtT* priorPtT = NULL, * priorStopPtT; |
| 1580 | // find prior span containing opp segment |
| 1581 | SkOpSegment* priorOpp = NULL; |
| 1582 | prior = span; |
| 1583 | while (!priorOpp && (prior = prior->prev())) { |
| 1584 | priorStopPtT = priorPtT = prior->ptT(); |
| 1585 | while ((priorPtT = priorPtT->next()) != priorStopPtT) { |
| 1586 | SkOpSegment* segment = priorPtT->span()->segment(); |
| 1587 | if (segment == opp) { |
| 1588 | priorOpp = opp; |
| 1589 | break; |
| 1590 | } |
| 1591 | } |
| 1592 | } |
| 1593 | if (!priorOpp) { |
| 1594 | continue; |
| 1595 | } |
| 1596 | SkOpPtT* oppStart = prior->ptT(); |
| 1597 | SkOpPtT* oppEnd = span->ptT(); |
| 1598 | bool swapped = priorPtT->fT > ptT->fT; |
| 1599 | if (swapped) { |
| 1600 | SkTSwap(priorPtT, ptT); |
| 1601 | SkTSwap(oppStart, oppEnd); |
| 1602 | } |
| 1603 | bool flipped = oppStart->fT > oppEnd->fT; |
| 1604 | bool coincident; |
| 1605 | if (coincidences->contains(priorPtT, ptT, oppStart, oppEnd, flipped)) { |
| 1606 | goto swapBack; |
| 1607 | } |
| 1608 | { |
| 1609 | // average t, find mid pt |
| 1610 | double midT = (prior->t() + span->t()) / 2; |
| 1611 | SkPoint midPt = this->ptAtT(midT); |
| 1612 | coincident = true; |
| 1613 | // if the mid pt is not near either end pt, project perpendicular through opp seg |
| 1614 | if (!SkDPoint::ApproximatelyEqual(priorPtT->fPt, midPt) |
| 1615 | && !SkDPoint::ApproximatelyEqual(ptT->fPt, midPt)) { |
| 1616 | coincident = false; |
| 1617 | SkIntersections i; |
| 1618 | int ptCount = SkPathOpsVerbToPoints(this->verb()); |
| 1619 | SkVector dxdy = (*CurveSlopeAtT[ptCount])(pts(), midT); |
| 1620 | SkDLine ray = {{{midPt.fX, midPt.fY}, |
| 1621 | {midPt.fX + dxdy.fY, midPt.fY - dxdy.fX}}}; |
| 1622 | int oppPtCount = SkPathOpsVerbToPoints(opp->verb()); |
| 1623 | (*CurveIntersectRay[oppPtCount])(opp->pts(), ray, &i); |
| 1624 | // measure distance and see if it's small enough to denote coincidence |
| 1625 | for (int index = 0; index < i.used(); ++index) { |
| 1626 | SkDPoint oppPt = i.pt(index); |
| 1627 | if (oppPt.approximatelyEqual(midPt)) { |
| 1628 | SkVector oppDxdy = (*CurveSlopeAtT[oppPtCount])(opp->pts(), |
| 1629 | i[index][0]); |
| 1630 | oppDxdy.normalize(); |
| 1631 | dxdy.normalize(); |
| 1632 | SkScalar flatness = SkScalarAbs(dxdy.cross(oppDxdy) / FLT_EPSILON); |
| 1633 | coincident |= flatness < 5000; // FIXME: replace with tuned value |
| 1634 | } |
| 1635 | } |
| 1636 | } |
| 1637 | } |
| 1638 | if (coincident) { |
| 1639 | // mark coincidence |
| 1640 | coincidences->add(priorPtT, ptT, oppStart, oppEnd, allocator); |
| 1641 | clear_visited(&fHead); |
| 1642 | missingCoincidence(coincidences, allocator); |
| 1643 | return; |
| 1644 | } |
| 1645 | swapBack: |
| 1646 | if (swapped) { |
| 1647 | SkTSwap(priorPtT, ptT); |
| 1648 | } |
caryclark | 5e27e0e | 2014-08-12 07:46:33 -0700 | [diff] [blame] | 1649 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1650 | } while ((span = span->next()->upCastable())); |
| 1651 | clear_visited(&fHead); |
caryclark | 5e27e0e | 2014-08-12 07:46:33 -0700 | [diff] [blame] | 1652 | } |
| 1653 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1654 | // Move nearby t values and pts so they all hang off the same span. Alignment happens later. |
| 1655 | bool SkOpSegment::moveNearby() { |
| 1656 | debugValidate(); |
| 1657 | SkOpSpanBase* spanS = &fHead; |
| 1658 | do { |
| 1659 | SkOpSpanBase* test = spanS->upCast()->next(); |
| 1660 | SkOpSpanBase* next; |
| 1661 | if (spanS->contains(test)) { |
| 1662 | if (!test->final()) { |
| 1663 | test->upCast()->detach(spanS->ptT()); |
| 1664 | continue; |
| 1665 | } else if (spanS != &fHead) { |
| 1666 | spanS->upCast()->detach(test->ptT()); |
| 1667 | spanS = test; |
| 1668 | continue; |
| 1669 | } |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1670 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1671 | do { // iterate through all spans associated with start |
| 1672 | SkOpPtT* startBase = spanS->ptT(); |
| 1673 | next = test->final() ? NULL : test->upCast()->next(); |
| 1674 | do { |
| 1675 | SkOpPtT* testBase = test->ptT(); |
| 1676 | do { |
| 1677 | if (startBase == testBase) { |
| 1678 | goto checkNextSpan; |
| 1679 | } |
| 1680 | if (testBase->duplicate()) { |
| 1681 | continue; |
| 1682 | } |
| 1683 | if (this->match(startBase, testBase->segment(), testBase->fT, testBase->fPt)) { |
| 1684 | if (test == &this->fTail) { |
| 1685 | if (spanS == &fHead) { |
| 1686 | debugValidate(); |
| 1687 | return true; // if this span has collapsed, remove it from parent |
| 1688 | } |
| 1689 | this->fTail.merge(spanS->upCast()); |
| 1690 | debugValidate(); |
| 1691 | return true; |
| 1692 | } |
| 1693 | spanS->merge(test->upCast()); |
| 1694 | spanS->upCast()->setNext(next); |
| 1695 | goto checkNextSpan; |
| 1696 | } |
| 1697 | } while ((testBase = testBase->next()) != test->ptT()); |
| 1698 | } while ((startBase = startBase->next()) != spanS->ptT()); |
| 1699 | checkNextSpan: |
| 1700 | ; |
| 1701 | } while ((test = next)); |
| 1702 | spanS = spanS->upCast()->next(); |
| 1703 | } while (!spanS->final()); |
| 1704 | debugValidate(); |
| 1705 | return true; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1706 | } |
| 1707 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1708 | bool SkOpSegment::operand() const { |
| 1709 | return fContour->operand(); |
| 1710 | } |
| 1711 | |
| 1712 | bool SkOpSegment::oppXor() const { |
| 1713 | return fContour->oppXor(); |
| 1714 | } |
| 1715 | |
| 1716 | bool SkOpSegment::ptsDisjoint(double t1, const SkPoint& pt1, double t2, const SkPoint& pt2) const { |
| 1717 | if (fVerb == SkPath::kLine_Verb) { |
| 1718 | return false; |
| 1719 | } |
| 1720 | // quads (and cubics) can loop back to nearly a line so that an opposite curve |
| 1721 | // hits in two places with very different t values. |
| 1722 | // OPTIMIZATION: curves could be preflighted so that, for example, something like |
| 1723 | // 'controls contained by ends' could avoid this check for common curves |
| 1724 | // 'ends are extremes in x or y' is cheaper to compute and real-world common |
| 1725 | // on the other hand, the below check is relatively inexpensive |
| 1726 | double midT = (t1 + t2) / 2; |
| 1727 | SkPoint midPt = this->ptAtT(midT); |
| 1728 | double seDistSq = SkTMax(pt1.distanceToSqd(pt2) * 2, FLT_EPSILON * 2); |
| 1729 | return midPt.distanceToSqd(pt1) > seDistSq || midPt.distanceToSqd(pt2) > seDistSq; |
| 1730 | } |
| 1731 | |
| 1732 | void SkOpSegment::setUpWindings(SkOpSpanBase* start, SkOpSpanBase* end, int* sumMiWinding, |
| 1733 | int* maxWinding, int* sumWinding) { |
| 1734 | int deltaSum = SpanSign(start, end); |
| 1735 | *maxWinding = *sumMiWinding; |
| 1736 | *sumWinding = *sumMiWinding -= deltaSum; |
| 1737 | SkASSERT(!DEBUG_LIMIT_WIND_SUM || abs(*sumWinding) <= DEBUG_LIMIT_WIND_SUM); |
| 1738 | } |
| 1739 | |
| 1740 | void SkOpSegment::setUpWindings(SkOpSpanBase* start, SkOpSpanBase* end, int* sumMiWinding, |
| 1741 | int* sumSuWinding, int* maxWinding, int* sumWinding, int* oppMaxWinding, |
| 1742 | int* oppSumWinding) { |
| 1743 | int deltaSum = SpanSign(start, end); |
| 1744 | int oppDeltaSum = OppSign(start, end); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1745 | if (operand()) { |
| 1746 | *maxWinding = *sumSuWinding; |
| 1747 | *sumWinding = *sumSuWinding -= deltaSum; |
| 1748 | *oppMaxWinding = *sumMiWinding; |
| 1749 | *oppSumWinding = *sumMiWinding -= oppDeltaSum; |
| 1750 | } else { |
| 1751 | *maxWinding = *sumMiWinding; |
| 1752 | *sumWinding = *sumMiWinding -= deltaSum; |
| 1753 | *oppMaxWinding = *sumSuWinding; |
| 1754 | *oppSumWinding = *sumSuWinding -= oppDeltaSum; |
| 1755 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1756 | SkASSERT(!DEBUG_LIMIT_WIND_SUM || abs(*sumWinding) <= DEBUG_LIMIT_WIND_SUM); |
| 1757 | SkASSERT(!DEBUG_LIMIT_WIND_SUM || abs(*oppSumWinding) <= DEBUG_LIMIT_WIND_SUM); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1760 | void SkOpSegment::sortAngles() { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1761 | SkOpSpanBase* span = &this->fHead; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1762 | do { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1763 | SkOpAngle* fromAngle = span->fromAngle(); |
| 1764 | SkOpAngle* toAngle = span->final() ? NULL : span->upCast()->toAngle(); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1765 | if (!fromAngle && !toAngle) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1766 | continue; |
| 1767 | } |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 1768 | #if DEBUG_ANGLE |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1769 | bool wroteAfterHeader = false; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 1770 | #endif |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1771 | SkOpAngle* baseAngle = fromAngle; |
| 1772 | if (fromAngle && toAngle) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1773 | #if DEBUG_ANGLE |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1774 | SkDebugf("%s [%d] tStart=%1.9g [%d]\n", __FUNCTION__, debugID(), span->t(), |
| 1775 | span->debugID()); |
| 1776 | wroteAfterHeader = true; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1777 | #endif |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1778 | fromAngle->insert(toAngle); |
| 1779 | } else if (!fromAngle) { |
| 1780 | baseAngle = toAngle; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1781 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1782 | SkOpPtT* ptT = span->ptT(), * stopPtT = ptT; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1783 | do { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1784 | SkOpSpanBase* oSpan = ptT->span(); |
| 1785 | if (oSpan == span) { |
| 1786 | continue; |
| 1787 | } |
| 1788 | SkOpAngle* oAngle = oSpan->fromAngle(); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 1789 | if (oAngle) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1790 | #if DEBUG_ANGLE |
| 1791 | if (!wroteAfterHeader) { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1792 | SkDebugf("%s [%d] tStart=%1.9g [%d]\n", __FUNCTION__, debugID(), |
| 1793 | span->t(), span->debugID()); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1794 | wroteAfterHeader = true; |
| 1795 | } |
| 1796 | #endif |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1797 | if (!oAngle->loopContains(baseAngle)) { |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 1798 | baseAngle->insert(oAngle); |
| 1799 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1800 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1801 | if (!oSpan->final()) { |
| 1802 | oAngle = oSpan->upCast()->toAngle(); |
| 1803 | if (oAngle) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1804 | #if DEBUG_ANGLE |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1805 | if (!wroteAfterHeader) { |
| 1806 | SkDebugf("%s [%d] tStart=%1.9g [%d]\n", __FUNCTION__, debugID(), |
| 1807 | span->t(), span->debugID()); |
| 1808 | wroteAfterHeader = true; |
| 1809 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1810 | #endif |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1811 | if (!oAngle->loopContains(baseAngle)) { |
| 1812 | baseAngle->insert(oAngle); |
| 1813 | } |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 1814 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1815 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1816 | } while ((ptT = ptT->next()) != stopPtT); |
| 1817 | if (baseAngle->loopCount() == 1) { |
| 1818 | span->setFromAngle(NULL); |
| 1819 | if (toAngle) { |
| 1820 | span->upCast()->setToAngle(NULL); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1821 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1822 | baseAngle = NULL; |
| 1823 | } |
| 1824 | #if DEBUG_SORT |
| 1825 | SkASSERT(!baseAngle || baseAngle->loopCount() > 1); |
| 1826 | #endif |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1827 | } while (!span->final() && (span = span->upCast()->next())); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 1830 | // return true if midpoints were computed |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1831 | bool SkOpSegment::subDivide(const SkOpSpanBase* start, const SkOpSpanBase* end, |
| 1832 | SkPoint edge[4]) const { |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 1833 | SkASSERT(start != end); |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1834 | const SkOpPtT& startPtT = *start->ptT(); |
| 1835 | const SkOpPtT& endPtT = *end->ptT(); |
| 1836 | edge[0] = startPtT.fPt; |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 1837 | int points = SkPathOpsVerbToPoints(fVerb); |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1838 | edge[points] = endPtT.fPt; |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 1839 | if (fVerb == SkPath::kLine_Verb) { |
| 1840 | return false; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1841 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1842 | double startT = startPtT.fT; |
| 1843 | double endT = endPtT.fT; |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 1844 | if ((startT == 0 || endT == 0) && (startT == 1 || endT == 1)) { |
| 1845 | // don't compute midpoints if we already have them |
| 1846 | if (fVerb == SkPath::kQuad_Verb) { |
| 1847 | edge[1] = fPts[1]; |
| 1848 | return false; |
| 1849 | } |
| 1850 | SkASSERT(fVerb == SkPath::kCubic_Verb); |
| 1851 | if (start < end) { |
| 1852 | edge[1] = fPts[1]; |
| 1853 | edge[2] = fPts[2]; |
| 1854 | return false; |
| 1855 | } |
| 1856 | edge[1] = fPts[2]; |
| 1857 | edge[2] = fPts[1]; |
| 1858 | return false; |
| 1859 | } |
| 1860 | const SkDPoint sub[2] = {{ edge[0].fX, edge[0].fY}, {edge[points].fX, edge[points].fY }}; |
| 1861 | if (fVerb == SkPath::kQuad_Verb) { |
| 1862 | edge[1] = SkDQuad::SubDivide(fPts, sub[0], sub[1], startT, endT).asSkPoint(); |
| 1863 | } else { |
| 1864 | SkASSERT(fVerb == SkPath::kCubic_Verb); |
| 1865 | SkDPoint ctrl[2]; |
| 1866 | SkDCubic::SubDivide(fPts, sub[0], sub[1], startT, endT, ctrl); |
| 1867 | edge[1] = ctrl[0].asSkPoint(); |
| 1868 | edge[2] = ctrl[1].asSkPoint(); |
| 1869 | } |
| 1870 | return true; |
| 1871 | } |
| 1872 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1873 | bool SkOpSegment::subDivide(const SkOpSpanBase* start, const SkOpSpanBase* end, |
| 1874 | SkDCubic* result) const { |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 1875 | SkASSERT(start != end); |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1876 | const SkOpPtT& startPtT = *start->ptT(); |
| 1877 | const SkOpPtT& endPtT = *end->ptT(); |
| 1878 | (*result)[0].set(startPtT.fPt); |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 1879 | int points = SkPathOpsVerbToPoints(fVerb); |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1880 | (*result)[points].set(endPtT.fPt); |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 1881 | if (fVerb == SkPath::kLine_Verb) { |
| 1882 | return false; |
| 1883 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1884 | double startT = startPtT.fT; |
| 1885 | double endT = endPtT.fT; |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 1886 | if ((startT == 0 || endT == 0) && (startT == 1 || endT == 1)) { |
| 1887 | // don't compute midpoints if we already have them |
| 1888 | if (fVerb == SkPath::kQuad_Verb) { |
| 1889 | (*result)[1].set(fPts[1]); |
| 1890 | return false; |
| 1891 | } |
| 1892 | SkASSERT(fVerb == SkPath::kCubic_Verb); |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1893 | if (startT == 0) { |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 1894 | (*result)[1].set(fPts[1]); |
| 1895 | (*result)[2].set(fPts[2]); |
| 1896 | return false; |
| 1897 | } |
| 1898 | (*result)[1].set(fPts[2]); |
| 1899 | (*result)[2].set(fPts[1]); |
| 1900 | return false; |
| 1901 | } |
| 1902 | if (fVerb == SkPath::kQuad_Verb) { |
| 1903 | (*result)[1] = SkDQuad::SubDivide(fPts, (*result)[0], (*result)[2], startT, endT); |
| 1904 | } else { |
| 1905 | SkASSERT(fVerb == SkPath::kCubic_Verb); |
| 1906 | SkDCubic::SubDivide(fPts, (*result)[0], (*result)[3], startT, endT, &(*result)[1]); |
| 1907 | } |
| 1908 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1911 | void SkOpSegment::subDivideBounds(const SkOpSpanBase* start, const SkOpSpanBase* end, |
| 1912 | SkPathOpsBounds* bounds) const { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1913 | SkPoint edge[4]; |
| 1914 | subDivide(start, end, edge); |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 1915 | (bounds->*SetCurveBounds[SkPathOpsVerbToPoints(fVerb)])(edge); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1918 | void SkOpSegment::undoneSpan(SkOpSpanBase** start, SkOpSpanBase** end) { |
| 1919 | SkOpSpan* span = this->head(); |
| 1920 | do { |
| 1921 | if (!span->done()) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1922 | break; |
| 1923 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1924 | } while ((span = span->next()->upCastable())); |
| 1925 | SkASSERT(span); |
| 1926 | *start = span; |
| 1927 | *end = span->next(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1930 | int SkOpSegment::updateOppWinding(const SkOpSpanBase* start, const SkOpSpanBase* end) const { |
| 1931 | const SkOpSpan* lesser = start->starter(end); |
| 1932 | int oppWinding = lesser->oppSum(); |
| 1933 | int oppSpanWinding = SkOpSegment::OppSign(start, end); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1934 | if (oppSpanWinding && UseInnerWinding(oppWinding - oppSpanWinding, oppWinding) |
| 1935 | && oppWinding != SK_MaxS32) { |
| 1936 | oppWinding -= oppSpanWinding; |
| 1937 | } |
| 1938 | return oppWinding; |
| 1939 | } |
| 1940 | |
| 1941 | int SkOpSegment::updateOppWinding(const SkOpAngle* angle) const { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1942 | const SkOpSpanBase* startSpan = angle->start(); |
| 1943 | const SkOpSpanBase* endSpan = angle->end(); |
| 1944 | return updateOppWinding(endSpan, startSpan); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
| 1947 | int SkOpSegment::updateOppWindingReverse(const SkOpAngle* angle) const { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1948 | const SkOpSpanBase* startSpan = angle->start(); |
| 1949 | const SkOpSpanBase* endSpan = angle->end(); |
| 1950 | return updateOppWinding(startSpan, endSpan); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1953 | int SkOpSegment::updateWinding(const SkOpSpanBase* start, const SkOpSpanBase* end) const { |
| 1954 | const SkOpSpan* lesser = start->starter(end); |
| 1955 | int winding = lesser->windSum(); |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 1956 | if (winding == SK_MinS32) { |
| 1957 | return winding; |
| 1958 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1959 | int spanWinding = SkOpSegment::SpanSign(start, end); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 1960 | if (winding && UseInnerWinding(winding - spanWinding, winding) |
| 1961 | && winding != SK_MaxS32) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1962 | winding -= spanWinding; |
| 1963 | } |
| 1964 | return winding; |
| 1965 | } |
| 1966 | |
| 1967 | int SkOpSegment::updateWinding(const SkOpAngle* angle) const { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1968 | const SkOpSpanBase* startSpan = angle->start(); |
| 1969 | const SkOpSpanBase* endSpan = angle->end(); |
| 1970 | return updateWinding(endSpan, startSpan); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1973 | int SkOpSegment::updateWindingReverse(const SkOpAngle* angle) const { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1974 | const SkOpSpanBase* startSpan = angle->start(); |
| 1975 | const SkOpSpanBase* endSpan = angle->end(); |
| 1976 | return updateWinding(startSpan, endSpan); |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 1977 | } |
| 1978 | |
| 1979 | // OPTIMIZATION: does the following also work, and is it any faster? |
| 1980 | // return outerWinding * innerWinding > 0 |
| 1981 | // || ((outerWinding + innerWinding < 0) ^ ((outerWinding - innerWinding) < 0))) |
| 1982 | bool SkOpSegment::UseInnerWinding(int outerWinding, int innerWinding) { |
| 1983 | SkASSERT(outerWinding != SK_MaxS32); |
| 1984 | SkASSERT(innerWinding != SK_MaxS32); |
| 1985 | int absOut = abs(outerWinding); |
| 1986 | int absIn = abs(innerWinding); |
| 1987 | bool result = absOut == absIn ? outerWinding < 0 : absOut < absIn; |
| 1988 | return result; |
| 1989 | } |
| 1990 | |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1991 | int SkOpSegment::windingAtT(double tHit, const SkOpSpan* span, bool crossOpp, |
| 1992 | SkScalar* dx) const { |
| 1993 | if (approximately_zero(tHit - span->t())) { // if we hit the end of a span, disregard |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1994 | return SK_MinS32; |
| 1995 | } |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1996 | int winding = crossOpp ? span->oppSum() : span->windSum(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1997 | SkASSERT(winding != SK_MinS32); |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 1998 | int windVal = crossOpp ? span->oppValue() : span->windValue(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1999 | #if DEBUG_WINDING_AT_T |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 2000 | SkDebugf("%s id=%d opp=%d tHit=%1.9g t=%1.9g oldWinding=%d windValue=%d", __FUNCTION__, |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 2001 | debugID(), crossOpp, tHit, span->t(), winding, windVal); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 2002 | #endif |
| 2003 | // see if a + change in T results in a +/- change in X (compute x'(T)) |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 2004 | *dx = (*CurveSlopeAtT[SkPathOpsVerbToPoints(fVerb)])(fPts, tHit).fX; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 2005 | if (fVerb > SkPath::kLine_Verb && approximately_zero(*dx)) { |
| 2006 | *dx = fPts[2].fX - fPts[1].fX - *dx; |
| 2007 | } |
| 2008 | if (*dx == 0) { |
| 2009 | #if DEBUG_WINDING_AT_T |
| 2010 | SkDebugf(" dx=0 winding=SK_MinS32\n"); |
| 2011 | #endif |
| 2012 | return SK_MinS32; |
| 2013 | } |
skia.committer@gmail.com | a4aced4 | 2013-07-09 07:00:56 +0000 | [diff] [blame] | 2014 | if (windVal < 0) { // reverse sign if opp contour traveled in reverse |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 2015 | *dx = -*dx; |
| 2016 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 2017 | if (winding * *dx > 0) { // if same signs, result is negative |
| 2018 | winding += *dx > 0 ? -windVal : windVal; |
| 2019 | } |
| 2020 | #if DEBUG_WINDING_AT_T |
| 2021 | SkDebugf(" dx=%c winding=%d\n", *dx > 0 ? '+' : '-', winding); |
| 2022 | #endif |
| 2023 | return winding; |
| 2024 | } |
| 2025 | |
| 2026 | int SkOpSegment::windSum(const SkOpAngle* angle) const { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 2027 | const SkOpSpan* minSpan = angle->start()->starter(angle->end()); |
| 2028 | return minSpan->windSum(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 2029 | } |