1 /*
2 * Copyright 2017 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 #include "include/core/SkPoint.h"
8 #include "include/private/base/SkTDArray.h"
9 #include "src/utils/SkPolyUtils.h"
10 #include "tests/Test.h"
11
12 #if !defined(SK_ENABLE_OPTIMIZE_SIZE)
13
DEF_TEST(InsetConvexPoly,reporter)14 DEF_TEST(InsetConvexPoly, reporter) {
15 SkTDArray<SkPoint> rrectPoly;
16
17 // round rect
18 *rrectPoly.append() = SkPoint::Make(-100, 55);
19 *rrectPoly.append() = SkPoint::Make(100, 55);
20 *rrectPoly.append() = SkPoint::Make(100 + 2.5f, 50 + 4.330127f);
21 *rrectPoly.append() = SkPoint::Make(100 + 3.535534f, 50 + 3.535534f);
22 *rrectPoly.append() = SkPoint::Make(100 + 4.330127f, 50 + 2.5f);
23 *rrectPoly.append() = SkPoint::Make(105, 50);
24 *rrectPoly.append() = SkPoint::Make(105, -50);
25 *rrectPoly.append() = SkPoint::Make(100 + 4.330127f, -50 - 2.5f);
26 *rrectPoly.append() = SkPoint::Make(100 + 3.535534f, -50 - 3.535534f);
27 *rrectPoly.append() = SkPoint::Make(100 + 2.5f, -50 - 4.330127f);
28 *rrectPoly.append() = SkPoint::Make(100, -55);
29 *rrectPoly.append() = SkPoint::Make(-100, -55);
30 *rrectPoly.append() = SkPoint::Make(-100 - 2.5f, -50 - 4.330127f);
31 *rrectPoly.append() = SkPoint::Make(-100 - 3.535534f, -50 - 3.535534f);
32 *rrectPoly.append() = SkPoint::Make(-100 - 4.330127f, -50 - 2.5f);
33 *rrectPoly.append() = SkPoint::Make(-105, -50);
34 *rrectPoly.append() = SkPoint::Make(-105, 50);
35 *rrectPoly.append() = SkPoint::Make(-100 - 4.330127f, 50 + 2.5f);
36 *rrectPoly.append() = SkPoint::Make(-100 - 3.535534f, 50 + 3.535534f);
37 *rrectPoly.append() = SkPoint::Make(-100 - 2.5f, 50 + 4.330127f);
38 REPORTER_ASSERT(reporter, SkIsConvexPolygon(rrectPoly.begin(), rrectPoly.size()));
39
40 // inset a little
41 SkTDArray<SkPoint> insetPoly;
42 bool result = SkInsetConvexPolygon(rrectPoly.begin(), rrectPoly.size(), 3, &insetPoly);
43 REPORTER_ASSERT(reporter, result);
44 REPORTER_ASSERT(reporter, SkIsConvexPolygon(insetPoly.begin(), insetPoly.size()));
45
46 // inset to rect
47 result = SkInsetConvexPolygon(rrectPoly.begin(), rrectPoly.size(), 10, &insetPoly);
48 REPORTER_ASSERT(reporter, result);
49 REPORTER_ASSERT(reporter, SkIsConvexPolygon(insetPoly.begin(), insetPoly.size()));
50 REPORTER_ASSERT(reporter, insetPoly.size() == 4);
51 if (insetPoly.size() == 4) {
52 REPORTER_ASSERT(reporter, insetPoly[0].equals(-95, 45));
53 REPORTER_ASSERT(reporter, insetPoly[1].equals(95, 45));
54 REPORTER_ASSERT(reporter, insetPoly[2].equals(95, -45));
55 REPORTER_ASSERT(reporter, insetPoly[3].equals(-95, -45));
56 }
57
58 // just to full inset
59 // fails, but outputs a line segment
60 result = SkInsetConvexPolygon(rrectPoly.begin(), rrectPoly.size(), 55, &insetPoly);
61 REPORTER_ASSERT(reporter, !result);
62 REPORTER_ASSERT(reporter, !SkIsConvexPolygon(insetPoly.begin(), insetPoly.size()));
63 REPORTER_ASSERT(reporter, insetPoly.size() == 2);
64 if (insetPoly.size() == 2) {
65 REPORTER_ASSERT(reporter, insetPoly[0].equals(-50, 0));
66 REPORTER_ASSERT(reporter, insetPoly[1].equals(50, 0));
67 }
68
69 // past full inset
70 result = SkInsetConvexPolygon(rrectPoly.begin(), rrectPoly.size(), 75, &insetPoly);
71 REPORTER_ASSERT(reporter, !result);
72 REPORTER_ASSERT(reporter, insetPoly.size() == 1);
73
74 // troublesome case
75 SkTDArray<SkPoint> clippedRRectPoly;
76 *clippedRRectPoly.append() = SkPoint::Make(335.928101f, 428.219055f);
77 *clippedRRectPoly.append() = SkPoint::Make(330.414459f, 423.034912f);
78 *clippedRRectPoly.append() = SkPoint::Make(325.749084f, 417.395508f);
79 *clippedRRectPoly.append() = SkPoint::Make(321.931946f, 411.300842f);
80 *clippedRRectPoly.append() = SkPoint::Make(318.963074f, 404.750977f);
81 *clippedRRectPoly.append() = SkPoint::Make(316.842468f, 397.745850f);
82 *clippedRRectPoly.append() = SkPoint::Make(315.570068f, 390.285522f);
83 *clippedRRectPoly.append() = SkPoint::Make(315.145966f, 382.369965f);
84 *clippedRRectPoly.append() = SkPoint::Make(315.570068f, 374.454346f);
85 *clippedRRectPoly.append() = SkPoint::Make(316.842468f, 366.994019f);
86 *clippedRRectPoly.append() = SkPoint::Make(318.963074f, 359.988892f);
87 *clippedRRectPoly.append() = SkPoint::Make(321.931946f, 353.439056f);
88 *clippedRRectPoly.append() = SkPoint::Make(325.749084f, 347.344421f);
89 *clippedRRectPoly.append() = SkPoint::Make(330.414459f, 341.705017f);
90 *clippedRRectPoly.append() = SkPoint::Make(335.928101f, 336.520813f);
91 *clippedRRectPoly.append() = SkPoint::Make(342.289948f, 331.791901f);
92 *clippedRRectPoly.append() = SkPoint::Make(377.312134f, 331.791901f);
93 *clippedRRectPoly.append() = SkPoint::Make(381.195313f, 332.532593f);
94 *clippedRRectPoly.append() = SkPoint::Make(384.464935f, 334.754700f);
95 *clippedRRectPoly.append() = SkPoint::Make(386.687042f, 338.024292f);
96 *clippedRRectPoly.append() = SkPoint::Make(387.427765f, 341.907532f);
97 *clippedRRectPoly.append() = SkPoint::Make(387.427765f, 422.832367f);
98 *clippedRRectPoly.append() = SkPoint::Make(386.687042f, 426.715576f);
99 *clippedRRectPoly.append() = SkPoint::Make(384.464935f, 429.985168f);
100 *clippedRRectPoly.append() = SkPoint::Make(381.195313f, 432.207275f);
101 *clippedRRectPoly.append() = SkPoint::Make(377.312134f, 432.947998f);
102 *clippedRRectPoly.append() = SkPoint::Make(342.289948f, 432.947998f);
103 REPORTER_ASSERT(reporter, SkIsConvexPolygon(clippedRRectPoly.begin(),
104 clippedRRectPoly.size()));
105
106 result = SkInsetConvexPolygon(clippedRRectPoly.begin(), clippedRRectPoly.size(), 32.3699417f,
107 &insetPoly);
108 REPORTER_ASSERT(reporter, result);
109 REPORTER_ASSERT(reporter, SkIsConvexPolygon(insetPoly.begin(), insetPoly.size()));
110 }
111
112 #endif // !defined(SK_ENABLE_OPTIMIZE_SIZE)
113