Jim Van Verth | 4db18ed | 2018-04-03 10:00:37 -0400 | [diff] [blame^] | 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 "Test.h" |
| 8 | #include "SkOffsetPolygon.h" |
| 9 | |
| 10 | static 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); |
| 26 | if (winding*perpDot < 0) { |
| 27 | return false; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | return true; |
| 32 | } |
| 33 | |
| 34 | DEF_TEST(OffsetSimplePoly, reporter) { |
| 35 | SkTDArray<SkPoint> rrectPoly; |
| 36 | |
| 37 | /////////////////////////////////////////////////////////////////////// |
| 38 | // Try convex tests first |
| 39 | |
| 40 | // round rect |
| 41 | *rrectPoly.push() = SkPoint::Make(-100, 55); |
| 42 | *rrectPoly.push() = SkPoint::Make(100, 55); |
| 43 | *rrectPoly.push() = SkPoint::Make(100 + 2.5f, 50 + 4.330127f); |
| 44 | *rrectPoly.push() = SkPoint::Make(100 + 3.535534f, 50 + 3.535534f); |
| 45 | *rrectPoly.push() = SkPoint::Make(100 + 4.330127f, 50 + 2.5f); |
| 46 | *rrectPoly.push() = SkPoint::Make(105, 50); |
| 47 | *rrectPoly.push() = SkPoint::Make(105, -50); |
| 48 | *rrectPoly.push() = SkPoint::Make(100 + 4.330127f, -50 - 2.5f); |
| 49 | *rrectPoly.push() = SkPoint::Make(100 + 3.535534f, -50 - 3.535534f); |
| 50 | *rrectPoly.push() = SkPoint::Make(100 + 2.5f, -50 - 4.330127f); |
| 51 | *rrectPoly.push() = SkPoint::Make(100, -55); |
| 52 | *rrectPoly.push() = SkPoint::Make(-100, -55); |
| 53 | *rrectPoly.push() = SkPoint::Make(-100 - 2.5f, -50 - 4.330127f); |
| 54 | *rrectPoly.push() = SkPoint::Make(-100 - 3.535534f, -50 - 3.535534f); |
| 55 | *rrectPoly.push() = SkPoint::Make(-100 - 4.330127f, -50 - 2.5f); |
| 56 | *rrectPoly.push() = SkPoint::Make(-105, -50); |
| 57 | *rrectPoly.push() = SkPoint::Make(-105, 50); |
| 58 | *rrectPoly.push() = SkPoint::Make(-100 - 4.330127f, 50 + 2.5f); |
| 59 | *rrectPoly.push() = SkPoint::Make(-100 - 3.535534f, 50 + 3.535534f); |
| 60 | *rrectPoly.push() = SkPoint::Make(-100 - 2.5f, 50 + 4.330127f); |
| 61 | REPORTER_ASSERT(reporter, is_convex(rrectPoly)); |
| 62 | |
| 63 | // inset a little |
| 64 | SkTDArray<SkPoint> offsetPoly; |
| 65 | bool result = SkOffsetSimplePolygon(&rrectPoly[0], rrectPoly.count(), 3, &offsetPoly); |
| 66 | REPORTER_ASSERT(reporter, result); |
| 67 | REPORTER_ASSERT(reporter, is_convex(offsetPoly)); |
| 68 | |
| 69 | // inset to rect |
| 70 | result = SkOffsetSimplePolygon(&rrectPoly[0], rrectPoly.count(), 10, &offsetPoly); |
| 71 | REPORTER_ASSERT(reporter, result); |
| 72 | REPORTER_ASSERT(reporter, is_convex(offsetPoly)); |
| 73 | REPORTER_ASSERT(reporter, offsetPoly.count() == 4); |
| 74 | if (offsetPoly.count() == 4) { |
| 75 | REPORTER_ASSERT(reporter, offsetPoly[0].equals(-95, 45)); |
| 76 | REPORTER_ASSERT(reporter, offsetPoly[1].equals(95, 45)); |
| 77 | REPORTER_ASSERT(reporter, offsetPoly[2].equals(95, -45)); |
| 78 | REPORTER_ASSERT(reporter, offsetPoly[3].equals(-95, -45)); |
| 79 | } |
| 80 | |
| 81 | // just to full inset |
| 82 | // fails, but outputs a line segment |
| 83 | result = SkOffsetSimplePolygon(&rrectPoly[0], rrectPoly.count(), 55, &offsetPoly); |
| 84 | REPORTER_ASSERT(reporter, !result); |
| 85 | REPORTER_ASSERT(reporter, !is_convex(offsetPoly)); |
| 86 | REPORTER_ASSERT(reporter, offsetPoly.count() == 2); |
| 87 | if (offsetPoly.count() == 2) { |
| 88 | REPORTER_ASSERT(reporter, offsetPoly[0].equals(-50, 0)); |
| 89 | REPORTER_ASSERT(reporter, offsetPoly[1].equals(50, 0)); |
| 90 | } |
| 91 | |
| 92 | // past full inset |
| 93 | result = SkOffsetSimplePolygon(&rrectPoly[0], rrectPoly.count(), 75, &offsetPoly); |
| 94 | REPORTER_ASSERT(reporter, !result); |
| 95 | REPORTER_ASSERT(reporter, offsetPoly.count() == 0); |
| 96 | |
| 97 | // troublesome case |
| 98 | SkTDArray<SkPoint> clippedRRectPoly; |
| 99 | *clippedRRectPoly.push() = SkPoint::Make(335.928101f, 428.219055f); |
| 100 | *clippedRRectPoly.push() = SkPoint::Make(330.414459f, 423.034912f); |
| 101 | *clippedRRectPoly.push() = SkPoint::Make(325.749084f, 417.395508f); |
| 102 | *clippedRRectPoly.push() = SkPoint::Make(321.931946f, 411.300842f); |
| 103 | *clippedRRectPoly.push() = SkPoint::Make(318.963074f, 404.750977f); |
| 104 | *clippedRRectPoly.push() = SkPoint::Make(316.842468f, 397.745850f); |
| 105 | *clippedRRectPoly.push() = SkPoint::Make(315.570068f, 390.285522f); |
| 106 | *clippedRRectPoly.push() = SkPoint::Make(315.145966f, 382.369965f); |
| 107 | *clippedRRectPoly.push() = SkPoint::Make(315.570068f, 374.454346f); |
| 108 | *clippedRRectPoly.push() = SkPoint::Make(316.842468f, 366.994019f); |
| 109 | *clippedRRectPoly.push() = SkPoint::Make(318.963074f, 359.988892f); |
| 110 | *clippedRRectPoly.push() = SkPoint::Make(321.931946f, 353.439056f); |
| 111 | *clippedRRectPoly.push() = SkPoint::Make(325.749084f, 347.344421f); |
| 112 | *clippedRRectPoly.push() = SkPoint::Make(330.414459f, 341.705017f); |
| 113 | *clippedRRectPoly.push() = SkPoint::Make(335.928101f, 336.520813f); |
| 114 | *clippedRRectPoly.push() = SkPoint::Make(342.289948f, 331.791901f); |
| 115 | *clippedRRectPoly.push() = SkPoint::Make(377.312134f, 331.791901f); |
| 116 | *clippedRRectPoly.push() = SkPoint::Make(381.195313f, 332.532593f); |
| 117 | *clippedRRectPoly.push() = SkPoint::Make(384.464935f, 334.754700f); |
| 118 | *clippedRRectPoly.push() = SkPoint::Make(386.687042f, 338.024292f); |
| 119 | *clippedRRectPoly.push() = SkPoint::Make(387.427765f, 341.907532f); |
| 120 | *clippedRRectPoly.push() = SkPoint::Make(387.427765f, 422.832367f); |
| 121 | *clippedRRectPoly.push() = SkPoint::Make(386.687042f, 426.715576f); |
| 122 | *clippedRRectPoly.push() = SkPoint::Make(384.464935f, 429.985168f); |
| 123 | *clippedRRectPoly.push() = SkPoint::Make(381.195313f, 432.207275f); |
| 124 | *clippedRRectPoly.push() = SkPoint::Make(377.312134f, 432.947998f); |
| 125 | *clippedRRectPoly.push() = SkPoint::Make(342.289948f, 432.947998f); |
| 126 | REPORTER_ASSERT(reporter, is_convex(clippedRRectPoly)); |
| 127 | |
| 128 | result = SkOffsetSimplePolygon(&clippedRRectPoly[0], clippedRRectPoly.count(), 32.3699417f, |
| 129 | &offsetPoly); |
| 130 | REPORTER_ASSERT(reporter, result); |
| 131 | REPORTER_ASSERT(reporter, is_convex(offsetPoly)); |
| 132 | |
| 133 | //////////////////////////////////////////////////////////////////////////////// |
| 134 | // Concave tests |
| 135 | |
| 136 | SkTDArray<SkPoint> starPoly; |
| 137 | *starPoly.push() = SkPoint::Make(0.0f, -50.0f); |
| 138 | *starPoly.push() = SkPoint::Make(14.43f, -25.0f); |
| 139 | *starPoly.push() = SkPoint::Make(43.30f, -25.0f); |
| 140 | *starPoly.push() = SkPoint::Make(28.86f, 0.0f); |
| 141 | *starPoly.push() = SkPoint::Make(43.30f, 25.0f); |
| 142 | *starPoly.push() = SkPoint::Make(14.43f, 25.0f); |
| 143 | *starPoly.push() = SkPoint::Make(0.0f, 50.0f); |
| 144 | *starPoly.push() = SkPoint::Make(-14.43f, 25.0f); |
| 145 | *starPoly.push() = SkPoint::Make(-43.30f, 25.0f); |
| 146 | *starPoly.push() = SkPoint::Make(-28.86f, 0.0f); |
| 147 | *starPoly.push() = SkPoint::Make(-43.30f, -25.0f); |
| 148 | *starPoly.push() = SkPoint::Make(-14.43f, -25.0f); |
| 149 | |
| 150 | // try a variety of distances |
| 151 | result = SkOffsetSimplePolygon(&starPoly[0], starPoly.count(), 0.1f, |
| 152 | &offsetPoly); |
| 153 | REPORTER_ASSERT(reporter, result); |
| 154 | |
| 155 | result = SkOffsetSimplePolygon(&starPoly[0], starPoly.count(), 5.665f, |
| 156 | &offsetPoly); |
| 157 | REPORTER_ASSERT(reporter, result); |
| 158 | |
| 159 | result = SkOffsetSimplePolygon(&starPoly[0], starPoly.count(), 28, |
| 160 | &offsetPoly); |
| 161 | REPORTER_ASSERT(reporter, result); |
| 162 | |
| 163 | // down to a point |
| 164 | result = SkOffsetSimplePolygon(&starPoly[0], starPoly.count(), 28.866f, |
| 165 | &offsetPoly); |
| 166 | REPORTER_ASSERT(reporter, !result); |
| 167 | |
| 168 | // and past |
| 169 | result = SkOffsetSimplePolygon(&starPoly[0], starPoly.count(), 50.5f, |
| 170 | &offsetPoly); |
| 171 | REPORTER_ASSERT(reporter, !result); |
| 172 | |
| 173 | // and now out |
| 174 | result = SkOffsetSimplePolygon(&starPoly[0], starPoly.count(), -0.1f, |
| 175 | &offsetPoly); |
| 176 | REPORTER_ASSERT(reporter, result); |
| 177 | |
| 178 | result = SkOffsetSimplePolygon(&starPoly[0], starPoly.count(), -5.6665f, |
| 179 | &offsetPoly); |
| 180 | REPORTER_ASSERT(reporter, result); |
| 181 | |
| 182 | result = SkOffsetSimplePolygon(&starPoly[0], starPoly.count(), -50, |
| 183 | &offsetPoly); |
| 184 | REPORTER_ASSERT(reporter, result); |
| 185 | |
| 186 | result = SkOffsetSimplePolygon(&starPoly[0], starPoly.count(), -100, |
| 187 | &offsetPoly); |
| 188 | REPORTER_ASSERT(reporter, result); |
| 189 | |
| 190 | SkTDArray<SkPoint> intersectingPoly; |
| 191 | *intersectingPoly.push() = SkPoint::Make(0.0f, -50.0f); |
| 192 | *intersectingPoly.push() = SkPoint::Make(14.43f, -25.0f); |
| 193 | *intersectingPoly.push() = SkPoint::Make(43.30f, -25.0f); |
| 194 | *intersectingPoly.push() = SkPoint::Make(-28.86f, 0.0f); |
| 195 | *intersectingPoly.push() = SkPoint::Make(43.30f, 25.0f); |
| 196 | *intersectingPoly.push() = SkPoint::Make(14.43f, 25.0f); |
| 197 | *intersectingPoly.push() = SkPoint::Make(0.0f, 50.0f); |
| 198 | *intersectingPoly.push() = SkPoint::Make(-14.43f, 25.0f); |
| 199 | *intersectingPoly.push() = SkPoint::Make(-43.30f, 25.0f); |
| 200 | *intersectingPoly.push() = SkPoint::Make(28.86f, 0.0f); |
| 201 | *intersectingPoly.push() = SkPoint::Make(-43.30f, -25.0f); |
| 202 | *intersectingPoly.push() = SkPoint::Make(-14.43f, -25.0f); |
| 203 | |
| 204 | result = SkOffsetSimplePolygon(&intersectingPoly[0], intersectingPoly.count(), -100, |
| 205 | &offsetPoly); |
| 206 | REPORTER_ASSERT(reporter, !result); |
| 207 | |
| 208 | |
| 209 | |
| 210 | } |