blob: e45d54317332d25d693fa8f5e2590e48f41c104f [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.com7ff5c842013-02-26 15:56:05 +0000137{{0,1}, {2,5}, {6,0}, {5,3}},
138{{0,6}, {3,5}, {1,0}, {5,2}},
139
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000140{{0,1}, {3,6}, {1,0}, {5,2}},
141{{0,1}, {2,5}, {1,0}, {6,3}},
142
caryclark@google.com5e0500f2013-02-20 12:51:37 +0000143{{1,2},{5,6},{1,0},{1,0}},
144{{0,1},{0,1},{2,1},{6,5}},
145
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000146{{0,6},{1,2},{1,0},{1,0}},
caryclark@google.com5e0500f2013-02-20 12:51:37 +0000147{{0,1},{0,1},{6,0},{2,1}},
148
caryclark@google.com47d73da2013-02-17 01:41:25 +0000149{{0,2},{0,1},{3,0},{1,0}},
150{{0,3},{0,1},{2,0},{1,0}},
151};
152
153const size_t newTestSetCount = sizeof(newTestSet) / sizeof(newTestSet[0]);
154
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000155static void oneOff(const Cubic& cubic1, const Cubic& cubic2) {
156 SkTDArray<Quadratic> quads1;
157 cubic_to_quadratics(cubic1, calcPrecision(cubic1), quads1);
caryclark@google.com9f602912013-01-24 21:47:16 +0000158#if ONE_OFF_DEBUG
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000159 for (int index = 0; index < quads1.count(); ++index) {
160 const Quadratic& q = quads1[index];
161 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", q[0].x, q[0].y,
162 q[1].x, q[1].y, q[2].x, q[2].y);
163 }
164 SkDebugf("\n");
caryclark@google.com9f602912013-01-24 21:47:16 +0000165#endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000166 SkTDArray<Quadratic> quads2;
167 cubic_to_quadratics(cubic2, calcPrecision(cubic2), quads2);
caryclark@google.com9f602912013-01-24 21:47:16 +0000168#if ONE_OFF_DEBUG
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000169 for (int index = 0; index < quads2.count(); ++index) {
170 const Quadratic& q = quads2[index];
171 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", q[0].x, q[0].y,
172 q[1].x, q[1].y, q[2].x, q[2].y);
173 }
174 SkDebugf("\n");
caryclark@google.com9f602912013-01-24 21:47:16 +0000175#endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000176 Intersections intersections2, intersections3;
177 intersect2(cubic1, cubic2, intersections2);
178 intersect3(cubic1, cubic2, intersections3);
179 int pt1, pt2, pt3;
180 bool found;
181 double tt1, tt2, last = -1;
182 _Point xy1, xy2;
183 for (pt1 = 0; pt1 < intersections2.used(); ++pt1) {
184 tt1 = intersections2.fT[0][pt1];
185 SkASSERT(!approximately_equal(last, tt1));
186 last = tt1;
187 xy_at_t(cubic1, tt1, xy1.x, xy1.y);
188 pt2 = intersections2.fFlip ? intersections2.used() - pt1 - 1 : pt1;
189 tt2 = intersections2.fT[1][pt2];
190 xy_at_t(cubic2, tt2, xy2.x, xy2.y);
191#if ONE_OFF_DEBUG
192 SkDebugf("%s t1=%1.9g (%1.9g, %1.9g) (%1.9g, %1.9g) (%1.9g, %1.9g) t2=%1.9g\n",
193 __FUNCTION__, tt1, xy1.x, xy1.y, intersections2.fPt[pt1].x,
194 intersections2.fPt[pt1].y, xy2.x, xy2.y, tt2);
195#endif
196 SkASSERT(xy1.approximatelyEqual(xy2));
197#if SK_DEBUG
198 found = false;
199 for (pt3 = 0; pt3 < intersections3.used(); ++pt3) {
200 if (roughly_equal(tt1, intersections3.fT[0][pt3])) {
201 found = true;
202 break;
203 }
204 }
205 SkASSERT(found);
206#endif
207 }
208 last = -1;
209 for (pt3 = 0; pt3 < intersections3.used(); ++pt3) {
210 found = false;
211 double tt3 = intersections3.fT[0][pt3];
212 SkASSERT(!approximately_equal(last, tt3));
213 last = tt3;
214 for (pt1 = 0; pt1 < intersections2.used(); ++pt1) {
215 if (approximately_equal(tt3, intersections2.fT[0][pt1])) {
216 found = true;
217 break;
218 }
219 }
220 if (!found) {
221 tt1 = intersections3.fT[0][pt3];
222 xy_at_t(cubic1, tt1, xy1.x, xy1.y);
223 pt2 = intersections3.fFlip ? intersections3.used() - pt3 - 1 : pt3;
224 tt2 = intersections3.fT[1][pt2];
225 xy_at_t(cubic2, tt2, xy2.x, xy2.y);
226 #if ONE_OFF_DEBUG
227 SkDebugf("%s t3=%1.9g (%1.9g, %1.9g) (%1.9g, %1.9g) (%1.9g, %1.9g) t2=%1.9g\n",
228 __FUNCTION__, tt1, xy1.x, xy1.y, intersections3.fPt[pt1].x,
229 intersections3.fPt[pt1].y, xy2.x, xy2.y, tt2);
230 #endif
231 SkASSERT(xy1.approximatelyEqual(xy2));
232 SkDebugf("%s missing in intersect2\n", __FUNCTION__);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000233 }
234 }
235}
236
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000237static void oneOff3(const Cubic& cubic1, const Cubic& cubic2) {
238 SkTDArray<Quadratic> quads1;
239 cubic_to_quadratics(cubic1, calcPrecision(cubic1), quads1);
240#if ONE_OFF_DEBUG
241 for (int index = 0; index < quads1.count(); ++index) {
242 const Quadratic& q = quads1[index];
243 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", q[0].x, q[0].y,
244 q[1].x, q[1].y, q[2].x, q[2].y);
245 }
246 SkDebugf("\n");
247#endif
248 SkTDArray<Quadratic> quads2;
249 cubic_to_quadratics(cubic2, calcPrecision(cubic2), quads2);
250#if ONE_OFF_DEBUG
251 for (int index = 0; index < quads2.count(); ++index) {
252 const Quadratic& q = quads2[index];
253 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", q[0].x, q[0].y,
254 q[1].x, q[1].y, q[2].x, q[2].y);
255 }
256 SkDebugf("\n");
257#endif
258 Intersections intersections3;
259 intersect3(cubic1, cubic2, intersections3);
260 int pt2, pt3;
261 double tt1, tt2, last = -1;
262 _Point xy1, xy2;
263 for (pt3 = 0; pt3 < intersections3.used(); ++pt3) {
264 double tt3 = intersections3.fT[0][pt3];
265 SkASSERT(!approximately_equal(last, tt3));
266 last = tt3;
267 tt1 = intersections3.fT[0][pt3];
268 xy_at_t(cubic1, tt1, xy1.x, xy1.y);
269 pt2 = intersections3.fFlip ? intersections3.used() - pt3 - 1 : pt3;
270 tt2 = intersections3.fT[1][pt2];
271 xy_at_t(cubic2, tt2, xy2.x, xy2.y);
272#if ONE_OFF_DEBUG
273 SkDebugf("%s t3=%1.9g (%1.9g, %1.9g) (%1.9g, %1.9g) (%1.9g, %1.9g) t2=%1.9g\n",
274 __FUNCTION__, tt1, xy1.x, xy1.y, intersections3.fPt[pt3].x,
275 intersections3.fPt[pt3].y, xy2.x, xy2.y, tt2);
276#endif
277 SkASSERT(xy1.approximatelyEqual(xy2));
278 }
279}
280
281static int fails[][2] = { {0, 23}, // fails in intersect2 recursing
282 {2, 7}, // answers differ, but neither is correct ('3' is closer)
283 {3, 26}, // fails in intersect2 recursing
284 {4, 9}, // fails in intersect2 recursing
285 {4, 10}, // fails in intersect2 recursing
286 {10, 17}, // fails in intersect2 recursing
287 {12, 14}, // loops indefinitely
288 {12, 21}, // fails in intersect2 recursing
289 {13, 21}, // fails in intersect2 recursing
290 {14, 21}, // fails in intersect2 recursing
291 {17, 25}, // fails in intersect2 recursing
292 {23, 25}, // fails in intersect2 recursing
293};
294
295static int failCount = sizeof(fails) / sizeof(fails[0]);
296
297static void oneOff(int outer, int inner) {
298 const Cubic& cubic1 = testSet[outer];
299 const Cubic& cubic2 = testSet[inner];
300 bool failing = false;
301 for (int i = 0; i < failCount; ++i) {
302 if ((fails[i][0] == outer && fails[i][1] == inner)
303 || (fails[i][1] == outer && fails[i][0] == inner)) {
304 failing = true;
305 break;
306 }
307 }
308 if (!failing) {
309 oneOff(cubic1, cubic2);
310 } else {
311 oneOff3(cubic1, cubic2);
312 }
313}
314
315void CubicIntersection_OneOffTest() {
316 oneOff(12, 14);
317}
318
caryclark@google.com47d73da2013-02-17 01:41:25 +0000319static void newOneOff(int outer, int inner) {
320 const Cubic& cubic1 = newTestSet[outer];
321 const Cubic& cubic2 = newTestSet[inner];
322 oneOff3(cubic1, cubic2);
323}
324
325void CubicIntersection_NewOneOffTest() {
326 newOneOff(0, 1);
327}
328
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000329static void oneOffTests() {
330 for (size_t outer = 0; outer < testSetCount - 1; ++outer) {
331 for (size_t inner = outer + 1; inner < testSetCount; ++inner) {
332 oneOff(outer, inner);
333 }
334 }
335}
336
337void CubicIntersection_OneOffTests() {
338 oneOffTests();
339}
340
caryclark@google.comf9502d72013-02-04 14:06:49 +0000341#define DEBUG_CRASH 0
caryclark@google.com73ca6242013-01-17 21:02:47 +0000342
343class CubicChopper {
344public:
345
346// only finds one intersection
347CubicChopper(const Cubic& c1, const Cubic& c2)
348 : cubic1(c1)
349 , cubic2(c2)
350 , depth(0) {
351}
352
353bool intersect(double minT1, double maxT1, double minT2, double maxT2) {
354 Cubic sub1, sub2;
355 // FIXME: carry last subdivide and reduceOrder result with cubic
356 sub_divide(cubic1, minT1, maxT1, sub1);
357 sub_divide(cubic2, minT2, maxT2, sub2);
358 Intersections i;
359 intersect2(sub1, sub2, i);
360 if (i.used() == 0) {
361 return false;
362 }
363 double x1, y1, x2, y2;
364 t1 = minT1 + i.fT[0][0] * (maxT1 - minT1);
365 t2 = minT2 + i.fT[1][0] * (maxT2 - minT2);
366 xy_at_t(cubic1, t1, x1, y1);
367 xy_at_t(cubic2, t2, x2, y2);
368 if (AlmostEqualUlps(x1, x2) && AlmostEqualUlps(y1, y2)) {
369 return true;
370 }
371 double half1 = (minT1 + maxT1) / 2;
372 double half2 = (minT2 + maxT2) / 2;
373 ++depth;
374 bool result;
375 if (depth & 1) {
376 result = intersect(minT1, half1, minT2, maxT2) || intersect(half1, maxT1, minT2, maxT2)
377 || intersect(minT1, maxT1, minT2, half2) || intersect(minT1, maxT1, half2, maxT2);
378 } else {
379 result = intersect(minT1, maxT1, minT2, half2) || intersect(minT1, maxT1, half2, maxT2)
380 || intersect(minT1, half1, minT2, maxT2) || intersect(half1, maxT1, minT2, maxT2);
381 }
382 --depth;
383 return result;
384}
385
386const Cubic& cubic1;
387const Cubic& cubic2;
388double t1;
389double t2;
390int depth;
391};
392
393#define TRY_OLD 0 // old way fails on test == 1
394
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000395void CubicIntersection_RandTestOld() {
caryclark@google.com73ca6242013-01-17 21:02:47 +0000396 srand(0);
397 const int tests = 1000000; // 10000000;
398 double largestFactor = DBL_MAX;
399 for (int test = 0; test < tests; ++test) {
400 Cubic cubic1, cubic2;
401 for (int i = 0; i < 4; ++i) {
402 cubic1[i].x = (double) rand() / RAND_MAX * 100;
403 cubic1[i].y = (double) rand() / RAND_MAX * 100;
404 cubic2[i].x = (double) rand() / RAND_MAX * 100;
405 cubic2[i].y = (double) rand() / RAND_MAX * 100;
406 }
407 if (test == 2513) { // the pair crosses three times, but the quadratic approximation
408 continue; // only sees one -- should be OK to ignore the other two?
409 }
410 if (test == 12932) { // this exposes a weakness when one cubic touches the other but
411 continue; // does not touch the quad approximation. Captured in qc.htm as cubic15
412 }
413 #if DEBUG_CRASH
414 char str[1024];
415 sprintf(str, "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n"
416 "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n",
417 cubic1[0].x, cubic1[0].y, cubic1[1].x, cubic1[1].y, cubic1[2].x, cubic1[2].y,
418 cubic1[3].x, cubic1[3].y,
419 cubic2[0].x, cubic2[0].y, cubic2[1].x, cubic2[1].y, cubic2[2].x, cubic2[2].y,
420 cubic2[3].x, cubic2[3].y);
421 #endif
422 _Rect rect1, rect2;
423 rect1.setBounds(cubic1);
424 rect2.setBounds(cubic2);
425 bool boundsIntersect = rect1.left <= rect2.right && rect2.left <= rect2.right
426 && rect1.top <= rect2.bottom && rect2.top <= rect1.bottom;
427 Intersections i1, i2;
428 #if TRY_OLD
429 bool oldIntersects = intersect(cubic1, cubic2, i1);
430 #else
431 bool oldIntersects = false;
432 #endif
433 if (test == -1) {
434 SkDebugf("ready...\n");
435 }
436 bool newIntersects = intersect2(cubic1, cubic2, i2);
437 if (!boundsIntersect && (oldIntersects || newIntersects)) {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000438 #if DEBUG_CRASH
caryclark@google.com73ca6242013-01-17 21:02:47 +0000439 SkDebugf("%s %d unexpected intersection boundsIntersect=%d oldIntersects=%d"
440 " newIntersects=%d\n%s %s\n", __FUNCTION__, test, boundsIntersect,
441 oldIntersects, newIntersects, __FUNCTION__, str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000442 #endif
caryclark@google.comaa358312013-01-29 20:28:49 +0000443 SkASSERT(0);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000444 }
445 if (oldIntersects && !newIntersects) {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000446 #if DEBUG_CRASH
caryclark@google.com73ca6242013-01-17 21:02:47 +0000447 SkDebugf("%s %d missing intersection oldIntersects=%d newIntersects=%d\n%s %s\n",
448 __FUNCTION__, test, oldIntersects, newIntersects, __FUNCTION__, str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000449 #endif
caryclark@google.comaa358312013-01-29 20:28:49 +0000450 SkASSERT(0);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000451 }
452 if (!oldIntersects && !newIntersects) {
453 continue;
454 }
455 if (i2.used() > 1) {
456 continue;
457 // just look at single intercepts for simplicity
458 }
459 Intersections self1, self2; // self-intersect checks
460 if (intersect(cubic1, self1)) {
461 continue;
462 }
463 if (intersect(cubic2, self2)) {
464 continue;
465 }
466 // binary search for range necessary to enclose real intersection
467 CubicChopper c(cubic1, cubic2);
468 bool result = c.intersect(0, 1, 0, 1);
469 if (!result) {
470 // FIXME: a failure here probably means that a core routine used by CubicChopper is failing
471 continue;
472 }
473 double delta1 = fabs(c.t1 - i2.fT[0][0]);
474 double delta2 = fabs(c.t2 - i2.fT[1][0]);
475 double calc1 = calcPrecision(cubic1);
476 double calc2 = calcPrecision(cubic2);
477 double factor1 = calc1 / delta1;
478 double factor2 = calc2 / delta2;
479 SkDebugf("%s %d calc1=%1.9g delta1=%1.9g factor1=%1.9g calc2=%1.9g delta2=%1.9g"
480 " factor2=%1.9g\n", __FUNCTION__, test,
481 calc1, delta1, factor1, calc2, delta2, factor2);
482 if (factor1 < largestFactor) {
483 SkDebugf("WE HAVE A WINNER! %1.9g\n", factor1);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000484 #if DEBUG_CRASH
caryclark@google.com73ca6242013-01-17 21:02:47 +0000485 SkDebugf("%s\n", str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000486 #endif
caryclark@google.com73ca6242013-01-17 21:02:47 +0000487 oneOff(cubic1, cubic2);
488 largestFactor = factor1;
489 }
490 if (factor2 < largestFactor) {
491 SkDebugf("WE HAVE A WINNER! %1.9g\n", factor2);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000492 #if DEBUG_CRASH
caryclark@google.com73ca6242013-01-17 21:02:47 +0000493 SkDebugf("%s\n", str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000494 #endif
caryclark@google.com73ca6242013-01-17 21:02:47 +0000495 oneOff(cubic1, cubic2);
496 largestFactor = factor2;
497 }
498 }
499}
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000500
501void CubicIntersection_RandTest() {
502 srand(0);
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000503 const int tests = 10000000;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000504 for (int test = 0; test < tests; ++test) {
505 Cubic cubic1, cubic2;
506 for (int i = 0; i < 4; ++i) {
507 cubic1[i].x = (double) rand() / RAND_MAX * 100;
508 cubic1[i].y = (double) rand() / RAND_MAX * 100;
509 cubic2[i].x = (double) rand() / RAND_MAX * 100;
510 cubic2[i].y = (double) rand() / RAND_MAX * 100;
511 }
512 #if DEBUG_CRASH
513 char str[1024];
514 sprintf(str, "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n"
515 "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n",
516 cubic1[0].x, cubic1[0].y, cubic1[1].x, cubic1[1].y, cubic1[2].x, cubic1[2].y,
517 cubic1[3].x, cubic1[3].y,
518 cubic2[0].x, cubic2[0].y, cubic2[1].x, cubic2[1].y, cubic2[2].x, cubic2[2].y,
519 cubic2[3].x, cubic2[3].y);
520 #endif
521 _Rect rect1, rect2;
522 rect1.setBounds(cubic1);
523 rect2.setBounds(cubic2);
524 bool boundsIntersect = rect1.left <= rect2.right && rect2.left <= rect2.right
525 && rect1.top <= rect2.bottom && rect2.top <= rect1.bottom;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000526 if (test == -1) {
527 SkDebugf("ready...\n");
528 }
529 Intersections intersections2;
530 bool newIntersects = intersect2(cubic1, cubic2, intersections2);
531 if (!boundsIntersect && newIntersects) {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000532 #if DEBUG_CRASH
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000533 SkDebugf("%s %d unexpected intersection boundsIntersect=%d "
534 " newIntersects=%d\n%s %s\n", __FUNCTION__, test, boundsIntersect,
535 newIntersects, __FUNCTION__, str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000536 #endif
caryclark@google.comaa358312013-01-29 20:28:49 +0000537 SkASSERT(0);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000538 }
539 for (int pt = 0; pt < intersections2.used(); ++pt) {
540 double tt1 = intersections2.fT[0][pt];
541 _Point xy1, xy2;
542 xy_at_t(cubic1, tt1, xy1.x, xy1.y);
543 int pt2 = intersections2.fFlip ? intersections2.used() - pt - 1 : pt;
544 double tt2 = intersections2.fT[1][pt2];
545 xy_at_t(cubic2, tt2, xy2.x, xy2.y);
caryclark@google.com9f602912013-01-24 21:47:16 +0000546 #if 0
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000547 SkDebugf("%s t1=%1.9g (%1.9g, %1.9g) (%1.9g, %1.9g) t2=%1.9g\n", __FUNCTION__,
548 tt1, xy1.x, xy1.y, xy2.x, xy2.y, tt2);
caryclark@google.com9f602912013-01-24 21:47:16 +0000549 #endif
caryclark@google.comaa358312013-01-29 20:28:49 +0000550 SkASSERT(xy1.approximatelyEqual(xy2));
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000551 }
552 }
553}
caryclark@google.com9f602912013-01-24 21:47:16 +0000554
caryclark@google.comf9502d72013-02-04 14:06:49 +0000555void CubicIntersection_IntersectionFinder() {
caryclark@google.com47d73da2013-02-17 01:41:25 +0000556 const Cubic& cubic1 = newTestSet[0];
557 const Cubic& cubic2 = newTestSet[1];
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000558
caryclark@google.com7ff5c842013-02-26 15:56:05 +0000559 double t1Seed = 0.77;
560 double t2Seed = 0.99;
caryclark@google.com5e0500f2013-02-20 12:51:37 +0000561 double t1Step = 0.1;
caryclark@google.com7ff5c842013-02-26 15:56:05 +0000562 double t2Step = 0.01;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000563 _Point t1[3], t2[3];
564 bool toggle = true;
565 do {
566 xy_at_t(cubic1, t1Seed - t1Step, t1[0].x, t1[0].y);
567 xy_at_t(cubic1, t1Seed, t1[1].x, t1[1].y);
568 xy_at_t(cubic1, t1Seed + t1Step, t1[2].x, t1[2].y);
569 xy_at_t(cubic2, t2Seed - t2Step, t2[0].x, t2[0].y);
570 xy_at_t(cubic2, t2Seed, t2[1].x, t2[1].y);
571 xy_at_t(cubic2, t2Seed + t2Step, t2[2].x, t2[2].y);
572 double dist[3][3];
573 dist[1][1] = t1[1].distance(t2[1]);
574 int best_i = 1, best_j = 1;
575 for (int i = 0; i < 3; ++i) {
576 for (int j = 0; j < 3; ++j) {
577 if (i == 1 && j == 1) {
578 continue;
579 }
580 dist[i][j] = t1[i].distance(t2[j]);
581 if (dist[best_i][best_j] > dist[i][j]) {
582 best_i = i;
583 best_j = j;
584 }
585 }
586 }
587 if (best_i == 0) {
588 t1Seed -= t1Step;
589 } else if (best_i == 2) {
590 t1Seed += t1Step;
591 }
592 if (best_j == 0) {
593 t2Seed -= t2Step;
594 } else if (best_j == 2) {
595 t2Seed += t2Step;
596 }
597 if (best_i == 1 && best_j == 1) {
598 if ((toggle ^= true)) {
599 t1Step /= 2;
600 } else {
601 t2Step /= 2;
602 }
603 }
604 } while (!t1[1].approximatelyEqual(t2[1]));
605 t1Step = t2Step = 0.1;
606 double t10 = t1Seed - t1Step * 2;
607 double t12 = t1Seed + t1Step * 2;
608 double t20 = t2Seed - t2Step * 2;
609 double t22 = t2Seed + t2Step * 2;
610 _Point test;
611 while (!approximately_zero(t1Step)) {
612 xy_at_t(cubic1, t10, test.x, test.y);
613 t10 += t1[1].approximatelyEqual(test) ? -t1Step : t1Step;
614 t1Step /= 2;
caryclark@google.com9f602912013-01-24 21:47:16 +0000615 }
caryclark@google.comf9502d72013-02-04 14:06:49 +0000616 t1Step = 0.1;
617 while (!approximately_zero(t1Step)) {
618 xy_at_t(cubic1, t12, test.x, test.y);
619 t12 -= t1[1].approximatelyEqual(test) ? -t1Step : t1Step;
620 t1Step /= 2;
621 }
622 while (!approximately_zero(t2Step)) {
623 xy_at_t(cubic2, t20, test.x, test.y);
624 t20 += t2[1].approximatelyEqual(test) ? -t2Step : t2Step;
625 t2Step /= 2;
626 }
627 t2Step = 0.1;
628 while (!approximately_zero(t2Step)) {
629 xy_at_t(cubic2, t22, test.x, test.y);
630 t22 -= t2[1].approximatelyEqual(test) ? -t2Step : t2Step;
631 t2Step /= 2;
632 }
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000633#if ONE_OFF_DEBUG
caryclark@google.comf9502d72013-02-04 14:06:49 +0000634 SkDebugf("%s t1=(%1.9g<%1.9g<%1.9g) t2=(%1.9g<%1.9g<%1.9g)\n", __FUNCTION__,
635 t10, t1Seed, t12, t20, t2Seed, t22);
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000636 _Point p10 = xy_at_t(cubic1, t10);
637 _Point p1Seed = xy_at_t(cubic1, t1Seed);
638 _Point p12 = xy_at_t(cubic1, t12);
639 SkDebugf("%s p1=(%1.9g,%1.9g)<(%1.9g,%1.9g)<(%1.9g,%1.9g)\n", __FUNCTION__,
640 p10.x, p10.y, p1Seed.x, p1Seed.y, p12.x, p12.y);
641 _Point p20 = xy_at_t(cubic2, t20);
642 _Point p2Seed = xy_at_t(cubic2, t2Seed);
643 _Point p22 = xy_at_t(cubic2, t22);
644 SkDebugf("%s p2=(%1.9g,%1.9g)<(%1.9g,%1.9g)<(%1.9g,%1.9g)\n", __FUNCTION__,
645 p20.x, p20.y, p2Seed.x, p2Seed.y, p22.x, p22.y);
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000646#endif
caryclark@google.com9f602912013-01-24 21:47:16 +0000647}
caryclark@google.comf9502d72013-02-04 14:06:49 +0000648
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000649static void coincidentTest() {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000650#if 0
caryclark@google.comf9502d72013-02-04 14:06:49 +0000651 Cubic cubic1 = {{0, 1}, {0, 2}, {1, 0}, {1, 0}};
652 Cubic cubic2 = {{0, 1}, {0, 2}, {1, 0}, {6, 1}};
caryclark@google.comf9502d72013-02-04 14:06:49 +0000653#endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000654}
655
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000656void CubicIntersection_SelfTest() {
657 const Cubic selfSet[] = {
658 {{3.34,8.98}, {1.95,10.27}, {3.76,7.65}, {4.96,10.64}},
659 {{3.13,2.74}, {1.08,4.62}, {3.71,0.94}, {2.01,3.81}},
skia.committer@gmail.com58433de2013-02-23 07:02:45 +0000660 {{6.71,3.14}, {7.99,2.75}, {8.27,1.96}, {6.35,3.57}},
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000661 {{12.81,7.27}, {7.22,6.98}, {12.49,8.97}, {11.42,6.18}},
662 };
663 size_t selfSetCount = sizeof(selfSet) / sizeof(selfSet[0]);
664 for (size_t index = 0; index < selfSetCount; ++index) {
665 const Cubic& cubic = selfSet[index];
666 #if ONE_OFF_DEBUG
667 SkTDArray<Quadratic> quads1;
668 cubic_to_quadratics(cubic, calcPrecision(cubic), quads1);
669 for (int index = 0; index < quads1.count(); ++index) {
670 const Quadratic& q = quads1[index];
671 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", q[0].x, q[0].y,
672 q[1].x, q[1].y, q[2].x, q[2].y);
673 }
674 SkDebugf("\n");
675 #endif
676 Intersections i;
677 int result = intersect(cubic, i);
678 SkASSERT(result == 1);
679 SkASSERT(i.used() == 1);
680 SkASSERT(!approximately_equal(i.fT[0][0], i.fT[1][0]));
681 _Point pt1 = xy_at_t(cubic, i.fT[0][0]);
682 _Point pt2 = xy_at_t(cubic, i.fT[1][0]);
683 SkASSERT(pt1.approximatelyEqual(pt2));
684 }
685}
686
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000687void CubicIntersection_Test() {
688 oneOffTests();
689 coincidentTest();
690 standardTestCases();
691}