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