blob: 0f4f18f2ad4727a248b1eeb2060b9752a0eeeccd [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 */
7#include "Test.h"
Jim Van Verth8664a1d2018-06-28 16:26:50 -04008#include "SkPolyUtils.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 Verth6784ffa2018-07-03 16:12:39 -040037 REPORTER_ASSERT(reporter, SkIsConvexPolygon(rrectPoly.begin(), rrectPoly.count()));
Jim Van Verth4db18ed2018-04-03 10:00:37 -040038
39 // inset a little
40 SkTDArray<SkPoint> offsetPoly;
Jim Van Verth6784ffa2018-07-03 16:12:39 -040041 bool result = SkOffsetSimplePolygon(rrectPoly.begin(), rrectPoly.count(), 3, &offsetPoly);
Jim Van Verth4db18ed2018-04-03 10:00:37 -040042 REPORTER_ASSERT(reporter, result);
Jim Van Verth6784ffa2018-07-03 16:12:39 -040043 REPORTER_ASSERT(reporter, SkIsConvexPolygon(offsetPoly.begin(), offsetPoly.count()));
Jim Van Verth4db18ed2018-04-03 10:00:37 -040044
45 // inset to rect
Jim Van Verth6784ffa2018-07-03 16:12:39 -040046 result = SkOffsetSimplePolygon(rrectPoly.begin(), rrectPoly.count(), 10, &offsetPoly);
Jim Van Verth4db18ed2018-04-03 10:00:37 -040047 REPORTER_ASSERT(reporter, result);
Jim Van Verth6784ffa2018-07-03 16:12:39 -040048 REPORTER_ASSERT(reporter, SkIsConvexPolygon(offsetPoly.begin(), offsetPoly.count()));
Jim Van Verth4db18ed2018-04-03 10:00:37 -040049 REPORTER_ASSERT(reporter, offsetPoly.count() == 4);
50 if (offsetPoly.count() == 4) {
51 REPORTER_ASSERT(reporter, offsetPoly[0].equals(-95, 45));
52 REPORTER_ASSERT(reporter, offsetPoly[1].equals(95, 45));
53 REPORTER_ASSERT(reporter, offsetPoly[2].equals(95, -45));
54 REPORTER_ASSERT(reporter, offsetPoly[3].equals(-95, -45));
55 }
56
57 // just to full inset
58 // fails, but outputs a line segment
Jim Van Verth6784ffa2018-07-03 16:12:39 -040059 result = SkOffsetSimplePolygon(rrectPoly.begin(), rrectPoly.count(), 55, &offsetPoly);
Jim Van Verth4db18ed2018-04-03 10:00:37 -040060 REPORTER_ASSERT(reporter, !result);
Jim Van Verth6784ffa2018-07-03 16:12:39 -040061 REPORTER_ASSERT(reporter, !SkIsConvexPolygon(offsetPoly.begin(), offsetPoly.count()));
Jim Van Verth4db18ed2018-04-03 10:00:37 -040062 REPORTER_ASSERT(reporter, offsetPoly.count() == 2);
63 if (offsetPoly.count() == 2) {
64 REPORTER_ASSERT(reporter, offsetPoly[0].equals(-50, 0));
65 REPORTER_ASSERT(reporter, offsetPoly[1].equals(50, 0));
66 }
67
68 // past full inset
Jim Van Verth6784ffa2018-07-03 16:12:39 -040069 result = SkOffsetSimplePolygon(rrectPoly.begin(), rrectPoly.count(), 75, &offsetPoly);
Jim Van Verth4db18ed2018-04-03 10:00:37 -040070 REPORTER_ASSERT(reporter, !result);
Jim Van Verth4db18ed2018-04-03 10:00:37 -040071
72 // troublesome case
73 SkTDArray<SkPoint> clippedRRectPoly;
74 *clippedRRectPoly.push() = SkPoint::Make(335.928101f, 428.219055f);
75 *clippedRRectPoly.push() = SkPoint::Make(330.414459f, 423.034912f);
76 *clippedRRectPoly.push() = SkPoint::Make(325.749084f, 417.395508f);
77 *clippedRRectPoly.push() = SkPoint::Make(321.931946f, 411.300842f);
78 *clippedRRectPoly.push() = SkPoint::Make(318.963074f, 404.750977f);
79 *clippedRRectPoly.push() = SkPoint::Make(316.842468f, 397.745850f);
80 *clippedRRectPoly.push() = SkPoint::Make(315.570068f, 390.285522f);
81 *clippedRRectPoly.push() = SkPoint::Make(315.145966f, 382.369965f);
82 *clippedRRectPoly.push() = SkPoint::Make(315.570068f, 374.454346f);
83 *clippedRRectPoly.push() = SkPoint::Make(316.842468f, 366.994019f);
84 *clippedRRectPoly.push() = SkPoint::Make(318.963074f, 359.988892f);
85 *clippedRRectPoly.push() = SkPoint::Make(321.931946f, 353.439056f);
86 *clippedRRectPoly.push() = SkPoint::Make(325.749084f, 347.344421f);
87 *clippedRRectPoly.push() = SkPoint::Make(330.414459f, 341.705017f);
88 *clippedRRectPoly.push() = SkPoint::Make(335.928101f, 336.520813f);
89 *clippedRRectPoly.push() = SkPoint::Make(342.289948f, 331.791901f);
90 *clippedRRectPoly.push() = SkPoint::Make(377.312134f, 331.791901f);
91 *clippedRRectPoly.push() = SkPoint::Make(381.195313f, 332.532593f);
92 *clippedRRectPoly.push() = SkPoint::Make(384.464935f, 334.754700f);
93 *clippedRRectPoly.push() = SkPoint::Make(386.687042f, 338.024292f);
94 *clippedRRectPoly.push() = SkPoint::Make(387.427765f, 341.907532f);
95 *clippedRRectPoly.push() = SkPoint::Make(387.427765f, 422.832367f);
96 *clippedRRectPoly.push() = SkPoint::Make(386.687042f, 426.715576f);
97 *clippedRRectPoly.push() = SkPoint::Make(384.464935f, 429.985168f);
98 *clippedRRectPoly.push() = SkPoint::Make(381.195313f, 432.207275f);
99 *clippedRRectPoly.push() = SkPoint::Make(377.312134f, 432.947998f);
100 *clippedRRectPoly.push() = SkPoint::Make(342.289948f, 432.947998f);
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400101 REPORTER_ASSERT(reporter, SkIsConvexPolygon(clippedRRectPoly.begin(),
102 clippedRRectPoly.count()));
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400103
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400104 result = SkOffsetSimplePolygon(clippedRRectPoly.begin(), clippedRRectPoly.count(), 32.3699417f,
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400105 &offsetPoly);
106 REPORTER_ASSERT(reporter, result);
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400107 REPORTER_ASSERT(reporter, SkIsConvexPolygon(offsetPoly.begin(), offsetPoly.count()));
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400108
109 ////////////////////////////////////////////////////////////////////////////////
110 // Concave tests
111
112 SkTDArray<SkPoint> starPoly;
113 *starPoly.push() = SkPoint::Make(0.0f, -50.0f);
114 *starPoly.push() = SkPoint::Make(14.43f, -25.0f);
115 *starPoly.push() = SkPoint::Make(43.30f, -25.0f);
116 *starPoly.push() = SkPoint::Make(28.86f, 0.0f);
117 *starPoly.push() = SkPoint::Make(43.30f, 25.0f);
118 *starPoly.push() = SkPoint::Make(14.43f, 25.0f);
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);
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400125 REPORTER_ASSERT(reporter, SkIsSimplePolygon(starPoly.begin(), starPoly.count()));
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400126
127 // try a variety of distances
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400128 result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), 0.1f,
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400129 &offsetPoly);
130 REPORTER_ASSERT(reporter, result);
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400131 REPORTER_ASSERT(reporter, SkIsSimplePolygon(offsetPoly.begin(), offsetPoly.count()));
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400132
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400133 result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), 5.665f,
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400134 &offsetPoly);
135 REPORTER_ASSERT(reporter, result);
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400136 REPORTER_ASSERT(reporter, SkIsSimplePolygon(offsetPoly.begin(), offsetPoly.count()));
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400137
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400138 result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), 28,
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400139 &offsetPoly);
140 REPORTER_ASSERT(reporter, result);
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400141 REPORTER_ASSERT(reporter, SkIsSimplePolygon(offsetPoly.begin(), offsetPoly.count()));
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400142
143 // down to a point
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400144 result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), 28.866f,
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400145 &offsetPoly);
146 REPORTER_ASSERT(reporter, !result);
147
148 // and past
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400149 result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), 50.5f,
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400150 &offsetPoly);
151 REPORTER_ASSERT(reporter, !result);
152
153 // and now out
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400154 result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), -0.1f,
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400155 &offsetPoly);
156 REPORTER_ASSERT(reporter, result);
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400157 REPORTER_ASSERT(reporter, SkIsSimplePolygon(offsetPoly.begin(), offsetPoly.count()));
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400158
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400159 result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), -5.6665f,
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400160 &offsetPoly);
161 REPORTER_ASSERT(reporter, result);
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400162 REPORTER_ASSERT(reporter, SkIsSimplePolygon(offsetPoly.begin(), offsetPoly.count()));
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400163
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400164 result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), -50,
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400165 &offsetPoly);
166 REPORTER_ASSERT(reporter, result);
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400167 REPORTER_ASSERT(reporter, SkIsSimplePolygon(offsetPoly.begin(), offsetPoly.count()));
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400168
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400169 result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), -100,
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400170 &offsetPoly);
171 REPORTER_ASSERT(reporter, result);
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400172 REPORTER_ASSERT(reporter, SkIsSimplePolygon(offsetPoly.begin(), offsetPoly.count()));
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400173
174 SkTDArray<SkPoint> intersectingPoly;
175 *intersectingPoly.push() = SkPoint::Make(0.0f, -50.0f);
176 *intersectingPoly.push() = SkPoint::Make(14.43f, -25.0f);
177 *intersectingPoly.push() = SkPoint::Make(43.30f, -25.0f);
178 *intersectingPoly.push() = SkPoint::Make(-28.86f, 0.0f);
179 *intersectingPoly.push() = SkPoint::Make(43.30f, 25.0f);
180 *intersectingPoly.push() = SkPoint::Make(14.43f, 25.0f);
181 *intersectingPoly.push() = SkPoint::Make(0.0f, 50.0f);
182 *intersectingPoly.push() = SkPoint::Make(-14.43f, 25.0f);
183 *intersectingPoly.push() = SkPoint::Make(-43.30f, 25.0f);
184 *intersectingPoly.push() = SkPoint::Make(28.86f, 0.0f);
185 *intersectingPoly.push() = SkPoint::Make(-43.30f, -25.0f);
186 *intersectingPoly.push() = SkPoint::Make(-14.43f, -25.0f);
187
Jim Van Verth8664a1d2018-06-28 16:26:50 -0400188 // SkOffsetSimplePolygon now assumes that the input is simple, so we'll just check for that
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400189 result = SkIsSimplePolygon(intersectingPoly.begin(), intersectingPoly.count());
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400190 REPORTER_ASSERT(reporter, !result);
Jim Van Verth4db18ed2018-04-03 10:00:37 -0400191}