blob: 194260f658920cff47172963c32c040a8bb91d9f [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) {
Cary Clark3a4a3212018-06-06 15:22:08 -0400138 trOrder = (int) this->orderable(rh);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000139 } 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 }
Cary Clark3a4a3212018-06-06 15:22:08 -0400170 // if a pair couldn't be ordered, there's not enough information to determine the sort
171 if (fUnorderable || lh->fUnorderable || rh->fUnorderable) {
172 // limit to lines; should work with curves, but wait for a failing test to verify
173 if (!fPart.isCurve() && !lh->fPart.isCurve() && !rh->fPart.isCurve()) {
174 // see if original raw data is orderable
175 // if two share a point, check if third has both points in same half plane
176 int ltShare = lh->fOriginalCurvePart[0] == fOriginalCurvePart[0];
177 int lrShare = lh->fOriginalCurvePart[0] == rh->fOriginalCurvePart[0];
178 int trShare = fOriginalCurvePart[0] == rh->fOriginalCurvePart[0];
179 // if only one pair are the same, the third point touches neither of the pair
180 if (ltShare + lrShare + trShare == 1) {
181 if (ltShare) {
182 int lrOOrder = lh->allOnOriginalSide(rh);
183 int trOOrder = rh->allOnOriginalSide(this);
184 // result must be 0 and 1 or 1 and 0 to be valid
185 if ((lrOOrder ^ trOOrder) == 1) {
186 return trOOrder;
187 }
188 } else if (lrShare) {
189 int ltOOrder = lh->allOnOriginalSide(this);
190 int trOOrder = rh->allOnOriginalSide(this);
191 if ((ltOOrder ^ trOOrder) == 1) {
192 return ltOOrder;
193 }
194 } else {
195 SkASSERT(trShare);
196 int ltOOrder = this->allOnOriginalSide(lh);
197 int lrOOrder = rh->allOnOriginalSide(lh);
198 if ((ltOOrder ^ lrOOrder) == 1) {
199 return lrOOrder;
200 }
201 }
202 }
203 }
204 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000205 if (lrOrder < 0) {
206 if (ltOrder < 0) {
Cary Clark59d5a0e2017-01-23 14:38:52 +0000207 return COMPARE_RESULT(11, trOrder);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000208 }
Cary Clark59d5a0e2017-01-23 14:38:52 +0000209 return COMPARE_RESULT(12, ltOrder);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000210 }
Cary Clark59d5a0e2017-01-23 14:38:52 +0000211 return COMPARE_RESULT(13, !lrOrder);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000212}
213
214// given a line, see if the opposite curve's convex hull is all on one side
215// returns -1=not on one side 0=this CW of test 1=this CCW of test
caryclark54359292015-03-26 07:52:43 -0700216int SkOpAngle::allOnOneSide(const SkOpAngle* test) {
caryclarkeed356d2016-09-14 07:18:20 -0700217 SkASSERT(!fPart.isCurve());
218 SkASSERT(test->fPart.isCurve());
219 SkDPoint origin = fPart.fCurve[0];
220 SkDVector line = fPart.fCurve[1] - origin;
caryclark81681942016-07-21 10:44:07 -0700221 double crosses[3];
caryclark54359292015-03-26 07:52:43 -0700222 SkPath::Verb testVerb = test->segment()->verb();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000223 int iMax = SkPathOpsVerbToPoints(testVerb);
224// SkASSERT(origin == test.fCurveHalf[0]);
caryclarkeed356d2016-09-14 07:18:20 -0700225 const SkDCurve& testCurve = test->fPart.fCurve;
caryclark54359292015-03-26 07:52:43 -0700226 for (int index = 1; index <= iMax; ++index) {
caryclark81681942016-07-21 10:44:07 -0700227 double xy1 = line.fX * (testCurve[index].fY - origin.fY);
228 double xy2 = line.fY * (testCurve[index].fX - origin.fX);
229 crosses[index - 1] = AlmostBequalUlps(xy1, xy2) ? 0 : xy1 - xy2;
caryclark54359292015-03-26 07:52:43 -0700230 }
231 if (crosses[0] * crosses[1] < 0) {
232 return -1;
233 }
234 if (SkPath::kCubic_Verb == testVerb) {
235 if (crosses[0] * crosses[2] < 0 || crosses[1] * crosses[2] < 0) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000236 return -1;
237 }
caryclark54359292015-03-26 07:52:43 -0700238 }
239 if (crosses[0]) {
240 return crosses[0] < 0;
241 }
242 if (crosses[1]) {
243 return crosses[1] < 0;
244 }
245 if (SkPath::kCubic_Verb == testVerb && crosses[2]) {
246 return crosses[2] < 0;
247 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000248 fUnorderable = true;
249 return -1;
250}
251
Cary Clark3a4a3212018-06-06 15:22:08 -0400252// experiment works only with lines for now
253int SkOpAngle::allOnOriginalSide(const SkOpAngle* test) {
254 SkASSERT(!fPart.isCurve());
255 SkASSERT(!test->fPart.isCurve());
256 SkDPoint origin = fOriginalCurvePart[0];
257 SkDVector line = fOriginalCurvePart[1] - origin;
258 double dots[2];
259 double crosses[2];
260 const SkDCurve& testCurve = test->fOriginalCurvePart;
261 for (int index = 0; index < 2; ++index) {
262 SkDVector testLine = testCurve[index] - origin;
263 double xy1 = line.fX * testLine.fY;
264 double xy2 = line.fY * testLine.fX;
265 dots[index] = line.fX * testLine.fX + line.fY * testLine.fY;
266 crosses[index] = AlmostBequalUlps(xy1, xy2) ? 0 : xy1 - xy2;
267 }
268 if (crosses[0] * crosses[1] < 0) {
269 return -1;
270 }
271 if (crosses[0]) {
272 return crosses[0] < 0;
273 }
274 if (crosses[1]) {
275 return crosses[1] < 0;
276 }
277 if ((!dots[0] && dots[1] < 0) || (dots[0] < 0 && !dots[1])) {
278 return 2; // 180 degrees apart
279 }
280 fUnorderable = true;
281 return -1;
282}
283
Cary Clarkff114282016-12-14 11:56:16 -0500284// To sort the angles, all curves are translated to have the same starting point.
285// If the curve's control point in its original position is on one side of a compared line,
286// and translated is on the opposite side, reverse the previously computed order.
287void SkOpAngle::alignmentSameSide(const SkOpAngle* test, int* order) const {
288 if (*order < 0) {
289 return;
290 }
291 if (fPart.isCurve()) {
292 // This should support all curve types, but only bug that requires this has lines
293 // Turning on for curves causes existing tests to fail
294 return;
295 }
296 if (test->fPart.isCurve()) {
297 return;
298 }
299 const SkDPoint& xOrigin = test->fPart.fCurve.fLine[0];
300 const SkDPoint& oOrigin = test->fOriginalCurvePart.fLine[0];
301 if (xOrigin == oOrigin) {
302 return;
303 }
304 int iMax = SkPathOpsVerbToPoints(this->segment()->verb());
305 SkDVector xLine = test->fPart.fCurve.fLine[1] - xOrigin;
306 SkDVector oLine = test->fOriginalCurvePart.fLine[1] - oOrigin;
307 for (int index = 1; index <= iMax; ++index) {
308 const SkDPoint& testPt = fPart.fCurve[index];
309 double xCross = oLine.crossCheck(testPt - xOrigin);
310 double oCross = xLine.crossCheck(testPt - oOrigin);
311 if (oCross * xCross < 0) {
312 *order ^= 1;
313 break;
314 }
315 }
316}
317
Cary Clark59d5a0e2017-01-23 14:38:52 +0000318bool SkOpAngle::checkCrossesZero() const {
319 int start = SkTMin(fSectorStart, fSectorEnd);
320 int end = SkTMax(fSectorStart, fSectorEnd);
321 bool crossesZero = end - start > 16;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000322 return crossesZero;
323}
caryclark@google.com07393ca2013-04-08 11:47:37 +0000324
caryclark54359292015-03-26 07:52:43 -0700325bool SkOpAngle::checkParallel(SkOpAngle* rh) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000326 SkDVector scratch[2];
327 const SkDVector* sweep, * tweep;
caryclarkeed356d2016-09-14 07:18:20 -0700328 if (this->fPart.isOrdered()) {
329 sweep = this->fPart.fSweep;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000330 } else {
caryclarkeed356d2016-09-14 07:18:20 -0700331 scratch[0] = this->fPart.fCurve[1] - this->fPart.fCurve[0];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000332 sweep = &scratch[0];
caryclark@google.coma5e55922013-05-07 18:51:31 +0000333 }
caryclarkeed356d2016-09-14 07:18:20 -0700334 if (rh->fPart.isOrdered()) {
335 tweep = rh->fPart.fSweep;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000336 } else {
caryclarkeed356d2016-09-14 07:18:20 -0700337 scratch[1] = rh->fPart.fCurve[1] - rh->fPart.fCurve[0];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000338 tweep = &scratch[1];
caryclark@google.com07393ca2013-04-08 11:47:37 +0000339 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000340 double s0xt0 = sweep->crossCheck(*tweep);
341 if (tangentsDiverge(rh, s0xt0)) {
342 return s0xt0 < 0;
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +0000343 }
caryclark54359292015-03-26 07:52:43 -0700344 // compute the perpendicular to the endpoints and see where it intersects the opposite curve
345 // if the intersections within the t range, do a cross check on those
346 bool inside;
caryclark55888e42016-07-18 10:01:36 -0700347 if (!fEnd->contains(rh->fEnd)) {
caryclark1049f122015-04-20 08:31:59 -0700348 if (this->endToSide(rh, &inside)) {
349 return inside;
350 }
351 if (rh->endToSide(this, &inside)) {
352 return !inside;
353 }
caryclark54359292015-03-26 07:52:43 -0700354 }
355 if (this->midToSide(rh, &inside)) {
356 return inside;
357 }
358 if (rh->midToSide(this, &inside)) {
359 return !inside;
360 }
361 // compute the cross check from the mid T values (last resort)
caryclarkeed356d2016-09-14 07:18:20 -0700362 SkDVector m0 = segment()->dPtAtT(this->midT()) - this->fPart.fCurve[0];
363 SkDVector m1 = rh->segment()->dPtAtT(rh->midT()) - rh->fPart.fCurve[0];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000364 double m0xm1 = m0.crossCheck(m1);
365 if (m0xm1 == 0) {
caryclark54359292015-03-26 07:52:43 -0700366 this->fUnorderable = true;
367 rh->fUnorderable = true;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000368 return true;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000369 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000370 return m0xm1 < 0;
371}
372
373// the original angle is too short to get meaningful sector information
374// lengthen it until it is long enough to be meaningful or leave it unset if lengthening it
375// would cause it to intersect one of the adjacent angles
376bool SkOpAngle::computeSector() {
377 if (fComputedSector) {
caryclarkdac1d172014-06-17 05:15:38 -0700378 return !fUnorderable;
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000379 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000380 fComputedSector = true;
caryclark54359292015-03-26 07:52:43 -0700381 bool stepUp = fStart->t() < fEnd->t();
caryclark55888e42016-07-18 10:01:36 -0700382 SkOpSpanBase* checkEnd = fEnd;
caryclark54359292015-03-26 07:52:43 -0700383 if (checkEnd->final() && stepUp) {
caryclarkccec0f92015-03-24 07:28:17 -0700384 fUnorderable = true;
385 return false;
386 }
caryclark54359292015-03-26 07:52:43 -0700387 do {
388// advance end
389 const SkOpSegment* other = checkEnd->segment();
390 const SkOpSpanBase* oSpan = other->head();
391 do {
392 if (oSpan->segment() != segment()) {
393 continue;
394 }
395 if (oSpan == checkEnd) {
396 continue;
397 }
398 if (!approximately_equal(oSpan->t(), checkEnd->t())) {
399 continue;
400 }
401 goto recomputeSector;
402 } while (!oSpan->final() && (oSpan = oSpan->upCast()->next()));
403 checkEnd = stepUp ? !checkEnd->final()
halcanary96fcdcc2015-08-27 07:41:13 -0700404 ? checkEnd->upCast()->next() : nullptr
caryclark54359292015-03-26 07:52:43 -0700405 : checkEnd->prev();
406 } while (checkEnd);
407recomputeSector:
caryclark55888e42016-07-18 10:01:36 -0700408 SkOpSpanBase* computedEnd = stepUp ? checkEnd ? checkEnd->prev() : fEnd->segment()->head()
caryclark54359292015-03-26 07:52:43 -0700409 : checkEnd ? checkEnd->upCast()->next() : fEnd->segment()->tail();
410 if (checkEnd == fEnd || computedEnd == fEnd || computedEnd == fStart) {
411 fUnorderable = true;
412 return false;
413 }
caryclark624637c2015-05-11 07:21:27 -0700414 if (stepUp != (fStart->t() < computedEnd->t())) {
415 fUnorderable = true;
416 return false;
417 }
caryclark54359292015-03-26 07:52:43 -0700418 SkOpSpanBase* saveEnd = fEnd;
419 fComputedEnd = fEnd = computedEnd;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000420 setSpans();
421 setSector();
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000422 fEnd = saveEnd;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000423 return !fUnorderable;
424}
425
caryclarkb36a3cd2016-10-18 07:59:44 -0700426int SkOpAngle::convexHullOverlaps(const SkOpAngle* rh) {
caryclarkeed356d2016-09-14 07:18:20 -0700427 const SkDVector* sweep = this->fPart.fSweep;
428 const SkDVector* tweep = rh->fPart.fSweep;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000429 double s0xs1 = sweep[0].crossCheck(sweep[1]);
430 double s0xt0 = sweep[0].crossCheck(tweep[0]);
431 double s1xt0 = sweep[1].crossCheck(tweep[0]);
432 bool tBetweenS = s0xs1 > 0 ? s0xt0 > 0 && s1xt0 < 0 : s0xt0 < 0 && s1xt0 > 0;
433 double s0xt1 = sweep[0].crossCheck(tweep[1]);
434 double s1xt1 = sweep[1].crossCheck(tweep[1]);
435 tBetweenS |= s0xs1 > 0 ? s0xt1 > 0 && s1xt1 < 0 : s0xt1 < 0 && s1xt1 > 0;
436 double t0xt1 = tweep[0].crossCheck(tweep[1]);
437 if (tBetweenS) {
438 return -1;
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000439 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000440 if ((s0xt0 == 0 && s1xt1 == 0) || (s1xt0 == 0 && s0xt1 == 0)) { // s0 to s1 equals t0 to t1
441 return -1;
442 }
443 bool sBetweenT = t0xt1 > 0 ? s0xt0 < 0 && s0xt1 > 0 : s0xt0 > 0 && s0xt1 < 0;
444 sBetweenT |= t0xt1 > 0 ? s1xt0 < 0 && s1xt1 > 0 : s1xt0 > 0 && s1xt1 < 0;
445 if (sBetweenT) {
446 return -1;
447 }
448 // if all of the sweeps are in the same half plane, then the order of any pair is enough
449 if (s0xt0 >= 0 && s0xt1 >= 0 && s1xt0 >= 0 && s1xt1 >= 0) {
450 return 0;
451 }
452 if (s0xt0 <= 0 && s0xt1 <= 0 && s1xt0 <= 0 && s1xt1 <= 0) {
453 return 1;
454 }
455 // if the outside sweeps are greater than 180 degress:
456 // first assume the inital tangents are the ordering
457 // if the midpoint direction matches the inital order, that is enough
caryclarkeed356d2016-09-14 07:18:20 -0700458 SkDVector m0 = this->segment()->dPtAtT(this->midT()) - this->fPart.fCurve[0];
459 SkDVector m1 = rh->segment()->dPtAtT(rh->midT()) - rh->fPart.fCurve[0];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000460 double m0xm1 = m0.crossCheck(m1);
461 if (s0xt0 > 0 && m0xm1 > 0) {
462 return 0;
463 }
464 if (s0xt0 < 0 && m0xm1 < 0) {
465 return 1;
466 }
467 if (tangentsDiverge(rh, s0xt0)) {
468 return s0xt0 < 0;
469 }
470 return m0xm1 < 0;
471}
472
473// OPTIMIZATION: longest can all be either lazily computed here or precomputed in setup
474double SkOpAngle::distEndRatio(double dist) const {
475 double longest = 0;
476 const SkOpSegment& segment = *this->segment();
477 int ptCount = SkPathOpsVerbToPoints(segment.verb());
478 const SkPoint* pts = segment.pts();
479 for (int idx1 = 0; idx1 <= ptCount - 1; ++idx1) {
480 for (int idx2 = idx1 + 1; idx2 <= ptCount; ++idx2) {
481 if (idx1 == idx2) {
482 continue;
483 }
484 SkDVector v;
485 v.set(pts[idx2] - pts[idx1]);
486 double lenSq = v.lengthSquared();
487 longest = SkTMax(longest, lenSq);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000488 }
489 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000490 return sqrt(longest) / dist;
491}
492
caryclark54359292015-03-26 07:52:43 -0700493bool SkOpAngle::endsIntersect(SkOpAngle* rh) {
494 SkPath::Verb lVerb = this->segment()->verb();
495 SkPath::Verb rVerb = rh->segment()->verb();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000496 int lPts = SkPathOpsVerbToPoints(lVerb);
497 int rPts = SkPathOpsVerbToPoints(rVerb);
caryclarkeed356d2016-09-14 07:18:20 -0700498 SkDLine rays[] = {{{this->fPart.fCurve[0], rh->fPart.fCurve[rPts]}},
499 {{this->fPart.fCurve[0], this->fPart.fCurve[lPts]}}};
caryclark55888e42016-07-18 10:01:36 -0700500 if (this->fEnd->contains(rh->fEnd)) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000501 return checkParallel(rh);
502 }
503 double smallTs[2] = {-1, -1};
504 bool limited[2] = {false, false};
505 for (int index = 0; index < 2; ++index) {
caryclark1049f122015-04-20 08:31:59 -0700506 SkPath::Verb cVerb = index ? rVerb : lVerb;
caryclark65f55312014-11-13 06:58:52 -0800507 // if the curve is a line, then the line and the ray intersect only at their crossing
caryclark1049f122015-04-20 08:31:59 -0700508 if (cVerb == SkPath::kLine_Verb) {
caryclark65f55312014-11-13 06:58:52 -0800509 continue;
510 }
caryclark54359292015-03-26 07:52:43 -0700511 const SkOpSegment& segment = index ? *rh->segment() : *this->segment();
512 SkIntersections i;
caryclark1049f122015-04-20 08:31:59 -0700513 (*CurveIntersectRay[cVerb])(segment.pts(), segment.weight(), rays[index], &i);
caryclark54359292015-03-26 07:52:43 -0700514 double tStart = index ? rh->fStart->t() : this->fStart->t();
515 double tEnd = index ? rh->fComputedEnd->t() : this->fComputedEnd->t();
516 bool testAscends = tStart < (index ? rh->fComputedEnd->t() : this->fComputedEnd->t());
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000517 double t = testAscends ? 0 : 1;
518 for (int idx2 = 0; idx2 < i.used(); ++idx2) {
519 double testT = i[0][idx2];
520 if (!approximately_between_orderable(tStart, testT, tEnd)) {
521 continue;
522 }
523 if (approximately_equal_orderable(tStart, testT)) {
524 continue;
525 }
526 smallTs[index] = t = testAscends ? SkTMax(t, testT) : SkTMin(t, testT);
527 limited[index] = approximately_equal_orderable(t, tEnd);
528 }
529 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000530 bool sRayLonger = false;
531 SkDVector sCept = {0, 0};
532 double sCeptT = -1;
533 int sIndex = -1;
534 bool useIntersect = false;
535 for (int index = 0; index < 2; ++index) {
536 if (smallTs[index] < 0) {
537 continue;
538 }
caryclark54359292015-03-26 07:52:43 -0700539 const SkOpSegment& segment = index ? *rh->segment() : *this->segment();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000540 const SkDPoint& dPt = segment.dPtAtT(smallTs[index]);
541 SkDVector cept = dPt - rays[index][0];
542 // If this point is on the curve, it should have been detected earlier by ordinary
543 // curve intersection. This may be hard to determine in general, but for lines,
544 // the point could be close to or equal to its end, but shouldn't be near the start.
545 if ((index ? lPts : rPts) == 1) {
546 SkDVector total = rays[index][1] - rays[index][0];
547 if (cept.lengthSquared() * 2 < total.lengthSquared()) {
548 continue;
549 }
550 }
551 SkDVector end = rays[index][1] - rays[index][0];
552 if (cept.fX * end.fX < 0 || cept.fY * end.fY < 0) {
553 continue;
554 }
555 double rayDist = cept.length();
556 double endDist = end.length();
557 bool rayLonger = rayDist > endDist;
558 if (limited[0] && limited[1] && rayLonger) {
559 useIntersect = true;
560 sRayLonger = rayLonger;
561 sCept = cept;
562 sCeptT = smallTs[index];
563 sIndex = index;
caryclark@google.coma5e55922013-05-07 18:51:31 +0000564 break;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000565 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000566 double delta = fabs(rayDist - endDist);
567 double minX, minY, maxX, maxY;
568 minX = minY = SK_ScalarInfinity;
569 maxX = maxY = -SK_ScalarInfinity;
caryclarkeed356d2016-09-14 07:18:20 -0700570 const SkDCurve& curve = index ? rh->fPart.fCurve : this->fPart.fCurve;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000571 int ptCount = index ? rPts : lPts;
572 for (int idx2 = 0; idx2 <= ptCount; ++idx2) {
573 minX = SkTMin(minX, curve[idx2].fX);
574 minY = SkTMin(minY, curve[idx2].fY);
575 maxX = SkTMax(maxX, curve[idx2].fX);
576 maxY = SkTMax(maxY, curve[idx2].fY);
577 }
578 double maxWidth = SkTMax(maxX - minX, maxY - minY);
579 delta /= maxWidth;
caryclark54359292015-03-26 07:52:43 -0700580 if (delta > 1e-3 && (useIntersect ^= true)) { // FIXME: move this magic number
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000581 sRayLonger = rayLonger;
582 sCept = cept;
583 sCeptT = smallTs[index];
584 sIndex = index;
585 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000586 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000587 if (useIntersect) {
caryclarkeed356d2016-09-14 07:18:20 -0700588 const SkDCurve& curve = sIndex ? rh->fPart.fCurve : this->fPart.fCurve;
caryclark54359292015-03-26 07:52:43 -0700589 const SkOpSegment& segment = sIndex ? *rh->segment() : *this->segment();
590 double tStart = sIndex ? rh->fStart->t() : fStart->t();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000591 SkDVector mid = segment.dPtAtT(tStart + (sCeptT - tStart) / 2) - curve[0];
592 double septDir = mid.crossCheck(sCept);
593 if (!septDir) {
594 return checkParallel(rh);
595 }
596 return sRayLonger ^ (sIndex == 0) ^ (septDir < 0);
597 } else {
598 return checkParallel(rh);
caryclark@google.coma5e55922013-05-07 18:51:31 +0000599 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000600}
601
caryclark54359292015-03-26 07:52:43 -0700602bool SkOpAngle::endToSide(const SkOpAngle* rh, bool* inside) const {
603 const SkOpSegment* segment = this->segment();
604 SkPath::Verb verb = segment->verb();
caryclark54359292015-03-26 07:52:43 -0700605 SkDLine rayEnd;
606 rayEnd[0].set(this->fEnd->pt());
607 rayEnd[1] = rayEnd[0];
caryclark1049f122015-04-20 08:31:59 -0700608 SkDVector slopeAtEnd = (*CurveDSlopeAtT[verb])(segment->pts(), segment->weight(),
609 this->fEnd->t());
caryclark54359292015-03-26 07:52:43 -0700610 rayEnd[1].fX += slopeAtEnd.fY;
611 rayEnd[1].fY -= slopeAtEnd.fX;
612 SkIntersections iEnd;
613 const SkOpSegment* oppSegment = rh->segment();
614 SkPath::Verb oppVerb = oppSegment->verb();
caryclark1049f122015-04-20 08:31:59 -0700615 (*CurveIntersectRay[oppVerb])(oppSegment->pts(), oppSegment->weight(), rayEnd, &iEnd);
caryclark54359292015-03-26 07:52:43 -0700616 double endDist;
617 int closestEnd = iEnd.closestTo(rh->fStart->t(), rh->fEnd->t(), rayEnd[0], &endDist);
618 if (closestEnd < 0) {
619 return false;
620 }
621 if (!endDist) {
622 return false;
623 }
624 SkDPoint start;
625 start.set(this->fStart->pt());
626 // OPTIMIZATION: multiple times in the code we find the max scalar
627 double minX, minY, maxX, maxY;
628 minX = minY = SK_ScalarInfinity;
629 maxX = maxY = -SK_ScalarInfinity;
caryclarkeed356d2016-09-14 07:18:20 -0700630 const SkDCurve& curve = rh->fPart.fCurve;
caryclark1049f122015-04-20 08:31:59 -0700631 int oppPts = SkPathOpsVerbToPoints(oppVerb);
caryclark54359292015-03-26 07:52:43 -0700632 for (int idx2 = 0; idx2 <= oppPts; ++idx2) {
633 minX = SkTMin(minX, curve[idx2].fX);
634 minY = SkTMin(minY, curve[idx2].fY);
635 maxX = SkTMax(maxX, curve[idx2].fX);
636 maxY = SkTMax(maxY, curve[idx2].fY);
637 }
638 double maxWidth = SkTMax(maxX - minX, maxY - minY);
639 endDist /= maxWidth;
caryclark55888e42016-07-18 10:01:36 -0700640 if (endDist < 5e-12) { // empirically found
caryclark54359292015-03-26 07:52:43 -0700641 return false;
642 }
643 const SkDPoint* endPt = &rayEnd[0];
644 SkDPoint oppPt = iEnd.pt(closestEnd);
645 SkDVector vLeft = *endPt - start;
646 SkDVector vRight = oppPt - start;
caryclark55888e42016-07-18 10:01:36 -0700647 double dir = vLeft.crossNoNormalCheck(vRight);
caryclark54359292015-03-26 07:52:43 -0700648 if (!dir) {
649 return false;
650 }
651 *inside = dir < 0;
652 return true;
653}
654
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000655/* y<0 y==0 y>0 x<0 x==0 x>0 xy<0 xy==0 xy>0
656 0 x x x
657 1 x x x
658 2 x x x
659 3 x x x
660 4 x x x
661 5 x x x
662 6 x x x
663 7 x x x
664 8 x x x
665 9 x x x
666 10 x x x
667 11 x x x
668 12 x x x
669 13 x x x
670 14 x x x
671 15 x x x
672*/
673int SkOpAngle::findSector(SkPath::Verb verb, double x, double y) const {
674 double absX = fabs(x);
675 double absY = fabs(y);
676 double xy = SkPath::kLine_Verb == verb || !AlmostEqualUlps(absX, absY) ? absX - absY : 0;
677 // If there are four quadrants and eight octants, and since the Latin for sixteen is sedecim,
678 // one could coin the term sedecimant for a space divided into 16 sections.
679 // http://english.stackexchange.com/questions/133688/word-for-something-partitioned-into-16-parts
680 static const int sedecimant[3][3][3] = {
681 // y<0 y==0 y>0
682 // x<0 x==0 x>0 x<0 x==0 x>0 x<0 x==0 x>0
683 {{ 4, 3, 2}, { 7, -1, 15}, {10, 11, 12}}, // abs(x) < abs(y)
684 {{ 5, -1, 1}, {-1, -1, -1}, { 9, -1, 13}}, // abs(x) == abs(y)
685 {{ 6, 3, 0}, { 7, -1, 15}, { 8, 11, 14}}, // abs(x) > abs(y)
686 };
687 int sector = sedecimant[(xy >= 0) + (xy > 0)][(y >= 0) + (y > 0)][(x >= 0) + (x > 0)] * 2 + 1;
caryclark65b427c2014-09-18 10:32:57 -0700688// SkASSERT(SkPath::kLine_Verb == verb || sector >= 0);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000689 return sector;
690}
691
caryclark54359292015-03-26 07:52:43 -0700692SkOpGlobalState* SkOpAngle::globalState() const {
693 return this->segment()->globalState();
694}
695
Cary Clark59d5a0e2017-01-23 14:38:52 +0000696
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000697// OPTIMIZE: if this loops to only one other angle, after first compare fails, insert on other side
698// OPTIMIZE: return where insertion succeeded. Then, start next insertion on opposite side
caryclarkb36a3cd2016-10-18 07:59:44 -0700699bool SkOpAngle::insert(SkOpAngle* angle) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000700 if (angle->fNext) {
701 if (loopCount() >= angle->loopCount()) {
702 if (!merge(angle)) {
caryclarkb36a3cd2016-10-18 07:59:44 -0700703 return true;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000704 }
705 } else if (fNext) {
706 if (!angle->merge(this)) {
caryclarkb36a3cd2016-10-18 07:59:44 -0700707 return true;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000708 }
709 } else {
710 angle->insert(this);
711 }
caryclarkb36a3cd2016-10-18 07:59:44 -0700712 return true;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000713 }
halcanary96fcdcc2015-08-27 07:41:13 -0700714 bool singleton = nullptr == fNext;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000715 if (singleton) {
716 fNext = this;
717 }
718 SkOpAngle* next = fNext;
719 if (next->fNext == this) {
720 if (singleton || angle->after(this)) {
721 this->fNext = angle;
722 angle->fNext = next;
723 } else {
724 next->fNext = angle;
725 angle->fNext = this;
726 }
727 debugValidateNext();
caryclarkb36a3cd2016-10-18 07:59:44 -0700728 return true;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000729 }
730 SkOpAngle* last = this;
caryclarkb36a3cd2016-10-18 07:59:44 -0700731 bool flipAmbiguity = false;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000732 do {
733 SkASSERT(last->fNext == next);
caryclarkb36a3cd2016-10-18 07:59:44 -0700734 if (angle->after(last) ^ (angle->tangentsAmbiguous() & flipAmbiguity)) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000735 last->fNext = angle;
736 angle->fNext = next;
737 debugValidateNext();
caryclarkb36a3cd2016-10-18 07:59:44 -0700738 return true;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000739 }
740 last = next;
caryclarkb36a3cd2016-10-18 07:59:44 -0700741 if (last == this) {
742 FAIL_IF(flipAmbiguity);
743 // We're in a loop. If a sort was ambiguous, flip it to end the loop.
744 flipAmbiguity = true;
745 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000746 next = next->fNext;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000747 } while (true);
caryclarkb36a3cd2016-10-18 07:59:44 -0700748 return true;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000749}
750
caryclark54359292015-03-26 07:52:43 -0700751SkOpSpanBase* SkOpAngle::lastMarked() const {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000752 if (fLastMarked) {
caryclark54359292015-03-26 07:52:43 -0700753 if (fLastMarked->chased()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700754 return nullptr;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000755 }
caryclark54359292015-03-26 07:52:43 -0700756 fLastMarked->setChased(true);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000757 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000758 return fLastMarked;
759}
760
caryclark54359292015-03-26 07:52:43 -0700761bool SkOpAngle::loopContains(const SkOpAngle* angle) const {
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000762 if (!fNext) {
763 return false;
764 }
765 const SkOpAngle* first = this;
766 const SkOpAngle* loop = this;
caryclark54359292015-03-26 07:52:43 -0700767 const SkOpSegment* tSegment = angle->fStart->segment();
768 double tStart = angle->fStart->t();
769 double tEnd = angle->fEnd->t();
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000770 do {
caryclark54359292015-03-26 07:52:43 -0700771 const SkOpSegment* lSegment = loop->fStart->segment();
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000772 if (lSegment != tSegment) {
773 continue;
774 }
caryclark54359292015-03-26 07:52:43 -0700775 double lStart = loop->fStart->t();
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000776 if (lStart != tEnd) {
777 continue;
778 }
caryclark54359292015-03-26 07:52:43 -0700779 double lEnd = loop->fEnd->t();
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000780 if (lEnd == tStart) {
781 return true;
782 }
783 } while ((loop = loop->fNext) != first);
784 return false;
785}
786
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000787int SkOpAngle::loopCount() const {
788 int count = 0;
789 const SkOpAngle* first = this;
790 const SkOpAngle* next = this;
791 do {
792 next = next->fNext;
793 ++count;
794 } while (next && next != first);
795 return count;
796}
797
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000798bool SkOpAngle::merge(SkOpAngle* angle) {
799 SkASSERT(fNext);
800 SkASSERT(angle->fNext);
801 SkOpAngle* working = angle;
802 do {
803 if (this == working) {
804 return false;
805 }
806 working = working->fNext;
807 } while (working != angle);
808 do {
809 SkOpAngle* next = working->fNext;
halcanary96fcdcc2015-08-27 07:41:13 -0700810 working->fNext = nullptr;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000811 insert(working);
812 working = next;
813 } while (working != angle);
814 // it's likely that a pair of the angles are unorderable
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000815 debugValidateNext();
816 return true;
817}
818
819double SkOpAngle::midT() const {
caryclark54359292015-03-26 07:52:43 -0700820 return (fStart->t() + fEnd->t()) / 2;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000821}
822
caryclark54359292015-03-26 07:52:43 -0700823bool SkOpAngle::midToSide(const SkOpAngle* rh, bool* inside) const {
824 const SkOpSegment* segment = this->segment();
825 SkPath::Verb verb = segment->verb();
caryclark54359292015-03-26 07:52:43 -0700826 const SkPoint& startPt = this->fStart->pt();
827 const SkPoint& endPt = this->fEnd->pt();
828 SkDPoint dStartPt;
829 dStartPt.set(startPt);
830 SkDLine rayMid;
831 rayMid[0].fX = (startPt.fX + endPt.fX) / 2;
832 rayMid[0].fY = (startPt.fY + endPt.fY) / 2;
833 rayMid[1].fX = rayMid[0].fX + (endPt.fY - startPt.fY);
834 rayMid[1].fY = rayMid[0].fY - (endPt.fX - startPt.fX);
835 SkIntersections iMid;
caryclark1049f122015-04-20 08:31:59 -0700836 (*CurveIntersectRay[verb])(segment->pts(), segment->weight(), rayMid, &iMid);
caryclark54359292015-03-26 07:52:43 -0700837 int iOutside = iMid.mostOutside(this->fStart->t(), this->fEnd->t(), dStartPt);
838 if (iOutside < 0) {
839 return false;
840 }
841 const SkOpSegment* oppSegment = rh->segment();
842 SkPath::Verb oppVerb = oppSegment->verb();
caryclark54359292015-03-26 07:52:43 -0700843 SkIntersections oppMid;
caryclark1049f122015-04-20 08:31:59 -0700844 (*CurveIntersectRay[oppVerb])(oppSegment->pts(), oppSegment->weight(), rayMid, &oppMid);
caryclark54359292015-03-26 07:52:43 -0700845 int oppOutside = oppMid.mostOutside(rh->fStart->t(), rh->fEnd->t(), dStartPt);
846 if (oppOutside < 0) {
847 return false;
848 }
849 SkDVector iSide = iMid.pt(iOutside) - dStartPt;
850 SkDVector oppSide = oppMid.pt(oppOutside) - dStartPt;
851 double dir = iSide.crossCheck(oppSide);
852 if (!dir) {
853 return false;
854 }
855 *inside = dir < 0;
856 return true;
857}
858
859bool SkOpAngle::oppositePlanes(const SkOpAngle* rh) const {
bungeman60e0fee2015-08-26 05:15:46 -0700860 int startSpan = SkTAbs(rh->fSectorStart - fSectorStart);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000861 return startSpan >= 8;
862}
863
caryclark54359292015-03-26 07:52:43 -0700864bool SkOpAngle::orderable(SkOpAngle* rh) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000865 int result;
caryclarkeed356d2016-09-14 07:18:20 -0700866 if (!fPart.isCurve()) {
867 if (!rh->fPart.isCurve()) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000868 double leftX = fTangentHalf.dx();
869 double leftY = fTangentHalf.dy();
caryclark54359292015-03-26 07:52:43 -0700870 double rightX = rh->fTangentHalf.dx();
871 double rightY = rh->fTangentHalf.dy();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000872 double x_ry = leftX * rightY;
873 double rx_y = rightX * leftY;
874 if (x_ry == rx_y) {
875 if (leftX * rightX < 0 || leftY * rightY < 0) {
876 return true; // exactly 180 degrees apart
877 }
878 goto unorderable;
879 }
880 SkASSERT(x_ry != rx_y); // indicates an undetected coincidence -- worth finding earlier
881 return x_ry < rx_y;
882 }
caryclark55888e42016-07-18 10:01:36 -0700883 if ((result = this->allOnOneSide(rh)) >= 0) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000884 return result;
885 }
caryclark54359292015-03-26 07:52:43 -0700886 if (fUnorderable || approximately_zero(rh->fSide)) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000887 goto unorderable;
888 }
caryclarkeed356d2016-09-14 07:18:20 -0700889 } else if (!rh->fPart.isCurve()) {
caryclark54359292015-03-26 07:52:43 -0700890 if ((result = rh->allOnOneSide(this)) >= 0) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000891 return !result;
892 }
caryclark54359292015-03-26 07:52:43 -0700893 if (rh->fUnorderable || approximately_zero(fSide)) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000894 goto unorderable;
895 }
caryclark55888e42016-07-18 10:01:36 -0700896 } else if ((result = this->convexHullOverlaps(rh)) >= 0) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000897 return result;
898 }
caryclark55888e42016-07-18 10:01:36 -0700899 return this->endsIntersect(rh);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000900unorderable:
901 fUnorderable = true;
caryclark54359292015-03-26 07:52:43 -0700902 rh->fUnorderable = true;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000903 return true;
904}
905
906// OPTIMIZE: if this shows up in a profile, add a previous pointer
907// as is, this should be rarely called
908SkOpAngle* SkOpAngle::previous() const {
909 SkOpAngle* last = fNext;
910 do {
911 SkOpAngle* next = last->fNext;
912 if (next == this) {
913 return last;
914 }
915 last = next;
916 } while (true);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000917}
918
caryclark54359292015-03-26 07:52:43 -0700919SkOpSegment* SkOpAngle::segment() const {
920 return fStart->segment();
921}
922
923void SkOpAngle::set(SkOpSpanBase* start, SkOpSpanBase* end) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000924 fStart = start;
caryclarkdac1d172014-06-17 05:15:38 -0700925 fComputedEnd = fEnd = end;
caryclark54359292015-03-26 07:52:43 -0700926 SkASSERT(start != end);
halcanary96fcdcc2015-08-27 07:41:13 -0700927 fNext = nullptr;
caryclarkb36a3cd2016-10-18 07:59:44 -0700928 fComputeSector = fComputedSector = fCheckCoincidence = fTangentsAmbiguous = false;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000929 setSpans();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000930 setSector();
caryclark1049f122015-04-20 08:31:59 -0700931 SkDEBUGCODE(fID = start ? start->globalState()->nextAngleID() : -1);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000932}
933
caryclark@google.com07393ca2013-04-08 11:47:37 +0000934void SkOpAngle::setSpans() {
caryclark54359292015-03-26 07:52:43 -0700935 fUnorderable = false;
halcanary96fcdcc2015-08-27 07:41:13 -0700936 fLastMarked = nullptr;
caryclark1049f122015-04-20 08:31:59 -0700937 if (!fStart) {
938 fUnorderable = true;
939 return;
940 }
caryclark54359292015-03-26 07:52:43 -0700941 const SkOpSegment* segment = fStart->segment();
942 const SkPoint* pts = segment->pts();
caryclark6c3b9cd2016-09-26 05:36:58 -0700943 SkDEBUGCODE(fPart.fCurve.fVerb = SkPath::kCubic_Verb); // required for SkDCurve debug check
caryclarkeed356d2016-09-14 07:18:20 -0700944 SkDEBUGCODE(fPart.fCurve[2].fX = fPart.fCurve[2].fY = fPart.fCurve[3].fX = fPart.fCurve[3].fY
caryclark6c3b9cd2016-09-26 05:36:58 -0700945 = SK_ScalarNaN); // make the non-line part uninitialized
946 SkDEBUGCODE(fPart.fCurve.fVerb = segment->verb()); // set the curve type for real
947 segment->subDivide(fStart, fEnd, &fPart.fCurve); // set at least the line part if not more
caryclarkeed356d2016-09-14 07:18:20 -0700948 fOriginalCurvePart = fPart.fCurve;
caryclark54359292015-03-26 07:52:43 -0700949 const SkPath::Verb verb = segment->verb();
caryclarkeed356d2016-09-14 07:18:20 -0700950 fPart.setCurveHullSweep(verb);
951 if (SkPath::kLine_Verb != verb && !fPart.isCurve()) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000952 SkDLine lineHalf;
caryclarkeed356d2016-09-14 07:18:20 -0700953 fPart.fCurve[1] = fPart.fCurve[SkPathOpsVerbToPoints(verb)];
954 fOriginalCurvePart[1] = fPart.fCurve[1];
955 lineHalf[0].set(fPart.fCurve[0].asSkPoint());
956 lineHalf[1].set(fPart.fCurve[1].asSkPoint());
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000957 fTangentHalf.lineEndPoints(lineHalf);
958 fSide = 0;
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000959 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000960 switch (verb) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000961 case SkPath::kLine_Verb: {
caryclark@google.com570863f2013-09-16 15:55:01 +0000962 SkASSERT(fStart != fEnd);
caryclark54359292015-03-26 07:52:43 -0700963 const SkPoint& cP1 = pts[fStart->t() < fEnd->t()];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000964 SkDLine lineHalf;
caryclark54359292015-03-26 07:52:43 -0700965 lineHalf[0].set(fStart->pt());
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000966 lineHalf[1].set(cP1);
967 fTangentHalf.lineEndPoints(lineHalf);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000968 fSide = 0;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000969 } return;
caryclark1049f122015-04-20 08:31:59 -0700970 case SkPath::kQuad_Verb:
971 case SkPath::kConic_Verb: {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000972 SkLineParameters tangentPart;
caryclarkeed356d2016-09-14 07:18:20 -0700973 (void) tangentPart.quadEndPoints(fPart.fCurve.fQuad);
974 fSide = -tangentPart.pointDistance(fPart.fCurve[2]); // not normalized -- compare sign only
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000975 } break;
976 case SkPath::kCubic_Verb: {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000977 SkLineParameters tangentPart;
caryclarkeed356d2016-09-14 07:18:20 -0700978 (void) tangentPart.cubicPart(fPart.fCurve.fCubic);
979 fSide = -tangentPart.pointDistance(fPart.fCurve[3]);
caryclark@google.comb3f09212013-04-17 15:49:16 +0000980 double testTs[4];
981 // OPTIMIZATION: keep inflections precomputed with cubic segment?
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000982 int testCount = SkDCubic::FindInflections(pts, testTs);
caryclark54359292015-03-26 07:52:43 -0700983 double startT = fStart->t();
984 double endT = fEnd->t();
caryclark@google.comb3f09212013-04-17 15:49:16 +0000985 double limitT = endT;
986 int index;
987 for (index = 0; index < testCount; ++index) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000988 if (!::between(startT, testTs[index], limitT)) {
caryclark@google.comb3f09212013-04-17 15:49:16 +0000989 testTs[index] = -1;
990 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000991 }
caryclark@google.comb3f09212013-04-17 15:49:16 +0000992 testTs[testCount++] = startT;
993 testTs[testCount++] = endT;
commit-bot@chromium.orgb76d3b62013-04-22 19:55:19 +0000994 SkTQSort<double>(testTs, &testTs[testCount - 1]);
caryclark@google.comb3f09212013-04-17 15:49:16 +0000995 double bestSide = 0;
996 int testCases = (testCount << 1) - 1;
997 index = 0;
998 while (testTs[index] < 0) {
999 ++index;
1000 }
1001 index <<= 1;
1002 for (; index < testCases; ++index) {
1003 int testIndex = index >> 1;
1004 double testT = testTs[testIndex];
1005 if (index & 1) {
1006 testT = (testT + testTs[testIndex + 1]) / 2;
1007 }
1008 // OPTIMIZE: could avoid call for t == startT, endT
caryclark1049f122015-04-20 08:31:59 -07001009 SkDPoint pt = dcubic_xy_at_t(pts, segment->weight(), testT);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001010 SkLineParameters tangentPart;
caryclarkeed356d2016-09-14 07:18:20 -07001011 tangentPart.cubicEndPoints(fPart.fCurve.fCubic);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001012 double testSide = tangentPart.pointDistance(pt);
caryclark@google.comb3f09212013-04-17 15:49:16 +00001013 if (fabs(bestSide) < fabs(testSide)) {
1014 bestSide = testSide;
1015 }
1016 }
1017 fSide = -bestSide; // compare sign only
caryclark@google.com07393ca2013-04-08 11:47:37 +00001018 } break;
1019 default:
1020 SkASSERT(0);
1021 }
caryclark@google.com07393ca2013-04-08 11:47:37 +00001022}
caryclark@google.com570863f2013-09-16 15:55:01 +00001023
caryclark54359292015-03-26 07:52:43 -07001024void SkOpAngle::setSector() {
caryclark1049f122015-04-20 08:31:59 -07001025 if (!fStart) {
1026 fUnorderable = true;
1027 return;
1028 }
caryclark54359292015-03-26 07:52:43 -07001029 const SkOpSegment* segment = fStart->segment();
1030 SkPath::Verb verb = segment->verb();
caryclarkeed356d2016-09-14 07:18:20 -07001031 fSectorStart = this->findSector(verb, fPart.fSweep[0].fX, fPart.fSweep[0].fY);
caryclark54359292015-03-26 07:52:43 -07001032 if (fSectorStart < 0) {
1033 goto deferTilLater;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001034 }
caryclarkeed356d2016-09-14 07:18:20 -07001035 if (!fPart.isCurve()) { // if it's a line or line-like, note that both sectors are the same
caryclark54359292015-03-26 07:52:43 -07001036 SkASSERT(fSectorStart >= 0);
1037 fSectorEnd = fSectorStart;
1038 fSectorMask = 1 << fSectorStart;
1039 return;
1040 }
1041 SkASSERT(SkPath::kLine_Verb != verb);
caryclarkeed356d2016-09-14 07:18:20 -07001042 fSectorEnd = this->findSector(verb, fPart.fSweep[1].fX, fPart.fSweep[1].fY);
caryclark54359292015-03-26 07:52:43 -07001043 if (fSectorEnd < 0) {
1044deferTilLater:
1045 fSectorStart = fSectorEnd = -1;
1046 fSectorMask = 0;
1047 fComputeSector = true; // can't determine sector until segment length can be found
1048 return;
1049 }
1050 if (fSectorEnd == fSectorStart
1051 && (fSectorStart & 3) != 3) { // if the sector has no span, it can't be an exact angle
1052 fSectorMask = 1 << fSectorStart;
1053 return;
1054 }
Cary Clark59d5a0e2017-01-23 14:38:52 +00001055 bool crossesZero = this->checkCrossesZero();
1056 int start = SkTMin(fSectorStart, fSectorEnd);
1057 bool curveBendsCCW = (fSectorStart == start) ^ crossesZero;
1058 // bump the start and end of the sector span if they are on exact compass points
1059 if ((fSectorStart & 3) == 3) {
1060 fSectorStart = (fSectorStart + (curveBendsCCW ? 1 : 31)) & 0x1f;
caryclark54359292015-03-26 07:52:43 -07001061 }
Cary Clark59d5a0e2017-01-23 14:38:52 +00001062 if ((fSectorEnd & 3) == 3) {
1063 fSectorEnd = (fSectorEnd + (curveBendsCCW ? 31 : 1)) & 0x1f;
1064 }
1065 crossesZero = this->checkCrossesZero();
1066 start = SkTMin(fSectorStart, fSectorEnd);
1067 int end = SkTMax(fSectorStart, fSectorEnd);
caryclark54359292015-03-26 07:52:43 -07001068 if (!crossesZero) {
1069 fSectorMask = (unsigned) -1 >> (31 - end + start) << start;
1070 } else {
Cary Clark59d5a0e2017-01-23 14:38:52 +00001071 fSectorMask = (unsigned) -1 >> (31 - start) | ((unsigned) -1 << end);
caryclark54359292015-03-26 07:52:43 -07001072 }
caryclark@google.com570863f2013-09-16 15:55:01 +00001073}
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001074
caryclark54359292015-03-26 07:52:43 -07001075SkOpSpan* SkOpAngle::starter() {
1076 return fStart->starter(fEnd);
1077}
1078
caryclarkb36a3cd2016-10-18 07:59:44 -07001079bool SkOpAngle::tangentsDiverge(const SkOpAngle* rh, double s0xt0) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001080 if (s0xt0 == 0) {
1081 return false;
1082 }
1083 // if the ctrl tangents are not nearly parallel, use them
1084 // solve for opposite direction displacement scale factor == m
1085 // initial dir = v1.cross(v2) == v2.x * v1.y - v2.y * v1.x
1086 // displacement of q1[1] : dq1 = { -m * v1.y, m * v1.x } + q1[1]
1087 // straight angle when : v2.x * (dq1.y - q1[0].y) == v2.y * (dq1.x - q1[0].x)
1088 // v2.x * (m * v1.x + v1.y) == v2.y * (-m * v1.y + v1.x)
1089 // - m * (v2.x * v1.x + v2.y * v1.y) == v2.x * v1.y - v2.y * v1.x
1090 // m = (v2.y * v1.x - v2.x * v1.y) / (v2.x * v1.x + v2.y * v1.y)
1091 // m = v1.cross(v2) / v1.dot(v2)
caryclarkeed356d2016-09-14 07:18:20 -07001092 const SkDVector* sweep = fPart.fSweep;
1093 const SkDVector* tweep = rh->fPart.fSweep;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001094 double s0dt0 = sweep[0].dot(tweep[0]);
1095 if (!s0dt0) {
1096 return true;
1097 }
1098 SkASSERT(s0dt0 != 0);
1099 double m = s0xt0 / s0dt0;
1100 double sDist = sweep[0].length() * m;
1101 double tDist = tweep[0].length() * m;
1102 bool useS = fabs(sDist) < fabs(tDist);
caryclark54359292015-03-26 07:52:43 -07001103 double mFactor = fabs(useS ? this->distEndRatio(sDist) : rh->distEndRatio(tDist));
caryclarkb36a3cd2016-10-18 07:59:44 -07001104 fTangentsAmbiguous = mFactor >= 50 && mFactor < 200;
caryclark55888e42016-07-18 10:01:36 -07001105 return mFactor < 50; // empirically found limit
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001106}