blob: 20c767124243a9d5e06a2c5a6166b3e7acd0f305 [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.com4aaaaea2013-02-28 16:12:39 +0000137{{0,1}, {3,5}, {2,1}, {3,1}},
138{{1,2}, {1,3}, {1,0}, {5,3}},
139
140{{0,1}, {2,5}, {6,0}, {5,3}},
caryclark@google.com7ff5c842013-02-26 15:56:05 +0000141{{0,6}, {3,5}, {1,0}, {5,2}},
142
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000143{{0,1}, {3,6}, {1,0}, {5,2}},
144{{0,1}, {2,5}, {1,0}, {6,3}},
145
caryclark@google.com5e0500f2013-02-20 12:51:37 +0000146{{1,2},{5,6},{1,0},{1,0}},
147{{0,1},{0,1},{2,1},{6,5}},
148
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000149{{0,6},{1,2},{1,0},{1,0}},
caryclark@google.com5e0500f2013-02-20 12:51:37 +0000150{{0,1},{0,1},{6,0},{2,1}},
151
caryclark@google.com47d73da2013-02-17 01:41:25 +0000152{{0,2},{0,1},{3,0},{1,0}},
153{{0,3},{0,1},{2,0},{1,0}},
154};
155
156const size_t newTestSetCount = sizeof(newTestSet) / sizeof(newTestSet[0]);
157
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000158#if 0
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000159static void oneOff(const Cubic& cubic1, const Cubic& cubic2) {
160 SkTDArray<Quadratic> quads1;
161 cubic_to_quadratics(cubic1, calcPrecision(cubic1), quads1);
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 < quads1.count(); ++index) {
164 const Quadratic& q = quads1[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 SkTDArray<Quadratic> quads2;
171 cubic_to_quadratics(cubic2, calcPrecision(cubic2), quads2);
caryclark@google.com9f602912013-01-24 21:47:16 +0000172#if ONE_OFF_DEBUG
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000173 for (int index = 0; index < quads2.count(); ++index) {
174 const Quadratic& q = quads2[index];
175 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", q[0].x, q[0].y,
176 q[1].x, q[1].y, q[2].x, q[2].y);
177 }
178 SkDebugf("\n");
caryclark@google.com9f602912013-01-24 21:47:16 +0000179#endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000180 Intersections intersections2, intersections3;
181 intersect2(cubic1, cubic2, intersections2);
182 intersect3(cubic1, cubic2, intersections3);
183 int pt1, pt2, pt3;
184 bool found;
185 double tt1, tt2, last = -1;
186 _Point xy1, xy2;
187 for (pt1 = 0; pt1 < intersections2.used(); ++pt1) {
188 tt1 = intersections2.fT[0][pt1];
189 SkASSERT(!approximately_equal(last, tt1));
190 last = tt1;
191 xy_at_t(cubic1, tt1, xy1.x, xy1.y);
192 pt2 = intersections2.fFlip ? intersections2.used() - pt1 - 1 : pt1;
193 tt2 = intersections2.fT[1][pt2];
194 xy_at_t(cubic2, tt2, xy2.x, xy2.y);
195#if ONE_OFF_DEBUG
196 SkDebugf("%s t1=%1.9g (%1.9g, %1.9g) (%1.9g, %1.9g) (%1.9g, %1.9g) t2=%1.9g\n",
197 __FUNCTION__, tt1, xy1.x, xy1.y, intersections2.fPt[pt1].x,
198 intersections2.fPt[pt1].y, xy2.x, xy2.y, tt2);
199#endif
200 SkASSERT(xy1.approximatelyEqual(xy2));
201#if SK_DEBUG
202 found = false;
203 for (pt3 = 0; pt3 < intersections3.used(); ++pt3) {
204 if (roughly_equal(tt1, intersections3.fT[0][pt3])) {
205 found = true;
206 break;
207 }
208 }
209 SkASSERT(found);
210#endif
211 }
212 last = -1;
213 for (pt3 = 0; pt3 < intersections3.used(); ++pt3) {
214 found = false;
215 double tt3 = intersections3.fT[0][pt3];
216 SkASSERT(!approximately_equal(last, tt3));
217 last = tt3;
218 for (pt1 = 0; pt1 < intersections2.used(); ++pt1) {
219 if (approximately_equal(tt3, intersections2.fT[0][pt1])) {
220 found = true;
221 break;
222 }
223 }
224 if (!found) {
225 tt1 = intersections3.fT[0][pt3];
226 xy_at_t(cubic1, tt1, xy1.x, xy1.y);
227 pt2 = intersections3.fFlip ? intersections3.used() - pt3 - 1 : pt3;
228 tt2 = intersections3.fT[1][pt2];
229 xy_at_t(cubic2, tt2, xy2.x, xy2.y);
230 #if ONE_OFF_DEBUG
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000231 SkDebugf("%s t1=%1.9g (%1.9g, %1.9g) (%1.9g, %1.9g) (%1.9g, %1.9g) t2=%1.9g\n",
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000232 __FUNCTION__, tt1, xy1.x, xy1.y, intersections3.fPt[pt1].x,
233 intersections3.fPt[pt1].y, xy2.x, xy2.y, tt2);
234 #endif
235 SkASSERT(xy1.approximatelyEqual(xy2));
236 SkDebugf("%s missing in intersect2\n", __FUNCTION__);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000237 }
238 }
239}
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000240#endif
caryclark@google.com73ca6242013-01-17 21:02:47 +0000241
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000242static void oneOff3(const Cubic& cubic1, const Cubic& cubic2) {
243 SkTDArray<Quadratic> quads1;
244 cubic_to_quadratics(cubic1, calcPrecision(cubic1), quads1);
245#if ONE_OFF_DEBUG
246 for (int index = 0; index < quads1.count(); ++index) {
247 const Quadratic& q = quads1[index];
248 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", q[0].x, q[0].y,
249 q[1].x, q[1].y, q[2].x, q[2].y);
250 }
251 SkDebugf("\n");
252#endif
253 SkTDArray<Quadratic> quads2;
254 cubic_to_quadratics(cubic2, calcPrecision(cubic2), quads2);
255#if ONE_OFF_DEBUG
256 for (int index = 0; index < quads2.count(); ++index) {
257 const Quadratic& q = quads2[index];
258 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", q[0].x, q[0].y,
259 q[1].x, q[1].y, q[2].x, q[2].y);
260 }
261 SkDebugf("\n");
262#endif
263 Intersections intersections3;
264 intersect3(cubic1, cubic2, intersections3);
265 int pt2, pt3;
266 double tt1, tt2, last = -1;
267 _Point xy1, xy2;
268 for (pt3 = 0; pt3 < intersections3.used(); ++pt3) {
269 double tt3 = intersections3.fT[0][pt3];
270 SkASSERT(!approximately_equal(last, tt3));
271 last = tt3;
272 tt1 = intersections3.fT[0][pt3];
273 xy_at_t(cubic1, tt1, xy1.x, xy1.y);
274 pt2 = intersections3.fFlip ? intersections3.used() - pt3 - 1 : pt3;
275 tt2 = intersections3.fT[1][pt2];
276 xy_at_t(cubic2, tt2, xy2.x, xy2.y);
277#if ONE_OFF_DEBUG
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000278 SkDebugf("%s t1=%1.9g (%1.9g, %1.9g) (%1.9g, %1.9g) (%1.9g, %1.9g) t2=%1.9g\n",
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000279 __FUNCTION__, tt1, xy1.x, xy1.y, intersections3.fPt[pt3].x,
280 intersections3.fPt[pt3].y, xy2.x, xy2.y, tt2);
281#endif
282 SkASSERT(xy1.approximatelyEqual(xy2));
283 }
284}
285
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000286#if 0
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000287static int fails[][2] = { {0, 23}, // fails in intersect2 recursing
288 {2, 7}, // answers differ, but neither is correct ('3' is closer)
289 {3, 26}, // fails in intersect2 recursing
290 {4, 9}, // fails in intersect2 recursing
291 {4, 10}, // fails in intersect2 recursing
292 {10, 17}, // fails in intersect2 recursing
293 {12, 14}, // loops indefinitely
294 {12, 21}, // fails in intersect2 recursing
295 {13, 21}, // fails in intersect2 recursing
296 {14, 21}, // fails in intersect2 recursing
297 {17, 25}, // fails in intersect2 recursing
298 {23, 25}, // fails in intersect2 recursing
299};
300
301static int failCount = sizeof(fails) / sizeof(fails[0]);
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000302#endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000303
304static void oneOff(int outer, int inner) {
305 const Cubic& cubic1 = testSet[outer];
306 const Cubic& cubic2 = testSet[inner];
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000307#if 0
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000308 bool failing = false;
309 for (int i = 0; i < failCount; ++i) {
310 if ((fails[i][0] == outer && fails[i][1] == inner)
311 || (fails[i][1] == outer && fails[i][0] == inner)) {
312 failing = true;
313 break;
314 }
315 }
316 if (!failing) {
317 oneOff(cubic1, cubic2);
318 } else {
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000319#endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000320 oneOff3(cubic1, cubic2);
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000321// }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000322}
323
324void CubicIntersection_OneOffTest() {
325 oneOff(12, 14);
326}
327
caryclark@google.com47d73da2013-02-17 01:41:25 +0000328static void newOneOff(int outer, int inner) {
329 const Cubic& cubic1 = newTestSet[outer];
330 const Cubic& cubic2 = newTestSet[inner];
331 oneOff3(cubic1, cubic2);
332}
333
334void CubicIntersection_NewOneOffTest() {
335 newOneOff(0, 1);
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000336 newOneOff(1, 0);
caryclark@google.com47d73da2013-02-17 01:41:25 +0000337}
338
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000339static void oneOffTests() {
340 for (size_t outer = 0; outer < testSetCount - 1; ++outer) {
341 for (size_t inner = outer + 1; inner < testSetCount; ++inner) {
342 oneOff(outer, inner);
343 }
344 }
345}
346
347void CubicIntersection_OneOffTests() {
348 oneOffTests();
349}
350
caryclark@google.comf9502d72013-02-04 14:06:49 +0000351#define DEBUG_CRASH 0
caryclark@google.com73ca6242013-01-17 21:02:47 +0000352
353class CubicChopper {
354public:
355
356// only finds one intersection
357CubicChopper(const Cubic& c1, const Cubic& c2)
358 : cubic1(c1)
359 , cubic2(c2)
360 , depth(0) {
361}
362
363bool intersect(double minT1, double maxT1, double minT2, double maxT2) {
364 Cubic sub1, sub2;
365 // FIXME: carry last subdivide and reduceOrder result with cubic
366 sub_divide(cubic1, minT1, maxT1, sub1);
367 sub_divide(cubic2, minT2, maxT2, sub2);
368 Intersections i;
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000369 intersect3(sub1, sub2, i);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000370 if (i.used() == 0) {
371 return false;
372 }
373 double x1, y1, x2, y2;
374 t1 = minT1 + i.fT[0][0] * (maxT1 - minT1);
375 t2 = minT2 + i.fT[1][0] * (maxT2 - minT2);
376 xy_at_t(cubic1, t1, x1, y1);
377 xy_at_t(cubic2, t2, x2, y2);
378 if (AlmostEqualUlps(x1, x2) && AlmostEqualUlps(y1, y2)) {
379 return true;
380 }
381 double half1 = (minT1 + maxT1) / 2;
382 double half2 = (minT2 + maxT2) / 2;
383 ++depth;
384 bool result;
385 if (depth & 1) {
386 result = intersect(minT1, half1, minT2, maxT2) || intersect(half1, maxT1, minT2, maxT2)
387 || intersect(minT1, maxT1, minT2, half2) || intersect(minT1, maxT1, half2, maxT2);
388 } else {
389 result = intersect(minT1, maxT1, minT2, half2) || intersect(minT1, maxT1, half2, maxT2)
390 || intersect(minT1, half1, minT2, maxT2) || intersect(half1, maxT1, minT2, maxT2);
391 }
392 --depth;
393 return result;
394}
395
396const Cubic& cubic1;
397const Cubic& cubic2;
398double t1;
399double t2;
400int depth;
401};
402
403#define TRY_OLD 0 // old way fails on test == 1
404
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000405void CubicIntersection_RandTestOld() {
caryclark@google.com73ca6242013-01-17 21:02:47 +0000406 srand(0);
407 const int tests = 1000000; // 10000000;
408 double largestFactor = DBL_MAX;
409 for (int test = 0; test < tests; ++test) {
410 Cubic cubic1, cubic2;
411 for (int i = 0; i < 4; ++i) {
412 cubic1[i].x = (double) rand() / RAND_MAX * 100;
413 cubic1[i].y = (double) rand() / RAND_MAX * 100;
414 cubic2[i].x = (double) rand() / RAND_MAX * 100;
415 cubic2[i].y = (double) rand() / RAND_MAX * 100;
416 }
417 if (test == 2513) { // the pair crosses three times, but the quadratic approximation
418 continue; // only sees one -- should be OK to ignore the other two?
419 }
420 if (test == 12932) { // this exposes a weakness when one cubic touches the other but
421 continue; // does not touch the quad approximation. Captured in qc.htm as cubic15
422 }
423 #if DEBUG_CRASH
424 char str[1024];
425 sprintf(str, "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n"
426 "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n",
427 cubic1[0].x, cubic1[0].y, cubic1[1].x, cubic1[1].y, cubic1[2].x, cubic1[2].y,
428 cubic1[3].x, cubic1[3].y,
429 cubic2[0].x, cubic2[0].y, cubic2[1].x, cubic2[1].y, cubic2[2].x, cubic2[2].y,
430 cubic2[3].x, cubic2[3].y);
431 #endif
432 _Rect rect1, rect2;
433 rect1.setBounds(cubic1);
434 rect2.setBounds(cubic2);
435 bool boundsIntersect = rect1.left <= rect2.right && rect2.left <= rect2.right
436 && rect1.top <= rect2.bottom && rect2.top <= rect1.bottom;
437 Intersections i1, i2;
438 #if TRY_OLD
439 bool oldIntersects = intersect(cubic1, cubic2, i1);
440 #else
441 bool oldIntersects = false;
442 #endif
443 if (test == -1) {
444 SkDebugf("ready...\n");
445 }
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000446 bool newIntersects = intersect3(cubic1, cubic2, i2);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000447 if (!boundsIntersect && (oldIntersects || newIntersects)) {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000448 #if DEBUG_CRASH
caryclark@google.com73ca6242013-01-17 21:02:47 +0000449 SkDebugf("%s %d unexpected intersection boundsIntersect=%d oldIntersects=%d"
450 " newIntersects=%d\n%s %s\n", __FUNCTION__, test, boundsIntersect,
451 oldIntersects, newIntersects, __FUNCTION__, str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000452 #endif
caryclark@google.comaa358312013-01-29 20:28:49 +0000453 SkASSERT(0);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000454 }
455 if (oldIntersects && !newIntersects) {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000456 #if DEBUG_CRASH
caryclark@google.com73ca6242013-01-17 21:02:47 +0000457 SkDebugf("%s %d missing intersection oldIntersects=%d newIntersects=%d\n%s %s\n",
458 __FUNCTION__, test, oldIntersects, newIntersects, __FUNCTION__, str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000459 #endif
caryclark@google.comaa358312013-01-29 20:28:49 +0000460 SkASSERT(0);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000461 }
462 if (!oldIntersects && !newIntersects) {
463 continue;
464 }
465 if (i2.used() > 1) {
466 continue;
467 // just look at single intercepts for simplicity
468 }
469 Intersections self1, self2; // self-intersect checks
470 if (intersect(cubic1, self1)) {
471 continue;
472 }
473 if (intersect(cubic2, self2)) {
474 continue;
475 }
476 // binary search for range necessary to enclose real intersection
477 CubicChopper c(cubic1, cubic2);
478 bool result = c.intersect(0, 1, 0, 1);
479 if (!result) {
480 // FIXME: a failure here probably means that a core routine used by CubicChopper is failing
481 continue;
482 }
483 double delta1 = fabs(c.t1 - i2.fT[0][0]);
484 double delta2 = fabs(c.t2 - i2.fT[1][0]);
485 double calc1 = calcPrecision(cubic1);
486 double calc2 = calcPrecision(cubic2);
487 double factor1 = calc1 / delta1;
488 double factor2 = calc2 / delta2;
489 SkDebugf("%s %d calc1=%1.9g delta1=%1.9g factor1=%1.9g calc2=%1.9g delta2=%1.9g"
490 " factor2=%1.9g\n", __FUNCTION__, test,
491 calc1, delta1, factor1, calc2, delta2, factor2);
492 if (factor1 < largestFactor) {
493 SkDebugf("WE HAVE A WINNER! %1.9g\n", factor1);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000494 #if DEBUG_CRASH
caryclark@google.com73ca6242013-01-17 21:02:47 +0000495 SkDebugf("%s\n", str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000496 #endif
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000497 oneOff3(cubic1, cubic2);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000498 largestFactor = factor1;
499 }
500 if (factor2 < largestFactor) {
501 SkDebugf("WE HAVE A WINNER! %1.9g\n", factor2);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000502 #if DEBUG_CRASH
caryclark@google.com73ca6242013-01-17 21:02:47 +0000503 SkDebugf("%s\n", str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000504 #endif
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000505 oneOff3(cubic1, cubic2);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000506 largestFactor = factor2;
507 }
508 }
509}
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000510
511void CubicIntersection_RandTest() {
512 srand(0);
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000513 const int tests = 10000000;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000514 for (int test = 0; test < tests; ++test) {
515 Cubic cubic1, cubic2;
516 for (int i = 0; i < 4; ++i) {
517 cubic1[i].x = (double) rand() / RAND_MAX * 100;
518 cubic1[i].y = (double) rand() / RAND_MAX * 100;
519 cubic2[i].x = (double) rand() / RAND_MAX * 100;
520 cubic2[i].y = (double) rand() / RAND_MAX * 100;
521 }
522 #if DEBUG_CRASH
523 char str[1024];
524 sprintf(str, "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n"
525 "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n",
526 cubic1[0].x, cubic1[0].y, cubic1[1].x, cubic1[1].y, cubic1[2].x, cubic1[2].y,
527 cubic1[3].x, cubic1[3].y,
528 cubic2[0].x, cubic2[0].y, cubic2[1].x, cubic2[1].y, cubic2[2].x, cubic2[2].y,
529 cubic2[3].x, cubic2[3].y);
530 #endif
531 _Rect rect1, rect2;
532 rect1.setBounds(cubic1);
533 rect2.setBounds(cubic2);
534 bool boundsIntersect = rect1.left <= rect2.right && rect2.left <= rect2.right
535 && rect1.top <= rect2.bottom && rect2.top <= rect1.bottom;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000536 if (test == -1) {
537 SkDebugf("ready...\n");
538 }
539 Intersections intersections2;
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000540 bool newIntersects = intersect3(cubic1, cubic2, intersections2);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000541 if (!boundsIntersect && newIntersects) {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000542 #if DEBUG_CRASH
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000543 SkDebugf("%s %d unexpected intersection boundsIntersect=%d "
544 " newIntersects=%d\n%s %s\n", __FUNCTION__, test, boundsIntersect,
545 newIntersects, __FUNCTION__, str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000546 #endif
caryclark@google.comaa358312013-01-29 20:28:49 +0000547 SkASSERT(0);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000548 }
549 for (int pt = 0; pt < intersections2.used(); ++pt) {
550 double tt1 = intersections2.fT[0][pt];
551 _Point xy1, xy2;
552 xy_at_t(cubic1, tt1, xy1.x, xy1.y);
553 int pt2 = intersections2.fFlip ? intersections2.used() - pt - 1 : pt;
554 double tt2 = intersections2.fT[1][pt2];
555 xy_at_t(cubic2, tt2, xy2.x, xy2.y);
caryclark@google.com9f602912013-01-24 21:47:16 +0000556 #if 0
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000557 SkDebugf("%s t1=%1.9g (%1.9g, %1.9g) (%1.9g, %1.9g) t2=%1.9g\n", __FUNCTION__,
558 tt1, xy1.x, xy1.y, xy2.x, xy2.y, tt2);
caryclark@google.com9f602912013-01-24 21:47:16 +0000559 #endif
caryclark@google.comaa358312013-01-29 20:28:49 +0000560 SkASSERT(xy1.approximatelyEqual(xy2));
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000561 }
562 }
563}
caryclark@google.com9f602912013-01-24 21:47:16 +0000564
caryclark@google.comf9502d72013-02-04 14:06:49 +0000565void CubicIntersection_IntersectionFinder() {
caryclark@google.com47d73da2013-02-17 01:41:25 +0000566 const Cubic& cubic1 = newTestSet[0];
567 const Cubic& cubic2 = newTestSet[1];
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000568
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000569 double t1Seed = 0.134792061;
570 double t2Seed = 0.134792094;
caryclark@google.com5e0500f2013-02-20 12:51:37 +0000571 double t1Step = 0.1;
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000572 double t2Step = 0.1;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000573 _Point t1[3], t2[3];
574 bool toggle = true;
575 do {
576 xy_at_t(cubic1, t1Seed - t1Step, t1[0].x, t1[0].y);
577 xy_at_t(cubic1, t1Seed, t1[1].x, t1[1].y);
578 xy_at_t(cubic1, t1Seed + t1Step, t1[2].x, t1[2].y);
579 xy_at_t(cubic2, t2Seed - t2Step, t2[0].x, t2[0].y);
580 xy_at_t(cubic2, t2Seed, t2[1].x, t2[1].y);
581 xy_at_t(cubic2, t2Seed + t2Step, t2[2].x, t2[2].y);
582 double dist[3][3];
583 dist[1][1] = t1[1].distance(t2[1]);
584 int best_i = 1, best_j = 1;
585 for (int i = 0; i < 3; ++i) {
586 for (int j = 0; j < 3; ++j) {
587 if (i == 1 && j == 1) {
588 continue;
589 }
590 dist[i][j] = t1[i].distance(t2[j]);
591 if (dist[best_i][best_j] > dist[i][j]) {
592 best_i = i;
593 best_j = j;
594 }
595 }
596 }
597 if (best_i == 0) {
598 t1Seed -= t1Step;
599 } else if (best_i == 2) {
600 t1Seed += t1Step;
601 }
602 if (best_j == 0) {
603 t2Seed -= t2Step;
604 } else if (best_j == 2) {
605 t2Seed += t2Step;
606 }
607 if (best_i == 1 && best_j == 1) {
608 if ((toggle ^= true)) {
609 t1Step /= 2;
610 } else {
611 t2Step /= 2;
612 }
613 }
614 } while (!t1[1].approximatelyEqual(t2[1]));
615 t1Step = t2Step = 0.1;
616 double t10 = t1Seed - t1Step * 2;
617 double t12 = t1Seed + t1Step * 2;
618 double t20 = t2Seed - t2Step * 2;
619 double t22 = t2Seed + t2Step * 2;
620 _Point test;
621 while (!approximately_zero(t1Step)) {
622 xy_at_t(cubic1, t10, test.x, test.y);
623 t10 += t1[1].approximatelyEqual(test) ? -t1Step : t1Step;
624 t1Step /= 2;
caryclark@google.com9f602912013-01-24 21:47:16 +0000625 }
caryclark@google.comf9502d72013-02-04 14:06:49 +0000626 t1Step = 0.1;
627 while (!approximately_zero(t1Step)) {
628 xy_at_t(cubic1, t12, test.x, test.y);
629 t12 -= t1[1].approximatelyEqual(test) ? -t1Step : t1Step;
630 t1Step /= 2;
631 }
632 while (!approximately_zero(t2Step)) {
633 xy_at_t(cubic2, t20, test.x, test.y);
634 t20 += t2[1].approximatelyEqual(test) ? -t2Step : t2Step;
635 t2Step /= 2;
636 }
637 t2Step = 0.1;
638 while (!approximately_zero(t2Step)) {
639 xy_at_t(cubic2, t22, test.x, test.y);
640 t22 -= t2[1].approximatelyEqual(test) ? -t2Step : t2Step;
641 t2Step /= 2;
642 }
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000643#if ONE_OFF_DEBUG
caryclark@google.comf9502d72013-02-04 14:06:49 +0000644 SkDebugf("%s t1=(%1.9g<%1.9g<%1.9g) t2=(%1.9g<%1.9g<%1.9g)\n", __FUNCTION__,
645 t10, t1Seed, t12, t20, t2Seed, t22);
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000646 _Point p10 = xy_at_t(cubic1, t10);
647 _Point p1Seed = xy_at_t(cubic1, t1Seed);
648 _Point p12 = xy_at_t(cubic1, t12);
649 SkDebugf("%s p1=(%1.9g,%1.9g)<(%1.9g,%1.9g)<(%1.9g,%1.9g)\n", __FUNCTION__,
650 p10.x, p10.y, p1Seed.x, p1Seed.y, p12.x, p12.y);
651 _Point p20 = xy_at_t(cubic2, t20);
652 _Point p2Seed = xy_at_t(cubic2, t2Seed);
653 _Point p22 = xy_at_t(cubic2, t22);
654 SkDebugf("%s p2=(%1.9g,%1.9g)<(%1.9g,%1.9g)<(%1.9g,%1.9g)\n", __FUNCTION__,
655 p20.x, p20.y, p2Seed.x, p2Seed.y, p22.x, p22.y);
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000656#endif
caryclark@google.com9f602912013-01-24 21:47:16 +0000657}
caryclark@google.comf9502d72013-02-04 14:06:49 +0000658
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000659static void coincidentTest() {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000660#if 0
caryclark@google.comf9502d72013-02-04 14:06:49 +0000661 Cubic cubic1 = {{0, 1}, {0, 2}, {1, 0}, {1, 0}};
662 Cubic cubic2 = {{0, 1}, {0, 2}, {1, 0}, {6, 1}};
caryclark@google.comf9502d72013-02-04 14:06:49 +0000663#endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000664}
665
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000666void CubicIntersection_SelfTest() {
667 const Cubic selfSet[] = {
668 {{3.34,8.98}, {1.95,10.27}, {3.76,7.65}, {4.96,10.64}},
669 {{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 +0000670 {{6.71,3.14}, {7.99,2.75}, {8.27,1.96}, {6.35,3.57}},
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000671 {{12.81,7.27}, {7.22,6.98}, {12.49,8.97}, {11.42,6.18}},
672 };
673 size_t selfSetCount = sizeof(selfSet) / sizeof(selfSet[0]);
674 for (size_t index = 0; index < selfSetCount; ++index) {
675 const Cubic& cubic = selfSet[index];
676 #if ONE_OFF_DEBUG
677 SkTDArray<Quadratic> quads1;
678 cubic_to_quadratics(cubic, calcPrecision(cubic), quads1);
679 for (int index = 0; index < quads1.count(); ++index) {
680 const Quadratic& q = quads1[index];
681 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", q[0].x, q[0].y,
682 q[1].x, q[1].y, q[2].x, q[2].y);
683 }
684 SkDebugf("\n");
685 #endif
686 Intersections i;
687 int result = intersect(cubic, i);
688 SkASSERT(result == 1);
689 SkASSERT(i.used() == 1);
690 SkASSERT(!approximately_equal(i.fT[0][0], i.fT[1][0]));
691 _Point pt1 = xy_at_t(cubic, i.fT[0][0]);
692 _Point pt2 = xy_at_t(cubic, i.fT[1][0]);
693 SkASSERT(pt1.approximatelyEqual(pt2));
694 }
695}
696
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000697void CubicIntersection_Test() {
698 oneOffTests();
699 coincidentTest();
700 standardTestCases();
701}