| 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 | |
| 10 | int (SkIntersections::*CurveVertical[])(const SkPoint[], SkScalar, SkScalar, SkScalar, bool) = { |
| 11 | NULL, |
| 12 | &SkIntersections::verticalLine, |
| 13 | &SkIntersections::verticalQuad, |
| 14 | &SkIntersections::verticalCubic |
| 15 | }; |
| 16 | |
| 17 | int (SkIntersections::*CurveRay[])(const SkPoint[], const SkDLine&) = { |
| 18 | NULL, |
| 19 | NULL, |
| 20 | &SkIntersections::quadRay, |
| 21 | &SkIntersections::cubicRay |
| 22 | }; |
| 23 | |
| 24 | int SkIntersections::coincidentUsed() const { |
| 25 | if (!fIsCoincident[0]) { |
| caryclark@google.com | cffbcc3 | 2013-06-04 17:59:42 +0000 | [diff] [blame] | 26 | SkASSERT(!fIsCoincident[1]); |
| caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 27 | return 0; |
| 28 | } |
| 29 | int count = 0; |
| 30 | SkDEBUGCODE(int count2 = 0;) |
| 31 | for (int index = 0; index < fUsed; ++index) { |
| 32 | if (fIsCoincident[0] & (1 << index)) { |
| 33 | ++count; |
| 34 | } |
| 35 | #ifdef SK_DEBUG |
| 36 | if (fIsCoincident[1] & (1 << index)) { |
| 37 | ++count2; |
| 38 | } |
| 39 | #endif |
| 40 | } |
| 41 | SkASSERT(count == count2); |
| 42 | return count; |
| 43 | } |
| 44 | |
| 45 | int SkIntersections::cubicRay(const SkPoint pts[4], const SkDLine& line) { |
| 46 | SkDCubic cubic; |
| 47 | cubic.set(pts); |
| caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 48 | fMax = 3; |
| caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 49 | return intersectRay(cubic, line); |
| 50 | } |
| 51 | |
| 52 | void SkIntersections::flip() { |
| 53 | for (int index = 0; index < fUsed; ++index) { |
| 54 | fT[1][index] = 1 - fT[1][index]; |
| 55 | } |
| 56 | } |
| 57 | |
| caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 58 | int SkIntersections::insert(double one, double two, const SkDPoint& pt) { |
| caryclark@google.com | b3f0921 | 2013-04-17 15:49:16 +0000 | [diff] [blame] | 59 | if (fIsCoincident[0] == 3 && between(fT[0][0], one, fT[0][1])) { |
| 60 | // For now, don't allow a mix of coincident and non-coincident intersections |
| 61 | return -1; |
| 62 | } |
| caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 63 | SkASSERT(fUsed <= 1 || fT[0][0] <= fT[0][1]); |
| 64 | int index; |
| 65 | for (index = 0; index < fUsed; ++index) { |
| 66 | double oldOne = fT[0][index]; |
| 67 | double oldTwo = fT[1][index]; |
| caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 68 | if (one == oldOne && two == oldTwo) { |
| 69 | return -1; |
| 70 | } |
| 71 | if (more_roughly_equal(oldOne, one) && more_roughly_equal(oldTwo, two)) { |
| caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 72 | if ((precisely_zero(one) && !precisely_zero(oldOne)) |
| 73 | || (precisely_equal(one, 1) && !precisely_equal(oldOne, 1)) |
| 74 | || (precisely_zero(two) && !precisely_zero(oldTwo)) |
| 75 | || (precisely_equal(two, 1) && !precisely_equal(oldTwo, 1))) { |
| 76 | fT[0][index] = one; |
| 77 | fT[1][index] = two; |
| caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 78 | fPt[index] = pt; |
| caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 79 | } |
| 80 | return -1; |
| 81 | } |
| 82 | #if ONE_OFF_DEBUG |
| 83 | if (pt.roughlyEqual(fPt[index])) { |
| 84 | SkDebugf("%s t=%1.9g pts roughly equal\n", __FUNCTION__, one); |
| 85 | } |
| 86 | #endif |
| 87 | if (fT[0][index] > one) { |
| 88 | break; |
| 89 | } |
| 90 | } |
| caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 91 | if (fUsed >= fMax) { |
| 92 | SkASSERT(0); // FIXME : this error, if it is to be handled at runtime in release, must |
| 93 | // be propagated all the way back down to the caller, and return failure. |
| 94 | fUsed = 0; |
| 95 | return 0; |
| 96 | } |
| caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 97 | int remaining = fUsed - index; |
| 98 | if (remaining > 0) { |
| 99 | memmove(&fPt[index + 1], &fPt[index], sizeof(fPt[0]) * remaining); |
| 100 | memmove(&fT[0][index + 1], &fT[0][index], sizeof(fT[0][0]) * remaining); |
| 101 | memmove(&fT[1][index + 1], &fT[1][index], sizeof(fT[1][0]) * remaining); |
| caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 102 | int clearMask = ~((1 << index) - 1); |
| 103 | fIsCoincident[0] += fIsCoincident[0] & clearMask; |
| 104 | fIsCoincident[1] += fIsCoincident[1] & clearMask; |
| 105 | fIsNear += fIsNear & clearMask; |
| caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 106 | } |
| caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 107 | fPt[index] = pt; |
| caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 108 | fT[0][index] = one; |
| 109 | fT[1][index] = two; |
| 110 | ++fUsed; |
| 111 | return index; |
| 112 | } |
| 113 | |
| caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 114 | void SkIntersections::insertNear(double one, double two, const SkDPoint& pt) { |
| 115 | int index = insert(one, two, pt); |
| 116 | if (index < 0) { |
| 117 | return; |
| 118 | } |
| 119 | fIsNear |= 1 << index; |
| 120 | } |
| 121 | |
| caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 122 | void SkIntersections::insertCoincident(double one, double two, const SkDPoint& pt) { |
| 123 | int index = insertSwap(one, two, pt); |
| 124 | int bit = 1 << index; |
| 125 | fIsCoincident[0] |= bit; |
| 126 | fIsCoincident[1] |= bit; |
| 127 | } |
| 128 | |
| 129 | void SkIntersections::offset(int base, double start, double end) { |
| 130 | for (int index = base; index < fUsed; ++index) { |
| 131 | double val = fT[fSwap][index]; |
| 132 | val *= end - start; |
| 133 | val += start; |
| 134 | fT[fSwap][index] = val; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | int SkIntersections::quadRay(const SkPoint pts[3], const SkDLine& line) { |
| 139 | SkDQuad quad; |
| 140 | quad.set(pts); |
| caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 141 | fMax = 2; |
| caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 142 | return intersectRay(quad, line); |
| 143 | } |
| 144 | |
| 145 | void SkIntersections::quickRemoveOne(int index, int replace) { |
| 146 | if (index < replace) { |
| 147 | fT[0][index] = fT[0][replace]; |
| 148 | } |
| 149 | } |
| 150 | |
| caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 151 | #if 0 |
| caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 152 | void SkIntersections::remove(double one, double two, const SkDPoint& startPt, |
| 153 | const SkDPoint& endPt) { |
| 154 | for (int index = fUsed - 1; index >= 0; --index) { |
| 155 | if (!(fIsCoincident[0] & (1 << index)) && (between(one, fT[fSwap][index], two) |
| 156 | || startPt.approximatelyEqual(fPt[index]) |
| 157 | || endPt.approximatelyEqual(fPt[index]))) { |
| 158 | SkASSERT(fUsed > 0); |
| 159 | removeOne(index); |
| 160 | } |
| 161 | } |
| 162 | } |
| caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 163 | #endif |
| caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 164 | |
| 165 | void SkIntersections::removeOne(int index) { |
| 166 | int remaining = --fUsed - index; |
| 167 | if (remaining <= 0) { |
| 168 | return; |
| 169 | } |
| 170 | memmove(&fPt[index], &fPt[index + 1], sizeof(fPt[0]) * remaining); |
| 171 | memmove(&fT[0][index], &fT[0][index + 1], sizeof(fT[0][0]) * remaining); |
| 172 | memmove(&fT[1][index], &fT[1][index + 1], sizeof(fT[1][0]) * remaining); |
| 173 | SkASSERT(fIsCoincident[0] == 0); |
| 174 | int coBit = fIsCoincident[0] & (1 << index); |
| 175 | fIsCoincident[0] -= ((fIsCoincident[0] >> 1) & ~((1 << index) - 1)) + coBit; |
| 176 | SkASSERT(!(coBit ^ (fIsCoincident[1] & (1 << index)))); |
| 177 | fIsCoincident[1] -= ((fIsCoincident[1] >> 1) & ~((1 << index) - 1)) + coBit; |
| caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 178 | fIsNear -= ((fIsNear >> 1) & ~((1 << index) - 1)) + (fIsNear & (1 << index)); |
| caryclark@google.com | 07393ca | 2013-04-08 11:47:37 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | void SkIntersections::swapPts() { |
| 182 | int index; |
| 183 | for (index = 0; index < fUsed; ++index) { |
| 184 | SkTSwap(fT[0][index], fT[1][index]); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | int SkIntersections::verticalLine(const SkPoint a[2], SkScalar top, SkScalar bottom, |
| 189 | SkScalar x, bool flipped) { |
| 190 | SkDLine line; |
| 191 | line.set(a); |
| 192 | return vertical(line, top, bottom, x, flipped); |
| 193 | } |
| 194 | |
| 195 | int SkIntersections::verticalQuad(const SkPoint a[3], SkScalar top, SkScalar bottom, |
| 196 | SkScalar x, bool flipped) { |
| 197 | SkDQuad quad; |
| 198 | quad.set(a); |
| 199 | return vertical(quad, top, bottom, x, flipped); |
| 200 | } |
| 201 | |
| 202 | int SkIntersections::verticalCubic(const SkPoint a[4], SkScalar top, SkScalar bottom, |
| 203 | SkScalar x, bool flipped) { |
| 204 | SkDCubic cubic; |
| 205 | cubic.set(a); |
| 206 | return vertical(cubic, top, bottom, x, flipped); |
| 207 | } |