blob: fa25843bd35a180529fe7f52eb97497dcb22eac4 [file] [log] [blame]
Brian Salomonab664fa2017-03-24 16:07:20 +00001/*
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 */
Mike Kleinc0bd9f92019-04-23 12:05:21 -05007#include "src/utils/SkPolyUtils.h"
8#include "tests/Test.h"
Brian Salomonab664fa2017-03-24 16:07:20 +00009
Brian Salomonab664fa2017-03-24 16:07:20 +000010DEF_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);
Jim Van Verth6784ffa2018-07-03 16:12:39 -040034 REPORTER_ASSERT(reporter, SkIsConvexPolygon(rrectPoly.begin(), rrectPoly.count()));
Brian Salomonab664fa2017-03-24 16:07:20 +000035
36 // inset a little
37 SkTDArray<SkPoint> insetPoly;
Jim Van Verth6784ffa2018-07-03 16:12:39 -040038 bool result = SkInsetConvexPolygon(rrectPoly.begin(), rrectPoly.count(), 3, &insetPoly);
Brian Salomonab664fa2017-03-24 16:07:20 +000039 REPORTER_ASSERT(reporter, result);
Jim Van Verth6784ffa2018-07-03 16:12:39 -040040 REPORTER_ASSERT(reporter, SkIsConvexPolygon(insetPoly.begin(), insetPoly.count()));
Brian Salomonab664fa2017-03-24 16:07:20 +000041
42 // inset to rect
Jim Van Verth6784ffa2018-07-03 16:12:39 -040043 result = SkInsetConvexPolygon(rrectPoly.begin(), rrectPoly.count(), 10, &insetPoly);
Brian Salomonab664fa2017-03-24 16:07:20 +000044 REPORTER_ASSERT(reporter, result);
Jim Van Verth6784ffa2018-07-03 16:12:39 -040045 REPORTER_ASSERT(reporter, SkIsConvexPolygon(insetPoly.begin(), insetPoly.count()));
Brian Salomonab664fa2017-03-24 16:07:20 +000046 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
Jim Van Verthda965502017-04-11 15:29:14 -040055 // fails, but outputs a line segment
Jim Van Verth6784ffa2018-07-03 16:12:39 -040056 result = SkInsetConvexPolygon(rrectPoly.begin(), rrectPoly.count(), 55, &insetPoly);
Jim Van Verthda965502017-04-11 15:29:14 -040057 REPORTER_ASSERT(reporter, !result);
Jim Van Verth6784ffa2018-07-03 16:12:39 -040058 REPORTER_ASSERT(reporter, !SkIsConvexPolygon(insetPoly.begin(), insetPoly.count()));
Jim Van Verthda965502017-04-11 15:29:14 -040059 REPORTER_ASSERT(reporter, insetPoly.count() == 2);
60 if (insetPoly.count() == 2) {
Brian Salomonab664fa2017-03-24 16:07:20 +000061 REPORTER_ASSERT(reporter, insetPoly[0].equals(-50, 0));
62 REPORTER_ASSERT(reporter, insetPoly[1].equals(50, 0));
Brian Salomonab664fa2017-03-24 16:07:20 +000063 }
64
65 // past full inset
Jim Van Verth6784ffa2018-07-03 16:12:39 -040066 result = SkInsetConvexPolygon(rrectPoly.begin(), rrectPoly.count(), 75, &insetPoly);
Brian Salomonab664fa2017-03-24 16:07:20 +000067 REPORTER_ASSERT(reporter, !result);
Jim Van Verth00673692018-07-23 11:23:39 -040068 REPORTER_ASSERT(reporter, insetPoly.count() == 1);
Brian Salomonab664fa2017-03-24 16:07:20 +000069
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);
Jim Van Verth6784ffa2018-07-03 16:12:39 -040099 REPORTER_ASSERT(reporter, SkIsConvexPolygon(clippedRRectPoly.begin(),
100 clippedRRectPoly.count()));
Brian Salomonab664fa2017-03-24 16:07:20 +0000101
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400102 result = SkInsetConvexPolygon(clippedRRectPoly.begin(), clippedRRectPoly.count(), 32.3699417f,
Brian Salomonab664fa2017-03-24 16:07:20 +0000103 &insetPoly);
104 REPORTER_ASSERT(reporter, result);
Jim Van Verth6784ffa2018-07-03 16:12:39 -0400105 REPORTER_ASSERT(reporter, SkIsConvexPolygon(insetPoly.begin(), insetPoly.count()));
Brian Salomonab664fa2017-03-24 16:07:20 +0000106}