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 | |
| 60 | static void oneOff(const Cubic& cubic1, const Cubic& cubic2) { |
| 61 | SkTDArray<Quadratic> quads1; |
| 62 | cubic_to_quadratics(cubic1, calcPrecision(cubic1), quads1); |
| 63 | for (int index = 0; index < quads1.count(); ++index) { |
| 64 | const Quadratic& q = quads1[index]; |
skia.committer@gmail.com | 15dd300 | 2013-01-18 07:07:28 +0000 | [diff] [blame] | 65 | 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] | 66 | q[1].x, q[1].y, q[2].x, q[2].y); |
| 67 | } |
| 68 | SkDebugf("\n"); |
| 69 | SkTDArray<Quadratic> quads2; |
| 70 | cubic_to_quadratics(cubic2, calcPrecision(cubic2), quads2); |
| 71 | for (int index = 0; index < quads2.count(); ++index) { |
| 72 | const Quadratic& q = quads2[index]; |
skia.committer@gmail.com | 15dd300 | 2013-01-18 07:07:28 +0000 | [diff] [blame] | 73 | 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] | 74 | q[1].x, q[1].y, q[2].x, q[2].y); |
| 75 | } |
| 76 | SkDebugf("\n"); |
| 77 | Intersections intersections2; |
| 78 | intersect2(cubic1, cubic2, intersections2); |
| 79 | for (int pt = 0; pt < intersections2.used(); ++pt) { |
| 80 | double tt1 = intersections2.fT[0][pt]; |
| 81 | double tx1, ty1; |
| 82 | xy_at_t(cubic1, tt1, tx1, ty1); |
| 83 | int pt2 = intersections2.fFlip ? intersections2.used() - pt - 1 : pt; |
| 84 | double tt2 = intersections2.fT[1][pt2]; |
| 85 | double tx2, ty2; |
| 86 | xy_at_t(cubic2, tt2, tx2, ty2); |
| 87 | SkDebugf("%s t1=%1.9g (%1.9g, %1.9g) (%1.9g, %1.9g) t2=%1.9g\n", __FUNCTION__, |
| 88 | tt1, tx1, ty1, tx2, ty2, tt2); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | static const Cubic testSet[] = { |
| 93 | {{67.426548091427676, 37.993772624988935}, {23.483695892376684, 90.476863174921306}, {35.597065061143162, 79.872482633158796}, {75.38634169631932, 18.244890038969412}}, |
| 94 | {{61.336508189019057, 82.693132843213675}, {44.639380902349664, 54.074825790745592}, {16.815615499771951, 20.049704667203923}, {41.866884958868326, 56.735503699973002}}, |
| 95 | |
| 96 | {{67.4265481, 37.9937726}, {23.4836959, 90.4768632}, {35.5970651, 79.8724826}, {75.3863417, 18.24489}}, |
| 97 | {{61.3365082, 82.6931328}, {44.6393809, 54.0748258}, {16.8156155, 20.0497047}, {41.866885, 56.7355037}}, |
| 98 | |
| 99 | {{18.1312339, 31.6473732}, {95.5711034, 63.5350219}, {92.3283165, 62.0158945}, {18.5656052, 32.1268808}}, |
| 100 | {{97.402018, 35.7169972}, {33.1127443, 25.8935163}, {1.13970027, 54.9424981}, {56.4860195, 60.529264}}, |
| 101 | }; |
| 102 | |
| 103 | const size_t testSetCount = sizeof(testSet) / sizeof(testSet[0]); |
| 104 | |
| 105 | void CubicIntersection_OneOffTest() { |
| 106 | for (size_t outer = 0; outer < testSetCount - 1; ++outer) { |
| 107 | SkDebugf("%s quads1[%d]\n", __FUNCTION__, outer); |
| 108 | const Cubic& cubic1 = testSet[outer]; |
| 109 | for (size_t inner = outer + 1; inner < testSetCount; ++inner) { |
| 110 | SkDebugf("%s quads2[%d]\n", __FUNCTION__, inner); |
| 111 | const Cubic& cubic2 = testSet[inner]; |
| 112 | oneOff(cubic1, cubic2); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | #define DEBUG_CRASH 1 |
| 118 | |
| 119 | class CubicChopper { |
| 120 | public: |
| 121 | |
| 122 | // only finds one intersection |
| 123 | CubicChopper(const Cubic& c1, const Cubic& c2) |
| 124 | : cubic1(c1) |
| 125 | , cubic2(c2) |
| 126 | , depth(0) { |
| 127 | } |
| 128 | |
| 129 | bool intersect(double minT1, double maxT1, double minT2, double maxT2) { |
| 130 | Cubic sub1, sub2; |
| 131 | // FIXME: carry last subdivide and reduceOrder result with cubic |
| 132 | sub_divide(cubic1, minT1, maxT1, sub1); |
| 133 | sub_divide(cubic2, minT2, maxT2, sub2); |
| 134 | Intersections i; |
| 135 | intersect2(sub1, sub2, i); |
| 136 | if (i.used() == 0) { |
| 137 | return false; |
| 138 | } |
| 139 | double x1, y1, x2, y2; |
| 140 | t1 = minT1 + i.fT[0][0] * (maxT1 - minT1); |
| 141 | t2 = minT2 + i.fT[1][0] * (maxT2 - minT2); |
| 142 | xy_at_t(cubic1, t1, x1, y1); |
| 143 | xy_at_t(cubic2, t2, x2, y2); |
| 144 | if (AlmostEqualUlps(x1, x2) && AlmostEqualUlps(y1, y2)) { |
| 145 | return true; |
| 146 | } |
| 147 | double half1 = (minT1 + maxT1) / 2; |
| 148 | double half2 = (minT2 + maxT2) / 2; |
| 149 | ++depth; |
| 150 | bool result; |
| 151 | if (depth & 1) { |
| 152 | result = intersect(minT1, half1, minT2, maxT2) || intersect(half1, maxT1, minT2, maxT2) |
| 153 | || intersect(minT1, maxT1, minT2, half2) || intersect(minT1, maxT1, half2, maxT2); |
| 154 | } else { |
| 155 | result = intersect(minT1, maxT1, minT2, half2) || intersect(minT1, maxT1, half2, maxT2) |
| 156 | || intersect(minT1, half1, minT2, maxT2) || intersect(half1, maxT1, minT2, maxT2); |
| 157 | } |
| 158 | --depth; |
| 159 | return result; |
| 160 | } |
| 161 | |
| 162 | const Cubic& cubic1; |
| 163 | const Cubic& cubic2; |
| 164 | double t1; |
| 165 | double t2; |
| 166 | int depth; |
| 167 | }; |
| 168 | |
| 169 | #define TRY_OLD 0 // old way fails on test == 1 |
| 170 | |
| 171 | void CubicIntersection_RandTest() { |
| 172 | srand(0); |
| 173 | const int tests = 1000000; // 10000000; |
| 174 | double largestFactor = DBL_MAX; |
| 175 | for (int test = 0; test < tests; ++test) { |
| 176 | Cubic cubic1, cubic2; |
| 177 | for (int i = 0; i < 4; ++i) { |
| 178 | cubic1[i].x = (double) rand() / RAND_MAX * 100; |
| 179 | cubic1[i].y = (double) rand() / RAND_MAX * 100; |
| 180 | cubic2[i].x = (double) rand() / RAND_MAX * 100; |
| 181 | cubic2[i].y = (double) rand() / RAND_MAX * 100; |
| 182 | } |
| 183 | if (test == 2513) { // the pair crosses three times, but the quadratic approximation |
| 184 | continue; // only sees one -- should be OK to ignore the other two? |
| 185 | } |
| 186 | if (test == 12932) { // this exposes a weakness when one cubic touches the other but |
| 187 | continue; // does not touch the quad approximation. Captured in qc.htm as cubic15 |
| 188 | } |
| 189 | #if DEBUG_CRASH |
| 190 | char str[1024]; |
| 191 | sprintf(str, "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n" |
| 192 | "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n", |
| 193 | cubic1[0].x, cubic1[0].y, cubic1[1].x, cubic1[1].y, cubic1[2].x, cubic1[2].y, |
| 194 | cubic1[3].x, cubic1[3].y, |
| 195 | cubic2[0].x, cubic2[0].y, cubic2[1].x, cubic2[1].y, cubic2[2].x, cubic2[2].y, |
| 196 | cubic2[3].x, cubic2[3].y); |
| 197 | #endif |
| 198 | _Rect rect1, rect2; |
| 199 | rect1.setBounds(cubic1); |
| 200 | rect2.setBounds(cubic2); |
| 201 | bool boundsIntersect = rect1.left <= rect2.right && rect2.left <= rect2.right |
| 202 | && rect1.top <= rect2.bottom && rect2.top <= rect1.bottom; |
| 203 | Intersections i1, i2; |
| 204 | #if TRY_OLD |
| 205 | bool oldIntersects = intersect(cubic1, cubic2, i1); |
| 206 | #else |
| 207 | bool oldIntersects = false; |
| 208 | #endif |
| 209 | if (test == -1) { |
| 210 | SkDebugf("ready...\n"); |
| 211 | } |
| 212 | bool newIntersects = intersect2(cubic1, cubic2, i2); |
| 213 | if (!boundsIntersect && (oldIntersects || newIntersects)) { |
| 214 | SkDebugf("%s %d unexpected intersection boundsIntersect=%d oldIntersects=%d" |
| 215 | " newIntersects=%d\n%s %s\n", __FUNCTION__, test, boundsIntersect, |
| 216 | oldIntersects, newIntersects, __FUNCTION__, str); |
| 217 | assert(0); |
| 218 | } |
| 219 | if (oldIntersects && !newIntersects) { |
| 220 | SkDebugf("%s %d missing intersection oldIntersects=%d newIntersects=%d\n%s %s\n", |
| 221 | __FUNCTION__, test, oldIntersects, newIntersects, __FUNCTION__, str); |
| 222 | assert(0); |
| 223 | } |
| 224 | if (!oldIntersects && !newIntersects) { |
| 225 | continue; |
| 226 | } |
| 227 | if (i2.used() > 1) { |
| 228 | continue; |
| 229 | // just look at single intercepts for simplicity |
| 230 | } |
| 231 | Intersections self1, self2; // self-intersect checks |
| 232 | if (intersect(cubic1, self1)) { |
| 233 | continue; |
| 234 | } |
| 235 | if (intersect(cubic2, self2)) { |
| 236 | continue; |
| 237 | } |
| 238 | // binary search for range necessary to enclose real intersection |
| 239 | CubicChopper c(cubic1, cubic2); |
| 240 | bool result = c.intersect(0, 1, 0, 1); |
| 241 | if (!result) { |
| 242 | // FIXME: a failure here probably means that a core routine used by CubicChopper is failing |
| 243 | continue; |
| 244 | } |
| 245 | double delta1 = fabs(c.t1 - i2.fT[0][0]); |
| 246 | double delta2 = fabs(c.t2 - i2.fT[1][0]); |
| 247 | double calc1 = calcPrecision(cubic1); |
| 248 | double calc2 = calcPrecision(cubic2); |
| 249 | double factor1 = calc1 / delta1; |
| 250 | double factor2 = calc2 / delta2; |
| 251 | SkDebugf("%s %d calc1=%1.9g delta1=%1.9g factor1=%1.9g calc2=%1.9g delta2=%1.9g" |
| 252 | " factor2=%1.9g\n", __FUNCTION__, test, |
| 253 | calc1, delta1, factor1, calc2, delta2, factor2); |
| 254 | if (factor1 < largestFactor) { |
| 255 | SkDebugf("WE HAVE A WINNER! %1.9g\n", factor1); |
| 256 | SkDebugf("%s\n", str); |
| 257 | oneOff(cubic1, cubic2); |
| 258 | largestFactor = factor1; |
| 259 | } |
| 260 | if (factor2 < largestFactor) { |
| 261 | SkDebugf("WE HAVE A WINNER! %1.9g\n", factor2); |
| 262 | SkDebugf("%s\n", str); |
| 263 | oneOff(cubic1, cubic2); |
| 264 | largestFactor = factor2; |
| 265 | } |
| 266 | } |
| 267 | } |