blob: 68e3ab59d5b95f64111a9baf54097eb383cd3f40 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
reed@android.com8a1c16f2008-12-17 15:59:43 +00009
10#include "SkCullPoints.h"
11#include "Sk64.h"
12
reed@android.com573a42d2010-04-13 13:36:20 +000013static bool cross_product_is_neg(const SkIPoint& v, int dx, int dy) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#if 0
15 return v.fX * dy - v.fY * dx < 0;
16#else
17 Sk64 tmp0, tmp1;
18
19 tmp0.setMul(v.fX, dy);
20 tmp1.setMul(dx, v.fY);
21 tmp0.sub(tmp1);
reed@google.comea282db2011-03-01 15:14:44 +000022 return tmp0.isNeg() != 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +000023#endif
24}
25
reed@android.com573a42d2010-04-13 13:36:20 +000026bool SkCullPoints::sect_test(int x0, int y0, int x1, int y1) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000027 const SkIRect& r = fR;
28
reed@android.com573a42d2010-04-13 13:36:20 +000029 if ((x0 < r.fLeft && x1 < r.fLeft) ||
30 (x0 > r.fRight && x1 > r.fRight) ||
31 (y0 < r.fTop && y1 < r.fTop) ||
32 (y0 > r.fBottom && y1 > r.fBottom)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000033 return false;
reed@android.com573a42d2010-04-13 13:36:20 +000034 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000035
36 // since the crossprod test is a little expensive, check for easy-in cases first
reed@android.com573a42d2010-04-13 13:36:20 +000037 if (r.contains(x0, y0) || r.contains(x1, y1)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000038 return true;
reed@android.com573a42d2010-04-13 13:36:20 +000039 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000040
41 // At this point we're not sure, so we do a crossprod test
42 SkIPoint vec;
43 const SkIPoint* rAsQuad = fAsQuad;
44
45 vec.set(x1 - x0, y1 - y0);
46 bool isNeg = cross_product_is_neg(vec, x0 - rAsQuad[0].fX, y0 - rAsQuad[0].fY);
47 for (int i = 1; i < 4; i++) {
reed@android.com573a42d2010-04-13 13:36:20 +000048 if (cross_product_is_neg(vec, x0 - rAsQuad[i].fX, y0 - rAsQuad[i].fY) != isNeg) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 return true;
50 }
51 }
52 return false; // we didn't intersect
53}
54
reed@android.com573a42d2010-04-13 13:36:20 +000055static void toQuad(const SkIRect& r, SkIPoint quad[4]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 SkASSERT(quad);
57
58 quad[0].set(r.fLeft, r.fTop);
59 quad[1].set(r.fRight, r.fTop);
60 quad[2].set(r.fRight, r.fBottom);
61 quad[3].set(r.fLeft, r.fBottom);
62}
63
reed@android.com573a42d2010-04-13 13:36:20 +000064SkCullPoints::SkCullPoints() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000065 SkIRect r;
66 r.setEmpty();
67 this->reset(r);
68}
69
reed@android.com573a42d2010-04-13 13:36:20 +000070SkCullPoints::SkCullPoints(const SkIRect& r) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 this->reset(r);
72}
73
reed@android.com573a42d2010-04-13 13:36:20 +000074void SkCullPoints::reset(const SkIRect& r) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 fR = r;
76 toQuad(fR, fAsQuad);
77 fPrevPt.set(0, 0);
78 fPrevResult = kNo_Result;
79}
80
reed@android.com573a42d2010-04-13 13:36:20 +000081void SkCullPoints::moveTo(int x, int y) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000082 fPrevPt.set(x, y);
83 fPrevResult = kNo_Result; // so we trigger a movetolineto later
84}
85
reed@android.com573a42d2010-04-13 13:36:20 +000086SkCullPoints::LineToResult SkCullPoints::lineTo(int x, int y, SkIPoint line[]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 SkASSERT(line != NULL);
88
89 LineToResult result = kNo_Result;
90 int x0 = fPrevPt.fX;
91 int y0 = fPrevPt.fY;
92
93 // need to upgrade sect_test to chop the result
94 // and to correctly return kLineTo_Result when the result is connected
95 // to the previous call-out
reed@android.com573a42d2010-04-13 13:36:20 +000096 if (this->sect_test(x0, y0, x, y)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 line[0].set(x0, y0);
98 line[1].set(x, y);
99
reed@android.com573a42d2010-04-13 13:36:20 +0000100 if (fPrevResult != kNo_Result && fPrevPt.equals(x0, y0)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 result = kLineTo_Result;
reed@android.com573a42d2010-04-13 13:36:20 +0000102 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 result = kMoveToLineTo_Result;
reed@android.com573a42d2010-04-13 13:36:20 +0000104 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 }
106
107 fPrevPt.set(x, y);
108 fPrevResult = result;
109
110 return result;
111}
112
113/////////////////////////////////////////////////////////////////////////////////////////////////
114
115#include "SkPath.h"
116
117SkCullPointsPath::SkCullPointsPath()
reed@android.com573a42d2010-04-13 13:36:20 +0000118 : fCP(), fPath(NULL) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119}
120
121SkCullPointsPath::SkCullPointsPath(const SkIRect& r, SkPath* dst)
reed@android.com573a42d2010-04-13 13:36:20 +0000122 : fCP(r), fPath(dst) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123}
124
reed@android.com573a42d2010-04-13 13:36:20 +0000125void SkCullPointsPath::reset(const SkIRect& r, SkPath* dst) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 fCP.reset(r);
127 fPath = dst;
128}
reed@android.com573a42d2010-04-13 13:36:20 +0000129
130void SkCullPointsPath::moveTo(int x, int y) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131 fCP.moveTo(x, y);
132}
133
reed@android.com573a42d2010-04-13 13:36:20 +0000134void SkCullPointsPath::lineTo(int x, int y) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 SkIPoint pts[2];
136
137 switch (fCP.lineTo(x, y, pts)) {
138 case SkCullPoints::kMoveToLineTo_Result:
139 fPath->moveTo(SkIntToScalar(pts[0].fX), SkIntToScalar(pts[0].fY));
140 // fall through to the lineto case
141 case SkCullPoints::kLineTo_Result:
142 fPath->lineTo(SkIntToScalar(pts[1].fX), SkIntToScalar(pts[1].fY));
143 break;
144 default:
145 break;
146 }
147}
148
mike@reedtribe.orga2a95f92012-07-03 02:35:36 +0000149///////////////////////////////////////////////////////////////////////////////
150
151#include "SkMatrix.h"
152#include "SkRegion.h"
153
154bool SkHitTestPath(const SkPath& path, SkRect& target, bool hires) {
155 if (target.isEmpty()) {
156 return false;
157 }
158
159 bool isInverse = path.isInverseFillType();
160 if (path.isEmpty()) {
161 return isInverse;
162 }
163
164 SkRect bounds = path.getBounds();
165
166 bool sects = SkRect::Intersects(target, bounds);
167 if (isInverse) {
168 if (!sects) {
169 return true;
170 }
171 } else {
172 if (!sects) {
173 return false;
174 }
175 if (target.contains(bounds)) {
176 return true;
177 }
178 }
179
180 SkPath devPath;
181 const SkPath* pathPtr;
182 SkRect devTarget;
183
184 if (hires) {
185 const SkScalar coordLimit = SkIntToScalar(16384);
186 const SkRect limit = { 0, 0, coordLimit, coordLimit };
187
188 SkMatrix matrix;
189 matrix.setRectToRect(bounds, limit, SkMatrix::kFill_ScaleToFit);
190
191 path.transform(matrix, &devPath);
192 matrix.mapRect(&devTarget, target);
193
194 pathPtr = &devPath;
195 } else {
196 devTarget = target;
197 pathPtr = &path;
198 }
199
200 SkIRect iTarget;
201 devTarget.round(&iTarget);
202 if (iTarget.isEmpty()) {
203 iTarget.fLeft = SkScalarFloorToInt(devTarget.fLeft);
204 iTarget.fTop = SkScalarFloorToInt(devTarget.fTop);
205 iTarget.fRight = iTarget.fLeft + 1;
206 iTarget.fBottom = iTarget.fTop + 1;
207 }
208
209 SkRegion clip(iTarget);
210 SkRegion rgn;
211 return rgn.setPath(*pathPtr, clip) ^ isInverse;
212}
213
214bool SkHitTestPath(const SkPath& path, SkScalar x, SkScalar y, bool hires) {
215 const SkScalar half = SK_ScalarHalf;
216 const SkScalar one = SK_Scalar1;
217 SkRect r = SkRect::MakeXYWH(x - half, y - half, one, one);
218 return SkHitTestPath(path, r, hires);
219}
220
221///////////////////////////////////////////////////////////////////////////////
222
223#include "SkGeometry.h"
224
225static int winding_mono_quad(const SkPoint pts[], SkScalar x, SkScalar y) {
226 SkScalar y0 = pts[0].fY;
227 SkScalar y2 = pts[2].fY;
228
229 int dir = 1;
230 if (y0 > y2) {
231 SkTSwap(y0, y2);
232 dir = -1;
233 }
234 if (y < y0 || y >= y2) {
235 return 0;
236 }
237
238 // bounds check on X (not required, but maybe faster)
239#if 0
240 if (pts[0].fX > x && pts[1].fX > x && pts[2].fX > x) {
241 return 0;
242 }
243#endif
244
245 SkScalar roots[2];
246 int n = SkFindUnitQuadRoots(pts[0].fY - 2 * pts[1].fY + pts[2].fY,
247 2 * (pts[1].fY - pts[0].fY),
248 pts[0].fY - y,
249 roots);
250 SkASSERT(n <= 1);
251 SkScalar xt;
252 if (0 == n) {
253 SkScalar mid = SkScalarAve(y0, y2);
254 // Need [0] and [2] if dir == 1
255 // and [2] and [0] if dir == -1
256 xt = y < mid ? pts[1 - dir].fX : pts[dir - 1].fX;
257 } else {
258 SkScalar t = roots[0];
259 SkScalar C = pts[0].fX;
260 SkScalar A = pts[2].fX - 2 * pts[1].fX + C;
261 SkScalar B = 2 * (pts[1].fX - C);
262 xt = SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C);
263 }
264 return xt < x ? dir : 0;
265}
266
267static bool is_mono(SkScalar y0, SkScalar y1, SkScalar y2) {
268// return SkScalarSignAsInt(y0 - y1) + SkScalarSignAsInt(y1 - y2) != 0;
269 if (y0 == y1) {
270 return true;
271 }
272 if (y0 < y1) {
273 return y1 <= y2;
274 } else {
275 return y1 >= y2;
276 }
277}
278
279static int winding_quad(const SkPoint pts[], SkScalar x, SkScalar y) {
280 SkPoint dst[5];
281 int n = 0;
282
283 if (!is_mono(pts[0].fY, pts[1].fY, pts[2].fY)) {
284 n = SkChopQuadAtYExtrema(pts, dst);
285 pts = dst;
286 }
287 int w = winding_mono_quad(pts, x, y);
288 if (n > 0) {
289 w += winding_mono_quad(&pts[2], x, y);
290 }
291 return w;
292}
293
294static int winding_line(const SkPoint pts[], SkScalar x, SkScalar y) {
295 SkScalar x0 = pts[0].fX;
296 SkScalar y0 = pts[0].fY;
297 SkScalar x1 = pts[1].fX;
298 SkScalar y1 = pts[1].fY;
299
300 SkScalar dy = y1 - y0;
301
302 int dir = 1;
303 if (y0 > y1) {
304 SkTSwap(y0, y1);
305 dir = -1;
306 }
307 if (y < y0 || y >= y1) {
308 return 0;
309 }
310
311 SkScalar cross = SkScalarMul(x1 - x0, y - pts[0].fY) -
312 SkScalarMul(dy, x - pts[0].fX);
313
314 if (SkScalarSignAsInt(cross) == dir) {
315 dir = 0;
316 }
317 return dir;
318}
319
320static int winding_cubic(const SkPoint pts[], SkScalar x, SkScalar y) {
321 return 0;
322}
323
324bool SkHitTestPathEx(const SkPath& path, SkScalar x, SkScalar y) {
325 bool isInverse = path.isInverseFillType();
326 if (path.isEmpty()) {
327 return isInverse;
328 }
329
330 const SkRect& bounds = path.getBounds();
331 if (!bounds.contains(x, y)) {
332 return isInverse;
333 }
334
335 SkPath::Iter iter(path, true);
336 bool done = false;
337 int w = 0;
338 do {
339 SkPoint pts[4];
340 switch (iter.next(pts, false)) {
341 case SkPath::kMove_Verb:
342 case SkPath::kClose_Verb:
343 break;
344 case SkPath::kLine_Verb:
345 w += winding_line(pts, x, y);
346 break;
347 case SkPath::kQuad_Verb:
348 w += winding_quad(pts, x, y);
349 break;
350 case SkPath::kCubic_Verb:
351 w += winding_cubic(pts, x, y);
352 break;
353 case SkPath::kDone_Verb:
354 done = true;
355 break;
356 }
357 } while (!done);
358
359 switch (path.getFillType()) {
360 case SkPath::kEvenOdd_FillType:
361 case SkPath::kInverseEvenOdd_FillType:
362 w &= 1;
363 break;
364 }
365 return SkToBool(w);
366}
367