blob: fe23316683ba71f67cdf89ec6bb0da03621bdf03 [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.comfa2aeee2013-07-15 13:29:13 +0000159int SkIntersections::insert(double one, double two, const SkDPoint& pt) {
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];
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000169 if (one == oldOne && two == oldTwo) {
170 return -1;
171 }
172 if (more_roughly_equal(oldOne, one) && more_roughly_equal(oldTwo, two)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000173 if ((precisely_zero(one) && !precisely_zero(oldOne))
174 || (precisely_equal(one, 1) && !precisely_equal(oldOne, 1))
175 || (precisely_zero(two) && !precisely_zero(oldTwo))
176 || (precisely_equal(two, 1) && !precisely_equal(oldTwo, 1))) {
177 fT[0][index] = one;
178 fT[1][index] = two;
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000179 fPt[index] = pt;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000180 }
181 return -1;
182 }
183 #if ONE_OFF_DEBUG
184 if (pt.roughlyEqual(fPt[index])) {
185 SkDebugf("%s t=%1.9g pts roughly equal\n", __FUNCTION__, one);
186 }
187 #endif
188 if (fT[0][index] > one) {
189 break;
190 }
191 }
192 SkASSERT(fUsed < 9);
193 int remaining = fUsed - index;
194 if (remaining > 0) {
195 memmove(&fPt[index + 1], &fPt[index], sizeof(fPt[0]) * remaining);
196 memmove(&fT[0][index + 1], &fT[0][index], sizeof(fT[0][0]) * remaining);
197 memmove(&fT[1][index + 1], &fT[1][index], sizeof(fT[1][0]) * remaining);
198 fIsCoincident[0] += fIsCoincident[0] & ~((1 << index) - 1);
199 fIsCoincident[1] += fIsCoincident[1] & ~((1 << index) - 1);
200 }
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000201 fPt[index] = pt;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000202 fT[0][index] = one;
203 fT[1][index] = two;
204 ++fUsed;
205 return index;
206}
207
208void SkIntersections::insertCoincident(double one, double two, const SkDPoint& pt) {
209 int index = insertSwap(one, two, pt);
210 int bit = 1 << index;
211 fIsCoincident[0] |= bit;
212 fIsCoincident[1] |= bit;
213}
214
215void SkIntersections::offset(int base, double start, double end) {
216 for (int index = base; index < fUsed; ++index) {
217 double val = fT[fSwap][index];
218 val *= end - start;
219 val += start;
220 fT[fSwap][index] = val;
221 }
222}
223
224int SkIntersections::quadRay(const SkPoint pts[3], const SkDLine& line) {
225 SkDQuad quad;
226 quad.set(pts);
227 return intersectRay(quad, line);
228}
229
230void SkIntersections::quickRemoveOne(int index, int replace) {
231 if (index < replace) {
232 fT[0][index] = fT[0][replace];
233 }
234}
235
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000236#if 0
caryclark@google.com07393ca2013-04-08 11:47:37 +0000237void SkIntersections::remove(double one, double two, const SkDPoint& startPt,
238 const SkDPoint& endPt) {
239 for (int index = fUsed - 1; index >= 0; --index) {
240 if (!(fIsCoincident[0] & (1 << index)) && (between(one, fT[fSwap][index], two)
241 || startPt.approximatelyEqual(fPt[index])
242 || endPt.approximatelyEqual(fPt[index]))) {
243 SkASSERT(fUsed > 0);
244 removeOne(index);
245 }
246 }
247}
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000248#endif
caryclark@google.com07393ca2013-04-08 11:47:37 +0000249
250void SkIntersections::removeOne(int index) {
251 int remaining = --fUsed - index;
252 if (remaining <= 0) {
253 return;
254 }
255 memmove(&fPt[index], &fPt[index + 1], sizeof(fPt[0]) * remaining);
256 memmove(&fT[0][index], &fT[0][index + 1], sizeof(fT[0][0]) * remaining);
257 memmove(&fT[1][index], &fT[1][index + 1], sizeof(fT[1][0]) * remaining);
258 SkASSERT(fIsCoincident[0] == 0);
259 int coBit = fIsCoincident[0] & (1 << index);
260 fIsCoincident[0] -= ((fIsCoincident[0] >> 1) & ~((1 << index) - 1)) + coBit;
261 SkASSERT(!(coBit ^ (fIsCoincident[1] & (1 << index))));
262 fIsCoincident[1] -= ((fIsCoincident[1] >> 1) & ~((1 << index) - 1)) + coBit;
263}
264
265void SkIntersections::swapPts() {
266 int index;
267 for (index = 0; index < fUsed; ++index) {
268 SkTSwap(fT[0][index], fT[1][index]);
269 }
270}
271
272int SkIntersections::verticalLine(const SkPoint a[2], SkScalar top, SkScalar bottom,
273 SkScalar x, bool flipped) {
274 SkDLine line;
275 line.set(a);
276 return vertical(line, top, bottom, x, flipped);
277}
278
279int SkIntersections::verticalQuad(const SkPoint a[3], SkScalar top, SkScalar bottom,
280 SkScalar x, bool flipped) {
281 SkDQuad quad;
282 quad.set(a);
283 return vertical(quad, top, bottom, x, flipped);
284}
285
286int SkIntersections::verticalCubic(const SkPoint a[4], SkScalar top, SkScalar bottom,
287 SkScalar x, bool flipped) {
288 SkDCubic cubic;
289 cubic.set(a);
290 return vertical(cubic, top, bottom, x, flipped);
291}