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