| caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 1 | // Another approach is to start with the implicit form of one curve and solve |
| 2 | // (seek implicit coefficients in QuadraticParameter.cpp |
| 3 | // by substituting in the parametric form of the other. |
| 4 | // The downside of this approach is that early rejects are difficult to come by. |
| 5 | // http://planetmath.org/encyclopedia/GaloisTheoreticDerivationOfTheQuarticFormula.html#step |
| 6 | |
| 7 | |
| caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 8 | #include "CubicUtilities.h" |
| caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 9 | #include "CurveIntersection.h" |
| 10 | #include "Intersections.h" |
| 11 | #include "QuadraticParameterization.h" |
| 12 | #include "QuarticRoot.h" |
| 13 | #include "QuadraticUtilities.h" |
| caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 14 | #include "TSearch.h" |
| 15 | |
| caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 16 | /* given the implicit form 0 = Ax^2 + Bxy + Cy^2 + Dx + Ey + F |
| 17 | * and given x = at^2 + bt + c (the parameterized form) |
| 18 | * y = dt^2 + et + f |
| skia.committer@gmail.com | 055c7c2 | 2012-09-15 02:01:41 +0000 | [diff] [blame] | 19 | * then |
| caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 20 | * 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 |
| 21 | */ |
| skia.committer@gmail.com | 15dd300 | 2013-01-18 07:07:28 +0000 | [diff] [blame] | 22 | |
| caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 23 | static int findRoots(const QuadImplicitForm& i, const Quadratic& q2, double roots[4], |
| caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 24 | bool oneHint) { |
| caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 25 | double a, b, c; |
| 26 | set_abc(&q2[0].x, a, b, c); |
| 27 | double d, e, f; |
| 28 | set_abc(&q2[0].y, d, e, f); |
| 29 | const double t4 = i.x2() * a * a |
| 30 | + i.xy() * a * d |
| 31 | + i.y2() * d * d; |
| 32 | const double t3 = 2 * i.x2() * a * b |
| 33 | + i.xy() * (a * e + b * d) |
| 34 | + 2 * i.y2() * d * e; |
| 35 | const double t2 = i.x2() * (b * b + 2 * a * c) |
| 36 | + i.xy() * (c * d + b * e + a * f) |
| 37 | + i.y2() * (e * e + 2 * d * f) |
| 38 | + i.x() * a |
| 39 | + i.y() * d; |
| 40 | const double t1 = 2 * i.x2() * b * c |
| 41 | + i.xy() * (c * e + b * f) |
| 42 | + 2 * i.y2() * e * f |
| 43 | + i.x() * b |
| 44 | + i.y() * e; |
| 45 | const double t0 = i.x2() * c * c |
| 46 | + i.xy() * c * f |
| 47 | + i.y2() * f * f |
| 48 | + i.x() * c |
| 49 | + i.y() * f |
| 50 | + i.c(); |
| caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 51 | int rootCount = reducedQuarticRoots(t4, t3, t2, t1, t0, oneHint, roots); |
| 52 | if (rootCount >= 0) { |
| 53 | return rootCount; |
| caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 54 | } |
| caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 55 | return quarticRootsReal(t4, t3, t2, t1, t0, roots); |
| caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static void addValidRoots(const double roots[4], const int count, const int side, Intersections& i) { |
| 59 | int index; |
| 60 | for (index = 0; index < count; ++index) { |
| 61 | if (!approximately_zero_or_more(roots[index]) || !approximately_one_or_less(roots[index])) { |
| 62 | continue; |
| 63 | } |
| 64 | double t = 1 - roots[index]; |
| 65 | if (approximately_less_than_zero(t)) { |
| 66 | t = 0; |
| 67 | } else if (approximately_greater_than_one(t)) { |
| 68 | t = 1; |
| 69 | } |
| 70 | i.insertOne(t, side); |
| 71 | } |
| 72 | } |
| 73 | |
| caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 74 | static bool onlyEndPtsInCommon(const Quadratic& q1, const Quadratic& q2, Intersections& i) { |
| 75 | // the idea here is to see at minimum do a quick reject by rotating all points |
| 76 | // to either side of the line formed by connecting the endpoints |
| 77 | // if the opposite curves points are on the line or on the other side, the |
| 78 | // curves at most intersect at the endpoints |
| 79 | for (int oddMan = 0; oddMan < 3; ++oddMan) { |
| 80 | const _Point* endPt[2]; |
| 81 | for (int opp = 1; opp < 3; ++opp) { |
| 82 | int end = oddMan ^ opp; |
| 83 | if (end == 3) { |
| 84 | end = opp; |
| 85 | } |
| 86 | endPt[opp - 1] = &q1[end]; |
| 87 | } |
| 88 | double origX = endPt[0]->x; |
| 89 | double origY = endPt[0]->y; |
| 90 | double adj = endPt[1]->x - origX; |
| 91 | double opp = endPt[1]->y - origY; |
| 92 | double sign = (q1[oddMan].y - origY) * adj - (q1[oddMan].x - origX) * opp; |
| caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 93 | if (approximately_zero(sign)) { |
| 94 | goto tryNextHalfPlane; |
| 95 | } |
| caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 96 | for (int n = 0; n < 3; ++n) { |
| 97 | double test = (q2[n].y - origY) * adj - (q2[n].x - origX) * opp; |
| 98 | if (test * sign > 0) { |
| 99 | goto tryNextHalfPlane; |
| 100 | } |
| 101 | } |
| 102 | for (int i1 = 0; i1 < 3; i1 += 2) { |
| 103 | for (int i2 = 0; i2 < 3; i2 += 2) { |
| 104 | if (q1[i1] == q2[i2]) { |
| caryclark@google.com | fb51afb | 2012-10-19 15:54:16 +0000 | [diff] [blame] | 105 | i.insert(i1 >> 1, i2 >> 1); |
| caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | } |
| 109 | assert(i.fUsed < 3); |
| 110 | return true; |
| 111 | tryNextHalfPlane: |
| 112 | ; |
| 113 | } |
| 114 | return false; |
| 115 | } |
| 116 | |
| caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 117 | // http://www.blackpawn.com/texts/pointinpoly/default.html |
| 118 | static bool pointInTriangle(const _Point& pt, const _Line* testLines[]) { |
| 119 | const _Point& A = (*testLines[0])[0]; |
| 120 | const _Point& B = (*testLines[1])[0]; |
| 121 | const _Point& C = (*testLines[2])[0]; |
| 122 | |
| skia.committer@gmail.com | 15dd300 | 2013-01-18 07:07:28 +0000 | [diff] [blame] | 123 | // Compute vectors |
| caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 124 | _Point v0 = C - A; |
| 125 | _Point v1 = B - A; |
| 126 | _Point v2 = pt - A; |
| 127 | |
| 128 | // Compute dot products |
| 129 | double dot00 = v0.dot(v0); |
| 130 | double dot01 = v0.dot(v1); |
| 131 | double dot02 = v0.dot(v2); |
| 132 | double dot11 = v1.dot(v1); |
| 133 | double dot12 = v1.dot(v2); |
| 134 | |
| 135 | // Compute barycentric coordinates |
| 136 | double invDenom = 1 / (dot00 * dot11 - dot01 * dot01); |
| 137 | double u = (dot11 * dot02 - dot01 * dot12) * invDenom; |
| 138 | double v = (dot00 * dot12 - dot01 * dot02) * invDenom; |
| 139 | |
| 140 | // Check if point is in triangle |
| 141 | return (u >= 0) && (v >= 0) && (u + v < 1); |
| 142 | } |
| 143 | |
| 144 | static bool addIntercept(const Quadratic& q1, const Quadratic& q2, double tMin, double tMax, |
| 145 | Intersections& i) { |
| 146 | double tMid = (tMin + tMax) / 2; |
| 147 | _Point mid; |
| 148 | xy_at_t(q2, tMid, mid.x, mid.y); |
| 149 | _Line line; |
| 150 | line[0] = line[1] = mid; |
| caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 151 | _Point dxdy; |
| 152 | dxdy_at_t(q2, tMid, dxdy); |
| 153 | line[0].x -= dxdy.x; |
| 154 | line[0].y -= dxdy.y; |
| 155 | line[1].x += dxdy.x; |
| 156 | line[1].y += dxdy.y; |
| caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 157 | Intersections rootTs; |
| 158 | int roots = intersect(q1, line, rootTs); |
| caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 159 | if (roots == 2) { |
| 160 | return false; |
| 161 | } |
| 162 | SkASSERT(roots == 1); |
| caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 163 | _Point pt2; |
| 164 | xy_at_t(q1, rootTs.fT[0][0], pt2.x, pt2.y); |
| 165 | if (!pt2.approximatelyEqual(mid)) { |
| 166 | return false; |
| 167 | } |
| 168 | i.add(rootTs.fT[0][0], tMid); |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | static bool isLinearInner(const Quadratic& q1, double t1s, double t1e, const Quadratic& q2, |
| 173 | double t2s, double t2e, Intersections& i) { |
| 174 | Quadratic hull; |
| 175 | sub_divide(q1, t1s, t1e, hull); |
| 176 | _Line line = {hull[2], hull[0]}; |
| 177 | const _Line* testLines[] = { &line, (const _Line*) &hull[0], (const _Line*) &hull[1] }; |
| 178 | size_t testCount = sizeof(testLines) / sizeof(testLines[0]); |
| 179 | SkTDArray<double> tsFound; |
| 180 | for (size_t index = 0; index < testCount; ++index) { |
| 181 | Intersections rootTs; |
| 182 | int roots = intersect(q2, *testLines[index], rootTs); |
| 183 | for (int idx2 = 0; idx2 < roots; ++idx2) { |
| 184 | double t = rootTs.fT[0][idx2]; |
| 185 | if (approximately_negative(t - t2s) || approximately_positive(t - t2e)) { |
| 186 | continue; |
| 187 | } |
| 188 | *tsFound.append() = rootTs.fT[0][idx2]; |
| 189 | } |
| 190 | } |
| 191 | int tCount = tsFound.count(); |
| 192 | if (!tCount) { |
| 193 | return true; |
| 194 | } |
| 195 | double tMin, tMax; |
| 196 | _Point dxy1, dxy2; |
| 197 | if (tCount == 1) { |
| 198 | tMin = tMax = tsFound[0]; |
| 199 | } else if (tCount > 1) { |
| 200 | QSort<double>(tsFound.begin(), tsFound.end() - 1); |
| 201 | tMin = tsFound[0]; |
| 202 | tMax = tsFound[1]; |
| 203 | } |
| 204 | _Point end; |
| 205 | xy_at_t(q2, t2s, end.x, end.y); |
| 206 | bool startInTriangle = pointInTriangle(end, testLines); |
| 207 | if (startInTriangle) { |
| 208 | tMin = t2s; |
| skia.committer@gmail.com | 15dd300 | 2013-01-18 07:07:28 +0000 | [diff] [blame] | 209 | } |
| caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 210 | xy_at_t(q2, t2e, end.x, end.y); |
| 211 | bool endInTriangle = pointInTriangle(end, testLines); |
| 212 | if (endInTriangle) { |
| 213 | tMax = t2e; |
| 214 | } |
| 215 | int split = 0; |
| 216 | if (tMin != tMax || tCount > 2) { |
| caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 217 | dxdy_at_t(q2, tMin, dxy2); |
| caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 218 | for (int index = 1; index < tCount; ++index) { |
| 219 | dxy1 = dxy2; |
| caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 220 | dxdy_at_t(q2, tsFound[index], dxy2); |
| caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 221 | double dot = dxy1.dot(dxy2); |
| 222 | if (dot < 0) { |
| 223 | split = index - 1; |
| 224 | break; |
| 225 | } |
| 226 | } |
| skia.committer@gmail.com | 15dd300 | 2013-01-18 07:07:28 +0000 | [diff] [blame] | 227 | |
| caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 228 | } |
| 229 | if (split == 0) { // there's one point |
| 230 | if (addIntercept(q1, q2, tMin, tMax, i)) { |
| 231 | return true; |
| 232 | } |
| 233 | i.swap(); |
| 234 | return isLinearInner(q2, tMin, tMax, q1, t1s, t1e, i); |
| 235 | } |
| 236 | // At this point, we have two ranges of t values -- treat each separately at the split |
| 237 | bool result; |
| 238 | if (addIntercept(q1, q2, tMin, tsFound[split - 1], i)) { |
| 239 | result = true; |
| 240 | } else { |
| 241 | i.swap(); |
| 242 | result = isLinearInner(q2, tMin, tsFound[split - 1], q1, t1s, t1e, i); |
| 243 | } |
| 244 | if (addIntercept(q1, q2, tsFound[split], tMax, i)) { |
| 245 | result = true; |
| 246 | } else { |
| 247 | i.swap(); |
| 248 | result |= isLinearInner(q2, tsFound[split], tMax, q1, t1s, t1e, i); |
| 249 | } |
| 250 | return result; |
| 251 | } |
| 252 | |
| 253 | static double flatMeasure(const Quadratic& q) { |
| caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 254 | _Point mid = q[1]; |
| 255 | mid -= q[0]; |
| 256 | _Point dxy = q[2]; |
| 257 | dxy -= q[0]; |
| 258 | double length = dxy.length(); // OPTIMIZE: get rid of sqrt |
| 259 | return fabs(mid.cross(dxy) / length); |
| caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | // FIXME ? should this measure both and then use the quad that is the flattest as the line? |
| 263 | static bool isLinear(const Quadratic& q1, const Quadratic& q2, Intersections& i) { |
| 264 | double measure = flatMeasure(q1); |
| 265 | // OPTIMIZE: (get rid of sqrt) use approximately_zero |
| 266 | if (!approximately_zero_sqrt(measure)) { |
| 267 | return false; |
| 268 | } |
| 269 | return isLinearInner(q1, 0, 1, q2, 0, 1, i); |
| 270 | } |
| 271 | |
| caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 272 | // FIXME: if flat measure is sufficiently large, then probably the quartic solution failed |
| caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 273 | static bool relaxedIsLinear(const Quadratic& q1, const Quadratic& q2, Intersections& i) { |
| 274 | double m1 = flatMeasure(q1); |
| 275 | double m2 = flatMeasure(q2); |
| caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 276 | #if SK_DEBUG |
| 277 | double min = SkTMin(m1, m2); |
| 278 | if (min > 5) { |
| 279 | SkDebugf("%s maybe not flat enough.. %1.9g\n", __FUNCTION__, min); |
| 280 | } |
| 281 | #endif |
| caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 282 | i.reset(); |
| caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 283 | if (m1 < m2) { |
| caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 284 | isLinearInner(q1, 0, 1, q2, 0, 1, i); |
| 285 | return false; |
| 286 | } else { |
| 287 | isLinearInner(q2, 0, 1, q1, 0, 1, i); |
| 288 | return true; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | #if 0 |
| 293 | static void unsortableExpanse(const Quadratic& q1, const Quadratic& q2, Intersections& i) { |
| 294 | const Quadratic* qs[2] = { &q1, &q2 }; |
| 295 | // need t values for start and end of unsortable expanse on both curves |
| 296 | // try projecting lines parallel to the end points |
| 297 | i.fT[0][0] = 0; |
| 298 | i.fT[0][1] = 1; |
| 299 | int flip = -1; // undecided |
| 300 | for (int qIdx = 0; qIdx < 2; qIdx++) { |
| 301 | for (int t = 0; t < 2; t++) { |
| 302 | _Point dxdy; |
| caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 303 | dxdy_at_t(*qs[qIdx], t, dxdy); |
| caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 304 | _Line perp; |
| 305 | perp[0] = perp[1] = (*qs[qIdx])[t == 0 ? 0 : 2]; |
| 306 | perp[0].x += dxdy.y; |
| 307 | perp[0].y -= dxdy.x; |
| 308 | perp[1].x -= dxdy.y; |
| 309 | perp[1].y += dxdy.x; |
| 310 | Intersections hitData; |
| 311 | int hits = intersectRay(*qs[qIdx ^ 1], perp, hitData); |
| 312 | assert(hits <= 1); |
| 313 | if (hits) { |
| 314 | if (flip < 0) { |
| 315 | _Point dxdy2; |
| caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 316 | dxdy_at_t(*qs[qIdx ^ 1], hitData.fT[0][0], dxdy2); |
| caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 317 | double dot = dxdy.dot(dxdy2); |
| 318 | flip = dot < 0; |
| 319 | i.fT[1][0] = flip; |
| 320 | i.fT[1][1] = !flip; |
| 321 | } |
| 322 | i.fT[qIdx ^ 1][t ^ flip] = hitData.fT[0][0]; |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | i.fUnsortable = true; // failed, probably coincident or near-coincident |
| 327 | i.fUsed = 2; |
| 328 | } |
| 329 | #endif |
| 330 | |
| caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 331 | bool intersect2(const Quadratic& q1, const Quadratic& q2, Intersections& i) { |
| caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 332 | // if the quads share an end point, check to see if they overlap |
| 333 | |
| 334 | if (onlyEndPtsInCommon(q1, q2, i)) { |
| caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 335 | return i.intersected(); |
| 336 | } |
| caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 337 | if (onlyEndPtsInCommon(q2, q1, i)) { |
| 338 | i.swapPts(); |
| 339 | return i.intersected(); |
| 340 | } |
| 341 | // see if either quad is really a line |
| 342 | if (isLinear(q1, q2, i)) { |
| 343 | return i.intersected(); |
| 344 | } |
| 345 | if (isLinear(q2, q1, i)) { |
| 346 | i.swapPts(); |
| 347 | return i.intersected(); |
| 348 | } |
| caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 349 | QuadImplicitForm i1(q1); |
| 350 | QuadImplicitForm i2(q2); |
| 351 | if (i1.implicit_match(i2)) { |
| 352 | // FIXME: compute T values |
| 353 | // compute the intersections of the ends to find the coincident span |
| 354 | bool useVertical = fabs(q1[0].x - q1[2].x) < fabs(q1[0].y - q1[2].y); |
| 355 | double t; |
| 356 | if ((t = axialIntersect(q1, q2[0], useVertical)) >= 0) { |
| 357 | i.addCoincident(t, 0); |
| 358 | } |
| 359 | if ((t = axialIntersect(q1, q2[2], useVertical)) >= 0) { |
| 360 | i.addCoincident(t, 1); |
| 361 | } |
| 362 | useVertical = fabs(q2[0].x - q2[2].x) < fabs(q2[0].y - q2[2].y); |
| 363 | if ((t = axialIntersect(q2, q1[0], useVertical)) >= 0) { |
| 364 | i.addCoincident(0, t); |
| 365 | } |
| 366 | if ((t = axialIntersect(q2, q1[2], useVertical)) >= 0) { |
| 367 | i.addCoincident(1, t); |
| 368 | } |
| 369 | assert(i.fCoincidentUsed <= 2); |
| 370 | return i.fCoincidentUsed > 0; |
| 371 | } |
| 372 | double roots1[4], roots2[4]; |
| caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 373 | bool useCubic = q1[0] == q2[0] || q1[0] == q2[2] || q1[2] == q2[0]; |
| caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 374 | int rootCount = findRoots(i2, q1, roots1, useCubic); |
| caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 375 | // OPTIMIZATION: could short circuit here if all roots are < 0 or > 1 |
| caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 376 | int rootCount2 = findRoots(i1, q2, roots2, useCubic); |
| caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 377 | addValidRoots(roots1, rootCount, 0, i); |
| caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 378 | addValidRoots(roots2, rootCount2, 1, i); |
| caryclark@google.com | 0b7da43 | 2012-10-31 19:00:20 +0000 | [diff] [blame] | 379 | if (i.insertBalanced() && i.fUsed <= 1) { |
| 380 | if (i.fUsed == 1) { |
| 381 | _Point xy1, xy2; |
| 382 | xy_at_t(q1, i.fT[0][0], xy1.x, xy1.y); |
| 383 | xy_at_t(q2, i.fT[1][0], xy2.x, xy2.y); |
| 384 | if (!xy1.approximatelyEqual(xy2)) { |
| 385 | --i.fUsed; |
| 386 | --i.fUsed2; |
| 387 | } |
| 388 | } |
| 389 | return i.intersected(); |
| 390 | } |
| caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 391 | _Point pts[4]; |
| caryclark@google.com | 0b7da43 | 2012-10-31 19:00:20 +0000 | [diff] [blame] | 392 | int closest[4]; |
| 393 | double dist[4]; |
| caryclark@google.com | d168874 | 2012-09-18 20:08:37 +0000 | [diff] [blame] | 394 | int index, ndex2; |
| 395 | for (ndex2 = 0; ndex2 < i.fUsed2; ++ndex2) { |
| 396 | xy_at_t(q2, i.fT[1][ndex2], pts[ndex2].x, pts[ndex2].y); |
| caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 397 | } |
| caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 398 | bool foundSomething = false; |
| caryclark@google.com | 0b7da43 | 2012-10-31 19:00:20 +0000 | [diff] [blame] | 399 | for (index = 0; index < i.fUsed; ++index) { |
| caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 400 | _Point xy; |
| 401 | xy_at_t(q1, i.fT[0][index], xy.x, xy.y); |
| caryclark@google.com | 0b7da43 | 2012-10-31 19:00:20 +0000 | [diff] [blame] | 402 | dist[index] = DBL_MAX; |
| 403 | closest[index] = -1; |
| caryclark@google.com | d168874 | 2012-09-18 20:08:37 +0000 | [diff] [blame] | 404 | for (ndex2 = 0; ndex2 < i.fUsed2; ++ndex2) { |
| caryclark@google.com | 0b7da43 | 2012-10-31 19:00:20 +0000 | [diff] [blame] | 405 | if (!pts[ndex2].approximatelyEqual(xy)) { |
| 406 | continue; |
| 407 | } |
| 408 | double dx = pts[ndex2].x - xy.x; |
| 409 | double dy = pts[ndex2].y - xy.y; |
| 410 | double distance = dx * dx + dy * dy; |
| 411 | if (dist[index] <= distance) { |
| 412 | continue; |
| 413 | } |
| 414 | for (int outer = 0; outer < index; ++outer) { |
| 415 | if (closest[outer] != ndex2) { |
| 416 | continue; |
| 417 | } |
| 418 | if (dist[outer] < distance) { |
| 419 | goto next; |
| 420 | } |
| 421 | closest[outer] = -1; |
| 422 | } |
| 423 | dist[index] = distance; |
| 424 | closest[index] = ndex2; |
| caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 425 | foundSomething = true; |
| caryclark@google.com | 0b7da43 | 2012-10-31 19:00:20 +0000 | [diff] [blame] | 426 | next: |
| 427 | ; |
| 428 | } |
| 429 | } |
| caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 430 | if (i.fUsed && i.fUsed2 && !foundSomething) { |
| 431 | if (relaxedIsLinear(q1, q2, i)) { |
| 432 | i.swapPts(); |
| caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 433 | } |
| caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 434 | return i.intersected(); |
| caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 435 | } |
| caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 436 | double roots1Copy[4], roots2Copy[4]; |
| 437 | memcpy(roots1Copy, i.fT[0], i.fUsed * sizeof(double)); |
| 438 | memcpy(roots2Copy, i.fT[1], i.fUsed2 * sizeof(double)); |
| 439 | int used = 0; |
| 440 | do { |
| 441 | double lowest = DBL_MAX; |
| 442 | int lowestIndex = -1; |
| 443 | for (index = 0; index < i.fUsed; ++index) { |
| 444 | if (closest[index] < 0) { |
| caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 445 | continue; |
| caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 446 | } |
| 447 | if (roots1Copy[index] < lowest) { |
| 448 | lowestIndex = index; |
| 449 | lowest = roots1Copy[index]; |
| 450 | } |
| caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 451 | } |
| caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 452 | if (lowestIndex < 0) { |
| 453 | break; |
| 454 | } |
| 455 | i.fT[0][used] = roots1Copy[lowestIndex]; |
| 456 | i.fT[1][used] = roots2Copy[closest[lowestIndex]]; |
| 457 | closest[lowestIndex] = -1; |
| 458 | } while (++used < i.fUsed); |
| 459 | i.fUsed = i.fUsed2 = used; |
| 460 | i.fFlip = false; |
| caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 461 | return i.intersected(); |
| 462 | } |