blob: b6e7d97d34913705c981869c1658b983b6c136c2 [file] [log] [blame]
Jim Van Verth4db18ed2018-04-03 10:00:37 -04001/*
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 */
Mike Kleinc0bd9f92019-04-23 12:05:21 -05007#include "src/utils/SkPolyUtils.h"
8#include "tests/Test.h"
Jim Van Verth4db18ed2018-04-03 10:00:37 -04009
Jim Van Verth4db18ed2018-04-03 10:00:37 -040010DEF_TEST(OffsetSimplePoly, reporter) {
11 SkTDArray<SkPoint> rrectPoly;
12
13 ///////////////////////////////////////////////////////////////////////
14 // Try convex tests first
15
16 // round rect
17 *rrectPoly.push() = SkPoint::Make(-100, 55);
18 *rrectPoly.push() = SkPoint::Make(100, 55);
19 *rrectPoly.push() = SkPoint::Make(100 + 2.5f, 50 + 4.330127f);
20 *rrectPoly.push() = SkPoint::Make(100 + 3.535534f, 50 + 3.535534f);
21 *rrectPoly.push() = SkPoint::Make(100 + 4.330127f, 50 + 2.5f);
22 *rrectPoly.push() = SkPoint::Make(105, 50);
23 *rrectPoly.push() = SkPoint::Make(105, -50);
24 *rrectPoly.push() = SkPoint::Make(100 + 4.330127f, -50 - 2.5f);
25 *rrectPoly.push() = SkPoint::Make(100 + 3.535534f, -50 - 3.535534f);
26 *rrectPoly.push() = SkPoint::Make(100 + 2.5f, -50 - 4.330127f);
27 *rrectPoly.push() = SkPoint::Make(100, -55);
28 *rrectPoly.push() = SkPoint::Make(-100, -55);
29 *rrectPoly.push() = SkPoint::Make(-100 - 2.5f, -50 - 4.330127f);
30 *rrectPoly.push() = SkPoint::Make(-100 - 3.535534f, -50 - 3.535534f);
31 *rrectPoly.push() = SkPoint::Make(-100 - 4.330127f, -50 - 2.5f);
32 *rrectPoly.push() = SkPoint::Make(-105, -50);
33 *rrectPoly.push() = SkPoint::Make(-105, 50);
34 *rrectPoly.push() = SkPoint::Make(-100 - 4.330127f, 50 + 2.5f);
35 *rrectPoly.push() = SkPoint::Make(-100 - 3.535534f, 50 + 3.535534f);
36 *rrectPoly.push() = SkPoint::Make(-100 - 2.5f, 50 + 4.330127f);
Jim Van Vertha5ef3972019-05-01 13:28:07 -040037 SkRect bounds;
38 bounds.setBoundsCheck(rrectPoly.begin(), rrectPoly.count());
39
Jim Van Verth6784ffa2018-07-03 16:12:39 -040040 REPORTER_ASSERT(reporter, SkIsConvexPolygon(rrectPoly.begin(), rrectPoly.count()));
Jim Van Verth4db18ed2018-04-03 10:00:37 -040041
42 // inset a little
43 SkTDArray<SkPoint> offsetPoly;
Jim Van Vertha5ef3972019-05-01 13:28:07 -040044 bool result = SkOffsetSimplePolygon(rrectPoly.begin(), rrectPoly.count(), bounds, 3,
45 &offsetPoly);
Jim Van Verth4db18ed2018-04-03 10:00:37 -040046 REPORTER_ASSERT(reporter, result);
Jim Van Verth6784ffa2018-07-03 16:12:39 -040047 REPORTER_ASSERT(reporter, SkIsConvexPolygon(offsetPoly.begin(), offsetPoly.count()));
Jim Van Verth4db18ed2018-04-03 10:00:37 -040048
49 // inset to rect
Jim Van Vertha5ef3972019-05-01 13:28:07 -040050 result = SkOffsetSimplePolygon(rrectPoly.begin(), rrectPoly.count(), bounds, 10, &offsetPoly);
Jim Van Verth4db18ed2018-04-03 10:00:37 -040051 REPORTER_ASSERT(reporter, result);
Jim Van Verth6784ffa2018-07-03 16:12:39 -040052 REPORTER_ASSERT(reporter, SkIsConvexPolygon(offsetPoly.begin(), offsetPoly.count()));
Jim Van Verth4db18ed2018-04-03 10:00:37 -040053 REPORTER_ASSERT(reporter, offsetPoly.count() == 4);
54 if (offsetPoly.count() == 4) {
55 REPORTER_ASSERT(reporter, offsetPoly[0].equals(-95, 45));
56 REPORTER_ASSERT(reporter, offsetPoly[1].equals(95, 45));
57 REPORTER_ASSERT(reporter, offsetPoly[2].equals(95, -45));
58 REPORTER_ASSERT(reporter, offsetPoly[3].equals(-95, -45));
59 }
60
61 // just to full inset
62 // fails, but outputs a line segment
Jim Van Vertha5ef3972019-05-01 13:28:07 -040063 result = SkOffsetSimplePolygon(rrectPoly.begin(), rrectPoly.count(), bounds, 55, &offsetPoly);
Jim Van Verth4db18ed2018-04-03 10:00:37 -040064 REPORTER_ASSERT(reporter, !result);
Jim Van Verth6784ffa2018-07-03 16:12:39 -040065 REPORTER_ASSERT(reporter, !SkIsConvexPolygon(offsetPoly.begin(), offsetPoly.count()));
Jim Van Verth4db18ed2018-04-03 10:00:37 -040066 REPORTER_ASSERT(reporter, offsetPoly.count() == 2);
67 if (offsetPoly.count() == 2) {
68 REPORTER_ASSERT(reporter, offsetPoly[0].equals(-50, 0));
69 REPORTER_ASSERT(reporter, offsetPoly[1].equals(50, 0));
70 }
71
72 // past full inset
Jim Van Vertha5ef3972019-05-01 13:28:07 -040073 result = SkOffsetSimplePolygon(rrectPoly.begin(), rrectPoly.count(), bounds, 75, &offsetPoly);
Jim Van Verth4db18ed2018-04-03 10:00:37 -040074 REPORTER_ASSERT(reporter, !result);
Jim Van Verth4db18ed2018-04-03 10:00:37 -040075
76 // troublesome case
77 SkTDArray<SkPoint> clippedRRectPoly;
78 *clippedRRectPoly.push() = SkPoint::Make(335.928101f, 428.219055f);
79 *clippedRRectPoly.push() = SkPoint::Make(330.414459f, 423.034912f);
80 *clippedRRectPoly.push() = SkPoint::Make(325.749084f, 417.395508f);
81 *clippedRRectPoly.push() = SkPoint::Make(321.931946f, 411.300842f);
82 *clippedRRectPoly.push() = SkPoint::Make(318.963074f, 404.750977f);
83 *clippedRRectPoly.push() = SkPoint::Make(316.842468f, 397.745850f);
84 *clippedRRectPoly.push() = SkPoint::Make(315.570068f, 390.285522f);
85 *clippedRRectPoly.push() = SkPoint::Make(315.145966f, 382.369965f);
86 *clippedRRectPoly.push() = SkPoint::Make(315.570068f, 374.454346f);
87 *clippedRRectPoly.push() = SkPoint::Make(316.842468f, 366.994019f);
88 *clippedRRectPoly.push() = SkPoint::Make(318.963074f, 359.988892f);
89 *clippedRRectPoly.push() = SkPoint::Make(321.931946f, 353.439056f);
90 *clippedRRectPoly.push() = SkPoint::Make(325.749084f, 347.344421f);
91 *clippedRRectPoly.push() = SkPoint::Make(330.414459f, 341.705017f);
92 *clippedRRectPoly.push() = SkPoint::Make(335.928101f, 336.520813f);
93 *clippedRRectPoly.push() = SkPoint::Make(342.289948f, 331.791901f);
94 *clippedRRectPoly.push() = SkPoint::Make(377.312134f, 331.791901f);
95 *clippedRRectPoly.push() = SkPoint::Make(381.195313f, 332.532593f);
96 *clippedRRectPoly.push() = SkPoint::Make(384.464935f, 334.754700f);
97 *clippedRRectPoly.push() = SkPoint::Make(386.687042f, 338.024292f);
98 *clippedRRectPoly.push() = SkPoint::Make(387.427765f, 341.907532f);
99 *clippedRRectPoly.push() = SkPoint::Make(387.427765f, 422.832367f);
100 *clippedRRectPoly.push() = SkPoint::Make(386.687042f, 426.715576f);
101 *clippedRRectPoly.push() = SkPoint::Make(384.464935f, 429.985168f);
102 *clippedRRectPoly.push() = SkPoint::Make(381.195313f, 432.207275f);
103 *clippedRRectPoly.push() = SkPoint::Make(377.312134f, 432.947998f);
104 *clippedRRectPoly.push() = SkPoint::Make(342.289948f, 432.947998f);
Jim Van Vertha5ef3972019-05-01 13:28:07 -0400105 bounds.setBoundsCheck(clippedRRectPoly.begin(), clippedRRectPoly.count());
106
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400107 REPORTER_ASSERT(reporter, SkIsConvexPolygon(clippedRRectPoly.begin(),
108 clippedRRectPoly.count()));
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400109
Jim Van Vertha5ef3972019-05-01 13:28:07 -0400110 result = SkOffsetSimplePolygon(clippedRRectPoly.begin(), clippedRRectPoly.count(), bounds,
111 32.3699417f, &offsetPoly);
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400112 REPORTER_ASSERT(reporter, result);
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400113 REPORTER_ASSERT(reporter, SkIsConvexPolygon(offsetPoly.begin(), offsetPoly.count()));
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400114
115 ////////////////////////////////////////////////////////////////////////////////
116 // Concave tests
117
118 SkTDArray<SkPoint> starPoly;
119 *starPoly.push() = SkPoint::Make(0.0f, -50.0f);
120 *starPoly.push() = SkPoint::Make(14.43f, -25.0f);
121 *starPoly.push() = SkPoint::Make(43.30f, -25.0f);
122 *starPoly.push() = SkPoint::Make(28.86f, 0.0f);
123 *starPoly.push() = SkPoint::Make(43.30f, 25.0f);
124 *starPoly.push() = SkPoint::Make(14.43f, 25.0f);
125 *starPoly.push() = SkPoint::Make(0.0f, 50.0f);
126 *starPoly.push() = SkPoint::Make(-14.43f, 25.0f);
127 *starPoly.push() = SkPoint::Make(-43.30f, 25.0f);
128 *starPoly.push() = SkPoint::Make(-28.86f, 0.0f);
129 *starPoly.push() = SkPoint::Make(-43.30f, -25.0f);
130 *starPoly.push() = SkPoint::Make(-14.43f, -25.0f);
Jim Van Vertha5ef3972019-05-01 13:28:07 -0400131 bounds.setBoundsCheck(starPoly.begin(), starPoly.count());
132
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400133 REPORTER_ASSERT(reporter, SkIsSimplePolygon(starPoly.begin(), starPoly.count()));
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400134
135 // try a variety of distances
Jim Van Vertha5ef3972019-05-01 13:28:07 -0400136 result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), bounds, 0.1f,
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400137 &offsetPoly);
138 REPORTER_ASSERT(reporter, result);
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400139 REPORTER_ASSERT(reporter, SkIsSimplePolygon(offsetPoly.begin(), offsetPoly.count()));
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400140
Jim Van Vertha5ef3972019-05-01 13:28:07 -0400141 result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), bounds, 5.665f,
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400142 &offsetPoly);
143 REPORTER_ASSERT(reporter, result);
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400144 REPORTER_ASSERT(reporter, SkIsSimplePolygon(offsetPoly.begin(), offsetPoly.count()));
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400145
Jim Van Vertha5ef3972019-05-01 13:28:07 -0400146 result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), bounds, 28,
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400147 &offsetPoly);
148 REPORTER_ASSERT(reporter, result);
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400149 REPORTER_ASSERT(reporter, SkIsSimplePolygon(offsetPoly.begin(), offsetPoly.count()));
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400150
151 // down to a point
Jim Van Vertha5ef3972019-05-01 13:28:07 -0400152 result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), bounds, 28.866f,
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400153 &offsetPoly);
154 REPORTER_ASSERT(reporter, !result);
155
156 // and past
Jim Van Vertha5ef3972019-05-01 13:28:07 -0400157 result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), bounds, 50.5f,
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400158 &offsetPoly);
159 REPORTER_ASSERT(reporter, !result);
160
161 // and now out
Jim Van Vertha5ef3972019-05-01 13:28:07 -0400162 result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), bounds, -0.1f,
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400163 &offsetPoly);
164 REPORTER_ASSERT(reporter, result);
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400165 REPORTER_ASSERT(reporter, SkIsSimplePolygon(offsetPoly.begin(), offsetPoly.count()));
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400166
Jim Van Vertha5ef3972019-05-01 13:28:07 -0400167 result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), bounds, -5.6665f,
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400168 &offsetPoly);
169 REPORTER_ASSERT(reporter, result);
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400170 REPORTER_ASSERT(reporter, SkIsSimplePolygon(offsetPoly.begin(), offsetPoly.count()));
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400171
Jim Van Vertha5ef3972019-05-01 13:28:07 -0400172 result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), bounds, -50,
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400173 &offsetPoly);
174 REPORTER_ASSERT(reporter, result);
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400175 REPORTER_ASSERT(reporter, SkIsSimplePolygon(offsetPoly.begin(), offsetPoly.count()));
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400176
Jim Van Vertha5ef3972019-05-01 13:28:07 -0400177 result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), bounds, -100,
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400178 &offsetPoly);
179 REPORTER_ASSERT(reporter, result);
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400180 REPORTER_ASSERT(reporter, SkIsSimplePolygon(offsetPoly.begin(), offsetPoly.count()));
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400181
182 SkTDArray<SkPoint> intersectingPoly;
183 *intersectingPoly.push() = SkPoint::Make(0.0f, -50.0f);
184 *intersectingPoly.push() = SkPoint::Make(14.43f, -25.0f);
185 *intersectingPoly.push() = SkPoint::Make(43.30f, -25.0f);
186 *intersectingPoly.push() = SkPoint::Make(-28.86f, 0.0f);
187 *intersectingPoly.push() = SkPoint::Make(43.30f, 25.0f);
188 *intersectingPoly.push() = SkPoint::Make(14.43f, 25.0f);
189 *intersectingPoly.push() = SkPoint::Make(0.0f, 50.0f);
190 *intersectingPoly.push() = SkPoint::Make(-14.43f, 25.0f);
191 *intersectingPoly.push() = SkPoint::Make(-43.30f, 25.0f);
192 *intersectingPoly.push() = SkPoint::Make(28.86f, 0.0f);
193 *intersectingPoly.push() = SkPoint::Make(-43.30f, -25.0f);
194 *intersectingPoly.push() = SkPoint::Make(-14.43f, -25.0f);
195
Jim Van Verth8664a1d2018-06-28 16:26:50 -0400196 // SkOffsetSimplePolygon now assumes that the input is simple, so we'll just check for that
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400197 result = SkIsSimplePolygon(intersectingPoly.begin(), intersectingPoly.count());
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400198 REPORTER_ASSERT(reporter, !result);
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400199}