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 | 4aaaaea | 2013-02-28 16:12:39 +0000 | [diff] [blame^] | 17 | static const double tLimits[2][2] = {{0.134, 0.145}, {0.134, 0.136}}; |
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 | |
skia.committer@gmail.com | 044679e | 2013-02-15 07:16:57 +0000 | [diff] [blame] | 69 | // this flavor centers potential intersections recursively. In contrast, '2' may inadvertently |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 70 | // chase intersections near quadratic ends, requiring odd hacks to find them. |
| 71 | static bool intersect3(const Cubic& cubic1, double t1s, double t1e, const Cubic& cubic2, |
| 72 | double t2s, double t2e, double precisionScale, Intersections& i) { |
| 73 | i.upDepth(); |
| 74 | bool result = false; |
| 75 | Cubic c1, c2; |
| 76 | sub_divide(cubic1, t1s, t1e, c1); |
| 77 | sub_divide(cubic2, t2s, t2e, c2); |
| 78 | SkTDArray<double> ts1; |
| 79 | cubic_to_quadratics(c1, calcPrecision(c1) * precisionScale, ts1); |
| 80 | SkTDArray<double> ts2; |
| 81 | cubic_to_quadratics(c2, calcPrecision(c2) * precisionScale, ts2); |
| 82 | double t1Start = t1s; |
| 83 | int ts1Count = ts1.count(); |
| 84 | for (int i1 = 0; i1 <= ts1Count; ++i1) { |
| 85 | const double tEnd1 = i1 < ts1Count ? ts1[i1] : 1; |
| 86 | const double t1 = t1s + (t1e - t1s) * tEnd1; |
| 87 | Quadratic s1; |
| 88 | int o1 = quadPart(cubic1, t1Start, t1, s1); |
| 89 | double t2Start = t2s; |
| 90 | int ts2Count = ts2.count(); |
| 91 | for (int i2 = 0; i2 <= ts2Count; ++i2) { |
| 92 | const double tEnd2 = i2 < ts2Count ? ts2[i2] : 1; |
| 93 | const double t2 = t2s + (t2e - t2s) * tEnd2; |
caryclark@google.com | c83c70e | 2013-02-22 21:50:07 +0000 | [diff] [blame] | 94 | if (cubic1 == cubic2 && t1Start >= t2Start) { |
| 95 | t2Start = t2; |
| 96 | continue; |
| 97 | } |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 98 | Quadratic s2; |
| 99 | int o2 = quadPart(cubic2, t2Start, t2, s2); |
caryclark@google.com | 5e0500f | 2013-02-20 12:51:37 +0000 | [diff] [blame] | 100 | #if ONE_OFF_DEBUG |
| 101 | if (tLimits[0][0] >= t1Start && tLimits[0][1] <= t1 |
| 102 | && tLimits[1][0] >= t2Start && tLimits[1][1] <= t2) { |
| 103 | Cubic cSub1, cSub2; |
| 104 | sub_divide(cubic1, t1Start, tEnd1, cSub1); |
| 105 | sub_divide(cubic2, t2Start, tEnd2, cSub2); |
| 106 | SkDebugf("t1=(%1.9g,%1.9g) t2=(%1.9g,%1.9g)\n", |
| 107 | t1Start, t1, t2Start, t2); |
| 108 | Intersections xlocals; |
| 109 | intersectWithOrder(s1, o1, s2, o2, xlocals); |
| 110 | SkDebugf("xlocals.fUsed=%d\n", xlocals.used()); |
| 111 | } |
| 112 | #endif |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 113 | Intersections locals; |
| 114 | intersectWithOrder(s1, o1, s2, o2, locals); |
| 115 | double coStart[2] = { -1 }; |
| 116 | _Point coPoint; |
caryclark@google.com | 4aaaaea | 2013-02-28 16:12:39 +0000 | [diff] [blame^] | 117 | int tCount = locals.used(); |
| 118 | for (int tIdx = 0; tIdx < tCount; ++tIdx) { |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 119 | double to1 = t1Start + (t1 - t1Start) * locals.fT[0][tIdx]; |
| 120 | double to2 = t2Start + (t2 - t2Start) * locals.fT[1][tIdx]; |
| 121 | // if the computed t is not sufficiently precise, iterate |
| 122 | _Point p1, p2; |
| 123 | xy_at_t(cubic1, to1, p1.x, p1.y); |
| 124 | xy_at_t(cubic2, to2, p2.x, p2.y); |
| 125 | if (p1.approximatelyEqual(p2)) { |
| 126 | if (locals.fIsCoincident[0] & 1 << tIdx) { |
| 127 | if (coStart[0] < 0) { |
| 128 | coStart[0] = to1; |
| 129 | coStart[1] = to2; |
| 130 | coPoint = p1; |
| 131 | } else { |
| 132 | i.insertCoincidentPair(coStart[0], to1, coStart[1], to2, coPoint, p1); |
| 133 | coStart[0] = -1; |
| 134 | } |
caryclark@google.com | c83c70e | 2013-02-22 21:50:07 +0000 | [diff] [blame] | 135 | result = true; |
| 136 | } else if (cubic1 != cubic2 || !approximately_equal(to1, to2)) { |
caryclark@google.com | 7ff5c84 | 2013-02-26 15:56:05 +0000 | [diff] [blame] | 137 | if (i.swapped()) { // FIXME: insert should respect swap |
| 138 | i.insert(to2, to1, p1); |
| 139 | } else { |
| 140 | i.insert(to1, to2, p1); |
| 141 | } |
caryclark@google.com | c83c70e | 2013-02-22 21:50:07 +0000 | [diff] [blame] | 142 | result = true; |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 143 | } |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 144 | } else { |
| 145 | double offset = precisionScale / 16; // FIME: const is arbitrary -- test & refine |
caryclark@google.com | 4aaaaea | 2013-02-28 16:12:39 +0000 | [diff] [blame^] | 146 | double c1Bottom = tIdx == 0 ? 0 : |
| 147 | (t1Start + (t1 - t1Start) * locals.fT[0][tIdx - 1] + to1) / 2; |
| 148 | double c1Min = SkTMax(c1Bottom, to1 - offset); |
| 149 | double c1Top = tIdx == tCount - 1 ? 1 : |
| 150 | (t1Start + (t1 - t1Start) * locals.fT[0][tIdx + 1] + to1) / 2; |
| 151 | double c1Max = SkTMin(c1Top, to1 + offset); |
| 152 | double c2Bottom = tIdx == 0 ? to2 : |
| 153 | (t2Start + (t2 - t2Start) * locals.fT[1][tIdx - 1] + to2) / 2; |
| 154 | double c2Top = tIdx == tCount - 1 ? to2 : |
| 155 | (t2Start + (t2 - t2Start) * locals.fT[1][tIdx + 1] + to2) / 2; |
| 156 | if (c2Bottom > c2Top) { |
| 157 | SkTSwap(c2Bottom, c2Top); |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 158 | } |
caryclark@google.com | 4aaaaea | 2013-02-28 16:12:39 +0000 | [diff] [blame^] | 159 | if (c2Bottom == to2) { |
| 160 | c2Bottom = 0; |
| 161 | } |
| 162 | if (c2Top == to2) { |
| 163 | c2Top = 1; |
| 164 | } |
| 165 | double c2Min = SkTMax(c2Bottom, to2 - offset); |
| 166 | double c2Max = SkTMin(c2Top, to2 + offset); |
| 167 | intersect3(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i); |
| 168 | // TODO: if no intersection is found, either quadratics intersected where |
| 169 | // cubics did not, or the intersection was missed. In the former case, expect |
| 170 | // the quadratics to be nearly parallel at the point of intersection, and check |
| 171 | // for that. |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | SkASSERT(coStart[0] == -1); |
| 175 | t2Start = t2; |
| 176 | } |
| 177 | t1Start = t1; |
| 178 | } |
| 179 | i.downDepth(); |
| 180 | return result; |
| 181 | } |
| 182 | |
skia.committer@gmail.com | 12eea2b | 2013-02-27 07:10:10 +0000 | [diff] [blame] | 183 | // intersect the end of the cubic with the other. Try lines from the end to control and opposite |
caryclark@google.com | 7ff5c84 | 2013-02-26 15:56:05 +0000 | [diff] [blame] | 184 | // end to determine range of t on opposite cubic. |
| 185 | static bool intersectEnd(const Cubic& cubic1, bool start, const Cubic& cubic2, const _Rect& bounds2, |
| 186 | Intersections& i) { |
| 187 | _Line line; |
| 188 | int t1Index = start ? 0 : 3; |
| 189 | line[0] = cubic1[t1Index]; |
| 190 | // don't bother if the two cubics are connnected |
| 191 | if (line[0].approximatelyEqual(cubic2[0]) || line[0].approximatelyEqual(cubic2[3])) { |
| 192 | return false; |
| 193 | } |
| 194 | double tMin = 1, tMax = 0; |
| 195 | for (int index = 0; index < 4; ++index) { |
| 196 | if (index == t1Index) { |
| 197 | continue; |
| 198 | } |
| 199 | _Vector dxy1 = cubic1[index] - line[0]; |
| 200 | dxy1 /= gPrecisionUnit; |
| 201 | line[1] = line[0] + dxy1; |
| 202 | _Rect lineBounds; |
| 203 | lineBounds.setBounds(line); |
| 204 | if (!bounds2.intersects(lineBounds)) { |
| 205 | continue; |
| 206 | } |
| 207 | Intersections local; |
| 208 | if (!intersect(cubic2, line, local)) { |
| 209 | continue; |
| 210 | } |
| 211 | for (int index = 0; index < local.fUsed; ++index) { |
| 212 | tMin = SkTMin(tMin, local.fT[0][index]); |
| 213 | tMax = SkTMax(tMax, local.fT[0][index]); |
| 214 | } |
| 215 | } |
| 216 | if (tMin > tMax) { |
| 217 | return false; |
| 218 | } |
| 219 | double tMin1 = start ? 0 : 1 - 1.0 / gPrecisionUnit; |
| 220 | double tMax1 = start ? 1.0 / gPrecisionUnit : 1; |
| 221 | double tMin2 = SkTMax(tMin - 1.0 / gPrecisionUnit, 0.0); |
| 222 | double tMax2 = SkTMin(tMax + 1.0 / gPrecisionUnit, 1.0); |
| 223 | return intersect3(cubic1, tMin1, tMax1, cubic2, tMin2, tMax2, 1, i); |
| 224 | } |
| 225 | |
caryclark@google.com | 47d73da | 2013-02-17 01:41:25 +0000 | [diff] [blame] | 226 | const double CLOSE_ENOUGH = 0.001; |
skia.committer@gmail.com | e7707c2 | 2013-02-17 07:02:20 +0000 | [diff] [blame] | 227 | |
caryclark@google.com | 47d73da | 2013-02-17 01:41:25 +0000 | [diff] [blame] | 228 | static bool closeStart(const Cubic& cubic, int cubicIndex, Intersections& i, _Point& pt) { |
| 229 | if (i.fT[cubicIndex][0] != 0 || i.fT[cubicIndex][1] > CLOSE_ENOUGH) { |
| 230 | return false; |
| 231 | } |
| 232 | pt = xy_at_t(cubic, (i.fT[cubicIndex][0] + i.fT[cubicIndex][1]) / 2); |
| 233 | return true; |
| 234 | } |
| 235 | |
| 236 | static bool closeEnd(const Cubic& cubic, int cubicIndex, Intersections& i, _Point& pt) { |
| 237 | int last = i.used() - 1; |
| 238 | if (i.fT[cubicIndex][last] != 1 || i.fT[cubicIndex][last - 1] < 1 - CLOSE_ENOUGH) { |
| 239 | return false; |
| 240 | } |
| 241 | pt = xy_at_t(cubic, (i.fT[cubicIndex][last] + i.fT[cubicIndex][last - 1]) / 2); |
| 242 | return true; |
| 243 | } |
| 244 | |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 245 | bool intersect3(const Cubic& c1, const Cubic& c2, Intersections& i) { |
| 246 | bool result = intersect3(c1, 0, 1, c2, 0, 1, 1, i); |
| 247 | // FIXME: pass in cached bounds from caller |
| 248 | _Rect c1Bounds, c2Bounds; |
| 249 | c1Bounds.setBounds(c1); // OPTIMIZE use setRawBounds ? |
| 250 | c2Bounds.setBounds(c2); |
| 251 | result |= intersectEnd(c1, false, c2, c2Bounds, i); |
| 252 | result |= intersectEnd(c1, true, c2, c2Bounds, i); |
| 253 | i.swap(); |
| 254 | result |= intersectEnd(c2, false, c1, c1Bounds, i); |
| 255 | result |= intersectEnd(c2, true, c1, c1Bounds, i); |
| 256 | i.swap(); |
caryclark@google.com | 47d73da | 2013-02-17 01:41:25 +0000 | [diff] [blame] | 257 | // If an end point and a second point very close to the end is returned, the second |
| 258 | // point may have been detected because the approximate quads |
| 259 | // intersected at the end and close to it. Verify that the second point is valid. |
| 260 | if (i.used() <= 1 || i.coincidentUsed()) { |
| 261 | return result; |
| 262 | } |
| 263 | _Point pt[2]; |
| 264 | if (closeStart(c1, 0, i, pt[0]) && closeStart(c2, 1, i, pt[1]) |
| 265 | && pt[0].approximatelyEqual(pt[1])) { |
| 266 | i.removeOne(1); |
| 267 | } |
| 268 | if (closeEnd(c1, 0, i, pt[0]) && closeEnd(c2, 1, i, pt[1]) |
| 269 | && pt[0].approximatelyEqual(pt[1])) { |
| 270 | i.removeOne(i.used() - 2); |
| 271 | } |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 272 | return result; |
| 273 | } |
| 274 | |
caryclark@google.com | d0a19eb | 2013-02-19 12:49:33 +0000 | [diff] [blame] | 275 | // Up promote the quad to a cubic. |
| 276 | // OPTIMIZATION If this is a common use case, optimize by duplicating |
| 277 | // the intersect 3 loop to avoid the promotion / demotion code |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 278 | int intersect(const Cubic& cubic, const Quadratic& quad, Intersections& i) { |
caryclark@google.com | d0a19eb | 2013-02-19 12:49:33 +0000 | [diff] [blame] | 279 | Cubic up; |
| 280 | toCubic(quad, up); |
| 281 | (void) intersect3(cubic, up, i); |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 282 | return i.used(); |
| 283 | } |
| 284 | |
caryclark@google.com | c83c70e | 2013-02-22 21:50:07 +0000 | [diff] [blame] | 285 | /* http://www.ag.jku.at/compass/compasssample.pdf |
| 286 | ( Self-Intersection Problems and Approximate Implicitization by Jan B. Thomassen |
| 287 | Centre of Mathematics for Applications, University of Oslo http://www.cma.uio.no janbth@math.uio.no |
| 288 | SINTEF Applied Mathematics http://www.sintef.no ) |
| 289 | describes a method to find the self intersection of a cubic by taking the gradient of the implicit |
| 290 | form dotted with the normal, and solving for the roots. My math foo is too poor to implement this.*/ |
| 291 | |
| 292 | int intersect(const Cubic& c, Intersections& i) { |
| 293 | // check to see if x or y end points are the extrema. Are other quick rejects possible? |
| 294 | if ((between(c[0].x, c[1].x, c[3].x) && between(c[0].x, c[2].x, c[3].x)) |
| 295 | || (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] | 296 | return false; |
| 297 | } |
caryclark@google.com | c83c70e | 2013-02-22 21:50:07 +0000 | [diff] [blame] | 298 | (void) intersect3(c, c, i); |
| 299 | return i.used(); |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 300 | } |