blob: 844b5934e109e87b876cb6c1f03bce58aca66835 [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();
reed@google.comb54455e2011-05-16 14:16:04 +000016 check_convexity(reporter, pt, SkPath::kConvex_Convexity);
reed@google.com7c424812011-05-15 04:38:34 +000017
18 SkPath line;
19 line.moveTo(12, 20);
20 line.lineTo(-12, -20);
21 line.close();
reed@google.comb54455e2011-05-16 14:16:04 +000022 check_convexity(reporter, pt, SkPath::kConvex_Convexity);
reed@google.com7c424812011-05-15 04:38:34 +000023
24 SkPath triLeft;
25 triLeft.moveTo(0, 0);
26 triLeft.lineTo(1, 0);
27 triLeft.lineTo(1, 1);
28 triLeft.close();
29 check_convexity(reporter, triLeft, SkPath::kConvex_Convexity);
30
31 SkPath triRight;
32 triRight.moveTo(0, 0);
33 triRight.lineTo(-1, 0);
34 triRight.lineTo(1, 1);
35 triRight.close();
36 check_convexity(reporter, triRight, SkPath::kConvex_Convexity);
37
38 SkPath square;
39 square.moveTo(0, 0);
40 square.lineTo(1, 0);
41 square.lineTo(1, 1);
42 square.lineTo(0, 1);
43 square.close();
44 check_convexity(reporter, square, SkPath::kConvex_Convexity);
45
46 SkPath redundantSquare;
47 redundantSquare.moveTo(0, 0);
48 redundantSquare.lineTo(0, 0);
49 redundantSquare.lineTo(0, 0);
50 redundantSquare.lineTo(1, 0);
51 redundantSquare.lineTo(1, 0);
52 redundantSquare.lineTo(1, 0);
53 redundantSquare.lineTo(1, 1);
54 redundantSquare.lineTo(1, 1);
55 redundantSquare.lineTo(1, 1);
56 redundantSquare.lineTo(0, 1);
57 redundantSquare.lineTo(0, 1);
58 redundantSquare.lineTo(0, 1);
59 redundantSquare.close();
60 check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity);
61
62 SkPath bowTie;
63 bowTie.moveTo(0, 0);
64 bowTie.lineTo(0, 0);
65 bowTie.lineTo(0, 0);
66 bowTie.lineTo(1, 1);
67 bowTie.lineTo(1, 1);
68 bowTie.lineTo(1, 1);
69 bowTie.lineTo(1, 0);
70 bowTie.lineTo(1, 0);
71 bowTie.lineTo(1, 0);
72 bowTie.lineTo(0, 1);
73 bowTie.lineTo(0, 1);
74 bowTie.lineTo(0, 1);
75 bowTie.close();
76 check_convexity(reporter, bowTie, SkPath::kConcave_Convexity);
77
78 SkPath spiral;
79 spiral.moveTo(0, 0);
epoger@google.com2047f002011-05-17 17:36:59 +000080 spiral.lineTo(100, 0);
81 spiral.lineTo(100, 100);
82 spiral.lineTo(0, 100);
83 spiral.lineTo(0, 50);
84 spiral.lineTo(50, 50);
85 spiral.lineTo(50, 75);
reed@google.com7c424812011-05-15 04:38:34 +000086 spiral.close();
reed@google.com85b6e392011-05-15 20:25:17 +000087 check_convexity(reporter, spiral, SkPath::kConcave_Convexity);
reed@google.com7c424812011-05-15 04:38:34 +000088
epoger@google.com2047f002011-05-17 17:36:59 +000089 // TODO(reed): We evaluate this path as concave for SK_SCALAR_IS_FLOAT,
90 // but convex for SK_SCALAR_IS_FIXED.
reed@google.com7c424812011-05-15 04:38:34 +000091 SkPath dent;
92 dent.moveTo(0, 0);
epoger@google.com2047f002011-05-17 17:36:59 +000093 dent.lineTo(100, 100);
94 dent.lineTo(0, 100);
95 dent.lineTo(-50, 200);
96 dent.lineTo(-200, 100);
reed@google.com7c424812011-05-15 04:38:34 +000097 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) {
reed@google.com04863fa2011-05-15 04:08:24 +0000136 static const SkPath::Convexity C = SkPath::kConcave_Convexity;
137 static const SkPath::Convexity V = SkPath::kConvex_Convexity;
138
139 SkPath path;
140
reed@google.comb54455e2011-05-16 14:16:04 +0000141 REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path));
reed@google.com04863fa2011-05-15 04:08:24 +0000142 path.addCircle(0, 0, 10);
143 REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path));
144 path.addCircle(0, 0, 10); // 2nd circle
145 REPORTER_ASSERT(reporter, C == SkPath::ComputeConvexity(path));
146 path.reset();
147 path.addRect(0, 0, 10, 10, SkPath::kCCW_Direction);
148 REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path));
149 path.reset();
150 path.addRect(0, 0, 10, 10, SkPath::kCW_Direction);
151 REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path));
152
153 static const struct {
154 const char* fPathStr;
155 SkPath::Convexity fExpectedConvexity;
156 } gRec[] = {
reed@google.comb54455e2011-05-16 14:16:04 +0000157 { "", SkPath::kConvex_Convexity },
158 { "0 0", SkPath::kConvex_Convexity },
159 { "0 0 10 10", SkPath::kConvex_Convexity },
reed@google.com85b6e392011-05-15 20:25:17 +0000160 { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity },
reed@google.com04863fa2011-05-15 04:08:24 +0000161 { "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@google.comb54455e2011-05-16 14:16:04 +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
reed@android.com6b82d1a2009-06-03 02:35:01 +0000201 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
202 check_convex_bounds(reporter, p, bounds);
reed@google.com62047cf2011-02-07 19:39:09 +0000203
reed@android.com6b82d1a2009-06-03 02:35:01 +0000204 p.reset();
reed@android.com6b82d1a2009-06-03 02:35:01 +0000205 p.addOval(bounds);
206 check_convex_bounds(reporter, p, bounds);
reed@google.com62047cf2011-02-07 19:39:09 +0000207
reed@android.com6b82d1a2009-06-03 02:35:01 +0000208 p.reset();
reed@android.com3abec1d2009-03-02 05:36:20 +0000209 p.addRect(bounds);
reed@android.com6b82d1a2009-06-03 02:35:01 +0000210 check_convex_bounds(reporter, p, bounds);
reed@android.com3abec1d2009-03-02 05:36:20 +0000211
212 REPORTER_ASSERT(reporter, p != p2);
213 REPORTER_ASSERT(reporter, !(p == p2));
214
215 // does getPoints return the right result
216 REPORTER_ASSERT(reporter, p.getPoints(NULL, 5) == 4);
217 SkPoint pts[4];
218 int count = p.getPoints(pts, 4);
219 REPORTER_ASSERT(reporter, count == 4);
220 bounds2.set(pts, 4);
221 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +0000222
reed@android.com3abec1d2009-03-02 05:36:20 +0000223 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
224 p.offset(SK_Scalar1*3, SK_Scalar1*4);
reed@android.comd252db02009-04-01 18:31:44 +0000225 REPORTER_ASSERT(reporter, bounds == p.getBounds());
reed@android.com3abec1d2009-03-02 05:36:20 +0000226
227#if 0 // isRect needs to be implemented
228 REPORTER_ASSERT(reporter, p.isRect(NULL));
229 bounds.setEmpty();
230 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
231 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +0000232
reed@android.com3abec1d2009-03-02 05:36:20 +0000233 // now force p to not be a rect
234 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
235 p.addRect(bounds);
236 REPORTER_ASSERT(reporter, !p.isRect(NULL));
237#endif
238
239 SkPoint pt;
240
241 p.moveTo(SK_Scalar1, 0);
242 p.getLastPt(&pt);
243 REPORTER_ASSERT(reporter, pt.fX == SK_Scalar1);
reed@google.com62047cf2011-02-07 19:39:09 +0000244
reed@google.com04863fa2011-05-15 04:08:24 +0000245 test_convexity(reporter);
reed@google.com7c424812011-05-15 04:38:34 +0000246 test_convexity2(reporter);
reed@android.com3abec1d2009-03-02 05:36:20 +0000247}
248
249#include "TestClassDef.h"
250DEFINE_TESTCLASS("Path", PathTestClass, TestPath)