blob: 08bffd61bfcf4637257d3a79464dfc84c860e58d [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[] = {
caryclark@google.com5e0500f2013-02-20 12:51:37 +0000137{{1,2},{5,6},{1,0},{1,0}},
138{{0,1},{0,1},{2,1},{6,5}},
139
140{{0,6},{1,2},{1,0},{1,0}},
141{{0,1},{0,1},{6,0},{2,1}},
142
caryclark@google.com47d73da2013-02-17 01:41:25 +0000143{{0,2},{0,1},{3,0},{1,0}},
144{{0,3},{0,1},{2,0},{1,0}},
145};
146
147const size_t newTestSetCount = sizeof(newTestSet) / sizeof(newTestSet[0]);
148
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000149static void oneOff(const Cubic& cubic1, const Cubic& cubic2) {
150 SkTDArray<Quadratic> quads1;
151 cubic_to_quadratics(cubic1, calcPrecision(cubic1), quads1);
caryclark@google.com9f602912013-01-24 21:47:16 +0000152#if ONE_OFF_DEBUG
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000153 for (int index = 0; index < quads1.count(); ++index) {
154 const Quadratic& q = quads1[index];
155 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", q[0].x, q[0].y,
156 q[1].x, q[1].y, q[2].x, q[2].y);
157 }
158 SkDebugf("\n");
caryclark@google.com9f602912013-01-24 21:47:16 +0000159#endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000160 SkTDArray<Quadratic> quads2;
161 cubic_to_quadratics(cubic2, calcPrecision(cubic2), quads2);
caryclark@google.com9f602912013-01-24 21:47:16 +0000162#if ONE_OFF_DEBUG
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000163 for (int index = 0; index < quads2.count(); ++index) {
164 const Quadratic& q = quads2[index];
165 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", q[0].x, q[0].y,
166 q[1].x, q[1].y, q[2].x, q[2].y);
167 }
168 SkDebugf("\n");
caryclark@google.com9f602912013-01-24 21:47:16 +0000169#endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000170 Intersections intersections2, intersections3;
171 intersect2(cubic1, cubic2, intersections2);
172 intersect3(cubic1, cubic2, intersections3);
173 int pt1, pt2, pt3;
174 bool found;
175 double tt1, tt2, last = -1;
176 _Point xy1, xy2;
177 for (pt1 = 0; pt1 < intersections2.used(); ++pt1) {
178 tt1 = intersections2.fT[0][pt1];
179 SkASSERT(!approximately_equal(last, tt1));
180 last = tt1;
181 xy_at_t(cubic1, tt1, xy1.x, xy1.y);
182 pt2 = intersections2.fFlip ? intersections2.used() - pt1 - 1 : pt1;
183 tt2 = intersections2.fT[1][pt2];
184 xy_at_t(cubic2, tt2, xy2.x, xy2.y);
185#if ONE_OFF_DEBUG
186 SkDebugf("%s t1=%1.9g (%1.9g, %1.9g) (%1.9g, %1.9g) (%1.9g, %1.9g) t2=%1.9g\n",
187 __FUNCTION__, tt1, xy1.x, xy1.y, intersections2.fPt[pt1].x,
188 intersections2.fPt[pt1].y, xy2.x, xy2.y, tt2);
189#endif
190 SkASSERT(xy1.approximatelyEqual(xy2));
191#if SK_DEBUG
192 found = false;
193 for (pt3 = 0; pt3 < intersections3.used(); ++pt3) {
194 if (roughly_equal(tt1, intersections3.fT[0][pt3])) {
195 found = true;
196 break;
197 }
198 }
199 SkASSERT(found);
200#endif
201 }
202 last = -1;
203 for (pt3 = 0; pt3 < intersections3.used(); ++pt3) {
204 found = false;
205 double tt3 = intersections3.fT[0][pt3];
206 SkASSERT(!approximately_equal(last, tt3));
207 last = tt3;
208 for (pt1 = 0; pt1 < intersections2.used(); ++pt1) {
209 if (approximately_equal(tt3, intersections2.fT[0][pt1])) {
210 found = true;
211 break;
212 }
213 }
214 if (!found) {
215 tt1 = intersections3.fT[0][pt3];
216 xy_at_t(cubic1, tt1, xy1.x, xy1.y);
217 pt2 = intersections3.fFlip ? intersections3.used() - pt3 - 1 : pt3;
218 tt2 = intersections3.fT[1][pt2];
219 xy_at_t(cubic2, tt2, xy2.x, xy2.y);
220 #if ONE_OFF_DEBUG
221 SkDebugf("%s t3=%1.9g (%1.9g, %1.9g) (%1.9g, %1.9g) (%1.9g, %1.9g) t2=%1.9g\n",
222 __FUNCTION__, tt1, xy1.x, xy1.y, intersections3.fPt[pt1].x,
223 intersections3.fPt[pt1].y, xy2.x, xy2.y, tt2);
224 #endif
225 SkASSERT(xy1.approximatelyEqual(xy2));
226 SkDebugf("%s missing in intersect2\n", __FUNCTION__);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000227 }
228 }
229}
230
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000231static void oneOff3(const Cubic& cubic1, const Cubic& cubic2) {
232 SkTDArray<Quadratic> quads1;
233 cubic_to_quadratics(cubic1, calcPrecision(cubic1), quads1);
234#if ONE_OFF_DEBUG
235 for (int index = 0; index < quads1.count(); ++index) {
236 const Quadratic& q = quads1[index];
237 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", q[0].x, q[0].y,
238 q[1].x, q[1].y, q[2].x, q[2].y);
239 }
240 SkDebugf("\n");
241#endif
242 SkTDArray<Quadratic> quads2;
243 cubic_to_quadratics(cubic2, calcPrecision(cubic2), quads2);
244#if ONE_OFF_DEBUG
245 for (int index = 0; index < quads2.count(); ++index) {
246 const Quadratic& q = quads2[index];
247 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", q[0].x, q[0].y,
248 q[1].x, q[1].y, q[2].x, q[2].y);
249 }
250 SkDebugf("\n");
251#endif
252 Intersections intersections3;
253 intersect3(cubic1, cubic2, intersections3);
254 int pt2, pt3;
255 double tt1, tt2, last = -1;
256 _Point xy1, xy2;
257 for (pt3 = 0; pt3 < intersections3.used(); ++pt3) {
258 double tt3 = intersections3.fT[0][pt3];
259 SkASSERT(!approximately_equal(last, tt3));
260 last = tt3;
261 tt1 = intersections3.fT[0][pt3];
262 xy_at_t(cubic1, tt1, xy1.x, xy1.y);
263 pt2 = intersections3.fFlip ? intersections3.used() - pt3 - 1 : pt3;
264 tt2 = intersections3.fT[1][pt2];
265 xy_at_t(cubic2, tt2, xy2.x, xy2.y);
266#if ONE_OFF_DEBUG
267 SkDebugf("%s t3=%1.9g (%1.9g, %1.9g) (%1.9g, %1.9g) (%1.9g, %1.9g) t2=%1.9g\n",
268 __FUNCTION__, tt1, xy1.x, xy1.y, intersections3.fPt[pt3].x,
269 intersections3.fPt[pt3].y, xy2.x, xy2.y, tt2);
270#endif
271 SkASSERT(xy1.approximatelyEqual(xy2));
272 }
273}
274
275static int fails[][2] = { {0, 23}, // fails in intersect2 recursing
276 {2, 7}, // answers differ, but neither is correct ('3' is closer)
277 {3, 26}, // fails in intersect2 recursing
278 {4, 9}, // fails in intersect2 recursing
279 {4, 10}, // fails in intersect2 recursing
280 {10, 17}, // fails in intersect2 recursing
281 {12, 14}, // loops indefinitely
282 {12, 21}, // fails in intersect2 recursing
283 {13, 21}, // fails in intersect2 recursing
284 {14, 21}, // fails in intersect2 recursing
285 {17, 25}, // fails in intersect2 recursing
286 {23, 25}, // fails in intersect2 recursing
287};
288
289static int failCount = sizeof(fails) / sizeof(fails[0]);
290
291static void oneOff(int outer, int inner) {
292 const Cubic& cubic1 = testSet[outer];
293 const Cubic& cubic2 = testSet[inner];
294 bool failing = false;
295 for (int i = 0; i < failCount; ++i) {
296 if ((fails[i][0] == outer && fails[i][1] == inner)
297 || (fails[i][1] == outer && fails[i][0] == inner)) {
298 failing = true;
299 break;
300 }
301 }
302 if (!failing) {
303 oneOff(cubic1, cubic2);
304 } else {
305 oneOff3(cubic1, cubic2);
306 }
307}
308
309void CubicIntersection_OneOffTest() {
310 oneOff(12, 14);
311}
312
caryclark@google.com47d73da2013-02-17 01:41:25 +0000313static void newOneOff(int outer, int inner) {
314 const Cubic& cubic1 = newTestSet[outer];
315 const Cubic& cubic2 = newTestSet[inner];
316 oneOff3(cubic1, cubic2);
317}
318
319void CubicIntersection_NewOneOffTest() {
320 newOneOff(0, 1);
321}
322
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000323static void oneOffTests() {
324 for (size_t outer = 0; outer < testSetCount - 1; ++outer) {
325 for (size_t inner = outer + 1; inner < testSetCount; ++inner) {
326 oneOff(outer, inner);
327 }
328 }
329}
330
331void CubicIntersection_OneOffTests() {
332 oneOffTests();
333}
334
caryclark@google.comf9502d72013-02-04 14:06:49 +0000335#define DEBUG_CRASH 0
caryclark@google.com73ca6242013-01-17 21:02:47 +0000336
337class CubicChopper {
338public:
339
340// only finds one intersection
341CubicChopper(const Cubic& c1, const Cubic& c2)
342 : cubic1(c1)
343 , cubic2(c2)
344 , depth(0) {
345}
346
347bool intersect(double minT1, double maxT1, double minT2, double maxT2) {
348 Cubic sub1, sub2;
349 // FIXME: carry last subdivide and reduceOrder result with cubic
350 sub_divide(cubic1, minT1, maxT1, sub1);
351 sub_divide(cubic2, minT2, maxT2, sub2);
352 Intersections i;
353 intersect2(sub1, sub2, i);
354 if (i.used() == 0) {
355 return false;
356 }
357 double x1, y1, x2, y2;
358 t1 = minT1 + i.fT[0][0] * (maxT1 - minT1);
359 t2 = minT2 + i.fT[1][0] * (maxT2 - minT2);
360 xy_at_t(cubic1, t1, x1, y1);
361 xy_at_t(cubic2, t2, x2, y2);
362 if (AlmostEqualUlps(x1, x2) && AlmostEqualUlps(y1, y2)) {
363 return true;
364 }
365 double half1 = (minT1 + maxT1) / 2;
366 double half2 = (minT2 + maxT2) / 2;
367 ++depth;
368 bool result;
369 if (depth & 1) {
370 result = intersect(minT1, half1, minT2, maxT2) || intersect(half1, maxT1, minT2, maxT2)
371 || intersect(minT1, maxT1, minT2, half2) || intersect(minT1, maxT1, half2, maxT2);
372 } else {
373 result = intersect(minT1, maxT1, minT2, half2) || intersect(minT1, maxT1, half2, maxT2)
374 || intersect(minT1, half1, minT2, maxT2) || intersect(half1, maxT1, minT2, maxT2);
375 }
376 --depth;
377 return result;
378}
379
380const Cubic& cubic1;
381const Cubic& cubic2;
382double t1;
383double t2;
384int depth;
385};
386
387#define TRY_OLD 0 // old way fails on test == 1
388
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000389void CubicIntersection_RandTestOld() {
caryclark@google.com73ca6242013-01-17 21:02:47 +0000390 srand(0);
391 const int tests = 1000000; // 10000000;
392 double largestFactor = DBL_MAX;
393 for (int test = 0; test < tests; ++test) {
394 Cubic cubic1, cubic2;
395 for (int i = 0; i < 4; ++i) {
396 cubic1[i].x = (double) rand() / RAND_MAX * 100;
397 cubic1[i].y = (double) rand() / RAND_MAX * 100;
398 cubic2[i].x = (double) rand() / RAND_MAX * 100;
399 cubic2[i].y = (double) rand() / RAND_MAX * 100;
400 }
401 if (test == 2513) { // the pair crosses three times, but the quadratic approximation
402 continue; // only sees one -- should be OK to ignore the other two?
403 }
404 if (test == 12932) { // this exposes a weakness when one cubic touches the other but
405 continue; // does not touch the quad approximation. Captured in qc.htm as cubic15
406 }
407 #if DEBUG_CRASH
408 char str[1024];
409 sprintf(str, "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n"
410 "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n",
411 cubic1[0].x, cubic1[0].y, cubic1[1].x, cubic1[1].y, cubic1[2].x, cubic1[2].y,
412 cubic1[3].x, cubic1[3].y,
413 cubic2[0].x, cubic2[0].y, cubic2[1].x, cubic2[1].y, cubic2[2].x, cubic2[2].y,
414 cubic2[3].x, cubic2[3].y);
415 #endif
416 _Rect rect1, rect2;
417 rect1.setBounds(cubic1);
418 rect2.setBounds(cubic2);
419 bool boundsIntersect = rect1.left <= rect2.right && rect2.left <= rect2.right
420 && rect1.top <= rect2.bottom && rect2.top <= rect1.bottom;
421 Intersections i1, i2;
422 #if TRY_OLD
423 bool oldIntersects = intersect(cubic1, cubic2, i1);
424 #else
425 bool oldIntersects = false;
426 #endif
427 if (test == -1) {
428 SkDebugf("ready...\n");
429 }
430 bool newIntersects = intersect2(cubic1, cubic2, i2);
431 if (!boundsIntersect && (oldIntersects || newIntersects)) {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000432 #if DEBUG_CRASH
caryclark@google.com73ca6242013-01-17 21:02:47 +0000433 SkDebugf("%s %d unexpected intersection boundsIntersect=%d oldIntersects=%d"
434 " newIntersects=%d\n%s %s\n", __FUNCTION__, test, boundsIntersect,
435 oldIntersects, newIntersects, __FUNCTION__, str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000436 #endif
caryclark@google.comaa358312013-01-29 20:28:49 +0000437 SkASSERT(0);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000438 }
439 if (oldIntersects && !newIntersects) {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000440 #if DEBUG_CRASH
caryclark@google.com73ca6242013-01-17 21:02:47 +0000441 SkDebugf("%s %d missing intersection oldIntersects=%d newIntersects=%d\n%s %s\n",
442 __FUNCTION__, test, oldIntersects, newIntersects, __FUNCTION__, str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000443 #endif
caryclark@google.comaa358312013-01-29 20:28:49 +0000444 SkASSERT(0);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000445 }
446 if (!oldIntersects && !newIntersects) {
447 continue;
448 }
449 if (i2.used() > 1) {
450 continue;
451 // just look at single intercepts for simplicity
452 }
453 Intersections self1, self2; // self-intersect checks
454 if (intersect(cubic1, self1)) {
455 continue;
456 }
457 if (intersect(cubic2, self2)) {
458 continue;
459 }
460 // binary search for range necessary to enclose real intersection
461 CubicChopper c(cubic1, cubic2);
462 bool result = c.intersect(0, 1, 0, 1);
463 if (!result) {
464 // FIXME: a failure here probably means that a core routine used by CubicChopper is failing
465 continue;
466 }
467 double delta1 = fabs(c.t1 - i2.fT[0][0]);
468 double delta2 = fabs(c.t2 - i2.fT[1][0]);
469 double calc1 = calcPrecision(cubic1);
470 double calc2 = calcPrecision(cubic2);
471 double factor1 = calc1 / delta1;
472 double factor2 = calc2 / delta2;
473 SkDebugf("%s %d calc1=%1.9g delta1=%1.9g factor1=%1.9g calc2=%1.9g delta2=%1.9g"
474 " factor2=%1.9g\n", __FUNCTION__, test,
475 calc1, delta1, factor1, calc2, delta2, factor2);
476 if (factor1 < largestFactor) {
477 SkDebugf("WE HAVE A WINNER! %1.9g\n", factor1);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000478 #if DEBUG_CRASH
caryclark@google.com73ca6242013-01-17 21:02:47 +0000479 SkDebugf("%s\n", str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000480 #endif
caryclark@google.com73ca6242013-01-17 21:02:47 +0000481 oneOff(cubic1, cubic2);
482 largestFactor = factor1;
483 }
484 if (factor2 < largestFactor) {
485 SkDebugf("WE HAVE A WINNER! %1.9g\n", factor2);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000486 #if DEBUG_CRASH
caryclark@google.com73ca6242013-01-17 21:02:47 +0000487 SkDebugf("%s\n", str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000488 #endif
caryclark@google.com73ca6242013-01-17 21:02:47 +0000489 oneOff(cubic1, cubic2);
490 largestFactor = factor2;
491 }
492 }
493}
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000494
495void CubicIntersection_RandTest() {
496 srand(0);
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000497 const int tests = 10000000;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000498 for (int test = 0; test < tests; ++test) {
499 Cubic cubic1, cubic2;
500 for (int i = 0; i < 4; ++i) {
501 cubic1[i].x = (double) rand() / RAND_MAX * 100;
502 cubic1[i].y = (double) rand() / RAND_MAX * 100;
503 cubic2[i].x = (double) rand() / RAND_MAX * 100;
504 cubic2[i].y = (double) rand() / RAND_MAX * 100;
505 }
506 #if DEBUG_CRASH
507 char str[1024];
508 sprintf(str, "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n"
509 "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n",
510 cubic1[0].x, cubic1[0].y, cubic1[1].x, cubic1[1].y, cubic1[2].x, cubic1[2].y,
511 cubic1[3].x, cubic1[3].y,
512 cubic2[0].x, cubic2[0].y, cubic2[1].x, cubic2[1].y, cubic2[2].x, cubic2[2].y,
513 cubic2[3].x, cubic2[3].y);
514 #endif
515 _Rect rect1, rect2;
516 rect1.setBounds(cubic1);
517 rect2.setBounds(cubic2);
518 bool boundsIntersect = rect1.left <= rect2.right && rect2.left <= rect2.right
519 && rect1.top <= rect2.bottom && rect2.top <= rect1.bottom;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000520 if (test == -1) {
521 SkDebugf("ready...\n");
522 }
523 Intersections intersections2;
524 bool newIntersects = intersect2(cubic1, cubic2, intersections2);
525 if (!boundsIntersect && newIntersects) {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000526 #if DEBUG_CRASH
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000527 SkDebugf("%s %d unexpected intersection boundsIntersect=%d "
528 " newIntersects=%d\n%s %s\n", __FUNCTION__, test, boundsIntersect,
529 newIntersects, __FUNCTION__, str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000530 #endif
caryclark@google.comaa358312013-01-29 20:28:49 +0000531 SkASSERT(0);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000532 }
533 for (int pt = 0; pt < intersections2.used(); ++pt) {
534 double tt1 = intersections2.fT[0][pt];
535 _Point xy1, xy2;
536 xy_at_t(cubic1, tt1, xy1.x, xy1.y);
537 int pt2 = intersections2.fFlip ? intersections2.used() - pt - 1 : pt;
538 double tt2 = intersections2.fT[1][pt2];
539 xy_at_t(cubic2, tt2, xy2.x, xy2.y);
caryclark@google.com9f602912013-01-24 21:47:16 +0000540 #if 0
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000541 SkDebugf("%s t1=%1.9g (%1.9g, %1.9g) (%1.9g, %1.9g) t2=%1.9g\n", __FUNCTION__,
542 tt1, xy1.x, xy1.y, xy2.x, xy2.y, tt2);
caryclark@google.com9f602912013-01-24 21:47:16 +0000543 #endif
caryclark@google.comaa358312013-01-29 20:28:49 +0000544 SkASSERT(xy1.approximatelyEqual(xy2));
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000545 }
546 }
547}
caryclark@google.com9f602912013-01-24 21:47:16 +0000548
caryclark@google.comf9502d72013-02-04 14:06:49 +0000549void CubicIntersection_IntersectionFinder() {
caryclark@google.com47d73da2013-02-17 01:41:25 +0000550 const Cubic& cubic1 = newTestSet[0];
551 const Cubic& cubic2 = newTestSet[1];
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000552
caryclark@google.com5e0500f2013-02-20 12:51:37 +0000553 double t1Seed = 0.599;
554 double t2Seed = 0.599;
555 double t1Step = 0.1;
556 double t2Step = 0.1;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000557 _Point t1[3], t2[3];
558 bool toggle = true;
559 do {
560 xy_at_t(cubic1, t1Seed - t1Step, t1[0].x, t1[0].y);
561 xy_at_t(cubic1, t1Seed, t1[1].x, t1[1].y);
562 xy_at_t(cubic1, t1Seed + t1Step, t1[2].x, t1[2].y);
563 xy_at_t(cubic2, t2Seed - t2Step, t2[0].x, t2[0].y);
564 xy_at_t(cubic2, t2Seed, t2[1].x, t2[1].y);
565 xy_at_t(cubic2, t2Seed + t2Step, t2[2].x, t2[2].y);
566 double dist[3][3];
567 dist[1][1] = t1[1].distance(t2[1]);
568 int best_i = 1, best_j = 1;
569 for (int i = 0; i < 3; ++i) {
570 for (int j = 0; j < 3; ++j) {
571 if (i == 1 && j == 1) {
572 continue;
573 }
574 dist[i][j] = t1[i].distance(t2[j]);
575 if (dist[best_i][best_j] > dist[i][j]) {
576 best_i = i;
577 best_j = j;
578 }
579 }
580 }
581 if (best_i == 0) {
582 t1Seed -= t1Step;
583 } else if (best_i == 2) {
584 t1Seed += t1Step;
585 }
586 if (best_j == 0) {
587 t2Seed -= t2Step;
588 } else if (best_j == 2) {
589 t2Seed += t2Step;
590 }
591 if (best_i == 1 && best_j == 1) {
592 if ((toggle ^= true)) {
593 t1Step /= 2;
594 } else {
595 t2Step /= 2;
596 }
597 }
598 } while (!t1[1].approximatelyEqual(t2[1]));
599 t1Step = t2Step = 0.1;
600 double t10 = t1Seed - t1Step * 2;
601 double t12 = t1Seed + t1Step * 2;
602 double t20 = t2Seed - t2Step * 2;
603 double t22 = t2Seed + t2Step * 2;
604 _Point test;
605 while (!approximately_zero(t1Step)) {
606 xy_at_t(cubic1, t10, test.x, test.y);
607 t10 += t1[1].approximatelyEqual(test) ? -t1Step : t1Step;
608 t1Step /= 2;
caryclark@google.com9f602912013-01-24 21:47:16 +0000609 }
caryclark@google.comf9502d72013-02-04 14:06:49 +0000610 t1Step = 0.1;
611 while (!approximately_zero(t1Step)) {
612 xy_at_t(cubic1, t12, test.x, test.y);
613 t12 -= t1[1].approximatelyEqual(test) ? -t1Step : t1Step;
614 t1Step /= 2;
615 }
616 while (!approximately_zero(t2Step)) {
617 xy_at_t(cubic2, t20, test.x, test.y);
618 t20 += t2[1].approximatelyEqual(test) ? -t2Step : t2Step;
619 t2Step /= 2;
620 }
621 t2Step = 0.1;
622 while (!approximately_zero(t2Step)) {
623 xy_at_t(cubic2, t22, test.x, test.y);
624 t22 -= t2[1].approximatelyEqual(test) ? -t2Step : t2Step;
625 t2Step /= 2;
626 }
627 SkDebugf("%s t1=(%1.9g<%1.9g<%1.9g) t2=(%1.9g<%1.9g<%1.9g)\n", __FUNCTION__,
628 t10, t1Seed, t12, t20, t2Seed, t22);
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000629 _Point p10 = xy_at_t(cubic1, t10);
630 _Point p1Seed = xy_at_t(cubic1, t1Seed);
631 _Point p12 = xy_at_t(cubic1, t12);
632 SkDebugf("%s p1=(%1.9g,%1.9g)<(%1.9g,%1.9g)<(%1.9g,%1.9g)\n", __FUNCTION__,
633 p10.x, p10.y, p1Seed.x, p1Seed.y, p12.x, p12.y);
634 _Point p20 = xy_at_t(cubic2, t20);
635 _Point p2Seed = xy_at_t(cubic2, t2Seed);
636 _Point p22 = xy_at_t(cubic2, t22);
637 SkDebugf("%s p2=(%1.9g,%1.9g)<(%1.9g,%1.9g)<(%1.9g,%1.9g)\n", __FUNCTION__,
638 p20.x, p20.y, p2Seed.x, p2Seed.y, p22.x, p22.y);
caryclark@google.com9f602912013-01-24 21:47:16 +0000639}
caryclark@google.comf9502d72013-02-04 14:06:49 +0000640
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000641static void coincidentTest() {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000642#if 0
caryclark@google.comf9502d72013-02-04 14:06:49 +0000643 Cubic cubic1 = {{0, 1}, {0, 2}, {1, 0}, {1, 0}};
644 Cubic cubic2 = {{0, 1}, {0, 2}, {1, 0}, {6, 1}};
caryclark@google.comf9502d72013-02-04 14:06:49 +0000645#endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000646}
647
648void CubicIntersection_Test() {
649 oneOffTests();
650 coincidentTest();
651 standardTestCases();
652}