blob: f24b2e4d7ad3dbaaa3865c1c00559dac672e7648 [file] [log] [blame]
caryclark@google.com9166dcb2013-04-08 11:50:00 +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.com8d0a5242013-07-16 16:11:16 +00008#include "PathOpsTestCommon.h"
caryclark@google.com9166dcb2013-04-08 11:50:00 +00009#include "SkIntersections.h"
10#include "SkPathOpsLine.h"
11#include "SkPathOpsQuad.h"
12#include "SkReduceOrder.h"
13#include "Test.h"
14
15static struct lineQuad {
caryclarka35ab3e2016-10-20 08:32:18 -070016 QuadPts quad;
caryclark@google.com9166dcb2013-04-08 11:50:00 +000017 SkDLine line;
18 int result;
19 SkDPoint expected[2];
20} lineQuadTests[] = {
21 // quad line results
22 {{{{1, 1}, {2, 1}, {0, 2}}}, {{{0, 0}, {1, 1}}}, 1, {{1, 1}, {0, 0}} },
23 {{{{0, 0}, {1, 1}, {3, 1}}}, {{{0, 0}, {3, 1}}}, 2, {{0, 0}, {3, 1}} },
24 {{{{2, 0}, {1, 1}, {2, 2}}}, {{{0, 0}, {0, 2}}}, 0, {{0, 0}, {0, 0}} },
25 {{{{4, 0}, {0, 1}, {4, 2}}}, {{{3, 1}, {4, 1}}}, 0, {{0, 0}, {0, 0}} },
26 {{{{0, 0}, {0, 1}, {1, 1}}}, {{{0, 1}, {1, 0}}}, 1, {{.25, .75}, {0, 0}} },
27};
28
caryclark@google.comad65a3e2013-04-15 19:13:59 +000029static size_t lineQuadTests_count = SK_ARRAY_COUNT(lineQuadTests);
caryclark@google.com9166dcb2013-04-08 11:50:00 +000030
31static int doIntersect(SkIntersections& intersections, const SkDQuad& quad, const SkDLine& line,
32 bool& flipped) {
33 int result;
34 flipped = false;
35 if (line[0].fX == line[1].fX) {
36 double top = line[0].fY;
37 double bottom = line[1].fY;
38 flipped = top > bottom;
39 if (flipped) {
40 SkTSwap<double>(top, bottom);
41 }
42 result = intersections.vertical(quad, top, bottom, line[0].fX, flipped);
43 } else if (line[0].fY == line[1].fY) {
44 double left = line[0].fX;
45 double right = line[1].fX;
46 flipped = left > right;
47 if (flipped) {
48 SkTSwap<double>(left, right);
49 }
50 result = intersections.horizontal(quad, left, right, line[0].fY, flipped);
51 } else {
52 intersections.intersect(quad, line);
53 result = intersections.used();
54 }
55 return result;
56}
57
58static struct oneLineQuad {
caryclarka35ab3e2016-10-20 08:32:18 -070059 QuadPts quad;
caryclark@google.com9166dcb2013-04-08 11:50:00 +000060 SkDLine line;
61} oneOffs[] = {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000062 {{{{97.9337616,100}, {88,112.94265}, {88,130}}},
63 {{{88.919838,120}, {107.058823,120}}}},
skia.committer@gmail.comf54ad6f2013-11-02 07:02:02 +000064 {{{{447.96701049804687, 894.4381103515625}, {448.007080078125, 894.4239501953125},
65 {448.0140380859375, 894.4215087890625}}},
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +000066 {{{490.43548583984375, 879.40740966796875}, {405.59262084960937, 909.435546875}}}},
caryclark@google.com570863f2013-09-16 15:55:01 +000067 {{{{142.589081, 102.283646}, {149.821579, 100}, {158, 100}}},
68 {{{90, 230}, {160, 60}}}},
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000069 {{{{1101, 10}, {1101, 8.3431453704833984}, {1099.828857421875, 7.1711997985839844}}},
70 {{{1099.828857421875,7.1711711883544922}, {1099.121337890625,7.8786783218383789}}}},
caryclark@google.coma5e55922013-05-07 18:51:31 +000071 {{{{973, 507}, {973, 508.24264526367187}, {972.12158203125, 509.12161254882812}}},
72 {{{930, 467}, {973, 510}}}},
caryclark@google.com9166dcb2013-04-08 11:50:00 +000073 {{{{369.848602, 145.680267}, {382.360413, 121.298294}, {406.207703, 121.298294}}},
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000074 {{{406.207703, 121.298294}, {348.781738, 123.864815}}}},
75};
caryclark@google.com9166dcb2013-04-08 11:50:00 +000076
caryclark@google.comad65a3e2013-04-15 19:13:59 +000077static size_t oneOffs_count = SK_ARRAY_COUNT(oneOffs);
caryclark@google.com9166dcb2013-04-08 11:50:00 +000078
79static void testOneOffs(skiatest::Reporter* reporter) {
caryclark@google.com9166dcb2013-04-08 11:50:00 +000080 bool flipped = false;
81 for (size_t index = 0; index < oneOffs_count; ++index) {
caryclarka35ab3e2016-10-20 08:32:18 -070082 const QuadPts& q = oneOffs[index].quad;
83 SkDQuad quad;
84 quad.debugSet(q.fPts);
caryclark@google.com8d0a5242013-07-16 16:11:16 +000085 SkASSERT(ValidQuad(quad));
caryclark@google.com9166dcb2013-04-08 11:50:00 +000086 const SkDLine& line = oneOffs[index].line;
caryclark@google.com8d0a5242013-07-16 16:11:16 +000087 SkASSERT(ValidLine(line));
caryclark@google.coma5e55922013-05-07 18:51:31 +000088 SkIntersections intersections;
caryclark@google.com9166dcb2013-04-08 11:50:00 +000089 int result = doIntersect(intersections, quad, line, flipped);
90 for (int inner = 0; inner < result; ++inner) {
91 double quadT = intersections[0][inner];
caryclark@google.com4fdbb222013-07-23 15:27:41 +000092 SkDPoint quadXY = quad.ptAtT(quadT);
caryclark@google.com9166dcb2013-04-08 11:50:00 +000093 double lineT = intersections[1][inner];
caryclark@google.com4fdbb222013-07-23 15:27:41 +000094 SkDPoint lineXY = line.ptAtT(lineT);
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000095 if (!quadXY.approximatelyEqual(lineXY)) {
96 quadXY.approximatelyEqual(lineXY);
97 SkDebugf("");
98 }
caryclark@google.com9166dcb2013-04-08 11:50:00 +000099 REPORTER_ASSERT(reporter, quadXY.approximatelyEqual(lineXY));
100 }
101 }
102}
103
tfarina@chromium.org78e7b4e2014-01-02 21:45:03 +0000104DEF_TEST(PathOpsQuadLineIntersectionOneOff, reporter) {
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000105 testOneOffs(reporter);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000106}
107
tfarina@chromium.org78e7b4e2014-01-02 21:45:03 +0000108DEF_TEST(PathOpsQuadLineIntersection, reporter) {
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000109 for (size_t index = 0; index < lineQuadTests_count; ++index) {
110 int iIndex = static_cast<int>(index);
caryclarka35ab3e2016-10-20 08:32:18 -0700111 const QuadPts& q = lineQuadTests[index].quad;
112 SkDQuad quad;
113 quad.debugSet(q.fPts);
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000114 SkASSERT(ValidQuad(quad));
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000115 const SkDLine& line = lineQuadTests[index].line;
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000116 SkASSERT(ValidLine(line));
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000117 SkReduceOrder reducer1, reducer2;
caryclark@google.com927b7022013-11-25 14:18:21 +0000118 int order1 = reducer1.reduce(quad);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000119 int order2 = reducer2.reduce(line);
120 if (order1 < 3) {
121 SkDebugf("%s [%d] quad order=%d\n", __FUNCTION__, iIndex, order1);
122 REPORTER_ASSERT(reporter, 0);
123 }
124 if (order2 < 2) {
125 SkDebugf("%s [%d] line order=%d\n", __FUNCTION__, iIndex, order2);
126 REPORTER_ASSERT(reporter, 0);
127 }
128 SkIntersections intersections;
129 bool flipped = false;
130 int result = doIntersect(intersections, quad, line, flipped);
131 REPORTER_ASSERT(reporter, result == lineQuadTests[index].result);
132 if (intersections.used() <= 0) {
133 continue;
134 }
135 for (int pt = 0; pt < result; ++pt) {
136 double tt1 = intersections[0][pt];
137 REPORTER_ASSERT(reporter, tt1 >= 0 && tt1 <= 1);
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000138 SkDPoint t1 = quad.ptAtT(tt1);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000139 double tt2 = intersections[1][pt];
140 REPORTER_ASSERT(reporter, tt2 >= 0 && tt2 <= 1);
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000141 SkDPoint t2 = line.ptAtT(tt2);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000142 if (!t1.approximatelyEqual(t2)) {
143 SkDebugf("%s [%d,%d] x!= t1=%1.9g (%1.9g,%1.9g) t2=%1.9g (%1.9g,%1.9g)\n",
144 __FUNCTION__, iIndex, pt, tt1, t1.fX, t1.fY, tt2, t2.fX, t2.fY);
145 REPORTER_ASSERT(reporter, 0);
146 }
147 if (!t1.approximatelyEqual(lineQuadTests[index].expected[0])
148 && (lineQuadTests[index].result == 1
149 || !t1.approximatelyEqual(lineQuadTests[index].expected[1]))) {
150 SkDebugf("%s t1=(%1.9g,%1.9g)\n", __FUNCTION__, t1.fX, t1.fY);
151 REPORTER_ASSERT(reporter, 0);
152 }
153 }
154 }
155}