aboutsummaryrefslogtreecommitdiffhomepage
path: root/gm/concavepaths.cpp
blob: 50c1abfebbfe62d6d509f58d2ab803014c3bb6a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "gm.h"
#include "SkCanvas.h"
#include "SkPath.h"

namespace {
// Concave test
void test_concave(SkCanvas* canvas, const SkPaint& paint) {
    SkPath path;
    canvas->translate(0, 0);
    path.moveTo(SkIntToScalar(20), SkIntToScalar(20));
    path.lineTo(SkIntToScalar(80), SkIntToScalar(20));
    path.lineTo(SkIntToScalar(30), SkIntToScalar(30));
    path.lineTo(SkIntToScalar(20), SkIntToScalar(80));
    canvas->drawPath(path, paint);
}

// Reverse concave test
void test_reverse_concave(SkCanvas* canvas, const SkPaint& paint) {
    SkPath path;
    canvas->save();
    canvas->translate(100, 0);
    path.moveTo(SkIntToScalar(20), SkIntToScalar(20));
    path.lineTo(SkIntToScalar(20), SkIntToScalar(80));
    path.lineTo(SkIntToScalar(30), SkIntToScalar(30));
    path.lineTo(SkIntToScalar(80), SkIntToScalar(20));
    canvas->drawPath(path, paint);
    canvas->restore();
}

// Bowtie (intersection)
void test_bowtie(SkCanvas* canvas, const SkPaint& paint) {
    SkPath path;
    canvas->save();
    canvas->translate(200, 0);
    path.moveTo(SkIntToScalar(20), SkIntToScalar(20));
    path.lineTo(SkIntToScalar(80), SkIntToScalar(80));
    path.lineTo(SkIntToScalar(80), SkIntToScalar(20));
    path.lineTo(SkIntToScalar(20), SkIntToScalar(80));
    canvas->drawPath(path, paint);
    canvas->restore();
}

// "fake" bowtie (concave, but no intersection)
void test_fake_bowtie(SkCanvas* canvas, const SkPaint& paint) {
    SkPath path;
    canvas->save();
    canvas->translate(300, 0);
    path.moveTo(SkIntToScalar(20), SkIntToScalar(20));
    path.lineTo(SkIntToScalar(50), SkIntToScalar(40));
    path.lineTo(SkIntToScalar(80), SkIntToScalar(20));
    path.lineTo(SkIntToScalar(80), SkIntToScalar(80));
    path.lineTo(SkIntToScalar(50), SkIntToScalar(60));
    path.lineTo(SkIntToScalar(20), SkIntToScalar(80));
    canvas->drawPath(path, paint);
    canvas->restore();
}

// Bowtie with a smaller right hand lobe. The outer vertex of the left hand
// lobe intrudes into the interior of the right hand lobe.
void test_intruding_vertex(SkCanvas* canvas, const SkPaint& paint) {
    SkPath path;
    canvas->save();
    canvas->translate(400, 0);
    path.setIsVolatile(true);
    path.moveTo(20, 20);
    path.lineTo(50, 50);
    path.lineTo(68, 20);
    path.lineTo(68, 80);
    path.lineTo(50, 50);
    path.lineTo(20, 80);
    canvas->drawPath(path, paint);
    canvas->restore();
}

// A shape with an edge that becomes inverted on AA stroking and that also contains
// a repeated start/end vertex.
void test_inversion_repeat_vertex(SkCanvas* canvas, const SkPaint& paint) {
    SkPath path;
    canvas->save();
    canvas->translate(400, 100);
    path.setIsVolatile(true);
    path.moveTo(80,     50);
    path.lineTo(40,     80);
    path.lineTo(60,     20);
    path.lineTo(20,     20);
    path.lineTo(39.99f, 80);
    path.lineTo(80,     50);
    canvas->drawPath(path, paint);
    canvas->restore();
}

// Fish test (intersection/concave)
void test_fish(SkCanvas* canvas, const SkPaint& paint) {
    SkPath path;
    canvas->save();
    canvas->translate(0, 100);
    path.moveTo(SkIntToScalar(20), SkIntToScalar(20));
    path.lineTo(SkIntToScalar(80), SkIntToScalar(80));
    path.lineTo(SkIntToScalar(70), SkIntToScalar(50));
    path.lineTo(SkIntToScalar(80), SkIntToScalar(20));
    path.lineTo(SkIntToScalar(20), SkIntToScalar(80));
    path.lineTo(SkIntToScalar(0), SkIntToScalar(50));
    canvas->drawPath(path, paint);
    canvas->restore();
}

// Overlapping "Fast-forward" icon: tests coincidence of inner and outer
// vertices generated by intersection.
void test_fast_forward(SkCanvas* canvas, const SkPaint& paint) {
    SkPath path;
    canvas->save();
    canvas->translate(100, 100);
    path.moveTo(SkIntToScalar(20), SkIntToScalar(20));
    path.lineTo(SkIntToScalar(60), SkIntToScalar(50));
    path.lineTo(SkIntToScalar(20), SkIntToScalar(80));
    path.moveTo(SkIntToScalar(40), SkIntToScalar(20));
    path.lineTo(SkIntToScalar(40), SkIntToScalar(80));
    path.lineTo(SkIntToScalar(80), SkIntToScalar(50));
    canvas->drawPath(path, paint);
    canvas->restore();
}

// Square polygon with a square hole.
void test_hole(SkCanvas* canvas, const SkPaint& paint) {
    SkPath path;
    canvas->save();
    canvas->translate(200, 100);
    path.moveTo(SkIntToScalar(20), SkIntToScalar(20));
    path.lineTo(SkIntToScalar(80), SkIntToScalar(20));
    path.lineTo(SkIntToScalar(80), SkIntToScalar(80));
    path.lineTo(SkIntToScalar(20), SkIntToScalar(80));
    path.moveTo(SkIntToScalar(30), SkIntToScalar(30));
    path.lineTo(SkIntToScalar(30), SkIntToScalar(70));
    path.lineTo(SkIntToScalar(70), SkIntToScalar(70));
    path.lineTo(SkIntToScalar(70), SkIntToScalar(30));
    canvas->drawPath(path, paint);
    canvas->restore();
}

// Star test (self-intersecting)
void test_star(SkCanvas* canvas, const SkPaint& paint) {
    SkPath path;
    canvas->save();
    canvas->translate(300, 100);
    path.moveTo(30, 20);
    path.lineTo(50, 80);
    path.lineTo(70, 20);
    path.lineTo(20, 57);
    path.lineTo(80, 57);
    path.close();
    canvas->drawPath(path, paint);
    canvas->restore();
}

// Exercise a case where the intersection is below a bottom edge.
void test_twist(SkCanvas* canvas, const SkPaint& paint) {
    SkPath path;
    canvas->save();
    path.moveTo(                 0.5,                    6);
    path.lineTo(5.8070392608642578125, 6.4612660408020019531);
    path.lineTo(-2.9186885356903076172, 2.811046600341796875);
    path.lineTo(0.49999994039535522461, -1.4124038219451904297);
    canvas->translate(420, 220);
    canvas->scale(10, 10);
    canvas->drawPath(path, paint);
    canvas->restore();
}

// Stairstep with repeated vert (intersection)
void test_stairstep(SkCanvas* canvas, const SkPaint& paint) {
    SkPath path;
    canvas->save();
    canvas->translate(0, 200);
    path.moveTo(SkIntToScalar(50), SkIntToScalar(50));
    path.lineTo(SkIntToScalar(50), SkIntToScalar(20));
    path.lineTo(SkIntToScalar(80), SkIntToScalar(20));
    path.lineTo(SkIntToScalar(50), SkIntToScalar(50));
    path.lineTo(SkIntToScalar(20), SkIntToScalar(50));
    path.lineTo(SkIntToScalar(20), SkIntToScalar(80));
    canvas->drawPath(path, paint);
    canvas->restore();
}

void test_stairstep2(SkCanvas* canvas, const SkPaint& paint) {
    SkPath path;
    canvas->save();
    canvas->translate(100, 200);
    path.moveTo(20, 60);
    path.lineTo(35, 80);
    path.lineTo(50, 60);
    path.lineTo(65, 80);
    path.lineTo(80, 60);
    canvas->drawPath(path, paint);
    canvas->restore();
}

// Overlapping segments
void test_overlapping(SkCanvas* canvas, const SkPaint& paint) {
    SkPath path;
    canvas->save();
    canvas->translate(200, 200);
    path.moveTo(SkIntToScalar(20), SkIntToScalar(80));
    path.lineTo(SkIntToScalar(80), SkIntToScalar(80));
    path.lineTo(SkIntToScalar(80), SkIntToScalar(20));
    path.lineTo(SkIntToScalar(80), SkIntToScalar(30));
    canvas->drawPath(path, paint);
    canvas->restore();
}

// Two "island" triangles inside a containing rect.
// This exercises the partnering code in the tessellator.
void test_partners(SkCanvas* canvas, const SkPaint& paint) {
    SkPath path;
    canvas->save();
    canvas->translate(300, 200);
    path.moveTo(20, 80);
    path.lineTo(80, 80);
    path.lineTo(80, 20);
    path.lineTo(20, 20);
    path.moveTo(30, 30);
    path.lineTo(45, 50);
    path.lineTo(30, 70);
    path.moveTo(70, 30);
    path.lineTo(70, 70);
    path.lineTo(55, 50);
    canvas->drawPath(path, paint);
    canvas->restore();
}

// A split edge causes one half to be merged to zero winding (destroyed).
// Test that the other half of the split doesn't also get zero winding.
void test_winding_merged_to_zero(SkCanvas* canvas, const SkPaint& paint) {
    SkPath path;
    canvas->save();
    canvas->translate(400, 350);
    path.moveTo(20, 80);
    path.moveTo(70,  -0.000001f);
    path.lineTo(70,   0.0);
    path.lineTo(60, -30.0);
    path.lineTo(40,  20.0);
    path.moveTo(50,  50.0);
    path.lineTo(50, -50.0);
    path.lineTo(10,  50.0);
    canvas->drawPath(path, paint);
    canvas->restore();
}

// Monotone test 1 (point in the middle)
void test_monotone_1(SkCanvas* canvas, const SkPaint& paint) {
    SkPath path;
    canvas->save();
    canvas->translate(0, 300);
    path.moveTo(SkIntToScalar(20), SkIntToScalar(20));
    path.quadTo(SkIntToScalar(20), SkIntToScalar(50),
                SkIntToScalar(80), SkIntToScalar(50));
    path.quadTo(SkIntToScalar(20), SkIntToScalar(50),
                SkIntToScalar(20), SkIntToScalar(80));
    canvas->drawPath(path, paint);
    canvas->restore();
}

// Monotone test 2 (point at the top)
void test_monotone_2(SkCanvas* canvas, const SkPaint& paint) {
    SkPath path;
    canvas->save();
    canvas->translate(100, 300);
    path.moveTo(SkIntToScalar(20), SkIntToScalar(20));
    path.lineTo(SkIntToScalar(80), SkIntToScalar(30));
    path.quadTo(SkIntToScalar(20), SkIntToScalar(20),
                SkIntToScalar(20), SkIntToScalar(80));
    canvas->drawPath(path, paint);
    canvas->restore();
}

// Monotone test 3 (point at the bottom)
void test_monotone_3(SkCanvas* canvas, const SkPaint& paint) {
    SkPath path;
    canvas->save();
    canvas->translate(200, 300);
    path.moveTo(SkIntToScalar(20), SkIntToScalar(80));
    path.lineTo(SkIntToScalar(80), SkIntToScalar(70));
    path.quadTo(SkIntToScalar(20), SkIntToScalar(80),
                SkIntToScalar(20), SkIntToScalar(20));
    canvas->drawPath(path, paint);
    canvas->restore();
}

// Monotone test 4 (merging of two monotones)
void test_monotone_4(SkCanvas* canvas, const SkPaint& paint) {
    SkPath path;
    canvas->save();
    canvas->translate(300, 300);
    path.moveTo(80, 25);
    path.lineTo(50, 39);
    path.lineTo(20, 25);
    path.lineTo(40, 45);
    path.lineTo(70, 50);
    path.lineTo(80, 80);
    canvas->drawPath(path, paint);
    canvas->restore();
}

// Monotone test 5 (aborted merging of two monotones)
void test_monotone_5(SkCanvas* canvas, const SkPaint& paint) {
    SkPath path;
    canvas->save();
    canvas->translate(0, 400);
    path.moveTo(50, 20);
    path.lineTo(80, 80);
    path.lineTo(50, 50);
    path.lineTo(20, 80);
    canvas->drawPath(path, paint);
    canvas->restore();
}
// Degenerate intersection test
void test_degenerate(SkCanvas* canvas, const SkPaint& paint) {
    SkPath path;
    canvas->save();
    canvas->translate(100, 400);
    path.moveTo(50, 20);
    path.lineTo(70, 30);
    path.lineTo(20, 50);
    path.moveTo(50, 20);
    path.lineTo(80, 80);
    path.lineTo(50, 80);
    canvas->drawPath(path, paint);
    canvas->restore();
}
// Two triangles with a coincident edge.
void test_coincident_edge(SkCanvas* canvas, const SkPaint& paint) {
    SkPath path;
    canvas->save();
    canvas->translate(200, 400);

    path.moveTo(80, 20);
    path.lineTo(80, 80);
    path.lineTo(20, 80);

    path.moveTo(20, 20);
    path.lineTo(80, 80);
    path.lineTo(20, 80);

    canvas->drawPath(path, paint);
    canvas->restore();
}
// Bowtie with a coincident triangle (one triangle vertex coincident with the
// bowtie's intersection).
void test_bowtie_coincident_triangle(SkCanvas* canvas, const SkPaint& paint) {
    SkPath path;
    canvas->save();
    canvas->translate(300, 400);
    path.moveTo(SkIntToScalar(20), SkIntToScalar(20));
    path.lineTo(SkIntToScalar(80), SkIntToScalar(80));
    path.lineTo(SkIntToScalar(80), SkIntToScalar(20));
    path.lineTo(SkIntToScalar(20), SkIntToScalar(80));
    path.moveTo(SkIntToScalar(50), SkIntToScalar(50));
    path.lineTo(SkIntToScalar(80), SkIntToScalar(20));
    path.lineTo(SkIntToScalar(80), SkIntToScalar(80));
    canvas->drawPath(path, paint);
    canvas->restore();
}

// Coincident edges (big ones first, coincident vert on top).
void test_coincident_edges_1(SkCanvas* canvas, const SkPaint& paint) {
    SkPath path;
    canvas->save();
    canvas->translate(0, 500);
    path.moveTo(SkIntToScalar(20), SkIntToScalar(20));
    path.lineTo(SkIntToScalar(80), SkIntToScalar(80));
    path.lineTo(SkIntToScalar(20), SkIntToScalar(80));
    path.moveTo(SkIntToScalar(20), SkIntToScalar(20));
    path.lineTo(SkIntToScalar(50), SkIntToScalar(50));
    path.lineTo(SkIntToScalar(20), SkIntToScalar(50));
    canvas->drawPath(path, paint);
    canvas->restore();
}
// Coincident edges (small ones first, coincident vert on top).
void test_coincident_edges_2(SkCanvas* canvas, const SkPaint& paint) {
    SkPath path;
    canvas->save();
    canvas->translate(100, 500);
    path.moveTo(SkIntToScalar(20), SkIntToScalar(20));
    path.lineTo(SkIntToScalar(50), SkIntToScalar(50));
    path.lineTo(SkIntToScalar(20), SkIntToScalar(50));
    path.moveTo(SkIntToScalar(20), SkIntToScalar(20));
    path.lineTo(SkIntToScalar(80), SkIntToScalar(80));
    path.lineTo(SkIntToScalar(20), SkIntToScalar(80));
    canvas->drawPath(path, paint);
    canvas->restore();
}
// Coincident edges (small ones first, coincident vert on bottom).
void test_coincident_edges_3(SkCanvas* canvas, const SkPaint& paint) {
    SkPath path;
    canvas->save();
    canvas->translate(200, 500);
    path.moveTo(SkIntToScalar(20), SkIntToScalar(80));
    path.lineTo(SkIntToScalar(20), SkIntToScalar(50));
    path.lineTo(SkIntToScalar(50), SkIntToScalar(50));
    path.moveTo(SkIntToScalar(20), SkIntToScalar(80));
    path.lineTo(SkIntToScalar(20), SkIntToScalar(20));
    path.lineTo(SkIntToScalar(80), SkIntToScalar(20));
    canvas->drawPath(path, paint);
    canvas->restore();
}
// Coincident edges (big ones first, coincident vert on bottom).
void test_coincident_edges_4(SkCanvas* canvas, const SkPaint& paint) {
    SkPath path;
    canvas->save();
    canvas->translate(300, 500);
    path.moveTo(SkIntToScalar(20), SkIntToScalar(80));
    path.lineTo(SkIntToScalar(20), SkIntToScalar(20));
    path.lineTo(SkIntToScalar(80), SkIntToScalar(20));
    path.moveTo(SkIntToScalar(20), SkIntToScalar(80));
    path.lineTo(SkIntToScalar(20), SkIntToScalar(50));
    path.lineTo(SkIntToScalar(50), SkIntToScalar(50));
    canvas->drawPath(path, paint);
    canvas->restore();
}

};

DEF_SIMPLE_GM(concavepaths, canvas, 500, 600) {
    SkPaint paint;

    paint.setAntiAlias(true);
    paint.setStyle(SkPaint::kFill_Style);

    test_concave(canvas, paint);
    test_reverse_concave(canvas, paint);
    test_bowtie(canvas, paint);
    test_fake_bowtie(canvas, paint);
    test_intruding_vertex(canvas, paint);
    test_fish(canvas, paint);
    test_fast_forward(canvas, paint);
    test_hole(canvas, paint);
    test_star(canvas, paint);
    test_twist(canvas, paint);
    test_inversion_repeat_vertex(canvas, paint);
    test_stairstep(canvas, paint);
    test_stairstep2(canvas, paint);
    test_overlapping(canvas, paint);
    test_partners(canvas, paint);
    test_winding_merged_to_zero(canvas, paint);
    test_monotone_1(canvas, paint);
    test_monotone_2(canvas, paint);
    test_monotone_3(canvas, paint);
    test_monotone_4(canvas, paint);
    test_monotone_5(canvas, paint);
    test_degenerate(canvas, paint);
    test_coincident_edge(canvas, paint);
    test_bowtie_coincident_triangle(canvas, paint);
    test_coincident_edges_1(canvas, paint);
    test_coincident_edges_2(canvas, paint);
    test_coincident_edges_3(canvas, paint);
    test_coincident_edges_4(canvas, paint);
}