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