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