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; |
| 16 | SkPoint topLeft = {SK_ScalarMin, SK_ScalarMin}; |
| 17 | do { |
| 18 | int index, endIndex; |
| 19 | bool topDone; |
| 20 | SkOpSegment* current = FindSortableTop(contourList, &firstContour, &index, &endIndex, |
| 21 | &topLeft, &topUnsortable, &topDone, false); |
| 22 | if (!current) { |
| 23 | if (topUnsortable || !topDone) { |
| 24 | topUnsortable = false; |
| 25 | SkASSERT(topLeft.fX != SK_ScalarMin && topLeft.fY != SK_ScalarMin); |
| 26 | topLeft.fX = topLeft.fY = SK_ScalarMin; |
| 27 | continue; |
| 28 | } |
| 29 | break; |
| 30 | } |
| 31 | SkTDArray<SkOpSpan*> chaseArray; |
| 32 | do { |
| 33 | if (current->activeWinding(index, endIndex)) { |
| 34 | do { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 35 | if (!unsortable && current->done()) { |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 36 | #if DEBUG_ACTIVE_SPANS |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 37 | DebugShowActiveSpans(contourList); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 38 | #endif |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 39 | if (simple->isEmpty()) { |
| 40 | simple->init(); |
| 41 | break; |
| 42 | } |
| 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; |
| 47 | SkOpSegment* next = current->findNextWinding(&chaseArray, &nextStart, &nextEnd, |
| 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@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [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); |
| 80 | if (last && !last->fLoop) { |
| 81 | *chaseArray.append() = last; |
| 82 | } |
| 83 | } |
| 84 | current = FindChase(chaseArray, index, endIndex); |
| 85 | #if DEBUG_ACTIVE_SPANS |
| 86 | DebugShowActiveSpans(contourList); |
| 87 | #endif |
| 88 | if (!current) { |
| 89 | break; |
| 90 | } |
| 91 | } while (true); |
| 92 | } while (true); |
| 93 | return simple->someAssemblyRequired(); |
| 94 | } |
| 95 | |
| 96 | // returns true if all edges were processed |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame] | 97 | static bool bridgeXor(SkTArray<SkOpContour*, true>& contourList, SkPathWriter* simple) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 98 | SkOpSegment* current; |
| 99 | int start, end; |
| 100 | bool unsortable = false; |
| 101 | bool closable = true; |
| 102 | while ((current = FindUndone(contourList, &start, &end))) { |
| 103 | do { |
| 104 | #if DEBUG_ACTIVE_SPANS |
| 105 | if (!unsortable && current->done()) { |
| 106 | DebugShowActiveSpans(contourList); |
| 107 | } |
| 108 | #endif |
| 109 | SkASSERT(unsortable || !current->done()); |
| 110 | int nextStart = start; |
| 111 | int nextEnd = end; |
| 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()) { |
| 117 | current->addCurveTo(start, end, simple, true); |
| 118 | SkASSERT(simple->isClosed()); |
| 119 | } |
| 120 | break; |
| 121 | } |
| 122 | #if DEBUG_FLOW |
| 123 | SkDebugf("%s current id=%d from=(%1.9g,%1.9g) to=(%1.9g,%1.9g)\n", __FUNCTION__, |
| 124 | current->debugID(), current->xyAtT(start).fX, current->xyAtT(start).fY, |
| 125 | current->xyAtT(end).fX, current->xyAtT(end).fY); |
| 126 | #endif |
| 127 | current->addCurveTo(start, end, simple, true); |
| 128 | current = next; |
| 129 | start = nextStart; |
| 130 | end = nextEnd; |
| 131 | } while (!simple->isClosed() && (!unsortable || !current->done(SkMin32(start, end)))); |
| 132 | if (!simple->isClosed()) { |
| 133 | SkASSERT(unsortable); |
| 134 | int min = SkMin32(start, end); |
| 135 | if (!current->done(min)) { |
| 136 | current->addCurveTo(start, end, simple, true); |
| 137 | current->markDone(min, 1); |
| 138 | } |
| 139 | closable = false; |
| 140 | } |
| 141 | simple->close(); |
| 142 | #if DEBUG_ACTIVE_SPANS |
| 143 | DebugShowActiveSpans(contourList); |
| 144 | #endif |
| 145 | } |
| 146 | return closable; |
| 147 | } |
| 148 | |
| 149 | // FIXME : add this as a member of SkPath |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 150 | bool Simplify(const SkPath& path, SkPath* result) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 151 | #if DEBUG_SORT || DEBUG_SWAP_TOP |
| 152 | gDebugSortCount = gDebugSortCountDefault; |
| 153 | #endif |
| 154 | // returns 1 for evenodd, -1 for winding, regardless of inverse-ness |
caryclark@google.com | 7dfbb07 | 2013-04-22 14:37:05 +0000 | [diff] [blame] | 155 | SkPath::FillType fillType = path.isInverseFillType() ? SkPath::kInverseEvenOdd_FillType |
| 156 | : SkPath::kEvenOdd_FillType; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 157 | |
| 158 | // turn path into list of segments |
| 159 | SkTArray<SkOpContour> contours; |
| 160 | SkOpEdgeBuilder builder(path, contours); |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 161 | if (!builder.finish()) { |
| 162 | return false; |
| 163 | } |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame] | 164 | SkTArray<SkOpContour*, true> contourList; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 165 | MakeContourList(contours, contourList, false, false); |
| 166 | SkOpContour** currentPtr = contourList.begin(); |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 167 | result->reset(); |
bungeman@google.com | a5809a3 | 2013-06-21 15:13:34 +0000 | [diff] [blame^] | 168 | result->setFillType(fillType); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 169 | if (!currentPtr) { |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 170 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 171 | } |
| 172 | SkOpContour** listEnd = contourList.end(); |
| 173 | // find all intersections between segments |
| 174 | do { |
| 175 | SkOpContour** nextPtr = currentPtr; |
| 176 | SkOpContour* current = *currentPtr++; |
| 177 | if (current->containsCubics()) { |
| 178 | AddSelfIntersectTs(current); |
| 179 | } |
| 180 | SkOpContour* next; |
| 181 | do { |
| 182 | next = *nextPtr++; |
| 183 | } while (AddIntersectTs(current, next) && nextPtr != listEnd); |
| 184 | } while (currentPtr != listEnd); |
| 185 | // eat through coincident edges |
| 186 | CoincidenceCheck(&contourList, 0); |
| 187 | FixOtherTIndex(&contourList); |
| 188 | SortSegments(&contourList); |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 189 | #if DEBUG_ACTIVE_SPANS || DEBUG_ACTIVE_SPANS_FIRST_ONLY |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 190 | DebugShowActiveSpans(contourList); |
| 191 | #endif |
| 192 | // construct closed contours |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 193 | SkPathWriter simple(*result); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 194 | if (builder.xorMask() == kWinding_PathOpsMask ? bridgeWinding(contourList, &simple) |
| 195 | : !bridgeXor(contourList, &simple)) |
| 196 | { // if some edges could not be resolved, assemble remaining fragments |
| 197 | SkPath temp; |
caryclark@google.com | 7dfbb07 | 2013-04-22 14:37:05 +0000 | [diff] [blame] | 198 | temp.setFillType(fillType); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 199 | SkPathWriter assembled(temp); |
| 200 | Assemble(simple, &assembled); |
| 201 | *result = *assembled.nativePath(); |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 202 | result->setFillType(fillType); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 203 | } |
caryclark@google.com | 66560ca | 2013-04-26 19:51:16 +0000 | [diff] [blame] | 204 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 205 | } |