caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +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 | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 8 | #include "PathOpsThreadedCommon.h" |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 9 | #include "SkIntersections.h" |
| 10 | #include "SkPathOpsLine.h" |
| 11 | #include "SkPathOpsQuad.h" |
| 12 | #include "SkReduceOrder.h" |
| 13 | |
| 14 | static int doIntersect(SkIntersections& intersections, const SkDQuad& quad, const SkDLine& line, |
| 15 | bool& flipped) { |
| 16 | int result; |
| 17 | flipped = false; |
| 18 | if (line[0].fX == line[1].fX) { |
| 19 | double top = line[0].fY; |
| 20 | double bottom = line[1].fY; |
| 21 | flipped = top > bottom; |
| 22 | if (flipped) { |
| 23 | SkTSwap<double>(top, bottom); |
| 24 | } |
| 25 | result = intersections.vertical(quad, top, bottom, line[0].fX, flipped); |
| 26 | } else if (line[0].fY == line[1].fY) { |
| 27 | double left = line[0].fX; |
| 28 | double right = line[1].fX; |
| 29 | flipped = left > right; |
| 30 | if (flipped) { |
| 31 | SkTSwap<double>(left, right); |
| 32 | } |
| 33 | result = intersections.horizontal(quad, left, right, line[0].fY, flipped); |
| 34 | } else { |
| 35 | intersections.intersect(quad, line); |
| 36 | result = intersections.used(); |
| 37 | } |
| 38 | return result; |
| 39 | } |
| 40 | |
| 41 | static void testLineIntersect(skiatest::Reporter* reporter, const SkDQuad& quad, |
| 42 | const SkDLine& line, const double x, const double y) { |
| 43 | char pathStr[1024]; |
| 44 | sk_bzero(pathStr, sizeof(pathStr)); |
| 45 | char* str = pathStr; |
| 46 | str += sprintf(str, " path.moveTo(%1.9g, %1.9g);\n", quad[0].fX, quad[0].fY); |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 47 | str += sprintf(str, " path.quadTo(%1.9g, %1.9g, %1.9g, %1.9g);\n", quad[1].fX, |
| 48 | quad[1].fY, quad[2].fX, quad[2].fY); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 49 | str += sprintf(str, " path.moveTo(%1.9g, %1.9g);\n", line[0].fX, line[0].fY); |
| 50 | str += sprintf(str, " path.lineTo(%1.9g, %1.9g);\n", line[1].fX, line[1].fY); |
| 51 | |
| 52 | SkIntersections intersections; |
| 53 | bool flipped = false; |
| 54 | int result = doIntersect(intersections, quad, line, flipped); |
| 55 | bool found = false; |
| 56 | for (int index = 0; index < result; ++index) { |
| 57 | double quadT = intersections[0][index]; |
| 58 | SkDPoint quadXY = quad.xyAtT(quadT); |
| 59 | double lineT = intersections[1][index]; |
| 60 | SkDPoint lineXY = line.xyAtT(lineT); |
| 61 | if (quadXY.approximatelyEqual(lineXY)) { |
| 62 | found = true; |
| 63 | } |
| 64 | } |
| 65 | REPORTER_ASSERT(reporter, found); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | // find a point on a quad by choosing a t from 0 to 1 |
| 70 | // create a vertical span above and below the point |
| 71 | // verify that intersecting the vertical span and the quad returns t |
| 72 | // verify that a vertical span starting at quad[0] intersects at t=0 |
| 73 | // verify that a vertical span starting at quad[2] intersects at t=1 |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 74 | static void testQuadLineIntersectMain(PathOpsThreadState* data) |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 75 | { |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 76 | PathOpsThreadState& state = *data; |
| 77 | REPORTER_ASSERT(state.fReporter, data); |
| 78 | int ax = state.fA & 0x03; |
| 79 | int ay = state.fA >> 2; |
| 80 | int bx = state.fB & 0x03; |
| 81 | int by = state.fB >> 2; |
| 82 | int cx = state.fC & 0x03; |
| 83 | int cy = state.fC >> 2; |
caryclark@google.com | db60de7 | 2013-04-11 12:33:23 +0000 | [diff] [blame] | 84 | SkDQuad quad = {{{(double) ax, (double) ay}, {(double) bx, (double) by}, |
| 85 | {(double) cx, (double) cy}}}; |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 86 | SkReduceOrder reducer; |
| 87 | int order = reducer.reduce(quad, SkReduceOrder::kFill_Style); |
| 88 | if (order < 3) { |
| 89 | return; |
| 90 | } |
| 91 | for (int tIndex = 0; tIndex <= 4; ++tIndex) { |
| 92 | SkDPoint xy = quad.xyAtT(tIndex / 4.0); |
| 93 | for (int h = -2; h <= 2; ++h) { |
| 94 | for (int v = -2; v <= 2; ++v) { |
| 95 | if (h == v && abs(h) != 1) { |
| 96 | continue; |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 97 | } |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 98 | double x = xy.fX; |
| 99 | double y = xy.fY; |
| 100 | SkDLine line = {{{x - h, y - v}, {x, y}}}; |
| 101 | testLineIntersect(state.fReporter, quad, line, x, y); |
| 102 | state.fReporter->bumpTestCount(); |
| 103 | SkDLine line2 = {{{x, y}, {x + h, y + v}}}; |
| 104 | testLineIntersect(state.fReporter, quad, line2, x, y); |
| 105 | state.fReporter->bumpTestCount(); |
| 106 | SkDLine line3 = {{{x - h, y - v}, {x + h, y + v}}}; |
| 107 | testLineIntersect(state.fReporter, quad, line3, x, y); |
| 108 | state.fReporter->bumpTestCount(); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 111 | } |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 112 | } |
| 113 | |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 114 | static void PathOpsQuadLineIntersectionThreadedTest(skiatest::Reporter* reporter) |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 115 | { |
caryclark@google.com | 16cfe40 | 2013-04-18 18:47:37 +0000 | [diff] [blame] | 116 | int threadCount = initializeTests(reporter, "testQuadLineIntersect"); |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 117 | PathOpsThreadedTestRunner testRunner(reporter, threadCount); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 118 | for (int a = 0; a < 16; ++a) { |
| 119 | for (int b = 0 ; b < 16; ++b) { |
| 120 | for (int c = 0 ; c < 16; ++c) { |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 121 | *testRunner.fRunnables.append() = SkNEW_ARGS(PathOpsThreadedRunnable, |
| 122 | (&testQuadLineIntersectMain, a, b, c, 0, &testRunner)); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 123 | } |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 124 | if (!reporter->allowExtendedTest()) goto finish; |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 125 | } |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 126 | } |
| 127 | finish: |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 128 | testRunner.render(); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | #include "TestClassDef.h" |
caryclark@google.com | ad65a3e | 2013-04-15 19:13:59 +0000 | [diff] [blame] | 132 | DEFINE_TESTCLASS_SHORT(PathOpsQuadLineIntersectionThreadedTest) |