blob: 9dea33244177ef096eda8ce6bed2e37a64edcbd3 [file] [log] [blame]
caryclark@google.com9e49fb62012-08-27 14:11:33 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
caryclark@google.comc6825902012-02-03 22:07:47 +00007#include "CurveIntersection.h"
caryclark@google.com8dcf1142012-07-02 20:27:02 +00008#include "CurveUtilities.h"
caryclark@google.com639df892012-01-10 21:46:10 +00009#include "CubicIntersection_TestData.h"
caryclark@google.com27accef2012-01-25 18:57:23 +000010#include "Intersection_Tests.h"
caryclark@google.com639df892012-01-10 21:46:10 +000011#include "Intersections.h"
12#include "TestUtilities.h"
13
14const int firstCubicIntersectionTest = 9;
15
caryclark@google.com45a8fc62013-02-14 15:29:11 +000016static void standardTestCases() {
caryclark@google.com639df892012-01-10 21:46:10 +000017 for (size_t index = firstCubicIntersectionTest; index < tests_count; ++index) {
18 const Cubic& cubic1 = tests[index][0];
19 const Cubic& cubic2 = tests[index][1];
20 Cubic reduce1, reduce2;
caryclark@google.com47d73da2013-02-17 01:41:25 +000021 int order1 = reduceOrder(cubic1, reduce1, kReduceOrder_NoQuadraticsAllowed,
22 kReduceOrder_TreatAsFill);
23 int order2 = reduceOrder(cubic2, reduce2, kReduceOrder_NoQuadraticsAllowed,
24 kReduceOrder_TreatAsFill);
caryclark@google.com639df892012-01-10 21:46:10 +000025 if (order1 < 4) {
caryclark@google.com27accef2012-01-25 18:57:23 +000026 printf("%s [%d] cubic1 order=%d\n", __FUNCTION__, (int) index, order1);
27 continue;
caryclark@google.com639df892012-01-10 21:46:10 +000028 }
29 if (order2 < 4) {
caryclark@google.com27accef2012-01-25 18:57:23 +000030 printf("%s [%d] cubic2 order=%d\n", __FUNCTION__, (int) index, order2);
31 continue;
caryclark@google.com639df892012-01-10 21:46:10 +000032 }
caryclark@google.com27accef2012-01-25 18:57:23 +000033 if (implicit_matches(reduce1, reduce2)) {
34 printf("%s [%d] coincident\n", __FUNCTION__, (int) index);
35 continue;
36 }
37 Intersections tIntersections;
caryclark@google.comc6825902012-02-03 22:07:47 +000038 intersect(reduce1, reduce2, tIntersections);
caryclark@google.com27accef2012-01-25 18:57:23 +000039 if (!tIntersections.intersected()) {
40 printf("%s [%d] no intersection\n", __FUNCTION__, (int) index);
41 continue;
42 }
43 for (int pt = 0; pt < tIntersections.used(); ++pt) {
44 double tt1 = tIntersections.fT[0][pt];
45 double tx1, ty1;
46 xy_at_t(cubic1, tt1, tx1, ty1);
47 double tt2 = tIntersections.fT[1][pt];
48 double tx2, ty2;
49 xy_at_t(cubic2, tt2, tx2, ty2);
caryclark@google.com6d0032a2013-01-04 19:41:13 +000050 if (!AlmostEqualUlps(tx1, tx2)) {
caryclark@google.com27accef2012-01-25 18:57:23 +000051 printf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
52 __FUNCTION__, (int)index, pt, tt1, tx1, ty1, tt2, tx2, ty2);
53 }
caryclark@google.com6d0032a2013-01-04 19:41:13 +000054 if (!AlmostEqualUlps(ty1, ty2)) {
caryclark@google.com27accef2012-01-25 18:57:23 +000055 printf("%s [%d,%d] y!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
56 __FUNCTION__, (int)index, pt, tt1, tx1, ty1, tt2, tx2, ty2);
caryclark@google.com639df892012-01-10 21:46:10 +000057 }
58 }
59 }
60}
caryclark@google.com73ca6242013-01-17 21:02:47 +000061
caryclark@google.com73ca6242013-01-17 21:02:47 +000062static const Cubic testSet[] = {
caryclark@google.com45a8fc62013-02-14 15:29:11 +000063{{0,1}, {4,5}, {1,0}, {5,3}},
64{{0,1}, {3,5}, {1,0}, {5,4}},
65
66{{0, 1}, {1, 6}, {1, 0}, {1, 0}},
67{{0, 1}, {0, 1}, {1, 0}, {6, 1}},
68
69{{0,1}, {3,4}, {1,0}, {5,1}},
70{{0,1}, {1,5}, {1,0}, {4,3}},
71
72{{0,1}, {1,2}, {1,0}, {6,1}},
73{{0,1}, {1,6}, {1,0}, {2,1}},
74
skia.committer@gmail.com044679e2013-02-15 07:16:57 +000075{{0,1}, {0,5}, {1,0}, {4,0}},
caryclark@google.com45a8fc62013-02-14 15:29:11 +000076{{0,1}, {0,4}, {1,0}, {5,0}},
77
caryclark@google.combeda3892013-02-07 13:13:41 +000078{{0,1}, {3,4}, {1,0}, {3,0}},
79{{0,1}, {0,3}, {1,0}, {4,3}},
80
81{{0, 0}, {1, 2}, {3, 4}, {4, 4}},
82{{0, 0}, {1, 2}, {3, 4}, {4, 4}},
83{{4, 4}, {3, 4}, {1, 2}, {0, 0}},
84
85{{0,1}, {2,3}, {1,0}, {1,0}},
86{{0,1}, {0,1}, {1,0}, {3,2}},
87
88{{0,2}, {0,1}, {1,0}, {1,0}},
89{{0,1}, {0,1}, {2,0}, {1,0}},
90
caryclark@google.comf9502d72013-02-04 14:06:49 +000091{{0, 0}, {0, 1}, {1, 1}, {1, 0}},
92{{1, 0}, {0, 0}, {0, 1}, {1, 1}},
93
caryclark@google.comaa358312013-01-29 20:28:49 +000094{{0, 1}, {0, 2}, {1, 0}, {1, 0}},
95{{0, 1}, {0, 1}, {1, 0}, {2, 0}},
96
caryclark@google.comf9502d72013-02-04 14:06:49 +000097{{0, 1}, {1, 6}, {1, 0}, {2, 0}},
98{{0, 1}, {0, 2}, {1, 0}, {6, 1}},
99
100{{0, 1}, {5, 6}, {1, 0}, {1, 0}},
101{{0, 1}, {0, 1}, {1, 0}, {6, 5}},
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000102
103{{95.837747722788592, 45.025976907939643}, {16.564570095652982, 0.72959763963222402}, {63.209855865319199, 68.047528419665767}, {57.640240647662544, 59.524565264361243}},
104{{51.593891741518817, 38.53849970667553}, {62.34752929878772, 74.924924725166022}, {74.810149322641152, 34.17966562983564}, {29.368398119401373, 94.66719277886078}},
105
106{{39.765160968417838, 33.060396198677083}, {5.1922921581157908, 66.854301452103215}, {31.619281802149157, 25.269248720849514}, {81.541621071073038, 70.025341524754353}},
107{{46.078911165743556, 48.259962651999651}, {20.24450549867214, 49.403916182650214}, {0.26325131778756683, 24.46489805563581}, {15.915006546264051, 83.515023059917155}},
108
caryclark@google.com9f602912013-01-24 21:47:16 +0000109{{65.454505973241524, 93.881892270353575}, {45.867360264932437, 92.723972719499827}, {2.1464054482739447, 74.636369140183717}, {33.774068594804994, 40.770872887582925}},
110{{72.963387832494163, 95.659300729473728}, {11.809496633619768, 82.209921247423594}, {13.456139067865974, 57.329313623406605}, {36.060621606214262, 70.867335643091849}},
111
112{{32.484981432782945, 75.082940782924624}, {42.467313093350882, 48.131159948246157}, {3.5963115764764657, 43.208665839959245}, {79.442476890721579, 89.709102357602262}},
113{{18.98573861410177, 93.308887208490106}, {40.405250173250792, 91.039661826118675}, {8.0467721950480584, 42.100282172719147}, {40.883324221187891, 26.030185504830527}},
114
115{{7.5374809128872498, 82.441702896003477}, {22.444346930107265, 22.138854312775123}, {66.76091829629658, 50.753805856571446}, {78.193478508942519, 97.7932997968948}},
116{{97.700573130371311, 53.53260215070685}, {87.72443481149358, 84.575876772671876}, {19.215031396232092, 47.032676472809484}, {11.989686410869325, 10.659507480757082}},
117
118{{26.192053931854691, 9.8504326817814416}, {10.174241480498686, 98.476562741434464}, {21.177712558385782, 33.814968789841501}, {75.329030899018534, 55.02231980442177}},
119{{56.222082700683771, 24.54395039218662}, {95.589995289030483, 81.050822735322086}, {28.180450866082897, 28.837706255185282}, {60.128952916771617, 87.311672180570511}},
120
121{{42.449716172390481, 52.379709366885805}, {27.896043159019225, 48.797373636065686}, {92.770268299044233, 89.899302036454571}, {12.102066544863426, 99.43241951960718}},
122{{45.77532924980639, 45.958701495993274}, {37.458701356062065, 68.393691335056758}, {37.569326692060258, 27.673713456687381}, {60.674866037757539, 62.47349659096146}},
123
caryclark@google.com73ca6242013-01-17 21:02:47 +0000124{{67.426548091427676, 37.993772624988935}, {23.483695892376684, 90.476863174921306}, {35.597065061143162, 79.872482633158796}, {75.38634169631932, 18.244890038969412}},
125{{61.336508189019057, 82.693132843213675}, {44.639380902349664, 54.074825790745592}, {16.815615499771951, 20.049704667203923}, {41.866884958868326, 56.735503699973002}},
126
127{{67.4265481, 37.9937726}, {23.4836959, 90.4768632}, {35.5970651, 79.8724826}, {75.3863417, 18.24489}},
128{{61.3365082, 82.6931328}, {44.6393809, 54.0748258}, {16.8156155, 20.0497047}, {41.866885, 56.7355037}},
129
130{{18.1312339, 31.6473732}, {95.5711034, 63.5350219}, {92.3283165, 62.0158945}, {18.5656052, 32.1268808}},
131{{97.402018, 35.7169972}, {33.1127443, 25.8935163}, {1.13970027, 54.9424981}, {56.4860195, 60.529264}},
132};
133
134const size_t testSetCount = sizeof(testSet) / sizeof(testSet[0]);
135
caryclark@google.com47d73da2013-02-17 01:41:25 +0000136static const Cubic newTestSet[] = {
137{{0,2},{0,1},{3,0},{1,0}},
138{{0,3},{0,1},{2,0},{1,0}},
139};
140
141const size_t newTestSetCount = sizeof(newTestSet) / sizeof(newTestSet[0]);
142
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000143static void oneOff(const Cubic& cubic1, const Cubic& cubic2) {
144 SkTDArray<Quadratic> quads1;
145 cubic_to_quadratics(cubic1, calcPrecision(cubic1), quads1);
caryclark@google.com9f602912013-01-24 21:47:16 +0000146#if ONE_OFF_DEBUG
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000147 for (int index = 0; index < quads1.count(); ++index) {
148 const Quadratic& q = quads1[index];
149 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", q[0].x, q[0].y,
150 q[1].x, q[1].y, q[2].x, q[2].y);
151 }
152 SkDebugf("\n");
caryclark@google.com9f602912013-01-24 21:47:16 +0000153#endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000154 SkTDArray<Quadratic> quads2;
155 cubic_to_quadratics(cubic2, calcPrecision(cubic2), quads2);
caryclark@google.com9f602912013-01-24 21:47:16 +0000156#if ONE_OFF_DEBUG
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000157 for (int index = 0; index < quads2.count(); ++index) {
158 const Quadratic& q = quads2[index];
159 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", q[0].x, q[0].y,
160 q[1].x, q[1].y, q[2].x, q[2].y);
161 }
162 SkDebugf("\n");
caryclark@google.com9f602912013-01-24 21:47:16 +0000163#endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000164 Intersections intersections2, intersections3;
165 intersect2(cubic1, cubic2, intersections2);
166 intersect3(cubic1, cubic2, intersections3);
167 int pt1, pt2, pt3;
168 bool found;
169 double tt1, tt2, last = -1;
170 _Point xy1, xy2;
171 for (pt1 = 0; pt1 < intersections2.used(); ++pt1) {
172 tt1 = intersections2.fT[0][pt1];
173 SkASSERT(!approximately_equal(last, tt1));
174 last = tt1;
175 xy_at_t(cubic1, tt1, xy1.x, xy1.y);
176 pt2 = intersections2.fFlip ? intersections2.used() - pt1 - 1 : pt1;
177 tt2 = intersections2.fT[1][pt2];
178 xy_at_t(cubic2, tt2, xy2.x, xy2.y);
179#if ONE_OFF_DEBUG
180 SkDebugf("%s t1=%1.9g (%1.9g, %1.9g) (%1.9g, %1.9g) (%1.9g, %1.9g) t2=%1.9g\n",
181 __FUNCTION__, tt1, xy1.x, xy1.y, intersections2.fPt[pt1].x,
182 intersections2.fPt[pt1].y, xy2.x, xy2.y, tt2);
183#endif
184 SkASSERT(xy1.approximatelyEqual(xy2));
185#if SK_DEBUG
186 found = false;
187 for (pt3 = 0; pt3 < intersections3.used(); ++pt3) {
188 if (roughly_equal(tt1, intersections3.fT[0][pt3])) {
189 found = true;
190 break;
191 }
192 }
193 SkASSERT(found);
194#endif
195 }
196 last = -1;
197 for (pt3 = 0; pt3 < intersections3.used(); ++pt3) {
198 found = false;
199 double tt3 = intersections3.fT[0][pt3];
200 SkASSERT(!approximately_equal(last, tt3));
201 last = tt3;
202 for (pt1 = 0; pt1 < intersections2.used(); ++pt1) {
203 if (approximately_equal(tt3, intersections2.fT[0][pt1])) {
204 found = true;
205 break;
206 }
207 }
208 if (!found) {
209 tt1 = intersections3.fT[0][pt3];
210 xy_at_t(cubic1, tt1, xy1.x, xy1.y);
211 pt2 = intersections3.fFlip ? intersections3.used() - pt3 - 1 : pt3;
212 tt2 = intersections3.fT[1][pt2];
213 xy_at_t(cubic2, tt2, xy2.x, xy2.y);
214 #if ONE_OFF_DEBUG
215 SkDebugf("%s t3=%1.9g (%1.9g, %1.9g) (%1.9g, %1.9g) (%1.9g, %1.9g) t2=%1.9g\n",
216 __FUNCTION__, tt1, xy1.x, xy1.y, intersections3.fPt[pt1].x,
217 intersections3.fPt[pt1].y, xy2.x, xy2.y, tt2);
218 #endif
219 SkASSERT(xy1.approximatelyEqual(xy2));
220 SkDebugf("%s missing in intersect2\n", __FUNCTION__);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000221 }
222 }
223}
224
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000225static void oneOff3(const Cubic& cubic1, const Cubic& cubic2) {
226 SkTDArray<Quadratic> quads1;
227 cubic_to_quadratics(cubic1, calcPrecision(cubic1), quads1);
228#if ONE_OFF_DEBUG
229 for (int index = 0; index < quads1.count(); ++index) {
230 const Quadratic& q = quads1[index];
231 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", q[0].x, q[0].y,
232 q[1].x, q[1].y, q[2].x, q[2].y);
233 }
234 SkDebugf("\n");
235#endif
236 SkTDArray<Quadratic> quads2;
237 cubic_to_quadratics(cubic2, calcPrecision(cubic2), quads2);
238#if ONE_OFF_DEBUG
239 for (int index = 0; index < quads2.count(); ++index) {
240 const Quadratic& q = quads2[index];
241 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", q[0].x, q[0].y,
242 q[1].x, q[1].y, q[2].x, q[2].y);
243 }
244 SkDebugf("\n");
245#endif
246 Intersections intersections3;
247 intersect3(cubic1, cubic2, intersections3);
248 int pt2, pt3;
249 double tt1, tt2, last = -1;
250 _Point xy1, xy2;
251 for (pt3 = 0; pt3 < intersections3.used(); ++pt3) {
252 double tt3 = intersections3.fT[0][pt3];
253 SkASSERT(!approximately_equal(last, tt3));
254 last = tt3;
255 tt1 = intersections3.fT[0][pt3];
256 xy_at_t(cubic1, tt1, xy1.x, xy1.y);
257 pt2 = intersections3.fFlip ? intersections3.used() - pt3 - 1 : pt3;
258 tt2 = intersections3.fT[1][pt2];
259 xy_at_t(cubic2, tt2, xy2.x, xy2.y);
260#if ONE_OFF_DEBUG
261 SkDebugf("%s t3=%1.9g (%1.9g, %1.9g) (%1.9g, %1.9g) (%1.9g, %1.9g) t2=%1.9g\n",
262 __FUNCTION__, tt1, xy1.x, xy1.y, intersections3.fPt[pt3].x,
263 intersections3.fPt[pt3].y, xy2.x, xy2.y, tt2);
264#endif
265 SkASSERT(xy1.approximatelyEqual(xy2));
266 }
267}
268
269static int fails[][2] = { {0, 23}, // fails in intersect2 recursing
270 {2, 7}, // answers differ, but neither is correct ('3' is closer)
271 {3, 26}, // fails in intersect2 recursing
272 {4, 9}, // fails in intersect2 recursing
273 {4, 10}, // fails in intersect2 recursing
274 {10, 17}, // fails in intersect2 recursing
275 {12, 14}, // loops indefinitely
276 {12, 21}, // fails in intersect2 recursing
277 {13, 21}, // fails in intersect2 recursing
278 {14, 21}, // fails in intersect2 recursing
279 {17, 25}, // fails in intersect2 recursing
280 {23, 25}, // fails in intersect2 recursing
281};
282
283static int failCount = sizeof(fails) / sizeof(fails[0]);
284
285static void oneOff(int outer, int inner) {
286 const Cubic& cubic1 = testSet[outer];
287 const Cubic& cubic2 = testSet[inner];
288 bool failing = false;
289 for (int i = 0; i < failCount; ++i) {
290 if ((fails[i][0] == outer && fails[i][1] == inner)
291 || (fails[i][1] == outer && fails[i][0] == inner)) {
292 failing = true;
293 break;
294 }
295 }
296 if (!failing) {
297 oneOff(cubic1, cubic2);
298 } else {
299 oneOff3(cubic1, cubic2);
300 }
301}
302
303void CubicIntersection_OneOffTest() {
304 oneOff(12, 14);
305}
306
caryclark@google.com47d73da2013-02-17 01:41:25 +0000307static void newOneOff(int outer, int inner) {
308 const Cubic& cubic1 = newTestSet[outer];
309 const Cubic& cubic2 = newTestSet[inner];
310 oneOff3(cubic1, cubic2);
311}
312
313void CubicIntersection_NewOneOffTest() {
314 newOneOff(0, 1);
315}
316
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000317static void oneOffTests() {
318 for (size_t outer = 0; outer < testSetCount - 1; ++outer) {
319 for (size_t inner = outer + 1; inner < testSetCount; ++inner) {
320 oneOff(outer, inner);
321 }
322 }
323}
324
325void CubicIntersection_OneOffTests() {
326 oneOffTests();
327}
328
caryclark@google.comf9502d72013-02-04 14:06:49 +0000329#define DEBUG_CRASH 0
caryclark@google.com73ca6242013-01-17 21:02:47 +0000330
331class CubicChopper {
332public:
333
334// only finds one intersection
335CubicChopper(const Cubic& c1, const Cubic& c2)
336 : cubic1(c1)
337 , cubic2(c2)
338 , depth(0) {
339}
340
341bool intersect(double minT1, double maxT1, double minT2, double maxT2) {
342 Cubic sub1, sub2;
343 // FIXME: carry last subdivide and reduceOrder result with cubic
344 sub_divide(cubic1, minT1, maxT1, sub1);
345 sub_divide(cubic2, minT2, maxT2, sub2);
346 Intersections i;
347 intersect2(sub1, sub2, i);
348 if (i.used() == 0) {
349 return false;
350 }
351 double x1, y1, x2, y2;
352 t1 = minT1 + i.fT[0][0] * (maxT1 - minT1);
353 t2 = minT2 + i.fT[1][0] * (maxT2 - minT2);
354 xy_at_t(cubic1, t1, x1, y1);
355 xy_at_t(cubic2, t2, x2, y2);
356 if (AlmostEqualUlps(x1, x2) && AlmostEqualUlps(y1, y2)) {
357 return true;
358 }
359 double half1 = (minT1 + maxT1) / 2;
360 double half2 = (minT2 + maxT2) / 2;
361 ++depth;
362 bool result;
363 if (depth & 1) {
364 result = intersect(minT1, half1, minT2, maxT2) || intersect(half1, maxT1, minT2, maxT2)
365 || intersect(minT1, maxT1, minT2, half2) || intersect(minT1, maxT1, half2, maxT2);
366 } else {
367 result = intersect(minT1, maxT1, minT2, half2) || intersect(minT1, maxT1, half2, maxT2)
368 || intersect(minT1, half1, minT2, maxT2) || intersect(half1, maxT1, minT2, maxT2);
369 }
370 --depth;
371 return result;
372}
373
374const Cubic& cubic1;
375const Cubic& cubic2;
376double t1;
377double t2;
378int depth;
379};
380
381#define TRY_OLD 0 // old way fails on test == 1
382
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000383void CubicIntersection_RandTestOld() {
caryclark@google.com73ca6242013-01-17 21:02:47 +0000384 srand(0);
385 const int tests = 1000000; // 10000000;
386 double largestFactor = DBL_MAX;
387 for (int test = 0; test < tests; ++test) {
388 Cubic cubic1, cubic2;
389 for (int i = 0; i < 4; ++i) {
390 cubic1[i].x = (double) rand() / RAND_MAX * 100;
391 cubic1[i].y = (double) rand() / RAND_MAX * 100;
392 cubic2[i].x = (double) rand() / RAND_MAX * 100;
393 cubic2[i].y = (double) rand() / RAND_MAX * 100;
394 }
395 if (test == 2513) { // the pair crosses three times, but the quadratic approximation
396 continue; // only sees one -- should be OK to ignore the other two?
397 }
398 if (test == 12932) { // this exposes a weakness when one cubic touches the other but
399 continue; // does not touch the quad approximation. Captured in qc.htm as cubic15
400 }
401 #if DEBUG_CRASH
402 char str[1024];
403 sprintf(str, "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n"
404 "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n",
405 cubic1[0].x, cubic1[0].y, cubic1[1].x, cubic1[1].y, cubic1[2].x, cubic1[2].y,
406 cubic1[3].x, cubic1[3].y,
407 cubic2[0].x, cubic2[0].y, cubic2[1].x, cubic2[1].y, cubic2[2].x, cubic2[2].y,
408 cubic2[3].x, cubic2[3].y);
409 #endif
410 _Rect rect1, rect2;
411 rect1.setBounds(cubic1);
412 rect2.setBounds(cubic2);
413 bool boundsIntersect = rect1.left <= rect2.right && rect2.left <= rect2.right
414 && rect1.top <= rect2.bottom && rect2.top <= rect1.bottom;
415 Intersections i1, i2;
416 #if TRY_OLD
417 bool oldIntersects = intersect(cubic1, cubic2, i1);
418 #else
419 bool oldIntersects = false;
420 #endif
421 if (test == -1) {
422 SkDebugf("ready...\n");
423 }
424 bool newIntersects = intersect2(cubic1, cubic2, i2);
425 if (!boundsIntersect && (oldIntersects || newIntersects)) {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000426 #if DEBUG_CRASH
caryclark@google.com73ca6242013-01-17 21:02:47 +0000427 SkDebugf("%s %d unexpected intersection boundsIntersect=%d oldIntersects=%d"
428 " newIntersects=%d\n%s %s\n", __FUNCTION__, test, boundsIntersect,
429 oldIntersects, newIntersects, __FUNCTION__, str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000430 #endif
caryclark@google.comaa358312013-01-29 20:28:49 +0000431 SkASSERT(0);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000432 }
433 if (oldIntersects && !newIntersects) {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000434 #if DEBUG_CRASH
caryclark@google.com73ca6242013-01-17 21:02:47 +0000435 SkDebugf("%s %d missing intersection oldIntersects=%d newIntersects=%d\n%s %s\n",
436 __FUNCTION__, test, oldIntersects, newIntersects, __FUNCTION__, str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000437 #endif
caryclark@google.comaa358312013-01-29 20:28:49 +0000438 SkASSERT(0);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000439 }
440 if (!oldIntersects && !newIntersects) {
441 continue;
442 }
443 if (i2.used() > 1) {
444 continue;
445 // just look at single intercepts for simplicity
446 }
447 Intersections self1, self2; // self-intersect checks
448 if (intersect(cubic1, self1)) {
449 continue;
450 }
451 if (intersect(cubic2, self2)) {
452 continue;
453 }
454 // binary search for range necessary to enclose real intersection
455 CubicChopper c(cubic1, cubic2);
456 bool result = c.intersect(0, 1, 0, 1);
457 if (!result) {
458 // FIXME: a failure here probably means that a core routine used by CubicChopper is failing
459 continue;
460 }
461 double delta1 = fabs(c.t1 - i2.fT[0][0]);
462 double delta2 = fabs(c.t2 - i2.fT[1][0]);
463 double calc1 = calcPrecision(cubic1);
464 double calc2 = calcPrecision(cubic2);
465 double factor1 = calc1 / delta1;
466 double factor2 = calc2 / delta2;
467 SkDebugf("%s %d calc1=%1.9g delta1=%1.9g factor1=%1.9g calc2=%1.9g delta2=%1.9g"
468 " factor2=%1.9g\n", __FUNCTION__, test,
469 calc1, delta1, factor1, calc2, delta2, factor2);
470 if (factor1 < largestFactor) {
471 SkDebugf("WE HAVE A WINNER! %1.9g\n", factor1);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000472 #if DEBUG_CRASH
caryclark@google.com73ca6242013-01-17 21:02:47 +0000473 SkDebugf("%s\n", str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000474 #endif
caryclark@google.com73ca6242013-01-17 21:02:47 +0000475 oneOff(cubic1, cubic2);
476 largestFactor = factor1;
477 }
478 if (factor2 < largestFactor) {
479 SkDebugf("WE HAVE A WINNER! %1.9g\n", factor2);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000480 #if DEBUG_CRASH
caryclark@google.com73ca6242013-01-17 21:02:47 +0000481 SkDebugf("%s\n", str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000482 #endif
caryclark@google.com73ca6242013-01-17 21:02:47 +0000483 oneOff(cubic1, cubic2);
484 largestFactor = factor2;
485 }
486 }
487}
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000488
489void CubicIntersection_RandTest() {
490 srand(0);
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000491 const int tests = 10000000;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000492 for (int test = 0; test < tests; ++test) {
493 Cubic cubic1, cubic2;
494 for (int i = 0; i < 4; ++i) {
495 cubic1[i].x = (double) rand() / RAND_MAX * 100;
496 cubic1[i].y = (double) rand() / RAND_MAX * 100;
497 cubic2[i].x = (double) rand() / RAND_MAX * 100;
498 cubic2[i].y = (double) rand() / RAND_MAX * 100;
499 }
500 #if DEBUG_CRASH
501 char str[1024];
502 sprintf(str, "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n"
503 "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n",
504 cubic1[0].x, cubic1[0].y, cubic1[1].x, cubic1[1].y, cubic1[2].x, cubic1[2].y,
505 cubic1[3].x, cubic1[3].y,
506 cubic2[0].x, cubic2[0].y, cubic2[1].x, cubic2[1].y, cubic2[2].x, cubic2[2].y,
507 cubic2[3].x, cubic2[3].y);
508 #endif
509 _Rect rect1, rect2;
510 rect1.setBounds(cubic1);
511 rect2.setBounds(cubic2);
512 bool boundsIntersect = rect1.left <= rect2.right && rect2.left <= rect2.right
513 && rect1.top <= rect2.bottom && rect2.top <= rect1.bottom;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000514 if (test == -1) {
515 SkDebugf("ready...\n");
516 }
517 Intersections intersections2;
518 bool newIntersects = intersect2(cubic1, cubic2, intersections2);
519 if (!boundsIntersect && newIntersects) {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000520 #if DEBUG_CRASH
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000521 SkDebugf("%s %d unexpected intersection boundsIntersect=%d "
522 " newIntersects=%d\n%s %s\n", __FUNCTION__, test, boundsIntersect,
523 newIntersects, __FUNCTION__, str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000524 #endif
caryclark@google.comaa358312013-01-29 20:28:49 +0000525 SkASSERT(0);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000526 }
527 for (int pt = 0; pt < intersections2.used(); ++pt) {
528 double tt1 = intersections2.fT[0][pt];
529 _Point xy1, xy2;
530 xy_at_t(cubic1, tt1, xy1.x, xy1.y);
531 int pt2 = intersections2.fFlip ? intersections2.used() - pt - 1 : pt;
532 double tt2 = intersections2.fT[1][pt2];
533 xy_at_t(cubic2, tt2, xy2.x, xy2.y);
caryclark@google.com9f602912013-01-24 21:47:16 +0000534 #if 0
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000535 SkDebugf("%s t1=%1.9g (%1.9g, %1.9g) (%1.9g, %1.9g) t2=%1.9g\n", __FUNCTION__,
536 tt1, xy1.x, xy1.y, xy2.x, xy2.y, tt2);
caryclark@google.com9f602912013-01-24 21:47:16 +0000537 #endif
caryclark@google.comaa358312013-01-29 20:28:49 +0000538 SkASSERT(xy1.approximatelyEqual(xy2));
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000539 }
540 }
541}
caryclark@google.com9f602912013-01-24 21:47:16 +0000542
caryclark@google.comf9502d72013-02-04 14:06:49 +0000543void CubicIntersection_IntersectionFinder() {
caryclark@google.com47d73da2013-02-17 01:41:25 +0000544 const Cubic& cubic1 = newTestSet[0];
545 const Cubic& cubic2 = newTestSet[1];
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000546
caryclark@google.com47d73da2013-02-17 01:41:25 +0000547 double t1Seed = 0.99;
548 double t2Seed = 0.99;
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000549 double t1Step = 0.01;
550 double t2Step = 0.01;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000551 _Point t1[3], t2[3];
552 bool toggle = true;
553 do {
554 xy_at_t(cubic1, t1Seed - t1Step, t1[0].x, t1[0].y);
555 xy_at_t(cubic1, t1Seed, t1[1].x, t1[1].y);
556 xy_at_t(cubic1, t1Seed + t1Step, t1[2].x, t1[2].y);
557 xy_at_t(cubic2, t2Seed - t2Step, t2[0].x, t2[0].y);
558 xy_at_t(cubic2, t2Seed, t2[1].x, t2[1].y);
559 xy_at_t(cubic2, t2Seed + t2Step, t2[2].x, t2[2].y);
560 double dist[3][3];
561 dist[1][1] = t1[1].distance(t2[1]);
562 int best_i = 1, best_j = 1;
563 for (int i = 0; i < 3; ++i) {
564 for (int j = 0; j < 3; ++j) {
565 if (i == 1 && j == 1) {
566 continue;
567 }
568 dist[i][j] = t1[i].distance(t2[j]);
569 if (dist[best_i][best_j] > dist[i][j]) {
570 best_i = i;
571 best_j = j;
572 }
573 }
574 }
575 if (best_i == 0) {
576 t1Seed -= t1Step;
577 } else if (best_i == 2) {
578 t1Seed += t1Step;
579 }
580 if (best_j == 0) {
581 t2Seed -= t2Step;
582 } else if (best_j == 2) {
583 t2Seed += t2Step;
584 }
585 if (best_i == 1 && best_j == 1) {
586 if ((toggle ^= true)) {
587 t1Step /= 2;
588 } else {
589 t2Step /= 2;
590 }
591 }
592 } while (!t1[1].approximatelyEqual(t2[1]));
593 t1Step = t2Step = 0.1;
594 double t10 = t1Seed - t1Step * 2;
595 double t12 = t1Seed + t1Step * 2;
596 double t20 = t2Seed - t2Step * 2;
597 double t22 = t2Seed + t2Step * 2;
598 _Point test;
599 while (!approximately_zero(t1Step)) {
600 xy_at_t(cubic1, t10, test.x, test.y);
601 t10 += t1[1].approximatelyEqual(test) ? -t1Step : t1Step;
602 t1Step /= 2;
caryclark@google.com9f602912013-01-24 21:47:16 +0000603 }
caryclark@google.comf9502d72013-02-04 14:06:49 +0000604 t1Step = 0.1;
605 while (!approximately_zero(t1Step)) {
606 xy_at_t(cubic1, t12, test.x, test.y);
607 t12 -= t1[1].approximatelyEqual(test) ? -t1Step : t1Step;
608 t1Step /= 2;
609 }
610 while (!approximately_zero(t2Step)) {
611 xy_at_t(cubic2, t20, test.x, test.y);
612 t20 += t2[1].approximatelyEqual(test) ? -t2Step : t2Step;
613 t2Step /= 2;
614 }
615 t2Step = 0.1;
616 while (!approximately_zero(t2Step)) {
617 xy_at_t(cubic2, t22, test.x, test.y);
618 t22 -= t2[1].approximatelyEqual(test) ? -t2Step : t2Step;
619 t2Step /= 2;
620 }
621 SkDebugf("%s t1=(%1.9g<%1.9g<%1.9g) t2=(%1.9g<%1.9g<%1.9g)\n", __FUNCTION__,
622 t10, t1Seed, t12, t20, t2Seed, t22);
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000623 _Point p10 = xy_at_t(cubic1, t10);
624 _Point p1Seed = xy_at_t(cubic1, t1Seed);
625 _Point p12 = xy_at_t(cubic1, t12);
626 SkDebugf("%s p1=(%1.9g,%1.9g)<(%1.9g,%1.9g)<(%1.9g,%1.9g)\n", __FUNCTION__,
627 p10.x, p10.y, p1Seed.x, p1Seed.y, p12.x, p12.y);
628 _Point p20 = xy_at_t(cubic2, t20);
629 _Point p2Seed = xy_at_t(cubic2, t2Seed);
630 _Point p22 = xy_at_t(cubic2, t22);
631 SkDebugf("%s p2=(%1.9g,%1.9g)<(%1.9g,%1.9g)<(%1.9g,%1.9g)\n", __FUNCTION__,
632 p20.x, p20.y, p2Seed.x, p2Seed.y, p22.x, p22.y);
caryclark@google.com9f602912013-01-24 21:47:16 +0000633}
caryclark@google.comf9502d72013-02-04 14:06:49 +0000634
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000635static void coincidentTest() {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000636#if 0
caryclark@google.comf9502d72013-02-04 14:06:49 +0000637 Cubic cubic1 = {{0, 1}, {0, 2}, {1, 0}, {1, 0}};
638 Cubic cubic2 = {{0, 1}, {0, 2}, {1, 0}, {6, 1}};
caryclark@google.comf9502d72013-02-04 14:06:49 +0000639#endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000640}
641
642void CubicIntersection_Test() {
643 oneOffTests();
644 coincidentTest();
645 standardTestCases();
646}