blob: 07ce6c8f8a4e5c58e612cdc2c1faab6873aafcc5 [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
caryclark@google.com4917f172012-03-05 22:01:21 +0000248static void testSimplifyTriangle18() {
249 SkPath path, out;
250 path.moveTo(0, 0);
251 path.lineTo(0, 1);
252 path.lineTo(1, 2);
253 path.close();
254 path.moveTo(1, 0);
255 path.lineTo(0, 1);
256 path.lineTo(0, 3);
257 path.close();
258 simplify(path, true, out);
259 comparePaths(path, out);
260}
261
262static void testSimplifyTriangle19() {
263 SkPath path, out;
264 path.setFillType(SkPath::kEvenOdd_FillType);
265 path.moveTo(0, 0);
266 path.lineTo(0, 1);
267 path.lineTo(3, 2);
268 path.close();
269 path.moveTo(0, 0);
270 path.lineTo(1, 1);
271 path.lineTo(2, 1);
272 path.close();
273 simplify(path, true, out);
274 comparePaths(path, out);
275}
276
277static void testSimplifyTriangle20() {
278 SkPath path, out;
279 path.moveTo(0, 0);
280 path.lineTo(2, 1);
281 path.lineTo(1, 3);
282 path.close();
283 path.moveTo(2, 0);
284 path.lineTo(3, 2);
285 path.lineTo(0, 3);
286 path.close();
287 simplify(path, true, out);
288 comparePaths(path, out);
289}
290
291static void testSimplifyTriangle21() {
292 SkPath path, out;
293 path.moveTo(0, 0);
294 path.lineTo(1, 0);
295 path.lineTo(1, 2);
296 path.close();
297 path.moveTo(2, 0);
298 path.lineTo(2, 1);
299 path.lineTo(0, 3);
300 path.close();
301 simplify(path, true, out);
302 comparePaths(path, out);
303}
304
305static void testSimplifyDegenerateTriangle1() {
306 SkPath path, out;
307 path.moveTo(0, 0);
308 path.lineTo(0, 0);
309 path.lineTo(0, 0);
310 path.close();
311 path.moveTo(0, 0);
312 path.lineTo(0, 0);
313 path.lineTo(0, 0);
314 path.close();
315 simplify(path, true, out);
316 comparePaths(path, out);
317}
318
319static void testSimplifyDegenerateTriangle2() {
320 SkPath path, out;
321 path.moveTo(0, 0);
322 path.lineTo(1, 1);
323 path.lineTo(2, 2);
324 path.close();
325 path.moveTo(1, 0);
326 path.lineTo(2, 2);
327 path.lineTo(3, 3);
328 path.close();
329 simplify(path, true, out);
330 comparePaths(path, out);
331}
332
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000333static void testSimplifyWindingParallelogram() {
334 SkPath path, out;
335 path.setFillType(SkPath::kWinding_FillType);
336 path.moveTo(20,10); // parallelogram _
337 path.lineTo(30,30); // \ \ .
338 path.lineTo(40,30); // \_\ .
339 path.lineTo(30,10);
340 path.close();
341 path.moveTo(20,10); // parallelogram _
342 path.lineTo(10,30); // / /
343 path.lineTo(20,30); // /_/
344 path.lineTo(30,10);
345 path.close();
346 simplify(path, true, out); // expect _
347 comparePaths(path, out); // / \ .
348} // /___\ .
349
350static void testSimplifyXorParallelogram() {
351 SkPath path, out;
352 path.setFillType(SkPath::kEvenOdd_FillType);
353 path.moveTo(20,10); // parallelogram _
354 path.lineTo(30,30); // \ \ .
355 path.lineTo(40,30); // \_\ .
356 path.lineTo(30,10);
357 path.close();
358 path.moveTo(20,10); // parallelogram _
359 path.lineTo(10,30); // / /
360 path.lineTo(20,30); // /_/
361 path.lineTo(30,10);
362 path.close();
363 simplify(path, true, out); // expect _
364 comparePaths(path, out); // \ /
365} //
366
367static void testSimplifyTriangle2() {
368 SkPath path, out;
369 path.setFillType(SkPath::kWinding_FillType);
370 path.moveTo(10,10); // triangle |\ .
371 path.lineTo(10,30); // |_\ .
372 path.lineTo(20,30);
373 path.close();
374 path.moveTo(10,10); // triangle _
375 path.lineTo(20,10); // \ |
376 path.lineTo(20,30); // \|
377 path.close(); // _
378 simplify(path, true, out); // expect | |
379 comparePaths(path, out); // |_|
380}
381
382static void testSimplifyNondegenerate4x4Triangles() {
383 char pathStr[1024];
384 bzero(pathStr, sizeof(pathStr));
385 for (int a = 0; a < 15; ++a) {
386 int ax = a & 0x03;
387 int ay = a >> 2;
388 for (int b = a + 1; b < 16; ++b) {
389 int bx = b & 0x03;
390 int by = b >> 2;
391 for (int c = a + 1; c < 16; ++c) {
392 if (b == c) {
393 continue;
394 }
395 int cx = c & 0x03;
396 int cy = c >> 2;
397 if ((bx - ax) * (cy - ay) == (by - ay) * (cx - ax)) {
398 continue;
399 }
400 for (int d = 0; d < 15; ++d) {
401 int dx = d & 0x03;
402 int dy = d >> 2;
403 for (int e = d + 1; e < 16; ++e) {
404 int ex = e & 0x03;
405 int ey = e >> 2;
406 for (int f = d + 1; f < 16; ++f) {
407 if (e == f) {
408 continue;
409 }
410 int fx = f & 0x03;
411 int fy = f >> 2;
412 if ((ex - dx) * (fy - dy) == (ey - dy) * (fx - dx)) {
413 continue;
414 }
415 SkPath path, out;
416 path.setFillType(SkPath::kWinding_FillType);
417 path.moveTo(ax, ay);
418 path.lineTo(bx, by);
419 path.lineTo(cx, cy);
420 path.close();
421 path.moveTo(dx, dy);
422 path.lineTo(ex, ey);
423 path.lineTo(fx, fy);
424 path.close();
425 if (1) {
426 char* str = pathStr;
427 str += sprintf(str, " path.moveTo(%d, %d);\n", ax, ay);
428 str += sprintf(str, " path.lineTo(%d, %d);\n", bx, by);
429 str += sprintf(str, " path.lineTo(%d, %d);\n", cx, cy);
430 str += sprintf(str, " path.close();\n");
431 str += sprintf(str, " path.moveTo(%d, %d);\n", dx, dy);
432 str += sprintf(str, " path.lineTo(%d, %d);\n", ex, ey);
433 str += sprintf(str, " path.lineTo(%d, %d);\n", fx, fy);
434 str += sprintf(str, " path.close();");
435 }
436 simplify(path, true, out);
437 comparePaths(path, out);
438 path.setFillType(SkPath::kEvenOdd_FillType);
439 simplify(path, true, out);
440 comparePaths(path, out);
441 }
442 }
443 }
444 }
445 }
446 }
447}
448
caryclark@google.com4917f172012-03-05 22:01:21 +0000449static void testSimplifyDegenerate4x4Triangles() {
450 char pathStr[1024];
451 bzero(pathStr, sizeof(pathStr));
452 for (int a = 0; a < 16; ++a) {
453 int ax = a & 0x03;
454 int ay = a >> 2;
455 for (int b = a ; b < 16; ++b) {
456 int bx = b & 0x03;
457 int by = b >> 2;
458 for (int c = a ; c < 16; ++c) {
459 int cx = c & 0x03;
460 int cy = c >> 2;
461 bool abcIsATriangle = (bx - ax) * (cy - ay)
462 != (by - ay) * (cx - ax);
463 for (int d = 0; d < 16; ++d) {
464 int dx = d & 0x03;
465 int dy = d >> 2;
466 for (int e = d ; e < 16; ++e) {
467 int ex = e & 0x03;
468 int ey = e >> 2;
469 for (int f = d ; f < 16; ++f) {
470 int fx = f & 0x03;
471 int fy = f >> 2;
472 if (abcIsATriangle && (ex - dx) * (fy - dy)
473 != (ey - dy) * (fx - dx)) {
474 continue;
475 }
476 SkPath path, out;
477 path.setFillType(SkPath::kWinding_FillType);
478 path.moveTo(ax, ay);
479 path.lineTo(bx, by);
480 path.lineTo(cx, cy);
481 path.close();
482 path.moveTo(dx, dy);
483 path.lineTo(ex, ey);
484 path.lineTo(fx, fy);
485 path.close();
486 if (1) {
487 char* str = pathStr;
488 str += sprintf(str, " path.moveTo(%d, %d);\n", ax, ay);
489 str += sprintf(str, " path.lineTo(%d, %d);\n", bx, by);
490 str += sprintf(str, " path.lineTo(%d, %d);\n", cx, cy);
491 str += sprintf(str, " path.close();\n");
492 str += sprintf(str, " path.moveTo(%d, %d);\n", dx, dy);
493 str += sprintf(str, " path.lineTo(%d, %d);\n", ex, ey);
494 str += sprintf(str, " path.lineTo(%d, %d);\n", fx, fy);
495 str += sprintf(str, " path.close();");
496 }
497 simplify(path, true, out);
498 comparePaths(path, out);
499 path.setFillType(SkPath::kEvenOdd_FillType);
500 simplify(path, true, out);
501 comparePaths(path, out);
502 }
503 }
504 }
505 }
506 }
507 }
508}
509
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000510static void testPathTriangleRendering() {
511 SkPath one, two;
512 one.moveTo(0, 0);
513 one.lineTo(3, 3);
514 one.lineTo(0, 3);
515 one.lineTo(1, 2);
516 one.close();
517 for (float x = .1f; x <= 2.9f; x += .1f) {
518 SkDebugf("%s x=%g\n", __FUNCTION__, x);
519 two.moveTo(0, 0);
520 two.lineTo(x, x);
521 two.lineTo(3, 3);
522 two.lineTo(0, 3);
523 two.lineTo(1, 2);
524 two.close();
525 comparePaths(one, two);
526 two.reset();
527 }
528}
529
530static void (*simplifyTests[])() = {
caryclark@google.com4917f172012-03-05 22:01:21 +0000531 testSimplifyDegenerateTriangle2,
532 testSimplifyDegenerateTriangle1,
533 testSimplifyTriangle21,
534 testSimplifyTriangle20,
535 testSimplifyTriangle19,
536 testSimplifyTriangle18,
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000537 testSimplifyTriangle17,
538 testSimplifyTriangle16,
539 testSimplifyTriangle15,
540 testSimplifyTriangle14,
541 testSimplifyTriangle13,
542 testSimplifyTriangle12,
543 testSimplifyTriangle11,
544 testSimplifyTriangle10,
545 testSimplifyTriangle7,
546 testSimplifyTriangle9,
547 testSimplifyTriangle8,
548 testSimplifyTriangle6,
549 testSimplifyTriangle5,
550 testSimplifyTriangle4,
551 testSimplifyTriangle3,
552 testSimplifyTriangle,
553 testSimplifyTriangle2,
554 testSimplifyWindingParallelogram,
555 testSimplifyXorParallelogram,
caryclark@google.com4917f172012-03-05 22:01:21 +0000556 testSimplifyDegenerate4x4Triangles,
caryclark@google.comcd4421d2012-03-01 19:16:31 +0000557 testSimplifyNondegenerate4x4Triangles,
558 testPathTriangleRendering,
559};
560
561static size_t simplifyTestsCount = sizeof(simplifyTests) / sizeof(simplifyTests[0]);
562
563static void (*firstTest)() = 0;
564
565void SimplifyPolygonPaths_Test() {
566 size_t index = 0;
567 if (firstTest) {
568 while (index < simplifyTestsCount && simplifyTests[index] != firstTest) {
569 ++index;
570 }
571 }
572 for ( ; index < simplifyTestsCount; ++index) {
573 (*simplifyTests[index])();
574 }
575}
576