blob: 36c0dd90f7c4fcfffd70316656af5061df3d0bc6 [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);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000065#if DEBUG_ANGLE
66 SkString bugOut;
67 bugOut.printf("%s [%d/%d] %d/%d tStart=%1.9g tEnd=%1.9g"
68 " < [%d/%d] %d/%d tStart=%1.9g tEnd=%1.9g"
69 " < [%d/%d] %d/%d tStart=%1.9g tEnd=%1.9g ", __FUNCTION__,
caryclark54359292015-03-26 07:52:43 -070070 lh->segment()->debugID(), lh->debugID(), lh->fSectorStart, lh->fSectorEnd,
71 lh->fStart->t(), lh->fEnd->t(),
72 segment()->debugID(), debugID(), fSectorStart, fSectorEnd, fStart->t(), fEnd->t(),
73 rh->segment()->debugID(), rh->debugID(), rh->fSectorStart, rh->fSectorEnd,
74 rh->fStart->t(), rh->fEnd->t());
75 SkString bugPart[3] = { lh->debugPart(), this->debugPart(), rh->debugPart() };
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000076#endif
caryclark54359292015-03-26 07:52:43 -070077 if (lh->fComputeSector && !lh->computeSector()) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000078 return COMPARE_RESULT(1, true);
79 }
caryclark54359292015-03-26 07:52:43 -070080 if (fComputeSector && !this->computeSector()) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000081 return COMPARE_RESULT(2, true);
82 }
caryclark54359292015-03-26 07:52:43 -070083 if (rh->fComputeSector && !rh->computeSector()) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000084 return COMPARE_RESULT(3, true);
85 }
86#if DEBUG_ANGLE // reset bugOut with computed sectors
87 bugOut.printf("%s [%d/%d] %d/%d tStart=%1.9g tEnd=%1.9g"
88 " < [%d/%d] %d/%d tStart=%1.9g tEnd=%1.9g"
89 " < [%d/%d] %d/%d tStart=%1.9g tEnd=%1.9g ", __FUNCTION__,
caryclark54359292015-03-26 07:52:43 -070090 lh->segment()->debugID(), lh->debugID(), lh->fSectorStart, lh->fSectorEnd,
91 lh->fStart->t(), lh->fEnd->t(),
92 segment()->debugID(), debugID(), fSectorStart, fSectorEnd, fStart->t(), fEnd->t(),
93 rh->segment()->debugID(), rh->debugID(), rh->fSectorStart, rh->fSectorEnd,
94 rh->fStart->t(), rh->fEnd->t());
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000095#endif
caryclark54359292015-03-26 07:52:43 -070096 bool ltrOverlap = (lh->fSectorMask | rh->fSectorMask) & fSectorMask;
97 bool lrOverlap = lh->fSectorMask & rh->fSectorMask;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000098 int lrOrder; // set to -1 if either order works
99 if (!lrOverlap) { // no lh/rh sector overlap
100 if (!ltrOverlap) { // no lh/this/rh sector overlap
caryclark54359292015-03-26 07:52:43 -0700101 return COMPARE_RESULT(4, (lh->fSectorEnd > rh->fSectorStart)
102 ^ (fSectorStart > lh->fSectorEnd) ^ (fSectorStart > rh->fSectorStart));
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000103 }
caryclark54359292015-03-26 07:52:43 -0700104 int lrGap = (rh->fSectorStart - lh->fSectorStart + 32) & 0x1f;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000105 /* A tiny change can move the start +/- 4. The order can only be determined if
106 lr gap is not 12 to 20 or -12 to -20.
107 -31 ..-21 1
108 -20 ..-12 -1
109 -11 .. -1 0
110 0 shouldn't get here
111 11 .. 1 1
112 12 .. 20 -1
113 21 .. 31 0
114 */
115 lrOrder = lrGap > 20 ? 0 : lrGap > 11 ? -1 : 1;
116 } else {
caryclark54359292015-03-26 07:52:43 -0700117 lrOrder = (int) lh->orderable(rh);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000118 if (!ltrOverlap) {
119 return COMPARE_RESULT(5, !lrOrder);
120 }
121 }
122 int ltOrder;
caryclark54359292015-03-26 07:52:43 -0700123 SkASSERT((lh->fSectorMask & fSectorMask) || (rh->fSectorMask & fSectorMask));
124 if (lh->fSectorMask & fSectorMask) {
125 ltOrder = (int) lh->orderable(this);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000126 } else {
caryclark54359292015-03-26 07:52:43 -0700127 int ltGap = (fSectorStart - lh->fSectorStart + 32) & 0x1f;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000128 ltOrder = ltGap > 20 ? 0 : ltGap > 11 ? -1 : 1;
129 }
130 int trOrder;
caryclark54359292015-03-26 07:52:43 -0700131 if (rh->fSectorMask & fSectorMask) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000132 trOrder = (int) orderable(rh);
133 } else {
caryclark54359292015-03-26 07:52:43 -0700134 int trGap = (rh->fSectorStart - fSectorStart + 32) & 0x1f;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000135 trOrder = trGap > 20 ? 0 : trGap > 11 ? -1 : 1;
136 }
137 if (lrOrder >= 0 && ltOrder >= 0 && trOrder >= 0) {
138 return COMPARE_RESULT(7, lrOrder ? (ltOrder & trOrder) : (ltOrder | trOrder));
139 }
140 SkASSERT(lrOrder >= 0 || ltOrder >= 0 || trOrder >= 0);
141// There's not enough information to sort. Get the pairs of angles in opposite planes.
142// If an order is < 0, the pair is already in an opposite plane. Check the remaining pairs.
143 // FIXME : once all variants are understood, rewrite this more simply
144 if (ltOrder == 0 && lrOrder == 0) {
145 SkASSERT(trOrder < 0);
146 // FIXME : once this is verified to work, remove one opposite angle call
caryclark54359292015-03-26 07:52:43 -0700147 SkDEBUGCODE(bool lrOpposite = lh->oppositePlanes(rh));
148 bool ltOpposite = lh->oppositePlanes(this);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000149 SkASSERT(lrOpposite != ltOpposite);
150 return COMPARE_RESULT(8, ltOpposite);
151 } else if (ltOrder == 1 && trOrder == 0) {
152 SkASSERT(lrOrder < 0);
caryclark54359292015-03-26 07:52:43 -0700153 SkDEBUGCODE(bool ltOpposite = lh->oppositePlanes(this));
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000154 bool trOpposite = oppositePlanes(rh);
155 SkASSERT(ltOpposite != trOpposite);
156 return COMPARE_RESULT(9, trOpposite);
157 } else if (lrOrder == 1 && trOrder == 1) {
158 SkASSERT(ltOrder < 0);
159 SkDEBUGCODE(bool trOpposite = oppositePlanes(rh));
caryclark54359292015-03-26 07:52:43 -0700160 bool lrOpposite = lh->oppositePlanes(rh);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000161 SkASSERT(lrOpposite != trOpposite);
162 return COMPARE_RESULT(10, lrOpposite);
163 }
164 if (lrOrder < 0) {
165 if (ltOrder < 0) {
166 return COMPARE_RESULT(11, trOrder);
167 }
168 return COMPARE_RESULT(12, ltOrder);
169 }
170 return COMPARE_RESULT(13, !lrOrder);
171}
172
173// given a line, see if the opposite curve's convex hull is all on one side
174// returns -1=not on one side 0=this CW of test 1=this CCW of test
caryclark54359292015-03-26 07:52:43 -0700175int SkOpAngle::allOnOneSide(const SkOpAngle* test) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000176 SkASSERT(!fIsCurve);
caryclark54359292015-03-26 07:52:43 -0700177 SkASSERT(test->fIsCurve);
178 const SkDPoint& origin = test->fCurvePart[0];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000179 SkVector line;
caryclark54359292015-03-26 07:52:43 -0700180 if (segment()->verb() == SkPath::kLine_Verb) {
181 const SkPoint* linePts = segment()->pts();
182 int lineStart = fStart->t() < fEnd->t() ? 0 : 1;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000183 line = linePts[lineStart ^ 1] - linePts[lineStart];
184 } else {
caryclarkfc95be12015-07-07 18:09:32 -0700185 line = (fCurvePart[1] - fCurvePart[0]).asSkVector();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000186 }
187 float crosses[3];
caryclark54359292015-03-26 07:52:43 -0700188 SkPath::Verb testVerb = test->segment()->verb();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000189 int iMax = SkPathOpsVerbToPoints(testVerb);
190// SkASSERT(origin == test.fCurveHalf[0]);
caryclark1049f122015-04-20 08:31:59 -0700191 const SkDCurve& testCurve = test->fCurvePart;
caryclark54359292015-03-26 07:52:43 -0700192 for (int index = 1; index <= iMax; ++index) {
193 float xy1 = (float) (line.fX * (testCurve[index].fY - origin.fY));
194 float xy2 = (float) (line.fY * (testCurve[index].fX - origin.fX));
195 crosses[index - 1] = AlmostEqualUlps(xy1, xy2) ? 0 : xy1 - xy2;
196 }
197 if (crosses[0] * crosses[1] < 0) {
198 return -1;
199 }
200 if (SkPath::kCubic_Verb == testVerb) {
201 if (crosses[0] * crosses[2] < 0 || crosses[1] * crosses[2] < 0) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000202 return -1;
203 }
caryclark54359292015-03-26 07:52:43 -0700204 }
205 if (crosses[0]) {
206 return crosses[0] < 0;
207 }
208 if (crosses[1]) {
209 return crosses[1] < 0;
210 }
211 if (SkPath::kCubic_Verb == testVerb && crosses[2]) {
212 return crosses[2] < 0;
213 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000214 fUnorderable = true;
215 return -1;
216}
217
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000218bool SkOpAngle::checkCrossesZero() const {
219 int start = SkTMin(fSectorStart, fSectorEnd);
220 int end = SkTMax(fSectorStart, fSectorEnd);
221 bool crossesZero = end - start > 16;
222 return crossesZero;
223}
caryclark@google.com07393ca2013-04-08 11:47:37 +0000224
caryclark54359292015-03-26 07:52:43 -0700225// loop looking for a pair of angle parts that are too close to be sorted
226/* This is called after other more simple intersection and angle sorting tests have been exhausted.
227 This should be rarely called -- the test below is thorough and time consuming.
228 This checks the distance between start points; the distance between
229*/
230void SkOpAngle::checkNearCoincidence() {
231 SkOpAngle* test = this;
232 do {
233 SkOpSegment* testSegment = test->segment();
234 double testStartT = test->start()->t();
235 SkDPoint testStartPt = testSegment->dPtAtT(testStartT);
236 double testEndT = test->end()->t();
237 SkDPoint testEndPt = testSegment->dPtAtT(testEndT);
238 double testLenSq = testStartPt.distanceSquared(testEndPt);
239 if (0) {
240 SkDebugf("%s testLenSq=%1.9g id=%d\n", __FUNCTION__, testLenSq, testSegment->debugID());
241 }
242 double testMidT = (testStartT + testEndT) / 2;
243 SkOpAngle* next = test;
244 while ((next = next->fNext) != this) {
245 SkOpSegment* nextSegment = next->segment();
246 double testMidDistSq = testSegment->distSq(testMidT, next);
247 double testEndDistSq = testSegment->distSq(testEndT, next);
248 double nextStartT = next->start()->t();
249 SkDPoint nextStartPt = nextSegment->dPtAtT(nextStartT);
250 double distSq = testStartPt.distanceSquared(nextStartPt);
251 double nextEndT = next->end()->t();
252 double nextMidT = (nextStartT + nextEndT) / 2;
253 double nextMidDistSq = nextSegment->distSq(nextMidT, test);
254 double nextEndDistSq = nextSegment->distSq(nextEndT, test);
255 if (0) {
256 SkDebugf("%s distSq=%1.9g testId=%d nextId=%d\n", __FUNCTION__, distSq,
257 testSegment->debugID(), nextSegment->debugID());
258 SkDebugf("%s testMidDistSq=%1.9g\n", __FUNCTION__, testMidDistSq);
259 SkDebugf("%s testEndDistSq=%1.9g\n", __FUNCTION__, testEndDistSq);
260 SkDebugf("%s nextMidDistSq=%1.9g\n", __FUNCTION__, nextMidDistSq);
261 SkDebugf("%s nextEndDistSq=%1.9g\n", __FUNCTION__, nextEndDistSq);
262 SkDPoint nextEndPt = nextSegment->dPtAtT(nextEndT);
263 double nextLenSq = nextStartPt.distanceSquared(nextEndPt);
264 SkDebugf("%s nextLenSq=%1.9g\n", __FUNCTION__, nextLenSq);
265 SkDebugf("\n");
266 }
267 }
268 test = test->fNext;
269 } while (test->fNext != this);
270}
271
272bool SkOpAngle::checkParallel(SkOpAngle* rh) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000273 SkDVector scratch[2];
274 const SkDVector* sweep, * tweep;
caryclark54359292015-03-26 07:52:43 -0700275 if (!this->fUnorderedSweep) {
276 sweep = this->fSweep;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000277 } else {
caryclark54359292015-03-26 07:52:43 -0700278 scratch[0] = this->fCurvePart[1] - this->fCurvePart[0];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000279 sweep = &scratch[0];
caryclark@google.coma5e55922013-05-07 18:51:31 +0000280 }
caryclark54359292015-03-26 07:52:43 -0700281 if (!rh->fUnorderedSweep) {
282 tweep = rh->fSweep;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000283 } else {
caryclark54359292015-03-26 07:52:43 -0700284 scratch[1] = rh->fCurvePart[1] - rh->fCurvePart[0];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000285 tweep = &scratch[1];
caryclark@google.com07393ca2013-04-08 11:47:37 +0000286 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000287 double s0xt0 = sweep->crossCheck(*tweep);
288 if (tangentsDiverge(rh, s0xt0)) {
289 return s0xt0 < 0;
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +0000290 }
caryclark54359292015-03-26 07:52:43 -0700291 // compute the perpendicular to the endpoints and see where it intersects the opposite curve
292 // if the intersections within the t range, do a cross check on those
293 bool inside;
caryclark1049f122015-04-20 08:31:59 -0700294 if (!fCurvePart[SkPathOpsVerbToPoints(this->segment()->verb())].approximatelyEqual(
295 rh->fCurvePart[SkPathOpsVerbToPoints(rh->segment()->verb())])) {
296 if (this->endToSide(rh, &inside)) {
297 return inside;
298 }
299 if (rh->endToSide(this, &inside)) {
300 return !inside;
301 }
caryclark54359292015-03-26 07:52:43 -0700302 }
303 if (this->midToSide(rh, &inside)) {
304 return inside;
305 }
306 if (rh->midToSide(this, &inside)) {
307 return !inside;
308 }
309 // compute the cross check from the mid T values (last resort)
310 SkDVector m0 = segment()->dPtAtT(this->midT()) - this->fCurvePart[0];
311 SkDVector m1 = rh->segment()->dPtAtT(rh->midT()) - rh->fCurvePart[0];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000312 double m0xm1 = m0.crossCheck(m1);
313 if (m0xm1 == 0) {
caryclark54359292015-03-26 07:52:43 -0700314 this->fUnorderable = true;
315 rh->fUnorderable = true;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000316 return true;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000317 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000318 return m0xm1 < 0;
319}
320
321// the original angle is too short to get meaningful sector information
322// lengthen it until it is long enough to be meaningful or leave it unset if lengthening it
323// would cause it to intersect one of the adjacent angles
324bool SkOpAngle::computeSector() {
325 if (fComputedSector) {
caryclarkdac1d172014-06-17 05:15:38 -0700326 return !fUnorderable;
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000327 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000328 fComputedSector = true;
caryclark54359292015-03-26 07:52:43 -0700329 bool stepUp = fStart->t() < fEnd->t();
330 const SkOpSpanBase* checkEnd = fEnd;
331 if (checkEnd->final() && stepUp) {
caryclarkccec0f92015-03-24 07:28:17 -0700332 fUnorderable = true;
333 return false;
334 }
caryclark54359292015-03-26 07:52:43 -0700335 do {
336// advance end
337 const SkOpSegment* other = checkEnd->segment();
338 const SkOpSpanBase* oSpan = other->head();
339 do {
340 if (oSpan->segment() != segment()) {
341 continue;
342 }
343 if (oSpan == checkEnd) {
344 continue;
345 }
346 if (!approximately_equal(oSpan->t(), checkEnd->t())) {
347 continue;
348 }
349 goto recomputeSector;
350 } while (!oSpan->final() && (oSpan = oSpan->upCast()->next()));
351 checkEnd = stepUp ? !checkEnd->final()
352 ? checkEnd->upCast()->next() : NULL
353 : checkEnd->prev();
354 } while (checkEnd);
355recomputeSector:
356 SkOpSpanBase* computedEnd = stepUp ? checkEnd ? checkEnd->prev() : fEnd->segment()->head()
357 : checkEnd ? checkEnd->upCast()->next() : fEnd->segment()->tail();
358 if (checkEnd == fEnd || computedEnd == fEnd || computedEnd == fStart) {
359 fUnorderable = true;
360 return false;
361 }
caryclark624637c2015-05-11 07:21:27 -0700362 if (stepUp != (fStart->t() < computedEnd->t())) {
363 fUnorderable = true;
364 return false;
365 }
caryclark54359292015-03-26 07:52:43 -0700366 SkOpSpanBase* saveEnd = fEnd;
367 fComputedEnd = fEnd = computedEnd;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000368 setSpans();
369 setSector();
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000370 fEnd = saveEnd;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000371 return !fUnorderable;
372}
373
caryclark54359292015-03-26 07:52:43 -0700374int SkOpAngle::convexHullOverlaps(const SkOpAngle* rh) const {
375 const SkDVector* sweep = this->fSweep;
376 const SkDVector* tweep = rh->fSweep;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000377 double s0xs1 = sweep[0].crossCheck(sweep[1]);
378 double s0xt0 = sweep[0].crossCheck(tweep[0]);
379 double s1xt0 = sweep[1].crossCheck(tweep[0]);
380 bool tBetweenS = s0xs1 > 0 ? s0xt0 > 0 && s1xt0 < 0 : s0xt0 < 0 && s1xt0 > 0;
381 double s0xt1 = sweep[0].crossCheck(tweep[1]);
382 double s1xt1 = sweep[1].crossCheck(tweep[1]);
383 tBetweenS |= s0xs1 > 0 ? s0xt1 > 0 && s1xt1 < 0 : s0xt1 < 0 && s1xt1 > 0;
384 double t0xt1 = tweep[0].crossCheck(tweep[1]);
385 if (tBetweenS) {
386 return -1;
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000387 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000388 if ((s0xt0 == 0 && s1xt1 == 0) || (s1xt0 == 0 && s0xt1 == 0)) { // s0 to s1 equals t0 to t1
389 return -1;
390 }
391 bool sBetweenT = t0xt1 > 0 ? s0xt0 < 0 && s0xt1 > 0 : s0xt0 > 0 && s0xt1 < 0;
392 sBetweenT |= t0xt1 > 0 ? s1xt0 < 0 && s1xt1 > 0 : s1xt0 > 0 && s1xt1 < 0;
393 if (sBetweenT) {
394 return -1;
395 }
396 // if all of the sweeps are in the same half plane, then the order of any pair is enough
397 if (s0xt0 >= 0 && s0xt1 >= 0 && s1xt0 >= 0 && s1xt1 >= 0) {
398 return 0;
399 }
400 if (s0xt0 <= 0 && s0xt1 <= 0 && s1xt0 <= 0 && s1xt1 <= 0) {
401 return 1;
402 }
403 // if the outside sweeps are greater than 180 degress:
404 // first assume the inital tangents are the ordering
405 // if the midpoint direction matches the inital order, that is enough
caryclark54359292015-03-26 07:52:43 -0700406 SkDVector m0 = this->segment()->dPtAtT(this->midT()) - this->fCurvePart[0];
407 SkDVector m1 = rh->segment()->dPtAtT(rh->midT()) - rh->fCurvePart[0];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000408 double m0xm1 = m0.crossCheck(m1);
409 if (s0xt0 > 0 && m0xm1 > 0) {
410 return 0;
411 }
412 if (s0xt0 < 0 && m0xm1 < 0) {
413 return 1;
414 }
415 if (tangentsDiverge(rh, s0xt0)) {
416 return s0xt0 < 0;
417 }
418 return m0xm1 < 0;
419}
420
421// OPTIMIZATION: longest can all be either lazily computed here or precomputed in setup
422double SkOpAngle::distEndRatio(double dist) const {
423 double longest = 0;
424 const SkOpSegment& segment = *this->segment();
425 int ptCount = SkPathOpsVerbToPoints(segment.verb());
426 const SkPoint* pts = segment.pts();
427 for (int idx1 = 0; idx1 <= ptCount - 1; ++idx1) {
428 for (int idx2 = idx1 + 1; idx2 <= ptCount; ++idx2) {
429 if (idx1 == idx2) {
430 continue;
431 }
432 SkDVector v;
433 v.set(pts[idx2] - pts[idx1]);
434 double lenSq = v.lengthSquared();
435 longest = SkTMax(longest, lenSq);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000436 }
437 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000438 return sqrt(longest) / dist;
439}
440
caryclark54359292015-03-26 07:52:43 -0700441bool SkOpAngle::endsIntersect(SkOpAngle* rh) {
442 SkPath::Verb lVerb = this->segment()->verb();
443 SkPath::Verb rVerb = rh->segment()->verb();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000444 int lPts = SkPathOpsVerbToPoints(lVerb);
445 int rPts = SkPathOpsVerbToPoints(rVerb);
caryclark54359292015-03-26 07:52:43 -0700446 SkDLine rays[] = {{{this->fCurvePart[0], rh->fCurvePart[rPts]}},
447 {{this->fCurvePart[0], this->fCurvePart[lPts]}}};
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000448 if (rays[0][1] == rays[1][1]) {
449 return checkParallel(rh);
450 }
451 double smallTs[2] = {-1, -1};
452 bool limited[2] = {false, false};
453 for (int index = 0; index < 2; ++index) {
caryclark1049f122015-04-20 08:31:59 -0700454 SkPath::Verb cVerb = index ? rVerb : lVerb;
caryclark65f55312014-11-13 06:58:52 -0800455 // if the curve is a line, then the line and the ray intersect only at their crossing
caryclark1049f122015-04-20 08:31:59 -0700456 if (cVerb == SkPath::kLine_Verb) {
caryclark65f55312014-11-13 06:58:52 -0800457 continue;
458 }
caryclark54359292015-03-26 07:52:43 -0700459 const SkOpSegment& segment = index ? *rh->segment() : *this->segment();
460 SkIntersections i;
caryclark1049f122015-04-20 08:31:59 -0700461 (*CurveIntersectRay[cVerb])(segment.pts(), segment.weight(), rays[index], &i);
caryclark54359292015-03-26 07:52:43 -0700462 double tStart = index ? rh->fStart->t() : this->fStart->t();
463 double tEnd = index ? rh->fComputedEnd->t() : this->fComputedEnd->t();
464 bool testAscends = tStart < (index ? rh->fComputedEnd->t() : this->fComputedEnd->t());
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000465 double t = testAscends ? 0 : 1;
466 for (int idx2 = 0; idx2 < i.used(); ++idx2) {
467 double testT = i[0][idx2];
468 if (!approximately_between_orderable(tStart, testT, tEnd)) {
469 continue;
470 }
471 if (approximately_equal_orderable(tStart, testT)) {
472 continue;
473 }
474 smallTs[index] = t = testAscends ? SkTMax(t, testT) : SkTMin(t, testT);
475 limited[index] = approximately_equal_orderable(t, tEnd);
476 }
477 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000478 bool sRayLonger = false;
479 SkDVector sCept = {0, 0};
480 double sCeptT = -1;
481 int sIndex = -1;
482 bool useIntersect = false;
483 for (int index = 0; index < 2; ++index) {
484 if (smallTs[index] < 0) {
485 continue;
486 }
caryclark54359292015-03-26 07:52:43 -0700487 const SkOpSegment& segment = index ? *rh->segment() : *this->segment();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000488 const SkDPoint& dPt = segment.dPtAtT(smallTs[index]);
489 SkDVector cept = dPt - rays[index][0];
490 // If this point is on the curve, it should have been detected earlier by ordinary
491 // curve intersection. This may be hard to determine in general, but for lines,
492 // the point could be close to or equal to its end, but shouldn't be near the start.
493 if ((index ? lPts : rPts) == 1) {
494 SkDVector total = rays[index][1] - rays[index][0];
495 if (cept.lengthSquared() * 2 < total.lengthSquared()) {
496 continue;
497 }
498 }
499 SkDVector end = rays[index][1] - rays[index][0];
500 if (cept.fX * end.fX < 0 || cept.fY * end.fY < 0) {
501 continue;
502 }
503 double rayDist = cept.length();
504 double endDist = end.length();
505 bool rayLonger = rayDist > endDist;
506 if (limited[0] && limited[1] && rayLonger) {
507 useIntersect = true;
508 sRayLonger = rayLonger;
509 sCept = cept;
510 sCeptT = smallTs[index];
511 sIndex = index;
caryclark@google.coma5e55922013-05-07 18:51:31 +0000512 break;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000513 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000514 double delta = fabs(rayDist - endDist);
515 double minX, minY, maxX, maxY;
516 minX = minY = SK_ScalarInfinity;
517 maxX = maxY = -SK_ScalarInfinity;
caryclark1049f122015-04-20 08:31:59 -0700518 const SkDCurve& curve = index ? rh->fCurvePart : this->fCurvePart;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000519 int ptCount = index ? rPts : lPts;
520 for (int idx2 = 0; idx2 <= ptCount; ++idx2) {
521 minX = SkTMin(minX, curve[idx2].fX);
522 minY = SkTMin(minY, curve[idx2].fY);
523 maxX = SkTMax(maxX, curve[idx2].fX);
524 maxY = SkTMax(maxY, curve[idx2].fY);
525 }
526 double maxWidth = SkTMax(maxX - minX, maxY - minY);
527 delta /= maxWidth;
caryclark54359292015-03-26 07:52:43 -0700528 if (delta > 1e-3 && (useIntersect ^= true)) { // FIXME: move this magic number
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000529 sRayLonger = rayLonger;
530 sCept = cept;
531 sCeptT = smallTs[index];
532 sIndex = index;
533 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000534 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000535 if (useIntersect) {
caryclark1049f122015-04-20 08:31:59 -0700536 const SkDCurve& curve = sIndex ? rh->fCurvePart : this->fCurvePart;
caryclark54359292015-03-26 07:52:43 -0700537 const SkOpSegment& segment = sIndex ? *rh->segment() : *this->segment();
538 double tStart = sIndex ? rh->fStart->t() : fStart->t();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000539 SkDVector mid = segment.dPtAtT(tStart + (sCeptT - tStart) / 2) - curve[0];
540 double septDir = mid.crossCheck(sCept);
541 if (!septDir) {
542 return checkParallel(rh);
543 }
544 return sRayLonger ^ (sIndex == 0) ^ (septDir < 0);
545 } else {
546 return checkParallel(rh);
caryclark@google.coma5e55922013-05-07 18:51:31 +0000547 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000548}
549
caryclark54359292015-03-26 07:52:43 -0700550bool SkOpAngle::endToSide(const SkOpAngle* rh, bool* inside) const {
551 const SkOpSegment* segment = this->segment();
552 SkPath::Verb verb = segment->verb();
caryclark54359292015-03-26 07:52:43 -0700553 SkDLine rayEnd;
554 rayEnd[0].set(this->fEnd->pt());
555 rayEnd[1] = rayEnd[0];
caryclark1049f122015-04-20 08:31:59 -0700556 SkDVector slopeAtEnd = (*CurveDSlopeAtT[verb])(segment->pts(), segment->weight(),
557 this->fEnd->t());
caryclark54359292015-03-26 07:52:43 -0700558 rayEnd[1].fX += slopeAtEnd.fY;
559 rayEnd[1].fY -= slopeAtEnd.fX;
560 SkIntersections iEnd;
561 const SkOpSegment* oppSegment = rh->segment();
562 SkPath::Verb oppVerb = oppSegment->verb();
caryclark1049f122015-04-20 08:31:59 -0700563 (*CurveIntersectRay[oppVerb])(oppSegment->pts(), oppSegment->weight(), rayEnd, &iEnd);
caryclark54359292015-03-26 07:52:43 -0700564 double endDist;
565 int closestEnd = iEnd.closestTo(rh->fStart->t(), rh->fEnd->t(), rayEnd[0], &endDist);
566 if (closestEnd < 0) {
567 return false;
568 }
569 if (!endDist) {
570 return false;
571 }
572 SkDPoint start;
573 start.set(this->fStart->pt());
574 // OPTIMIZATION: multiple times in the code we find the max scalar
575 double minX, minY, maxX, maxY;
576 minX = minY = SK_ScalarInfinity;
577 maxX = maxY = -SK_ScalarInfinity;
caryclark1049f122015-04-20 08:31:59 -0700578 const SkDCurve& curve = rh->fCurvePart;
579 int oppPts = SkPathOpsVerbToPoints(oppVerb);
caryclark54359292015-03-26 07:52:43 -0700580 for (int idx2 = 0; idx2 <= oppPts; ++idx2) {
581 minX = SkTMin(minX, curve[idx2].fX);
582 minY = SkTMin(minY, curve[idx2].fY);
583 maxX = SkTMax(maxX, curve[idx2].fX);
584 maxY = SkTMax(maxY, curve[idx2].fY);
585 }
586 double maxWidth = SkTMax(maxX - minX, maxY - minY);
587 endDist /= maxWidth;
588 if (endDist < 5e-11) { // empirically found
589 return false;
590 }
591 const SkDPoint* endPt = &rayEnd[0];
592 SkDPoint oppPt = iEnd.pt(closestEnd);
593 SkDVector vLeft = *endPt - start;
594 SkDVector vRight = oppPt - start;
595 double dir = vLeft.crossCheck(vRight);
596 if (!dir) {
597 return false;
598 }
599 *inside = dir < 0;
600 return true;
601}
602
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000603/* y<0 y==0 y>0 x<0 x==0 x>0 xy<0 xy==0 xy>0
604 0 x x x
605 1 x x x
606 2 x x x
607 3 x x x
608 4 x x x
609 5 x x x
610 6 x x x
611 7 x x x
612 8 x x x
613 9 x x x
614 10 x x x
615 11 x x x
616 12 x x x
617 13 x x x
618 14 x x x
619 15 x x x
620*/
621int SkOpAngle::findSector(SkPath::Verb verb, double x, double y) const {
622 double absX = fabs(x);
623 double absY = fabs(y);
624 double xy = SkPath::kLine_Verb == verb || !AlmostEqualUlps(absX, absY) ? absX - absY : 0;
625 // If there are four quadrants and eight octants, and since the Latin for sixteen is sedecim,
626 // one could coin the term sedecimant for a space divided into 16 sections.
627 // http://english.stackexchange.com/questions/133688/word-for-something-partitioned-into-16-parts
628 static const int sedecimant[3][3][3] = {
629 // y<0 y==0 y>0
630 // x<0 x==0 x>0 x<0 x==0 x>0 x<0 x==0 x>0
631 {{ 4, 3, 2}, { 7, -1, 15}, {10, 11, 12}}, // abs(x) < abs(y)
632 {{ 5, -1, 1}, {-1, -1, -1}, { 9, -1, 13}}, // abs(x) == abs(y)
633 {{ 6, 3, 0}, { 7, -1, 15}, { 8, 11, 14}}, // abs(x) > abs(y)
634 };
635 int sector = sedecimant[(xy >= 0) + (xy > 0)][(y >= 0) + (y > 0)][(x >= 0) + (x > 0)] * 2 + 1;
caryclark65b427c2014-09-18 10:32:57 -0700636// SkASSERT(SkPath::kLine_Verb == verb || sector >= 0);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000637 return sector;
638}
639
caryclark54359292015-03-26 07:52:43 -0700640SkOpGlobalState* SkOpAngle::globalState() const {
641 return this->segment()->globalState();
642}
643
644
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000645// OPTIMIZE: if this loops to only one other angle, after first compare fails, insert on other side
646// OPTIMIZE: return where insertion succeeded. Then, start next insertion on opposite side
647void SkOpAngle::insert(SkOpAngle* angle) {
648 if (angle->fNext) {
649 if (loopCount() >= angle->loopCount()) {
650 if (!merge(angle)) {
651 return;
652 }
653 } else if (fNext) {
654 if (!angle->merge(this)) {
655 return;
656 }
657 } else {
658 angle->insert(this);
659 }
660 return;
661 }
662 bool singleton = NULL == fNext;
663 if (singleton) {
664 fNext = this;
665 }
666 SkOpAngle* next = fNext;
667 if (next->fNext == this) {
668 if (singleton || angle->after(this)) {
669 this->fNext = angle;
670 angle->fNext = next;
671 } else {
672 next->fNext = angle;
673 angle->fNext = this;
674 }
675 debugValidateNext();
676 return;
677 }
678 SkOpAngle* last = this;
679 do {
680 SkASSERT(last->fNext == next);
681 if (angle->after(last)) {
682 last->fNext = angle;
683 angle->fNext = next;
684 debugValidateNext();
685 return;
686 }
687 last = next;
688 next = next->fNext;
caryclark54359292015-03-26 07:52:43 -0700689 if (last == this) {
690 if (next->fUnorderable) {
691 fUnorderable = true;
692 } else {
693 globalState()->setAngleCoincidence();
694 this->fNext = angle;
695 angle->fNext = next;
696 angle->fCheckCoincidence = true;
697 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000698 return;
699 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000700 } while (true);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000701}
702
caryclark54359292015-03-26 07:52:43 -0700703SkOpSpanBase* SkOpAngle::lastMarked() const {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000704 if (fLastMarked) {
caryclark54359292015-03-26 07:52:43 -0700705 if (fLastMarked->chased()) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000706 return NULL;
707 }
caryclark54359292015-03-26 07:52:43 -0700708 fLastMarked->setChased(true);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000709 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000710 return fLastMarked;
711}
712
caryclark54359292015-03-26 07:52:43 -0700713bool SkOpAngle::loopContains(const SkOpAngle* angle) const {
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000714 if (!fNext) {
715 return false;
716 }
717 const SkOpAngle* first = this;
718 const SkOpAngle* loop = this;
caryclark54359292015-03-26 07:52:43 -0700719 const SkOpSegment* tSegment = angle->fStart->segment();
720 double tStart = angle->fStart->t();
721 double tEnd = angle->fEnd->t();
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000722 do {
caryclark54359292015-03-26 07:52:43 -0700723 const SkOpSegment* lSegment = loop->fStart->segment();
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000724 if (lSegment != tSegment) {
725 continue;
726 }
caryclark54359292015-03-26 07:52:43 -0700727 double lStart = loop->fStart->t();
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000728 if (lStart != tEnd) {
729 continue;
730 }
caryclark54359292015-03-26 07:52:43 -0700731 double lEnd = loop->fEnd->t();
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000732 if (lEnd == tStart) {
733 return true;
734 }
735 } while ((loop = loop->fNext) != first);
736 return false;
737}
738
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000739int SkOpAngle::loopCount() const {
740 int count = 0;
741 const SkOpAngle* first = this;
742 const SkOpAngle* next = this;
743 do {
744 next = next->fNext;
745 ++count;
746 } while (next && next != first);
747 return count;
748}
749
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000750bool SkOpAngle::merge(SkOpAngle* angle) {
751 SkASSERT(fNext);
752 SkASSERT(angle->fNext);
753 SkOpAngle* working = angle;
754 do {
755 if (this == working) {
756 return false;
757 }
758 working = working->fNext;
759 } while (working != angle);
760 do {
761 SkOpAngle* next = working->fNext;
762 working->fNext = NULL;
763 insert(working);
764 working = next;
765 } while (working != angle);
766 // it's likely that a pair of the angles are unorderable
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000767 debugValidateNext();
768 return true;
769}
770
771double SkOpAngle::midT() const {
caryclark54359292015-03-26 07:52:43 -0700772 return (fStart->t() + fEnd->t()) / 2;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000773}
774
caryclark54359292015-03-26 07:52:43 -0700775bool SkOpAngle::midToSide(const SkOpAngle* rh, bool* inside) const {
776 const SkOpSegment* segment = this->segment();
777 SkPath::Verb verb = segment->verb();
caryclark54359292015-03-26 07:52:43 -0700778 const SkPoint& startPt = this->fStart->pt();
779 const SkPoint& endPt = this->fEnd->pt();
780 SkDPoint dStartPt;
781 dStartPt.set(startPt);
782 SkDLine rayMid;
783 rayMid[0].fX = (startPt.fX + endPt.fX) / 2;
784 rayMid[0].fY = (startPt.fY + endPt.fY) / 2;
785 rayMid[1].fX = rayMid[0].fX + (endPt.fY - startPt.fY);
786 rayMid[1].fY = rayMid[0].fY - (endPt.fX - startPt.fX);
787 SkIntersections iMid;
caryclark1049f122015-04-20 08:31:59 -0700788 (*CurveIntersectRay[verb])(segment->pts(), segment->weight(), rayMid, &iMid);
caryclark54359292015-03-26 07:52:43 -0700789 int iOutside = iMid.mostOutside(this->fStart->t(), this->fEnd->t(), dStartPt);
790 if (iOutside < 0) {
791 return false;
792 }
793 const SkOpSegment* oppSegment = rh->segment();
794 SkPath::Verb oppVerb = oppSegment->verb();
caryclark54359292015-03-26 07:52:43 -0700795 SkIntersections oppMid;
caryclark1049f122015-04-20 08:31:59 -0700796 (*CurveIntersectRay[oppVerb])(oppSegment->pts(), oppSegment->weight(), rayMid, &oppMid);
caryclark54359292015-03-26 07:52:43 -0700797 int oppOutside = oppMid.mostOutside(rh->fStart->t(), rh->fEnd->t(), dStartPt);
798 if (oppOutside < 0) {
799 return false;
800 }
801 SkDVector iSide = iMid.pt(iOutside) - dStartPt;
802 SkDVector oppSide = oppMid.pt(oppOutside) - dStartPt;
803 double dir = iSide.crossCheck(oppSide);
804 if (!dir) {
805 return false;
806 }
807 *inside = dir < 0;
808 return true;
809}
810
811bool SkOpAngle::oppositePlanes(const SkOpAngle* rh) const {
812 int startSpan = abs(rh->fSectorStart - fSectorStart);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000813 return startSpan >= 8;
814}
815
caryclark54359292015-03-26 07:52:43 -0700816bool SkOpAngle::orderable(SkOpAngle* rh) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000817 int result;
818 if (!fIsCurve) {
caryclark54359292015-03-26 07:52:43 -0700819 if (!rh->fIsCurve) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000820 double leftX = fTangentHalf.dx();
821 double leftY = fTangentHalf.dy();
caryclark54359292015-03-26 07:52:43 -0700822 double rightX = rh->fTangentHalf.dx();
823 double rightY = rh->fTangentHalf.dy();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000824 double x_ry = leftX * rightY;
825 double rx_y = rightX * leftY;
826 if (x_ry == rx_y) {
827 if (leftX * rightX < 0 || leftY * rightY < 0) {
828 return true; // exactly 180 degrees apart
829 }
830 goto unorderable;
831 }
832 SkASSERT(x_ry != rx_y); // indicates an undetected coincidence -- worth finding earlier
833 return x_ry < rx_y;
834 }
835 if ((result = allOnOneSide(rh)) >= 0) {
836 return result;
837 }
caryclark54359292015-03-26 07:52:43 -0700838 if (fUnorderable || approximately_zero(rh->fSide)) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000839 goto unorderable;
840 }
caryclark54359292015-03-26 07:52:43 -0700841 } else if (!rh->fIsCurve) {
842 if ((result = rh->allOnOneSide(this)) >= 0) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000843 return !result;
844 }
caryclark54359292015-03-26 07:52:43 -0700845 if (rh->fUnorderable || approximately_zero(fSide)) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000846 goto unorderable;
847 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000848 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000849 if ((result = convexHullOverlaps(rh)) >= 0) {
850 return result;
851 }
852 return endsIntersect(rh);
853unorderable:
854 fUnorderable = true;
caryclark54359292015-03-26 07:52:43 -0700855 rh->fUnorderable = true;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000856 return true;
857}
858
859// OPTIMIZE: if this shows up in a profile, add a previous pointer
860// as is, this should be rarely called
861SkOpAngle* SkOpAngle::previous() const {
862 SkOpAngle* last = fNext;
863 do {
864 SkOpAngle* next = last->fNext;
865 if (next == this) {
866 return last;
867 }
868 last = next;
869 } while (true);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000870}
871
caryclark54359292015-03-26 07:52:43 -0700872SkOpSegment* SkOpAngle::segment() const {
873 return fStart->segment();
874}
875
876void SkOpAngle::set(SkOpSpanBase* start, SkOpSpanBase* end) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000877 fStart = start;
caryclarkdac1d172014-06-17 05:15:38 -0700878 fComputedEnd = fEnd = end;
caryclark54359292015-03-26 07:52:43 -0700879 SkASSERT(start != end);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000880 fNext = NULL;
caryclark54359292015-03-26 07:52:43 -0700881 fComputeSector = fComputedSector = fCheckCoincidence = false;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000882 setSpans();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000883 setSector();
caryclark1049f122015-04-20 08:31:59 -0700884 SkDEBUGCODE(fID = start ? start->globalState()->nextAngleID() : -1);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000885}
886
887void SkOpAngle::setCurveHullSweep() {
888 fUnorderedSweep = false;
889 fSweep[0] = fCurvePart[1] - fCurvePart[0];
caryclark54359292015-03-26 07:52:43 -0700890 const SkOpSegment* segment = fStart->segment();
891 if (SkPath::kLine_Verb == segment->verb()) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000892 fSweep[1] = fSweep[0];
893 return;
894 }
895 fSweep[1] = fCurvePart[2] - fCurvePart[0];
caryclark54359292015-03-26 07:52:43 -0700896 if (SkPath::kCubic_Verb != segment->verb()) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000897 if (!fSweep[0].fX && !fSweep[0].fY) {
898 fSweep[0] = fSweep[1];
899 }
900 return;
901 }
902 SkDVector thirdSweep = fCurvePart[3] - fCurvePart[0];
903 if (fSweep[0].fX == 0 && fSweep[0].fY == 0) {
904 fSweep[0] = fSweep[1];
905 fSweep[1] = thirdSweep;
906 if (fSweep[0].fX == 0 && fSweep[0].fY == 0) {
907 fSweep[0] = fSweep[1];
908 fCurvePart[1] = fCurvePart[3];
909 fIsCurve = false;
910 }
911 return;
912 }
913 double s1x3 = fSweep[0].crossCheck(thirdSweep);
914 double s3x2 = thirdSweep.crossCheck(fSweep[1]);
915 if (s1x3 * s3x2 >= 0) { // if third vector is on or between first two vectors
916 return;
917 }
918 double s2x1 = fSweep[1].crossCheck(fSweep[0]);
919 // FIXME: If the sweep of the cubic is greater than 180 degrees, we're in trouble
920 // probably such wide sweeps should be artificially subdivided earlier so that never happens
921 SkASSERT(s1x3 * s2x1 < 0 || s1x3 * s3x2 < 0);
922 if (s3x2 * s2x1 < 0) {
923 SkASSERT(s2x1 * s1x3 > 0);
924 fSweep[0] = fSweep[1];
925 fUnorderedSweep = true;
926 }
927 fSweep[1] = thirdSweep;
928}
929
caryclark@google.com07393ca2013-04-08 11:47:37 +0000930void SkOpAngle::setSpans() {
caryclark54359292015-03-26 07:52:43 -0700931 fUnorderable = false;
caryclark@google.com570863f2013-09-16 15:55:01 +0000932 fLastMarked = NULL;
caryclark1049f122015-04-20 08:31:59 -0700933 if (!fStart) {
934 fUnorderable = true;
935 return;
936 }
caryclark54359292015-03-26 07:52:43 -0700937 const SkOpSegment* segment = fStart->segment();
938 const SkPoint* pts = segment->pts();
caryclark1049f122015-04-20 08:31:59 -0700939 SkDEBUGCODE(fCurvePart.fVerb = SkPath::kCubic_Verb);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000940 SkDEBUGCODE(fCurvePart[2].fX = fCurvePart[2].fY = fCurvePart[3].fX = fCurvePart[3].fY
941 = SK_ScalarNaN);
caryclark1049f122015-04-20 08:31:59 -0700942 SkDEBUGCODE(fCurvePart.fVerb = segment->verb());
caryclark54359292015-03-26 07:52:43 -0700943 segment->subDivide(fStart, fEnd, &fCurvePart);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000944 setCurveHullSweep();
caryclark54359292015-03-26 07:52:43 -0700945 const SkPath::Verb verb = segment->verb();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000946 if (verb != SkPath::kLine_Verb
947 && !(fIsCurve = fSweep[0].crossCheck(fSweep[1]) != 0)) {
948 SkDLine lineHalf;
949 lineHalf[0].set(fCurvePart[0].asSkPoint());
950 lineHalf[1].set(fCurvePart[SkPathOpsVerbToPoints(verb)].asSkPoint());
951 fTangentHalf.lineEndPoints(lineHalf);
952 fSide = 0;
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000953 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000954 switch (verb) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000955 case SkPath::kLine_Verb: {
caryclark@google.com570863f2013-09-16 15:55:01 +0000956 SkASSERT(fStart != fEnd);
caryclark54359292015-03-26 07:52:43 -0700957 const SkPoint& cP1 = pts[fStart->t() < fEnd->t()];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000958 SkDLine lineHalf;
caryclark54359292015-03-26 07:52:43 -0700959 lineHalf[0].set(fStart->pt());
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000960 lineHalf[1].set(cP1);
961 fTangentHalf.lineEndPoints(lineHalf);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000962 fSide = 0;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000963 fIsCurve = false;
964 } return;
caryclark1049f122015-04-20 08:31:59 -0700965 case SkPath::kQuad_Verb:
966 case SkPath::kConic_Verb: {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000967 SkLineParameters tangentPart;
caryclark1049f122015-04-20 08:31:59 -0700968 (void) tangentPart.quadEndPoints(fCurvePart.fQuad);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000969 fSide = -tangentPart.pointDistance(fCurvePart[2]); // not normalized -- compare sign only
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000970 } break;
971 case SkPath::kCubic_Verb: {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000972 SkLineParameters tangentPart;
caryclark1049f122015-04-20 08:31:59 -0700973 (void) tangentPart.cubicPart(fCurvePart.fCubic);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000974 fSide = -tangentPart.pointDistance(fCurvePart[3]);
caryclark@google.comb3f09212013-04-17 15:49:16 +0000975 double testTs[4];
976 // OPTIMIZATION: keep inflections precomputed with cubic segment?
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000977 int testCount = SkDCubic::FindInflections(pts, testTs);
caryclark54359292015-03-26 07:52:43 -0700978 double startT = fStart->t();
979 double endT = fEnd->t();
caryclark@google.comb3f09212013-04-17 15:49:16 +0000980 double limitT = endT;
981 int index;
982 for (index = 0; index < testCount; ++index) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000983 if (!::between(startT, testTs[index], limitT)) {
caryclark@google.comb3f09212013-04-17 15:49:16 +0000984 testTs[index] = -1;
985 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000986 }
caryclark@google.comb3f09212013-04-17 15:49:16 +0000987 testTs[testCount++] = startT;
988 testTs[testCount++] = endT;
commit-bot@chromium.orgb76d3b62013-04-22 19:55:19 +0000989 SkTQSort<double>(testTs, &testTs[testCount - 1]);
caryclark@google.comb3f09212013-04-17 15:49:16 +0000990 double bestSide = 0;
991 int testCases = (testCount << 1) - 1;
992 index = 0;
993 while (testTs[index] < 0) {
994 ++index;
995 }
996 index <<= 1;
997 for (; index < testCases; ++index) {
998 int testIndex = index >> 1;
999 double testT = testTs[testIndex];
1000 if (index & 1) {
1001 testT = (testT + testTs[testIndex + 1]) / 2;
1002 }
1003 // OPTIMIZE: could avoid call for t == startT, endT
caryclark1049f122015-04-20 08:31:59 -07001004 SkDPoint pt = dcubic_xy_at_t(pts, segment->weight(), testT);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001005 SkLineParameters tangentPart;
caryclark1049f122015-04-20 08:31:59 -07001006 tangentPart.cubicEndPoints(fCurvePart.fCubic);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001007 double testSide = tangentPart.pointDistance(pt);
caryclark@google.comb3f09212013-04-17 15:49:16 +00001008 if (fabs(bestSide) < fabs(testSide)) {
1009 bestSide = testSide;
1010 }
1011 }
1012 fSide = -bestSide; // compare sign only
caryclark@google.com07393ca2013-04-08 11:47:37 +00001013 } break;
1014 default:
1015 SkASSERT(0);
1016 }
caryclark@google.com07393ca2013-04-08 11:47:37 +00001017}
caryclark@google.com570863f2013-09-16 15:55:01 +00001018
caryclark54359292015-03-26 07:52:43 -07001019void SkOpAngle::setSector() {
caryclark1049f122015-04-20 08:31:59 -07001020 if (!fStart) {
1021 fUnorderable = true;
1022 return;
1023 }
caryclark54359292015-03-26 07:52:43 -07001024 const SkOpSegment* segment = fStart->segment();
1025 SkPath::Verb verb = segment->verb();
1026 fSectorStart = this->findSector(verb, fSweep[0].fX, fSweep[0].fY);
1027 if (fSectorStart < 0) {
1028 goto deferTilLater;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001029 }
caryclark54359292015-03-26 07:52:43 -07001030 if (!fIsCurve) { // if it's a line or line-like, note that both sectors are the same
1031 SkASSERT(fSectorStart >= 0);
1032 fSectorEnd = fSectorStart;
1033 fSectorMask = 1 << fSectorStart;
1034 return;
1035 }
1036 SkASSERT(SkPath::kLine_Verb != verb);
1037 fSectorEnd = this->findSector(verb, fSweep[1].fX, fSweep[1].fY);
1038 if (fSectorEnd < 0) {
1039deferTilLater:
1040 fSectorStart = fSectorEnd = -1;
1041 fSectorMask = 0;
1042 fComputeSector = true; // can't determine sector until segment length can be found
1043 return;
1044 }
1045 if (fSectorEnd == fSectorStart
1046 && (fSectorStart & 3) != 3) { // if the sector has no span, it can't be an exact angle
1047 fSectorMask = 1 << fSectorStart;
1048 return;
1049 }
1050 bool crossesZero = this->checkCrossesZero();
1051 int start = SkTMin(fSectorStart, fSectorEnd);
1052 bool curveBendsCCW = (fSectorStart == start) ^ crossesZero;
1053 // bump the start and end of the sector span if they are on exact compass points
1054 if ((fSectorStart & 3) == 3) {
1055 fSectorStart = (fSectorStart + (curveBendsCCW ? 1 : 31)) & 0x1f;
1056 }
1057 if ((fSectorEnd & 3) == 3) {
1058 fSectorEnd = (fSectorEnd + (curveBendsCCW ? 31 : 1)) & 0x1f;
1059 }
1060 crossesZero = this->checkCrossesZero();
1061 start = SkTMin(fSectorStart, fSectorEnd);
1062 int end = SkTMax(fSectorStart, fSectorEnd);
1063 if (!crossesZero) {
1064 fSectorMask = (unsigned) -1 >> (31 - end + start) << start;
1065 } else {
1066 fSectorMask = (unsigned) -1 >> (31 - start) | (-1 << end);
1067 }
caryclark@google.com570863f2013-09-16 15:55:01 +00001068}
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001069
caryclark54359292015-03-26 07:52:43 -07001070SkOpSpan* SkOpAngle::starter() {
1071 return fStart->starter(fEnd);
1072}
1073
1074bool SkOpAngle::tangentsDiverge(const SkOpAngle* rh, double s0xt0) const {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001075 if (s0xt0 == 0) {
1076 return false;
1077 }
1078 // if the ctrl tangents are not nearly parallel, use them
1079 // solve for opposite direction displacement scale factor == m
1080 // initial dir = v1.cross(v2) == v2.x * v1.y - v2.y * v1.x
1081 // displacement of q1[1] : dq1 = { -m * v1.y, m * v1.x } + q1[1]
1082 // straight angle when : v2.x * (dq1.y - q1[0].y) == v2.y * (dq1.x - q1[0].x)
1083 // v2.x * (m * v1.x + v1.y) == v2.y * (-m * v1.y + v1.x)
1084 // - m * (v2.x * v1.x + v2.y * v1.y) == v2.x * v1.y - v2.y * v1.x
1085 // m = (v2.y * v1.x - v2.x * v1.y) / (v2.x * v1.x + v2.y * v1.y)
1086 // m = v1.cross(v2) / v1.dot(v2)
1087 const SkDVector* sweep = fSweep;
caryclark54359292015-03-26 07:52:43 -07001088 const SkDVector* tweep = rh->fSweep;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001089 double s0dt0 = sweep[0].dot(tweep[0]);
1090 if (!s0dt0) {
1091 return true;
1092 }
1093 SkASSERT(s0dt0 != 0);
1094 double m = s0xt0 / s0dt0;
1095 double sDist = sweep[0].length() * m;
1096 double tDist = tweep[0].length() * m;
1097 bool useS = fabs(sDist) < fabs(tDist);
caryclark54359292015-03-26 07:52:43 -07001098 double mFactor = fabs(useS ? this->distEndRatio(sDist) : rh->distEndRatio(tDist));
caryclark1049f122015-04-20 08:31:59 -07001099 return mFactor < 2400; // empirically found limit
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001100}