blob: ae52a25d4778e517acb44ca7096f9254cd40019f [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
16void CubicIntersection_Test() {
17 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;
21 int order1 = reduceOrder(cubic1, reduce1, kReduceOrder_NoQuadraticsAllowed);
22 int order2 = reduceOrder(cubic2, reduce2, kReduceOrder_NoQuadraticsAllowed);
23 if (order1 < 4) {
caryclark@google.com27accef2012-01-25 18:57:23 +000024 printf("%s [%d] cubic1 order=%d\n", __FUNCTION__, (int) index, order1);
25 continue;
caryclark@google.com639df892012-01-10 21:46:10 +000026 }
27 if (order2 < 4) {
caryclark@google.com27accef2012-01-25 18:57:23 +000028 printf("%s [%d] cubic2 order=%d\n", __FUNCTION__, (int) index, order2);
29 continue;
caryclark@google.com639df892012-01-10 21:46:10 +000030 }
caryclark@google.com27accef2012-01-25 18:57:23 +000031 if (implicit_matches(reduce1, reduce2)) {
32 printf("%s [%d] coincident\n", __FUNCTION__, (int) index);
33 continue;
34 }
35 Intersections tIntersections;
caryclark@google.comc6825902012-02-03 22:07:47 +000036 intersect(reduce1, reduce2, tIntersections);
caryclark@google.com27accef2012-01-25 18:57:23 +000037 if (!tIntersections.intersected()) {
38 printf("%s [%d] no intersection\n", __FUNCTION__, (int) index);
39 continue;
40 }
41 for (int pt = 0; pt < tIntersections.used(); ++pt) {
42 double tt1 = tIntersections.fT[0][pt];
43 double tx1, ty1;
44 xy_at_t(cubic1, tt1, tx1, ty1);
45 double tt2 = tIntersections.fT[1][pt];
46 double tx2, ty2;
47 xy_at_t(cubic2, tt2, tx2, ty2);
caryclark@google.com6d0032a2013-01-04 19:41:13 +000048 if (!AlmostEqualUlps(tx1, tx2)) {
caryclark@google.com27accef2012-01-25 18:57:23 +000049 printf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
50 __FUNCTION__, (int)index, pt, tt1, tx1, ty1, tt2, tx2, ty2);
51 }
caryclark@google.com6d0032a2013-01-04 19:41:13 +000052 if (!AlmostEqualUlps(ty1, ty2)) {
caryclark@google.com27accef2012-01-25 18:57:23 +000053 printf("%s [%d,%d] y!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
54 __FUNCTION__, (int)index, pt, tt1, tx1, ty1, tt2, tx2, ty2);
caryclark@google.com639df892012-01-10 21:46:10 +000055 }
56 }
57 }
58}
caryclark@google.com73ca6242013-01-17 21:02:47 +000059
caryclark@google.com86adc0d2013-01-30 12:50:38 +000060#define ONE_OFF_DEBUG 0
caryclark@google.com9f602912013-01-24 21:47:16 +000061
caryclark@google.com73ca6242013-01-17 21:02:47 +000062static void oneOff(const Cubic& cubic1, const Cubic& cubic2) {
63 SkTDArray<Quadratic> quads1;
64 cubic_to_quadratics(cubic1, calcPrecision(cubic1), quads1);
caryclark@google.com9f602912013-01-24 21:47:16 +000065#if ONE_OFF_DEBUG
caryclark@google.com73ca6242013-01-17 21:02:47 +000066 for (int index = 0; index < quads1.count(); ++index) {
67 const Quadratic& q = quads1[index];
caryclark@google.com9f602912013-01-24 21:47:16 +000068 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", q[0].x, q[0].y,
caryclark@google.com73ca6242013-01-17 21:02:47 +000069 q[1].x, q[1].y, q[2].x, q[2].y);
70 }
71 SkDebugf("\n");
caryclark@google.com9f602912013-01-24 21:47:16 +000072#endif
caryclark@google.com73ca6242013-01-17 21:02:47 +000073 SkTDArray<Quadratic> quads2;
74 cubic_to_quadratics(cubic2, calcPrecision(cubic2), quads2);
caryclark@google.com9f602912013-01-24 21:47:16 +000075#if ONE_OFF_DEBUG
caryclark@google.com73ca6242013-01-17 21:02:47 +000076 for (int index = 0; index < quads2.count(); ++index) {
77 const Quadratic& q = quads2[index];
caryclark@google.com9f602912013-01-24 21:47:16 +000078 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", q[0].x, q[0].y,
caryclark@google.com73ca6242013-01-17 21:02:47 +000079 q[1].x, q[1].y, q[2].x, q[2].y);
80 }
81 SkDebugf("\n");
caryclark@google.com9f602912013-01-24 21:47:16 +000082#endif
caryclark@google.com73ca6242013-01-17 21:02:47 +000083 Intersections intersections2;
84 intersect2(cubic1, cubic2, intersections2);
85 for (int pt = 0; pt < intersections2.used(); ++pt) {
86 double tt1 = intersections2.fT[0][pt];
caryclark@google.com05c4bad2013-01-19 13:22:39 +000087 _Point xy1, xy2;
88 xy_at_t(cubic1, tt1, xy1.x, xy1.y);
caryclark@google.com73ca6242013-01-17 21:02:47 +000089 int pt2 = intersections2.fFlip ? intersections2.used() - pt - 1 : pt;
90 double tt2 = intersections2.fT[1][pt2];
caryclark@google.com05c4bad2013-01-19 13:22:39 +000091 xy_at_t(cubic2, tt2, xy2.x, xy2.y);
caryclark@google.com9f602912013-01-24 21:47:16 +000092#if ONE_OFF_DEBUG
caryclark@google.com73ca6242013-01-17 21:02:47 +000093 SkDebugf("%s t1=%1.9g (%1.9g, %1.9g) (%1.9g, %1.9g) t2=%1.9g\n", __FUNCTION__,
caryclark@google.com05c4bad2013-01-19 13:22:39 +000094 tt1, xy1.x, xy1.y, xy2.x, xy2.y, tt2);
caryclark@google.com9f602912013-01-24 21:47:16 +000095#endif
caryclark@google.comaa358312013-01-29 20:28:49 +000096 SkASSERT(xy1.approximatelyEqual(xy2));
caryclark@google.com73ca6242013-01-17 21:02:47 +000097 }
caryclark@google.combeda3892013-02-07 13:13:41 +000098 for (int pt = 0; pt < intersections2.coincidentUsed(); ++pt) {
99 double tt1 = intersections2.fCoincidentT[0][pt];
100 _Point xy1, xy2;
101 xy_at_t(cubic1, tt1, xy1.x, xy1.y);
102 int pt2 = intersections2.fFlip ? intersections2.used() - pt - 1 : pt;
103 double tt2 = intersections2.fCoincidentT[1][pt2];
104 xy_at_t(cubic2, tt2, xy2.x, xy2.y);
105#if ONE_OFF_DEBUG
106 SkDebugf("%s t1=%1.9g (%1.9g, %1.9g) (%1.9g, %1.9g) t2=%1.9g\n", __FUNCTION__,
107 tt1, xy1.x, xy1.y, xy2.x, xy2.y, tt2);
108#endif
109 SkASSERT(xy1.approximatelyEqual(xy2));
110 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000111}
112
113static const Cubic testSet[] = {
caryclark@google.combeda3892013-02-07 13:13:41 +0000114{{0,1}, {3,4}, {1,0}, {3,0}},
115{{0,1}, {0,3}, {1,0}, {4,3}},
116
117{{0, 0}, {1, 2}, {3, 4}, {4, 4}},
118{{0, 0}, {1, 2}, {3, 4}, {4, 4}},
119{{4, 4}, {3, 4}, {1, 2}, {0, 0}},
120
121{{0,1}, {2,3}, {1,0}, {1,0}},
122{{0,1}, {0,1}, {1,0}, {3,2}},
123
124{{0,2}, {0,1}, {1,0}, {1,0}},
125{{0,1}, {0,1}, {2,0}, {1,0}},
126
caryclark@google.comf9502d72013-02-04 14:06:49 +0000127{{0, 0}, {0, 1}, {1, 1}, {1, 0}},
128{{1, 0}, {0, 0}, {0, 1}, {1, 1}},
129
caryclark@google.comaa358312013-01-29 20:28:49 +0000130{{0, 1}, {0, 2}, {1, 0}, {1, 0}},
131{{0, 1}, {0, 1}, {1, 0}, {2, 0}},
132
caryclark@google.comf9502d72013-02-04 14:06:49 +0000133{{0, 1}, {1, 6}, {1, 0}, {2, 0}},
134{{0, 1}, {0, 2}, {1, 0}, {6, 1}},
135
136{{0, 1}, {5, 6}, {1, 0}, {1, 0}},
137{{0, 1}, {0, 1}, {1, 0}, {6, 5}},
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000138
139{{95.837747722788592, 45.025976907939643}, {16.564570095652982, 0.72959763963222402}, {63.209855865319199, 68.047528419665767}, {57.640240647662544, 59.524565264361243}},
140{{51.593891741518817, 38.53849970667553}, {62.34752929878772, 74.924924725166022}, {74.810149322641152, 34.17966562983564}, {29.368398119401373, 94.66719277886078}},
141
142{{39.765160968417838, 33.060396198677083}, {5.1922921581157908, 66.854301452103215}, {31.619281802149157, 25.269248720849514}, {81.541621071073038, 70.025341524754353}},
143{{46.078911165743556, 48.259962651999651}, {20.24450549867214, 49.403916182650214}, {0.26325131778756683, 24.46489805563581}, {15.915006546264051, 83.515023059917155}},
144
caryclark@google.com9f602912013-01-24 21:47:16 +0000145{{65.454505973241524, 93.881892270353575}, {45.867360264932437, 92.723972719499827}, {2.1464054482739447, 74.636369140183717}, {33.774068594804994, 40.770872887582925}},
146{{72.963387832494163, 95.659300729473728}, {11.809496633619768, 82.209921247423594}, {13.456139067865974, 57.329313623406605}, {36.060621606214262, 70.867335643091849}},
147
148{{32.484981432782945, 75.082940782924624}, {42.467313093350882, 48.131159948246157}, {3.5963115764764657, 43.208665839959245}, {79.442476890721579, 89.709102357602262}},
149{{18.98573861410177, 93.308887208490106}, {40.405250173250792, 91.039661826118675}, {8.0467721950480584, 42.100282172719147}, {40.883324221187891, 26.030185504830527}},
150
151{{7.5374809128872498, 82.441702896003477}, {22.444346930107265, 22.138854312775123}, {66.76091829629658, 50.753805856571446}, {78.193478508942519, 97.7932997968948}},
152{{97.700573130371311, 53.53260215070685}, {87.72443481149358, 84.575876772671876}, {19.215031396232092, 47.032676472809484}, {11.989686410869325, 10.659507480757082}},
153
154{{26.192053931854691, 9.8504326817814416}, {10.174241480498686, 98.476562741434464}, {21.177712558385782, 33.814968789841501}, {75.329030899018534, 55.02231980442177}},
155{{56.222082700683771, 24.54395039218662}, {95.589995289030483, 81.050822735322086}, {28.180450866082897, 28.837706255185282}, {60.128952916771617, 87.311672180570511}},
156
157{{42.449716172390481, 52.379709366885805}, {27.896043159019225, 48.797373636065686}, {92.770268299044233, 89.899302036454571}, {12.102066544863426, 99.43241951960718}},
158{{45.77532924980639, 45.958701495993274}, {37.458701356062065, 68.393691335056758}, {37.569326692060258, 27.673713456687381}, {60.674866037757539, 62.47349659096146}},
159
caryclark@google.com73ca6242013-01-17 21:02:47 +0000160{{67.426548091427676, 37.993772624988935}, {23.483695892376684, 90.476863174921306}, {35.597065061143162, 79.872482633158796}, {75.38634169631932, 18.244890038969412}},
161{{61.336508189019057, 82.693132843213675}, {44.639380902349664, 54.074825790745592}, {16.815615499771951, 20.049704667203923}, {41.866884958868326, 56.735503699973002}},
162
163{{67.4265481, 37.9937726}, {23.4836959, 90.4768632}, {35.5970651, 79.8724826}, {75.3863417, 18.24489}},
164{{61.3365082, 82.6931328}, {44.6393809, 54.0748258}, {16.8156155, 20.0497047}, {41.866885, 56.7355037}},
165
166{{18.1312339, 31.6473732}, {95.5711034, 63.5350219}, {92.3283165, 62.0158945}, {18.5656052, 32.1268808}},
167{{97.402018, 35.7169972}, {33.1127443, 25.8935163}, {1.13970027, 54.9424981}, {56.4860195, 60.529264}},
168};
169
170const size_t testSetCount = sizeof(testSet) / sizeof(testSet[0]);
171
172void CubicIntersection_OneOffTest() {
173 for (size_t outer = 0; outer < testSetCount - 1; ++outer) {
caryclark@google.com9f602912013-01-24 21:47:16 +0000174#if ONE_OFF_DEBUG
caryclark@google.com73ca6242013-01-17 21:02:47 +0000175 SkDebugf("%s quads1[%d]\n", __FUNCTION__, outer);
caryclark@google.com9f602912013-01-24 21:47:16 +0000176#endif
caryclark@google.com73ca6242013-01-17 21:02:47 +0000177 const Cubic& cubic1 = testSet[outer];
178 for (size_t inner = outer + 1; inner < testSetCount; ++inner) {
caryclark@google.com9f602912013-01-24 21:47:16 +0000179#if ONE_OFF_DEBUG
caryclark@google.com73ca6242013-01-17 21:02:47 +0000180 SkDebugf("%s quads2[%d]\n", __FUNCTION__, inner);
caryclark@google.com9f602912013-01-24 21:47:16 +0000181#endif
caryclark@google.com73ca6242013-01-17 21:02:47 +0000182 const Cubic& cubic2 = testSet[inner];
183 oneOff(cubic1, cubic2);
184 }
185 }
186}
187
caryclark@google.comf9502d72013-02-04 14:06:49 +0000188#define DEBUG_CRASH 0
caryclark@google.com73ca6242013-01-17 21:02:47 +0000189
190class CubicChopper {
191public:
192
193// only finds one intersection
194CubicChopper(const Cubic& c1, const Cubic& c2)
195 : cubic1(c1)
196 , cubic2(c2)
197 , depth(0) {
198}
199
200bool intersect(double minT1, double maxT1, double minT2, double maxT2) {
201 Cubic sub1, sub2;
202 // FIXME: carry last subdivide and reduceOrder result with cubic
203 sub_divide(cubic1, minT1, maxT1, sub1);
204 sub_divide(cubic2, minT2, maxT2, sub2);
205 Intersections i;
206 intersect2(sub1, sub2, i);
207 if (i.used() == 0) {
208 return false;
209 }
210 double x1, y1, x2, y2;
211 t1 = minT1 + i.fT[0][0] * (maxT1 - minT1);
212 t2 = minT2 + i.fT[1][0] * (maxT2 - minT2);
213 xy_at_t(cubic1, t1, x1, y1);
214 xy_at_t(cubic2, t2, x2, y2);
215 if (AlmostEqualUlps(x1, x2) && AlmostEqualUlps(y1, y2)) {
216 return true;
217 }
218 double half1 = (minT1 + maxT1) / 2;
219 double half2 = (minT2 + maxT2) / 2;
220 ++depth;
221 bool result;
222 if (depth & 1) {
223 result = intersect(minT1, half1, minT2, maxT2) || intersect(half1, maxT1, minT2, maxT2)
224 || intersect(minT1, maxT1, minT2, half2) || intersect(minT1, maxT1, half2, maxT2);
225 } else {
226 result = intersect(minT1, maxT1, minT2, half2) || intersect(minT1, maxT1, half2, maxT2)
227 || intersect(minT1, half1, minT2, maxT2) || intersect(half1, maxT1, minT2, maxT2);
228 }
229 --depth;
230 return result;
231}
232
233const Cubic& cubic1;
234const Cubic& cubic2;
235double t1;
236double t2;
237int depth;
238};
239
240#define TRY_OLD 0 // old way fails on test == 1
241
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000242void CubicIntersection_RandTestOld() {
caryclark@google.com73ca6242013-01-17 21:02:47 +0000243 srand(0);
244 const int tests = 1000000; // 10000000;
245 double largestFactor = DBL_MAX;
246 for (int test = 0; test < tests; ++test) {
247 Cubic cubic1, cubic2;
248 for (int i = 0; i < 4; ++i) {
249 cubic1[i].x = (double) rand() / RAND_MAX * 100;
250 cubic1[i].y = (double) rand() / RAND_MAX * 100;
251 cubic2[i].x = (double) rand() / RAND_MAX * 100;
252 cubic2[i].y = (double) rand() / RAND_MAX * 100;
253 }
254 if (test == 2513) { // the pair crosses three times, but the quadratic approximation
255 continue; // only sees one -- should be OK to ignore the other two?
256 }
257 if (test == 12932) { // this exposes a weakness when one cubic touches the other but
258 continue; // does not touch the quad approximation. Captured in qc.htm as cubic15
259 }
260 #if DEBUG_CRASH
261 char str[1024];
262 sprintf(str, "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n"
263 "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n",
264 cubic1[0].x, cubic1[0].y, cubic1[1].x, cubic1[1].y, cubic1[2].x, cubic1[2].y,
265 cubic1[3].x, cubic1[3].y,
266 cubic2[0].x, cubic2[0].y, cubic2[1].x, cubic2[1].y, cubic2[2].x, cubic2[2].y,
267 cubic2[3].x, cubic2[3].y);
268 #endif
269 _Rect rect1, rect2;
270 rect1.setBounds(cubic1);
271 rect2.setBounds(cubic2);
272 bool boundsIntersect = rect1.left <= rect2.right && rect2.left <= rect2.right
273 && rect1.top <= rect2.bottom && rect2.top <= rect1.bottom;
274 Intersections i1, i2;
275 #if TRY_OLD
276 bool oldIntersects = intersect(cubic1, cubic2, i1);
277 #else
278 bool oldIntersects = false;
279 #endif
280 if (test == -1) {
281 SkDebugf("ready...\n");
282 }
283 bool newIntersects = intersect2(cubic1, cubic2, i2);
284 if (!boundsIntersect && (oldIntersects || newIntersects)) {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000285 #if DEBUG_CRASH
caryclark@google.com73ca6242013-01-17 21:02:47 +0000286 SkDebugf("%s %d unexpected intersection boundsIntersect=%d oldIntersects=%d"
287 " newIntersects=%d\n%s %s\n", __FUNCTION__, test, boundsIntersect,
288 oldIntersects, newIntersects, __FUNCTION__, str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000289 #endif
caryclark@google.comaa358312013-01-29 20:28:49 +0000290 SkASSERT(0);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000291 }
292 if (oldIntersects && !newIntersects) {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000293 #if DEBUG_CRASH
caryclark@google.com73ca6242013-01-17 21:02:47 +0000294 SkDebugf("%s %d missing intersection oldIntersects=%d newIntersects=%d\n%s %s\n",
295 __FUNCTION__, test, oldIntersects, newIntersects, __FUNCTION__, str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000296 #endif
caryclark@google.comaa358312013-01-29 20:28:49 +0000297 SkASSERT(0);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000298 }
299 if (!oldIntersects && !newIntersects) {
300 continue;
301 }
302 if (i2.used() > 1) {
303 continue;
304 // just look at single intercepts for simplicity
305 }
306 Intersections self1, self2; // self-intersect checks
307 if (intersect(cubic1, self1)) {
308 continue;
309 }
310 if (intersect(cubic2, self2)) {
311 continue;
312 }
313 // binary search for range necessary to enclose real intersection
314 CubicChopper c(cubic1, cubic2);
315 bool result = c.intersect(0, 1, 0, 1);
316 if (!result) {
317 // FIXME: a failure here probably means that a core routine used by CubicChopper is failing
318 continue;
319 }
320 double delta1 = fabs(c.t1 - i2.fT[0][0]);
321 double delta2 = fabs(c.t2 - i2.fT[1][0]);
322 double calc1 = calcPrecision(cubic1);
323 double calc2 = calcPrecision(cubic2);
324 double factor1 = calc1 / delta1;
325 double factor2 = calc2 / delta2;
326 SkDebugf("%s %d calc1=%1.9g delta1=%1.9g factor1=%1.9g calc2=%1.9g delta2=%1.9g"
327 " factor2=%1.9g\n", __FUNCTION__, test,
328 calc1, delta1, factor1, calc2, delta2, factor2);
329 if (factor1 < largestFactor) {
330 SkDebugf("WE HAVE A WINNER! %1.9g\n", factor1);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000331 #if DEBUG_CRASH
caryclark@google.com73ca6242013-01-17 21:02:47 +0000332 SkDebugf("%s\n", str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000333 #endif
caryclark@google.com73ca6242013-01-17 21:02:47 +0000334 oneOff(cubic1, cubic2);
335 largestFactor = factor1;
336 }
337 if (factor2 < largestFactor) {
338 SkDebugf("WE HAVE A WINNER! %1.9g\n", factor2);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000339 #if DEBUG_CRASH
caryclark@google.com73ca6242013-01-17 21:02:47 +0000340 SkDebugf("%s\n", str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000341 #endif
caryclark@google.com73ca6242013-01-17 21:02:47 +0000342 oneOff(cubic1, cubic2);
343 largestFactor = factor2;
344 }
345 }
346}
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000347
348void CubicIntersection_RandTest() {
349 srand(0);
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000350 const int tests = 10000000;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000351 for (int test = 0; test < tests; ++test) {
352 Cubic cubic1, cubic2;
353 for (int i = 0; i < 4; ++i) {
354 cubic1[i].x = (double) rand() / RAND_MAX * 100;
355 cubic1[i].y = (double) rand() / RAND_MAX * 100;
356 cubic2[i].x = (double) rand() / RAND_MAX * 100;
357 cubic2[i].y = (double) rand() / RAND_MAX * 100;
358 }
359 #if DEBUG_CRASH
360 char str[1024];
361 sprintf(str, "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n"
362 "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n",
363 cubic1[0].x, cubic1[0].y, cubic1[1].x, cubic1[1].y, cubic1[2].x, cubic1[2].y,
364 cubic1[3].x, cubic1[3].y,
365 cubic2[0].x, cubic2[0].y, cubic2[1].x, cubic2[1].y, cubic2[2].x, cubic2[2].y,
366 cubic2[3].x, cubic2[3].y);
367 #endif
368 _Rect rect1, rect2;
369 rect1.setBounds(cubic1);
370 rect2.setBounds(cubic2);
371 bool boundsIntersect = rect1.left <= rect2.right && rect2.left <= rect2.right
372 && rect1.top <= rect2.bottom && rect2.top <= rect1.bottom;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000373 if (test == -1) {
374 SkDebugf("ready...\n");
375 }
376 Intersections intersections2;
377 bool newIntersects = intersect2(cubic1, cubic2, intersections2);
378 if (!boundsIntersect && newIntersects) {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000379 #if DEBUG_CRASH
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000380 SkDebugf("%s %d unexpected intersection boundsIntersect=%d "
381 " newIntersects=%d\n%s %s\n", __FUNCTION__, test, boundsIntersect,
382 newIntersects, __FUNCTION__, str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000383 #endif
caryclark@google.comaa358312013-01-29 20:28:49 +0000384 SkASSERT(0);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000385 }
386 for (int pt = 0; pt < intersections2.used(); ++pt) {
387 double tt1 = intersections2.fT[0][pt];
388 _Point xy1, xy2;
389 xy_at_t(cubic1, tt1, xy1.x, xy1.y);
390 int pt2 = intersections2.fFlip ? intersections2.used() - pt - 1 : pt;
391 double tt2 = intersections2.fT[1][pt2];
392 xy_at_t(cubic2, tt2, xy2.x, xy2.y);
caryclark@google.com9f602912013-01-24 21:47:16 +0000393 #if 0
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000394 SkDebugf("%s t1=%1.9g (%1.9g, %1.9g) (%1.9g, %1.9g) t2=%1.9g\n", __FUNCTION__,
395 tt1, xy1.x, xy1.y, xy2.x, xy2.y, tt2);
caryclark@google.com9f602912013-01-24 21:47:16 +0000396 #endif
caryclark@google.comaa358312013-01-29 20:28:49 +0000397 SkASSERT(xy1.approximatelyEqual(xy2));
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000398 }
399 }
400}
caryclark@google.com9f602912013-01-24 21:47:16 +0000401
caryclark@google.comf9502d72013-02-04 14:06:49 +0000402void CubicIntersection_IntersectionFinder() {
caryclark@google.combeda3892013-02-07 13:13:41 +0000403 Cubic cubic1 = {{0,1}, {3,4}, {1,0}, {3,0}};
404 Cubic cubic2 = {{0,1}, {0,3}, {1,0}, {4,3}};
405 double t1Seed = 0.496;
406 double t2Seed = 0.711;
407 double t1Step = 0.1;
408 double t2Step = 0.1;
caryclark@google.comf9502d72013-02-04 14:06:49 +0000409 _Point t1[3], t2[3];
410 bool toggle = true;
411 do {
412 xy_at_t(cubic1, t1Seed - t1Step, t1[0].x, t1[0].y);
413 xy_at_t(cubic1, t1Seed, t1[1].x, t1[1].y);
414 xy_at_t(cubic1, t1Seed + t1Step, t1[2].x, t1[2].y);
415 xy_at_t(cubic2, t2Seed - t2Step, t2[0].x, t2[0].y);
416 xy_at_t(cubic2, t2Seed, t2[1].x, t2[1].y);
417 xy_at_t(cubic2, t2Seed + t2Step, t2[2].x, t2[2].y);
418 double dist[3][3];
419 dist[1][1] = t1[1].distance(t2[1]);
420 int best_i = 1, best_j = 1;
421 for (int i = 0; i < 3; ++i) {
422 for (int j = 0; j < 3; ++j) {
423 if (i == 1 && j == 1) {
424 continue;
425 }
426 dist[i][j] = t1[i].distance(t2[j]);
427 if (dist[best_i][best_j] > dist[i][j]) {
428 best_i = i;
429 best_j = j;
430 }
431 }
432 }
433 if (best_i == 0) {
434 t1Seed -= t1Step;
435 } else if (best_i == 2) {
436 t1Seed += t1Step;
437 }
438 if (best_j == 0) {
439 t2Seed -= t2Step;
440 } else if (best_j == 2) {
441 t2Seed += t2Step;
442 }
443 if (best_i == 1 && best_j == 1) {
444 if ((toggle ^= true)) {
445 t1Step /= 2;
446 } else {
447 t2Step /= 2;
448 }
449 }
450 } while (!t1[1].approximatelyEqual(t2[1]));
451 t1Step = t2Step = 0.1;
452 double t10 = t1Seed - t1Step * 2;
453 double t12 = t1Seed + t1Step * 2;
454 double t20 = t2Seed - t2Step * 2;
455 double t22 = t2Seed + t2Step * 2;
456 _Point test;
457 while (!approximately_zero(t1Step)) {
458 xy_at_t(cubic1, t10, test.x, test.y);
459 t10 += t1[1].approximatelyEqual(test) ? -t1Step : t1Step;
460 t1Step /= 2;
caryclark@google.com9f602912013-01-24 21:47:16 +0000461 }
caryclark@google.comf9502d72013-02-04 14:06:49 +0000462 t1Step = 0.1;
463 while (!approximately_zero(t1Step)) {
464 xy_at_t(cubic1, t12, test.x, test.y);
465 t12 -= t1[1].approximatelyEqual(test) ? -t1Step : t1Step;
466 t1Step /= 2;
467 }
468 while (!approximately_zero(t2Step)) {
469 xy_at_t(cubic2, t20, test.x, test.y);
470 t20 += t2[1].approximatelyEqual(test) ? -t2Step : t2Step;
471 t2Step /= 2;
472 }
473 t2Step = 0.1;
474 while (!approximately_zero(t2Step)) {
475 xy_at_t(cubic2, t22, test.x, test.y);
476 t22 -= t2[1].approximatelyEqual(test) ? -t2Step : t2Step;
477 t2Step /= 2;
478 }
479 SkDebugf("%s t1=(%1.9g<%1.9g<%1.9g) t2=(%1.9g<%1.9g<%1.9g)\n", __FUNCTION__,
480 t10, t1Seed, t12, t20, t2Seed, t22);
caryclark@google.com9f602912013-01-24 21:47:16 +0000481}
caryclark@google.comf9502d72013-02-04 14:06:49 +0000482
483#if 0
484void CubicIntersection_CoincidentTest() {
485 Cubic cubic1 = {{0, 1}, {0, 2}, {1, 0}, {1, 0}};
486 Cubic cubic2 = {{0, 1}, {0, 2}, {1, 0}, {6, 1}};
skia.committer@gmail.com0c38ed32013-02-05 07:02:01 +0000487
caryclark@google.comf9502d72013-02-04 14:06:49 +0000488}
489#endif