OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2013 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 "SkIntersections.h" |
| 8 #include "SkOpContour.h" |
| 9 #include "SkPathWriter.h" |
| 10 #include "TSearch.h" |
| 11 |
| 12 void SkOpContour::addCoincident(int index, SkOpContour* other, int otherIndex, |
| 13 const SkIntersections& ts, bool swap) { |
| 14 SkCoincidence& coincidence = *fCoincidences.append(); |
| 15 coincidence.fContours[0] = this; // FIXME: no need to store |
| 16 coincidence.fContours[1] = other; |
| 17 coincidence.fSegments[0] = index; |
| 18 coincidence.fSegments[1] = otherIndex; |
| 19 coincidence.fTs[swap][0] = ts[0][0]; |
| 20 coincidence.fTs[swap][1] = ts[0][1]; |
| 21 coincidence.fTs[!swap][0] = ts[1][0]; |
| 22 coincidence.fTs[!swap][1] = ts[1][1]; |
| 23 coincidence.fPts[0] = ts.pt(0).asSkPoint(); |
| 24 coincidence.fPts[1] = ts.pt(1).asSkPoint(); |
| 25 } |
| 26 |
| 27 SkOpSegment* SkOpContour::nonVerticalSegment(int& start, int& end) { |
| 28 int segmentCount = fSortedSegments.count(); |
| 29 SkASSERT(segmentCount > 0); |
| 30 for (int sortedIndex = fFirstSorted; sortedIndex < segmentCount; ++sortedInd
ex) { |
| 31 SkOpSegment* testSegment = fSortedSegments[sortedIndex]; |
| 32 if (testSegment->done()) { |
| 33 continue; |
| 34 } |
| 35 start = end = 0; |
| 36 while (testSegment->nextCandidate(start, end)) { |
| 37 if (!testSegment->isVertical(start, end)) { |
| 38 return testSegment; |
| 39 } |
| 40 } |
| 41 } |
| 42 return NULL; |
| 43 } |
| 44 |
| 45 void SkOpContour::resolveCoincidence(SkTDArray<SkOpContour*>& contourList) { |
| 46 int count = fCoincidences.count(); |
| 47 for (int index = 0; index < count; ++index) { |
| 48 SkCoincidence& coincidence = fCoincidences[index]; |
| 49 SkASSERT(coincidence.fContours[0] == this); |
| 50 int thisIndex = coincidence.fSegments[0]; |
| 51 SkOpSegment& thisOne = fSegments[thisIndex]; |
| 52 SkOpContour* otherContour = coincidence.fContours[1]; |
| 53 int otherIndex = coincidence.fSegments[1]; |
| 54 SkOpSegment& other = otherContour->fSegments[otherIndex]; |
| 55 if ((thisOne.done() || other.done()) && thisOne.complete() && other.comp
lete()) { |
| 56 continue; |
| 57 } |
| 58 #if DEBUG_CONCIDENT |
| 59 thisOne.debugShowTs(); |
| 60 other.debugShowTs(); |
| 61 #endif |
| 62 double startT = coincidence.fTs[0][0]; |
| 63 double endT = coincidence.fTs[0][1]; |
| 64 bool cancelers = false; |
| 65 if (startT > endT) { |
| 66 SkTSwap<double>(startT, endT); |
| 67 cancelers ^= true; // FIXME: just assign true |
| 68 } |
| 69 SkASSERT(!approximately_negative(endT - startT)); |
| 70 double oStartT = coincidence.fTs[1][0]; |
| 71 double oEndT = coincidence.fTs[1][1]; |
| 72 if (oStartT > oEndT) { |
| 73 SkTSwap<double>(oStartT, oEndT); |
| 74 cancelers ^= true; |
| 75 } |
| 76 SkASSERT(!approximately_negative(oEndT - oStartT)); |
| 77 bool opp = fOperand ^ otherContour->fOperand; |
| 78 if (cancelers && !opp) { |
| 79 // make sure startT and endT have t entries |
| 80 if (startT > 0 || oEndT < 1 |
| 81 || thisOne.isMissing(startT) || other.isMissing(oEndT)) { |
| 82 thisOne.addTPair(startT, other, oEndT, true, coincidence.fPts[0]
); |
| 83 } |
| 84 if (oStartT > 0 || endT < 1 |
| 85 || thisOne.isMissing(endT) || other.isMissing(oStartT)) { |
| 86 other.addTPair(oStartT, thisOne, endT, true, coincidence.fPts[1]
); |
| 87 } |
| 88 if (!thisOne.done() && !other.done()) { |
| 89 thisOne.addTCancel(startT, endT, other, oStartT, oEndT); |
| 90 } |
| 91 } else { |
| 92 if (startT > 0 || oStartT > 0 |
| 93 || thisOne.isMissing(startT) || other.isMissing(oStartT)) { |
| 94 thisOne.addTPair(startT, other, oStartT, true, coincidence.fPts[
0]); |
| 95 } |
| 96 if (endT < 1 || oEndT < 1 |
| 97 || thisOne.isMissing(endT) || other.isMissing(oEndT)) { |
| 98 other.addTPair(oEndT, thisOne, endT, true, coincidence.fPts[1]); |
| 99 } |
| 100 if (!thisOne.done() && !other.done()) { |
| 101 thisOne.addTCoincident(startT, endT, other, oStartT, oEndT); |
| 102 } |
| 103 } |
| 104 #if DEBUG_CONCIDENT |
| 105 thisOne.debugShowTs(); |
| 106 other.debugShowTs(); |
| 107 #endif |
| 108 #if DEBUG_SHOW_WINDING |
| 109 debugShowWindingValues(contourList); |
| 110 #endif |
| 111 } |
| 112 } |
| 113 |
| 114 // first pass, add missing T values |
| 115 // second pass, determine winding values of overlaps |
| 116 void SkOpContour::addCoincidentPoints() { |
| 117 int count = fCoincidences.count(); |
| 118 for (int index = 0; index < count; ++index) { |
| 119 SkCoincidence& coincidence = fCoincidences[index]; |
| 120 SkASSERT(coincidence.fContours[0] == this); |
| 121 int thisIndex = coincidence.fSegments[0]; |
| 122 SkOpSegment& thisOne = fSegments[thisIndex]; |
| 123 SkOpContour* otherContour = coincidence.fContours[1]; |
| 124 int otherIndex = coincidence.fSegments[1]; |
| 125 SkOpSegment& other = otherContour->fSegments[otherIndex]; |
| 126 if ((thisOne.done() || other.done()) && thisOne.complete() && other.comp
lete()) { |
| 127 // OPTIMIZATION: remove from array |
| 128 continue; |
| 129 } |
| 130 #if DEBUG_CONCIDENT |
| 131 thisOne.debugShowTs(); |
| 132 other.debugShowTs(); |
| 133 #endif |
| 134 double startT = coincidence.fTs[0][0]; |
| 135 double endT = coincidence.fTs[0][1]; |
| 136 bool cancelers; |
| 137 if ((cancelers = startT > endT)) { |
| 138 SkTSwap(startT, endT); |
| 139 SkTSwap(coincidence.fPts[0], coincidence.fPts[1]); |
| 140 } |
| 141 SkASSERT(!approximately_negative(endT - startT)); |
| 142 double oStartT = coincidence.fTs[1][0]; |
| 143 double oEndT = coincidence.fTs[1][1]; |
| 144 if (oStartT > oEndT) { |
| 145 SkTSwap<double>(oStartT, oEndT); |
| 146 cancelers ^= true; |
| 147 } |
| 148 SkASSERT(!approximately_negative(oEndT - oStartT)); |
| 149 bool opp = fOperand ^ otherContour->fOperand; |
| 150 if (cancelers && !opp) { |
| 151 // make sure startT and endT have t entries |
| 152 if (startT > 0 || oEndT < 1 |
| 153 || thisOne.isMissing(startT) || other.isMissing(oEndT)) { |
| 154 thisOne.addTPair(startT, other, oEndT, true, coincidence.fPts[0]
); |
| 155 } |
| 156 if (oStartT > 0 || endT < 1 |
| 157 || thisOne.isMissing(endT) || other.isMissing(oStartT)) { |
| 158 other.addTPair(oStartT, thisOne, endT, true, coincidence.fPts[1]
); |
| 159 } |
| 160 } else { |
| 161 if (startT > 0 || oStartT > 0 |
| 162 || thisOne.isMissing(startT) || other.isMissing(oStartT)) { |
| 163 thisOne.addTPair(startT, other, oStartT, true, coincidence.fPts[
0]); |
| 164 } |
| 165 if (endT < 1 || oEndT < 1 |
| 166 || thisOne.isMissing(endT) || other.isMissing(oEndT)) { |
| 167 other.addTPair(oEndT, thisOne, endT, true, coincidence.fPts[1]); |
| 168 } |
| 169 } |
| 170 #if DEBUG_CONCIDENT |
| 171 thisOne.debugShowTs(); |
| 172 other.debugShowTs(); |
| 173 #endif |
| 174 } |
| 175 } |
| 176 |
| 177 void SkOpContour::calcCoincidentWinding() { |
| 178 int count = fCoincidences.count(); |
| 179 for (int index = 0; index < count; ++index) { |
| 180 SkCoincidence& coincidence = fCoincidences[index]; |
| 181 SkASSERT(coincidence.fContours[0] == this); |
| 182 int thisIndex = coincidence.fSegments[0]; |
| 183 SkOpSegment& thisOne = fSegments[thisIndex]; |
| 184 if (thisOne.done()) { |
| 185 continue; |
| 186 } |
| 187 SkOpContour* otherContour = coincidence.fContours[1]; |
| 188 int otherIndex = coincidence.fSegments[1]; |
| 189 SkOpSegment& other = otherContour->fSegments[otherIndex]; |
| 190 if (other.done()) { |
| 191 continue; |
| 192 } |
| 193 double startT = coincidence.fTs[0][0]; |
| 194 double endT = coincidence.fTs[0][1]; |
| 195 bool cancelers; |
| 196 if ((cancelers = startT > endT)) { |
| 197 SkTSwap<double>(startT, endT); |
| 198 } |
| 199 SkASSERT(!approximately_negative(endT - startT)); |
| 200 double oStartT = coincidence.fTs[1][0]; |
| 201 double oEndT = coincidence.fTs[1][1]; |
| 202 if (oStartT > oEndT) { |
| 203 SkTSwap<double>(oStartT, oEndT); |
| 204 cancelers ^= true; |
| 205 } |
| 206 SkASSERT(!approximately_negative(oEndT - oStartT)); |
| 207 bool opp = fOperand ^ otherContour->fOperand; |
| 208 if (cancelers && !opp) { |
| 209 // make sure startT and endT have t entries |
| 210 if (!thisOne.done() && !other.done()) { |
| 211 thisOne.addTCancel(startT, endT, other, oStartT, oEndT); |
| 212 } |
| 213 } else { |
| 214 if (!thisOne.done() && !other.done()) { |
| 215 thisOne.addTCoincident(startT, endT, other, oStartT, oEndT); |
| 216 } |
| 217 } |
| 218 #if DEBUG_CONCIDENT |
| 219 thisOne.debugShowTs(); |
| 220 other.debugShowTs(); |
| 221 #endif |
| 222 } |
| 223 } |
| 224 |
| 225 void SkOpContour::sortSegments() { |
| 226 int segmentCount = fSegments.count(); |
| 227 fSortedSegments.setReserve(segmentCount); |
| 228 for (int test = 0; test < segmentCount; ++test) { |
| 229 *fSortedSegments.append() = &fSegments[test]; |
| 230 } |
| 231 QSort<SkOpSegment>(fSortedSegments.begin(), fSortedSegments.end() - 1); |
| 232 fFirstSorted = 0; |
| 233 } |
| 234 |
| 235 void SkOpContour::toPath(SkPathWriter& path) const { |
| 236 int segmentCount = fSegments.count(); |
| 237 const SkPoint& pt = fSegments.front().pts()[0]; |
| 238 path.deferredMove(pt); |
| 239 for (int test = 0; test < segmentCount; ++test) { |
| 240 fSegments[test].addCurveTo(0, 1, path, true); |
| 241 } |
| 242 path.close(); |
| 243 } |
| 244 |
| 245 void SkOpContour::topSortableSegment(const SkPoint& topLeft, SkPoint& bestXY, Sk
OpSegment*& topStart) { |
| 246 int segmentCount = fSortedSegments.count(); |
| 247 SkASSERT(segmentCount > 0); |
| 248 int sortedIndex = fFirstSorted; |
| 249 fDone = true; // may be cleared below |
| 250 for ( ; sortedIndex < segmentCount; ++sortedIndex) { |
| 251 SkOpSegment* testSegment = fSortedSegments[sortedIndex]; |
| 252 if (testSegment->done()) { |
| 253 if (sortedIndex == fFirstSorted) { |
| 254 ++fFirstSorted; |
| 255 } |
| 256 continue; |
| 257 } |
| 258 fDone = false; |
| 259 SkPoint testXY = testSegment->activeLeftTop(true, NULL); |
| 260 if (topStart) { |
| 261 if (testXY.fY < topLeft.fY) { |
| 262 continue; |
| 263 } |
| 264 if (testXY.fY == topLeft.fY && testXY.fX < topLeft.fX) { |
| 265 continue; |
| 266 } |
| 267 if (bestXY.fY < testXY.fY) { |
| 268 continue; |
| 269 } |
| 270 if (bestXY.fY == testXY.fY && bestXY.fX < testXY.fX) { |
| 271 continue; |
| 272 } |
| 273 } |
| 274 topStart = testSegment; |
| 275 bestXY = testXY; |
| 276 } |
| 277 } |
| 278 |
| 279 SkOpSegment* SkOpContour::undoneSegment(int& start, int& end) { |
| 280 int segmentCount = fSegments.count(); |
| 281 for (int test = 0; test < segmentCount; ++test) { |
| 282 SkOpSegment* testSegment = &fSegments[test]; |
| 283 if (testSegment->done()) { |
| 284 continue; |
| 285 } |
| 286 testSegment->undoneSpan(start, end); |
| 287 return testSegment; |
| 288 } |
| 289 return NULL; |
| 290 } |
| 291 |
| 292 #if DEBUG_DUMP |
| 293 void SkOpContour::dump() { |
| 294 int i; |
| 295 const char className[] = "SkOpContour"; |
| 296 const int tab = 4; |
| 297 SkDebugf("%s %p (contour=%d)\n", className, this, fID); |
| 298 for (i = 0; i < fSegments.count(); ++i) { |
| 299 SkDebugf("%*s.fSegments[%d]:\n", tab + sizeof(className), |
| 300 className, i); |
| 301 fSegments[i].dump(); |
| 302 } |
| 303 SkDebugf("%*s.fBounds=(l:%1.9g, t:%1.9g r:%1.9g, b:%1.9g)\n", |
| 304 tab + sizeof(className), className, |
| 305 fBounds.fLeft, fBounds.fTop, |
| 306 fBounds.fRight, fBounds.fBottom); |
| 307 SkDebugf("%*s.fContainsIntercepts=%d\n", tab + sizeof(className), |
| 308 className, fContainsIntercepts); |
| 309 SkDebugf("%*s.fContainsCurves=%d\n", tab + sizeof(className), |
| 310 className, fContainsCurves); |
| 311 } |
| 312 #endif |
| 313 |
| 314 #if DEBUG_SHOW_WINDING |
| 315 int SkOpContour::debugShowWindingValues(int totalSegments, int ofInterest) { |
| 316 int count = fSegments.count(); |
| 317 int sum = 0; |
| 318 for (int index = 0; index < count; ++index) { |
| 319 sum += fSegments[index].debugShowWindingValues(totalSegments, ofInterest
); |
| 320 } |
| 321 // SkDebugf("%s sum=%d\n", __FUNCTION__, sum); |
| 322 return sum; |
| 323 } |
| 324 |
| 325 static void SkOpContour::debugShowWindingValues(SkTDArray<SkOpContour*>& contour
List) { |
| 326 // int ofInterest = 1 << 1 | 1 << 5 | 1 << 9 | 1 << 13; |
| 327 // int ofInterest = 1 << 4 | 1 << 8 | 1 << 12 | 1 << 16; |
| 328 int ofInterest = 1 << 5 | 1 << 8; |
| 329 int total = 0; |
| 330 int index; |
| 331 for (index = 0; index < contourList.count(); ++index) { |
| 332 total += contourList[index]->segments().count(); |
| 333 } |
| 334 int sum = 0; |
| 335 for (index = 0; index < contourList.count(); ++index) { |
| 336 sum += contourList[index]->debugShowWindingValues(total, ofInterest); |
| 337 } |
| 338 // SkDebugf("%s total=%d\n", __FUNCTION__, sum); |
| 339 } |
| 340 #endif |
| 341 |
| 342 void SkOpContour::setBounds() { |
| 343 int count = fSegments.count(); |
| 344 if (count == 0) { |
| 345 SkDebugf("%s empty contour\n", __FUNCTION__); |
| 346 SkASSERT(0); |
| 347 // FIXME: delete empty contour? |
| 348 return; |
| 349 } |
| 350 fBounds = fSegments.front().bounds(); |
| 351 for (int index = 1; index < count; ++index) { |
| 352 fBounds.add(fSegments[index].bounds()); |
| 353 } |
| 354 } |
| 355 |
OLD | NEW |