blob: 2e738fc0e4b9a252cea58753fd57390f0a82f93e [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.com9f602912013-01-24 21:47:16 +000060#define ONE_OFF_DEBUG 1
61
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.com05c4bad2013-01-19 13:22:39 +000096 assert(xy1.approximatelyEqual(xy2));
caryclark@google.com73ca6242013-01-17 21:02:47 +000097 }
98}
99
100static const Cubic testSet[] = {
caryclark@google.com9f602912013-01-24 21:47:16 +0000101{{65.454505973241524, 93.881892270353575}, {45.867360264932437, 92.723972719499827}, {2.1464054482739447, 74.636369140183717}, {33.774068594804994, 40.770872887582925}},
102{{72.963387832494163, 95.659300729473728}, {11.809496633619768, 82.209921247423594}, {13.456139067865974, 57.329313623406605}, {36.060621606214262, 70.867335643091849}},
103
104{{32.484981432782945, 75.082940782924624}, {42.467313093350882, 48.131159948246157}, {3.5963115764764657, 43.208665839959245}, {79.442476890721579, 89.709102357602262}},
105{{18.98573861410177, 93.308887208490106}, {40.405250173250792, 91.039661826118675}, {8.0467721950480584, 42.100282172719147}, {40.883324221187891, 26.030185504830527}},
106
107{{7.5374809128872498, 82.441702896003477}, {22.444346930107265, 22.138854312775123}, {66.76091829629658, 50.753805856571446}, {78.193478508942519, 97.7932997968948}},
108{{97.700573130371311, 53.53260215070685}, {87.72443481149358, 84.575876772671876}, {19.215031396232092, 47.032676472809484}, {11.989686410869325, 10.659507480757082}},
109
110{{26.192053931854691, 9.8504326817814416}, {10.174241480498686, 98.476562741434464}, {21.177712558385782, 33.814968789841501}, {75.329030899018534, 55.02231980442177}},
111{{56.222082700683771, 24.54395039218662}, {95.589995289030483, 81.050822735322086}, {28.180450866082897, 28.837706255185282}, {60.128952916771617, 87.311672180570511}},
112
113{{42.449716172390481, 52.379709366885805}, {27.896043159019225, 48.797373636065686}, {92.770268299044233, 89.899302036454571}, {12.102066544863426, 99.43241951960718}},
114{{45.77532924980639, 45.958701495993274}, {37.458701356062065, 68.393691335056758}, {37.569326692060258, 27.673713456687381}, {60.674866037757539, 62.47349659096146}},
115
caryclark@google.com73ca6242013-01-17 21:02:47 +0000116{{67.426548091427676, 37.993772624988935}, {23.483695892376684, 90.476863174921306}, {35.597065061143162, 79.872482633158796}, {75.38634169631932, 18.244890038969412}},
117{{61.336508189019057, 82.693132843213675}, {44.639380902349664, 54.074825790745592}, {16.815615499771951, 20.049704667203923}, {41.866884958868326, 56.735503699973002}},
118
119{{67.4265481, 37.9937726}, {23.4836959, 90.4768632}, {35.5970651, 79.8724826}, {75.3863417, 18.24489}},
120{{61.3365082, 82.6931328}, {44.6393809, 54.0748258}, {16.8156155, 20.0497047}, {41.866885, 56.7355037}},
121
122{{18.1312339, 31.6473732}, {95.5711034, 63.5350219}, {92.3283165, 62.0158945}, {18.5656052, 32.1268808}},
123{{97.402018, 35.7169972}, {33.1127443, 25.8935163}, {1.13970027, 54.9424981}, {56.4860195, 60.529264}},
124};
125
126const size_t testSetCount = sizeof(testSet) / sizeof(testSet[0]);
127
128void CubicIntersection_OneOffTest() {
129 for (size_t outer = 0; outer < testSetCount - 1; ++outer) {
caryclark@google.com9f602912013-01-24 21:47:16 +0000130#if ONE_OFF_DEBUG
caryclark@google.com73ca6242013-01-17 21:02:47 +0000131 SkDebugf("%s quads1[%d]\n", __FUNCTION__, outer);
caryclark@google.com9f602912013-01-24 21:47:16 +0000132#endif
caryclark@google.com73ca6242013-01-17 21:02:47 +0000133 const Cubic& cubic1 = testSet[outer];
134 for (size_t inner = outer + 1; inner < testSetCount; ++inner) {
caryclark@google.com9f602912013-01-24 21:47:16 +0000135#if ONE_OFF_DEBUG
caryclark@google.com73ca6242013-01-17 21:02:47 +0000136 SkDebugf("%s quads2[%d]\n", __FUNCTION__, inner);
caryclark@google.com9f602912013-01-24 21:47:16 +0000137#endif
caryclark@google.com73ca6242013-01-17 21:02:47 +0000138 const Cubic& cubic2 = testSet[inner];
139 oneOff(cubic1, cubic2);
140 }
141 }
142}
143
144#define DEBUG_CRASH 1
145
146class CubicChopper {
147public:
148
149// only finds one intersection
150CubicChopper(const Cubic& c1, const Cubic& c2)
151 : cubic1(c1)
152 , cubic2(c2)
153 , depth(0) {
154}
155
156bool intersect(double minT1, double maxT1, double minT2, double maxT2) {
157 Cubic sub1, sub2;
158 // FIXME: carry last subdivide and reduceOrder result with cubic
159 sub_divide(cubic1, minT1, maxT1, sub1);
160 sub_divide(cubic2, minT2, maxT2, sub2);
161 Intersections i;
162 intersect2(sub1, sub2, i);
163 if (i.used() == 0) {
164 return false;
165 }
166 double x1, y1, x2, y2;
167 t1 = minT1 + i.fT[0][0] * (maxT1 - minT1);
168 t2 = minT2 + i.fT[1][0] * (maxT2 - minT2);
169 xy_at_t(cubic1, t1, x1, y1);
170 xy_at_t(cubic2, t2, x2, y2);
171 if (AlmostEqualUlps(x1, x2) && AlmostEqualUlps(y1, y2)) {
172 return true;
173 }
174 double half1 = (minT1 + maxT1) / 2;
175 double half2 = (minT2 + maxT2) / 2;
176 ++depth;
177 bool result;
178 if (depth & 1) {
179 result = intersect(minT1, half1, minT2, maxT2) || intersect(half1, maxT1, minT2, maxT2)
180 || intersect(minT1, maxT1, minT2, half2) || intersect(minT1, maxT1, half2, maxT2);
181 } else {
182 result = intersect(minT1, maxT1, minT2, half2) || intersect(minT1, maxT1, half2, maxT2)
183 || intersect(minT1, half1, minT2, maxT2) || intersect(half1, maxT1, minT2, maxT2);
184 }
185 --depth;
186 return result;
187}
188
189const Cubic& cubic1;
190const Cubic& cubic2;
191double t1;
192double t2;
193int depth;
194};
195
196#define TRY_OLD 0 // old way fails on test == 1
197
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000198void CubicIntersection_RandTestOld() {
caryclark@google.com73ca6242013-01-17 21:02:47 +0000199 srand(0);
200 const int tests = 1000000; // 10000000;
201 double largestFactor = DBL_MAX;
202 for (int test = 0; test < tests; ++test) {
203 Cubic cubic1, cubic2;
204 for (int i = 0; i < 4; ++i) {
205 cubic1[i].x = (double) rand() / RAND_MAX * 100;
206 cubic1[i].y = (double) rand() / RAND_MAX * 100;
207 cubic2[i].x = (double) rand() / RAND_MAX * 100;
208 cubic2[i].y = (double) rand() / RAND_MAX * 100;
209 }
210 if (test == 2513) { // the pair crosses three times, but the quadratic approximation
211 continue; // only sees one -- should be OK to ignore the other two?
212 }
213 if (test == 12932) { // this exposes a weakness when one cubic touches the other but
214 continue; // does not touch the quad approximation. Captured in qc.htm as cubic15
215 }
216 #if DEBUG_CRASH
217 char str[1024];
218 sprintf(str, "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n"
219 "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n",
220 cubic1[0].x, cubic1[0].y, cubic1[1].x, cubic1[1].y, cubic1[2].x, cubic1[2].y,
221 cubic1[3].x, cubic1[3].y,
222 cubic2[0].x, cubic2[0].y, cubic2[1].x, cubic2[1].y, cubic2[2].x, cubic2[2].y,
223 cubic2[3].x, cubic2[3].y);
224 #endif
225 _Rect rect1, rect2;
226 rect1.setBounds(cubic1);
227 rect2.setBounds(cubic2);
228 bool boundsIntersect = rect1.left <= rect2.right && rect2.left <= rect2.right
229 && rect1.top <= rect2.bottom && rect2.top <= rect1.bottom;
230 Intersections i1, i2;
231 #if TRY_OLD
232 bool oldIntersects = intersect(cubic1, cubic2, i1);
233 #else
234 bool oldIntersects = false;
235 #endif
236 if (test == -1) {
237 SkDebugf("ready...\n");
238 }
239 bool newIntersects = intersect2(cubic1, cubic2, i2);
240 if (!boundsIntersect && (oldIntersects || newIntersects)) {
241 SkDebugf("%s %d unexpected intersection boundsIntersect=%d oldIntersects=%d"
242 " newIntersects=%d\n%s %s\n", __FUNCTION__, test, boundsIntersect,
243 oldIntersects, newIntersects, __FUNCTION__, str);
244 assert(0);
245 }
246 if (oldIntersects && !newIntersects) {
247 SkDebugf("%s %d missing intersection oldIntersects=%d newIntersects=%d\n%s %s\n",
248 __FUNCTION__, test, oldIntersects, newIntersects, __FUNCTION__, str);
249 assert(0);
250 }
251 if (!oldIntersects && !newIntersects) {
252 continue;
253 }
254 if (i2.used() > 1) {
255 continue;
256 // just look at single intercepts for simplicity
257 }
258 Intersections self1, self2; // self-intersect checks
259 if (intersect(cubic1, self1)) {
260 continue;
261 }
262 if (intersect(cubic2, self2)) {
263 continue;
264 }
265 // binary search for range necessary to enclose real intersection
266 CubicChopper c(cubic1, cubic2);
267 bool result = c.intersect(0, 1, 0, 1);
268 if (!result) {
269 // FIXME: a failure here probably means that a core routine used by CubicChopper is failing
270 continue;
271 }
272 double delta1 = fabs(c.t1 - i2.fT[0][0]);
273 double delta2 = fabs(c.t2 - i2.fT[1][0]);
274 double calc1 = calcPrecision(cubic1);
275 double calc2 = calcPrecision(cubic2);
276 double factor1 = calc1 / delta1;
277 double factor2 = calc2 / delta2;
278 SkDebugf("%s %d calc1=%1.9g delta1=%1.9g factor1=%1.9g calc2=%1.9g delta2=%1.9g"
279 " factor2=%1.9g\n", __FUNCTION__, test,
280 calc1, delta1, factor1, calc2, delta2, factor2);
281 if (factor1 < largestFactor) {
282 SkDebugf("WE HAVE A WINNER! %1.9g\n", factor1);
283 SkDebugf("%s\n", str);
284 oneOff(cubic1, cubic2);
285 largestFactor = factor1;
286 }
287 if (factor2 < largestFactor) {
288 SkDebugf("WE HAVE A WINNER! %1.9g\n", factor2);
289 SkDebugf("%s\n", str);
290 oneOff(cubic1, cubic2);
291 largestFactor = factor2;
292 }
293 }
294}
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000295
296void CubicIntersection_RandTest() {
297 srand(0);
298 const int tests = 1000000; // 10000000;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000299 for (int test = 0; test < tests; ++test) {
300 Cubic cubic1, cubic2;
301 for (int i = 0; i < 4; ++i) {
302 cubic1[i].x = (double) rand() / RAND_MAX * 100;
303 cubic1[i].y = (double) rand() / RAND_MAX * 100;
304 cubic2[i].x = (double) rand() / RAND_MAX * 100;
305 cubic2[i].y = (double) rand() / RAND_MAX * 100;
306 }
307 #if DEBUG_CRASH
308 char str[1024];
309 sprintf(str, "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n"
310 "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n",
311 cubic1[0].x, cubic1[0].y, cubic1[1].x, cubic1[1].y, cubic1[2].x, cubic1[2].y,
312 cubic1[3].x, cubic1[3].y,
313 cubic2[0].x, cubic2[0].y, cubic2[1].x, cubic2[1].y, cubic2[2].x, cubic2[2].y,
314 cubic2[3].x, cubic2[3].y);
315 #endif
316 _Rect rect1, rect2;
317 rect1.setBounds(cubic1);
318 rect2.setBounds(cubic2);
319 bool boundsIntersect = rect1.left <= rect2.right && rect2.left <= rect2.right
320 && rect1.top <= rect2.bottom && rect2.top <= rect1.bottom;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000321 if (test == -1) {
322 SkDebugf("ready...\n");
323 }
324 Intersections intersections2;
325 bool newIntersects = intersect2(cubic1, cubic2, intersections2);
326 if (!boundsIntersect && newIntersects) {
327 SkDebugf("%s %d unexpected intersection boundsIntersect=%d "
328 " newIntersects=%d\n%s %s\n", __FUNCTION__, test, boundsIntersect,
329 newIntersects, __FUNCTION__, str);
330 assert(0);
331 }
332 for (int pt = 0; pt < intersections2.used(); ++pt) {
333 double tt1 = intersections2.fT[0][pt];
334 _Point xy1, xy2;
335 xy_at_t(cubic1, tt1, xy1.x, xy1.y);
336 int pt2 = intersections2.fFlip ? intersections2.used() - pt - 1 : pt;
337 double tt2 = intersections2.fT[1][pt2];
338 xy_at_t(cubic2, tt2, xy2.x, xy2.y);
caryclark@google.com9f602912013-01-24 21:47:16 +0000339 #if 0
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000340 SkDebugf("%s t1=%1.9g (%1.9g, %1.9g) (%1.9g, %1.9g) t2=%1.9g\n", __FUNCTION__,
341 tt1, xy1.x, xy1.y, xy2.x, xy2.y, tt2);
caryclark@google.com9f602912013-01-24 21:47:16 +0000342 #endif
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000343 assert(xy1.approximatelyEqual(xy2));
344 }
345 }
346}
caryclark@google.com9f602912013-01-24 21:47:16 +0000347
348static Cubic deltaTestSet[] = {
349 {{1, 4}, {1, 4.*2/3}, {1, 4.*1/3}, {1, 0}},
350 {{0, 3}, {1, 2}, {2, 1}, {3, 0}},
351 {{1, 4}, {1, 4.*2/3}, {1, 4.*1/3}, {1, 0}},
352 {{3.5, 1}, {2.5, 2}, {1.5, 3}, {0.5, 4}}
353};
354
355size_t deltaTestSetLen = sizeof(deltaTestSet) / sizeof(deltaTestSet[0]);
356
357static double deltaTestSetT[] = {
358 3./8,
359 5./12,
360 6./8,
361 9./12
362};
363
364size_t deltaTestSetTLen = sizeof(deltaTestSetT) / sizeof(deltaTestSetT[0]);
365
366static double expectedT[] = {
367 0.5,
368 1./3,
369 1./8,
370 5./6
371};
372
373size_t expectedTLen = sizeof(expectedT) / sizeof(expectedT[0]);
374
375// FIXME: this test no longer valid -- does not take minimum scale contribution into account
376void CubicIntersection_ComputeDeltaTest() {
377 SkASSERT(deltaTestSetLen == deltaTestSetTLen);
378 SkASSERT(expectedTLen == deltaTestSetTLen);
379 for (size_t index = 0; index < deltaTestSetLen; index += 2) {
380 const Cubic& c1 = deltaTestSet[index];
381 const Cubic& c2 = deltaTestSet[index + 1];
382 double t1 = deltaTestSetT[index];
383 double t2 = deltaTestSetT[index + 1];
384 double d1, d2;
385 computeDelta(c1, t1, 1, c2, t2, 1, d1, d2);
386 SkASSERT(approximately_equal(t1 + d1, expectedT[index])
387 || approximately_equal(t1 - d1, expectedT[index]));
388 SkASSERT(approximately_equal(t2 + d2, expectedT[index + 1])
389 || approximately_equal(t2 - d2, expectedT[index + 1]));
390 }
391}