caryclark@google.com | 9e49fb6 | 2012-08-27 14:11:33 +0000 | [diff] [blame] | 1 | /* |
| 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.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 7 | #include "CurveIntersection.h" |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 8 | #include "CurveUtilities.h" |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 9 | #include "CubicIntersection_TestData.h" |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 10 | #include "Intersection_Tests.h" |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 11 | #include "Intersections.h" |
| 12 | #include "TestUtilities.h" |
| 13 | |
| 14 | const int firstCubicIntersectionTest = 9; |
| 15 | |
| 16 | void 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.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 24 | printf("%s [%d] cubic1 order=%d\n", __FUNCTION__, (int) index, order1); |
| 25 | continue; |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 26 | } |
| 27 | if (order2 < 4) { |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 28 | printf("%s [%d] cubic2 order=%d\n", __FUNCTION__, (int) index, order2); |
| 29 | continue; |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 30 | } |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 31 | if (implicit_matches(reduce1, reduce2)) { |
| 32 | printf("%s [%d] coincident\n", __FUNCTION__, (int) index); |
| 33 | continue; |
| 34 | } |
| 35 | Intersections tIntersections; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 36 | intersect(reduce1, reduce2, tIntersections); |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 37 | 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.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 48 | if (!AlmostEqualUlps(tx1, tx2)) { |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 49 | 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.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 52 | if (!AlmostEqualUlps(ty1, ty2)) { |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 53 | 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.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | } |
| 58 | } |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 59 | |
caryclark@google.com | 86adc0d | 2013-01-30 12:50:38 +0000 | [diff] [blame] | 60 | #define ONE_OFF_DEBUG 0 |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 61 | |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 62 | static void oneOff(const Cubic& cubic1, const Cubic& cubic2) { |
| 63 | SkTDArray<Quadratic> quads1; |
| 64 | cubic_to_quadratics(cubic1, calcPrecision(cubic1), quads1); |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 65 | #if ONE_OFF_DEBUG |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 66 | for (int index = 0; index < quads1.count(); ++index) { |
| 67 | const Quadratic& q = quads1[index]; |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 68 | SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", q[0].x, q[0].y, |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 69 | q[1].x, q[1].y, q[2].x, q[2].y); |
| 70 | } |
| 71 | SkDebugf("\n"); |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 72 | #endif |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 73 | SkTDArray<Quadratic> quads2; |
| 74 | cubic_to_quadratics(cubic2, calcPrecision(cubic2), quads2); |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 75 | #if ONE_OFF_DEBUG |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 76 | for (int index = 0; index < quads2.count(); ++index) { |
| 77 | const Quadratic& q = quads2[index]; |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 78 | SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", q[0].x, q[0].y, |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 79 | q[1].x, q[1].y, q[2].x, q[2].y); |
| 80 | } |
| 81 | SkDebugf("\n"); |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 82 | #endif |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 83 | 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.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 87 | _Point xy1, xy2; |
| 88 | xy_at_t(cubic1, tt1, xy1.x, xy1.y); |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 89 | int pt2 = intersections2.fFlip ? intersections2.used() - pt - 1 : pt; |
| 90 | double tt2 = intersections2.fT[1][pt2]; |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 91 | xy_at_t(cubic2, tt2, xy2.x, xy2.y); |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 92 | #if ONE_OFF_DEBUG |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 93 | SkDebugf("%s t1=%1.9g (%1.9g, %1.9g) (%1.9g, %1.9g) t2=%1.9g\n", __FUNCTION__, |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 94 | tt1, xy1.x, xy1.y, xy2.x, xy2.y, tt2); |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 95 | #endif |
caryclark@google.com | aa35831 | 2013-01-29 20:28:49 +0000 | [diff] [blame] | 96 | SkASSERT(xy1.approximatelyEqual(xy2)); |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
| 100 | static const Cubic testSet[] = { |
caryclark@google.com | aa35831 | 2013-01-29 20:28:49 +0000 | [diff] [blame] | 101 | {{0, 1}, {0, 2}, {1, 0}, {1, 0}}, |
| 102 | {{0, 1}, {0, 1}, {1, 0}, {2, 0}}, |
| 103 | |
caryclark@google.com | 85ec74c | 2013-01-28 19:25:51 +0000 | [diff] [blame] | 104 | {{0, 0}, {0, 1}, {1, 1}, {1, 0}}, |
| 105 | {{1, 0}, {0, 0}, {0, 1}, {1, 1}}, |
| 106 | |
| 107 | {{95.837747722788592, 45.025976907939643}, {16.564570095652982, 0.72959763963222402}, {63.209855865319199, 68.047528419665767}, {57.640240647662544, 59.524565264361243}}, |
| 108 | {{51.593891741518817, 38.53849970667553}, {62.34752929878772, 74.924924725166022}, {74.810149322641152, 34.17966562983564}, {29.368398119401373, 94.66719277886078}}, |
| 109 | |
| 110 | {{39.765160968417838, 33.060396198677083}, {5.1922921581157908, 66.854301452103215}, {31.619281802149157, 25.269248720849514}, {81.541621071073038, 70.025341524754353}}, |
| 111 | {{46.078911165743556, 48.259962651999651}, {20.24450549867214, 49.403916182650214}, {0.26325131778756683, 24.46489805563581}, {15.915006546264051, 83.515023059917155}}, |
| 112 | |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 113 | {{65.454505973241524, 93.881892270353575}, {45.867360264932437, 92.723972719499827}, {2.1464054482739447, 74.636369140183717}, {33.774068594804994, 40.770872887582925}}, |
| 114 | {{72.963387832494163, 95.659300729473728}, {11.809496633619768, 82.209921247423594}, {13.456139067865974, 57.329313623406605}, {36.060621606214262, 70.867335643091849}}, |
| 115 | |
| 116 | {{32.484981432782945, 75.082940782924624}, {42.467313093350882, 48.131159948246157}, {3.5963115764764657, 43.208665839959245}, {79.442476890721579, 89.709102357602262}}, |
| 117 | {{18.98573861410177, 93.308887208490106}, {40.405250173250792, 91.039661826118675}, {8.0467721950480584, 42.100282172719147}, {40.883324221187891, 26.030185504830527}}, |
| 118 | |
| 119 | {{7.5374809128872498, 82.441702896003477}, {22.444346930107265, 22.138854312775123}, {66.76091829629658, 50.753805856571446}, {78.193478508942519, 97.7932997968948}}, |
| 120 | {{97.700573130371311, 53.53260215070685}, {87.72443481149358, 84.575876772671876}, {19.215031396232092, 47.032676472809484}, {11.989686410869325, 10.659507480757082}}, |
| 121 | |
| 122 | {{26.192053931854691, 9.8504326817814416}, {10.174241480498686, 98.476562741434464}, {21.177712558385782, 33.814968789841501}, {75.329030899018534, 55.02231980442177}}, |
| 123 | {{56.222082700683771, 24.54395039218662}, {95.589995289030483, 81.050822735322086}, {28.180450866082897, 28.837706255185282}, {60.128952916771617, 87.311672180570511}}, |
| 124 | |
| 125 | {{42.449716172390481, 52.379709366885805}, {27.896043159019225, 48.797373636065686}, {92.770268299044233, 89.899302036454571}, {12.102066544863426, 99.43241951960718}}, |
| 126 | {{45.77532924980639, 45.958701495993274}, {37.458701356062065, 68.393691335056758}, {37.569326692060258, 27.673713456687381}, {60.674866037757539, 62.47349659096146}}, |
| 127 | |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 128 | {{67.426548091427676, 37.993772624988935}, {23.483695892376684, 90.476863174921306}, {35.597065061143162, 79.872482633158796}, {75.38634169631932, 18.244890038969412}}, |
| 129 | {{61.336508189019057, 82.693132843213675}, {44.639380902349664, 54.074825790745592}, {16.815615499771951, 20.049704667203923}, {41.866884958868326, 56.735503699973002}}, |
| 130 | |
| 131 | {{67.4265481, 37.9937726}, {23.4836959, 90.4768632}, {35.5970651, 79.8724826}, {75.3863417, 18.24489}}, |
| 132 | {{61.3365082, 82.6931328}, {44.6393809, 54.0748258}, {16.8156155, 20.0497047}, {41.866885, 56.7355037}}, |
| 133 | |
| 134 | {{18.1312339, 31.6473732}, {95.5711034, 63.5350219}, {92.3283165, 62.0158945}, {18.5656052, 32.1268808}}, |
| 135 | {{97.402018, 35.7169972}, {33.1127443, 25.8935163}, {1.13970027, 54.9424981}, {56.4860195, 60.529264}}, |
| 136 | }; |
| 137 | |
| 138 | const size_t testSetCount = sizeof(testSet) / sizeof(testSet[0]); |
| 139 | |
| 140 | void CubicIntersection_OneOffTest() { |
| 141 | for (size_t outer = 0; outer < testSetCount - 1; ++outer) { |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 142 | #if ONE_OFF_DEBUG |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 143 | SkDebugf("%s quads1[%d]\n", __FUNCTION__, outer); |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 144 | #endif |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 145 | const Cubic& cubic1 = testSet[outer]; |
| 146 | for (size_t inner = outer + 1; inner < testSetCount; ++inner) { |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 147 | #if ONE_OFF_DEBUG |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 148 | SkDebugf("%s quads2[%d]\n", __FUNCTION__, inner); |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 149 | #endif |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 150 | const Cubic& cubic2 = testSet[inner]; |
| 151 | oneOff(cubic1, cubic2); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | #define DEBUG_CRASH 1 |
| 157 | |
| 158 | class CubicChopper { |
| 159 | public: |
| 160 | |
| 161 | // only finds one intersection |
| 162 | CubicChopper(const Cubic& c1, const Cubic& c2) |
| 163 | : cubic1(c1) |
| 164 | , cubic2(c2) |
| 165 | , depth(0) { |
| 166 | } |
| 167 | |
| 168 | bool intersect(double minT1, double maxT1, double minT2, double maxT2) { |
| 169 | Cubic sub1, sub2; |
| 170 | // FIXME: carry last subdivide and reduceOrder result with cubic |
| 171 | sub_divide(cubic1, minT1, maxT1, sub1); |
| 172 | sub_divide(cubic2, minT2, maxT2, sub2); |
| 173 | Intersections i; |
| 174 | intersect2(sub1, sub2, i); |
| 175 | if (i.used() == 0) { |
| 176 | return false; |
| 177 | } |
| 178 | double x1, y1, x2, y2; |
| 179 | t1 = minT1 + i.fT[0][0] * (maxT1 - minT1); |
| 180 | t2 = minT2 + i.fT[1][0] * (maxT2 - minT2); |
| 181 | xy_at_t(cubic1, t1, x1, y1); |
| 182 | xy_at_t(cubic2, t2, x2, y2); |
| 183 | if (AlmostEqualUlps(x1, x2) && AlmostEqualUlps(y1, y2)) { |
| 184 | return true; |
| 185 | } |
| 186 | double half1 = (minT1 + maxT1) / 2; |
| 187 | double half2 = (minT2 + maxT2) / 2; |
| 188 | ++depth; |
| 189 | bool result; |
| 190 | if (depth & 1) { |
| 191 | result = intersect(minT1, half1, minT2, maxT2) || intersect(half1, maxT1, minT2, maxT2) |
| 192 | || intersect(minT1, maxT1, minT2, half2) || intersect(minT1, maxT1, half2, maxT2); |
| 193 | } else { |
| 194 | result = intersect(minT1, maxT1, minT2, half2) || intersect(minT1, maxT1, half2, maxT2) |
| 195 | || intersect(minT1, half1, minT2, maxT2) || intersect(half1, maxT1, minT2, maxT2); |
| 196 | } |
| 197 | --depth; |
| 198 | return result; |
| 199 | } |
| 200 | |
| 201 | const Cubic& cubic1; |
| 202 | const Cubic& cubic2; |
| 203 | double t1; |
| 204 | double t2; |
| 205 | int depth; |
| 206 | }; |
| 207 | |
| 208 | #define TRY_OLD 0 // old way fails on test == 1 |
| 209 | |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 210 | void CubicIntersection_RandTestOld() { |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 211 | srand(0); |
| 212 | const int tests = 1000000; // 10000000; |
| 213 | double largestFactor = DBL_MAX; |
| 214 | for (int test = 0; test < tests; ++test) { |
| 215 | Cubic cubic1, cubic2; |
| 216 | for (int i = 0; i < 4; ++i) { |
| 217 | cubic1[i].x = (double) rand() / RAND_MAX * 100; |
| 218 | cubic1[i].y = (double) rand() / RAND_MAX * 100; |
| 219 | cubic2[i].x = (double) rand() / RAND_MAX * 100; |
| 220 | cubic2[i].y = (double) rand() / RAND_MAX * 100; |
| 221 | } |
| 222 | if (test == 2513) { // the pair crosses three times, but the quadratic approximation |
| 223 | continue; // only sees one -- should be OK to ignore the other two? |
| 224 | } |
| 225 | if (test == 12932) { // this exposes a weakness when one cubic touches the other but |
| 226 | continue; // does not touch the quad approximation. Captured in qc.htm as cubic15 |
| 227 | } |
| 228 | #if DEBUG_CRASH |
| 229 | char str[1024]; |
| 230 | sprintf(str, "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n" |
| 231 | "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n", |
| 232 | cubic1[0].x, cubic1[0].y, cubic1[1].x, cubic1[1].y, cubic1[2].x, cubic1[2].y, |
| 233 | cubic1[3].x, cubic1[3].y, |
| 234 | cubic2[0].x, cubic2[0].y, cubic2[1].x, cubic2[1].y, cubic2[2].x, cubic2[2].y, |
| 235 | cubic2[3].x, cubic2[3].y); |
| 236 | #endif |
| 237 | _Rect rect1, rect2; |
| 238 | rect1.setBounds(cubic1); |
| 239 | rect2.setBounds(cubic2); |
| 240 | bool boundsIntersect = rect1.left <= rect2.right && rect2.left <= rect2.right |
| 241 | && rect1.top <= rect2.bottom && rect2.top <= rect1.bottom; |
| 242 | Intersections i1, i2; |
| 243 | #if TRY_OLD |
| 244 | bool oldIntersects = intersect(cubic1, cubic2, i1); |
| 245 | #else |
| 246 | bool oldIntersects = false; |
| 247 | #endif |
| 248 | if (test == -1) { |
| 249 | SkDebugf("ready...\n"); |
| 250 | } |
| 251 | bool newIntersects = intersect2(cubic1, cubic2, i2); |
| 252 | if (!boundsIntersect && (oldIntersects || newIntersects)) { |
| 253 | SkDebugf("%s %d unexpected intersection boundsIntersect=%d oldIntersects=%d" |
| 254 | " newIntersects=%d\n%s %s\n", __FUNCTION__, test, boundsIntersect, |
| 255 | oldIntersects, newIntersects, __FUNCTION__, str); |
caryclark@google.com | aa35831 | 2013-01-29 20:28:49 +0000 | [diff] [blame] | 256 | SkASSERT(0); |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 257 | } |
| 258 | if (oldIntersects && !newIntersects) { |
| 259 | SkDebugf("%s %d missing intersection oldIntersects=%d newIntersects=%d\n%s %s\n", |
| 260 | __FUNCTION__, test, oldIntersects, newIntersects, __FUNCTION__, str); |
caryclark@google.com | aa35831 | 2013-01-29 20:28:49 +0000 | [diff] [blame] | 261 | SkASSERT(0); |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 262 | } |
| 263 | if (!oldIntersects && !newIntersects) { |
| 264 | continue; |
| 265 | } |
| 266 | if (i2.used() > 1) { |
| 267 | continue; |
| 268 | // just look at single intercepts for simplicity |
| 269 | } |
| 270 | Intersections self1, self2; // self-intersect checks |
| 271 | if (intersect(cubic1, self1)) { |
| 272 | continue; |
| 273 | } |
| 274 | if (intersect(cubic2, self2)) { |
| 275 | continue; |
| 276 | } |
| 277 | // binary search for range necessary to enclose real intersection |
| 278 | CubicChopper c(cubic1, cubic2); |
| 279 | bool result = c.intersect(0, 1, 0, 1); |
| 280 | if (!result) { |
| 281 | // FIXME: a failure here probably means that a core routine used by CubicChopper is failing |
| 282 | continue; |
| 283 | } |
| 284 | double delta1 = fabs(c.t1 - i2.fT[0][0]); |
| 285 | double delta2 = fabs(c.t2 - i2.fT[1][0]); |
| 286 | double calc1 = calcPrecision(cubic1); |
| 287 | double calc2 = calcPrecision(cubic2); |
| 288 | double factor1 = calc1 / delta1; |
| 289 | double factor2 = calc2 / delta2; |
| 290 | SkDebugf("%s %d calc1=%1.9g delta1=%1.9g factor1=%1.9g calc2=%1.9g delta2=%1.9g" |
| 291 | " factor2=%1.9g\n", __FUNCTION__, test, |
| 292 | calc1, delta1, factor1, calc2, delta2, factor2); |
| 293 | if (factor1 < largestFactor) { |
| 294 | SkDebugf("WE HAVE A WINNER! %1.9g\n", factor1); |
| 295 | SkDebugf("%s\n", str); |
| 296 | oneOff(cubic1, cubic2); |
| 297 | largestFactor = factor1; |
| 298 | } |
| 299 | if (factor2 < largestFactor) { |
| 300 | SkDebugf("WE HAVE A WINNER! %1.9g\n", factor2); |
| 301 | SkDebugf("%s\n", str); |
| 302 | oneOff(cubic1, cubic2); |
| 303 | largestFactor = factor2; |
| 304 | } |
| 305 | } |
| 306 | } |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 307 | |
| 308 | void CubicIntersection_RandTest() { |
| 309 | srand(0); |
caryclark@google.com | 85ec74c | 2013-01-28 19:25:51 +0000 | [diff] [blame] | 310 | const int tests = 10000000; |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 311 | for (int test = 0; test < tests; ++test) { |
| 312 | Cubic cubic1, cubic2; |
| 313 | for (int i = 0; i < 4; ++i) { |
| 314 | cubic1[i].x = (double) rand() / RAND_MAX * 100; |
| 315 | cubic1[i].y = (double) rand() / RAND_MAX * 100; |
| 316 | cubic2[i].x = (double) rand() / RAND_MAX * 100; |
| 317 | cubic2[i].y = (double) rand() / RAND_MAX * 100; |
| 318 | } |
| 319 | #if DEBUG_CRASH |
| 320 | char str[1024]; |
| 321 | sprintf(str, "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n" |
| 322 | "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n", |
| 323 | cubic1[0].x, cubic1[0].y, cubic1[1].x, cubic1[1].y, cubic1[2].x, cubic1[2].y, |
| 324 | cubic1[3].x, cubic1[3].y, |
| 325 | cubic2[0].x, cubic2[0].y, cubic2[1].x, cubic2[1].y, cubic2[2].x, cubic2[2].y, |
| 326 | cubic2[3].x, cubic2[3].y); |
| 327 | #endif |
| 328 | _Rect rect1, rect2; |
| 329 | rect1.setBounds(cubic1); |
| 330 | rect2.setBounds(cubic2); |
| 331 | bool boundsIntersect = rect1.left <= rect2.right && rect2.left <= rect2.right |
| 332 | && rect1.top <= rect2.bottom && rect2.top <= rect1.bottom; |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 333 | if (test == -1) { |
| 334 | SkDebugf("ready...\n"); |
| 335 | } |
| 336 | Intersections intersections2; |
| 337 | bool newIntersects = intersect2(cubic1, cubic2, intersections2); |
| 338 | if (!boundsIntersect && newIntersects) { |
| 339 | SkDebugf("%s %d unexpected intersection boundsIntersect=%d " |
| 340 | " newIntersects=%d\n%s %s\n", __FUNCTION__, test, boundsIntersect, |
| 341 | newIntersects, __FUNCTION__, str); |
caryclark@google.com | aa35831 | 2013-01-29 20:28:49 +0000 | [diff] [blame] | 342 | SkASSERT(0); |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 343 | } |
| 344 | for (int pt = 0; pt < intersections2.used(); ++pt) { |
| 345 | double tt1 = intersections2.fT[0][pt]; |
| 346 | _Point xy1, xy2; |
| 347 | xy_at_t(cubic1, tt1, xy1.x, xy1.y); |
| 348 | int pt2 = intersections2.fFlip ? intersections2.used() - pt - 1 : pt; |
| 349 | double tt2 = intersections2.fT[1][pt2]; |
| 350 | xy_at_t(cubic2, tt2, xy2.x, xy2.y); |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 351 | #if 0 |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 352 | SkDebugf("%s t1=%1.9g (%1.9g, %1.9g) (%1.9g, %1.9g) t2=%1.9g\n", __FUNCTION__, |
| 353 | tt1, xy1.x, xy1.y, xy2.x, xy2.y, tt2); |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 354 | #endif |
caryclark@google.com | aa35831 | 2013-01-29 20:28:49 +0000 | [diff] [blame] | 355 | SkASSERT(xy1.approximatelyEqual(xy2)); |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | } |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 359 | |
| 360 | static Cubic deltaTestSet[] = { |
| 361 | {{1, 4}, {1, 4.*2/3}, {1, 4.*1/3}, {1, 0}}, |
| 362 | {{0, 3}, {1, 2}, {2, 1}, {3, 0}}, |
| 363 | {{1, 4}, {1, 4.*2/3}, {1, 4.*1/3}, {1, 0}}, |
| 364 | {{3.5, 1}, {2.5, 2}, {1.5, 3}, {0.5, 4}} |
| 365 | }; |
| 366 | |
| 367 | size_t deltaTestSetLen = sizeof(deltaTestSet) / sizeof(deltaTestSet[0]); |
| 368 | |
| 369 | static double deltaTestSetT[] = { |
| 370 | 3./8, |
| 371 | 5./12, |
| 372 | 6./8, |
| 373 | 9./12 |
| 374 | }; |
| 375 | |
| 376 | size_t deltaTestSetTLen = sizeof(deltaTestSetT) / sizeof(deltaTestSetT[0]); |
| 377 | |
| 378 | static double expectedT[] = { |
| 379 | 0.5, |
| 380 | 1./3, |
| 381 | 1./8, |
| 382 | 5./6 |
| 383 | }; |
| 384 | |
| 385 | size_t expectedTLen = sizeof(expectedT) / sizeof(expectedT[0]); |
| 386 | |
| 387 | // FIXME: this test no longer valid -- does not take minimum scale contribution into account |
| 388 | void CubicIntersection_ComputeDeltaTest() { |
| 389 | SkASSERT(deltaTestSetLen == deltaTestSetTLen); |
| 390 | SkASSERT(expectedTLen == deltaTestSetTLen); |
| 391 | for (size_t index = 0; index < deltaTestSetLen; index += 2) { |
| 392 | const Cubic& c1 = deltaTestSet[index]; |
| 393 | const Cubic& c2 = deltaTestSet[index + 1]; |
| 394 | double t1 = deltaTestSetT[index]; |
| 395 | double t2 = deltaTestSetT[index + 1]; |
| 396 | double d1, d2; |
| 397 | computeDelta(c1, t1, 1, c2, t2, 1, d1, d2); |
| 398 | SkASSERT(approximately_equal(t1 + d1, expectedT[index]) |
| 399 | || approximately_equal(t1 - d1, expectedT[index])); |
| 400 | SkASSERT(approximately_equal(t2 + d2, expectedT[index + 1]) |
| 401 | || approximately_equal(t2 - d2, expectedT[index + 1])); |
| 402 | } |
| 403 | } |