blob: 53767899438b4f7baecc6251653bcb78749bd77a [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 */
7#include "Test.h"
8#include "SkInsetConvexPolygon.h"
9
10static bool is_convex(const SkTDArray<SkPoint>& poly) {
11 if (poly.count() < 3) {
12 return false;
13 }
14
15 SkVector v0 = poly[0] - poly[poly.count() - 1];
16 SkVector v1 = poly[1] - poly[poly.count() - 1];
17 SkScalar winding = v0.cross(v1);
18
19 for (int i = 0; i < poly.count()-1; ++i) {
20 int j = i + 1;
21 int k = (i + 2) % poly.count();
22
23 SkVector v0 = poly[j] - poly[i];
24 SkVector v1 = poly[k] - poly[i];
25 SkScalar perpDot = v0.cross(v1);
Jim Van Verth291932e2017-03-29 14:37:28 -040026 if (winding*perpDot < 0) {
Brian Salomonab664fa2017-03-24 16:07:20 +000027 return false;
28 }
29 }
30
31 return true;
32}
33
34DEF_TEST(InsetConvexPoly, reporter) {
35 SkTDArray<SkPoint> rrectPoly;
36
37 // round rect
38 *rrectPoly.push() = SkPoint::Make(-100, 55);
39 *rrectPoly.push() = SkPoint::Make(100, 55);
40 *rrectPoly.push() = SkPoint::Make(100 + 2.5f, 50 + 4.330127f);
41 *rrectPoly.push() = SkPoint::Make(100 + 3.535534f, 50 + 3.535534f);
42 *rrectPoly.push() = SkPoint::Make(100 + 4.330127f, 50 + 2.5f);
43 *rrectPoly.push() = SkPoint::Make(105, 50);
44 *rrectPoly.push() = SkPoint::Make(105, -50);
45 *rrectPoly.push() = SkPoint::Make(100 + 4.330127f, -50 - 2.5f);
46 *rrectPoly.push() = SkPoint::Make(100 + 3.535534f, -50 - 3.535534f);
47 *rrectPoly.push() = SkPoint::Make(100 + 2.5f, -50 - 4.330127f);
48 *rrectPoly.push() = SkPoint::Make(100, -55);
49 *rrectPoly.push() = SkPoint::Make(-100, -55);
50 *rrectPoly.push() = SkPoint::Make(-100 - 2.5f, -50 - 4.330127f);
51 *rrectPoly.push() = SkPoint::Make(-100 - 3.535534f, -50 - 3.535534f);
52 *rrectPoly.push() = SkPoint::Make(-100 - 4.330127f, -50 - 2.5f);
53 *rrectPoly.push() = SkPoint::Make(-105, -50);
54 *rrectPoly.push() = SkPoint::Make(-105, 50);
55 *rrectPoly.push() = SkPoint::Make(-100 - 4.330127f, 50 + 2.5f);
56 *rrectPoly.push() = SkPoint::Make(-100 - 3.535534f, 50 + 3.535534f);
57 *rrectPoly.push() = SkPoint::Make(-100 - 2.5f, 50 + 4.330127f);
58 REPORTER_ASSERT(reporter, is_convex(rrectPoly));
59
60 // inset a little
61 SkTDArray<SkPoint> insetPoly;
62 bool result = SkInsetConvexPolygon(&rrectPoly[0], rrectPoly.count(), 3, &insetPoly);
63 REPORTER_ASSERT(reporter, result);
64 REPORTER_ASSERT(reporter, is_convex(insetPoly));
65
66 // inset to rect
67 result = SkInsetConvexPolygon(&rrectPoly[0], rrectPoly.count(), 10, &insetPoly);
68 REPORTER_ASSERT(reporter, result);
69 REPORTER_ASSERT(reporter, is_convex(insetPoly));
70 REPORTER_ASSERT(reporter, insetPoly.count() == 4);
71 if (insetPoly.count() == 4) {
72 REPORTER_ASSERT(reporter, insetPoly[0].equals(-95, 45));
73 REPORTER_ASSERT(reporter, insetPoly[1].equals(95, 45));
74 REPORTER_ASSERT(reporter, insetPoly[2].equals(95, -45));
75 REPORTER_ASSERT(reporter, insetPoly[3].equals(-95, -45));
76 }
77
78 // just to full inset
79 // for shadows having a flat poly here is fine
80 // may want to revisit for strokes
81 result = SkInsetConvexPolygon(&rrectPoly[0], rrectPoly.count(), 55, &insetPoly);
82 REPORTER_ASSERT(reporter, result);
83 REPORTER_ASSERT(reporter, is_convex(insetPoly));
84 REPORTER_ASSERT(reporter, insetPoly.count() == 4);
85 if (insetPoly.count() == 4) {
86 REPORTER_ASSERT(reporter, insetPoly[0].equals(-50, 0));
87 REPORTER_ASSERT(reporter, insetPoly[1].equals(50, 0));
88 REPORTER_ASSERT(reporter, insetPoly[2].equals(50, 0));
89 REPORTER_ASSERT(reporter, insetPoly[3].equals(-50, 0));
90 }
91
92 // past full inset
93 result = SkInsetConvexPolygon(&rrectPoly[0], rrectPoly.count(), 75, &insetPoly);
94 REPORTER_ASSERT(reporter, !result);
95
96 // troublesome case
97 SkTDArray<SkPoint> clippedRRectPoly;
98 *clippedRRectPoly.push() = SkPoint::Make(335.928101f, 428.219055f);
99 *clippedRRectPoly.push() = SkPoint::Make(330.414459f, 423.034912f);
100 *clippedRRectPoly.push() = SkPoint::Make(325.749084f, 417.395508f);
101 *clippedRRectPoly.push() = SkPoint::Make(321.931946f, 411.300842f);
102 *clippedRRectPoly.push() = SkPoint::Make(318.963074f, 404.750977f);
103 *clippedRRectPoly.push() = SkPoint::Make(316.842468f, 397.745850f);
104 *clippedRRectPoly.push() = SkPoint::Make(315.570068f, 390.285522f);
105 *clippedRRectPoly.push() = SkPoint::Make(315.145966f, 382.369965f);
106 *clippedRRectPoly.push() = SkPoint::Make(315.570068f, 374.454346f);
107 *clippedRRectPoly.push() = SkPoint::Make(316.842468f, 366.994019f);
108 *clippedRRectPoly.push() = SkPoint::Make(318.963074f, 359.988892f);
109 *clippedRRectPoly.push() = SkPoint::Make(321.931946f, 353.439056f);
110 *clippedRRectPoly.push() = SkPoint::Make(325.749084f, 347.344421f);
111 *clippedRRectPoly.push() = SkPoint::Make(330.414459f, 341.705017f);
112 *clippedRRectPoly.push() = SkPoint::Make(335.928101f, 336.520813f);
113 *clippedRRectPoly.push() = SkPoint::Make(342.289948f, 331.791901f);
114 *clippedRRectPoly.push() = SkPoint::Make(377.312134f, 331.791901f);
115 *clippedRRectPoly.push() = SkPoint::Make(381.195313f, 332.532593f);
116 *clippedRRectPoly.push() = SkPoint::Make(384.464935f, 334.754700f);
117 *clippedRRectPoly.push() = SkPoint::Make(386.687042f, 338.024292f);
118 *clippedRRectPoly.push() = SkPoint::Make(387.427765f, 341.907532f);
119 *clippedRRectPoly.push() = SkPoint::Make(387.427765f, 422.832367f);
120 *clippedRRectPoly.push() = SkPoint::Make(386.687042f, 426.715576f);
121 *clippedRRectPoly.push() = SkPoint::Make(384.464935f, 429.985168f);
122 *clippedRRectPoly.push() = SkPoint::Make(381.195313f, 432.207275f);
123 *clippedRRectPoly.push() = SkPoint::Make(377.312134f, 432.947998f);
124 *clippedRRectPoly.push() = SkPoint::Make(342.289948f, 432.947998f);
125 REPORTER_ASSERT(reporter, is_convex(clippedRRectPoly));
126
127 result = SkInsetConvexPolygon(&clippedRRectPoly[0], clippedRRectPoly.count(), 32.3699417f,
128 &insetPoly);
129 REPORTER_ASSERT(reporter, result);
130 REPORTER_ASSERT(reporter, is_convex(insetPoly));
131}