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 | 9d5f99b | 2013-01-22 12:55:54 +0000 | [diff] [blame] | 7 | |
| 8 | #include "CubicUtilities.h" |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 9 | #include "CurveIntersection.h" |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 10 | #include "Intersections.h" |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 11 | #include "IntersectionUtilities.h" |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 12 | #include "LineIntersection.h" |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 13 | #include "LineUtilities.h" |
caryclark@google.com | d0a19eb | 2013-02-19 12:49:33 +0000 | [diff] [blame] | 14 | #include "QuadraticUtilities.h" |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 15 | |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 16 | #if ONE_OFF_DEBUG |
caryclark@google.com | 7ff5c84 | 2013-02-26 15:56:05 +0000 | [diff] [blame^] | 17 | static const double tLimits[2][2] = {{0.772784538, 0.77278492}, {0.999111748, 0.999112129}}; |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 18 | #endif |
| 19 | |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 20 | #define DEBUG_QUAD_PART 0 |
caryclark@google.com | 47d73da | 2013-02-17 01:41:25 +0000 | [diff] [blame] | 21 | #define SWAP_TOP_DEBUG 0 |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 22 | |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 23 | static int quadPart(const Cubic& cubic, double tStart, double tEnd, Quadratic& simple) { |
| 24 | Cubic part; |
| 25 | sub_divide(cubic, tStart, tEnd, part); |
| 26 | Quadratic quad; |
| 27 | demote_cubic_to_quad(part, quad); |
| 28 | // FIXME: should reduceOrder be looser in this use case if quartic is going to blow up on an |
| 29 | // extremely shallow quadratic? |
caryclark@google.com | 47d73da | 2013-02-17 01:41:25 +0000 | [diff] [blame] | 30 | int order = reduceOrder(quad, simple, kReduceOrder_TreatAsFill); |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 31 | #if DEBUG_QUAD_PART |
| 32 | SkDebugf("%s cubic=(%1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g) t=(%1.17g,%1.17g)\n", |
| 33 | __FUNCTION__, cubic[0].x, cubic[0].y, cubic[1].x, cubic[1].y, cubic[2].x, cubic[2].y, |
| 34 | cubic[3].x, cubic[3].y, tStart, tEnd); |
| 35 | SkDebugf("%s part=(%1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g)" |
| 36 | " quad=(%1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g)\n", __FUNCTION__, part[0].x, part[0].y, |
| 37 | part[1].x, part[1].y, part[2].x, part[2].y, part[3].x, part[3].y, quad[0].x, quad[0].y, |
| 38 | quad[1].x, quad[1].y, quad[2].x, quad[2].y); |
| 39 | SkDebugf("%s simple=(%1.17g,%1.17g", __FUNCTION__, simple[0].x, simple[0].y); |
| 40 | if (order > 1) { |
| 41 | SkDebugf(" %1.17g,%1.17g", simple[1].x, simple[1].y); |
| 42 | } |
| 43 | if (order > 2) { |
| 44 | SkDebugf(" %1.17g,%1.17g", simple[2].x, simple[2].y); |
| 45 | } |
| 46 | SkDebugf(")\n"); |
| 47 | SkASSERT(order < 4 && order > 0); |
| 48 | #endif |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 49 | return order; |
| 50 | } |
| 51 | |
| 52 | static void intersectWithOrder(const Quadratic& simple1, int order1, const Quadratic& simple2, |
| 53 | int order2, Intersections& i) { |
| 54 | if (order1 == 3 && order2 == 3) { |
| 55 | intersect2(simple1, simple2, i); |
| 56 | } else if (order1 <= 2 && order2 <= 2) { |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 57 | intersect((const _Line&) simple1, (const _Line&) simple2, i); |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 58 | } else if (order1 == 3 && order2 <= 2) { |
| 59 | intersect(simple1, (const _Line&) simple2, i); |
| 60 | } else { |
| 61 | SkASSERT(order1 <= 2 && order2 == 3); |
| 62 | intersect(simple2, (const _Line&) simple1, i); |
| 63 | for (int s = 0; s < i.fUsed; ++s) { |
| 64 | SkTSwap(i.fT[0][s], i.fT[1][s]); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 69 | static double distanceFromEnd(double t) { |
| 70 | return t > 0.5 ? 1 - t : t; |
| 71 | } |
| 72 | |
| 73 | // OPTIMIZATION: this used to try to guess the value for delta, and that may still be worthwhile |
| 74 | static void bumpForRetry(double t1, double t2, double& s1, double& e1, double& s2, double& e2) { |
| 75 | double dt1 = distanceFromEnd(t1); |
| 76 | double dt2 = distanceFromEnd(t2); |
caryclark@google.com | 7ff5c84 | 2013-02-26 15:56:05 +0000 | [diff] [blame^] | 77 | double delta = 1.0 / gPrecisionUnit; |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 78 | if (dt1 < dt2) { |
| 79 | if (t1 == dt1) { |
| 80 | s1 = SkTMax(s1 - delta, 0.); |
| 81 | } else { |
| 82 | e1 = SkTMin(e1 + delta, 1.); |
| 83 | } |
| 84 | } else { |
| 85 | if (t2 == dt2) { |
| 86 | s2 = SkTMax(s2 - delta, 0.); |
| 87 | } else { |
| 88 | e2 = SkTMin(e2 + delta, 1.); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | static bool doIntersect(const Cubic& cubic1, double t1s, double t1m, double t1e, |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 94 | const Cubic& cubic2, double t2s, double t2m, double t2e, Intersections& i) { |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 95 | bool result = false; |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 96 | i.upDepth(); |
| 97 | // divide the quadratics at the new t value and try again |
| 98 | double p1s = t1s; |
| 99 | double p1e = t1m; |
| 100 | for (int p1 = 0; p1 < 2; ++p1) { |
| 101 | Quadratic s1a; |
| 102 | int o1a = quadPart(cubic1, p1s, p1e, s1a); |
| 103 | double p2s = t2s; |
| 104 | double p2e = t2m; |
| 105 | for (int p2 = 0; p2 < 2; ++p2) { |
| 106 | Quadratic s2a; |
| 107 | int o2a = quadPart(cubic2, p2s, p2e, s2a); |
| 108 | Intersections locals; |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 109 | #if ONE_OFF_DEBUG |
| 110 | if (tLimits[0][0] >= p1s && tLimits[0][1] <= p1e |
| 111 | && tLimits[1][0] >= p2s && tLimits[1][1] <= p2e) { |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 112 | SkDebugf("t1=(%1.9g,%1.9g) o1=%d t2=(%1.9g,%1.9g) o2=%d\n", |
| 113 | p1s, p1e, o1a, p2s, p2e, o2a); |
| 114 | if (o1a == 2) { |
| 115 | SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", |
| 116 | s1a[0].x, s1a[0].y, s1a[1].x, s1a[1].y); |
| 117 | } else { |
| 118 | SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", |
| 119 | s1a[0].x, s1a[0].y, s1a[1].x, s1a[1].y, s1a[2].x, s1a[2].y); |
| 120 | } |
| 121 | if (o2a == 2) { |
| 122 | SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", |
| 123 | s2a[0].x, s2a[0].y, s2a[1].x, s2a[1].y); |
| 124 | } else { |
| 125 | SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", |
| 126 | s2a[0].x, s2a[0].y, s2a[1].x, s2a[1].y, s2a[2].x, s2a[2].y); |
| 127 | } |
| 128 | Intersections xlocals; |
| 129 | intersectWithOrder(s1a, o1a, s2a, o2a, xlocals); |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 130 | SkDebugf("xlocals.fUsed=%d depth=%d\n", xlocals.used(), i.depth()); |
skia.committer@gmail.com | ee235f9 | 2013-02-08 07:16:45 +0000 | [diff] [blame] | 131 | } |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 132 | #endif |
| 133 | intersectWithOrder(s1a, o1a, s2a, o2a, locals); |
| 134 | for (int tIdx = 0; tIdx < locals.used(); ++tIdx) { |
| 135 | double to1 = p1s + (p1e - p1s) * locals.fT[0][tIdx]; |
| 136 | double to2 = p2s + (p2e - p2s) * locals.fT[1][tIdx]; |
| 137 | // if the computed t is not sufficiently precise, iterate |
| 138 | _Point p1, p2; |
| 139 | xy_at_t(cubic1, to1, p1.x, p1.y); |
| 140 | xy_at_t(cubic2, to2, p2.x, p2.y); |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 141 | #if ONE_OFF_DEBUG |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 142 | SkDebugf("to1=%1.9g p1=(%1.9g,%1.9g) to2=%1.9g p2=(%1.9g,%1.9g) d=%1.9g\n", |
| 143 | to1, p1.x, p1.y, to2, p2.x, p2.y, p1.distance(p2)); |
skia.committer@gmail.com | ee235f9 | 2013-02-08 07:16:45 +0000 | [diff] [blame] | 144 | |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 145 | #endif |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 146 | if (p1.approximatelyEqualHalf(p2)) { |
| 147 | i.insertSwap(to1, to2, p1); |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 148 | result = true; |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 149 | } else { |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 150 | result = doIntersect(cubic1, p1s, to1, p1e, cubic2, p2s, to2, p2e, i); |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 151 | if (!result && p1.approximatelyEqual(p2)) { |
| 152 | i.insertSwap(to1, to2, p1); |
caryclark@google.com | 47d73da | 2013-02-17 01:41:25 +0000 | [diff] [blame] | 153 | #if SWAP_TOP_DEBUG |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 154 | SkDebugf("!!!\n"); |
caryclark@google.com | 47d73da | 2013-02-17 01:41:25 +0000 | [diff] [blame] | 155 | #endif |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 156 | result = true; |
| 157 | } else |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 158 | // if both cubics curve in the same direction, the quadratic intersection |
skia.committer@gmail.com | ee235f9 | 2013-02-08 07:16:45 +0000 | [diff] [blame] | 159 | // may mark a range that does not contain the cubic intersection. If no |
| 160 | // intersection is found, look again including the t distance of the |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 161 | // of the quadratic intersection nearest a quadratic end (which in turn is |
skia.committer@gmail.com | ee235f9 | 2013-02-08 07:16:45 +0000 | [diff] [blame] | 162 | // nearest the actual cubic) |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 163 | if (!result) { |
| 164 | double b1s = p1s; |
| 165 | double b1e = p1e; |
| 166 | double b2s = p2s; |
| 167 | double b2e = p2e; |
| 168 | bumpForRetry(locals.fT[0][tIdx], locals.fT[1][tIdx], b1s, b1e, b2s, b2e); |
| 169 | result = doIntersect(cubic1, b1s, to1, b1e, cubic2, b2s, to2, b2e, i); |
| 170 | } |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | p2s = p2e; |
| 174 | p2e = t2e; |
| 175 | } |
| 176 | p1s = p1e; |
| 177 | p1e = t1e; |
| 178 | } |
| 179 | i.downDepth(); |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 180 | return result; |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 181 | } |
caryclark@google.com | 85ec74c | 2013-01-28 19:25:51 +0000 | [diff] [blame] | 182 | |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 183 | // this flavor approximates the cubics with quads to find the intersecting ts |
| 184 | // OPTIMIZE: if this strategy proves successful, the quad approximations, or the ts used |
| 185 | // to create the approximations, could be stored in the cubic segment |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 186 | // FIXME: this strategy needs to intersect the convex hull on either end with the opposite to |
| 187 | // account for inset quadratics that cause the endpoint intersection to avoid detection |
| 188 | // the segments can be very short -- the length of the maximum quadratic error (precision) |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 189 | static bool intersect2(const Cubic& cubic1, double t1s, double t1e, const Cubic& cubic2, |
caryclark@google.com | 85ec74c | 2013-01-28 19:25:51 +0000 | [diff] [blame] | 190 | double t2s, double t2e, double precisionScale, Intersections& i) { |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 191 | Cubic c1, c2; |
| 192 | sub_divide(cubic1, t1s, t1e, c1); |
| 193 | sub_divide(cubic2, t2s, t2e, c2); |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 194 | SkTDArray<double> ts1; |
caryclark@google.com | 85ec74c | 2013-01-28 19:25:51 +0000 | [diff] [blame] | 195 | cubic_to_quadratics(c1, calcPrecision(c1) * precisionScale, ts1); |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 196 | SkTDArray<double> ts2; |
caryclark@google.com | 85ec74c | 2013-01-28 19:25:51 +0000 | [diff] [blame] | 197 | cubic_to_quadratics(c2, calcPrecision(c2) * precisionScale, ts2); |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 198 | double t1Start = t1s; |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 199 | int ts1Count = ts1.count(); |
| 200 | for (int i1 = 0; i1 <= ts1Count; ++i1) { |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 201 | const double tEnd1 = i1 < ts1Count ? ts1[i1] : 1; |
| 202 | const double t1 = t1s + (t1e - t1s) * tEnd1; |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 203 | Quadratic s1; |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 204 | int o1 = quadPart(cubic1, t1Start, t1, s1); |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 205 | double t2Start = t2s; |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 206 | int ts2Count = ts2.count(); |
| 207 | for (int i2 = 0; i2 <= ts2Count; ++i2) { |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 208 | const double tEnd2 = i2 < ts2Count ? ts2[i2] : 1; |
| 209 | const double t2 = t2s + (t2e - t2s) * tEnd2; |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 210 | Quadratic s2; |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 211 | int o2 = quadPart(cubic2, t2Start, t2, s2); |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 212 | #if ONE_OFF_DEBUG |
| 213 | if (tLimits[0][0] >= t1Start && tLimits[0][1] <= t1 |
| 214 | && tLimits[1][0] >= t2Start && tLimits[1][1] <= t2) { |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 215 | Cubic cSub1, cSub2; |
| 216 | sub_divide(cubic1, t1Start, tEnd1, cSub1); |
| 217 | sub_divide(cubic2, t2Start, tEnd2, cSub2); |
| 218 | SkDebugf("t1=(%1.9g,%1.9g) t2=(%1.9g,%1.9g)\n", |
| 219 | t1Start, t1, t2Start, t2); |
| 220 | Intersections xlocals; |
| 221 | intersectWithOrder(s1, o1, s2, o2, xlocals); |
| 222 | SkDebugf("xlocals.fUsed=%d\n", xlocals.used()); |
| 223 | } |
| 224 | #endif |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 225 | Intersections locals; |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 226 | intersectWithOrder(s1, o1, s2, o2, locals); |
skia.committer@gmail.com | ee235f9 | 2013-02-08 07:16:45 +0000 | [diff] [blame] | 227 | |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 228 | for (int tIdx = 0; tIdx < locals.used(); ++tIdx) { |
| 229 | double to1 = t1Start + (t1 - t1Start) * locals.fT[0][tIdx]; |
| 230 | double to2 = t2Start + (t2 - t2Start) * locals.fT[1][tIdx]; |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 231 | // if the computed t is not sufficiently precise, iterate |
| 232 | _Point p1, p2; |
| 233 | xy_at_t(cubic1, to1, p1.x, p1.y); |
| 234 | xy_at_t(cubic2, to2, p2.x, p2.y); |
| 235 | if (p1.approximatelyEqual(p2)) { |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 236 | i.insert(to1, to2, p1); |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 237 | } else { |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 238 | #if ONE_OFF_DEBUG |
| 239 | if (tLimits[0][0] >= t1Start && tLimits[0][1] <= t1 |
| 240 | && tLimits[1][0] >= t2Start && tLimits[1][1] <= t2) { |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 241 | SkDebugf("t1=(%1.9g,%1.9g) t2=(%1.9g,%1.9g)\n", |
| 242 | t1Start, t1, t2Start, t2); |
| 243 | } |
| 244 | #endif |
| 245 | bool found = doIntersect(cubic1, t1Start, to1, t1, cubic2, t2Start, to2, t2, i); |
| 246 | if (!found) { |
| 247 | double b1s = t1Start; |
| 248 | double b1e = t1; |
| 249 | double b2s = t2Start; |
| 250 | double b2e = t2; |
| 251 | bumpForRetry(locals.fT[0][tIdx], locals.fT[1][tIdx], b1s, b1e, b2s, b2e); |
| 252 | doIntersect(cubic1, b1s, to1, b1e, cubic2, b2s, to2, b2e, i); |
| 253 | } |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 254 | } |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 255 | } |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 256 | int coincidentCount = locals.coincidentUsed(); |
| 257 | if (coincidentCount) { |
| 258 | // FIXME: one day, we'll probably need to allow coincident + non-coincident pts |
| 259 | SkASSERT(coincidentCount == locals.used()); |
| 260 | SkASSERT(coincidentCount == 2); |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 261 | double coTs[2][2]; |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 262 | for (int tIdx = 0; tIdx < coincidentCount; ++tIdx) { |
| 263 | if (locals.fIsCoincident[0] & (1 << tIdx)) { |
| 264 | coTs[0][tIdx] = t1Start + (t1 - t1Start) * locals.fT[0][tIdx]; |
| 265 | } |
| 266 | if (locals.fIsCoincident[1] & (1 << tIdx)) { |
| 267 | coTs[1][tIdx] = t2Start + (t2 - t2Start) * locals.fT[1][tIdx]; |
| 268 | } |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 269 | } |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 270 | i.insertCoincidentPair(coTs[0][0], coTs[0][1], coTs[1][0], coTs[1][1], |
| 271 | locals.fPt[0], locals.fPt[1]); |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 272 | } |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 273 | t2Start = t2; |
| 274 | } |
| 275 | t1Start = t1; |
| 276 | } |
| 277 | return i.intersected(); |
| 278 | } |
| 279 | |
skia.committer@gmail.com | 044679e | 2013-02-15 07:16:57 +0000 | [diff] [blame] | 280 | // this flavor centers potential intersections recursively. In contrast, '2' may inadvertently |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 281 | // chase intersections near quadratic ends, requiring odd hacks to find them. |
| 282 | static bool intersect3(const Cubic& cubic1, double t1s, double t1e, const Cubic& cubic2, |
| 283 | double t2s, double t2e, double precisionScale, Intersections& i) { |
| 284 | i.upDepth(); |
| 285 | bool result = false; |
| 286 | Cubic c1, c2; |
| 287 | sub_divide(cubic1, t1s, t1e, c1); |
| 288 | sub_divide(cubic2, t2s, t2e, c2); |
| 289 | SkTDArray<double> ts1; |
| 290 | cubic_to_quadratics(c1, calcPrecision(c1) * precisionScale, ts1); |
| 291 | SkTDArray<double> ts2; |
| 292 | cubic_to_quadratics(c2, calcPrecision(c2) * precisionScale, ts2); |
| 293 | double t1Start = t1s; |
| 294 | int ts1Count = ts1.count(); |
| 295 | for (int i1 = 0; i1 <= ts1Count; ++i1) { |
| 296 | const double tEnd1 = i1 < ts1Count ? ts1[i1] : 1; |
| 297 | const double t1 = t1s + (t1e - t1s) * tEnd1; |
| 298 | Quadratic s1; |
| 299 | int o1 = quadPart(cubic1, t1Start, t1, s1); |
| 300 | double t2Start = t2s; |
| 301 | int ts2Count = ts2.count(); |
| 302 | for (int i2 = 0; i2 <= ts2Count; ++i2) { |
| 303 | const double tEnd2 = i2 < ts2Count ? ts2[i2] : 1; |
| 304 | const double t2 = t2s + (t2e - t2s) * tEnd2; |
caryclark@google.com | c83c70e | 2013-02-22 21:50:07 +0000 | [diff] [blame] | 305 | if (cubic1 == cubic2 && t1Start >= t2Start) { |
| 306 | t2Start = t2; |
| 307 | continue; |
| 308 | } |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 309 | Quadratic s2; |
| 310 | int o2 = quadPart(cubic2, t2Start, t2, s2); |
caryclark@google.com | 5e0500f | 2013-02-20 12:51:37 +0000 | [diff] [blame] | 311 | #if ONE_OFF_DEBUG |
| 312 | if (tLimits[0][0] >= t1Start && tLimits[0][1] <= t1 |
| 313 | && tLimits[1][0] >= t2Start && tLimits[1][1] <= t2) { |
| 314 | Cubic cSub1, cSub2; |
| 315 | sub_divide(cubic1, t1Start, tEnd1, cSub1); |
| 316 | sub_divide(cubic2, t2Start, tEnd2, cSub2); |
| 317 | SkDebugf("t1=(%1.9g,%1.9g) t2=(%1.9g,%1.9g)\n", |
| 318 | t1Start, t1, t2Start, t2); |
| 319 | Intersections xlocals; |
| 320 | intersectWithOrder(s1, o1, s2, o2, xlocals); |
| 321 | SkDebugf("xlocals.fUsed=%d\n", xlocals.used()); |
| 322 | } |
| 323 | #endif |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 324 | Intersections locals; |
| 325 | intersectWithOrder(s1, o1, s2, o2, locals); |
| 326 | double coStart[2] = { -1 }; |
| 327 | _Point coPoint; |
| 328 | for (int tIdx = 0; tIdx < locals.used(); ++tIdx) { |
| 329 | double to1 = t1Start + (t1 - t1Start) * locals.fT[0][tIdx]; |
| 330 | double to2 = t2Start + (t2 - t2Start) * locals.fT[1][tIdx]; |
| 331 | // if the computed t is not sufficiently precise, iterate |
| 332 | _Point p1, p2; |
| 333 | xy_at_t(cubic1, to1, p1.x, p1.y); |
| 334 | xy_at_t(cubic2, to2, p2.x, p2.y); |
| 335 | if (p1.approximatelyEqual(p2)) { |
| 336 | if (locals.fIsCoincident[0] & 1 << tIdx) { |
| 337 | if (coStart[0] < 0) { |
| 338 | coStart[0] = to1; |
| 339 | coStart[1] = to2; |
| 340 | coPoint = p1; |
| 341 | } else { |
| 342 | i.insertCoincidentPair(coStart[0], to1, coStart[1], to2, coPoint, p1); |
| 343 | coStart[0] = -1; |
| 344 | } |
caryclark@google.com | c83c70e | 2013-02-22 21:50:07 +0000 | [diff] [blame] | 345 | result = true; |
| 346 | } else if (cubic1 != cubic2 || !approximately_equal(to1, to2)) { |
caryclark@google.com | 7ff5c84 | 2013-02-26 15:56:05 +0000 | [diff] [blame^] | 347 | if (i.swapped()) { // FIXME: insert should respect swap |
| 348 | i.insert(to2, to1, p1); |
| 349 | } else { |
| 350 | i.insert(to1, to2, p1); |
| 351 | } |
caryclark@google.com | c83c70e | 2013-02-22 21:50:07 +0000 | [diff] [blame] | 352 | result = true; |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 353 | } |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 354 | } else { |
| 355 | double offset = precisionScale / 16; // FIME: const is arbitrary -- test & refine |
| 356 | double c1Min = SkTMax(0., to1 - offset); |
| 357 | double c1Max = SkTMin(1., to1 + offset); |
| 358 | double c2Min = SkTMax(0., to2 - offset); |
| 359 | double c2Max = SkTMin(1., to2 + offset); |
| 360 | bool found = intersect3(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i); |
| 361 | if (false && !found) { |
| 362 | // either offset was overagressive or cubics didn't really intersect |
| 363 | // if they didn't intersect, then quad tangents ought to be nearly parallel |
| 364 | offset = precisionScale / 2; // try much less agressive offset |
| 365 | c1Min = SkTMax(0., to1 - offset); |
| 366 | c1Max = SkTMin(1., to1 + offset); |
| 367 | c2Min = SkTMax(0., to2 - offset); |
| 368 | c2Max = SkTMin(1., to2 + offset); |
| 369 | found = intersect3(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i); |
| 370 | if (found) { |
| 371 | SkDebugf("%s *** over-aggressive? offset=%1.9g depth=%d\n", __FUNCTION__, |
| 372 | offset, i.depth()); |
| 373 | } |
| 374 | // try parallel measure |
caryclark@google.com | 7ff5c84 | 2013-02-26 15:56:05 +0000 | [diff] [blame^] | 375 | _Vector d1 = dxdy_at_t(cubic1, to1); |
| 376 | _Vector d2 = dxdy_at_t(cubic2, to2); |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 377 | double shallow = d1.cross(d2); |
| 378 | #if 1 || ONE_OFF_DEBUG // not sure this is worth debugging |
| 379 | if (!approximately_zero(shallow)) { |
| 380 | SkDebugf("%s *** near-miss? shallow=%1.9g depth=%d\n", __FUNCTION__, |
| 381 | offset, i.depth()); |
| 382 | } |
| 383 | #endif |
| 384 | if (i.depth() == 1 && shallow < 0.6) { |
| 385 | SkDebugf("%s !!! near-miss? shallow=%1.9g depth=%d\n", __FUNCTION__, |
| 386 | offset, i.depth()); |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | SkASSERT(coStart[0] == -1); |
| 392 | t2Start = t2; |
| 393 | } |
| 394 | t1Start = t1; |
| 395 | } |
| 396 | i.downDepth(); |
| 397 | return result; |
| 398 | } |
| 399 | |
caryclark@google.com | 7ff5c84 | 2013-02-26 15:56:05 +0000 | [diff] [blame^] | 400 | // intersect the end of the cubic with the other. Try lines from the end to control and opposite |
| 401 | // end to determine range of t on opposite cubic. |
| 402 | static bool intersectEnd(const Cubic& cubic1, bool start, const Cubic& cubic2, const _Rect& bounds2, |
| 403 | Intersections& i) { |
| 404 | _Line line; |
| 405 | int t1Index = start ? 0 : 3; |
| 406 | line[0] = cubic1[t1Index]; |
| 407 | // don't bother if the two cubics are connnected |
| 408 | if (line[0].approximatelyEqual(cubic2[0]) || line[0].approximatelyEqual(cubic2[3])) { |
| 409 | return false; |
| 410 | } |
| 411 | double tMin = 1, tMax = 0; |
| 412 | for (int index = 0; index < 4; ++index) { |
| 413 | if (index == t1Index) { |
| 414 | continue; |
| 415 | } |
| 416 | _Vector dxy1 = cubic1[index] - line[0]; |
| 417 | dxy1 /= gPrecisionUnit; |
| 418 | line[1] = line[0] + dxy1; |
| 419 | _Rect lineBounds; |
| 420 | lineBounds.setBounds(line); |
| 421 | if (!bounds2.intersects(lineBounds)) { |
| 422 | continue; |
| 423 | } |
| 424 | Intersections local; |
| 425 | if (!intersect(cubic2, line, local)) { |
| 426 | continue; |
| 427 | } |
| 428 | for (int index = 0; index < local.fUsed; ++index) { |
| 429 | tMin = SkTMin(tMin, local.fT[0][index]); |
| 430 | tMax = SkTMax(tMax, local.fT[0][index]); |
| 431 | } |
| 432 | } |
| 433 | if (tMin > tMax) { |
| 434 | return false; |
| 435 | } |
| 436 | double tMin1 = start ? 0 : 1 - 1.0 / gPrecisionUnit; |
| 437 | double tMax1 = start ? 1.0 / gPrecisionUnit : 1; |
| 438 | double tMin2 = SkTMax(tMin - 1.0 / gPrecisionUnit, 0.0); |
| 439 | double tMax2 = SkTMin(tMax + 1.0 / gPrecisionUnit, 1.0); |
| 440 | return intersect3(cubic1, tMin1, tMax1, cubic2, tMin2, tMax2, 1, i); |
| 441 | } |
| 442 | |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 443 | // FIXME: add intersection of convex hull on cubics' ends with the opposite cubic. The hull line |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 444 | // segments can be constructed to be only as long as the calculated precision suggests. If the hull |
| 445 | // line segments intersect the cubic, then use the intersections to construct a subdivision for |
| 446 | // quadratic curve fitting. |
| 447 | bool intersect2(const Cubic& c1, const Cubic& c2, Intersections& i) { |
caryclark@google.com | 85ec74c | 2013-01-28 19:25:51 +0000 | [diff] [blame] | 448 | bool result = intersect2(c1, 0, 1, c2, 0, 1, 1, i); |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 449 | // FIXME: pass in cached bounds from caller |
| 450 | _Rect c1Bounds, c2Bounds; |
| 451 | c1Bounds.setBounds(c1); // OPTIMIZE use setRawBounds ? |
| 452 | c2Bounds.setBounds(c2); |
| 453 | result |= intersectEnd(c1, false, c2, c2Bounds, i); |
| 454 | result |= intersectEnd(c1, true, c2, c2Bounds, i); |
caryclark@google.com | 85ec74c | 2013-01-28 19:25:51 +0000 | [diff] [blame] | 455 | i.swap(); |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 456 | result |= intersectEnd(c2, false, c1, c1Bounds, i); |
| 457 | result |= intersectEnd(c2, true, c1, c1Bounds, i); |
caryclark@google.com | 85ec74c | 2013-01-28 19:25:51 +0000 | [diff] [blame] | 458 | i.swap(); |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 459 | return result; |
| 460 | } |
| 461 | |
caryclark@google.com | 47d73da | 2013-02-17 01:41:25 +0000 | [diff] [blame] | 462 | const double CLOSE_ENOUGH = 0.001; |
skia.committer@gmail.com | e7707c2 | 2013-02-17 07:02:20 +0000 | [diff] [blame] | 463 | |
caryclark@google.com | 47d73da | 2013-02-17 01:41:25 +0000 | [diff] [blame] | 464 | static bool closeStart(const Cubic& cubic, int cubicIndex, Intersections& i, _Point& pt) { |
| 465 | if (i.fT[cubicIndex][0] != 0 || i.fT[cubicIndex][1] > CLOSE_ENOUGH) { |
| 466 | return false; |
| 467 | } |
| 468 | pt = xy_at_t(cubic, (i.fT[cubicIndex][0] + i.fT[cubicIndex][1]) / 2); |
| 469 | return true; |
| 470 | } |
| 471 | |
| 472 | static bool closeEnd(const Cubic& cubic, int cubicIndex, Intersections& i, _Point& pt) { |
| 473 | int last = i.used() - 1; |
| 474 | if (i.fT[cubicIndex][last] != 1 || i.fT[cubicIndex][last - 1] < 1 - CLOSE_ENOUGH) { |
| 475 | return false; |
| 476 | } |
| 477 | pt = xy_at_t(cubic, (i.fT[cubicIndex][last] + i.fT[cubicIndex][last - 1]) / 2); |
| 478 | return true; |
| 479 | } |
| 480 | |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 481 | bool intersect3(const Cubic& c1, const Cubic& c2, Intersections& i) { |
| 482 | bool result = intersect3(c1, 0, 1, c2, 0, 1, 1, i); |
| 483 | // FIXME: pass in cached bounds from caller |
| 484 | _Rect c1Bounds, c2Bounds; |
| 485 | c1Bounds.setBounds(c1); // OPTIMIZE use setRawBounds ? |
| 486 | c2Bounds.setBounds(c2); |
| 487 | result |= intersectEnd(c1, false, c2, c2Bounds, i); |
| 488 | result |= intersectEnd(c1, true, c2, c2Bounds, i); |
| 489 | i.swap(); |
| 490 | result |= intersectEnd(c2, false, c1, c1Bounds, i); |
| 491 | result |= intersectEnd(c2, true, c1, c1Bounds, i); |
| 492 | i.swap(); |
caryclark@google.com | 47d73da | 2013-02-17 01:41:25 +0000 | [diff] [blame] | 493 | // If an end point and a second point very close to the end is returned, the second |
| 494 | // point may have been detected because the approximate quads |
| 495 | // intersected at the end and close to it. Verify that the second point is valid. |
| 496 | if (i.used() <= 1 || i.coincidentUsed()) { |
| 497 | return result; |
| 498 | } |
| 499 | _Point pt[2]; |
| 500 | if (closeStart(c1, 0, i, pt[0]) && closeStart(c2, 1, i, pt[1]) |
| 501 | && pt[0].approximatelyEqual(pt[1])) { |
| 502 | i.removeOne(1); |
| 503 | } |
| 504 | if (closeEnd(c1, 0, i, pt[0]) && closeEnd(c2, 1, i, pt[1]) |
| 505 | && pt[0].approximatelyEqual(pt[1])) { |
| 506 | i.removeOne(i.used() - 2); |
| 507 | } |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 508 | return result; |
| 509 | } |
| 510 | |
caryclark@google.com | d0a19eb | 2013-02-19 12:49:33 +0000 | [diff] [blame] | 511 | // Up promote the quad to a cubic. |
| 512 | // OPTIMIZATION If this is a common use case, optimize by duplicating |
| 513 | // the intersect 3 loop to avoid the promotion / demotion code |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 514 | int intersect(const Cubic& cubic, const Quadratic& quad, Intersections& i) { |
caryclark@google.com | d0a19eb | 2013-02-19 12:49:33 +0000 | [diff] [blame] | 515 | Cubic up; |
| 516 | toCubic(quad, up); |
| 517 | (void) intersect3(cubic, up, i); |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 518 | return i.used(); |
| 519 | } |
| 520 | |
caryclark@google.com | c83c70e | 2013-02-22 21:50:07 +0000 | [diff] [blame] | 521 | /* http://www.ag.jku.at/compass/compasssample.pdf |
| 522 | ( Self-Intersection Problems and Approximate Implicitization by Jan B. Thomassen |
| 523 | Centre of Mathematics for Applications, University of Oslo http://www.cma.uio.no janbth@math.uio.no |
| 524 | SINTEF Applied Mathematics http://www.sintef.no ) |
| 525 | describes a method to find the self intersection of a cubic by taking the gradient of the implicit |
| 526 | form dotted with the normal, and solving for the roots. My math foo is too poor to implement this.*/ |
| 527 | |
| 528 | int intersect(const Cubic& c, Intersections& i) { |
| 529 | // check to see if x or y end points are the extrema. Are other quick rejects possible? |
| 530 | if ((between(c[0].x, c[1].x, c[3].x) && between(c[0].x, c[2].x, c[3].x)) |
| 531 | || (between(c[0].y, c[1].y, c[3].y) && between(c[0].y, c[2].y, c[3].y))) { |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 532 | return false; |
| 533 | } |
caryclark@google.com | c83c70e | 2013-02-22 21:50:07 +0000 | [diff] [blame] | 534 | (void) intersect3(c, c, i); |
| 535 | return i.used(); |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 536 | } |