| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkIntersections.h" | 8 #include "SkIntersections.h" |
| 9 | 9 |
| 10 int (SkIntersections::*CurveVertical[])(const SkPoint[], SkScalar, SkScalar, SkS
calar, bool) = { | 10 int (SkIntersections::*CurveVertical[])(const SkPoint[], SkScalar, SkScalar, SkS
calar, bool) = { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 } | 49 } |
| 50 | 50 |
| 51 void SkIntersections::flip() { | 51 void SkIntersections::flip() { |
| 52 for (int index = 0; index < fUsed; ++index) { | 52 for (int index = 0; index < fUsed; ++index) { |
| 53 fT[1][index] = 1 - fT[1][index]; | 53 fT[1][index] = 1 - fT[1][index]; |
| 54 } | 54 } |
| 55 } | 55 } |
| 56 | 56 |
| 57 void SkIntersections::insertCoincidentPair(double s1, double e1, double s2, doub
le e2, | 57 void SkIntersections::insertCoincidentPair(double s1, double e1, double s2, doub
le e2, |
| 58 const SkDPoint& startPt, const SkDPoint& endPt) { | 58 const SkDPoint& startPt, const SkDPoint& endPt) { |
| 59 if (fSwap) { | 59 SkASSERT(s1 < e1); |
| 60 remove(s2, e2, startPt, endPt); | 60 SkASSERT(s2 != e2); |
| 61 } else { | 61 if (coincidentUsed() != fUsed) { // if the curve is partially coincident, tr
eat it as fully so |
| 62 remove(s1, e1, startPt, endPt); | 62 for (int index = fUsed - 1; index >= 0; --index) { |
| 63 if (fIsCoincident[0] & (1 << index)) { |
| 64 continue; |
| 65 } |
| 66 double nonCoinT = fT[0][index]; |
| 67 if (!between(s1, nonCoinT, e1)) { |
| 68 if (s1 > nonCoinT) { |
| 69 s1 = nonCoinT; |
| 70 } else { |
| 71 e1 = nonCoinT; |
| 72 } |
| 73 } |
| 74 nonCoinT = fT[1][index]; |
| 75 if (!between(s2, nonCoinT, e2)) { |
| 76 if ((s2 > nonCoinT) ^ (s2 > e2)) { |
| 77 s2 = nonCoinT; |
| 78 } else { |
| 79 e2 = nonCoinT; |
| 80 } |
| 81 } |
| 82 removeOne(index); |
| 83 } |
| 63 } | 84 } |
| 64 SkASSERT(coincidentUsed() == fUsed); | 85 SkASSERT(coincidentUsed() == fUsed); |
| 65 SkASSERT((coincidentUsed() & 1) != 1); | 86 SkASSERT((coincidentUsed() & 1) != 1); |
| 66 int i1 = 0; | 87 int i1 = 0; |
| 67 int i2 = 0; | 88 int i2 = 0; |
| 68 do { | 89 do { |
| 69 while (i1 < fUsed && !(fIsCoincident[fSwap] & (1 << i1))) { | 90 while (i1 < fUsed && !(fIsCoincident[fSwap] & (1 << i1))) { |
| 70 ++i1; | 91 ++i1; |
| 71 } | 92 } |
| 72 if (i1 == fUsed) { | 93 if (i1 == fUsed) { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 fT[fSwap ^ 1][iEnd2] = e2; | 149 fT[fSwap ^ 1][iEnd2] = e2; |
| 129 } | 150 } |
| 130 return; | 151 return; |
| 131 } | 152 } |
| 132 } while (true); | 153 } while (true); |
| 133 SkASSERT(fUsed < 9); | 154 SkASSERT(fUsed < 9); |
| 134 insertCoincident(s1, s2, startPt); | 155 insertCoincident(s1, s2, startPt); |
| 135 insertCoincident(e1, e2, endPt); | 156 insertCoincident(e1, e2, endPt); |
| 136 } | 157 } |
| 137 | 158 |
| 138 int SkIntersections::insert(double one, double two, const SkDPoint& pt) { | 159 int SkIntersections::insert(double one, double two, double x, double y) { |
| 139 if (fIsCoincident[0] == 3 && between(fT[0][0], one, fT[0][1])) { | 160 if (fIsCoincident[0] == 3 && between(fT[0][0], one, fT[0][1])) { |
| 140 // For now, don't allow a mix of coincident and non-coincident intersect
ions | 161 // For now, don't allow a mix of coincident and non-coincident intersect
ions |
| 141 return -1; | 162 return -1; |
| 142 } | 163 } |
| 143 SkASSERT(fUsed <= 1 || fT[0][0] <= fT[0][1]); | 164 SkASSERT(fUsed <= 1 || fT[0][0] <= fT[0][1]); |
| 144 int index; | 165 int index; |
| 145 for (index = 0; index < fUsed; ++index) { | 166 for (index = 0; index < fUsed; ++index) { |
| 146 double oldOne = fT[0][index]; | 167 double oldOne = fT[0][index]; |
| 147 double oldTwo = fT[1][index]; | 168 double oldTwo = fT[1][index]; |
| 148 if (roughly_equal(oldOne, one) && roughly_equal(oldTwo, two)) { | 169 if (roughly_equal(oldOne, one) && roughly_equal(oldTwo, two)) { |
| 149 if ((precisely_zero(one) && !precisely_zero(oldOne)) | 170 if ((precisely_zero(one) && !precisely_zero(oldOne)) |
| 150 || (precisely_equal(one, 1) && !precisely_equal(oldOne, 1)) | 171 || (precisely_equal(one, 1) && !precisely_equal(oldOne, 1)) |
| 151 || (precisely_zero(two) && !precisely_zero(oldTwo)) | 172 || (precisely_zero(two) && !precisely_zero(oldTwo)) |
| 152 || (precisely_equal(two, 1) && !precisely_equal(oldTwo, 1)))
{ | 173 || (precisely_equal(two, 1) && !precisely_equal(oldTwo, 1)))
{ |
| 153 fT[0][index] = one; | 174 fT[0][index] = one; |
| 154 fT[1][index] = two; | 175 fT[1][index] = two; |
| 155 fPt[index] = pt; | 176 fPt[index].fX = x; |
| 177 fPt[index].fY = y; |
| 156 } | 178 } |
| 157 return -1; | 179 return -1; |
| 158 } | 180 } |
| 159 #if ONE_OFF_DEBUG | 181 #if ONE_OFF_DEBUG |
| 160 if (pt.roughlyEqual(fPt[index])) { | 182 if (pt.roughlyEqual(fPt[index])) { |
| 161 SkDebugf("%s t=%1.9g pts roughly equal\n", __FUNCTION__, one); | 183 SkDebugf("%s t=%1.9g pts roughly equal\n", __FUNCTION__, one); |
| 162 } | 184 } |
| 163 #endif | 185 #endif |
| 164 if (fT[0][index] > one) { | 186 if (fT[0][index] > one) { |
| 165 break; | 187 break; |
| 166 } | 188 } |
| 167 } | 189 } |
| 168 SkASSERT(fUsed < 9); | 190 SkASSERT(fUsed < 9); |
| 169 int remaining = fUsed - index; | 191 int remaining = fUsed - index; |
| 170 if (remaining > 0) { | 192 if (remaining > 0) { |
| 171 memmove(&fPt[index + 1], &fPt[index], sizeof(fPt[0]) * remaining); | 193 memmove(&fPt[index + 1], &fPt[index], sizeof(fPt[0]) * remaining); |
| 172 memmove(&fT[0][index + 1], &fT[0][index], sizeof(fT[0][0]) * remaining); | 194 memmove(&fT[0][index + 1], &fT[0][index], sizeof(fT[0][0]) * remaining); |
| 173 memmove(&fT[1][index + 1], &fT[1][index], sizeof(fT[1][0]) * remaining); | 195 memmove(&fT[1][index + 1], &fT[1][index], sizeof(fT[1][0]) * remaining); |
| 174 fIsCoincident[0] += fIsCoincident[0] & ~((1 << index) - 1); | 196 fIsCoincident[0] += fIsCoincident[0] & ~((1 << index) - 1); |
| 175 fIsCoincident[1] += fIsCoincident[1] & ~((1 << index) - 1); | 197 fIsCoincident[1] += fIsCoincident[1] & ~((1 << index) - 1); |
| 176 } | 198 } |
| 177 fPt[index] = pt; | 199 fPt[index].fX = x; |
| 200 fPt[index].fY = y; |
| 178 fT[0][index] = one; | 201 fT[0][index] = one; |
| 179 fT[1][index] = two; | 202 fT[1][index] = two; |
| 180 ++fUsed; | 203 ++fUsed; |
| 181 return index; | 204 return index; |
| 182 } | 205 } |
| 183 | 206 |
| 207 int SkIntersections::insert(double one, double two, const SkDPoint& pt) { |
| 208 return insert(one, two, pt.fX, pt.fY); |
| 209 } |
| 210 |
| 184 void SkIntersections::insertCoincident(double one, double two, const SkDPoint& p
t) { | 211 void SkIntersections::insertCoincident(double one, double two, const SkDPoint& p
t) { |
| 185 int index = insertSwap(one, two, pt); | 212 int index = insertSwap(one, two, pt); |
| 186 int bit = 1 << index; | 213 int bit = 1 << index; |
| 187 fIsCoincident[0] |= bit; | 214 fIsCoincident[0] |= bit; |
| 188 fIsCoincident[1] |= bit; | 215 fIsCoincident[1] |= bit; |
| 189 } | 216 } |
| 190 | 217 |
| 191 void SkIntersections::offset(int base, double start, double end) { | 218 void SkIntersections::offset(int base, double start, double end) { |
| 192 for (int index = base; index < fUsed; ++index) { | 219 for (int index = base; index < fUsed; ++index) { |
| 193 double val = fT[fSwap][index]; | 220 double val = fT[fSwap][index]; |
| 194 val *= end - start; | 221 val *= end - start; |
| 195 val += start; | 222 val += start; |
| 196 fT[fSwap][index] = val; | 223 fT[fSwap][index] = val; |
| 197 } | 224 } |
| 198 } | 225 } |
| 199 | 226 |
| 200 int SkIntersections::quadRay(const SkPoint pts[3], const SkDLine& line) { | 227 int SkIntersections::quadRay(const SkPoint pts[3], const SkDLine& line) { |
| 201 SkDQuad quad; | 228 SkDQuad quad; |
| 202 quad.set(pts); | 229 quad.set(pts); |
| 203 return intersectRay(quad, line); | 230 return intersectRay(quad, line); |
| 204 } | 231 } |
| 205 | 232 |
| 206 void SkIntersections::quickRemoveOne(int index, int replace) { | 233 void SkIntersections::quickRemoveOne(int index, int replace) { |
| 207 if (index < replace) { | 234 if (index < replace) { |
| 208 fT[0][index] = fT[0][replace]; | 235 fT[0][index] = fT[0][replace]; |
| 209 } | 236 } |
| 210 } | 237 } |
| 211 | 238 |
| 239 #if 0 |
| 212 void SkIntersections::remove(double one, double two, const SkDPoint& startPt, | 240 void SkIntersections::remove(double one, double two, const SkDPoint& startPt, |
| 213 const SkDPoint& endPt) { | 241 const SkDPoint& endPt) { |
| 214 for (int index = fUsed - 1; index >= 0; --index) { | 242 for (int index = fUsed - 1; index >= 0; --index) { |
| 215 if (!(fIsCoincident[0] & (1 << index)) && (between(one, fT[fSwap][index]
, two) | 243 if (!(fIsCoincident[0] & (1 << index)) && (between(one, fT[fSwap][index]
, two) |
| 216 || startPt.approximatelyEqual(fPt[index]) | 244 || startPt.approximatelyEqual(fPt[index]) |
| 217 || endPt.approximatelyEqual(fPt[index]))) { | 245 || endPt.approximatelyEqual(fPt[index]))) { |
| 218 SkASSERT(fUsed > 0); | 246 SkASSERT(fUsed > 0); |
| 219 removeOne(index); | 247 removeOne(index); |
| 220 } | 248 } |
| 221 } | 249 } |
| 222 } | 250 } |
| 251 #endif |
| 223 | 252 |
| 224 void SkIntersections::removeOne(int index) { | 253 void SkIntersections::removeOne(int index) { |
| 225 int remaining = --fUsed - index; | 254 int remaining = --fUsed - index; |
| 226 if (remaining <= 0) { | 255 if (remaining <= 0) { |
| 227 return; | 256 return; |
| 228 } | 257 } |
| 229 memmove(&fPt[index], &fPt[index + 1], sizeof(fPt[0]) * remaining); | 258 memmove(&fPt[index], &fPt[index + 1], sizeof(fPt[0]) * remaining); |
| 230 memmove(&fT[0][index], &fT[0][index + 1], sizeof(fT[0][0]) * remaining); | 259 memmove(&fT[0][index], &fT[0][index + 1], sizeof(fT[0][0]) * remaining); |
| 231 memmove(&fT[1][index], &fT[1][index + 1], sizeof(fT[1][0]) * remaining); | 260 memmove(&fT[1][index], &fT[1][index + 1], sizeof(fT[1][0]) * remaining); |
| 232 SkASSERT(fIsCoincident[0] == 0); | 261 SkASSERT(fIsCoincident[0] == 0); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 256 quad.set(a); | 285 quad.set(a); |
| 257 return vertical(quad, top, bottom, x, flipped); | 286 return vertical(quad, top, bottom, x, flipped); |
| 258 } | 287 } |
| 259 | 288 |
| 260 int SkIntersections::verticalCubic(const SkPoint a[4], SkScalar top, SkScalar bo
ttom, | 289 int SkIntersections::verticalCubic(const SkPoint a[4], SkScalar top, SkScalar bo
ttom, |
| 261 SkScalar x, bool flipped) { | 290 SkScalar x, bool flipped) { |
| 262 SkDCubic cubic; | 291 SkDCubic cubic; |
| 263 cubic.set(a); | 292 cubic.set(a); |
| 264 return vertical(cubic, top, bottom, x, flipped); | 293 return vertical(cubic, top, bottom, x, flipped); |
| 265 } | 294 } |
| OLD | NEW |