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 | */ |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 7 | #include "SkOpAngle.h" |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 8 | #include "SkOpSegment.h" |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 9 | #include "SkPathOpsCurve.h" |
commit-bot@chromium.org | b76d3b6 | 2013-04-22 19:55:19 +0000 | [diff] [blame] | 10 | #include "SkTSort.h" |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 11 | |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 12 | /* Angles are sorted counterclockwise. The smallest angle has a positive x and the smallest |
| 13 | positive y. The largest angle has a positive x and a zero y. */ |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 14 | |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 15 | #if DEBUG_ANGLE |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 16 | static bool CompareResult(const char* func, SkString* bugOut, SkString* bugPart, int append, |
| 17 | bool compare) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 18 | SkDebugf("%s %c %d\n", bugOut->c_str(), compare ? 'T' : 'F', append); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 19 | SkDebugf("%sPart %s\n", func, bugPart[0].c_str()); |
| 20 | SkDebugf("%sPart %s\n", func, bugPart[1].c_str()); |
| 21 | SkDebugf("%sPart %s\n", func, bugPart[2].c_str()); |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 22 | return compare; |
| 23 | } |
| 24 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 25 | #define COMPARE_RESULT(append, compare) CompareResult(__FUNCTION__, &bugOut, bugPart, append, \ |
| 26 | compare) |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 27 | #else |
skia.committer@gmail.com | 8f6ef40 | 2013-06-05 07:01:06 +0000 | [diff] [blame] | 28 | #define COMPARE_RESULT(append, compare) compare |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 29 | #endif |
| 30 | |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 31 | /* quarter angle values for sector |
| 32 | |
| 33 | 31 x > 0, y == 0 horizontal line (to the right) |
| 34 | 0 x > 0, y == epsilon quad/cubic horizontal tangent eventually going +y |
| 35 | 1 x > 0, y > 0, x > y nearer horizontal angle |
| 36 | 2 x + e == y quad/cubic 45 going horiz |
| 37 | 3 x > 0, y > 0, x == y 45 angle |
| 38 | 4 x == y + e quad/cubic 45 going vert |
| 39 | 5 x > 0, y > 0, x < y nearer vertical angle |
| 40 | 6 x == epsilon, y > 0 quad/cubic vertical tangent eventually going +x |
| 41 | 7 x == 0, y > 0 vertical line (to the top) |
| 42 | |
| 43 | 8 7 6 |
| 44 | 9 | 5 |
| 45 | 10 | 4 |
| 46 | 11 | 3 |
| 47 | 12 \ | / 2 |
| 48 | 13 | 1 |
| 49 | 14 | 0 |
| 50 | 15 --------------+------------- 31 |
| 51 | 16 | 30 |
| 52 | 17 | 29 |
| 53 | 18 / | \ 28 |
| 54 | 19 | 27 |
| 55 | 20 | 26 |
| 56 | 21 | 25 |
| 57 | 22 23 24 |
| 58 | */ |
| 59 | |
| 60 | // return true if lh < this < rh |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 61 | bool SkOpAngle::after(SkOpAngle* test) { |
| 62 | SkOpAngle* lh = test; |
| 63 | SkOpAngle* rh = lh->fNext; |
| 64 | SkASSERT(lh != rh); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 65 | #if DEBUG_ANGLE |
| 66 | SkString bugOut; |
| 67 | bugOut.printf("%s [%d/%d] %d/%d tStart=%1.9g tEnd=%1.9g" |
| 68 | " < [%d/%d] %d/%d tStart=%1.9g tEnd=%1.9g" |
| 69 | " < [%d/%d] %d/%d tStart=%1.9g tEnd=%1.9g ", __FUNCTION__, |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 70 | lh->segment()->debugID(), lh->debugID(), lh->fSectorStart, lh->fSectorEnd, |
| 71 | lh->fStart->t(), lh->fEnd->t(), |
| 72 | segment()->debugID(), debugID(), fSectorStart, fSectorEnd, fStart->t(), fEnd->t(), |
| 73 | rh->segment()->debugID(), rh->debugID(), rh->fSectorStart, rh->fSectorEnd, |
| 74 | rh->fStart->t(), rh->fEnd->t()); |
| 75 | SkString bugPart[3] = { lh->debugPart(), this->debugPart(), rh->debugPart() }; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 76 | #endif |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 77 | if (lh->fComputeSector && !lh->computeSector()) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 78 | return COMPARE_RESULT(1, true); |
| 79 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 80 | if (fComputeSector && !this->computeSector()) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 81 | return COMPARE_RESULT(2, true); |
| 82 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 83 | if (rh->fComputeSector && !rh->computeSector()) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 84 | return COMPARE_RESULT(3, true); |
| 85 | } |
| 86 | #if DEBUG_ANGLE // reset bugOut with computed sectors |
| 87 | bugOut.printf("%s [%d/%d] %d/%d tStart=%1.9g tEnd=%1.9g" |
| 88 | " < [%d/%d] %d/%d tStart=%1.9g tEnd=%1.9g" |
| 89 | " < [%d/%d] %d/%d tStart=%1.9g tEnd=%1.9g ", __FUNCTION__, |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 90 | lh->segment()->debugID(), lh->debugID(), lh->fSectorStart, lh->fSectorEnd, |
| 91 | lh->fStart->t(), lh->fEnd->t(), |
| 92 | segment()->debugID(), debugID(), fSectorStart, fSectorEnd, fStart->t(), fEnd->t(), |
| 93 | rh->segment()->debugID(), rh->debugID(), rh->fSectorStart, rh->fSectorEnd, |
| 94 | rh->fStart->t(), rh->fEnd->t()); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 95 | #endif |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 96 | bool ltrOverlap = (lh->fSectorMask | rh->fSectorMask) & fSectorMask; |
| 97 | bool lrOverlap = lh->fSectorMask & rh->fSectorMask; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 98 | int lrOrder; // set to -1 if either order works |
| 99 | if (!lrOverlap) { // no lh/rh sector overlap |
| 100 | if (!ltrOverlap) { // no lh/this/rh sector overlap |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 101 | return COMPARE_RESULT(4, (lh->fSectorEnd > rh->fSectorStart) |
| 102 | ^ (fSectorStart > lh->fSectorEnd) ^ (fSectorStart > rh->fSectorStart)); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 103 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 104 | int lrGap = (rh->fSectorStart - lh->fSectorStart + 32) & 0x1f; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 105 | /* A tiny change can move the start +/- 4. The order can only be determined if |
| 106 | lr gap is not 12 to 20 or -12 to -20. |
| 107 | -31 ..-21 1 |
| 108 | -20 ..-12 -1 |
| 109 | -11 .. -1 0 |
| 110 | 0 shouldn't get here |
| 111 | 11 .. 1 1 |
| 112 | 12 .. 20 -1 |
| 113 | 21 .. 31 0 |
| 114 | */ |
| 115 | lrOrder = lrGap > 20 ? 0 : lrGap > 11 ? -1 : 1; |
| 116 | } else { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 117 | lrOrder = (int) lh->orderable(rh); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 118 | if (!ltrOverlap) { |
| 119 | return COMPARE_RESULT(5, !lrOrder); |
| 120 | } |
| 121 | } |
| 122 | int ltOrder; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 123 | SkASSERT((lh->fSectorMask & fSectorMask) || (rh->fSectorMask & fSectorMask)); |
| 124 | if (lh->fSectorMask & fSectorMask) { |
| 125 | ltOrder = (int) lh->orderable(this); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 126 | } else { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 127 | int ltGap = (fSectorStart - lh->fSectorStart + 32) & 0x1f; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 128 | ltOrder = ltGap > 20 ? 0 : ltGap > 11 ? -1 : 1; |
| 129 | } |
| 130 | int trOrder; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 131 | if (rh->fSectorMask & fSectorMask) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 132 | trOrder = (int) orderable(rh); |
| 133 | } else { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 134 | int trGap = (rh->fSectorStart - fSectorStart + 32) & 0x1f; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 135 | trOrder = trGap > 20 ? 0 : trGap > 11 ? -1 : 1; |
| 136 | } |
| 137 | if (lrOrder >= 0 && ltOrder >= 0 && trOrder >= 0) { |
| 138 | return COMPARE_RESULT(7, lrOrder ? (ltOrder & trOrder) : (ltOrder | trOrder)); |
| 139 | } |
| 140 | SkASSERT(lrOrder >= 0 || ltOrder >= 0 || trOrder >= 0); |
| 141 | // There's not enough information to sort. Get the pairs of angles in opposite planes. |
| 142 | // If an order is < 0, the pair is already in an opposite plane. Check the remaining pairs. |
| 143 | // FIXME : once all variants are understood, rewrite this more simply |
| 144 | if (ltOrder == 0 && lrOrder == 0) { |
| 145 | SkASSERT(trOrder < 0); |
| 146 | // FIXME : once this is verified to work, remove one opposite angle call |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 147 | SkDEBUGCODE(bool lrOpposite = lh->oppositePlanes(rh)); |
| 148 | bool ltOpposite = lh->oppositePlanes(this); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 149 | SkASSERT(lrOpposite != ltOpposite); |
| 150 | return COMPARE_RESULT(8, ltOpposite); |
| 151 | } else if (ltOrder == 1 && trOrder == 0) { |
| 152 | SkASSERT(lrOrder < 0); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 153 | SkDEBUGCODE(bool ltOpposite = lh->oppositePlanes(this)); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 154 | bool trOpposite = oppositePlanes(rh); |
| 155 | SkASSERT(ltOpposite != trOpposite); |
| 156 | return COMPARE_RESULT(9, trOpposite); |
| 157 | } else if (lrOrder == 1 && trOrder == 1) { |
| 158 | SkASSERT(ltOrder < 0); |
| 159 | SkDEBUGCODE(bool trOpposite = oppositePlanes(rh)); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 160 | bool lrOpposite = lh->oppositePlanes(rh); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 161 | SkASSERT(lrOpposite != trOpposite); |
| 162 | return COMPARE_RESULT(10, lrOpposite); |
| 163 | } |
| 164 | if (lrOrder < 0) { |
| 165 | if (ltOrder < 0) { |
| 166 | return COMPARE_RESULT(11, trOrder); |
| 167 | } |
| 168 | return COMPARE_RESULT(12, ltOrder); |
| 169 | } |
| 170 | return COMPARE_RESULT(13, !lrOrder); |
| 171 | } |
| 172 | |
| 173 | // given a line, see if the opposite curve's convex hull is all on one side |
| 174 | // returns -1=not on one side 0=this CW of test 1=this CCW of test |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 175 | int SkOpAngle::allOnOneSide(const SkOpAngle* test) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 176 | SkASSERT(!fIsCurve); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 177 | SkASSERT(test->fIsCurve); |
| 178 | const SkDPoint& origin = test->fCurvePart[0]; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 179 | SkVector line; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 180 | if (segment()->verb() == SkPath::kLine_Verb) { |
| 181 | const SkPoint* linePts = segment()->pts(); |
| 182 | int lineStart = fStart->t() < fEnd->t() ? 0 : 1; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 183 | line = linePts[lineStart ^ 1] - linePts[lineStart]; |
| 184 | } else { |
caryclark | fc95be1 | 2015-07-07 18:09:32 -0700 | [diff] [blame^] | 185 | line = (fCurvePart[1] - fCurvePart[0]).asSkVector(); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 186 | } |
| 187 | float crosses[3]; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 188 | SkPath::Verb testVerb = test->segment()->verb(); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 189 | int iMax = SkPathOpsVerbToPoints(testVerb); |
| 190 | // SkASSERT(origin == test.fCurveHalf[0]); |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 191 | const SkDCurve& testCurve = test->fCurvePart; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 192 | for (int index = 1; index <= iMax; ++index) { |
| 193 | float xy1 = (float) (line.fX * (testCurve[index].fY - origin.fY)); |
| 194 | float xy2 = (float) (line.fY * (testCurve[index].fX - origin.fX)); |
| 195 | crosses[index - 1] = AlmostEqualUlps(xy1, xy2) ? 0 : xy1 - xy2; |
| 196 | } |
| 197 | if (crosses[0] * crosses[1] < 0) { |
| 198 | return -1; |
| 199 | } |
| 200 | if (SkPath::kCubic_Verb == testVerb) { |
| 201 | if (crosses[0] * crosses[2] < 0 || crosses[1] * crosses[2] < 0) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 202 | return -1; |
| 203 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 204 | } |
| 205 | if (crosses[0]) { |
| 206 | return crosses[0] < 0; |
| 207 | } |
| 208 | if (crosses[1]) { |
| 209 | return crosses[1] < 0; |
| 210 | } |
| 211 | if (SkPath::kCubic_Verb == testVerb && crosses[2]) { |
| 212 | return crosses[2] < 0; |
| 213 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 214 | fUnorderable = true; |
| 215 | return -1; |
| 216 | } |
| 217 | |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 218 | bool SkOpAngle::checkCrossesZero() const { |
| 219 | int start = SkTMin(fSectorStart, fSectorEnd); |
| 220 | int end = SkTMax(fSectorStart, fSectorEnd); |
| 221 | bool crossesZero = end - start > 16; |
| 222 | return crossesZero; |
| 223 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 224 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 225 | // loop looking for a pair of angle parts that are too close to be sorted |
| 226 | /* This is called after other more simple intersection and angle sorting tests have been exhausted. |
| 227 | This should be rarely called -- the test below is thorough and time consuming. |
| 228 | This checks the distance between start points; the distance between |
| 229 | */ |
| 230 | void SkOpAngle::checkNearCoincidence() { |
| 231 | SkOpAngle* test = this; |
| 232 | do { |
| 233 | SkOpSegment* testSegment = test->segment(); |
| 234 | double testStartT = test->start()->t(); |
| 235 | SkDPoint testStartPt = testSegment->dPtAtT(testStartT); |
| 236 | double testEndT = test->end()->t(); |
| 237 | SkDPoint testEndPt = testSegment->dPtAtT(testEndT); |
| 238 | double testLenSq = testStartPt.distanceSquared(testEndPt); |
| 239 | if (0) { |
| 240 | SkDebugf("%s testLenSq=%1.9g id=%d\n", __FUNCTION__, testLenSq, testSegment->debugID()); |
| 241 | } |
| 242 | double testMidT = (testStartT + testEndT) / 2; |
| 243 | SkOpAngle* next = test; |
| 244 | while ((next = next->fNext) != this) { |
| 245 | SkOpSegment* nextSegment = next->segment(); |
| 246 | double testMidDistSq = testSegment->distSq(testMidT, next); |
| 247 | double testEndDistSq = testSegment->distSq(testEndT, next); |
| 248 | double nextStartT = next->start()->t(); |
| 249 | SkDPoint nextStartPt = nextSegment->dPtAtT(nextStartT); |
| 250 | double distSq = testStartPt.distanceSquared(nextStartPt); |
| 251 | double nextEndT = next->end()->t(); |
| 252 | double nextMidT = (nextStartT + nextEndT) / 2; |
| 253 | double nextMidDistSq = nextSegment->distSq(nextMidT, test); |
| 254 | double nextEndDistSq = nextSegment->distSq(nextEndT, test); |
| 255 | if (0) { |
| 256 | SkDebugf("%s distSq=%1.9g testId=%d nextId=%d\n", __FUNCTION__, distSq, |
| 257 | testSegment->debugID(), nextSegment->debugID()); |
| 258 | SkDebugf("%s testMidDistSq=%1.9g\n", __FUNCTION__, testMidDistSq); |
| 259 | SkDebugf("%s testEndDistSq=%1.9g\n", __FUNCTION__, testEndDistSq); |
| 260 | SkDebugf("%s nextMidDistSq=%1.9g\n", __FUNCTION__, nextMidDistSq); |
| 261 | SkDebugf("%s nextEndDistSq=%1.9g\n", __FUNCTION__, nextEndDistSq); |
| 262 | SkDPoint nextEndPt = nextSegment->dPtAtT(nextEndT); |
| 263 | double nextLenSq = nextStartPt.distanceSquared(nextEndPt); |
| 264 | SkDebugf("%s nextLenSq=%1.9g\n", __FUNCTION__, nextLenSq); |
| 265 | SkDebugf("\n"); |
| 266 | } |
| 267 | } |
| 268 | test = test->fNext; |
| 269 | } while (test->fNext != this); |
| 270 | } |
| 271 | |
| 272 | bool SkOpAngle::checkParallel(SkOpAngle* rh) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 273 | SkDVector scratch[2]; |
| 274 | const SkDVector* sweep, * tweep; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 275 | if (!this->fUnorderedSweep) { |
| 276 | sweep = this->fSweep; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 277 | } else { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 278 | scratch[0] = this->fCurvePart[1] - this->fCurvePart[0]; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 279 | sweep = &scratch[0]; |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 280 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 281 | if (!rh->fUnorderedSweep) { |
| 282 | tweep = rh->fSweep; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 283 | } else { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 284 | scratch[1] = rh->fCurvePart[1] - rh->fCurvePart[0]; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 285 | tweep = &scratch[1]; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 286 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 287 | double s0xt0 = sweep->crossCheck(*tweep); |
| 288 | if (tangentsDiverge(rh, s0xt0)) { |
| 289 | return s0xt0 < 0; |
skia.committer@gmail.com | 8f6ef40 | 2013-06-05 07:01:06 +0000 | [diff] [blame] | 290 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 291 | // compute the perpendicular to the endpoints and see where it intersects the opposite curve |
| 292 | // if the intersections within the t range, do a cross check on those |
| 293 | bool inside; |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 294 | if (!fCurvePart[SkPathOpsVerbToPoints(this->segment()->verb())].approximatelyEqual( |
| 295 | rh->fCurvePart[SkPathOpsVerbToPoints(rh->segment()->verb())])) { |
| 296 | if (this->endToSide(rh, &inside)) { |
| 297 | return inside; |
| 298 | } |
| 299 | if (rh->endToSide(this, &inside)) { |
| 300 | return !inside; |
| 301 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 302 | } |
| 303 | if (this->midToSide(rh, &inside)) { |
| 304 | return inside; |
| 305 | } |
| 306 | if (rh->midToSide(this, &inside)) { |
| 307 | return !inside; |
| 308 | } |
| 309 | // compute the cross check from the mid T values (last resort) |
| 310 | SkDVector m0 = segment()->dPtAtT(this->midT()) - this->fCurvePart[0]; |
| 311 | SkDVector m1 = rh->segment()->dPtAtT(rh->midT()) - rh->fCurvePart[0]; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 312 | double m0xm1 = m0.crossCheck(m1); |
| 313 | if (m0xm1 == 0) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 314 | this->fUnorderable = true; |
| 315 | rh->fUnorderable = true; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 316 | return true; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 317 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 318 | return m0xm1 < 0; |
| 319 | } |
| 320 | |
| 321 | // the original angle is too short to get meaningful sector information |
| 322 | // lengthen it until it is long enough to be meaningful or leave it unset if lengthening it |
| 323 | // would cause it to intersect one of the adjacent angles |
| 324 | bool SkOpAngle::computeSector() { |
| 325 | if (fComputedSector) { |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 326 | return !fUnorderable; |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 327 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 328 | fComputedSector = true; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 329 | bool stepUp = fStart->t() < fEnd->t(); |
| 330 | const SkOpSpanBase* checkEnd = fEnd; |
| 331 | if (checkEnd->final() && stepUp) { |
caryclark | ccec0f9 | 2015-03-24 07:28:17 -0700 | [diff] [blame] | 332 | fUnorderable = true; |
| 333 | return false; |
| 334 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 335 | do { |
| 336 | // advance end |
| 337 | const SkOpSegment* other = checkEnd->segment(); |
| 338 | const SkOpSpanBase* oSpan = other->head(); |
| 339 | do { |
| 340 | if (oSpan->segment() != segment()) { |
| 341 | continue; |
| 342 | } |
| 343 | if (oSpan == checkEnd) { |
| 344 | continue; |
| 345 | } |
| 346 | if (!approximately_equal(oSpan->t(), checkEnd->t())) { |
| 347 | continue; |
| 348 | } |
| 349 | goto recomputeSector; |
| 350 | } while (!oSpan->final() && (oSpan = oSpan->upCast()->next())); |
| 351 | checkEnd = stepUp ? !checkEnd->final() |
| 352 | ? checkEnd->upCast()->next() : NULL |
| 353 | : checkEnd->prev(); |
| 354 | } while (checkEnd); |
| 355 | recomputeSector: |
| 356 | SkOpSpanBase* computedEnd = stepUp ? checkEnd ? checkEnd->prev() : fEnd->segment()->head() |
| 357 | : checkEnd ? checkEnd->upCast()->next() : fEnd->segment()->tail(); |
| 358 | if (checkEnd == fEnd || computedEnd == fEnd || computedEnd == fStart) { |
| 359 | fUnorderable = true; |
| 360 | return false; |
| 361 | } |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 362 | if (stepUp != (fStart->t() < computedEnd->t())) { |
| 363 | fUnorderable = true; |
| 364 | return false; |
| 365 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 366 | SkOpSpanBase* saveEnd = fEnd; |
| 367 | fComputedEnd = fEnd = computedEnd; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 368 | setSpans(); |
| 369 | setSector(); |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 370 | fEnd = saveEnd; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 371 | return !fUnorderable; |
| 372 | } |
| 373 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 374 | int SkOpAngle::convexHullOverlaps(const SkOpAngle* rh) const { |
| 375 | const SkDVector* sweep = this->fSweep; |
| 376 | const SkDVector* tweep = rh->fSweep; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 377 | double s0xs1 = sweep[0].crossCheck(sweep[1]); |
| 378 | double s0xt0 = sweep[0].crossCheck(tweep[0]); |
| 379 | double s1xt0 = sweep[1].crossCheck(tweep[0]); |
| 380 | bool tBetweenS = s0xs1 > 0 ? s0xt0 > 0 && s1xt0 < 0 : s0xt0 < 0 && s1xt0 > 0; |
| 381 | double s0xt1 = sweep[0].crossCheck(tweep[1]); |
| 382 | double s1xt1 = sweep[1].crossCheck(tweep[1]); |
| 383 | tBetweenS |= s0xs1 > 0 ? s0xt1 > 0 && s1xt1 < 0 : s0xt1 < 0 && s1xt1 > 0; |
| 384 | double t0xt1 = tweep[0].crossCheck(tweep[1]); |
| 385 | if (tBetweenS) { |
| 386 | return -1; |
caryclark@google.com | a2bbc6e | 2013-11-01 17:36:03 +0000 | [diff] [blame] | 387 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 388 | if ((s0xt0 == 0 && s1xt1 == 0) || (s1xt0 == 0 && s0xt1 == 0)) { // s0 to s1 equals t0 to t1 |
| 389 | return -1; |
| 390 | } |
| 391 | bool sBetweenT = t0xt1 > 0 ? s0xt0 < 0 && s0xt1 > 0 : s0xt0 > 0 && s0xt1 < 0; |
| 392 | sBetweenT |= t0xt1 > 0 ? s1xt0 < 0 && s1xt1 > 0 : s1xt0 > 0 && s1xt1 < 0; |
| 393 | if (sBetweenT) { |
| 394 | return -1; |
| 395 | } |
| 396 | // if all of the sweeps are in the same half plane, then the order of any pair is enough |
| 397 | if (s0xt0 >= 0 && s0xt1 >= 0 && s1xt0 >= 0 && s1xt1 >= 0) { |
| 398 | return 0; |
| 399 | } |
| 400 | if (s0xt0 <= 0 && s0xt1 <= 0 && s1xt0 <= 0 && s1xt1 <= 0) { |
| 401 | return 1; |
| 402 | } |
| 403 | // if the outside sweeps are greater than 180 degress: |
| 404 | // first assume the inital tangents are the ordering |
| 405 | // if the midpoint direction matches the inital order, that is enough |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 406 | SkDVector m0 = this->segment()->dPtAtT(this->midT()) - this->fCurvePart[0]; |
| 407 | SkDVector m1 = rh->segment()->dPtAtT(rh->midT()) - rh->fCurvePart[0]; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 408 | double m0xm1 = m0.crossCheck(m1); |
| 409 | if (s0xt0 > 0 && m0xm1 > 0) { |
| 410 | return 0; |
| 411 | } |
| 412 | if (s0xt0 < 0 && m0xm1 < 0) { |
| 413 | return 1; |
| 414 | } |
| 415 | if (tangentsDiverge(rh, s0xt0)) { |
| 416 | return s0xt0 < 0; |
| 417 | } |
| 418 | return m0xm1 < 0; |
| 419 | } |
| 420 | |
| 421 | // OPTIMIZATION: longest can all be either lazily computed here or precomputed in setup |
| 422 | double SkOpAngle::distEndRatio(double dist) const { |
| 423 | double longest = 0; |
| 424 | const SkOpSegment& segment = *this->segment(); |
| 425 | int ptCount = SkPathOpsVerbToPoints(segment.verb()); |
| 426 | const SkPoint* pts = segment.pts(); |
| 427 | for (int idx1 = 0; idx1 <= ptCount - 1; ++idx1) { |
| 428 | for (int idx2 = idx1 + 1; idx2 <= ptCount; ++idx2) { |
| 429 | if (idx1 == idx2) { |
| 430 | continue; |
| 431 | } |
| 432 | SkDVector v; |
| 433 | v.set(pts[idx2] - pts[idx1]); |
| 434 | double lenSq = v.lengthSquared(); |
| 435 | longest = SkTMax(longest, lenSq); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 436 | } |
| 437 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 438 | return sqrt(longest) / dist; |
| 439 | } |
| 440 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 441 | bool SkOpAngle::endsIntersect(SkOpAngle* rh) { |
| 442 | SkPath::Verb lVerb = this->segment()->verb(); |
| 443 | SkPath::Verb rVerb = rh->segment()->verb(); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 444 | int lPts = SkPathOpsVerbToPoints(lVerb); |
| 445 | int rPts = SkPathOpsVerbToPoints(rVerb); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 446 | SkDLine rays[] = {{{this->fCurvePart[0], rh->fCurvePart[rPts]}}, |
| 447 | {{this->fCurvePart[0], this->fCurvePart[lPts]}}}; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 448 | if (rays[0][1] == rays[1][1]) { |
| 449 | return checkParallel(rh); |
| 450 | } |
| 451 | double smallTs[2] = {-1, -1}; |
| 452 | bool limited[2] = {false, false}; |
| 453 | for (int index = 0; index < 2; ++index) { |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 454 | SkPath::Verb cVerb = index ? rVerb : lVerb; |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 455 | // if the curve is a line, then the line and the ray intersect only at their crossing |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 456 | if (cVerb == SkPath::kLine_Verb) { |
caryclark | 65f5531 | 2014-11-13 06:58:52 -0800 | [diff] [blame] | 457 | continue; |
| 458 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 459 | const SkOpSegment& segment = index ? *rh->segment() : *this->segment(); |
| 460 | SkIntersections i; |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 461 | (*CurveIntersectRay[cVerb])(segment.pts(), segment.weight(), rays[index], &i); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 462 | double tStart = index ? rh->fStart->t() : this->fStart->t(); |
| 463 | double tEnd = index ? rh->fComputedEnd->t() : this->fComputedEnd->t(); |
| 464 | bool testAscends = tStart < (index ? rh->fComputedEnd->t() : this->fComputedEnd->t()); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 465 | double t = testAscends ? 0 : 1; |
| 466 | for (int idx2 = 0; idx2 < i.used(); ++idx2) { |
| 467 | double testT = i[0][idx2]; |
| 468 | if (!approximately_between_orderable(tStart, testT, tEnd)) { |
| 469 | continue; |
| 470 | } |
| 471 | if (approximately_equal_orderable(tStart, testT)) { |
| 472 | continue; |
| 473 | } |
| 474 | smallTs[index] = t = testAscends ? SkTMax(t, testT) : SkTMin(t, testT); |
| 475 | limited[index] = approximately_equal_orderable(t, tEnd); |
| 476 | } |
| 477 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 478 | bool sRayLonger = false; |
| 479 | SkDVector sCept = {0, 0}; |
| 480 | double sCeptT = -1; |
| 481 | int sIndex = -1; |
| 482 | bool useIntersect = false; |
| 483 | for (int index = 0; index < 2; ++index) { |
| 484 | if (smallTs[index] < 0) { |
| 485 | continue; |
| 486 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 487 | const SkOpSegment& segment = index ? *rh->segment() : *this->segment(); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 488 | const SkDPoint& dPt = segment.dPtAtT(smallTs[index]); |
| 489 | SkDVector cept = dPt - rays[index][0]; |
| 490 | // If this point is on the curve, it should have been detected earlier by ordinary |
| 491 | // curve intersection. This may be hard to determine in general, but for lines, |
| 492 | // the point could be close to or equal to its end, but shouldn't be near the start. |
| 493 | if ((index ? lPts : rPts) == 1) { |
| 494 | SkDVector total = rays[index][1] - rays[index][0]; |
| 495 | if (cept.lengthSquared() * 2 < total.lengthSquared()) { |
| 496 | continue; |
| 497 | } |
| 498 | } |
| 499 | SkDVector end = rays[index][1] - rays[index][0]; |
| 500 | if (cept.fX * end.fX < 0 || cept.fY * end.fY < 0) { |
| 501 | continue; |
| 502 | } |
| 503 | double rayDist = cept.length(); |
| 504 | double endDist = end.length(); |
| 505 | bool rayLonger = rayDist > endDist; |
| 506 | if (limited[0] && limited[1] && rayLonger) { |
| 507 | useIntersect = true; |
| 508 | sRayLonger = rayLonger; |
| 509 | sCept = cept; |
| 510 | sCeptT = smallTs[index]; |
| 511 | sIndex = index; |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 512 | break; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 513 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 514 | double delta = fabs(rayDist - endDist); |
| 515 | double minX, minY, maxX, maxY; |
| 516 | minX = minY = SK_ScalarInfinity; |
| 517 | maxX = maxY = -SK_ScalarInfinity; |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 518 | const SkDCurve& curve = index ? rh->fCurvePart : this->fCurvePart; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 519 | int ptCount = index ? rPts : lPts; |
| 520 | for (int idx2 = 0; idx2 <= ptCount; ++idx2) { |
| 521 | minX = SkTMin(minX, curve[idx2].fX); |
| 522 | minY = SkTMin(minY, curve[idx2].fY); |
| 523 | maxX = SkTMax(maxX, curve[idx2].fX); |
| 524 | maxY = SkTMax(maxY, curve[idx2].fY); |
| 525 | } |
| 526 | double maxWidth = SkTMax(maxX - minX, maxY - minY); |
| 527 | delta /= maxWidth; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 528 | if (delta > 1e-3 && (useIntersect ^= true)) { // FIXME: move this magic number |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 529 | sRayLonger = rayLonger; |
| 530 | sCept = cept; |
| 531 | sCeptT = smallTs[index]; |
| 532 | sIndex = index; |
| 533 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 534 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 535 | if (useIntersect) { |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 536 | const SkDCurve& curve = sIndex ? rh->fCurvePart : this->fCurvePart; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 537 | const SkOpSegment& segment = sIndex ? *rh->segment() : *this->segment(); |
| 538 | double tStart = sIndex ? rh->fStart->t() : fStart->t(); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 539 | SkDVector mid = segment.dPtAtT(tStart + (sCeptT - tStart) / 2) - curve[0]; |
| 540 | double septDir = mid.crossCheck(sCept); |
| 541 | if (!septDir) { |
| 542 | return checkParallel(rh); |
| 543 | } |
| 544 | return sRayLonger ^ (sIndex == 0) ^ (septDir < 0); |
| 545 | } else { |
| 546 | return checkParallel(rh); |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 547 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 548 | } |
| 549 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 550 | bool SkOpAngle::endToSide(const SkOpAngle* rh, bool* inside) const { |
| 551 | const SkOpSegment* segment = this->segment(); |
| 552 | SkPath::Verb verb = segment->verb(); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 553 | SkDLine rayEnd; |
| 554 | rayEnd[0].set(this->fEnd->pt()); |
| 555 | rayEnd[1] = rayEnd[0]; |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 556 | SkDVector slopeAtEnd = (*CurveDSlopeAtT[verb])(segment->pts(), segment->weight(), |
| 557 | this->fEnd->t()); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 558 | rayEnd[1].fX += slopeAtEnd.fY; |
| 559 | rayEnd[1].fY -= slopeAtEnd.fX; |
| 560 | SkIntersections iEnd; |
| 561 | const SkOpSegment* oppSegment = rh->segment(); |
| 562 | SkPath::Verb oppVerb = oppSegment->verb(); |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 563 | (*CurveIntersectRay[oppVerb])(oppSegment->pts(), oppSegment->weight(), rayEnd, &iEnd); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 564 | double endDist; |
| 565 | int closestEnd = iEnd.closestTo(rh->fStart->t(), rh->fEnd->t(), rayEnd[0], &endDist); |
| 566 | if (closestEnd < 0) { |
| 567 | return false; |
| 568 | } |
| 569 | if (!endDist) { |
| 570 | return false; |
| 571 | } |
| 572 | SkDPoint start; |
| 573 | start.set(this->fStart->pt()); |
| 574 | // OPTIMIZATION: multiple times in the code we find the max scalar |
| 575 | double minX, minY, maxX, maxY; |
| 576 | minX = minY = SK_ScalarInfinity; |
| 577 | maxX = maxY = -SK_ScalarInfinity; |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 578 | const SkDCurve& curve = rh->fCurvePart; |
| 579 | int oppPts = SkPathOpsVerbToPoints(oppVerb); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 580 | for (int idx2 = 0; idx2 <= oppPts; ++idx2) { |
| 581 | minX = SkTMin(minX, curve[idx2].fX); |
| 582 | minY = SkTMin(minY, curve[idx2].fY); |
| 583 | maxX = SkTMax(maxX, curve[idx2].fX); |
| 584 | maxY = SkTMax(maxY, curve[idx2].fY); |
| 585 | } |
| 586 | double maxWidth = SkTMax(maxX - minX, maxY - minY); |
| 587 | endDist /= maxWidth; |
| 588 | if (endDist < 5e-11) { // empirically found |
| 589 | return false; |
| 590 | } |
| 591 | const SkDPoint* endPt = &rayEnd[0]; |
| 592 | SkDPoint oppPt = iEnd.pt(closestEnd); |
| 593 | SkDVector vLeft = *endPt - start; |
| 594 | SkDVector vRight = oppPt - start; |
| 595 | double dir = vLeft.crossCheck(vRight); |
| 596 | if (!dir) { |
| 597 | return false; |
| 598 | } |
| 599 | *inside = dir < 0; |
| 600 | return true; |
| 601 | } |
| 602 | |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 603 | /* y<0 y==0 y>0 x<0 x==0 x>0 xy<0 xy==0 xy>0 |
| 604 | 0 x x x |
| 605 | 1 x x x |
| 606 | 2 x x x |
| 607 | 3 x x x |
| 608 | 4 x x x |
| 609 | 5 x x x |
| 610 | 6 x x x |
| 611 | 7 x x x |
| 612 | 8 x x x |
| 613 | 9 x x x |
| 614 | 10 x x x |
| 615 | 11 x x x |
| 616 | 12 x x x |
| 617 | 13 x x x |
| 618 | 14 x x x |
| 619 | 15 x x x |
| 620 | */ |
| 621 | int SkOpAngle::findSector(SkPath::Verb verb, double x, double y) const { |
| 622 | double absX = fabs(x); |
| 623 | double absY = fabs(y); |
| 624 | double xy = SkPath::kLine_Verb == verb || !AlmostEqualUlps(absX, absY) ? absX - absY : 0; |
| 625 | // If there are four quadrants and eight octants, and since the Latin for sixteen is sedecim, |
| 626 | // one could coin the term sedecimant for a space divided into 16 sections. |
| 627 | // http://english.stackexchange.com/questions/133688/word-for-something-partitioned-into-16-parts |
| 628 | static const int sedecimant[3][3][3] = { |
| 629 | // y<0 y==0 y>0 |
| 630 | // x<0 x==0 x>0 x<0 x==0 x>0 x<0 x==0 x>0 |
| 631 | {{ 4, 3, 2}, { 7, -1, 15}, {10, 11, 12}}, // abs(x) < abs(y) |
| 632 | {{ 5, -1, 1}, {-1, -1, -1}, { 9, -1, 13}}, // abs(x) == abs(y) |
| 633 | {{ 6, 3, 0}, { 7, -1, 15}, { 8, 11, 14}}, // abs(x) > abs(y) |
| 634 | }; |
| 635 | int sector = sedecimant[(xy >= 0) + (xy > 0)][(y >= 0) + (y > 0)][(x >= 0) + (x > 0)] * 2 + 1; |
caryclark | 65b427c | 2014-09-18 10:32:57 -0700 | [diff] [blame] | 636 | // SkASSERT(SkPath::kLine_Verb == verb || sector >= 0); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 637 | return sector; |
| 638 | } |
| 639 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 640 | SkOpGlobalState* SkOpAngle::globalState() const { |
| 641 | return this->segment()->globalState(); |
| 642 | } |
| 643 | |
| 644 | |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 645 | // OPTIMIZE: if this loops to only one other angle, after first compare fails, insert on other side |
| 646 | // OPTIMIZE: return where insertion succeeded. Then, start next insertion on opposite side |
| 647 | void SkOpAngle::insert(SkOpAngle* angle) { |
| 648 | if (angle->fNext) { |
| 649 | if (loopCount() >= angle->loopCount()) { |
| 650 | if (!merge(angle)) { |
| 651 | return; |
| 652 | } |
| 653 | } else if (fNext) { |
| 654 | if (!angle->merge(this)) { |
| 655 | return; |
| 656 | } |
| 657 | } else { |
| 658 | angle->insert(this); |
| 659 | } |
| 660 | return; |
| 661 | } |
| 662 | bool singleton = NULL == fNext; |
| 663 | if (singleton) { |
| 664 | fNext = this; |
| 665 | } |
| 666 | SkOpAngle* next = fNext; |
| 667 | if (next->fNext == this) { |
| 668 | if (singleton || angle->after(this)) { |
| 669 | this->fNext = angle; |
| 670 | angle->fNext = next; |
| 671 | } else { |
| 672 | next->fNext = angle; |
| 673 | angle->fNext = this; |
| 674 | } |
| 675 | debugValidateNext(); |
| 676 | return; |
| 677 | } |
| 678 | SkOpAngle* last = this; |
| 679 | do { |
| 680 | SkASSERT(last->fNext == next); |
| 681 | if (angle->after(last)) { |
| 682 | last->fNext = angle; |
| 683 | angle->fNext = next; |
| 684 | debugValidateNext(); |
| 685 | return; |
| 686 | } |
| 687 | last = next; |
| 688 | next = next->fNext; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 689 | if (last == this) { |
| 690 | if (next->fUnorderable) { |
| 691 | fUnorderable = true; |
| 692 | } else { |
| 693 | globalState()->setAngleCoincidence(); |
| 694 | this->fNext = angle; |
| 695 | angle->fNext = next; |
| 696 | angle->fCheckCoincidence = true; |
| 697 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 698 | return; |
| 699 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 700 | } while (true); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 701 | } |
| 702 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 703 | SkOpSpanBase* SkOpAngle::lastMarked() const { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 704 | if (fLastMarked) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 705 | if (fLastMarked->chased()) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 706 | return NULL; |
| 707 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 708 | fLastMarked->setChased(true); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 709 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 710 | return fLastMarked; |
| 711 | } |
| 712 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 713 | bool SkOpAngle::loopContains(const SkOpAngle* angle) const { |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 714 | if (!fNext) { |
| 715 | return false; |
| 716 | } |
| 717 | const SkOpAngle* first = this; |
| 718 | const SkOpAngle* loop = this; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 719 | const SkOpSegment* tSegment = angle->fStart->segment(); |
| 720 | double tStart = angle->fStart->t(); |
| 721 | double tEnd = angle->fEnd->t(); |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 722 | do { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 723 | const SkOpSegment* lSegment = loop->fStart->segment(); |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 724 | if (lSegment != tSegment) { |
| 725 | continue; |
| 726 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 727 | double lStart = loop->fStart->t(); |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 728 | if (lStart != tEnd) { |
| 729 | continue; |
| 730 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 731 | double lEnd = loop->fEnd->t(); |
commit-bot@chromium.org | 8cb1daa | 2014-04-25 12:59:11 +0000 | [diff] [blame] | 732 | if (lEnd == tStart) { |
| 733 | return true; |
| 734 | } |
| 735 | } while ((loop = loop->fNext) != first); |
| 736 | return false; |
| 737 | } |
| 738 | |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 739 | int SkOpAngle::loopCount() const { |
| 740 | int count = 0; |
| 741 | const SkOpAngle* first = this; |
| 742 | const SkOpAngle* next = this; |
| 743 | do { |
| 744 | next = next->fNext; |
| 745 | ++count; |
| 746 | } while (next && next != first); |
| 747 | return count; |
| 748 | } |
| 749 | |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 750 | bool SkOpAngle::merge(SkOpAngle* angle) { |
| 751 | SkASSERT(fNext); |
| 752 | SkASSERT(angle->fNext); |
| 753 | SkOpAngle* working = angle; |
| 754 | do { |
| 755 | if (this == working) { |
| 756 | return false; |
| 757 | } |
| 758 | working = working->fNext; |
| 759 | } while (working != angle); |
| 760 | do { |
| 761 | SkOpAngle* next = working->fNext; |
| 762 | working->fNext = NULL; |
| 763 | insert(working); |
| 764 | working = next; |
| 765 | } while (working != angle); |
| 766 | // it's likely that a pair of the angles are unorderable |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 767 | debugValidateNext(); |
| 768 | return true; |
| 769 | } |
| 770 | |
| 771 | double SkOpAngle::midT() const { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 772 | return (fStart->t() + fEnd->t()) / 2; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 773 | } |
| 774 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 775 | bool SkOpAngle::midToSide(const SkOpAngle* rh, bool* inside) const { |
| 776 | const SkOpSegment* segment = this->segment(); |
| 777 | SkPath::Verb verb = segment->verb(); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 778 | const SkPoint& startPt = this->fStart->pt(); |
| 779 | const SkPoint& endPt = this->fEnd->pt(); |
| 780 | SkDPoint dStartPt; |
| 781 | dStartPt.set(startPt); |
| 782 | SkDLine rayMid; |
| 783 | rayMid[0].fX = (startPt.fX + endPt.fX) / 2; |
| 784 | rayMid[0].fY = (startPt.fY + endPt.fY) / 2; |
| 785 | rayMid[1].fX = rayMid[0].fX + (endPt.fY - startPt.fY); |
| 786 | rayMid[1].fY = rayMid[0].fY - (endPt.fX - startPt.fX); |
| 787 | SkIntersections iMid; |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 788 | (*CurveIntersectRay[verb])(segment->pts(), segment->weight(), rayMid, &iMid); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 789 | int iOutside = iMid.mostOutside(this->fStart->t(), this->fEnd->t(), dStartPt); |
| 790 | if (iOutside < 0) { |
| 791 | return false; |
| 792 | } |
| 793 | const SkOpSegment* oppSegment = rh->segment(); |
| 794 | SkPath::Verb oppVerb = oppSegment->verb(); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 795 | SkIntersections oppMid; |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 796 | (*CurveIntersectRay[oppVerb])(oppSegment->pts(), oppSegment->weight(), rayMid, &oppMid); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 797 | int oppOutside = oppMid.mostOutside(rh->fStart->t(), rh->fEnd->t(), dStartPt); |
| 798 | if (oppOutside < 0) { |
| 799 | return false; |
| 800 | } |
| 801 | SkDVector iSide = iMid.pt(iOutside) - dStartPt; |
| 802 | SkDVector oppSide = oppMid.pt(oppOutside) - dStartPt; |
| 803 | double dir = iSide.crossCheck(oppSide); |
| 804 | if (!dir) { |
| 805 | return false; |
| 806 | } |
| 807 | *inside = dir < 0; |
| 808 | return true; |
| 809 | } |
| 810 | |
| 811 | bool SkOpAngle::oppositePlanes(const SkOpAngle* rh) const { |
| 812 | int startSpan = abs(rh->fSectorStart - fSectorStart); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 813 | return startSpan >= 8; |
| 814 | } |
| 815 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 816 | bool SkOpAngle::orderable(SkOpAngle* rh) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 817 | int result; |
| 818 | if (!fIsCurve) { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 819 | if (!rh->fIsCurve) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 820 | double leftX = fTangentHalf.dx(); |
| 821 | double leftY = fTangentHalf.dy(); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 822 | double rightX = rh->fTangentHalf.dx(); |
| 823 | double rightY = rh->fTangentHalf.dy(); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 824 | double x_ry = leftX * rightY; |
| 825 | double rx_y = rightX * leftY; |
| 826 | if (x_ry == rx_y) { |
| 827 | if (leftX * rightX < 0 || leftY * rightY < 0) { |
| 828 | return true; // exactly 180 degrees apart |
| 829 | } |
| 830 | goto unorderable; |
| 831 | } |
| 832 | SkASSERT(x_ry != rx_y); // indicates an undetected coincidence -- worth finding earlier |
| 833 | return x_ry < rx_y; |
| 834 | } |
| 835 | if ((result = allOnOneSide(rh)) >= 0) { |
| 836 | return result; |
| 837 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 838 | if (fUnorderable || approximately_zero(rh->fSide)) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 839 | goto unorderable; |
| 840 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 841 | } else if (!rh->fIsCurve) { |
| 842 | if ((result = rh->allOnOneSide(this)) >= 0) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 843 | return !result; |
| 844 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 845 | if (rh->fUnorderable || approximately_zero(fSide)) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 846 | goto unorderable; |
| 847 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 848 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 849 | if ((result = convexHullOverlaps(rh)) >= 0) { |
| 850 | return result; |
| 851 | } |
| 852 | return endsIntersect(rh); |
| 853 | unorderable: |
| 854 | fUnorderable = true; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 855 | rh->fUnorderable = true; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 856 | return true; |
| 857 | } |
| 858 | |
| 859 | // OPTIMIZE: if this shows up in a profile, add a previous pointer |
| 860 | // as is, this should be rarely called |
| 861 | SkOpAngle* SkOpAngle::previous() const { |
| 862 | SkOpAngle* last = fNext; |
| 863 | do { |
| 864 | SkOpAngle* next = last->fNext; |
| 865 | if (next == this) { |
| 866 | return last; |
| 867 | } |
| 868 | last = next; |
| 869 | } while (true); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 870 | } |
| 871 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 872 | SkOpSegment* SkOpAngle::segment() const { |
| 873 | return fStart->segment(); |
| 874 | } |
| 875 | |
| 876 | void SkOpAngle::set(SkOpSpanBase* start, SkOpSpanBase* end) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 877 | fStart = start; |
caryclark | dac1d17 | 2014-06-17 05:15:38 -0700 | [diff] [blame] | 878 | fComputedEnd = fEnd = end; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 879 | SkASSERT(start != end); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 880 | fNext = NULL; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 881 | fComputeSector = fComputedSector = fCheckCoincidence = false; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 882 | setSpans(); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 883 | setSector(); |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 884 | SkDEBUGCODE(fID = start ? start->globalState()->nextAngleID() : -1); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | void SkOpAngle::setCurveHullSweep() { |
| 888 | fUnorderedSweep = false; |
| 889 | fSweep[0] = fCurvePart[1] - fCurvePart[0]; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 890 | const SkOpSegment* segment = fStart->segment(); |
| 891 | if (SkPath::kLine_Verb == segment->verb()) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 892 | fSweep[1] = fSweep[0]; |
| 893 | return; |
| 894 | } |
| 895 | fSweep[1] = fCurvePart[2] - fCurvePart[0]; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 896 | if (SkPath::kCubic_Verb != segment->verb()) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 897 | if (!fSweep[0].fX && !fSweep[0].fY) { |
| 898 | fSweep[0] = fSweep[1]; |
| 899 | } |
| 900 | return; |
| 901 | } |
| 902 | SkDVector thirdSweep = fCurvePart[3] - fCurvePart[0]; |
| 903 | if (fSweep[0].fX == 0 && fSweep[0].fY == 0) { |
| 904 | fSweep[0] = fSweep[1]; |
| 905 | fSweep[1] = thirdSweep; |
| 906 | if (fSweep[0].fX == 0 && fSweep[0].fY == 0) { |
| 907 | fSweep[0] = fSweep[1]; |
| 908 | fCurvePart[1] = fCurvePart[3]; |
| 909 | fIsCurve = false; |
| 910 | } |
| 911 | return; |
| 912 | } |
| 913 | double s1x3 = fSweep[0].crossCheck(thirdSweep); |
| 914 | double s3x2 = thirdSweep.crossCheck(fSweep[1]); |
| 915 | if (s1x3 * s3x2 >= 0) { // if third vector is on or between first two vectors |
| 916 | return; |
| 917 | } |
| 918 | double s2x1 = fSweep[1].crossCheck(fSweep[0]); |
| 919 | // FIXME: If the sweep of the cubic is greater than 180 degrees, we're in trouble |
| 920 | // probably such wide sweeps should be artificially subdivided earlier so that never happens |
| 921 | SkASSERT(s1x3 * s2x1 < 0 || s1x3 * s3x2 < 0); |
| 922 | if (s3x2 * s2x1 < 0) { |
| 923 | SkASSERT(s2x1 * s1x3 > 0); |
| 924 | fSweep[0] = fSweep[1]; |
| 925 | fUnorderedSweep = true; |
| 926 | } |
| 927 | fSweep[1] = thirdSweep; |
| 928 | } |
| 929 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 930 | void SkOpAngle::setSpans() { |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 931 | fUnorderable = false; |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 932 | fLastMarked = NULL; |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 933 | if (!fStart) { |
| 934 | fUnorderable = true; |
| 935 | return; |
| 936 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 937 | const SkOpSegment* segment = fStart->segment(); |
| 938 | const SkPoint* pts = segment->pts(); |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 939 | SkDEBUGCODE(fCurvePart.fVerb = SkPath::kCubic_Verb); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 940 | SkDEBUGCODE(fCurvePart[2].fX = fCurvePart[2].fY = fCurvePart[3].fX = fCurvePart[3].fY |
| 941 | = SK_ScalarNaN); |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 942 | SkDEBUGCODE(fCurvePart.fVerb = segment->verb()); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 943 | segment->subDivide(fStart, fEnd, &fCurvePart); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 944 | setCurveHullSweep(); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 945 | const SkPath::Verb verb = segment->verb(); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 946 | if (verb != SkPath::kLine_Verb |
| 947 | && !(fIsCurve = fSweep[0].crossCheck(fSweep[1]) != 0)) { |
| 948 | SkDLine lineHalf; |
| 949 | lineHalf[0].set(fCurvePart[0].asSkPoint()); |
| 950 | lineHalf[1].set(fCurvePart[SkPathOpsVerbToPoints(verb)].asSkPoint()); |
| 951 | fTangentHalf.lineEndPoints(lineHalf); |
| 952 | fSide = 0; |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 953 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 954 | switch (verb) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 955 | case SkPath::kLine_Verb: { |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 956 | SkASSERT(fStart != fEnd); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 957 | const SkPoint& cP1 = pts[fStart->t() < fEnd->t()]; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 958 | SkDLine lineHalf; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 959 | lineHalf[0].set(fStart->pt()); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 960 | lineHalf[1].set(cP1); |
| 961 | fTangentHalf.lineEndPoints(lineHalf); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 962 | fSide = 0; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 963 | fIsCurve = false; |
| 964 | } return; |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 965 | case SkPath::kQuad_Verb: |
| 966 | case SkPath::kConic_Verb: { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 967 | SkLineParameters tangentPart; |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 968 | (void) tangentPart.quadEndPoints(fCurvePart.fQuad); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 969 | fSide = -tangentPart.pointDistance(fCurvePart[2]); // not normalized -- compare sign only |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 970 | } break; |
| 971 | case SkPath::kCubic_Verb: { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 972 | SkLineParameters tangentPart; |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 973 | (void) tangentPart.cubicPart(fCurvePart.fCubic); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 974 | fSide = -tangentPart.pointDistance(fCurvePart[3]); |
caryclark@google.com | b3f0921 | 2013-04-17 15:49:16 +0000 | [diff] [blame] | 975 | double testTs[4]; |
| 976 | // OPTIMIZATION: keep inflections precomputed with cubic segment? |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 977 | int testCount = SkDCubic::FindInflections(pts, testTs); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 978 | double startT = fStart->t(); |
| 979 | double endT = fEnd->t(); |
caryclark@google.com | b3f0921 | 2013-04-17 15:49:16 +0000 | [diff] [blame] | 980 | double limitT = endT; |
| 981 | int index; |
| 982 | for (index = 0; index < testCount; ++index) { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 983 | if (!::between(startT, testTs[index], limitT)) { |
caryclark@google.com | b3f0921 | 2013-04-17 15:49:16 +0000 | [diff] [blame] | 984 | testTs[index] = -1; |
| 985 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 986 | } |
caryclark@google.com | b3f0921 | 2013-04-17 15:49:16 +0000 | [diff] [blame] | 987 | testTs[testCount++] = startT; |
| 988 | testTs[testCount++] = endT; |
commit-bot@chromium.org | b76d3b6 | 2013-04-22 19:55:19 +0000 | [diff] [blame] | 989 | SkTQSort<double>(testTs, &testTs[testCount - 1]); |
caryclark@google.com | b3f0921 | 2013-04-17 15:49:16 +0000 | [diff] [blame] | 990 | double bestSide = 0; |
| 991 | int testCases = (testCount << 1) - 1; |
| 992 | index = 0; |
| 993 | while (testTs[index] < 0) { |
| 994 | ++index; |
| 995 | } |
| 996 | index <<= 1; |
| 997 | for (; index < testCases; ++index) { |
| 998 | int testIndex = index >> 1; |
| 999 | double testT = testTs[testIndex]; |
| 1000 | if (index & 1) { |
| 1001 | testT = (testT + testTs[testIndex + 1]) / 2; |
| 1002 | } |
| 1003 | // OPTIMIZE: could avoid call for t == startT, endT |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 1004 | SkDPoint pt = dcubic_xy_at_t(pts, segment->weight(), testT); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1005 | SkLineParameters tangentPart; |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 1006 | tangentPart.cubicEndPoints(fCurvePart.fCubic); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1007 | double testSide = tangentPart.pointDistance(pt); |
caryclark@google.com | b3f0921 | 2013-04-17 15:49:16 +0000 | [diff] [blame] | 1008 | if (fabs(bestSide) < fabs(testSide)) { |
| 1009 | bestSide = testSide; |
| 1010 | } |
| 1011 | } |
| 1012 | fSide = -bestSide; // compare sign only |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1013 | } break; |
| 1014 | default: |
| 1015 | SkASSERT(0); |
| 1016 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 1017 | } |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 1018 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1019 | void SkOpAngle::setSector() { |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 1020 | if (!fStart) { |
| 1021 | fUnorderable = true; |
| 1022 | return; |
| 1023 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1024 | const SkOpSegment* segment = fStart->segment(); |
| 1025 | SkPath::Verb verb = segment->verb(); |
| 1026 | fSectorStart = this->findSector(verb, fSweep[0].fX, fSweep[0].fY); |
| 1027 | if (fSectorStart < 0) { |
| 1028 | goto deferTilLater; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1029 | } |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1030 | if (!fIsCurve) { // if it's a line or line-like, note that both sectors are the same |
| 1031 | SkASSERT(fSectorStart >= 0); |
| 1032 | fSectorEnd = fSectorStart; |
| 1033 | fSectorMask = 1 << fSectorStart; |
| 1034 | return; |
| 1035 | } |
| 1036 | SkASSERT(SkPath::kLine_Verb != verb); |
| 1037 | fSectorEnd = this->findSector(verb, fSweep[1].fX, fSweep[1].fY); |
| 1038 | if (fSectorEnd < 0) { |
| 1039 | deferTilLater: |
| 1040 | fSectorStart = fSectorEnd = -1; |
| 1041 | fSectorMask = 0; |
| 1042 | fComputeSector = true; // can't determine sector until segment length can be found |
| 1043 | return; |
| 1044 | } |
| 1045 | if (fSectorEnd == fSectorStart |
| 1046 | && (fSectorStart & 3) != 3) { // if the sector has no span, it can't be an exact angle |
| 1047 | fSectorMask = 1 << fSectorStart; |
| 1048 | return; |
| 1049 | } |
| 1050 | bool crossesZero = this->checkCrossesZero(); |
| 1051 | int start = SkTMin(fSectorStart, fSectorEnd); |
| 1052 | bool curveBendsCCW = (fSectorStart == start) ^ crossesZero; |
| 1053 | // bump the start and end of the sector span if they are on exact compass points |
| 1054 | if ((fSectorStart & 3) == 3) { |
| 1055 | fSectorStart = (fSectorStart + (curveBendsCCW ? 1 : 31)) & 0x1f; |
| 1056 | } |
| 1057 | if ((fSectorEnd & 3) == 3) { |
| 1058 | fSectorEnd = (fSectorEnd + (curveBendsCCW ? 31 : 1)) & 0x1f; |
| 1059 | } |
| 1060 | crossesZero = this->checkCrossesZero(); |
| 1061 | start = SkTMin(fSectorStart, fSectorEnd); |
| 1062 | int end = SkTMax(fSectorStart, fSectorEnd); |
| 1063 | if (!crossesZero) { |
| 1064 | fSectorMask = (unsigned) -1 >> (31 - end + start) << start; |
| 1065 | } else { |
| 1066 | fSectorMask = (unsigned) -1 >> (31 - start) | (-1 << end); |
| 1067 | } |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 1068 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1069 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1070 | SkOpSpan* SkOpAngle::starter() { |
| 1071 | return fStart->starter(fEnd); |
| 1072 | } |
| 1073 | |
| 1074 | bool SkOpAngle::tangentsDiverge(const SkOpAngle* rh, double s0xt0) const { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1075 | if (s0xt0 == 0) { |
| 1076 | return false; |
| 1077 | } |
| 1078 | // if the ctrl tangents are not nearly parallel, use them |
| 1079 | // solve for opposite direction displacement scale factor == m |
| 1080 | // initial dir = v1.cross(v2) == v2.x * v1.y - v2.y * v1.x |
| 1081 | // displacement of q1[1] : dq1 = { -m * v1.y, m * v1.x } + q1[1] |
| 1082 | // straight angle when : v2.x * (dq1.y - q1[0].y) == v2.y * (dq1.x - q1[0].x) |
| 1083 | // v2.x * (m * v1.x + v1.y) == v2.y * (-m * v1.y + v1.x) |
| 1084 | // - m * (v2.x * v1.x + v2.y * v1.y) == v2.x * v1.y - v2.y * v1.x |
| 1085 | // m = (v2.y * v1.x - v2.x * v1.y) / (v2.x * v1.x + v2.y * v1.y) |
| 1086 | // m = v1.cross(v2) / v1.dot(v2) |
| 1087 | const SkDVector* sweep = fSweep; |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1088 | const SkDVector* tweep = rh->fSweep; |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1089 | double s0dt0 = sweep[0].dot(tweep[0]); |
| 1090 | if (!s0dt0) { |
| 1091 | return true; |
| 1092 | } |
| 1093 | SkASSERT(s0dt0 != 0); |
| 1094 | double m = s0xt0 / s0dt0; |
| 1095 | double sDist = sweep[0].length() * m; |
| 1096 | double tDist = tweep[0].length() * m; |
| 1097 | bool useS = fabs(sDist) < fabs(tDist); |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 1098 | double mFactor = fabs(useS ? this->distEndRatio(sDist) : rh->distEndRatio(tDist)); |
caryclark | 1049f12 | 2015-04-20 08:31:59 -0700 | [diff] [blame] | 1099 | return mFactor < 2400; // empirically found limit |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 1100 | } |