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