blob: c8d58c148734112fc6789b6ce900d8ab2ca4b8ad [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/* libs/graphics/effects/SkCullPoints.cpp
2**
3** Copyright 2006, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#include "SkCullPoints.h"
19#include "Sk64.h"
20
reed@android.com573a42d2010-04-13 13:36:20 +000021static bool cross_product_is_neg(const SkIPoint& v, int dx, int dy) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000022#if 0
23 return v.fX * dy - v.fY * dx < 0;
24#else
25 Sk64 tmp0, tmp1;
26
27 tmp0.setMul(v.fX, dy);
28 tmp1.setMul(dx, v.fY);
29 tmp0.sub(tmp1);
reed@google.comea282db2011-03-01 15:14:44 +000030 return tmp0.isNeg() != 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +000031#endif
32}
33
reed@android.com573a42d2010-04-13 13:36:20 +000034bool SkCullPoints::sect_test(int x0, int y0, int x1, int y1) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000035 const SkIRect& r = fR;
36
reed@android.com573a42d2010-04-13 13:36:20 +000037 if ((x0 < r.fLeft && x1 < r.fLeft) ||
38 (x0 > r.fRight && x1 > r.fRight) ||
39 (y0 < r.fTop && y1 < r.fTop) ||
40 (y0 > r.fBottom && y1 > r.fBottom)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 return false;
reed@android.com573a42d2010-04-13 13:36:20 +000042 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000043
44 // since the crossprod test is a little expensive, check for easy-in cases first
reed@android.com573a42d2010-04-13 13:36:20 +000045 if (r.contains(x0, y0) || r.contains(x1, y1)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000046 return true;
reed@android.com573a42d2010-04-13 13:36:20 +000047 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000048
49 // At this point we're not sure, so we do a crossprod test
50 SkIPoint vec;
51 const SkIPoint* rAsQuad = fAsQuad;
52
53 vec.set(x1 - x0, y1 - y0);
54 bool isNeg = cross_product_is_neg(vec, x0 - rAsQuad[0].fX, y0 - rAsQuad[0].fY);
55 for (int i = 1; i < 4; i++) {
reed@android.com573a42d2010-04-13 13:36:20 +000056 if (cross_product_is_neg(vec, x0 - rAsQuad[i].fX, y0 - rAsQuad[i].fY) != isNeg) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000057 return true;
58 }
59 }
60 return false; // we didn't intersect
61}
62
reed@android.com573a42d2010-04-13 13:36:20 +000063static void toQuad(const SkIRect& r, SkIPoint quad[4]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 SkASSERT(quad);
65
66 quad[0].set(r.fLeft, r.fTop);
67 quad[1].set(r.fRight, r.fTop);
68 quad[2].set(r.fRight, r.fBottom);
69 quad[3].set(r.fLeft, r.fBottom);
70}
71
reed@android.com573a42d2010-04-13 13:36:20 +000072SkCullPoints::SkCullPoints() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 SkIRect r;
74 r.setEmpty();
75 this->reset(r);
76}
77
reed@android.com573a42d2010-04-13 13:36:20 +000078SkCullPoints::SkCullPoints(const SkIRect& r) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 this->reset(r);
80}
81
reed@android.com573a42d2010-04-13 13:36:20 +000082void SkCullPoints::reset(const SkIRect& r) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 fR = r;
84 toQuad(fR, fAsQuad);
85 fPrevPt.set(0, 0);
86 fPrevResult = kNo_Result;
87}
88
reed@android.com573a42d2010-04-13 13:36:20 +000089void SkCullPoints::moveTo(int x, int y) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000090 fPrevPt.set(x, y);
91 fPrevResult = kNo_Result; // so we trigger a movetolineto later
92}
93
reed@android.com573a42d2010-04-13 13:36:20 +000094SkCullPoints::LineToResult SkCullPoints::lineTo(int x, int y, SkIPoint line[]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000095 SkASSERT(line != NULL);
96
97 LineToResult result = kNo_Result;
98 int x0 = fPrevPt.fX;
99 int y0 = fPrevPt.fY;
100
101 // need to upgrade sect_test to chop the result
102 // and to correctly return kLineTo_Result when the result is connected
103 // to the previous call-out
reed@android.com573a42d2010-04-13 13:36:20 +0000104 if (this->sect_test(x0, y0, x, y)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 line[0].set(x0, y0);
106 line[1].set(x, y);
107
reed@android.com573a42d2010-04-13 13:36:20 +0000108 if (fPrevResult != kNo_Result && fPrevPt.equals(x0, y0)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109 result = kLineTo_Result;
reed@android.com573a42d2010-04-13 13:36:20 +0000110 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 result = kMoveToLineTo_Result;
reed@android.com573a42d2010-04-13 13:36:20 +0000112 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113 }
114
115 fPrevPt.set(x, y);
116 fPrevResult = result;
117
118 return result;
119}
120
121/////////////////////////////////////////////////////////////////////////////////////////////////
122
123#include "SkPath.h"
124
125SkCullPointsPath::SkCullPointsPath()
reed@android.com573a42d2010-04-13 13:36:20 +0000126 : fCP(), fPath(NULL) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127}
128
129SkCullPointsPath::SkCullPointsPath(const SkIRect& r, SkPath* dst)
reed@android.com573a42d2010-04-13 13:36:20 +0000130 : fCP(r), fPath(dst) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131}
132
reed@android.com573a42d2010-04-13 13:36:20 +0000133void SkCullPointsPath::reset(const SkIRect& r, SkPath* dst) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 fCP.reset(r);
135 fPath = dst;
136}
reed@android.com573a42d2010-04-13 13:36:20 +0000137
138void SkCullPointsPath::moveTo(int x, int y) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 fCP.moveTo(x, y);
140}
141
reed@android.com573a42d2010-04-13 13:36:20 +0000142void SkCullPointsPath::lineTo(int x, int y) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143 SkIPoint pts[2];
144
145 switch (fCP.lineTo(x, y, pts)) {
146 case SkCullPoints::kMoveToLineTo_Result:
147 fPath->moveTo(SkIntToScalar(pts[0].fX), SkIntToScalar(pts[0].fY));
148 // fall through to the lineto case
149 case SkCullPoints::kLineTo_Result:
150 fPath->lineTo(SkIntToScalar(pts[1].fX), SkIntToScalar(pts[1].fY));
151 break;
152 default:
153 break;
154 }
155}
156