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 | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 14 | |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 15 | #define DEBUG_COMPUTE_DELTA 1 |
| 16 | #define COMPUTE_DELTA 0 |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 17 | #define DEBUG_QUAD_PART 0 |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 18 | |
caryclark@google.com | 0d3d09e | 2012-12-10 14:50:04 +0000 | [diff] [blame] | 19 | static const double tClipLimit = 0.8; // http://cagd.cs.byu.edu/~tom/papers/bezclip.pdf see Multiple intersections |
| 20 | |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 21 | class CubicIntersections : public Intersections { |
| 22 | public: |
| 23 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 24 | CubicIntersections(const Cubic& c1, const Cubic& c2, Intersections& i) |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 25 | : cubic1(c1) |
| 26 | , cubic2(c2) |
| 27 | , intersections(i) |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 28 | , depth(0) |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 29 | , splits(0) { |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 30 | } |
| 31 | |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 32 | bool intersect() { |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 33 | double minT1, minT2, maxT1, maxT2; |
| 34 | if (!bezier_clip(cubic2, cubic1, minT1, maxT1)) { |
| 35 | return false; |
| 36 | } |
| 37 | if (!bezier_clip(cubic1, cubic2, minT2, maxT2)) { |
| 38 | return false; |
| 39 | } |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 40 | int split; |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 41 | if (maxT1 - minT1 < maxT2 - minT2) { |
| 42 | intersections.swap(); |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 43 | minT2 = 0; |
| 44 | maxT2 = 1; |
| 45 | split = maxT1 - minT1 > tClipLimit; |
| 46 | } else { |
| 47 | minT1 = 0; |
| 48 | maxT1 = 1; |
| 49 | split = (maxT2 - minT2 > tClipLimit) << 1; |
| 50 | } |
| 51 | return chop(minT1, maxT1, minT2, maxT2, split); |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 52 | } |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 53 | |
| 54 | protected: |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 55 | |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 56 | bool intersect(double minT1, double maxT1, double minT2, double maxT2) { |
| 57 | Cubic smaller, larger; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 58 | // FIXME: carry last subdivide and reduceOrder result with cubic |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 59 | sub_divide(cubic1, minT1, maxT1, intersections.swapped() ? larger : smaller); |
| 60 | sub_divide(cubic2, minT2, maxT2, intersections.swapped() ? smaller : larger); |
| 61 | Cubic smallResult; |
| 62 | if (reduceOrder(smaller, smallResult, |
| 63 | kReduceOrder_NoQuadraticsAllowed) <= 2) { |
| 64 | Cubic largeResult; |
| 65 | if (reduceOrder(larger, largeResult, |
| 66 | kReduceOrder_NoQuadraticsAllowed) <= 2) { |
| 67 | const _Line& smallLine = (const _Line&) smallResult; |
| 68 | const _Line& largeLine = (const _Line&) largeResult; |
| 69 | double smallT[2]; |
| 70 | double largeT[2]; |
| 71 | // FIXME: this doesn't detect or deal with coincident lines |
| 72 | if (!::intersect(smallLine, largeLine, smallT, largeT)) { |
| 73 | return false; |
| 74 | } |
| 75 | if (intersections.swapped()) { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 76 | smallT[0] = interp(minT2, maxT2, smallT[0]); |
| 77 | largeT[0] = interp(minT1, maxT1, largeT[0]); |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 78 | } else { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 79 | smallT[0] = interp(minT1, maxT1, smallT[0]); |
| 80 | largeT[0] = interp(minT2, maxT2, largeT[0]); |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 81 | } |
| 82 | intersections.add(smallT[0], largeT[0]); |
| 83 | return true; |
| 84 | } |
| 85 | } |
| 86 | double minT, maxT; |
| 87 | if (!bezier_clip(smaller, larger, minT, maxT)) { |
| 88 | if (minT == maxT) { |
| 89 | if (intersections.swapped()) { |
| 90 | minT1 = (minT1 + maxT1) / 2; |
| 91 | minT2 = interp(minT2, maxT2, minT); |
| 92 | } else { |
| 93 | minT1 = interp(minT1, maxT1, minT); |
| 94 | minT2 = (minT2 + maxT2) / 2; |
| 95 | } |
| 96 | intersections.add(minT1, minT2); |
| 97 | return true; |
| 98 | } |
| 99 | return false; |
| 100 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 101 | |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 102 | int split; |
| 103 | if (intersections.swapped()) { |
| 104 | double newMinT1 = interp(minT1, maxT1, minT); |
| 105 | double newMaxT1 = interp(minT1, maxT1, maxT); |
| 106 | split = (newMaxT1 - newMinT1 > (maxT1 - minT1) * tClipLimit) << 1; |
| 107 | #define VERBOSE 0 |
| 108 | #if VERBOSE |
| 109 | printf("%s d=%d s=%d new1=(%g,%g) old1=(%g,%g) split=%d\n", |
| 110 | __FUNCTION__, depth, splits, newMinT1, newMaxT1, minT1, maxT1, |
| 111 | split); |
| 112 | #endif |
| 113 | minT1 = newMinT1; |
| 114 | maxT1 = newMaxT1; |
| 115 | } else { |
| 116 | double newMinT2 = interp(minT2, maxT2, minT); |
| 117 | double newMaxT2 = interp(minT2, maxT2, maxT); |
| 118 | split = newMaxT2 - newMinT2 > (maxT2 - minT2) * tClipLimit; |
| 119 | #if VERBOSE |
| 120 | printf("%s d=%d s=%d new2=(%g,%g) old2=(%g,%g) split=%d\n", |
| 121 | __FUNCTION__, depth, splits, newMinT2, newMaxT2, minT2, maxT2, |
| 122 | split); |
| 123 | #endif |
| 124 | minT2 = newMinT2; |
| 125 | maxT2 = newMaxT2; |
| 126 | } |
| 127 | return chop(minT1, maxT1, minT2, maxT2, split); |
| 128 | } |
| 129 | |
| 130 | bool chop(double minT1, double maxT1, double minT2, double maxT2, int split) { |
| 131 | ++depth; |
| 132 | intersections.swap(); |
| 133 | if (split) { |
| 134 | ++splits; |
| 135 | if (split & 2) { |
| 136 | double middle1 = (maxT1 + minT1) / 2; |
| 137 | intersect(minT1, middle1, minT2, maxT2); |
| 138 | intersect(middle1, maxT1, minT2, maxT2); |
| 139 | } else { |
| 140 | double middle2 = (maxT2 + minT2) / 2; |
| 141 | intersect(minT1, maxT1, minT2, middle2); |
| 142 | intersect(minT1, maxT1, middle2, maxT2); |
| 143 | } |
| 144 | --splits; |
| 145 | intersections.swap(); |
| 146 | --depth; |
| 147 | return intersections.intersected(); |
| 148 | } |
| 149 | bool result = intersect(minT1, maxT1, minT2, maxT2); |
| 150 | intersections.swap(); |
| 151 | --depth; |
| 152 | return result; |
| 153 | } |
| 154 | |
| 155 | private: |
| 156 | |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 157 | const Cubic& cubic1; |
| 158 | const Cubic& cubic2; |
| 159 | Intersections& intersections; |
| 160 | int depth; |
| 161 | int splits; |
| 162 | }; |
| 163 | |
| 164 | bool intersect(const Cubic& c1, const Cubic& c2, Intersections& i) { |
| 165 | CubicIntersections c(c1, c2, i); |
| 166 | return c.intersect(); |
| 167 | } |
| 168 | |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 169 | #if COMPUTE_DELTA |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 170 | static void cubicTangent(const Cubic& cubic, double t, _Line& tangent, _Point& pt, _Point& dxy) { |
| 171 | xy_at_t(cubic, t, tangent[0].x, tangent[0].y); |
| 172 | pt = tangent[1] = tangent[0]; |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 173 | dxdy_at_t(cubic, t, dxy); |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 174 | if (dxy.approximatelyZero()) { |
| 175 | if (approximately_zero(t)) { |
| 176 | SkASSERT(cubic[0].approximatelyEqual(cubic[1])); |
| 177 | dxy = cubic[2]; |
| 178 | dxy -= cubic[0]; |
| 179 | } else { |
| 180 | SkASSERT(approximately_equal(t, 1)); |
| 181 | SkASSERT(cubic[3].approximatelyEqual(cubic[2])); |
| 182 | dxy = cubic[3]; |
| 183 | dxy -= cubic[1]; |
| 184 | } |
| 185 | SkASSERT(!dxy.approximatelyZero()); |
| 186 | } |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 187 | tangent[0] -= dxy; |
| 188 | tangent[1] += dxy; |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 189 | #if DEBUG_COMPUTE_DELTA |
| 190 | SkDebugf("%s t=%1.9g tangent=(%1.9g,%1.9g %1.9g,%1.9g)" |
| 191 | " pt=(%1.9g %1.9g) dxy=(%1.9g %1.9g)\n", __FUNCTION__, t, |
| 192 | tangent[0].x, tangent[0].y, tangent[1].x, tangent[1].y, pt.x, pt.y, |
| 193 | dxy.x, dxy.y); |
| 194 | #endif |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 195 | } |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 196 | #endif |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 197 | |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 198 | #if COMPUTE_DELTA |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 199 | static double cubicDelta(const _Point& dxy, _Line& tangent, double scale) { |
| 200 | double tangentLen = dxy.length(); |
| 201 | tangent[0] -= tangent[1]; |
| 202 | double intersectLen = tangent[0].length(); |
| 203 | double result = intersectLen / tangentLen + scale; |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 204 | #if DEBUG_COMPUTE_DELTA |
| 205 | SkDebugf("%s tangent=(%1.9g,%1.9g %1.9g,%1.9g) intersectLen=%1.9g tangentLen=%1.9g scale=%1.9g" |
| 206 | " result=%1.9g\n", __FUNCTION__, tangent[0].x, tangent[0].y, tangent[1].x, tangent[1].y, |
| 207 | intersectLen, tangentLen, scale, result); |
| 208 | #endif |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 209 | return result; |
| 210 | } |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 211 | #endif |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 212 | |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 213 | #if COMPUTE_DELTA |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 214 | // FIXME: after testing, make this static |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 215 | static void computeDelta(const Cubic& c1, double t1, double scale1, const Cubic& c2, double t2, |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 216 | double scale2, double& delta1, double& delta2) { |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 217 | #if DEBUG_COMPUTE_DELTA |
| 218 | SkDebugf("%s c1=(%1.9g,%1.9g %1.9g,%1.9g %1.9g,%1.9g %1.9g,%1.9g) t1=%1.9g scale1=%1.9g" |
| 219 | " c2=(%1.9g,%1.9g %1.9g,%1.9g %1.9g,%1.9g %1.9g,%1.9g) t2=%1.9g scale2=%1.9g\n", |
| 220 | __FUNCTION__, |
| 221 | c1[0].x, c1[0].y, c1[1].x, c1[1].y, c1[2].x, c1[2].y, c1[3].x, c1[3].y, t1, scale1, |
| 222 | c2[0].x, c2[0].y, c2[1].x, c2[1].y, c2[2].x, c2[2].y, c2[3].x, c2[3].y, t2, scale2); |
| 223 | #endif |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 224 | _Line tangent1, tangent2, line1, line2; |
| 225 | _Point dxy1, dxy2; |
| 226 | cubicTangent(c1, t1, line1, tangent1[0], dxy1); |
| 227 | cubicTangent(c2, t2, line2, tangent2[0], dxy2); |
| 228 | double range1[2], range2[2]; |
| 229 | int found = intersect(line1, line2, range1, range2); |
| 230 | if (found == 0) { |
| 231 | range1[0] = 0.5; |
| 232 | } else { |
| 233 | SkASSERT(found == 1); |
| 234 | } |
| 235 | xy_at_t(line1, range1[0], tangent1[1].x, tangent1[1].y); |
| 236 | #if SK_DEBUG |
| 237 | if (found == 1) { |
| 238 | xy_at_t(line2, range2[0], tangent2[1].x, tangent2[1].y); |
| 239 | SkASSERT(tangent2[1].approximatelyEqual(tangent1[1])); |
| 240 | } |
| 241 | #endif |
| 242 | tangent2[1] = tangent1[1]; |
| 243 | delta1 = cubicDelta(dxy1, tangent1, scale1 / precisionUnit); |
| 244 | delta2 = cubicDelta(dxy2, tangent2, scale2 / precisionUnit); |
| 245 | } |
| 246 | |
caryclark@google.com | 85ec74c | 2013-01-28 19:25:51 +0000 | [diff] [blame] | 247 | #if SK_DEBUG |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 248 | int debugDepth; |
caryclark@google.com | 85ec74c | 2013-01-28 19:25:51 +0000 | [diff] [blame] | 249 | #endif |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 250 | #endif |
| 251 | |
| 252 | static int quadPart(const Cubic& cubic, double tStart, double tEnd, Quadratic& simple) { |
| 253 | Cubic part; |
| 254 | sub_divide(cubic, tStart, tEnd, part); |
| 255 | Quadratic quad; |
| 256 | demote_cubic_to_quad(part, quad); |
| 257 | // FIXME: should reduceOrder be looser in this use case if quartic is going to blow up on an |
| 258 | // extremely shallow quadratic? |
| 259 | int order = reduceOrder(quad, simple); |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 260 | #if DEBUG_QUAD_PART |
| 261 | 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", |
| 262 | __FUNCTION__, cubic[0].x, cubic[0].y, cubic[1].x, cubic[1].y, cubic[2].x, cubic[2].y, |
| 263 | cubic[3].x, cubic[3].y, tStart, tEnd); |
| 264 | SkDebugf("%s part=(%1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g)" |
| 265 | " quad=(%1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g)\n", __FUNCTION__, part[0].x, part[0].y, |
| 266 | part[1].x, part[1].y, part[2].x, part[2].y, part[3].x, part[3].y, quad[0].x, quad[0].y, |
| 267 | quad[1].x, quad[1].y, quad[2].x, quad[2].y); |
| 268 | SkDebugf("%s simple=(%1.17g,%1.17g", __FUNCTION__, simple[0].x, simple[0].y); |
| 269 | if (order > 1) { |
| 270 | SkDebugf(" %1.17g,%1.17g", simple[1].x, simple[1].y); |
| 271 | } |
| 272 | if (order > 2) { |
| 273 | SkDebugf(" %1.17g,%1.17g", simple[2].x, simple[2].y); |
| 274 | } |
| 275 | SkDebugf(")\n"); |
| 276 | SkASSERT(order < 4 && order > 0); |
| 277 | #endif |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 278 | return order; |
| 279 | } |
| 280 | |
| 281 | static void intersectWithOrder(const Quadratic& simple1, int order1, const Quadratic& simple2, |
| 282 | int order2, Intersections& i) { |
| 283 | if (order1 == 3 && order2 == 3) { |
| 284 | intersect2(simple1, simple2, i); |
| 285 | } else if (order1 <= 2 && order2 <= 2) { |
| 286 | i.fUsed = intersect((const _Line&) simple1, (const _Line&) simple2, i.fT[0], i.fT[1]); |
| 287 | } else if (order1 == 3 && order2 <= 2) { |
| 288 | intersect(simple1, (const _Line&) simple2, i); |
| 289 | } else { |
| 290 | SkASSERT(order1 <= 2 && order2 == 3); |
| 291 | intersect(simple2, (const _Line&) simple1, i); |
| 292 | for (int s = 0; s < i.fUsed; ++s) { |
| 293 | SkTSwap(i.fT[0][s], i.fT[1][s]); |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 298 | static double distanceFromEnd(double t) { |
| 299 | return t > 0.5 ? 1 - t : t; |
| 300 | } |
| 301 | |
| 302 | // OPTIMIZATION: this used to try to guess the value for delta, and that may still be worthwhile |
| 303 | static void bumpForRetry(double t1, double t2, double& s1, double& e1, double& s2, double& e2) { |
| 304 | double dt1 = distanceFromEnd(t1); |
| 305 | double dt2 = distanceFromEnd(t2); |
| 306 | double delta = 1.0 / precisionUnit; |
| 307 | if (dt1 < dt2) { |
| 308 | if (t1 == dt1) { |
| 309 | s1 = SkTMax(s1 - delta, 0.); |
| 310 | } else { |
| 311 | e1 = SkTMin(e1 + delta, 1.); |
| 312 | } |
| 313 | } else { |
| 314 | if (t2 == dt2) { |
| 315 | s2 = SkTMax(s2 - delta, 0.); |
| 316 | } else { |
| 317 | e2 = SkTMin(e2 + delta, 1.); |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | 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] | 323 | const Cubic& cubic2, double t2s, double t2m, double t2e, Intersections& i) { |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 324 | bool result = false; |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 325 | i.upDepth(); |
| 326 | // divide the quadratics at the new t value and try again |
| 327 | double p1s = t1s; |
| 328 | double p1e = t1m; |
| 329 | for (int p1 = 0; p1 < 2; ++p1) { |
| 330 | Quadratic s1a; |
| 331 | int o1a = quadPart(cubic1, p1s, p1e, s1a); |
| 332 | double p2s = t2s; |
| 333 | double p2e = t2m; |
| 334 | for (int p2 = 0; p2 < 2; ++p2) { |
| 335 | Quadratic s2a; |
| 336 | int o2a = quadPart(cubic2, p2s, p2e, s2a); |
| 337 | Intersections locals; |
| 338 | #if 0 && SK_DEBUG |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 339 | if (0.497026154 >= p1s && 0.497026535 <= p1e |
| 340 | && 0.710440575 >= p2s && 0.710440956 <= p2e) { |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 341 | SkDebugf("t1=(%1.9g,%1.9g) o1=%d t2=(%1.9g,%1.9g) o2=%d\n", |
| 342 | p1s, p1e, o1a, p2s, p2e, o2a); |
| 343 | if (o1a == 2) { |
| 344 | SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", |
| 345 | s1a[0].x, s1a[0].y, s1a[1].x, s1a[1].y); |
| 346 | } else { |
| 347 | SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", |
| 348 | s1a[0].x, s1a[0].y, s1a[1].x, s1a[1].y, s1a[2].x, s1a[2].y); |
| 349 | } |
| 350 | if (o2a == 2) { |
| 351 | SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", |
| 352 | s2a[0].x, s2a[0].y, s2a[1].x, s2a[1].y); |
| 353 | } else { |
| 354 | SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", |
| 355 | s2a[0].x, s2a[0].y, s2a[1].x, s2a[1].y, s2a[2].x, s2a[2].y); |
| 356 | } |
| 357 | Intersections xlocals; |
| 358 | intersectWithOrder(s1a, o1a, s2a, o2a, xlocals); |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 359 | SkDebugf("xlocals.fUsed=%d\n", xlocals.used()); |
skia.committer@gmail.com | ee235f9 | 2013-02-08 07:16:45 +0000 | [diff] [blame^] | 360 | } |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 361 | #endif |
| 362 | intersectWithOrder(s1a, o1a, s2a, o2a, locals); |
| 363 | for (int tIdx = 0; tIdx < locals.used(); ++tIdx) { |
| 364 | double to1 = p1s + (p1e - p1s) * locals.fT[0][tIdx]; |
| 365 | double to2 = p2s + (p2e - p2s) * locals.fT[1][tIdx]; |
| 366 | // if the computed t is not sufficiently precise, iterate |
| 367 | _Point p1, p2; |
| 368 | xy_at_t(cubic1, to1, p1.x, p1.y); |
| 369 | xy_at_t(cubic2, to2, p2.x, p2.y); |
| 370 | #if 0 && SK_DEBUG |
| 371 | SkDebugf("to1=%1.9g p1=(%1.9g,%1.9g) to2=%1.9g p2=(%1.9g,%1.9g) d=%1.9g\n", |
| 372 | 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^] | 373 | |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 374 | #endif |
| 375 | if (p1.approximatelyEqual(p2)) { |
| 376 | i.insert(i.swapped() ? to2 : to1, i.swapped() ? to1 : to2); |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 377 | result = true; |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 378 | } else { |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 379 | result = doIntersect(cubic1, p1s, to1, p1e, cubic2, p2s, to2, p2e, i); |
| 380 | // 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^] | 381 | // may mark a range that does not contain the cubic intersection. If no |
| 382 | // intersection is found, look again including the t distance of the |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 383 | // 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^] | 384 | // nearest the actual cubic) |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 385 | if (!result) { |
| 386 | double b1s = p1s; |
| 387 | double b1e = p1e; |
| 388 | double b2s = p2s; |
| 389 | double b2e = p2e; |
| 390 | bumpForRetry(locals.fT[0][tIdx], locals.fT[1][tIdx], b1s, b1e, b2s, b2e); |
| 391 | result = doIntersect(cubic1, b1s, to1, b1e, cubic2, b2s, to2, b2e, i); |
| 392 | } |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | p2s = p2e; |
| 396 | p2e = t2e; |
| 397 | } |
| 398 | p1s = p1e; |
| 399 | p1e = t1e; |
| 400 | } |
| 401 | i.downDepth(); |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 402 | return result; |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 403 | } |
caryclark@google.com | 85ec74c | 2013-01-28 19:25:51 +0000 | [diff] [blame] | 404 | |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 405 | // this flavor approximates the cubics with quads to find the intersecting ts |
| 406 | // OPTIMIZE: if this strategy proves successful, the quad approximations, or the ts used |
| 407 | // to create the approximations, could be stored in the cubic segment |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 408 | // FIXME: this strategy needs to intersect the convex hull on either end with the opposite to |
| 409 | // account for inset quadratics that cause the endpoint intersection to avoid detection |
| 410 | // 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] | 411 | 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] | 412 | double t2s, double t2e, double precisionScale, Intersections& i) { |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 413 | Cubic c1, c2; |
| 414 | sub_divide(cubic1, t1s, t1e, c1); |
| 415 | sub_divide(cubic2, t2s, t2e, c2); |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 416 | SkTDArray<double> ts1; |
caryclark@google.com | 85ec74c | 2013-01-28 19:25:51 +0000 | [diff] [blame] | 417 | cubic_to_quadratics(c1, calcPrecision(c1) * precisionScale, ts1); |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 418 | SkTDArray<double> ts2; |
caryclark@google.com | 85ec74c | 2013-01-28 19:25:51 +0000 | [diff] [blame] | 419 | cubic_to_quadratics(c2, calcPrecision(c2) * precisionScale, ts2); |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 420 | double t1Start = t1s; |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 421 | int ts1Count = ts1.count(); |
| 422 | for (int i1 = 0; i1 <= ts1Count; ++i1) { |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 423 | const double tEnd1 = i1 < ts1Count ? ts1[i1] : 1; |
| 424 | const double t1 = t1s + (t1e - t1s) * tEnd1; |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 425 | Quadratic s1; |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 426 | int o1 = quadPart(cubic1, t1Start, t1, s1); |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 427 | double t2Start = t2s; |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 428 | int ts2Count = ts2.count(); |
| 429 | for (int i2 = 0; i2 <= ts2Count; ++i2) { |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 430 | const double tEnd2 = i2 < ts2Count ? ts2[i2] : 1; |
| 431 | const double t2 = t2s + (t2e - t2s) * tEnd2; |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 432 | Quadratic s2; |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 433 | int o2 = quadPart(cubic2, t2Start, t2, s2); |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 434 | #if 0 && SK_DEBUG |
| 435 | if (0.497026154 >= t1Start && 0.497026535 <= t1 |
| 436 | && 0.710440575 + 0.0004 >= t2Start && 0.710440956 <= t2) { |
| 437 | Cubic cSub1, cSub2; |
| 438 | sub_divide(cubic1, t1Start, tEnd1, cSub1); |
| 439 | sub_divide(cubic2, t2Start, tEnd2, cSub2); |
| 440 | SkDebugf("t1=(%1.9g,%1.9g) t2=(%1.9g,%1.9g)\n", |
| 441 | t1Start, t1, t2Start, t2); |
| 442 | Intersections xlocals; |
| 443 | intersectWithOrder(s1, o1, s2, o2, xlocals); |
| 444 | SkDebugf("xlocals.fUsed=%d\n", xlocals.used()); |
| 445 | } |
| 446 | #endif |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 447 | Intersections locals; |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 448 | intersectWithOrder(s1, o1, s2, o2, locals); |
skia.committer@gmail.com | ee235f9 | 2013-02-08 07:16:45 +0000 | [diff] [blame^] | 449 | |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 450 | for (int tIdx = 0; tIdx < locals.used(); ++tIdx) { |
| 451 | double to1 = t1Start + (t1 - t1Start) * locals.fT[0][tIdx]; |
| 452 | double to2 = t2Start + (t2 - t2Start) * locals.fT[1][tIdx]; |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 453 | // if the computed t is not sufficiently precise, iterate |
| 454 | _Point p1, p2; |
| 455 | xy_at_t(cubic1, to1, p1.x, p1.y); |
| 456 | xy_at_t(cubic2, to2, p2.x, p2.y); |
| 457 | if (p1.approximatelyEqual(p2)) { |
| 458 | i.insert(i.swapped() ? to2 : to1, i.swapped() ? to1 : to2); |
| 459 | } else { |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 460 | #if COMPUTE_DELTA |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 461 | double dt1, dt2; |
| 462 | computeDelta(cubic1, to1, (t1e - t1s), cubic2, to2, (t2e - t2s), dt1, dt2); |
caryclark@google.com | 85ec74c | 2013-01-28 19:25:51 +0000 | [diff] [blame] | 463 | double scale = precisionScale; |
| 464 | if (dt1 > 0.125 || dt2 > 0.125) { |
| 465 | scale /= 2; |
| 466 | SkDebugf("%s scale=%1.9g\n", __FUNCTION__, scale); |
| 467 | } |
| 468 | #if SK_DEBUG |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 469 | ++debugDepth; |
caryclark@google.com | aa35831 | 2013-01-29 20:28:49 +0000 | [diff] [blame] | 470 | SkASSERT(debugDepth < 10); |
caryclark@google.com | 85ec74c | 2013-01-28 19:25:51 +0000 | [diff] [blame] | 471 | #endif |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 472 | i.swap(); |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 473 | intersect2(cubic2, SkTMax(to2 - dt2, 0.), SkTMin(to2 + dt2, 1.), |
caryclark@google.com | 85ec74c | 2013-01-28 19:25:51 +0000 | [diff] [blame] | 474 | cubic1, SkTMax(to1 - dt1, 0.), SkTMin(to1 + dt1, 1.), scale, i); |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 475 | i.swap(); |
caryclark@google.com | 85ec74c | 2013-01-28 19:25:51 +0000 | [diff] [blame] | 476 | #if SK_DEBUG |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 477 | --debugDepth; |
caryclark@google.com | 85ec74c | 2013-01-28 19:25:51 +0000 | [diff] [blame] | 478 | #endif |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 479 | #else |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 480 | #if 0 && SK_DEBUG |
| 481 | if (0.497026154 >= t1Start && 0.497026535 <= t1 |
| 482 | && 0.710440575 >= t2Start && 0.710440956 <= t2) { |
| 483 | SkDebugf("t1=(%1.9g,%1.9g) t2=(%1.9g,%1.9g)\n", |
| 484 | t1Start, t1, t2Start, t2); |
| 485 | } |
| 486 | #endif |
| 487 | bool found = doIntersect(cubic1, t1Start, to1, t1, cubic2, t2Start, to2, t2, i); |
| 488 | if (!found) { |
| 489 | double b1s = t1Start; |
| 490 | double b1e = t1; |
| 491 | double b2s = t2Start; |
| 492 | double b2e = t2; |
| 493 | bumpForRetry(locals.fT[0][tIdx], locals.fT[1][tIdx], b1s, b1e, b2s, b2e); |
| 494 | doIntersect(cubic1, b1s, to1, b1e, cubic2, b2s, to2, b2e, i); |
| 495 | } |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 496 | #endif |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 497 | } |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 498 | } |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 499 | if (locals.coincidentUsed()) { |
| 500 | SkASSERT(locals.coincidentUsed() == 2); |
| 501 | double coTs[2][2]; |
| 502 | for (int tIdx = 0; tIdx < locals.coincidentUsed(); ++tIdx) { |
| 503 | coTs[0][tIdx] = t1Start + (t1 - t1Start) * locals.fCoincidentT[0][tIdx]; |
| 504 | coTs[1][tIdx] = t2Start + (t2 - t2Start) * locals.fCoincidentT[1][tIdx]; |
| 505 | } |
| 506 | i.addCoincident(coTs[0][0], coTs[0][1], coTs[1][0], coTs[1][1]); |
| 507 | } |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 508 | t2Start = t2; |
| 509 | } |
| 510 | t1Start = t1; |
| 511 | } |
| 512 | return i.intersected(); |
| 513 | } |
| 514 | |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 515 | static bool intersectEnd(const Cubic& cubic1, bool start, const Cubic& cubic2, const _Rect& bounds2, |
| 516 | Intersections& i) { |
| 517 | _Line line1; |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 518 | line1[1] = cubic1[start ? 0 : 3]; |
| 519 | if (line1[1].approximatelyEqual(cubic2[0]) || line1[1].approximatelyEqual(cubic2[3])) { |
| 520 | return false; |
| 521 | } |
| 522 | line1[0] = line1[1]; |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 523 | _Point dxy1 = line1[0] - cubic1[start ? 1 : 2]; |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 524 | if (dxy1.approximatelyZero()) { |
| 525 | dxy1 = line1[0] - cubic1[start ? 2 : 1]; |
| 526 | } |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 527 | dxy1 /= precisionUnit; |
| 528 | line1[1] += dxy1; |
| 529 | _Rect line1Bounds; |
| 530 | line1Bounds.setBounds(line1); |
| 531 | if (!bounds2.intersects(line1Bounds)) { |
| 532 | return false; |
| 533 | } |
| 534 | _Line line2; |
| 535 | line2[0] = line2[1] = line1[0]; |
| 536 | _Point dxy2 = line2[0] - cubic1[start ? 3 : 0]; |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 537 | SkASSERT(!dxy2.approximatelyZero()); |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 538 | dxy2 /= precisionUnit; |
| 539 | line2[1] += dxy2; |
| 540 | #if 0 // this is so close to the first bounds test it isn't worth the short circuit test |
| 541 | _Rect line2Bounds; |
| 542 | line2Bounds.setBounds(line2); |
| 543 | if (!bounds2.intersects(line2Bounds)) { |
| 544 | return false; |
| 545 | } |
| 546 | #endif |
| 547 | Intersections local1; |
| 548 | if (!intersect(cubic2, line1, local1)) { |
| 549 | return false; |
| 550 | } |
| 551 | Intersections local2; |
| 552 | if (!intersect(cubic2, line2, local2)) { |
| 553 | return false; |
| 554 | } |
| 555 | double tMin, tMax; |
| 556 | tMin = tMax = local1.fT[0][0]; |
| 557 | for (int index = 1; index < local1.fUsed; ++index) { |
caryclark@google.com | aa35831 | 2013-01-29 20:28:49 +0000 | [diff] [blame] | 558 | tMin = SkTMin(tMin, local1.fT[0][index]); |
| 559 | tMax = SkTMax(tMax, local1.fT[0][index]); |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 560 | } |
| 561 | for (int index = 1; index < local2.fUsed; ++index) { |
caryclark@google.com | aa35831 | 2013-01-29 20:28:49 +0000 | [diff] [blame] | 562 | tMin = SkTMin(tMin, local2.fT[0][index]); |
| 563 | tMax = SkTMax(tMax, local2.fT[0][index]); |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 564 | } |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 565 | #if SK_DEBUG && COMPUTE_DELTA |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 566 | debugDepth = 0; |
caryclark@google.com | 85ec74c | 2013-01-28 19:25:51 +0000 | [diff] [blame] | 567 | #endif |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 568 | return intersect2(cubic1, start ? 0 : 1, start ? 1.0 / precisionUnit : 1 - 1.0 / precisionUnit, |
caryclark@google.com | 85ec74c | 2013-01-28 19:25:51 +0000 | [diff] [blame] | 569 | cubic2, tMin, tMax, 1, i); |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 570 | } |
| 571 | |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 572 | // 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] | 573 | // segments can be constructed to be only as long as the calculated precision suggests. If the hull |
| 574 | // line segments intersect the cubic, then use the intersections to construct a subdivision for |
| 575 | // quadratic curve fitting. |
| 576 | bool intersect2(const Cubic& c1, const Cubic& c2, Intersections& i) { |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 577 | #if SK_DEBUG && COMPUTE_DELTA |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 578 | debugDepth = 0; |
caryclark@google.com | 85ec74c | 2013-01-28 19:25:51 +0000 | [diff] [blame] | 579 | #endif |
| 580 | bool result = intersect2(c1, 0, 1, c2, 0, 1, 1, i); |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 581 | // FIXME: pass in cached bounds from caller |
| 582 | _Rect c1Bounds, c2Bounds; |
| 583 | c1Bounds.setBounds(c1); // OPTIMIZE use setRawBounds ? |
| 584 | c2Bounds.setBounds(c2); |
| 585 | result |= intersectEnd(c1, false, c2, c2Bounds, i); |
| 586 | result |= intersectEnd(c1, true, c2, c2Bounds, i); |
caryclark@google.com | 85ec74c | 2013-01-28 19:25:51 +0000 | [diff] [blame] | 587 | i.swap(); |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 588 | result |= intersectEnd(c2, false, c1, c1Bounds, i); |
| 589 | result |= intersectEnd(c2, true, c1, c1Bounds, i); |
caryclark@google.com | 85ec74c | 2013-01-28 19:25:51 +0000 | [diff] [blame] | 590 | i.swap(); |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 591 | return result; |
| 592 | } |
| 593 | |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 594 | int intersect(const Cubic& cubic, const Quadratic& quad, Intersections& i) { |
| 595 | SkTDArray<double> ts; |
| 596 | double precision = calcPrecision(cubic); |
| 597 | cubic_to_quadratics(cubic, precision, ts); |
| 598 | double tStart = 0; |
| 599 | Cubic part; |
| 600 | int tsCount = ts.count(); |
| 601 | for (int idx = 0; idx <= tsCount; ++idx) { |
| 602 | double t = idx < tsCount ? ts[idx] : 1; |
| 603 | Quadratic q1; |
| 604 | sub_divide(cubic, tStart, t, part); |
| 605 | demote_cubic_to_quad(part, q1); |
| 606 | Intersections locals; |
| 607 | intersect2(q1, quad, locals); |
| 608 | for (int tIdx = 0; tIdx < locals.used(); ++tIdx) { |
| 609 | double globalT = tStart + (t - tStart) * locals.fT[0][tIdx]; |
| 610 | i.insertOne(globalT, 0); |
| 611 | globalT = locals.fT[1][tIdx]; |
| 612 | i.insertOne(globalT, 1); |
| 613 | } |
| 614 | tStart = t; |
| 615 | } |
| 616 | return i.used(); |
| 617 | } |
| 618 | |
| 619 | bool intersect(const Cubic& cubic, Intersections& i) { |
| 620 | SkTDArray<double> ts; |
| 621 | double precision = calcPrecision(cubic); |
| 622 | cubic_to_quadratics(cubic, precision, ts); |
| 623 | int tsCount = ts.count(); |
| 624 | if (tsCount == 1) { |
| 625 | return false; |
| 626 | } |
| 627 | double t1Start = 0; |
| 628 | Cubic part; |
| 629 | for (int idx = 0; idx < tsCount; ++idx) { |
| 630 | double t1 = ts[idx]; |
| 631 | Quadratic q1; |
| 632 | sub_divide(cubic, t1Start, t1, part); |
| 633 | demote_cubic_to_quad(part, q1); |
| 634 | double t2Start = t1; |
| 635 | for (int i2 = idx + 1; i2 <= tsCount; ++i2) { |
| 636 | const double t2 = i2 < tsCount ? ts[i2] : 1; |
| 637 | Quadratic q2; |
| 638 | sub_divide(cubic, t2Start, t2, part); |
| 639 | demote_cubic_to_quad(part, q2); |
| 640 | Intersections locals; |
| 641 | intersect2(q1, q2, locals); |
| 642 | for (int tIdx = 0; tIdx < locals.used(); ++tIdx) { |
| 643 | // discard intersections at cusp? (maximum curvature) |
| 644 | double t1sect = locals.fT[0][tIdx]; |
| 645 | double t2sect = locals.fT[1][tIdx]; |
| 646 | if (idx + 1 == i2 && t1sect == 1 && t2sect == 0) { |
| 647 | continue; |
| 648 | } |
| 649 | double to1 = t1Start + (t1 - t1Start) * t1sect; |
| 650 | double to2 = t2Start + (t2 - t2Start) * t2sect; |
| 651 | i.insert(to1, to2); |
| 652 | } |
| 653 | t2Start = t2; |
| 654 | } |
| 655 | t1Start = t1; |
| 656 | } |
| 657 | return i.intersected(); |
| 658 | } |