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" |
| 8 | #include "SkOpEdgeBuilder.h" |
| 9 | #include "SkPathOpsCommon.h" |
| 10 | #include "SkPathWriter.h" |
| 11 | |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame] | 12 | static bool bridgeWinding(SkTArray<SkOpContour*, true>& contourList, SkPathWriter* simple) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 13 | bool firstContour = true; |
| 14 | bool unsortable = false; |
| 15 | bool topUnsortable = false; |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 16 | bool firstPass = true; |
| 17 | SkPoint lastTopLeft; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 18 | SkPoint topLeft = {SK_ScalarMin, SK_ScalarMin}; |
| 19 | do { |
| 20 | int index, endIndex; |
| 21 | bool topDone; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame^] | 22 | bool onlyVertical = false; |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 23 | lastTopLeft = topLeft; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 24 | SkOpSegment* current = FindSortableTop(contourList, SkOpAngle::kUnaryWinding, &firstContour, |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame^] | 25 | &index, &endIndex, &topLeft, &topUnsortable, &topDone, &onlyVertical, firstPass); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 26 | if (!current) { |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 27 | if ((!topUnsortable || firstPass) && !topDone) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 28 | SkASSERT(topLeft.fX != SK_ScalarMin && topLeft.fY != SK_ScalarMin); |
| 29 | topLeft.fX = topLeft.fY = SK_ScalarMin; |
| 30 | continue; |
| 31 | } |
| 32 | break; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame^] | 33 | } else if (onlyVertical) { |
| 34 | break; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 35 | } |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 36 | firstPass = !topUnsortable || lastTopLeft != topLeft; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame^] | 37 | SkTDArray<SkOpSpan*> chase; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 38 | do { |
| 39 | if (current->activeWinding(index, endIndex)) { |
| 40 | do { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 41 | if (!unsortable && current->done()) { |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame^] | 42 | break; |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 43 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 44 | SkASSERT(unsortable || !current->done()); |
| 45 | int nextStart = index; |
| 46 | int nextEnd = endIndex; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame^] | 47 | SkOpSegment* next = current->findNextWinding(&chase, &nextStart, &nextEnd, |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 48 | &unsortable); |
| 49 | if (!next) { |
| 50 | if (!unsortable && simple->hasMove() |
| 51 | && current->verb() != SkPath::kLine_Verb |
| 52 | && !simple->isClosed()) { |
| 53 | current->addCurveTo(index, endIndex, simple, true); |
| 54 | SkASSERT(simple->isClosed()); |
| 55 | } |
| 56 | break; |
| 57 | } |
| 58 | #if DEBUG_FLOW |
| 59 | SkDebugf("%s current id=%d from=(%1.9g,%1.9g) to=(%1.9g,%1.9g)\n", __FUNCTION__, |
| 60 | current->debugID(), current->xyAtT(index).fX, current->xyAtT(index).fY, |
| 61 | current->xyAtT(endIndex).fX, current->xyAtT(endIndex).fY); |
| 62 | #endif |
| 63 | current->addCurveTo(index, endIndex, simple, true); |
| 64 | current = next; |
| 65 | index = nextStart; |
| 66 | endIndex = nextEnd; |
| 67 | } while (!simple->isClosed() && (!unsortable |
| 68 | || !current->done(SkMin32(index, endIndex)))); |
| 69 | if (current->activeWinding(index, endIndex) && !simple->isClosed()) { |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame^] | 70 | // SkASSERT(unsortable || simple->isEmpty()); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 71 | int min = SkMin32(index, endIndex); |
| 72 | if (!current->done(min)) { |
| 73 | current->addCurveTo(index, endIndex, simple, true); |
| 74 | current->markDoneUnary(min); |
| 75 | } |
| 76 | } |
| 77 | simple->close(); |
| 78 | } else { |
| 79 | SkOpSpan* last = current->markAndChaseDoneUnary(index, endIndex); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 80 | if (last && !last->fChased && !last->fLoop) { |
| 81 | last->fChased = true; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame^] | 82 | SkASSERT(!SkPathOpsDebug::ChaseContains(chase, last)); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 83 | // assert that last isn't already in array |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame^] | 84 | *chase.append() = last; |
| 85 | #if DEBUG_WINDING |
| 86 | SkDebugf("%s chase.append id=%d windSum=%d small=%d\n", __FUNCTION__, |
| 87 | last->fOther->span(last->fOtherIndex).fOther->debugID(), last->fWindSum, |
| 88 | last->fSmall); |
| 89 | #endif |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame^] | 92 | current = FindChase(&chase, &index, &endIndex); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 93 | #if DEBUG_ACTIVE_SPANS |
| 94 | DebugShowActiveSpans(contourList); |
| 95 | #endif |
| 96 | if (!current) { |
| 97 | break; |
| 98 | } |
| 99 | } while (true); |
| 100 | } while (true); |
| 101 | return simple->someAssemblyRequired(); |
| 102 | } |
| 103 | |
| 104 | // returns true if all edges were processed |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame] | 105 | static bool bridgeXor(SkTArray<SkOpContour*, true>& contourList, SkPathWriter* simple) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 106 | SkOpSegment* current; |
| 107 | int start, end; |
| 108 | bool unsortable = false; |
| 109 | bool closable = true; |
| 110 | while ((current = FindUndone(contourList, &start, &end))) { |
| 111 | do { |
| 112 | #if DEBUG_ACTIVE_SPANS |
| 113 | if (!unsortable && current->done()) { |
| 114 | DebugShowActiveSpans(contourList); |
| 115 | } |
| 116 | #endif |
| 117 | SkASSERT(unsortable || !current->done()); |
| 118 | int nextStart = start; |
| 119 | int nextEnd = end; |
| 120 | SkOpSegment* next = current->findNextXor(&nextStart, &nextEnd, &unsortable); |
| 121 | if (!next) { |
| 122 | if (!unsortable && simple->hasMove() |
| 123 | && current->verb() != SkPath::kLine_Verb |
| 124 | && !simple->isClosed()) { |
| 125 | current->addCurveTo(start, end, simple, true); |
| 126 | SkASSERT(simple->isClosed()); |
| 127 | } |
| 128 | break; |
| 129 | } |
| 130 | #if DEBUG_FLOW |
| 131 | SkDebugf("%s current id=%d from=(%1.9g,%1.9g) to=(%1.9g,%1.9g)\n", __FUNCTION__, |
| 132 | current->debugID(), current->xyAtT(start).fX, current->xyAtT(start).fY, |
| 133 | current->xyAtT(end).fX, current->xyAtT(end).fY); |
| 134 | #endif |
| 135 | current->addCurveTo(start, end, simple, true); |
| 136 | current = next; |
| 137 | start = nextStart; |
| 138 | end = nextEnd; |
| 139 | } while (!simple->isClosed() && (!unsortable || !current->done(SkMin32(start, end)))); |
| 140 | if (!simple->isClosed()) { |
| 141 | SkASSERT(unsortable); |
| 142 | int min = SkMin32(start, end); |
| 143 | if (!current->done(min)) { |
| 144 | current->addCurveTo(start, end, simple, true); |
| 145 | current->markDone(min, 1); |
| 146 | } |
| 147 | closable = false; |
| 148 | } |
| 149 | simple->close(); |
| 150 | #if DEBUG_ACTIVE_SPANS |
| 151 | DebugShowActiveSpans(contourList); |
| 152 | #endif |
| 153 | } |
| 154 | return closable; |
| 155 | } |
| 156 | |
| 157 | // FIXME : add this as a member of SkPath |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 158 | bool Simplify(const SkPath& path, SkPath* result) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 159 | #if DEBUG_SORT || DEBUG_SWAP_TOP |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 160 | SkPathOpsDebug::gSortCount = SkPathOpsDebug::gSortCountDefault; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 161 | #endif |
| 162 | // returns 1 for evenodd, -1 for winding, regardless of inverse-ness |
caryclark@google.com | 7dfbb07 | 2013-04-22 14:37:05 +0000 | [diff] [blame] | 163 | SkPath::FillType fillType = path.isInverseFillType() ? SkPath::kInverseEvenOdd_FillType |
| 164 | : SkPath::kEvenOdd_FillType; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 165 | |
| 166 | // turn path into list of segments |
| 167 | SkTArray<SkOpContour> contours; |
| 168 | SkOpEdgeBuilder builder(path, contours); |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 169 | if (!builder.finish()) { |
| 170 | return false; |
| 171 | } |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame] | 172 | SkTArray<SkOpContour*, true> contourList; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 173 | MakeContourList(contours, contourList, false, false); |
| 174 | SkOpContour** currentPtr = contourList.begin(); |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 175 | result->reset(); |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame] | 176 | result->setFillType(fillType); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 177 | if (!currentPtr) { |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 178 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 179 | } |
| 180 | SkOpContour** listEnd = contourList.end(); |
| 181 | // find all intersections between segments |
| 182 | do { |
| 183 | SkOpContour** nextPtr = currentPtr; |
| 184 | SkOpContour* current = *currentPtr++; |
| 185 | if (current->containsCubics()) { |
| 186 | AddSelfIntersectTs(current); |
| 187 | } |
| 188 | SkOpContour* next; |
| 189 | do { |
| 190 | next = *nextPtr++; |
| 191 | } while (AddIntersectTs(current, next) && nextPtr != listEnd); |
| 192 | } while (currentPtr != listEnd); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 193 | if (!HandleCoincidence(&contourList, 0)) { |
| 194 | return false; |
| 195 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 196 | // construct closed contours |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 197 | SkPathWriter simple(*result); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 198 | if (builder.xorMask() == kWinding_PathOpsMask ? bridgeWinding(contourList, &simple) |
| 199 | : !bridgeXor(contourList, &simple)) |
| 200 | { // if some edges could not be resolved, assemble remaining fragments |
| 201 | SkPath temp; |
caryclark@google.com | 7dfbb07 | 2013-04-22 14:37:05 +0000 | [diff] [blame] | 202 | temp.setFillType(fillType); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 203 | SkPathWriter assembled(temp); |
| 204 | Assemble(simple, &assembled); |
| 205 | *result = *assembled.nativePath(); |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 206 | result->setFillType(fillType); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 207 | } |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 208 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 209 | } |