blob: c07e8cc73f050a3681680109b8c1a38402e4a372 [file] [log] [blame]
caryclark@google.com07393ca2013-04-08 11:47:37 +00001/*
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.com07393ca2013-04-08 11:47:37 +00007#include "SkOpAngle.h"
caryclark@google.comcffbcc32013-06-04 17:59:42 +00008#include "SkOpSegment.h"
caryclark@google.com07393ca2013-04-08 11:47:37 +00009#include "SkPathOpsCurve.h"
commit-bot@chromium.orgb76d3b62013-04-22 19:55:19 +000010#include "SkTSort.h"
caryclark@google.com07393ca2013-04-08 11:47:37 +000011
caryclark@google.comcffbcc32013-06-04 17:59:42 +000012/* 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.com07393ca2013-04-08 11:47:37 +000014
caryclark@google.comcffbcc32013-06-04 17:59:42 +000015#if DEBUG_ANGLE
caryclark54359292015-03-26 07:52:43 -070016 static bool CompareResult(const char* func, SkString* bugOut, SkString* bugPart, int append,
17 bool compare) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000018 SkDebugf("%s %c %d\n", bugOut->c_str(), compare ? 'T' : 'F', append);
caryclark54359292015-03-26 07:52:43 -070019 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.comcffbcc32013-06-04 17:59:42 +000022 return compare;
23 }
24
caryclark54359292015-03-26 07:52:43 -070025 #define COMPARE_RESULT(append, compare) CompareResult(__FUNCTION__, &bugOut, bugPart, append, \
26 compare)
caryclark@google.comcffbcc32013-06-04 17:59:42 +000027#else
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +000028 #define COMPARE_RESULT(append, compare) compare
caryclark@google.comcffbcc32013-06-04 17:59:42 +000029#endif
30
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000031/* quarter angle values for sector
32
3331 x > 0, y == 0 horizontal line (to the right)
340 x > 0, y == epsilon quad/cubic horizontal tangent eventually going +y
351 x > 0, y > 0, x > y nearer horizontal angle
362 x + e == y quad/cubic 45 going horiz
373 x > 0, y > 0, x == y 45 angle
384 x == y + e quad/cubic 45 going vert
395 x > 0, y > 0, x < y nearer vertical angle
406 x == epsilon, y > 0 quad/cubic vertical tangent eventually going +x
417 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
caryclark54359292015-03-26 07:52:43 -070061bool SkOpAngle::after(SkOpAngle* test) {
62 SkOpAngle* lh = test;
63 SkOpAngle* rh = lh->fNext;
64 SkASSERT(lh != rh);
Cary Clark59d5a0e2017-01-23 14:38:52 +000065 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.org4431e772014-04-14 17:08:59 +000071#if DEBUG_ANGLE
72 SkString bugOut;
Cary Clark59d5a0e2017-01-23 14:38:52 +000073 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());
caryclark54359292015-03-26 07:52:43 -070081 SkString bugPart[3] = { lh->debugPart(), this->debugPart(), rh->debugPart() };
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000082#endif
caryclark54359292015-03-26 07:52:43 -070083 if (lh->fComputeSector && !lh->computeSector()) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000084 return COMPARE_RESULT(1, true);
85 }
caryclark54359292015-03-26 07:52:43 -070086 if (fComputeSector && !this->computeSector()) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000087 return COMPARE_RESULT(2, true);
88 }
caryclark54359292015-03-26 07:52:43 -070089 if (rh->fComputeSector && !rh->computeSector()) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000090 return COMPARE_RESULT(3, true);
91 }
92#if DEBUG_ANGLE // reset bugOut with computed sectors
Cary Clark59d5a0e2017-01-23 14:38:52 +000093 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.org4431e772014-04-14 17:08:59 +0000101#endif
Cary Clark59d5a0e2017-01-23 14:38:52 +0000102 bool ltrOverlap = (lh->fSectorMask | rh->fSectorMask) & fSectorMask;
103 bool lrOverlap = lh->fSectorMask & rh->fSectorMask;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000104 int lrOrder; // set to -1 if either order works
Cary Clark59d5a0e2017-01-23 14:38:52 +0000105 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.org4431e772014-04-14 17:08:59 +0000109 }
caryclark54359292015-03-26 07:52:43 -0700110 int lrGap = (rh->fSectorStart - lh->fSectorStart + 32) & 0x1f;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000111 /* 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 {
caryclark54359292015-03-26 07:52:43 -0700123 lrOrder = (int) lh->orderable(rh);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000124 if (!ltrOverlap) {
Cary Clark59d5a0e2017-01-23 14:38:52 +0000125 return COMPARE_RESULT(5, !lrOrder);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000126 }
127 }
128 int ltOrder;
Cary Clark59d5a0e2017-01-23 14:38:52 +0000129 SkASSERT((lh->fSectorMask & fSectorMask) || (rh->fSectorMask & fSectorMask));
caryclark54359292015-03-26 07:52:43 -0700130 if (lh->fSectorMask & fSectorMask) {
131 ltOrder = (int) lh->orderable(this);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000132 } else {
caryclark54359292015-03-26 07:52:43 -0700133 int ltGap = (fSectorStart - lh->fSectorStart + 32) & 0x1f;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000134 ltOrder = ltGap > 20 ? 0 : ltGap > 11 ? -1 : 1;
135 }
136 int trOrder;
caryclark54359292015-03-26 07:52:43 -0700137 if (rh->fSectorMask & fSectorMask) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000138 trOrder = (int) orderable(rh);
139 } else {
caryclark54359292015-03-26 07:52:43 -0700140 int trGap = (rh->fSectorStart - fSectorStart + 32) & 0x1f;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000141 trOrder = trGap > 20 ? 0 : trGap > 11 ? -1 : 1;
142 }
Cary Clarkff114282016-12-14 11:56:16 -0500143 this->alignmentSameSide(lh, &ltOrder);
144 this->alignmentSameSide(rh, &trOrder);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000145 if (lrOrder >= 0 && ltOrder >= 0 && trOrder >= 0) {
Cary Clark59d5a0e2017-01-23 14:38:52 +0000146 return COMPARE_RESULT(7, lrOrder ? (ltOrder & trOrder) : (ltOrder | trOrder));
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000147 }
148 SkASSERT(lrOrder >= 0 || ltOrder >= 0 || trOrder >= 0);
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
caryclark54359292015-03-26 07:52:43 -0700155 SkDEBUGCODE(bool lrOpposite = lh->oppositePlanes(rh));
156 bool ltOpposite = lh->oppositePlanes(this);
caryclarka35ab3e2016-10-20 08:32:18 -0700157 SkOPASSERT(lrOpposite != ltOpposite);
Cary Clark59d5a0e2017-01-23 14:38:52 +0000158 return COMPARE_RESULT(8, ltOpposite);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000159 } else if (ltOrder == 1 && trOrder == 0) {
160 SkASSERT(lrOrder < 0);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000161 bool trOpposite = oppositePlanes(rh);
Cary Clark59d5a0e2017-01-23 14:38:52 +0000162 return COMPARE_RESULT(9, trOpposite);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000163 } else if (lrOrder == 1 && trOrder == 1) {
164 SkASSERT(ltOrder < 0);
caryclark78a37a52016-10-20 12:36:16 -0700165// SkDEBUGCODE(bool trOpposite = oppositePlanes(rh));
caryclark54359292015-03-26 07:52:43 -0700166 bool lrOpposite = lh->oppositePlanes(rh);
caryclark78a37a52016-10-20 12:36:16 -0700167// SkASSERT(lrOpposite != trOpposite);
Cary Clark59d5a0e2017-01-23 14:38:52 +0000168 return COMPARE_RESULT(10, lrOpposite);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000169 }
170 if (lrOrder < 0) {
171 if (ltOrder < 0) {
Cary Clark59d5a0e2017-01-23 14:38:52 +0000172 return COMPARE_RESULT(11, trOrder);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000173 }
Cary Clark59d5a0e2017-01-23 14:38:52 +0000174 return COMPARE_RESULT(12, ltOrder);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000175 }
Cary Clark59d5a0e2017-01-23 14:38:52 +0000176 return COMPARE_RESULT(13, !lrOrder);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000177}
178
179// given a line, see if the opposite curve's convex hull is all on one side
180// returns -1=not on one side 0=this CW of test 1=this CCW of test
caryclark54359292015-03-26 07:52:43 -0700181int SkOpAngle::allOnOneSide(const SkOpAngle* test) {
caryclarkeed356d2016-09-14 07:18:20 -0700182 SkASSERT(!fPart.isCurve());
183 SkASSERT(test->fPart.isCurve());
184 SkDPoint origin = fPart.fCurve[0];
185 SkDVector line = fPart.fCurve[1] - origin;
caryclark81681942016-07-21 10:44:07 -0700186 double crosses[3];
caryclark54359292015-03-26 07:52:43 -0700187 SkPath::Verb testVerb = test->segment()->verb();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000188 int iMax = SkPathOpsVerbToPoints(testVerb);
189// SkASSERT(origin == test.fCurveHalf[0]);
caryclarkeed356d2016-09-14 07:18:20 -0700190 const SkDCurve& testCurve = test->fPart.fCurve;
caryclark54359292015-03-26 07:52:43 -0700191 for (int index = 1; index <= iMax; ++index) {
caryclark81681942016-07-21 10:44:07 -0700192 double xy1 = line.fX * (testCurve[index].fY - origin.fY);
193 double xy2 = line.fY * (testCurve[index].fX - origin.fX);
194 crosses[index - 1] = AlmostBequalUlps(xy1, xy2) ? 0 : xy1 - xy2;
caryclark54359292015-03-26 07:52:43 -0700195 }
196 if (crosses[0] * crosses[1] < 0) {
197 return -1;
198 }
199 if (SkPath::kCubic_Verb == testVerb) {
200 if (crosses[0] * crosses[2] < 0 || crosses[1] * crosses[2] < 0) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000201 return -1;
202 }
caryclark54359292015-03-26 07:52:43 -0700203 }
204 if (crosses[0]) {
205 return crosses[0] < 0;
206 }
207 if (crosses[1]) {
208 return crosses[1] < 0;
209 }
210 if (SkPath::kCubic_Verb == testVerb && crosses[2]) {
211 return crosses[2] < 0;
212 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000213 fUnorderable = true;
214 return -1;
215}
216
Cary Clarkff114282016-12-14 11:56:16 -0500217// To sort the angles, all curves are translated to have the same starting point.
218// If the curve's control point in its original position is on one side of a compared line,
219// and translated is on the opposite side, reverse the previously computed order.
220void SkOpAngle::alignmentSameSide(const SkOpAngle* test, int* order) const {
221 if (*order < 0) {
222 return;
223 }
224 if (fPart.isCurve()) {
225 // This should support all curve types, but only bug that requires this has lines
226 // Turning on for curves causes existing tests to fail
227 return;
228 }
229 if (test->fPart.isCurve()) {
230 return;
231 }
232 const SkDPoint& xOrigin = test->fPart.fCurve.fLine[0];
233 const SkDPoint& oOrigin = test->fOriginalCurvePart.fLine[0];
234 if (xOrigin == oOrigin) {
235 return;
236 }
237 int iMax = SkPathOpsVerbToPoints(this->segment()->verb());
238 SkDVector xLine = test->fPart.fCurve.fLine[1] - xOrigin;
239 SkDVector oLine = test->fOriginalCurvePart.fLine[1] - oOrigin;
240 for (int index = 1; index <= iMax; ++index) {
241 const SkDPoint& testPt = fPart.fCurve[index];
242 double xCross = oLine.crossCheck(testPt - xOrigin);
243 double oCross = xLine.crossCheck(testPt - oOrigin);
244 if (oCross * xCross < 0) {
245 *order ^= 1;
246 break;
247 }
248 }
249}
250
Cary Clark59d5a0e2017-01-23 14:38:52 +0000251bool SkOpAngle::checkCrossesZero() const {
252 int start = SkTMin(fSectorStart, fSectorEnd);
253 int end = SkTMax(fSectorStart, fSectorEnd);
254 bool crossesZero = end - start > 16;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000255 return crossesZero;
256}
caryclark@google.com07393ca2013-04-08 11:47:37 +0000257
caryclark54359292015-03-26 07:52:43 -0700258bool SkOpAngle::checkParallel(SkOpAngle* rh) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000259 SkDVector scratch[2];
260 const SkDVector* sweep, * tweep;
caryclarkeed356d2016-09-14 07:18:20 -0700261 if (this->fPart.isOrdered()) {
262 sweep = this->fPart.fSweep;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000263 } else {
caryclarkeed356d2016-09-14 07:18:20 -0700264 scratch[0] = this->fPart.fCurve[1] - this->fPart.fCurve[0];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000265 sweep = &scratch[0];
caryclark@google.coma5e55922013-05-07 18:51:31 +0000266 }
caryclarkeed356d2016-09-14 07:18:20 -0700267 if (rh->fPart.isOrdered()) {
268 tweep = rh->fPart.fSweep;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000269 } else {
caryclarkeed356d2016-09-14 07:18:20 -0700270 scratch[1] = rh->fPart.fCurve[1] - rh->fPart.fCurve[0];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000271 tweep = &scratch[1];
caryclark@google.com07393ca2013-04-08 11:47:37 +0000272 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000273 double s0xt0 = sweep->crossCheck(*tweep);
274 if (tangentsDiverge(rh, s0xt0)) {
275 return s0xt0 < 0;
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +0000276 }
caryclark54359292015-03-26 07:52:43 -0700277 // compute the perpendicular to the endpoints and see where it intersects the opposite curve
278 // if the intersections within the t range, do a cross check on those
279 bool inside;
caryclark55888e42016-07-18 10:01:36 -0700280 if (!fEnd->contains(rh->fEnd)) {
caryclark1049f122015-04-20 08:31:59 -0700281 if (this->endToSide(rh, &inside)) {
282 return inside;
283 }
284 if (rh->endToSide(this, &inside)) {
285 return !inside;
286 }
caryclark54359292015-03-26 07:52:43 -0700287 }
288 if (this->midToSide(rh, &inside)) {
289 return inside;
290 }
291 if (rh->midToSide(this, &inside)) {
292 return !inside;
293 }
294 // compute the cross check from the mid T values (last resort)
caryclarkeed356d2016-09-14 07:18:20 -0700295 SkDVector m0 = segment()->dPtAtT(this->midT()) - this->fPart.fCurve[0];
296 SkDVector m1 = rh->segment()->dPtAtT(rh->midT()) - rh->fPart.fCurve[0];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000297 double m0xm1 = m0.crossCheck(m1);
298 if (m0xm1 == 0) {
caryclark54359292015-03-26 07:52:43 -0700299 this->fUnorderable = true;
300 rh->fUnorderable = true;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000301 return true;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000302 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000303 return m0xm1 < 0;
304}
305
306// the original angle is too short to get meaningful sector information
307// lengthen it until it is long enough to be meaningful or leave it unset if lengthening it
308// would cause it to intersect one of the adjacent angles
309bool SkOpAngle::computeSector() {
310 if (fComputedSector) {
caryclarkdac1d172014-06-17 05:15:38 -0700311 return !fUnorderable;
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000312 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000313 fComputedSector = true;
caryclark54359292015-03-26 07:52:43 -0700314 bool stepUp = fStart->t() < fEnd->t();
caryclark55888e42016-07-18 10:01:36 -0700315 SkOpSpanBase* checkEnd = fEnd;
caryclark54359292015-03-26 07:52:43 -0700316 if (checkEnd->final() && stepUp) {
caryclarkccec0f92015-03-24 07:28:17 -0700317 fUnorderable = true;
318 return false;
319 }
caryclark54359292015-03-26 07:52:43 -0700320 do {
321// advance end
322 const SkOpSegment* other = checkEnd->segment();
323 const SkOpSpanBase* oSpan = other->head();
324 do {
325 if (oSpan->segment() != segment()) {
326 continue;
327 }
328 if (oSpan == checkEnd) {
329 continue;
330 }
331 if (!approximately_equal(oSpan->t(), checkEnd->t())) {
332 continue;
333 }
334 goto recomputeSector;
335 } while (!oSpan->final() && (oSpan = oSpan->upCast()->next()));
336 checkEnd = stepUp ? !checkEnd->final()
halcanary96fcdcc2015-08-27 07:41:13 -0700337 ? checkEnd->upCast()->next() : nullptr
caryclark54359292015-03-26 07:52:43 -0700338 : checkEnd->prev();
339 } while (checkEnd);
340recomputeSector:
caryclark55888e42016-07-18 10:01:36 -0700341 SkOpSpanBase* computedEnd = stepUp ? checkEnd ? checkEnd->prev() : fEnd->segment()->head()
caryclark54359292015-03-26 07:52:43 -0700342 : checkEnd ? checkEnd->upCast()->next() : fEnd->segment()->tail();
343 if (checkEnd == fEnd || computedEnd == fEnd || computedEnd == fStart) {
344 fUnorderable = true;
345 return false;
346 }
caryclark624637c2015-05-11 07:21:27 -0700347 if (stepUp != (fStart->t() < computedEnd->t())) {
348 fUnorderable = true;
349 return false;
350 }
caryclark54359292015-03-26 07:52:43 -0700351 SkOpSpanBase* saveEnd = fEnd;
352 fComputedEnd = fEnd = computedEnd;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000353 setSpans();
354 setSector();
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000355 fEnd = saveEnd;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000356 return !fUnorderable;
357}
358
caryclarkb36a3cd2016-10-18 07:59:44 -0700359int SkOpAngle::convexHullOverlaps(const SkOpAngle* rh) {
caryclarkeed356d2016-09-14 07:18:20 -0700360 const SkDVector* sweep = this->fPart.fSweep;
361 const SkDVector* tweep = rh->fPart.fSweep;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000362 double s0xs1 = sweep[0].crossCheck(sweep[1]);
363 double s0xt0 = sweep[0].crossCheck(tweep[0]);
364 double s1xt0 = sweep[1].crossCheck(tweep[0]);
365 bool tBetweenS = s0xs1 > 0 ? s0xt0 > 0 && s1xt0 < 0 : s0xt0 < 0 && s1xt0 > 0;
366 double s0xt1 = sweep[0].crossCheck(tweep[1]);
367 double s1xt1 = sweep[1].crossCheck(tweep[1]);
368 tBetweenS |= s0xs1 > 0 ? s0xt1 > 0 && s1xt1 < 0 : s0xt1 < 0 && s1xt1 > 0;
369 double t0xt1 = tweep[0].crossCheck(tweep[1]);
370 if (tBetweenS) {
371 return -1;
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000372 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000373 if ((s0xt0 == 0 && s1xt1 == 0) || (s1xt0 == 0 && s0xt1 == 0)) { // s0 to s1 equals t0 to t1
374 return -1;
375 }
376 bool sBetweenT = t0xt1 > 0 ? s0xt0 < 0 && s0xt1 > 0 : s0xt0 > 0 && s0xt1 < 0;
377 sBetweenT |= t0xt1 > 0 ? s1xt0 < 0 && s1xt1 > 0 : s1xt0 > 0 && s1xt1 < 0;
378 if (sBetweenT) {
379 return -1;
380 }
381 // if all of the sweeps are in the same half plane, then the order of any pair is enough
382 if (s0xt0 >= 0 && s0xt1 >= 0 && s1xt0 >= 0 && s1xt1 >= 0) {
383 return 0;
384 }
385 if (s0xt0 <= 0 && s0xt1 <= 0 && s1xt0 <= 0 && s1xt1 <= 0) {
386 return 1;
387 }
388 // if the outside sweeps are greater than 180 degress:
389 // first assume the inital tangents are the ordering
390 // if the midpoint direction matches the inital order, that is enough
caryclarkeed356d2016-09-14 07:18:20 -0700391 SkDVector m0 = this->segment()->dPtAtT(this->midT()) - this->fPart.fCurve[0];
392 SkDVector m1 = rh->segment()->dPtAtT(rh->midT()) - rh->fPart.fCurve[0];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000393 double m0xm1 = m0.crossCheck(m1);
394 if (s0xt0 > 0 && m0xm1 > 0) {
395 return 0;
396 }
397 if (s0xt0 < 0 && m0xm1 < 0) {
398 return 1;
399 }
400 if (tangentsDiverge(rh, s0xt0)) {
401 return s0xt0 < 0;
402 }
403 return m0xm1 < 0;
404}
405
406// OPTIMIZATION: longest can all be either lazily computed here or precomputed in setup
407double SkOpAngle::distEndRatio(double dist) const {
408 double longest = 0;
409 const SkOpSegment& segment = *this->segment();
410 int ptCount = SkPathOpsVerbToPoints(segment.verb());
411 const SkPoint* pts = segment.pts();
412 for (int idx1 = 0; idx1 <= ptCount - 1; ++idx1) {
413 for (int idx2 = idx1 + 1; idx2 <= ptCount; ++idx2) {
414 if (idx1 == idx2) {
415 continue;
416 }
417 SkDVector v;
418 v.set(pts[idx2] - pts[idx1]);
419 double lenSq = v.lengthSquared();
420 longest = SkTMax(longest, lenSq);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000421 }
422 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000423 return sqrt(longest) / dist;
424}
425
caryclark54359292015-03-26 07:52:43 -0700426bool SkOpAngle::endsIntersect(SkOpAngle* rh) {
427 SkPath::Verb lVerb = this->segment()->verb();
428 SkPath::Verb rVerb = rh->segment()->verb();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000429 int lPts = SkPathOpsVerbToPoints(lVerb);
430 int rPts = SkPathOpsVerbToPoints(rVerb);
caryclarkeed356d2016-09-14 07:18:20 -0700431 SkDLine rays[] = {{{this->fPart.fCurve[0], rh->fPart.fCurve[rPts]}},
432 {{this->fPart.fCurve[0], this->fPart.fCurve[lPts]}}};
caryclark55888e42016-07-18 10:01:36 -0700433 if (this->fEnd->contains(rh->fEnd)) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000434 return checkParallel(rh);
435 }
436 double smallTs[2] = {-1, -1};
437 bool limited[2] = {false, false};
438 for (int index = 0; index < 2; ++index) {
caryclark1049f122015-04-20 08:31:59 -0700439 SkPath::Verb cVerb = index ? rVerb : lVerb;
caryclark65f55312014-11-13 06:58:52 -0800440 // if the curve is a line, then the line and the ray intersect only at their crossing
caryclark1049f122015-04-20 08:31:59 -0700441 if (cVerb == SkPath::kLine_Verb) {
caryclark65f55312014-11-13 06:58:52 -0800442 continue;
443 }
caryclark54359292015-03-26 07:52:43 -0700444 const SkOpSegment& segment = index ? *rh->segment() : *this->segment();
445 SkIntersections i;
caryclark1049f122015-04-20 08:31:59 -0700446 (*CurveIntersectRay[cVerb])(segment.pts(), segment.weight(), rays[index], &i);
caryclark54359292015-03-26 07:52:43 -0700447 double tStart = index ? rh->fStart->t() : this->fStart->t();
448 double tEnd = index ? rh->fComputedEnd->t() : this->fComputedEnd->t();
449 bool testAscends = tStart < (index ? rh->fComputedEnd->t() : this->fComputedEnd->t());
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000450 double t = testAscends ? 0 : 1;
451 for (int idx2 = 0; idx2 < i.used(); ++idx2) {
452 double testT = i[0][idx2];
453 if (!approximately_between_orderable(tStart, testT, tEnd)) {
454 continue;
455 }
456 if (approximately_equal_orderable(tStart, testT)) {
457 continue;
458 }
459 smallTs[index] = t = testAscends ? SkTMax(t, testT) : SkTMin(t, testT);
460 limited[index] = approximately_equal_orderable(t, tEnd);
461 }
462 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000463 bool sRayLonger = false;
464 SkDVector sCept = {0, 0};
465 double sCeptT = -1;
466 int sIndex = -1;
467 bool useIntersect = false;
468 for (int index = 0; index < 2; ++index) {
469 if (smallTs[index] < 0) {
470 continue;
471 }
caryclark54359292015-03-26 07:52:43 -0700472 const SkOpSegment& segment = index ? *rh->segment() : *this->segment();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000473 const SkDPoint& dPt = segment.dPtAtT(smallTs[index]);
474 SkDVector cept = dPt - rays[index][0];
475 // If this point is on the curve, it should have been detected earlier by ordinary
476 // curve intersection. This may be hard to determine in general, but for lines,
477 // the point could be close to or equal to its end, but shouldn't be near the start.
478 if ((index ? lPts : rPts) == 1) {
479 SkDVector total = rays[index][1] - rays[index][0];
480 if (cept.lengthSquared() * 2 < total.lengthSquared()) {
481 continue;
482 }
483 }
484 SkDVector end = rays[index][1] - rays[index][0];
485 if (cept.fX * end.fX < 0 || cept.fY * end.fY < 0) {
486 continue;
487 }
488 double rayDist = cept.length();
489 double endDist = end.length();
490 bool rayLonger = rayDist > endDist;
491 if (limited[0] && limited[1] && rayLonger) {
492 useIntersect = true;
493 sRayLonger = rayLonger;
494 sCept = cept;
495 sCeptT = smallTs[index];
496 sIndex = index;
caryclark@google.coma5e55922013-05-07 18:51:31 +0000497 break;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000498 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000499 double delta = fabs(rayDist - endDist);
500 double minX, minY, maxX, maxY;
501 minX = minY = SK_ScalarInfinity;
502 maxX = maxY = -SK_ScalarInfinity;
caryclarkeed356d2016-09-14 07:18:20 -0700503 const SkDCurve& curve = index ? rh->fPart.fCurve : this->fPart.fCurve;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000504 int ptCount = index ? rPts : lPts;
505 for (int idx2 = 0; idx2 <= ptCount; ++idx2) {
506 minX = SkTMin(minX, curve[idx2].fX);
507 minY = SkTMin(minY, curve[idx2].fY);
508 maxX = SkTMax(maxX, curve[idx2].fX);
509 maxY = SkTMax(maxY, curve[idx2].fY);
510 }
511 double maxWidth = SkTMax(maxX - minX, maxY - minY);
512 delta /= maxWidth;
caryclark54359292015-03-26 07:52:43 -0700513 if (delta > 1e-3 && (useIntersect ^= true)) { // FIXME: move this magic number
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000514 sRayLonger = rayLonger;
515 sCept = cept;
516 sCeptT = smallTs[index];
517 sIndex = index;
518 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000519 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000520 if (useIntersect) {
caryclarkeed356d2016-09-14 07:18:20 -0700521 const SkDCurve& curve = sIndex ? rh->fPart.fCurve : this->fPart.fCurve;
caryclark54359292015-03-26 07:52:43 -0700522 const SkOpSegment& segment = sIndex ? *rh->segment() : *this->segment();
523 double tStart = sIndex ? rh->fStart->t() : fStart->t();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000524 SkDVector mid = segment.dPtAtT(tStart + (sCeptT - tStart) / 2) - curve[0];
525 double septDir = mid.crossCheck(sCept);
526 if (!septDir) {
527 return checkParallel(rh);
528 }
529 return sRayLonger ^ (sIndex == 0) ^ (septDir < 0);
530 } else {
531 return checkParallel(rh);
caryclark@google.coma5e55922013-05-07 18:51:31 +0000532 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000533}
534
caryclark54359292015-03-26 07:52:43 -0700535bool SkOpAngle::endToSide(const SkOpAngle* rh, bool* inside) const {
536 const SkOpSegment* segment = this->segment();
537 SkPath::Verb verb = segment->verb();
caryclark54359292015-03-26 07:52:43 -0700538 SkDLine rayEnd;
539 rayEnd[0].set(this->fEnd->pt());
540 rayEnd[1] = rayEnd[0];
caryclark1049f122015-04-20 08:31:59 -0700541 SkDVector slopeAtEnd = (*CurveDSlopeAtT[verb])(segment->pts(), segment->weight(),
542 this->fEnd->t());
caryclark54359292015-03-26 07:52:43 -0700543 rayEnd[1].fX += slopeAtEnd.fY;
544 rayEnd[1].fY -= slopeAtEnd.fX;
545 SkIntersections iEnd;
546 const SkOpSegment* oppSegment = rh->segment();
547 SkPath::Verb oppVerb = oppSegment->verb();
caryclark1049f122015-04-20 08:31:59 -0700548 (*CurveIntersectRay[oppVerb])(oppSegment->pts(), oppSegment->weight(), rayEnd, &iEnd);
caryclark54359292015-03-26 07:52:43 -0700549 double endDist;
550 int closestEnd = iEnd.closestTo(rh->fStart->t(), rh->fEnd->t(), rayEnd[0], &endDist);
551 if (closestEnd < 0) {
552 return false;
553 }
554 if (!endDist) {
555 return false;
556 }
557 SkDPoint start;
558 start.set(this->fStart->pt());
559 // OPTIMIZATION: multiple times in the code we find the max scalar
560 double minX, minY, maxX, maxY;
561 minX = minY = SK_ScalarInfinity;
562 maxX = maxY = -SK_ScalarInfinity;
caryclarkeed356d2016-09-14 07:18:20 -0700563 const SkDCurve& curve = rh->fPart.fCurve;
caryclark1049f122015-04-20 08:31:59 -0700564 int oppPts = SkPathOpsVerbToPoints(oppVerb);
caryclark54359292015-03-26 07:52:43 -0700565 for (int idx2 = 0; idx2 <= oppPts; ++idx2) {
566 minX = SkTMin(minX, curve[idx2].fX);
567 minY = SkTMin(minY, curve[idx2].fY);
568 maxX = SkTMax(maxX, curve[idx2].fX);
569 maxY = SkTMax(maxY, curve[idx2].fY);
570 }
571 double maxWidth = SkTMax(maxX - minX, maxY - minY);
572 endDist /= maxWidth;
caryclark55888e42016-07-18 10:01:36 -0700573 if (endDist < 5e-12) { // empirically found
caryclark54359292015-03-26 07:52:43 -0700574 return false;
575 }
576 const SkDPoint* endPt = &rayEnd[0];
577 SkDPoint oppPt = iEnd.pt(closestEnd);
578 SkDVector vLeft = *endPt - start;
579 SkDVector vRight = oppPt - start;
caryclark55888e42016-07-18 10:01:36 -0700580 double dir = vLeft.crossNoNormalCheck(vRight);
caryclark54359292015-03-26 07:52:43 -0700581 if (!dir) {
582 return false;
583 }
584 *inside = dir < 0;
585 return true;
586}
587
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000588/* y<0 y==0 y>0 x<0 x==0 x>0 xy<0 xy==0 xy>0
589 0 x x x
590 1 x x x
591 2 x x x
592 3 x x x
593 4 x x x
594 5 x x x
595 6 x x x
596 7 x x x
597 8 x x x
598 9 x x x
599 10 x x x
600 11 x x x
601 12 x x x
602 13 x x x
603 14 x x x
604 15 x x x
605*/
606int SkOpAngle::findSector(SkPath::Verb verb, double x, double y) const {
607 double absX = fabs(x);
608 double absY = fabs(y);
609 double xy = SkPath::kLine_Verb == verb || !AlmostEqualUlps(absX, absY) ? absX - absY : 0;
610 // If there are four quadrants and eight octants, and since the Latin for sixteen is sedecim,
611 // one could coin the term sedecimant for a space divided into 16 sections.
612 // http://english.stackexchange.com/questions/133688/word-for-something-partitioned-into-16-parts
613 static const int sedecimant[3][3][3] = {
614 // y<0 y==0 y>0
615 // x<0 x==0 x>0 x<0 x==0 x>0 x<0 x==0 x>0
616 {{ 4, 3, 2}, { 7, -1, 15}, {10, 11, 12}}, // abs(x) < abs(y)
617 {{ 5, -1, 1}, {-1, -1, -1}, { 9, -1, 13}}, // abs(x) == abs(y)
618 {{ 6, 3, 0}, { 7, -1, 15}, { 8, 11, 14}}, // abs(x) > abs(y)
619 };
620 int sector = sedecimant[(xy >= 0) + (xy > 0)][(y >= 0) + (y > 0)][(x >= 0) + (x > 0)] * 2 + 1;
caryclark65b427c2014-09-18 10:32:57 -0700621// SkASSERT(SkPath::kLine_Verb == verb || sector >= 0);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000622 return sector;
623}
624
caryclark54359292015-03-26 07:52:43 -0700625SkOpGlobalState* SkOpAngle::globalState() const {
626 return this->segment()->globalState();
627}
628
Cary Clark59d5a0e2017-01-23 14:38:52 +0000629
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000630// OPTIMIZE: if this loops to only one other angle, after first compare fails, insert on other side
631// OPTIMIZE: return where insertion succeeded. Then, start next insertion on opposite side
caryclarkb36a3cd2016-10-18 07:59:44 -0700632bool SkOpAngle::insert(SkOpAngle* angle) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000633 if (angle->fNext) {
634 if (loopCount() >= angle->loopCount()) {
635 if (!merge(angle)) {
caryclarkb36a3cd2016-10-18 07:59:44 -0700636 return true;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000637 }
638 } else if (fNext) {
639 if (!angle->merge(this)) {
caryclarkb36a3cd2016-10-18 07:59:44 -0700640 return true;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000641 }
642 } else {
643 angle->insert(this);
644 }
caryclarkb36a3cd2016-10-18 07:59:44 -0700645 return true;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000646 }
halcanary96fcdcc2015-08-27 07:41:13 -0700647 bool singleton = nullptr == fNext;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000648 if (singleton) {
649 fNext = this;
650 }
651 SkOpAngle* next = fNext;
652 if (next->fNext == this) {
653 if (singleton || angle->after(this)) {
654 this->fNext = angle;
655 angle->fNext = next;
656 } else {
657 next->fNext = angle;
658 angle->fNext = this;
659 }
660 debugValidateNext();
caryclarkb36a3cd2016-10-18 07:59:44 -0700661 return true;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000662 }
663 SkOpAngle* last = this;
caryclarkb36a3cd2016-10-18 07:59:44 -0700664 bool flipAmbiguity = false;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000665 do {
666 SkASSERT(last->fNext == next);
caryclarkb36a3cd2016-10-18 07:59:44 -0700667 if (angle->after(last) ^ (angle->tangentsAmbiguous() & flipAmbiguity)) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000668 last->fNext = angle;
669 angle->fNext = next;
670 debugValidateNext();
caryclarkb36a3cd2016-10-18 07:59:44 -0700671 return true;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000672 }
673 last = next;
caryclarkb36a3cd2016-10-18 07:59:44 -0700674 if (last == this) {
675 FAIL_IF(flipAmbiguity);
676 // We're in a loop. If a sort was ambiguous, flip it to end the loop.
677 flipAmbiguity = true;
678 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000679 next = next->fNext;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000680 } while (true);
caryclarkb36a3cd2016-10-18 07:59:44 -0700681 return true;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000682}
683
caryclark54359292015-03-26 07:52:43 -0700684SkOpSpanBase* SkOpAngle::lastMarked() const {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000685 if (fLastMarked) {
caryclark54359292015-03-26 07:52:43 -0700686 if (fLastMarked->chased()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700687 return nullptr;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000688 }
caryclark54359292015-03-26 07:52:43 -0700689 fLastMarked->setChased(true);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000690 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000691 return fLastMarked;
692}
693
caryclark54359292015-03-26 07:52:43 -0700694bool SkOpAngle::loopContains(const SkOpAngle* angle) const {
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000695 if (!fNext) {
696 return false;
697 }
698 const SkOpAngle* first = this;
699 const SkOpAngle* loop = this;
caryclark54359292015-03-26 07:52:43 -0700700 const SkOpSegment* tSegment = angle->fStart->segment();
701 double tStart = angle->fStart->t();
702 double tEnd = angle->fEnd->t();
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000703 do {
caryclark54359292015-03-26 07:52:43 -0700704 const SkOpSegment* lSegment = loop->fStart->segment();
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000705 if (lSegment != tSegment) {
706 continue;
707 }
caryclark54359292015-03-26 07:52:43 -0700708 double lStart = loop->fStart->t();
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000709 if (lStart != tEnd) {
710 continue;
711 }
caryclark54359292015-03-26 07:52:43 -0700712 double lEnd = loop->fEnd->t();
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000713 if (lEnd == tStart) {
714 return true;
715 }
716 } while ((loop = loop->fNext) != first);
717 return false;
718}
719
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000720int SkOpAngle::loopCount() const {
721 int count = 0;
722 const SkOpAngle* first = this;
723 const SkOpAngle* next = this;
724 do {
725 next = next->fNext;
726 ++count;
727 } while (next && next != first);
728 return count;
729}
730
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000731bool SkOpAngle::merge(SkOpAngle* angle) {
732 SkASSERT(fNext);
733 SkASSERT(angle->fNext);
734 SkOpAngle* working = angle;
735 do {
736 if (this == working) {
737 return false;
738 }
739 working = working->fNext;
740 } while (working != angle);
741 do {
742 SkOpAngle* next = working->fNext;
halcanary96fcdcc2015-08-27 07:41:13 -0700743 working->fNext = nullptr;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000744 insert(working);
745 working = next;
746 } while (working != angle);
747 // it's likely that a pair of the angles are unorderable
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000748 debugValidateNext();
749 return true;
750}
751
752double SkOpAngle::midT() const {
caryclark54359292015-03-26 07:52:43 -0700753 return (fStart->t() + fEnd->t()) / 2;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000754}
755
caryclark54359292015-03-26 07:52:43 -0700756bool SkOpAngle::midToSide(const SkOpAngle* rh, bool* inside) const {
757 const SkOpSegment* segment = this->segment();
758 SkPath::Verb verb = segment->verb();
caryclark54359292015-03-26 07:52:43 -0700759 const SkPoint& startPt = this->fStart->pt();
760 const SkPoint& endPt = this->fEnd->pt();
761 SkDPoint dStartPt;
762 dStartPt.set(startPt);
763 SkDLine rayMid;
764 rayMid[0].fX = (startPt.fX + endPt.fX) / 2;
765 rayMid[0].fY = (startPt.fY + endPt.fY) / 2;
766 rayMid[1].fX = rayMid[0].fX + (endPt.fY - startPt.fY);
767 rayMid[1].fY = rayMid[0].fY - (endPt.fX - startPt.fX);
768 SkIntersections iMid;
caryclark1049f122015-04-20 08:31:59 -0700769 (*CurveIntersectRay[verb])(segment->pts(), segment->weight(), rayMid, &iMid);
caryclark54359292015-03-26 07:52:43 -0700770 int iOutside = iMid.mostOutside(this->fStart->t(), this->fEnd->t(), dStartPt);
771 if (iOutside < 0) {
772 return false;
773 }
774 const SkOpSegment* oppSegment = rh->segment();
775 SkPath::Verb oppVerb = oppSegment->verb();
caryclark54359292015-03-26 07:52:43 -0700776 SkIntersections oppMid;
caryclark1049f122015-04-20 08:31:59 -0700777 (*CurveIntersectRay[oppVerb])(oppSegment->pts(), oppSegment->weight(), rayMid, &oppMid);
caryclark54359292015-03-26 07:52:43 -0700778 int oppOutside = oppMid.mostOutside(rh->fStart->t(), rh->fEnd->t(), dStartPt);
779 if (oppOutside < 0) {
780 return false;
781 }
782 SkDVector iSide = iMid.pt(iOutside) - dStartPt;
783 SkDVector oppSide = oppMid.pt(oppOutside) - dStartPt;
784 double dir = iSide.crossCheck(oppSide);
785 if (!dir) {
786 return false;
787 }
788 *inside = dir < 0;
789 return true;
790}
791
792bool SkOpAngle::oppositePlanes(const SkOpAngle* rh) const {
bungeman60e0fee2015-08-26 05:15:46 -0700793 int startSpan = SkTAbs(rh->fSectorStart - fSectorStart);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000794 return startSpan >= 8;
795}
796
caryclark54359292015-03-26 07:52:43 -0700797bool SkOpAngle::orderable(SkOpAngle* rh) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000798 int result;
caryclarkeed356d2016-09-14 07:18:20 -0700799 if (!fPart.isCurve()) {
800 if (!rh->fPart.isCurve()) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000801 double leftX = fTangentHalf.dx();
802 double leftY = fTangentHalf.dy();
caryclark54359292015-03-26 07:52:43 -0700803 double rightX = rh->fTangentHalf.dx();
804 double rightY = rh->fTangentHalf.dy();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000805 double x_ry = leftX * rightY;
806 double rx_y = rightX * leftY;
807 if (x_ry == rx_y) {
808 if (leftX * rightX < 0 || leftY * rightY < 0) {
809 return true; // exactly 180 degrees apart
810 }
811 goto unorderable;
812 }
813 SkASSERT(x_ry != rx_y); // indicates an undetected coincidence -- worth finding earlier
814 return x_ry < rx_y;
815 }
caryclark55888e42016-07-18 10:01:36 -0700816 if ((result = this->allOnOneSide(rh)) >= 0) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000817 return result;
818 }
caryclark54359292015-03-26 07:52:43 -0700819 if (fUnorderable || approximately_zero(rh->fSide)) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000820 goto unorderable;
821 }
caryclarkeed356d2016-09-14 07:18:20 -0700822 } else if (!rh->fPart.isCurve()) {
caryclark54359292015-03-26 07:52:43 -0700823 if ((result = rh->allOnOneSide(this)) >= 0) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000824 return !result;
825 }
caryclark54359292015-03-26 07:52:43 -0700826 if (rh->fUnorderable || approximately_zero(fSide)) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000827 goto unorderable;
828 }
caryclark55888e42016-07-18 10:01:36 -0700829 } else if ((result = this->convexHullOverlaps(rh)) >= 0) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000830 return result;
831 }
caryclark55888e42016-07-18 10:01:36 -0700832 return this->endsIntersect(rh);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000833unorderable:
834 fUnorderable = true;
caryclark54359292015-03-26 07:52:43 -0700835 rh->fUnorderable = true;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000836 return true;
837}
838
839// OPTIMIZE: if this shows up in a profile, add a previous pointer
840// as is, this should be rarely called
841SkOpAngle* SkOpAngle::previous() const {
842 SkOpAngle* last = fNext;
843 do {
844 SkOpAngle* next = last->fNext;
845 if (next == this) {
846 return last;
847 }
848 last = next;
849 } while (true);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000850}
851
caryclark54359292015-03-26 07:52:43 -0700852SkOpSegment* SkOpAngle::segment() const {
853 return fStart->segment();
854}
855
856void SkOpAngle::set(SkOpSpanBase* start, SkOpSpanBase* end) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000857 fStart = start;
caryclarkdac1d172014-06-17 05:15:38 -0700858 fComputedEnd = fEnd = end;
caryclark54359292015-03-26 07:52:43 -0700859 SkASSERT(start != end);
halcanary96fcdcc2015-08-27 07:41:13 -0700860 fNext = nullptr;
caryclarkb36a3cd2016-10-18 07:59:44 -0700861 fComputeSector = fComputedSector = fCheckCoincidence = fTangentsAmbiguous = false;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000862 setSpans();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000863 setSector();
caryclark1049f122015-04-20 08:31:59 -0700864 SkDEBUGCODE(fID = start ? start->globalState()->nextAngleID() : -1);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000865}
866
caryclark@google.com07393ca2013-04-08 11:47:37 +0000867void SkOpAngle::setSpans() {
caryclark54359292015-03-26 07:52:43 -0700868 fUnorderable = false;
halcanary96fcdcc2015-08-27 07:41:13 -0700869 fLastMarked = nullptr;
caryclark1049f122015-04-20 08:31:59 -0700870 if (!fStart) {
871 fUnorderable = true;
872 return;
873 }
caryclark54359292015-03-26 07:52:43 -0700874 const SkOpSegment* segment = fStart->segment();
875 const SkPoint* pts = segment->pts();
caryclark6c3b9cd2016-09-26 05:36:58 -0700876 SkDEBUGCODE(fPart.fCurve.fVerb = SkPath::kCubic_Verb); // required for SkDCurve debug check
caryclarkeed356d2016-09-14 07:18:20 -0700877 SkDEBUGCODE(fPart.fCurve[2].fX = fPart.fCurve[2].fY = fPart.fCurve[3].fX = fPart.fCurve[3].fY
caryclark6c3b9cd2016-09-26 05:36:58 -0700878 = SK_ScalarNaN); // make the non-line part uninitialized
879 SkDEBUGCODE(fPart.fCurve.fVerb = segment->verb()); // set the curve type for real
880 segment->subDivide(fStart, fEnd, &fPart.fCurve); // set at least the line part if not more
caryclarkeed356d2016-09-14 07:18:20 -0700881 fOriginalCurvePart = fPart.fCurve;
caryclark54359292015-03-26 07:52:43 -0700882 const SkPath::Verb verb = segment->verb();
caryclarkeed356d2016-09-14 07:18:20 -0700883 fPart.setCurveHullSweep(verb);
884 if (SkPath::kLine_Verb != verb && !fPart.isCurve()) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000885 SkDLine lineHalf;
caryclarkeed356d2016-09-14 07:18:20 -0700886 fPart.fCurve[1] = fPart.fCurve[SkPathOpsVerbToPoints(verb)];
887 fOriginalCurvePart[1] = fPart.fCurve[1];
888 lineHalf[0].set(fPart.fCurve[0].asSkPoint());
889 lineHalf[1].set(fPart.fCurve[1].asSkPoint());
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000890 fTangentHalf.lineEndPoints(lineHalf);
891 fSide = 0;
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000892 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000893 switch (verb) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000894 case SkPath::kLine_Verb: {
caryclark@google.com570863f2013-09-16 15:55:01 +0000895 SkASSERT(fStart != fEnd);
caryclark54359292015-03-26 07:52:43 -0700896 const SkPoint& cP1 = pts[fStart->t() < fEnd->t()];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000897 SkDLine lineHalf;
caryclark54359292015-03-26 07:52:43 -0700898 lineHalf[0].set(fStart->pt());
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000899 lineHalf[1].set(cP1);
900 fTangentHalf.lineEndPoints(lineHalf);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000901 fSide = 0;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000902 } return;
caryclark1049f122015-04-20 08:31:59 -0700903 case SkPath::kQuad_Verb:
904 case SkPath::kConic_Verb: {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000905 SkLineParameters tangentPart;
caryclarkeed356d2016-09-14 07:18:20 -0700906 (void) tangentPart.quadEndPoints(fPart.fCurve.fQuad);
907 fSide = -tangentPart.pointDistance(fPart.fCurve[2]); // not normalized -- compare sign only
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000908 } break;
909 case SkPath::kCubic_Verb: {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000910 SkLineParameters tangentPart;
caryclarkeed356d2016-09-14 07:18:20 -0700911 (void) tangentPart.cubicPart(fPart.fCurve.fCubic);
912 fSide = -tangentPart.pointDistance(fPart.fCurve[3]);
caryclark@google.comb3f09212013-04-17 15:49:16 +0000913 double testTs[4];
914 // OPTIMIZATION: keep inflections precomputed with cubic segment?
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000915 int testCount = SkDCubic::FindInflections(pts, testTs);
caryclark54359292015-03-26 07:52:43 -0700916 double startT = fStart->t();
917 double endT = fEnd->t();
caryclark@google.comb3f09212013-04-17 15:49:16 +0000918 double limitT = endT;
919 int index;
920 for (index = 0; index < testCount; ++index) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000921 if (!::between(startT, testTs[index], limitT)) {
caryclark@google.comb3f09212013-04-17 15:49:16 +0000922 testTs[index] = -1;
923 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000924 }
caryclark@google.comb3f09212013-04-17 15:49:16 +0000925 testTs[testCount++] = startT;
926 testTs[testCount++] = endT;
commit-bot@chromium.orgb76d3b62013-04-22 19:55:19 +0000927 SkTQSort<double>(testTs, &testTs[testCount - 1]);
caryclark@google.comb3f09212013-04-17 15:49:16 +0000928 double bestSide = 0;
929 int testCases = (testCount << 1) - 1;
930 index = 0;
931 while (testTs[index] < 0) {
932 ++index;
933 }
934 index <<= 1;
935 for (; index < testCases; ++index) {
936 int testIndex = index >> 1;
937 double testT = testTs[testIndex];
938 if (index & 1) {
939 testT = (testT + testTs[testIndex + 1]) / 2;
940 }
941 // OPTIMIZE: could avoid call for t == startT, endT
caryclark1049f122015-04-20 08:31:59 -0700942 SkDPoint pt = dcubic_xy_at_t(pts, segment->weight(), testT);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000943 SkLineParameters tangentPart;
caryclarkeed356d2016-09-14 07:18:20 -0700944 tangentPart.cubicEndPoints(fPart.fCurve.fCubic);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000945 double testSide = tangentPart.pointDistance(pt);
caryclark@google.comb3f09212013-04-17 15:49:16 +0000946 if (fabs(bestSide) < fabs(testSide)) {
947 bestSide = testSide;
948 }
949 }
950 fSide = -bestSide; // compare sign only
caryclark@google.com07393ca2013-04-08 11:47:37 +0000951 } break;
952 default:
953 SkASSERT(0);
954 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000955}
caryclark@google.com570863f2013-09-16 15:55:01 +0000956
caryclark54359292015-03-26 07:52:43 -0700957void SkOpAngle::setSector() {
caryclark1049f122015-04-20 08:31:59 -0700958 if (!fStart) {
959 fUnorderable = true;
960 return;
961 }
caryclark54359292015-03-26 07:52:43 -0700962 const SkOpSegment* segment = fStart->segment();
963 SkPath::Verb verb = segment->verb();
caryclarkeed356d2016-09-14 07:18:20 -0700964 fSectorStart = this->findSector(verb, fPart.fSweep[0].fX, fPart.fSweep[0].fY);
caryclark54359292015-03-26 07:52:43 -0700965 if (fSectorStart < 0) {
966 goto deferTilLater;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000967 }
caryclarkeed356d2016-09-14 07:18:20 -0700968 if (!fPart.isCurve()) { // if it's a line or line-like, note that both sectors are the same
caryclark54359292015-03-26 07:52:43 -0700969 SkASSERT(fSectorStart >= 0);
970 fSectorEnd = fSectorStart;
971 fSectorMask = 1 << fSectorStart;
972 return;
973 }
974 SkASSERT(SkPath::kLine_Verb != verb);
caryclarkeed356d2016-09-14 07:18:20 -0700975 fSectorEnd = this->findSector(verb, fPart.fSweep[1].fX, fPart.fSweep[1].fY);
caryclark54359292015-03-26 07:52:43 -0700976 if (fSectorEnd < 0) {
977deferTilLater:
978 fSectorStart = fSectorEnd = -1;
979 fSectorMask = 0;
980 fComputeSector = true; // can't determine sector until segment length can be found
981 return;
982 }
983 if (fSectorEnd == fSectorStart
984 && (fSectorStart & 3) != 3) { // if the sector has no span, it can't be an exact angle
985 fSectorMask = 1 << fSectorStart;
986 return;
987 }
Cary Clark59d5a0e2017-01-23 14:38:52 +0000988 bool crossesZero = this->checkCrossesZero();
989 int start = SkTMin(fSectorStart, fSectorEnd);
990 bool curveBendsCCW = (fSectorStart == start) ^ crossesZero;
991 // bump the start and end of the sector span if they are on exact compass points
992 if ((fSectorStart & 3) == 3) {
993 fSectorStart = (fSectorStart + (curveBendsCCW ? 1 : 31)) & 0x1f;
caryclark54359292015-03-26 07:52:43 -0700994 }
Cary Clark59d5a0e2017-01-23 14:38:52 +0000995 if ((fSectorEnd & 3) == 3) {
996 fSectorEnd = (fSectorEnd + (curveBendsCCW ? 31 : 1)) & 0x1f;
997 }
998 crossesZero = this->checkCrossesZero();
999 start = SkTMin(fSectorStart, fSectorEnd);
1000 int end = SkTMax(fSectorStart, fSectorEnd);
caryclark54359292015-03-26 07:52:43 -07001001 if (!crossesZero) {
1002 fSectorMask = (unsigned) -1 >> (31 - end + start) << start;
1003 } else {
Cary Clark59d5a0e2017-01-23 14:38:52 +00001004 fSectorMask = (unsigned) -1 >> (31 - start) | ((unsigned) -1 << end);
caryclark54359292015-03-26 07:52:43 -07001005 }
caryclark@google.com570863f2013-09-16 15:55:01 +00001006}
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001007
caryclark54359292015-03-26 07:52:43 -07001008SkOpSpan* SkOpAngle::starter() {
1009 return fStart->starter(fEnd);
1010}
1011
caryclarkb36a3cd2016-10-18 07:59:44 -07001012bool SkOpAngle::tangentsDiverge(const SkOpAngle* rh, double s0xt0) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001013 if (s0xt0 == 0) {
1014 return false;
1015 }
1016 // if the ctrl tangents are not nearly parallel, use them
1017 // solve for opposite direction displacement scale factor == m
1018 // initial dir = v1.cross(v2) == v2.x * v1.y - v2.y * v1.x
1019 // displacement of q1[1] : dq1 = { -m * v1.y, m * v1.x } + q1[1]
1020 // straight angle when : v2.x * (dq1.y - q1[0].y) == v2.y * (dq1.x - q1[0].x)
1021 // v2.x * (m * v1.x + v1.y) == v2.y * (-m * v1.y + v1.x)
1022 // - m * (v2.x * v1.x + v2.y * v1.y) == v2.x * v1.y - v2.y * v1.x
1023 // m = (v2.y * v1.x - v2.x * v1.y) / (v2.x * v1.x + v2.y * v1.y)
1024 // m = v1.cross(v2) / v1.dot(v2)
caryclarkeed356d2016-09-14 07:18:20 -07001025 const SkDVector* sweep = fPart.fSweep;
1026 const SkDVector* tweep = rh->fPart.fSweep;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001027 double s0dt0 = sweep[0].dot(tweep[0]);
1028 if (!s0dt0) {
1029 return true;
1030 }
1031 SkASSERT(s0dt0 != 0);
1032 double m = s0xt0 / s0dt0;
1033 double sDist = sweep[0].length() * m;
1034 double tDist = tweep[0].length() * m;
1035 bool useS = fabs(sDist) < fabs(tDist);
caryclark54359292015-03-26 07:52:43 -07001036 double mFactor = fabs(useS ? this->distEndRatio(sDist) : rh->distEndRatio(tDist));
caryclarkb36a3cd2016-10-18 07:59:44 -07001037 fTangentsAmbiguous = mFactor >= 50 && mFactor < 200;
caryclark55888e42016-07-18 10:01:36 -07001038 return mFactor < 50; // empirically found limit
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001039}