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@google.com | a2bbc6e | 2013-11-01 17:36:03 +0000 | [diff] [blame] | 7 | #include "SkAddIntersections.h" |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 8 | #include "SkOpCoincidence.h" |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 9 | #include "SkOpEdgeBuilder.h" |
| 10 | #include "SkPathOpsCommon.h" |
| 11 | #include "SkPathWriter.h" |
commit-bot@chromium.org | b76d3b6 | 2013-04-22 19:55:19 +0000 | [diff] [blame] | 12 | #include "SkTSort.h" |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 13 | |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 14 | const SkOpAngle* AngleWinding(SkOpSpanBase* start, SkOpSpanBase* end, int* windingPtr, |
| 15 | bool* sortablePtr) { |
| 16 | // find first angle, initialize winding to computed fWindSum |
| 17 | SkOpSegment* segment = start->segment(); |
| 18 | const SkOpAngle* angle = segment->spanToAngle(start, end); |
| 19 | if (!angle) { |
| 20 | *windingPtr = SK_MinS32; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 21 | return nullptr; |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 22 | } |
| 23 | bool computeWinding = false; |
| 24 | const SkOpAngle* firstAngle = angle; |
| 25 | bool loop = false; |
| 26 | bool unorderable = false; |
| 27 | int winding = SK_MinS32; |
| 28 | do { |
| 29 | angle = angle->next(); |
| 30 | unorderable |= angle->unorderable(); |
| 31 | if ((computeWinding = unorderable || (angle == firstAngle && loop))) { |
| 32 | break; // if we get here, there's no winding, loop is unorderable |
| 33 | } |
| 34 | loop |= angle == firstAngle; |
| 35 | segment = angle->segment(); |
| 36 | winding = segment->windSum(angle); |
| 37 | } while (winding == SK_MinS32); |
| 38 | // if the angle loop contains an unorderable span, the angle order may be useless |
| 39 | // directly compute the winding in this case for each span |
| 40 | if (computeWinding) { |
| 41 | firstAngle = angle; |
| 42 | winding = SK_MinS32; |
| 43 | do { |
| 44 | SkOpSpanBase* startSpan = angle->start(); |
| 45 | SkOpSpanBase* endSpan = angle->end(); |
| 46 | SkOpSpan* lesser = startSpan->starter(endSpan); |
| 47 | int testWinding = lesser->windSum(); |
| 48 | if (testWinding == SK_MinS32) { |
| 49 | testWinding = lesser->computeWindSum(); |
| 50 | } |
| 51 | if (testWinding != SK_MinS32) { |
| 52 | segment = angle->segment(); |
| 53 | winding = testWinding; |
| 54 | } |
| 55 | angle = angle->next(); |
| 56 | } while (angle != firstAngle); |
| 57 | } |
| 58 | *sortablePtr = !unorderable; |
| 59 | *windingPtr = winding; |
| 60 | return angle; |
| 61 | } |
| 62 | |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 63 | SkOpSegment* FindUndone(SkOpContourHead* contourList, SkOpSpanBase** startPtr, |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 64 | SkOpSpanBase** endPtr) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 65 | SkOpSegment* result; |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 66 | SkOpContour* contour = contourList; |
| 67 | do { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 68 | result = contour->undoneSegment(startPtr, endPtr); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 69 | if (result) { |
| 70 | return result; |
| 71 | } |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 72 | } while ((contour = contour->next())); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 73 | return nullptr; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 74 | } |
| 75 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 76 | SkOpSegment* FindChase(SkTDArray<SkOpSpanBase*>* chase, SkOpSpanBase** startPtr, |
| 77 | SkOpSpanBase** endPtr) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 78 | while (chase->count()) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 79 | SkOpSpanBase* span; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 80 | chase->pop(&span); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 81 | SkOpSegment* segment = span->segment(); |
| 82 | *startPtr = span->ptT()->next()->span(); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 83 | bool done = true; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 84 | *endPtr = nullptr; |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 85 | if (SkOpAngle* last = segment->activeAngle(*startPtr, startPtr, endPtr, &done)) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 86 | *startPtr = last->start(); |
| 87 | *endPtr = last->end(); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 88 | #if TRY_ROTATE |
| 89 | *chase->insert(0) = span; |
| 90 | #else |
| 91 | *chase->append() = span; |
| 92 | #endif |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 93 | return last->segment(); |
| 94 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 95 | if (done) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 96 | continue; |
| 97 | } |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 98 | // find first angle, initialize winding to computed wind sum |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 99 | int winding; |
| 100 | bool sortable; |
| 101 | const SkOpAngle* angle = AngleWinding(*startPtr, *endPtr, &winding, &sortable); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 102 | if (winding == SK_MinS32) { |
| 103 | continue; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 104 | } |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 105 | int sumWinding SK_INIT_TO_AVOID_WARNING; |
| 106 | if (sortable) { |
| 107 | segment = angle->segment(); |
| 108 | sumWinding = segment->updateWindingReverse(angle); |
| 109 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 110 | SkOpSegment* first = nullptr; |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 111 | const SkOpAngle* firstAngle = angle; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 112 | while ((angle = angle->next()) != firstAngle) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 113 | segment = angle->segment(); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 114 | SkOpSpanBase* start = angle->start(); |
| 115 | SkOpSpanBase* end = angle->end(); |
djsollen | 2f12463 | 2016-04-29 13:53:05 -0700 | [diff] [blame] | 116 | int maxWinding SK_INIT_TO_AVOID_WARNING; |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 117 | if (sortable) { |
| 118 | segment->setUpWinding(start, end, &maxWinding, &sumWinding); |
| 119 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 120 | if (!segment->done(angle)) { |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 121 | if (!first && (sortable || start->starter(end)->windSum() != SK_MinS32)) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 122 | first = segment; |
| 123 | *startPtr = start; |
| 124 | *endPtr = end; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 125 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 126 | // OPTIMIZATION: should this also add to the chase? |
caryclark | bca19f7 | 2015-05-13 08:23:48 -0700 | [diff] [blame] | 127 | if (sortable) { |
| 128 | (void) segment->markAngle(maxWinding, sumWinding, angle); |
| 129 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 130 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 131 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 132 | if (first) { |
| 133 | #if TRY_ROTATE |
| 134 | *chase->insert(0) = span; |
| 135 | #else |
| 136 | *chase->append() = span; |
| 137 | #endif |
| 138 | return first; |
| 139 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 140 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 141 | return nullptr; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 142 | } |
| 143 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 144 | #if DEBUG_ACTIVE_SPANS |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 145 | void DebugShowActiveSpans(SkOpContourHead* contourList) { |
| 146 | SkOpContour* contour = contourList; |
| 147 | do { |
| 148 | contour->debugShowActiveSpans(); |
| 149 | } while ((contour = contour->next())); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 150 | } |
| 151 | #endif |
| 152 | |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 153 | bool SortContourList(SkOpContourHead** contourList, bool evenOdd, bool oppEvenOdd) { |
| 154 | SkTDArray<SkOpContour* > list; |
| 155 | SkOpContour* contour = *contourList; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 156 | do { |
| 157 | if (contour->count()) { |
| 158 | contour->setOppXor(contour->operand() ? evenOdd : oppEvenOdd); |
| 159 | *list.append() = contour; |
| 160 | } |
| 161 | } while ((contour = contour->next())); |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 162 | int count = list.count(); |
| 163 | if (!count) { |
| 164 | return false; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 165 | } |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 166 | if (count > 1) { |
| 167 | SkTQSort<SkOpContour>(list.begin(), list.end() - 1); |
| 168 | } |
| 169 | contour = list[0]; |
| 170 | SkOpContourHead* contourHead = static_cast<SkOpContourHead*>(contour); |
| 171 | contour->globalState()->setContourHead(contourHead); |
| 172 | *contourList = contourHead; |
| 173 | for (int index = 1; index < count; ++index) { |
| 174 | SkOpContour* next = list[index]; |
| 175 | contour->setNext(next); |
| 176 | contour = next; |
| 177 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 178 | contour->setNext(nullptr); |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 179 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 180 | } |
| 181 | |
commit-bot@chromium.org | b76d3b6 | 2013-04-22 19:55:19 +0000 | [diff] [blame] | 182 | class DistanceLessThan { |
| 183 | public: |
| 184 | DistanceLessThan(double* distances) : fDistances(distances) { } |
| 185 | double* fDistances; |
| 186 | bool operator()(const int one, const int two) { |
| 187 | return fDistances[one] < fDistances[two]; |
| 188 | } |
| 189 | }; |
| 190 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 191 | /* |
| 192 | check start and end of each contour |
| 193 | if not the same, record them |
| 194 | match them up |
| 195 | connect closest |
| 196 | reassemble contour pieces into new path |
| 197 | */ |
| 198 | void Assemble(const SkPathWriter& path, SkPathWriter* simple) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 199 | SkChunkAlloc allocator(4096); // FIXME: constant-ize, tune |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 200 | SkOpContourHead contour; |
caryclark | dae6b97 | 2016-06-08 04:28:19 -0700 | [diff] [blame] | 201 | SkOpGlobalState globalState(nullptr, &contour SkDEBUGPARAMS(false) |
| 202 | SkDEBUGPARAMS(nullptr)); |
caryclark | 03b03ca | 2015-04-23 09:13:37 -0700 | [diff] [blame] | 203 | #if DEBUG_SHOW_TEST_NAME |
| 204 | SkDebugf("</div>\n"); |
| 205 | #endif |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 206 | #if DEBUG_PATH_CONSTRUCTION |
| 207 | SkDebugf("%s\n", __FUNCTION__); |
| 208 | #endif |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 209 | SkOpEdgeBuilder builder(path, &contour, &allocator, &globalState); |
| 210 | builder.finish(&allocator); |
| 211 | SkTDArray<const SkOpContour* > runs; // indices of partial contours |
| 212 | const SkOpContour* eContour = builder.head(); |
| 213 | do { |
| 214 | if (!eContour->count()) { |
| 215 | continue; |
| 216 | } |
| 217 | const SkPoint& eStart = eContour->start(); |
| 218 | const SkPoint& eEnd = eContour->end(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 219 | #if DEBUG_ASSEMBLE |
| 220 | SkDebugf("%s contour", __FUNCTION__); |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 221 | if (!SkDPoint::ApproximatelyEqual(eStart, eEnd)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 222 | SkDebugf("[%d]", runs.count()); |
| 223 | } else { |
| 224 | SkDebugf(" "); |
| 225 | } |
| 226 | SkDebugf(" start=(%1.9g,%1.9g) end=(%1.9g,%1.9g)\n", |
| 227 | eStart.fX, eStart.fY, eEnd.fX, eEnd.fY); |
| 228 | #endif |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 229 | if (SkDPoint::ApproximatelyEqual(eStart, eEnd)) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 230 | eContour->toPath(simple); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 231 | continue; |
| 232 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 233 | *runs.append() = eContour; |
| 234 | } while ((eContour = eContour->next())); |
| 235 | int count = runs.count(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 236 | if (count == 0) { |
| 237 | return; |
| 238 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 239 | SkTDArray<int> sLink, eLink; |
| 240 | sLink.append(count); |
| 241 | eLink.append(count); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 242 | int rIndex, iIndex; |
| 243 | for (rIndex = 0; rIndex < count; ++rIndex) { |
| 244 | sLink[rIndex] = eLink[rIndex] = SK_MaxS32; |
| 245 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 246 | const int ends = count * 2; // all starts and ends |
| 247 | const int entries = (ends - 1) * count; // folded triangle : n * (n - 1) / 2 |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 248 | SkTDArray<double> distances; |
| 249 | distances.append(entries); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 250 | for (rIndex = 0; rIndex < ends - 1; ++rIndex) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 251 | const SkOpContour* oContour = runs[rIndex >> 1]; |
| 252 | const SkPoint& oPt = rIndex & 1 ? oContour->end() : oContour->start(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 253 | const int row = rIndex < count - 1 ? rIndex * ends : (ends - rIndex - 2) |
| 254 | * ends - rIndex - 1; |
| 255 | for (iIndex = rIndex + 1; iIndex < ends; ++iIndex) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 256 | const SkOpContour* iContour = runs[iIndex >> 1]; |
| 257 | const SkPoint& iPt = iIndex & 1 ? iContour->end() : iContour->start(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 258 | double dx = iPt.fX - oPt.fX; |
| 259 | double dy = iPt.fY - oPt.fY; |
| 260 | double dist = dx * dx + dy * dy; |
| 261 | distances[row + iIndex] = dist; // oStart distance from iStart |
| 262 | } |
| 263 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 264 | SkTDArray<int> sortedDist; |
| 265 | sortedDist.append(entries); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 266 | for (rIndex = 0; rIndex < entries; ++rIndex) { |
| 267 | sortedDist[rIndex] = rIndex; |
| 268 | } |
commit-bot@chromium.org | b76d3b6 | 2013-04-22 19:55:19 +0000 | [diff] [blame] | 269 | SkTQSort<int>(sortedDist.begin(), sortedDist.end() - 1, DistanceLessThan(distances.begin())); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 270 | int remaining = count; // number of start/end pairs |
| 271 | for (rIndex = 0; rIndex < entries; ++rIndex) { |
| 272 | int pair = sortedDist[rIndex]; |
| 273 | int row = pair / ends; |
| 274 | int col = pair - row * ends; |
| 275 | int thingOne = row < col ? row : ends - row - 2; |
| 276 | int ndxOne = thingOne >> 1; |
| 277 | bool endOne = thingOne & 1; |
| 278 | int* linkOne = endOne ? eLink.begin() : sLink.begin(); |
| 279 | if (linkOne[ndxOne] != SK_MaxS32) { |
| 280 | continue; |
| 281 | } |
| 282 | int thingTwo = row < col ? col : ends - row + col - 1; |
| 283 | int ndxTwo = thingTwo >> 1; |
| 284 | bool endTwo = thingTwo & 1; |
| 285 | int* linkTwo = endTwo ? eLink.begin() : sLink.begin(); |
| 286 | if (linkTwo[ndxTwo] != SK_MaxS32) { |
| 287 | continue; |
| 288 | } |
| 289 | SkASSERT(&linkOne[ndxOne] != &linkTwo[ndxTwo]); |
| 290 | bool flip = endOne == endTwo; |
| 291 | linkOne[ndxOne] = flip ? ~ndxTwo : ndxTwo; |
| 292 | linkTwo[ndxTwo] = flip ? ~ndxOne : ndxOne; |
| 293 | if (!--remaining) { |
| 294 | break; |
| 295 | } |
| 296 | } |
| 297 | SkASSERT(!remaining); |
| 298 | #if DEBUG_ASSEMBLE |
| 299 | for (rIndex = 0; rIndex < count; ++rIndex) { |
| 300 | int s = sLink[rIndex]; |
| 301 | int e = eLink[rIndex]; |
| 302 | SkDebugf("%s %c%d <- s%d - e%d -> %c%d\n", __FUNCTION__, s < 0 ? 's' : 'e', |
| 303 | s < 0 ? ~s : s, rIndex, rIndex, e < 0 ? 'e' : 's', e < 0 ? ~e : e); |
| 304 | } |
| 305 | #endif |
| 306 | rIndex = 0; |
| 307 | do { |
| 308 | bool forward = true; |
| 309 | bool first = true; |
| 310 | int sIndex = sLink[rIndex]; |
| 311 | SkASSERT(sIndex != SK_MaxS32); |
| 312 | sLink[rIndex] = SK_MaxS32; |
| 313 | int eIndex; |
| 314 | if (sIndex < 0) { |
| 315 | eIndex = sLink[~sIndex]; |
| 316 | sLink[~sIndex] = SK_MaxS32; |
| 317 | } else { |
| 318 | eIndex = eLink[sIndex]; |
| 319 | eLink[sIndex] = SK_MaxS32; |
| 320 | } |
| 321 | SkASSERT(eIndex != SK_MaxS32); |
| 322 | #if DEBUG_ASSEMBLE |
| 323 | SkDebugf("%s sIndex=%c%d eIndex=%c%d\n", __FUNCTION__, sIndex < 0 ? 's' : 'e', |
| 324 | sIndex < 0 ? ~sIndex : sIndex, eIndex < 0 ? 's' : 'e', |
| 325 | eIndex < 0 ? ~eIndex : eIndex); |
| 326 | #endif |
| 327 | do { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 328 | const SkOpContour* contour = runs[rIndex]; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 329 | if (first) { |
| 330 | first = false; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 331 | const SkPoint* startPtr = &contour->start(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 332 | simple->deferredMove(startPtr[0]); |
| 333 | } |
| 334 | if (forward) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 335 | contour->toPartialForward(simple); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 336 | } else { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 337 | contour->toPartialBackward(simple); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 338 | } |
| 339 | #if DEBUG_ASSEMBLE |
| 340 | SkDebugf("%s rIndex=%d eIndex=%s%d close=%d\n", __FUNCTION__, rIndex, |
| 341 | eIndex < 0 ? "~" : "", eIndex < 0 ? ~eIndex : eIndex, |
| 342 | sIndex == ((rIndex != eIndex) ^ forward ? eIndex : ~eIndex)); |
| 343 | #endif |
| 344 | if (sIndex == ((rIndex != eIndex) ^ forward ? eIndex : ~eIndex)) { |
| 345 | simple->close(); |
| 346 | break; |
| 347 | } |
| 348 | if (forward) { |
| 349 | eIndex = eLink[rIndex]; |
| 350 | SkASSERT(eIndex != SK_MaxS32); |
| 351 | eLink[rIndex] = SK_MaxS32; |
| 352 | if (eIndex >= 0) { |
| 353 | SkASSERT(sLink[eIndex] == rIndex); |
| 354 | sLink[eIndex] = SK_MaxS32; |
| 355 | } else { |
| 356 | SkASSERT(eLink[~eIndex] == ~rIndex); |
| 357 | eLink[~eIndex] = SK_MaxS32; |
| 358 | } |
| 359 | } else { |
| 360 | eIndex = sLink[rIndex]; |
| 361 | SkASSERT(eIndex != SK_MaxS32); |
| 362 | sLink[rIndex] = SK_MaxS32; |
| 363 | if (eIndex >= 0) { |
| 364 | SkASSERT(eLink[eIndex] == rIndex); |
| 365 | eLink[eIndex] = SK_MaxS32; |
| 366 | } else { |
| 367 | SkASSERT(sLink[~eIndex] == ~rIndex); |
| 368 | sLink[~eIndex] = SK_MaxS32; |
| 369 | } |
| 370 | } |
| 371 | rIndex = eIndex; |
| 372 | if (rIndex < 0) { |
| 373 | forward ^= 1; |
| 374 | rIndex = ~rIndex; |
| 375 | } |
| 376 | } while (true); |
| 377 | for (rIndex = 0; rIndex < count; ++rIndex) { |
| 378 | if (sLink[rIndex] != SK_MaxS32) { |
| 379 | break; |
| 380 | } |
| 381 | } |
| 382 | } while (rIndex < count); |
| 383 | #if DEBUG_ASSEMBLE |
| 384 | for (rIndex = 0; rIndex < count; ++rIndex) { |
| 385 | SkASSERT(sLink[rIndex] == SK_MaxS32); |
| 386 | SkASSERT(eLink[rIndex] == SK_MaxS32); |
| 387 | } |
| 388 | #endif |
| 389 | } |
caryclark@google.com | a2bbc6e | 2013-11-01 17:36:03 +0000 | [diff] [blame] | 390 | |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 391 | static void align(SkOpContourHead* contourList) { |
| 392 | SkOpContour* contour = contourList; |
| 393 | do { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 394 | contour->align(); |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 395 | } while ((contour = contour->next())); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 396 | } |
| 397 | |
caryclark | 27c8eb8 | 2015-07-06 11:38:33 -0700 | [diff] [blame] | 398 | static void addAlignIntersections(SkOpContourHead* contourList, SkChunkAlloc* allocator) { |
| 399 | SkOpContour* contour = contourList; |
| 400 | do { |
| 401 | contour->addAlignIntersections(contourList, allocator); |
| 402 | } while ((contour = contour->next())); |
| 403 | } |
| 404 | |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 405 | static void calcAngles(SkOpContourHead* contourList, SkChunkAlloc* allocator) { |
| 406 | SkOpContour* contour = contourList; |
| 407 | do { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 408 | contour->calcAngles(allocator); |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 409 | } while ((contour = contour->next())); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 410 | } |
| 411 | |
caryclark | d434972 | 2015-07-23 12:40:22 -0700 | [diff] [blame] | 412 | static void findCollapsed(SkOpContourHead* contourList) { |
| 413 | SkOpContour* contour = contourList; |
| 414 | do { |
| 415 | contour->findCollapsed(); |
| 416 | } while ((contour = contour->next())); |
| 417 | } |
| 418 | |
caryclark | 27c8eb8 | 2015-07-06 11:38:33 -0700 | [diff] [blame] | 419 | static bool missingCoincidence(SkOpContourHead* contourList, |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 420 | SkOpCoincidence* coincidence, SkChunkAlloc* allocator) { |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 421 | SkOpContour* contour = contourList; |
caryclark | 27c8eb8 | 2015-07-06 11:38:33 -0700 | [diff] [blame] | 422 | bool result = false; |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 423 | do { |
caryclark | 27c8eb8 | 2015-07-06 11:38:33 -0700 | [diff] [blame] | 424 | result |= contour->missingCoincidence(coincidence, allocator); |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 425 | } while ((contour = contour->next())); |
caryclark | 27c8eb8 | 2015-07-06 11:38:33 -0700 | [diff] [blame] | 426 | return result; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 427 | } |
| 428 | |
caryclark | d78c088 | 2016-02-24 09:03:07 -0800 | [diff] [blame] | 429 | static bool moveMultiples(SkOpContourHead* contourList) { |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 430 | SkOpContour* contour = contourList; |
| 431 | do { |
caryclark | d78c088 | 2016-02-24 09:03:07 -0800 | [diff] [blame] | 432 | if (!contour->moveMultiples()) { |
| 433 | return false; |
| 434 | } |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 435 | } while ((contour = contour->next())); |
caryclark | d78c088 | 2016-02-24 09:03:07 -0800 | [diff] [blame] | 436 | return true; |
caryclark | 08bc848 | 2015-04-24 09:08:57 -0700 | [diff] [blame] | 437 | } |
| 438 | |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 439 | static void moveNearby(SkOpContourHead* contourList) { |
| 440 | SkOpContour* contour = contourList; |
| 441 | do { |
caryclark | 08bc848 | 2015-04-24 09:08:57 -0700 | [diff] [blame] | 442 | contour->moveNearby(); |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 443 | } while ((contour = contour->next())); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 444 | } |
| 445 | |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 446 | static void sortAngles(SkOpContourHead* contourList) { |
| 447 | SkOpContour* contour = contourList; |
| 448 | do { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 449 | contour->sortAngles(); |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 450 | } while ((contour = contour->next())); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 451 | } |
| 452 | |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 453 | bool HandleCoincidence(SkOpContourHead* contourList, SkOpCoincidence* coincidence, |
| 454 | SkChunkAlloc* allocator) { |
| 455 | SkOpGlobalState* globalState = contourList->globalState(); |
caryclark | 08bc848 | 2015-04-24 09:08:57 -0700 | [diff] [blame] | 456 | // combine t values when multiple intersections occur on some segments but not others |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 457 | DEBUG_COINCIDENCE_HEALTH(contourList, "start"); |
caryclark | d78c088 | 2016-02-24 09:03:07 -0800 | [diff] [blame] | 458 | if (!moveMultiples(contourList)) { |
| 459 | return false; |
| 460 | } |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 461 | DEBUG_COINCIDENCE_HEALTH(contourList, "moveMultiples"); |
caryclark | d434972 | 2015-07-23 12:40:22 -0700 | [diff] [blame] | 462 | findCollapsed(contourList); |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 463 | DEBUG_COINCIDENCE_HEALTH(contourList, "findCollapsed"); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 464 | // move t values and points together to eliminate small/tiny gaps |
caryclark | 08bc848 | 2015-04-24 09:08:57 -0700 | [diff] [blame] | 465 | moveNearby(contourList); |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 466 | DEBUG_COINCIDENCE_HEALTH(contourList, "moveNearby"); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 467 | align(contourList); // give all span members common values |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 468 | DEBUG_COINCIDENCE_HEALTH(contourList, "align"); |
caryclark | 27c8eb8 | 2015-07-06 11:38:33 -0700 | [diff] [blame] | 469 | coincidence->fixAligned(); // aligning may have marked a coincidence pt-t deleted |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 470 | DEBUG_COINCIDENCE_HEALTH(contourList, "fixAligned"); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 471 | #if DEBUG_VALIDATE |
| 472 | globalState->setPhase(SkOpGlobalState::kIntersecting); |
caryclark@google.com | a2bbc6e | 2013-11-01 17:36:03 +0000 | [diff] [blame] | 473 | #endif |
caryclark | 27c8eb8 | 2015-07-06 11:38:33 -0700 | [diff] [blame] | 474 | // look for intersections on line segments formed by moving end points |
| 475 | addAlignIntersections(contourList, allocator); |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 476 | DEBUG_COINCIDENCE_HEALTH(contourList, "addAlignIntersections"); |
| 477 | if (coincidence->addMissing(allocator)) { |
| 478 | DEBUG_COINCIDENCE_HEALTH(contourList, "addMissing"); |
| 479 | moveNearby(contourList); |
| 480 | DEBUG_COINCIDENCE_HEALTH(contourList, "moveNearby2"); |
| 481 | align(contourList); // give all span members common values |
| 482 | DEBUG_COINCIDENCE_HEALTH(contourList, "align2"); |
| 483 | coincidence->fixAligned(); // aligning may have marked a coincidence pt-t deleted |
| 484 | DEBUG_COINCIDENCE_HEALTH(contourList, "fixAligned2"); |
| 485 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 486 | #if DEBUG_VALIDATE |
| 487 | globalState->setPhase(SkOpGlobalState::kWalking); |
| 488 | #endif |
caryclark | 27c8eb8 | 2015-07-06 11:38:33 -0700 | [diff] [blame] | 489 | // check to see if, loosely, coincident ranges may be expanded |
| 490 | if (coincidence->expand()) { |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 491 | DEBUG_COINCIDENCE_HEALTH(contourList, "expand1"); |
| 492 | if (!coincidence->addExpanded(allocator PATH_OPS_DEBUG_VALIDATE_PARAMS(globalState))) { |
| 493 | return false; |
| 494 | } |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 495 | } |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 496 | DEBUG_COINCIDENCE_HEALTH(contourList, "expand2"); |
caryclark | 27c8eb8 | 2015-07-06 11:38:33 -0700 | [diff] [blame] | 497 | // the expanded ranges may not align -- add the missing spans |
caryclark | 5c5cfe2 | 2016-04-05 07:28:48 -0700 | [diff] [blame] | 498 | if (!coincidence->mark()) { // mark spans of coincident segments as coincident |
| 499 | return false; |
| 500 | } |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 501 | DEBUG_COINCIDENCE_HEALTH(contourList, "mark1"); |
caryclark | 27c8eb8 | 2015-07-06 11:38:33 -0700 | [diff] [blame] | 502 | // look for coincidence missed earlier |
| 503 | if (missingCoincidence(contourList, coincidence, allocator)) { |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 504 | DEBUG_COINCIDENCE_HEALTH(contourList, "missingCoincidence1"); |
caryclark | 27c8eb8 | 2015-07-06 11:38:33 -0700 | [diff] [blame] | 505 | (void) coincidence->expand(); |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 506 | DEBUG_COINCIDENCE_HEALTH(contourList, "expand3"); |
| 507 | if (!coincidence->addExpanded(allocator PATH_OPS_DEBUG_VALIDATE_PARAMS(globalState))) { |
| 508 | return false; |
| 509 | } |
| 510 | DEBUG_COINCIDENCE_HEALTH(contourList, "addExpanded2"); |
caryclark | 27c8eb8 | 2015-07-06 11:38:33 -0700 | [diff] [blame] | 511 | coincidence->mark(); |
| 512 | } |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 513 | DEBUG_COINCIDENCE_HEALTH(contourList, "missingCoincidence2"); |
caryclark | 27c8eb8 | 2015-07-06 11:38:33 -0700 | [diff] [blame] | 514 | SkOpCoincidence overlaps; |
| 515 | do { |
| 516 | SkOpCoincidence* pairs = overlaps.isEmpty() ? coincidence : &overlaps; |
| 517 | if (!pairs->apply()) { // adjust the winding value to account for coincident edges |
| 518 | return false; |
| 519 | } |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 520 | DEBUG_COINCIDENCE_HEALTH(contourList, "pairs->apply"); |
caryclark | 27c8eb8 | 2015-07-06 11:38:33 -0700 | [diff] [blame] | 521 | // For each coincident pair that overlaps another, when the receivers (the 1st of the pair) |
| 522 | // are different, construct a new pair to resolve their mutual span |
| 523 | pairs->findOverlaps(&overlaps, allocator); |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 524 | DEBUG_COINCIDENCE_HEALTH(contourList, "pairs->findOverlaps"); |
caryclark | 27c8eb8 | 2015-07-06 11:38:33 -0700 | [diff] [blame] | 525 | } while (!overlaps.isEmpty()); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 526 | calcAngles(contourList, allocator); |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 527 | sortAngles(contourList); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 528 | if (globalState->angleCoincidence()) { |
caryclark | 27c8eb8 | 2015-07-06 11:38:33 -0700 | [diff] [blame] | 529 | (void) missingCoincidence(contourList, coincidence, allocator); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 530 | if (!coincidence->apply()) { |
| 531 | return false; |
| 532 | } |
| 533 | } |
| 534 | #if DEBUG_ACTIVE_SPANS |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 535 | coincidence->debugShowCoincidence(); |
| 536 | DebugShowActiveSpans(contourList); |
caryclark@google.com | a2bbc6e | 2013-11-01 17:36:03 +0000 | [diff] [blame] | 537 | #endif |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 538 | return true; |
caryclark@google.com | a2bbc6e | 2013-11-01 17:36:03 +0000 | [diff] [blame] | 539 | } |