blob: 4227ee5277a23799174315830117736b8ac7c6d8 [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"
8#include "SkIntersections.h"
9#include "SkPathOpsLine.h"
10#include "SkPathOpsQuad.h"
11#include "SkReduceOrder.h"
12#include "Test.h"
13
14static struct lineQuad {
15 SkDQuad quad;
16 SkDLine line;
17 int result;
18 SkDPoint expected[2];
19} lineQuadTests[] = {
20 // quad line results
21 {{{{1, 1}, {2, 1}, {0, 2}}}, {{{0, 0}, {1, 1}}}, 1, {{1, 1}, {0, 0}} },
22 {{{{0, 0}, {1, 1}, {3, 1}}}, {{{0, 0}, {3, 1}}}, 2, {{0, 0}, {3, 1}} },
23 {{{{2, 0}, {1, 1}, {2, 2}}}, {{{0, 0}, {0, 2}}}, 0, {{0, 0}, {0, 0}} },
24 {{{{4, 0}, {0, 1}, {4, 2}}}, {{{3, 1}, {4, 1}}}, 0, {{0, 0}, {0, 0}} },
25 {{{{0, 0}, {0, 1}, {1, 1}}}, {{{0, 1}, {1, 0}}}, 1, {{.25, .75}, {0, 0}} },
26};
27
caryclark@google.comad65a3e2013-04-15 19:13:59 +000028static size_t lineQuadTests_count = SK_ARRAY_COUNT(lineQuadTests);
caryclark@google.com9166dcb2013-04-08 11:50:00 +000029
30static int doIntersect(SkIntersections& intersections, const SkDQuad& quad, const SkDLine& line,
31 bool& flipped) {
32 int result;
33 flipped = false;
34 if (line[0].fX == line[1].fX) {
35 double top = line[0].fY;
36 double bottom = line[1].fY;
37 flipped = top > bottom;
38 if (flipped) {
39 SkTSwap<double>(top, bottom);
40 }
41 result = intersections.vertical(quad, top, bottom, line[0].fX, flipped);
42 } else if (line[0].fY == line[1].fY) {
43 double left = line[0].fX;
44 double right = line[1].fX;
45 flipped = left > right;
46 if (flipped) {
47 SkTSwap<double>(left, right);
48 }
49 result = intersections.horizontal(quad, left, right, line[0].fY, flipped);
50 } else {
51 intersections.intersect(quad, line);
52 result = intersections.used();
53 }
54 return result;
55}
56
57static struct oneLineQuad {
58 SkDQuad quad;
59 SkDLine line;
60} oneOffs[] = {
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000061 {{{{1101, 10}, {1101, 8.3431453704833984}, {1099.828857421875, 7.1711997985839844}}},
62 {{{1099.828857421875,7.1711711883544922}, {1099.121337890625,7.8786783218383789}}}},
caryclark@google.coma5e55922013-05-07 18:51:31 +000063 {{{{973, 507}, {973, 508.24264526367187}, {972.12158203125, 509.12161254882812}}},
64 {{{930, 467}, {973, 510}}}},
caryclark@google.com9166dcb2013-04-08 11:50:00 +000065 {{{{369.848602, 145.680267}, {382.360413, 121.298294}, {406.207703, 121.298294}}},
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000066 {{{406.207703, 121.298294}, {348.781738, 123.864815}}}},
67};
caryclark@google.com9166dcb2013-04-08 11:50:00 +000068
caryclark@google.comad65a3e2013-04-15 19:13:59 +000069static size_t oneOffs_count = SK_ARRAY_COUNT(oneOffs);
caryclark@google.com9166dcb2013-04-08 11:50:00 +000070
71static void testOneOffs(skiatest::Reporter* reporter) {
caryclark@google.com9166dcb2013-04-08 11:50:00 +000072 bool flipped = false;
73 for (size_t index = 0; index < oneOffs_count; ++index) {
74 const SkDQuad& quad = oneOffs[index].quad;
75 const SkDLine& line = oneOffs[index].line;
caryclark@google.coma5e55922013-05-07 18:51:31 +000076 SkIntersections intersections;
caryclark@google.com9166dcb2013-04-08 11:50:00 +000077 int result = doIntersect(intersections, quad, line, flipped);
78 for (int inner = 0; inner < result; ++inner) {
79 double quadT = intersections[0][inner];
80 SkDPoint quadXY = quad.xyAtT(quadT);
81 double lineT = intersections[1][inner];
82 SkDPoint lineXY = line.xyAtT(lineT);
83 REPORTER_ASSERT(reporter, quadXY.approximatelyEqual(lineXY));
84 }
85 }
86}
87
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000088static void PathOpsQuadLineIntersectionTestOne(skiatest::Reporter* reporter) {
caryclark@google.com9166dcb2013-04-08 11:50:00 +000089 testOneOffs(reporter);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +000090}
91
92static void PathOpsQuadLineIntersectionTest(skiatest::Reporter* reporter) {
caryclark@google.com9166dcb2013-04-08 11:50:00 +000093 for (size_t index = 0; index < lineQuadTests_count; ++index) {
94 int iIndex = static_cast<int>(index);
95 const SkDQuad& quad = lineQuadTests[index].quad;
96 const SkDLine& line = lineQuadTests[index].line;
97 SkReduceOrder reducer1, reducer2;
98 int order1 = reducer1.reduce(quad, SkReduceOrder::kFill_Style);
99 int order2 = reducer2.reduce(line);
100 if (order1 < 3) {
101 SkDebugf("%s [%d] quad order=%d\n", __FUNCTION__, iIndex, order1);
102 REPORTER_ASSERT(reporter, 0);
103 }
104 if (order2 < 2) {
105 SkDebugf("%s [%d] line order=%d\n", __FUNCTION__, iIndex, order2);
106 REPORTER_ASSERT(reporter, 0);
107 }
108 SkIntersections intersections;
109 bool flipped = false;
110 int result = doIntersect(intersections, quad, line, flipped);
111 REPORTER_ASSERT(reporter, result == lineQuadTests[index].result);
112 if (intersections.used() <= 0) {
113 continue;
114 }
115 for (int pt = 0; pt < result; ++pt) {
116 double tt1 = intersections[0][pt];
117 REPORTER_ASSERT(reporter, tt1 >= 0 && tt1 <= 1);
118 SkDPoint t1 = quad.xyAtT(tt1);
119 double tt2 = intersections[1][pt];
120 REPORTER_ASSERT(reporter, tt2 >= 0 && tt2 <= 1);
121 SkDPoint t2 = line.xyAtT(tt2);
122 if (!t1.approximatelyEqual(t2)) {
123 SkDebugf("%s [%d,%d] x!= t1=%1.9g (%1.9g,%1.9g) t2=%1.9g (%1.9g,%1.9g)\n",
124 __FUNCTION__, iIndex, pt, tt1, t1.fX, t1.fY, tt2, t2.fX, t2.fY);
125 REPORTER_ASSERT(reporter, 0);
126 }
127 if (!t1.approximatelyEqual(lineQuadTests[index].expected[0])
128 && (lineQuadTests[index].result == 1
129 || !t1.approximatelyEqual(lineQuadTests[index].expected[1]))) {
130 SkDebugf("%s t1=(%1.9g,%1.9g)\n", __FUNCTION__, t1.fX, t1.fY);
131 REPORTER_ASSERT(reporter, 0);
132 }
133 }
134 }
135}
136
137#include "TestClassDef.h"
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000138DEFINE_TESTCLASS_SHORT(PathOpsQuadLineIntersectionTest)
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000139
140DEFINE_TESTCLASS_SHORT(PathOpsQuadLineIntersectionTestOne)