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