blob: ace8474d1dae4e93ba7fa7872fa27d147bc0190b [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) {
59 if (fSwap) {
60 remove(s2, e2, startPt, endPt);
61 } else {
62 remove(s1, e1, startPt, endPt);
63 }
64 SkASSERT(coincidentUsed() == fUsed);
65 SkASSERT((coincidentUsed() & 1) != 1);
66 int i1 = 0;
67 int i2 = 0;
68 do {
69 while (i1 < fUsed && !(fIsCoincident[fSwap] & (1 << i1))) {
70 ++i1;
71 }
72 if (i1 == fUsed) {
73 break;
74 }
75 SkASSERT(i1 < fUsed);
76 int iEnd1 = i1 + 1;
77 while (!(fIsCoincident[fSwap] & (1 << iEnd1))) {
78 ++iEnd1;
79 }
80 SkASSERT(iEnd1 < fUsed);
81 double cs1 = fT[fSwap][i1];
82 double ce1 = fT[fSwap][iEnd1];
83 bool s1in = between(cs1, s1, ce1) || startPt.approximatelyEqual(fPt[i1])
84 || startPt.approximatelyEqual(fPt[iEnd1]);
85 bool e1in = between(cs1, e1, ce1) || endPt.approximatelyEqual(fPt[i1])
86 || endPt.approximatelyEqual(fPt[iEnd1]);
87 while (i2 < fUsed && !(fIsCoincident[fSwap ^ 1] & (1 << i2))) {
88 ++i2;
89 }
90 int iEnd2 = i2 + 1;
91 while (!(fIsCoincident[fSwap ^ 1] & (1 << iEnd2))) {
92 ++iEnd2;
93 }
94 SkASSERT(iEnd2 < fUsed);
95 double cs2 = fT[fSwap ^ 1][i2];
96 double ce2 = fT[fSwap ^ 1][iEnd2];
97 bool s2in = between(cs2, s2, ce2) || startPt.approximatelyEqual(fPt[i2])
98 || startPt.approximatelyEqual(fPt[iEnd2]);
99 bool e2in = between(cs2, e2, ce2) || endPt.approximatelyEqual(fPt[i2])
100 || endPt.approximatelyEqual(fPt[iEnd2]);
101 if ((s1in | e1in) & (s2in | e2in)) {
102 if (s1 < cs1) {
103 fT[fSwap][i1] = s1;
104 fPt[i1] = startPt;
105 } else if (e1 < cs1) {
106 fT[fSwap][i1] = e1;
107 fPt[i1] = endPt;
108 }
109 if (s1 > ce1) {
110 fT[fSwap][iEnd1] = s1;
111 fPt[iEnd1] = startPt;
112 } else if (e1 > ce1) {
113 fT[fSwap][iEnd1] = e1;
114 fPt[iEnd1] = endPt;
115 }
116 if (s2 > e2) {
117 SkTSwap(cs2, ce2);
118 SkTSwap(i2, iEnd2);
119 }
120 if (s2 < cs2) {
121 fT[fSwap ^ 1][i2] = s2;
122 } else if (e2 < cs2) {
123 fT[fSwap ^ 1][i2] = e2;
124 }
125 if (s2 > ce2) {
126 fT[fSwap ^ 1][iEnd2] = s2;
127 } else if (e2 > ce2) {
128 fT[fSwap ^ 1][iEnd2] = e2;
129 }
130 return;
131 }
132 } while (true);
133 SkASSERT(fUsed < 9);
134 insertCoincident(s1, s2, startPt);
135 insertCoincident(e1, e2, endPt);
136}
137
138int SkIntersections::insert(double one, double two, const SkDPoint& pt) {
caryclark@google.comb3f09212013-04-17 15:49:16 +0000139 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 intersections
141 return -1;
142 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000143 SkASSERT(fUsed <= 1 || fT[0][0] <= fT[0][1]);
144 int index;
145 for (index = 0; index < fUsed; ++index) {
146 double oldOne = fT[0][index];
147 double oldTwo = fT[1][index];
148 if (roughly_equal(oldOne, one) && roughly_equal(oldTwo, two)) {
149 if ((precisely_zero(one) && !precisely_zero(oldOne))
150 || (precisely_equal(one, 1) && !precisely_equal(oldOne, 1))
151 || (precisely_zero(two) && !precisely_zero(oldTwo))
152 || (precisely_equal(two, 1) && !precisely_equal(oldTwo, 1))) {
153 fT[0][index] = one;
154 fT[1][index] = two;
155 fPt[index] = pt;
156 }
157 return -1;
158 }
159 #if ONE_OFF_DEBUG
160 if (pt.roughlyEqual(fPt[index])) {
161 SkDebugf("%s t=%1.9g pts roughly equal\n", __FUNCTION__, one);
162 }
163 #endif
164 if (fT[0][index] > one) {
165 break;
166 }
167 }
168 SkASSERT(fUsed < 9);
169 int remaining = fUsed - index;
170 if (remaining > 0) {
171 memmove(&fPt[index + 1], &fPt[index], sizeof(fPt[0]) * remaining);
172 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);
174 fIsCoincident[0] += fIsCoincident[0] & ~((1 << index) - 1);
175 fIsCoincident[1] += fIsCoincident[1] & ~((1 << index) - 1);
176 }
177 fPt[index] = pt;
178 fT[0][index] = one;
179 fT[1][index] = two;
180 ++fUsed;
181 return index;
182}
183
184void SkIntersections::insertCoincident(double one, double two, const SkDPoint& pt) {
185 int index = insertSwap(one, two, pt);
186 int bit = 1 << index;
187 fIsCoincident[0] |= bit;
188 fIsCoincident[1] |= bit;
189}
190
191void SkIntersections::offset(int base, double start, double end) {
192 for (int index = base; index < fUsed; ++index) {
193 double val = fT[fSwap][index];
194 val *= end - start;
195 val += start;
196 fT[fSwap][index] = val;
197 }
198}
199
200int SkIntersections::quadRay(const SkPoint pts[3], const SkDLine& line) {
201 SkDQuad quad;
202 quad.set(pts);
203 return intersectRay(quad, line);
204}
205
206void SkIntersections::quickRemoveOne(int index, int replace) {
207 if (index < replace) {
208 fT[0][index] = fT[0][replace];
209 }
210}
211
212void SkIntersections::remove(double one, double two, const SkDPoint& startPt,
213 const SkDPoint& endPt) {
214 for (int index = fUsed - 1; index >= 0; --index) {
215 if (!(fIsCoincident[0] & (1 << index)) && (between(one, fT[fSwap][index], two)
216 || startPt.approximatelyEqual(fPt[index])
217 || endPt.approximatelyEqual(fPt[index]))) {
218 SkASSERT(fUsed > 0);
219 removeOne(index);
220 }
221 }
222}
223
224void SkIntersections::removeOne(int index) {
225 int remaining = --fUsed - index;
226 if (remaining <= 0) {
227 return;
228 }
229 memmove(&fPt[index], &fPt[index + 1], sizeof(fPt[0]) * remaining);
230 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);
232 SkASSERT(fIsCoincident[0] == 0);
233 int coBit = fIsCoincident[0] & (1 << index);
234 fIsCoincident[0] -= ((fIsCoincident[0] >> 1) & ~((1 << index) - 1)) + coBit;
235 SkASSERT(!(coBit ^ (fIsCoincident[1] & (1 << index))));
236 fIsCoincident[1] -= ((fIsCoincident[1] >> 1) & ~((1 << index) - 1)) + coBit;
237}
238
239void SkIntersections::swapPts() {
240 int index;
241 for (index = 0; index < fUsed; ++index) {
242 SkTSwap(fT[0][index], fT[1][index]);
243 }
244}
245
246int SkIntersections::verticalLine(const SkPoint a[2], SkScalar top, SkScalar bottom,
247 SkScalar x, bool flipped) {
248 SkDLine line;
249 line.set(a);
250 return vertical(line, top, bottom, x, flipped);
251}
252
253int SkIntersections::verticalQuad(const SkPoint a[3], SkScalar top, SkScalar bottom,
254 SkScalar x, bool flipped) {
255 SkDQuad quad;
256 quad.set(a);
257 return vertical(quad, top, bottom, x, flipped);
258}
259
260int SkIntersections::verticalCubic(const SkPoint a[4], SkScalar top, SkScalar bottom,
261 SkScalar x, bool flipped) {
262 SkDCubic cubic;
263 cubic.set(a);
264 return vertical(cubic, top, bottom, x, flipped);
265}