blob: 99f93dd544981acabe770be8177343e358373667 [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);
caryclarkeed356d2016-09-14 07:18:20 -070065 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]);
caryclark55888e42016-07-18 10:01:36 -070070
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000071#if DEBUG_ANGLE
72 SkString bugOut;
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__,
caryclark54359292015-03-26 07:52:43 -070076 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());
81 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
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__,
caryclark54359292015-03-26 07:52:43 -070096 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
caryclark54359292015-03-26 07:52:43 -0700102 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
105 if (!lrOverlap) { // no lh/rh sector overlap
106 if (!ltrOverlap) { // no lh/this/rh sector overlap
caryclark54359292015-03-26 07:52:43 -0700107 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) {
125 return COMPARE_RESULT(5, !lrOrder);
126 }
127 }
128 int ltOrder;
caryclark54359292015-03-26 07:52:43 -0700129 SkASSERT((lh->fSectorMask & fSectorMask) || (rh->fSectorMask & fSectorMask));
130 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 }
143 if (lrOrder >= 0 && ltOrder >= 0 && trOrder >= 0) {
144 return COMPARE_RESULT(7, lrOrder ? (ltOrder & trOrder) : (ltOrder | trOrder));
145 }
146 SkASSERT(lrOrder >= 0 || ltOrder >= 0 || trOrder >= 0);
147// There's not enough information to sort. Get the pairs of angles in opposite planes.
148// If an order is < 0, the pair is already in an opposite plane. Check the remaining pairs.
149 // FIXME : once all variants are understood, rewrite this more simply
150 if (ltOrder == 0 && lrOrder == 0) {
151 SkASSERT(trOrder < 0);
152 // FIXME : once this is verified to work, remove one opposite angle call
caryclark54359292015-03-26 07:52:43 -0700153 SkDEBUGCODE(bool lrOpposite = lh->oppositePlanes(rh));
154 bool ltOpposite = lh->oppositePlanes(this);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000155 SkASSERT(lrOpposite != ltOpposite);
156 return COMPARE_RESULT(8, ltOpposite);
157 } else if (ltOrder == 1 && trOrder == 0) {
158 SkASSERT(lrOrder < 0);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000159 bool trOpposite = oppositePlanes(rh);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000160 return COMPARE_RESULT(9, trOpposite);
161 } else if (lrOrder == 1 && trOrder == 1) {
162 SkASSERT(ltOrder < 0);
163 SkDEBUGCODE(bool trOpposite = oppositePlanes(rh));
caryclark54359292015-03-26 07:52:43 -0700164 bool lrOpposite = lh->oppositePlanes(rh);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000165 SkASSERT(lrOpposite != trOpposite);
166 return COMPARE_RESULT(10, lrOpposite);
167 }
168 if (lrOrder < 0) {
169 if (ltOrder < 0) {
170 return COMPARE_RESULT(11, trOrder);
171 }
172 return COMPARE_RESULT(12, ltOrder);
173 }
174 return COMPARE_RESULT(13, !lrOrder);
175}
176
177// given a line, see if the opposite curve's convex hull is all on one side
178// returns -1=not on one side 0=this CW of test 1=this CCW of test
caryclark54359292015-03-26 07:52:43 -0700179int SkOpAngle::allOnOneSide(const SkOpAngle* test) {
caryclarkeed356d2016-09-14 07:18:20 -0700180 SkASSERT(!fPart.isCurve());
181 SkASSERT(test->fPart.isCurve());
182 SkDPoint origin = fPart.fCurve[0];
183 SkDVector line = fPart.fCurve[1] - origin;
caryclark81681942016-07-21 10:44:07 -0700184 double crosses[3];
caryclark54359292015-03-26 07:52:43 -0700185 SkPath::Verb testVerb = test->segment()->verb();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000186 int iMax = SkPathOpsVerbToPoints(testVerb);
187// SkASSERT(origin == test.fCurveHalf[0]);
caryclarkeed356d2016-09-14 07:18:20 -0700188 const SkDCurve& testCurve = test->fPart.fCurve;
caryclark54359292015-03-26 07:52:43 -0700189 for (int index = 1; index <= iMax; ++index) {
caryclark81681942016-07-21 10:44:07 -0700190 double xy1 = line.fX * (testCurve[index].fY - origin.fY);
191 double xy2 = line.fY * (testCurve[index].fX - origin.fX);
192 crosses[index - 1] = AlmostBequalUlps(xy1, xy2) ? 0 : xy1 - xy2;
caryclark54359292015-03-26 07:52:43 -0700193 }
194 if (crosses[0] * crosses[1] < 0) {
195 return -1;
196 }
197 if (SkPath::kCubic_Verb == testVerb) {
198 if (crosses[0] * crosses[2] < 0 || crosses[1] * crosses[2] < 0) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000199 return -1;
200 }
caryclark54359292015-03-26 07:52:43 -0700201 }
202 if (crosses[0]) {
203 return crosses[0] < 0;
204 }
205 if (crosses[1]) {
206 return crosses[1] < 0;
207 }
208 if (SkPath::kCubic_Verb == testVerb && crosses[2]) {
209 return crosses[2] < 0;
210 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000211 fUnorderable = true;
212 return -1;
213}
214
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000215bool SkOpAngle::checkCrossesZero() const {
216 int start = SkTMin(fSectorStart, fSectorEnd);
217 int end = SkTMax(fSectorStart, fSectorEnd);
218 bool crossesZero = end - start > 16;
219 return crossesZero;
220}
caryclark@google.com07393ca2013-04-08 11:47:37 +0000221
caryclark54359292015-03-26 07:52:43 -0700222bool SkOpAngle::checkParallel(SkOpAngle* rh) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000223 SkDVector scratch[2];
224 const SkDVector* sweep, * tweep;
caryclarkeed356d2016-09-14 07:18:20 -0700225 if (this->fPart.isOrdered()) {
226 sweep = this->fPart.fSweep;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000227 } else {
caryclarkeed356d2016-09-14 07:18:20 -0700228 scratch[0] = this->fPart.fCurve[1] - this->fPart.fCurve[0];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000229 sweep = &scratch[0];
caryclark@google.coma5e55922013-05-07 18:51:31 +0000230 }
caryclarkeed356d2016-09-14 07:18:20 -0700231 if (rh->fPart.isOrdered()) {
232 tweep = rh->fPart.fSweep;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000233 } else {
caryclarkeed356d2016-09-14 07:18:20 -0700234 scratch[1] = rh->fPart.fCurve[1] - rh->fPart.fCurve[0];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000235 tweep = &scratch[1];
caryclark@google.com07393ca2013-04-08 11:47:37 +0000236 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000237 double s0xt0 = sweep->crossCheck(*tweep);
238 if (tangentsDiverge(rh, s0xt0)) {
239 return s0xt0 < 0;
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +0000240 }
caryclark54359292015-03-26 07:52:43 -0700241 // compute the perpendicular to the endpoints and see where it intersects the opposite curve
242 // if the intersections within the t range, do a cross check on those
243 bool inside;
caryclark55888e42016-07-18 10:01:36 -0700244 if (!fEnd->contains(rh->fEnd)) {
caryclark1049f122015-04-20 08:31:59 -0700245 if (this->endToSide(rh, &inside)) {
246 return inside;
247 }
248 if (rh->endToSide(this, &inside)) {
249 return !inside;
250 }
caryclark54359292015-03-26 07:52:43 -0700251 }
252 if (this->midToSide(rh, &inside)) {
253 return inside;
254 }
255 if (rh->midToSide(this, &inside)) {
256 return !inside;
257 }
258 // compute the cross check from the mid T values (last resort)
caryclarkeed356d2016-09-14 07:18:20 -0700259 SkDVector m0 = segment()->dPtAtT(this->midT()) - this->fPart.fCurve[0];
260 SkDVector m1 = rh->segment()->dPtAtT(rh->midT()) - rh->fPart.fCurve[0];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000261 double m0xm1 = m0.crossCheck(m1);
262 if (m0xm1 == 0) {
caryclark54359292015-03-26 07:52:43 -0700263 this->fUnorderable = true;
264 rh->fUnorderable = true;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000265 return true;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000266 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000267 return m0xm1 < 0;
268}
269
270// the original angle is too short to get meaningful sector information
271// lengthen it until it is long enough to be meaningful or leave it unset if lengthening it
272// would cause it to intersect one of the adjacent angles
273bool SkOpAngle::computeSector() {
274 if (fComputedSector) {
caryclarkdac1d172014-06-17 05:15:38 -0700275 return !fUnorderable;
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000276 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000277 fComputedSector = true;
caryclark54359292015-03-26 07:52:43 -0700278 bool stepUp = fStart->t() < fEnd->t();
caryclark55888e42016-07-18 10:01:36 -0700279 SkOpSpanBase* checkEnd = fEnd;
caryclark54359292015-03-26 07:52:43 -0700280 if (checkEnd->final() && stepUp) {
caryclarkccec0f92015-03-24 07:28:17 -0700281 fUnorderable = true;
282 return false;
283 }
caryclark54359292015-03-26 07:52:43 -0700284 do {
285// advance end
286 const SkOpSegment* other = checkEnd->segment();
287 const SkOpSpanBase* oSpan = other->head();
288 do {
289 if (oSpan->segment() != segment()) {
290 continue;
291 }
292 if (oSpan == checkEnd) {
293 continue;
294 }
295 if (!approximately_equal(oSpan->t(), checkEnd->t())) {
296 continue;
297 }
298 goto recomputeSector;
299 } while (!oSpan->final() && (oSpan = oSpan->upCast()->next()));
300 checkEnd = stepUp ? !checkEnd->final()
halcanary96fcdcc2015-08-27 07:41:13 -0700301 ? checkEnd->upCast()->next() : nullptr
caryclark54359292015-03-26 07:52:43 -0700302 : checkEnd->prev();
303 } while (checkEnd);
304recomputeSector:
caryclark55888e42016-07-18 10:01:36 -0700305 SkOpSpanBase* computedEnd = stepUp ? checkEnd ? checkEnd->prev() : fEnd->segment()->head()
caryclark54359292015-03-26 07:52:43 -0700306 : checkEnd ? checkEnd->upCast()->next() : fEnd->segment()->tail();
307 if (checkEnd == fEnd || computedEnd == fEnd || computedEnd == fStart) {
308 fUnorderable = true;
309 return false;
310 }
caryclark624637c2015-05-11 07:21:27 -0700311 if (stepUp != (fStart->t() < computedEnd->t())) {
312 fUnorderable = true;
313 return false;
314 }
caryclark54359292015-03-26 07:52:43 -0700315 SkOpSpanBase* saveEnd = fEnd;
316 fComputedEnd = fEnd = computedEnd;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000317 setSpans();
318 setSector();
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000319 fEnd = saveEnd;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000320 return !fUnorderable;
321}
322
caryclarkb36a3cd2016-10-18 07:59:44 -0700323int SkOpAngle::convexHullOverlaps(const SkOpAngle* rh) {
caryclarkeed356d2016-09-14 07:18:20 -0700324 const SkDVector* sweep = this->fPart.fSweep;
325 const SkDVector* tweep = rh->fPart.fSweep;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000326 double s0xs1 = sweep[0].crossCheck(sweep[1]);
327 double s0xt0 = sweep[0].crossCheck(tweep[0]);
328 double s1xt0 = sweep[1].crossCheck(tweep[0]);
329 bool tBetweenS = s0xs1 > 0 ? s0xt0 > 0 && s1xt0 < 0 : s0xt0 < 0 && s1xt0 > 0;
330 double s0xt1 = sweep[0].crossCheck(tweep[1]);
331 double s1xt1 = sweep[1].crossCheck(tweep[1]);
332 tBetweenS |= s0xs1 > 0 ? s0xt1 > 0 && s1xt1 < 0 : s0xt1 < 0 && s1xt1 > 0;
333 double t0xt1 = tweep[0].crossCheck(tweep[1]);
334 if (tBetweenS) {
335 return -1;
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000336 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000337 if ((s0xt0 == 0 && s1xt1 == 0) || (s1xt0 == 0 && s0xt1 == 0)) { // s0 to s1 equals t0 to t1
338 return -1;
339 }
340 bool sBetweenT = t0xt1 > 0 ? s0xt0 < 0 && s0xt1 > 0 : s0xt0 > 0 && s0xt1 < 0;
341 sBetweenT |= t0xt1 > 0 ? s1xt0 < 0 && s1xt1 > 0 : s1xt0 > 0 && s1xt1 < 0;
342 if (sBetweenT) {
343 return -1;
344 }
345 // if all of the sweeps are in the same half plane, then the order of any pair is enough
346 if (s0xt0 >= 0 && s0xt1 >= 0 && s1xt0 >= 0 && s1xt1 >= 0) {
347 return 0;
348 }
349 if (s0xt0 <= 0 && s0xt1 <= 0 && s1xt0 <= 0 && s1xt1 <= 0) {
350 return 1;
351 }
352 // if the outside sweeps are greater than 180 degress:
353 // first assume the inital tangents are the ordering
354 // if the midpoint direction matches the inital order, that is enough
caryclarkeed356d2016-09-14 07:18:20 -0700355 SkDVector m0 = this->segment()->dPtAtT(this->midT()) - this->fPart.fCurve[0];
356 SkDVector m1 = rh->segment()->dPtAtT(rh->midT()) - rh->fPart.fCurve[0];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000357 double m0xm1 = m0.crossCheck(m1);
358 if (s0xt0 > 0 && m0xm1 > 0) {
359 return 0;
360 }
361 if (s0xt0 < 0 && m0xm1 < 0) {
362 return 1;
363 }
364 if (tangentsDiverge(rh, s0xt0)) {
365 return s0xt0 < 0;
366 }
367 return m0xm1 < 0;
368}
369
370// OPTIMIZATION: longest can all be either lazily computed here or precomputed in setup
371double SkOpAngle::distEndRatio(double dist) const {
372 double longest = 0;
373 const SkOpSegment& segment = *this->segment();
374 int ptCount = SkPathOpsVerbToPoints(segment.verb());
375 const SkPoint* pts = segment.pts();
376 for (int idx1 = 0; idx1 <= ptCount - 1; ++idx1) {
377 for (int idx2 = idx1 + 1; idx2 <= ptCount; ++idx2) {
378 if (idx1 == idx2) {
379 continue;
380 }
381 SkDVector v;
382 v.set(pts[idx2] - pts[idx1]);
383 double lenSq = v.lengthSquared();
384 longest = SkTMax(longest, lenSq);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000385 }
386 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000387 return sqrt(longest) / dist;
388}
389
caryclark54359292015-03-26 07:52:43 -0700390bool SkOpAngle::endsIntersect(SkOpAngle* rh) {
391 SkPath::Verb lVerb = this->segment()->verb();
392 SkPath::Verb rVerb = rh->segment()->verb();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000393 int lPts = SkPathOpsVerbToPoints(lVerb);
394 int rPts = SkPathOpsVerbToPoints(rVerb);
caryclarkeed356d2016-09-14 07:18:20 -0700395 SkDLine rays[] = {{{this->fPart.fCurve[0], rh->fPart.fCurve[rPts]}},
396 {{this->fPart.fCurve[0], this->fPart.fCurve[lPts]}}};
caryclark55888e42016-07-18 10:01:36 -0700397 if (this->fEnd->contains(rh->fEnd)) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000398 return checkParallel(rh);
399 }
400 double smallTs[2] = {-1, -1};
401 bool limited[2] = {false, false};
402 for (int index = 0; index < 2; ++index) {
caryclark1049f122015-04-20 08:31:59 -0700403 SkPath::Verb cVerb = index ? rVerb : lVerb;
caryclark65f55312014-11-13 06:58:52 -0800404 // if the curve is a line, then the line and the ray intersect only at their crossing
caryclark1049f122015-04-20 08:31:59 -0700405 if (cVerb == SkPath::kLine_Verb) {
caryclark65f55312014-11-13 06:58:52 -0800406 continue;
407 }
caryclark54359292015-03-26 07:52:43 -0700408 const SkOpSegment& segment = index ? *rh->segment() : *this->segment();
409 SkIntersections i;
caryclark1049f122015-04-20 08:31:59 -0700410 (*CurveIntersectRay[cVerb])(segment.pts(), segment.weight(), rays[index], &i);
caryclark54359292015-03-26 07:52:43 -0700411 double tStart = index ? rh->fStart->t() : this->fStart->t();
412 double tEnd = index ? rh->fComputedEnd->t() : this->fComputedEnd->t();
413 bool testAscends = tStart < (index ? rh->fComputedEnd->t() : this->fComputedEnd->t());
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000414 double t = testAscends ? 0 : 1;
415 for (int idx2 = 0; idx2 < i.used(); ++idx2) {
416 double testT = i[0][idx2];
417 if (!approximately_between_orderable(tStart, testT, tEnd)) {
418 continue;
419 }
420 if (approximately_equal_orderable(tStart, testT)) {
421 continue;
422 }
423 smallTs[index] = t = testAscends ? SkTMax(t, testT) : SkTMin(t, testT);
424 limited[index] = approximately_equal_orderable(t, tEnd);
425 }
426 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000427 bool sRayLonger = false;
428 SkDVector sCept = {0, 0};
429 double sCeptT = -1;
430 int sIndex = -1;
431 bool useIntersect = false;
432 for (int index = 0; index < 2; ++index) {
433 if (smallTs[index] < 0) {
434 continue;
435 }
caryclark54359292015-03-26 07:52:43 -0700436 const SkOpSegment& segment = index ? *rh->segment() : *this->segment();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000437 const SkDPoint& dPt = segment.dPtAtT(smallTs[index]);
438 SkDVector cept = dPt - rays[index][0];
439 // If this point is on the curve, it should have been detected earlier by ordinary
440 // curve intersection. This may be hard to determine in general, but for lines,
441 // the point could be close to or equal to its end, but shouldn't be near the start.
442 if ((index ? lPts : rPts) == 1) {
443 SkDVector total = rays[index][1] - rays[index][0];
444 if (cept.lengthSquared() * 2 < total.lengthSquared()) {
445 continue;
446 }
447 }
448 SkDVector end = rays[index][1] - rays[index][0];
449 if (cept.fX * end.fX < 0 || cept.fY * end.fY < 0) {
450 continue;
451 }
452 double rayDist = cept.length();
453 double endDist = end.length();
454 bool rayLonger = rayDist > endDist;
455 if (limited[0] && limited[1] && rayLonger) {
456 useIntersect = true;
457 sRayLonger = rayLonger;
458 sCept = cept;
459 sCeptT = smallTs[index];
460 sIndex = index;
caryclark@google.coma5e55922013-05-07 18:51:31 +0000461 break;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000462 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000463 double delta = fabs(rayDist - endDist);
464 double minX, minY, maxX, maxY;
465 minX = minY = SK_ScalarInfinity;
466 maxX = maxY = -SK_ScalarInfinity;
caryclarkeed356d2016-09-14 07:18:20 -0700467 const SkDCurve& curve = index ? rh->fPart.fCurve : this->fPart.fCurve;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000468 int ptCount = index ? rPts : lPts;
469 for (int idx2 = 0; idx2 <= ptCount; ++idx2) {
470 minX = SkTMin(minX, curve[idx2].fX);
471 minY = SkTMin(minY, curve[idx2].fY);
472 maxX = SkTMax(maxX, curve[idx2].fX);
473 maxY = SkTMax(maxY, curve[idx2].fY);
474 }
475 double maxWidth = SkTMax(maxX - minX, maxY - minY);
476 delta /= maxWidth;
caryclark54359292015-03-26 07:52:43 -0700477 if (delta > 1e-3 && (useIntersect ^= true)) { // FIXME: move this magic number
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000478 sRayLonger = rayLonger;
479 sCept = cept;
480 sCeptT = smallTs[index];
481 sIndex = index;
482 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000483 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000484 if (useIntersect) {
caryclarkeed356d2016-09-14 07:18:20 -0700485 const SkDCurve& curve = sIndex ? rh->fPart.fCurve : this->fPart.fCurve;
caryclark54359292015-03-26 07:52:43 -0700486 const SkOpSegment& segment = sIndex ? *rh->segment() : *this->segment();
487 double tStart = sIndex ? rh->fStart->t() : fStart->t();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000488 SkDVector mid = segment.dPtAtT(tStart + (sCeptT - tStart) / 2) - curve[0];
489 double septDir = mid.crossCheck(sCept);
490 if (!septDir) {
491 return checkParallel(rh);
492 }
493 return sRayLonger ^ (sIndex == 0) ^ (septDir < 0);
494 } else {
495 return checkParallel(rh);
caryclark@google.coma5e55922013-05-07 18:51:31 +0000496 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000497}
498
caryclark54359292015-03-26 07:52:43 -0700499bool SkOpAngle::endToSide(const SkOpAngle* rh, bool* inside) const {
500 const SkOpSegment* segment = this->segment();
501 SkPath::Verb verb = segment->verb();
caryclark54359292015-03-26 07:52:43 -0700502 SkDLine rayEnd;
503 rayEnd[0].set(this->fEnd->pt());
504 rayEnd[1] = rayEnd[0];
caryclark1049f122015-04-20 08:31:59 -0700505 SkDVector slopeAtEnd = (*CurveDSlopeAtT[verb])(segment->pts(), segment->weight(),
506 this->fEnd->t());
caryclark54359292015-03-26 07:52:43 -0700507 rayEnd[1].fX += slopeAtEnd.fY;
508 rayEnd[1].fY -= slopeAtEnd.fX;
509 SkIntersections iEnd;
510 const SkOpSegment* oppSegment = rh->segment();
511 SkPath::Verb oppVerb = oppSegment->verb();
caryclark1049f122015-04-20 08:31:59 -0700512 (*CurveIntersectRay[oppVerb])(oppSegment->pts(), oppSegment->weight(), rayEnd, &iEnd);
caryclark54359292015-03-26 07:52:43 -0700513 double endDist;
514 int closestEnd = iEnd.closestTo(rh->fStart->t(), rh->fEnd->t(), rayEnd[0], &endDist);
515 if (closestEnd < 0) {
516 return false;
517 }
518 if (!endDist) {
519 return false;
520 }
521 SkDPoint start;
522 start.set(this->fStart->pt());
523 // OPTIMIZATION: multiple times in the code we find the max scalar
524 double minX, minY, maxX, maxY;
525 minX = minY = SK_ScalarInfinity;
526 maxX = maxY = -SK_ScalarInfinity;
caryclarkeed356d2016-09-14 07:18:20 -0700527 const SkDCurve& curve = rh->fPart.fCurve;
caryclark1049f122015-04-20 08:31:59 -0700528 int oppPts = SkPathOpsVerbToPoints(oppVerb);
caryclark54359292015-03-26 07:52:43 -0700529 for (int idx2 = 0; idx2 <= oppPts; ++idx2) {
530 minX = SkTMin(minX, curve[idx2].fX);
531 minY = SkTMin(minY, curve[idx2].fY);
532 maxX = SkTMax(maxX, curve[idx2].fX);
533 maxY = SkTMax(maxY, curve[idx2].fY);
534 }
535 double maxWidth = SkTMax(maxX - minX, maxY - minY);
536 endDist /= maxWidth;
caryclark55888e42016-07-18 10:01:36 -0700537 if (endDist < 5e-12) { // empirically found
caryclark54359292015-03-26 07:52:43 -0700538 return false;
539 }
540 const SkDPoint* endPt = &rayEnd[0];
541 SkDPoint oppPt = iEnd.pt(closestEnd);
542 SkDVector vLeft = *endPt - start;
543 SkDVector vRight = oppPt - start;
caryclark55888e42016-07-18 10:01:36 -0700544 double dir = vLeft.crossNoNormalCheck(vRight);
caryclark54359292015-03-26 07:52:43 -0700545 if (!dir) {
546 return false;
547 }
548 *inside = dir < 0;
549 return true;
550}
551
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000552/* y<0 y==0 y>0 x<0 x==0 x>0 xy<0 xy==0 xy>0
553 0 x x x
554 1 x x x
555 2 x x x
556 3 x x x
557 4 x x x
558 5 x x x
559 6 x x x
560 7 x x x
561 8 x x x
562 9 x x x
563 10 x x x
564 11 x x x
565 12 x x x
566 13 x x x
567 14 x x x
568 15 x x x
569*/
570int SkOpAngle::findSector(SkPath::Verb verb, double x, double y) const {
571 double absX = fabs(x);
572 double absY = fabs(y);
573 double xy = SkPath::kLine_Verb == verb || !AlmostEqualUlps(absX, absY) ? absX - absY : 0;
574 // If there are four quadrants and eight octants, and since the Latin for sixteen is sedecim,
575 // one could coin the term sedecimant for a space divided into 16 sections.
576 // http://english.stackexchange.com/questions/133688/word-for-something-partitioned-into-16-parts
577 static const int sedecimant[3][3][3] = {
578 // y<0 y==0 y>0
579 // x<0 x==0 x>0 x<0 x==0 x>0 x<0 x==0 x>0
580 {{ 4, 3, 2}, { 7, -1, 15}, {10, 11, 12}}, // abs(x) < abs(y)
581 {{ 5, -1, 1}, {-1, -1, -1}, { 9, -1, 13}}, // abs(x) == abs(y)
582 {{ 6, 3, 0}, { 7, -1, 15}, { 8, 11, 14}}, // abs(x) > abs(y)
583 };
584 int sector = sedecimant[(xy >= 0) + (xy > 0)][(y >= 0) + (y > 0)][(x >= 0) + (x > 0)] * 2 + 1;
caryclark65b427c2014-09-18 10:32:57 -0700585// SkASSERT(SkPath::kLine_Verb == verb || sector >= 0);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000586 return sector;
587}
588
caryclark54359292015-03-26 07:52:43 -0700589SkOpGlobalState* SkOpAngle::globalState() const {
590 return this->segment()->globalState();
591}
592
593
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000594// OPTIMIZE: if this loops to only one other angle, after first compare fails, insert on other side
595// OPTIMIZE: return where insertion succeeded. Then, start next insertion on opposite side
caryclarkb36a3cd2016-10-18 07:59:44 -0700596bool SkOpAngle::insert(SkOpAngle* angle) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000597 if (angle->fNext) {
598 if (loopCount() >= angle->loopCount()) {
599 if (!merge(angle)) {
caryclarkb36a3cd2016-10-18 07:59:44 -0700600 return true;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000601 }
602 } else if (fNext) {
603 if (!angle->merge(this)) {
caryclarkb36a3cd2016-10-18 07:59:44 -0700604 return true;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000605 }
606 } else {
607 angle->insert(this);
608 }
caryclarkb36a3cd2016-10-18 07:59:44 -0700609 return true;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000610 }
halcanary96fcdcc2015-08-27 07:41:13 -0700611 bool singleton = nullptr == fNext;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000612 if (singleton) {
613 fNext = this;
614 }
615 SkOpAngle* next = fNext;
616 if (next->fNext == this) {
617 if (singleton || angle->after(this)) {
618 this->fNext = angle;
619 angle->fNext = next;
620 } else {
621 next->fNext = angle;
622 angle->fNext = this;
623 }
624 debugValidateNext();
caryclarkb36a3cd2016-10-18 07:59:44 -0700625 return true;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000626 }
627 SkOpAngle* last = this;
caryclarkb36a3cd2016-10-18 07:59:44 -0700628 bool flipAmbiguity = false;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000629 do {
630 SkASSERT(last->fNext == next);
caryclarkb36a3cd2016-10-18 07:59:44 -0700631 if (angle->after(last) ^ (angle->tangentsAmbiguous() & flipAmbiguity)) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000632 last->fNext = angle;
633 angle->fNext = next;
634 debugValidateNext();
caryclarkb36a3cd2016-10-18 07:59:44 -0700635 return true;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000636 }
637 last = next;
caryclarkb36a3cd2016-10-18 07:59:44 -0700638 if (last == this) {
639 FAIL_IF(flipAmbiguity);
640 // We're in a loop. If a sort was ambiguous, flip it to end the loop.
641 flipAmbiguity = true;
642 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000643 next = next->fNext;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000644 } while (true);
caryclarkb36a3cd2016-10-18 07:59:44 -0700645 return true;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000646}
647
caryclark54359292015-03-26 07:52:43 -0700648SkOpSpanBase* SkOpAngle::lastMarked() const {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000649 if (fLastMarked) {
caryclark54359292015-03-26 07:52:43 -0700650 if (fLastMarked->chased()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700651 return nullptr;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000652 }
caryclark54359292015-03-26 07:52:43 -0700653 fLastMarked->setChased(true);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000654 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000655 return fLastMarked;
656}
657
caryclark54359292015-03-26 07:52:43 -0700658bool SkOpAngle::loopContains(const SkOpAngle* angle) const {
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000659 if (!fNext) {
660 return false;
661 }
662 const SkOpAngle* first = this;
663 const SkOpAngle* loop = this;
caryclark54359292015-03-26 07:52:43 -0700664 const SkOpSegment* tSegment = angle->fStart->segment();
665 double tStart = angle->fStart->t();
666 double tEnd = angle->fEnd->t();
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000667 do {
caryclark54359292015-03-26 07:52:43 -0700668 const SkOpSegment* lSegment = loop->fStart->segment();
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000669 if (lSegment != tSegment) {
670 continue;
671 }
caryclark54359292015-03-26 07:52:43 -0700672 double lStart = loop->fStart->t();
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000673 if (lStart != tEnd) {
674 continue;
675 }
caryclark54359292015-03-26 07:52:43 -0700676 double lEnd = loop->fEnd->t();
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000677 if (lEnd == tStart) {
678 return true;
679 }
680 } while ((loop = loop->fNext) != first);
681 return false;
682}
683
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000684int SkOpAngle::loopCount() const {
685 int count = 0;
686 const SkOpAngle* first = this;
687 const SkOpAngle* next = this;
688 do {
689 next = next->fNext;
690 ++count;
691 } while (next && next != first);
692 return count;
693}
694
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000695bool SkOpAngle::merge(SkOpAngle* angle) {
696 SkASSERT(fNext);
697 SkASSERT(angle->fNext);
698 SkOpAngle* working = angle;
699 do {
700 if (this == working) {
701 return false;
702 }
703 working = working->fNext;
704 } while (working != angle);
705 do {
706 SkOpAngle* next = working->fNext;
halcanary96fcdcc2015-08-27 07:41:13 -0700707 working->fNext = nullptr;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000708 insert(working);
709 working = next;
710 } while (working != angle);
711 // it's likely that a pair of the angles are unorderable
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000712 debugValidateNext();
713 return true;
714}
715
716double SkOpAngle::midT() const {
caryclark54359292015-03-26 07:52:43 -0700717 return (fStart->t() + fEnd->t()) / 2;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000718}
719
caryclark54359292015-03-26 07:52:43 -0700720bool SkOpAngle::midToSide(const SkOpAngle* rh, bool* inside) const {
721 const SkOpSegment* segment = this->segment();
722 SkPath::Verb verb = segment->verb();
caryclark54359292015-03-26 07:52:43 -0700723 const SkPoint& startPt = this->fStart->pt();
724 const SkPoint& endPt = this->fEnd->pt();
725 SkDPoint dStartPt;
726 dStartPt.set(startPt);
727 SkDLine rayMid;
728 rayMid[0].fX = (startPt.fX + endPt.fX) / 2;
729 rayMid[0].fY = (startPt.fY + endPt.fY) / 2;
730 rayMid[1].fX = rayMid[0].fX + (endPt.fY - startPt.fY);
731 rayMid[1].fY = rayMid[0].fY - (endPt.fX - startPt.fX);
732 SkIntersections iMid;
caryclark1049f122015-04-20 08:31:59 -0700733 (*CurveIntersectRay[verb])(segment->pts(), segment->weight(), rayMid, &iMid);
caryclark54359292015-03-26 07:52:43 -0700734 int iOutside = iMid.mostOutside(this->fStart->t(), this->fEnd->t(), dStartPt);
735 if (iOutside < 0) {
736 return false;
737 }
738 const SkOpSegment* oppSegment = rh->segment();
739 SkPath::Verb oppVerb = oppSegment->verb();
caryclark54359292015-03-26 07:52:43 -0700740 SkIntersections oppMid;
caryclark1049f122015-04-20 08:31:59 -0700741 (*CurveIntersectRay[oppVerb])(oppSegment->pts(), oppSegment->weight(), rayMid, &oppMid);
caryclark54359292015-03-26 07:52:43 -0700742 int oppOutside = oppMid.mostOutside(rh->fStart->t(), rh->fEnd->t(), dStartPt);
743 if (oppOutside < 0) {
744 return false;
745 }
746 SkDVector iSide = iMid.pt(iOutside) - dStartPt;
747 SkDVector oppSide = oppMid.pt(oppOutside) - dStartPt;
748 double dir = iSide.crossCheck(oppSide);
749 if (!dir) {
750 return false;
751 }
752 *inside = dir < 0;
753 return true;
754}
755
756bool SkOpAngle::oppositePlanes(const SkOpAngle* rh) const {
bungeman60e0fee2015-08-26 05:15:46 -0700757 int startSpan = SkTAbs(rh->fSectorStart - fSectorStart);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000758 return startSpan >= 8;
759}
760
caryclark54359292015-03-26 07:52:43 -0700761bool SkOpAngle::orderable(SkOpAngle* rh) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000762 int result;
caryclarkeed356d2016-09-14 07:18:20 -0700763 if (!fPart.isCurve()) {
764 if (!rh->fPart.isCurve()) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000765 double leftX = fTangentHalf.dx();
766 double leftY = fTangentHalf.dy();
caryclark54359292015-03-26 07:52:43 -0700767 double rightX = rh->fTangentHalf.dx();
768 double rightY = rh->fTangentHalf.dy();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000769 double x_ry = leftX * rightY;
770 double rx_y = rightX * leftY;
771 if (x_ry == rx_y) {
772 if (leftX * rightX < 0 || leftY * rightY < 0) {
773 return true; // exactly 180 degrees apart
774 }
775 goto unorderable;
776 }
777 SkASSERT(x_ry != rx_y); // indicates an undetected coincidence -- worth finding earlier
778 return x_ry < rx_y;
779 }
caryclark55888e42016-07-18 10:01:36 -0700780 if ((result = this->allOnOneSide(rh)) >= 0) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000781 return result;
782 }
caryclark54359292015-03-26 07:52:43 -0700783 if (fUnorderable || approximately_zero(rh->fSide)) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000784 goto unorderable;
785 }
caryclarkeed356d2016-09-14 07:18:20 -0700786 } else if (!rh->fPart.isCurve()) {
caryclark54359292015-03-26 07:52:43 -0700787 if ((result = rh->allOnOneSide(this)) >= 0) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000788 return !result;
789 }
caryclark54359292015-03-26 07:52:43 -0700790 if (rh->fUnorderable || approximately_zero(fSide)) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000791 goto unorderable;
792 }
caryclark55888e42016-07-18 10:01:36 -0700793 } else if ((result = this->convexHullOverlaps(rh)) >= 0) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000794 return result;
795 }
caryclark55888e42016-07-18 10:01:36 -0700796 return this->endsIntersect(rh);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000797unorderable:
798 fUnorderable = true;
caryclark54359292015-03-26 07:52:43 -0700799 rh->fUnorderable = true;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000800 return true;
801}
802
803// OPTIMIZE: if this shows up in a profile, add a previous pointer
804// as is, this should be rarely called
805SkOpAngle* SkOpAngle::previous() const {
806 SkOpAngle* last = fNext;
807 do {
808 SkOpAngle* next = last->fNext;
809 if (next == this) {
810 return last;
811 }
812 last = next;
813 } while (true);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000814}
815
caryclark54359292015-03-26 07:52:43 -0700816SkOpSegment* SkOpAngle::segment() const {
817 return fStart->segment();
818}
819
820void SkOpAngle::set(SkOpSpanBase* start, SkOpSpanBase* end) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000821 fStart = start;
caryclarkdac1d172014-06-17 05:15:38 -0700822 fComputedEnd = fEnd = end;
caryclark54359292015-03-26 07:52:43 -0700823 SkASSERT(start != end);
halcanary96fcdcc2015-08-27 07:41:13 -0700824 fNext = nullptr;
caryclarkb36a3cd2016-10-18 07:59:44 -0700825 fComputeSector = fComputedSector = fCheckCoincidence = fTangentsAmbiguous = false;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000826 setSpans();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000827 setSector();
caryclark1049f122015-04-20 08:31:59 -0700828 SkDEBUGCODE(fID = start ? start->globalState()->nextAngleID() : -1);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000829}
830
caryclark@google.com07393ca2013-04-08 11:47:37 +0000831void SkOpAngle::setSpans() {
caryclark54359292015-03-26 07:52:43 -0700832 fUnorderable = false;
halcanary96fcdcc2015-08-27 07:41:13 -0700833 fLastMarked = nullptr;
caryclark1049f122015-04-20 08:31:59 -0700834 if (!fStart) {
835 fUnorderable = true;
836 return;
837 }
caryclark54359292015-03-26 07:52:43 -0700838 const SkOpSegment* segment = fStart->segment();
839 const SkPoint* pts = segment->pts();
caryclark6c3b9cd2016-09-26 05:36:58 -0700840 SkDEBUGCODE(fPart.fCurve.fVerb = SkPath::kCubic_Verb); // required for SkDCurve debug check
caryclarkeed356d2016-09-14 07:18:20 -0700841 SkDEBUGCODE(fPart.fCurve[2].fX = fPart.fCurve[2].fY = fPart.fCurve[3].fX = fPart.fCurve[3].fY
caryclark6c3b9cd2016-09-26 05:36:58 -0700842 = SK_ScalarNaN); // make the non-line part uninitialized
843 SkDEBUGCODE(fPart.fCurve.fVerb = segment->verb()); // set the curve type for real
844 segment->subDivide(fStart, fEnd, &fPart.fCurve); // set at least the line part if not more
caryclarkeed356d2016-09-14 07:18:20 -0700845 fOriginalCurvePart = fPart.fCurve;
caryclark54359292015-03-26 07:52:43 -0700846 const SkPath::Verb verb = segment->verb();
caryclarkeed356d2016-09-14 07:18:20 -0700847 fPart.setCurveHullSweep(verb);
848 if (SkPath::kLine_Verb != verb && !fPart.isCurve()) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000849 SkDLine lineHalf;
caryclarkeed356d2016-09-14 07:18:20 -0700850 fPart.fCurve[1] = fPart.fCurve[SkPathOpsVerbToPoints(verb)];
851 fOriginalCurvePart[1] = fPart.fCurve[1];
852 lineHalf[0].set(fPart.fCurve[0].asSkPoint());
853 lineHalf[1].set(fPart.fCurve[1].asSkPoint());
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000854 fTangentHalf.lineEndPoints(lineHalf);
855 fSide = 0;
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000856 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000857 switch (verb) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000858 case SkPath::kLine_Verb: {
caryclark@google.com570863f2013-09-16 15:55:01 +0000859 SkASSERT(fStart != fEnd);
caryclark54359292015-03-26 07:52:43 -0700860 const SkPoint& cP1 = pts[fStart->t() < fEnd->t()];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000861 SkDLine lineHalf;
caryclark54359292015-03-26 07:52:43 -0700862 lineHalf[0].set(fStart->pt());
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000863 lineHalf[1].set(cP1);
864 fTangentHalf.lineEndPoints(lineHalf);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000865 fSide = 0;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000866 } return;
caryclark1049f122015-04-20 08:31:59 -0700867 case SkPath::kQuad_Verb:
868 case SkPath::kConic_Verb: {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000869 SkLineParameters tangentPart;
caryclarkeed356d2016-09-14 07:18:20 -0700870 (void) tangentPart.quadEndPoints(fPart.fCurve.fQuad);
871 fSide = -tangentPart.pointDistance(fPart.fCurve[2]); // not normalized -- compare sign only
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000872 } break;
873 case SkPath::kCubic_Verb: {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000874 SkLineParameters tangentPart;
caryclarkeed356d2016-09-14 07:18:20 -0700875 (void) tangentPart.cubicPart(fPart.fCurve.fCubic);
876 fSide = -tangentPart.pointDistance(fPart.fCurve[3]);
caryclark@google.comb3f09212013-04-17 15:49:16 +0000877 double testTs[4];
878 // OPTIMIZATION: keep inflections precomputed with cubic segment?
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000879 int testCount = SkDCubic::FindInflections(pts, testTs);
caryclark54359292015-03-26 07:52:43 -0700880 double startT = fStart->t();
881 double endT = fEnd->t();
caryclark@google.comb3f09212013-04-17 15:49:16 +0000882 double limitT = endT;
883 int index;
884 for (index = 0; index < testCount; ++index) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000885 if (!::between(startT, testTs[index], limitT)) {
caryclark@google.comb3f09212013-04-17 15:49:16 +0000886 testTs[index] = -1;
887 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000888 }
caryclark@google.comb3f09212013-04-17 15:49:16 +0000889 testTs[testCount++] = startT;
890 testTs[testCount++] = endT;
commit-bot@chromium.orgb76d3b62013-04-22 19:55:19 +0000891 SkTQSort<double>(testTs, &testTs[testCount - 1]);
caryclark@google.comb3f09212013-04-17 15:49:16 +0000892 double bestSide = 0;
893 int testCases = (testCount << 1) - 1;
894 index = 0;
895 while (testTs[index] < 0) {
896 ++index;
897 }
898 index <<= 1;
899 for (; index < testCases; ++index) {
900 int testIndex = index >> 1;
901 double testT = testTs[testIndex];
902 if (index & 1) {
903 testT = (testT + testTs[testIndex + 1]) / 2;
904 }
905 // OPTIMIZE: could avoid call for t == startT, endT
caryclark1049f122015-04-20 08:31:59 -0700906 SkDPoint pt = dcubic_xy_at_t(pts, segment->weight(), testT);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000907 SkLineParameters tangentPart;
caryclarkeed356d2016-09-14 07:18:20 -0700908 tangentPart.cubicEndPoints(fPart.fCurve.fCubic);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000909 double testSide = tangentPart.pointDistance(pt);
caryclark@google.comb3f09212013-04-17 15:49:16 +0000910 if (fabs(bestSide) < fabs(testSide)) {
911 bestSide = testSide;
912 }
913 }
914 fSide = -bestSide; // compare sign only
caryclark@google.com07393ca2013-04-08 11:47:37 +0000915 } break;
916 default:
917 SkASSERT(0);
918 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000919}
caryclark@google.com570863f2013-09-16 15:55:01 +0000920
caryclark54359292015-03-26 07:52:43 -0700921void SkOpAngle::setSector() {
caryclark1049f122015-04-20 08:31:59 -0700922 if (!fStart) {
923 fUnorderable = true;
924 return;
925 }
caryclark54359292015-03-26 07:52:43 -0700926 const SkOpSegment* segment = fStart->segment();
927 SkPath::Verb verb = segment->verb();
caryclarkeed356d2016-09-14 07:18:20 -0700928 fSectorStart = this->findSector(verb, fPart.fSweep[0].fX, fPart.fSweep[0].fY);
caryclark54359292015-03-26 07:52:43 -0700929 if (fSectorStart < 0) {
930 goto deferTilLater;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000931 }
caryclarkeed356d2016-09-14 07:18:20 -0700932 if (!fPart.isCurve()) { // if it's a line or line-like, note that both sectors are the same
caryclark54359292015-03-26 07:52:43 -0700933 SkASSERT(fSectorStart >= 0);
934 fSectorEnd = fSectorStart;
935 fSectorMask = 1 << fSectorStart;
936 return;
937 }
938 SkASSERT(SkPath::kLine_Verb != verb);
caryclarkeed356d2016-09-14 07:18:20 -0700939 fSectorEnd = this->findSector(verb, fPart.fSweep[1].fX, fPart.fSweep[1].fY);
caryclark54359292015-03-26 07:52:43 -0700940 if (fSectorEnd < 0) {
941deferTilLater:
942 fSectorStart = fSectorEnd = -1;
943 fSectorMask = 0;
944 fComputeSector = true; // can't determine sector until segment length can be found
945 return;
946 }
947 if (fSectorEnd == fSectorStart
948 && (fSectorStart & 3) != 3) { // if the sector has no span, it can't be an exact angle
949 fSectorMask = 1 << fSectorStart;
950 return;
951 }
952 bool crossesZero = this->checkCrossesZero();
953 int start = SkTMin(fSectorStart, fSectorEnd);
954 bool curveBendsCCW = (fSectorStart == start) ^ crossesZero;
955 // bump the start and end of the sector span if they are on exact compass points
956 if ((fSectorStart & 3) == 3) {
957 fSectorStart = (fSectorStart + (curveBendsCCW ? 1 : 31)) & 0x1f;
958 }
959 if ((fSectorEnd & 3) == 3) {
960 fSectorEnd = (fSectorEnd + (curveBendsCCW ? 31 : 1)) & 0x1f;
961 }
962 crossesZero = this->checkCrossesZero();
963 start = SkTMin(fSectorStart, fSectorEnd);
964 int end = SkTMax(fSectorStart, fSectorEnd);
965 if (!crossesZero) {
966 fSectorMask = (unsigned) -1 >> (31 - end + start) << start;
967 } else {
caryclark3127c992015-12-09 12:02:30 -0800968 fSectorMask = (unsigned) -1 >> (31 - start) | ((unsigned) -1 << end);
caryclark54359292015-03-26 07:52:43 -0700969 }
caryclark@google.com570863f2013-09-16 15:55:01 +0000970}
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000971
caryclark54359292015-03-26 07:52:43 -0700972SkOpSpan* SkOpAngle::starter() {
973 return fStart->starter(fEnd);
974}
975
caryclarkb36a3cd2016-10-18 07:59:44 -0700976bool SkOpAngle::tangentsDiverge(const SkOpAngle* rh, double s0xt0) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000977 if (s0xt0 == 0) {
978 return false;
979 }
980 // if the ctrl tangents are not nearly parallel, use them
981 // solve for opposite direction displacement scale factor == m
982 // initial dir = v1.cross(v2) == v2.x * v1.y - v2.y * v1.x
983 // displacement of q1[1] : dq1 = { -m * v1.y, m * v1.x } + q1[1]
984 // straight angle when : v2.x * (dq1.y - q1[0].y) == v2.y * (dq1.x - q1[0].x)
985 // v2.x * (m * v1.x + v1.y) == v2.y * (-m * v1.y + v1.x)
986 // - m * (v2.x * v1.x + v2.y * v1.y) == v2.x * v1.y - v2.y * v1.x
987 // m = (v2.y * v1.x - v2.x * v1.y) / (v2.x * v1.x + v2.y * v1.y)
988 // m = v1.cross(v2) / v1.dot(v2)
caryclarkeed356d2016-09-14 07:18:20 -0700989 const SkDVector* sweep = fPart.fSweep;
990 const SkDVector* tweep = rh->fPart.fSweep;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000991 double s0dt0 = sweep[0].dot(tweep[0]);
992 if (!s0dt0) {
993 return true;
994 }
995 SkASSERT(s0dt0 != 0);
996 double m = s0xt0 / s0dt0;
997 double sDist = sweep[0].length() * m;
998 double tDist = tweep[0].length() * m;
999 bool useS = fabs(sDist) < fabs(tDist);
caryclark54359292015-03-26 07:52:43 -07001000 double mFactor = fabs(useS ? this->distEndRatio(sDist) : rh->distEndRatio(tDist));
caryclarkb36a3cd2016-10-18 07:59:44 -07001001 fTangentsAmbiguous = mFactor >= 50 && mFactor < 200;
caryclark55888e42016-07-18 10:01:36 -07001002 return mFactor < 50; // empirically found limit
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001003}