blob: b3a188c1e82307434d848e08dabbab16c6806e03 [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 */
7#include "SkIntersections.h"
8#include "SkOpAngle.h"
caryclark@google.comcffbcc32013-06-04 17:59:42 +00009#include "SkOpSegment.h"
caryclark@google.com07393ca2013-04-08 11:47:37 +000010#include "SkPathOpsCurve.h"
commit-bot@chromium.orgb76d3b62013-04-22 19:55:19 +000011#include "SkTSort.h"
caryclark@google.com07393ca2013-04-08 11:47:37 +000012
caryclark@google.comcffbcc32013-06-04 17:59:42 +000013#if DEBUG_ANGLE
14#include "SkString.h"
caryclark@google.coma5e55922013-05-07 18:51:31 +000015#endif
16
caryclark@google.comcffbcc32013-06-04 17:59:42 +000017/* Angles are sorted counterclockwise. The smallest angle has a positive x and the smallest
18 positive y. The largest angle has a positive x and a zero y. */
caryclark@google.com07393ca2013-04-08 11:47:37 +000019
caryclark@google.comcffbcc32013-06-04 17:59:42 +000020#if DEBUG_ANGLE
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000021 static bool CompareResult(SkString* bugOut, int append, bool compare) {
22 SkDebugf("%s %c %d\n", bugOut->c_str(), compare ? 'T' : 'F', append);
caryclark@google.comcffbcc32013-06-04 17:59:42 +000023 return compare;
24 }
25
26 #define COMPARE_RESULT(append, compare) CompareResult(&bugOut, append, compare)
27#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
61bool SkOpAngle::after(const SkOpAngle* test) const {
62 const SkOpAngle& lh = *test;
63 const SkOpAngle& rh = *lh.fNext;
64 SkASSERT(&lh != &rh);
65#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__,
70 lh.fSegment->debugID(), lh.debugID(), lh.fSectorStart, lh.fSectorEnd,
71 lh.fSegment->t(lh.fStart), lh.fSegment->t(lh.fEnd),
72 fSegment->debugID(), debugID(), fSectorStart, fSectorEnd, fSegment->t(fStart),
73 fSegment->t(fEnd),
74 rh.fSegment->debugID(), rh.debugID(), rh.fSectorStart, rh.fSectorEnd,
75 rh.fSegment->t(rh.fStart), rh.fSegment->t(rh.fEnd));
76#endif
77 if (lh.fComputeSector && !const_cast<SkOpAngle&>(lh).computeSector()) {
78 return COMPARE_RESULT(1, true);
79 }
80 if (fComputeSector && !const_cast<SkOpAngle*>(this)->computeSector()) {
81 return COMPARE_RESULT(2, true);
82 }
83 if (rh.fComputeSector && !const_cast<SkOpAngle&>(rh).computeSector()) {
84 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__,
90 lh.fSegment->debugID(), lh.debugID(), lh.fSectorStart, lh.fSectorEnd,
91 lh.fSegment->t(lh.fStart), lh.fSegment->t(lh.fEnd),
92 fSegment->debugID(), debugID(), fSectorStart, fSectorEnd, fSegment->t(fStart),
93 fSegment->t(fEnd),
94 rh.fSegment->debugID(), rh.debugID(), rh.fSectorStart, rh.fSectorEnd,
95 rh.fSegment->t(rh.fStart), rh.fSegment->t(rh.fEnd));
96#endif
97 bool ltrOverlap = (lh.fSectorMask | rh.fSectorMask) & fSectorMask;
98 bool lrOverlap = lh.fSectorMask & rh.fSectorMask;
99 int lrOrder; // set to -1 if either order works
100 if (!lrOverlap) { // no lh/rh sector overlap
101 if (!ltrOverlap) { // no lh/this/rh sector overlap
102 return COMPARE_RESULT(4, (lh.fSectorEnd > rh.fSectorStart)
103 ^ (fSectorStart > lh.fSectorEnd) ^ (fSectorStart > rh.fSectorStart));
104 }
105 int lrGap = (rh.fSectorStart - lh.fSectorStart + 32) & 0x1f;
106 /* A tiny change can move the start +/- 4. The order can only be determined if
107 lr gap is not 12 to 20 or -12 to -20.
108 -31 ..-21 1
109 -20 ..-12 -1
110 -11 .. -1 0
111 0 shouldn't get here
112 11 .. 1 1
113 12 .. 20 -1
114 21 .. 31 0
115 */
116 lrOrder = lrGap > 20 ? 0 : lrGap > 11 ? -1 : 1;
117 } else {
118 lrOrder = (int) lh.orderable(rh);
119 if (!ltrOverlap) {
120 return COMPARE_RESULT(5, !lrOrder);
121 }
122 }
123 int ltOrder;
124 SkASSERT((lh.fSectorMask & fSectorMask) || (rh.fSectorMask & fSectorMask));
125 if (lh.fSectorMask & fSectorMask) {
126 ltOrder = (int) lh.orderable(*this);
127 } else {
128 int ltGap = (fSectorStart - lh.fSectorStart + 32) & 0x1f;
129 ltOrder = ltGap > 20 ? 0 : ltGap > 11 ? -1 : 1;
130 }
131 int trOrder;
132 if (rh.fSectorMask & fSectorMask) {
133 trOrder = (int) orderable(rh);
134 } else {
135 int trGap = (rh.fSectorStart - fSectorStart + 32) & 0x1f;
136 trOrder = trGap > 20 ? 0 : trGap > 11 ? -1 : 1;
137 }
138 if (lrOrder >= 0 && ltOrder >= 0 && trOrder >= 0) {
139 return COMPARE_RESULT(7, lrOrder ? (ltOrder & trOrder) : (ltOrder | trOrder));
140 }
141 SkASSERT(lrOrder >= 0 || ltOrder >= 0 || trOrder >= 0);
142// There's not enough information to sort. Get the pairs of angles in opposite planes.
143// If an order is < 0, the pair is already in an opposite plane. Check the remaining pairs.
144 // FIXME : once all variants are understood, rewrite this more simply
145 if (ltOrder == 0 && lrOrder == 0) {
146 SkASSERT(trOrder < 0);
147 // FIXME : once this is verified to work, remove one opposite angle call
148 SkDEBUGCODE(bool lrOpposite = lh.oppositePlanes(rh));
149 bool ltOpposite = lh.oppositePlanes(*this);
150 SkASSERT(lrOpposite != ltOpposite);
151 return COMPARE_RESULT(8, ltOpposite);
152 } else if (ltOrder == 1 && trOrder == 0) {
153 SkASSERT(lrOrder < 0);
154 SkDEBUGCODE(bool ltOpposite = lh.oppositePlanes(*this));
155 bool trOpposite = oppositePlanes(rh);
156 SkASSERT(ltOpposite != trOpposite);
157 return COMPARE_RESULT(9, trOpposite);
158 } else if (lrOrder == 1 && trOrder == 1) {
159 SkASSERT(ltOrder < 0);
160 SkDEBUGCODE(bool trOpposite = oppositePlanes(rh));
161 bool lrOpposite = lh.oppositePlanes(rh);
162 SkASSERT(lrOpposite != trOpposite);
163 return COMPARE_RESULT(10, lrOpposite);
164 }
165 if (lrOrder < 0) {
166 if (ltOrder < 0) {
167 return COMPARE_RESULT(11, trOrder);
168 }
169 return COMPARE_RESULT(12, ltOrder);
170 }
171 return COMPARE_RESULT(13, !lrOrder);
172}
173
174// given a line, see if the opposite curve's convex hull is all on one side
175// returns -1=not on one side 0=this CW of test 1=this CCW of test
176int SkOpAngle::allOnOneSide(const SkOpAngle& test) const {
177 SkASSERT(!fIsCurve);
178 SkASSERT(test.fIsCurve);
179 const SkDPoint& origin = test.fCurvePart[0];
180 SkVector line;
181 if (fSegment->verb() == SkPath::kLine_Verb) {
182 const SkPoint* linePts = fSegment->pts();
183 int lineStart = fStart < fEnd ? 0 : 1;
184 line = linePts[lineStart ^ 1] - linePts[lineStart];
185 } else {
186 SkPoint shortPts[2] = { fCurvePart[0].asSkPoint(), fCurvePart[1].asSkPoint() };
187 line = shortPts[1] - shortPts[0];
188 }
189 float crosses[3];
190 SkPath::Verb testVerb = test.fSegment->verb();
191 int iMax = SkPathOpsVerbToPoints(testVerb);
192// SkASSERT(origin == test.fCurveHalf[0]);
193 const SkDCubic& testCurve = test.fCurvePart;
194// do {
195 for (int index = 1; index <= iMax; ++index) {
196 float xy1 = (float) (line.fX * (testCurve[index].fY - origin.fY));
197 float xy2 = (float) (line.fY * (testCurve[index].fX - origin.fX));
198 crosses[index - 1] = AlmostEqualUlps(xy1, xy2) ? 0 : xy1 - xy2;
199 }
200 if (crosses[0] * crosses[1] < 0) {
201 return -1;
202 }
203 if (SkPath::kCubic_Verb == testVerb) {
204 if (crosses[0] * crosses[2] < 0 || crosses[1] * crosses[2] < 0) {
205 return -1;
206 }
207 }
208 if (crosses[0]) {
209 return crosses[0] < 0;
210 }
211 if (crosses[1]) {
212 return crosses[1] < 0;
213 }
214 if (SkPath::kCubic_Verb == testVerb && crosses[2]) {
215 return crosses[2] < 0;
216 }
217 fUnorderable = true;
218 return -1;
219}
220
221bool SkOpAngle::calcSlop(double x, double y, double rx, double ry, bool* result) const {
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000222 double absX = fabs(x);
223 double absY = fabs(y);
224 double length = absX < absY ? absX / 2 + absY : absX + absY / 2;
225 int exponent;
226 (void) frexp(length, &exponent);
227 double epsilon = ldexp(FLT_EPSILON, exponent);
228 SkPath::Verb verb = fSegment->verb();
229 SkASSERT(verb == SkPath::kQuad_Verb || verb == SkPath::kCubic_Verb);
230 // FIXME: the quad and cubic factors are made up ; determine actual values
231 double slop = verb == SkPath::kQuad_Verb ? 4 * epsilon : 512 * epsilon;
232 double xSlop = slop;
233 double ySlop = x * y < 0 ? -xSlop : xSlop; // OPTIMIZATION: use copysign / _copysign ?
234 double x1 = x - xSlop;
235 double y1 = y + ySlop;
236 double x_ry1 = x1 * ry;
237 double rx_y1 = rx * y1;
238 *result = x_ry1 < rx_y1;
239 double x2 = x + xSlop;
240 double y2 = y - ySlop;
241 double x_ry2 = x2 * ry;
242 double rx_y2 = rx * y2;
243 bool less2 = x_ry2 < rx_y2;
244 return *result == less2;
245}
246
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000247bool SkOpAngle::checkCrossesZero() const {
248 int start = SkTMin(fSectorStart, fSectorEnd);
249 int end = SkTMax(fSectorStart, fSectorEnd);
250 bool crossesZero = end - start > 16;
251 return crossesZero;
252}
caryclark@google.com07393ca2013-04-08 11:47:37 +0000253
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000254bool SkOpAngle::checkParallel(const SkOpAngle& rh) const {
255 SkDVector scratch[2];
256 const SkDVector* sweep, * tweep;
257 if (!fUnorderedSweep) {
258 sweep = fSweep;
259 } else {
260 scratch[0] = fCurvePart[1] - fCurvePart[0];
261 sweep = &scratch[0];
caryclark@google.coma5e55922013-05-07 18:51:31 +0000262 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000263 if (!rh.fUnorderedSweep) {
264 tweep = rh.fSweep;
265 } else {
266 scratch[1] = rh.fCurvePart[1] - rh.fCurvePart[0];
267 tweep = &scratch[1];
caryclark@google.com07393ca2013-04-08 11:47:37 +0000268 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000269 double s0xt0 = sweep->crossCheck(*tweep);
270 if (tangentsDiverge(rh, s0xt0)) {
271 return s0xt0 < 0;
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +0000272 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000273 SkDVector m0 = fSegment->dPtAtT(midT()) - fCurvePart[0];
274 SkDVector m1 = rh.fSegment->dPtAtT(rh.midT()) - rh.fCurvePart[0];
275 double m0xm1 = m0.crossCheck(m1);
276 if (m0xm1 == 0) {
277 fUnorderable = true;
278 rh.fUnorderable = true;
279 return true;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000280 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000281 return m0xm1 < 0;
282}
283
284// the original angle is too short to get meaningful sector information
285// lengthen it until it is long enough to be meaningful or leave it unset if lengthening it
286// would cause it to intersect one of the adjacent angles
287bool SkOpAngle::computeSector() {
288 if (fComputedSector) {
caryclarkdac1d172014-06-17 05:15:38 -0700289 return !fUnorderable;
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000290 }
caryclark65b427c2014-09-18 10:32:57 -0700291// SkASSERT(fSegment->verb() != SkPath::kLine_Verb && small());
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000292 fComputedSector = true;
293 int step = fStart < fEnd ? 1 : -1;
294 int limit = step > 0 ? fSegment->count() : -1;
295 int checkEnd = fEnd;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000296 do {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000297// advance end
298 const SkOpSpan& span = fSegment->span(checkEnd);
299 const SkOpSegment* other = span.fOther;
300 int oCount = other->count();
301 for (int oIndex = 0; oIndex < oCount; ++oIndex) {
302 const SkOpSpan& oSpan = other->span(oIndex);
303 if (oSpan.fOther != fSegment) {
304 continue;
305 }
306 if (oSpan.fOtherIndex == checkEnd) {
307 continue;
308 }
309 if (!approximately_equal(oSpan.fOtherT, span.fT)) {
310 continue;
311 }
312 goto recomputeSector;
313 }
314 checkEnd += step;
315 } while (checkEnd != limit);
316recomputeSector:
317 if (checkEnd == fEnd || checkEnd - step == fEnd) {
318 fUnorderable = true;
319 return false;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000320 }
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000321 int saveEnd = fEnd;
caryclarkdac1d172014-06-17 05:15:38 -0700322 fComputedEnd = fEnd = checkEnd - step;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000323 setSpans();
324 setSector();
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000325 fEnd = saveEnd;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000326 return !fUnorderable;
327}
328
329// returns -1 if overlaps 0 if no overlap cw 1 if no overlap ccw
330int SkOpAngle::convexHullOverlaps(const SkOpAngle& rh) const {
331 const SkDVector* sweep = fSweep;
332 const SkDVector* tweep = rh.fSweep;
333 double s0xs1 = sweep[0].crossCheck(sweep[1]);
334 double s0xt0 = sweep[0].crossCheck(tweep[0]);
335 double s1xt0 = sweep[1].crossCheck(tweep[0]);
336 bool tBetweenS = s0xs1 > 0 ? s0xt0 > 0 && s1xt0 < 0 : s0xt0 < 0 && s1xt0 > 0;
337 double s0xt1 = sweep[0].crossCheck(tweep[1]);
338 double s1xt1 = sweep[1].crossCheck(tweep[1]);
339 tBetweenS |= s0xs1 > 0 ? s0xt1 > 0 && s1xt1 < 0 : s0xt1 < 0 && s1xt1 > 0;
340 double t0xt1 = tweep[0].crossCheck(tweep[1]);
341 if (tBetweenS) {
342 return -1;
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +0000343 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000344 if ((s0xt0 == 0 && s1xt1 == 0) || (s1xt0 == 0 && s0xt1 == 0)) { // s0 to s1 equals t0 to t1
345 return -1;
346 }
347 bool sBetweenT = t0xt1 > 0 ? s0xt0 < 0 && s0xt1 > 0 : s0xt0 > 0 && s0xt1 < 0;
348 sBetweenT |= t0xt1 > 0 ? s1xt0 < 0 && s1xt1 > 0 : s1xt0 > 0 && s1xt1 < 0;
349 if (sBetweenT) {
350 return -1;
351 }
352 // if all of the sweeps are in the same half plane, then the order of any pair is enough
353 if (s0xt0 >= 0 && s0xt1 >= 0 && s1xt0 >= 0 && s1xt1 >= 0) {
354 return 0;
355 }
356 if (s0xt0 <= 0 && s0xt1 <= 0 && s1xt0 <= 0 && s1xt1 <= 0) {
357 return 1;
358 }
359 // if the outside sweeps are greater than 180 degress:
360 // first assume the inital tangents are the ordering
361 // if the midpoint direction matches the inital order, that is enough
362 SkDVector m0 = fSegment->dPtAtT(midT()) - fCurvePart[0];
363 SkDVector m1 = rh.fSegment->dPtAtT(rh.midT()) - rh.fCurvePart[0];
364 double m0xm1 = m0.crossCheck(m1);
365 if (s0xt0 > 0 && m0xm1 > 0) {
366 return 0;
367 }
368 if (s0xt0 < 0 && m0xm1 < 0) {
369 return 1;
370 }
371 if (tangentsDiverge(rh, s0xt0)) {
372 return s0xt0 < 0;
373 }
374 return m0xm1 < 0;
375}
376
377// OPTIMIZATION: longest can all be either lazily computed here or precomputed in setup
378double SkOpAngle::distEndRatio(double dist) const {
379 double longest = 0;
380 const SkOpSegment& segment = *this->segment();
381 int ptCount = SkPathOpsVerbToPoints(segment.verb());
382 const SkPoint* pts = segment.pts();
383 for (int idx1 = 0; idx1 <= ptCount - 1; ++idx1) {
384 for (int idx2 = idx1 + 1; idx2 <= ptCount; ++idx2) {
385 if (idx1 == idx2) {
386 continue;
387 }
388 SkDVector v;
389 v.set(pts[idx2] - pts[idx1]);
390 double lenSq = v.lengthSquared();
391 longest = SkTMax(longest, lenSq);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000392 }
393 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000394 return sqrt(longest) / dist;
395}
396
397bool SkOpAngle::endsIntersect(const SkOpAngle& rh) const {
398 SkPath::Verb lVerb = fSegment->verb();
399 SkPath::Verb rVerb = rh.fSegment->verb();
400 int lPts = SkPathOpsVerbToPoints(lVerb);
401 int rPts = SkPathOpsVerbToPoints(rVerb);
402 SkDLine rays[] = {{{fCurvePart[0], rh.fCurvePart[rPts]}},
403 {{fCurvePart[0], fCurvePart[lPts]}}};
404 if (rays[0][1] == rays[1][1]) {
405 return checkParallel(rh);
406 }
407 double smallTs[2] = {-1, -1};
408 bool limited[2] = {false, false};
409 for (int index = 0; index < 2; ++index) {
410 const SkOpSegment& segment = index ? *rh.fSegment : *fSegment;
411 SkIntersections i;
caryclark65f55312014-11-13 06:58:52 -0800412 int cPts = index ? rPts : lPts;
413 (*CurveIntersectRay[cPts])(segment.pts(), rays[index], &i);
414 // if the curve is a line, then the line and the ray intersect only at their crossing
415 if (cPts == 1) { // line
416 continue;
417 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000418// SkASSERT(i.used() >= 1);
caryclarkdac1d172014-06-17 05:15:38 -0700419// if (i.used() <= 1) {
420// continue;
421// }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000422 double tStart = segment.t(index ? rh.fStart : fStart);
caryclarkdac1d172014-06-17 05:15:38 -0700423 double tEnd = segment.t(index ? rh.fComputedEnd : fComputedEnd);
424 bool testAscends = index ? rh.fStart < rh.fComputedEnd : fStart < fComputedEnd;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000425 double t = testAscends ? 0 : 1;
426 for (int idx2 = 0; idx2 < i.used(); ++idx2) {
427 double testT = i[0][idx2];
428 if (!approximately_between_orderable(tStart, testT, tEnd)) {
429 continue;
430 }
431 if (approximately_equal_orderable(tStart, testT)) {
432 continue;
433 }
434 smallTs[index] = t = testAscends ? SkTMax(t, testT) : SkTMin(t, testT);
435 limited[index] = approximately_equal_orderable(t, tEnd);
436 }
437 }
438#if 0
439 if (smallTs[0] < 0 && smallTs[1] < 0) { // if neither ray intersects, do endpoint sort
440 double m0xm1 = 0;
441 if (lVerb == SkPath::kLine_Verb) {
442 SkASSERT(rVerb != SkPath::kLine_Verb);
443 SkDVector m0 = rays[1][1] - fCurvePart[0];
444 SkDPoint endPt;
445 endPt.set(rh.fSegment->pts()[rh.fStart < rh.fEnd ? rPts : 0]);
446 SkDVector m1 = endPt - fCurvePart[0];
447 m0xm1 = m0.crossCheck(m1);
448 }
449 if (rVerb == SkPath::kLine_Verb) {
450 SkDPoint endPt;
451 endPt.set(fSegment->pts()[fStart < fEnd ? lPts : 0]);
452 SkDVector m0 = endPt - fCurvePart[0];
453 SkDVector m1 = rays[0][1] - fCurvePart[0];
454 m0xm1 = m0.crossCheck(m1);
455 }
456 if (m0xm1 != 0) {
457 return m0xm1 < 0;
458 }
459 }
caryclark@google.coma5e55922013-05-07 18:51:31 +0000460#endif
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000461 bool sRayLonger = false;
462 SkDVector sCept = {0, 0};
463 double sCeptT = -1;
464 int sIndex = -1;
465 bool useIntersect = false;
466 for (int index = 0; index < 2; ++index) {
467 if (smallTs[index] < 0) {
468 continue;
469 }
470 const SkOpSegment& segment = index ? *rh.fSegment : *fSegment;
471 const SkDPoint& dPt = segment.dPtAtT(smallTs[index]);
472 SkDVector cept = dPt - rays[index][0];
473 // If this point is on the curve, it should have been detected earlier by ordinary
474 // curve intersection. This may be hard to determine in general, but for lines,
475 // the point could be close to or equal to its end, but shouldn't be near the start.
476 if ((index ? lPts : rPts) == 1) {
477 SkDVector total = rays[index][1] - rays[index][0];
478 if (cept.lengthSquared() * 2 < total.lengthSquared()) {
479 continue;
480 }
481 }
482 SkDVector end = rays[index][1] - rays[index][0];
483 if (cept.fX * end.fX < 0 || cept.fY * end.fY < 0) {
484 continue;
485 }
486 double rayDist = cept.length();
487 double endDist = end.length();
488 bool rayLonger = rayDist > endDist;
489 if (limited[0] && limited[1] && rayLonger) {
490 useIntersect = true;
491 sRayLonger = rayLonger;
492 sCept = cept;
493 sCeptT = smallTs[index];
494 sIndex = index;
caryclark@google.coma5e55922013-05-07 18:51:31 +0000495 break;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000496 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000497 double delta = fabs(rayDist - endDist);
498 double minX, minY, maxX, maxY;
499 minX = minY = SK_ScalarInfinity;
500 maxX = maxY = -SK_ScalarInfinity;
501 const SkDCubic& curve = index ? rh.fCurvePart : fCurvePart;
502 int ptCount = index ? rPts : lPts;
503 for (int idx2 = 0; idx2 <= ptCount; ++idx2) {
504 minX = SkTMin(minX, curve[idx2].fX);
505 minY = SkTMin(minY, curve[idx2].fY);
506 maxX = SkTMax(maxX, curve[idx2].fX);
507 maxY = SkTMax(maxY, curve[idx2].fY);
508 }
509 double maxWidth = SkTMax(maxX - minX, maxY - minY);
510 delta /= maxWidth;
511 if (delta > 1e-4 && (useIntersect ^= true)) { // FIXME: move this magic number
512 sRayLonger = rayLonger;
513 sCept = cept;
514 sCeptT = smallTs[index];
515 sIndex = index;
516 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000517 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000518 if (useIntersect) {
519 const SkDCubic& curve = sIndex ? rh.fCurvePart : fCurvePart;
520 const SkOpSegment& segment = sIndex ? *rh.fSegment : *fSegment;
521 double tStart = segment.t(sIndex ? rh.fStart : fStart);
522 SkDVector mid = segment.dPtAtT(tStart + (sCeptT - tStart) / 2) - curve[0];
523 double septDir = mid.crossCheck(sCept);
524 if (!septDir) {
525 return checkParallel(rh);
526 }
527 return sRayLonger ^ (sIndex == 0) ^ (septDir < 0);
528 } else {
529 return checkParallel(rh);
caryclark@google.coma5e55922013-05-07 18:51:31 +0000530 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000531}
532
533// Most of the time, the first one can be found trivially by detecting the smallest sector value.
534// If all angles have the same sector value, actual sorting is required.
535const SkOpAngle* SkOpAngle::findFirst() const {
536 const SkOpAngle* best = this;
537 int bestStart = SkTMin(fSectorStart, fSectorEnd);
538 const SkOpAngle* angle = this;
539 while ((angle = angle->fNext) != this) {
540 int angleEnd = SkTMax(angle->fSectorStart, angle->fSectorEnd);
541 if (angleEnd < bestStart) {
542 return angle; // we wrapped around
543 }
544 int angleStart = SkTMin(angle->fSectorStart, angle->fSectorEnd);
545 if (bestStart > angleStart) {
546 best = angle;
547 bestStart = angleStart;
548 }
549 }
550 // back up to the first possible angle
551 const SkOpAngle* firstBest = best;
552 angle = best;
553 int bestEnd = SkTMax(best->fSectorStart, best->fSectorEnd);
554 while ((angle = angle->previous()) != firstBest) {
555 if (angle->fStop) {
556 break;
557 }
558 int angleStart = SkTMin(angle->fSectorStart, angle->fSectorEnd);
559 // angles that are smaller by one aren't necessary better, since the larger may be a line
skia.committer@gmail.coma1ed7ae2014-04-15 03:04:18 +0000560 // and the smaller may be a curve that curls to the other side of the line.
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000561 if (bestEnd + 1 < angleStart) {
562 return best;
563 }
564 best = angle;
565 bestEnd = SkTMax(angle->fSectorStart, angle->fSectorEnd);
566 }
567 // in the case where all angles are nearly in the same sector, check the order to find the best
568 firstBest = best;
569 angle = best;
570 do {
571 angle = angle->fNext;
572 if (angle->fStop) {
573 return firstBest;
574 }
575 bool orderable = best->orderable(*angle); // note: may return an unorderable angle
576 if (orderable == 0) {
577 return angle;
578 }
579 best = angle;
580 } while (angle != firstBest);
581 // if the angles are equally ordered, fall back on the initial tangent
582 bool foundBelow = false;
583 while ((angle = angle->fNext)) {
584 SkDVector scratch[2];
585 const SkDVector* sweep;
586 if (!angle->fUnorderedSweep) {
587 sweep = angle->fSweep;
588 } else {
589 scratch[0] = angle->fCurvePart[1] - angle->fCurvePart[0];
590 sweep = &scratch[0];
591 }
592 bool isAbove = sweep->fY <= 0;
593 if (isAbove && foundBelow) {
594 return angle;
595 }
596 foundBelow |= !isAbove;
597 if (angle == firstBest) {
598 return NULL; // should not loop around
599 }
600 }
601 SkASSERT(0); // should never get here
602 return NULL;
603}
604
605/* y<0 y==0 y>0 x<0 x==0 x>0 xy<0 xy==0 xy>0
606 0 x x x
607 1 x x x
608 2 x x x
609 3 x x x
610 4 x x x
611 5 x x x
612 6 x x x
613 7 x x x
614 8 x x x
615 9 x x x
616 10 x x x
617 11 x x x
618 12 x x x
619 13 x x x
620 14 x x x
621 15 x x x
622*/
623int SkOpAngle::findSector(SkPath::Verb verb, double x, double y) const {
624 double absX = fabs(x);
625 double absY = fabs(y);
626 double xy = SkPath::kLine_Verb == verb || !AlmostEqualUlps(absX, absY) ? absX - absY : 0;
627 // If there are four quadrants and eight octants, and since the Latin for sixteen is sedecim,
628 // one could coin the term sedecimant for a space divided into 16 sections.
629 // http://english.stackexchange.com/questions/133688/word-for-something-partitioned-into-16-parts
630 static const int sedecimant[3][3][3] = {
631 // y<0 y==0 y>0
632 // x<0 x==0 x>0 x<0 x==0 x>0 x<0 x==0 x>0
633 {{ 4, 3, 2}, { 7, -1, 15}, {10, 11, 12}}, // abs(x) < abs(y)
634 {{ 5, -1, 1}, {-1, -1, -1}, { 9, -1, 13}}, // abs(x) == abs(y)
635 {{ 6, 3, 0}, { 7, -1, 15}, { 8, 11, 14}}, // abs(x) > abs(y)
636 };
637 int sector = sedecimant[(xy >= 0) + (xy > 0)][(y >= 0) + (y > 0)][(x >= 0) + (x > 0)] * 2 + 1;
caryclark65b427c2014-09-18 10:32:57 -0700638// SkASSERT(SkPath::kLine_Verb == verb || sector >= 0);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000639 return sector;
640}
641
642// OPTIMIZE: if this loops to only one other angle, after first compare fails, insert on other side
643// OPTIMIZE: return where insertion succeeded. Then, start next insertion on opposite side
644void SkOpAngle::insert(SkOpAngle* angle) {
645 if (angle->fNext) {
646 if (loopCount() >= angle->loopCount()) {
647 if (!merge(angle)) {
648 return;
649 }
650 } else if (fNext) {
651 if (!angle->merge(this)) {
652 return;
653 }
654 } else {
655 angle->insert(this);
656 }
657 return;
658 }
659 bool singleton = NULL == fNext;
660 if (singleton) {
661 fNext = this;
662 }
663 SkOpAngle* next = fNext;
664 if (next->fNext == this) {
caryclark65f55312014-11-13 06:58:52 -0800665 if (angle->overlap(*this)) { // angles are essentially coincident
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000666 return;
667 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000668 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);
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000681 if (angle->overlap(*last) || angle->overlap(*next)) {
682 return;
683 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000684 if (angle->after(last)) {
685 last->fNext = angle;
686 angle->fNext = next;
687 debugValidateNext();
688 return;
689 }
690 last = next;
691 next = next->fNext;
692 if (last == this && next->fUnorderable) {
693 fUnorderable = true;
694 return;
695 }
696 SkASSERT(last != this);
697 } while (true);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000698}
699
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000700bool SkOpAngle::isHorizontal() const {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000701 return !fIsCurve && fSweep[0].fY == 0;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000702}
703
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000704SkOpSpan* SkOpAngle::lastMarked() const {
705 if (fLastMarked) {
706 if (fLastMarked->fChased) {
707 return NULL;
708 }
709 fLastMarked->fChased = true;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000710 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000711 return fLastMarked;
712}
713
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000714bool SkOpAngle::loopContains(const SkOpAngle& test) const {
715 if (!fNext) {
716 return false;
717 }
718 const SkOpAngle* first = this;
719 const SkOpAngle* loop = this;
720 const SkOpSegment* tSegment = test.fSegment;
721 double tStart = tSegment->span(test.fStart).fT;
722 double tEnd = tSegment->span(test.fEnd).fT;
723 do {
724 const SkOpSegment* lSegment = loop->fSegment;
725 // FIXME : use precisely_equal ? or compare points exactly ?
726 if (lSegment != tSegment) {
727 continue;
728 }
729 double lStart = lSegment->span(loop->fStart).fT;
730 if (lStart != tEnd) {
731 continue;
732 }
733 double lEnd = lSegment->span(loop->fEnd).fT;
734 if (lEnd == tStart) {
735 return true;
736 }
737 } while ((loop = loop->fNext) != first);
738 return false;
739}
740
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000741int SkOpAngle::loopCount() const {
742 int count = 0;
743 const SkOpAngle* first = this;
744 const SkOpAngle* next = this;
745 do {
746 next = next->fNext;
747 ++count;
748 } while (next && next != first);
749 return count;
750}
751
752// OPTIMIZATION: can this be done better in after when angles are sorted?
753void SkOpAngle::markStops() {
754 SkOpAngle* angle = this;
755 int lastEnd = SkTMax(fSectorStart, fSectorEnd);
756 do {
757 angle = angle->fNext;
758 int angleStart = SkTMin(angle->fSectorStart, angle->fSectorEnd);
759 // angles that are smaller by one aren't necessary better, since the larger may be a line
skia.committer@gmail.coma1ed7ae2014-04-15 03:04:18 +0000760 // and the smaller may be a curve that curls to the other side of the line.
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000761 if (lastEnd + 1 < angleStart) {
762 angle->fStop = true;
763 }
764 lastEnd = SkTMax(angle->fSectorStart, angle->fSectorEnd);
765 } while (angle != this);
766}
767
768bool SkOpAngle::merge(SkOpAngle* angle) {
769 SkASSERT(fNext);
770 SkASSERT(angle->fNext);
771 SkOpAngle* working = angle;
772 do {
773 if (this == working) {
774 return false;
775 }
776 working = working->fNext;
777 } while (working != angle);
778 do {
779 SkOpAngle* next = working->fNext;
780 working->fNext = NULL;
781 insert(working);
782 working = next;
783 } while (working != angle);
784 // it's likely that a pair of the angles are unorderable
caryclark65f55312014-11-13 06:58:52 -0800785#if 0 && DEBUG_ANGLE
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000786 SkOpAngle* last = angle;
787 working = angle->fNext;
788 do {
789 SkASSERT(last->fNext == working);
790 last->fNext = working->fNext;
791 SkASSERT(working->after(last));
792 last->fNext = working;
793 last = working;
794 working = working->fNext;
795 } while (last != angle);
796#endif
797 debugValidateNext();
798 return true;
799}
800
801double SkOpAngle::midT() const {
802 return (fSegment->t(fStart) + fSegment->t(fEnd)) / 2;
803}
804
805bool SkOpAngle::oppositePlanes(const SkOpAngle& rh) const {
806 int startSpan = abs(rh.fSectorStart - fSectorStart);
807 return startSpan >= 8;
808}
809
810bool SkOpAngle::orderable(const SkOpAngle& rh) const {
811 int result;
812 if (!fIsCurve) {
813 if (!rh.fIsCurve) {
814 double leftX = fTangentHalf.dx();
815 double leftY = fTangentHalf.dy();
816 double rightX = rh.fTangentHalf.dx();
817 double rightY = rh.fTangentHalf.dy();
818 double x_ry = leftX * rightY;
819 double rx_y = rightX * leftY;
820 if (x_ry == rx_y) {
821 if (leftX * rightX < 0 || leftY * rightY < 0) {
822 return true; // exactly 180 degrees apart
823 }
824 goto unorderable;
825 }
826 SkASSERT(x_ry != rx_y); // indicates an undetected coincidence -- worth finding earlier
827 return x_ry < rx_y;
828 }
829 if ((result = allOnOneSide(rh)) >= 0) {
830 return result;
831 }
832 if (fUnorderable || approximately_zero(rh.fSide)) {
833 goto unorderable;
834 }
835 } else if (!rh.fIsCurve) {
836 if ((result = rh.allOnOneSide(*this)) >= 0) {
837 return !result;
838 }
839 if (rh.fUnorderable || approximately_zero(fSide)) {
840 goto unorderable;
841 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000842 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000843 if ((result = convexHullOverlaps(rh)) >= 0) {
844 return result;
845 }
846 return endsIntersect(rh);
847unorderable:
848 fUnorderable = true;
849 rh.fUnorderable = true;
850 return true;
851}
852
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000853bool SkOpAngle::overlap(const SkOpAngle& other) const {
854 int min = SkTMin(fStart, fEnd);
855 const SkOpSpan& span = fSegment->span(min);
856 const SkOpSegment* oSeg = other.fSegment;
857 int oMin = SkTMin(other.fStart, other.fEnd);
858 const SkOpSpan& oSpan = oSeg->span(oMin);
859 if (!span.fSmall && !oSpan.fSmall) {
860 return false;
861 }
862 if (fSegment->span(fStart).fPt != oSeg->span(other.fStart).fPt) {
863 return false;
864 }
865 // see if small span is contained by opposite span
866 return span.fSmall ? oSeg->containsPt(fSegment->span(fEnd).fPt, other.fEnd, other.fStart)
867 : fSegment->containsPt(oSeg->span(other.fEnd).fPt, fEnd, fStart);
868}
869
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000870// OPTIMIZE: if this shows up in a profile, add a previous pointer
871// as is, this should be rarely called
872SkOpAngle* SkOpAngle::previous() const {
873 SkOpAngle* last = fNext;
874 do {
875 SkOpAngle* next = last->fNext;
876 if (next == this) {
877 return last;
878 }
879 last = next;
880 } while (true);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000881}
882
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000883void SkOpAngle::set(const SkOpSegment* segment, int start, int end) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000884 fSegment = segment;
885 fStart = start;
caryclarkdac1d172014-06-17 05:15:38 -0700886 fComputedEnd = fEnd = end;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000887 fNext = NULL;
888 fComputeSector = fComputedSector = false;
889 fStop = false;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000890 setSpans();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000891 setSector();
892}
893
894void SkOpAngle::setCurveHullSweep() {
895 fUnorderedSweep = false;
896 fSweep[0] = fCurvePart[1] - fCurvePart[0];
897 if (SkPath::kLine_Verb == fSegment->verb()) {
898 fSweep[1] = fSweep[0];
899 return;
900 }
901 fSweep[1] = fCurvePart[2] - fCurvePart[0];
902 if (SkPath::kCubic_Verb != fSegment->verb()) {
903 if (!fSweep[0].fX && !fSweep[0].fY) {
904 fSweep[0] = fSweep[1];
905 }
906 return;
907 }
908 SkDVector thirdSweep = fCurvePart[3] - fCurvePart[0];
909 if (fSweep[0].fX == 0 && fSweep[0].fY == 0) {
910 fSweep[0] = fSweep[1];
911 fSweep[1] = thirdSweep;
912 if (fSweep[0].fX == 0 && fSweep[0].fY == 0) {
913 fSweep[0] = fSweep[1];
914 fCurvePart[1] = fCurvePart[3];
915 fIsCurve = false;
916 }
917 return;
918 }
919 double s1x3 = fSweep[0].crossCheck(thirdSweep);
920 double s3x2 = thirdSweep.crossCheck(fSweep[1]);
921 if (s1x3 * s3x2 >= 0) { // if third vector is on or between first two vectors
922 return;
923 }
924 double s2x1 = fSweep[1].crossCheck(fSweep[0]);
925 // FIXME: If the sweep of the cubic is greater than 180 degrees, we're in trouble
926 // probably such wide sweeps should be artificially subdivided earlier so that never happens
927 SkASSERT(s1x3 * s2x1 < 0 || s1x3 * s3x2 < 0);
928 if (s3x2 * s2x1 < 0) {
929 SkASSERT(s2x1 * s1x3 > 0);
930 fSweep[0] = fSweep[1];
931 fUnorderedSweep = true;
932 }
933 fSweep[1] = thirdSweep;
934}
935
936void SkOpAngle::setSector() {
937 SkPath::Verb verb = fSegment->verb();
938 if (SkPath::kLine_Verb != verb && small()) {
caryclark65b427c2014-09-18 10:32:57 -0700939 goto deferTilLater;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000940 }
941 fSectorStart = findSector(verb, fSweep[0].fX, fSweep[0].fY);
caryclark65b427c2014-09-18 10:32:57 -0700942 if (fSectorStart < 0) {
943 goto deferTilLater;
944 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000945 if (!fIsCurve) { // if it's a line or line-like, note that both sectors are the same
946 SkASSERT(fSectorStart >= 0);
947 fSectorEnd = fSectorStart;
948 fSectorMask = 1 << fSectorStart;
949 return;
950 }
951 SkASSERT(SkPath::kLine_Verb != verb);
952 fSectorEnd = findSector(verb, fSweep[1].fX, fSweep[1].fY);
caryclark65b427c2014-09-18 10:32:57 -0700953 if (fSectorEnd < 0) {
954deferTilLater:
955 fSectorStart = fSectorEnd = -1;
956 fSectorMask = 0;
957 fComputeSector = true; // can't determine sector until segment length can be found
958 return;
959 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000960 if (fSectorEnd == fSectorStart) {
961 SkASSERT((fSectorStart & 3) != 3); // if the sector has no span, it can't be an exact angle
962 fSectorMask = 1 << fSectorStart;
963 return;
964 }
965 bool crossesZero = checkCrossesZero();
966 int start = SkTMin(fSectorStart, fSectorEnd);
967 bool curveBendsCCW = (fSectorStart == start) ^ crossesZero;
968 // bump the start and end of the sector span if they are on exact compass points
969 if ((fSectorStart & 3) == 3) {
970 fSectorStart = (fSectorStart + (curveBendsCCW ? 1 : 31)) & 0x1f;
971 }
972 if ((fSectorEnd & 3) == 3) {
973 fSectorEnd = (fSectorEnd + (curveBendsCCW ? 31 : 1)) & 0x1f;
974 }
975 crossesZero = checkCrossesZero();
976 start = SkTMin(fSectorStart, fSectorEnd);
977 int end = SkTMax(fSectorStart, fSectorEnd);
978 if (!crossesZero) {
979 fSectorMask = (unsigned) -1 >> (31 - end + start) << start;
980 } else {
981 fSectorMask = (unsigned) -1 >> (31 - start) | (-1 << end);
982 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000983}
984
caryclark@google.com07393ca2013-04-08 11:47:37 +0000985void SkOpAngle::setSpans() {
caryclark@google.com570863f2013-09-16 15:55:01 +0000986 fUnorderable = fSegment->isTiny(this);
987 fLastMarked = NULL;
caryclark@google.com570863f2013-09-16 15:55:01 +0000988 const SkPoint* pts = fSegment->pts();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000989 SkDEBUGCODE(fCurvePart[2].fX = fCurvePart[2].fY = fCurvePart[3].fX = fCurvePart[3].fY
990 = SK_ScalarNaN);
991 fSegment->subDivide(fStart, fEnd, &fCurvePart);
992 setCurveHullSweep();
993 const SkPath::Verb verb = fSegment->verb();
994 if (verb != SkPath::kLine_Verb
995 && !(fIsCurve = fSweep[0].crossCheck(fSweep[1]) != 0)) {
996 SkDLine lineHalf;
997 lineHalf[0].set(fCurvePart[0].asSkPoint());
998 lineHalf[1].set(fCurvePart[SkPathOpsVerbToPoints(verb)].asSkPoint());
999 fTangentHalf.lineEndPoints(lineHalf);
1000 fSide = 0;
caryclark@google.comcffbcc32013-06-04 17:59:42 +00001001 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001002 switch (verb) {
caryclark@google.com07393ca2013-04-08 11:47:37 +00001003 case SkPath::kLine_Verb: {
caryclark@google.com570863f2013-09-16 15:55:01 +00001004 SkASSERT(fStart != fEnd);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001005 const SkPoint& cP1 = pts[fStart < fEnd];
1006 SkDLine lineHalf;
1007 lineHalf[0].set(fSegment->span(fStart).fPt);
1008 lineHalf[1].set(cP1);
1009 fTangentHalf.lineEndPoints(lineHalf);
caryclark@google.com07393ca2013-04-08 11:47:37 +00001010 fSide = 0;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001011 fIsCurve = false;
1012 } return;
caryclark@google.com07393ca2013-04-08 11:47:37 +00001013 case SkPath::kQuad_Verb: {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001014 SkLineParameters tangentPart;
1015 SkDQuad& quad2 = *SkTCast<SkDQuad*>(&fCurvePart);
1016 (void) tangentPart.quadEndPoints(quad2);
1017 fSide = -tangentPart.pointDistance(fCurvePart[2]); // not normalized -- compare sign only
caryclark@google.comcffbcc32013-06-04 17:59:42 +00001018 } break;
1019 case SkPath::kCubic_Verb: {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001020 SkLineParameters tangentPart;
1021 (void) tangentPart.cubicPart(fCurvePart);
1022 fSide = -tangentPart.pointDistance(fCurvePart[3]);
caryclark@google.comb3f09212013-04-17 15:49:16 +00001023 double testTs[4];
1024 // OPTIMIZATION: keep inflections precomputed with cubic segment?
caryclark@google.comcffbcc32013-06-04 17:59:42 +00001025 int testCount = SkDCubic::FindInflections(pts, testTs);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001026 double startT = fSegment->t(fStart);
caryclark@google.comcffbcc32013-06-04 17:59:42 +00001027 double endT = fSegment->t(fEnd);
caryclark@google.comb3f09212013-04-17 15:49:16 +00001028 double limitT = endT;
1029 int index;
1030 for (index = 0; index < testCount; ++index) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001031 if (!::between(startT, testTs[index], limitT)) {
caryclark@google.comb3f09212013-04-17 15:49:16 +00001032 testTs[index] = -1;
1033 }
caryclark@google.com07393ca2013-04-08 11:47:37 +00001034 }
caryclark@google.comb3f09212013-04-17 15:49:16 +00001035 testTs[testCount++] = startT;
1036 testTs[testCount++] = endT;
commit-bot@chromium.orgb76d3b62013-04-22 19:55:19 +00001037 SkTQSort<double>(testTs, &testTs[testCount - 1]);
caryclark@google.comb3f09212013-04-17 15:49:16 +00001038 double bestSide = 0;
1039 int testCases = (testCount << 1) - 1;
1040 index = 0;
1041 while (testTs[index] < 0) {
1042 ++index;
1043 }
1044 index <<= 1;
1045 for (; index < testCases; ++index) {
1046 int testIndex = index >> 1;
1047 double testT = testTs[testIndex];
1048 if (index & 1) {
1049 testT = (testT + testTs[testIndex + 1]) / 2;
1050 }
1051 // OPTIMIZE: could avoid call for t == startT, endT
caryclark@google.comcffbcc32013-06-04 17:59:42 +00001052 SkDPoint pt = dcubic_xy_at_t(pts, testT);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001053 SkLineParameters tangentPart;
1054 tangentPart.cubicEndPoints(fCurvePart);
1055 double testSide = tangentPart.pointDistance(pt);
caryclark@google.comb3f09212013-04-17 15:49:16 +00001056 if (fabs(bestSide) < fabs(testSide)) {
1057 bestSide = testSide;
1058 }
1059 }
1060 fSide = -bestSide; // compare sign only
caryclark@google.com07393ca2013-04-08 11:47:37 +00001061 } break;
1062 default:
1063 SkASSERT(0);
1064 }
caryclark@google.com07393ca2013-04-08 11:47:37 +00001065}
caryclark@google.com570863f2013-09-16 15:55:01 +00001066
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001067bool SkOpAngle::small() const {
1068 int min = SkMin32(fStart, fEnd);
1069 int max = SkMax32(fStart, fEnd);
1070 for (int index = min; index < max; ++index) {
1071 const SkOpSpan& mSpan = fSegment->span(index);
1072 if (!mSpan.fSmall) {
1073 return false;
1074 }
1075 }
1076 return true;
caryclark@google.com570863f2013-09-16 15:55:01 +00001077}
commit-bot@chromium.org4431e772014-04-14 17:08:59 +00001078
1079bool SkOpAngle::tangentsDiverge(const SkOpAngle& rh, double s0xt0) const {
1080 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)
1092 const SkDVector* sweep = fSweep;
1093 const SkDVector* tweep = rh.fSweep;
1094 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);
1103 double mFactor = fabs(useS ? distEndRatio(sDist) : rh.distEndRatio(tDist));
1104 return mFactor < 5000; // empirically found limit
1105}
caryclarkdac1d172014-06-17 05:15:38 -07001106
1107SkOpAngleSet::SkOpAngleSet()
1108 : fAngles(NULL)
1109#if DEBUG_ANGLE
1110 , fCount(0)
1111#endif
1112{
1113}
1114
1115SkOpAngleSet::~SkOpAngleSet() {
1116 SkDELETE(fAngles);
1117}
1118
1119SkOpAngle& SkOpAngleSet::push_back() {
1120 if (!fAngles) {
1121 fAngles = SkNEW_ARGS(SkChunkAlloc, (2));
1122 }
1123 void* ptr = fAngles->allocThrow(sizeof(SkOpAngle));
1124 SkOpAngle* angle = (SkOpAngle*) ptr;
1125#if DEBUG_ANGLE
1126 angle->setID(++fCount);
1127#endif
1128 return *angle;
1129}
1130
1131void SkOpAngleSet::reset() {
1132 if (fAngles) {
1133 fAngles->reset();
1134 }
1135}