caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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/pathops/SkIntersections.h" |
| 8 | #include "src/pathops/SkPathOpsCubic.h" |
| 9 | #include "src/pathops/SkPathOpsLine.h" |
| 10 | #include "src/pathops/SkReduceOrder.h" |
| 11 | #include "tests/PathOpsTestCommon.h" |
| 12 | #include "tests/Test.h" |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 13 | |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 14 | #include <utility> |
| 15 | |
commit-bot@chromium.org | 91fc81c | 2014-04-30 13:37:48 +0000 | [diff] [blame] | 16 | struct lineCubic { |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 17 | CubicPts cubic; |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 18 | SkDLine line; |
commit-bot@chromium.org | 91fc81c | 2014-04-30 13:37:48 +0000 | [diff] [blame] | 19 | }; |
| 20 | |
| 21 | static lineCubic failLineCubicTests[] = { |
| 22 | {{{{37.5273438,-1.44140625}, {37.8736992,-1.69921875}, {38.1640625,-2.140625}, |
| 23 | {38.3984375,-2.765625}}}, |
| 24 | {{{40.625,-5.7890625}, {37.7109375,1.3515625}}}}, |
| 25 | }; |
| 26 | |
| 27 | static const size_t failLineCubicTests_count = SK_ARRAY_COUNT(failLineCubicTests); |
| 28 | |
| 29 | static void testFail(skiatest::Reporter* reporter, int iIndex) { |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 30 | const CubicPts& cuPts = failLineCubicTests[iIndex].cubic; |
| 31 | SkDCubic cubic; |
| 32 | cubic.debugSet(cuPts.fPts); |
commit-bot@chromium.org | 91fc81c | 2014-04-30 13:37:48 +0000 | [diff] [blame] | 33 | SkASSERT(ValidCubic(cubic)); |
| 34 | const SkDLine& line = failLineCubicTests[iIndex].line; |
| 35 | SkASSERT(ValidLine(line)); |
| 36 | SkReduceOrder reduce1; |
| 37 | SkReduceOrder reduce2; |
| 38 | int order1 = reduce1.reduce(cubic, SkReduceOrder::kNo_Quadratics); |
| 39 | int order2 = reduce2.reduce(line); |
| 40 | if (order1 < 4) { |
| 41 | SkDebugf("[%d] cubic order=%d\n", iIndex, order1); |
| 42 | REPORTER_ASSERT(reporter, 0); |
| 43 | } |
| 44 | if (order2 < 2) { |
| 45 | SkDebugf("[%d] line order=%d\n", iIndex, order2); |
| 46 | REPORTER_ASSERT(reporter, 0); |
| 47 | } |
| 48 | if (order1 == 4 && order2 == 2) { |
| 49 | SkIntersections i; |
| 50 | int roots = i.intersect(cubic, line); |
| 51 | REPORTER_ASSERT(reporter, roots == 0); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | static lineCubic lineCubicTests[] = { |
caryclark | aec2510 | 2015-04-29 08:28:30 -0700 | [diff] [blame] | 56 | {{{{0, 6}, {1.0851458311080933, 4.3722810745239258}, {1.5815209150314331, 3.038947582244873}, {1.9683018922805786, 1.9999997615814209}}}, |
| 57 | {{{3,2}, {1,2}}}}, |
| 58 | |
caryclark | 5435929 | 2015-03-26 07:52:43 -0700 | [diff] [blame] | 59 | {{{{0.468027353,4}, {1.06734705,1.33333337}, {1.36700678,0}, {3,0}}}, |
| 60 | {{{2,1}, {0,1}}}}, |
| 61 | |
commit-bot@chromium.org | 2db7fe7 | 2014-05-07 15:31:40 +0000 | [diff] [blame] | 62 | {{{{-634.60540771484375, -481.262939453125}, {266.2696533203125, -752.70867919921875}, |
| 63 | {-751.8370361328125, -317.37921142578125}, {-969.7427978515625, 824.7255859375}}}, |
| 64 | {{{-287.9506133720805678, -557.1376476615772617}, |
| 65 | {-285.9506133720805678, -557.1376476615772617}}}}, |
| 66 | |
| 67 | {{{{36.7184372,0.888650894}, {36.7184372,0.888650894}, {35.1233864,0.554015458}, |
| 68 | {34.5114098,-0.115255356}}}, {{{35.4531212,0}, {31.9375,0}}}}, |
commit-bot@chromium.org | 91fc81c | 2014-04-30 13:37:48 +0000 | [diff] [blame] | 69 | |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 70 | {{{{421, 378}, {421, 380.209137f}, {418.761414f, 382}, {416, 382}}}, |
| 71 | {{{320, 378}, {421, 378.000031f}}}}, |
| 72 | |
| 73 | {{{{416, 383}, {418.761414f, 383}, {421, 380.761414f}, {421, 378}}}, |
| 74 | {{{320, 378}, {421, 378.000031f}}}}, |
| 75 | |
caryclark@google.com | a2bbc6e | 2013-11-01 17:36:03 +0000 | [diff] [blame] | 76 | {{{{154,715}, {151.238571,715}, {149,712.761414}, {149,710}}}, |
| 77 | {{{149,675}, {149,710.001465}}}}, |
| 78 | |
skia.committer@gmail.com | b0a0589 | 2013-10-03 07:01:37 +0000 | [diff] [blame] | 79 | {{{{0,1}, {1,6}, {4,1}, {4,3}}}, |
| 80 | {{{6,1}, {1,4}}}}, |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 81 | |
| 82 | {{{{0,1}, {2,6}, {4,1}, {5,4}}}, |
| 83 | {{{6,2}, {1,4}}}}, |
| 84 | |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 85 | {{{{0,4}, {3,4}, {6,2}, {5,2}}}, |
| 86 | {{{4,3}, {2,6}}}}, |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 87 | #if 0 |
| 88 | {{{{258, 122}, {260.761414, 122}, { 263, 124.238579}, {263, 127}}}, |
| 89 | {{{259.82843, 125.17157}, {261.535522, 123.46447}}}}, |
| 90 | #endif |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 91 | {{{{1006.6951293945312,291}, {1023.263671875,291}, {1033.8402099609375,304.43145751953125}, |
| 92 | {1030.318359375,321}}}, |
| 93 | {{{979.30487060546875,561}, {1036.695068359375,291}}}}, |
| 94 | {{{{259.30487060546875,561}, {242.73631286621094,561}, {232.15980529785156,547.56854248046875}, |
| 95 | {235.68154907226562,531}}}, |
| 96 | {{{286.69512939453125,291}, {229.30485534667969,561}}}}, |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 97 | {{{{1, 2}, {2, 6}, {2, 0}, {1, 0}}}, {{{1, 0}, {1, 2}}}}, |
| 98 | {{{{0, 0}, {0, 1}, {0, 1}, {1, 1}}}, {{{0, 1}, {1, 0}}}}, |
| 99 | }; |
| 100 | |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 101 | static const size_t lineCubicTests_count = SK_ARRAY_COUNT(lineCubicTests); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 102 | |
commit-bot@chromium.org | 2db7fe7 | 2014-05-07 15:31:40 +0000 | [diff] [blame] | 103 | static int doIntersect(SkIntersections& intersections, const SkDCubic& cubic, const SkDLine& line) { |
| 104 | int result; |
| 105 | bool flipped = false; |
| 106 | if (line[0].fX == line[1].fX) { |
| 107 | double top = line[0].fY; |
| 108 | double bottom = line[1].fY; |
| 109 | flipped = top > bottom; |
| 110 | if (flipped) { |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 111 | using std::swap; |
| 112 | swap(top, bottom); |
commit-bot@chromium.org | 2db7fe7 | 2014-05-07 15:31:40 +0000 | [diff] [blame] | 113 | } |
| 114 | result = intersections.vertical(cubic, top, bottom, line[0].fX, flipped); |
| 115 | } else if (line[0].fY == line[1].fY) { |
| 116 | double left = line[0].fX; |
| 117 | double right = line[1].fX; |
| 118 | flipped = left > right; |
| 119 | if (flipped) { |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 120 | using std::swap; |
| 121 | swap(left, right); |
commit-bot@chromium.org | 2db7fe7 | 2014-05-07 15:31:40 +0000 | [diff] [blame] | 122 | } |
| 123 | result = intersections.horizontal(cubic, left, right, line[0].fY, flipped); |
| 124 | } else { |
| 125 | intersections.intersect(cubic, line); |
| 126 | result = intersections.used(); |
| 127 | } |
| 128 | return result; |
| 129 | } |
| 130 | |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 131 | static void testOne(skiatest::Reporter* reporter, int iIndex) { |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 132 | const CubicPts& cuPts = lineCubicTests[iIndex].cubic; |
| 133 | SkDCubic cubic; |
| 134 | cubic.debugSet(cuPts.fPts); |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 135 | SkASSERT(ValidCubic(cubic)); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 136 | const SkDLine& line = lineCubicTests[iIndex].line; |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 137 | SkASSERT(ValidLine(line)); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 138 | SkReduceOrder reduce1; |
| 139 | SkReduceOrder reduce2; |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 140 | int order1 = reduce1.reduce(cubic, SkReduceOrder::kNo_Quadratics); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 141 | int order2 = reduce2.reduce(line); |
| 142 | if (order1 < 4) { |
| 143 | SkDebugf("[%d] cubic order=%d\n", iIndex, order1); |
| 144 | REPORTER_ASSERT(reporter, 0); |
| 145 | } |
| 146 | if (order2 < 2) { |
| 147 | SkDebugf("[%d] line order=%d\n", iIndex, order2); |
| 148 | REPORTER_ASSERT(reporter, 0); |
| 149 | } |
| 150 | if (order1 == 4 && order2 == 2) { |
| 151 | SkIntersections i; |
commit-bot@chromium.org | 2db7fe7 | 2014-05-07 15:31:40 +0000 | [diff] [blame] | 152 | int roots = doIntersect(i, cubic, line); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 153 | for (int pt = 0; pt < roots; ++pt) { |
| 154 | double tt1 = i[0][pt]; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 155 | SkDPoint xy1 = cubic.ptAtT(tt1); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 156 | double tt2 = i[1][pt]; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 157 | SkDPoint xy2 = line.ptAtT(tt2); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 158 | if (!xy1.approximatelyEqual(xy2)) { |
| 159 | SkDebugf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n", |
| 160 | __FUNCTION__, iIndex, pt, tt1, xy1.fX, xy1.fY, tt2, xy2.fX, xy2.fY); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 161 | } |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 162 | REPORTER_ASSERT(reporter, xy1.approximatelyEqual(xy2)); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 163 | } |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 164 | #if ONE_OFF_DEBUG |
| 165 | double cubicT = i[0][0]; |
| 166 | SkDPoint prev = cubic.ptAtT(cubicT * 2 - 1); |
| 167 | SkDPoint sect = cubic.ptAtT(cubicT); |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 168 | SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", prev.fX, prev.fY, sect.fX, sect.fY); |
| 169 | SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", sect.fX, sect.fY, cubic[3].fX, cubic[3].fY); |
| 170 | SkDPoint prevL = line.ptAtT(i[1][0] - 0.0000007); |
| 171 | SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", prevL.fX, prevL.fY, i.pt(0).fX, i.pt(0).fY); |
| 172 | SkDPoint nextL = line.ptAtT(i[1][0] + 0.0000007); |
| 173 | SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", i.pt(0).fX, i.pt(0).fY, nextL.fX, nextL.fY); |
| 174 | SkDebugf("prevD=%1.9g dist=%1.9g nextD=%1.9g\n", prev.distance(nextL), |
| 175 | sect.distance(i.pt(0)), cubic[3].distance(prevL)); |
| 176 | #endif |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
commit-bot@chromium.org | 91fc81c | 2014-04-30 13:37:48 +0000 | [diff] [blame] | 180 | DEF_TEST(PathOpsFailCubicLineIntersection, reporter) { |
| 181 | for (size_t index = 0; index < failLineCubicTests_count; ++index) { |
| 182 | int iIndex = static_cast<int>(index); |
| 183 | testFail(reporter, iIndex); |
| 184 | reporter->bumpTestCount(); |
| 185 | } |
| 186 | } |
| 187 | |
tfarina@chromium.org | 78e7b4e | 2014-01-02 21:45:03 +0000 | [diff] [blame] | 188 | DEF_TEST(PathOpsCubicLineIntersection, reporter) { |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 189 | for (size_t index = 0; index < lineCubicTests_count; ++index) { |
| 190 | int iIndex = static_cast<int>(index); |
| 191 | testOne(reporter, iIndex); |
| 192 | reporter->bumpTestCount(); |
| 193 | } |
| 194 | } |
| 195 | |
tfarina@chromium.org | 78e7b4e | 2014-01-02 21:45:03 +0000 | [diff] [blame] | 196 | DEF_TEST(PathOpsCubicLineIntersectionOneOff, reporter) { |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 197 | int iIndex = 0; |
| 198 | testOne(reporter, iIndex); |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 199 | const CubicPts& cuPts = lineCubicTests[iIndex].cubic; |
| 200 | SkDCubic cubic; |
| 201 | cubic.debugSet(cuPts.fPts); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 202 | const SkDLine& line = lineCubicTests[iIndex].line; |
| 203 | SkIntersections i; |
| 204 | i.intersect(cubic, line); |
| 205 | SkASSERT(i.used() == 1); |
caryclark@google.com | 07e97fc | 2013-07-08 17:17:02 +0000 | [diff] [blame] | 206 | } |