Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | // Another approach is to start with the implicit form of one curve and solve |
michael@0 | 2 | // (seek implicit coefficients in QuadraticParameter.cpp |
michael@0 | 3 | // by substituting in the parametric form of the other. |
michael@0 | 4 | // The downside of this approach is that early rejects are difficult to come by. |
michael@0 | 5 | // http://planetmath.org/encyclopedia/GaloisTheoreticDerivationOfTheQuarticFormula.html#step |
michael@0 | 6 | |
michael@0 | 7 | |
michael@0 | 8 | #include "SkDQuadImplicit.h" |
michael@0 | 9 | #include "SkIntersections.h" |
michael@0 | 10 | #include "SkPathOpsLine.h" |
michael@0 | 11 | #include "SkQuarticRoot.h" |
michael@0 | 12 | #include "SkTArray.h" |
michael@0 | 13 | #include "SkTSort.h" |
michael@0 | 14 | |
michael@0 | 15 | /* given the implicit form 0 = Ax^2 + Bxy + Cy^2 + Dx + Ey + F |
michael@0 | 16 | * and given x = at^2 + bt + c (the parameterized form) |
michael@0 | 17 | * y = dt^2 + et + f |
michael@0 | 18 | * then |
michael@0 | 19 | * 0 = A(at^2+bt+c)(at^2+bt+c)+B(at^2+bt+c)(dt^2+et+f)+C(dt^2+et+f)(dt^2+et+f)+D(at^2+bt+c)+E(dt^2+et+f)+F |
michael@0 | 20 | */ |
michael@0 | 21 | |
michael@0 | 22 | static int findRoots(const SkDQuadImplicit& i, const SkDQuad& quad, double roots[4], |
michael@0 | 23 | bool oneHint, bool flip, int firstCubicRoot) { |
michael@0 | 24 | SkDQuad flipped; |
michael@0 | 25 | const SkDQuad& q = flip ? (flipped = quad.flip()) : quad; |
michael@0 | 26 | double a, b, c; |
michael@0 | 27 | SkDQuad::SetABC(&q[0].fX, &a, &b, &c); |
michael@0 | 28 | double d, e, f; |
michael@0 | 29 | SkDQuad::SetABC(&q[0].fY, &d, &e, &f); |
michael@0 | 30 | const double t4 = i.x2() * a * a |
michael@0 | 31 | + i.xy() * a * d |
michael@0 | 32 | + i.y2() * d * d; |
michael@0 | 33 | const double t3 = 2 * i.x2() * a * b |
michael@0 | 34 | + i.xy() * (a * e + b * d) |
michael@0 | 35 | + 2 * i.y2() * d * e; |
michael@0 | 36 | const double t2 = i.x2() * (b * b + 2 * a * c) |
michael@0 | 37 | + i.xy() * (c * d + b * e + a * f) |
michael@0 | 38 | + i.y2() * (e * e + 2 * d * f) |
michael@0 | 39 | + i.x() * a |
michael@0 | 40 | + i.y() * d; |
michael@0 | 41 | const double t1 = 2 * i.x2() * b * c |
michael@0 | 42 | + i.xy() * (c * e + b * f) |
michael@0 | 43 | + 2 * i.y2() * e * f |
michael@0 | 44 | + i.x() * b |
michael@0 | 45 | + i.y() * e; |
michael@0 | 46 | const double t0 = i.x2() * c * c |
michael@0 | 47 | + i.xy() * c * f |
michael@0 | 48 | + i.y2() * f * f |
michael@0 | 49 | + i.x() * c |
michael@0 | 50 | + i.y() * f |
michael@0 | 51 | + i.c(); |
michael@0 | 52 | int rootCount = SkReducedQuarticRoots(t4, t3, t2, t1, t0, oneHint, roots); |
michael@0 | 53 | if (rootCount < 0) { |
michael@0 | 54 | rootCount = SkQuarticRootsReal(firstCubicRoot, t4, t3, t2, t1, t0, roots); |
michael@0 | 55 | } |
michael@0 | 56 | if (flip) { |
michael@0 | 57 | for (int index = 0; index < rootCount; ++index) { |
michael@0 | 58 | roots[index] = 1 - roots[index]; |
michael@0 | 59 | } |
michael@0 | 60 | } |
michael@0 | 61 | return rootCount; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | static int addValidRoots(const double roots[4], const int count, double valid[4]) { |
michael@0 | 65 | int result = 0; |
michael@0 | 66 | int index; |
michael@0 | 67 | for (index = 0; index < count; ++index) { |
michael@0 | 68 | if (!approximately_zero_or_more(roots[index]) || !approximately_one_or_less(roots[index])) { |
michael@0 | 69 | continue; |
michael@0 | 70 | } |
michael@0 | 71 | double t = 1 - roots[index]; |
michael@0 | 72 | if (approximately_less_than_zero(t)) { |
michael@0 | 73 | t = 0; |
michael@0 | 74 | } else if (approximately_greater_than_one(t)) { |
michael@0 | 75 | t = 1; |
michael@0 | 76 | } |
michael@0 | 77 | valid[result++] = t; |
michael@0 | 78 | } |
michael@0 | 79 | return result; |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | static bool only_end_pts_in_common(const SkDQuad& q1, const SkDQuad& q2) { |
michael@0 | 83 | // the idea here is to see at minimum do a quick reject by rotating all points |
michael@0 | 84 | // to either side of the line formed by connecting the endpoints |
michael@0 | 85 | // if the opposite curves points are on the line or on the other side, the |
michael@0 | 86 | // curves at most intersect at the endpoints |
michael@0 | 87 | for (int oddMan = 0; oddMan < 3; ++oddMan) { |
michael@0 | 88 | const SkDPoint* endPt[2]; |
michael@0 | 89 | for (int opp = 1; opp < 3; ++opp) { |
michael@0 | 90 | int end = oddMan ^ opp; // choose a value not equal to oddMan |
michael@0 | 91 | if (3 == end) { // and correct so that largest value is 1 or 2 |
michael@0 | 92 | end = opp; |
michael@0 | 93 | } |
michael@0 | 94 | endPt[opp - 1] = &q1[end]; |
michael@0 | 95 | } |
michael@0 | 96 | double origX = endPt[0]->fX; |
michael@0 | 97 | double origY = endPt[0]->fY; |
michael@0 | 98 | double adj = endPt[1]->fX - origX; |
michael@0 | 99 | double opp = endPt[1]->fY - origY; |
michael@0 | 100 | double sign = (q1[oddMan].fY - origY) * adj - (q1[oddMan].fX - origX) * opp; |
michael@0 | 101 | if (approximately_zero(sign)) { |
michael@0 | 102 | goto tryNextHalfPlane; |
michael@0 | 103 | } |
michael@0 | 104 | for (int n = 0; n < 3; ++n) { |
michael@0 | 105 | double test = (q2[n].fY - origY) * adj - (q2[n].fX - origX) * opp; |
michael@0 | 106 | if (test * sign > 0 && !precisely_zero(test)) { |
michael@0 | 107 | goto tryNextHalfPlane; |
michael@0 | 108 | } |
michael@0 | 109 | } |
michael@0 | 110 | return true; |
michael@0 | 111 | tryNextHalfPlane: |
michael@0 | 112 | ; |
michael@0 | 113 | } |
michael@0 | 114 | return false; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | // returns false if there's more than one intercept or the intercept doesn't match the point |
michael@0 | 118 | // returns true if the intercept was successfully added or if the |
michael@0 | 119 | // original quads need to be subdivided |
michael@0 | 120 | static bool add_intercept(const SkDQuad& q1, const SkDQuad& q2, double tMin, double tMax, |
michael@0 | 121 | SkIntersections* i, bool* subDivide) { |
michael@0 | 122 | double tMid = (tMin + tMax) / 2; |
michael@0 | 123 | SkDPoint mid = q2.ptAtT(tMid); |
michael@0 | 124 | SkDLine line; |
michael@0 | 125 | line[0] = line[1] = mid; |
michael@0 | 126 | SkDVector dxdy = q2.dxdyAtT(tMid); |
michael@0 | 127 | line[0] -= dxdy; |
michael@0 | 128 | line[1] += dxdy; |
michael@0 | 129 | SkIntersections rootTs; |
michael@0 | 130 | rootTs.allowNear(false); |
michael@0 | 131 | int roots = rootTs.intersect(q1, line); |
michael@0 | 132 | if (roots == 0) { |
michael@0 | 133 | if (subDivide) { |
michael@0 | 134 | *subDivide = true; |
michael@0 | 135 | } |
michael@0 | 136 | return true; |
michael@0 | 137 | } |
michael@0 | 138 | if (roots == 2) { |
michael@0 | 139 | return false; |
michael@0 | 140 | } |
michael@0 | 141 | SkDPoint pt2 = q1.ptAtT(rootTs[0][0]); |
michael@0 | 142 | if (!pt2.approximatelyEqual(mid)) { |
michael@0 | 143 | return false; |
michael@0 | 144 | } |
michael@0 | 145 | i->insertSwap(rootTs[0][0], tMid, pt2); |
michael@0 | 146 | return true; |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | static bool is_linear_inner(const SkDQuad& q1, double t1s, double t1e, const SkDQuad& q2, |
michael@0 | 150 | double t2s, double t2e, SkIntersections* i, bool* subDivide) { |
michael@0 | 151 | SkDQuad hull = q1.subDivide(t1s, t1e); |
michael@0 | 152 | SkDLine line = {{hull[2], hull[0]}}; |
michael@0 | 153 | const SkDLine* testLines[] = { &line, (const SkDLine*) &hull[0], (const SkDLine*) &hull[1] }; |
michael@0 | 154 | const size_t kTestCount = SK_ARRAY_COUNT(testLines); |
michael@0 | 155 | SkSTArray<kTestCount * 2, double, true> tsFound; |
michael@0 | 156 | for (size_t index = 0; index < kTestCount; ++index) { |
michael@0 | 157 | SkIntersections rootTs; |
michael@0 | 158 | rootTs.allowNear(false); |
michael@0 | 159 | int roots = rootTs.intersect(q2, *testLines[index]); |
michael@0 | 160 | for (int idx2 = 0; idx2 < roots; ++idx2) { |
michael@0 | 161 | double t = rootTs[0][idx2]; |
michael@0 | 162 | #ifdef SK_DEBUG |
michael@0 | 163 | SkDPoint qPt = q2.ptAtT(t); |
michael@0 | 164 | SkDPoint lPt = testLines[index]->ptAtT(rootTs[1][idx2]); |
michael@0 | 165 | SkASSERT(qPt.approximatelyPEqual(lPt)); |
michael@0 | 166 | #endif |
michael@0 | 167 | if (approximately_negative(t - t2s) || approximately_positive(t - t2e)) { |
michael@0 | 168 | continue; |
michael@0 | 169 | } |
michael@0 | 170 | tsFound.push_back(rootTs[0][idx2]); |
michael@0 | 171 | } |
michael@0 | 172 | } |
michael@0 | 173 | int tCount = tsFound.count(); |
michael@0 | 174 | if (tCount <= 0) { |
michael@0 | 175 | return true; |
michael@0 | 176 | } |
michael@0 | 177 | double tMin, tMax; |
michael@0 | 178 | if (tCount == 1) { |
michael@0 | 179 | tMin = tMax = tsFound[0]; |
michael@0 | 180 | } else { |
michael@0 | 181 | SkASSERT(tCount > 1); |
michael@0 | 182 | SkTQSort<double>(tsFound.begin(), tsFound.end() - 1); |
michael@0 | 183 | tMin = tsFound[0]; |
michael@0 | 184 | tMax = tsFound[tsFound.count() - 1]; |
michael@0 | 185 | } |
michael@0 | 186 | SkDPoint end = q2.ptAtT(t2s); |
michael@0 | 187 | bool startInTriangle = hull.pointInHull(end); |
michael@0 | 188 | if (startInTriangle) { |
michael@0 | 189 | tMin = t2s; |
michael@0 | 190 | } |
michael@0 | 191 | end = q2.ptAtT(t2e); |
michael@0 | 192 | bool endInTriangle = hull.pointInHull(end); |
michael@0 | 193 | if (endInTriangle) { |
michael@0 | 194 | tMax = t2e; |
michael@0 | 195 | } |
michael@0 | 196 | int split = 0; |
michael@0 | 197 | SkDVector dxy1, dxy2; |
michael@0 | 198 | if (tMin != tMax || tCount > 2) { |
michael@0 | 199 | dxy2 = q2.dxdyAtT(tMin); |
michael@0 | 200 | for (int index = 1; index < tCount; ++index) { |
michael@0 | 201 | dxy1 = dxy2; |
michael@0 | 202 | dxy2 = q2.dxdyAtT(tsFound[index]); |
michael@0 | 203 | double dot = dxy1.dot(dxy2); |
michael@0 | 204 | if (dot < 0) { |
michael@0 | 205 | split = index - 1; |
michael@0 | 206 | break; |
michael@0 | 207 | } |
michael@0 | 208 | } |
michael@0 | 209 | } |
michael@0 | 210 | if (split == 0) { // there's one point |
michael@0 | 211 | if (add_intercept(q1, q2, tMin, tMax, i, subDivide)) { |
michael@0 | 212 | return true; |
michael@0 | 213 | } |
michael@0 | 214 | i->swap(); |
michael@0 | 215 | return is_linear_inner(q2, tMin, tMax, q1, t1s, t1e, i, subDivide); |
michael@0 | 216 | } |
michael@0 | 217 | // At this point, we have two ranges of t values -- treat each separately at the split |
michael@0 | 218 | bool result; |
michael@0 | 219 | if (add_intercept(q1, q2, tMin, tsFound[split - 1], i, subDivide)) { |
michael@0 | 220 | result = true; |
michael@0 | 221 | } else { |
michael@0 | 222 | i->swap(); |
michael@0 | 223 | result = is_linear_inner(q2, tMin, tsFound[split - 1], q1, t1s, t1e, i, subDivide); |
michael@0 | 224 | } |
michael@0 | 225 | if (add_intercept(q1, q2, tsFound[split], tMax, i, subDivide)) { |
michael@0 | 226 | result = true; |
michael@0 | 227 | } else { |
michael@0 | 228 | i->swap(); |
michael@0 | 229 | result |= is_linear_inner(q2, tsFound[split], tMax, q1, t1s, t1e, i, subDivide); |
michael@0 | 230 | } |
michael@0 | 231 | return result; |
michael@0 | 232 | } |
michael@0 | 233 | |
michael@0 | 234 | static double flat_measure(const SkDQuad& q) { |
michael@0 | 235 | SkDVector mid = q[1] - q[0]; |
michael@0 | 236 | SkDVector dxy = q[2] - q[0]; |
michael@0 | 237 | double length = dxy.length(); // OPTIMIZE: get rid of sqrt |
michael@0 | 238 | return fabs(mid.cross(dxy) / length); |
michael@0 | 239 | } |
michael@0 | 240 | |
michael@0 | 241 | // FIXME ? should this measure both and then use the quad that is the flattest as the line? |
michael@0 | 242 | static bool is_linear(const SkDQuad& q1, const SkDQuad& q2, SkIntersections* i) { |
michael@0 | 243 | double measure = flat_measure(q1); |
michael@0 | 244 | // OPTIMIZE: (get rid of sqrt) use approximately_zero |
michael@0 | 245 | if (!approximately_zero_sqrt(measure)) { |
michael@0 | 246 | return false; |
michael@0 | 247 | } |
michael@0 | 248 | return is_linear_inner(q1, 0, 1, q2, 0, 1, i, NULL); |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | // FIXME: if flat measure is sufficiently large, then probably the quartic solution failed |
michael@0 | 252 | // avoid imprecision incurred with chopAt |
michael@0 | 253 | static void relaxed_is_linear(const SkDQuad* q1, double s1, double e1, const SkDQuad* q2, |
michael@0 | 254 | double s2, double e2, SkIntersections* i) { |
michael@0 | 255 | double m1 = flat_measure(*q1); |
michael@0 | 256 | double m2 = flat_measure(*q2); |
michael@0 | 257 | i->reset(); |
michael@0 | 258 | const SkDQuad* rounder, *flatter; |
michael@0 | 259 | double sf, midf, ef, sr, er; |
michael@0 | 260 | if (m2 < m1) { |
michael@0 | 261 | rounder = q1; |
michael@0 | 262 | sr = s1; |
michael@0 | 263 | er = e1; |
michael@0 | 264 | flatter = q2; |
michael@0 | 265 | sf = s2; |
michael@0 | 266 | midf = (s2 + e2) / 2; |
michael@0 | 267 | ef = e2; |
michael@0 | 268 | } else { |
michael@0 | 269 | rounder = q2; |
michael@0 | 270 | sr = s2; |
michael@0 | 271 | er = e2; |
michael@0 | 272 | flatter = q1; |
michael@0 | 273 | sf = s1; |
michael@0 | 274 | midf = (s1 + e1) / 2; |
michael@0 | 275 | ef = e1; |
michael@0 | 276 | } |
michael@0 | 277 | bool subDivide = false; |
michael@0 | 278 | is_linear_inner(*flatter, sf, ef, *rounder, sr, er, i, &subDivide); |
michael@0 | 279 | if (subDivide) { |
michael@0 | 280 | relaxed_is_linear(flatter, sf, midf, rounder, sr, er, i); |
michael@0 | 281 | relaxed_is_linear(flatter, midf, ef, rounder, sr, er, i); |
michael@0 | 282 | } |
michael@0 | 283 | if (m2 < m1) { |
michael@0 | 284 | i->swapPts(); |
michael@0 | 285 | } |
michael@0 | 286 | } |
michael@0 | 287 | |
michael@0 | 288 | // each time through the loop, this computes values it had from the last loop |
michael@0 | 289 | // if i == j == 1, the center values are still good |
michael@0 | 290 | // otherwise, for i != 1 or j != 1, four of the values are still good |
michael@0 | 291 | // and if i == 1 ^ j == 1, an additional value is good |
michael@0 | 292 | static bool binary_search(const SkDQuad& quad1, const SkDQuad& quad2, double* t1Seed, |
michael@0 | 293 | double* t2Seed, SkDPoint* pt) { |
michael@0 | 294 | double tStep = ROUGH_EPSILON; |
michael@0 | 295 | SkDPoint t1[3], t2[3]; |
michael@0 | 296 | int calcMask = ~0; |
michael@0 | 297 | do { |
michael@0 | 298 | if (calcMask & (1 << 1)) t1[1] = quad1.ptAtT(*t1Seed); |
michael@0 | 299 | if (calcMask & (1 << 4)) t2[1] = quad2.ptAtT(*t2Seed); |
michael@0 | 300 | if (t1[1].approximatelyEqual(t2[1])) { |
michael@0 | 301 | *pt = t1[1]; |
michael@0 | 302 | #if ONE_OFF_DEBUG |
michael@0 | 303 | SkDebugf("%s t1=%1.9g t2=%1.9g (%1.9g,%1.9g) == (%1.9g,%1.9g)\n", __FUNCTION__, |
michael@0 | 304 | t1Seed, t2Seed, t1[1].fX, t1[1].fY, t2[1].fX, t2[1].fY); |
michael@0 | 305 | #endif |
michael@0 | 306 | return true; |
michael@0 | 307 | } |
michael@0 | 308 | if (calcMask & (1 << 0)) t1[0] = quad1.ptAtT(*t1Seed - tStep); |
michael@0 | 309 | if (calcMask & (1 << 2)) t1[2] = quad1.ptAtT(*t1Seed + tStep); |
michael@0 | 310 | if (calcMask & (1 << 3)) t2[0] = quad2.ptAtT(*t2Seed - tStep); |
michael@0 | 311 | if (calcMask & (1 << 5)) t2[2] = quad2.ptAtT(*t2Seed + tStep); |
michael@0 | 312 | double dist[3][3]; |
michael@0 | 313 | // OPTIMIZE: using calcMask value permits skipping some distance calcuations |
michael@0 | 314 | // if prior loop's results are moved to correct slot for reuse |
michael@0 | 315 | dist[1][1] = t1[1].distanceSquared(t2[1]); |
michael@0 | 316 | int best_i = 1, best_j = 1; |
michael@0 | 317 | for (int i = 0; i < 3; ++i) { |
michael@0 | 318 | for (int j = 0; j < 3; ++j) { |
michael@0 | 319 | if (i == 1 && j == 1) { |
michael@0 | 320 | continue; |
michael@0 | 321 | } |
michael@0 | 322 | dist[i][j] = t1[i].distanceSquared(t2[j]); |
michael@0 | 323 | if (dist[best_i][best_j] > dist[i][j]) { |
michael@0 | 324 | best_i = i; |
michael@0 | 325 | best_j = j; |
michael@0 | 326 | } |
michael@0 | 327 | } |
michael@0 | 328 | } |
michael@0 | 329 | if (best_i == 1 && best_j == 1) { |
michael@0 | 330 | tStep /= 2; |
michael@0 | 331 | if (tStep < FLT_EPSILON_HALF) { |
michael@0 | 332 | break; |
michael@0 | 333 | } |
michael@0 | 334 | calcMask = (1 << 0) | (1 << 2) | (1 << 3) | (1 << 5); |
michael@0 | 335 | continue; |
michael@0 | 336 | } |
michael@0 | 337 | if (best_i == 0) { |
michael@0 | 338 | *t1Seed -= tStep; |
michael@0 | 339 | t1[2] = t1[1]; |
michael@0 | 340 | t1[1] = t1[0]; |
michael@0 | 341 | calcMask = 1 << 0; |
michael@0 | 342 | } else if (best_i == 2) { |
michael@0 | 343 | *t1Seed += tStep; |
michael@0 | 344 | t1[0] = t1[1]; |
michael@0 | 345 | t1[1] = t1[2]; |
michael@0 | 346 | calcMask = 1 << 2; |
michael@0 | 347 | } else { |
michael@0 | 348 | calcMask = 0; |
michael@0 | 349 | } |
michael@0 | 350 | if (best_j == 0) { |
michael@0 | 351 | *t2Seed -= tStep; |
michael@0 | 352 | t2[2] = t2[1]; |
michael@0 | 353 | t2[1] = t2[0]; |
michael@0 | 354 | calcMask |= 1 << 3; |
michael@0 | 355 | } else if (best_j == 2) { |
michael@0 | 356 | *t2Seed += tStep; |
michael@0 | 357 | t2[0] = t2[1]; |
michael@0 | 358 | t2[1] = t2[2]; |
michael@0 | 359 | calcMask |= 1 << 5; |
michael@0 | 360 | } |
michael@0 | 361 | } while (true); |
michael@0 | 362 | #if ONE_OFF_DEBUG |
michael@0 | 363 | SkDebugf("%s t1=%1.9g t2=%1.9g (%1.9g,%1.9g) != (%1.9g,%1.9g) %s\n", __FUNCTION__, |
michael@0 | 364 | t1Seed, t2Seed, t1[1].fX, t1[1].fY, t1[2].fX, t1[2].fY); |
michael@0 | 365 | #endif |
michael@0 | 366 | return false; |
michael@0 | 367 | } |
michael@0 | 368 | |
michael@0 | 369 | static void lookNearEnd(const SkDQuad& q1, const SkDQuad& q2, int testT, |
michael@0 | 370 | const SkIntersections& orig, bool swap, SkIntersections* i) { |
michael@0 | 371 | if (orig.used() == 1 && orig[!swap][0] == testT) { |
michael@0 | 372 | return; |
michael@0 | 373 | } |
michael@0 | 374 | if (orig.used() == 2 && orig[!swap][1] == testT) { |
michael@0 | 375 | return; |
michael@0 | 376 | } |
michael@0 | 377 | SkDLine tmpLine; |
michael@0 | 378 | int testTIndex = testT << 1; |
michael@0 | 379 | tmpLine[0] = tmpLine[1] = q2[testTIndex]; |
michael@0 | 380 | tmpLine[1].fX += q2[1].fY - q2[testTIndex].fY; |
michael@0 | 381 | tmpLine[1].fY -= q2[1].fX - q2[testTIndex].fX; |
michael@0 | 382 | SkIntersections impTs; |
michael@0 | 383 | impTs.intersectRay(q1, tmpLine); |
michael@0 | 384 | for (int index = 0; index < impTs.used(); ++index) { |
michael@0 | 385 | SkDPoint realPt = impTs.pt(index); |
michael@0 | 386 | if (!tmpLine[0].approximatelyEqual(realPt)) { |
michael@0 | 387 | continue; |
michael@0 | 388 | } |
michael@0 | 389 | if (swap) { |
michael@0 | 390 | i->insert(testT, impTs[0][index], tmpLine[0]); |
michael@0 | 391 | } else { |
michael@0 | 392 | i->insert(impTs[0][index], testT, tmpLine[0]); |
michael@0 | 393 | } |
michael@0 | 394 | } |
michael@0 | 395 | } |
michael@0 | 396 | |
michael@0 | 397 | int SkIntersections::intersect(const SkDQuad& q1, const SkDQuad& q2) { |
michael@0 | 398 | fMax = 4; |
michael@0 | 399 | // if the quads share an end point, check to see if they overlap |
michael@0 | 400 | for (int i1 = 0; i1 < 3; i1 += 2) { |
michael@0 | 401 | for (int i2 = 0; i2 < 3; i2 += 2) { |
michael@0 | 402 | if (q1[i1].asSkPoint() == q2[i2].asSkPoint()) { |
michael@0 | 403 | insert(i1 >> 1, i2 >> 1, q1[i1]); |
michael@0 | 404 | } |
michael@0 | 405 | } |
michael@0 | 406 | } |
michael@0 | 407 | SkASSERT(fUsed < 3); |
michael@0 | 408 | if (only_end_pts_in_common(q1, q2)) { |
michael@0 | 409 | return fUsed; |
michael@0 | 410 | } |
michael@0 | 411 | if (only_end_pts_in_common(q2, q1)) { |
michael@0 | 412 | return fUsed; |
michael@0 | 413 | } |
michael@0 | 414 | // see if either quad is really a line |
michael@0 | 415 | // FIXME: figure out why reduce step didn't find this earlier |
michael@0 | 416 | if (is_linear(q1, q2, this)) { |
michael@0 | 417 | return fUsed; |
michael@0 | 418 | } |
michael@0 | 419 | SkIntersections swapped; |
michael@0 | 420 | swapped.setMax(fMax); |
michael@0 | 421 | if (is_linear(q2, q1, &swapped)) { |
michael@0 | 422 | swapped.swapPts(); |
michael@0 | 423 | set(swapped); |
michael@0 | 424 | return fUsed; |
michael@0 | 425 | } |
michael@0 | 426 | SkIntersections copyI(*this); |
michael@0 | 427 | lookNearEnd(q1, q2, 0, *this, false, ©I); |
michael@0 | 428 | lookNearEnd(q1, q2, 1, *this, false, ©I); |
michael@0 | 429 | lookNearEnd(q2, q1, 0, *this, true, ©I); |
michael@0 | 430 | lookNearEnd(q2, q1, 1, *this, true, ©I); |
michael@0 | 431 | int innerEqual = 0; |
michael@0 | 432 | if (copyI.fUsed >= 2) { |
michael@0 | 433 | SkASSERT(copyI.fUsed <= 4); |
michael@0 | 434 | double width = copyI[0][1] - copyI[0][0]; |
michael@0 | 435 | int midEnd = 1; |
michael@0 | 436 | for (int index = 2; index < copyI.fUsed; ++index) { |
michael@0 | 437 | double testWidth = copyI[0][index] - copyI[0][index - 1]; |
michael@0 | 438 | if (testWidth <= width) { |
michael@0 | 439 | continue; |
michael@0 | 440 | } |
michael@0 | 441 | midEnd = index; |
michael@0 | 442 | } |
michael@0 | 443 | for (int index = 0; index < 2; ++index) { |
michael@0 | 444 | double testT = (copyI[0][midEnd] * (index + 1) |
michael@0 | 445 | + copyI[0][midEnd - 1] * (2 - index)) / 3; |
michael@0 | 446 | SkDPoint testPt1 = q1.ptAtT(testT); |
michael@0 | 447 | testT = (copyI[1][midEnd] * (index + 1) + copyI[1][midEnd - 1] * (2 - index)) / 3; |
michael@0 | 448 | SkDPoint testPt2 = q2.ptAtT(testT); |
michael@0 | 449 | innerEqual += testPt1.approximatelyEqual(testPt2); |
michael@0 | 450 | } |
michael@0 | 451 | } |
michael@0 | 452 | bool expectCoincident = copyI.fUsed >= 2 && innerEqual == 2; |
michael@0 | 453 | if (expectCoincident) { |
michael@0 | 454 | reset(); |
michael@0 | 455 | insertCoincident(copyI[0][0], copyI[1][0], copyI.fPt[0]); |
michael@0 | 456 | int last = copyI.fUsed - 1; |
michael@0 | 457 | insertCoincident(copyI[0][last], copyI[1][last], copyI.fPt[last]); |
michael@0 | 458 | return fUsed; |
michael@0 | 459 | } |
michael@0 | 460 | SkDQuadImplicit i1(q1); |
michael@0 | 461 | SkDQuadImplicit i2(q2); |
michael@0 | 462 | int index; |
michael@0 | 463 | bool flip1 = q1[2] == q2[0]; |
michael@0 | 464 | bool flip2 = q1[0] == q2[2]; |
michael@0 | 465 | bool useCubic = q1[0] == q2[0]; |
michael@0 | 466 | double roots1[4]; |
michael@0 | 467 | int rootCount = findRoots(i2, q1, roots1, useCubic, flip1, 0); |
michael@0 | 468 | // OPTIMIZATION: could short circuit here if all roots are < 0 or > 1 |
michael@0 | 469 | double roots1Copy[4]; |
michael@0 | 470 | int r1Count = addValidRoots(roots1, rootCount, roots1Copy); |
michael@0 | 471 | SkDPoint pts1[4]; |
michael@0 | 472 | for (index = 0; index < r1Count; ++index) { |
michael@0 | 473 | pts1[index] = q1.ptAtT(roots1Copy[index]); |
michael@0 | 474 | } |
michael@0 | 475 | double roots2[4]; |
michael@0 | 476 | int rootCount2 = findRoots(i1, q2, roots2, useCubic, flip2, 0); |
michael@0 | 477 | double roots2Copy[4]; |
michael@0 | 478 | int r2Count = addValidRoots(roots2, rootCount2, roots2Copy); |
michael@0 | 479 | SkDPoint pts2[4]; |
michael@0 | 480 | for (index = 0; index < r2Count; ++index) { |
michael@0 | 481 | pts2[index] = q2.ptAtT(roots2Copy[index]); |
michael@0 | 482 | } |
michael@0 | 483 | if (r1Count == r2Count && r1Count <= 1) { |
michael@0 | 484 | if (r1Count == 1 && used() == 0) { |
michael@0 | 485 | if (pts1[0].approximatelyEqual(pts2[0])) { |
michael@0 | 486 | insert(roots1Copy[0], roots2Copy[0], pts1[0]); |
michael@0 | 487 | } else if (pts1[0].moreRoughlyEqual(pts2[0])) { |
michael@0 | 488 | // experiment: try to find intersection by chasing t |
michael@0 | 489 | if (binary_search(q1, q2, roots1Copy, roots2Copy, pts1)) { |
michael@0 | 490 | insert(roots1Copy[0], roots2Copy[0], pts1[0]); |
michael@0 | 491 | } |
michael@0 | 492 | } |
michael@0 | 493 | } |
michael@0 | 494 | return fUsed; |
michael@0 | 495 | } |
michael@0 | 496 | int closest[4]; |
michael@0 | 497 | double dist[4]; |
michael@0 | 498 | bool foundSomething = false; |
michael@0 | 499 | for (index = 0; index < r1Count; ++index) { |
michael@0 | 500 | dist[index] = DBL_MAX; |
michael@0 | 501 | closest[index] = -1; |
michael@0 | 502 | for (int ndex2 = 0; ndex2 < r2Count; ++ndex2) { |
michael@0 | 503 | if (!pts2[ndex2].approximatelyEqual(pts1[index])) { |
michael@0 | 504 | continue; |
michael@0 | 505 | } |
michael@0 | 506 | double dx = pts2[ndex2].fX - pts1[index].fX; |
michael@0 | 507 | double dy = pts2[ndex2].fY - pts1[index].fY; |
michael@0 | 508 | double distance = dx * dx + dy * dy; |
michael@0 | 509 | if (dist[index] <= distance) { |
michael@0 | 510 | continue; |
michael@0 | 511 | } |
michael@0 | 512 | for (int outer = 0; outer < index; ++outer) { |
michael@0 | 513 | if (closest[outer] != ndex2) { |
michael@0 | 514 | continue; |
michael@0 | 515 | } |
michael@0 | 516 | if (dist[outer] < distance) { |
michael@0 | 517 | goto next; |
michael@0 | 518 | } |
michael@0 | 519 | closest[outer] = -1; |
michael@0 | 520 | } |
michael@0 | 521 | dist[index] = distance; |
michael@0 | 522 | closest[index] = ndex2; |
michael@0 | 523 | foundSomething = true; |
michael@0 | 524 | next: |
michael@0 | 525 | ; |
michael@0 | 526 | } |
michael@0 | 527 | } |
michael@0 | 528 | if (r1Count && r2Count && !foundSomething) { |
michael@0 | 529 | relaxed_is_linear(&q1, 0, 1, &q2, 0, 1, this); |
michael@0 | 530 | return fUsed; |
michael@0 | 531 | } |
michael@0 | 532 | int used = 0; |
michael@0 | 533 | do { |
michael@0 | 534 | double lowest = DBL_MAX; |
michael@0 | 535 | int lowestIndex = -1; |
michael@0 | 536 | for (index = 0; index < r1Count; ++index) { |
michael@0 | 537 | if (closest[index] < 0) { |
michael@0 | 538 | continue; |
michael@0 | 539 | } |
michael@0 | 540 | if (roots1Copy[index] < lowest) { |
michael@0 | 541 | lowestIndex = index; |
michael@0 | 542 | lowest = roots1Copy[index]; |
michael@0 | 543 | } |
michael@0 | 544 | } |
michael@0 | 545 | if (lowestIndex < 0) { |
michael@0 | 546 | break; |
michael@0 | 547 | } |
michael@0 | 548 | insert(roots1Copy[lowestIndex], roots2Copy[closest[lowestIndex]], |
michael@0 | 549 | pts1[lowestIndex]); |
michael@0 | 550 | closest[lowestIndex] = -1; |
michael@0 | 551 | } while (++used < r1Count); |
michael@0 | 552 | return fUsed; |
michael@0 | 553 | } |