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