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/SkPathOpsLine.h" |
| 9 | #include "src/pathops/SkPathOpsQuad.h" |
| 10 | #include "src/pathops/SkReduceOrder.h" |
| 11 | #include "tests/PathOpsExtendedTest.h" |
| 12 | #include "tests/PathOpsTestCommon.h" |
| 13 | #include "tests/Test.h" |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 14 | |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 15 | #include <utility> |
| 16 | |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 17 | static struct lineQuad { |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 18 | QuadPts quad; |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 19 | SkDLine line; |
| 20 | int result; |
| 21 | SkDPoint expected[2]; |
| 22 | } lineQuadTests[] = { |
| 23 | // quad line results |
| 24 | {{{{1, 1}, {2, 1}, {0, 2}}}, {{{0, 0}, {1, 1}}}, 1, {{1, 1}, {0, 0}} }, |
| 25 | {{{{0, 0}, {1, 1}, {3, 1}}}, {{{0, 0}, {3, 1}}}, 2, {{0, 0}, {3, 1}} }, |
| 26 | {{{{2, 0}, {1, 1}, {2, 2}}}, {{{0, 0}, {0, 2}}}, 0, {{0, 0}, {0, 0}} }, |
| 27 | {{{{4, 0}, {0, 1}, {4, 2}}}, {{{3, 1}, {4, 1}}}, 0, {{0, 0}, {0, 0}} }, |
| 28 | {{{{0, 0}, {0, 1}, {1, 1}}}, {{{0, 1}, {1, 0}}}, 1, {{.25, .75}, {0, 0}} }, |
| 29 | }; |
| 30 | |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 31 | static size_t lineQuadTests_count = SK_ARRAY_COUNT(lineQuadTests); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 32 | |
| 33 | static int doIntersect(SkIntersections& intersections, const SkDQuad& quad, const SkDLine& line, |
| 34 | bool& flipped) { |
| 35 | int result; |
| 36 | flipped = false; |
| 37 | if (line[0].fX == line[1].fX) { |
| 38 | double top = line[0].fY; |
| 39 | double bottom = line[1].fY; |
| 40 | flipped = top > bottom; |
| 41 | if (flipped) { |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 42 | using std::swap; |
| 43 | swap(top, bottom); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 44 | } |
| 45 | result = intersections.vertical(quad, top, bottom, line[0].fX, flipped); |
| 46 | } else if (line[0].fY == line[1].fY) { |
| 47 | double left = line[0].fX; |
| 48 | double right = line[1].fX; |
| 49 | flipped = left > right; |
| 50 | if (flipped) { |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 51 | using std::swap; |
| 52 | swap(left, right); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 53 | } |
| 54 | result = intersections.horizontal(quad, left, right, line[0].fY, flipped); |
| 55 | } else { |
| 56 | intersections.intersect(quad, line); |
| 57 | result = intersections.used(); |
| 58 | } |
| 59 | return result; |
| 60 | } |
| 61 | |
| 62 | static struct oneLineQuad { |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 63 | QuadPts quad; |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 64 | SkDLine line; |
| 65 | } oneOffs[] = { |
commit-bot@chromium.org | 4431e77 | 2014-04-14 17:08:59 +0000 | [diff] [blame] | 66 | {{{{97.9337616,100}, {88,112.94265}, {88,130}}}, |
| 67 | {{{88.919838,120}, {107.058823,120}}}}, |
skia.committer@gmail.com | f54ad6f | 2013-11-02 07:02:02 +0000 | [diff] [blame] | 68 | {{{{447.96701049804687, 894.4381103515625}, {448.007080078125, 894.4239501953125}, |
| 69 | {448.0140380859375, 894.4215087890625}}}, |
caryclark@google.com | a2bbc6e | 2013-11-01 17:36:03 +0000 | [diff] [blame] | 70 | {{{490.43548583984375, 879.40740966796875}, {405.59262084960937, 909.435546875}}}}, |
caryclark@google.com | 570863f | 2013-09-16 15:55:01 +0000 | [diff] [blame] | 71 | {{{{142.589081, 102.283646}, {149.821579, 100}, {158, 100}}}, |
| 72 | {{{90, 230}, {160, 60}}}}, |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 73 | {{{{1101, 10}, {1101, 8.3431453704833984}, {1099.828857421875, 7.1711997985839844}}}, |
| 74 | {{{1099.828857421875,7.1711711883544922}, {1099.121337890625,7.8786783218383789}}}}, |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 75 | {{{{973, 507}, {973, 508.24264526367187}, {972.12158203125, 509.12161254882812}}}, |
| 76 | {{{930, 467}, {973, 510}}}}, |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 77 | {{{{369.848602, 145.680267}, {382.360413, 121.298294}, {406.207703, 121.298294}}}, |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 78 | {{{406.207703, 121.298294}, {348.781738, 123.864815}}}}, |
| 79 | }; |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 80 | |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 81 | static size_t oneOffs_count = SK_ARRAY_COUNT(oneOffs); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 82 | |
| 83 | static void testOneOffs(skiatest::Reporter* reporter) { |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 84 | bool flipped = false; |
| 85 | for (size_t index = 0; index < oneOffs_count; ++index) { |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 86 | const QuadPts& q = oneOffs[index].quad; |
| 87 | SkDQuad quad; |
| 88 | quad.debugSet(q.fPts); |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 89 | SkASSERT(ValidQuad(quad)); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 90 | const SkDLine& line = oneOffs[index].line; |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 91 | SkASSERT(ValidLine(line)); |
caryclark@google.com | a5e5592 | 2013-05-07 18:51:31 +0000 | [diff] [blame] | 92 | SkIntersections intersections; |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 93 | int result = doIntersect(intersections, quad, line, flipped); |
| 94 | for (int inner = 0; inner < result; ++inner) { |
| 95 | double quadT = intersections[0][inner]; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 96 | SkDPoint quadXY = quad.ptAtT(quadT); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 97 | double lineT = intersections[1][inner]; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 98 | SkDPoint lineXY = line.ptAtT(lineT); |
caryclark@google.com | 7eaa53d | 2013-10-02 14:49:34 +0000 | [diff] [blame] | 99 | if (!quadXY.approximatelyEqual(lineXY)) { |
| 100 | quadXY.approximatelyEqual(lineXY); |
| 101 | SkDebugf(""); |
| 102 | } |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 103 | REPORTER_ASSERT(reporter, quadXY.approximatelyEqual(lineXY)); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
tfarina@chromium.org | 78e7b4e | 2014-01-02 21:45:03 +0000 | [diff] [blame] | 108 | DEF_TEST(PathOpsQuadLineIntersectionOneOff, reporter) { |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 109 | testOneOffs(reporter); |
caryclark@google.com | fa2aeee | 2013-07-15 13:29:13 +0000 | [diff] [blame] | 110 | } |
| 111 | |
tfarina@chromium.org | 78e7b4e | 2014-01-02 21:45:03 +0000 | [diff] [blame] | 112 | DEF_TEST(PathOpsQuadLineIntersection, reporter) { |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 113 | for (size_t index = 0; index < lineQuadTests_count; ++index) { |
| 114 | int iIndex = static_cast<int>(index); |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 115 | const QuadPts& q = lineQuadTests[index].quad; |
| 116 | SkDQuad quad; |
| 117 | quad.debugSet(q.fPts); |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 118 | SkASSERT(ValidQuad(quad)); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 119 | const SkDLine& line = lineQuadTests[index].line; |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 120 | SkASSERT(ValidLine(line)); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 121 | SkReduceOrder reducer1, reducer2; |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 122 | int order1 = reducer1.reduce(quad); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 123 | int order2 = reducer2.reduce(line); |
| 124 | if (order1 < 3) { |
| 125 | SkDebugf("%s [%d] quad order=%d\n", __FUNCTION__, iIndex, order1); |
| 126 | REPORTER_ASSERT(reporter, 0); |
| 127 | } |
| 128 | if (order2 < 2) { |
| 129 | SkDebugf("%s [%d] line order=%d\n", __FUNCTION__, iIndex, order2); |
| 130 | REPORTER_ASSERT(reporter, 0); |
| 131 | } |
| 132 | SkIntersections intersections; |
| 133 | bool flipped = false; |
| 134 | int result = doIntersect(intersections, quad, line, flipped); |
| 135 | REPORTER_ASSERT(reporter, result == lineQuadTests[index].result); |
| 136 | if (intersections.used() <= 0) { |
| 137 | continue; |
| 138 | } |
| 139 | for (int pt = 0; pt < result; ++pt) { |
| 140 | double tt1 = intersections[0][pt]; |
| 141 | REPORTER_ASSERT(reporter, tt1 >= 0 && tt1 <= 1); |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 142 | SkDPoint t1 = quad.ptAtT(tt1); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 143 | double tt2 = intersections[1][pt]; |
| 144 | REPORTER_ASSERT(reporter, tt2 >= 0 && tt2 <= 1); |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 145 | SkDPoint t2 = line.ptAtT(tt2); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 146 | if (!t1.approximatelyEqual(t2)) { |
| 147 | SkDebugf("%s [%d,%d] x!= t1=%1.9g (%1.9g,%1.9g) t2=%1.9g (%1.9g,%1.9g)\n", |
| 148 | __FUNCTION__, iIndex, pt, tt1, t1.fX, t1.fY, tt2, t2.fX, t2.fY); |
| 149 | REPORTER_ASSERT(reporter, 0); |
| 150 | } |
| 151 | if (!t1.approximatelyEqual(lineQuadTests[index].expected[0]) |
| 152 | && (lineQuadTests[index].result == 1 |
| 153 | || !t1.approximatelyEqual(lineQuadTests[index].expected[1]))) { |
| 154 | SkDebugf("%s t1=(%1.9g,%1.9g)\n", __FUNCTION__, t1.fX, t1.fY); |
| 155 | REPORTER_ASSERT(reporter, 0); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |