blob: a903075f71a2b200c7d5f3af24ad4524aea4210e [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