Debian Codegraph @

gm/concavepaths.cpp

raw
1 /*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "gm/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkPaint.h"
11 #include "include/core/SkPathBuilder.h"
12 #include "include/core/SkScalar.h"
13
14 namespace {
15 // Concave test
16 void test_concave(SkCanvas* canvas, const SkPaint& paint) {
17 canvas->translate(0, 0);
18 canvas->drawPath(SkPath::Polygon({{{20,20}, {80,20}, {30,30}, {20,80}}}, false), paint);
19 }
20
21 // Reverse concave test
22 void test_reverse_concave(SkCanvas* canvas, const SkPaint& paint) {
23 canvas->save();
24 canvas->translate(100, 0);
25 canvas->drawPath(SkPath::Polygon({{{20,20}, {20,80}, {30,30}, {80,20}}}, false), paint);
26 canvas->restore();
27 }
28
29 // Bowtie (intersection)
30 void test_bowtie(SkCanvas* canvas, const SkPaint& paint) {
31 canvas->save();
32 canvas->translate(200, 0);
33 canvas->drawPath(SkPath::Polygon({{{20,20}, {80,80}, {80,20}, {20,80}}}, false), paint);
34 canvas->restore();
35 }
36
37 // "fake" bowtie (concave, but no intersection)
38 void test_fake_bowtie(SkCanvas* canvas, const SkPaint& paint) {
39 canvas->save();
40 canvas->translate(300, 0);
41 canvas->drawPath(SkPath::Polygon({{{20,20}, {50,40}, {80,20}, {80,80}, {50,60}, {20,80}}},
42 false), paint);
43 canvas->restore();
44 }
45
46 // Bowtie with a smaller right hand lobe. The outer vertex of the left hand
47 // lobe intrudes into the interior of the right hand lobe.
48 void test_intruding_vertex(SkCanvas* canvas, const SkPaint& paint) {
49 canvas->save();
50 canvas->translate(400, 0);
51 canvas->drawPath(SkPath::Polygon({{{20,20}, {50,50}, {68,20}, {68,80}, {50,50}, {20,80}}},
52 false, SkPathFillType::kWinding, true), paint);
53 canvas->restore();
54 }
55
56 // A shape with an edge that becomes inverted on AA stroking and that also contains
57 // a repeated start/end vertex.
58 void test_inversion_repeat_vertex(SkCanvas* canvas, const SkPaint& paint) {
59 canvas->save();
60 canvas->translate(400, 100);
61 const SkPoint pts[] = {
62 {80,50}, {40,80}, {60,20}, {20,20}, {39.99f,80}, {80,50},
63 };
64 canvas->drawPath(SkPath::Polygon(pts, false,
65 SkPathFillType::kWinding, true), paint);
66 canvas->restore();
67 }
68
69 // Fish test (intersection/concave)
70 void test_fish(SkCanvas* canvas, const SkPaint& paint) {
71 canvas->save();
72 canvas->translate(0, 100);
73 canvas->drawPath(SkPath::Polygon({{{20,20}, {80,80}, {70,50}, {80,20}, {20,80}, {0,50}}}, false,
74 SkPathFillType::kWinding, true), paint);
75 canvas->restore();
76 }
77
78 // Overlapping "Fast-forward" icon: tests coincidence of inner and outer
79 // vertices generated by intersection.
80 void test_fast_forward(SkCanvas* canvas, const SkPaint& paint) {
81 canvas->save();
82 canvas->translate(100, 100);
83 auto path = SkPathBuilder().addPolygon({{{20,20}, {60,50}, {20,80}}}, false)
84 .addPolygon({{{40,20}, {40,80}, {80,50}}}, false)
85 .detach();
86 canvas->drawPath(path, paint);
87 canvas->restore();
88 }
89
90 // Square polygon with a square hole.
91 void test_hole(SkCanvas* canvas, const SkPaint& paint) {
92 canvas->save();
93 canvas->translate(200, 100);
94 auto path = SkPathBuilder().addPolygon({{{20,20}, {80,20}, {80,80}, {20,80}}}, false)
95 .addPolygon({{{30,30}, {30,70}, {70,70}, {70,30}}}, false)
96 .detach();
97 canvas->drawPath(path, paint);
98 canvas->restore();
99 }
100
101 // Star test (self-intersecting)
102 void test_star(SkCanvas* canvas, const SkPaint& paint) {
103 canvas->save();
104 canvas->translate(300, 100);
105 canvas->drawPath(SkPath::Polygon({{{30,20}, {50,80}, {70,20}, {20,57}, {80,57}}}, false),
106 paint);
107 canvas->restore();
108 }
109
110 // Exercise a case where the intersection is below a bottom edge.
111 void test_twist(SkCanvas* canvas, const SkPaint& paint) {
112 canvas->save();
113 canvas->translate(420, 220);
114 canvas->scale(10, 10);
115 const SkPoint pts[] = {
116 {0.5f, 6},
117 {5.8070392608642578125f, 6.4612660408020019531f},
118 {-2.9186885356903076172f, 2.811046600341796875f},
119 {0.49999994039535522461f, -1.4124038219451904297f},
120 };
121 canvas->drawPath(SkPath::Polygon(pts, false), paint);
122 canvas->restore();
123 }
124
125 // Stairstep with repeated vert (intersection)
126 void test_stairstep(SkCanvas* canvas, const SkPaint& paint) {
127 canvas->save();
128 canvas->translate(0, 200);
129 canvas->drawPath(SkPath::Polygon({{{50,50}, {50,20}, {80,20}, {50,50}, {20,50}, {20,80}}},
130 false),
131 paint);
132 canvas->restore();
133 }
134
135 void test_stairstep2(SkCanvas* canvas, const SkPaint& paint) {
136 canvas->save();
137 canvas->translate(100, 200);
138 canvas->drawPath(SkPath::Polygon({{{20,60}, {35,80}, {50,60}, {65,80}, {80,60}}}, false),
139 paint);
140 canvas->restore();
141 }
142
143 // Overlapping segments
144 void test_overlapping(SkCanvas* canvas, const SkPaint& paint) {
145 canvas->save();
146 canvas->translate(200, 200);
147 canvas->drawPath(SkPath::Polygon({{{20,80}, {80,80}, {80,20}, {80,30}}}, false), paint);
148 canvas->restore();
149 }
150
151 // Two "island" triangles inside a containing rect.
152 // This exercises the partnering code in the tessellator.
153 void test_partners(SkCanvas* canvas, const SkPaint& paint) {
154 canvas->save();
155 canvas->translate(300, 200);
156 auto path = SkPathBuilder().addPolygon({{{20,80}, {80,80}, {80,20}, {20,20}}}, false)
157 .addPolygon({{{30,30}, {45,50}, {30,70}}}, false)
158 .addPolygon({{{70,30}, {70,70}, {55,50}}}, false)
159 .detach();
160 canvas->drawPath(path, paint);
161 canvas->restore();
162 }
163
164 // A split edge causes one half to be merged to zero winding (destroyed).
165 // Test that the other half of the split doesn't also get zero winding.
166 void test_winding_merged_to_zero(SkCanvas* canvas, const SkPaint& paint) {
167 SkPathBuilder path;
168 canvas->save();
169 canvas->translate(400, 350);
170 path.moveTo(20, 80);
171 path.moveTo(70, -0.000001f);
172 path.lineTo(70, 0.0);
173 path.lineTo(60, -30.0);
174 path.lineTo(40, 20.0);
175 path.moveTo(50, 50.0);
176 path.lineTo(50, -50.0);
177 path.lineTo(10, 50.0);
178 canvas->drawPath(path.detach(), paint);
179 canvas->restore();
180 }
181
182 // Monotone test 1 (point in the middle)
183 void test_monotone_1(SkCanvas* canvas, const SkPaint& paint) {
184 SkPathBuilder path;
185 canvas->save();
186 canvas->translate(0, 300);
187 path.moveTo(20, 20);
188 path.quadTo(20, 50, 80, 50);
189 path.quadTo(20, 50, 20, 80);
190 canvas->drawPath(path.detach(), paint);
191 canvas->restore();
192 }
193
194 // Monotone test 2 (point at the top)
195 void test_monotone_2(SkCanvas* canvas, const SkPaint& paint) {
196 SkPathBuilder path;
197 canvas->save();
198 canvas->translate(100, 300);
199 path.moveTo(20, 20);
200 path.lineTo(80, 30);
201 path.quadTo(20, 20, 20, 80);
202 canvas->drawPath(path.detach(), paint);
203 canvas->restore();
204 }
205
206 // Monotone test 3 (point at the bottom)
207 void test_monotone_3(SkCanvas* canvas, const SkPaint& paint) {
208 SkPathBuilder path;
209 canvas->save();
210 canvas->translate(200, 300);
211 path.moveTo(20, 80);
212 path.lineTo(80, 70);
213 path.quadTo(20, 80, 20, 20);
214 canvas->drawPath(path.detach(), paint);
215 canvas->restore();
216 }
217
218 // Monotone test 4 (merging of two monotones)
219 void test_monotone_4(SkCanvas* canvas, const SkPaint& paint) {
220 SkPathBuilder path;
221 canvas->save();
222 canvas->translate(300, 300);
223 path.moveTo(80, 25);
224 path.lineTo(50, 39);
225 path.lineTo(20, 25);
226 path.lineTo(40, 45);
227 path.lineTo(70, 50);
228 path.lineTo(80, 80);
229 canvas->drawPath(path.detach(), paint);
230 canvas->restore();
231 }
232
233 // Monotone test 5 (aborted merging of two monotones)
234 void test_monotone_5(SkCanvas* canvas, const SkPaint& paint) {
235 SkPathBuilder path;
236 canvas->save();
237 canvas->translate(0, 400);
238 path.moveTo(50, 20);
239 path.lineTo(80, 80);
240 path.lineTo(50, 50);
241 path.lineTo(20, 80);
242 canvas->drawPath(path.detach(), paint);
243 canvas->restore();
244 }
245 // Degenerate intersection test
246 void test_degenerate(SkCanvas* canvas, const SkPaint& paint) {
247 SkPathBuilder path;
248 canvas->save();
249 canvas->translate(100, 400);
250 path.moveTo(50, 20);
251 path.lineTo(70, 30);
252 path.lineTo(20, 50);
253 path.moveTo(50, 20);
254 path.lineTo(80, 80);
255 path.lineTo(50, 80);
256 canvas->drawPath(path.detach(), paint);
257 canvas->restore();
258 }
259 // Two triangles with a coincident edge.
260 void test_coincident_edge(SkCanvas* canvas, const SkPaint& paint) {
261 SkPathBuilder path;
262 canvas->save();
263 canvas->translate(200, 400);
264
265 path.moveTo(80, 20);
266 path.lineTo(80, 80);
267 path.lineTo(20, 80);
268
269 path.moveTo(20, 20);
270 path.lineTo(80, 80);
271 path.lineTo(20, 80);
272
273 canvas->drawPath(path.detach(), paint);
274 canvas->restore();
275 }
276 // Bowtie with a coincident triangle (one triangle vertex coincident with the
277 // bowtie's intersection).
278 void test_bowtie_coincident_triangle(SkCanvas* canvas, const SkPaint& paint) {
279 SkPathBuilder path;
280 canvas->save();
281 canvas->translate(300, 400);
282 path.moveTo(20, 20);
283 path.lineTo(80, 80);
284 path.lineTo(80, 20);
285 path.lineTo(20, 80);
286 path.moveTo(50, 50);
287 path.lineTo(80, 20);
288 path.lineTo(80, 80);
289 canvas->drawPath(path.detach(), paint);
290 canvas->restore();
291 }
292
293 // Collinear outer boundary edges. In the edge-AA codepath, this creates an overlap region
294 // which contains a boundary edge. It can't be removed, but it must have the correct winding.
295 void test_collinear_outer_boundary_edge(SkCanvas* canvas, const SkPaint& paint) {
296 SkPathBuilder path;
297 canvas->save();
298 canvas->translate(400, 400);
299 path.moveTo(20, 20);
300 path.lineTo(20, 50);
301 path.lineTo(50, 50);
302 path.moveTo(80, 50);
303 path.lineTo(50, 50);
304 path.lineTo(80, 20);
305 canvas->drawPath(path.detach(), paint);
306 canvas->restore();
307 }
308
309 // Coincident edges (big ones first, coincident vert on top).
310 void test_coincident_edges_1(SkCanvas* canvas, const SkPaint& paint) {
311 SkPathBuilder path;
312 canvas->save();
313 canvas->translate(0, 500);
314 path.moveTo(20, 20);
315 path.lineTo(80, 80);
316 path.lineTo(20, 80);
317 path.moveTo(20, 20);
318 path.lineTo(50, 50);
319 path.lineTo(20, 50);
320 canvas->drawPath(path.detach(), paint);
321 canvas->restore();
322 }
323 // Coincident edges (small ones first, coincident vert on top).
324 void test_coincident_edges_2(SkCanvas* canvas, const SkPaint& paint) {
325 SkPathBuilder path;
326 canvas->save();
327 canvas->translate(100, 500);
328 path.moveTo(20, 20);
329 path.lineTo(50, 50);
330 path.lineTo(20, 50);
331 path.moveTo(20, 20);
332 path.lineTo(80, 80);
333 path.lineTo(20, 80);
334 canvas->drawPath(path.detach(), paint);
335 canvas->restore();
336 }
337 // Coincident edges (small ones first, coincident vert on bottom).
338 void test_coincident_edges_3(SkCanvas* canvas, const SkPaint& paint) {
339 SkPathBuilder path;
340 canvas->save();
341 canvas->translate(200, 500);
342 path.moveTo(20, 80);
343 path.lineTo(20, 50);
344 path.lineTo(50, 50);
345 path.moveTo(20, 80);
346 path.lineTo(20, 20);
347 path.lineTo(80, 20);
348 canvas->drawPath(path.detach(), paint);
349 canvas->restore();
350 }
351 // Coincident edges (big ones first, coincident vert on bottom).
352 void test_coincident_edges_4(SkCanvas* canvas, const SkPaint& paint) {
353 SkPathBuilder path;
354 canvas->save();
355 canvas->translate(300, 500);
356 path.moveTo(20, 80);
357 path.lineTo(20, 20);
358 path.lineTo(80, 20);
359 path.moveTo(20, 80);
360 path.lineTo(20, 50);
361 path.lineTo(50, 50);
362 canvas->drawPath(path.detach(), paint);
363 canvas->restore();
364 }
365
366 } // namespace
367
368 DEF_SIMPLE_GM(concavepaths, canvas, 500, 600) {
369 SkPaint paint;
370
371 paint.setAntiAlias(true);
372 paint.setStyle(SkPaint::kFill_Style);
373
374 test_concave(canvas, paint);
375 test_reverse_concave(canvas, paint);
376 test_bowtie(canvas, paint);
377 test_fake_bowtie(canvas, paint);
378 test_intruding_vertex(canvas, paint);
379 test_fish(canvas, paint);
380 test_fast_forward(canvas, paint);
381 test_hole(canvas, paint);
382 test_star(canvas, paint);
383 test_twist(canvas, paint);
384 test_inversion_repeat_vertex(canvas, paint);
385 test_stairstep(canvas, paint);
386 test_stairstep2(canvas, paint);
387 test_overlapping(canvas, paint);
388 test_partners(canvas, paint);
389 test_winding_merged_to_zero(canvas, paint);
390 test_monotone_1(canvas, paint);
391 test_monotone_2(canvas, paint);
392 test_monotone_3(canvas, paint);
393 test_monotone_4(canvas, paint);
394 test_monotone_5(canvas, paint);
395 test_degenerate(canvas, paint);
396 test_coincident_edge(canvas, paint);
397 test_bowtie_coincident_triangle(canvas, paint);
398 test_collinear_outer_boundary_edge(canvas, paint);
399 test_coincident_edges_1(canvas, paint);
400 test_coincident_edges_2(canvas, paint);
401 test_coincident_edges_3(canvas, paint);
402 test_coincident_edges_4(canvas, paint);
403 }