1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
/*
* Copyright 2017 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "Test.h"
#include "SkOffsetPolygon.h"
static bool is_convex(const SkTDArray<SkPoint>& poly) {
if (poly.count() < 3) {
return false;
}
SkVector v0 = poly[0] - poly[poly.count() - 1];
SkVector v1 = poly[1] - poly[poly.count() - 1];
SkScalar winding = v0.cross(v1);
for (int i = 0; i < poly.count()-1; ++i) {
int j = i + 1;
int k = (i + 2) % poly.count();
SkVector v0 = poly[j] - poly[i];
SkVector v1 = poly[k] - poly[i];
SkScalar perpDot = v0.cross(v1);
if (winding*perpDot < 0) {
return false;
}
}
return true;
}
DEF_TEST(OffsetSimplePoly, reporter) {
SkTDArray<SkPoint> rrectPoly;
///////////////////////////////////////////////////////////////////////
// Try convex tests first
// round rect
*rrectPoly.push() = SkPoint::Make(-100, 55);
*rrectPoly.push() = SkPoint::Make(100, 55);
*rrectPoly.push() = SkPoint::Make(100 + 2.5f, 50 + 4.330127f);
*rrectPoly.push() = SkPoint::Make(100 + 3.535534f, 50 + 3.535534f);
*rrectPoly.push() = SkPoint::Make(100 + 4.330127f, 50 + 2.5f);
*rrectPoly.push() = SkPoint::Make(105, 50);
*rrectPoly.push() = SkPoint::Make(105, -50);
*rrectPoly.push() = SkPoint::Make(100 + 4.330127f, -50 - 2.5f);
*rrectPoly.push() = SkPoint::Make(100 + 3.535534f, -50 - 3.535534f);
*rrectPoly.push() = SkPoint::Make(100 + 2.5f, -50 - 4.330127f);
*rrectPoly.push() = SkPoint::Make(100, -55);
*rrectPoly.push() = SkPoint::Make(-100, -55);
*rrectPoly.push() = SkPoint::Make(-100 - 2.5f, -50 - 4.330127f);
*rrectPoly.push() = SkPoint::Make(-100 - 3.535534f, -50 - 3.535534f);
*rrectPoly.push() = SkPoint::Make(-100 - 4.330127f, -50 - 2.5f);
*rrectPoly.push() = SkPoint::Make(-105, -50);
*rrectPoly.push() = SkPoint::Make(-105, 50);
*rrectPoly.push() = SkPoint::Make(-100 - 4.330127f, 50 + 2.5f);
*rrectPoly.push() = SkPoint::Make(-100 - 3.535534f, 50 + 3.535534f);
*rrectPoly.push() = SkPoint::Make(-100 - 2.5f, 50 + 4.330127f);
REPORTER_ASSERT(reporter, is_convex(rrectPoly));
// inset a little
SkTDArray<SkPoint> offsetPoly;
bool result = SkOffsetSimplePolygon(&rrectPoly[0], rrectPoly.count(), 3, &offsetPoly);
REPORTER_ASSERT(reporter, result);
REPORTER_ASSERT(reporter, is_convex(offsetPoly));
// inset to rect
result = SkOffsetSimplePolygon(&rrectPoly[0], rrectPoly.count(), 10, &offsetPoly);
REPORTER_ASSERT(reporter, result);
REPORTER_ASSERT(reporter, is_convex(offsetPoly));
REPORTER_ASSERT(reporter, offsetPoly.count() == 4);
if (offsetPoly.count() == 4) {
REPORTER_ASSERT(reporter, offsetPoly[0].equals(-95, 45));
REPORTER_ASSERT(reporter, offsetPoly[1].equals(95, 45));
REPORTER_ASSERT(reporter, offsetPoly[2].equals(95, -45));
REPORTER_ASSERT(reporter, offsetPoly[3].equals(-95, -45));
}
// just to full inset
// fails, but outputs a line segment
result = SkOffsetSimplePolygon(&rrectPoly[0], rrectPoly.count(), 55, &offsetPoly);
REPORTER_ASSERT(reporter, !result);
REPORTER_ASSERT(reporter, !is_convex(offsetPoly));
REPORTER_ASSERT(reporter, offsetPoly.count() == 2);
if (offsetPoly.count() == 2) {
REPORTER_ASSERT(reporter, offsetPoly[0].equals(-50, 0));
REPORTER_ASSERT(reporter, offsetPoly[1].equals(50, 0));
}
// past full inset
result = SkOffsetSimplePolygon(&rrectPoly[0], rrectPoly.count(), 75, &offsetPoly);
REPORTER_ASSERT(reporter, !result);
// troublesome case
SkTDArray<SkPoint> clippedRRectPoly;
*clippedRRectPoly.push() = SkPoint::Make(335.928101f, 428.219055f);
*clippedRRectPoly.push() = SkPoint::Make(330.414459f, 423.034912f);
*clippedRRectPoly.push() = SkPoint::Make(325.749084f, 417.395508f);
*clippedRRectPoly.push() = SkPoint::Make(321.931946f, 411.300842f);
*clippedRRectPoly.push() = SkPoint::Make(318.963074f, 404.750977f);
*clippedRRectPoly.push() = SkPoint::Make(316.842468f, 397.745850f);
*clippedRRectPoly.push() = SkPoint::Make(315.570068f, 390.285522f);
*clippedRRectPoly.push() = SkPoint::Make(315.145966f, 382.369965f);
*clippedRRectPoly.push() = SkPoint::Make(315.570068f, 374.454346f);
*clippedRRectPoly.push() = SkPoint::Make(316.842468f, 366.994019f);
*clippedRRectPoly.push() = SkPoint::Make(318.963074f, 359.988892f);
*clippedRRectPoly.push() = SkPoint::Make(321.931946f, 353.439056f);
*clippedRRectPoly.push() = SkPoint::Make(325.749084f, 347.344421f);
*clippedRRectPoly.push() = SkPoint::Make(330.414459f, 341.705017f);
*clippedRRectPoly.push() = SkPoint::Make(335.928101f, 336.520813f);
*clippedRRectPoly.push() = SkPoint::Make(342.289948f, 331.791901f);
*clippedRRectPoly.push() = SkPoint::Make(377.312134f, 331.791901f);
*clippedRRectPoly.push() = SkPoint::Make(381.195313f, 332.532593f);
*clippedRRectPoly.push() = SkPoint::Make(384.464935f, 334.754700f);
*clippedRRectPoly.push() = SkPoint::Make(386.687042f, 338.024292f);
*clippedRRectPoly.push() = SkPoint::Make(387.427765f, 341.907532f);
*clippedRRectPoly.push() = SkPoint::Make(387.427765f, 422.832367f);
*clippedRRectPoly.push() = SkPoint::Make(386.687042f, 426.715576f);
*clippedRRectPoly.push() = SkPoint::Make(384.464935f, 429.985168f);
*clippedRRectPoly.push() = SkPoint::Make(381.195313f, 432.207275f);
*clippedRRectPoly.push() = SkPoint::Make(377.312134f, 432.947998f);
*clippedRRectPoly.push() = SkPoint::Make(342.289948f, 432.947998f);
REPORTER_ASSERT(reporter, is_convex(clippedRRectPoly));
result = SkOffsetSimplePolygon(&clippedRRectPoly[0], clippedRRectPoly.count(), 32.3699417f,
&offsetPoly);
REPORTER_ASSERT(reporter, result);
REPORTER_ASSERT(reporter, is_convex(offsetPoly));
////////////////////////////////////////////////////////////////////////////////
// Concave tests
SkTDArray<SkPoint> starPoly;
*starPoly.push() = SkPoint::Make(0.0f, -50.0f);
*starPoly.push() = SkPoint::Make(14.43f, -25.0f);
*starPoly.push() = SkPoint::Make(43.30f, -25.0f);
*starPoly.push() = SkPoint::Make(28.86f, 0.0f);
*starPoly.push() = SkPoint::Make(43.30f, 25.0f);
*starPoly.push() = SkPoint::Make(14.43f, 25.0f);
*starPoly.push() = SkPoint::Make(0.0f, 50.0f);
*starPoly.push() = SkPoint::Make(-14.43f, 25.0f);
*starPoly.push() = SkPoint::Make(-43.30f, 25.0f);
*starPoly.push() = SkPoint::Make(-28.86f, 0.0f);
*starPoly.push() = SkPoint::Make(-43.30f, -25.0f);
*starPoly.push() = SkPoint::Make(-14.43f, -25.0f);
// try a variety of distances
result = SkOffsetSimplePolygon(&starPoly[0], starPoly.count(), 0.1f,
&offsetPoly);
REPORTER_ASSERT(reporter, result);
result = SkOffsetSimplePolygon(&starPoly[0], starPoly.count(), 5.665f,
&offsetPoly);
REPORTER_ASSERT(reporter, result);
result = SkOffsetSimplePolygon(&starPoly[0], starPoly.count(), 28,
&offsetPoly);
REPORTER_ASSERT(reporter, result);
// down to a point
result = SkOffsetSimplePolygon(&starPoly[0], starPoly.count(), 28.866f,
&offsetPoly);
REPORTER_ASSERT(reporter, !result);
// and past
result = SkOffsetSimplePolygon(&starPoly[0], starPoly.count(), 50.5f,
&offsetPoly);
REPORTER_ASSERT(reporter, !result);
// and now out
result = SkOffsetSimplePolygon(&starPoly[0], starPoly.count(), -0.1f,
&offsetPoly);
REPORTER_ASSERT(reporter, result);
result = SkOffsetSimplePolygon(&starPoly[0], starPoly.count(), -5.6665f,
&offsetPoly);
REPORTER_ASSERT(reporter, result);
result = SkOffsetSimplePolygon(&starPoly[0], starPoly.count(), -50,
&offsetPoly);
REPORTER_ASSERT(reporter, result);
result = SkOffsetSimplePolygon(&starPoly[0], starPoly.count(), -100,
&offsetPoly);
REPORTER_ASSERT(reporter, result);
SkTDArray<SkPoint> intersectingPoly;
*intersectingPoly.push() = SkPoint::Make(0.0f, -50.0f);
*intersectingPoly.push() = SkPoint::Make(14.43f, -25.0f);
*intersectingPoly.push() = SkPoint::Make(43.30f, -25.0f);
*intersectingPoly.push() = SkPoint::Make(-28.86f, 0.0f);
*intersectingPoly.push() = SkPoint::Make(43.30f, 25.0f);
*intersectingPoly.push() = SkPoint::Make(14.43f, 25.0f);
*intersectingPoly.push() = SkPoint::Make(0.0f, 50.0f);
*intersectingPoly.push() = SkPoint::Make(-14.43f, 25.0f);
*intersectingPoly.push() = SkPoint::Make(-43.30f, 25.0f);
*intersectingPoly.push() = SkPoint::Make(28.86f, 0.0f);
*intersectingPoly.push() = SkPoint::Make(-43.30f, -25.0f);
*intersectingPoly.push() = SkPoint::Make(-14.43f, -25.0f);
result = SkOffsetSimplePolygon(&intersectingPoly[0], intersectingPoly.count(), -100,
&offsetPoly);
REPORTER_ASSERT(reporter, !result);
}
|