blob: 1161d2de29ba7c64dcc0dfa77e979afe1c579963 [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;
11} lineQuadTests[] = {
caryclark@google.comfa0588f2012-04-26 21:01:06 +000012 {{{2, 0}, {1, 1}, {2, 2}}, {{0, 0}, {0, 2}}},
13 {{{4, 0}, {0, 1}, {4, 2}}, {{3, 1}, {4, 1}}},
14 {{{0, 0}, {0, 1}, {1, 1}}, {{0, 1}, {1, 0}}},
caryclark@google.com27accef2012-01-25 18:57:23 +000015};
16
17size_t lineQuadTests_count = sizeof(lineQuadTests) / sizeof(lineQuadTests[0]);
18
19const int firstLineQuadIntersectionTest = 0;
20
21void LineQuadraticIntersection_Test() {
22 for (size_t index = firstLineQuadIntersectionTest; index < lineQuadTests_count; ++index) {
23 const Quadratic& quad = lineQuadTests[index].quad;
24 const _Line& line = lineQuadTests[index].line;
25 Quadratic reduce1;
26 _Line reduce2;
27 int order1 = reduceOrder(quad, reduce1);
28 int order2 = reduceOrder(line, reduce2);
29 if (order1 < 3) {
30 printf("[%d] quad order=%d\n", (int) index, order1);
31 }
32 if (order2 < 2) {
33 printf("[%d] line order=%d\n", (int) index, order2);
34 }
35 if (order1 == 3 && order2 == 2) {
36 Intersections intersections;
caryclark@google.comc6825902012-02-03 22:07:47 +000037 intersect(reduce1, reduce2, intersections);
caryclark@google.com27accef2012-01-25 18:57:23 +000038 if (intersections.intersected()) {
39 for (int pt = 0; pt < intersections.used(); ++pt) {
40 double tt1 = intersections.fT[0][pt];
41 double tx1, ty1;
42 xy_at_t(quad, tt1, tx1, ty1);
43 double tt2 = intersections.fT[1][pt];
44 double tx2, ty2;
45 xy_at_t(line, tt2, tx2, ty2);
46 if (!approximately_equal(tx1, tx2)) {
47 printf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
48 __FUNCTION__, (int)index, pt, tt1, tx1, ty1, tt2, tx2, ty2);
49 }
50 if (!approximately_equal(ty1, ty2)) {
51 printf("%s [%d,%d] y!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
52 __FUNCTION__, (int)index, pt, tt1, tx1, ty1, tt2, tx2, ty2);
53 }
54 }
55 }
56 }
57 }
58}
caryclark@google.com24bec792012-08-20 12:43:57 +000059
60static void testLineIntersect(State4& state, const Quadratic& quad, const _Line& line,
61 const double x, const double y) {
62 char pathStr[1024];
63 bzero(pathStr, sizeof(pathStr));
64 char* str = pathStr;
65 str += sprintf(str, " path.moveTo(%1.9g, %1.9g);\n", quad[0].x, quad[0].y);
66 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);
67 str += sprintf(str, " path.moveTo(%1.9g, %1.9g);\n", line[0].x, line[0].y);
68 str += sprintf(str, " path.lineTo(%1.9g, %1.9g);\n", line[1].x, line[1].y);
69
70 Intersections intersections;
71 int result;
72 bool flipped = false;
73 if (line[0].x == line[1].x) {
74 double top = line[0].y;
75 double bottom = line[1].y;
76 bool flipped = top > bottom;
77 if (flipped) {
78 SkTSwap<double>(top, bottom);
79 }
80 result = verticalIntersect(quad, top, bottom, line[0].x, flipped, intersections);
81 } else if (line[0].y == line[1].y) {
82 double left = line[0].x;
83 double right = line[1].x;
84 bool flipped = left > right;
85 if (flipped) {
86 SkTSwap<double>(left, right);
87 }
88 result = horizontalIntersect(quad, left, right, line[0].y, flipped, intersections);
89 } else {
90 intersect(quad, line, intersections);
91 result = intersections.fUsed;
92 }
93 bool found = false;
94 for (int index = 0; index < result; ++index) {
95 double quadT = intersections.fT[0][index];
96 double quadX, quadY;
97 xy_at_t(quad, quadT, quadX, quadY);
98 double lineT = intersections.fT[1][index];
99 if (flipped) {
100 lineT = 1 - lineT;
101 }
102 double lineX, lineY;
103 xy_at_t(line, lineT, lineX, lineY);
104 if (fabs(quadX - lineX) < FLT_EPSILON && fabs(quadY - lineY) < FLT_EPSILON
105 && fabs(x - lineX) < FLT_EPSILON && fabs(y - lineY) < FLT_EPSILON) {
106 found = true;
107 }
108 }
109 SkASSERT(found);
110 state.testsRun++;
111}
112
113
114// find a point on a quad by choosing a t from 0 to 1
115// create a vertical span above and below the point
116// verify that intersecting the vertical span and the quad returns t
117// verify that a vertical span starting at quad[0] intersects at t=0
118// verify that a vertical span starting at quad[2] intersects at t=1
119static void* testQuadLineIntersectMain(void* data)
120{
121 SkASSERT(data);
122 State4& state = *(State4*) data;
123 do {
124 int ax = state.a & 0x03;
125 int ay = state.a >> 2;
126 int bx = state.b & 0x03;
127 int by = state.b >> 2;
128 int cx = state.c & 0x03;
129 int cy = state.c >> 2;
130 Quadratic quad = {{ax, ay}, {bx, by}, {cx, cy}};
131 Quadratic reduced;
132 int order = reduceOrder(quad, reduced);
133 if (order < 3) {
134 continue; // skip degenerates
135 }
136 for (int tIndex = 0; tIndex <= 4; ++tIndex) {
137 double x, y;
138 xy_at_t(quad, tIndex / 4.0, x, y);
139 for (int h = -2; h <= 2; ++h) {
140 for (int v = -2; v <= 2; ++v) {
141 if (h == v && abs(h) != 1) {
142 continue;
143 }
144 _Line line = {{x - h, y - v}, {x, y}};
145 testLineIntersect(state, quad, line, x, y);
146 _Line line2 = {{x, y}, {x + h, y + v}};
147 testLineIntersect(state, quad, line2, x, y);
148 _Line line3 = {{x - h, y - v}, {x + h, y + v}};
149 testLineIntersect(state, quad, line3, x, y);
150 }
151 }
152 }
153 } while (runNextTestSet(state));
154 return NULL;
155}
156
157void QuadLineIntersectThreaded_Test(int& testsRun)
158{
159 SkDebugf("%s\n", __FUNCTION__);
160 const char testStr[] = "testQuadLineIntersect";
161 initializeTests(testStr, sizeof(testStr));
162 int testsStart = testsRun;
163 for (int a = 0; a < 16; ++a) {
164 for (int b = 0 ; b < 16; ++b) {
165 for (int c = 0 ; c < 16; ++c) {
166 testsRun += dispatchTest4(testQuadLineIntersectMain,
167 a, b, c, 0);
168 }
169 if (!gRunTestsInOneThread) SkDebugf(".");
170 }
171 if (!gRunTestsInOneThread) SkDebugf("%d", a);
172 }
173 testsRun += waitForCompletion();
174 SkDebugf("\n%s tests=%d total=%d\n", __FUNCTION__, testsRun - testsStart, testsRun);
175}