blob: a72a57dd6e144a5337c9bec3e308dcb94ce52bcb [file] [log] [blame]
caryclark@google.com235f56a2012-09-14 14:19:30 +00001// 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.com73ca6242013-01-17 21:02:47 +00008#include "CubicUtilities.h"
caryclark@google.com235f56a2012-09-14 14:19:30 +00009#include "CurveIntersection.h"
10#include "Intersections.h"
11#include "QuadraticParameterization.h"
12#include "QuarticRoot.h"
13#include "QuadraticUtilities.h"
caryclark@google.com73ca6242013-01-17 21:02:47 +000014#include "TSearch.h"
15
caryclark@google.com85ec74c2013-01-28 19:25:51 +000016#if SK_DEBUG
17#include "LineUtilities.h"
18#endif
19
caryclark@google.com235f56a2012-09-14 14:19:30 +000020/* given the implicit form 0 = Ax^2 + Bxy + Cy^2 + Dx + Ey + F
21 * and given x = at^2 + bt + c (the parameterized form)
22 * y = dt^2 + et + f
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +000023 * then
caryclark@google.com235f56a2012-09-14 14:19:30 +000024 * 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
25 */
skia.committer@gmail.com15dd3002013-01-18 07:07:28 +000026
caryclark@google.com73ca6242013-01-17 21:02:47 +000027static int findRoots(const QuadImplicitForm& i, const Quadratic& q2, double roots[4],
caryclark@google.com9f602912013-01-24 21:47:16 +000028 bool oneHint) {
caryclark@google.com235f56a2012-09-14 14:19:30 +000029 double a, b, c;
30 set_abc(&q2[0].x, a, b, c);
31 double d, e, f;
32 set_abc(&q2[0].y, d, e, f);
33 const double t4 = i.x2() * a * a
34 + i.xy() * a * d
35 + i.y2() * d * d;
36 const double t3 = 2 * i.x2() * a * b
37 + i.xy() * (a * e + b * d)
38 + 2 * i.y2() * d * e;
39 const double t2 = i.x2() * (b * b + 2 * a * c)
40 + i.xy() * (c * d + b * e + a * f)
41 + i.y2() * (e * e + 2 * d * f)
42 + i.x() * a
43 + i.y() * d;
44 const double t1 = 2 * i.x2() * b * c
45 + i.xy() * (c * e + b * f)
46 + 2 * i.y2() * e * f
47 + i.x() * b
48 + i.y() * e;
49 const double t0 = i.x2() * c * c
50 + i.xy() * c * f
51 + i.y2() * f * f
52 + i.x() * c
53 + i.y() * f
54 + i.c();
caryclark@google.com9f602912013-01-24 21:47:16 +000055 int rootCount = reducedQuarticRoots(t4, t3, t2, t1, t0, oneHint, roots);
56 if (rootCount >= 0) {
57 return rootCount;
caryclark@google.com73ca6242013-01-17 21:02:47 +000058 }
caryclark@google.com9f602912013-01-24 21:47:16 +000059 return quarticRootsReal(t4, t3, t2, t1, t0, roots);
caryclark@google.com235f56a2012-09-14 14:19:30 +000060}
61
62static void addValidRoots(const double roots[4], const int count, const int side, Intersections& i) {
63 int index;
64 for (index = 0; index < count; ++index) {
65 if (!approximately_zero_or_more(roots[index]) || !approximately_one_or_less(roots[index])) {
66 continue;
67 }
68 double t = 1 - roots[index];
69 if (approximately_less_than_zero(t)) {
70 t = 0;
71 } else if (approximately_greater_than_one(t)) {
72 t = 1;
73 }
74 i.insertOne(t, side);
75 }
76}
77
caryclark@google.com6aea33f2012-10-09 14:11:58 +000078static bool onlyEndPtsInCommon(const Quadratic& q1, const Quadratic& q2, Intersections& i) {
79// the idea here is to see at minimum do a quick reject by rotating all points
80// to either side of the line formed by connecting the endpoints
81// if the opposite curves points are on the line or on the other side, the
82// curves at most intersect at the endpoints
83 for (int oddMan = 0; oddMan < 3; ++oddMan) {
84 const _Point* endPt[2];
85 for (int opp = 1; opp < 3; ++opp) {
86 int end = oddMan ^ opp;
87 if (end == 3) {
88 end = opp;
89 }
90 endPt[opp - 1] = &q1[end];
91 }
92 double origX = endPt[0]->x;
93 double origY = endPt[0]->y;
94 double adj = endPt[1]->x - origX;
95 double opp = endPt[1]->y - origY;
96 double sign = (q1[oddMan].y - origY) * adj - (q1[oddMan].x - origX) * opp;
caryclark@google.com73ca6242013-01-17 21:02:47 +000097 if (approximately_zero(sign)) {
98 goto tryNextHalfPlane;
99 }
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000100 for (int n = 0; n < 3; ++n) {
101 double test = (q2[n].y - origY) * adj - (q2[n].x - origX) * opp;
102 if (test * sign > 0) {
103 goto tryNextHalfPlane;
104 }
105 }
106 for (int i1 = 0; i1 < 3; i1 += 2) {
107 for (int i2 = 0; i2 < 3; i2 += 2) {
108 if (q1[i1] == q2[i2]) {
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000109 i.insert(i1 >> 1, i2 >> 1);
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000110 }
111 }
112 }
113 assert(i.fUsed < 3);
114 return true;
115tryNextHalfPlane:
116 ;
117 }
118 return false;
119}
120
caryclark@google.com73ca6242013-01-17 21:02:47 +0000121// http://www.blackpawn.com/texts/pointinpoly/default.html
122static bool pointInTriangle(const _Point& pt, const _Line* testLines[]) {
123 const _Point& A = (*testLines[0])[0];
124 const _Point& B = (*testLines[1])[0];
125 const _Point& C = (*testLines[2])[0];
126
skia.committer@gmail.com15dd3002013-01-18 07:07:28 +0000127// Compute vectors
caryclark@google.com73ca6242013-01-17 21:02:47 +0000128 _Point v0 = C - A;
129 _Point v1 = B - A;
130 _Point v2 = pt - A;
131
132// Compute dot products
133 double dot00 = v0.dot(v0);
134 double dot01 = v0.dot(v1);
135 double dot02 = v0.dot(v2);
136 double dot11 = v1.dot(v1);
137 double dot12 = v1.dot(v2);
138
139// Compute barycentric coordinates
140 double invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
141 double u = (dot11 * dot02 - dot01 * dot12) * invDenom;
142 double v = (dot00 * dot12 - dot01 * dot02) * invDenom;
143
144// Check if point is in triangle
145 return (u >= 0) && (v >= 0) && (u + v < 1);
146}
147
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000148// returns false if there's more than one intercept or the intercept doesn't match the point
skia.committer@gmail.comcdcb2ce2013-01-29 07:05:52 +0000149// returns true if the intercept was successfully added or if the
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000150// original quads need to be subdivided
caryclark@google.com73ca6242013-01-17 21:02:47 +0000151static bool addIntercept(const Quadratic& q1, const Quadratic& q2, double tMin, double tMax,
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000152 Intersections& i, bool* subDivide) {
caryclark@google.com73ca6242013-01-17 21:02:47 +0000153 double tMid = (tMin + tMax) / 2;
154 _Point mid;
155 xy_at_t(q2, tMid, mid.x, mid.y);
156 _Line line;
157 line[0] = line[1] = mid;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000158 _Point dxdy;
159 dxdy_at_t(q2, tMid, dxdy);
160 line[0].x -= dxdy.x;
161 line[0].y -= dxdy.y;
162 line[1].x += dxdy.x;
163 line[1].y += dxdy.y;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000164 Intersections rootTs;
165 int roots = intersect(q1, line, rootTs);
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000166 if (roots == 0) {
167 if (subDivide) {
168 *subDivide = true;
169 }
170 return true;
171 }
caryclark@google.com9f602912013-01-24 21:47:16 +0000172 if (roots == 2) {
173 return false;
174 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000175 _Point pt2;
176 xy_at_t(q1, rootTs.fT[0][0], pt2.x, pt2.y);
177 if (!pt2.approximatelyEqual(mid)) {
178 return false;
179 }
180 i.add(rootTs.fT[0][0], tMid);
181 return true;
182}
183
184static bool isLinearInner(const Quadratic& q1, double t1s, double t1e, const Quadratic& q2,
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000185 double t2s, double t2e, Intersections& i, bool* subDivide) {
caryclark@google.com73ca6242013-01-17 21:02:47 +0000186 Quadratic hull;
187 sub_divide(q1, t1s, t1e, hull);
188 _Line line = {hull[2], hull[0]};
189 const _Line* testLines[] = { &line, (const _Line*) &hull[0], (const _Line*) &hull[1] };
190 size_t testCount = sizeof(testLines) / sizeof(testLines[0]);
191 SkTDArray<double> tsFound;
192 for (size_t index = 0; index < testCount; ++index) {
193 Intersections rootTs;
194 int roots = intersect(q2, *testLines[index], rootTs);
195 for (int idx2 = 0; idx2 < roots; ++idx2) {
196 double t = rootTs.fT[0][idx2];
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000197#if SK_DEBUG
198 _Point qPt, lPt;
199 xy_at_t(q2, t, qPt.x, qPt.y);
200 xy_at_t(*testLines[index], rootTs.fT[1][idx2], lPt.x, lPt.y);
201 SkASSERT(qPt.approximatelyEqual(lPt));
202#endif
caryclark@google.com73ca6242013-01-17 21:02:47 +0000203 if (approximately_negative(t - t2s) || approximately_positive(t - t2e)) {
204 continue;
205 }
206 *tsFound.append() = rootTs.fT[0][idx2];
207 }
208 }
209 int tCount = tsFound.count();
210 if (!tCount) {
211 return true;
212 }
213 double tMin, tMax;
214 _Point dxy1, dxy2;
215 if (tCount == 1) {
216 tMin = tMax = tsFound[0];
217 } else if (tCount > 1) {
218 QSort<double>(tsFound.begin(), tsFound.end() - 1);
219 tMin = tsFound[0];
220 tMax = tsFound[1];
221 }
222 _Point end;
223 xy_at_t(q2, t2s, end.x, end.y);
224 bool startInTriangle = pointInTriangle(end, testLines);
225 if (startInTriangle) {
226 tMin = t2s;
skia.committer@gmail.com15dd3002013-01-18 07:07:28 +0000227 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000228 xy_at_t(q2, t2e, end.x, end.y);
229 bool endInTriangle = pointInTriangle(end, testLines);
230 if (endInTriangle) {
231 tMax = t2e;
232 }
233 int split = 0;
234 if (tMin != tMax || tCount > 2) {
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000235 dxdy_at_t(q2, tMin, dxy2);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000236 for (int index = 1; index < tCount; ++index) {
237 dxy1 = dxy2;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000238 dxdy_at_t(q2, tsFound[index], dxy2);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000239 double dot = dxy1.dot(dxy2);
240 if (dot < 0) {
241 split = index - 1;
242 break;
243 }
244 }
skia.committer@gmail.com15dd3002013-01-18 07:07:28 +0000245
caryclark@google.com73ca6242013-01-17 21:02:47 +0000246 }
247 if (split == 0) { // there's one point
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000248 if (addIntercept(q1, q2, tMin, tMax, i, subDivide)) {
caryclark@google.com73ca6242013-01-17 21:02:47 +0000249 return true;
250 }
251 i.swap();
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000252 return isLinearInner(q2, tMin, tMax, q1, t1s, t1e, i, subDivide);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000253 }
254 // At this point, we have two ranges of t values -- treat each separately at the split
255 bool result;
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000256 if (addIntercept(q1, q2, tMin, tsFound[split - 1], i, subDivide)) {
caryclark@google.com73ca6242013-01-17 21:02:47 +0000257 result = true;
258 } else {
259 i.swap();
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000260 result = isLinearInner(q2, tMin, tsFound[split - 1], q1, t1s, t1e, i, subDivide);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000261 }
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000262 if (addIntercept(q1, q2, tsFound[split], tMax, i, subDivide)) {
caryclark@google.com73ca6242013-01-17 21:02:47 +0000263 result = true;
264 } else {
265 i.swap();
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000266 result |= isLinearInner(q2, tsFound[split], tMax, q1, t1s, t1e, i, subDivide);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000267 }
268 return result;
269}
270
271static double flatMeasure(const Quadratic& q) {
caryclark@google.com9f602912013-01-24 21:47:16 +0000272 _Point mid = q[1];
273 mid -= q[0];
274 _Point dxy = q[2];
275 dxy -= q[0];
276 double length = dxy.length(); // OPTIMIZE: get rid of sqrt
277 return fabs(mid.cross(dxy) / length);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000278}
279
280// FIXME ? should this measure both and then use the quad that is the flattest as the line?
281static bool isLinear(const Quadratic& q1, const Quadratic& q2, Intersections& i) {
282 double measure = flatMeasure(q1);
283 // OPTIMIZE: (get rid of sqrt) use approximately_zero
284 if (!approximately_zero_sqrt(measure)) {
285 return false;
286 }
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000287 return isLinearInner(q1, 0, 1, q2, 0, 1, i, NULL);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000288}
289
caryclark@google.com9f602912013-01-24 21:47:16 +0000290// FIXME: if flat measure is sufficiently large, then probably the quartic solution failed
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000291static void relaxedIsLinear(const Quadratic& q1, const Quadratic& q2, Intersections& i) {
caryclark@google.com73ca6242013-01-17 21:02:47 +0000292 double m1 = flatMeasure(q1);
293 double m2 = flatMeasure(q2);
caryclark@google.com9f602912013-01-24 21:47:16 +0000294#if SK_DEBUG
295 double min = SkTMin(m1, m2);
296 if (min > 5) {
297 SkDebugf("%s maybe not flat enough.. %1.9g\n", __FUNCTION__, min);
298 }
299#endif
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000300 i.reset();
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000301 const Quadratic& rounder = m2 < m1 ? q1 : q2;
302 const Quadratic& flatter = m2 < m1 ? q2 : q1;
303 bool subDivide = false;
304 isLinearInner(flatter, 0, 1, rounder, 0, 1, i, &subDivide);
305 if (subDivide) {
306 QuadraticPair pair;
307 chop_at(flatter, pair, 0.5);
308 Intersections firstI, secondI;
309 relaxedIsLinear(pair.first(), rounder, firstI);
310 for (int index = 0; index < firstI.used(); ++index) {
311 i.insert(firstI.fT[0][index] * 0.5, firstI.fT[1][index]);
312 }
313 relaxedIsLinear(pair.second(), rounder, secondI);
314 for (int index = 0; index < secondI.used(); ++index) {
315 i.insert(0.5 + secondI.fT[0][index] * 0.5, secondI.fT[1][index]);
316 }
317 }
318 if (m2 < m1) {
319 i.swapPts();
caryclark@google.com73ca6242013-01-17 21:02:47 +0000320 }
321}
322
323#if 0
324static void unsortableExpanse(const Quadratic& q1, const Quadratic& q2, Intersections& i) {
325 const Quadratic* qs[2] = { &q1, &q2 };
326 // need t values for start and end of unsortable expanse on both curves
327 // try projecting lines parallel to the end points
328 i.fT[0][0] = 0;
329 i.fT[0][1] = 1;
330 int flip = -1; // undecided
331 for (int qIdx = 0; qIdx < 2; qIdx++) {
332 for (int t = 0; t < 2; t++) {
333 _Point dxdy;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000334 dxdy_at_t(*qs[qIdx], t, dxdy);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000335 _Line perp;
336 perp[0] = perp[1] = (*qs[qIdx])[t == 0 ? 0 : 2];
337 perp[0].x += dxdy.y;
338 perp[0].y -= dxdy.x;
339 perp[1].x -= dxdy.y;
340 perp[1].y += dxdy.x;
341 Intersections hitData;
342 int hits = intersectRay(*qs[qIdx ^ 1], perp, hitData);
343 assert(hits <= 1);
344 if (hits) {
345 if (flip < 0) {
346 _Point dxdy2;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000347 dxdy_at_t(*qs[qIdx ^ 1], hitData.fT[0][0], dxdy2);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000348 double dot = dxdy.dot(dxdy2);
349 flip = dot < 0;
350 i.fT[1][0] = flip;
351 i.fT[1][1] = !flip;
352 }
353 i.fT[qIdx ^ 1][t ^ flip] = hitData.fT[0][0];
354 }
355 }
356 }
357 i.fUnsortable = true; // failed, probably coincident or near-coincident
358 i.fUsed = 2;
359}
360#endif
361
caryclark@google.com235f56a2012-09-14 14:19:30 +0000362bool intersect2(const Quadratic& q1, const Quadratic& q2, Intersections& i) {
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000363 // if the quads share an end point, check to see if they overlap
364
365 if (onlyEndPtsInCommon(q1, q2, i)) {
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000366 return i.intersected();
367 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000368 if (onlyEndPtsInCommon(q2, q1, i)) {
369 i.swapPts();
370 return i.intersected();
371 }
372 // see if either quad is really a line
373 if (isLinear(q1, q2, i)) {
374 return i.intersected();
375 }
376 if (isLinear(q2, q1, i)) {
377 i.swapPts();
378 return i.intersected();
379 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000380 QuadImplicitForm i1(q1);
381 QuadImplicitForm i2(q2);
382 if (i1.implicit_match(i2)) {
383 // FIXME: compute T values
384 // compute the intersections of the ends to find the coincident span
385 bool useVertical = fabs(q1[0].x - q1[2].x) < fabs(q1[0].y - q1[2].y);
386 double t;
387 if ((t = axialIntersect(q1, q2[0], useVertical)) >= 0) {
388 i.addCoincident(t, 0);
389 }
390 if ((t = axialIntersect(q1, q2[2], useVertical)) >= 0) {
391 i.addCoincident(t, 1);
392 }
393 useVertical = fabs(q2[0].x - q2[2].x) < fabs(q2[0].y - q2[2].y);
394 if ((t = axialIntersect(q2, q1[0], useVertical)) >= 0) {
395 i.addCoincident(0, t);
396 }
397 if ((t = axialIntersect(q2, q1[2], useVertical)) >= 0) {
398 i.addCoincident(1, t);
399 }
400 assert(i.fCoincidentUsed <= 2);
401 return i.fCoincidentUsed > 0;
402 }
403 double roots1[4], roots2[4];
caryclark@google.com73ca6242013-01-17 21:02:47 +0000404 bool useCubic = q1[0] == q2[0] || q1[0] == q2[2] || q1[2] == q2[0];
caryclark@google.com9f602912013-01-24 21:47:16 +0000405 int rootCount = findRoots(i2, q1, roots1, useCubic);
caryclark@google.com235f56a2012-09-14 14:19:30 +0000406 // OPTIMIZATION: could short circuit here if all roots are < 0 or > 1
caryclark@google.com9f602912013-01-24 21:47:16 +0000407 int rootCount2 = findRoots(i1, q2, roots2, useCubic);
caryclark@google.com235f56a2012-09-14 14:19:30 +0000408 addValidRoots(roots1, rootCount, 0, i);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000409 addValidRoots(roots2, rootCount2, 1, i);
caryclark@google.com0b7da432012-10-31 19:00:20 +0000410 if (i.insertBalanced() && i.fUsed <= 1) {
411 if (i.fUsed == 1) {
412 _Point xy1, xy2;
413 xy_at_t(q1, i.fT[0][0], xy1.x, xy1.y);
414 xy_at_t(q2, i.fT[1][0], xy2.x, xy2.y);
415 if (!xy1.approximatelyEqual(xy2)) {
416 --i.fUsed;
417 --i.fUsed2;
418 }
419 }
420 return i.intersected();
421 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000422 _Point pts[4];
caryclark@google.com0b7da432012-10-31 19:00:20 +0000423 int closest[4];
424 double dist[4];
caryclark@google.comd1688742012-09-18 20:08:37 +0000425 int index, ndex2;
426 for (ndex2 = 0; ndex2 < i.fUsed2; ++ndex2) {
427 xy_at_t(q2, i.fT[1][ndex2], pts[ndex2].x, pts[ndex2].y);
caryclark@google.com235f56a2012-09-14 14:19:30 +0000428 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000429 bool foundSomething = false;
caryclark@google.com0b7da432012-10-31 19:00:20 +0000430 for (index = 0; index < i.fUsed; ++index) {
caryclark@google.com235f56a2012-09-14 14:19:30 +0000431 _Point xy;
432 xy_at_t(q1, i.fT[0][index], xy.x, xy.y);
caryclark@google.com0b7da432012-10-31 19:00:20 +0000433 dist[index] = DBL_MAX;
434 closest[index] = -1;
caryclark@google.comd1688742012-09-18 20:08:37 +0000435 for (ndex2 = 0; ndex2 < i.fUsed2; ++ndex2) {
caryclark@google.com0b7da432012-10-31 19:00:20 +0000436 if (!pts[ndex2].approximatelyEqual(xy)) {
437 continue;
438 }
439 double dx = pts[ndex2].x - xy.x;
440 double dy = pts[ndex2].y - xy.y;
441 double distance = dx * dx + dy * dy;
442 if (dist[index] <= distance) {
443 continue;
444 }
445 for (int outer = 0; outer < index; ++outer) {
446 if (closest[outer] != ndex2) {
447 continue;
448 }
449 if (dist[outer] < distance) {
450 goto next;
451 }
452 closest[outer] = -1;
453 }
454 dist[index] = distance;
455 closest[index] = ndex2;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000456 foundSomething = true;
caryclark@google.com0b7da432012-10-31 19:00:20 +0000457 next:
458 ;
459 }
460 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000461 if (i.fUsed && i.fUsed2 && !foundSomething) {
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000462 relaxedIsLinear(q1, q2, i);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000463 return i.intersected();
caryclark@google.com235f56a2012-09-14 14:19:30 +0000464 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000465 double roots1Copy[4], roots2Copy[4];
466 memcpy(roots1Copy, i.fT[0], i.fUsed * sizeof(double));
467 memcpy(roots2Copy, i.fT[1], i.fUsed2 * sizeof(double));
468 int used = 0;
469 do {
470 double lowest = DBL_MAX;
471 int lowestIndex = -1;
472 for (index = 0; index < i.fUsed; ++index) {
473 if (closest[index] < 0) {
caryclark@google.com235f56a2012-09-14 14:19:30 +0000474 continue;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000475 }
476 if (roots1Copy[index] < lowest) {
477 lowestIndex = index;
478 lowest = roots1Copy[index];
479 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000480 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000481 if (lowestIndex < 0) {
482 break;
483 }
484 i.fT[0][used] = roots1Copy[lowestIndex];
485 i.fT[1][used] = roots2Copy[closest[lowestIndex]];
486 closest[lowestIndex] = -1;
487 } while (++used < i.fUsed);
488 i.fUsed = i.fUsed2 = used;
489 i.fFlip = false;
caryclark@google.com235f56a2012-09-14 14:19:30 +0000490 return i.intersected();
491}