caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +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 | */ |
skia.committer@gmail.com | b0a327e | 2012-11-21 02:02:25 +0000 | [diff] [blame] | 7 | |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 8 | #include "Simplify.h" |
| 9 | |
| 10 | namespace Op { |
| 11 | |
caryclark@google.com | 7ba591e | 2012-11-20 14:21:54 +0000 | [diff] [blame] | 12 | #define INCLUDED_BY_SHAPE_OPS 1 |
| 13 | |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 14 | #include "Simplify.cpp" |
| 15 | |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 16 | // FIXME: this and find chase should be merge together, along with |
| 17 | // other code that walks winding in angles |
| 18 | // OPTIMIZATION: Probably, the walked winding should be rolled into the angle structure |
| 19 | // so it isn't duplicated by walkers like this one |
caryclark@google.com | 7fce0de | 2012-11-29 14:31:50 +0000 | [diff] [blame] | 20 | static Segment* findChaseOp(SkTDArray<Span*>& chase, int& nextStart, int& nextEnd) { |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 21 | while (chase.count()) { |
| 22 | Span* span; |
| 23 | chase.pop(&span); |
| 24 | const Span& backPtr = span->fOther->span(span->fOtherIndex); |
| 25 | Segment* segment = backPtr.fOther; |
caryclark@google.com | 7fce0de | 2012-11-29 14:31:50 +0000 | [diff] [blame] | 26 | nextStart = backPtr.fOtherIndex; |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 27 | SkTDArray<Angle> angles; |
| 28 | int done = 0; |
caryclark@google.com | 7fce0de | 2012-11-29 14:31:50 +0000 | [diff] [blame] | 29 | if (segment->activeAngle(nextStart, done, angles)) { |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 30 | Angle* last = angles.end() - 1; |
caryclark@google.com | 7fce0de | 2012-11-29 14:31:50 +0000 | [diff] [blame] | 31 | nextStart = last->start(); |
| 32 | nextEnd = last->end(); |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 33 | #if TRY_ROTATE |
| 34 | *chase.insert(0) = span; |
| 35 | #else |
| 36 | *chase.append() = span; |
| 37 | #endif |
| 38 | return last->segment(); |
| 39 | } |
| 40 | if (done == angles.count()) { |
| 41 | continue; |
| 42 | } |
| 43 | SkTDArray<Angle*> sorted; |
| 44 | bool sortable = Segment::SortAngles(angles, sorted); |
caryclark@google.com | 7fce0de | 2012-11-29 14:31:50 +0000 | [diff] [blame] | 45 | int angleCount = sorted.count(); |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 46 | #if DEBUG_SORT |
caryclark@google.com | 7fce0de | 2012-11-29 14:31:50 +0000 | [diff] [blame] | 47 | sorted[0]->segment()->debugShowSort(__FUNCTION__, sorted, 0); |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 48 | #endif |
| 49 | if (!sortable) { |
| 50 | continue; |
| 51 | } |
| 52 | // find first angle, initialize winding to computed fWindSum |
| 53 | int firstIndex = -1; |
| 54 | const Angle* angle; |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 55 | do { |
| 56 | angle = sorted[++firstIndex]; |
| 57 | segment = angle->segment(); |
caryclark@google.com | 7fce0de | 2012-11-29 14:31:50 +0000 | [diff] [blame] | 58 | } while (segment->windSum(angle) == SK_MinS32); |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 59 | #if DEBUG_SORT |
caryclark@google.com | 7fce0de | 2012-11-29 14:31:50 +0000 | [diff] [blame] | 60 | segment->debugShowSort(__FUNCTION__, sorted, firstIndex); |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 61 | #endif |
caryclark@google.com | 7fce0de | 2012-11-29 14:31:50 +0000 | [diff] [blame] | 62 | int sumMiWinding = segment->updateWindingReverse(angle); |
| 63 | int sumSuWinding = segment->updateOppWindingReverse(angle); |
| 64 | if (segment->operand()) { |
| 65 | SkTSwap<int>(sumMiWinding, sumSuWinding); |
| 66 | } |
| 67 | int nextIndex = firstIndex + 1; |
| 68 | int lastIndex = firstIndex != 0 ? firstIndex : angleCount; |
| 69 | Segment* first = NULL; |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 70 | do { |
| 71 | SkASSERT(nextIndex != firstIndex); |
| 72 | if (nextIndex == angleCount) { |
| 73 | nextIndex = 0; |
| 74 | } |
caryclark@google.com | 7fce0de | 2012-11-29 14:31:50 +0000 | [diff] [blame] | 75 | int maxWinding, sumWinding, oppMaxWinding, oppSumWinding; |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 76 | angle = sorted[nextIndex]; |
| 77 | segment = angle->segment(); |
caryclark@google.com | 7fce0de | 2012-11-29 14:31:50 +0000 | [diff] [blame] | 78 | int start = angle->start(); |
| 79 | int end = angle->end(); |
| 80 | segment->setUpWindings(start, end, sumMiWinding, sumSuWinding, |
| 81 | maxWinding, sumWinding, oppMaxWinding, oppSumWinding); |
| 82 | if (!segment->done(angle)) { |
| 83 | if (!first) { |
| 84 | first = segment; |
| 85 | nextStart = start; |
| 86 | nextEnd = end; |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 87 | } |
caryclark@google.com | 7fce0de | 2012-11-29 14:31:50 +0000 | [diff] [blame] | 88 | (void) segment->markAngle(maxWinding, sumWinding, oppMaxWinding, |
| 89 | oppSumWinding, true, angle); |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 90 | } |
| 91 | } while (++nextIndex != lastIndex); |
caryclark@google.com | 7fce0de | 2012-11-29 14:31:50 +0000 | [diff] [blame] | 92 | if (first) { |
| 93 | #if TRY_ROTATE |
| 94 | *chase.insert(0) = span; |
| 95 | #else |
| 96 | *chase.append() = span; |
| 97 | #endif |
| 98 | return first; |
| 99 | } |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 100 | } |
| 101 | return NULL; |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 102 | } |
| 103 | |
caryclark@google.com | 7fce0de | 2012-11-29 14:31:50 +0000 | [diff] [blame] | 104 | /* |
caryclark@google.com | 57cff8d | 2012-11-14 21:14:56 +0000 | [diff] [blame] | 105 | static bool windingIsActive(int winding, int oppWinding, int spanWinding, int oppSpanWinding, |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 106 | bool windingIsOp, ShapeOp op) { |
| 107 | bool active = windingIsActive(winding, spanWinding); |
| 108 | if (!active) { |
| 109 | return false; |
| 110 | } |
caryclark@google.com | 57cff8d | 2012-11-14 21:14:56 +0000 | [diff] [blame] | 111 | if (oppSpanWinding && windingIsActive(oppWinding, oppSpanWinding)) { |
caryclark@google.com | 7fce0de | 2012-11-29 14:31:50 +0000 | [diff] [blame] | 112 | switch (op) { |
| 113 | case kIntersect_Op: |
| 114 | case kUnion_Op: |
| 115 | return true; |
| 116 | case kDifference_Op: { |
| 117 | int absSpan = abs(spanWinding); |
| 118 | int absOpp = abs(oppSpanWinding); |
| 119 | return windingIsOp ? absSpan < absOpp : absSpan > absOpp; |
| 120 | } |
| 121 | case kXor_Op: |
| 122 | return spanWinding != oppSpanWinding; |
| 123 | default: |
| 124 | SkASSERT(0); |
| 125 | } |
caryclark@google.com | 57cff8d | 2012-11-14 21:14:56 +0000 | [diff] [blame] | 126 | } |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 127 | bool opActive = oppWinding != 0; |
| 128 | return gOpLookup[op][opActive][windingIsOp]; |
| 129 | } |
caryclark@google.com | 7fce0de | 2012-11-29 14:31:50 +0000 | [diff] [blame] | 130 | */ |
caryclark@google.com | 57cff8d | 2012-11-14 21:14:56 +0000 | [diff] [blame] | 131 | |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 132 | static bool bridgeOp(SkTDArray<Contour*>& contourList, const ShapeOp op, |
caryclark@google.com | 7fce0de | 2012-11-29 14:31:50 +0000 | [diff] [blame] | 133 | const int xorMask, const int xorOpMask, PathWrapper& simple) { |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 134 | bool firstContour = true; |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 135 | bool unsortable = false; |
| 136 | bool closable = true; |
caryclark@google.com | f839c03 | 2012-10-26 21:03:50 +0000 | [diff] [blame] | 137 | SkPoint topLeft = {SK_ScalarMin, SK_ScalarMin}; |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 138 | do { |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 139 | int index, endIndex; |
caryclark@google.com | f839c03 | 2012-10-26 21:03:50 +0000 | [diff] [blame] | 140 | Segment* current = findSortableTop(contourList, index, endIndex, topLeft); |
caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 141 | if (!current) { |
| 142 | break; |
| 143 | } |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 144 | if (firstContour) { |
caryclark@google.com | 7fce0de | 2012-11-29 14:31:50 +0000 | [diff] [blame] | 145 | current->initWinding(index, endIndex, 0, 0); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 146 | firstContour = false; |
| 147 | } else { |
caryclark@google.com | 6ec1526 | 2012-11-16 20:16:50 +0000 | [diff] [blame] | 148 | int minIndex = SkMin32(index, endIndex); |
| 149 | int sumWinding = current->windSum(minIndex); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 150 | if (sumWinding == SK_MinS32) { |
caryclark@google.com | 7fce0de | 2012-11-29 14:31:50 +0000 | [diff] [blame] | 151 | sumWinding = current->computeSum(index, endIndex, true); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 152 | } |
| 153 | if (sumWinding == SK_MinS32) { |
caryclark@google.com | 7fce0de | 2012-11-29 14:31:50 +0000 | [diff] [blame] | 154 | int contourWinding = innerContourCheck(contourList, current, |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 155 | index, endIndex, false); |
caryclark@google.com | 7fce0de | 2012-11-29 14:31:50 +0000 | [diff] [blame] | 156 | int oppContourWinding = innerContourCheck(contourList, current, |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 157 | index, endIndex, true); |
caryclark@google.com | 7fce0de | 2012-11-29 14:31:50 +0000 | [diff] [blame] | 158 | current->initWinding(index, endIndex, contourWinding, oppContourWinding); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 159 | } |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 160 | } |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 161 | SkTDArray<Span*> chaseArray; |
| 162 | do { |
caryclark@google.com | 7fce0de | 2012-11-29 14:31:50 +0000 | [diff] [blame] | 163 | if (current->activeOp(index, endIndex, xorMask, xorOpMask, op)) { |
| 164 | bool active = true; |
| 165 | do { |
| 166 | #if DEBUG_ACTIVE_SPANS |
| 167 | if (!unsortable && current->done()) { |
| 168 | debugShowActiveSpans(contourList); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 169 | } |
caryclark@google.com | 7fce0de | 2012-11-29 14:31:50 +0000 | [diff] [blame] | 170 | #endif |
| 171 | SkASSERT(unsortable || !current->done()); |
| 172 | int nextStart = index; |
| 173 | int nextEnd = endIndex; |
| 174 | Segment* next = current->findNextOp(chaseArray, nextStart, nextEnd, |
| 175 | unsortable, op, xorMask, xorOpMask); |
| 176 | if (!next) { |
| 177 | SkASSERT(!unsortable); |
| 178 | if (!unsortable && simple.hasMove() |
| 179 | && current->verb() != SkPath::kLine_Verb |
| 180 | && !simple.isClosed()) { |
| 181 | current->addCurveTo(index, endIndex, simple, true); |
| 182 | SkASSERT(simple.isClosed()); |
| 183 | } |
| 184 | active = false; |
| 185 | break; |
| 186 | } |
| 187 | current->addCurveTo(index, endIndex, simple, true); |
| 188 | current = next; |
| 189 | index = nextStart; |
| 190 | endIndex = nextEnd; |
| 191 | } while (!simple.isClosed() && ((!unsortable) || !current->done())); |
| 192 | if (active && !simple.isClosed()) { |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 193 | SkASSERT(unsortable); |
| 194 | int min = SkMin32(index, endIndex); |
| 195 | if (!current->done(min)) { |
| 196 | current->addCurveTo(index, endIndex, simple, true); |
caryclark@google.com | 7fce0de | 2012-11-29 14:31:50 +0000 | [diff] [blame] | 197 | current->markDoneBinary(min); |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 198 | } |
| 199 | closable = false; |
| 200 | } |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 201 | simple.close(); |
caryclark@google.com | 7fce0de | 2012-11-29 14:31:50 +0000 | [diff] [blame] | 202 | } else { |
| 203 | Span* last = current->markAndChaseDoneBinary(index, endIndex); |
| 204 | if (last) { |
| 205 | *chaseArray.append() = last; |
| 206 | } |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 207 | } |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 208 | current = findChaseOp(chaseArray, index, endIndex); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 209 | #if DEBUG_ACTIVE_SPANS |
| 210 | debugShowActiveSpans(contourList); |
| 211 | #endif |
| 212 | if (!current) { |
| 213 | break; |
| 214 | } |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 215 | } while (true); |
| 216 | } while (true); |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 217 | return closable; |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | } // end of Op namespace |
| 221 | |
| 222 | |
| 223 | void operate(const SkPath& one, const SkPath& two, ShapeOp op, SkPath& result) { |
| 224 | result.reset(); |
| 225 | result.setFillType(SkPath::kEvenOdd_FillType); |
| 226 | // turn path into list of segments |
| 227 | SkTArray<Op::Contour> contours; |
| 228 | // FIXME: add self-intersecting cubics' T values to segment |
| 229 | Op::EdgeBuilder builder(one, contours); |
caryclark@google.com | 7fce0de | 2012-11-29 14:31:50 +0000 | [diff] [blame] | 230 | const int xorMask = builder.xorMask(); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 231 | builder.addOperand(two); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 232 | builder.finish(); |
caryclark@google.com | 7fce0de | 2012-11-29 14:31:50 +0000 | [diff] [blame] | 233 | const int xorOpMask = builder.xorMask(); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 234 | SkTDArray<Op::Contour*> contourList; |
| 235 | makeContourList(contours, contourList); |
| 236 | Op::Contour** currentPtr = contourList.begin(); |
| 237 | if (!currentPtr) { |
| 238 | return; |
| 239 | } |
| 240 | Op::Contour** listEnd = contourList.end(); |
| 241 | // find all intersections between segments |
| 242 | do { |
| 243 | Op::Contour** nextPtr = currentPtr; |
| 244 | Op::Contour* current = *currentPtr++; |
| 245 | Op::Contour* next; |
| 246 | do { |
| 247 | next = *nextPtr++; |
| 248 | } while (addIntersectTs(current, next) && nextPtr != listEnd); |
| 249 | } while (currentPtr != listEnd); |
| 250 | // eat through coincident edges |
skia.committer@gmail.com | b0a327e | 2012-11-21 02:02:25 +0000 | [diff] [blame] | 251 | |
caryclark@google.com | 7ba591e | 2012-11-20 14:21:54 +0000 | [diff] [blame] | 252 | int total = 0; |
| 253 | int index; |
| 254 | for (index = 0; index < contourList.count(); ++index) { |
| 255 | total += contourList[index]->segments().count(); |
| 256 | } |
caryclark@google.com | 729e1c4 | 2012-11-21 21:36:34 +0000 | [diff] [blame] | 257 | #if DEBUG_SHOW_WINDING |
caryclark@google.com | 7ba591e | 2012-11-20 14:21:54 +0000 | [diff] [blame] | 258 | Op::Contour::debugShowWindingValues(contourList); |
| 259 | #endif |
caryclark@google.com | 7fce0de | 2012-11-29 14:31:50 +0000 | [diff] [blame] | 260 | coincidenceCheck(contourList, (xorMask == kEvenOdd_Mask) |
| 261 | ^ (xorOpMask == kEvenOdd_Mask), total); |
caryclark@google.com | 729e1c4 | 2012-11-21 21:36:34 +0000 | [diff] [blame] | 262 | #if DEBUG_SHOW_WINDING |
caryclark@google.com | 7ba591e | 2012-11-20 14:21:54 +0000 | [diff] [blame] | 263 | Op::Contour::debugShowWindingValues(contourList); |
| 264 | #endif |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 265 | fixOtherTIndex(contourList); |
caryclark@google.com | 31143cf | 2012-11-09 22:14:19 +0000 | [diff] [blame] | 266 | sortSegments(contourList); |
| 267 | #if DEBUG_ACTIVE_SPANS |
| 268 | debugShowActiveSpans(contourList); |
| 269 | #endif |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 270 | // construct closed contours |
caryclark@google.com | f839c03 | 2012-10-26 21:03:50 +0000 | [diff] [blame] | 271 | Op::PathWrapper wrapper(result); |
caryclark@google.com | 7fce0de | 2012-11-29 14:31:50 +0000 | [diff] [blame] | 272 | bridgeOp(contourList, op, xorMask, xorOpMask, wrapper); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 273 | } |