blob: 3ea0e0bae7d0872b07aea59433e34ffbbb41b9fc [file] [log] [blame]
caryclark@google.comcd4421d2012-03-01 19:16:31 +00001#include "EdgeWalker_Test.h"
2#include "Intersection_Tests.h"
3
4static void testSimplifyTriangle() {
5 SkPath path, out;
6 path.setFillType(SkPath::kWinding_FillType);
7 path.moveTo(10,10); // triangle |\ .
8 path.lineTo(10,30); // |_\ .
9 path.lineTo(20,30);
10 path.close();
11 path.moveTo(20,10); // triangle /|
12 path.lineTo(10,30); // /_|
13 path.lineTo(20,30);
14 path.close();
15 simplify(path, true, out); // expect |\/|
16 comparePaths(path, out); // |__|
17}
18
19static void testSimplifyTriangle3() {
20 SkPath path, out;
21 path.moveTo(0, 0);
22 path.lineTo(1, 0);
23 path.lineTo(0, 1);
24 path.close();
25 path.moveTo(0, 0);
26 path.lineTo(1, 0);
27 path.lineTo(3, 1);
28 path.close();
29 simplify(path, true, out);
30 comparePaths(path, out);
31}
32
33static void testSimplifyTriangle4() {
34 SkPath path, out;
35 path.moveTo(0, 0);
36 path.lineTo(1, 0);
37 path.lineTo(0, 1);
38 path.close();
39 path.moveTo(0, 0);
40 path.lineTo(1, 0);
41 path.lineTo(2, 1);
42 path.close();
43 simplify(path, true, out);
44 comparePaths(path, out);
45}
46
47static void testSimplifyTriangle5() {
48 SkPath path, out;
49 path.moveTo(0, 0);
50 path.lineTo(1, 0);
51 path.lineTo(0, 1);
52 path.close();
53 path.moveTo(0, 0);
54 path.lineTo(1, 1);
55 path.lineTo(2, 1);
56 path.close();
57 simplify(path, true, out);
58 comparePaths(path, out);
59}
60
61static void testSimplifyTriangle6() {
62 SkPath path, out;
63 path.moveTo(0, 0);
64 path.lineTo(1, 0);
65 path.lineTo(0, 1);
66 path.lineTo(0, 0);
67 path.close();
68 path.moveTo(0, 0);
69 path.lineTo(0, 1);
70 path.lineTo(3, 1);
71 path.lineTo(0, 0);
72 path.close();
73 simplify(path, true, out);
74 comparePaths(path, out);
75}
76
77static void testSimplifyTriangle7() {
78 SkPath path, out;
79 path.moveTo(0, 0);
80 path.lineTo(1, 0);
81 path.lineTo(0, 1);
82 path.lineTo(0, 0);
83 path.close();
84 path.moveTo(0, 0);
85 path.lineTo(1, 1);
86 path.lineTo(0, 2);
87 path.lineTo(0, 0);
88 path.close();
89 simplify(path, true, out);
90 comparePaths(path, out);
91}
92
93static void testSimplifyTriangle8() {
94 SkPath path, out;
95 path.moveTo(0, 0);
96 path.lineTo(1, 0);
97 path.lineTo(0, 1);
98 path.lineTo(0, 0);
99 path.close();
100 path.moveTo(0, 1);
101 path.lineTo(1, 2);
102 path.lineTo(1, 3);
103 path.lineTo(0, 1);
104 path.close();
105 simplify(path, true, out);
106 comparePaths(path, out);
107}
108
109static void testSimplifyTriangle9() {
110 SkPath path, out;
111 path.moveTo(0, 0);
112 path.lineTo(1, 0);
113 path.lineTo(1, 1);
114 path.lineTo(0, 0);
115 path.close();
116 path.moveTo(0, 0);
117 path.lineTo(1, 1);
118 path.lineTo(2, 1);
119 path.lineTo(0, 0);
120 path.close();
121 simplify(path, true, out);
122 comparePaths(path, out);
123}
124
125static void testSimplifyTriangle10() {
126 SkPath path, out;
127 path.moveTo(0, 0);
128 path.lineTo(1, 0);
129 path.lineTo(1, 1);
130 path.lineTo(0, 0);
131 path.close();
132 path.moveTo(0, 0);
133 path.lineTo(2, 0);
134 path.lineTo(0, 1);
135 path.lineTo(0, 0);
136 path.close();
137 simplify(path, true, out);
138 comparePaths(path, out);
139}
140
141static void testSimplifyTriangle11() {
142 SkPath path, out;
143 path.moveTo(0, 0);
144 path.lineTo(1, 0);
145 path.lineTo(0, 2);
146 path.lineTo(0, 0);
147 path.close();
148 path.moveTo(0, 0);
149 path.lineTo(2, 1);
150 path.lineTo(2, 2);
151 path.lineTo(0, 0);
152 path.close();
153 simplify(path, true, out);
154 comparePaths(path, out);
155}
156
157static void testSimplifyTriangle12() {
158 SkPath path, out;
159 path.moveTo(0, 0);
160 path.lineTo(1, 0);
161 path.lineTo(1, 2);
162 path.lineTo(0, 0);
163 path.close();
164 path.moveTo(2, 0);
165 path.lineTo(0, 3);
166 path.lineTo(1, 1);
167 path.lineTo(2, 0);
168 path.close();
169 simplify(path, true, out);
170 comparePaths(path, out);
171}
172
173static void testSimplifyTriangle13() {
174 SkPath path, out;
175 path.moveTo(0, 0);
176 path.lineTo(1, 0);
177 path.lineTo(0, 3);
178 path.lineTo(0, 0);
179 path.close();
180 path.moveTo(3, 0);
181 path.lineTo(0, 3);
182 path.lineTo(1, 1);
183 path.lineTo(3, 0);
184 path.close();
185 simplify(path, true, out);
186 comparePaths(path, out);
187}
188
189static void testSimplifyTriangle14() {
190 SkPath path, out;
191 path.moveTo(0, 0);
192 path.lineTo(1, 0);
193 path.lineTo(0, 1);
194 path.lineTo(0, 0);
195 path.close();
196 path.moveTo(0, 0);
197 path.lineTo(1, 0);
198 path.lineTo(0, 1);
199 path.lineTo(0, 0);
200 path.close();
201 simplify(path, true, out);
202 comparePaths(path, out);
203}
204
205static void testSimplifyTriangle15() {
206 SkPath path, out;
207 path.setFillType(SkPath::kEvenOdd_FillType);
208 path.moveTo(0, 0);
209 path.lineTo(0, 1);
210 path.lineTo(1, 2);
211 path.close();
212 path.moveTo(0, 0);
213 path.lineTo(0, 1);
214 path.lineTo(2, 2);
215 path.close();
216 simplify(path, true, out);
217 comparePaths(path, out);
218}
219
220static void testSimplifyTriangle16() {
221 SkPath path, out;
222 path.moveTo(0, 0);
223 path.lineTo(0, 1);
224 path.lineTo(1, 2);
225 path.close();
226 path.moveTo(0, 0);
227 path.lineTo(0, 1);
228 path.lineTo(1, 3);
229 path.close();
230 simplify(path, true, out);
231 comparePaths(path, out);
232}
233
234static void testSimplifyTriangle17() {
235 SkPath path, out;
236 path.moveTo(0, 0);
237 path.lineTo(0, 1);
238 path.lineTo(1, 2);
239 path.close();
240 path.moveTo(0, 0);
241 path.lineTo(1, 3);
242 path.lineTo(0, 1);
243 path.close();
244 simplify(path, true, out);
245 comparePaths(path, out);
246}
247
248static void testSimplifyWindingParallelogram() {
249 SkPath path, out;
250 path.setFillType(SkPath::kWinding_FillType);
251 path.moveTo(20,10); // parallelogram _
252 path.lineTo(30,30); // \ \ .
253 path.lineTo(40,30); // \_\ .
254 path.lineTo(30,10);
255 path.close();
256 path.moveTo(20,10); // parallelogram _
257 path.lineTo(10,30); // / /
258 path.lineTo(20,30); // /_/
259 path.lineTo(30,10);
260 path.close();
261 simplify(path, true, out); // expect _
262 comparePaths(path, out); // / \ .
263} // /___\ .
264
265static void testSimplifyXorParallelogram() {
266 SkPath path, out;
267 path.setFillType(SkPath::kEvenOdd_FillType);
268 path.moveTo(20,10); // parallelogram _
269 path.lineTo(30,30); // \ \ .
270 path.lineTo(40,30); // \_\ .
271 path.lineTo(30,10);
272 path.close();
273 path.moveTo(20,10); // parallelogram _
274 path.lineTo(10,30); // / /
275 path.lineTo(20,30); // /_/
276 path.lineTo(30,10);
277 path.close();
278 simplify(path, true, out); // expect _
279 comparePaths(path, out); // \ /
280} //
281
282static void testSimplifyTriangle2() {
283 SkPath path, out;
284 path.setFillType(SkPath::kWinding_FillType);
285 path.moveTo(10,10); // triangle |\ .
286 path.lineTo(10,30); // |_\ .
287 path.lineTo(20,30);
288 path.close();
289 path.moveTo(10,10); // triangle _
290 path.lineTo(20,10); // \ |
291 path.lineTo(20,30); // \|
292 path.close(); // _
293 simplify(path, true, out); // expect | |
294 comparePaths(path, out); // |_|
295}
296
297static void testSimplifyNondegenerate4x4Triangles() {
298 char pathStr[1024];
299 bzero(pathStr, sizeof(pathStr));
300 for (int a = 0; a < 15; ++a) {
301 int ax = a & 0x03;
302 int ay = a >> 2;
303 for (int b = a + 1; b < 16; ++b) {
304 int bx = b & 0x03;
305 int by = b >> 2;
306 for (int c = a + 1; c < 16; ++c) {
307 if (b == c) {
308 continue;
309 }
310 int cx = c & 0x03;
311 int cy = c >> 2;
312 if ((bx - ax) * (cy - ay) == (by - ay) * (cx - ax)) {
313 continue;
314 }
315 for (int d = 0; d < 15; ++d) {
316 int dx = d & 0x03;
317 int dy = d >> 2;
318 for (int e = d + 1; e < 16; ++e) {
319 int ex = e & 0x03;
320 int ey = e >> 2;
321 for (int f = d + 1; f < 16; ++f) {
322 if (e == f) {
323 continue;
324 }
325 int fx = f & 0x03;
326 int fy = f >> 2;
327 if ((ex - dx) * (fy - dy) == (ey - dy) * (fx - dx)) {
328 continue;
329 }
330 SkPath path, out;
331 path.setFillType(SkPath::kWinding_FillType);
332 path.moveTo(ax, ay);
333 path.lineTo(bx, by);
334 path.lineTo(cx, cy);
335 path.close();
336 path.moveTo(dx, dy);
337 path.lineTo(ex, ey);
338 path.lineTo(fx, fy);
339 path.close();
340 if (1) {
341 char* str = pathStr;
342 str += sprintf(str, " path.moveTo(%d, %d);\n", ax, ay);
343 str += sprintf(str, " path.lineTo(%d, %d);\n", bx, by);
344 str += sprintf(str, " path.lineTo(%d, %d);\n", cx, cy);
345 str += sprintf(str, " path.close();\n");
346 str += sprintf(str, " path.moveTo(%d, %d);\n", dx, dy);
347 str += sprintf(str, " path.lineTo(%d, %d);\n", ex, ey);
348 str += sprintf(str, " path.lineTo(%d, %d);\n", fx, fy);
349 str += sprintf(str, " path.close();");
350 }
351 simplify(path, true, out);
352 comparePaths(path, out);
353 path.setFillType(SkPath::kEvenOdd_FillType);
354 simplify(path, true, out);
355 comparePaths(path, out);
356 }
357 }
358 }
359 }
360 }
361 }
362}
363
364static void testPathTriangleRendering() {
365 SkPath one, two;
366 one.moveTo(0, 0);
367 one.lineTo(3, 3);
368 one.lineTo(0, 3);
369 one.lineTo(1, 2);
370 one.close();
371 for (float x = .1f; x <= 2.9f; x += .1f) {
372 SkDebugf("%s x=%g\n", __FUNCTION__, x);
373 two.moveTo(0, 0);
374 two.lineTo(x, x);
375 two.lineTo(3, 3);
376 two.lineTo(0, 3);
377 two.lineTo(1, 2);
378 two.close();
379 comparePaths(one, two);
380 two.reset();
381 }
382}
383
384static void (*simplifyTests[])() = {
385 testSimplifyTriangle17,
386 testSimplifyTriangle16,
387 testSimplifyTriangle15,
388 testSimplifyTriangle14,
389 testSimplifyTriangle13,
390 testSimplifyTriangle12,
391 testSimplifyTriangle11,
392 testSimplifyTriangle10,
393 testSimplifyTriangle7,
394 testSimplifyTriangle9,
395 testSimplifyTriangle8,
396 testSimplifyTriangle6,
397 testSimplifyTriangle5,
398 testSimplifyTriangle4,
399 testSimplifyTriangle3,
400 testSimplifyTriangle,
401 testSimplifyTriangle2,
402 testSimplifyWindingParallelogram,
403 testSimplifyXorParallelogram,
404 testSimplifyNondegenerate4x4Triangles,
405 testPathTriangleRendering,
406};
407
408static size_t simplifyTestsCount = sizeof(simplifyTests) / sizeof(simplifyTests[0]);
409
410static void (*firstTest)() = 0;
411
412void SimplifyPolygonPaths_Test() {
413 size_t index = 0;
414 if (firstTest) {
415 while (index < simplifyTestsCount && simplifyTests[index] != firstTest) {
416 ++index;
417 }
418 }
419 for ( ; index < simplifyTestsCount; ++index) {
420 (*simplifyTests[index])();
421 }
422}
423