blob: af6cc080efe741c522760882ca6b1aa001306b94 [file] [log] [blame]
caryclark@google.com07393ca2013-04-08 11:47:37 +00001/*
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
10int (SkIntersections::*CurveVertical[])(const SkPoint[], SkScalar, SkScalar, SkScalar, bool) = {
11 NULL,
12 &SkIntersections::verticalLine,
13 &SkIntersections::verticalQuad,
14 &SkIntersections::verticalCubic
15};
16
17int (SkIntersections::*CurveRay[])(const SkPoint[], const SkDLine&) = {
18 NULL,
19 NULL,
20 &SkIntersections::quadRay,
21 &SkIntersections::cubicRay
22};
23
24int SkIntersections::coincidentUsed() const {
25 if (!fIsCoincident[0]) {
caryclark@google.comcffbcc32013-06-04 17:59:42 +000026 SkASSERT(!fIsCoincident[1]);
caryclark@google.com07393ca2013-04-08 11:47:37 +000027 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
45int SkIntersections::cubicRay(const SkPoint pts[4], const SkDLine& line) {
46 SkDCubic cubic;
47 cubic.set(pts);
48 return intersectRay(cubic, line);
49}
50
51void SkIntersections::flip() {
52 for (int index = 0; index < fUsed; ++index) {
53 fT[1][index] = 1 - fT[1][index];
54 }
55}
56
57void SkIntersections::insertCoincidentPair(double s1, double e1, double s2, double e2,
58 const SkDPoint& startPt, const SkDPoint& endPt) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +000059 SkASSERT(s1 < e1);
60 SkASSERT(s2 != e2);
61 if (coincidentUsed() != fUsed) { // if the curve is partially coincident, treat it as fully so
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 }
caryclark@google.com07393ca2013-04-08 11:47:37 +000084 }
85 SkASSERT(coincidentUsed() == fUsed);
86 SkASSERT((coincidentUsed() & 1) != 1);
87 int i1 = 0;
88 int i2 = 0;
89 do {
90 while (i1 < fUsed && !(fIsCoincident[fSwap] & (1 << i1))) {
91 ++i1;
92 }
93 if (i1 == fUsed) {
94 break;
95 }
96 SkASSERT(i1 < fUsed);
97 int iEnd1 = i1 + 1;
98 while (!(fIsCoincident[fSwap] & (1 << iEnd1))) {
99 ++iEnd1;
100 }
101 SkASSERT(iEnd1 < fUsed);
102 double cs1 = fT[fSwap][i1];
103 double ce1 = fT[fSwap][iEnd1];
104 bool s1in = between(cs1, s1, ce1) || startPt.approximatelyEqual(fPt[i1])
105 || startPt.approximatelyEqual(fPt[iEnd1]);
106 bool e1in = between(cs1, e1, ce1) || endPt.approximatelyEqual(fPt[i1])
107 || endPt.approximatelyEqual(fPt[iEnd1]);
108 while (i2 < fUsed && !(fIsCoincident[fSwap ^ 1] & (1 << i2))) {
109 ++i2;
110 }
111 int iEnd2 = i2 + 1;
112 while (!(fIsCoincident[fSwap ^ 1] & (1 << iEnd2))) {
113 ++iEnd2;
114 }
115 SkASSERT(iEnd2 < fUsed);
116 double cs2 = fT[fSwap ^ 1][i2];
117 double ce2 = fT[fSwap ^ 1][iEnd2];
118 bool s2in = between(cs2, s2, ce2) || startPt.approximatelyEqual(fPt[i2])
119 || startPt.approximatelyEqual(fPt[iEnd2]);
120 bool e2in = between(cs2, e2, ce2) || endPt.approximatelyEqual(fPt[i2])
121 || endPt.approximatelyEqual(fPt[iEnd2]);
122 if ((s1in | e1in) & (s2in | e2in)) {
123 if (s1 < cs1) {
124 fT[fSwap][i1] = s1;
125 fPt[i1] = startPt;
126 } else if (e1 < cs1) {
127 fT[fSwap][i1] = e1;
128 fPt[i1] = endPt;
129 }
130 if (s1 > ce1) {
131 fT[fSwap][iEnd1] = s1;
132 fPt[iEnd1] = startPt;
133 } else if (e1 > ce1) {
134 fT[fSwap][iEnd1] = e1;
135 fPt[iEnd1] = endPt;
136 }
137 if (s2 > e2) {
138 SkTSwap(cs2, ce2);
139 SkTSwap(i2, iEnd2);
140 }
141 if (s2 < cs2) {
142 fT[fSwap ^ 1][i2] = s2;
143 } else if (e2 < cs2) {
144 fT[fSwap ^ 1][i2] = e2;
145 }
146 if (s2 > ce2) {
147 fT[fSwap ^ 1][iEnd2] = s2;
148 } else if (e2 > ce2) {
149 fT[fSwap ^ 1][iEnd2] = e2;
150 }
151 return;
152 }
153 } while (true);
154 SkASSERT(fUsed < 9);
155 insertCoincident(s1, s2, startPt);
156 insertCoincident(e1, e2, endPt);
157}
158
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000159int SkIntersections::insert(double one, double two, double x, double y) {
caryclark@google.comb3f09212013-04-17 15:49:16 +0000160 if (fIsCoincident[0] == 3 && between(fT[0][0], one, fT[0][1])) {
161 // For now, don't allow a mix of coincident and non-coincident intersections
162 return -1;
163 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000164 SkASSERT(fUsed <= 1 || fT[0][0] <= fT[0][1]);
165 int index;
166 for (index = 0; index < fUsed; ++index) {
167 double oldOne = fT[0][index];
168 double oldTwo = fT[1][index];
169 if (roughly_equal(oldOne, one) && roughly_equal(oldTwo, two)) {
170 if ((precisely_zero(one) && !precisely_zero(oldOne))
171 || (precisely_equal(one, 1) && !precisely_equal(oldOne, 1))
172 || (precisely_zero(two) && !precisely_zero(oldTwo))
173 || (precisely_equal(two, 1) && !precisely_equal(oldTwo, 1))) {
174 fT[0][index] = one;
175 fT[1][index] = two;
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000176 fPt[index].fX = x;
177 fPt[index].fY = y;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000178 }
179 return -1;
180 }
181 #if ONE_OFF_DEBUG
182 if (pt.roughlyEqual(fPt[index])) {
183 SkDebugf("%s t=%1.9g pts roughly equal\n", __FUNCTION__, one);
184 }
185 #endif
186 if (fT[0][index] > one) {
187 break;
188 }
189 }
190 SkASSERT(fUsed < 9);
191 int remaining = fUsed - index;
192 if (remaining > 0) {
193 memmove(&fPt[index + 1], &fPt[index], sizeof(fPt[0]) * remaining);
194 memmove(&fT[0][index + 1], &fT[0][index], sizeof(fT[0][0]) * remaining);
195 memmove(&fT[1][index + 1], &fT[1][index], sizeof(fT[1][0]) * remaining);
196 fIsCoincident[0] += fIsCoincident[0] & ~((1 << index) - 1);
197 fIsCoincident[1] += fIsCoincident[1] & ~((1 << index) - 1);
198 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000199 fPt[index].fX = x;
200 fPt[index].fY = y;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000201 fT[0][index] = one;
202 fT[1][index] = two;
203 ++fUsed;
204 return index;
205}
206
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000207int SkIntersections::insert(double one, double two, const SkDPoint& pt) {
208 return insert(one, two, pt.fX, pt.fY);
209}
210
caryclark@google.com07393ca2013-04-08 11:47:37 +0000211void SkIntersections::insertCoincident(double one, double two, const SkDPoint& pt) {
212 int index = insertSwap(one, two, pt);
213 int bit = 1 << index;
214 fIsCoincident[0] |= bit;
215 fIsCoincident[1] |= bit;
216}
217
218void SkIntersections::offset(int base, double start, double end) {
219 for (int index = base; index < fUsed; ++index) {
220 double val = fT[fSwap][index];
221 val *= end - start;
222 val += start;
223 fT[fSwap][index] = val;
224 }
225}
226
227int SkIntersections::quadRay(const SkPoint pts[3], const SkDLine& line) {
228 SkDQuad quad;
229 quad.set(pts);
230 return intersectRay(quad, line);
231}
232
233void SkIntersections::quickRemoveOne(int index, int replace) {
234 if (index < replace) {
235 fT[0][index] = fT[0][replace];
236 }
237}
238
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000239#if 0
caryclark@google.com07393ca2013-04-08 11:47:37 +0000240void SkIntersections::remove(double one, double two, const SkDPoint& startPt,
241 const SkDPoint& endPt) {
242 for (int index = fUsed - 1; index >= 0; --index) {
243 if (!(fIsCoincident[0] & (1 << index)) && (between(one, fT[fSwap][index], two)
244 || startPt.approximatelyEqual(fPt[index])
245 || endPt.approximatelyEqual(fPt[index]))) {
246 SkASSERT(fUsed > 0);
247 removeOne(index);
248 }
249 }
250}
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000251#endif
caryclark@google.com07393ca2013-04-08 11:47:37 +0000252
253void SkIntersections::removeOne(int index) {
254 int remaining = --fUsed - index;
255 if (remaining <= 0) {
256 return;
257 }
258 memmove(&fPt[index], &fPt[index + 1], sizeof(fPt[0]) * remaining);
259 memmove(&fT[0][index], &fT[0][index + 1], sizeof(fT[0][0]) * remaining);
260 memmove(&fT[1][index], &fT[1][index + 1], sizeof(fT[1][0]) * remaining);
261 SkASSERT(fIsCoincident[0] == 0);
262 int coBit = fIsCoincident[0] & (1 << index);
263 fIsCoincident[0] -= ((fIsCoincident[0] >> 1) & ~((1 << index) - 1)) + coBit;
264 SkASSERT(!(coBit ^ (fIsCoincident[1] & (1 << index))));
265 fIsCoincident[1] -= ((fIsCoincident[1] >> 1) & ~((1 << index) - 1)) + coBit;
266}
267
268void SkIntersections::swapPts() {
269 int index;
270 for (index = 0; index < fUsed; ++index) {
271 SkTSwap(fT[0][index], fT[1][index]);
272 }
273}
274
275int SkIntersections::verticalLine(const SkPoint a[2], SkScalar top, SkScalar bottom,
276 SkScalar x, bool flipped) {
277 SkDLine line;
278 line.set(a);
279 return vertical(line, top, bottom, x, flipped);
280}
281
282int SkIntersections::verticalQuad(const SkPoint a[3], SkScalar top, SkScalar bottom,
283 SkScalar x, bool flipped) {
284 SkDQuad quad;
285 quad.set(a);
286 return vertical(quad, top, bottom, x, flipped);
287}
288
289int SkIntersections::verticalCubic(const SkPoint a[4], SkScalar top, SkScalar bottom,
290 SkScalar x, bool flipped) {
291 SkDCubic cubic;
292 cubic.set(a);
293 return vertical(cubic, top, bottom, x, flipped);
294}