blob: 66c4d17dd7bd14b85f23eee21a0771f97c08462f [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 }
98}
99
100static const Cubic testSet[] = {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000101{{0, 0}, {0, 1}, {1, 1}, {1, 0}},
102{{1, 0}, {0, 0}, {0, 1}, {1, 1}},
103
caryclark@google.comaa358312013-01-29 20:28:49 +0000104{{0, 1}, {0, 2}, {1, 0}, {1, 0}},
105{{0, 1}, {0, 1}, {1, 0}, {2, 0}},
106
caryclark@google.comf9502d72013-02-04 14:06:49 +0000107{{0, 1}, {1, 6}, {1, 0}, {2, 0}},
108{{0, 1}, {0, 2}, {1, 0}, {6, 1}},
109
110{{0, 1}, {5, 6}, {1, 0}, {1, 0}},
111{{0, 1}, {0, 1}, {1, 0}, {6, 5}},
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000112
113{{95.837747722788592, 45.025976907939643}, {16.564570095652982, 0.72959763963222402}, {63.209855865319199, 68.047528419665767}, {57.640240647662544, 59.524565264361243}},
114{{51.593891741518817, 38.53849970667553}, {62.34752929878772, 74.924924725166022}, {74.810149322641152, 34.17966562983564}, {29.368398119401373, 94.66719277886078}},
115
116{{39.765160968417838, 33.060396198677083}, {5.1922921581157908, 66.854301452103215}, {31.619281802149157, 25.269248720849514}, {81.541621071073038, 70.025341524754353}},
117{{46.078911165743556, 48.259962651999651}, {20.24450549867214, 49.403916182650214}, {0.26325131778756683, 24.46489805563581}, {15.915006546264051, 83.515023059917155}},
118
caryclark@google.com9f602912013-01-24 21:47:16 +0000119{{65.454505973241524, 93.881892270353575}, {45.867360264932437, 92.723972719499827}, {2.1464054482739447, 74.636369140183717}, {33.774068594804994, 40.770872887582925}},
120{{72.963387832494163, 95.659300729473728}, {11.809496633619768, 82.209921247423594}, {13.456139067865974, 57.329313623406605}, {36.060621606214262, 70.867335643091849}},
121
122{{32.484981432782945, 75.082940782924624}, {42.467313093350882, 48.131159948246157}, {3.5963115764764657, 43.208665839959245}, {79.442476890721579, 89.709102357602262}},
123{{18.98573861410177, 93.308887208490106}, {40.405250173250792, 91.039661826118675}, {8.0467721950480584, 42.100282172719147}, {40.883324221187891, 26.030185504830527}},
124
125{{7.5374809128872498, 82.441702896003477}, {22.444346930107265, 22.138854312775123}, {66.76091829629658, 50.753805856571446}, {78.193478508942519, 97.7932997968948}},
126{{97.700573130371311, 53.53260215070685}, {87.72443481149358, 84.575876772671876}, {19.215031396232092, 47.032676472809484}, {11.989686410869325, 10.659507480757082}},
127
128{{26.192053931854691, 9.8504326817814416}, {10.174241480498686, 98.476562741434464}, {21.177712558385782, 33.814968789841501}, {75.329030899018534, 55.02231980442177}},
129{{56.222082700683771, 24.54395039218662}, {95.589995289030483, 81.050822735322086}, {28.180450866082897, 28.837706255185282}, {60.128952916771617, 87.311672180570511}},
130
131{{42.449716172390481, 52.379709366885805}, {27.896043159019225, 48.797373636065686}, {92.770268299044233, 89.899302036454571}, {12.102066544863426, 99.43241951960718}},
132{{45.77532924980639, 45.958701495993274}, {37.458701356062065, 68.393691335056758}, {37.569326692060258, 27.673713456687381}, {60.674866037757539, 62.47349659096146}},
133
caryclark@google.com73ca6242013-01-17 21:02:47 +0000134{{67.426548091427676, 37.993772624988935}, {23.483695892376684, 90.476863174921306}, {35.597065061143162, 79.872482633158796}, {75.38634169631932, 18.244890038969412}},
135{{61.336508189019057, 82.693132843213675}, {44.639380902349664, 54.074825790745592}, {16.815615499771951, 20.049704667203923}, {41.866884958868326, 56.735503699973002}},
136
137{{67.4265481, 37.9937726}, {23.4836959, 90.4768632}, {35.5970651, 79.8724826}, {75.3863417, 18.24489}},
138{{61.3365082, 82.6931328}, {44.6393809, 54.0748258}, {16.8156155, 20.0497047}, {41.866885, 56.7355037}},
139
140{{18.1312339, 31.6473732}, {95.5711034, 63.5350219}, {92.3283165, 62.0158945}, {18.5656052, 32.1268808}},
141{{97.402018, 35.7169972}, {33.1127443, 25.8935163}, {1.13970027, 54.9424981}, {56.4860195, 60.529264}},
142};
143
144const size_t testSetCount = sizeof(testSet) / sizeof(testSet[0]);
145
146void CubicIntersection_OneOffTest() {
147 for (size_t outer = 0; outer < testSetCount - 1; ++outer) {
caryclark@google.com9f602912013-01-24 21:47:16 +0000148#if ONE_OFF_DEBUG
caryclark@google.com73ca6242013-01-17 21:02:47 +0000149 SkDebugf("%s quads1[%d]\n", __FUNCTION__, outer);
caryclark@google.com9f602912013-01-24 21:47:16 +0000150#endif
caryclark@google.com73ca6242013-01-17 21:02:47 +0000151 const Cubic& cubic1 = testSet[outer];
152 for (size_t inner = outer + 1; inner < testSetCount; ++inner) {
caryclark@google.com9f602912013-01-24 21:47:16 +0000153#if ONE_OFF_DEBUG
caryclark@google.com73ca6242013-01-17 21:02:47 +0000154 SkDebugf("%s quads2[%d]\n", __FUNCTION__, inner);
caryclark@google.com9f602912013-01-24 21:47:16 +0000155#endif
caryclark@google.com73ca6242013-01-17 21:02:47 +0000156 const Cubic& cubic2 = testSet[inner];
157 oneOff(cubic1, cubic2);
158 }
159 }
160}
161
caryclark@google.comf9502d72013-02-04 14:06:49 +0000162#define DEBUG_CRASH 0
caryclark@google.com73ca6242013-01-17 21:02:47 +0000163
164class CubicChopper {
165public:
166
167// only finds one intersection
168CubicChopper(const Cubic& c1, const Cubic& c2)
169 : cubic1(c1)
170 , cubic2(c2)
171 , depth(0) {
172}
173
174bool intersect(double minT1, double maxT1, double minT2, double maxT2) {
175 Cubic sub1, sub2;
176 // FIXME: carry last subdivide and reduceOrder result with cubic
177 sub_divide(cubic1, minT1, maxT1, sub1);
178 sub_divide(cubic2, minT2, maxT2, sub2);
179 Intersections i;
180 intersect2(sub1, sub2, i);
181 if (i.used() == 0) {
182 return false;
183 }
184 double x1, y1, x2, y2;
185 t1 = minT1 + i.fT[0][0] * (maxT1 - minT1);
186 t2 = minT2 + i.fT[1][0] * (maxT2 - minT2);
187 xy_at_t(cubic1, t1, x1, y1);
188 xy_at_t(cubic2, t2, x2, y2);
189 if (AlmostEqualUlps(x1, x2) && AlmostEqualUlps(y1, y2)) {
190 return true;
191 }
192 double half1 = (minT1 + maxT1) / 2;
193 double half2 = (minT2 + maxT2) / 2;
194 ++depth;
195 bool result;
196 if (depth & 1) {
197 result = intersect(minT1, half1, minT2, maxT2) || intersect(half1, maxT1, minT2, maxT2)
198 || intersect(minT1, maxT1, minT2, half2) || intersect(minT1, maxT1, half2, maxT2);
199 } else {
200 result = intersect(minT1, maxT1, minT2, half2) || intersect(minT1, maxT1, half2, maxT2)
201 || intersect(minT1, half1, minT2, maxT2) || intersect(half1, maxT1, minT2, maxT2);
202 }
203 --depth;
204 return result;
205}
206
207const Cubic& cubic1;
208const Cubic& cubic2;
209double t1;
210double t2;
211int depth;
212};
213
214#define TRY_OLD 0 // old way fails on test == 1
215
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000216void CubicIntersection_RandTestOld() {
caryclark@google.com73ca6242013-01-17 21:02:47 +0000217 srand(0);
218 const int tests = 1000000; // 10000000;
219 double largestFactor = DBL_MAX;
220 for (int test = 0; test < tests; ++test) {
221 Cubic cubic1, cubic2;
222 for (int i = 0; i < 4; ++i) {
223 cubic1[i].x = (double) rand() / RAND_MAX * 100;
224 cubic1[i].y = (double) rand() / RAND_MAX * 100;
225 cubic2[i].x = (double) rand() / RAND_MAX * 100;
226 cubic2[i].y = (double) rand() / RAND_MAX * 100;
227 }
228 if (test == 2513) { // the pair crosses three times, but the quadratic approximation
229 continue; // only sees one -- should be OK to ignore the other two?
230 }
231 if (test == 12932) { // this exposes a weakness when one cubic touches the other but
232 continue; // does not touch the quad approximation. Captured in qc.htm as cubic15
233 }
234 #if DEBUG_CRASH
235 char str[1024];
236 sprintf(str, "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n"
237 "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n",
238 cubic1[0].x, cubic1[0].y, cubic1[1].x, cubic1[1].y, cubic1[2].x, cubic1[2].y,
239 cubic1[3].x, cubic1[3].y,
240 cubic2[0].x, cubic2[0].y, cubic2[1].x, cubic2[1].y, cubic2[2].x, cubic2[2].y,
241 cubic2[3].x, cubic2[3].y);
242 #endif
243 _Rect rect1, rect2;
244 rect1.setBounds(cubic1);
245 rect2.setBounds(cubic2);
246 bool boundsIntersect = rect1.left <= rect2.right && rect2.left <= rect2.right
247 && rect1.top <= rect2.bottom && rect2.top <= rect1.bottom;
248 Intersections i1, i2;
249 #if TRY_OLD
250 bool oldIntersects = intersect(cubic1, cubic2, i1);
251 #else
252 bool oldIntersects = false;
253 #endif
254 if (test == -1) {
255 SkDebugf("ready...\n");
256 }
257 bool newIntersects = intersect2(cubic1, cubic2, i2);
258 if (!boundsIntersect && (oldIntersects || newIntersects)) {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000259 #if DEBUG_CRASH
caryclark@google.com73ca6242013-01-17 21:02:47 +0000260 SkDebugf("%s %d unexpected intersection boundsIntersect=%d oldIntersects=%d"
261 " newIntersects=%d\n%s %s\n", __FUNCTION__, test, boundsIntersect,
262 oldIntersects, newIntersects, __FUNCTION__, str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000263 #endif
caryclark@google.comaa358312013-01-29 20:28:49 +0000264 SkASSERT(0);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000265 }
266 if (oldIntersects && !newIntersects) {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000267 #if DEBUG_CRASH
caryclark@google.com73ca6242013-01-17 21:02:47 +0000268 SkDebugf("%s %d missing intersection oldIntersects=%d newIntersects=%d\n%s %s\n",
269 __FUNCTION__, test, oldIntersects, newIntersects, __FUNCTION__, str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000270 #endif
caryclark@google.comaa358312013-01-29 20:28:49 +0000271 SkASSERT(0);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000272 }
273 if (!oldIntersects && !newIntersects) {
274 continue;
275 }
276 if (i2.used() > 1) {
277 continue;
278 // just look at single intercepts for simplicity
279 }
280 Intersections self1, self2; // self-intersect checks
281 if (intersect(cubic1, self1)) {
282 continue;
283 }
284 if (intersect(cubic2, self2)) {
285 continue;
286 }
287 // binary search for range necessary to enclose real intersection
288 CubicChopper c(cubic1, cubic2);
289 bool result = c.intersect(0, 1, 0, 1);
290 if (!result) {
291 // FIXME: a failure here probably means that a core routine used by CubicChopper is failing
292 continue;
293 }
294 double delta1 = fabs(c.t1 - i2.fT[0][0]);
295 double delta2 = fabs(c.t2 - i2.fT[1][0]);
296 double calc1 = calcPrecision(cubic1);
297 double calc2 = calcPrecision(cubic2);
298 double factor1 = calc1 / delta1;
299 double factor2 = calc2 / delta2;
300 SkDebugf("%s %d calc1=%1.9g delta1=%1.9g factor1=%1.9g calc2=%1.9g delta2=%1.9g"
301 " factor2=%1.9g\n", __FUNCTION__, test,
302 calc1, delta1, factor1, calc2, delta2, factor2);
303 if (factor1 < largestFactor) {
304 SkDebugf("WE HAVE A WINNER! %1.9g\n", factor1);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000305 #if DEBUG_CRASH
caryclark@google.com73ca6242013-01-17 21:02:47 +0000306 SkDebugf("%s\n", str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000307 #endif
caryclark@google.com73ca6242013-01-17 21:02:47 +0000308 oneOff(cubic1, cubic2);
309 largestFactor = factor1;
310 }
311 if (factor2 < largestFactor) {
312 SkDebugf("WE HAVE A WINNER! %1.9g\n", factor2);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000313 #if DEBUG_CRASH
caryclark@google.com73ca6242013-01-17 21:02:47 +0000314 SkDebugf("%s\n", str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000315 #endif
caryclark@google.com73ca6242013-01-17 21:02:47 +0000316 oneOff(cubic1, cubic2);
317 largestFactor = factor2;
318 }
319 }
320}
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000321
322void CubicIntersection_RandTest() {
323 srand(0);
caryclark@google.com85ec74c2013-01-28 19:25:51 +0000324 const int tests = 10000000;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000325 for (int test = 0; test < tests; ++test) {
326 Cubic cubic1, cubic2;
327 for (int i = 0; i < 4; ++i) {
328 cubic1[i].x = (double) rand() / RAND_MAX * 100;
329 cubic1[i].y = (double) rand() / RAND_MAX * 100;
330 cubic2[i].x = (double) rand() / RAND_MAX * 100;
331 cubic2[i].y = (double) rand() / RAND_MAX * 100;
332 }
333 #if DEBUG_CRASH
334 char str[1024];
335 sprintf(str, "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n"
336 "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n",
337 cubic1[0].x, cubic1[0].y, cubic1[1].x, cubic1[1].y, cubic1[2].x, cubic1[2].y,
338 cubic1[3].x, cubic1[3].y,
339 cubic2[0].x, cubic2[0].y, cubic2[1].x, cubic2[1].y, cubic2[2].x, cubic2[2].y,
340 cubic2[3].x, cubic2[3].y);
341 #endif
342 _Rect rect1, rect2;
343 rect1.setBounds(cubic1);
344 rect2.setBounds(cubic2);
345 bool boundsIntersect = rect1.left <= rect2.right && rect2.left <= rect2.right
346 && rect1.top <= rect2.bottom && rect2.top <= rect1.bottom;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000347 if (test == -1) {
348 SkDebugf("ready...\n");
349 }
350 Intersections intersections2;
351 bool newIntersects = intersect2(cubic1, cubic2, intersections2);
352 if (!boundsIntersect && newIntersects) {
caryclark@google.comf9502d72013-02-04 14:06:49 +0000353 #if DEBUG_CRASH
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000354 SkDebugf("%s %d unexpected intersection boundsIntersect=%d "
355 " newIntersects=%d\n%s %s\n", __FUNCTION__, test, boundsIntersect,
356 newIntersects, __FUNCTION__, str);
caryclark@google.comf9502d72013-02-04 14:06:49 +0000357 #endif
caryclark@google.comaa358312013-01-29 20:28:49 +0000358 SkASSERT(0);
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000359 }
360 for (int pt = 0; pt < intersections2.used(); ++pt) {
361 double tt1 = intersections2.fT[0][pt];
362 _Point xy1, xy2;
363 xy_at_t(cubic1, tt1, xy1.x, xy1.y);
364 int pt2 = intersections2.fFlip ? intersections2.used() - pt - 1 : pt;
365 double tt2 = intersections2.fT[1][pt2];
366 xy_at_t(cubic2, tt2, xy2.x, xy2.y);
caryclark@google.com9f602912013-01-24 21:47:16 +0000367 #if 0
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000368 SkDebugf("%s t1=%1.9g (%1.9g, %1.9g) (%1.9g, %1.9g) t2=%1.9g\n", __FUNCTION__,
369 tt1, xy1.x, xy1.y, xy2.x, xy2.y, tt2);
caryclark@google.com9f602912013-01-24 21:47:16 +0000370 #endif
caryclark@google.comaa358312013-01-29 20:28:49 +0000371 SkASSERT(xy1.approximatelyEqual(xy2));
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000372 }
373 }
374}
caryclark@google.com9f602912013-01-24 21:47:16 +0000375
caryclark@google.comf9502d72013-02-04 14:06:49 +0000376void CubicIntersection_IntersectionFinder() {
377 Cubic cubic1 = {{0, 1}, {0, 2}, {1, 0}, {1, 0}};
378 Cubic cubic2 = {{0, 1}, {0, 2}, {1, 0}, {6, 1}};
379 double t1Seed = 0.19923954998177532;
380 double t2Seed = 0.17140596934291233;
381 double t1Step = 0.00035501449125494022;
382 double t2Step = 0.00020896171344569892;
383 _Point t1[3], t2[3];
384 bool toggle = true;
385 do {
386 xy_at_t(cubic1, t1Seed - t1Step, t1[0].x, t1[0].y);
387 xy_at_t(cubic1, t1Seed, t1[1].x, t1[1].y);
388 xy_at_t(cubic1, t1Seed + t1Step, t1[2].x, t1[2].y);
389 xy_at_t(cubic2, t2Seed - t2Step, t2[0].x, t2[0].y);
390 xy_at_t(cubic2, t2Seed, t2[1].x, t2[1].y);
391 xy_at_t(cubic2, t2Seed + t2Step, t2[2].x, t2[2].y);
392 double dist[3][3];
393 dist[1][1] = t1[1].distance(t2[1]);
394 int best_i = 1, best_j = 1;
395 for (int i = 0; i < 3; ++i) {
396 for (int j = 0; j < 3; ++j) {
397 if (i == 1 && j == 1) {
398 continue;
399 }
400 dist[i][j] = t1[i].distance(t2[j]);
401 if (dist[best_i][best_j] > dist[i][j]) {
402 best_i = i;
403 best_j = j;
404 }
405 }
406 }
407 if (best_i == 0) {
408 t1Seed -= t1Step;
409 } else if (best_i == 2) {
410 t1Seed += t1Step;
411 }
412 if (best_j == 0) {
413 t2Seed -= t2Step;
414 } else if (best_j == 2) {
415 t2Seed += t2Step;
416 }
417 if (best_i == 1 && best_j == 1) {
418 if ((toggle ^= true)) {
419 t1Step /= 2;
420 } else {
421 t2Step /= 2;
422 }
423 }
424 } while (!t1[1].approximatelyEqual(t2[1]));
425 t1Step = t2Step = 0.1;
426 double t10 = t1Seed - t1Step * 2;
427 double t12 = t1Seed + t1Step * 2;
428 double t20 = t2Seed - t2Step * 2;
429 double t22 = t2Seed + t2Step * 2;
430 _Point test;
431 while (!approximately_zero(t1Step)) {
432 xy_at_t(cubic1, t10, test.x, test.y);
433 t10 += t1[1].approximatelyEqual(test) ? -t1Step : t1Step;
434 t1Step /= 2;
caryclark@google.com9f602912013-01-24 21:47:16 +0000435 }
caryclark@google.comf9502d72013-02-04 14:06:49 +0000436 t1Step = 0.1;
437 while (!approximately_zero(t1Step)) {
438 xy_at_t(cubic1, t12, test.x, test.y);
439 t12 -= t1[1].approximatelyEqual(test) ? -t1Step : t1Step;
440 t1Step /= 2;
441 }
442 while (!approximately_zero(t2Step)) {
443 xy_at_t(cubic2, t20, test.x, test.y);
444 t20 += t2[1].approximatelyEqual(test) ? -t2Step : t2Step;
445 t2Step /= 2;
446 }
447 t2Step = 0.1;
448 while (!approximately_zero(t2Step)) {
449 xy_at_t(cubic2, t22, test.x, test.y);
450 t22 -= t2[1].approximatelyEqual(test) ? -t2Step : t2Step;
451 t2Step /= 2;
452 }
453 SkDebugf("%s t1=(%1.9g<%1.9g<%1.9g) t2=(%1.9g<%1.9g<%1.9g)\n", __FUNCTION__,
454 t10, t1Seed, t12, t20, t2Seed, t22);
caryclark@google.com9f602912013-01-24 21:47:16 +0000455}
caryclark@google.comf9502d72013-02-04 14:06:49 +0000456
457#if 0
458void CubicIntersection_CoincidentTest() {
459 Cubic cubic1 = {{0, 1}, {0, 2}, {1, 0}, {1, 0}};
460 Cubic cubic2 = {{0, 1}, {0, 2}, {1, 0}, {6, 1}};
461
462}
463#endif