blob: e641fdcb207d81d3be361cb10558c37f5421afdc [file] [log] [blame]
caryclark@google.comc6825902012-02-03 22:07:47 +00001#include "CurveIntersection.h"
caryclark@google.com8dcf1142012-07-02 20:27:02 +00002#include "CurveUtilities.h"
caryclark@google.com24bec792012-08-20 12:43:57 +00003#include "EdgeWalker_Test.h"
caryclark@google.com27accef2012-01-25 18:57:23 +00004#include "Intersection_Tests.h"
5#include "Intersections.h"
caryclark@google.com27accef2012-01-25 18:57:23 +00006#include "TestUtilities.h"
7
8struct lineQuad {
9 Quadratic quad;
10 _Line line;
caryclark@google.com03f97062012-08-21 13:13:52 +000011 int result;
12 _Point expected[2];
caryclark@google.com27accef2012-01-25 18:57:23 +000013} lineQuadTests[] = {
caryclark@google.com03f97062012-08-21 13:13:52 +000014 // quad line results
15 {{{1, 1}, {2, 1}, {0, 2}}, {{0, 0}, {1, 1}}, 1, {{1, 1} }},
16 {{{0, 0}, {1, 1}, {3, 1}}, {{0, 0}, {3, 1}}, 2, {{0, 0}, {3, 1}}},
17 {{{2, 0}, {1, 1}, {2, 2}}, {{0, 0}, {0, 2}}, 0 },
18 {{{4, 0}, {0, 1}, {4, 2}}, {{3, 1}, {4, 1}}, 0, },
19 {{{0, 0}, {0, 1}, {1, 1}}, {{0, 1}, {1, 0}}, 1, {{.25, .75} }},
caryclark@google.com27accef2012-01-25 18:57:23 +000020};
21
22size_t lineQuadTests_count = sizeof(lineQuadTests) / sizeof(lineQuadTests[0]);
23
24const int firstLineQuadIntersectionTest = 0;
25
caryclark@google.com03f97062012-08-21 13:13:52 +000026static int doIntersect(Intersections& intersections, const Quadratic& quad, const _Line& line, bool& flipped) {
27 int result;
28 flipped = false;
29 if (line[0].x == line[1].x) {
30 double top = line[0].y;
31 double bottom = line[1].y;
32 flipped = top > bottom;
33 if (flipped) {
34 SkTSwap<double>(top, bottom);
35 }
36 result = verticalIntersect(quad, top, bottom, line[0].x, flipped, intersections);
37 } else if (line[0].y == line[1].y) {
38 double left = line[0].x;
39 double right = line[1].x;
40 flipped = left > right;
41 if (flipped) {
42 SkTSwap<double>(left, right);
43 }
44 result = horizontalIntersect(quad, left, right, line[0].y, flipped, intersections);
45 } else {
46 intersect(quad, line, intersections);
47 result = intersections.fUsed;
48 }
49 return result;
50}
51
caryclark@google.com27accef2012-01-25 18:57:23 +000052void LineQuadraticIntersection_Test() {
53 for (size_t index = firstLineQuadIntersectionTest; index < lineQuadTests_count; ++index) {
54 const Quadratic& quad = lineQuadTests[index].quad;
55 const _Line& line = lineQuadTests[index].line;
56 Quadratic reduce1;
57 _Line reduce2;
58 int order1 = reduceOrder(quad, reduce1);
59 int order2 = reduceOrder(line, reduce2);
60 if (order1 < 3) {
caryclark@google.com03f97062012-08-21 13:13:52 +000061 SkDebugf("%s [%d] quad order=%d\n", __FUNCTION__, (int) index, order1);
62 SkASSERT(0);
caryclark@google.com27accef2012-01-25 18:57:23 +000063 }
64 if (order2 < 2) {
caryclark@google.com03f97062012-08-21 13:13:52 +000065 SkDebugf("%s [%d] line order=%d\n", __FUNCTION__, (int) index, order2);
66 SkASSERT(0);
caryclark@google.com27accef2012-01-25 18:57:23 +000067 }
caryclark@google.com03f97062012-08-21 13:13:52 +000068 Intersections intersections;
69 bool flipped = false;
70 int result = doIntersect(intersections, quad, line, flipped);
71 SkASSERT(result == lineQuadTests[index].result);
72 if (!intersections.intersected()) {
73 continue;
74 }
75 for (int pt = 0; pt < result; ++pt) {
76 double tt1 = intersections.fT[0][pt];
77 SkASSERT(tt1 >= 0 && tt1 <= 1);
78 _Point t1, t2;
79 xy_at_t(quad, tt1, t1.x, t1.y);
80 double tt2 = intersections.fT[1][pt];
81 SkASSERT(tt2 >= 0 && tt2 <= 1);
82 xy_at_t(line, tt2, t2.x, t2.y);
83 if (!approximately_equal(t1.x, t2.x)) {
84 SkDebugf("%s [%d,%d] x!= t1=%1.9g (%1.9g,%1.9g) t2=%1.9g (%1.9g,%1.9g)\n",
85 __FUNCTION__, (int)index, pt, tt1, t1.x, t1.y, tt2, t2.x, t2.y);
86 SkASSERT(0);
87 }
88 if (!approximately_equal(t1.y, t2.y)) {
89 SkDebugf("%s [%d,%d] y!= t1=%1.9g (%1.9g,%1.9g) t2=%1.9g (%1.9g,%1.9g)\n",
90 __FUNCTION__, (int)index, pt, tt1, t1.x, t1.y, tt2, t2.x, t2.y);
91 SkASSERT(0);
92 }
93 if (!t1.approximatelyEqual(lineQuadTests[index].expected[0])
94 && (lineQuadTests[index].result == 1
95 || !t1.approximatelyEqual(lineQuadTests[index].expected[1]))) {
96 SkDebugf("%s t1=(%1.9g,%1.9g)\n", __FUNCTION__, t1.x, t1.y);
97 SkASSERT(0);
caryclark@google.com27accef2012-01-25 18:57:23 +000098 }
99 }
100 }
101}
caryclark@google.com24bec792012-08-20 12:43:57 +0000102
103static void testLineIntersect(State4& state, const Quadratic& quad, const _Line& line,
104 const double x, const double y) {
105 char pathStr[1024];
106 bzero(pathStr, sizeof(pathStr));
107 char* str = pathStr;
108 str += sprintf(str, " path.moveTo(%1.9g, %1.9g);\n", quad[0].x, quad[0].y);
109 str += sprintf(str, " path.quadTo(%1.9g, %1.9g, %1.9g, %1.9g);\n", quad[1].x, quad[1].y, quad[2].x, quad[2].y);
110 str += sprintf(str, " path.moveTo(%1.9g, %1.9g);\n", line[0].x, line[0].y);
111 str += sprintf(str, " path.lineTo(%1.9g, %1.9g);\n", line[1].x, line[1].y);
112
113 Intersections intersections;
caryclark@google.com24bec792012-08-20 12:43:57 +0000114 bool flipped = false;
caryclark@google.com03f97062012-08-21 13:13:52 +0000115 int result = doIntersect(intersections, quad, line, flipped);
caryclark@google.com24bec792012-08-20 12:43:57 +0000116 bool found = false;
117 for (int index = 0; index < result; ++index) {
118 double quadT = intersections.fT[0][index];
119 double quadX, quadY;
120 xy_at_t(quad, quadT, quadX, quadY);
121 double lineT = intersections.fT[1][index];
caryclark@google.com24bec792012-08-20 12:43:57 +0000122 double lineX, lineY;
123 xy_at_t(line, lineT, lineX, lineY);
124 if (fabs(quadX - lineX) < FLT_EPSILON && fabs(quadY - lineY) < FLT_EPSILON
125 && fabs(x - lineX) < FLT_EPSILON && fabs(y - lineY) < FLT_EPSILON) {
126 found = true;
127 }
128 }
129 SkASSERT(found);
130 state.testsRun++;
131}
132
133
134// find a point on a quad by choosing a t from 0 to 1
135// create a vertical span above and below the point
136// verify that intersecting the vertical span and the quad returns t
137// verify that a vertical span starting at quad[0] intersects at t=0
138// verify that a vertical span starting at quad[2] intersects at t=1
139static void* testQuadLineIntersectMain(void* data)
140{
141 SkASSERT(data);
142 State4& state = *(State4*) data;
143 do {
144 int ax = state.a & 0x03;
145 int ay = state.a >> 2;
146 int bx = state.b & 0x03;
147 int by = state.b >> 2;
148 int cx = state.c & 0x03;
149 int cy = state.c >> 2;
150 Quadratic quad = {{ax, ay}, {bx, by}, {cx, cy}};
151 Quadratic reduced;
152 int order = reduceOrder(quad, reduced);
153 if (order < 3) {
154 continue; // skip degenerates
155 }
156 for (int tIndex = 0; tIndex <= 4; ++tIndex) {
157 double x, y;
158 xy_at_t(quad, tIndex / 4.0, x, y);
159 for (int h = -2; h <= 2; ++h) {
160 for (int v = -2; v <= 2; ++v) {
161 if (h == v && abs(h) != 1) {
162 continue;
163 }
164 _Line line = {{x - h, y - v}, {x, y}};
165 testLineIntersect(state, quad, line, x, y);
166 _Line line2 = {{x, y}, {x + h, y + v}};
167 testLineIntersect(state, quad, line2, x, y);
168 _Line line3 = {{x - h, y - v}, {x + h, y + v}};
169 testLineIntersect(state, quad, line3, x, y);
170 }
171 }
172 }
173 } while (runNextTestSet(state));
174 return NULL;
175}
176
177void QuadLineIntersectThreaded_Test(int& testsRun)
178{
179 SkDebugf("%s\n", __FUNCTION__);
180 const char testStr[] = "testQuadLineIntersect";
181 initializeTests(testStr, sizeof(testStr));
182 int testsStart = testsRun;
183 for (int a = 0; a < 16; ++a) {
184 for (int b = 0 ; b < 16; ++b) {
185 for (int c = 0 ; c < 16; ++c) {
186 testsRun += dispatchTest4(testQuadLineIntersectMain,
187 a, b, c, 0);
188 }
189 if (!gRunTestsInOneThread) SkDebugf(".");
190 }
191 if (!gRunTestsInOneThread) SkDebugf("%d", a);
192 }
193 testsRun += waitForCompletion();
194 SkDebugf("\n%s tests=%d total=%d\n", __FUNCTION__, testsRun - testsStart, testsRun);
195}