blob: bc2a516f3abd6df0880630508c71aade91f78c85 [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 */
Mike Kleinc0bd9f92019-04-23 12:05:21 -05007#include "include/core/SkString.h"
8#include "src/pathops/SkIntersections.h"
9#include "src/pathops/SkPathOpsLine.h"
10#include "src/pathops/SkPathOpsQuad.h"
11#include "src/pathops/SkReduceOrder.h"
12#include "tests/PathOpsExtendedTest.h"
13#include "tests/PathOpsTestCommon.h"
14#include "tests/PathOpsThreadedCommon.h"
caryclark@google.com818b0cc2013-04-08 11:50:46 +000015
Ben Wagnerf08d1d02018-06-18 15:11:00 -040016#include <utility>
17
caryclark@google.com818b0cc2013-04-08 11:50:46 +000018static int doIntersect(SkIntersections& intersections, const SkDQuad& quad, const SkDLine& line,
19 bool& flipped) {
20 int result;
21 flipped = false;
22 if (line[0].fX == line[1].fX) {
23 double top = line[0].fY;
24 double bottom = line[1].fY;
25 flipped = top > bottom;
26 if (flipped) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -040027 using std::swap;
28 swap(top, bottom);
caryclark@google.com818b0cc2013-04-08 11:50:46 +000029 }
30 result = intersections.vertical(quad, top, bottom, line[0].fX, flipped);
31 } else if (line[0].fY == line[1].fY) {
32 double left = line[0].fX;
33 double right = line[1].fX;
34 flipped = left > right;
35 if (flipped) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -040036 using std::swap;
37 swap(left, right);
caryclark@google.com818b0cc2013-04-08 11:50:46 +000038 }
39 result = intersections.horizontal(quad, left, right, line[0].fY, flipped);
40 } else {
41 intersections.intersect(quad, line);
42 result = intersections.used();
43 }
44 return result;
45}
46
47static void testLineIntersect(skiatest::Reporter* reporter, const SkDQuad& quad,
48 const SkDLine& line, const double x, const double y) {
caryclark8f186432016-10-06 11:46:25 -070049 SkString pathStr;
50 pathStr.appendf(" path.moveTo(%1.9g, %1.9g);\n", quad[0].fX, quad[0].fY);
51 pathStr.appendf(" path.quadTo(%1.9g, %1.9g, %1.9g, %1.9g);\n", quad[1].fX,
caryclark@google.com66089e42013-04-10 15:55:37 +000052 quad[1].fY, quad[2].fX, quad[2].fY);
caryclark8f186432016-10-06 11:46:25 -070053 pathStr.appendf(" path.moveTo(%1.9g, %1.9g);\n", line[0].fX, line[0].fY);
54 pathStr.appendf(" path.lineTo(%1.9g, %1.9g);\n", line[1].fX, line[1].fY);
caryclark@google.com818b0cc2013-04-08 11:50:46 +000055
56 SkIntersections intersections;
57 bool flipped = false;
58 int result = doIntersect(intersections, quad, line, flipped);
59 bool found = false;
60 for (int index = 0; index < result; ++index) {
61 double quadT = intersections[0][index];
caryclark@google.com4fdbb222013-07-23 15:27:41 +000062 SkDPoint quadXY = quad.ptAtT(quadT);
caryclark@google.com818b0cc2013-04-08 11:50:46 +000063 double lineT = intersections[1][index];
caryclark@google.com4fdbb222013-07-23 15:27:41 +000064 SkDPoint lineXY = line.ptAtT(lineT);
caryclark@google.com818b0cc2013-04-08 11:50:46 +000065 if (quadXY.approximatelyEqual(lineXY)) {
66 found = true;
67 }
68 }
69 REPORTER_ASSERT(reporter, found);
70}
71
caryclark@google.com818b0cc2013-04-08 11:50:46 +000072// find a point on a quad by choosing a t from 0 to 1
73// create a vertical span above and below the point
74// verify that intersecting the vertical span and the quad returns t
75// verify that a vertical span starting at quad[0] intersects at t=0
76// verify that a vertical span starting at quad[2] intersects at t=1
caryclark@google.com66089e42013-04-10 15:55:37 +000077static void testQuadLineIntersectMain(PathOpsThreadState* data)
caryclark@google.com818b0cc2013-04-08 11:50:46 +000078{
caryclark@google.com66089e42013-04-10 15:55:37 +000079 PathOpsThreadState& state = *data;
80 REPORTER_ASSERT(state.fReporter, data);
81 int ax = state.fA & 0x03;
82 int ay = state.fA >> 2;
83 int bx = state.fB & 0x03;
84 int by = state.fB >> 2;
85 int cx = state.fC & 0x03;
86 int cy = state.fC >> 2;
caryclarka35ab3e2016-10-20 08:32:18 -070087 QuadPts q = {{{(double) ax, (double) ay}, {(double) bx, (double) by},
caryclark@google.comdb60de72013-04-11 12:33:23 +000088 {(double) cx, (double) cy}}};
caryclarka35ab3e2016-10-20 08:32:18 -070089 SkDQuad quad;
90 quad.debugSet(q.fPts);
caryclark@google.com66089e42013-04-10 15:55:37 +000091 SkReduceOrder reducer;
caryclark@google.com927b7022013-11-25 14:18:21 +000092 int order = reducer.reduce(quad);
caryclark@google.com66089e42013-04-10 15:55:37 +000093 if (order < 3) {
94 return;
95 }
96 for (int tIndex = 0; tIndex <= 4; ++tIndex) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +000097 SkDPoint xy = quad.ptAtT(tIndex / 4.0);
caryclark@google.com66089e42013-04-10 15:55:37 +000098 for (int h = -2; h <= 2; ++h) {
99 for (int v = -2; v <= 2; ++v) {
bungeman60e0fee2015-08-26 05:15:46 -0700100 if (h == v && SkTAbs(h) != 1) {
caryclark@google.com66089e42013-04-10 15:55:37 +0000101 continue;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000102 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000103 double x = xy.fX;
104 double y = xy.fY;
105 SkDLine line = {{{x - h, y - v}, {x, y}}};
106 testLineIntersect(state.fReporter, quad, line, x, y);
107 state.fReporter->bumpTestCount();
108 SkDLine line2 = {{{x, y}, {x + h, y + v}}};
109 testLineIntersect(state.fReporter, quad, line2, x, y);
110 state.fReporter->bumpTestCount();
111 SkDLine line3 = {{{x - h, y - v}, {x + h, y + v}}};
112 testLineIntersect(state.fReporter, quad, line3, x, y);
113 state.fReporter->bumpTestCount();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000114 }
115 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000116 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000117}
118
tfarina@chromium.org78e7b4e2014-01-02 21:45:03 +0000119DEF_TEST(PathOpsQuadLineIntersectionThreaded, reporter) {
mtklein406654b2014-09-03 15:34:37 -0700120 initializeTests(reporter, "testQuadLineIntersect");
121 PathOpsThreadedTestRunner testRunner(reporter);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000122 for (int a = 0; a < 16; ++a) {
123 for (int b = 0 ; b < 16; ++b) {
124 for (int c = 0 ; c < 16; ++c) {
halcanary385fe4d2015-08-26 13:07:48 -0700125 *testRunner.fRunnables.append() = new PathOpsThreadedRunnable(
126 &testQuadLineIntersectMain, a, b, c, 0, &testRunner);
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000127 }
caryclark@google.com66089e42013-04-10 15:55:37 +0000128 if (!reporter->allowExtendedTest()) goto finish;
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000129 }
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000130 }
131finish:
caryclark@google.com66089e42013-04-10 15:55:37 +0000132 testRunner.render();
caryclark@google.com818b0cc2013-04-08 11:50:46 +0000133}