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 | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 8 | #include "PathOpsTestCommon.h" |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 9 | #include "PathOpsThreadedCommon.h" |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 10 | #include "SkIntersections.h" |
| 11 | #include "SkPathOpsLine.h" |
| 12 | #include "SkPathOpsQuad.h" |
| 13 | #include "SkReduceOrder.h" |
caryclark | 8f18643 | 2016-10-06 11:46:25 -0700 | [diff] [blame] | 14 | #include "SkString.h" |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 15 | |
| 16 | static int doIntersect(SkIntersections& intersections, const SkDQuad& quad, const SkDLine& line, |
| 17 | bool& flipped) { |
| 18 | int result; |
| 19 | flipped = false; |
| 20 | if (line[0].fX == line[1].fX) { |
| 21 | double top = line[0].fY; |
| 22 | double bottom = line[1].fY; |
| 23 | flipped = top > bottom; |
| 24 | if (flipped) { |
| 25 | SkTSwap<double>(top, bottom); |
| 26 | } |
| 27 | result = intersections.vertical(quad, top, bottom, line[0].fX, flipped); |
| 28 | } else if (line[0].fY == line[1].fY) { |
| 29 | double left = line[0].fX; |
| 30 | double right = line[1].fX; |
| 31 | flipped = left > right; |
| 32 | if (flipped) { |
| 33 | SkTSwap<double>(left, right); |
| 34 | } |
| 35 | result = intersections.horizontal(quad, left, right, line[0].fY, flipped); |
| 36 | } else { |
| 37 | intersections.intersect(quad, line); |
| 38 | result = intersections.used(); |
| 39 | } |
| 40 | return result; |
| 41 | } |
| 42 | |
| 43 | static void testLineIntersect(skiatest::Reporter* reporter, const SkDQuad& quad, |
| 44 | const SkDLine& line, const double x, const double y) { |
caryclark | 8f18643 | 2016-10-06 11:46:25 -0700 | [diff] [blame] | 45 | SkString pathStr; |
| 46 | pathStr.appendf(" path.moveTo(%1.9g, %1.9g);\n", quad[0].fX, quad[0].fY); |
| 47 | pathStr.appendf(" path.quadTo(%1.9g, %1.9g, %1.9g, %1.9g);\n", quad[1].fX, |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 48 | quad[1].fY, quad[2].fX, quad[2].fY); |
caryclark | 8f18643 | 2016-10-06 11:46:25 -0700 | [diff] [blame] | 49 | pathStr.appendf(" path.moveTo(%1.9g, %1.9g);\n", line[0].fX, line[0].fY); |
| 50 | pathStr.appendf(" path.lineTo(%1.9g, %1.9g);\n", line[1].fX, line[1].fY); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 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]; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 58 | SkDPoint quadXY = quad.ptAtT(quadT); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 59 | double lineT = intersections[1][index]; |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 60 | SkDPoint lineXY = line.ptAtT(lineT); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 61 | if (quadXY.approximatelyEqual(lineXY)) { |
| 62 | found = true; |
| 63 | } |
| 64 | } |
| 65 | REPORTER_ASSERT(reporter, found); |
| 66 | } |
| 67 | |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 68 | // find a point on a quad by choosing a t from 0 to 1 |
| 69 | // create a vertical span above and below the point |
| 70 | // verify that intersecting the vertical span and the quad returns t |
| 71 | // verify that a vertical span starting at quad[0] intersects at t=0 |
| 72 | // 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] | 73 | static void testQuadLineIntersectMain(PathOpsThreadState* data) |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 74 | { |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 75 | PathOpsThreadState& state = *data; |
| 76 | REPORTER_ASSERT(state.fReporter, data); |
| 77 | int ax = state.fA & 0x03; |
| 78 | int ay = state.fA >> 2; |
| 79 | int bx = state.fB & 0x03; |
| 80 | int by = state.fB >> 2; |
| 81 | int cx = state.fC & 0x03; |
| 82 | int cy = state.fC >> 2; |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 83 | QuadPts q = {{{(double) ax, (double) ay}, {(double) bx, (double) by}, |
caryclark@google.com | db60de7 | 2013-04-11 12:33:23 +0000 | [diff] [blame] | 84 | {(double) cx, (double) cy}}}; |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 85 | SkDQuad quad; |
| 86 | quad.debugSet(q.fPts); |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 87 | SkReduceOrder reducer; |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 88 | int order = reducer.reduce(quad); |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 89 | if (order < 3) { |
| 90 | return; |
| 91 | } |
| 92 | for (int tIndex = 0; tIndex <= 4; ++tIndex) { |
caryclark@google.com | 4fdbb22 | 2013-07-23 15:27:41 +0000 | [diff] [blame] | 93 | SkDPoint xy = quad.ptAtT(tIndex / 4.0); |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 94 | for (int h = -2; h <= 2; ++h) { |
| 95 | for (int v = -2; v <= 2; ++v) { |
bungeman | 60e0fee | 2015-08-26 05:15:46 -0700 | [diff] [blame] | 96 | if (h == v && SkTAbs(h) != 1) { |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 97 | continue; |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 98 | } |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 99 | double x = xy.fX; |
| 100 | double y = xy.fY; |
| 101 | SkDLine line = {{{x - h, y - v}, {x, y}}}; |
| 102 | testLineIntersect(state.fReporter, quad, line, x, y); |
| 103 | state.fReporter->bumpTestCount(); |
| 104 | SkDLine line2 = {{{x, y}, {x + h, y + v}}}; |
| 105 | testLineIntersect(state.fReporter, quad, line2, x, y); |
| 106 | state.fReporter->bumpTestCount(); |
| 107 | SkDLine line3 = {{{x - h, y - v}, {x + h, y + v}}}; |
| 108 | testLineIntersect(state.fReporter, quad, line3, x, y); |
| 109 | state.fReporter->bumpTestCount(); |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 110 | } |
| 111 | } |
caryclark@google.com | 66089e4 | 2013-04-10 15:55:37 +0000 | [diff] [blame] | 112 | } |
caryclark@google.com | 818b0cc | 2013-04-08 11:50:46 +0000 | [diff] [blame] | 113 | } |
| 114 | |
tfarina@chromium.org | 78e7b4e | 2014-01-02 21:45:03 +0000 | [diff] [blame] | 115 | DEF_TEST(PathOpsQuadLineIntersectionThreaded, reporter) { |
mtklein | 406654b | 2014-09-03 15:34:37 -0700 | [diff] [blame] | 116 | initializeTests(reporter, "testQuadLineIntersect"); |
| 117 | PathOpsThreadedTestRunner testRunner(reporter); |
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) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 121 | *testRunner.fRunnables.append() = new 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 | } |