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