caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +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 | */ |
| 7 | |
| 8 | #include "SkIntersections.h" |
| 9 | #include "SkPathOpsCubic.h" |
| 10 | #include "SkPathOpsLine.h" |
| 11 | #include "SkPathOpsPoint.h" |
| 12 | #include "SkPathOpsQuad.h" |
| 13 | #include "SkPathOpsRect.h" |
| 14 | #include "SkReduceOrder.h" |
commit-bot@chromium.org | b76d3b6 | 2013-04-22 19:55:19 +0000 | [diff] [blame] | 15 | #include "SkTSort.h" |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 16 | |
| 17 | #if ONE_OFF_DEBUG |
| 18 | static const double tLimits1[2][2] = {{0.36, 0.37}, {0.63, 0.64}}; |
| 19 | static const double tLimits2[2][2] = {{-0.865211397, -0.865215212}, {-0.865207696, -0.865208078}}; |
| 20 | #endif |
| 21 | |
| 22 | #define DEBUG_QUAD_PART 0 |
| 23 | #define SWAP_TOP_DEBUG 0 |
| 24 | |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame^] | 25 | static const int kCubicToQuadSubdivisionDepth = 8; // slots reserved for cubic to quads subdivision |
| 26 | |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 27 | static int quadPart(const SkDCubic& cubic, double tStart, double tEnd, SkReduceOrder* reducer) { |
| 28 | SkDCubic part = cubic.subDivide(tStart, tEnd); |
| 29 | SkDQuad quad = part.toQuad(); |
| 30 | // FIXME: should reduceOrder be looser in this use case if quartic is going to blow up on an |
| 31 | // extremely shallow quadratic? |
| 32 | int order = reducer->reduce(quad, SkReduceOrder::kFill_Style); |
| 33 | #if DEBUG_QUAD_PART |
| 34 | SkDebugf("%s cubic=(%1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g)" |
| 35 | " t=(%1.17g,%1.17g)\n", __FUNCTION__, cubic[0].fX, cubic[0].fY, |
| 36 | cubic[1].fX, cubic[1].fY, cubic[2].fX, cubic[2].fY, |
| 37 | cubic[3].fX, cubic[3].fY, tStart, tEnd); |
| 38 | SkDebugf("%s part=(%1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g)" |
| 39 | " quad=(%1.17g,%1.17g %1.17g,%1.17g %1.17g,%1.17g)\n", __FUNCTION__, |
| 40 | part[0].fX, part[0].fY, part[1].fX, part[1].fY, part[2].fX, part[2].fY, |
| 41 | part[3].fX, part[3].fY, quad[0].fX, quad[0].fY, |
| 42 | quad[1].fX, quad[1].fY, quad[2].fX, quad[2].fY); |
| 43 | SkDebugf("%s simple=(%1.17g,%1.17g", __FUNCTION__, reducer->fQuad[0].fX, reducer->fQuad[0].fY); |
| 44 | if (order > 1) { |
| 45 | SkDebugf(" %1.17g,%1.17g", reducer->fQuad[1].fX, reducer->fQuad[1].fY); |
| 46 | } |
| 47 | if (order > 2) { |
| 48 | SkDebugf(" %1.17g,%1.17g", reducer->fQuad[2].fX, reducer->fQuad[2].fY); |
| 49 | } |
| 50 | SkDebugf(")\n"); |
| 51 | SkASSERT(order < 4 && order > 0); |
| 52 | #endif |
| 53 | return order; |
| 54 | } |
| 55 | |
| 56 | static void intersectWithOrder(const SkDQuad& simple1, int order1, const SkDQuad& simple2, |
| 57 | int order2, SkIntersections& i) { |
| 58 | if (order1 == 3 && order2 == 3) { |
| 59 | i.intersect(simple1, simple2); |
| 60 | } else if (order1 <= 2 && order2 <= 2) { |
| 61 | i.intersect((const SkDLine&) simple1, (const SkDLine&) simple2); |
| 62 | } else if (order1 == 3 && order2 <= 2) { |
| 63 | i.intersect(simple1, (const SkDLine&) simple2); |
| 64 | } else { |
| 65 | SkASSERT(order1 <= 2 && order2 == 3); |
| 66 | i.intersect(simple2, (const SkDLine&) simple1); |
| 67 | i.swapPts(); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // this flavor centers potential intersections recursively. In contrast, '2' may inadvertently |
| 72 | // chase intersections near quadratic ends, requiring odd hacks to find them. |
| 73 | static void intersect(const SkDCubic& cubic1, double t1s, double t1e, const SkDCubic& cubic2, |
| 74 | double t2s, double t2e, double precisionScale, SkIntersections& i) { |
| 75 | i.upDepth(); |
| 76 | SkDCubic c1 = cubic1.subDivide(t1s, t1e); |
| 77 | SkDCubic c2 = cubic2.subDivide(t2s, t2e); |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame^] | 78 | SkSTArray<kCubicToQuadSubdivisionDepth, double, true> ts1; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 79 | // OPTIMIZE: if c1 == c2, call once (happens when detecting self-intersection) |
| 80 | c1.toQuadraticTs(c1.calcPrecision() * precisionScale, &ts1); |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame^] | 81 | SkSTArray<kCubicToQuadSubdivisionDepth, double, true> ts2; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 82 | c2.toQuadraticTs(c2.calcPrecision() * precisionScale, &ts2); |
| 83 | double t1Start = t1s; |
| 84 | int ts1Count = ts1.count(); |
| 85 | for (int i1 = 0; i1 <= ts1Count; ++i1) { |
| 86 | const double tEnd1 = i1 < ts1Count ? ts1[i1] : 1; |
| 87 | const double t1 = t1s + (t1e - t1s) * tEnd1; |
| 88 | SkReduceOrder s1; |
| 89 | int o1 = quadPart(cubic1, t1Start, t1, &s1); |
| 90 | double t2Start = t2s; |
| 91 | int ts2Count = ts2.count(); |
| 92 | for (int i2 = 0; i2 <= ts2Count; ++i2) { |
| 93 | const double tEnd2 = i2 < ts2Count ? ts2[i2] : 1; |
| 94 | const double t2 = t2s + (t2e - t2s) * tEnd2; |
| 95 | if (&cubic1 == &cubic2 && t1Start >= t2Start) { |
| 96 | t2Start = t2; |
| 97 | continue; |
| 98 | } |
| 99 | SkReduceOrder s2; |
| 100 | int o2 = quadPart(cubic2, t2Start, t2, &s2); |
| 101 | #if ONE_OFF_DEBUG |
| 102 | char tab[] = " "; |
| 103 | if (tLimits1[0][0] >= t1Start && tLimits1[0][1] <= t1 |
| 104 | && tLimits1[1][0] >= t2Start && tLimits1[1][1] <= t2) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 105 | SkDebugf("%.*s %s t1=(%1.9g,%1.9g) t2=(%1.9g,%1.9g)", i.depth()*2, tab, |
| 106 | __FUNCTION__, t1Start, t1, t2Start, t2); |
| 107 | SkIntersections xlocals; |
| 108 | intersectWithOrder(s1.fQuad, o1, s2.fQuad, o2, xlocals); |
| 109 | SkDebugf(" xlocals.fUsed=%d\n", xlocals.used()); |
| 110 | } |
| 111 | #endif |
| 112 | SkIntersections locals; |
| 113 | intersectWithOrder(s1.fQuad, o1, s2.fQuad, o2, locals); |
| 114 | double coStart[2] = { -1 }; |
| 115 | SkDPoint coPoint; |
| 116 | int tCount = locals.used(); |
| 117 | for (int tIdx = 0; tIdx < tCount; ++tIdx) { |
| 118 | double to1 = t1Start + (t1 - t1Start) * locals[0][tIdx]; |
| 119 | double to2 = t2Start + (t2 - t2Start) * locals[1][tIdx]; |
| 120 | // if the computed t is not sufficiently precise, iterate |
| 121 | SkDPoint p1 = cubic1.xyAtT(to1); |
| 122 | SkDPoint p2 = cubic2.xyAtT(to2); |
| 123 | if (p1.approximatelyEqual(p2)) { |
| 124 | if (locals.isCoincident(tIdx)) { |
| 125 | if (coStart[0] < 0) { |
| 126 | coStart[0] = to1; |
| 127 | coStart[1] = to2; |
| 128 | coPoint = p1; |
| 129 | } else { |
| 130 | i.insertCoincidentPair(coStart[0], to1, coStart[1], to2, coPoint, p1); |
| 131 | coStart[0] = -1; |
| 132 | } |
| 133 | } else if (&cubic1 != &cubic2 || !approximately_equal(to1, to2)) { |
| 134 | if (i.swapped()) { // FIXME: insert should respect swap |
| 135 | i.insert(to2, to1, p1); |
| 136 | } else { |
| 137 | i.insert(to1, to2, p1); |
| 138 | } |
| 139 | } |
| 140 | } else { |
| 141 | double offset = precisionScale / 16; // FIME: const is arbitrary: test, refine |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 142 | double c1Bottom = tIdx == 0 ? 0 : |
| 143 | (t1Start + (t1 - t1Start) * locals[0][tIdx - 1] + to1) / 2; |
caryclark@google.com | 3b97af5 | 2013-04-23 11:56:44 +0000 | [diff] [blame] | 144 | double c1Min = SkTMax(c1Bottom, to1 - offset); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 145 | double c1Top = tIdx == tCount - 1 ? 1 : |
| 146 | (t1Start + (t1 - t1Start) * locals[0][tIdx + 1] + to1) / 2; |
caryclark@google.com | 3b97af5 | 2013-04-23 11:56:44 +0000 | [diff] [blame] | 147 | double c1Max = SkTMin(c1Top, to1 + offset); |
| 148 | double c2Min = SkTMax(0., to2 - offset); |
| 149 | double c2Max = SkTMin(1., to2 + offset); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 150 | #if ONE_OFF_DEBUG |
| 151 | SkDebugf("%.*s %s 1 contains1=%d/%d contains2=%d/%d\n", i.depth()*2, tab, |
| 152 | __FUNCTION__, |
| 153 | c1Min <= tLimits1[0][1] && tLimits1[0][0] <= c1Max |
| 154 | && c2Min <= tLimits1[1][1] && tLimits1[1][0] <= c2Max, |
| 155 | to1 - offset <= tLimits1[0][1] && tLimits1[0][0] <= to1 + offset |
| 156 | && to2 - offset <= tLimits1[1][1] && tLimits1[1][0] <= to2 + offset, |
| 157 | c1Min <= tLimits2[0][1] && tLimits2[0][0] <= c1Max |
| 158 | && c2Min <= tLimits2[1][1] && tLimits2[1][0] <= c2Max, |
| 159 | to1 - offset <= tLimits2[0][1] && tLimits2[0][0] <= to1 + offset |
| 160 | && to2 - offset <= tLimits2[1][1] && tLimits2[1][0] <= to2 + offset); |
| 161 | SkDebugf("%.*s %s 1 c1Bottom=%1.9g c1Top=%1.9g c2Bottom=%1.9g c2Top=%1.9g" |
| 162 | " 1-o=%1.9g 1+o=%1.9g 2-o=%1.9g 2+o=%1.9g offset=%1.9g\n", |
| 163 | i.depth()*2, tab, __FUNCTION__, c1Bottom, c1Top, 0., 1., |
| 164 | to1 - offset, to1 + offset, to2 - offset, to2 + offset, offset); |
| 165 | SkDebugf("%.*s %s 1 to1=%1.9g to2=%1.9g c1Min=%1.9g c1Max=%1.9g c2Min=%1.9g" |
| 166 | " c2Max=%1.9g\n", i.depth()*2, tab, __FUNCTION__, to1, to2, c1Min, |
| 167 | c1Max, c2Min, c2Max); |
| 168 | #endif |
| 169 | intersect(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i); |
| 170 | #if ONE_OFF_DEBUG |
| 171 | SkDebugf("%.*s %s 1 i.used=%d t=%1.9g\n", i.depth()*2, tab, __FUNCTION__, |
| 172 | i.used(), i.used() > 0 ? i[0][i.used() - 1] : -1); |
| 173 | #endif |
| 174 | if (tCount > 1) { |
caryclark@google.com | 3b97af5 | 2013-04-23 11:56:44 +0000 | [diff] [blame] | 175 | c1Min = SkTMax(0., to1 - offset); |
| 176 | c1Max = SkTMin(1., to1 + offset); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 177 | double c2Bottom = tIdx == 0 ? to2 : |
| 178 | (t2Start + (t2 - t2Start) * locals[1][tIdx - 1] + to2) / 2; |
| 179 | double c2Top = tIdx == tCount - 1 ? to2 : |
| 180 | (t2Start + (t2 - t2Start) * locals[1][tIdx + 1] + to2) / 2; |
| 181 | if (c2Bottom > c2Top) { |
| 182 | SkTSwap(c2Bottom, c2Top); |
| 183 | } |
| 184 | if (c2Bottom == to2) { |
| 185 | c2Bottom = 0; |
| 186 | } |
| 187 | if (c2Top == to2) { |
| 188 | c2Top = 1; |
| 189 | } |
caryclark@google.com | 3b97af5 | 2013-04-23 11:56:44 +0000 | [diff] [blame] | 190 | c2Min = SkTMax(c2Bottom, to2 - offset); |
| 191 | c2Max = SkTMin(c2Top, to2 + offset); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 192 | #if ONE_OFF_DEBUG |
| 193 | SkDebugf("%.*s %s 2 contains1=%d/%d contains2=%d/%d\n", i.depth()*2, tab, |
| 194 | __FUNCTION__, |
| 195 | c1Min <= tLimits1[0][1] && tLimits1[0][0] <= c1Max |
| 196 | && c2Min <= tLimits1[1][1] && tLimits1[1][0] <= c2Max, |
| 197 | to1 - offset <= tLimits1[0][1] && tLimits1[0][0] <= to1 + offset |
| 198 | && to2 - offset <= tLimits1[1][1] && tLimits1[1][0] <= to2 + offset, |
| 199 | c1Min <= tLimits2[0][1] && tLimits2[0][0] <= c1Max |
| 200 | && c2Min <= tLimits2[1][1] && tLimits2[1][0] <= c2Max, |
| 201 | to1 - offset <= tLimits2[0][1] && tLimits2[0][0] <= to1 + offset |
| 202 | && to2 - offset <= tLimits2[1][1] && tLimits2[1][0] <= to2 + offset); |
| 203 | SkDebugf("%.*s %s 2 c1Bottom=%1.9g c1Top=%1.9g c2Bottom=%1.9g c2Top=%1.9g" |
| 204 | " 1-o=%1.9g 1+o=%1.9g 2-o=%1.9g 2+o=%1.9g offset=%1.9g\n", |
| 205 | i.depth()*2, tab, __FUNCTION__, 0., 1., c2Bottom, c2Top, |
| 206 | to1 - offset, to1 + offset, to2 - offset, to2 + offset, offset); |
| 207 | SkDebugf("%.*s %s 2 to1=%1.9g to2=%1.9g c1Min=%1.9g c1Max=%1.9g c2Min=%1.9g" |
| 208 | " c2Max=%1.9g\n", i.depth()*2, tab, __FUNCTION__, to1, to2, c1Min, |
| 209 | c1Max, c2Min, c2Max); |
| 210 | #endif |
| 211 | intersect(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i); |
| 212 | #if ONE_OFF_DEBUG |
| 213 | SkDebugf("%.*s %s 2 i.used=%d t=%1.9g\n", i.depth()*2, tab, __FUNCTION__, |
| 214 | i.used(), i.used() > 0 ? i[0][i.used() - 1] : -1); |
| 215 | #endif |
caryclark@google.com | 3b97af5 | 2013-04-23 11:56:44 +0000 | [diff] [blame] | 216 | c1Min = SkTMax(c1Bottom, to1 - offset); |
| 217 | c1Max = SkTMin(c1Top, to1 + offset); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 218 | #if ONE_OFF_DEBUG |
| 219 | SkDebugf("%.*s %s 3 contains1=%d/%d contains2=%d/%d\n", i.depth()*2, tab, |
| 220 | __FUNCTION__, |
| 221 | c1Min <= tLimits1[0][1] && tLimits1[0][0] <= c1Max |
| 222 | && c2Min <= tLimits1[1][1] && tLimits1[1][0] <= c2Max, |
| 223 | to1 - offset <= tLimits1[0][1] && tLimits1[0][0] <= to1 + offset |
| 224 | && to2 - offset <= tLimits1[1][1] && tLimits1[1][0] <= to2 + offset, |
| 225 | c1Min <= tLimits2[0][1] && tLimits2[0][0] <= c1Max |
| 226 | && c2Min <= tLimits2[1][1] && tLimits2[1][0] <= c2Max, |
| 227 | to1 - offset <= tLimits2[0][1] && tLimits2[0][0] <= to1 + offset |
| 228 | && to2 - offset <= tLimits2[1][1] && tLimits2[1][0] <= to2 + offset); |
| 229 | SkDebugf("%.*s %s 3 c1Bottom=%1.9g c1Top=%1.9g c2Bottom=%1.9g c2Top=%1.9g" |
| 230 | " 1-o=%1.9g 1+o=%1.9g 2-o=%1.9g 2+o=%1.9g offset=%1.9g\n", |
| 231 | i.depth()*2, tab, __FUNCTION__, 0., 1., c2Bottom, c2Top, |
| 232 | to1 - offset, to1 + offset, to2 - offset, to2 + offset, offset); |
| 233 | SkDebugf("%.*s %s 3 to1=%1.9g to2=%1.9g c1Min=%1.9g c1Max=%1.9g c2Min=%1.9g" |
| 234 | " c2Max=%1.9g\n", i.depth()*2, tab, __FUNCTION__, to1, to2, c1Min, |
| 235 | c1Max, c2Min, c2Max); |
| 236 | #endif |
| 237 | intersect(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i); |
| 238 | #if ONE_OFF_DEBUG |
| 239 | SkDebugf("%.*s %s 3 i.used=%d t=%1.9g\n", i.depth()*2, tab, __FUNCTION__, |
| 240 | i.used(), i.used() > 0 ? i[0][i.used() - 1] : -1); |
| 241 | #endif |
| 242 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 243 | intersect(cubic1, c1Min, c1Max, cubic2, c2Min, c2Max, offset, i); |
| 244 | // FIXME: if no intersection is found, either quadratics intersected where |
| 245 | // cubics did not, or the intersection was missed. In the former case, expect |
| 246 | // the quadratics to be nearly parallel at the point of intersection, and check |
| 247 | // for that. |
| 248 | } |
| 249 | } |
| 250 | SkASSERT(coStart[0] == -1); |
| 251 | t2Start = t2; |
| 252 | } |
| 253 | t1Start = t1; |
| 254 | } |
| 255 | i.downDepth(); |
| 256 | } |
| 257 | |
| 258 | #define LINE_FRACTION 0.1 |
| 259 | |
| 260 | // intersect the end of the cubic with the other. Try lines from the end to control and opposite |
| 261 | // end to determine range of t on opposite cubic. |
| 262 | static void intersectEnd(const SkDCubic& cubic1, bool start, const SkDCubic& cubic2, |
| 263 | const SkDRect& bounds2, SkIntersections& i) { |
| 264 | SkDLine line; |
| 265 | int t1Index = start ? 0 : 3; |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 266 | // don't bother if the two cubics are connnected |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 267 | #if 1 |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame^] | 268 | static const int kPointsInCubic = 4; // FIXME: move to DCubic, replace '4' with this |
| 269 | static const int kMaxLineCubicIntersections = 3; |
| 270 | SkSTArray<(kMaxLineCubicIntersections - 1) * kMaxLineCubicIntersections, double, true> tVals; |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 271 | line[0] = cubic1[t1Index]; |
| 272 | // this variant looks for intersections with the end point and lines parallel to other points |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame^] | 273 | for (int index = 0; index < kPointsInCubic; ++index) { |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 274 | if (index == t1Index) { |
| 275 | continue; |
| 276 | } |
| 277 | SkDVector dxy1 = cubic1[index] - line[0]; |
| 278 | dxy1 /= SkDCubic::gPrecisionUnit; |
| 279 | line[1] = line[0] + dxy1; |
| 280 | SkDRect lineBounds; |
| 281 | lineBounds.setBounds(line); |
| 282 | if (!bounds2.intersects(&lineBounds)) { |
| 283 | continue; |
| 284 | } |
| 285 | SkIntersections local; |
| 286 | if (!local.intersect(cubic2, line)) { |
| 287 | continue; |
| 288 | } |
| 289 | for (int idx2 = 0; idx2 < local.used(); ++idx2) { |
| 290 | double foundT = local[0][idx2]; |
| 291 | if (approximately_less_than_zero(foundT) |
| 292 | || approximately_greater_than_one(foundT)) { |
| 293 | continue; |
| 294 | } |
| 295 | if (local.pt(idx2).approximatelyEqual(line[0])) { |
| 296 | if (i.swapped()) { // FIXME: insert should respect swap |
| 297 | i.insert(foundT, start ? 0 : 1, line[0]); |
| 298 | } else { |
| 299 | i.insert(start ? 0 : 1, foundT, line[0]); |
| 300 | } |
| 301 | } else { |
caryclark@google.com | d892bd8 | 2013-06-17 14:10:36 +0000 | [diff] [blame^] | 302 | tVals.push_back(foundT); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | } |
| 306 | if (tVals.count() == 0) { |
| 307 | return; |
| 308 | } |
commit-bot@chromium.org | b76d3b6 | 2013-04-22 19:55:19 +0000 | [diff] [blame] | 309 | SkTQSort<double>(tVals.begin(), tVals.end() - 1); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 310 | double tMin1 = start ? 0 : 1 - LINE_FRACTION; |
| 311 | double tMax1 = start ? LINE_FRACTION : 1; |
| 312 | int tIdx = 0; |
| 313 | do { |
| 314 | int tLast = tIdx; |
| 315 | while (tLast + 1 < tVals.count() && roughly_equal(tVals[tLast + 1], tVals[tIdx])) { |
| 316 | ++tLast; |
| 317 | } |
caryclark@google.com | 3b97af5 | 2013-04-23 11:56:44 +0000 | [diff] [blame] | 318 | double tMin2 = SkTMax(tVals[tIdx] - LINE_FRACTION, 0.0); |
| 319 | double tMax2 = SkTMin(tVals[tLast] + LINE_FRACTION, 1.0); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 320 | int lastUsed = i.used(); |
| 321 | intersect(cubic1, tMin1, tMax1, cubic2, tMin2, tMax2, 1, i); |
| 322 | if (lastUsed == i.used()) { |
caryclark@google.com | 3b97af5 | 2013-04-23 11:56:44 +0000 | [diff] [blame] | 323 | tMin2 = SkTMax(tVals[tIdx] - (1.0 / SkDCubic::gPrecisionUnit), 0.0); |
| 324 | tMax2 = SkTMin(tVals[tLast] + (1.0 / SkDCubic::gPrecisionUnit), 1.0); |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 325 | intersect(cubic1, tMin1, tMax1, cubic2, tMin2, tMax2, 1, i); |
| 326 | } |
| 327 | tIdx = tLast + 1; |
| 328 | } while (tIdx < tVals.count()); |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 329 | #else |
| 330 | const SkDPoint& endPt = cubic1[t1Index]; |
| 331 | if (!bounds2.contains(endPt)) { |
| 332 | return; |
| 333 | } |
| 334 | // this variant looks for intersections within an 'x' of the endpoint |
| 335 | double delta = SkTMax(bounds2.width(), bounds2.height()); |
| 336 | for (int index = 0; index < 2; ++index) { |
| 337 | if (index == 0) { |
| 338 | line[0].fY = line[1].fY = endPt.fY; |
| 339 | line[0].fX = endPt.fX - delta; |
| 340 | line[1].fX = endPt.fX + delta; |
| 341 | } else { |
| 342 | line[0].fX = line[1].fX = cubic1[t1Index].fX; |
| 343 | line[0].fY = endPt.fY - delta; |
| 344 | line[1].fY = endPt.fY + delta; |
| 345 | } |
| 346 | SkIntersections local; |
| 347 | local.intersectRay(cubic2, line); // OPTIMIZE: special for horizontal/vertical lines |
| 348 | int used = local.used(); |
| 349 | for (int index = 0; index < used; ++index) { |
| 350 | double foundT = local[0][index]; |
| 351 | if (approximately_less_than_zero(foundT) || approximately_greater_than_one(foundT)) { |
| 352 | continue; |
| 353 | } |
| 354 | if (!local.pt(index).approximatelyEqual(endPt)) { |
| 355 | continue; |
| 356 | } |
| 357 | if (i.swapped()) { // FIXME: insert should respect swap |
| 358 | i.insert(foundT, start ? 0 : 1, endPt); |
| 359 | } else { |
| 360 | i.insert(start ? 0 : 1, foundT, endPt); |
| 361 | } |
| 362 | return; |
| 363 | } |
| 364 | } |
| 365 | // the above doesn't catch when the end of the cubic missed the other cubic because the quad |
| 366 | // approximation moved too far away, so something like the below is still needed. The enabled |
| 367 | // code above tries to avoid this heavy lifting unless the convex hull intersected the cubic. |
| 368 | double tMin1 = start ? 0 : 1 - LINE_FRACTION; |
| 369 | double tMax1 = start ? LINE_FRACTION : 1; |
| 370 | double tMin2 = SkTMax(foundT - LINE_FRACTION, 0.0); |
| 371 | double tMax2 = SkTMin(foundT + LINE_FRACTION, 1.0); |
| 372 | int lastUsed = i.used(); |
| 373 | intersect(cubic1, tMin1, tMax1, cubic2, tMin2, tMax2, 1, i); |
| 374 | if (lastUsed == i.used()) { |
| 375 | tMin2 = SkTMax(foundT - (1.0 / SkDCubic::gPrecisionUnit), 0.0); |
| 376 | tMax2 = SkTMin(foundT + (1.0 / SkDCubic::gPrecisionUnit), 1.0); |
| 377 | intersect(cubic1, tMin1, tMax1, cubic2, tMin2, tMax2, 1, i); |
| 378 | } |
| 379 | #endif |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 380 | return; |
| 381 | } |
| 382 | |
| 383 | const double CLOSE_ENOUGH = 0.001; |
| 384 | |
| 385 | static bool closeStart(const SkDCubic& cubic, int cubicIndex, SkIntersections& i, SkDPoint& pt) { |
| 386 | if (i[cubicIndex][0] != 0 || i[cubicIndex][1] > CLOSE_ENOUGH) { |
| 387 | return false; |
| 388 | } |
| 389 | pt = cubic.xyAtT((i[cubicIndex][0] + i[cubicIndex][1]) / 2); |
| 390 | return true; |
| 391 | } |
| 392 | |
| 393 | static bool closeEnd(const SkDCubic& cubic, int cubicIndex, SkIntersections& i, SkDPoint& pt) { |
| 394 | int last = i.used() - 1; |
| 395 | if (i[cubicIndex][last] != 1 || i[cubicIndex][last - 1] < 1 - CLOSE_ENOUGH) { |
| 396 | return false; |
| 397 | } |
| 398 | pt = cubic.xyAtT((i[cubicIndex][last] + i[cubicIndex][last - 1]) / 2); |
| 399 | return true; |
| 400 | } |
| 401 | |
| 402 | int SkIntersections::intersect(const SkDCubic& c1, const SkDCubic& c2) { |
| 403 | ::intersect(c1, 0, 1, c2, 0, 1, 1, *this); |
| 404 | // FIXME: pass in cached bounds from caller |
| 405 | SkDRect c1Bounds, c2Bounds; |
| 406 | c1Bounds.setBounds(c1); // OPTIMIZE use setRawBounds ? |
| 407 | c2Bounds.setBounds(c2); |
| 408 | intersectEnd(c1, false, c2, c2Bounds, *this); |
| 409 | intersectEnd(c1, true, c2, c2Bounds, *this); |
| 410 | bool selfIntersect = &c1 == &c2; |
| 411 | if (!selfIntersect) { |
| 412 | swap(); |
| 413 | intersectEnd(c2, false, c1, c1Bounds, *this); |
| 414 | intersectEnd(c2, true, c1, c1Bounds, *this); |
| 415 | swap(); |
| 416 | } |
| 417 | // If an end point and a second point very close to the end is returned, the second |
| 418 | // point may have been detected because the approximate quads |
| 419 | // intersected at the end and close to it. Verify that the second point is valid. |
| 420 | if (fUsed <= 1 || coincidentUsed()) { |
| 421 | return fUsed; |
| 422 | } |
| 423 | SkDPoint pt[2]; |
| 424 | if (closeStart(c1, 0, *this, pt[0]) && closeStart(c2, 1, *this, pt[1]) |
| 425 | && pt[0].approximatelyEqual(pt[1])) { |
| 426 | removeOne(1); |
| 427 | } |
| 428 | if (closeEnd(c1, 0, *this, pt[0]) && closeEnd(c2, 1, *this, pt[1]) |
| 429 | && pt[0].approximatelyEqual(pt[1])) { |
| 430 | removeOne(used() - 2); |
| 431 | } |
caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 432 | // vet the pairs of t values to see if the mid value is also on the curve. If so, mark |
| 433 | // the span as coincident |
| 434 | if (fUsed >= 2 && !coincidentUsed()) { |
| 435 | int last = fUsed - 1; |
| 436 | int match = 0; |
| 437 | for (int index = 0; index < last; ++index) { |
| 438 | double mid1 = (fT[0][index] + fT[0][index + 1]) / 2; |
| 439 | double mid2 = (fT[1][index] + fT[1][index + 1]) / 2; |
| 440 | pt[0] = c1.xyAtT(mid1); |
| 441 | pt[1] = c2.xyAtT(mid2); |
| 442 | if (pt[0].approximatelyEqual(pt[1])) { |
| 443 | match |= 1 << index; |
| 444 | } |
| 445 | } |
| 446 | if (match) { |
| 447 | if (((match + 1) & match) != 0) { |
| 448 | SkDebugf("%s coincident hole\n", __FUNCTION__); |
| 449 | } |
| 450 | // for now, assume that everything from start to finish is coincident |
| 451 | if (fUsed > 2) { |
| 452 | fPt[1] = fPt[last]; |
| 453 | fT[0][1] = fT[0][last]; |
| 454 | fT[1][1] = fT[1][last]; |
| 455 | fIsCoincident[0] = 0x03; |
| 456 | fIsCoincident[1] = 0x03; |
| 457 | fUsed = 2; |
| 458 | } |
| 459 | } |
| 460 | } |
caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 461 | return fUsed; |
| 462 | } |
| 463 | |
| 464 | // Up promote the quad to a cubic. |
| 465 | // OPTIMIZATION If this is a common use case, optimize by duplicating |
| 466 | // the intersect 3 loop to avoid the promotion / demotion code |
| 467 | int SkIntersections::intersect(const SkDCubic& cubic, const SkDQuad& quad) { |
| 468 | SkDCubic up = quad.toCubic(); |
| 469 | (void) intersect(cubic, up); |
| 470 | return used(); |
| 471 | } |
| 472 | |
| 473 | /* http://www.ag.jku.at/compass/compasssample.pdf |
| 474 | ( Self-Intersection Problems and Approximate Implicitization by Jan B. Thomassen |
| 475 | Centre of Mathematics for Applications, University of Oslo http://www.cma.uio.no janbth@math.uio.no |
| 476 | SINTEF Applied Mathematics http://www.sintef.no ) |
| 477 | describes a method to find the self intersection of a cubic by taking the gradient of the implicit |
| 478 | form dotted with the normal, and solving for the roots. My math foo is too poor to implement this.*/ |
| 479 | |
| 480 | int SkIntersections::intersect(const SkDCubic& c) { |
| 481 | // check to see if x or y end points are the extrema. Are other quick rejects possible? |
| 482 | if (c.endsAreExtremaInXOrY()) { |
| 483 | return false; |
| 484 | } |
| 485 | (void) intersect(c, c); |
| 486 | if (used() > 0) { |
| 487 | SkASSERT(used() == 1); |
| 488 | if (fT[0][0] > fT[1][0]) { |
| 489 | swapPts(); |
| 490 | } |
| 491 | } |
| 492 | return used(); |
| 493 | } |