blob: 16153404c4de7e1b86ef17a29ba494a4b83ab1aa [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.coma5e55922013-05-07 18:51:31 +000061 {{{{973, 507}, {973, 508.24264526367187}, {972.12158203125, 509.12161254882812}}},
62 {{{930, 467}, {973, 510}}}},
caryclark@google.com9166dcb2013-04-08 11:50:00 +000063 {{{{369.848602, 145.680267}, {382.360413, 121.298294}, {406.207703, 121.298294}}},
64 {{{406.207703, 121.298294}, {348.781738, 123.864815}}}}
65 };
66
caryclark@google.comad65a3e2013-04-15 19:13:59 +000067static size_t oneOffs_count = SK_ARRAY_COUNT(oneOffs);
caryclark@google.com9166dcb2013-04-08 11:50:00 +000068
69static void testOneOffs(skiatest::Reporter* reporter) {
caryclark@google.com9166dcb2013-04-08 11:50:00 +000070 bool flipped = false;
71 for (size_t index = 0; index < oneOffs_count; ++index) {
72 const SkDQuad& quad = oneOffs[index].quad;
73 const SkDLine& line = oneOffs[index].line;
caryclark@google.coma5e55922013-05-07 18:51:31 +000074 SkIntersections intersections;
caryclark@google.com9166dcb2013-04-08 11:50:00 +000075 int result = doIntersect(intersections, quad, line, flipped);
76 for (int inner = 0; inner < result; ++inner) {
77 double quadT = intersections[0][inner];
78 SkDPoint quadXY = quad.xyAtT(quadT);
79 double lineT = intersections[1][inner];
80 SkDPoint lineXY = line.xyAtT(lineT);
81 REPORTER_ASSERT(reporter, quadXY.approximatelyEqual(lineXY));
82 }
83 }
84}
85
caryclark@google.comad65a3e2013-04-15 19:13:59 +000086static void PathOpsQuadLineIntersectionTest(skiatest::Reporter* reporter) {
caryclark@google.com9166dcb2013-04-08 11:50:00 +000087 testOneOffs(reporter);
88 for (size_t index = 0; index < lineQuadTests_count; ++index) {
89 int iIndex = static_cast<int>(index);
90 const SkDQuad& quad = lineQuadTests[index].quad;
91 const SkDLine& line = lineQuadTests[index].line;
92 SkReduceOrder reducer1, reducer2;
93 int order1 = reducer1.reduce(quad, SkReduceOrder::kFill_Style);
94 int order2 = reducer2.reduce(line);
95 if (order1 < 3) {
96 SkDebugf("%s [%d] quad order=%d\n", __FUNCTION__, iIndex, order1);
97 REPORTER_ASSERT(reporter, 0);
98 }
99 if (order2 < 2) {
100 SkDebugf("%s [%d] line order=%d\n", __FUNCTION__, iIndex, order2);
101 REPORTER_ASSERT(reporter, 0);
102 }
103 SkIntersections intersections;
104 bool flipped = false;
105 int result = doIntersect(intersections, quad, line, flipped);
106 REPORTER_ASSERT(reporter, result == lineQuadTests[index].result);
107 if (intersections.used() <= 0) {
108 continue;
109 }
110 for (int pt = 0; pt < result; ++pt) {
111 double tt1 = intersections[0][pt];
112 REPORTER_ASSERT(reporter, tt1 >= 0 && tt1 <= 1);
113 SkDPoint t1 = quad.xyAtT(tt1);
114 double tt2 = intersections[1][pt];
115 REPORTER_ASSERT(reporter, tt2 >= 0 && tt2 <= 1);
116 SkDPoint t2 = line.xyAtT(tt2);
117 if (!t1.approximatelyEqual(t2)) {
118 SkDebugf("%s [%d,%d] x!= t1=%1.9g (%1.9g,%1.9g) t2=%1.9g (%1.9g,%1.9g)\n",
119 __FUNCTION__, iIndex, pt, tt1, t1.fX, t1.fY, tt2, t2.fX, t2.fY);
120 REPORTER_ASSERT(reporter, 0);
121 }
122 if (!t1.approximatelyEqual(lineQuadTests[index].expected[0])
123 && (lineQuadTests[index].result == 1
124 || !t1.approximatelyEqual(lineQuadTests[index].expected[1]))) {
125 SkDebugf("%s t1=(%1.9g,%1.9g)\n", __FUNCTION__, t1.fX, t1.fY);
126 REPORTER_ASSERT(reporter, 0);
127 }
128 }
129 }
130}
131
132#include "TestClassDef.h"
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000133DEFINE_TESTCLASS_SHORT(PathOpsQuadLineIntersectionTest)