Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
PolyUtilsTest.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2018 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 */
11#include "tests/Test.h"
12
13#include <cstdint>
14
15#if !defined(SK_ENABLE_OPTIMIZE_SIZE)
16
17DEF_TEST(PolyUtils, reporter) {
18
20 // init simple index map
21 uint16_t indexMap[1024];
22 for (int i = 0; i < 1024; ++i) {
23 indexMap[i] = i;
24 }
25 SkTDArray<uint16_t> triangleIndices;
26
27 // skinny triangle
28 *poly.append() = SkPoint::Make(-100, 55);
29 *poly.append() = SkPoint::Make(100, 55);
30 *poly.append() = SkPoint::Make(102.5f, 54.330127f);
35 &triangleIndices));
36
37 // switch winding
38 poly[2].set(102.5f, 55.330127f);
42 triangleIndices.clear();
44 &triangleIndices));
45
46 // make degenerate
47 poly[2].set(100 + 2.5f, 55);
49 // TODO: should these fail?
52 triangleIndices.clear();
54 &triangleIndices));
55
56 // giant triangle
57 poly[0].set(-1.0e+37f, 1.0e+37f);
58 poly[1].set(1.0e+37f, 1.0e+37f);
59 poly[2].set(-1.0e+37f, -1.0e+37f);
63 triangleIndices.clear();
65 &triangleIndices));
66
67 // teeny triangle
68 poly[0].set(-1.0e-38f, 1.0e-38f);
69 poly[1].set(-1.0e-38f, -1.0e-38f);
70 poly[2].set(1.0e-38f, 1.0e-38f);
72 // TODO: should these fail?
75 triangleIndices.clear();
77 &triangleIndices));
78
79 // triangle way off in space (relative to size so we don't completely obliterate values)
80 poly[0].set(-100 + 1.0e+9f, 55 - 1.0e+9f);
81 poly[1].set(100 + 1.0e+9f, 55 - 1.0e+9f);
82 poly[2].set(150 + 1.0e+9f, 100 - 1.0e+9f);
86 triangleIndices.clear();
88 &triangleIndices));
89
90 ///////////////////////////////////////////////////////////////////////
91 // round rect
92 poly.clear();
93 *poly.append() = SkPoint::Make(-100, 55);
94 *poly.append() = SkPoint::Make(100, 55);
95 *poly.append() = SkPoint::Make(100 + 2.5f, 50 + 4.330127f);
96 *poly.append() = SkPoint::Make(100 + 3.535534f, 50 + 3.535534f);
97 *poly.append() = SkPoint::Make(100 + 4.330127f, 50 + 2.5f);
98 *poly.append() = SkPoint::Make(105, 50);
99 *poly.append() = SkPoint::Make(105, -50);
100 *poly.append() = SkPoint::Make(100 + 4.330127f, -50 - 2.5f);
101 *poly.append() = SkPoint::Make(100 + 3.535534f, -50 - 3.535534f);
102 *poly.append() = SkPoint::Make(100 + 2.5f, -50 - 4.330127f);
103 *poly.append() = SkPoint::Make(100, -55);
104 *poly.append() = SkPoint::Make(-100, -55);
105 *poly.append() = SkPoint::Make(-100 - 2.5f, -50 - 4.330127f);
106 *poly.append() = SkPoint::Make(-100 - 3.535534f, -50 - 3.535534f);
107 *poly.append() = SkPoint::Make(-100 - 4.330127f, -50 - 2.5f);
108 *poly.append() = SkPoint::Make(-105, -50);
109 *poly.append() = SkPoint::Make(-105, 50);
110 *poly.append() = SkPoint::Make(-100 - 4.330127f, 50 + 2.5f);
111 *poly.append() = SkPoint::Make(-100 - 3.535534f, 50 + 3.535534f);
112 *poly.append() = SkPoint::Make(-100 - 2.5f, 50 + 4.330127f);
116 triangleIndices.clear();
118 &triangleIndices));
119
120 // translate far enough to obliterate some low bits
121 for (int i = 0; i < poly.size(); ++i) {
122 poly[i].offset(1.0e+7f, 1.0e+7f);
123 }
125 // Due to floating point error it's no longer convex
128 triangleIndices.clear();
130 &triangleIndices));
131
132 // translate a little farther to create some coincident vertices
133 for (int i = 0; i < poly.size(); ++i) {
134 poly[i].offset(4.0e+7f, 4.0e+7f);
135 }
139 // This can't handle coincident vertices
140 triangleIndices.clear();
142 &triangleIndices));
143
144 // troublesome case -- clipped roundrect
145 poly.clear();
146 *poly.append() = SkPoint::Make(335.928101f, 428.219055f);
147 *poly.append() = SkPoint::Make(330.414459f, 423.034912f);
148 *poly.append() = SkPoint::Make(325.749084f, 417.395508f);
149 *poly.append() = SkPoint::Make(321.931946f, 411.300842f);
150 *poly.append() = SkPoint::Make(318.963074f, 404.750977f);
151 *poly.append() = SkPoint::Make(316.842468f, 397.745850f);
152 *poly.append() = SkPoint::Make(315.570068f, 390.285522f);
153 *poly.append() = SkPoint::Make(315.145966f, 382.369965f);
154 *poly.append() = SkPoint::Make(315.570068f, 374.454346f);
155 *poly.append() = SkPoint::Make(316.842468f, 366.994019f);
156 *poly.append() = SkPoint::Make(318.963074f, 359.988892f);
157 *poly.append() = SkPoint::Make(321.931946f, 353.439056f);
158 *poly.append() = SkPoint::Make(325.749084f, 347.344421f);
159 *poly.append() = SkPoint::Make(330.414459f, 341.705017f);
160 *poly.append() = SkPoint::Make(335.928101f, 336.520813f);
161 *poly.append() = SkPoint::Make(342.289948f, 331.791901f);
162 *poly.append() = SkPoint::Make(377.312134f, 331.791901f);
163 *poly.append() = SkPoint::Make(381.195313f, 332.532593f);
164 *poly.append() = SkPoint::Make(384.464935f, 334.754700f);
165 *poly.append() = SkPoint::Make(386.687042f, 338.024292f);
166 *poly.append() = SkPoint::Make(387.427765f, 341.907532f);
167 *poly.append() = SkPoint::Make(387.427765f, 422.832367f);
168 *poly.append() = SkPoint::Make(386.687042f, 426.715576f);
169 *poly.append() = SkPoint::Make(384.464935f, 429.985168f);
170 *poly.append() = SkPoint::Make(381.195313f, 432.207275f);
171 *poly.append() = SkPoint::Make(377.312134f, 432.947998f);
172 *poly.append() = SkPoint::Make(342.289948f, 432.947998f);
176 triangleIndices.clear();
178 &triangleIndices));
179
180 // a star is born
181 poly.clear();
182 *poly.append() = SkPoint::Make(0.0f, -50.0f);
183 *poly.append() = SkPoint::Make(14.43f, -25.0f);
184 *poly.append() = SkPoint::Make(43.30f, -25.0f);
185 *poly.append() = SkPoint::Make(28.86f, 0.0f);
186 *poly.append() = SkPoint::Make(43.30f, 25.0f);
187 *poly.append() = SkPoint::Make(14.43f, 25.0f);
188 *poly.append() = SkPoint::Make(0.0f, 50.0f);
189 *poly.append() = SkPoint::Make(-14.43f, 25.0f);
190 *poly.append() = SkPoint::Make(-43.30f, 25.0f);
191 *poly.append() = SkPoint::Make(-28.86f, 0.0f);
192 *poly.append() = SkPoint::Make(-43.30f, -25.0f);
193 *poly.append() = SkPoint::Make(-14.43f, -25.0f);
197 triangleIndices.clear();
199 &triangleIndices));
200
201 // many spiked star
202 {
203 const SkScalar c = SkIntToScalar(45);
204 const SkScalar r1 = SkIntToScalar(20);
205 const SkScalar r2 = SkIntToScalar(3);
206 const int n = 500;
207 poly.clear();
208 SkScalar rad = 0;
209 const SkScalar drad = SK_ScalarPI / n;
210 for (int i = 0; i < n; i++) {
211 *poly.append() = SkPoint::Make(c + SkScalarCos(rad) * r1, c + SkScalarSin(rad) * r1);
212 rad += drad;
213 *poly.append() = SkPoint::Make(c + SkScalarCos(rad) * r2, c + SkScalarSin(rad) * r2);
214 rad += drad;
215 }
219 triangleIndices.clear();
221 &triangleIndices));
222 }
223
224 // self-intersecting polygon
225 poly.clear();
226 *poly.append() = SkPoint::Make(0.0f, -50.0f);
227 *poly.append() = SkPoint::Make(14.43f, -25.0f);
228 *poly.append() = SkPoint::Make(43.30f, -25.0f);
229 *poly.append() = SkPoint::Make(-28.86f, 0.0f);
230 *poly.append() = SkPoint::Make(43.30f, 25.0f);
231 *poly.append() = SkPoint::Make(14.43f, 25.0f);
232 *poly.append() = SkPoint::Make(0.0f, 50.0f);
233 *poly.append() = SkPoint::Make(-14.43f, 25.0f);
234 *poly.append() = SkPoint::Make(-43.30f, 25.0f);
235 *poly.append() = SkPoint::Make(28.86f, 0.0f);
236 *poly.append() = SkPoint::Make(-43.30f, -25.0f);
237 *poly.append() = SkPoint::Make(-14.43f, -25.0f);
241 triangleIndices.clear();
242 // running this just to make sure it doesn't crash
243 // the fact that it succeeds doesn't mean anything since the input is not simple
245 &triangleIndices));
246
247 // self-intersecting polygon with coincident point
248 poly.clear();
249 *poly.append() = SkPoint::Make(0.0f, 0.0f);
250 *poly.append() = SkPoint::Make(-50, -50);
251 *poly.append() = SkPoint::Make(50, -50);
252 *poly.append() = SkPoint::Make(0.00000001f, -0.00000001f);
253 *poly.append() = SkPoint::Make(-50, 50);
254 *poly.append() = SkPoint::Make(50, 50);
258 triangleIndices.clear();
259 // running this just to make sure it doesn't crash
261 &triangleIndices));
262
263 // self-intersecting polygon with two equal edges
264 poly.clear();
265 *poly.append() = SkPoint::Make(0.0f, 0.0f);
266 *poly.append() = SkPoint::Make(10, 0);
267 *poly.append() = SkPoint::Make(0, 10);
268 *poly.append() = SkPoint::Make(10, 10);
269 *poly.append() = SkPoint::Make(10, 0);
270 *poly.append() = SkPoint::Make(0, 10);
274 triangleIndices.clear();
275 // running this just to make sure it doesn't crash
277 &triangleIndices));
278
279 // absurd self-intersecting polygon
280 poly.clear();
281 *poly.append() = SkPoint::Make(0.0000f, 0.0000f);
282 *poly.append() = SkPoint::Make(-32768.0625f, 0.0000f);
283 *poly.append() = SkPoint::Make(0.0000f, 138.0000f);
284 *poly.append() = SkPoint::Make(3284.8125f, -10411310938997512334153865557442560.0000f);
285 *poly.append() = SkPoint::Make(-32768.7500f, 0.0000f);
286 *poly.append() = SkPoint::Make(138.0000f, 3172.8125f);
287 *poly.append() = SkPoint::Make(0.0000f, -2147485952.0000f);
288 *poly.append() = SkPoint::Make(0.0000f, 170.0000f);
289 *poly.append() = SkPoint::Make(3284.8125f, 0.0000f);
290 *poly.append() = SkPoint::Make(-32768.0625f, 0.0000f);
291 *poly.append() = SkPoint::Make(137.0000f, 4105.6875f);
292 *poly.append() = SkPoint::Make(0.0000f, -32768.0625f);
293 *poly.append() = SkPoint::Make(0.0000f, 138.0000f);
294 *poly.append() = SkPoint::Make(3283.0000f, 0.0000f);
295 *poly.append() = SkPoint::Make(-32768.0625f, 0.0000f);
296 *poly.append() = SkPoint::Make(138.0000f, 3284.8125f);
297 *poly.append() = SkPoint::Make(0.0000f, -32768.0625f);
298 *poly.append() = SkPoint::Make(0.0000f, 138.0000f);
299 *poly.append() = SkPoint::Make(3284.8125f, 0.0000f);
300 *poly.append() = SkPoint::Make(-32768.0625f, 0.0000f);
301 *poly.append() = SkPoint::Make(138.0000f, 821.1250f);
302 *poly.append() = SkPoint::Make(0.0000f, -32768.0625f);
303 *poly.append() = SkPoint::Make(0.0000f, 138.0000f);
304 *poly.append() = SkPoint::Make(3284.8125f, 0.0000f);
305 *poly.append() = SkPoint::Make(-32768.0625f, 0.0000f);
306 *poly.append() = SkPoint::Make(138.0000f, 3284.8125f);
307 *poly.append() = SkPoint::Make(0.0000f, -30897.0625f);
308 *poly.append() = SkPoint::Make(0.0000f, 138.0000f);
309 *poly.append() = SkPoint::Make(3284.8125f, 0.0000f);
310 *poly.append() = SkPoint::Make(-32768.0625f, 0.0000f);
311 *poly.append() = SkPoint::Make(138.0000f, 3284.8125f);
312 *poly.append() = SkPoint::Make(0.0000f, -32768.0625f);
313 *poly.append() = SkPoint::Make(0.0000f, 138.0000f);
314 *poly.append() = SkPoint::Make(3284.5625f, 0.0000f);
315 *poly.append() = SkPoint::Make(-32768.0625f, 0.0000f);
316 *poly.append() = SkPoint::Make(138.0000f, 3284.8125f);
317 *poly.append() = SkPoint::Make(0.0000f, -32768.0625f);
318 *poly.append() = SkPoint::Make(0.0000f, 138.0000f);
319 *poly.append() = SkPoint::Make(3526523879424.0000f, 0.0000f);
320 *poly.append() = SkPoint::Make(-32768.9375f, 0.0000f);
321 *poly.append() = SkPoint::Make(138.0000f, 3284.8125f);
322 *poly.append() = SkPoint::Make(0.0000f, -32768.0625f);
323 *poly.append() = SkPoint::Make(0.0000f, 138.0000f);
324 *poly.append() = SkPoint::Make(3284.8125f, 0.0000f);
325 *poly.append() = SkPoint::Make(-32768.0625f, 0.0000f);
326 *poly.append() = SkPoint::Make(129.0000f, 3284.8125f);
327 *poly.append() = SkPoint::Make(0.0000f, -32768.0625f);
328 *poly.append() = SkPoint::Make(0.0000f, 138.0000f);
329 *poly.append() = SkPoint::Make(3284.8125f, 0.0000f);
330 *poly.append() = SkPoint::Make(-32768.0625f, 0.0000f);
331 *poly.append() = SkPoint::Make(138.0000f, 3284.8125f);
332 *poly.append() = SkPoint::Make(0.0000f, -32768.0625f);
333 *poly.append() = SkPoint::Make(0.0000f, 859832320.0000f);
334 *poly.append() = SkPoint::Make(0.0000f, 0.0000f);
335 *poly.append() = SkPoint::Make(-32768.0625f, 0.0000f);
336 *poly.append() = SkPoint::Make(138.0000f, 3284.8125f);
337 *poly.append() = SkPoint::Make(0.0000f, -32768.0625f);
338 *poly.append() = SkPoint::Make(0.0000f, 129.0000f);
339 *poly.append() = SkPoint::Make(3284.8125f, 0.0000f);
340 *poly.append() = SkPoint::Make(-33554468.0000f, 0.0000f);
341 *poly.append() = SkPoint::Make(138.0000f, 3284.8125f);
342 *poly.append() = SkPoint::Make(0.0000f, -32768.0625f);
343 *poly.append() = SkPoint::Make(0.0000f, 219.0000f);
344 *poly.append() = SkPoint::Make(3220.8125f, 0.0000f);
345 *poly.append() = SkPoint::Make(-35840.0625f, 0.0000f);
346 *poly.append() = SkPoint::Make(0.0000f, 3284.8125f);
347 *poly.append() = SkPoint::Make(0.0000f, -41625560509365411790244566154608640.0000f);
348 *poly.append() = SkPoint::Make(0.0000f, 215282736.0000f);
349 *poly.append() = SkPoint::Make(0.0000f, 0.0000f);
350 *poly.append() = SkPoint::Make(-41625560509365411790244566154608640.0000f, 0.0000f);
351 *poly.append() = SkPoint::Make(215282736.0000f, 0.0000f);
352 *poly.append() = SkPoint::Make(0.0000f, -32768.0625f);
353 *poly.append() = SkPoint::Make(0.0000f, 138.0000f);
354 *poly.append() = SkPoint::Make(0.0000f, 0.0000f);
355 *poly.append() = SkPoint::Make(-32768.0625f, 0.0000f);
356 *poly.append() = SkPoint::Make(138.0000f, 3156.8125f);
357 *poly.append() = SkPoint::Make(0.0000f, -32768.0625f);
358 *poly.append() = SkPoint::Make(0.0000f, 129.0000f);
359 *poly.append() = SkPoint::Make(7433.6875f, 0.0000f);
360 *poly.append() = SkPoint::Make(-32768.0625f, 0.0000f);
361 *poly.append() = SkPoint::Make(138.0000f, 3284.8125f);
362 *poly.append() = SkPoint::Make(0.0000f, -32768.0625f);
363 *poly.append() = SkPoint::Make(0.0000f, 0.0000f);
364 *poly.append() = SkPoint::Make(59324728941049917997056.0000f, 0.0000f);
365 *poly.append() = SkPoint::Make(-35840.0625f, 0.0000f);
366 *poly.append() = SkPoint::Make(0.0000f, 3284.8125f);
367 *poly.append() = SkPoint::Make(0.0000f, -32768.0625f);
368 *poly.append() = SkPoint::Make(0.0000f, 138.0000f);
369 *poly.append() = SkPoint::Make(0.0000f, -32768.0625f);
370 *poly.append() = SkPoint::Make(0.0000f, 0.0000f);
371 *poly.append() = SkPoint::Make(137.0000f, 4105.6875f);
372 *poly.append() = SkPoint::Make(0.0000f, -32768.0625f);
373 *poly.append() = SkPoint::Make(0.0000f, 138.0000f);
374 *poly.append() = SkPoint::Make(3283.0000f, 0.0000f);
375 *poly.append() = SkPoint::Make(-32768.0625f, 0.0000f);
376 *poly.append() = SkPoint::Make(138.0000f, 3284.8125f);
377 *poly.append() = SkPoint::Make(0.0000f, -32768.0625f);
378 *poly.append() = SkPoint::Make(0.0000f, 138.0000f);
379 *poly.append() = SkPoint::Make(3284.8125f, 0.0000f);
380 *poly.append() = SkPoint::Make(0.0000f, 0.0000f);
381 *poly.append() = SkPoint::Make(138.0000f, 3284.5625f);
382 *poly.append() = SkPoint::Make(0.0000f, 0.0000f);
383 *poly.append() = SkPoint::Make(-44882437151680690189392273689542656.0000f, 134217728.0000f);
384 *poly.append() = SkPoint::Make(0.0000f, 0.0000f);
385 *poly.append() = SkPoint::Make(0.0000f, -32768.0625f);
386 *poly.append() = SkPoint::Make(0.0000f, 217055232.0000f);
387 *poly.append() = SkPoint::Make(138.3125f, 0.0000f);
388 *poly.append() = SkPoint::Make(-32768.0625f, 0.0000f);
389 *poly.append() = SkPoint::Make(138.0000f, 3284.5625f);
390 *poly.append() = SkPoint::Make(0.0000f, -32768.0625f);
391 *poly.append() = SkPoint::Make(0.0000f, 138.0000f);
392 *poly.append() = SkPoint::Make(3284.8125f, 0.0000f);
393 *poly.append() = SkPoint::Make(-32768.0625f, 0.0000f);
394 *poly.append() = SkPoint::Make(138.0000f, 3284.8125f);
395 *poly.append() = SkPoint::Make(0.0000f, -32768.0625f);
396 *poly.append() = SkPoint::Make(0.0000f, 138.0000f);
397 *poly.append() = SkPoint::Make(3284.8125f, 0.0000f);
398 *poly.append() = SkPoint::Make(-32768.0625f, 0.0000f);
399 *poly.append() = SkPoint::Make(138.0000f, 3284.8125f);
400 *poly.append() = SkPoint::Make(0.0000f, -32768.0625f);
401 *poly.append() = SkPoint::Make(0.0000f, 138.0000f);
402 *poly.append() = SkPoint::Make(2152988672.0000f, 0.0000f);
403 *poly.append() = SkPoint::Make(-32768.0625f, 0.0000f);
404 *poly.append() = SkPoint::Make(138.0000f, 3284.8125f);
405 *poly.append() = SkPoint::Make(0.0000f, -32768.0625f);
406 *poly.append() = SkPoint::Make(0.0000f, 138.0000f);
407 *poly.append() = SkPoint::Make(3284.8125f, 0.0000f);
408 *poly.append() = SkPoint::Make(-32768.0625f, 0.0000f);
409 *poly.append() = SkPoint::Make(138.0000f, 3284.8125f);
410 *poly.append() = SkPoint::Make(0.0000f, -32768.0625f);
411 *poly.append() = SkPoint::Make(0.0000f, 138.0000f);
412 *poly.append() = SkPoint::Make(3284.8125f, 0.0000f);
413 *poly.append() = SkPoint::Make(-32768.0625f, 0.0000f);
417 triangleIndices.clear();
418 // running this just to make sure it doesn't crash
420 &triangleIndices));
421}
422
423#endif // !defined(SK_ENABLE_OPTIMIZE_SIZE)
reporter
bool SkIsConvexPolygon(const SkPoint *polygonVerts, int polygonSize)
bool SkTriangulateSimplePolygon(const SkPoint *polygonVerts, uint16_t *indexMap, int polygonSize, SkTDArray< uint16_t > *triangleIndices)
bool SkIsSimplePolygon(const SkPoint *polygon, int polygonSize)
int SkGetPolygonWinding(const SkPoint *polygonVerts, int polygonSize)
#define SkScalarSin(radians)
Definition SkScalar.h:45
#define SkIntToScalar(x)
Definition SkScalar.h:57
#define SkScalarCos(radians)
Definition SkScalar.h:46
#define SK_ScalarPI
Definition SkScalar.h:21
#define DEF_TEST(name, reporter)
Definition Test.h:312
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
int size() const
Definition SkTDArray.h:138
T * begin()
Definition SkTDArray.h:150
T * append()
Definition SkTDArray.h:191
void clear()
Definition SkTDArray.h:175
float SkScalar
Definition extension.cpp:12
static constexpr SkPoint Make(float x, float y)