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