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" |
| 8 | #include "SkIntersections.h" |
| 9 | #include "SkPathOpsLine.h" |
| 10 | #include "SkPathOpsQuad.h" |
| 11 | #include "SkReduceOrder.h" |
| 12 | #include "Test.h" |
| 13 | |
| 14 | static struct lineQuad { |
| 15 | SkDQuad quad; |
| 16 | SkDLine line; |
| 17 | int result; |
| 18 | SkDPoint expected[2]; |
| 19 | } lineQuadTests[] = { |
| 20 | // quad line results |
| 21 | {{{{1, 1}, {2, 1}, {0, 2}}}, {{{0, 0}, {1, 1}}}, 1, {{1, 1}, {0, 0}} }, |
| 22 | {{{{0, 0}, {1, 1}, {3, 1}}}, {{{0, 0}, {3, 1}}}, 2, {{0, 0}, {3, 1}} }, |
| 23 | {{{{2, 0}, {1, 1}, {2, 2}}}, {{{0, 0}, {0, 2}}}, 0, {{0, 0}, {0, 0}} }, |
| 24 | {{{{4, 0}, {0, 1}, {4, 2}}}, {{{3, 1}, {4, 1}}}, 0, {{0, 0}, {0, 0}} }, |
| 25 | {{{{0, 0}, {0, 1}, {1, 1}}}, {{{0, 1}, {1, 0}}}, 1, {{.25, .75}, {0, 0}} }, |
| 26 | }; |
| 27 | |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame^] | 28 | static size_t lineQuadTests_count = SK_ARRAY_COUNT(lineQuadTests); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 29 | |
| 30 | static int doIntersect(SkIntersections& intersections, const SkDQuad& quad, const SkDLine& line, |
| 31 | bool& flipped) { |
| 32 | int result; |
| 33 | flipped = false; |
| 34 | if (line[0].fX == line[1].fX) { |
| 35 | double top = line[0].fY; |
| 36 | double bottom = line[1].fY; |
| 37 | flipped = top > bottom; |
| 38 | if (flipped) { |
| 39 | SkTSwap<double>(top, bottom); |
| 40 | } |
| 41 | result = intersections.vertical(quad, top, bottom, line[0].fX, flipped); |
| 42 | } else if (line[0].fY == line[1].fY) { |
| 43 | double left = line[0].fX; |
| 44 | double right = line[1].fX; |
| 45 | flipped = left > right; |
| 46 | if (flipped) { |
| 47 | SkTSwap<double>(left, right); |
| 48 | } |
| 49 | result = intersections.horizontal(quad, left, right, line[0].fY, flipped); |
| 50 | } else { |
| 51 | intersections.intersect(quad, line); |
| 52 | result = intersections.used(); |
| 53 | } |
| 54 | return result; |
| 55 | } |
| 56 | |
| 57 | static struct oneLineQuad { |
| 58 | SkDQuad quad; |
| 59 | SkDLine line; |
| 60 | } oneOffs[] = { |
| 61 | {{{{369.848602, 145.680267}, {382.360413, 121.298294}, {406.207703, 121.298294}}}, |
| 62 | {{{406.207703, 121.298294}, {348.781738, 123.864815}}}} |
| 63 | }; |
| 64 | |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame^] | 65 | static size_t oneOffs_count = SK_ARRAY_COUNT(oneOffs); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 66 | |
| 67 | static void testOneOffs(skiatest::Reporter* reporter) { |
| 68 | SkIntersections intersections; |
| 69 | bool flipped = false; |
| 70 | for (size_t index = 0; index < oneOffs_count; ++index) { |
| 71 | const SkDQuad& quad = oneOffs[index].quad; |
| 72 | const SkDLine& line = oneOffs[index].line; |
| 73 | int result = doIntersect(intersections, quad, line, flipped); |
| 74 | for (int inner = 0; inner < result; ++inner) { |
| 75 | double quadT = intersections[0][inner]; |
| 76 | SkDPoint quadXY = quad.xyAtT(quadT); |
| 77 | double lineT = intersections[1][inner]; |
| 78 | SkDPoint lineXY = line.xyAtT(lineT); |
| 79 | REPORTER_ASSERT(reporter, quadXY.approximatelyEqual(lineXY)); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame^] | 84 | static void PathOpsQuadLineIntersectionTest(skiatest::Reporter* reporter) { |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 85 | testOneOffs(reporter); |
| 86 | for (size_t index = 0; index < lineQuadTests_count; ++index) { |
| 87 | int iIndex = static_cast<int>(index); |
| 88 | const SkDQuad& quad = lineQuadTests[index].quad; |
| 89 | const SkDLine& line = lineQuadTests[index].line; |
| 90 | SkReduceOrder reducer1, reducer2; |
| 91 | int order1 = reducer1.reduce(quad, SkReduceOrder::kFill_Style); |
| 92 | int order2 = reducer2.reduce(line); |
| 93 | if (order1 < 3) { |
| 94 | SkDebugf("%s [%d] quad order=%d\n", __FUNCTION__, iIndex, order1); |
| 95 | REPORTER_ASSERT(reporter, 0); |
| 96 | } |
| 97 | if (order2 < 2) { |
| 98 | SkDebugf("%s [%d] line order=%d\n", __FUNCTION__, iIndex, order2); |
| 99 | REPORTER_ASSERT(reporter, 0); |
| 100 | } |
| 101 | SkIntersections intersections; |
| 102 | bool flipped = false; |
| 103 | int result = doIntersect(intersections, quad, line, flipped); |
| 104 | REPORTER_ASSERT(reporter, result == lineQuadTests[index].result); |
| 105 | if (intersections.used() <= 0) { |
| 106 | continue; |
| 107 | } |
| 108 | for (int pt = 0; pt < result; ++pt) { |
| 109 | double tt1 = intersections[0][pt]; |
| 110 | REPORTER_ASSERT(reporter, tt1 >= 0 && tt1 <= 1); |
| 111 | SkDPoint t1 = quad.xyAtT(tt1); |
| 112 | double tt2 = intersections[1][pt]; |
| 113 | REPORTER_ASSERT(reporter, tt2 >= 0 && tt2 <= 1); |
| 114 | SkDPoint t2 = line.xyAtT(tt2); |
| 115 | if (!t1.approximatelyEqual(t2)) { |
| 116 | SkDebugf("%s [%d,%d] x!= t1=%1.9g (%1.9g,%1.9g) t2=%1.9g (%1.9g,%1.9g)\n", |
| 117 | __FUNCTION__, iIndex, pt, tt1, t1.fX, t1.fY, tt2, t2.fX, t2.fY); |
| 118 | REPORTER_ASSERT(reporter, 0); |
| 119 | } |
| 120 | if (!t1.approximatelyEqual(lineQuadTests[index].expected[0]) |
| 121 | && (lineQuadTests[index].result == 1 |
| 122 | || !t1.approximatelyEqual(lineQuadTests[index].expected[1]))) { |
| 123 | SkDebugf("%s t1=(%1.9g,%1.9g)\n", __FUNCTION__, t1.fX, t1.fY); |
| 124 | REPORTER_ASSERT(reporter, 0); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | #include "TestClassDef.h" |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame^] | 131 | DEFINE_TESTCLASS_SHORT(PathOpsQuadLineIntersectionTest) |