blob: 7766cfe0288182098dfd316e69f819f38bd2969e [file] [log] [blame]
caryclark@google.com818b0cc2013-04-08 11:50:46 +00001/*
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.com66089e42013-04-10 15:55:37 +00008#include "PathOpsThreadedCommon.h"
caryclark@google.com818b0cc2013-04-08 11:50:46 +00009#include "SkIntersections.h"
10#include "SkPathOpsLine.h"
11#include "SkPathOpsQuad.h"
12#include "SkReduceOrder.h"
caryclark8f186432016-10-06 11:46:25 -070013#include "SkString.h"
caryclark@google.com818b0cc2013-04-08 11:50:46 +000014
15static int doIntersect(SkIntersections& intersections, const SkDQuad& quad, const SkDLine& line,
16 bool& flipped) {
17 int result;
18 flipped = false;
19 if (line[0].fX == line[1].fX) {
20 double top = line[0].fY;
21 double bottom = line[1].fY;
22 flipped = top > bottom;
23 if (flipped) {
24 SkTSwap<double>(top, bottom);
25 }
26 result = intersections.vertical(quad, top, bottom, line[0].fX, flipped);
27 } else if (line[0].fY == line[1].fY) {
28 double left = line[0].fX;
29 double right = line[1].fX;
30 flipped = left > right;
31 if (flipped) {
32 SkTSwap<double>(left, right);
33 }
34 result = intersections.horizontal(quad, left, right, line[0].fY, flipped);
35 } else {
36 intersections.intersect(quad, line);
37 result = intersections.used();
38 }
39 return result;
40}
41
42static void testLineIntersect(skiatest::Reporter* reporter, const SkDQuad& quad,
43 const SkDLine& line, const double x, const double y) {
caryclark8f186432016-10-06 11:46:25 -070044 SkString pathStr;
45 pathStr.appendf(" path.moveTo(%1.9g, %1.9g);\n", quad[0].fX, quad[0].fY);
46 pathStr.appendf(" path.quadTo(%1.9g, %1.9g, %1.9g, %1.9g);\n", quad[1].fX,
caryclark@google.com66089e42013-04-10 15:55:37 +000047 quad[1].fY, quad[2].fX, quad[2].fY);
caryclark8f186432016-10-06 11:46:25 -070048 pathStr.appendf(" path.moveTo(%1.9g, %1.9g);\n", line[0].fX, line[0].fY);
49 pathStr.appendf(" path.lineTo(%1.9g, %1.9g);\n", line[1].fX, line[1].fY);
caryclark@google.com818b0cc2013-04-08 11:50:46 +000050
51 SkIntersections intersections;
52 bool flipped = false;
53 int result = doIntersect(intersections, quad, line, flipped);
54 bool found = false;
55 for (int index = 0; index < result; ++index) {
56 double quadT = intersections[0][index];
caryclark@google.com4fdbb222013-07-23 15:27:41 +000057 SkDPoint quadXY = quad.ptAtT(quadT);
caryclark@google.com818b0cc2013-04-08 11:50:46 +000058 double lineT = intersections[1][index];
caryclark@google.com4fdbb222013-07-23 15:27:41 +000059 SkDPoint lineXY = line.ptAtT(lineT);
caryclark@google.com818b0cc2013-04-08 11:50:46 +000060 if (quadXY.approximatelyEqual(lineXY)) {
61 found = true;
62 }
63 }
64 REPORTER_ASSERT(reporter, found);
65}
66
caryclark@google.com818b0cc2013-04-08 11:50:46 +000067// find a point on a quad by choosing a t from 0 to 1
68// create a vertical span above and below the point
69// verify that intersecting the vertical span and the quad returns t
70// verify that a vertical span starting at quad[0] intersects at t=0
71// verify that a vertical span starting at quad[2] intersects at t=1
caryclark@google.com66089e42013-04-10 15:55:37 +000072static void testQuadLineIntersectMain(PathOpsThreadState* data)
caryclark@google.com818b0cc2013-04-08 11:50:46 +000073{
caryclark@google.com66089e42013-04-10 15:55:37 +000074 PathOpsThreadState& state = *data;
75 REPORTER_ASSERT(state.fReporter, data);
76 int ax = state.fA & 0x03;
77 int ay = state.fA >> 2;
78 int bx = state.fB & 0x03;
79 int by = state.fB >> 2;
80 int cx = state.fC & 0x03;
81 int cy = state.fC >> 2;
caryclark@google.comdb60de72013-04-11 12:33:23 +000082 SkDQuad quad = {{{(double) ax, (double) ay}, {(double) bx, (double) by},
83 {(double) cx, (double) cy}}};
caryclark@google.com66089e42013-04-10 15:55:37 +000084 SkReduceOrder reducer;
caryclark@google.com927b7022013-11-25 14:18:21 +000085 int order = reducer.reduce(quad);
caryclark@google.com66089e42013-04-10 15:55:37 +000086 if (order < 3) {
87 return;
88 }
89 for (int tIndex = 0; tIndex <= 4; ++tIndex) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +000090 SkDPoint xy = quad.ptAtT(tIndex / 4.0);
caryclark@google.com66089e42013-04-10 15:55:37 +000091 for (int h = -2; h <= 2; ++h) {
92 for (int v = -2; v <= 2; ++v) {
bungeman60e0fee2015-08-26 05:15:46 -070093 if (h == v && SkTAbs(h) != 1) {
caryclark@google.com66089e42013-04-10 15:55:37 +000094 continue;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000095 }
caryclark@google.com66089e42013-04-10 15:55:37 +000096 double x = xy.fX;
97 double y = xy.fY;
98 SkDLine line = {{{x - h, y - v}, {x, y}}};
99 testLineIntersect(state.fReporter, quad, line, x, y);
100 state.fReporter->bumpTestCount();
101 SkDLine line2 = {{{x, y}, {x + h, y + v}}};
102 testLineIntersect(state.fReporter, quad, line2, x, y);
103 state.fReporter->bumpTestCount();
104 SkDLine line3 = {{{x - h, y - v}, {x + h, y + v}}};
105 testLineIntersect(state.fReporter, quad, line3, x, y);
106 state.fReporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000107 }
108 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000109 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000110}
111
tfarina@chromium.org78e7b4e2014-01-02 21:45:03 +0000112DEF_TEST(PathOpsQuadLineIntersectionThreaded, reporter) {
mtklein406654b2014-09-03 15:34:37 -0700113 initializeTests(reporter, "testQuadLineIntersect");
114 PathOpsThreadedTestRunner testRunner(reporter);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000115 for (int a = 0; a < 16; ++a) {
116 for (int b = 0 ; b < 16; ++b) {
117 for (int c = 0 ; c < 16; ++c) {
halcanary385fe4d2015-08-26 13:07:48 -0700118 *testRunner.fRunnables.append() = new PathOpsThreadedRunnable(
119 &testQuadLineIntersectMain, a, b, c, 0, &testRunner);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000120 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000121 if (!reporter->allowExtendedTest()) goto finish;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000122 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000123 }
124finish:
caryclark@google.com66089e42013-04-10 15:55:37 +0000125 testRunner.render();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000126}