Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [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 | */ |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 7 | #include "src/utils/SkPolyUtils.h" |
| 8 | #include "tests/Test.h" |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 9 | |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 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); |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 34 | REPORTER_ASSERT(reporter, SkIsConvexPolygon(rrectPoly.begin(), rrectPoly.count())); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 35 | |
| 36 | // inset a little |
| 37 | SkTDArray<SkPoint> insetPoly; |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 38 | bool result = SkInsetConvexPolygon(rrectPoly.begin(), rrectPoly.count(), 3, &insetPoly); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 39 | REPORTER_ASSERT(reporter, result); |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 40 | REPORTER_ASSERT(reporter, SkIsConvexPolygon(insetPoly.begin(), insetPoly.count())); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 41 | |
| 42 | // inset to rect |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 43 | result = SkInsetConvexPolygon(rrectPoly.begin(), rrectPoly.count(), 10, &insetPoly); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 44 | REPORTER_ASSERT(reporter, result); |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 45 | REPORTER_ASSERT(reporter, SkIsConvexPolygon(insetPoly.begin(), insetPoly.count())); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 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 |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 55 | // fails, but outputs a line segment |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 56 | result = SkInsetConvexPolygon(rrectPoly.begin(), rrectPoly.count(), 55, &insetPoly); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 57 | REPORTER_ASSERT(reporter, !result); |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 58 | REPORTER_ASSERT(reporter, !SkIsConvexPolygon(insetPoly.begin(), insetPoly.count())); |
Jim Van Verth | da96550 | 2017-04-11 15:29:14 -0400 | [diff] [blame] | 59 | REPORTER_ASSERT(reporter, insetPoly.count() == 2); |
| 60 | if (insetPoly.count() == 2) { |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 61 | REPORTER_ASSERT(reporter, insetPoly[0].equals(-50, 0)); |
| 62 | REPORTER_ASSERT(reporter, insetPoly[1].equals(50, 0)); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // past full inset |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 66 | result = SkInsetConvexPolygon(rrectPoly.begin(), rrectPoly.count(), 75, &insetPoly); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 67 | REPORTER_ASSERT(reporter, !result); |
Jim Van Verth | 0067369 | 2018-07-23 11:23:39 -0400 | [diff] [blame] | 68 | REPORTER_ASSERT(reporter, insetPoly.count() == 1); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 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); |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 99 | REPORTER_ASSERT(reporter, SkIsConvexPolygon(clippedRRectPoly.begin(), |
| 100 | clippedRRectPoly.count())); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 101 | |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 102 | result = SkInsetConvexPolygon(clippedRRectPoly.begin(), clippedRRectPoly.count(), 32.3699417f, |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 103 | &insetPoly); |
| 104 | REPORTER_ASSERT(reporter, result); |
Jim Van Verth | 6784ffa | 2018-07-03 16:12:39 -0400 | [diff] [blame] | 105 | REPORTER_ASSERT(reporter, SkIsConvexPolygon(insetPoly.begin(), insetPoly.count())); |
Brian Salomon | ab664fa | 2017-03-24 16:07:20 +0000 | [diff] [blame] | 106 | } |