blob: f9c4feb889d1da8bdca836c10306f829474e125f [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
caryclark@google.comd4c8e1e2013-03-05 14:13:13 +000014#define SHOW_ORIGINAL 1
15
caryclark@google.com639df892012-01-10 21:46:10 +000016const int firstCubicIntersectionTest = 9;
17
caryclark@google.com45a8fc62013-02-14 15:29:11 +000018static void standardTestCases() {
caryclark@google.com639df892012-01-10 21:46:10 +000019 for (size_t index = firstCubicIntersectionTest; index < tests_count; ++index) {
20 const Cubic& cubic1 = tests[index][0];
21 const Cubic& cubic2 = tests[index][1];
22 Cubic reduce1, reduce2;
caryclark@google.com47d73da2013-02-17 01:41:25 +000023 int order1 = reduceOrder(cubic1, reduce1, kReduceOrder_NoQuadraticsAllowed,
24 kReduceOrder_TreatAsFill);
25 int order2 = reduceOrder(cubic2, reduce2, kReduceOrder_NoQuadraticsAllowed,
26 kReduceOrder_TreatAsFill);
caryclark@google.com639df892012-01-10 21:46:10 +000027 if (order1 < 4) {
caryclark@google.com27accef2012-01-25 18:57:23 +000028 printf("%s [%d] cubic1 order=%d\n", __FUNCTION__, (int) index, order1);
29 continue;
caryclark@google.com639df892012-01-10 21:46:10 +000030 }
31 if (order2 < 4) {
caryclark@google.com27accef2012-01-25 18:57:23 +000032 printf("%s [%d] cubic2 order=%d\n", __FUNCTION__, (int) index, order2);
33 continue;
caryclark@google.com639df892012-01-10 21:46:10 +000034 }
caryclark@google.com27accef2012-01-25 18:57:23 +000035 if (implicit_matches(reduce1, reduce2)) {
36 printf("%s [%d] coincident\n", __FUNCTION__, (int) index);
37 continue;
38 }
39 Intersections tIntersections;
caryclark@google.comc6825902012-02-03 22:07:47 +000040 intersect(reduce1, reduce2, tIntersections);
caryclark@google.com27accef2012-01-25 18:57:23 +000041 if (!tIntersections.intersected()) {
42 printf("%s [%d] no intersection\n", __FUNCTION__, (int) index);
43 continue;
44 }
45 for (int pt = 0; pt < tIntersections.used(); ++pt) {
46 double tt1 = tIntersections.fT[0][pt];
47 double tx1, ty1;
48 xy_at_t(cubic1, tt1, tx1, ty1);
49 double tt2 = tIntersections.fT[1][pt];
50 double tx2, ty2;
51 xy_at_t(cubic2, tt2, tx2, ty2);
caryclark@google.com6d0032a2013-01-04 19:41:13 +000052 if (!AlmostEqualUlps(tx1, tx2)) {
caryclark@google.com27accef2012-01-25 18:57:23 +000053 printf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
54 __FUNCTION__, (int)index, pt, tt1, tx1, ty1, tt2, tx2, ty2);
55 }
caryclark@google.com6d0032a2013-01-04 19:41:13 +000056 if (!AlmostEqualUlps(ty1, ty2)) {
caryclark@google.com27accef2012-01-25 18:57:23 +000057 printf("%s [%d,%d] y!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
58 __FUNCTION__, (int)index, pt, tt1, tx1, ty1, tt2, tx2, ty2);
caryclark@google.com639df892012-01-10 21:46:10 +000059 }
60 }
61 }
62}
caryclark@google.com73ca6242013-01-17 21:02:47 +000063
caryclark@google.com73ca6242013-01-17 21:02:47 +000064static const Cubic testSet[] = {
caryclark@google.com45a8fc62013-02-14 15:29:11 +000065{{0,1}, {4,5}, {1,0}, {5,3}},
66{{0,1}, {3,5}, {1,0}, {5,4}},
67
68{{0, 1}, {1, 6}, {1, 0}, {1, 0}},
69{{0, 1}, {0, 1}, {1, 0}, {6, 1}},
70
71{{0,1}, {3,4}, {1,0}, {5,1}},
72{{0,1}, {1,5}, {1,0}, {4,3}},
73
74{{0,1}, {1,2}, {1,0}, {6,1}},
75{{0,1}, {1,6}, {1,0}, {2,1}},
76
skia.committer@gmail.com044679e2013-02-15 07:16:57 +000077{{0,1}, {0,5}, {1,0}, {4,0}},
caryclark@google.com45a8fc62013-02-14 15:29:11 +000078{{0,1}, {0,4}, {1,0}, {5,0}},
79
caryclark@google.combeda3892013-02-07 13:13:41 +000080{{0,1}, {3,4}, {1,0}, {3,0}},
81{{0,1}, {0,3}, {1,0}, {4,3}},
82
83{{0, 0}, {1, 2}, {3, 4}, {4, 4}},
84{{0, 0}, {1, 2}, {3, 4}, {4, 4}},
85{{4, 4}, {3, 4}, {1, 2}, {0, 0}},
86
87{{0,1}, {2,3}, {1,0}, {1,0}},
88{{0,1}, {0,1}, {1,0}, {3,2}},
89
90{{0,2}, {0,1}, {1,0}, {1,0}},
91{{0,1}, {0,1}, {2,0}, {1,0}},
92
caryclark@google.comf9502d72013-02-04 14:06:49 +000093{{0, 0}, {0, 1}, {1, 1}, {1, 0}},
94{{1, 0}, {0, 0}, {0, 1}, {1, 1}},
95
caryclark@google.comaa358312013-01-29 20:28:49 +000096{{0, 1}, {0, 2}, {1, 0}, {1, 0}},
97{{0, 1}, {0, 1}, {1, 0}, {2, 0}},
98
caryclark@google.comf9502d72013-02-04 14:06:49 +000099{{0, 1}, {1, 6}, {1, 0}, {2, 0}},
100{{0, 1}, {0, 2}, {1, 0}, {6, 1}},
101
102{{0, 1}, {5, 6}, {1, 0}, {1, 0}},
103{{0, 1}, {0, 1}, {1, 0}, {6, 5}},
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000104
105{{95.837747722788592, 45.025976907939643}, {16.564570095652982, 0.72959763963222402}, {63.209855865319199, 68.047528419665767}, {57.640240647662544, 59.524565264361243}},
106{{51.593891741518817, 38.53849970667553}, {62.34752929878772, 74.924924725166022}, {74.810149322641152, 34.17966562983564}, {29.368398119401373, 94.66719277886078}},
107
108{{39.765160968417838, 33.060396198677083}, {5.1922921581157908, 66.854301452103215}, {31.619281802149157, 25.269248720849514}, {81.541621071073038, 70.025341524754353}},
109{{46.078911165743556, 48.259962651999651}, {20.24450549867214, 49.403916182650214}, {0.26325131778756683, 24.46489805563581}, {15.915006546264051, 83.515023059917155}},
110
caryclark@google.com9f602912013-01-24 21:47:16 +0000111{{65.454505973241524, 93.881892270353575}, {45.867360264932437, 92.723972719499827}, {2.1464054482739447, 74.636369140183717}, {33.774068594804994, 40.770872887582925}},
112{{72.963387832494163, 95.659300729473728}, {11.809496633619768, 82.209921247423594}, {13.456139067865974, 57.329313623406605}, {36.060621606214262, 70.867335643091849}},
113
114{{32.484981432782945, 75.082940782924624}, {42.467313093350882, 48.131159948246157}, {3.5963115764764657, 43.208665839959245}, {79.442476890721579, 89.709102357602262}},
115{{18.98573861410177, 93.308887208490106}, {40.405250173250792, 91.039661826118675}, {8.0467721950480584, 42.100282172719147}, {40.883324221187891, 26.030185504830527}},
116
117{{7.5374809128872498, 82.441702896003477}, {22.444346930107265, 22.138854312775123}, {66.76091829629658, 50.753805856571446}, {78.193478508942519, 97.7932997968948}},
118{{97.700573130371311, 53.53260215070685}, {87.72443481149358, 84.575876772671876}, {19.215031396232092, 47.032676472809484}, {11.989686410869325, 10.659507480757082}},
119
120{{26.192053931854691, 9.8504326817814416}, {10.174241480498686, 98.476562741434464}, {21.177712558385782, 33.814968789841501}, {75.329030899018534, 55.02231980442177}},
121{{56.222082700683771, 24.54395039218662}, {95.589995289030483, 81.050822735322086}, {28.180450866082897, 28.837706255185282}, {60.128952916771617, 87.311672180570511}},
122
123{{42.449716172390481, 52.379709366885805}, {27.896043159019225, 48.797373636065686}, {92.770268299044233, 89.899302036454571}, {12.102066544863426, 99.43241951960718}},
124{{45.77532924980639, 45.958701495993274}, {37.458701356062065, 68.393691335056758}, {37.569326692060258, 27.673713456687381}, {60.674866037757539, 62.47349659096146}},
125
caryclark@google.com73ca6242013-01-17 21:02:47 +0000126{{67.426548091427676, 37.993772624988935}, {23.483695892376684, 90.476863174921306}, {35.597065061143162, 79.872482633158796}, {75.38634169631932, 18.244890038969412}},
127{{61.336508189019057, 82.693132843213675}, {44.639380902349664, 54.074825790745592}, {16.815615499771951, 20.049704667203923}, {41.866884958868326, 56.735503699973002}},
128
129{{67.4265481, 37.9937726}, {23.4836959, 90.4768632}, {35.5970651, 79.8724826}, {75.3863417, 18.24489}},
130{{61.3365082, 82.6931328}, {44.6393809, 54.0748258}, {16.8156155, 20.0497047}, {41.866885, 56.7355037}},
131
132{{18.1312339, 31.6473732}, {95.5711034, 63.5350219}, {92.3283165, 62.0158945}, {18.5656052, 32.1268808}},
133{{97.402018, 35.7169972}, {33.1127443, 25.8935163}, {1.13970027, 54.9424981}, {56.4860195, 60.529264}},
134};
135
136const size_t testSetCount = sizeof(testSet) / sizeof(testSet[0]);
137
caryclark@google.com47d73da2013-02-17 01:41:25 +0000138static const Cubic newTestSet[] = {
caryclark@google.comd4c8e1e2013-03-05 14:13:13 +0000139{{0,2}, {1,5}, {1,0}, {6,1}},
140{{0,1}, {1,6}, {2,0}, {5,1}},
141
142{{0,1}, {1,5}, {2,1}, {4,0}},
143{{1,2}, {0,4}, {1,0}, {5,1}},
144
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000145{{0,1}, {3,5}, {2,1}, {3,1}},
146{{1,2}, {1,3}, {1,0}, {5,3}},
147
skia.committer@gmail.com631cdcb2013-03-01 12:12:55 +0000148{{0,1}, {2,5}, {6,0}, {5,3}},
caryclark@google.com7ff5c842013-02-26 15:56:05 +0000149{{0,6}, {3,5}, {1,0}, {5,2}},
150
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000151{{0,1}, {3,6}, {1,0}, {5,2}},
152{{0,1}, {2,5}, {1,0}, {6,3}},
153
caryclark@google.com5e0500f2013-02-20 12:51:37 +0000154{{1,2},{5,6},{1,0},{1,0}},
155{{0,1},{0,1},{2,1},{6,5}},
156
skia.committer@gmail.comd454ec12013-02-21 07:15:03 +0000157{{0,6},{1,2},{1,0},{1,0}},
caryclark@google.com5e0500f2013-02-20 12:51:37 +0000158{{0,1},{0,1},{6,0},{2,1}},
159
caryclark@google.com47d73da2013-02-17 01:41:25 +0000160{{0,2},{0,1},{3,0},{1,0}},
161{{0,3},{0,1},{2,0},{1,0}},
162};
163
164const size_t newTestSetCount = sizeof(newTestSet) / sizeof(newTestSet[0]);
165
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000166#if 0
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000167static void oneOff(const Cubic& cubic1, const Cubic& cubic2) {
168 SkTDArray<Quadratic> quads1;
169 cubic_to_quadratics(cubic1, calcPrecision(cubic1), quads1);
caryclark@google.comd4c8e1e2013-03-05 14:13:13 +0000170#if SHOW_ORIGINAL
171 SkDebugf("computed quadratics given\n");
172 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}}, {%1.9g,%1.9g}},\n",
173 cubic1[0].x, cubic1[0].y, cubic1[1].x, cubic1[1].y,
174 cubic1[2].x, cubic1[2].y, cubic1[3].x, cubic1[3].y));
175 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}}, {%1.9g,%1.9g}},\n",
176 cubic2[0].x, cubic2[0].y, cubic2[1].x, cubic2[1].y,
177 cubic2[2].x, cubic2[2].y, cubic2[3].x, cubic2[3].y));
178#endif
caryclark@google.com9f602912013-01-24 21:47:16 +0000179#if ONE_OFF_DEBUG
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000180 for (int index = 0; index < quads1.count(); ++index) {
181 const Quadratic& q = quads1[index];
182 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", q[0].x, q[0].y,
183 q[1].x, q[1].y, q[2].x, q[2].y);
184 }
185 SkDebugf("\n");
caryclark@google.com9f602912013-01-24 21:47:16 +0000186#endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000187 SkTDArray<Quadratic> quads2;
188 cubic_to_quadratics(cubic2, calcPrecision(cubic2), quads2);
caryclark@google.com9f602912013-01-24 21:47:16 +0000189#if ONE_OFF_DEBUG
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000190 for (int index = 0; index < quads2.count(); ++index) {
191 const Quadratic& q = quads2[index];
192 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", q[0].x, q[0].y,
193 q[1].x, q[1].y, q[2].x, q[2].y);
194 }
195 SkDebugf("\n");
caryclark@google.com9f602912013-01-24 21:47:16 +0000196#endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000197 Intersections intersections2, intersections3;
198 intersect2(cubic1, cubic2, intersections2);
199 intersect3(cubic1, cubic2, intersections3);
200 int pt1, pt2, pt3;
201 bool found;
202 double tt1, tt2, last = -1;
203 _Point xy1, xy2;
204 for (pt1 = 0; pt1 < intersections2.used(); ++pt1) {
205 tt1 = intersections2.fT[0][pt1];
206 SkASSERT(!approximately_equal(last, tt1));
207 last = tt1;
208 xy_at_t(cubic1, tt1, xy1.x, xy1.y);
209 pt2 = intersections2.fFlip ? intersections2.used() - pt1 - 1 : pt1;
210 tt2 = intersections2.fT[1][pt2];
211 xy_at_t(cubic2, tt2, xy2.x, xy2.y);
212#if ONE_OFF_DEBUG
213 SkDebugf("%s t1=%1.9g (%1.9g, %1.9g) (%1.9g, %1.9g) (%1.9g, %1.9g) t2=%1.9g\n",
214 __FUNCTION__, tt1, xy1.x, xy1.y, intersections2.fPt[pt1].x,
215 intersections2.fPt[pt1].y, xy2.x, xy2.y, tt2);
216#endif
217 SkASSERT(xy1.approximatelyEqual(xy2));
218#if SK_DEBUG
219 found = false;
220 for (pt3 = 0; pt3 < intersections3.used(); ++pt3) {
221 if (roughly_equal(tt1, intersections3.fT[0][pt3])) {
222 found = true;
223 break;
224 }
225 }
226 SkASSERT(found);
227#endif
228 }
229 last = -1;
230 for (pt3 = 0; pt3 < intersections3.used(); ++pt3) {
231 found = false;
232 double tt3 = intersections3.fT[0][pt3];
233 SkASSERT(!approximately_equal(last, tt3));
234 last = tt3;
235 for (pt1 = 0; pt1 < intersections2.used(); ++pt1) {
236 if (approximately_equal(tt3, intersections2.fT[0][pt1])) {
237 found = true;
238 break;
239 }
240 }
241 if (!found) {
242 tt1 = intersections3.fT[0][pt3];
243 xy_at_t(cubic1, tt1, xy1.x, xy1.y);
244 pt2 = intersections3.fFlip ? intersections3.used() - pt3 - 1 : pt3;
245 tt2 = intersections3.fT[1][pt2];
246 xy_at_t(cubic2, tt2, xy2.x, xy2.y);
247 #if ONE_OFF_DEBUG
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000248 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 +0000249 __FUNCTION__, tt1, xy1.x, xy1.y, intersections3.fPt[pt1].x,
250 intersections3.fPt[pt1].y, xy2.x, xy2.y, tt2);
251 #endif
252 SkASSERT(xy1.approximatelyEqual(xy2));
253 SkDebugf("%s missing in intersect2\n", __FUNCTION__);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000254 }
255 }
256}
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000257#endif
caryclark@google.com73ca6242013-01-17 21:02:47 +0000258
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000259static void oneOff3(const Cubic& cubic1, const Cubic& cubic2) {
260 SkTDArray<Quadratic> quads1;
261 cubic_to_quadratics(cubic1, calcPrecision(cubic1), quads1);
262#if ONE_OFF_DEBUG
263 for (int index = 0; index < quads1.count(); ++index) {
264 const Quadratic& q = quads1[index];
265 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", q[0].x, q[0].y,
266 q[1].x, q[1].y, q[2].x, q[2].y);
267 }
268 SkDebugf("\n");
269#endif
270 SkTDArray<Quadratic> quads2;
271 cubic_to_quadratics(cubic2, calcPrecision(cubic2), quads2);
272#if ONE_OFF_DEBUG
273 for (int index = 0; index < quads2.count(); ++index) {
274 const Quadratic& q = quads2[index];
275 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", q[0].x, q[0].y,
276 q[1].x, q[1].y, q[2].x, q[2].y);
277 }
278 SkDebugf("\n");
279#endif
280 Intersections intersections3;
281 intersect3(cubic1, cubic2, intersections3);
282 int pt2, pt3;
283 double tt1, tt2, last = -1;
284 _Point xy1, xy2;
285 for (pt3 = 0; pt3 < intersections3.used(); ++pt3) {
286 double tt3 = intersections3.fT[0][pt3];
287 SkASSERT(!approximately_equal(last, tt3));
288 last = tt3;
289 tt1 = intersections3.fT[0][pt3];
290 xy_at_t(cubic1, tt1, xy1.x, xy1.y);
291 pt2 = intersections3.fFlip ? intersections3.used() - pt3 - 1 : pt3;
292 tt2 = intersections3.fT[1][pt2];
293 xy_at_t(cubic2, tt2, xy2.x, xy2.y);
294#if ONE_OFF_DEBUG
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000295 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 +0000296 __FUNCTION__, tt1, xy1.x, xy1.y, intersections3.fPt[pt3].x,
297 intersections3.fPt[pt3].y, xy2.x, xy2.y, tt2);
298#endif
299 SkASSERT(xy1.approximatelyEqual(xy2));
300 }
301}
302
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000303#if 0
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000304static int fails[][2] = { {0, 23}, // fails in intersect2 recursing
305 {2, 7}, // answers differ, but neither is correct ('3' is closer)
306 {3, 26}, // fails in intersect2 recursing
307 {4, 9}, // fails in intersect2 recursing
308 {4, 10}, // fails in intersect2 recursing
309 {10, 17}, // fails in intersect2 recursing
310 {12, 14}, // loops indefinitely
311 {12, 21}, // fails in intersect2 recursing
312 {13, 21}, // fails in intersect2 recursing
313 {14, 21}, // fails in intersect2 recursing
314 {17, 25}, // fails in intersect2 recursing
315 {23, 25}, // fails in intersect2 recursing
316};
317
318static int failCount = sizeof(fails) / sizeof(fails[0]);
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000319#endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000320
321static void oneOff(int outer, int inner) {
322 const Cubic& cubic1 = testSet[outer];
323 const Cubic& cubic2 = testSet[inner];
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000324#if 0
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000325 bool failing = false;
326 for (int i = 0; i < failCount; ++i) {
327 if ((fails[i][0] == outer && fails[i][1] == inner)
328 || (fails[i][1] == outer && fails[i][0] == inner)) {
329 failing = true;
330 break;
331 }
332 }
333 if (!failing) {
334 oneOff(cubic1, cubic2);
335 } else {
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000336#endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000337 oneOff3(cubic1, cubic2);
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000338// }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000339}
340
341void CubicIntersection_OneOffTest() {
342 oneOff(12, 14);
343}
344
caryclark@google.com47d73da2013-02-17 01:41:25 +0000345static void newOneOff(int outer, int inner) {
346 const Cubic& cubic1 = newTestSet[outer];
347 const Cubic& cubic2 = newTestSet[inner];
348 oneOff3(cubic1, cubic2);
349}
350
351void CubicIntersection_NewOneOffTest() {
352 newOneOff(0, 1);
353}
354
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000355static void oneOffTests() {
356 for (size_t outer = 0; outer < testSetCount - 1; ++outer) {
357 for (size_t inner = outer + 1; inner < testSetCount; ++inner) {
358 oneOff(outer, inner);
359 }
360 }
361}
362
363void CubicIntersection_OneOffTests() {
364 oneOffTests();
365}
366
caryclark@google.comf9502d72013-02-04 14:06:49 +0000367#define DEBUG_CRASH 0
caryclark@google.com73ca6242013-01-17 21:02:47 +0000368
369class CubicChopper {
370public:
371
372// only finds one intersection
373CubicChopper(const Cubic& c1, const Cubic& c2)
374 : cubic1(c1)
375 , cubic2(c2)
376 , depth(0) {
377}
378
379bool intersect(double minT1, double maxT1, double minT2, double maxT2) {
380 Cubic sub1, sub2;
381 // FIXME: carry last subdivide and reduceOrder result with cubic
382 sub_divide(cubic1, minT1, maxT1, sub1);
383 sub_divide(cubic2, minT2, maxT2, sub2);
384 Intersections i;
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000385 intersect3(sub1, sub2, i);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000386 if (i.used() == 0) {
387 return false;
388 }
389 double x1, y1, x2, y2;
390 t1 = minT1 + i.fT[0][0] * (maxT1 - minT1);
391 t2 = minT2 + i.fT[1][0] * (maxT2 - minT2);
392 xy_at_t(cubic1, t1, x1, y1);
393 xy_at_t(cubic2, t2, x2, y2);
394 if (AlmostEqualUlps(x1, x2) && AlmostEqualUlps(y1, y2)) {
395 return true;
396 }
397 double half1 = (minT1 + maxT1) / 2;
398 double half2 = (minT2 + maxT2) / 2;
399 ++depth;
400 bool result;
401 if (depth & 1) {
402 result = intersect(minT1, half1, minT2, maxT2) || intersect(half1, maxT1, minT2, maxT2)
403 || intersect(minT1, maxT1, minT2, half2) || intersect(minT1, maxT1, half2, maxT2);
404 } else {
405 result = intersect(minT1, maxT1, minT2, half2) || intersect(minT1, maxT1, half2, maxT2)
406 || intersect(minT1, half1, minT2, maxT2) || intersect(half1, maxT1, minT2, maxT2);
407 }
408 --depth;
409 return result;
410}
411
412const Cubic& cubic1;
413const Cubic& cubic2;
414double t1;
415double t2;
416int depth;
417};
418
419#define TRY_OLD 0 // old way fails on test == 1
420
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000421void CubicIntersection_RandTestOld() {
caryclark@google.com73ca6242013-01-17 21:02:47 +0000422 srand(0);
423 const int tests = 1000000; // 10000000;
424 double largestFactor = DBL_MAX;
425 for (int test = 0; test < tests; ++test) {
426 Cubic cubic1, cubic2;
427 for (int i = 0; i < 4; ++i) {
428 cubic1[i].x = (double) rand() / RAND_MAX * 100;
429 cubic1[i].y = (double) rand() / RAND_MAX * 100;
430 cubic2[i].x = (double) rand() / RAND_MAX * 100;
431 cubic2[i].y = (double) rand() / RAND_MAX * 100;
432 }
433 if (test == 2513) { // the pair crosses three times, but the quadratic approximation
434 continue; // only sees one -- should be OK to ignore the other two?
435 }
436 if (test == 12932) { // this exposes a weakness when one cubic touches the other but
437 continue; // does not touch the quad approximation. Captured in qc.htm as cubic15
438 }
439 #if DEBUG_CRASH
440 char str[1024];
441 sprintf(str, "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n"
442 "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n",
443 cubic1[0].x, cubic1[0].y, cubic1[1].x, cubic1[1].y, cubic1[2].x, cubic1[2].y,
444 cubic1[3].x, cubic1[3].y,
445 cubic2[0].x, cubic2[0].y, cubic2[1].x, cubic2[1].y, cubic2[2].x, cubic2[2].y,
446 cubic2[3].x, cubic2[3].y);
447 #endif
448 _Rect rect1, rect2;
449 rect1.setBounds(cubic1);
450 rect2.setBounds(cubic2);
451 bool boundsIntersect = rect1.left <= rect2.right && rect2.left <= rect2.right
452 && rect1.top <= rect2.bottom && rect2.top <= rect1.bottom;
453 Intersections i1, i2;
454 #if TRY_OLD
455 bool oldIntersects = intersect(cubic1, cubic2, i1);
456 #else
457 bool oldIntersects = false;
458 #endif
459 if (test == -1) {
460 SkDebugf("ready...\n");
461 }
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000462 bool newIntersects = intersect3(cubic1, cubic2, i2);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000463 if (!boundsIntersect && (oldIntersects || newIntersects)) {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000464 #if DEBUG_CRASH
caryclark@google.com73ca6242013-01-17 21:02:47 +0000465 SkDebugf("%s %d unexpected intersection boundsIntersect=%d oldIntersects=%d"
466 " newIntersects=%d\n%s %s\n", __FUNCTION__, test, boundsIntersect,
467 oldIntersects, newIntersects, __FUNCTION__, str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000468 #endif
caryclark@google.comaa358312013-01-29 20:28:49 +0000469 SkASSERT(0);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000470 }
471 if (oldIntersects && !newIntersects) {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000472 #if DEBUG_CRASH
caryclark@google.com73ca6242013-01-17 21:02:47 +0000473 SkDebugf("%s %d missing intersection oldIntersects=%d newIntersects=%d\n%s %s\n",
474 __FUNCTION__, test, oldIntersects, newIntersects, __FUNCTION__, str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000475 #endif
caryclark@google.comaa358312013-01-29 20:28:49 +0000476 SkASSERT(0);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000477 }
478 if (!oldIntersects && !newIntersects) {
479 continue;
480 }
481 if (i2.used() > 1) {
482 continue;
483 // just look at single intercepts for simplicity
484 }
485 Intersections self1, self2; // self-intersect checks
486 if (intersect(cubic1, self1)) {
487 continue;
488 }
489 if (intersect(cubic2, self2)) {
490 continue;
491 }
492 // binary search for range necessary to enclose real intersection
493 CubicChopper c(cubic1, cubic2);
494 bool result = c.intersect(0, 1, 0, 1);
495 if (!result) {
496 // FIXME: a failure here probably means that a core routine used by CubicChopper is failing
497 continue;
498 }
499 double delta1 = fabs(c.t1 - i2.fT[0][0]);
500 double delta2 = fabs(c.t2 - i2.fT[1][0]);
501 double calc1 = calcPrecision(cubic1);
502 double calc2 = calcPrecision(cubic2);
503 double factor1 = calc1 / delta1;
504 double factor2 = calc2 / delta2;
505 SkDebugf("%s %d calc1=%1.9g delta1=%1.9g factor1=%1.9g calc2=%1.9g delta2=%1.9g"
506 " factor2=%1.9g\n", __FUNCTION__, test,
507 calc1, delta1, factor1, calc2, delta2, factor2);
508 if (factor1 < largestFactor) {
509 SkDebugf("WE HAVE A WINNER! %1.9g\n", factor1);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000510 #if DEBUG_CRASH
caryclark@google.com73ca6242013-01-17 21:02:47 +0000511 SkDebugf("%s\n", str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000512 #endif
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000513 oneOff3(cubic1, cubic2);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000514 largestFactor = factor1;
515 }
516 if (factor2 < largestFactor) {
517 SkDebugf("WE HAVE A WINNER! %1.9g\n", factor2);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000518 #if DEBUG_CRASH
caryclark@google.com73ca6242013-01-17 21:02:47 +0000519 SkDebugf("%s\n", str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000520 #endif
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000521 oneOff3(cubic1, cubic2);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000522 largestFactor = factor2;
523 }
524 }
525}
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000526
527void CubicIntersection_RandTest() {
528 srand(0);
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000529 const int tests = 10000000;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000530 for (int test = 0; test < tests; ++test) {
531 Cubic cubic1, cubic2;
532 for (int i = 0; i < 4; ++i) {
533 cubic1[i].x = (double) rand() / RAND_MAX * 100;
534 cubic1[i].y = (double) rand() / RAND_MAX * 100;
535 cubic2[i].x = (double) rand() / RAND_MAX * 100;
536 cubic2[i].y = (double) rand() / RAND_MAX * 100;
537 }
538 #if DEBUG_CRASH
539 char str[1024];
540 sprintf(str, "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n"
541 "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n",
542 cubic1[0].x, cubic1[0].y, cubic1[1].x, cubic1[1].y, cubic1[2].x, cubic1[2].y,
543 cubic1[3].x, cubic1[3].y,
544 cubic2[0].x, cubic2[0].y, cubic2[1].x, cubic2[1].y, cubic2[2].x, cubic2[2].y,
545 cubic2[3].x, cubic2[3].y);
546 #endif
547 _Rect rect1, rect2;
548 rect1.setBounds(cubic1);
549 rect2.setBounds(cubic2);
550 bool boundsIntersect = rect1.left <= rect2.right && rect2.left <= rect2.right
551 && rect1.top <= rect2.bottom && rect2.top <= rect1.bottom;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000552 if (test == -1) {
553 SkDebugf("ready...\n");
554 }
555 Intersections intersections2;
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000556 bool newIntersects = intersect3(cubic1, cubic2, intersections2);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000557 if (!boundsIntersect && newIntersects) {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000558 #if DEBUG_CRASH
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000559 SkDebugf("%s %d unexpected intersection boundsIntersect=%d "
560 " newIntersects=%d\n%s %s\n", __FUNCTION__, test, boundsIntersect,
561 newIntersects, __FUNCTION__, str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000562 #endif
caryclark@google.comaa358312013-01-29 20:28:49 +0000563 SkASSERT(0);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000564 }
565 for (int pt = 0; pt < intersections2.used(); ++pt) {
566 double tt1 = intersections2.fT[0][pt];
567 _Point xy1, xy2;
568 xy_at_t(cubic1, tt1, xy1.x, xy1.y);
569 int pt2 = intersections2.fFlip ? intersections2.used() - pt - 1 : pt;
570 double tt2 = intersections2.fT[1][pt2];
571 xy_at_t(cubic2, tt2, xy2.x, xy2.y);
caryclark@google.com9f602912013-01-24 21:47:16 +0000572 #if 0
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000573 SkDebugf("%s t1=%1.9g (%1.9g, %1.9g) (%1.9g, %1.9g) t2=%1.9g\n", __FUNCTION__,
574 tt1, xy1.x, xy1.y, xy2.x, xy2.y, tt2);
caryclark@google.com9f602912013-01-24 21:47:16 +0000575 #endif
caryclark@google.comaa358312013-01-29 20:28:49 +0000576 SkASSERT(xy1.approximatelyEqual(xy2));
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000577 }
578 }
579}
caryclark@google.com9f602912013-01-24 21:47:16 +0000580
caryclark@google.comd4c8e1e2013-03-05 14:13:13 +0000581static void intersectionFinder(int index0, int index1, double t1Seed, double t2Seed,
582 double t1Step, double t2Step) {
583 const Cubic& cubic1 = newTestSet[index0];
584 const Cubic& cubic2 = newTestSet[index1];
caryclark@google.comf9502d72013-02-04 14:06:49 +0000585 _Point t1[3], t2[3];
586 bool toggle = true;
587 do {
588 xy_at_t(cubic1, t1Seed - t1Step, t1[0].x, t1[0].y);
589 xy_at_t(cubic1, t1Seed, t1[1].x, t1[1].y);
590 xy_at_t(cubic1, t1Seed + t1Step, t1[2].x, t1[2].y);
591 xy_at_t(cubic2, t2Seed - t2Step, t2[0].x, t2[0].y);
592 xy_at_t(cubic2, t2Seed, t2[1].x, t2[1].y);
593 xy_at_t(cubic2, t2Seed + t2Step, t2[2].x, t2[2].y);
594 double dist[3][3];
595 dist[1][1] = t1[1].distance(t2[1]);
596 int best_i = 1, best_j = 1;
597 for (int i = 0; i < 3; ++i) {
598 for (int j = 0; j < 3; ++j) {
599 if (i == 1 && j == 1) {
600 continue;
601 }
602 dist[i][j] = t1[i].distance(t2[j]);
603 if (dist[best_i][best_j] > dist[i][j]) {
604 best_i = i;
605 best_j = j;
606 }
607 }
608 }
609 if (best_i == 0) {
610 t1Seed -= t1Step;
611 } else if (best_i == 2) {
612 t1Seed += t1Step;
613 }
614 if (best_j == 0) {
615 t2Seed -= t2Step;
616 } else if (best_j == 2) {
617 t2Seed += t2Step;
618 }
619 if (best_i == 1 && best_j == 1) {
620 if ((toggle ^= true)) {
621 t1Step /= 2;
622 } else {
623 t2Step /= 2;
624 }
625 }
626 } while (!t1[1].approximatelyEqual(t2[1]));
627 t1Step = t2Step = 0.1;
628 double t10 = t1Seed - t1Step * 2;
629 double t12 = t1Seed + t1Step * 2;
630 double t20 = t2Seed - t2Step * 2;
631 double t22 = t2Seed + t2Step * 2;
632 _Point test;
633 while (!approximately_zero(t1Step)) {
634 xy_at_t(cubic1, t10, test.x, test.y);
635 t10 += t1[1].approximatelyEqual(test) ? -t1Step : t1Step;
636 t1Step /= 2;
caryclark@google.com9f602912013-01-24 21:47:16 +0000637 }
caryclark@google.comf9502d72013-02-04 14:06:49 +0000638 t1Step = 0.1;
639 while (!approximately_zero(t1Step)) {
640 xy_at_t(cubic1, t12, test.x, test.y);
641 t12 -= t1[1].approximatelyEqual(test) ? -t1Step : t1Step;
642 t1Step /= 2;
643 }
644 while (!approximately_zero(t2Step)) {
645 xy_at_t(cubic2, t20, test.x, test.y);
646 t20 += t2[1].approximatelyEqual(test) ? -t2Step : t2Step;
647 t2Step /= 2;
648 }
649 t2Step = 0.1;
650 while (!approximately_zero(t2Step)) {
651 xy_at_t(cubic2, t22, test.x, test.y);
652 t22 -= t2[1].approximatelyEqual(test) ? -t2Step : t2Step;
653 t2Step /= 2;
654 }
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000655#if ONE_OFF_DEBUG
caryclark@google.comf9502d72013-02-04 14:06:49 +0000656 SkDebugf("%s t1=(%1.9g<%1.9g<%1.9g) t2=(%1.9g<%1.9g<%1.9g)\n", __FUNCTION__,
657 t10, t1Seed, t12, t20, t2Seed, t22);
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000658 _Point p10 = xy_at_t(cubic1, t10);
659 _Point p1Seed = xy_at_t(cubic1, t1Seed);
660 _Point p12 = xy_at_t(cubic1, t12);
661 SkDebugf("%s p1=(%1.9g,%1.9g)<(%1.9g,%1.9g)<(%1.9g,%1.9g)\n", __FUNCTION__,
662 p10.x, p10.y, p1Seed.x, p1Seed.y, p12.x, p12.y);
663 _Point p20 = xy_at_t(cubic2, t20);
664 _Point p2Seed = xy_at_t(cubic2, t2Seed);
665 _Point p22 = xy_at_t(cubic2, t22);
666 SkDebugf("%s p2=(%1.9g,%1.9g)<(%1.9g,%1.9g)<(%1.9g,%1.9g)\n", __FUNCTION__,
667 p20.x, p20.y, p2Seed.x, p2Seed.y, p22.x, p22.y);
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000668#endif
caryclark@google.com9f602912013-01-24 21:47:16 +0000669}
caryclark@google.comf9502d72013-02-04 14:06:49 +0000670
caryclark@google.comd4c8e1e2013-03-05 14:13:13 +0000671void CubicIntersection_IntersectionFinder() {
672
673 double t1Seed = 0.867315861;
674 double t2Seed = 0.912837717;
675 double t1Step = 0.01;
676 double t2Step = 0.01;
677 intersectionFinder(0, 1, t1Seed, t2Seed, t1Step, t2Step);
678 intersectionFinder(0, 1, 0.830515061, 0.860978176, t1Step, t2Step);
679}
680
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000681static void coincidentTest() {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000682#if 0
caryclark@google.comf9502d72013-02-04 14:06:49 +0000683 Cubic cubic1 = {{0, 1}, {0, 2}, {1, 0}, {1, 0}};
684 Cubic cubic2 = {{0, 1}, {0, 2}, {1, 0}, {6, 1}};
caryclark@google.comf9502d72013-02-04 14:06:49 +0000685#endif
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000686}
687
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000688void CubicIntersection_SelfTest() {
689 const Cubic selfSet[] = {
690 {{3.34,8.98}, {1.95,10.27}, {3.76,7.65}, {4.96,10.64}},
691 {{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 +0000692 {{6.71,3.14}, {7.99,2.75}, {8.27,1.96}, {6.35,3.57}},
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000693 {{12.81,7.27}, {7.22,6.98}, {12.49,8.97}, {11.42,6.18}},
694 };
695 size_t selfSetCount = sizeof(selfSet) / sizeof(selfSet[0]);
696 for (size_t index = 0; index < selfSetCount; ++index) {
697 const Cubic& cubic = selfSet[index];
698 #if ONE_OFF_DEBUG
699 SkTDArray<Quadratic> quads1;
700 cubic_to_quadratics(cubic, calcPrecision(cubic), quads1);
701 for (int index = 0; index < quads1.count(); ++index) {
702 const Quadratic& q = quads1[index];
703 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", q[0].x, q[0].y,
704 q[1].x, q[1].y, q[2].x, q[2].y);
705 }
706 SkDebugf("\n");
707 #endif
708 Intersections i;
709 int result = intersect(cubic, i);
710 SkASSERT(result == 1);
711 SkASSERT(i.used() == 1);
712 SkASSERT(!approximately_equal(i.fT[0][0], i.fT[1][0]));
713 _Point pt1 = xy_at_t(cubic, i.fT[0][0]);
714 _Point pt2 = xy_at_t(cubic, i.fT[1][0]);
715 SkASSERT(pt1.approximatelyEqual(pt2));
716 }
717}
718
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000719void CubicIntersection_Test() {
720 oneOffTests();
721 coincidentTest();
722 standardTestCases();
723}