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 | */ |
| 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" |
| 12 | |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame^] | 13 | static bool bridgeWinding(SkOpContourHead* contourList, SkPathWriter* simple) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 14 | bool unsortable = false; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 15 | do { |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 16 | SkOpSpan* span = FindSortableTop(contourList); |
| 17 | if (!span) { |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 18 | break; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 19 | } |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 20 | SkOpSegment* current = span->segment(); |
| 21 | SkOpSpanBase* start = span->next(); |
| 22 | SkOpSpanBase* end = span; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 23 | SkTDArray<SkOpSpanBase*> chase; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 24 | do { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 25 | if (current->activeWinding(start, end)) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 26 | do { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 27 | if (!unsortable && current->done()) { |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 28 | break; |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 29 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 30 | SkASSERT(unsortable || !current->done()); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 31 | SkOpSpanBase* nextStart = start; |
| 32 | SkOpSpanBase* nextEnd = end; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 33 | SkOpSegment* next = current->findNextWinding(&chase, &nextStart, &nextEnd, |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 34 | &unsortable); |
| 35 | if (!next) { |
| 36 | if (!unsortable && simple->hasMove() |
| 37 | && current->verb() != SkPath::kLine_Verb |
| 38 | && !simple->isClosed()) { |
caryclark | 81a478c | 2016-09-09 09:37:57 -0700 | [diff] [blame] | 39 | // FIXME: put in the next two lines to avoid handling already added |
| 40 | if (start->starter(end)->checkAlreadyAdded()) { |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame^] | 41 | simple->finishContour(); |
caryclark | 81a478c | 2016-09-09 09:37:57 -0700 | [diff] [blame] | 42 | } else if (!current->addCurveTo(start, end, simple)) { |
caryclark | ef784fb | 2015-10-30 12:03:06 -0700 | [diff] [blame] | 43 | return false; |
| 44 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 45 | if (!simple->isClosed()) { |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 46 | SkPathOpsDebug::ShowActiveSpans(contourList); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 47 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 48 | } |
| 49 | break; |
| 50 | } |
| 51 | #if DEBUG_FLOW |
| 52 | SkDebugf("%s current id=%d from=(%1.9g,%1.9g) to=(%1.9g,%1.9g)\n", __FUNCTION__, |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 53 | current->debugID(), start->pt().fX, start->pt().fY, |
| 54 | end->pt().fX, end->pt().fY); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 55 | #endif |
caryclark | ef784fb | 2015-10-30 12:03:06 -0700 | [diff] [blame] | 56 | if (!current->addCurveTo(start, end, simple)) { |
| 57 | return false; |
| 58 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 59 | current = next; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 60 | start = nextStart; |
| 61 | end = nextEnd; |
| 62 | } while (!simple->isClosed() && (!unsortable || !start->starter(end)->done())); |
| 63 | if (current->activeWinding(start, end) && !simple->isClosed()) { |
| 64 | SkOpSpan* spanStart = start->starter(end); |
| 65 | if (!spanStart->done()) { |
caryclark | ef784fb | 2015-10-30 12:03:06 -0700 | [diff] [blame] | 66 | if (!current->addCurveTo(start, end, simple)) { |
| 67 | return false; |
| 68 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 69 | current->markDone(spanStart); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame^] | 72 | simple->finishContour(); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 73 | } else { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 74 | SkOpSpanBase* last = current->markAndChaseDone(start, end); |
| 75 | if (last && !last->chased()) { |
| 76 | last->setChased(true); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 77 | SkASSERT(!SkPathOpsDebug::ChaseContains(chase, last)); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 78 | *chase.append() = last; |
| 79 | #if DEBUG_WINDING |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 80 | SkDebugf("%s chase.append id=%d", __FUNCTION__, last->segment()->debugID()); |
| 81 | if (!last->final()) { |
| 82 | SkDebugf(" windSum=%d", last->upCast()->windSum()); |
| 83 | } |
| 84 | SkDebugf("\n"); |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 85 | #endif |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 88 | current = FindChase(&chase, &start, &end); |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 89 | SkPathOpsDebug::ShowActiveSpans(contourList); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 90 | if (!current) { |
| 91 | break; |
| 92 | } |
| 93 | } while (true); |
| 94 | } while (true); |
caryclark | ef784fb | 2015-10-30 12:03:06 -0700 | [diff] [blame] | 95 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // returns true if all edges were processed |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame^] | 99 | static bool bridgeXor(SkOpContourHead* contourList, SkPathWriter* simple) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 100 | SkOpSegment* current; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 101 | SkOpSpanBase* start; |
| 102 | SkOpSpanBase* end; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 103 | bool unsortable = false; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 104 | while ((current = FindUndone(contourList, &start, &end))) { |
| 105 | do { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 106 | if (!unsortable && current->done()) { |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 107 | SkPathOpsDebug::ShowActiveSpans(contourList); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 108 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 109 | SkASSERT(unsortable || !current->done()); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 110 | SkOpSpanBase* nextStart = start; |
| 111 | SkOpSpanBase* nextEnd = end; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 112 | SkOpSegment* next = current->findNextXor(&nextStart, &nextEnd, &unsortable); |
| 113 | if (!next) { |
| 114 | if (!unsortable && simple->hasMove() |
| 115 | && current->verb() != SkPath::kLine_Verb |
| 116 | && !simple->isClosed()) { |
caryclark | ef784fb | 2015-10-30 12:03:06 -0700 | [diff] [blame] | 117 | if (!current->addCurveTo(start, end, simple)) { |
| 118 | return false; |
| 119 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 120 | if (!simple->isClosed()) { |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 121 | SkPathOpsDebug::ShowActiveSpans(contourList); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 122 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 123 | } |
| 124 | break; |
| 125 | } |
| 126 | #if DEBUG_FLOW |
| 127 | SkDebugf("%s current id=%d from=(%1.9g,%1.9g) to=(%1.9g,%1.9g)\n", __FUNCTION__, |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 128 | current->debugID(), start->pt().fX, start->pt().fY, |
| 129 | end->pt().fX, end->pt().fY); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 130 | #endif |
caryclark | ef784fb | 2015-10-30 12:03:06 -0700 | [diff] [blame] | 131 | if (!current->addCurveTo(start, end, simple)) { |
| 132 | return false; |
| 133 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 134 | current = next; |
| 135 | start = nextStart; |
| 136 | end = nextEnd; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 137 | } while (!simple->isClosed() && (!unsortable || !start->starter(end)->done())); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 138 | if (!simple->isClosed()) { |
| 139 | SkASSERT(unsortable); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 140 | SkOpSpan* spanStart = start->starter(end); |
| 141 | if (!spanStart->done()) { |
caryclark | ef784fb | 2015-10-30 12:03:06 -0700 | [diff] [blame] | 142 | if (!current->addCurveTo(start, end, simple)) { |
| 143 | return false; |
| 144 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 145 | current->markDone(spanStart); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 146 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 147 | } |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame^] | 148 | simple->finishContour(); |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 149 | SkPathOpsDebug::ShowActiveSpans(contourList); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 150 | } |
caryclark | ef784fb | 2015-10-30 12:03:06 -0700 | [diff] [blame] | 151 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // FIXME : add this as a member of SkPath |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 155 | bool SimplifyDebug(const SkPath& path, SkPath* result |
| 156 | SkDEBUGPARAMS(bool skipAssert) SkDEBUGPARAMS(const char* testName)) { |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 157 | // returns 1 for evenodd, -1 for winding, regardless of inverse-ness |
| 158 | SkPath::FillType fillType = path.isInverseFillType() ? SkPath::kInverseEvenOdd_FillType |
| 159 | : SkPath::kEvenOdd_FillType; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 160 | if (path.isConvex()) { |
| 161 | if (result != &path) { |
| 162 | *result = path; |
| 163 | } |
| 164 | result->setFillType(fillType); |
| 165 | return true; |
| 166 | } |
reed | 0dc4dd6 | 2015-03-24 13:55:33 -0700 | [diff] [blame] | 167 | // turn path into list of segments |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 168 | SkChunkAlloc allocator(4096); // FIXME: constant-ize, tune |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 169 | SkOpContour contour; |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 170 | SkOpContourHead* contourList = static_cast<SkOpContourHead*>(&contour); |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 171 | SkOpGlobalState globalState(contourList, &allocator |
| 172 | SkDEBUGPARAMS(skipAssert) SkDEBUGPARAMS(testName)); |
| 173 | SkOpCoincidence coincidence(&globalState); |
| 174 | SkScalar scaleFactor = ScaleFactor(path); |
| 175 | SkPath scaledPath; |
| 176 | const SkPath* workingPath; |
| 177 | if (scaleFactor > SK_Scalar1) { |
| 178 | ScalePath(path, 1.f / scaleFactor, &scaledPath); |
| 179 | workingPath = &scaledPath; |
| 180 | } else { |
| 181 | workingPath = &path; |
| 182 | } |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 183 | #if DEBUG_SORT |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 184 | SkPathOpsDebug::gSortCount = SkPathOpsDebug::gSortCountDefault; |
| 185 | #endif |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame^] | 186 | SkOpEdgeBuilder builder(*workingPath, contourList, &globalState); |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 187 | if (!builder.finish()) { |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 188 | return false; |
| 189 | } |
caryclark | 03b03ca | 2015-04-23 09:13:37 -0700 | [diff] [blame] | 190 | #if DEBUG_DUMP_SEGMENTS |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 191 | contour.dumpSegments(); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 192 | #endif |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 193 | if (!SortContourList(&contourList, false, false)) { |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 194 | result->reset(); |
| 195 | result->setFillType(fillType); |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 196 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 197 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 198 | // find all intersections between segments |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 199 | SkOpContour* current = contourList; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 200 | do { |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 201 | SkOpContour* next = current; |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 202 | while (AddIntersectTs(current, next, &coincidence) |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 203 | && (next = next->next())); |
| 204 | } while ((current = current->next())); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 205 | #if DEBUG_VALIDATE |
| 206 | globalState.setPhase(SkOpGlobalState::kWalking); |
| 207 | #endif |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 208 | if (!HandleCoincidence(contourList, &coincidence)) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 209 | return false; |
| 210 | } |
caryclark | 26ad22a | 2015-10-16 09:03:38 -0700 | [diff] [blame] | 211 | #if DEBUG_DUMP_ALIGNMENT |
| 212 | contour.dumpSegments("aligned"); |
| 213 | #endif |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 214 | // construct closed contours |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 215 | result->reset(); |
| 216 | result->setFillType(fillType); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 217 | SkPathWriter wrapper(*result); |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame^] | 218 | if (builder.xorMask() == kWinding_PathOpsMask ? !bridgeWinding(contourList, &wrapper) |
| 219 | : !bridgeXor(contourList, &wrapper)) { |
caryclark | ef784fb | 2015-10-30 12:03:06 -0700 | [diff] [blame] | 220 | return false; |
| 221 | } |
caryclark | eed356d | 2016-09-14 07:18:20 -0700 | [diff] [blame^] | 222 | wrapper.assemble(); // if some edges could not be resolved, assemble remaining |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 223 | if (scaleFactor > 1) { |
| 224 | ScalePath(*result, scaleFactor, result); |
| 225 | } |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 226 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 227 | } |
caryclark | 5b5ddd7 | 2015-05-18 05:12:56 -0700 | [diff] [blame] | 228 | |
caryclark | 55888e4 | 2016-07-18 10:01:36 -0700 | [diff] [blame] | 229 | bool Simplify(const SkPath& path, SkPath* result) { |
| 230 | return SimplifyDebug(path, result SkDEBUGPARAMS(true) SkDEBUGPARAMS(nullptr)); |
| 231 | } |