blob: 82f230dd52dd30a47cac236ee33e46691f0a3a7d [file] [log] [blame]
reed@android.com3abec1d2009-03-02 05:36:20 +00001#include "Test.h"
2#include "SkPath.h"
reed@google.com04863fa2011-05-15 04:08:24 +00003#include "SkParse.h"
reed@android.com60bc6d52010-02-11 11:09:39 +00004#include "SkSize.h"
reed@android.com3abec1d2009-03-02 05:36:20 +00005
reed@google.com7c424812011-05-15 04:38:34 +00006static void check_convexity(skiatest::Reporter* reporter, const SkPath& path,
7 SkPath::Convexity expected) {
8 SkPath::Convexity c = SkPath::ComputeConvexity(path);
9 REPORTER_ASSERT(reporter, c == expected);
10}
11
12static void test_convexity2(skiatest::Reporter* reporter) {
13 SkPath pt;
14 pt.moveTo(0, 0);
15 pt.close();
16// check_convexity(reporter, pt, SkPath::kConvex_Convexity);
17 check_convexity(reporter, pt, SkPath::kUnknown_Convexity);
18
19 SkPath line;
20 line.moveTo(12, 20);
21 line.lineTo(-12, -20);
22 line.close();
23 // check_convexity(reporter, pt, SkPath::kConvex_Convexity);
24 check_convexity(reporter, pt, SkPath::kUnknown_Convexity);
25
26 SkPath triLeft;
27 triLeft.moveTo(0, 0);
28 triLeft.lineTo(1, 0);
29 triLeft.lineTo(1, 1);
30 triLeft.close();
31 check_convexity(reporter, triLeft, SkPath::kConvex_Convexity);
32
33 SkPath triRight;
34 triRight.moveTo(0, 0);
35 triRight.lineTo(-1, 0);
36 triRight.lineTo(1, 1);
37 triRight.close();
38 check_convexity(reporter, triRight, SkPath::kConvex_Convexity);
39
40 SkPath square;
41 square.moveTo(0, 0);
42 square.lineTo(1, 0);
43 square.lineTo(1, 1);
44 square.lineTo(0, 1);
45 square.close();
46 check_convexity(reporter, square, SkPath::kConvex_Convexity);
47
48 SkPath redundantSquare;
49 redundantSquare.moveTo(0, 0);
50 redundantSquare.lineTo(0, 0);
51 redundantSquare.lineTo(0, 0);
52 redundantSquare.lineTo(1, 0);
53 redundantSquare.lineTo(1, 0);
54 redundantSquare.lineTo(1, 0);
55 redundantSquare.lineTo(1, 1);
56 redundantSquare.lineTo(1, 1);
57 redundantSquare.lineTo(1, 1);
58 redundantSquare.lineTo(0, 1);
59 redundantSquare.lineTo(0, 1);
60 redundantSquare.lineTo(0, 1);
61 redundantSquare.close();
62 check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity);
63
64 SkPath bowTie;
65 bowTie.moveTo(0, 0);
66 bowTie.lineTo(0, 0);
67 bowTie.lineTo(0, 0);
68 bowTie.lineTo(1, 1);
69 bowTie.lineTo(1, 1);
70 bowTie.lineTo(1, 1);
71 bowTie.lineTo(1, 0);
72 bowTie.lineTo(1, 0);
73 bowTie.lineTo(1, 0);
74 bowTie.lineTo(0, 1);
75 bowTie.lineTo(0, 1);
76 bowTie.lineTo(0, 1);
77 bowTie.close();
78 check_convexity(reporter, bowTie, SkPath::kConcave_Convexity);
79
80 SkPath spiral;
81 spiral.moveTo(0, 0);
82 spiral.lineTo(1, 0);
83 spiral.lineTo(1, 1);
84 spiral.lineTo(0, 1);
85 spiral.lineTo(0,.5);
86 spiral.lineTo(.5,.5);
87 spiral.lineTo(.5,.75);
88 spiral.close();
89// check_convexity(reporter, spiral, SkPath::kConcave_Convexity);
90
91 SkPath dent;
92 dent.moveTo(0, 0);
93 dent.lineTo(1, 1);
94 dent.lineTo(0, 1);
95 dent.lineTo(-.5,2);
96 dent.lineTo(-2, 1);
97 dent.close();
98 check_convexity(reporter, dent, SkPath::kConcave_Convexity);
99}
100
reed@android.com6b82d1a2009-06-03 02:35:01 +0000101static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
102 const SkRect& bounds) {
103 REPORTER_ASSERT(reporter, p.isConvex());
104 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
reed@google.com62047cf2011-02-07 19:39:09 +0000105
reed@android.com6b82d1a2009-06-03 02:35:01 +0000106 SkPath p2(p);
107 REPORTER_ASSERT(reporter, p2.isConvex());
108 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
109
110 SkPath other;
111 other.swap(p2);
112 REPORTER_ASSERT(reporter, other.isConvex());
113 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
114}
115
reed@google.com04863fa2011-05-15 04:08:24 +0000116static void setFromString(SkPath* path, const char str[]) {
117 bool first = true;
118 while (str) {
119 SkScalar x, y;
120 str = SkParse::FindScalar(str, &x);
121 if (NULL == str) {
122 break;
123 }
124 str = SkParse::FindScalar(str, &y);
125 SkASSERT(str);
126 if (first) {
127 path->moveTo(x, y);
128 first = false;
129 } else {
130 path->lineTo(x, y);
131 }
132 }
133}
134
135static void test_convexity(skiatest::Reporter* reporter) {
136 static const SkPath::Convexity U = SkPath::kUnknown_Convexity;
137 static const SkPath::Convexity C = SkPath::kConcave_Convexity;
138 static const SkPath::Convexity V = SkPath::kConvex_Convexity;
139
140 SkPath path;
141
142 REPORTER_ASSERT(reporter, U == SkPath::ComputeConvexity(path));
143 path.addCircle(0, 0, 10);
144 REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path));
145 path.addCircle(0, 0, 10); // 2nd circle
146 REPORTER_ASSERT(reporter, C == SkPath::ComputeConvexity(path));
147 path.reset();
148 path.addRect(0, 0, 10, 10, SkPath::kCCW_Direction);
149 REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path));
150 path.reset();
151 path.addRect(0, 0, 10, 10, SkPath::kCW_Direction);
152 REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path));
153
154 static const struct {
155 const char* fPathStr;
156 SkPath::Convexity fExpectedConvexity;
157 } gRec[] = {
158 { "0 0", SkPath::kUnknown_Convexity },
159 { "0 0 10 10", SkPath::kUnknown_Convexity },
160 { "0 0 10 10 20 20 0 0 10 10", SkPath::kUnknown_Convexity },
161 { "0 0 10 10 10 20", SkPath::kConvex_Convexity },
162 { "0 0 10 10 10 0", SkPath::kConvex_Convexity },
163 { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity },
164 { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity },
165 };
166
167 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
168 SkPath path;
169 setFromString(&path, gRec[i].fPathStr);
170 SkPath::Convexity c = SkPath::ComputeConvexity(path);
171 REPORTER_ASSERT(reporter, c == gRec[i].fExpectedConvexity);
172 }
173}
174
175void TestPath(skiatest::Reporter* reporter);
176void TestPath(skiatest::Reporter* reporter) {
reed@android.com60bc6d52010-02-11 11:09:39 +0000177 {
178 SkSize size;
179 size.fWidth = 3.4f;
180 size.width();
181 size = SkSize::Make(3,4);
182 SkISize isize = SkISize::Make(3,4);
183 }
184
185 SkTSize<SkScalar>::Make(3,4);
186
reed@android.com3abec1d2009-03-02 05:36:20 +0000187 SkPath p, p2;
188 SkRect bounds, bounds2;
reed@android.com80e39a72009-04-02 16:59:40 +0000189
reed@android.com3abec1d2009-03-02 05:36:20 +0000190 REPORTER_ASSERT(reporter, p.isEmpty());
reed@android.com6b82d1a2009-06-03 02:35:01 +0000191 REPORTER_ASSERT(reporter, !p.isConvex());
reed@android.com3abec1d2009-03-02 05:36:20 +0000192 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
193 REPORTER_ASSERT(reporter, !p.isInverseFillType());
194 REPORTER_ASSERT(reporter, p == p2);
195 REPORTER_ASSERT(reporter, !(p != p2));
196
reed@android.comd252db02009-04-01 18:31:44 +0000197 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
reed@android.com80e39a72009-04-02 16:59:40 +0000198
reed@android.com3abec1d2009-03-02 05:36:20 +0000199 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
reed@android.com6b82d1a2009-06-03 02:35:01 +0000200
201 p.setIsConvex(false);
202 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
203 check_convex_bounds(reporter, p, bounds);
reed@google.com62047cf2011-02-07 19:39:09 +0000204
reed@android.com6b82d1a2009-06-03 02:35:01 +0000205 p.reset();
206 p.setIsConvex(false);
207 p.addOval(bounds);
208 check_convex_bounds(reporter, p, bounds);
reed@google.com62047cf2011-02-07 19:39:09 +0000209
reed@android.com6b82d1a2009-06-03 02:35:01 +0000210 p.reset();
211 p.setIsConvex(false);
reed@android.com3abec1d2009-03-02 05:36:20 +0000212 p.addRect(bounds);
reed@android.com6b82d1a2009-06-03 02:35:01 +0000213 check_convex_bounds(reporter, p, bounds);
reed@android.com3abec1d2009-03-02 05:36:20 +0000214
215 REPORTER_ASSERT(reporter, p != p2);
216 REPORTER_ASSERT(reporter, !(p == p2));
217
218 // does getPoints return the right result
219 REPORTER_ASSERT(reporter, p.getPoints(NULL, 5) == 4);
220 SkPoint pts[4];
221 int count = p.getPoints(pts, 4);
222 REPORTER_ASSERT(reporter, count == 4);
223 bounds2.set(pts, 4);
224 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +0000225
reed@android.com3abec1d2009-03-02 05:36:20 +0000226 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
227 p.offset(SK_Scalar1*3, SK_Scalar1*4);
reed@android.comd252db02009-04-01 18:31:44 +0000228 REPORTER_ASSERT(reporter, bounds == p.getBounds());
reed@android.com3abec1d2009-03-02 05:36:20 +0000229
230#if 0 // isRect needs to be implemented
231 REPORTER_ASSERT(reporter, p.isRect(NULL));
232 bounds.setEmpty();
233 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
234 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +0000235
reed@android.com3abec1d2009-03-02 05:36:20 +0000236 // now force p to not be a rect
237 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
238 p.addRect(bounds);
239 REPORTER_ASSERT(reporter, !p.isRect(NULL));
240#endif
241
242 SkPoint pt;
243
244 p.moveTo(SK_Scalar1, 0);
245 p.getLastPt(&pt);
246 REPORTER_ASSERT(reporter, pt.fX == SK_Scalar1);
reed@google.com62047cf2011-02-07 19:39:09 +0000247
248 // check that reset and rewind clear the convex hint back to false
249 p.setIsConvex(false);
250 REPORTER_ASSERT(reporter, !p.isConvex());
251 p.setIsConvex(true);
252 REPORTER_ASSERT(reporter, p.isConvex());
253 p.reset();
254 REPORTER_ASSERT(reporter, !p.isConvex());
255 p.setIsConvex(true);
256 REPORTER_ASSERT(reporter, p.isConvex());
257 p.rewind();
258 REPORTER_ASSERT(reporter, !p.isConvex());
reed@google.com04863fa2011-05-15 04:08:24 +0000259
260 test_convexity(reporter);
reed@google.com7c424812011-05-15 04:38:34 +0000261 test_convexity2(reporter);
reed@android.com3abec1d2009-03-02 05:36:20 +0000262}
263
264#include "TestClassDef.h"
265DEFINE_TESTCLASS("Path", PathTestClass, TestPath)